blob: 274c487ab383e315304a0d51f1674a40614f5beb [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
John Zulauf43cc7462020-12-03 12:33:12 -070026const static std::array<AccessAddressType, static_cast<size_t>(AccessAddressType::kTypeCount)> kAddressTypes = {
27 AccessAddressType::kLinear, AccessAddressType::kIdealized};
28
John Zulauf9cb530d2019-09-30 14:14:10 -060029static const char *string_SyncHazardVUID(SyncHazard hazard) {
30 switch (hazard) {
31 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070032 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060033 break;
34 case SyncHazard::READ_AFTER_WRITE:
35 return "SYNC-HAZARD-READ_AFTER_WRITE";
36 break;
37 case SyncHazard::WRITE_AFTER_READ:
38 return "SYNC-HAZARD-WRITE_AFTER_READ";
39 break;
40 case SyncHazard::WRITE_AFTER_WRITE:
41 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
42 break;
John Zulauf2f952d22020-02-10 11:34:51 -070043 case SyncHazard::READ_RACING_WRITE:
44 return "SYNC-HAZARD-READ-RACING-WRITE";
45 break;
46 case SyncHazard::WRITE_RACING_WRITE:
47 return "SYNC-HAZARD-WRITE-RACING-WRITE";
48 break;
49 case SyncHazard::WRITE_RACING_READ:
50 return "SYNC-HAZARD-WRITE-RACING-READ";
51 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060052 default:
53 assert(0);
54 }
55 return "SYNC-HAZARD-INVALID";
56}
57
John Zulauf59e25072020-07-17 10:55:21 -060058static bool IsHazardVsRead(SyncHazard hazard) {
59 switch (hazard) {
60 case SyncHazard::NONE:
61 return false;
62 break;
63 case SyncHazard::READ_AFTER_WRITE:
64 return false;
65 break;
66 case SyncHazard::WRITE_AFTER_READ:
67 return true;
68 break;
69 case SyncHazard::WRITE_AFTER_WRITE:
70 return false;
71 break;
72 case SyncHazard::READ_RACING_WRITE:
73 return false;
74 break;
75 case SyncHazard::WRITE_RACING_WRITE:
76 return false;
77 break;
78 case SyncHazard::WRITE_RACING_READ:
79 return true;
80 break;
81 default:
82 assert(0);
83 }
84 return false;
85}
86
John Zulauf9cb530d2019-09-30 14:14:10 -060087static const char *string_SyncHazard(SyncHazard hazard) {
88 switch (hazard) {
89 case SyncHazard::NONE:
90 return "NONR";
91 break;
92 case SyncHazard::READ_AFTER_WRITE:
93 return "READ_AFTER_WRITE";
94 break;
95 case SyncHazard::WRITE_AFTER_READ:
96 return "WRITE_AFTER_READ";
97 break;
98 case SyncHazard::WRITE_AFTER_WRITE:
99 return "WRITE_AFTER_WRITE";
100 break;
John Zulauf2f952d22020-02-10 11:34:51 -0700101 case SyncHazard::READ_RACING_WRITE:
102 return "READ_RACING_WRITE";
103 break;
104 case SyncHazard::WRITE_RACING_WRITE:
105 return "WRITE_RACING_WRITE";
106 break;
107 case SyncHazard::WRITE_RACING_READ:
108 return "WRITE_RACING_READ";
109 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600110 default:
111 assert(0);
112 }
113 return "INVALID HAZARD";
114}
115
John Zulauf37ceaed2020-07-03 16:18:15 -0600116static const SyncStageAccessInfoType *SyncStageAccessInfoFromMask(SyncStageAccessFlags flags) {
117 // Return the info for the first bit found
118 const SyncStageAccessInfoType *info = nullptr;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700119 for (size_t i = 0; i < flags.size(); i++) {
120 if (flags.test(i)) {
121 info = &syncStageAccessInfoByStageAccessIndex[i];
122 break;
John Zulauf37ceaed2020-07-03 16:18:15 -0600123 }
124 }
125 return info;
126}
127
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700128static std::string string_SyncStageAccessFlags(const SyncStageAccessFlags &flags, const char *sep = "|") {
John Zulauf59e25072020-07-17 10:55:21 -0600129 std::string out_str;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700130 if (flags.none()) {
John Zulauf389c34b2020-07-28 11:19:35 -0600131 out_str = "0";
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700132 } else {
133 for (size_t i = 0; i < syncStageAccessInfoByStageAccessIndex.size(); i++) {
134 const auto &info = syncStageAccessInfoByStageAccessIndex[i];
135 if ((flags & info.stage_access_bit).any()) {
136 if (!out_str.empty()) {
137 out_str.append(sep);
138 }
139 out_str.append(info.name);
John Zulauf59e25072020-07-17 10:55:21 -0600140 }
John Zulauf59e25072020-07-17 10:55:21 -0600141 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700142 if (out_str.length() == 0) {
143 out_str.append("Unhandled SyncStageAccess");
144 }
John Zulauf59e25072020-07-17 10:55:21 -0600145 }
146 return out_str;
147}
148
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700149static std::string string_UsageTag(const ResourceUsageTag &tag) {
150 std::stringstream out;
151
152 out << "command: " << CommandTypeString(tag.command);
153 out << ", seq_no: " << ((tag.index >> 1) & UINT32_MAX) << ", reset_no: " << (tag.index >> 33);
154 if (tag.index & 1) {
155 out << ", subcmd: " << (tag.index & 1);
156 }
157 return out.str();
158}
159
John Zulauf37ceaed2020-07-03 16:18:15 -0600160static std::string string_UsageTag(const HazardResult &hazard) {
161 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600162 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
163 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600164 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600165 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
166 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf59e25072020-07-17 10:55:21 -0600167 out << "(usage: " << usage_info.name << ", prior_usage: " << stage_access_name;
168 if (IsHazardVsRead(hazard.hazard)) {
169 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
170 out << ", read_barriers: " << string_VkPipelineStageFlags(barriers);
171 } else {
172 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
173 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
174 }
175
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700176 out << ", " << string_UsageTag(tag) << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600177 return out.str();
178}
179
John Zulaufd14743a2020-07-03 09:42:39 -0600180// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
181// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
182// also reflects this special case for read hazard detection (using access instead of exec scope)
John Zulaufb027cdb2020-05-21 14:25:22 -0600183static constexpr VkPipelineStageFlags kColorAttachmentExecScope = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700184static const SyncStageAccessFlags kColorAttachmentAccessScope =
185 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
186 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
187 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
188 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600189static constexpr VkPipelineStageFlags kDepthStencilAttachmentExecScope =
190 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700191static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
192 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
193 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
194 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600195
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700196static const SyncOrderingBarrier kColorAttachmentRasterOrder = {kColorAttachmentExecScope, kColorAttachmentAccessScope};
197static const SyncOrderingBarrier kDepthStencilAttachmentRasterOrder = {kDepthStencilAttachmentExecScope,
198 kDepthStencilAttachmentAccessScope};
199static const SyncOrderingBarrier kAttachmentRasterOrder = {kDepthStencilAttachmentExecScope | kColorAttachmentExecScope,
200 kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope};
John Zulauf7635de32020-05-29 17:14:15 -0600201// 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 -0600202static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600203
John Zulaufb02c1eb2020-10-06 16:33:36 -0600204static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) {
205 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
206}
207
208static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
209
locke-lunarg3c038002020-04-30 23:08:08 -0600210inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
211 if (size == VK_WHOLE_SIZE) {
212 return (whole_size - offset);
213 }
214 return size;
215}
216
John Zulauf3e86bf02020-09-12 10:47:57 -0600217static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
218 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
219}
220
John Zulauf16adfc92020-04-08 10:28:33 -0600221template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600222static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600223 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
224}
225
John Zulauf355e49b2020-04-24 15:11:15 -0600226static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600227
John Zulauf3e86bf02020-09-12 10:47:57 -0600228static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
229 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
230}
231
232static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
233 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
234}
235
John Zulauf0cb5be22020-01-23 12:18:22 -0700236// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
237VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
238 VkPipelineStageFlags expanded = stage_mask;
239 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
240 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
241 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
242 if (all_commands.first & queue_flags) {
243 expanded |= all_commands.second;
244 }
245 }
246 }
247 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
248 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
249 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
250 }
251 return expanded;
252}
253
John Zulauf36bcf6a2020-02-03 15:12:52 -0700254VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
Jeremy Gebben91c36902020-11-09 08:17:08 -0700255 const std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
John Zulauf36bcf6a2020-02-03 15:12:52 -0700256 VkPipelineStageFlags unscanned = stage_mask;
257 VkPipelineStageFlags related = 0;
Jonah Ryan-Davis185189c2020-07-14 10:28:52 -0400258 for (const auto &entry : map) {
259 const auto &stage = entry.first;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700260 if (stage & unscanned) {
261 related = related | entry.second;
262 unscanned = unscanned & ~stage;
263 if (!unscanned) break;
264 }
265 }
266 return related;
267}
268
269VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
270 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
271}
272
273VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
274 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
275}
276
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700277static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700278
John Zulauf3e86bf02020-09-12 10:47:57 -0600279ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
280 VkDeviceSize stride) {
281 VkDeviceSize range_start = offset + first_index * stride;
282 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600283 if (count == UINT32_MAX) {
284 range_size = buf_whole_size - range_start;
285 } else {
286 range_size = count * stride;
287 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600288 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600289}
290
locke-lunarg654e3692020-06-04 17:19:15 -0600291SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
292 VkShaderStageFlagBits stage_flag) {
293 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
294 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
295 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
296 }
297 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
298 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
299 assert(0);
300 }
301 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
302 return stage_access->second.uniform_read;
303 }
304
305 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
306 // Because if write hazard happens, read hazard might or might not happen.
307 // But if write hazard doesn't happen, read hazard is impossible to happen.
308 if (descriptor_data.is_writable) {
309 return stage_access->second.shader_write;
310 }
311 return stage_access->second.shader_read;
312}
313
locke-lunarg37047832020-06-12 13:44:45 -0600314bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
315 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
316 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
317 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
318 ? true
319 : false;
320}
321
322bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
323 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
324 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
325 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
326 ? true
327 : false;
328}
329
John Zulauf355e49b2020-04-24 15:11:15 -0600330// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600331template <typename Action>
332static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
333 Action &action) {
334 // At this point the "apply over range" logic only supports a single memory binding
335 if (!SimpleBinding(image_state)) return;
336 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600337 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700338 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
339 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600340 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700341 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600342 }
343}
344
John Zulauf7635de32020-05-29 17:14:15 -0600345// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
346// Used by both validation and record operations
347//
348// The signature for Action() reflect the needs of both uses.
349template <typename Action>
350void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
351 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
352 VkExtent3D extent = CastTo3D(render_area.extent);
353 VkOffset3D offset = CastTo3D(render_area.offset);
354 const auto &rp_ci = rp_state.createInfo;
355 const auto *attachment_ci = rp_ci.pAttachments;
356 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
357
358 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
359 const auto *color_attachments = subpass_ci.pColorAttachments;
360 const auto *color_resolve = subpass_ci.pResolveAttachments;
361 if (color_resolve && color_attachments) {
362 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
363 const auto &color_attach = color_attachments[i].attachment;
364 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
365 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
366 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
367 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
368 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
369 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
370 }
371 }
372 }
373
374 // Depth stencil resolve only if the extension is present
375 const auto ds_resolve = lvl_find_in_chain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
376 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
377 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
378 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
379 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
380 const auto src_ci = attachment_ci[src_at];
381 // The formats are required to match so we can pick either
382 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
383 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
384 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
385 VkImageAspectFlags aspect_mask = 0u;
386
387 // Figure out which aspects are actually touched during resolve operations
388 const char *aspect_string = nullptr;
389 if (resolve_depth && resolve_stencil) {
390 // Validate all aspects together
391 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
392 aspect_string = "depth/stencil";
393 } else if (resolve_depth) {
394 // Validate depth only
395 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
396 aspect_string = "depth";
397 } else if (resolve_stencil) {
398 // Validate all stencil only
399 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
400 aspect_string = "stencil";
401 }
402
403 if (aspect_mask) {
404 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
Jeremy Gebbenec5cd382020-11-16 15:53:45 -0700405 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kAttachmentRasterOrder, offset, extent,
John Zulauf7635de32020-05-29 17:14:15 -0600406 aspect_mask);
407 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
408 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
409 }
410 }
411}
412
413// Action for validating resolve operations
414class ValidateResolveAction {
415 public:
416 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
417 const char *func_name)
418 : render_pass_(render_pass),
419 subpass_(subpass),
420 context_(context),
421 sync_state_(sync_state),
422 func_name_(func_name),
423 skip_(false) {}
424 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
425 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
426 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
427 HazardResult hazard;
428 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
429 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600430 skip_ |= sync_state_.LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
431 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600432 " to resolve attachment %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -0600433 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name,
John Zulauf37ceaed2020-07-03 16:18:15 -0600434 src_at, dst_at, string_UsageTag(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600435 }
436 }
437 // Providing a mechanism for the constructing caller to get the result of the validation
438 bool GetSkip() const { return skip_; }
439
440 private:
441 VkRenderPass render_pass_;
442 const uint32_t subpass_;
443 const AccessContext &context_;
444 const SyncValidator &sync_state_;
445 const char *func_name_;
446 bool skip_;
447};
448
449// Update action for resolve operations
450class UpdateStateResolveAction {
451 public:
452 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
453 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
454 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
455 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
456 // Ignores validation only arguments...
457 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
458 }
459
460 private:
461 AccessContext &context_;
462 const ResourceUsageTag &tag_;
463};
464
John Zulauf59e25072020-07-17 10:55:21 -0600465void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700466 const SyncStageAccessFlags &prior_, const ResourceUsageTag &tag_) {
John Zulauf59e25072020-07-17 10:55:21 -0600467 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
468 usage_index = usage_index_;
469 hazard = hazard_;
470 prior_access = prior_;
471 tag = tag_;
472}
473
John Zulauf540266b2020-04-06 18:54:53 -0600474AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
475 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600476 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600477 Reset();
478 const auto &subpass_dep = dependencies[subpass];
479 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600480 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600481 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600482 const auto prev_pass = prev_dep.first->pass;
483 const auto &prev_barriers = prev_dep.second;
484 assert(prev_dep.second.size());
485 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
486 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700487 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600488
489 async_.reserve(subpass_dep.async.size());
490 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700491 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600492 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600493 if (subpass_dep.barrier_from_external.size()) {
494 src_external_ = TrackBack(external_context, queue_flags, subpass_dep.barrier_from_external);
John Zulaufe5da6e52020-03-18 15:32:18 -0600495 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600496 if (subpass_dep.barrier_to_external.size()) {
497 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600498 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700499}
500
John Zulauf5f13a792020-03-10 07:31:21 -0600501template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700502HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600503 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600504 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600505 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600506
507 HazardResult hazard;
508 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
509 hazard = detector.Detect(prev);
510 }
511 return hazard;
512}
513
John Zulauf3d84f1b2020-03-09 13:33:25 -0600514// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
515// the DAG of the contexts (for example subpasses)
516template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700517HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600518 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600519 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600520
John Zulauf1a224292020-06-30 14:52:13 -0600521 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600522 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
523 // so we'll check these first
524 for (const auto &async_context : async_) {
525 hazard = async_context->DetectAsyncHazard(type, detector, range);
526 if (hazard.hazard) return hazard;
527 }
John Zulauf5f13a792020-03-10 07:31:21 -0600528 }
529
John Zulauf1a224292020-06-30 14:52:13 -0600530 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600531
John Zulauf69133422020-05-20 14:55:53 -0600532 const auto &accesses = GetAccessStateMap(type);
533 const auto from = accesses.lower_bound(range);
534 const auto to = accesses.upper_bound(range);
535 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600536
John Zulauf69133422020-05-20 14:55:53 -0600537 for (auto pos = from; pos != to; ++pos) {
538 // Cover any leading gap, or gap between entries
539 if (detect_prev) {
540 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
541 // Cover any leading gap, or gap between entries
542 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600543 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600544 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600545 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600546 if (hazard.hazard) return hazard;
547 }
John Zulauf69133422020-05-20 14:55:53 -0600548 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
549 gap.begin = pos->first.end;
550 }
551
552 hazard = detector.Detect(pos);
553 if (hazard.hazard) return hazard;
554 }
555
556 if (detect_prev) {
557 // Detect in the trailing empty as needed
558 gap.end = range.end;
559 if (gap.non_empty()) {
560 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600561 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600562 }
563
564 return hazard;
565}
566
567// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
568template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700569HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
570 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600571 auto &accesses = GetAccessStateMap(type);
572 const auto from = accesses.lower_bound(range);
573 const auto to = accesses.upper_bound(range);
574
John Zulauf3d84f1b2020-03-09 13:33:25 -0600575 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600576 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700577 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600578 }
John Zulauf16adfc92020-04-08 10:28:33 -0600579
John Zulauf3d84f1b2020-03-09 13:33:25 -0600580 return hazard;
581}
582
John Zulaufb02c1eb2020-10-06 16:33:36 -0600583struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700584 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600585 void operator()(ResourceAccessState *access) const {
586 assert(access);
587 access->ApplyBarriers(barriers, true);
588 }
589 const std::vector<SyncBarrier> &barriers;
590};
591
592struct ApplyTrackbackBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700593 explicit ApplyTrackbackBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600594 void operator()(ResourceAccessState *access) const {
595 assert(access);
596 assert(!access->HasPendingState());
597 access->ApplyBarriers(barriers, false);
598 access->ApplyPendingBarriers(kCurrentCommandTag);
599 }
600 const std::vector<SyncBarrier> &barriers;
601};
602
603// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
604// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
605// *different* map from dest.
606// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
607// range [first, last)
608template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600609static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
610 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600611 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600612 auto at = entry;
613 for (auto pos = first; pos != last; ++pos) {
614 // Every member of the input iterator range must fit within the remaining portion of entry
615 assert(at->first.includes(pos->first));
616 assert(at != dest->end());
617 // Trim up at to the same size as the entry to resolve
618 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600619 auto access = pos->second; // intentional copy
620 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600621 at->second.Resolve(access);
622 ++at; // Go to the remaining unused section of entry
623 }
624}
625
John Zulaufa0a98292020-09-18 09:30:10 -0600626static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
627 SyncBarrier merged = {};
628 for (const auto &barrier : barriers) {
629 merged.Merge(barrier);
630 }
631 return merged;
632}
633
John Zulaufb02c1eb2020-10-06 16:33:36 -0600634template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700635void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600636 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
637 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600638 if (!range.non_empty()) return;
639
John Zulauf355e49b2020-04-24 15:11:15 -0600640 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
641 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600642 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600643 if (current->pos_B->valid) {
644 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600645 auto access = src_pos->second; // intentional copy
646 barrier_action(&access);
647
John Zulauf16adfc92020-04-08 10:28:33 -0600648 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600649 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
650 trimmed->second.Resolve(access);
651 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600652 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600653 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600654 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600655 }
John Zulauf16adfc92020-04-08 10:28:33 -0600656 } else {
657 // we have to descend to fill this gap
658 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600659 if (current->pos_A->valid) {
660 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
661 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600662 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600663 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -0600664 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600665 // There isn't anything in dest in current)range, so we can accumulate directly into it.
666 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600667 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
668 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
669 barrier_action(&pos->second);
John Zulauf355e49b2020-04-24 15:11:15 -0600670 }
671 }
672 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
673 // iterator of the outer while.
674
675 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
676 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
677 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600678 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
679 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600680 current.seek(seek_to);
681 } else if (!current->pos_A->valid && infill_state) {
682 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
683 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
684 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600685 }
John Zulauf5f13a792020-03-10 07:31:21 -0600686 }
John Zulauf16adfc92020-04-08 10:28:33 -0600687 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600688 }
John Zulauf1a224292020-06-30 14:52:13 -0600689
690 // Infill if range goes passed both the current and resolve map prior contents
691 if (recur_to_infill && (current->range.end < range.end)) {
692 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
693 ResourceAccessRangeMap gap_map;
694 const auto the_end = resolve_map->end();
695 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
696 for (auto &access : gap_map) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600697 barrier_action(&access.second);
John Zulauf1a224292020-06-30 14:52:13 -0600698 resolve_map->insert(the_end, access);
699 }
700 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600701}
702
John Zulauf43cc7462020-12-03 12:33:12 -0700703void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
704 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600705 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600706 if (range.non_empty() && infill_state) {
707 descent_map->insert(std::make_pair(range, *infill_state));
708 }
709 } else {
710 // Look for something to fill the gap further along.
711 for (const auto &prev_dep : prev_) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600712 const ApplyTrackbackBarriersAction barrier_action(prev_dep.barriers);
713 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600714 }
715
John Zulaufe5da6e52020-03-18 15:32:18 -0600716 if (src_external_.context) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600717 const ApplyTrackbackBarriersAction barrier_action(src_external_.barriers);
718 src_external_.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600719 }
720 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600721}
722
John Zulauf43cc7462020-12-03 12:33:12 -0700723AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
724 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -0600725}
726
John Zulauf1507ee42020-05-18 11:33:09 -0600727static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
728 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
729 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
730 return stage_access;
731}
732static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
733 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
734 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
735 return stage_access;
736}
737
John Zulauf7635de32020-05-29 17:14:15 -0600738// Caller must manage returned pointer
739static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
740 uint32_t subpass, const VkRect2D &render_area,
741 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
742 auto *proxy = new AccessContext(context);
743 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600744 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600745 return proxy;
746}
747
John Zulaufb02c1eb2020-10-06 16:33:36 -0600748template <typename BarrierAction>
John Zulauf52446eb2020-10-22 16:40:08 -0600749class ResolveAccessRangeFunctor {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600750 public:
John Zulauf43cc7462020-12-03 12:33:12 -0700751 ResolveAccessRangeFunctor(const AccessContext &context, AccessAddressType address_type, ResourceAccessRangeMap *descent_map,
752 const ResourceAccessState *infill_state, BarrierAction &barrier_action)
John Zulauf52446eb2020-10-22 16:40:08 -0600753 : context_(context),
754 address_type_(address_type),
755 descent_map_(descent_map),
756 infill_state_(infill_state),
757 barrier_action_(barrier_action) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600758 ResolveAccessRangeFunctor() = delete;
759 void operator()(const ResourceAccessRange &range) const {
760 context_.ResolveAccessRange(address_type_, range, barrier_action_, descent_map_, infill_state_);
761 }
762
763 private:
John Zulauf52446eb2020-10-22 16:40:08 -0600764 const AccessContext &context_;
John Zulauf43cc7462020-12-03 12:33:12 -0700765 const AccessAddressType address_type_;
John Zulauf52446eb2020-10-22 16:40:08 -0600766 ResourceAccessRangeMap *const descent_map_;
767 const ResourceAccessState *infill_state_;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600768 BarrierAction &barrier_action_;
769};
770
John Zulaufb02c1eb2020-10-06 16:33:36 -0600771template <typename BarrierAction>
772void AccessContext::ResolveAccessRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -0700773 BarrierAction &barrier_action, AccessAddressType address_type,
774 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600775 const ResolveAccessRangeFunctor<BarrierAction> action(*this, address_type, descent_map, infill_state, barrier_action);
776 ApplyOverImageRange(image_state, subresource_range, action);
John Zulauf62f10592020-04-03 12:20:02 -0600777}
778
John Zulauf7635de32020-05-29 17:14:15 -0600779// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600780bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600781 const VkRect2D &render_area, uint32_t subpass,
782 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
783 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600784 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600785 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
786 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
787 // those affects have not been recorded yet.
788 //
789 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
790 // to apply and only copy then, if this proves a hot spot.
791 std::unique_ptr<AccessContext> proxy_for_prev;
792 TrackBack proxy_track_back;
793
John Zulauf355e49b2020-04-24 15:11:15 -0600794 const auto &transitions = rp_state.subpass_transitions[subpass];
795 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600796 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
797
798 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
799 if (prev_needs_proxy) {
800 if (!proxy_for_prev) {
801 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
802 render_area, attachment_views));
803 proxy_track_back = *track_back;
804 proxy_track_back.context = proxy_for_prev.get();
805 }
806 track_back = &proxy_track_back;
807 }
808 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600809 if (hazard.hazard) {
John Zulauf389c34b2020-07-28 11:19:35 -0600810 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
811 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
812 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
813 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
814 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
815 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600816 }
817 }
818 return skip;
819}
820
John Zulauf1507ee42020-05-18 11:33:09 -0600821bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600822 const VkRect2D &render_area, uint32_t subpass,
823 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
824 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600825 bool skip = false;
826 const auto *attachment_ci = rp_state.createInfo.pAttachments;
827 VkExtent3D extent = CastTo3D(render_area.extent);
828 VkOffset3D offset = CastTo3D(render_area.offset);
John Zulaufa0a98292020-09-18 09:30:10 -0600829
John Zulauf1507ee42020-05-18 11:33:09 -0600830 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
831 if (subpass == rp_state.attachment_first_subpass[i]) {
832 if (attachment_views[i] == nullptr) continue;
833 const IMAGE_VIEW_STATE &view = *attachment_views[i];
834 const IMAGE_STATE *image = view.image_state.get();
835 if (image == nullptr) continue;
836 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -0600837
838 // Need check in the following way
839 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
840 // vs. transition
841 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
842 // for each aspect loaded.
843
844 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600845 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600846 const bool is_color = !(has_depth || has_stencil);
847
848 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -0600849 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -0600850
John Zulaufaff20662020-06-01 14:07:58 -0600851 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600852 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -0600853
John Zulaufb02c1eb2020-10-06 16:33:36 -0600854 auto hazard_range = view.normalized_subresource_range;
855 bool checked_stencil = false;
856 if (is_color) {
John Zulauf859089b2020-10-29 17:37:03 -0600857 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, kColorAttachmentRasterOrder, offset,
858 extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600859 aspect = "color";
860 } else {
861 if (has_depth) {
862 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf859089b2020-10-29 17:37:03 -0600863 hazard = DetectHazard(*image, load_index, hazard_range, kDepthStencilAttachmentRasterOrder, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600864 aspect = "depth";
865 }
866 if (!hazard.hazard && has_stencil) {
867 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf859089b2020-10-29 17:37:03 -0600868 hazard =
869 DetectHazard(*image, stencil_load_index, hazard_range, kDepthStencilAttachmentRasterOrder, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600870 aspect = "stencil";
871 checked_stencil = true;
872 }
873 }
874
875 if (hazard.hazard) {
876 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
877 if (hazard.tag == kCurrentCommandTag) {
878 // Hazard vs. ILT
879 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
880 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
881 " aspect %s during load with loadOp %s.",
882 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
883 } else {
John Zulauf1507ee42020-05-18 11:33:09 -0600884 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
885 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600886 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -0600887 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600888 string_UsageTag(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -0600889 }
890 }
891 }
892 }
893 return skip;
894}
895
John Zulaufaff20662020-06-01 14:07:58 -0600896// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
897// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
898// store is part of the same Next/End operation.
899// The latter is handled in layout transistion validation directly
900bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
901 const VkRect2D &render_area, uint32_t subpass,
902 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
903 const char *func_name) const {
904 bool skip = false;
905 const auto *attachment_ci = rp_state.createInfo.pAttachments;
906 VkExtent3D extent = CastTo3D(render_area.extent);
907 VkOffset3D offset = CastTo3D(render_area.offset);
908
909 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
910 if (subpass == rp_state.attachment_last_subpass[i]) {
911 if (attachment_views[i] == nullptr) continue;
912 const IMAGE_VIEW_STATE &view = *attachment_views[i];
913 const IMAGE_STATE *image = view.image_state.get();
914 if (image == nullptr) continue;
915 const auto &ci = attachment_ci[i];
916
917 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
918 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
919 // sake, we treat DONT_CARE as writing.
920 const bool has_depth = FormatHasDepth(ci.format);
921 const bool has_stencil = FormatHasStencil(ci.format);
922 const bool is_color = !(has_depth || has_stencil);
923 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
924 if (!has_stencil && !store_op_stores) continue;
925
926 HazardResult hazard;
927 const char *aspect = nullptr;
928 bool checked_stencil = false;
929 if (is_color) {
930 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
931 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
932 aspect = "color";
933 } else {
934 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
935 auto hazard_range = view.normalized_subresource_range;
936 if (has_depth && store_op_stores) {
937 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
938 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
939 kAttachmentRasterOrder, offset, extent);
940 aspect = "depth";
941 }
942 if (!hazard.hazard && has_stencil && stencil_op_stores) {
943 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
944 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
945 kAttachmentRasterOrder, offset, extent);
946 aspect = "stencil";
947 checked_stencil = true;
948 }
949 }
950
951 if (hazard.hazard) {
952 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
953 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -0600954 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
955 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600956 " %s aspect during store with %s %s. Access info %s",
John Zulauf1dae9192020-06-16 15:46:44 -0600957 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600958 store_op_string, string_UsageTag(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -0600959 }
960 }
961 }
962 return skip;
963}
964
John Zulaufb027cdb2020-05-21 14:25:22 -0600965bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
966 const VkRect2D &render_area,
967 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
968 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600969 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
970 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
971 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600972}
973
John Zulauf3d84f1b2020-03-09 13:33:25 -0600974class HazardDetector {
975 SyncStageAccessIndex usage_index_;
976
977 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600978 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700979 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
980 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600981 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700982 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -0600983};
984
John Zulauf69133422020-05-20 14:55:53 -0600985class HazardDetectorWithOrdering {
986 const SyncStageAccessIndex usage_index_;
987 const SyncOrderingBarrier &ordering_;
988
989 public:
990 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
991 return pos->second.DetectHazard(usage_index_, ordering_);
992 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700993 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
994 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -0600995 }
996 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
997 : usage_index_(usage), ordering_(ordering) {}
998};
999
John Zulauf16adfc92020-04-08 10:28:33 -06001000HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001001 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001002 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001003 const auto base_address = ResourceBaseAddress(buffer);
1004 HazardDetector detector(usage_index);
1005 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001006}
1007
John Zulauf69133422020-05-20 14:55:53 -06001008template <typename Detector>
1009HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1010 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1011 const VkExtent3D &extent, DetectOptions options) const {
1012 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001013 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001014 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1015 base_address);
1016 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001017 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001018 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001019 if (hazard.hazard) return hazard;
1020 }
1021 return HazardResult();
1022}
1023
John Zulauf540266b2020-04-06 18:54:53 -06001024HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1025 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1026 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001027 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1028 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -06001029 return DetectHazard(image, current_usage, subresource_range, offset, extent);
1030}
1031
1032HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1033 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1034 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -06001035 HazardDetector detector(current_usage);
1036 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
1037}
1038
1039HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1040 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
1041 const VkOffset3D &offset, const VkExtent3D &extent) const {
1042 HazardDetectorWithOrdering detector(current_usage, ordering);
1043 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001044}
1045
John Zulaufb027cdb2020-05-21 14:25:22 -06001046// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
1047// should have reported the issue regarding an invalid attachment entry
1048HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
1049 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
1050 VkImageAspectFlags aspect_mask) const {
1051 if (view != nullptr) {
1052 const IMAGE_STATE *image = view->image_state.get();
1053 if (image != nullptr) {
1054 auto *detect_range = &view->normalized_subresource_range;
1055 VkImageSubresourceRange masked_range;
1056 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1057 masked_range = view->normalized_subresource_range;
1058 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1059 detect_range = &masked_range;
1060 }
1061
1062 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1063 if (detect_range->aspectMask) {
1064 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
1065 }
1066 }
1067 }
1068 return HazardResult();
1069}
John Zulauf43cc7462020-12-03 12:33:12 -07001070
John Zulauf3d84f1b2020-03-09 13:33:25 -06001071class BarrierHazardDetector {
1072 public:
1073 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1074 SyncStageAccessFlags src_access_scope)
1075 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1076
John Zulauf5f13a792020-03-10 07:31:21 -06001077 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1078 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001079 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001080 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001081 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001082 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001083 }
1084
1085 private:
1086 SyncStageAccessIndex usage_index_;
1087 VkPipelineStageFlags src_exec_scope_;
1088 SyncStageAccessFlags src_access_scope_;
1089};
1090
John Zulauf16adfc92020-04-08 10:28:33 -06001091HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001092 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001093 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001094 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001095 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1096 VkOffset3D zero_offset = {0, 0, 0};
1097 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001098}
1099
John Zulauf355e49b2020-04-24 15:11:15 -06001100HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001101 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001102 const VkImageMemoryBarrier &barrier) const {
1103 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1104 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1105 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1106}
1107
John Zulauf9cb530d2019-09-30 14:14:10 -06001108template <typename Flags, typename Map>
1109SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1110 SyncStageAccessFlags scope = 0;
1111 for (const auto &bit_scope : map) {
1112 if (flag_mask < bit_scope.first) break;
1113
1114 if (flag_mask & bit_scope.first) {
1115 scope |= bit_scope.second;
1116 }
1117 }
1118 return scope;
1119}
1120
1121SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
1122 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1123}
1124
1125SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
1126 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
1127}
1128
1129// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
1130SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001131 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1132 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1133 // 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 -06001134 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1135}
1136
1137template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001138void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001139 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1140 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -06001141 auto pos = accesses->lower_bound(range);
1142 if (pos == accesses->end() || !pos->first.intersects(range)) {
1143 // The range is empty, fill it with a default value.
1144 pos = action.Infill(accesses, pos, range);
1145 } else if (range.begin < pos->first.begin) {
1146 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001147 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001148 } else if (pos->first.begin < range.begin) {
1149 // Trim the beginning if needed
1150 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1151 ++pos;
1152 }
1153
1154 const auto the_end = accesses->end();
1155 while ((pos != the_end) && pos->first.intersects(range)) {
1156 if (pos->first.end > range.end) {
1157 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1158 }
1159
1160 pos = action(accesses, pos);
1161 if (pos == the_end) break;
1162
1163 auto next = pos;
1164 ++next;
1165 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1166 // Need to infill if next is disjoint
1167 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001168 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001169 next = action.Infill(accesses, next, new_range);
1170 }
1171 pos = next;
1172 }
1173}
1174
1175struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001176 using Iterator = ResourceAccessRangeMap::iterator;
1177 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001178 // this is only called on gaps, and never returns a gap.
1179 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001180 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001181 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001182 }
John Zulauf5f13a792020-03-10 07:31:21 -06001183
John Zulauf5c5e88d2019-12-26 11:22:02 -07001184 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001185 auto &access_state = pos->second;
1186 access_state.Update(usage, tag);
1187 return pos;
1188 }
1189
John Zulauf43cc7462020-12-03 12:33:12 -07001190 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001191 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001192 : type(type_), context(context_), usage(usage_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001193 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001194 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001195 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001196 const ResourceUsageTag &tag;
1197};
1198
John Zulauf89311b42020-09-29 16:28:47 -06001199// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1200// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1201class ApplyBarrierFunctor {
1202 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001203 using Iterator = ResourceAccessRangeMap::iterator;
1204 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001205
John Zulauf5c5e88d2019-12-26 11:22:02 -07001206 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001207 auto &access_state = pos->second;
John Zulauf89311b42020-09-29 16:28:47 -06001208 access_state.ApplyBarrier(barrier_, layout_transition_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001209 return pos;
1210 }
1211
John Zulauf89311b42020-09-29 16:28:47 -06001212 ApplyBarrierFunctor(const SyncBarrier &barrier, bool layout_transition)
1213 : barrier_(barrier), layout_transition_(layout_transition) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001214
John Zulauf89311b42020-09-29 16:28:47 -06001215 private:
1216 const SyncBarrier barrier_;
1217 const bool layout_transition_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001218};
1219
John Zulauf89311b42020-09-29 16:28:47 -06001220// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1221// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1222// of a collection is known/present.
John Zulauf1e331ec2020-12-04 18:29:38 -07001223struct PipelineBarrierOp {
1224 SyncBarrier barrier;
1225 bool layout_transition;
1226 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1227 : barrier(barrier_), layout_transition(layout_transition_) {}
1228 PipelineBarrierOp() = default;
1229 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1230};
1231
1232template <typename BarrierOp>
John Zulauf89311b42020-09-29 16:28:47 -06001233class ApplyBarrierOpsFunctor {
1234 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001235 using Iterator = ResourceAccessRangeMap::iterator;
1236 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001237
John Zulauf5c5e88d2019-12-26 11:22:02 -07001238 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001239 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001240 for (const auto &op : barrier_ops_) {
1241 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001242 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001243
John Zulauf89311b42020-09-29 16:28:47 -06001244 if (resolve_) {
1245 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1246 // another walk
1247 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001248 }
1249 return pos;
1250 }
1251
John Zulauf89311b42020-09-29 16:28:47 -06001252 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulauf1e331ec2020-12-04 18:29:38 -07001253 ApplyBarrierOpsFunctor(bool resolve, const std::vector<BarrierOp> &barrier_ops, const ResourceUsageTag &tag)
1254 : resolve_(resolve), barrier_ops_(barrier_ops), tag_(tag) {}
John Zulauf89311b42020-09-29 16:28:47 -06001255
1256 private:
1257 bool resolve_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001258 const std::vector<BarrierOp> &barrier_ops_;
1259 const ResourceUsageTag &tag_;
1260};
1261
1262// This functor resolves the pendinging state.
1263class ResolvePendingBarrierFunctor {
1264 public:
1265 using Iterator = ResourceAccessRangeMap::iterator;
1266 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1267
1268 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1269 auto &access_state = pos->second;
1270 access_state.ApplyPendingBarriers(tag_);
1271 return pos;
1272 }
1273
1274 ResolvePendingBarrierFunctor(const ResourceUsageTag &tag) : tag_(tag) {}
1275
1276 private:
John Zulauf89311b42020-09-29 16:28:47 -06001277 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001278};
1279
John Zulauf43cc7462020-12-03 12:33:12 -07001280void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -06001281 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001282 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1283 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001284}
1285
John Zulauf16adfc92020-04-08 10:28:33 -06001286void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001287 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001288 if (!SimpleBinding(buffer)) return;
1289 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf43cc7462020-12-03 12:33:12 -07001290 UpdateAccessState(AccessAddressType::kLinear, current_usage, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001291}
John Zulauf355e49b2020-04-24 15:11:15 -06001292
John Zulauf540266b2020-04-06 18:54:53 -06001293void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001294 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001295 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001296 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001297 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001298 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1299 base_address);
1300 const auto address_type = ImageAddressType(image);
John Zulauf16adfc92020-04-08 10:28:33 -06001301 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001302 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001303 UpdateMemoryAccessState(&GetAccessStateMap(address_type), *range_gen, action);
John Zulauf5f13a792020-03-10 07:31:21 -06001304 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001305}
John Zulauf7635de32020-05-29 17:14:15 -06001306void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1307 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1308 if (view != nullptr) {
1309 const IMAGE_STATE *image = view->image_state.get();
1310 if (image != nullptr) {
1311 auto *update_range = &view->normalized_subresource_range;
1312 VkImageSubresourceRange masked_range;
1313 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1314 masked_range = view->normalized_subresource_range;
1315 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1316 update_range = &masked_range;
1317 }
1318 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1319 }
1320 }
1321}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001322
John Zulauf355e49b2020-04-24 15:11:15 -06001323void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1324 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1325 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001326 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1327 subresource.layerCount};
1328 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1329}
1330
John Zulauf540266b2020-04-06 18:54:53 -06001331template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001332void AccessContext::UpdateResourceAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001333 if (!SimpleBinding(buffer)) return;
1334 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf43cc7462020-12-03 12:33:12 -07001335 UpdateMemoryAccessState(&GetAccessStateMap(AccessAddressType::kLinear), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001336}
1337
1338template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001339void AccessContext::UpdateResourceAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1340 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001341 if (!SimpleBinding(image)) return;
1342 const auto address_type = ImageAddressType(image);
1343 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001344
John Zulauf16adfc92020-04-08 10:28:33 -06001345 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001346 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
1347 image.createInfo.extent, base_address);
1348
John Zulauf540266b2020-04-06 18:54:53 -06001349 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001350 UpdateMemoryAccessState(accesses, *range_gen, action);
John Zulauf540266b2020-04-06 18:54:53 -06001351 }
1352}
1353
John Zulauf7635de32020-05-29 17:14:15 -06001354void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1355 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1356 const ResourceUsageTag &tag) {
1357 UpdateStateResolveAction update(*this, tag);
1358 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1359}
1360
John Zulaufaff20662020-06-01 14:07:58 -06001361void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1362 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1363 const ResourceUsageTag &tag) {
1364 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1365 VkExtent3D extent = CastTo3D(render_area.extent);
1366 VkOffset3D offset = CastTo3D(render_area.offset);
1367
1368 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1369 if (rp_state.attachment_last_subpass[i] == subpass) {
1370 if (attachment_views[i] == nullptr) continue; // UNUSED
1371 const auto &view = *attachment_views[i];
1372 const IMAGE_STATE *image = view.image_state.get();
1373 if (image == nullptr) continue;
1374
1375 const auto &ci = attachment_ci[i];
1376 const bool has_depth = FormatHasDepth(ci.format);
1377 const bool has_stencil = FormatHasStencil(ci.format);
1378 const bool is_color = !(has_depth || has_stencil);
1379 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1380
1381 if (is_color && store_op_stores) {
1382 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1383 offset, extent, tag);
1384 } else {
1385 auto update_range = view.normalized_subresource_range;
1386 if (has_depth && store_op_stores) {
1387 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1388 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1389 tag);
1390 }
1391 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1392 if (has_stencil && stencil_op_stores) {
1393 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1394 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1395 tag);
1396 }
1397 }
1398 }
1399 }
1400}
1401
John Zulauf540266b2020-04-06 18:54:53 -06001402template <typename Action>
1403void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1404 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001405 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001406 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001407 }
1408}
1409
1410void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001411 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1412 auto &context = contexts[subpass_index];
John Zulaufb02c1eb2020-10-06 16:33:36 -06001413 ApplyTrackbackBarriersAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001414 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001415 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001416 }
1417 }
1418}
1419
John Zulauf355e49b2020-04-24 15:11:15 -06001420// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001421HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001422 if (!attach_view) return HazardResult();
1423 const auto image_state = attach_view->image_state.get();
1424 if (!image_state) return HazardResult();
1425
John Zulauf355e49b2020-04-24 15:11:15 -06001426 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001427 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001428
1429 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001430 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1431 const auto merged_barrier = MergeBarriers(track_back.barriers);
1432 HazardResult hazard =
1433 track_back.context->DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
1434 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001435 if (!hazard.hazard) {
1436 // The Async hazard check is against the current context's async set.
John Zulaufa0a98292020-09-18 09:30:10 -06001437 hazard = DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001438 attach_view->normalized_subresource_range, kDetectAsync);
1439 }
John Zulaufa0a98292020-09-18 09:30:10 -06001440
John Zulauf355e49b2020-04-24 15:11:15 -06001441 return hazard;
1442}
1443
John Zulaufb02c1eb2020-10-06 16:33:36 -06001444void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
1445 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1446 const ResourceUsageTag &tag) {
1447 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001448 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001449 for (const auto &transition : transitions) {
1450 const auto prev_pass = transition.prev_pass;
1451 const auto attachment_view = attachment_views[transition.attachment];
1452 if (!attachment_view) continue;
1453 const auto *image = attachment_view->image_state.get();
1454 if (!image) continue;
1455 if (!SimpleBinding(*image)) continue;
1456
1457 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1458 assert(trackback);
1459
1460 // Import the attachments into the current context
1461 const auto *prev_context = trackback->context;
1462 assert(prev_context);
1463 const auto address_type = ImageAddressType(*image);
1464 auto &target_map = GetAccessStateMap(address_type);
1465 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
1466 prev_context->ResolveAccessRange(*image, attachment_view->normalized_subresource_range, barrier_action, address_type,
John Zulauf646cc292020-10-23 09:16:45 -06001467 &target_map, &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001468 }
1469
John Zulauf86356ca2020-10-19 11:46:41 -06001470 // If there were no transitions skip this global map walk
1471 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001472 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulauf86356ca2020-10-19 11:46:41 -06001473 ApplyGlobalBarriers(apply_pending_action);
1474 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001475}
1476
John Zulauf355e49b2020-04-24 15:11:15 -06001477// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1478bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1479
1480 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001481 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001482 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1483 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06001484
John Zulauf86356ca2020-10-19 11:46:41 -06001485 assert(pRenderPassBegin);
1486 if (nullptr == pRenderPassBegin) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06001487
John Zulauf86356ca2020-10-19 11:46:41 -06001488 const uint32_t subpass = 0;
John Zulauf355e49b2020-04-24 15:11:15 -06001489
John Zulauf86356ca2020-10-19 11:46:41 -06001490 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
1491 // hasn't happened yet)
1492 const std::vector<AccessContext> empty_context_vector;
1493 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1494 const_cast<AccessContext *>(&cb_access_context_));
John Zulauf355e49b2020-04-24 15:11:15 -06001495
John Zulauf86356ca2020-10-19 11:46:41 -06001496 // Create a view list
1497 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1498 assert(fb_state);
1499 if (nullptr == fb_state) return skip;
1500 // NOTE: Must not use COMMAND_BUFFER_STATE variant of this as RecordCmdBeginRenderPass hasn't run and thus
1501 // the activeRenderPass.* fields haven't been set.
1502 const auto views = sync_state_->GetAttachmentViews(*pRenderPassBegin, *fb_state);
1503
1504 // Validate transitions
1505 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
1506
1507 // Validate load operations if there were no layout transition hazards
1508 if (!skip) {
1509 temp_context.RecordLayoutTransitions(rp_state, subpass, views, kCurrentCommandTag);
1510 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001511 }
John Zulauf86356ca2020-10-19 11:46:41 -06001512
John Zulauf355e49b2020-04-24 15:11:15 -06001513 return skip;
1514}
1515
locke-lunarg61870c22020-06-09 14:51:50 -06001516bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1517 const char *func_name) const {
1518 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001519 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001520 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001521 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1522 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001523 return skip;
1524 }
1525
1526 using DescriptorClass = cvdescriptorset::DescriptorClass;
1527 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1528 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1529 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1530 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1531
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001532 for (const auto &stage_state : pipe->stage_state) {
1533 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1534 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001535 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001536 }
locke-lunarg61870c22020-06-09 14:51:50 -06001537 for (const auto &set_binding : stage_state.descriptor_uses) {
1538 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1539 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1540 set_binding.first.second);
1541 const auto descriptor_type = binding_it.GetType();
1542 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1543 auto array_idx = 0;
1544
1545 if (binding_it.IsVariableDescriptorCount()) {
1546 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1547 }
1548 SyncStageAccessIndex sync_index =
1549 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1550
1551 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1552 uint32_t index = i - index_range.start;
1553 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1554 switch (descriptor->GetClass()) {
1555 case DescriptorClass::ImageSampler:
1556 case DescriptorClass::Image: {
1557 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001558 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001559 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001560 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1561 img_view_state = image_sampler_descriptor->GetImageViewState();
1562 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001563 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001564 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1565 img_view_state = image_descriptor->GetImageViewState();
1566 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001567 }
1568 if (!img_view_state) continue;
1569 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1570 VkExtent3D extent = {};
1571 VkOffset3D offset = {};
1572 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1573 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1574 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1575 } else {
1576 extent = img_state->createInfo.extent;
1577 }
John Zulauf361fb532020-07-22 10:45:39 -06001578 HazardResult hazard;
1579 const auto &subresource_range = img_view_state->normalized_subresource_range;
1580 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1581 // Input attachments are subject to raster ordering rules
1582 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
1583 kAttachmentRasterOrder, offset, extent);
1584 } else {
1585 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1586 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001587 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001588 skip |= sync_state_->LogError(
1589 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001590 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1591 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001592 func_name, string_SyncHazard(hazard.hazard),
1593 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1594 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001595 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001596 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1597 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1598 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001599 }
1600 break;
1601 }
1602 case DescriptorClass::TexelBuffer: {
1603 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1604 if (!buf_view_state) continue;
1605 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001606 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001607 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001608 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001609 skip |= sync_state_->LogError(
1610 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001611 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1612 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001613 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1614 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001615 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001616 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1617 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1618 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001619 }
1620 break;
1621 }
1622 case DescriptorClass::GeneralBuffer: {
1623 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1624 auto buf_state = buffer_descriptor->GetBufferState();
1625 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001626 const ResourceAccessRange range =
1627 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001628 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001629 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001630 skip |= sync_state_->LogError(
1631 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001632 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1633 func_name, string_SyncHazard(hazard.hazard),
1634 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001635 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001636 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001637 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1638 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1639 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001640 }
1641 break;
1642 }
1643 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1644 default:
1645 break;
1646 }
1647 }
1648 }
1649 }
1650 return skip;
1651}
1652
1653void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1654 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001655 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001656 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001657 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1658 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001659 return;
1660 }
1661
1662 using DescriptorClass = cvdescriptorset::DescriptorClass;
1663 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1664 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1665 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1666 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1667
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001668 for (const auto &stage_state : pipe->stage_state) {
1669 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1670 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001671 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001672 }
locke-lunarg61870c22020-06-09 14:51:50 -06001673 for (const auto &set_binding : stage_state.descriptor_uses) {
1674 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1675 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1676 set_binding.first.second);
1677 const auto descriptor_type = binding_it.GetType();
1678 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1679 auto array_idx = 0;
1680
1681 if (binding_it.IsVariableDescriptorCount()) {
1682 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1683 }
1684 SyncStageAccessIndex sync_index =
1685 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1686
1687 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1688 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1689 switch (descriptor->GetClass()) {
1690 case DescriptorClass::ImageSampler:
1691 case DescriptorClass::Image: {
1692 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1693 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1694 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1695 } else {
1696 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1697 }
1698 if (!img_view_state) continue;
1699 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1700 VkExtent3D extent = {};
1701 VkOffset3D offset = {};
1702 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1703 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1704 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1705 } else {
1706 extent = img_state->createInfo.extent;
1707 }
1708 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1709 offset, extent, tag);
1710 break;
1711 }
1712 case DescriptorClass::TexelBuffer: {
1713 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1714 if (!buf_view_state) continue;
1715 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001716 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001717 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1718 break;
1719 }
1720 case DescriptorClass::GeneralBuffer: {
1721 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1722 auto buf_state = buffer_descriptor->GetBufferState();
1723 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001724 const ResourceAccessRange range =
1725 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001726 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1727 break;
1728 }
1729 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1730 default:
1731 break;
1732 }
1733 }
1734 }
1735 }
1736}
1737
1738bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1739 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001740 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1741 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06001742 return skip;
1743 }
1744
1745 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1746 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001747 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06001748
1749 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001750 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06001751 if (binding_description.binding < binding_buffers_size) {
1752 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07001753 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001754
locke-lunarg1ae57d62020-11-18 10:49:19 -07001755 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001756 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1757 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001758 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1759 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001760 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001761 buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001762 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001763 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001764 }
1765 }
1766 }
1767 return skip;
1768}
1769
1770void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001771 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1772 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06001773 return;
1774 }
1775 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1776 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001777 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06001778
1779 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001780 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06001781 if (binding_description.binding < binding_buffers_size) {
1782 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07001783 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001784
locke-lunarg1ae57d62020-11-18 10:49:19 -07001785 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001786 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1787 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001788 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1789 }
1790 }
1791}
1792
1793bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1794 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001795 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07001796 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001797 }
locke-lunarg61870c22020-06-09 14:51:50 -06001798
locke-lunarg1ae57d62020-11-18 10:49:19 -07001799 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001800 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001801 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1802 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001803 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1804 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001805 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001806 index_buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001807 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001808 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001809 }
1810
1811 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1812 // We will detect more accurate range in the future.
1813 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1814 return skip;
1815}
1816
1817void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07001818 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) return;
locke-lunarg61870c22020-06-09 14:51:50 -06001819
locke-lunarg1ae57d62020-11-18 10:49:19 -07001820 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001821 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001822 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1823 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001824 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1825
1826 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1827 // We will detect more accurate range in the future.
1828 RecordDrawVertex(UINT32_MAX, 0, tag);
1829}
1830
1831bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06001832 bool skip = false;
1833 if (!current_renderpass_context_) return skip;
1834 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1835 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1836 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001837}
1838
1839void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001840 if (current_renderpass_context_) {
locke-lunarg7077d502020-06-18 21:37:26 -06001841 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1842 tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001843 }
locke-lunarg61870c22020-06-09 14:51:50 -06001844}
1845
John Zulauf355e49b2020-04-24 15:11:15 -06001846bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001847 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001848 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06001849 skip |=
1850 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001851
1852 return skip;
1853}
1854
1855bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1856 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001857 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001858 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001859 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06001860 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1861 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001862
1863 return skip;
1864}
1865
1866void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1867 assert(sync_state_);
1868 if (!cb_state_) return;
1869
1870 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06001871 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06001872 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06001873 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001874 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001875}
1876
John Zulauf355e49b2020-04-24 15:11:15 -06001877void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001878 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001879 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001880 current_context_ = &current_renderpass_context_->CurrentContext();
1881}
1882
John Zulauf355e49b2020-04-24 15:11:15 -06001883void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001884 assert(current_renderpass_context_);
1885 if (!current_renderpass_context_) return;
1886
John Zulauf1a224292020-06-30 14:52:13 -06001887 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001888 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001889 current_renderpass_context_ = nullptr;
1890}
1891
John Zulauf49beb112020-11-04 16:06:31 -07001892bool CommandBufferAccessContext::ValidateSetEvent(VkCommandBuffer commandBuffer, VkEvent event,
1893 VkPipelineStageFlags stageMask) const {
1894 return false;
1895}
1896
1897void CommandBufferAccessContext::RecordSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {}
1898
1899bool CommandBufferAccessContext::ValidateResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
1900 VkPipelineStageFlags stageMask) const {
1901 return false;
1902}
1903
1904void CommandBufferAccessContext::RecordResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {}
1905
1906bool CommandBufferAccessContext::ValidateWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
1907 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
1908 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1909 uint32_t bufferMemoryBarrierCount,
1910 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1911 uint32_t imageMemoryBarrierCount,
1912 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
1913 return false;
1914}
1915
1916void CommandBufferAccessContext::RecordWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
1917 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
1918 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1919 uint32_t bufferMemoryBarrierCount,
1920 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1921 uint32_t imageMemoryBarrierCount,
1922 const VkImageMemoryBarrier *pImageMemoryBarriers) const {}
1923
locke-lunarg61870c22020-06-09 14:51:50 -06001924bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1925 const VkRect2D &render_area, const char *func_name) const {
1926 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001927 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
1928 if (!pipe ||
1929 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001930 return skip;
1931 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001932 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001933 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1934 VkExtent3D extent = CastTo3D(render_area.extent);
1935 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001936
John Zulauf1a224292020-06-30 14:52:13 -06001937 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001938 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001939 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1940 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001941 if (location >= subpass.colorAttachmentCount ||
1942 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001943 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001944 }
locke-lunarg96dc9632020-06-10 17:22:18 -06001945 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001946 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1947 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06001948 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001949 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001950 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001951 func_name, string_SyncHazard(hazard.hazard),
1952 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1953 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001954 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001955 }
1956 }
1957 }
locke-lunarg37047832020-06-12 13:44:45 -06001958
1959 // PHASE1 TODO: Add layout based read/vs. write selection.
1960 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001961 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06001962 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001963 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001964 bool depth_write = false, stencil_write = false;
1965
1966 // PHASE1 TODO: These validation should be in core_checks.
1967 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001968 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1969 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06001970 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1971 depth_write = true;
1972 }
1973 // PHASE1 TODO: It needs to check if stencil is writable.
1974 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1975 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1976 // PHASE1 TODO: These validation should be in core_checks.
1977 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001978 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06001979 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1980 stencil_write = true;
1981 }
1982
1983 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1984 if (depth_write) {
1985 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001986 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1987 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001988 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001989 skip |= sync_state.LogError(
1990 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001991 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001992 func_name, string_SyncHazard(hazard.hazard),
1993 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1994 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001995 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001996 }
1997 }
1998 if (stencil_write) {
1999 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002000 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2001 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002002 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002003 skip |= sync_state.LogError(
2004 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002005 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002006 func_name, string_SyncHazard(hazard.hazard),
2007 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2008 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002009 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002010 }
locke-lunarg61870c22020-06-09 14:51:50 -06002011 }
2012 }
2013 return skip;
2014}
2015
locke-lunarg96dc9632020-06-10 17:22:18 -06002016void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
2017 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002018 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2019 if (!pipe ||
2020 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002021 return;
2022 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002023 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002024 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
2025 VkExtent3D extent = CastTo3D(render_area.extent);
2026 VkOffset3D offset = CastTo3D(render_area.offset);
2027
John Zulauf1a224292020-06-30 14:52:13 -06002028 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002029 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002030 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2031 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002032 if (location >= subpass.colorAttachmentCount ||
2033 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002034 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002035 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002036 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002037 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
2038 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002039 }
2040 }
locke-lunarg37047832020-06-12 13:44:45 -06002041
2042 // PHASE1 TODO: Add layout based read/vs. write selection.
2043 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002044 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002045 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002046 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002047 bool depth_write = false, stencil_write = false;
2048
2049 // PHASE1 TODO: These validation should be in core_checks.
2050 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002051 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2052 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002053 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2054 depth_write = true;
2055 }
2056 // PHASE1 TODO: It needs to check if stencil is writable.
2057 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2058 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2059 // PHASE1 TODO: These validation should be in core_checks.
2060 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002061 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002062 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2063 stencil_write = true;
2064 }
2065
2066 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2067 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002068 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2069 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002070 }
2071 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002072 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2073 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002074 }
locke-lunarg61870c22020-06-09 14:51:50 -06002075 }
2076}
2077
John Zulauf1507ee42020-05-18 11:33:09 -06002078bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
2079 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002080 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002081 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06002082 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2083 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002084 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2085 func_name);
2086
John Zulauf355e49b2020-04-24 15:11:15 -06002087 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002088 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06002089 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002090 if (!skip) {
2091 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2092 // on a copy of the (empty) next context.
2093 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2094 AccessContext temp_context(next_context);
2095 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
2096 skip |= temp_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
2097 }
John Zulauf7635de32020-05-29 17:14:15 -06002098 return skip;
2099}
2100bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
2101 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002102 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002103 bool skip = false;
2104 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2105 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002106 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2107 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06002108 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002109 return skip;
2110}
2111
John Zulauf7635de32020-05-29 17:14:15 -06002112AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2113 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2114}
2115
2116bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2117 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002118 bool skip = false;
2119
John Zulauf7635de32020-05-29 17:14:15 -06002120 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2121 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2122 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2123 // to apply and only copy then, if this proves a hot spot.
2124 std::unique_ptr<AccessContext> proxy_for_current;
2125
John Zulauf355e49b2020-04-24 15:11:15 -06002126 // Validate the "finalLayout" transitions to external
2127 // Get them from where there we're hidding in the extra entry.
2128 const auto &final_transitions = rp_state_->subpass_transitions.back();
2129 for (const auto &transition : final_transitions) {
2130 const auto &attach_view = attachment_views_[transition.attachment];
2131 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2132 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002133 auto *context = trackback.context;
2134
2135 if (transition.prev_pass == current_subpass_) {
2136 if (!proxy_for_current) {
2137 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2138 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2139 }
2140 context = proxy_for_current.get();
2141 }
2142
John Zulaufa0a98292020-09-18 09:30:10 -06002143 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2144 const auto merged_barrier = MergeBarriers(trackback.barriers);
2145 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope,
2146 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2147 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002148 if (hazard.hazard) {
2149 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2150 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf389c34b2020-07-28 11:19:35 -06002151 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002152 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf389c34b2020-07-28 11:19:35 -06002153 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf37ceaed2020-07-03 16:18:15 -06002154 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002155 }
2156 }
2157 return skip;
2158}
2159
2160void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2161 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002162 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002163}
2164
John Zulauf1507ee42020-05-18 11:33:09 -06002165void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2166 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2167 auto &subpass_context = subpass_contexts_[current_subpass_];
2168 VkExtent3D extent = CastTo3D(render_area.extent);
2169 VkOffset3D offset = CastTo3D(render_area.offset);
2170
2171 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2172 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2173 if (attachment_views_[i] == nullptr) continue; // UNUSED
2174 const auto &view = *attachment_views_[i];
2175 const IMAGE_STATE *image = view.image_state.get();
2176 if (image == nullptr) continue;
2177
2178 const auto &ci = attachment_ci[i];
2179 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002180 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002181 const bool is_color = !(has_depth || has_stencil);
2182
2183 if (is_color) {
2184 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2185 extent, tag);
2186 } else {
2187 auto update_range = view.normalized_subresource_range;
2188 if (has_depth) {
2189 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2190 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2191 }
2192 if (has_stencil) {
2193 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2194 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2195 tag);
2196 }
2197 }
2198 }
2199 }
2200}
2201
John Zulauf355e49b2020-04-24 15:11:15 -06002202void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002203 const AccessContext *external_context, VkQueueFlags queue_flags,
2204 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002205 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002206 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002207 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2208 // Add this for all subpasses here so that they exsist during next subpass validation
2209 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002210 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002211 }
2212 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2213
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002214 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002215 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002216 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002217}
John Zulauf1507ee42020-05-18 11:33:09 -06002218
2219void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002220 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2221 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002222 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002223
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002224 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2225 // subpass, so their tag needs to be different from the layout and load operations below.
2226 ResourceUsageTag next_tag = tag;
2227 next_tag.index++;
John Zulauf355e49b2020-04-24 15:11:15 -06002228 current_subpass_++;
2229 assert(current_subpass_ < subpass_contexts_.size());
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002230 subpass_contexts_[current_subpass_].SetStartTag(next_tag);
2231 RecordLayoutTransitions(next_tag);
2232 RecordLoadOperations(render_area, next_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002233}
2234
John Zulauf1a224292020-06-30 14:52:13 -06002235void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2236 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002237 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002238 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002239 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002240
John Zulauf355e49b2020-04-24 15:11:15 -06002241 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002242 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002243
2244 // Add the "finalLayout" transitions to external
2245 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002246 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2247 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2248 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002249 const auto &final_transitions = rp_state_->subpass_transitions.back();
2250 for (const auto &transition : final_transitions) {
2251 const auto &attachment = attachment_views_[transition.attachment];
2252 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002253 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf1e331ec2020-12-04 18:29:38 -07002254 std::vector<PipelineBarrierOp> barrier_ops;
2255 barrier_ops.reserve(last_trackback.barriers.size());
2256 for (const auto &barrier : last_trackback.barriers) {
2257 barrier_ops.emplace_back(barrier, true);
2258 }
2259 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, barrier_ops, tag);
2260 external_context->UpdateResourceAccess(*attachment->image_state, attachment->normalized_subresource_range, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002261 }
2262}
2263
John Zulauf3d84f1b2020-03-09 13:33:25 -06002264SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2265 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2266 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2267 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2268 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2269 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2270 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2271}
2272
John Zulaufb02c1eb2020-10-06 16:33:36 -06002273// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2274void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2275 for (const auto &barrier : barriers) {
2276 ApplyBarrier(barrier, layout_transition);
2277 }
2278}
2279
John Zulauf89311b42020-09-29 16:28:47 -06002280// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2281// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2282// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002283void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2284 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002285 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002286 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002287 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002288 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002289 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002290 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002291}
John Zulauf9cb530d2019-09-30 14:14:10 -06002292HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2293 HazardResult hazard;
2294 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002295 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002296 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002297 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002298 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002299 }
2300 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002301 // Write operation:
2302 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2303 // If reads exists -- test only against them because either:
2304 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2305 // * the read weren't hazards, and thus if the write is safe w.r.t. the reads, no hazard vs. last_write is possible if
2306 // the current write happens after the reads, so just test the write against the reades
2307 // Otherwise test against last_write
2308 //
2309 // Look for casus belli for WAR
2310 if (last_read_count) {
2311 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2312 const auto &read_access = last_reads[read_index];
2313 if (IsReadHazard(usage_stage, read_access)) {
2314 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2315 break;
2316 }
2317 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002318 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002319 // Write-After-Write check -- if we have a previous write to test against
2320 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002321 }
2322 }
2323 return hazard;
2324}
2325
John Zulauf69133422020-05-20 14:55:53 -06002326HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2327 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2328 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002329 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002330 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002331 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2332 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002333 if (IsRead(usage_bit)) {
2334 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2335 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2336 if (is_raw_hazard) {
2337 // NOTE: we know last_write is non-zero
2338 // See if the ordering rules save us from the simple RAW check above
2339 // First check to see if the current usage is covered by the ordering rules
2340 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2341 const bool usage_is_ordered =
2342 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2343 if (usage_is_ordered) {
2344 // Now see of the most recent write (or a subsequent read) are ordered
2345 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2346 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002347 }
2348 }
John Zulauf4285ee92020-09-23 10:20:52 -06002349 if (is_raw_hazard) {
2350 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2351 }
John Zulauf361fb532020-07-22 10:45:39 -06002352 } else {
2353 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002354 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulauf361fb532020-07-22 10:45:39 -06002355 if (last_read_count) {
John Zulauf361fb532020-07-22 10:45:39 -06002356 // Look for any WAR hazards outside the ordered set of stages
John Zulauf4285ee92020-09-23 10:20:52 -06002357 VkPipelineStageFlags ordered_stages = 0;
2358 if (usage_write_is_ordered) {
2359 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2360 ordered_stages = GetOrderedStages(ordering);
2361 }
2362 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2363 if ((ordered_stages & last_read_stages) != last_read_stages) {
2364 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2365 const auto &read_access = last_reads[read_index];
2366 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2367 if (IsReadHazard(usage_stage, read_access)) {
2368 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2369 break;
2370 }
John Zulaufd14743a2020-07-03 09:42:39 -06002371 }
2372 }
John Zulauf4285ee92020-09-23 10:20:52 -06002373 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002374 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002375 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002376 }
John Zulauf69133422020-05-20 14:55:53 -06002377 }
2378 }
2379 return hazard;
2380}
2381
John Zulauf2f952d22020-02-10 11:34:51 -07002382// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002383HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002384 HazardResult hazard;
2385 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002386 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2387 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2388 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002389 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002390 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002391 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002392 }
2393 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002394 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002395 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002396 } else if (last_read_count > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002397 // Any reads during the other subpass will conflict with this write, so we need to check them all.
2398 for (uint32_t i = 0; i < last_read_count; i++) {
2399 if (last_reads[i].tag.index >= start_tag.index) {
2400 hazard.Set(this, usage_index, WRITE_RACING_READ, last_reads[i].access, last_reads[i].tag);
2401 break;
2402 }
2403 }
John Zulauf2f952d22020-02-10 11:34:51 -07002404 }
2405 }
2406 return hazard;
2407}
2408
John Zulauf36bcf6a2020-02-03 15:12:52 -07002409HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002410 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002411 // Only supporting image layout transitions for now
2412 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2413 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002414 // only test for WAW if there no intervening read operations.
2415 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2416 if (last_read_count) {
John Zulauf355e49b2020-04-24 15:11:15 -06002417 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002418 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002419 const auto &read_access = last_reads[read_index];
2420 // If the read stage is not in the src sync sync
2421 // *AND* not execution chained with an existing sync barrier (that's the or)
2422 // then the barrier access is unsafe (R/W after R)
2423 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002424 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002425 break;
2426 }
2427 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002428 } else if (last_write.any()) {
John Zulauf361fb532020-07-22 10:45:39 -06002429 // If the previous write is *not* in the 1st access scope
2430 // *AND* the current barrier is not in the dependency chain
2431 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2432 // then the barrier access is unsafe (R/W after W)
2433 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
2434 // TODO: Do we need a difference hazard name for this?
2435 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2436 }
John Zulaufd14743a2020-07-03 09:42:39 -06002437 }
John Zulauf361fb532020-07-22 10:45:39 -06002438
John Zulauf0cb5be22020-01-23 12:18:22 -07002439 return hazard;
2440}
2441
John Zulauf5f13a792020-03-10 07:31:21 -06002442// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2443// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2444// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2445void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2446 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002447 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2448 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002449 *this = other;
2450 } else if (!other.write_tag.IsBefore(write_tag)) {
2451 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2452 // dependency chaining logic or any stage expansion)
2453 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002454 pending_write_barriers |= other.pending_write_barriers;
2455 pending_layout_transition |= other.pending_layout_transition;
2456 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002457
John Zulaufd14743a2020-07-03 09:42:39 -06002458 // Merge the read states
John Zulauf4285ee92020-09-23 10:20:52 -06002459 const auto pre_merge_count = last_read_count;
2460 const auto pre_merge_stages = last_read_stages;
John Zulauf5f13a792020-03-10 07:31:21 -06002461 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2462 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002463 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002464 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002465 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2466 // but we should wait on profiling data for that.
2467 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002468 auto &my_read = last_reads[my_read_index];
2469 if (other_read.stage == my_read.stage) {
2470 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002471 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002472 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002473 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002474 my_read.pending_dep_chain = other_read.pending_dep_chain;
2475 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
2476 // May require tracking more than one access per stage.
2477 my_read.barriers = other_read.barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002478 if (my_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
2479 // Since I'm overwriting the fragement stage read, also update the input attachment info
2480 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06002481 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002482 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002483 } else if (other_read.tag.IsBefore(my_read.tag)) {
2484 // The read tags match so merge the barriers
2485 my_read.barriers |= other_read.barriers;
2486 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002487 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002488
John Zulauf5f13a792020-03-10 07:31:21 -06002489 break;
2490 }
2491 }
2492 } else {
2493 // The other read stage doesn't exist in this, so add it.
2494 last_reads[last_read_count] = other_read;
2495 last_read_count++;
2496 last_read_stages |= other_read.stage;
John Zulauf4285ee92020-09-23 10:20:52 -06002497 if (other_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06002498 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002499 }
John Zulauf5f13a792020-03-10 07:31:21 -06002500 }
2501 }
John Zulauf361fb532020-07-22 10:45:39 -06002502 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002503 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2504 // ignore it.
John Zulauf5f13a792020-03-10 07:31:21 -06002505}
2506
John Zulauf9cb530d2019-09-30 14:14:10 -06002507void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2508 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2509 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002510 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002511 // Mulitple outstanding reads may be of interest and do dependency chains independently
2512 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2513 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002514 uint32_t update_index = kStageCount;
John Zulauf9cb530d2019-09-30 14:14:10 -06002515 if (usage_stage & last_read_stages) {
2516 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf4285ee92020-09-23 10:20:52 -06002517 if (last_reads[read_index].stage == usage_stage) {
2518 update_index = read_index;
John Zulauf9cb530d2019-09-30 14:14:10 -06002519 break;
2520 }
2521 }
John Zulauf4285ee92020-09-23 10:20:52 -06002522 assert(update_index < last_read_count);
John Zulauf9cb530d2019-09-30 14:14:10 -06002523 } else {
John Zulauf9cb530d2019-09-30 14:14:10 -06002524 assert(last_read_count < last_reads.size());
John Zulauf4285ee92020-09-23 10:20:52 -06002525 update_index = last_read_count++;
John Zulauf9cb530d2019-09-30 14:14:10 -06002526 last_read_stages |= usage_stage;
2527 }
John Zulauf4285ee92020-09-23 10:20:52 -06002528 last_reads[update_index].Set(usage_stage, usage_bit, 0, tag);
2529
2530 // Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
2531 if (usage_stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06002532 // TODO Revisit re: multiple reads for a given stage
2533 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002534 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002535 } else {
2536 // Assume write
2537 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002538 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002539 }
2540}
John Zulauf5f13a792020-03-10 07:31:21 -06002541
John Zulauf89311b42020-09-29 16:28:47 -06002542// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2543// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2544// We can overwrite them as *this* write is now after them.
2545//
2546// Note: intentionally ignore pending barriers and chains (i.e. don't apply or clear them), let ApplyPendingBarriers handle them.
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002547void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulauf89311b42020-09-29 16:28:47 -06002548 last_read_count = 0;
2549 last_read_stages = 0;
2550 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06002551 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06002552
2553 write_barriers = 0;
2554 write_dependency_chain = 0;
2555 write_tag = tag;
2556 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002557}
2558
John Zulauf89311b42020-09-29 16:28:47 -06002559// Apply the memory barrier without updating the existing barriers. The execution barrier
2560// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2561// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2562// replace the current write barriers or add to them, so accumulate to pending as well.
2563void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2564 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2565 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06002566 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
2567 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
2568 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
2569 // transistion *as* a write and in scope with the barrier (it's before visibility).
2570 if (layout_transition || InSourceScopeOrChain(barrier.src_exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06002571 pending_write_barriers |= barrier.dst_access_scope;
2572 pending_write_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002573 }
John Zulauf89311b42020-09-29 16:28:47 -06002574 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2575 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06002576
John Zulauf89311b42020-09-29 16:28:47 -06002577 if (!pending_layout_transition) {
2578 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2579 // don't need to be tracked as we're just going to zero them.
John Zulaufa0a98292020-09-18 09:30:10 -06002580 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf89311b42020-09-29 16:28:47 -06002581 ReadState &access = last_reads[read_index];
2582 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2583 if (barrier.src_exec_scope & (access.stage | access.barriers)) {
2584 access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002585 }
2586 }
John Zulaufa0a98292020-09-18 09:30:10 -06002587 }
John Zulaufa0a98292020-09-18 09:30:10 -06002588}
2589
John Zulauf89311b42020-09-29 16:28:47 -06002590void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
2591 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06002592 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
2593 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
2594 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06002595 }
John Zulauf89311b42020-09-29 16:28:47 -06002596
2597 // Apply the accumulate execution barriers (and thus update chaining information)
2598 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
2599 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2600 ReadState &access = last_reads[read_index];
2601 access.barriers |= access.pending_dep_chain;
2602 read_execution_barriers |= access.barriers;
2603 access.pending_dep_chain = 0;
2604 }
2605
2606 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
2607 write_dependency_chain |= pending_write_dep_chain;
2608 write_barriers |= pending_write_barriers;
2609 pending_write_dep_chain = 0;
2610 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002611}
2612
John Zulauf59e25072020-07-17 10:55:21 -06002613// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002614VkPipelineStageFlags ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
John Zulauf59e25072020-07-17 10:55:21 -06002615 VkPipelineStageFlags barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06002616
2617 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2618 const auto &read_access = last_reads[read_index];
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002619 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06002620 barriers = read_access.barriers;
2621 break;
John Zulauf59e25072020-07-17 10:55:21 -06002622 }
2623 }
John Zulauf4285ee92020-09-23 10:20:52 -06002624
John Zulauf59e25072020-07-17 10:55:21 -06002625 return barriers;
2626}
2627
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002628inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlagBits usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06002629 assert(IsRead(usage));
2630 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
2631 // * the previous reads are not hazards, and thus last_write must be visible and available to
2632 // any reads that happen after.
2633 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2634 // the current read will be also not be a hazard, thus reporting a hazard here adds no needed information.
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002635 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06002636}
2637
John Zulauf4285ee92020-09-23 10:20:52 -06002638VkPipelineStageFlags ResourceAccessState::GetOrderedStages(const SyncOrderingBarrier &ordering) const {
2639 // Whether the stage are in the ordering scope only matters if the current write is ordered
2640 VkPipelineStageFlags ordered_stages = last_read_stages & ordering.exec_scope;
2641 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002642 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06002643 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06002644 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
2645 ordered_stages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
2646 }
2647
2648 return ordered_stages;
2649}
2650
2651inline ResourceAccessState::ReadState *ResourceAccessState::GetReadStateForStage(VkPipelineStageFlagBits stage,
2652 uint32_t search_limit) {
2653 ReadState *read_state = nullptr;
2654 search_limit = std::min(search_limit, last_read_count);
2655 for (uint32_t i = 0; i < search_limit; i++) {
2656 if (last_reads[i].stage == stage) {
2657 read_state = &last_reads[i];
2658 break;
2659 }
2660 }
2661 return read_state;
2662}
2663
John Zulaufd1f85d42020-04-15 12:23:15 -06002664void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002665 auto *access_context = GetAccessContextNoInsert(command_buffer);
2666 if (access_context) {
2667 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002668 }
2669}
2670
John Zulaufd1f85d42020-04-15 12:23:15 -06002671void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2672 auto access_found = cb_access_state.find(command_buffer);
2673 if (access_found != cb_access_state.end()) {
2674 access_found->second->Reset();
2675 cb_access_state.erase(access_found);
2676 }
2677}
2678
John Zulauf89311b42020-09-29 16:28:47 -06002679void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2680 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags src_access_scope,
2681 SyncStageAccessFlags dst_access_scope, uint32_t memory_barrier_count,
2682 const VkMemoryBarrier *pMemoryBarriers, const ResourceUsageTag &tag) {
John Zulauf1e331ec2020-12-04 18:29:38 -07002683 std::vector<PipelineBarrierOp> barrier_ops;
2684 barrier_ops.reserve(std::min<uint32_t>(1, memory_barrier_count));
John Zulauf89311b42020-09-29 16:28:47 -06002685 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
2686 const auto &barrier = pMemoryBarriers[barrier_index];
2687 SyncBarrier sync_barrier(src_exec_scope, SyncStageAccess::AccessScope(src_access_scope, barrier.srcAccessMask),
2688 dst_exec_scope, SyncStageAccess::AccessScope(dst_access_scope, barrier.dstAccessMask));
John Zulauf1e331ec2020-12-04 18:29:38 -07002689 barrier_ops.emplace_back(sync_barrier, false);
John Zulauf89311b42020-09-29 16:28:47 -06002690 }
2691 if (0 == memory_barrier_count) {
2692 // If there are no global memory barriers, force an exec barrier
John Zulauf1e331ec2020-12-04 18:29:38 -07002693 barrier_ops.emplace_back(SyncBarrier(src_exec_scope, 0, dst_exec_scope, 0), false);
John Zulauf89311b42020-09-29 16:28:47 -06002694 }
John Zulauf1e331ec2020-12-04 18:29:38 -07002695 ApplyBarrierOpsFunctor<PipelineBarrierOp> barriers_functor(true /* resolve */, barrier_ops, tag);
John Zulauf540266b2020-04-06 18:54:53 -06002696 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002697}
2698
John Zulauf540266b2020-04-06 18:54:53 -06002699void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002700 const SyncStageAccessFlags &src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2701 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002702 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002703 for (uint32_t index = 0; index < barrier_count; index++) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002704 auto barrier = barriers[index]; // barrier is a copy
John Zulauf9cb530d2019-09-30 14:14:10 -06002705 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2706 if (!buffer) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002707 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
2708 const ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002709 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2710 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06002711 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2712 const ApplyBarrierFunctor update_action(sync_barrier, false /* layout_transition */);
2713 context->UpdateResourceAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002714 }
2715}
2716
John Zulauf540266b2020-04-06 18:54:53 -06002717void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002718 const SyncStageAccessFlags &src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2719 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002720 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002721 for (uint32_t index = 0; index < barrier_count; index++) {
2722 const auto &barrier = barriers[index];
2723 const auto *image = Get<IMAGE_STATE>(barrier.image);
2724 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002725 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002726 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2727 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2728 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06002729 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2730 const ApplyBarrierFunctor barrier_action(sync_barrier, layout_transition);
2731 context->UpdateResourceAccess(*image, subresource_range, barrier_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002732 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002733}
2734
2735bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2736 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2737 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002738 const auto *cb_context = GetAccessContext(commandBuffer);
2739 assert(cb_context);
2740 if (!cb_context) return skip;
2741 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002742
John Zulauf3d84f1b2020-03-09 13:33:25 -06002743 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002744 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002745 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002746
2747 for (uint32_t region = 0; region < regionCount; region++) {
2748 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002749 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002750 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002751 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002752 if (hazard.hazard) {
2753 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002754 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002755 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002756 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002757 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002758 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002759 }
John Zulauf16adfc92020-04-08 10:28:33 -06002760 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002761 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06002762 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002763 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002764 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002765 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002766 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002767 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002768 }
2769 }
2770 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002771 }
2772 return skip;
2773}
2774
2775void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2776 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002777 auto *cb_context = GetAccessContext(commandBuffer);
2778 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002779 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002780 auto *context = cb_context->GetCurrentAccessContext();
2781
John Zulauf9cb530d2019-09-30 14:14:10 -06002782 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002783 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002784
2785 for (uint32_t region = 0; region < regionCount; region++) {
2786 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002787 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002788 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002789 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002790 }
John Zulauf16adfc92020-04-08 10:28:33 -06002791 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002792 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002793 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002794 }
2795 }
2796}
2797
Jeff Leger178b1e52020-10-05 12:22:23 -04002798bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
2799 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
2800 bool skip = false;
2801 const auto *cb_context = GetAccessContext(commandBuffer);
2802 assert(cb_context);
2803 if (!cb_context) return skip;
2804 const auto *context = cb_context->GetCurrentAccessContext();
2805
2806 // If we have no previous accesses, we have no hazards
2807 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2808 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2809
2810 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2811 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2812 if (src_buffer) {
2813 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2814 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
2815 if (hazard.hazard) {
2816 // TODO -- add tag information to log msg when useful.
2817 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
2818 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
2819 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
2820 region, string_UsageTag(hazard).c_str());
2821 }
2822 }
2823 if (dst_buffer && !skip) {
2824 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2825 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
2826 if (hazard.hazard) {
2827 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
2828 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
2829 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
2830 region, string_UsageTag(hazard).c_str());
2831 }
2832 }
2833 if (skip) break;
2834 }
2835 return skip;
2836}
2837
2838void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
2839 auto *cb_context = GetAccessContext(commandBuffer);
2840 assert(cb_context);
2841 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
2842 auto *context = cb_context->GetCurrentAccessContext();
2843
2844 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2845 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2846
2847 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2848 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2849 if (src_buffer) {
2850 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2851 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
2852 }
2853 if (dst_buffer) {
2854 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2855 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
2856 }
2857 }
2858}
2859
John Zulauf5c5e88d2019-12-26 11:22:02 -07002860bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2861 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2862 const VkImageCopy *pRegions) const {
2863 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002864 const auto *cb_access_context = GetAccessContext(commandBuffer);
2865 assert(cb_access_context);
2866 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002867
John Zulauf3d84f1b2020-03-09 13:33:25 -06002868 const auto *context = cb_access_context->GetCurrentAccessContext();
2869 assert(context);
2870 if (!context) return skip;
2871
2872 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2873 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002874 for (uint32_t region = 0; region < regionCount; region++) {
2875 const auto &copy_region = pRegions[region];
2876 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002877 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002878 copy_region.srcOffset, copy_region.extent);
2879 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002880 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002881 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002882 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002883 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002884 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002885 }
2886
2887 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002888 VkExtent3D dst_copy_extent =
2889 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002890 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002891 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002892 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002893 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002894 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002895 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002896 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002897 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002898 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002899 }
2900 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002901
John Zulauf5c5e88d2019-12-26 11:22:02 -07002902 return skip;
2903}
2904
2905void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2906 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2907 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002908 auto *cb_access_context = GetAccessContext(commandBuffer);
2909 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002910 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002911 auto *context = cb_access_context->GetCurrentAccessContext();
2912 assert(context);
2913
John Zulauf5c5e88d2019-12-26 11:22:02 -07002914 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002915 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002916
2917 for (uint32_t region = 0; region < regionCount; region++) {
2918 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002919 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002920 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2921 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002922 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002923 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002924 VkExtent3D dst_copy_extent =
2925 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002926 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2927 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002928 }
2929 }
2930}
2931
Jeff Leger178b1e52020-10-05 12:22:23 -04002932bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
2933 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
2934 bool skip = false;
2935 const auto *cb_access_context = GetAccessContext(commandBuffer);
2936 assert(cb_access_context);
2937 if (!cb_access_context) return skip;
2938
2939 const auto *context = cb_access_context->GetCurrentAccessContext();
2940 assert(context);
2941 if (!context) return skip;
2942
2943 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2944 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2945 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2946 const auto &copy_region = pCopyImageInfo->pRegions[region];
2947 if (src_image) {
2948 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
2949 copy_region.srcOffset, copy_region.extent);
2950 if (hazard.hazard) {
2951 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
2952 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
2953 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
2954 region, string_UsageTag(hazard).c_str());
2955 }
2956 }
2957
2958 if (dst_image) {
2959 VkExtent3D dst_copy_extent =
2960 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2961 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
2962 copy_region.dstOffset, dst_copy_extent);
2963 if (hazard.hazard) {
2964 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
2965 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
2966 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
2967 region, string_UsageTag(hazard).c_str());
2968 }
2969 if (skip) break;
2970 }
2971 }
2972
2973 return skip;
2974}
2975
2976void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
2977 auto *cb_access_context = GetAccessContext(commandBuffer);
2978 assert(cb_access_context);
2979 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
2980 auto *context = cb_access_context->GetCurrentAccessContext();
2981 assert(context);
2982
2983 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2984 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2985
2986 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2987 const auto &copy_region = pCopyImageInfo->pRegions[region];
2988 if (src_image) {
2989 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2990 copy_region.extent, tag);
2991 }
2992 if (dst_image) {
2993 VkExtent3D dst_copy_extent =
2994 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2995 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2996 dst_copy_extent, tag);
2997 }
2998 }
2999}
3000
John Zulauf9cb530d2019-09-30 14:14:10 -06003001bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3002 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3003 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3004 uint32_t bufferMemoryBarrierCount,
3005 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3006 uint32_t imageMemoryBarrierCount,
3007 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3008 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003009 const auto *cb_access_context = GetAccessContext(commandBuffer);
3010 assert(cb_access_context);
3011 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003012
John Zulauf3d84f1b2020-03-09 13:33:25 -06003013 const auto *context = cb_access_context->GetCurrentAccessContext();
3014 assert(context);
3015 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003016
John Zulauf3d84f1b2020-03-09 13:33:25 -06003017 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003018 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
3019 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07003020 // Validate Image Layout transitions
3021 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
3022 const auto &barrier = pImageMemoryBarriers[index];
3023 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
3024 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
3025 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06003026 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07003027 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003028 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003029 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003030 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003031 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003032 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07003033 }
3034 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003035
3036 return skip;
3037}
3038
3039void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3040 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3041 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3042 uint32_t bufferMemoryBarrierCount,
3043 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3044 uint32_t imageMemoryBarrierCount,
3045 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003046 auto *cb_access_context = GetAccessContext(commandBuffer);
3047 assert(cb_access_context);
3048 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06003049 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003050 auto access_context = cb_access_context->GetCurrentAccessContext();
3051 assert(access_context);
3052 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003053
John Zulauf3d84f1b2020-03-09 13:33:25 -06003054 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003055 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003056 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003057 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
3058 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
3059 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf89311b42020-09-29 16:28:47 -06003060
3061 // These two apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
3062 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
3063 // of the barriers is maintained.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003064 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
3065 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06003066 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06003067 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003068
John Zulauf89311b42020-09-29 16:28:47 -06003069 // Apply the global barriers last as is it walks all memory, it can also clean up the "pending" state without requiring an
3070 // additional pass, updating the dependency chains *last* as it goes along.
3071 // This is needed to guarantee order independence of the three lists.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003072 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf89311b42020-09-29 16:28:47 -06003073 pMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003074}
3075
3076void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3077 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3078 // The state tracker sets up the device state
3079 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3080
John Zulauf5f13a792020-03-10 07:31:21 -06003081 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3082 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003083 // TODO: Find a good way to do this hooklessly.
3084 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3085 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3086 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3087
John Zulaufd1f85d42020-04-15 12:23:15 -06003088 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3089 sync_device_state->ResetCommandBufferCallback(command_buffer);
3090 });
3091 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3092 sync_device_state->FreeCommandBufferCallback(command_buffer);
3093 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003094}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003095
John Zulauf355e49b2020-04-24 15:11:15 -06003096bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003097 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003098 bool skip = false;
3099 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
3100 auto cb_context = GetAccessContext(commandBuffer);
3101
3102 if (rp_state && cb_context) {
3103 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
3104 }
3105
3106 return skip;
3107}
3108
3109bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3110 VkSubpassContents contents) const {
3111 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3112 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3113 subpass_begin_info.contents = contents;
3114 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
3115 return skip;
3116}
3117
3118bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003119 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003120 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3121 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
3122 return skip;
3123}
3124
3125bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3126 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003127 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003128 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3129 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
3130 return skip;
3131}
3132
John Zulauf3d84f1b2020-03-09 13:33:25 -06003133void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3134 VkResult result) {
3135 // The state tracker sets up the command buffer state
3136 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3137
3138 // Create/initialize the structure that trackers accesses at the command buffer scope.
3139 auto cb_access_context = GetAccessContext(commandBuffer);
3140 assert(cb_access_context);
3141 cb_access_context->Reset();
3142}
3143
3144void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06003145 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003146 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003147 if (cb_context) {
3148 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003149 }
3150}
3151
3152void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3153 VkSubpassContents contents) {
3154 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3155 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3156 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003157 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003158}
3159
3160void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3161 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3162 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003163 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003164}
3165
3166void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3167 const VkRenderPassBeginInfo *pRenderPassBegin,
3168 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3169 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003170 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
3171}
3172
Mike Schuchardt2df08912020-12-15 16:28:09 -08003173bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3174 const VkSubpassEndInfo *pSubpassEndInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003175 bool skip = false;
3176
3177 auto cb_context = GetAccessContext(commandBuffer);
3178 assert(cb_context);
3179 auto cb_state = cb_context->GetCommandBufferState();
3180 if (!cb_state) return skip;
3181
3182 auto rp_state = cb_state->activeRenderPass;
3183 if (!rp_state) return skip;
3184
3185 skip |= cb_context->ValidateNextSubpass(func_name);
3186
3187 return skip;
3188}
3189
3190bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3191 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
3192 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3193 subpass_begin_info.contents = contents;
3194 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
3195 return skip;
3196}
3197
Mike Schuchardt2df08912020-12-15 16:28:09 -08003198bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3199 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003200 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3201 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
3202 return skip;
3203}
3204
3205bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3206 const VkSubpassEndInfo *pSubpassEndInfo) const {
3207 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3208 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
3209 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003210}
3211
3212void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003213 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003214 auto cb_context = GetAccessContext(commandBuffer);
3215 assert(cb_context);
3216 auto cb_state = cb_context->GetCommandBufferState();
3217 if (!cb_state) return;
3218
3219 auto rp_state = cb_state->activeRenderPass;
3220 if (!rp_state) return;
3221
John Zulauf355e49b2020-04-24 15:11:15 -06003222 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003223}
3224
3225void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3226 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
3227 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3228 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003229 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003230}
3231
3232void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3233 const VkSubpassEndInfo *pSubpassEndInfo) {
3234 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003235 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003236}
3237
3238void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3239 const VkSubpassEndInfo *pSubpassEndInfo) {
3240 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003241 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003242}
3243
Mike Schuchardt2df08912020-12-15 16:28:09 -08003244bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003245 const char *func_name) const {
3246 bool skip = false;
3247
3248 auto cb_context = GetAccessContext(commandBuffer);
3249 assert(cb_context);
3250 auto cb_state = cb_context->GetCommandBufferState();
3251 if (!cb_state) return skip;
3252
3253 auto rp_state = cb_state->activeRenderPass;
3254 if (!rp_state) return skip;
3255
3256 skip |= cb_context->ValidateEndRenderpass(func_name);
3257 return skip;
3258}
3259
3260bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3261 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
3262 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
3263 return skip;
3264}
3265
Mike Schuchardt2df08912020-12-15 16:28:09 -08003266bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003267 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
3268 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
3269 return skip;
3270}
3271
3272bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003273 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003274 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
3275 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
3276 return skip;
3277}
3278
3279void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3280 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003281 // Resolve the all subpass contexts to the command buffer contexts
3282 auto cb_context = GetAccessContext(commandBuffer);
3283 assert(cb_context);
3284 auto cb_state = cb_context->GetCommandBufferState();
3285 if (!cb_state) return;
3286
locke-lunargaecf2152020-05-12 17:15:41 -06003287 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06003288 if (!rp_state) return;
3289
John Zulauf355e49b2020-04-24 15:11:15 -06003290 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06003291}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003292
John Zulauf33fc1d52020-07-17 11:01:10 -06003293// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3294// updates to a resource which do not conflict at the byte level.
3295// TODO: Revisit this rule to see if it needs to be tighter or looser
3296// TODO: Add programatic control over suppression heuristics
3297bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3298 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3299}
3300
John Zulauf3d84f1b2020-03-09 13:33:25 -06003301void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003302 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003303 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003304}
3305
3306void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003307 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003308 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003309}
3310
3311void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003312 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003313 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003314}
locke-lunarga19c71d2020-03-02 18:17:04 -07003315
Jeff Leger178b1e52020-10-05 12:22:23 -04003316template <typename BufferImageCopyRegionType>
3317bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3318 VkImageLayout dstImageLayout, uint32_t regionCount,
3319 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003320 bool skip = false;
3321 const auto *cb_access_context = GetAccessContext(commandBuffer);
3322 assert(cb_access_context);
3323 if (!cb_access_context) return skip;
3324
Jeff Leger178b1e52020-10-05 12:22:23 -04003325 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3326 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3327
locke-lunarga19c71d2020-03-02 18:17:04 -07003328 const auto *context = cb_access_context->GetCurrentAccessContext();
3329 assert(context);
3330 if (!context) return skip;
3331
3332 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003333 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3334
3335 for (uint32_t region = 0; region < regionCount; region++) {
3336 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003337 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003338 ResourceAccessRange src_range =
3339 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003340 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003341 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003342 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003343 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003344 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003345 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003346 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003347 }
3348 }
3349 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003350 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003351 copy_region.imageOffset, copy_region.imageExtent);
3352 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003353 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003354 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003355 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003356 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003357 }
3358 if (skip) break;
3359 }
3360 if (skip) break;
3361 }
3362 return skip;
3363}
3364
Jeff Leger178b1e52020-10-05 12:22:23 -04003365bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3366 VkImageLayout dstImageLayout, uint32_t regionCount,
3367 const VkBufferImageCopy *pRegions) const {
3368 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3369 COPY_COMMAND_VERSION_1);
3370}
3371
3372bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3373 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3374 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3375 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3376 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3377}
3378
3379template <typename BufferImageCopyRegionType>
3380void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3381 VkImageLayout dstImageLayout, uint32_t regionCount,
3382 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003383 auto *cb_access_context = GetAccessContext(commandBuffer);
3384 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003385
3386 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3387 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3388
3389 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003390 auto *context = cb_access_context->GetCurrentAccessContext();
3391 assert(context);
3392
3393 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003394 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003395
3396 for (uint32_t region = 0; region < regionCount; region++) {
3397 const auto &copy_region = pRegions[region];
3398 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003399 ResourceAccessRange src_range =
3400 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003401 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003402 }
3403 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003404 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003405 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003406 }
3407 }
3408}
3409
Jeff Leger178b1e52020-10-05 12:22:23 -04003410void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3411 VkImageLayout dstImageLayout, uint32_t regionCount,
3412 const VkBufferImageCopy *pRegions) {
3413 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3414 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3415}
3416
3417void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3418 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3419 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3420 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3421 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3422 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3423}
3424
3425template <typename BufferImageCopyRegionType>
3426bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3427 VkBuffer dstBuffer, uint32_t regionCount,
3428 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003429 bool skip = false;
3430 const auto *cb_access_context = GetAccessContext(commandBuffer);
3431 assert(cb_access_context);
3432 if (!cb_access_context) return skip;
3433
Jeff Leger178b1e52020-10-05 12:22:23 -04003434 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3435 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3436
locke-lunarga19c71d2020-03-02 18:17:04 -07003437 const auto *context = cb_access_context->GetCurrentAccessContext();
3438 assert(context);
3439 if (!context) return skip;
3440
3441 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3442 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3443 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3444 for (uint32_t region = 0; region < regionCount; region++) {
3445 const auto &copy_region = pRegions[region];
3446 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003447 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003448 copy_region.imageOffset, copy_region.imageExtent);
3449 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003450 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003451 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003452 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003453 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003454 }
3455 }
3456 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06003457 ResourceAccessRange dst_range =
3458 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003459 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003460 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003461 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003462 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003463 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003464 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003465 }
3466 }
3467 if (skip) break;
3468 }
3469 return skip;
3470}
3471
Jeff Leger178b1e52020-10-05 12:22:23 -04003472bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3473 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3474 const VkBufferImageCopy *pRegions) const {
3475 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3476 COPY_COMMAND_VERSION_1);
3477}
3478
3479bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3480 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3481 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3482 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3483 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3484}
3485
3486template <typename BufferImageCopyRegionType>
3487void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3488 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3489 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003490 auto *cb_access_context = GetAccessContext(commandBuffer);
3491 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003492
3493 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3494 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3495
3496 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003497 auto *context = cb_access_context->GetCurrentAccessContext();
3498 assert(context);
3499
3500 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003501 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3502 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 -06003503 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003504
3505 for (uint32_t region = 0; region < regionCount; region++) {
3506 const auto &copy_region = pRegions[region];
3507 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003508 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003509 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003510 }
3511 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003512 ResourceAccessRange dst_range =
3513 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003514 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003515 }
3516 }
3517}
3518
Jeff Leger178b1e52020-10-05 12:22:23 -04003519void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3520 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3521 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3522 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3523}
3524
3525void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3526 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3527 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3528 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3529 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3530 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3531}
3532
3533template <typename RegionType>
3534bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3535 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3536 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003537 bool skip = false;
3538 const auto *cb_access_context = GetAccessContext(commandBuffer);
3539 assert(cb_access_context);
3540 if (!cb_access_context) return skip;
3541
3542 const auto *context = cb_access_context->GetCurrentAccessContext();
3543 assert(context);
3544 if (!context) return skip;
3545
3546 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3547 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3548
3549 for (uint32_t region = 0; region < regionCount; region++) {
3550 const auto &blit_region = pRegions[region];
3551 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003552 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3553 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3554 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3555 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3556 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3557 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3558 auto hazard =
3559 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003560 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003561 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003562 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003563 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003564 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003565 }
3566 }
3567
3568 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003569 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3570 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3571 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3572 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3573 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3574 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3575 auto hazard =
3576 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003577 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003578 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003579 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003580 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003581 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003582 }
3583 if (skip) break;
3584 }
3585 }
3586
3587 return skip;
3588}
3589
Jeff Leger178b1e52020-10-05 12:22:23 -04003590bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3591 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3592 const VkImageBlit *pRegions, VkFilter filter) const {
3593 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3594 "vkCmdBlitImage");
3595}
3596
3597bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3598 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3599 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3600 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3601 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3602}
3603
3604template <typename RegionType>
3605void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3606 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3607 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003608 auto *cb_access_context = GetAccessContext(commandBuffer);
3609 assert(cb_access_context);
3610 auto *context = cb_access_context->GetCurrentAccessContext();
3611 assert(context);
3612
3613 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003614 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003615
3616 for (uint32_t region = 0; region < regionCount; region++) {
3617 const auto &blit_region = pRegions[region];
3618 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003619 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3620 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3621 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3622 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3623 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3624 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3625 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003626 }
3627 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003628 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3629 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3630 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3631 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3632 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3633 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3634 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003635 }
3636 }
3637}
locke-lunarg36ba2592020-04-03 09:42:04 -06003638
Jeff Leger178b1e52020-10-05 12:22:23 -04003639void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3640 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3641 const VkImageBlit *pRegions, VkFilter filter) {
3642 auto *cb_access_context = GetAccessContext(commandBuffer);
3643 assert(cb_access_context);
3644 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3645 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3646 pRegions, filter);
3647 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3648}
3649
3650void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3651 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3652 auto *cb_access_context = GetAccessContext(commandBuffer);
3653 assert(cb_access_context);
3654 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3655 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3656 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3657 pBlitImageInfo->filter, tag);
3658}
3659
locke-lunarg61870c22020-06-09 14:51:50 -06003660bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
3661 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
3662 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003663 bool skip = false;
3664 if (drawCount == 0) return skip;
3665
3666 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3667 VkDeviceSize size = struct_size;
3668 if (drawCount == 1 || stride == size) {
3669 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003670 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003671 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3672 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003673 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003674 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003675 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003676 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003677 }
3678 } else {
3679 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003680 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003681 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3682 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003683 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003684 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3685 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3686 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003687 break;
3688 }
3689 }
3690 }
3691 return skip;
3692}
3693
locke-lunarg61870c22020-06-09 14:51:50 -06003694void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3695 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3696 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003697 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3698 VkDeviceSize size = struct_size;
3699 if (drawCount == 1 || stride == size) {
3700 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003701 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003702 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3703 } else {
3704 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003705 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003706 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3707 }
3708 }
3709}
3710
locke-lunarg61870c22020-06-09 14:51:50 -06003711bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3712 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003713 bool skip = false;
3714
3715 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003716 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003717 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3718 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003719 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003720 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003721 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003722 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003723 }
3724 return skip;
3725}
3726
locke-lunarg61870c22020-06-09 14:51:50 -06003727void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003728 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003729 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003730 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3731}
3732
locke-lunarg36ba2592020-04-03 09:42:04 -06003733bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003734 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003735 const auto *cb_access_context = GetAccessContext(commandBuffer);
3736 assert(cb_access_context);
3737 if (!cb_access_context) return skip;
3738
locke-lunarg61870c22020-06-09 14:51:50 -06003739 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003740 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003741}
3742
3743void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003744 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003745 auto *cb_access_context = GetAccessContext(commandBuffer);
3746 assert(cb_access_context);
3747 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003748
locke-lunarg61870c22020-06-09 14:51:50 -06003749 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003750}
locke-lunarge1a67022020-04-29 00:15:36 -06003751
3752bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003753 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003754 const auto *cb_access_context = GetAccessContext(commandBuffer);
3755 assert(cb_access_context);
3756 if (!cb_access_context) return skip;
3757
3758 const auto *context = cb_access_context->GetCurrentAccessContext();
3759 assert(context);
3760 if (!context) return skip;
3761
locke-lunarg61870c22020-06-09 14:51:50 -06003762 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3763 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3764 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003765 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003766}
3767
3768void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003769 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003770 auto *cb_access_context = GetAccessContext(commandBuffer);
3771 assert(cb_access_context);
3772 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3773 auto *context = cb_access_context->GetCurrentAccessContext();
3774 assert(context);
3775
locke-lunarg61870c22020-06-09 14:51:50 -06003776 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3777 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003778}
3779
3780bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3781 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003782 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003783 const auto *cb_access_context = GetAccessContext(commandBuffer);
3784 assert(cb_access_context);
3785 if (!cb_access_context) return skip;
3786
locke-lunarg61870c22020-06-09 14:51:50 -06003787 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3788 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3789 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003790 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003791}
3792
3793void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3794 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003795 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003796 auto *cb_access_context = GetAccessContext(commandBuffer);
3797 assert(cb_access_context);
3798 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003799
locke-lunarg61870c22020-06-09 14:51:50 -06003800 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3801 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3802 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003803}
3804
3805bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3806 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003807 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003808 const auto *cb_access_context = GetAccessContext(commandBuffer);
3809 assert(cb_access_context);
3810 if (!cb_access_context) return skip;
3811
locke-lunarg61870c22020-06-09 14:51:50 -06003812 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3813 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3814 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003815 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003816}
3817
3818void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3819 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003820 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003821 auto *cb_access_context = GetAccessContext(commandBuffer);
3822 assert(cb_access_context);
3823 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003824
locke-lunarg61870c22020-06-09 14:51:50 -06003825 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3826 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3827 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003828}
3829
3830bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3831 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003832 bool skip = false;
3833 if (drawCount == 0) return skip;
3834
locke-lunargff255f92020-05-13 18:53:52 -06003835 const auto *cb_access_context = GetAccessContext(commandBuffer);
3836 assert(cb_access_context);
3837 if (!cb_access_context) return skip;
3838
3839 const auto *context = cb_access_context->GetCurrentAccessContext();
3840 assert(context);
3841 if (!context) return skip;
3842
locke-lunarg61870c22020-06-09 14:51:50 -06003843 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3844 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3845 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3846 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003847
3848 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3849 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3850 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003851 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003852 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003853}
3854
3855void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3856 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003857 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003858 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003859 auto *cb_access_context = GetAccessContext(commandBuffer);
3860 assert(cb_access_context);
3861 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3862 auto *context = cb_access_context->GetCurrentAccessContext();
3863 assert(context);
3864
locke-lunarg61870c22020-06-09 14:51:50 -06003865 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3866 cb_access_context->RecordDrawSubpassAttachment(tag);
3867 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003868
3869 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3870 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3871 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003872 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003873}
3874
3875bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3876 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003877 bool skip = false;
3878 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003879 const auto *cb_access_context = GetAccessContext(commandBuffer);
3880 assert(cb_access_context);
3881 if (!cb_access_context) return skip;
3882
3883 const auto *context = cb_access_context->GetCurrentAccessContext();
3884 assert(context);
3885 if (!context) return skip;
3886
locke-lunarg61870c22020-06-09 14:51:50 -06003887 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3888 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3889 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3890 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003891
3892 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3893 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3894 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003895 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003896 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003897}
3898
3899void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3900 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003901 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003902 auto *cb_access_context = GetAccessContext(commandBuffer);
3903 assert(cb_access_context);
3904 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3905 auto *context = cb_access_context->GetCurrentAccessContext();
3906 assert(context);
3907
locke-lunarg61870c22020-06-09 14:51:50 -06003908 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3909 cb_access_context->RecordDrawSubpassAttachment(tag);
3910 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003911
3912 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3913 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3914 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003915 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003916}
3917
3918bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3919 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3920 uint32_t stride, const char *function) const {
3921 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003922 const auto *cb_access_context = GetAccessContext(commandBuffer);
3923 assert(cb_access_context);
3924 if (!cb_access_context) return skip;
3925
3926 const auto *context = cb_access_context->GetCurrentAccessContext();
3927 assert(context);
3928 if (!context) return skip;
3929
locke-lunarg61870c22020-06-09 14:51:50 -06003930 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3931 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3932 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3933 function);
3934 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003935
3936 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3937 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3938 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003939 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003940 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003941}
3942
3943bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3944 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3945 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003946 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3947 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003948}
3949
3950void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3951 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3952 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003953 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3954 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003955 auto *cb_access_context = GetAccessContext(commandBuffer);
3956 assert(cb_access_context);
3957 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3958 auto *context = cb_access_context->GetCurrentAccessContext();
3959 assert(context);
3960
locke-lunarg61870c22020-06-09 14:51:50 -06003961 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3962 cb_access_context->RecordDrawSubpassAttachment(tag);
3963 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3964 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003965
3966 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3967 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3968 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003969 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003970}
3971
3972bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3973 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3974 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003975 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3976 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003977}
3978
3979void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3980 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3981 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003982 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3983 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003984 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003985}
3986
3987bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3988 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3989 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003990 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3991 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003992}
3993
3994void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3995 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3996 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003997 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3998 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003999 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4000}
4001
4002bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4003 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4004 uint32_t stride, const char *function) const {
4005 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004006 const auto *cb_access_context = GetAccessContext(commandBuffer);
4007 assert(cb_access_context);
4008 if (!cb_access_context) return skip;
4009
4010 const auto *context = cb_access_context->GetCurrentAccessContext();
4011 assert(context);
4012 if (!context) return skip;
4013
locke-lunarg61870c22020-06-09 14:51:50 -06004014 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4015 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
4016 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
4017 stride, function);
4018 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004019
4020 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4021 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4022 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004023 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004024 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004025}
4026
4027bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4028 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4029 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004030 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4031 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004032}
4033
4034void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4035 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4036 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004037 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4038 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004039 auto *cb_access_context = GetAccessContext(commandBuffer);
4040 assert(cb_access_context);
4041 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
4042 auto *context = cb_access_context->GetCurrentAccessContext();
4043 assert(context);
4044
locke-lunarg61870c22020-06-09 14:51:50 -06004045 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4046 cb_access_context->RecordDrawSubpassAttachment(tag);
4047 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4048 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004049
4050 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4051 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004052 // We will update the index and vertex buffer in SubmitQueue in the future.
4053 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004054}
4055
4056bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4057 VkDeviceSize offset, VkBuffer countBuffer,
4058 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4059 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004060 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4061 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004062}
4063
4064void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4065 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4066 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004067 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4068 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004069 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4070}
4071
4072bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4073 VkDeviceSize offset, VkBuffer countBuffer,
4074 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4075 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004076 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4077 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004078}
4079
4080void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4081 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4082 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004083 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4084 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004085 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4086}
4087
4088bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4089 const VkClearColorValue *pColor, uint32_t rangeCount,
4090 const VkImageSubresourceRange *pRanges) const {
4091 bool skip = false;
4092 const auto *cb_access_context = GetAccessContext(commandBuffer);
4093 assert(cb_access_context);
4094 if (!cb_access_context) return skip;
4095
4096 const auto *context = cb_access_context->GetCurrentAccessContext();
4097 assert(context);
4098 if (!context) return skip;
4099
4100 const auto *image_state = Get<IMAGE_STATE>(image);
4101
4102 for (uint32_t index = 0; index < rangeCount; index++) {
4103 const auto &range = pRanges[index];
4104 if (image_state) {
4105 auto hazard =
4106 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4107 if (hazard.hazard) {
4108 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004109 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004110 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004111 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004112 }
4113 }
4114 }
4115 return skip;
4116}
4117
4118void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4119 const VkClearColorValue *pColor, uint32_t rangeCount,
4120 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004121 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004122 auto *cb_access_context = GetAccessContext(commandBuffer);
4123 assert(cb_access_context);
4124 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4125 auto *context = cb_access_context->GetCurrentAccessContext();
4126 assert(context);
4127
4128 const auto *image_state = Get<IMAGE_STATE>(image);
4129
4130 for (uint32_t index = 0; index < rangeCount; index++) {
4131 const auto &range = pRanges[index];
4132 if (image_state) {
4133 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4134 tag);
4135 }
4136 }
4137}
4138
4139bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4140 VkImageLayout imageLayout,
4141 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4142 const VkImageSubresourceRange *pRanges) const {
4143 bool skip = false;
4144 const auto *cb_access_context = GetAccessContext(commandBuffer);
4145 assert(cb_access_context);
4146 if (!cb_access_context) return skip;
4147
4148 const auto *context = cb_access_context->GetCurrentAccessContext();
4149 assert(context);
4150 if (!context) return skip;
4151
4152 const auto *image_state = Get<IMAGE_STATE>(image);
4153
4154 for (uint32_t index = 0; index < rangeCount; index++) {
4155 const auto &range = pRanges[index];
4156 if (image_state) {
4157 auto hazard =
4158 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4159 if (hazard.hazard) {
4160 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004161 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004162 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004163 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004164 }
4165 }
4166 }
4167 return skip;
4168}
4169
4170void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4171 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4172 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004173 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004174 auto *cb_access_context = GetAccessContext(commandBuffer);
4175 assert(cb_access_context);
4176 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4177 auto *context = cb_access_context->GetCurrentAccessContext();
4178 assert(context);
4179
4180 const auto *image_state = Get<IMAGE_STATE>(image);
4181
4182 for (uint32_t index = 0; index < rangeCount; index++) {
4183 const auto &range = pRanges[index];
4184 if (image_state) {
4185 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4186 tag);
4187 }
4188 }
4189}
4190
4191bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4192 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4193 VkDeviceSize dstOffset, VkDeviceSize stride,
4194 VkQueryResultFlags flags) const {
4195 bool skip = false;
4196 const auto *cb_access_context = GetAccessContext(commandBuffer);
4197 assert(cb_access_context);
4198 if (!cb_access_context) return skip;
4199
4200 const auto *context = cb_access_context->GetCurrentAccessContext();
4201 assert(context);
4202 if (!context) return skip;
4203
4204 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4205
4206 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004207 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004208 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4209 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004210 skip |=
4211 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4212 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4213 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004214 }
4215 }
locke-lunargff255f92020-05-13 18:53:52 -06004216
4217 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004218 return skip;
4219}
4220
4221void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4222 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4223 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004224 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4225 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004226 auto *cb_access_context = GetAccessContext(commandBuffer);
4227 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004228 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004229 auto *context = cb_access_context->GetCurrentAccessContext();
4230 assert(context);
4231
4232 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4233
4234 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004235 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004236 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4237 }
locke-lunargff255f92020-05-13 18:53:52 -06004238
4239 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004240}
4241
4242bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4243 VkDeviceSize size, uint32_t data) const {
4244 bool skip = false;
4245 const auto *cb_access_context = GetAccessContext(commandBuffer);
4246 assert(cb_access_context);
4247 if (!cb_access_context) return skip;
4248
4249 const auto *context = cb_access_context->GetCurrentAccessContext();
4250 assert(context);
4251 if (!context) return skip;
4252
4253 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4254
4255 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004256 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004257 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4258 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004259 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004260 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004261 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004262 }
4263 }
4264 return skip;
4265}
4266
4267void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4268 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004269 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004270 auto *cb_access_context = GetAccessContext(commandBuffer);
4271 assert(cb_access_context);
4272 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4273 auto *context = cb_access_context->GetCurrentAccessContext();
4274 assert(context);
4275
4276 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4277
4278 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004279 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004280 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4281 }
4282}
4283
4284bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4285 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4286 const VkImageResolve *pRegions) const {
4287 bool skip = false;
4288 const auto *cb_access_context = GetAccessContext(commandBuffer);
4289 assert(cb_access_context);
4290 if (!cb_access_context) return skip;
4291
4292 const auto *context = cb_access_context->GetCurrentAccessContext();
4293 assert(context);
4294 if (!context) return skip;
4295
4296 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4297 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4298
4299 for (uint32_t region = 0; region < regionCount; region++) {
4300 const auto &resolve_region = pRegions[region];
4301 if (src_image) {
4302 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4303 resolve_region.srcOffset, resolve_region.extent);
4304 if (hazard.hazard) {
4305 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004306 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004307 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004308 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004309 }
4310 }
4311
4312 if (dst_image) {
4313 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4314 resolve_region.dstOffset, resolve_region.extent);
4315 if (hazard.hazard) {
4316 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004317 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004318 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004319 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004320 }
4321 if (skip) break;
4322 }
4323 }
4324
4325 return skip;
4326}
4327
4328void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4329 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4330 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004331 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4332 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004333 auto *cb_access_context = GetAccessContext(commandBuffer);
4334 assert(cb_access_context);
4335 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4336 auto *context = cb_access_context->GetCurrentAccessContext();
4337 assert(context);
4338
4339 auto *src_image = Get<IMAGE_STATE>(srcImage);
4340 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4341
4342 for (uint32_t region = 0; region < regionCount; region++) {
4343 const auto &resolve_region = pRegions[region];
4344 if (src_image) {
4345 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4346 resolve_region.srcOffset, resolve_region.extent, tag);
4347 }
4348 if (dst_image) {
4349 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4350 resolve_region.dstOffset, resolve_region.extent, tag);
4351 }
4352 }
4353}
4354
Jeff Leger178b1e52020-10-05 12:22:23 -04004355bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4356 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4357 bool skip = false;
4358 const auto *cb_access_context = GetAccessContext(commandBuffer);
4359 assert(cb_access_context);
4360 if (!cb_access_context) return skip;
4361
4362 const auto *context = cb_access_context->GetCurrentAccessContext();
4363 assert(context);
4364 if (!context) return skip;
4365
4366 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4367 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4368
4369 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4370 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4371 if (src_image) {
4372 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4373 resolve_region.srcOffset, resolve_region.extent);
4374 if (hazard.hazard) {
4375 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4376 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4377 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
4378 region, string_UsageTag(hazard).c_str());
4379 }
4380 }
4381
4382 if (dst_image) {
4383 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4384 resolve_region.dstOffset, resolve_region.extent);
4385 if (hazard.hazard) {
4386 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4387 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4388 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
4389 region, string_UsageTag(hazard).c_str());
4390 }
4391 if (skip) break;
4392 }
4393 }
4394
4395 return skip;
4396}
4397
4398void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4399 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4400 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4401 auto *cb_access_context = GetAccessContext(commandBuffer);
4402 assert(cb_access_context);
4403 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4404 auto *context = cb_access_context->GetCurrentAccessContext();
4405 assert(context);
4406
4407 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4408 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4409
4410 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4411 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4412 if (src_image) {
4413 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4414 resolve_region.srcOffset, resolve_region.extent, tag);
4415 }
4416 if (dst_image) {
4417 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4418 resolve_region.dstOffset, resolve_region.extent, tag);
4419 }
4420 }
4421}
4422
locke-lunarge1a67022020-04-29 00:15:36 -06004423bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4424 VkDeviceSize dataSize, const void *pData) const {
4425 bool skip = false;
4426 const auto *cb_access_context = GetAccessContext(commandBuffer);
4427 assert(cb_access_context);
4428 if (!cb_access_context) return skip;
4429
4430 const auto *context = cb_access_context->GetCurrentAccessContext();
4431 assert(context);
4432 if (!context) return skip;
4433
4434 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4435
4436 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004437 // VK_WHOLE_SIZE not allowed
4438 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004439 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4440 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004441 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004442 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004443 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004444 }
4445 }
4446 return skip;
4447}
4448
4449void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4450 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004451 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004452 auto *cb_access_context = GetAccessContext(commandBuffer);
4453 assert(cb_access_context);
4454 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4455 auto *context = cb_access_context->GetCurrentAccessContext();
4456 assert(context);
4457
4458 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4459
4460 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004461 // VK_WHOLE_SIZE not allowed
4462 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004463 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4464 }
4465}
locke-lunargff255f92020-05-13 18:53:52 -06004466
4467bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4468 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4469 bool skip = false;
4470 const auto *cb_access_context = GetAccessContext(commandBuffer);
4471 assert(cb_access_context);
4472 if (!cb_access_context) return skip;
4473
4474 const auto *context = cb_access_context->GetCurrentAccessContext();
4475 assert(context);
4476 if (!context) return skip;
4477
4478 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4479
4480 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004481 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004482 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4483 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004484 skip |=
4485 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4486 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4487 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004488 }
4489 }
4490 return skip;
4491}
4492
4493void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4494 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004495 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004496 auto *cb_access_context = GetAccessContext(commandBuffer);
4497 assert(cb_access_context);
4498 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4499 auto *context = cb_access_context->GetCurrentAccessContext();
4500 assert(context);
4501
4502 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4503
4504 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004505 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004506 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4507 }
4508}
John Zulauf49beb112020-11-04 16:06:31 -07004509
4510bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
4511 bool skip = false;
4512 const auto *cb_context = GetAccessContext(commandBuffer);
4513 assert(cb_context);
4514 if (!cb_context) return skip;
4515
4516 return cb_context->ValidateSetEvent(commandBuffer, event, stageMask);
4517}
4518
4519void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4520 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
4521 auto *cb_context = GetAccessContext(commandBuffer);
4522 assert(cb_context);
4523 if (!cb_context) return;
4524
4525 cb_context->RecordSetEvent(commandBuffer, event, stageMask);
4526}
4527
4528bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
4529 VkPipelineStageFlags stageMask) const {
4530 bool skip = false;
4531 const auto *cb_context = GetAccessContext(commandBuffer);
4532 assert(cb_context);
4533 if (!cb_context) return skip;
4534
4535 return cb_context->ValidateResetEvent(commandBuffer, event, stageMask);
4536}
4537
4538void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4539 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
4540 auto *cb_context = GetAccessContext(commandBuffer);
4541 assert(cb_context);
4542 if (!cb_context) return;
4543
4544 cb_context->RecordResetEvent(commandBuffer, event, stageMask);
4545}
4546
4547bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4548 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4549 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4550 uint32_t bufferMemoryBarrierCount,
4551 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4552 uint32_t imageMemoryBarrierCount,
4553 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
4554 bool skip = false;
4555 const auto *cb_context = GetAccessContext(commandBuffer);
4556 assert(cb_context);
4557 if (!cb_context) return skip;
4558
4559 return cb_context->ValidateWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
4560 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
4561 pImageMemoryBarriers);
4562}
4563
4564void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4565 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4566 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4567 uint32_t bufferMemoryBarrierCount,
4568 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4569 uint32_t imageMemoryBarrierCount,
4570 const VkImageMemoryBarrier *pImageMemoryBarriers) {
4571 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
4572 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
4573 imageMemoryBarrierCount, pImageMemoryBarriers);
4574
4575 auto *cb_context = GetAccessContext(commandBuffer);
4576 assert(cb_context);
4577 if (!cb_context) return;
4578
4579 cb_context->RecordWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
4580 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
4581 pImageMemoryBarriers);
4582}