blob: 9b570618e0d598263c66f9f41057c567bcab2d4a [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.
1223class ApplyBarrierOpsFunctor {
1224 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001225 using Iterator = ResourceAccessRangeMap::iterator;
1226 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001227
John Zulauf89311b42020-09-29 16:28:47 -06001228 struct BarrierOp {
1229 SyncBarrier barrier;
1230 bool layout_transition;
1231 BarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1232 : barrier(barrier_), layout_transition(layout_transition_) {}
1233 BarrierOp() = default;
1234 };
John Zulauf5c5e88d2019-12-26 11:22:02 -07001235 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001236 auto &access_state = pos->second;
John Zulauf89311b42020-09-29 16:28:47 -06001237 for (const auto op : barrier_ops_) {
1238 access_state.ApplyBarrier(op.barrier, op.layout_transition);
1239 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001240
John Zulauf89311b42020-09-29 16:28:47 -06001241 if (resolve_) {
1242 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1243 // another walk
1244 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001245 }
1246 return pos;
1247 }
1248
John Zulauf89311b42020-09-29 16:28:47 -06001249 // A valid tag is required IFF any of the barriers ops are a layout transition, as transitions are write ops
1250 ApplyBarrierOpsFunctor(bool resolve, size_t size_hint, const ResourceUsageTag &tag)
1251 : resolve_(resolve), barrier_ops_(), tag_(tag) {
1252 if (size_hint) {
1253 barrier_ops_.reserve(size_hint);
1254 }
1255 };
1256
1257 // A valid tag is required IFF layout_transition is true, as transitions are write ops
1258 ApplyBarrierOpsFunctor(bool resolve, const std::vector<SyncBarrier> &barriers, bool layout_transition,
1259 const ResourceUsageTag &tag)
John Zulaufb02c1eb2020-10-06 16:33:36 -06001260 : resolve_(resolve), barrier_ops_(), tag_(tag) {
1261 barrier_ops_.reserve(barriers.size());
John Zulauf89311b42020-09-29 16:28:47 -06001262 for (const auto &barrier : barriers) {
1263 barrier_ops_.emplace_back(barrier, layout_transition);
John Zulauf9cb530d2019-09-30 14:14:10 -06001264 }
1265 }
1266
John Zulauf89311b42020-09-29 16:28:47 -06001267 void PushBack(const SyncBarrier &barrier, bool layout_transition) { barrier_ops_.emplace_back(barrier, layout_transition); }
1268
1269 void PushBack(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
1270 barrier_ops_.reserve(barrier_ops_.size() + barriers.size());
1271 for (const auto &barrier : barriers) {
1272 barrier_ops_.emplace_back(barrier, layout_transition);
1273 }
1274 }
1275
1276 private:
1277 bool resolve_;
1278 std::vector<BarrierOp> barrier_ops_;
1279 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001280};
1281
John Zulauf43cc7462020-12-03 12:33:12 -07001282void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -06001283 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001284 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1285 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001286}
1287
John Zulauf16adfc92020-04-08 10:28:33 -06001288void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001289 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001290 if (!SimpleBinding(buffer)) return;
1291 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf43cc7462020-12-03 12:33:12 -07001292 UpdateAccessState(AccessAddressType::kLinear, current_usage, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001293}
John Zulauf355e49b2020-04-24 15:11:15 -06001294
John Zulauf540266b2020-04-06 18:54:53 -06001295void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001296 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001297 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001298 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001299 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001300 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1301 base_address);
1302 const auto address_type = ImageAddressType(image);
John Zulauf16adfc92020-04-08 10:28:33 -06001303 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001304 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001305 UpdateMemoryAccessState(&GetAccessStateMap(address_type), *range_gen, action);
John Zulauf5f13a792020-03-10 07:31:21 -06001306 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001307}
John Zulauf7635de32020-05-29 17:14:15 -06001308void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1309 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1310 if (view != nullptr) {
1311 const IMAGE_STATE *image = view->image_state.get();
1312 if (image != nullptr) {
1313 auto *update_range = &view->normalized_subresource_range;
1314 VkImageSubresourceRange masked_range;
1315 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1316 masked_range = view->normalized_subresource_range;
1317 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1318 update_range = &masked_range;
1319 }
1320 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1321 }
1322 }
1323}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001324
John Zulauf355e49b2020-04-24 15:11:15 -06001325void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1326 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1327 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001328 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1329 subresource.layerCount};
1330 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1331}
1332
John Zulauf540266b2020-04-06 18:54:53 -06001333template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001334void AccessContext::UpdateResourceAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001335 if (!SimpleBinding(buffer)) return;
1336 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf43cc7462020-12-03 12:33:12 -07001337 UpdateMemoryAccessState(&GetAccessStateMap(AccessAddressType::kLinear), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001338}
1339
1340template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001341void AccessContext::UpdateResourceAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1342 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001343 if (!SimpleBinding(image)) return;
1344 const auto address_type = ImageAddressType(image);
1345 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001346
John Zulauf16adfc92020-04-08 10:28:33 -06001347 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001348 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
1349 image.createInfo.extent, base_address);
1350
John Zulauf540266b2020-04-06 18:54:53 -06001351 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001352 UpdateMemoryAccessState(accesses, *range_gen, action);
John Zulauf540266b2020-04-06 18:54:53 -06001353 }
1354}
1355
John Zulauf7635de32020-05-29 17:14:15 -06001356void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1357 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1358 const ResourceUsageTag &tag) {
1359 UpdateStateResolveAction update(*this, tag);
1360 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1361}
1362
John Zulaufaff20662020-06-01 14:07:58 -06001363void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1364 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1365 const ResourceUsageTag &tag) {
1366 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1367 VkExtent3D extent = CastTo3D(render_area.extent);
1368 VkOffset3D offset = CastTo3D(render_area.offset);
1369
1370 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1371 if (rp_state.attachment_last_subpass[i] == subpass) {
1372 if (attachment_views[i] == nullptr) continue; // UNUSED
1373 const auto &view = *attachment_views[i];
1374 const IMAGE_STATE *image = view.image_state.get();
1375 if (image == nullptr) continue;
1376
1377 const auto &ci = attachment_ci[i];
1378 const bool has_depth = FormatHasDepth(ci.format);
1379 const bool has_stencil = FormatHasStencil(ci.format);
1380 const bool is_color = !(has_depth || has_stencil);
1381 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1382
1383 if (is_color && store_op_stores) {
1384 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1385 offset, extent, tag);
1386 } else {
1387 auto update_range = view.normalized_subresource_range;
1388 if (has_depth && store_op_stores) {
1389 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1390 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1391 tag);
1392 }
1393 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1394 if (has_stencil && stencil_op_stores) {
1395 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1396 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1397 tag);
1398 }
1399 }
1400 }
1401 }
1402}
1403
John Zulauf540266b2020-04-06 18:54:53 -06001404template <typename Action>
1405void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1406 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001407 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001408 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001409 }
1410}
1411
1412void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001413 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1414 auto &context = contexts[subpass_index];
John Zulaufb02c1eb2020-10-06 16:33:36 -06001415 ApplyTrackbackBarriersAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001416 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001417 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001418 }
1419 }
1420}
1421
John Zulauf355e49b2020-04-24 15:11:15 -06001422// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001423HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001424 if (!attach_view) return HazardResult();
1425 const auto image_state = attach_view->image_state.get();
1426 if (!image_state) return HazardResult();
1427
John Zulauf355e49b2020-04-24 15:11:15 -06001428 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001429 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001430
1431 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001432 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1433 const auto merged_barrier = MergeBarriers(track_back.barriers);
1434 HazardResult hazard =
1435 track_back.context->DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
1436 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001437 if (!hazard.hazard) {
1438 // The Async hazard check is against the current context's async set.
John Zulaufa0a98292020-09-18 09:30:10 -06001439 hazard = DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001440 attach_view->normalized_subresource_range, kDetectAsync);
1441 }
John Zulaufa0a98292020-09-18 09:30:10 -06001442
John Zulauf355e49b2020-04-24 15:11:15 -06001443 return hazard;
1444}
1445
John Zulaufb02c1eb2020-10-06 16:33:36 -06001446void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
1447 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1448 const ResourceUsageTag &tag) {
1449 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001450 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001451 for (const auto &transition : transitions) {
1452 const auto prev_pass = transition.prev_pass;
1453 const auto attachment_view = attachment_views[transition.attachment];
1454 if (!attachment_view) continue;
1455 const auto *image = attachment_view->image_state.get();
1456 if (!image) continue;
1457 if (!SimpleBinding(*image)) continue;
1458
1459 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1460 assert(trackback);
1461
1462 // Import the attachments into the current context
1463 const auto *prev_context = trackback->context;
1464 assert(prev_context);
1465 const auto address_type = ImageAddressType(*image);
1466 auto &target_map = GetAccessStateMap(address_type);
1467 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
1468 prev_context->ResolveAccessRange(*image, attachment_view->normalized_subresource_range, barrier_action, address_type,
John Zulauf646cc292020-10-23 09:16:45 -06001469 &target_map, &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001470 }
1471
John Zulauf86356ca2020-10-19 11:46:41 -06001472 // If there were no transitions skip this global map walk
1473 if (transitions.size()) {
1474 ApplyBarrierOpsFunctor apply_pending_action(true /* resolve */, 0, tag);
1475 ApplyGlobalBarriers(apply_pending_action);
1476 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001477}
1478
John Zulauf355e49b2020-04-24 15:11:15 -06001479// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1480bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1481
1482 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001483 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001484 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1485 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06001486
John Zulauf86356ca2020-10-19 11:46:41 -06001487 assert(pRenderPassBegin);
1488 if (nullptr == pRenderPassBegin) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06001489
John Zulauf86356ca2020-10-19 11:46:41 -06001490 const uint32_t subpass = 0;
John Zulauf355e49b2020-04-24 15:11:15 -06001491
John Zulauf86356ca2020-10-19 11:46:41 -06001492 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
1493 // hasn't happened yet)
1494 const std::vector<AccessContext> empty_context_vector;
1495 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1496 const_cast<AccessContext *>(&cb_access_context_));
John Zulauf355e49b2020-04-24 15:11:15 -06001497
John Zulauf86356ca2020-10-19 11:46:41 -06001498 // Create a view list
1499 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1500 assert(fb_state);
1501 if (nullptr == fb_state) return skip;
1502 // NOTE: Must not use COMMAND_BUFFER_STATE variant of this as RecordCmdBeginRenderPass hasn't run and thus
1503 // the activeRenderPass.* fields haven't been set.
1504 const auto views = sync_state_->GetAttachmentViews(*pRenderPassBegin, *fb_state);
1505
1506 // Validate transitions
1507 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
1508
1509 // Validate load operations if there were no layout transition hazards
1510 if (!skip) {
1511 temp_context.RecordLayoutTransitions(rp_state, subpass, views, kCurrentCommandTag);
1512 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001513 }
John Zulauf86356ca2020-10-19 11:46:41 -06001514
John Zulauf355e49b2020-04-24 15:11:15 -06001515 return skip;
1516}
1517
locke-lunarg61870c22020-06-09 14:51:50 -06001518bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1519 const char *func_name) const {
1520 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001521 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001522 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001523 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1524 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001525 return skip;
1526 }
1527
1528 using DescriptorClass = cvdescriptorset::DescriptorClass;
1529 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1530 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1531 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1532 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1533
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001534 for (const auto &stage_state : pipe->stage_state) {
1535 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1536 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001537 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001538 }
locke-lunarg61870c22020-06-09 14:51:50 -06001539 for (const auto &set_binding : stage_state.descriptor_uses) {
1540 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1541 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1542 set_binding.first.second);
1543 const auto descriptor_type = binding_it.GetType();
1544 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1545 auto array_idx = 0;
1546
1547 if (binding_it.IsVariableDescriptorCount()) {
1548 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1549 }
1550 SyncStageAccessIndex sync_index =
1551 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1552
1553 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1554 uint32_t index = i - index_range.start;
1555 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1556 switch (descriptor->GetClass()) {
1557 case DescriptorClass::ImageSampler:
1558 case DescriptorClass::Image: {
1559 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001560 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001561 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001562 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1563 img_view_state = image_sampler_descriptor->GetImageViewState();
1564 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001565 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001566 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1567 img_view_state = image_descriptor->GetImageViewState();
1568 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001569 }
1570 if (!img_view_state) continue;
1571 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1572 VkExtent3D extent = {};
1573 VkOffset3D offset = {};
1574 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1575 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1576 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1577 } else {
1578 extent = img_state->createInfo.extent;
1579 }
John Zulauf361fb532020-07-22 10:45:39 -06001580 HazardResult hazard;
1581 const auto &subresource_range = img_view_state->normalized_subresource_range;
1582 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1583 // Input attachments are subject to raster ordering rules
1584 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
1585 kAttachmentRasterOrder, offset, extent);
1586 } else {
1587 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1588 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001589 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001590 skip |= sync_state_->LogError(
1591 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001592 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1593 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001594 func_name, string_SyncHazard(hazard.hazard),
1595 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1596 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001597 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001598 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1599 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1600 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001601 }
1602 break;
1603 }
1604 case DescriptorClass::TexelBuffer: {
1605 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1606 if (!buf_view_state) continue;
1607 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001608 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001609 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001610 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001611 skip |= sync_state_->LogError(
1612 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001613 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1614 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001615 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1616 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001617 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001618 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1619 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1620 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001621 }
1622 break;
1623 }
1624 case DescriptorClass::GeneralBuffer: {
1625 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1626 auto buf_state = buffer_descriptor->GetBufferState();
1627 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001628 const ResourceAccessRange range =
1629 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001630 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001631 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001632 skip |= sync_state_->LogError(
1633 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001634 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1635 func_name, string_SyncHazard(hazard.hazard),
1636 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001637 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001638 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001639 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1640 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1641 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001642 }
1643 break;
1644 }
1645 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1646 default:
1647 break;
1648 }
1649 }
1650 }
1651 }
1652 return skip;
1653}
1654
1655void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1656 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001657 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001658 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001659 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1660 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001661 return;
1662 }
1663
1664 using DescriptorClass = cvdescriptorset::DescriptorClass;
1665 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1666 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1667 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1668 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1669
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001670 for (const auto &stage_state : pipe->stage_state) {
1671 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1672 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001673 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001674 }
locke-lunarg61870c22020-06-09 14:51:50 -06001675 for (const auto &set_binding : stage_state.descriptor_uses) {
1676 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1677 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1678 set_binding.first.second);
1679 const auto descriptor_type = binding_it.GetType();
1680 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1681 auto array_idx = 0;
1682
1683 if (binding_it.IsVariableDescriptorCount()) {
1684 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1685 }
1686 SyncStageAccessIndex sync_index =
1687 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1688
1689 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1690 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1691 switch (descriptor->GetClass()) {
1692 case DescriptorClass::ImageSampler:
1693 case DescriptorClass::Image: {
1694 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1695 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1696 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1697 } else {
1698 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1699 }
1700 if (!img_view_state) continue;
1701 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1702 VkExtent3D extent = {};
1703 VkOffset3D offset = {};
1704 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1705 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1706 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1707 } else {
1708 extent = img_state->createInfo.extent;
1709 }
1710 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1711 offset, extent, tag);
1712 break;
1713 }
1714 case DescriptorClass::TexelBuffer: {
1715 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1716 if (!buf_view_state) continue;
1717 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001718 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001719 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1720 break;
1721 }
1722 case DescriptorClass::GeneralBuffer: {
1723 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1724 auto buf_state = buffer_descriptor->GetBufferState();
1725 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001726 const ResourceAccessRange range =
1727 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001728 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1729 break;
1730 }
1731 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1732 default:
1733 break;
1734 }
1735 }
1736 }
1737 }
1738}
1739
1740bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1741 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001742 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1743 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06001744 return skip;
1745 }
1746
1747 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1748 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001749 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06001750
1751 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001752 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06001753 if (binding_description.binding < binding_buffers_size) {
1754 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07001755 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001756
locke-lunarg1ae57d62020-11-18 10:49:19 -07001757 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001758 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1759 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001760 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1761 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001762 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001763 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 -06001764 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001765 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001766 }
1767 }
1768 }
1769 return skip;
1770}
1771
1772void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001773 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1774 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06001775 return;
1776 }
1777 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1778 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001779 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06001780
1781 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001782 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06001783 if (binding_description.binding < binding_buffers_size) {
1784 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07001785 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001786
locke-lunarg1ae57d62020-11-18 10:49:19 -07001787 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001788 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1789 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001790 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1791 }
1792 }
1793}
1794
1795bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1796 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001797 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07001798 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001799 }
locke-lunarg61870c22020-06-09 14:51:50 -06001800
locke-lunarg1ae57d62020-11-18 10:49:19 -07001801 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001802 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001803 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1804 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001805 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1806 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001807 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001808 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 -06001809 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001810 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001811 }
1812
1813 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1814 // We will detect more accurate range in the future.
1815 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1816 return skip;
1817}
1818
1819void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07001820 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 -06001821
locke-lunarg1ae57d62020-11-18 10:49:19 -07001822 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001823 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001824 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1825 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001826 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1827
1828 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1829 // We will detect more accurate range in the future.
1830 RecordDrawVertex(UINT32_MAX, 0, tag);
1831}
1832
1833bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06001834 bool skip = false;
1835 if (!current_renderpass_context_) return skip;
1836 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1837 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1838 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001839}
1840
1841void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001842 if (current_renderpass_context_) {
locke-lunarg7077d502020-06-18 21:37:26 -06001843 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1844 tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001845 }
locke-lunarg61870c22020-06-09 14:51:50 -06001846}
1847
John Zulauf355e49b2020-04-24 15:11:15 -06001848bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001849 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001850 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06001851 skip |=
1852 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001853
1854 return skip;
1855}
1856
1857bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1858 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001859 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001860 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001861 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06001862 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1863 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001864
1865 return skip;
1866}
1867
1868void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1869 assert(sync_state_);
1870 if (!cb_state_) return;
1871
1872 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06001873 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06001874 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06001875 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001876 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001877}
1878
John Zulauf355e49b2020-04-24 15:11:15 -06001879void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001880 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001881 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001882 current_context_ = &current_renderpass_context_->CurrentContext();
1883}
1884
John Zulauf355e49b2020-04-24 15:11:15 -06001885void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001886 assert(current_renderpass_context_);
1887 if (!current_renderpass_context_) return;
1888
John Zulauf1a224292020-06-30 14:52:13 -06001889 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001890 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001891 current_renderpass_context_ = nullptr;
1892}
1893
John Zulauf49beb112020-11-04 16:06:31 -07001894bool CommandBufferAccessContext::ValidateSetEvent(VkCommandBuffer commandBuffer, VkEvent event,
1895 VkPipelineStageFlags stageMask) const {
1896 return false;
1897}
1898
1899void CommandBufferAccessContext::RecordSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {}
1900
1901bool CommandBufferAccessContext::ValidateResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
1902 VkPipelineStageFlags stageMask) const {
1903 return false;
1904}
1905
1906void CommandBufferAccessContext::RecordResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {}
1907
1908bool CommandBufferAccessContext::ValidateWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
1909 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
1910 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1911 uint32_t bufferMemoryBarrierCount,
1912 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1913 uint32_t imageMemoryBarrierCount,
1914 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
1915 return false;
1916}
1917
1918void CommandBufferAccessContext::RecordWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
1919 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
1920 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1921 uint32_t bufferMemoryBarrierCount,
1922 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1923 uint32_t imageMemoryBarrierCount,
1924 const VkImageMemoryBarrier *pImageMemoryBarriers) const {}
1925
locke-lunarg61870c22020-06-09 14:51:50 -06001926bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1927 const VkRect2D &render_area, const char *func_name) const {
1928 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001929 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
1930 if (!pipe ||
1931 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001932 return skip;
1933 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001934 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001935 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1936 VkExtent3D extent = CastTo3D(render_area.extent);
1937 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001938
John Zulauf1a224292020-06-30 14:52:13 -06001939 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001940 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001941 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1942 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001943 if (location >= subpass.colorAttachmentCount ||
1944 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001945 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001946 }
locke-lunarg96dc9632020-06-10 17:22:18 -06001947 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001948 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1949 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06001950 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001951 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001952 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001953 func_name, string_SyncHazard(hazard.hazard),
1954 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1955 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001956 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001957 }
1958 }
1959 }
locke-lunarg37047832020-06-12 13:44:45 -06001960
1961 // PHASE1 TODO: Add layout based read/vs. write selection.
1962 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001963 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06001964 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001965 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001966 bool depth_write = false, stencil_write = false;
1967
1968 // PHASE1 TODO: These validation should be in core_checks.
1969 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001970 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1971 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06001972 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1973 depth_write = true;
1974 }
1975 // PHASE1 TODO: It needs to check if stencil is writable.
1976 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1977 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1978 // PHASE1 TODO: These validation should be in core_checks.
1979 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001980 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06001981 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1982 stencil_write = true;
1983 }
1984
1985 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1986 if (depth_write) {
1987 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001988 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1989 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001990 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001991 skip |= sync_state.LogError(
1992 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001993 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001994 func_name, string_SyncHazard(hazard.hazard),
1995 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1996 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001997 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001998 }
1999 }
2000 if (stencil_write) {
2001 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002002 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2003 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002004 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002005 skip |= sync_state.LogError(
2006 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002007 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002008 func_name, string_SyncHazard(hazard.hazard),
2009 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2010 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002011 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002012 }
locke-lunarg61870c22020-06-09 14:51:50 -06002013 }
2014 }
2015 return skip;
2016}
2017
locke-lunarg96dc9632020-06-10 17:22:18 -06002018void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
2019 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002020 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2021 if (!pipe ||
2022 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002023 return;
2024 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002025 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002026 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
2027 VkExtent3D extent = CastTo3D(render_area.extent);
2028 VkOffset3D offset = CastTo3D(render_area.offset);
2029
John Zulauf1a224292020-06-30 14:52:13 -06002030 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002031 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002032 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2033 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002034 if (location >= subpass.colorAttachmentCount ||
2035 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002036 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002037 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002038 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002039 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
2040 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002041 }
2042 }
locke-lunarg37047832020-06-12 13:44:45 -06002043
2044 // PHASE1 TODO: Add layout based read/vs. write selection.
2045 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002046 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002047 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002048 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002049 bool depth_write = false, stencil_write = false;
2050
2051 // PHASE1 TODO: These validation should be in core_checks.
2052 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002053 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2054 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002055 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2056 depth_write = true;
2057 }
2058 // PHASE1 TODO: It needs to check if stencil is writable.
2059 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2060 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2061 // PHASE1 TODO: These validation should be in core_checks.
2062 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002063 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002064 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2065 stencil_write = true;
2066 }
2067
2068 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2069 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002070 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2071 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002072 }
2073 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002074 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2075 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002076 }
locke-lunarg61870c22020-06-09 14:51:50 -06002077 }
2078}
2079
John Zulauf1507ee42020-05-18 11:33:09 -06002080bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
2081 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002082 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002083 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06002084 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2085 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002086 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2087 func_name);
2088
John Zulauf355e49b2020-04-24 15:11:15 -06002089 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002090 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06002091 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002092 if (!skip) {
2093 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2094 // on a copy of the (empty) next context.
2095 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2096 AccessContext temp_context(next_context);
2097 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
2098 skip |= temp_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
2099 }
John Zulauf7635de32020-05-29 17:14:15 -06002100 return skip;
2101}
2102bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
2103 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002104 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002105 bool skip = false;
2106 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2107 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002108 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2109 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06002110 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002111 return skip;
2112}
2113
John Zulauf7635de32020-05-29 17:14:15 -06002114AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2115 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2116}
2117
2118bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2119 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002120 bool skip = false;
2121
John Zulauf7635de32020-05-29 17:14:15 -06002122 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2123 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2124 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2125 // to apply and only copy then, if this proves a hot spot.
2126 std::unique_ptr<AccessContext> proxy_for_current;
2127
John Zulauf355e49b2020-04-24 15:11:15 -06002128 // Validate the "finalLayout" transitions to external
2129 // Get them from where there we're hidding in the extra entry.
2130 const auto &final_transitions = rp_state_->subpass_transitions.back();
2131 for (const auto &transition : final_transitions) {
2132 const auto &attach_view = attachment_views_[transition.attachment];
2133 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2134 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002135 auto *context = trackback.context;
2136
2137 if (transition.prev_pass == current_subpass_) {
2138 if (!proxy_for_current) {
2139 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2140 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2141 }
2142 context = proxy_for_current.get();
2143 }
2144
John Zulaufa0a98292020-09-18 09:30:10 -06002145 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2146 const auto merged_barrier = MergeBarriers(trackback.barriers);
2147 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope,
2148 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2149 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002150 if (hazard.hazard) {
2151 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2152 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf389c34b2020-07-28 11:19:35 -06002153 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002154 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf389c34b2020-07-28 11:19:35 -06002155 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf37ceaed2020-07-03 16:18:15 -06002156 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002157 }
2158 }
2159 return skip;
2160}
2161
2162void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2163 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002164 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002165}
2166
John Zulauf1507ee42020-05-18 11:33:09 -06002167void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2168 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2169 auto &subpass_context = subpass_contexts_[current_subpass_];
2170 VkExtent3D extent = CastTo3D(render_area.extent);
2171 VkOffset3D offset = CastTo3D(render_area.offset);
2172
2173 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2174 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2175 if (attachment_views_[i] == nullptr) continue; // UNUSED
2176 const auto &view = *attachment_views_[i];
2177 const IMAGE_STATE *image = view.image_state.get();
2178 if (image == nullptr) continue;
2179
2180 const auto &ci = attachment_ci[i];
2181 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002182 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002183 const bool is_color = !(has_depth || has_stencil);
2184
2185 if (is_color) {
2186 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2187 extent, tag);
2188 } else {
2189 auto update_range = view.normalized_subresource_range;
2190 if (has_depth) {
2191 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2192 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2193 }
2194 if (has_stencil) {
2195 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2196 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2197 tag);
2198 }
2199 }
2200 }
2201 }
2202}
2203
John Zulauf355e49b2020-04-24 15:11:15 -06002204void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002205 const AccessContext *external_context, VkQueueFlags queue_flags,
2206 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002207 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002208 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002209 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2210 // Add this for all subpasses here so that they exsist during next subpass validation
2211 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002212 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002213 }
2214 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2215
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002216 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002217 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002218 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002219}
John Zulauf1507ee42020-05-18 11:33:09 -06002220
2221void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002222 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2223 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002224 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002225
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002226 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2227 // subpass, so their tag needs to be different from the layout and load operations below.
2228 ResourceUsageTag next_tag = tag;
2229 next_tag.index++;
John Zulauf355e49b2020-04-24 15:11:15 -06002230 current_subpass_++;
2231 assert(current_subpass_ < subpass_contexts_.size());
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002232 subpass_contexts_[current_subpass_].SetStartTag(next_tag);
2233 RecordLayoutTransitions(next_tag);
2234 RecordLoadOperations(render_area, next_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002235}
2236
John Zulauf1a224292020-06-30 14:52:13 -06002237void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2238 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002239 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002240 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002241 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002242
John Zulauf355e49b2020-04-24 15:11:15 -06002243 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002244 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002245
2246 // Add the "finalLayout" transitions to external
2247 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002248 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2249 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2250 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002251 const auto &final_transitions = rp_state_->subpass_transitions.back();
2252 for (const auto &transition : final_transitions) {
2253 const auto &attachment = attachment_views_[transition.attachment];
2254 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002255 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf89311b42020-09-29 16:28:47 -06002256 ApplyBarrierOpsFunctor barrier_ops(true /* resolve */, last_trackback.barriers, true /* layout transition */, tag);
2257 external_context->UpdateResourceAccess(*attachment->image_state, attachment->normalized_subresource_range, barrier_ops);
John Zulauf355e49b2020-04-24 15:11:15 -06002258 }
2259}
2260
John Zulauf3d84f1b2020-03-09 13:33:25 -06002261SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2262 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2263 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2264 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2265 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2266 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2267 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2268}
2269
John Zulaufb02c1eb2020-10-06 16:33:36 -06002270// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2271void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2272 for (const auto &barrier : barriers) {
2273 ApplyBarrier(barrier, layout_transition);
2274 }
2275}
2276
John Zulauf89311b42020-09-29 16:28:47 -06002277// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2278// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2279// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002280void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2281 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002282 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002283 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002284 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002285 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002286 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002287 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002288}
John Zulauf9cb530d2019-09-30 14:14:10 -06002289HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2290 HazardResult hazard;
2291 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002292 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002293 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002294 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002295 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002296 }
2297 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002298 // Write operation:
2299 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2300 // If reads exists -- test only against them because either:
2301 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2302 // * 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
2303 // the current write happens after the reads, so just test the write against the reades
2304 // Otherwise test against last_write
2305 //
2306 // Look for casus belli for WAR
2307 if (last_read_count) {
2308 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2309 const auto &read_access = last_reads[read_index];
2310 if (IsReadHazard(usage_stage, read_access)) {
2311 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2312 break;
2313 }
2314 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002315 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002316 // Write-After-Write check -- if we have a previous write to test against
2317 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002318 }
2319 }
2320 return hazard;
2321}
2322
John Zulauf69133422020-05-20 14:55:53 -06002323HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2324 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2325 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002326 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002327 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002328 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2329 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002330 if (IsRead(usage_bit)) {
2331 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2332 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2333 if (is_raw_hazard) {
2334 // NOTE: we know last_write is non-zero
2335 // See if the ordering rules save us from the simple RAW check above
2336 // First check to see if the current usage is covered by the ordering rules
2337 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2338 const bool usage_is_ordered =
2339 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2340 if (usage_is_ordered) {
2341 // Now see of the most recent write (or a subsequent read) are ordered
2342 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2343 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002344 }
2345 }
John Zulauf4285ee92020-09-23 10:20:52 -06002346 if (is_raw_hazard) {
2347 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2348 }
John Zulauf361fb532020-07-22 10:45:39 -06002349 } else {
2350 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002351 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulauf361fb532020-07-22 10:45:39 -06002352 if (last_read_count) {
John Zulauf361fb532020-07-22 10:45:39 -06002353 // Look for any WAR hazards outside the ordered set of stages
John Zulauf4285ee92020-09-23 10:20:52 -06002354 VkPipelineStageFlags ordered_stages = 0;
2355 if (usage_write_is_ordered) {
2356 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2357 ordered_stages = GetOrderedStages(ordering);
2358 }
2359 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2360 if ((ordered_stages & last_read_stages) != last_read_stages) {
2361 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2362 const auto &read_access = last_reads[read_index];
2363 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2364 if (IsReadHazard(usage_stage, read_access)) {
2365 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2366 break;
2367 }
John Zulaufd14743a2020-07-03 09:42:39 -06002368 }
2369 }
John Zulauf4285ee92020-09-23 10:20:52 -06002370 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002371 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002372 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002373 }
John Zulauf69133422020-05-20 14:55:53 -06002374 }
2375 }
2376 return hazard;
2377}
2378
John Zulauf2f952d22020-02-10 11:34:51 -07002379// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002380HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002381 HazardResult hazard;
2382 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002383 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2384 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2385 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002386 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002387 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002388 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002389 }
2390 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002391 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002392 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002393 } else if (last_read_count > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002394 // Any reads during the other subpass will conflict with this write, so we need to check them all.
2395 for (uint32_t i = 0; i < last_read_count; i++) {
2396 if (last_reads[i].tag.index >= start_tag.index) {
2397 hazard.Set(this, usage_index, WRITE_RACING_READ, last_reads[i].access, last_reads[i].tag);
2398 break;
2399 }
2400 }
John Zulauf2f952d22020-02-10 11:34:51 -07002401 }
2402 }
2403 return hazard;
2404}
2405
John Zulauf36bcf6a2020-02-03 15:12:52 -07002406HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002407 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002408 // Only supporting image layout transitions for now
2409 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2410 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002411 // only test for WAW if there no intervening read operations.
2412 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2413 if (last_read_count) {
John Zulauf355e49b2020-04-24 15:11:15 -06002414 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002415 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002416 const auto &read_access = last_reads[read_index];
2417 // If the read stage is not in the src sync sync
2418 // *AND* not execution chained with an existing sync barrier (that's the or)
2419 // then the barrier access is unsafe (R/W after R)
2420 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002421 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002422 break;
2423 }
2424 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002425 } else if (last_write.any()) {
John Zulauf361fb532020-07-22 10:45:39 -06002426 // If the previous write is *not* in the 1st access scope
2427 // *AND* the current barrier is not in the dependency chain
2428 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2429 // then the barrier access is unsafe (R/W after W)
2430 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
2431 // TODO: Do we need a difference hazard name for this?
2432 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2433 }
John Zulaufd14743a2020-07-03 09:42:39 -06002434 }
John Zulauf361fb532020-07-22 10:45:39 -06002435
John Zulauf0cb5be22020-01-23 12:18:22 -07002436 return hazard;
2437}
2438
John Zulauf5f13a792020-03-10 07:31:21 -06002439// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2440// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2441// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2442void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2443 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002444 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2445 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002446 *this = other;
2447 } else if (!other.write_tag.IsBefore(write_tag)) {
2448 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2449 // dependency chaining logic or any stage expansion)
2450 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002451 pending_write_barriers |= other.pending_write_barriers;
2452 pending_layout_transition |= other.pending_layout_transition;
2453 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002454
John Zulaufd14743a2020-07-03 09:42:39 -06002455 // Merge the read states
John Zulauf4285ee92020-09-23 10:20:52 -06002456 const auto pre_merge_count = last_read_count;
2457 const auto pre_merge_stages = last_read_stages;
John Zulauf5f13a792020-03-10 07:31:21 -06002458 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2459 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002460 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002461 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002462 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2463 // but we should wait on profiling data for that.
2464 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002465 auto &my_read = last_reads[my_read_index];
2466 if (other_read.stage == my_read.stage) {
2467 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002468 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002469 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002470 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002471 my_read.pending_dep_chain = other_read.pending_dep_chain;
2472 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
2473 // May require tracking more than one access per stage.
2474 my_read.barriers = other_read.barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002475 if (my_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
2476 // Since I'm overwriting the fragement stage read, also update the input attachment info
2477 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06002478 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002479 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002480 } else if (other_read.tag.IsBefore(my_read.tag)) {
2481 // The read tags match so merge the barriers
2482 my_read.barriers |= other_read.barriers;
2483 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002484 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002485
John Zulauf5f13a792020-03-10 07:31:21 -06002486 break;
2487 }
2488 }
2489 } else {
2490 // The other read stage doesn't exist in this, so add it.
2491 last_reads[last_read_count] = other_read;
2492 last_read_count++;
2493 last_read_stages |= other_read.stage;
John Zulauf4285ee92020-09-23 10:20:52 -06002494 if (other_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06002495 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002496 }
John Zulauf5f13a792020-03-10 07:31:21 -06002497 }
2498 }
John Zulauf361fb532020-07-22 10:45:39 -06002499 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002500 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2501 // ignore it.
John Zulauf5f13a792020-03-10 07:31:21 -06002502}
2503
John Zulauf9cb530d2019-09-30 14:14:10 -06002504void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2505 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2506 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002507 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002508 // Mulitple outstanding reads may be of interest and do dependency chains independently
2509 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2510 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002511 uint32_t update_index = kStageCount;
John Zulauf9cb530d2019-09-30 14:14:10 -06002512 if (usage_stage & last_read_stages) {
2513 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf4285ee92020-09-23 10:20:52 -06002514 if (last_reads[read_index].stage == usage_stage) {
2515 update_index = read_index;
John Zulauf9cb530d2019-09-30 14:14:10 -06002516 break;
2517 }
2518 }
John Zulauf4285ee92020-09-23 10:20:52 -06002519 assert(update_index < last_read_count);
John Zulauf9cb530d2019-09-30 14:14:10 -06002520 } else {
John Zulauf9cb530d2019-09-30 14:14:10 -06002521 assert(last_read_count < last_reads.size());
John Zulauf4285ee92020-09-23 10:20:52 -06002522 update_index = last_read_count++;
John Zulauf9cb530d2019-09-30 14:14:10 -06002523 last_read_stages |= usage_stage;
2524 }
John Zulauf4285ee92020-09-23 10:20:52 -06002525 last_reads[update_index].Set(usage_stage, usage_bit, 0, tag);
2526
2527 // Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
2528 if (usage_stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06002529 // TODO Revisit re: multiple reads for a given stage
2530 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002531 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002532 } else {
2533 // Assume write
2534 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002535 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002536 }
2537}
John Zulauf5f13a792020-03-10 07:31:21 -06002538
John Zulauf89311b42020-09-29 16:28:47 -06002539// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2540// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2541// We can overwrite them as *this* write is now after them.
2542//
2543// 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 -07002544void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulauf89311b42020-09-29 16:28:47 -06002545 last_read_count = 0;
2546 last_read_stages = 0;
2547 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06002548 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06002549
2550 write_barriers = 0;
2551 write_dependency_chain = 0;
2552 write_tag = tag;
2553 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002554}
2555
John Zulauf89311b42020-09-29 16:28:47 -06002556// Apply the memory barrier without updating the existing barriers. The execution barrier
2557// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2558// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2559// replace the current write barriers or add to them, so accumulate to pending as well.
2560void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2561 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2562 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06002563 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
2564 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
2565 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
2566 // transistion *as* a write and in scope with the barrier (it's before visibility).
2567 if (layout_transition || InSourceScopeOrChain(barrier.src_exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06002568 pending_write_barriers |= barrier.dst_access_scope;
2569 pending_write_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002570 }
John Zulauf89311b42020-09-29 16:28:47 -06002571 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2572 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06002573
John Zulauf89311b42020-09-29 16:28:47 -06002574 if (!pending_layout_transition) {
2575 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2576 // don't need to be tracked as we're just going to zero them.
John Zulaufa0a98292020-09-18 09:30:10 -06002577 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf89311b42020-09-29 16:28:47 -06002578 ReadState &access = last_reads[read_index];
2579 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2580 if (barrier.src_exec_scope & (access.stage | access.barriers)) {
2581 access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002582 }
2583 }
John Zulaufa0a98292020-09-18 09:30:10 -06002584 }
John Zulaufa0a98292020-09-18 09:30:10 -06002585}
2586
John Zulauf89311b42020-09-29 16:28:47 -06002587void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
2588 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06002589 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
2590 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
2591 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06002592 }
John Zulauf89311b42020-09-29 16:28:47 -06002593
2594 // Apply the accumulate execution barriers (and thus update chaining information)
2595 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
2596 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2597 ReadState &access = last_reads[read_index];
2598 access.barriers |= access.pending_dep_chain;
2599 read_execution_barriers |= access.barriers;
2600 access.pending_dep_chain = 0;
2601 }
2602
2603 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
2604 write_dependency_chain |= pending_write_dep_chain;
2605 write_barriers |= pending_write_barriers;
2606 pending_write_dep_chain = 0;
2607 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002608}
2609
John Zulauf59e25072020-07-17 10:55:21 -06002610// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002611VkPipelineStageFlags ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
John Zulauf59e25072020-07-17 10:55:21 -06002612 VkPipelineStageFlags barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06002613
2614 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2615 const auto &read_access = last_reads[read_index];
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002616 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06002617 barriers = read_access.barriers;
2618 break;
John Zulauf59e25072020-07-17 10:55:21 -06002619 }
2620 }
John Zulauf4285ee92020-09-23 10:20:52 -06002621
John Zulauf59e25072020-07-17 10:55:21 -06002622 return barriers;
2623}
2624
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002625inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlagBits usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06002626 assert(IsRead(usage));
2627 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
2628 // * the previous reads are not hazards, and thus last_write must be visible and available to
2629 // any reads that happen after.
2630 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2631 // 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 -07002632 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06002633}
2634
John Zulauf4285ee92020-09-23 10:20:52 -06002635VkPipelineStageFlags ResourceAccessState::GetOrderedStages(const SyncOrderingBarrier &ordering) const {
2636 // Whether the stage are in the ordering scope only matters if the current write is ordered
2637 VkPipelineStageFlags ordered_stages = last_read_stages & ordering.exec_scope;
2638 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002639 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06002640 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06002641 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
2642 ordered_stages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
2643 }
2644
2645 return ordered_stages;
2646}
2647
2648inline ResourceAccessState::ReadState *ResourceAccessState::GetReadStateForStage(VkPipelineStageFlagBits stage,
2649 uint32_t search_limit) {
2650 ReadState *read_state = nullptr;
2651 search_limit = std::min(search_limit, last_read_count);
2652 for (uint32_t i = 0; i < search_limit; i++) {
2653 if (last_reads[i].stage == stage) {
2654 read_state = &last_reads[i];
2655 break;
2656 }
2657 }
2658 return read_state;
2659}
2660
John Zulaufd1f85d42020-04-15 12:23:15 -06002661void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002662 auto *access_context = GetAccessContextNoInsert(command_buffer);
2663 if (access_context) {
2664 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002665 }
2666}
2667
John Zulaufd1f85d42020-04-15 12:23:15 -06002668void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2669 auto access_found = cb_access_state.find(command_buffer);
2670 if (access_found != cb_access_state.end()) {
2671 access_found->second->Reset();
2672 cb_access_state.erase(access_found);
2673 }
2674}
2675
John Zulauf89311b42020-09-29 16:28:47 -06002676void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2677 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags src_access_scope,
2678 SyncStageAccessFlags dst_access_scope, uint32_t memory_barrier_count,
2679 const VkMemoryBarrier *pMemoryBarriers, const ResourceUsageTag &tag) {
2680 ApplyBarrierOpsFunctor barriers_functor(true /* resolve */, std::min<uint32_t>(1, memory_barrier_count), tag);
2681 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
2682 const auto &barrier = pMemoryBarriers[barrier_index];
2683 SyncBarrier sync_barrier(src_exec_scope, SyncStageAccess::AccessScope(src_access_scope, barrier.srcAccessMask),
2684 dst_exec_scope, SyncStageAccess::AccessScope(dst_access_scope, barrier.dstAccessMask));
2685 barriers_functor.PushBack(sync_barrier, false);
2686 }
2687 if (0 == memory_barrier_count) {
2688 // If there are no global memory barriers, force an exec barrier
2689 barriers_functor.PushBack(SyncBarrier(src_exec_scope, 0, dst_exec_scope, 0), false);
2690 }
John Zulauf540266b2020-04-06 18:54:53 -06002691 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002692}
2693
John Zulauf540266b2020-04-06 18:54:53 -06002694void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002695 const SyncStageAccessFlags &src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2696 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002697 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002698 for (uint32_t index = 0; index < barrier_count; index++) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002699 auto barrier = barriers[index]; // barrier is a copy
John Zulauf9cb530d2019-09-30 14:14:10 -06002700 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2701 if (!buffer) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002702 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
2703 const ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002704 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2705 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06002706 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2707 const ApplyBarrierFunctor update_action(sync_barrier, false /* layout_transition */);
2708 context->UpdateResourceAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002709 }
2710}
2711
John Zulauf540266b2020-04-06 18:54:53 -06002712void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002713 const SyncStageAccessFlags &src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2714 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002715 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002716 for (uint32_t index = 0; index < barrier_count; index++) {
2717 const auto &barrier = barriers[index];
2718 const auto *image = Get<IMAGE_STATE>(barrier.image);
2719 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002720 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002721 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2722 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2723 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06002724 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2725 const ApplyBarrierFunctor barrier_action(sync_barrier, layout_transition);
2726 context->UpdateResourceAccess(*image, subresource_range, barrier_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002727 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002728}
2729
2730bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2731 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2732 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002733 const auto *cb_context = GetAccessContext(commandBuffer);
2734 assert(cb_context);
2735 if (!cb_context) return skip;
2736 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002737
John Zulauf3d84f1b2020-03-09 13:33:25 -06002738 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002739 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002740 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002741
2742 for (uint32_t region = 0; region < regionCount; region++) {
2743 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002744 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002745 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002746 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002747 if (hazard.hazard) {
2748 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002749 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002750 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002751 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002752 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002753 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002754 }
John Zulauf16adfc92020-04-08 10:28:33 -06002755 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002756 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06002757 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002758 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002759 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002760 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002761 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002762 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002763 }
2764 }
2765 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002766 }
2767 return skip;
2768}
2769
2770void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2771 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002772 auto *cb_context = GetAccessContext(commandBuffer);
2773 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002774 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002775 auto *context = cb_context->GetCurrentAccessContext();
2776
John Zulauf9cb530d2019-09-30 14:14:10 -06002777 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002778 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002779
2780 for (uint32_t region = 0; region < regionCount; region++) {
2781 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002782 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002783 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002784 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002785 }
John Zulauf16adfc92020-04-08 10:28:33 -06002786 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002787 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002788 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002789 }
2790 }
2791}
2792
Jeff Leger178b1e52020-10-05 12:22:23 -04002793bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
2794 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
2795 bool skip = false;
2796 const auto *cb_context = GetAccessContext(commandBuffer);
2797 assert(cb_context);
2798 if (!cb_context) return skip;
2799 const auto *context = cb_context->GetCurrentAccessContext();
2800
2801 // If we have no previous accesses, we have no hazards
2802 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2803 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2804
2805 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2806 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2807 if (src_buffer) {
2808 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2809 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
2810 if (hazard.hazard) {
2811 // TODO -- add tag information to log msg when useful.
2812 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
2813 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
2814 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
2815 region, string_UsageTag(hazard).c_str());
2816 }
2817 }
2818 if (dst_buffer && !skip) {
2819 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2820 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
2821 if (hazard.hazard) {
2822 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
2823 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
2824 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
2825 region, string_UsageTag(hazard).c_str());
2826 }
2827 }
2828 if (skip) break;
2829 }
2830 return skip;
2831}
2832
2833void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
2834 auto *cb_context = GetAccessContext(commandBuffer);
2835 assert(cb_context);
2836 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
2837 auto *context = cb_context->GetCurrentAccessContext();
2838
2839 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2840 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2841
2842 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2843 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2844 if (src_buffer) {
2845 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2846 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
2847 }
2848 if (dst_buffer) {
2849 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2850 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
2851 }
2852 }
2853}
2854
John Zulauf5c5e88d2019-12-26 11:22:02 -07002855bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2856 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2857 const VkImageCopy *pRegions) const {
2858 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002859 const auto *cb_access_context = GetAccessContext(commandBuffer);
2860 assert(cb_access_context);
2861 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002862
John Zulauf3d84f1b2020-03-09 13:33:25 -06002863 const auto *context = cb_access_context->GetCurrentAccessContext();
2864 assert(context);
2865 if (!context) return skip;
2866
2867 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2868 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002869 for (uint32_t region = 0; region < regionCount; region++) {
2870 const auto &copy_region = pRegions[region];
2871 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002872 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002873 copy_region.srcOffset, copy_region.extent);
2874 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002875 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002876 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002877 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002878 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002879 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002880 }
2881
2882 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002883 VkExtent3D dst_copy_extent =
2884 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002885 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002886 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002887 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002888 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002889 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002890 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002891 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002892 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002893 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002894 }
2895 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002896
John Zulauf5c5e88d2019-12-26 11:22:02 -07002897 return skip;
2898}
2899
2900void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2901 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2902 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002903 auto *cb_access_context = GetAccessContext(commandBuffer);
2904 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002905 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002906 auto *context = cb_access_context->GetCurrentAccessContext();
2907 assert(context);
2908
John Zulauf5c5e88d2019-12-26 11:22:02 -07002909 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002910 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002911
2912 for (uint32_t region = 0; region < regionCount; region++) {
2913 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002914 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002915 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2916 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002917 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002918 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002919 VkExtent3D dst_copy_extent =
2920 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002921 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2922 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002923 }
2924 }
2925}
2926
Jeff Leger178b1e52020-10-05 12:22:23 -04002927bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
2928 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
2929 bool skip = false;
2930 const auto *cb_access_context = GetAccessContext(commandBuffer);
2931 assert(cb_access_context);
2932 if (!cb_access_context) return skip;
2933
2934 const auto *context = cb_access_context->GetCurrentAccessContext();
2935 assert(context);
2936 if (!context) return skip;
2937
2938 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2939 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2940 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2941 const auto &copy_region = pCopyImageInfo->pRegions[region];
2942 if (src_image) {
2943 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
2944 copy_region.srcOffset, copy_region.extent);
2945 if (hazard.hazard) {
2946 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
2947 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
2948 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
2949 region, string_UsageTag(hazard).c_str());
2950 }
2951 }
2952
2953 if (dst_image) {
2954 VkExtent3D dst_copy_extent =
2955 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2956 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
2957 copy_region.dstOffset, dst_copy_extent);
2958 if (hazard.hazard) {
2959 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
2960 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
2961 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
2962 region, string_UsageTag(hazard).c_str());
2963 }
2964 if (skip) break;
2965 }
2966 }
2967
2968 return skip;
2969}
2970
2971void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
2972 auto *cb_access_context = GetAccessContext(commandBuffer);
2973 assert(cb_access_context);
2974 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
2975 auto *context = cb_access_context->GetCurrentAccessContext();
2976 assert(context);
2977
2978 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2979 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2980
2981 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2982 const auto &copy_region = pCopyImageInfo->pRegions[region];
2983 if (src_image) {
2984 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2985 copy_region.extent, tag);
2986 }
2987 if (dst_image) {
2988 VkExtent3D dst_copy_extent =
2989 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2990 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2991 dst_copy_extent, tag);
2992 }
2993 }
2994}
2995
John Zulauf9cb530d2019-09-30 14:14:10 -06002996bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2997 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2998 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2999 uint32_t bufferMemoryBarrierCount,
3000 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3001 uint32_t imageMemoryBarrierCount,
3002 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3003 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003004 const auto *cb_access_context = GetAccessContext(commandBuffer);
3005 assert(cb_access_context);
3006 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003007
John Zulauf3d84f1b2020-03-09 13:33:25 -06003008 const auto *context = cb_access_context->GetCurrentAccessContext();
3009 assert(context);
3010 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003011
John Zulauf3d84f1b2020-03-09 13:33:25 -06003012 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003013 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
3014 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07003015 // Validate Image Layout transitions
3016 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
3017 const auto &barrier = pImageMemoryBarriers[index];
3018 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
3019 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
3020 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06003021 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07003022 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003023 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003024 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003025 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003026 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003027 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07003028 }
3029 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003030
3031 return skip;
3032}
3033
3034void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3035 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3036 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3037 uint32_t bufferMemoryBarrierCount,
3038 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3039 uint32_t imageMemoryBarrierCount,
3040 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003041 auto *cb_access_context = GetAccessContext(commandBuffer);
3042 assert(cb_access_context);
3043 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06003044 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003045 auto access_context = cb_access_context->GetCurrentAccessContext();
3046 assert(access_context);
3047 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003048
John Zulauf3d84f1b2020-03-09 13:33:25 -06003049 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003050 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003051 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003052 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
3053 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
3054 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf89311b42020-09-29 16:28:47 -06003055
3056 // These two apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
3057 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
3058 // of the barriers is maintained.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003059 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
3060 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06003061 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06003062 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003063
John Zulauf89311b42020-09-29 16:28:47 -06003064 // Apply the global barriers last as is it walks all memory, it can also clean up the "pending" state without requiring an
3065 // additional pass, updating the dependency chains *last* as it goes along.
3066 // This is needed to guarantee order independence of the three lists.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003067 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf89311b42020-09-29 16:28:47 -06003068 pMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003069}
3070
3071void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3072 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3073 // The state tracker sets up the device state
3074 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3075
John Zulauf5f13a792020-03-10 07:31:21 -06003076 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3077 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003078 // TODO: Find a good way to do this hooklessly.
3079 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3080 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3081 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3082
John Zulaufd1f85d42020-04-15 12:23:15 -06003083 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3084 sync_device_state->ResetCommandBufferCallback(command_buffer);
3085 });
3086 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3087 sync_device_state->FreeCommandBufferCallback(command_buffer);
3088 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003089}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003090
John Zulauf355e49b2020-04-24 15:11:15 -06003091bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003092 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003093 bool skip = false;
3094 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
3095 auto cb_context = GetAccessContext(commandBuffer);
3096
3097 if (rp_state && cb_context) {
3098 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
3099 }
3100
3101 return skip;
3102}
3103
3104bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3105 VkSubpassContents contents) const {
3106 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3107 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3108 subpass_begin_info.contents = contents;
3109 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
3110 return skip;
3111}
3112
3113bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003114 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003115 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3116 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
3117 return skip;
3118}
3119
3120bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3121 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003122 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003123 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3124 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
3125 return skip;
3126}
3127
John Zulauf3d84f1b2020-03-09 13:33:25 -06003128void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3129 VkResult result) {
3130 // The state tracker sets up the command buffer state
3131 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3132
3133 // Create/initialize the structure that trackers accesses at the command buffer scope.
3134 auto cb_access_context = GetAccessContext(commandBuffer);
3135 assert(cb_access_context);
3136 cb_access_context->Reset();
3137}
3138
3139void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06003140 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003141 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003142 if (cb_context) {
3143 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003144 }
3145}
3146
3147void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3148 VkSubpassContents contents) {
3149 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3150 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3151 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003152 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003153}
3154
3155void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3156 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3157 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003158 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003159}
3160
3161void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3162 const VkRenderPassBeginInfo *pRenderPassBegin,
3163 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3164 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003165 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
3166}
3167
Mike Schuchardt2df08912020-12-15 16:28:09 -08003168bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3169 const VkSubpassEndInfo *pSubpassEndInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003170 bool skip = false;
3171
3172 auto cb_context = GetAccessContext(commandBuffer);
3173 assert(cb_context);
3174 auto cb_state = cb_context->GetCommandBufferState();
3175 if (!cb_state) return skip;
3176
3177 auto rp_state = cb_state->activeRenderPass;
3178 if (!rp_state) return skip;
3179
3180 skip |= cb_context->ValidateNextSubpass(func_name);
3181
3182 return skip;
3183}
3184
3185bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3186 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
3187 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3188 subpass_begin_info.contents = contents;
3189 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
3190 return skip;
3191}
3192
Mike Schuchardt2df08912020-12-15 16:28:09 -08003193bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3194 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003195 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3196 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
3197 return skip;
3198}
3199
3200bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3201 const VkSubpassEndInfo *pSubpassEndInfo) const {
3202 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3203 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
3204 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003205}
3206
3207void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003208 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003209 auto cb_context = GetAccessContext(commandBuffer);
3210 assert(cb_context);
3211 auto cb_state = cb_context->GetCommandBufferState();
3212 if (!cb_state) return;
3213
3214 auto rp_state = cb_state->activeRenderPass;
3215 if (!rp_state) return;
3216
John Zulauf355e49b2020-04-24 15:11:15 -06003217 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003218}
3219
3220void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3221 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
3222 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3223 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003224 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003225}
3226
3227void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3228 const VkSubpassEndInfo *pSubpassEndInfo) {
3229 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003230 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003231}
3232
3233void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3234 const VkSubpassEndInfo *pSubpassEndInfo) {
3235 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003236 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003237}
3238
Mike Schuchardt2df08912020-12-15 16:28:09 -08003239bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003240 const char *func_name) const {
3241 bool skip = false;
3242
3243 auto cb_context = GetAccessContext(commandBuffer);
3244 assert(cb_context);
3245 auto cb_state = cb_context->GetCommandBufferState();
3246 if (!cb_state) return skip;
3247
3248 auto rp_state = cb_state->activeRenderPass;
3249 if (!rp_state) return skip;
3250
3251 skip |= cb_context->ValidateEndRenderpass(func_name);
3252 return skip;
3253}
3254
3255bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3256 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
3257 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
3258 return skip;
3259}
3260
Mike Schuchardt2df08912020-12-15 16:28:09 -08003261bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003262 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
3263 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
3264 return skip;
3265}
3266
3267bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003268 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003269 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
3270 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
3271 return skip;
3272}
3273
3274void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3275 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003276 // Resolve the all subpass contexts to the command buffer contexts
3277 auto cb_context = GetAccessContext(commandBuffer);
3278 assert(cb_context);
3279 auto cb_state = cb_context->GetCommandBufferState();
3280 if (!cb_state) return;
3281
locke-lunargaecf2152020-05-12 17:15:41 -06003282 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06003283 if (!rp_state) return;
3284
John Zulauf355e49b2020-04-24 15:11:15 -06003285 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06003286}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003287
John Zulauf33fc1d52020-07-17 11:01:10 -06003288// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3289// updates to a resource which do not conflict at the byte level.
3290// TODO: Revisit this rule to see if it needs to be tighter or looser
3291// TODO: Add programatic control over suppression heuristics
3292bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3293 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3294}
3295
John Zulauf3d84f1b2020-03-09 13:33:25 -06003296void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003297 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003298 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003299}
3300
3301void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003302 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003303 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003304}
3305
3306void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(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::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003309}
locke-lunarga19c71d2020-03-02 18:17:04 -07003310
Jeff Leger178b1e52020-10-05 12:22:23 -04003311template <typename BufferImageCopyRegionType>
3312bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3313 VkImageLayout dstImageLayout, uint32_t regionCount,
3314 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003315 bool skip = false;
3316 const auto *cb_access_context = GetAccessContext(commandBuffer);
3317 assert(cb_access_context);
3318 if (!cb_access_context) return skip;
3319
Jeff Leger178b1e52020-10-05 12:22:23 -04003320 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3321 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3322
locke-lunarga19c71d2020-03-02 18:17:04 -07003323 const auto *context = cb_access_context->GetCurrentAccessContext();
3324 assert(context);
3325 if (!context) return skip;
3326
3327 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003328 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3329
3330 for (uint32_t region = 0; region < regionCount; region++) {
3331 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003332 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003333 ResourceAccessRange src_range =
3334 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003335 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003336 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003337 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003338 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003339 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003340 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003341 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003342 }
3343 }
3344 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003345 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003346 copy_region.imageOffset, copy_region.imageExtent);
3347 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003348 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003349 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003350 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003351 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003352 }
3353 if (skip) break;
3354 }
3355 if (skip) break;
3356 }
3357 return skip;
3358}
3359
Jeff Leger178b1e52020-10-05 12:22:23 -04003360bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3361 VkImageLayout dstImageLayout, uint32_t regionCount,
3362 const VkBufferImageCopy *pRegions) const {
3363 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3364 COPY_COMMAND_VERSION_1);
3365}
3366
3367bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3368 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3369 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3370 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3371 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3372}
3373
3374template <typename BufferImageCopyRegionType>
3375void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3376 VkImageLayout dstImageLayout, uint32_t regionCount,
3377 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003378 auto *cb_access_context = GetAccessContext(commandBuffer);
3379 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003380
3381 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3382 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3383
3384 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003385 auto *context = cb_access_context->GetCurrentAccessContext();
3386 assert(context);
3387
3388 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003389 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003390
3391 for (uint32_t region = 0; region < regionCount; region++) {
3392 const auto &copy_region = pRegions[region];
3393 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003394 ResourceAccessRange src_range =
3395 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003396 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003397 }
3398 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003399 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003400 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003401 }
3402 }
3403}
3404
Jeff Leger178b1e52020-10-05 12:22:23 -04003405void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3406 VkImageLayout dstImageLayout, uint32_t regionCount,
3407 const VkBufferImageCopy *pRegions) {
3408 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3409 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3410}
3411
3412void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3413 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3414 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3415 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3416 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3417 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3418}
3419
3420template <typename BufferImageCopyRegionType>
3421bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3422 VkBuffer dstBuffer, uint32_t regionCount,
3423 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003424 bool skip = false;
3425 const auto *cb_access_context = GetAccessContext(commandBuffer);
3426 assert(cb_access_context);
3427 if (!cb_access_context) return skip;
3428
Jeff Leger178b1e52020-10-05 12:22:23 -04003429 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3430 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3431
locke-lunarga19c71d2020-03-02 18:17:04 -07003432 const auto *context = cb_access_context->GetCurrentAccessContext();
3433 assert(context);
3434 if (!context) return skip;
3435
3436 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3437 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3438 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3439 for (uint32_t region = 0; region < regionCount; region++) {
3440 const auto &copy_region = pRegions[region];
3441 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003442 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003443 copy_region.imageOffset, copy_region.imageExtent);
3444 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003445 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003446 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003447 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003448 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003449 }
3450 }
3451 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06003452 ResourceAccessRange dst_range =
3453 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003454 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003455 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003456 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003457 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003458 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003459 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003460 }
3461 }
3462 if (skip) break;
3463 }
3464 return skip;
3465}
3466
Jeff Leger178b1e52020-10-05 12:22:23 -04003467bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3468 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3469 const VkBufferImageCopy *pRegions) const {
3470 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3471 COPY_COMMAND_VERSION_1);
3472}
3473
3474bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3475 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3476 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3477 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3478 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3479}
3480
3481template <typename BufferImageCopyRegionType>
3482void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3483 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3484 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003485 auto *cb_access_context = GetAccessContext(commandBuffer);
3486 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003487
3488 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3489 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3490
3491 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003492 auto *context = cb_access_context->GetCurrentAccessContext();
3493 assert(context);
3494
3495 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003496 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3497 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 -06003498 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003499
3500 for (uint32_t region = 0; region < regionCount; region++) {
3501 const auto &copy_region = pRegions[region];
3502 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003503 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003504 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003505 }
3506 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003507 ResourceAccessRange dst_range =
3508 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003509 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003510 }
3511 }
3512}
3513
Jeff Leger178b1e52020-10-05 12:22:23 -04003514void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3515 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3516 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3517 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3518}
3519
3520void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3521 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3522 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3523 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3524 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3525 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3526}
3527
3528template <typename RegionType>
3529bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3530 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3531 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003532 bool skip = false;
3533 const auto *cb_access_context = GetAccessContext(commandBuffer);
3534 assert(cb_access_context);
3535 if (!cb_access_context) return skip;
3536
3537 const auto *context = cb_access_context->GetCurrentAccessContext();
3538 assert(context);
3539 if (!context) return skip;
3540
3541 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3542 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3543
3544 for (uint32_t region = 0; region < regionCount; region++) {
3545 const auto &blit_region = pRegions[region];
3546 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003547 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3548 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3549 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3550 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3551 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3552 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3553 auto hazard =
3554 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003555 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003556 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003557 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003558 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003559 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003560 }
3561 }
3562
3563 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003564 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3565 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3566 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3567 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3568 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3569 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3570 auto hazard =
3571 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003572 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003573 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003574 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003575 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003576 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003577 }
3578 if (skip) break;
3579 }
3580 }
3581
3582 return skip;
3583}
3584
Jeff Leger178b1e52020-10-05 12:22:23 -04003585bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3586 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3587 const VkImageBlit *pRegions, VkFilter filter) const {
3588 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3589 "vkCmdBlitImage");
3590}
3591
3592bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3593 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3594 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3595 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3596 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3597}
3598
3599template <typename RegionType>
3600void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3601 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3602 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003603 auto *cb_access_context = GetAccessContext(commandBuffer);
3604 assert(cb_access_context);
3605 auto *context = cb_access_context->GetCurrentAccessContext();
3606 assert(context);
3607
3608 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003609 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003610
3611 for (uint32_t region = 0; region < regionCount; region++) {
3612 const auto &blit_region = pRegions[region];
3613 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003614 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3615 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3616 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3617 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3618 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3619 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3620 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003621 }
3622 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003623 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3624 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3625 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3626 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3627 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3628 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3629 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003630 }
3631 }
3632}
locke-lunarg36ba2592020-04-03 09:42:04 -06003633
Jeff Leger178b1e52020-10-05 12:22:23 -04003634void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3635 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3636 const VkImageBlit *pRegions, VkFilter filter) {
3637 auto *cb_access_context = GetAccessContext(commandBuffer);
3638 assert(cb_access_context);
3639 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3640 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3641 pRegions, filter);
3642 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3643}
3644
3645void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3646 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3647 auto *cb_access_context = GetAccessContext(commandBuffer);
3648 assert(cb_access_context);
3649 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3650 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3651 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3652 pBlitImageInfo->filter, tag);
3653}
3654
locke-lunarg61870c22020-06-09 14:51:50 -06003655bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
3656 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
3657 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003658 bool skip = false;
3659 if (drawCount == 0) return skip;
3660
3661 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3662 VkDeviceSize size = struct_size;
3663 if (drawCount == 1 || stride == size) {
3664 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003665 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003666 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3667 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003668 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003669 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003670 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003671 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003672 }
3673 } else {
3674 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003675 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003676 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3677 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003678 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003679 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3680 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3681 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003682 break;
3683 }
3684 }
3685 }
3686 return skip;
3687}
3688
locke-lunarg61870c22020-06-09 14:51:50 -06003689void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3690 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3691 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003692 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3693 VkDeviceSize size = struct_size;
3694 if (drawCount == 1 || stride == size) {
3695 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003696 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003697 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3698 } else {
3699 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003700 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003701 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3702 }
3703 }
3704}
3705
locke-lunarg61870c22020-06-09 14:51:50 -06003706bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3707 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003708 bool skip = false;
3709
3710 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003711 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003712 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3713 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003714 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003715 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003716 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003717 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003718 }
3719 return skip;
3720}
3721
locke-lunarg61870c22020-06-09 14:51:50 -06003722void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003723 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003724 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003725 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3726}
3727
locke-lunarg36ba2592020-04-03 09:42:04 -06003728bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003729 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003730 const auto *cb_access_context = GetAccessContext(commandBuffer);
3731 assert(cb_access_context);
3732 if (!cb_access_context) return skip;
3733
locke-lunarg61870c22020-06-09 14:51:50 -06003734 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003735 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003736}
3737
3738void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003739 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003740 auto *cb_access_context = GetAccessContext(commandBuffer);
3741 assert(cb_access_context);
3742 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003743
locke-lunarg61870c22020-06-09 14:51:50 -06003744 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003745}
locke-lunarge1a67022020-04-29 00:15:36 -06003746
3747bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003748 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003749 const auto *cb_access_context = GetAccessContext(commandBuffer);
3750 assert(cb_access_context);
3751 if (!cb_access_context) return skip;
3752
3753 const auto *context = cb_access_context->GetCurrentAccessContext();
3754 assert(context);
3755 if (!context) return skip;
3756
locke-lunarg61870c22020-06-09 14:51:50 -06003757 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3758 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3759 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003760 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003761}
3762
3763void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003764 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003765 auto *cb_access_context = GetAccessContext(commandBuffer);
3766 assert(cb_access_context);
3767 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3768 auto *context = cb_access_context->GetCurrentAccessContext();
3769 assert(context);
3770
locke-lunarg61870c22020-06-09 14:51:50 -06003771 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3772 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003773}
3774
3775bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3776 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003777 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003778 const auto *cb_access_context = GetAccessContext(commandBuffer);
3779 assert(cb_access_context);
3780 if (!cb_access_context) return skip;
3781
locke-lunarg61870c22020-06-09 14:51:50 -06003782 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3783 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3784 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003785 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003786}
3787
3788void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3789 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003790 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003791 auto *cb_access_context = GetAccessContext(commandBuffer);
3792 assert(cb_access_context);
3793 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003794
locke-lunarg61870c22020-06-09 14:51:50 -06003795 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3796 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3797 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003798}
3799
3800bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3801 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003802 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003803 const auto *cb_access_context = GetAccessContext(commandBuffer);
3804 assert(cb_access_context);
3805 if (!cb_access_context) return skip;
3806
locke-lunarg61870c22020-06-09 14:51:50 -06003807 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3808 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3809 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003810 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003811}
3812
3813void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3814 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003815 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003816 auto *cb_access_context = GetAccessContext(commandBuffer);
3817 assert(cb_access_context);
3818 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003819
locke-lunarg61870c22020-06-09 14:51:50 -06003820 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3821 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3822 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003823}
3824
3825bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3826 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003827 bool skip = false;
3828 if (drawCount == 0) return skip;
3829
locke-lunargff255f92020-05-13 18:53:52 -06003830 const auto *cb_access_context = GetAccessContext(commandBuffer);
3831 assert(cb_access_context);
3832 if (!cb_access_context) return skip;
3833
3834 const auto *context = cb_access_context->GetCurrentAccessContext();
3835 assert(context);
3836 if (!context) return skip;
3837
locke-lunarg61870c22020-06-09 14:51:50 -06003838 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3839 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3840 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3841 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003842
3843 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3844 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3845 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003846 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003847 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003848}
3849
3850void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3851 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003852 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003853 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003854 auto *cb_access_context = GetAccessContext(commandBuffer);
3855 assert(cb_access_context);
3856 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3857 auto *context = cb_access_context->GetCurrentAccessContext();
3858 assert(context);
3859
locke-lunarg61870c22020-06-09 14:51:50 -06003860 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3861 cb_access_context->RecordDrawSubpassAttachment(tag);
3862 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003863
3864 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3865 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3866 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003867 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003868}
3869
3870bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3871 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003872 bool skip = false;
3873 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003874 const auto *cb_access_context = GetAccessContext(commandBuffer);
3875 assert(cb_access_context);
3876 if (!cb_access_context) return skip;
3877
3878 const auto *context = cb_access_context->GetCurrentAccessContext();
3879 assert(context);
3880 if (!context) return skip;
3881
locke-lunarg61870c22020-06-09 14:51:50 -06003882 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3883 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3884 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3885 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003886
3887 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3888 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3889 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003890 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003891 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003892}
3893
3894void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3895 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003896 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003897 auto *cb_access_context = GetAccessContext(commandBuffer);
3898 assert(cb_access_context);
3899 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3900 auto *context = cb_access_context->GetCurrentAccessContext();
3901 assert(context);
3902
locke-lunarg61870c22020-06-09 14:51:50 -06003903 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3904 cb_access_context->RecordDrawSubpassAttachment(tag);
3905 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003906
3907 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3908 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3909 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003910 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003911}
3912
3913bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3914 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3915 uint32_t stride, const char *function) const {
3916 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003917 const auto *cb_access_context = GetAccessContext(commandBuffer);
3918 assert(cb_access_context);
3919 if (!cb_access_context) return skip;
3920
3921 const auto *context = cb_access_context->GetCurrentAccessContext();
3922 assert(context);
3923 if (!context) return skip;
3924
locke-lunarg61870c22020-06-09 14:51:50 -06003925 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3926 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3927 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3928 function);
3929 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003930
3931 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3932 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3933 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003934 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003935 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003936}
3937
3938bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3939 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3940 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003941 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3942 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003943}
3944
3945void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3946 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3947 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003948 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3949 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003950 auto *cb_access_context = GetAccessContext(commandBuffer);
3951 assert(cb_access_context);
3952 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3953 auto *context = cb_access_context->GetCurrentAccessContext();
3954 assert(context);
3955
locke-lunarg61870c22020-06-09 14:51:50 -06003956 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3957 cb_access_context->RecordDrawSubpassAttachment(tag);
3958 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3959 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003960
3961 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3962 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3963 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003964 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003965}
3966
3967bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3968 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3969 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003970 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3971 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003972}
3973
3974void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3975 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3976 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003977 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3978 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003979 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003980}
3981
3982bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3983 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3984 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003985 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3986 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003987}
3988
3989void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3990 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3991 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003992 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3993 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003994 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3995}
3996
3997bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3998 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3999 uint32_t stride, const char *function) const {
4000 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004001 const auto *cb_access_context = GetAccessContext(commandBuffer);
4002 assert(cb_access_context);
4003 if (!cb_access_context) return skip;
4004
4005 const auto *context = cb_access_context->GetCurrentAccessContext();
4006 assert(context);
4007 if (!context) return skip;
4008
locke-lunarg61870c22020-06-09 14:51:50 -06004009 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4010 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
4011 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
4012 stride, function);
4013 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004014
4015 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4016 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4017 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004018 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004019 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004020}
4021
4022bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4023 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4024 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004025 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4026 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004027}
4028
4029void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4030 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4031 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004032 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4033 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004034 auto *cb_access_context = GetAccessContext(commandBuffer);
4035 assert(cb_access_context);
4036 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
4037 auto *context = cb_access_context->GetCurrentAccessContext();
4038 assert(context);
4039
locke-lunarg61870c22020-06-09 14:51:50 -06004040 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4041 cb_access_context->RecordDrawSubpassAttachment(tag);
4042 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4043 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004044
4045 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4046 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004047 // We will update the index and vertex buffer in SubmitQueue in the future.
4048 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004049}
4050
4051bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4052 VkDeviceSize offset, VkBuffer countBuffer,
4053 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4054 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004055 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4056 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004057}
4058
4059void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4060 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4061 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004062 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4063 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004064 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4065}
4066
4067bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4068 VkDeviceSize offset, VkBuffer countBuffer,
4069 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4070 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004071 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4072 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004073}
4074
4075void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4076 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4077 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004078 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4079 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004080 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4081}
4082
4083bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4084 const VkClearColorValue *pColor, uint32_t rangeCount,
4085 const VkImageSubresourceRange *pRanges) const {
4086 bool skip = false;
4087 const auto *cb_access_context = GetAccessContext(commandBuffer);
4088 assert(cb_access_context);
4089 if (!cb_access_context) return skip;
4090
4091 const auto *context = cb_access_context->GetCurrentAccessContext();
4092 assert(context);
4093 if (!context) return skip;
4094
4095 const auto *image_state = Get<IMAGE_STATE>(image);
4096
4097 for (uint32_t index = 0; index < rangeCount; index++) {
4098 const auto &range = pRanges[index];
4099 if (image_state) {
4100 auto hazard =
4101 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4102 if (hazard.hazard) {
4103 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004104 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004105 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004106 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004107 }
4108 }
4109 }
4110 return skip;
4111}
4112
4113void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4114 const VkClearColorValue *pColor, uint32_t rangeCount,
4115 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004116 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004117 auto *cb_access_context = GetAccessContext(commandBuffer);
4118 assert(cb_access_context);
4119 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4120 auto *context = cb_access_context->GetCurrentAccessContext();
4121 assert(context);
4122
4123 const auto *image_state = Get<IMAGE_STATE>(image);
4124
4125 for (uint32_t index = 0; index < rangeCount; index++) {
4126 const auto &range = pRanges[index];
4127 if (image_state) {
4128 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4129 tag);
4130 }
4131 }
4132}
4133
4134bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4135 VkImageLayout imageLayout,
4136 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4137 const VkImageSubresourceRange *pRanges) const {
4138 bool skip = false;
4139 const auto *cb_access_context = GetAccessContext(commandBuffer);
4140 assert(cb_access_context);
4141 if (!cb_access_context) return skip;
4142
4143 const auto *context = cb_access_context->GetCurrentAccessContext();
4144 assert(context);
4145 if (!context) return skip;
4146
4147 const auto *image_state = Get<IMAGE_STATE>(image);
4148
4149 for (uint32_t index = 0; index < rangeCount; index++) {
4150 const auto &range = pRanges[index];
4151 if (image_state) {
4152 auto hazard =
4153 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4154 if (hazard.hazard) {
4155 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004156 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004157 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004158 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004159 }
4160 }
4161 }
4162 return skip;
4163}
4164
4165void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4166 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4167 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004168 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004169 auto *cb_access_context = GetAccessContext(commandBuffer);
4170 assert(cb_access_context);
4171 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4172 auto *context = cb_access_context->GetCurrentAccessContext();
4173 assert(context);
4174
4175 const auto *image_state = Get<IMAGE_STATE>(image);
4176
4177 for (uint32_t index = 0; index < rangeCount; index++) {
4178 const auto &range = pRanges[index];
4179 if (image_state) {
4180 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4181 tag);
4182 }
4183 }
4184}
4185
4186bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4187 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4188 VkDeviceSize dstOffset, VkDeviceSize stride,
4189 VkQueryResultFlags flags) const {
4190 bool skip = false;
4191 const auto *cb_access_context = GetAccessContext(commandBuffer);
4192 assert(cb_access_context);
4193 if (!cb_access_context) return skip;
4194
4195 const auto *context = cb_access_context->GetCurrentAccessContext();
4196 assert(context);
4197 if (!context) return skip;
4198
4199 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4200
4201 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004202 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004203 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4204 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004205 skip |=
4206 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4207 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4208 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004209 }
4210 }
locke-lunargff255f92020-05-13 18:53:52 -06004211
4212 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004213 return skip;
4214}
4215
4216void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4217 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4218 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004219 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4220 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004221 auto *cb_access_context = GetAccessContext(commandBuffer);
4222 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004223 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004224 auto *context = cb_access_context->GetCurrentAccessContext();
4225 assert(context);
4226
4227 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4228
4229 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004230 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004231 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4232 }
locke-lunargff255f92020-05-13 18:53:52 -06004233
4234 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004235}
4236
4237bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4238 VkDeviceSize size, uint32_t data) const {
4239 bool skip = false;
4240 const auto *cb_access_context = GetAccessContext(commandBuffer);
4241 assert(cb_access_context);
4242 if (!cb_access_context) return skip;
4243
4244 const auto *context = cb_access_context->GetCurrentAccessContext();
4245 assert(context);
4246 if (!context) return skip;
4247
4248 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4249
4250 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004251 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004252 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4253 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004254 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004255 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004256 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004257 }
4258 }
4259 return skip;
4260}
4261
4262void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4263 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004264 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004265 auto *cb_access_context = GetAccessContext(commandBuffer);
4266 assert(cb_access_context);
4267 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4268 auto *context = cb_access_context->GetCurrentAccessContext();
4269 assert(context);
4270
4271 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4272
4273 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004274 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004275 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4276 }
4277}
4278
4279bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4280 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4281 const VkImageResolve *pRegions) const {
4282 bool skip = false;
4283 const auto *cb_access_context = GetAccessContext(commandBuffer);
4284 assert(cb_access_context);
4285 if (!cb_access_context) return skip;
4286
4287 const auto *context = cb_access_context->GetCurrentAccessContext();
4288 assert(context);
4289 if (!context) return skip;
4290
4291 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4292 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4293
4294 for (uint32_t region = 0; region < regionCount; region++) {
4295 const auto &resolve_region = pRegions[region];
4296 if (src_image) {
4297 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4298 resolve_region.srcOffset, resolve_region.extent);
4299 if (hazard.hazard) {
4300 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004301 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004302 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004303 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004304 }
4305 }
4306
4307 if (dst_image) {
4308 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4309 resolve_region.dstOffset, resolve_region.extent);
4310 if (hazard.hazard) {
4311 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004312 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004313 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004314 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004315 }
4316 if (skip) break;
4317 }
4318 }
4319
4320 return skip;
4321}
4322
4323void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4324 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4325 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004326 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4327 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004328 auto *cb_access_context = GetAccessContext(commandBuffer);
4329 assert(cb_access_context);
4330 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4331 auto *context = cb_access_context->GetCurrentAccessContext();
4332 assert(context);
4333
4334 auto *src_image = Get<IMAGE_STATE>(srcImage);
4335 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4336
4337 for (uint32_t region = 0; region < regionCount; region++) {
4338 const auto &resolve_region = pRegions[region];
4339 if (src_image) {
4340 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4341 resolve_region.srcOffset, resolve_region.extent, tag);
4342 }
4343 if (dst_image) {
4344 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4345 resolve_region.dstOffset, resolve_region.extent, tag);
4346 }
4347 }
4348}
4349
Jeff Leger178b1e52020-10-05 12:22:23 -04004350bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4351 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4352 bool skip = false;
4353 const auto *cb_access_context = GetAccessContext(commandBuffer);
4354 assert(cb_access_context);
4355 if (!cb_access_context) return skip;
4356
4357 const auto *context = cb_access_context->GetCurrentAccessContext();
4358 assert(context);
4359 if (!context) return skip;
4360
4361 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4362 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4363
4364 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4365 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4366 if (src_image) {
4367 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4368 resolve_region.srcOffset, resolve_region.extent);
4369 if (hazard.hazard) {
4370 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4371 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4372 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
4373 region, string_UsageTag(hazard).c_str());
4374 }
4375 }
4376
4377 if (dst_image) {
4378 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4379 resolve_region.dstOffset, resolve_region.extent);
4380 if (hazard.hazard) {
4381 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4382 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4383 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
4384 region, string_UsageTag(hazard).c_str());
4385 }
4386 if (skip) break;
4387 }
4388 }
4389
4390 return skip;
4391}
4392
4393void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4394 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4395 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4396 auto *cb_access_context = GetAccessContext(commandBuffer);
4397 assert(cb_access_context);
4398 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4399 auto *context = cb_access_context->GetCurrentAccessContext();
4400 assert(context);
4401
4402 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4403 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4404
4405 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4406 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4407 if (src_image) {
4408 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4409 resolve_region.srcOffset, resolve_region.extent, tag);
4410 }
4411 if (dst_image) {
4412 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4413 resolve_region.dstOffset, resolve_region.extent, tag);
4414 }
4415 }
4416}
4417
locke-lunarge1a67022020-04-29 00:15:36 -06004418bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4419 VkDeviceSize dataSize, const void *pData) const {
4420 bool skip = false;
4421 const auto *cb_access_context = GetAccessContext(commandBuffer);
4422 assert(cb_access_context);
4423 if (!cb_access_context) return skip;
4424
4425 const auto *context = cb_access_context->GetCurrentAccessContext();
4426 assert(context);
4427 if (!context) return skip;
4428
4429 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4430
4431 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004432 // VK_WHOLE_SIZE not allowed
4433 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004434 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4435 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004436 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004437 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004438 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004439 }
4440 }
4441 return skip;
4442}
4443
4444void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4445 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004446 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004447 auto *cb_access_context = GetAccessContext(commandBuffer);
4448 assert(cb_access_context);
4449 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4450 auto *context = cb_access_context->GetCurrentAccessContext();
4451 assert(context);
4452
4453 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4454
4455 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004456 // VK_WHOLE_SIZE not allowed
4457 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004458 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4459 }
4460}
locke-lunargff255f92020-05-13 18:53:52 -06004461
4462bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4463 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4464 bool skip = false;
4465 const auto *cb_access_context = GetAccessContext(commandBuffer);
4466 assert(cb_access_context);
4467 if (!cb_access_context) return skip;
4468
4469 const auto *context = cb_access_context->GetCurrentAccessContext();
4470 assert(context);
4471 if (!context) return skip;
4472
4473 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4474
4475 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004476 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004477 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4478 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004479 skip |=
4480 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4481 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4482 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004483 }
4484 }
4485 return skip;
4486}
4487
4488void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4489 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004490 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004491 auto *cb_access_context = GetAccessContext(commandBuffer);
4492 assert(cb_access_context);
4493 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4494 auto *context = cb_access_context->GetCurrentAccessContext();
4495 assert(context);
4496
4497 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4498
4499 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004500 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004501 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4502 }
4503}
John Zulauf49beb112020-11-04 16:06:31 -07004504
4505bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
4506 bool skip = false;
4507 const auto *cb_context = GetAccessContext(commandBuffer);
4508 assert(cb_context);
4509 if (!cb_context) return skip;
4510
4511 return cb_context->ValidateSetEvent(commandBuffer, event, stageMask);
4512}
4513
4514void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4515 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
4516 auto *cb_context = GetAccessContext(commandBuffer);
4517 assert(cb_context);
4518 if (!cb_context) return;
4519
4520 cb_context->RecordSetEvent(commandBuffer, event, stageMask);
4521}
4522
4523bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
4524 VkPipelineStageFlags stageMask) const {
4525 bool skip = false;
4526 const auto *cb_context = GetAccessContext(commandBuffer);
4527 assert(cb_context);
4528 if (!cb_context) return skip;
4529
4530 return cb_context->ValidateResetEvent(commandBuffer, event, stageMask);
4531}
4532
4533void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4534 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
4535 auto *cb_context = GetAccessContext(commandBuffer);
4536 assert(cb_context);
4537 if (!cb_context) return;
4538
4539 cb_context->RecordResetEvent(commandBuffer, event, stageMask);
4540}
4541
4542bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4543 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4544 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4545 uint32_t bufferMemoryBarrierCount,
4546 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4547 uint32_t imageMemoryBarrierCount,
4548 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
4549 bool skip = false;
4550 const auto *cb_context = GetAccessContext(commandBuffer);
4551 assert(cb_context);
4552 if (!cb_context) return skip;
4553
4554 return cb_context->ValidateWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
4555 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
4556 pImageMemoryBarriers);
4557}
4558
4559void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4560 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4561 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4562 uint32_t bufferMemoryBarrierCount,
4563 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4564 uint32_t imageMemoryBarrierCount,
4565 const VkImageMemoryBarrier *pImageMemoryBarriers) {
4566 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
4567 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
4568 imageMemoryBarrierCount, pImageMemoryBarriers);
4569
4570 auto *cb_context = GetAccessContext(commandBuffer);
4571 assert(cb_context);
4572 if (!cb_context) return;
4573
4574 cb_context->RecordWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
4575 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
4576 pImageMemoryBarriers);
4577}