blob: f252d333246cbe73410b2b2245286ea789e7ff21 [file] [log] [blame]
locke-lunarg8ec19162020-06-16 18:48:34 -06001/* Copyright (c) 2019-2020 The Khronos Group Inc.
2 * Copyright (c) 2019-2020 Valve Corporation
3 * Copyright (c) 2019-2020 LunarG, Inc.
John Zulauf9cb530d2019-09-30 14:14:10 -06004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Author: John Zulauf <jzulauf@lunarg.com>
18 */
19
20#include <limits>
21#include <vector>
locke-lunarg296a3c92020-03-25 01:04:29 -060022#include <memory>
23#include <bitset>
John Zulauf9cb530d2019-09-30 14:14:10 -060024#include "synchronization_validation.h"
25
26static const char *string_SyncHazardVUID(SyncHazard hazard) {
27 switch (hazard) {
28 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070029 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060030 break;
31 case SyncHazard::READ_AFTER_WRITE:
32 return "SYNC-HAZARD-READ_AFTER_WRITE";
33 break;
34 case SyncHazard::WRITE_AFTER_READ:
35 return "SYNC-HAZARD-WRITE_AFTER_READ";
36 break;
37 case SyncHazard::WRITE_AFTER_WRITE:
38 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
39 break;
John Zulauf2f952d22020-02-10 11:34:51 -070040 case SyncHazard::READ_RACING_WRITE:
41 return "SYNC-HAZARD-READ-RACING-WRITE";
42 break;
43 case SyncHazard::WRITE_RACING_WRITE:
44 return "SYNC-HAZARD-WRITE-RACING-WRITE";
45 break;
46 case SyncHazard::WRITE_RACING_READ:
47 return "SYNC-HAZARD-WRITE-RACING-READ";
48 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060049 default:
50 assert(0);
51 }
52 return "SYNC-HAZARD-INVALID";
53}
54
John Zulauf59e25072020-07-17 10:55:21 -060055static bool IsHazardVsRead(SyncHazard hazard) {
56 switch (hazard) {
57 case SyncHazard::NONE:
58 return false;
59 break;
60 case SyncHazard::READ_AFTER_WRITE:
61 return false;
62 break;
63 case SyncHazard::WRITE_AFTER_READ:
64 return true;
65 break;
66 case SyncHazard::WRITE_AFTER_WRITE:
67 return false;
68 break;
69 case SyncHazard::READ_RACING_WRITE:
70 return false;
71 break;
72 case SyncHazard::WRITE_RACING_WRITE:
73 return false;
74 break;
75 case SyncHazard::WRITE_RACING_READ:
76 return true;
77 break;
78 default:
79 assert(0);
80 }
81 return false;
82}
83
John Zulauf9cb530d2019-09-30 14:14:10 -060084static const char *string_SyncHazard(SyncHazard hazard) {
85 switch (hazard) {
86 case SyncHazard::NONE:
87 return "NONR";
88 break;
89 case SyncHazard::READ_AFTER_WRITE:
90 return "READ_AFTER_WRITE";
91 break;
92 case SyncHazard::WRITE_AFTER_READ:
93 return "WRITE_AFTER_READ";
94 break;
95 case SyncHazard::WRITE_AFTER_WRITE:
96 return "WRITE_AFTER_WRITE";
97 break;
John Zulauf2f952d22020-02-10 11:34:51 -070098 case SyncHazard::READ_RACING_WRITE:
99 return "READ_RACING_WRITE";
100 break;
101 case SyncHazard::WRITE_RACING_WRITE:
102 return "WRITE_RACING_WRITE";
103 break;
104 case SyncHazard::WRITE_RACING_READ:
105 return "WRITE_RACING_READ";
106 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600107 default:
108 assert(0);
109 }
110 return "INVALID HAZARD";
111}
112
John Zulauf37ceaed2020-07-03 16:18:15 -0600113static const SyncStageAccessInfoType *SyncStageAccessInfoFromMask(SyncStageAccessFlags flags) {
114 // Return the info for the first bit found
115 const SyncStageAccessInfoType *info = nullptr;
116 uint32_t index = 0;
117 while (flags) {
118 if (flags & 0x1) {
119 flags = 0;
120 info = &syncStageAccessInfoByStageAccessIndex[index];
121 } else {
122 flags = flags >> 1;
123 index++;
124 }
125 }
126 return info;
127}
128
John Zulauf59e25072020-07-17 10:55:21 -0600129static std::string string_SyncStageAccessFlags(SyncStageAccessFlags flags, const char *sep = "|") {
130 std::string out_str;
131 uint32_t index = 0;
John Zulauf389c34b2020-07-28 11:19:35 -0600132 if (0 == flags) {
133 out_str = "0";
134 }
John Zulauf59e25072020-07-17 10:55:21 -0600135 while (flags) {
136 const auto &info = syncStageAccessInfoByStageAccessIndex[index];
137 if (flags & info.stage_access_bit) {
138 if (!out_str.empty()) {
139 out_str.append(sep);
140 }
141 out_str.append(info.name);
142 flags = flags & ~info.stage_access_bit;
143 }
144 index++;
145 assert(index < syncStageAccessInfoByStageAccessIndex.size());
146 }
147 if (out_str.length() == 0) {
148 out_str.append("Unhandled SyncStageAccess");
149 }
150 return out_str;
151}
152
John Zulauf37ceaed2020-07-03 16:18:15 -0600153static std::string string_UsageTag(const HazardResult &hazard) {
154 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600155 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
156 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600157 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600158 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
159 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf59e25072020-07-17 10:55:21 -0600160 out << "(usage: " << usage_info.name << ", prior_usage: " << stage_access_name;
161 if (IsHazardVsRead(hazard.hazard)) {
162 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
163 out << ", read_barriers: " << string_VkPipelineStageFlags(barriers);
164 } else {
165 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
166 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
167 }
168
169 out << ", command: " << CommandTypeString(tag.command);
170 out << ", seq_no: " << (tag.index & 0xFFFFFFFF) << ", reset_no: " << (tag.index >> 32) << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600171 return out.str();
172}
173
John Zulaufd14743a2020-07-03 09:42:39 -0600174// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
175// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
176// also reflects this special case for read hazard detection (using access instead of exec scope)
John Zulaufb027cdb2020-05-21 14:25:22 -0600177static constexpr VkPipelineStageFlags kColorAttachmentExecScope = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
178static constexpr SyncStageAccessFlags kColorAttachmentAccessScope =
179 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
180 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
John Zulaufd14743a2020-07-03 09:42:39 -0600181 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
182 SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600183static constexpr VkPipelineStageFlags kDepthStencilAttachmentExecScope =
184 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
185static constexpr SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
186 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
187 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
188 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
John Zulaufd14743a2020-07-03 09:42:39 -0600189 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
190 SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600191
192static constexpr SyncOrderingBarrier kColorAttachmentRasterOrder = {kColorAttachmentExecScope, kColorAttachmentAccessScope};
193static constexpr SyncOrderingBarrier kDepthStencilAttachmentRasterOrder = {kDepthStencilAttachmentExecScope,
194 kDepthStencilAttachmentAccessScope};
195static constexpr SyncOrderingBarrier kAttachmentRasterOrder = {kDepthStencilAttachmentExecScope | kColorAttachmentExecScope,
196 kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope};
John Zulauf7635de32020-05-29 17:14:15 -0600197// 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 -0600198static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600199
John Zulaufb02c1eb2020-10-06 16:33:36 -0600200static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) {
201 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
202}
203
204static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
205
locke-lunarg3c038002020-04-30 23:08:08 -0600206inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
207 if (size == VK_WHOLE_SIZE) {
208 return (whole_size - offset);
209 }
210 return size;
211}
212
John Zulauf3e86bf02020-09-12 10:47:57 -0600213static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
214 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
215}
216
John Zulauf16adfc92020-04-08 10:28:33 -0600217template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600218static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600219 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
220}
221
John Zulauf355e49b2020-04-24 15:11:15 -0600222static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600223
John Zulauf3e86bf02020-09-12 10:47:57 -0600224static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
225 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
226}
227
228static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
229 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
230}
231
John Zulauf0cb5be22020-01-23 12:18:22 -0700232// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
233VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
234 VkPipelineStageFlags expanded = stage_mask;
235 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
236 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
237 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
238 if (all_commands.first & queue_flags) {
239 expanded |= all_commands.second;
240 }
241 }
242 }
243 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
244 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
245 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
246 }
247 return expanded;
248}
249
John Zulauf36bcf6a2020-02-03 15:12:52 -0700250VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
251 std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
252 VkPipelineStageFlags unscanned = stage_mask;
253 VkPipelineStageFlags related = 0;
Jonah Ryan-Davis185189c2020-07-14 10:28:52 -0400254 for (const auto &entry : map) {
255 const auto &stage = entry.first;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700256 if (stage & unscanned) {
257 related = related | entry.second;
258 unscanned = unscanned & ~stage;
259 if (!unscanned) break;
260 }
261 }
262 return related;
263}
264
265VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
266 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
267}
268
269VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
270 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
271}
272
John Zulauf5c5e88d2019-12-26 11:22:02 -0700273static const ResourceAccessRange full_range(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700274
John Zulauf3e86bf02020-09-12 10:47:57 -0600275ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
276 VkDeviceSize stride) {
277 VkDeviceSize range_start = offset + first_index * stride;
278 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600279 if (count == UINT32_MAX) {
280 range_size = buf_whole_size - range_start;
281 } else {
282 range_size = count * stride;
283 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600284 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600285}
286
locke-lunarg654e3692020-06-04 17:19:15 -0600287SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
288 VkShaderStageFlagBits stage_flag) {
289 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
290 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
291 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
292 }
293 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
294 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
295 assert(0);
296 }
297 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
298 return stage_access->second.uniform_read;
299 }
300
301 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
302 // Because if write hazard happens, read hazard might or might not happen.
303 // But if write hazard doesn't happen, read hazard is impossible to happen.
304 if (descriptor_data.is_writable) {
305 return stage_access->second.shader_write;
306 }
307 return stage_access->second.shader_read;
308}
309
locke-lunarg37047832020-06-12 13:44:45 -0600310bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
311 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
312 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
313 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
314 ? true
315 : false;
316}
317
318bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
319 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
320 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
321 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
322 ? true
323 : false;
324}
325
John Zulauf355e49b2020-04-24 15:11:15 -0600326// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
327const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = {
328 AccessContext::AddressType::kLinearAddress, AccessContext::AddressType::kIdealizedAddress};
329
John Zulaufb02c1eb2020-10-06 16:33:36 -0600330template <typename Action>
331static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
332 Action &action) {
333 // At this point the "apply over range" logic only supports a single memory binding
334 if (!SimpleBinding(image_state)) return;
335 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
336 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
337 image_state.createInfo.extent);
338 const auto base_address = ResourceBaseAddress(image_state);
339 for (; range_gen->non_empty(); ++range_gen) {
340 action((*range_gen + base_address));
341 }
342}
343
John Zulauf7635de32020-05-29 17:14:15 -0600344// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
345// Used by both validation and record operations
346//
347// The signature for Action() reflect the needs of both uses.
348template <typename Action>
349void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
350 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
351 VkExtent3D extent = CastTo3D(render_area.extent);
352 VkOffset3D offset = CastTo3D(render_area.offset);
353 const auto &rp_ci = rp_state.createInfo;
354 const auto *attachment_ci = rp_ci.pAttachments;
355 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
356
357 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
358 const auto *color_attachments = subpass_ci.pColorAttachments;
359 const auto *color_resolve = subpass_ci.pResolveAttachments;
360 if (color_resolve && color_attachments) {
361 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
362 const auto &color_attach = color_attachments[i].attachment;
363 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
364 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
365 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
366 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
367 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
368 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
369 }
370 }
371 }
372
373 // Depth stencil resolve only if the extension is present
374 const auto ds_resolve = lvl_find_in_chain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
375 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
376 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
377 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
378 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
379 const auto src_ci = attachment_ci[src_at];
380 // The formats are required to match so we can pick either
381 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
382 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
383 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
384 VkImageAspectFlags aspect_mask = 0u;
385
386 // Figure out which aspects are actually touched during resolve operations
387 const char *aspect_string = nullptr;
388 if (resolve_depth && resolve_stencil) {
389 // Validate all aspects together
390 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
391 aspect_string = "depth/stencil";
392 } else if (resolve_depth) {
393 // Validate depth only
394 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
395 aspect_string = "depth";
396 } else if (resolve_stencil) {
397 // Validate all stencil only
398 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
399 aspect_string = "stencil";
400 }
401
402 if (aspect_mask) {
403 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
404 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kDepthStencilAttachmentRasterOrder, offset, extent,
405 aspect_mask);
406 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
407 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
408 }
409 }
410}
411
412// Action for validating resolve operations
413class ValidateResolveAction {
414 public:
415 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
416 const char *func_name)
417 : render_pass_(render_pass),
418 subpass_(subpass),
419 context_(context),
420 sync_state_(sync_state),
421 func_name_(func_name),
422 skip_(false) {}
423 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
424 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
425 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
426 HazardResult hazard;
427 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
428 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600429 skip_ |= sync_state_.LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
430 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600431 " to resolve attachment %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -0600432 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name,
John Zulauf37ceaed2020-07-03 16:18:15 -0600433 src_at, dst_at, string_UsageTag(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600434 }
435 }
436 // Providing a mechanism for the constructing caller to get the result of the validation
437 bool GetSkip() const { return skip_; }
438
439 private:
440 VkRenderPass render_pass_;
441 const uint32_t subpass_;
442 const AccessContext &context_;
443 const SyncValidator &sync_state_;
444 const char *func_name_;
445 bool skip_;
446};
447
448// Update action for resolve operations
449class UpdateStateResolveAction {
450 public:
451 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
452 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
453 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
454 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
455 // Ignores validation only arguments...
456 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
457 }
458
459 private:
460 AccessContext &context_;
461 const ResourceUsageTag &tag_;
462};
463
John Zulauf59e25072020-07-17 10:55:21 -0600464void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
465 SyncStageAccessFlags prior_, const ResourceUsageTag &tag_) {
466 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
467 usage_index = usage_index_;
468 hazard = hazard_;
469 prior_access = prior_;
470 tag = tag_;
471}
472
John Zulauf540266b2020-04-06 18:54:53 -0600473AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
474 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600475 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600476 Reset();
477 const auto &subpass_dep = dependencies[subpass];
478 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600479 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600480 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600481 const auto prev_pass = prev_dep.first->pass;
482 const auto &prev_barriers = prev_dep.second;
483 assert(prev_dep.second.size());
484 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
485 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700486 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600487
488 async_.reserve(subpass_dep.async.size());
489 for (const auto async_subpass : subpass_dep.async) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600490 // TODO -- review why async is storing non-const
John Zulauf540266b2020-04-06 18:54:53 -0600491 async_.emplace_back(const_cast<AccessContext *>(&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 Zulauf16adfc92020-04-08 10:28:33 -0600502HazardResult AccessContext::DetectPreviousHazard(AddressType 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 Zulauf355e49b2020-04-24 15:11:15 -0600517HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
518 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 Zulauf355e49b2020-04-24 15:11:15 -0600569HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600570 auto &accesses = GetAccessStateMap(type);
571 const auto from = accesses.lower_bound(range);
572 const auto to = accesses.upper_bound(range);
573
John Zulauf3d84f1b2020-03-09 13:33:25 -0600574 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600575 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
576 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600577 }
John Zulauf16adfc92020-04-08 10:28:33 -0600578
John Zulauf3d84f1b2020-03-09 13:33:25 -0600579 return hazard;
580}
581
John Zulaufb02c1eb2020-10-06 16:33:36 -0600582struct ApplySubpassTransitionBarriersAction {
583 ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
584 void operator()(ResourceAccessState *access) const {
585 assert(access);
586 access->ApplyBarriers(barriers, true);
587 }
588 const std::vector<SyncBarrier> &barriers;
589};
590
591struct ApplyTrackbackBarriersAction {
592 ApplyTrackbackBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
593 void operator()(ResourceAccessState *access) const {
594 assert(access);
595 assert(!access->HasPendingState());
596 access->ApplyBarriers(barriers, false);
597 access->ApplyPendingBarriers(kCurrentCommandTag);
598 }
599 const std::vector<SyncBarrier> &barriers;
600};
601
602// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
603// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
604// *different* map from dest.
605// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
606// range [first, last)
607template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600608static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
609 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600610 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600611 auto at = entry;
612 for (auto pos = first; pos != last; ++pos) {
613 // Every member of the input iterator range must fit within the remaining portion of entry
614 assert(at->first.includes(pos->first));
615 assert(at != dest->end());
616 // Trim up at to the same size as the entry to resolve
617 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600618 auto access = pos->second; // intentional copy
619 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600620 at->second.Resolve(access);
621 ++at; // Go to the remaining unused section of entry
622 }
623}
624
John Zulaufa0a98292020-09-18 09:30:10 -0600625static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
626 SyncBarrier merged = {};
627 for (const auto &barrier : barriers) {
628 merged.Merge(barrier);
629 }
630 return merged;
631}
632
John Zulaufb02c1eb2020-10-06 16:33:36 -0600633template <typename BarrierAction>
634void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600635 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
636 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600637 if (!range.non_empty()) return;
638
John Zulauf355e49b2020-04-24 15:11:15 -0600639 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
640 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600641 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600642 if (current->pos_B->valid) {
643 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600644 auto access = src_pos->second; // intentional copy
645 barrier_action(&access);
646
John Zulauf16adfc92020-04-08 10:28:33 -0600647 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600648 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
649 trimmed->second.Resolve(access);
650 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600651 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600652 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600653 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600654 }
John Zulauf16adfc92020-04-08 10:28:33 -0600655 } else {
656 // we have to descend to fill this gap
657 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600658 if (current->pos_A->valid) {
659 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
660 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600661 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600662 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -0600663 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600664 // There isn't anything in dest in current)range, so we can accumulate directly into it.
665 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600666 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
667 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
668 barrier_action(&pos->second);
John Zulauf355e49b2020-04-24 15:11:15 -0600669 }
670 }
671 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
672 // iterator of the outer while.
673
674 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
675 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
676 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600677 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
678 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600679 current.seek(seek_to);
680 } else if (!current->pos_A->valid && infill_state) {
681 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
682 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
683 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600684 }
John Zulauf5f13a792020-03-10 07:31:21 -0600685 }
John Zulauf16adfc92020-04-08 10:28:33 -0600686 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600687 }
John Zulauf1a224292020-06-30 14:52:13 -0600688
689 // Infill if range goes passed both the current and resolve map prior contents
690 if (recur_to_infill && (current->range.end < range.end)) {
691 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
692 ResourceAccessRangeMap gap_map;
693 const auto the_end = resolve_map->end();
694 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
695 for (auto &access : gap_map) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600696 barrier_action(&access.second);
John Zulauf1a224292020-06-30 14:52:13 -0600697 resolve_map->insert(the_end, access);
698 }
699 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600700}
701
John Zulauf355e49b2020-04-24 15:11:15 -0600702void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
703 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600704 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600705 if (range.non_empty() && infill_state) {
706 descent_map->insert(std::make_pair(range, *infill_state));
707 }
708 } else {
709 // Look for something to fill the gap further along.
710 for (const auto &prev_dep : prev_) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600711 const ApplyTrackbackBarriersAction barrier_action(prev_dep.barriers);
712 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600713 }
714
John Zulaufe5da6e52020-03-18 15:32:18 -0600715 if (src_external_.context) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600716 const ApplyTrackbackBarriersAction barrier_action(src_external_.barriers);
717 src_external_.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600718 }
719 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600720}
721
John Zulauf16adfc92020-04-08 10:28:33 -0600722AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600723 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600724}
725
John Zulauf16adfc92020-04-08 10:28:33 -0600726
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:
751 ResolveAccessRangeFunctor(const AccessContext &context, AccessContext::AddressType address_type,
752 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
753 BarrierAction &barrier_action)
John Zulauf52446eb2020-10-22 16:40:08 -0600754 : context_(context),
755 address_type_(address_type),
756 descent_map_(descent_map),
757 infill_state_(infill_state),
758 barrier_action_(barrier_action) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600759 ResolveAccessRangeFunctor() = delete;
760 void operator()(const ResourceAccessRange &range) const {
761 context_.ResolveAccessRange(address_type_, range, barrier_action_, descent_map_, infill_state_);
762 }
763
764 private:
John Zulauf52446eb2020-10-22 16:40:08 -0600765 const AccessContext &context_;
766 const AccessContext::AddressType address_type_;
767 ResourceAccessRangeMap *const descent_map_;
768 const ResourceAccessState *infill_state_;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600769 BarrierAction &barrier_action_;
770};
771
John Zulaufb02c1eb2020-10-06 16:33:36 -0600772template <typename BarrierAction>
773void AccessContext::ResolveAccessRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range,
774 BarrierAction &barrier_action, AddressType address_type, ResourceAccessRangeMap *descent_map,
775 const ResourceAccessState *infill_state) const {
776 const ResolveAccessRangeFunctor<BarrierAction> action(*this, address_type, descent_map, infill_state, barrier_action);
777 ApplyOverImageRange(image_state, subresource_range, action);
John Zulauf62f10592020-04-03 12:20:02 -0600778}
779
John Zulauf7635de32020-05-29 17:14:15 -0600780// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600781bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600782 const VkRect2D &render_area, uint32_t subpass,
783 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
784 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600785 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600786 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
787 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
788 // those affects have not been recorded yet.
789 //
790 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
791 // to apply and only copy then, if this proves a hot spot.
792 std::unique_ptr<AccessContext> proxy_for_prev;
793 TrackBack proxy_track_back;
794
John Zulauf355e49b2020-04-24 15:11:15 -0600795 const auto &transitions = rp_state.subpass_transitions[subpass];
796 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600797 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
798
799 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
800 if (prev_needs_proxy) {
801 if (!proxy_for_prev) {
802 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
803 render_area, attachment_views));
804 proxy_track_back = *track_back;
805 proxy_track_back.context = proxy_for_prev.get();
806 }
807 track_back = &proxy_track_back;
808 }
809 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600810 if (hazard.hazard) {
John Zulauf389c34b2020-07-28 11:19:35 -0600811 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
812 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
813 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
814 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
815 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
816 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600817 }
818 }
819 return skip;
820}
821
John Zulauf1507ee42020-05-18 11:33:09 -0600822bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600823 const VkRect2D &render_area, uint32_t subpass,
824 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
825 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600826 bool skip = false;
827 const auto *attachment_ci = rp_state.createInfo.pAttachments;
828 VkExtent3D extent = CastTo3D(render_area.extent);
829 VkOffset3D offset = CastTo3D(render_area.offset);
John Zulaufa0a98292020-09-18 09:30:10 -0600830
John Zulauf1507ee42020-05-18 11:33:09 -0600831 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
832 if (subpass == rp_state.attachment_first_subpass[i]) {
833 if (attachment_views[i] == nullptr) continue;
834 const IMAGE_VIEW_STATE &view = *attachment_views[i];
835 const IMAGE_STATE *image = view.image_state.get();
836 if (image == nullptr) continue;
837 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -0600838
839 // Need check in the following way
840 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
841 // vs. transition
842 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
843 // for each aspect loaded.
844
845 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600846 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600847 const bool is_color = !(has_depth || has_stencil);
848
849 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -0600850 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -0600851
John Zulaufaff20662020-06-01 14:07:58 -0600852 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600853 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -0600854
John Zulaufb02c1eb2020-10-06 16:33:36 -0600855 auto hazard_range = view.normalized_subresource_range;
856 bool checked_stencil = false;
857 if (is_color) {
858 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, offset, extent);
859 aspect = "color";
860 } else {
861 if (has_depth) {
862 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
863 hazard = DetectHazard(*image, load_index, hazard_range, offset, extent);
864 aspect = "depth";
865 }
866 if (!hazard.hazard && has_stencil) {
867 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
868 hazard = DetectHazard(*image, stencil_load_index, hazard_range, offset, extent);
869 aspect = "stencil";
870 checked_stencil = true;
871 }
872 }
873
874 if (hazard.hazard) {
875 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
876 if (hazard.tag == kCurrentCommandTag) {
877 // Hazard vs. ILT
878 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
879 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
880 " aspect %s during load with loadOp %s.",
881 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
882 } else {
John Zulauf1507ee42020-05-18 11:33:09 -0600883 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
884 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600885 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -0600886 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600887 string_UsageTag(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -0600888 }
889 }
890 }
891 }
892 return skip;
893}
894
John Zulaufaff20662020-06-01 14:07:58 -0600895// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
896// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
897// store is part of the same Next/End operation.
898// The latter is handled in layout transistion validation directly
899bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
900 const VkRect2D &render_area, uint32_t subpass,
901 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
902 const char *func_name) const {
903 bool skip = false;
904 const auto *attachment_ci = rp_state.createInfo.pAttachments;
905 VkExtent3D extent = CastTo3D(render_area.extent);
906 VkOffset3D offset = CastTo3D(render_area.offset);
907
908 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
909 if (subpass == rp_state.attachment_last_subpass[i]) {
910 if (attachment_views[i] == nullptr) continue;
911 const IMAGE_VIEW_STATE &view = *attachment_views[i];
912 const IMAGE_STATE *image = view.image_state.get();
913 if (image == nullptr) continue;
914 const auto &ci = attachment_ci[i];
915
916 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
917 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
918 // sake, we treat DONT_CARE as writing.
919 const bool has_depth = FormatHasDepth(ci.format);
920 const bool has_stencil = FormatHasStencil(ci.format);
921 const bool is_color = !(has_depth || has_stencil);
922 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
923 if (!has_stencil && !store_op_stores) continue;
924
925 HazardResult hazard;
926 const char *aspect = nullptr;
927 bool checked_stencil = false;
928 if (is_color) {
929 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
930 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
931 aspect = "color";
932 } else {
933 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
934 auto hazard_range = view.normalized_subresource_range;
935 if (has_depth && store_op_stores) {
936 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
937 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
938 kAttachmentRasterOrder, offset, extent);
939 aspect = "depth";
940 }
941 if (!hazard.hazard && has_stencil && stencil_op_stores) {
942 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
943 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
944 kAttachmentRasterOrder, offset, extent);
945 aspect = "stencil";
946 checked_stencil = true;
947 }
948 }
949
950 if (hazard.hazard) {
951 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
952 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -0600953 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
954 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600955 " %s aspect during store with %s %s. Access info %s",
John Zulauf1dae9192020-06-16 15:46:44 -0600956 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600957 store_op_string, string_UsageTag(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -0600958 }
959 }
960 }
961 return skip;
962}
963
John Zulaufb027cdb2020-05-21 14:25:22 -0600964bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
965 const VkRect2D &render_area,
966 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
967 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600968 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
969 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
970 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600971}
972
John Zulauf3d84f1b2020-03-09 13:33:25 -0600973class HazardDetector {
974 SyncStageAccessIndex usage_index_;
975
976 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600977 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600978 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
979 return pos->second.DetectAsyncHazard(usage_index_);
980 }
981 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
982};
983
John Zulauf69133422020-05-20 14:55:53 -0600984class HazardDetectorWithOrdering {
985 const SyncStageAccessIndex usage_index_;
986 const SyncOrderingBarrier &ordering_;
987
988 public:
989 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
990 return pos->second.DetectHazard(usage_index_, ordering_);
991 }
992 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
993 return pos->second.DetectAsyncHazard(usage_index_);
994 }
995 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
996 : usage_index_(usage), ordering_(ordering) {}
997};
998
John Zulauf16adfc92020-04-08 10:28:33 -0600999HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -06001000 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001001 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -06001002 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001003}
1004
John Zulauf16adfc92020-04-08 10:28:33 -06001005HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001006 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001007 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001008 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -06001009}
1010
John Zulauf69133422020-05-20 14:55:53 -06001011template <typename Detector>
1012HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1013 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1014 const VkExtent3D &extent, DetectOptions options) const {
1015 if (!SimpleBinding(image)) return HazardResult();
1016 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
1017 const auto address_type = ImageAddressType(image);
1018 const auto base_address = ResourceBaseAddress(image);
1019 for (; range_gen->non_empty(); ++range_gen) {
1020 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
1021 if (hazard.hazard) return hazard;
1022 }
1023 return HazardResult();
1024}
1025
John Zulauf540266b2020-04-06 18:54:53 -06001026HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1027 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1028 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001029 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1030 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -06001031 return DetectHazard(image, current_usage, subresource_range, offset, extent);
1032}
1033
1034HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1035 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1036 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -06001037 HazardDetector detector(current_usage);
1038 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
1039}
1040
1041HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1042 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
1043 const VkOffset3D &offset, const VkExtent3D &extent) const {
1044 HazardDetectorWithOrdering detector(current_usage, ordering);
1045 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001046}
1047
John Zulaufb027cdb2020-05-21 14:25:22 -06001048// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
1049// should have reported the issue regarding an invalid attachment entry
1050HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
1051 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
1052 VkImageAspectFlags aspect_mask) const {
1053 if (view != nullptr) {
1054 const IMAGE_STATE *image = view->image_state.get();
1055 if (image != nullptr) {
1056 auto *detect_range = &view->normalized_subresource_range;
1057 VkImageSubresourceRange masked_range;
1058 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1059 masked_range = view->normalized_subresource_range;
1060 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1061 detect_range = &masked_range;
1062 }
1063
1064 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1065 if (detect_range->aspectMask) {
1066 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
1067 }
1068 }
1069 }
1070 return HazardResult();
1071}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001072class BarrierHazardDetector {
1073 public:
1074 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1075 SyncStageAccessFlags src_access_scope)
1076 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1077
John Zulauf5f13a792020-03-10 07:31:21 -06001078 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1079 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001080 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001081 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
1082 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1083 return pos->second.DetectAsyncHazard(usage_index_);
1084 }
1085
1086 private:
1087 SyncStageAccessIndex usage_index_;
1088 VkPipelineStageFlags src_exec_scope_;
1089 SyncStageAccessFlags src_access_scope_;
1090};
1091
John Zulauf16adfc92020-04-08 10:28:33 -06001092HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -06001093 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001094 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001095 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -06001096 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001097}
1098
John Zulauf16adfc92020-04-08 10:28:33 -06001099HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001100 SyncStageAccessFlags src_access_scope,
1101 const VkImageSubresourceRange &subresource_range,
1102 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001103 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1104 VkOffset3D zero_offset = {0, 0, 0};
1105 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001106}
1107
John Zulauf355e49b2020-04-24 15:11:15 -06001108HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1109 SyncStageAccessFlags src_stage_accesses,
1110 const VkImageMemoryBarrier &barrier) const {
1111 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1112 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1113 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1114}
1115
John Zulauf9cb530d2019-09-30 14:14:10 -06001116template <typename Flags, typename Map>
1117SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1118 SyncStageAccessFlags scope = 0;
1119 for (const auto &bit_scope : map) {
1120 if (flag_mask < bit_scope.first) break;
1121
1122 if (flag_mask & bit_scope.first) {
1123 scope |= bit_scope.second;
1124 }
1125 }
1126 return scope;
1127}
1128
1129SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
1130 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1131}
1132
1133SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
1134 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
1135}
1136
1137// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
1138SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001139 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1140 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1141 // 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 -06001142 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1143}
1144
1145template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001146void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001147 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1148 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -06001149 auto pos = accesses->lower_bound(range);
1150 if (pos == accesses->end() || !pos->first.intersects(range)) {
1151 // The range is empty, fill it with a default value.
1152 pos = action.Infill(accesses, pos, range);
1153 } else if (range.begin < pos->first.begin) {
1154 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001155 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001156 } else if (pos->first.begin < range.begin) {
1157 // Trim the beginning if needed
1158 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1159 ++pos;
1160 }
1161
1162 const auto the_end = accesses->end();
1163 while ((pos != the_end) && pos->first.intersects(range)) {
1164 if (pos->first.end > range.end) {
1165 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1166 }
1167
1168 pos = action(accesses, pos);
1169 if (pos == the_end) break;
1170
1171 auto next = pos;
1172 ++next;
1173 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1174 // Need to infill if next is disjoint
1175 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001176 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001177 next = action.Infill(accesses, next, new_range);
1178 }
1179 pos = next;
1180 }
1181}
1182
1183struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001184 using Iterator = ResourceAccessRangeMap::iterator;
1185 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001186 // this is only called on gaps, and never returns a gap.
1187 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001188 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001189 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001190 }
John Zulauf5f13a792020-03-10 07:31:21 -06001191
John Zulauf5c5e88d2019-12-26 11:22:02 -07001192 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001193 auto &access_state = pos->second;
1194 access_state.Update(usage, tag);
1195 return pos;
1196 }
1197
John Zulauf16adfc92020-04-08 10:28:33 -06001198 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001199 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001200 : type(type_), context(context_), usage(usage_), tag(tag_) {}
1201 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001202 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001203 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001204 const ResourceUsageTag &tag;
1205};
1206
John Zulauf89311b42020-09-29 16:28:47 -06001207// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1208// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1209class ApplyBarrierFunctor {
1210 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001211 using Iterator = ResourceAccessRangeMap::iterator;
1212 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001213
John Zulauf5c5e88d2019-12-26 11:22:02 -07001214 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001215 auto &access_state = pos->second;
John Zulauf89311b42020-09-29 16:28:47 -06001216 access_state.ApplyBarrier(barrier_, layout_transition_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001217 return pos;
1218 }
1219
John Zulauf89311b42020-09-29 16:28:47 -06001220 ApplyBarrierFunctor(const SyncBarrier &barrier, bool layout_transition)
1221 : barrier_(barrier), layout_transition_(layout_transition) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001222
John Zulauf89311b42020-09-29 16:28:47 -06001223 private:
1224 const SyncBarrier barrier_;
1225 const bool layout_transition_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001226};
1227
John Zulauf89311b42020-09-29 16:28:47 -06001228// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1229// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1230// of a collection is known/present.
1231class ApplyBarrierOpsFunctor {
1232 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001233 using Iterator = ResourceAccessRangeMap::iterator;
1234 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001235
John Zulauf89311b42020-09-29 16:28:47 -06001236 struct BarrierOp {
1237 SyncBarrier barrier;
1238 bool layout_transition;
1239 BarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1240 : barrier(barrier_), layout_transition(layout_transition_) {}
1241 BarrierOp() = default;
1242 };
John Zulauf5c5e88d2019-12-26 11:22:02 -07001243 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001244 auto &access_state = pos->second;
John Zulauf89311b42020-09-29 16:28:47 -06001245 for (const auto op : barrier_ops_) {
1246 access_state.ApplyBarrier(op.barrier, op.layout_transition);
1247 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001248
John Zulauf89311b42020-09-29 16:28:47 -06001249 if (resolve_) {
1250 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1251 // another walk
1252 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001253 }
1254 return pos;
1255 }
1256
John Zulauf89311b42020-09-29 16:28:47 -06001257 // A valid tag is required IFF any of the barriers ops are a layout transition, as transitions are write ops
1258 ApplyBarrierOpsFunctor(bool resolve, size_t size_hint, const ResourceUsageTag &tag)
1259 : resolve_(resolve), barrier_ops_(), tag_(tag) {
1260 if (size_hint) {
1261 barrier_ops_.reserve(size_hint);
1262 }
1263 };
1264
1265 // A valid tag is required IFF layout_transition is true, as transitions are write ops
1266 ApplyBarrierOpsFunctor(bool resolve, const std::vector<SyncBarrier> &barriers, bool layout_transition,
1267 const ResourceUsageTag &tag)
John Zulaufb02c1eb2020-10-06 16:33:36 -06001268 : resolve_(resolve), barrier_ops_(), tag_(tag) {
1269 barrier_ops_.reserve(barriers.size());
John Zulauf89311b42020-09-29 16:28:47 -06001270 for (const auto &barrier : barriers) {
1271 barrier_ops_.emplace_back(barrier, layout_transition);
John Zulauf9cb530d2019-09-30 14:14:10 -06001272 }
1273 }
1274
John Zulauf89311b42020-09-29 16:28:47 -06001275 void PushBack(const SyncBarrier &barrier, bool layout_transition) { barrier_ops_.emplace_back(barrier, layout_transition); }
1276
1277 void PushBack(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
1278 barrier_ops_.reserve(barrier_ops_.size() + barriers.size());
1279 for (const auto &barrier : barriers) {
1280 barrier_ops_.emplace_back(barrier, layout_transition);
1281 }
1282 }
1283
1284 private:
1285 bool resolve_;
1286 std::vector<BarrierOp> barrier_ops_;
1287 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001288};
1289
John Zulauf355e49b2020-04-24 15:11:15 -06001290void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1291 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001292 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1293 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001294}
1295
John Zulauf16adfc92020-04-08 10:28:33 -06001296void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001297 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001298 if (!SimpleBinding(buffer)) return;
1299 const auto base_address = ResourceBaseAddress(buffer);
1300 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1301}
John Zulauf355e49b2020-04-24 15:11:15 -06001302
John Zulauf540266b2020-04-06 18:54:53 -06001303void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001304 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001305 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001306 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001307 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001308 const auto address_type = ImageAddressType(image);
1309 const auto base_address = ResourceBaseAddress(image);
1310 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001311 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001312 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001313 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001314}
John Zulauf7635de32020-05-29 17:14:15 -06001315void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1316 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1317 if (view != nullptr) {
1318 const IMAGE_STATE *image = view->image_state.get();
1319 if (image != nullptr) {
1320 auto *update_range = &view->normalized_subresource_range;
1321 VkImageSubresourceRange masked_range;
1322 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1323 masked_range = view->normalized_subresource_range;
1324 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1325 update_range = &masked_range;
1326 }
1327 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1328 }
1329 }
1330}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001331
John Zulauf355e49b2020-04-24 15:11:15 -06001332void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1333 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1334 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001335 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1336 subresource.layerCount};
1337 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1338}
1339
John Zulauf540266b2020-04-06 18:54:53 -06001340template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001341void AccessContext::UpdateResourceAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001342 if (!SimpleBinding(buffer)) return;
1343 const auto base_address = ResourceBaseAddress(buffer);
1344 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001345}
1346
1347template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001348void AccessContext::UpdateResourceAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1349 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001350 if (!SimpleBinding(image)) return;
1351 const auto address_type = ImageAddressType(image);
1352 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001353
locke-lunargae26eac2020-04-16 15:29:05 -06001354 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001355 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001356
John Zulauf16adfc92020-04-08 10:28:33 -06001357 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001358 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001359 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001360 }
1361}
1362
John Zulauf7635de32020-05-29 17:14:15 -06001363void AccessContext::UpdateAttachmentResolveAccess(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 UpdateStateResolveAction update(*this, tag);
1367 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1368}
1369
John Zulaufaff20662020-06-01 14:07:58 -06001370void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1371 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1372 const ResourceUsageTag &tag) {
1373 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1374 VkExtent3D extent = CastTo3D(render_area.extent);
1375 VkOffset3D offset = CastTo3D(render_area.offset);
1376
1377 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1378 if (rp_state.attachment_last_subpass[i] == subpass) {
1379 if (attachment_views[i] == nullptr) continue; // UNUSED
1380 const auto &view = *attachment_views[i];
1381 const IMAGE_STATE *image = view.image_state.get();
1382 if (image == nullptr) continue;
1383
1384 const auto &ci = attachment_ci[i];
1385 const bool has_depth = FormatHasDepth(ci.format);
1386 const bool has_stencil = FormatHasStencil(ci.format);
1387 const bool is_color = !(has_depth || has_stencil);
1388 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1389
1390 if (is_color && store_op_stores) {
1391 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1392 offset, extent, tag);
1393 } else {
1394 auto update_range = view.normalized_subresource_range;
1395 if (has_depth && store_op_stores) {
1396 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1397 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1398 tag);
1399 }
1400 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1401 if (has_stencil && stencil_op_stores) {
1402 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1403 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1404 tag);
1405 }
1406 }
1407 }
1408 }
1409}
1410
John Zulauf540266b2020-04-06 18:54:53 -06001411template <typename Action>
1412void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1413 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001414 for (const auto address_type : kAddressTypes) {
1415 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001416 }
1417}
1418
1419void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001420 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1421 auto &context = contexts[subpass_index];
John Zulaufb02c1eb2020-10-06 16:33:36 -06001422 ApplyTrackbackBarriersAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001423 for (const auto address_type : kAddressTypes) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001424 context.ResolveAccessRange(address_type, full_range, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001425 }
1426 }
1427}
1428
John Zulauf355e49b2020-04-24 15:11:15 -06001429// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001430HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001431 if (!attach_view) return HazardResult();
1432 const auto image_state = attach_view->image_state.get();
1433 if (!image_state) return HazardResult();
1434
John Zulauf355e49b2020-04-24 15:11:15 -06001435 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001436 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001437
1438 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001439 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1440 const auto merged_barrier = MergeBarriers(track_back.barriers);
1441 HazardResult hazard =
1442 track_back.context->DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
1443 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001444 if (!hazard.hazard) {
1445 // The Async hazard check is against the current context's async set.
John Zulaufa0a98292020-09-18 09:30:10 -06001446 hazard = DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001447 attach_view->normalized_subresource_range, kDetectAsync);
1448 }
John Zulaufa0a98292020-09-18 09:30:10 -06001449
John Zulauf355e49b2020-04-24 15:11:15 -06001450 return hazard;
1451}
1452
John Zulaufb02c1eb2020-10-06 16:33:36 -06001453void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
1454 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1455 const ResourceUsageTag &tag) {
1456 const auto &transitions = rp_state.subpass_transitions[subpass];
1457 for (const auto &transition : transitions) {
1458 const auto prev_pass = transition.prev_pass;
1459 const auto attachment_view = attachment_views[transition.attachment];
1460 if (!attachment_view) continue;
1461 const auto *image = attachment_view->image_state.get();
1462 if (!image) continue;
1463 if (!SimpleBinding(*image)) continue;
1464
1465 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1466 assert(trackback);
1467
1468 // Import the attachments into the current context
1469 const auto *prev_context = trackback->context;
1470 assert(prev_context);
1471 const auto address_type = ImageAddressType(*image);
1472 auto &target_map = GetAccessStateMap(address_type);
1473 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
1474 prev_context->ResolveAccessRange(*image, attachment_view->normalized_subresource_range, barrier_action, address_type,
1475 &target_map, nullptr);
1476 }
1477
John Zulauf86356ca2020-10-19 11:46:41 -06001478 // If there were no transitions skip this global map walk
1479 if (transitions.size()) {
1480 ApplyBarrierOpsFunctor apply_pending_action(true /* resolve */, 0, tag);
1481 ApplyGlobalBarriers(apply_pending_action);
1482 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001483}
1484
John Zulauf355e49b2020-04-24 15:11:15 -06001485// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1486bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1487
1488 const VkRenderPassBeginInfo *pRenderPassBegin,
1489 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1490 const char *func_name) const {
1491 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1492 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06001493
John Zulauf86356ca2020-10-19 11:46:41 -06001494 assert(pRenderPassBegin);
1495 if (nullptr == pRenderPassBegin) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06001496
John Zulauf86356ca2020-10-19 11:46:41 -06001497 const uint32_t subpass = 0;
John Zulauf355e49b2020-04-24 15:11:15 -06001498
John Zulauf86356ca2020-10-19 11:46:41 -06001499 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
1500 // hasn't happened yet)
1501 const std::vector<AccessContext> empty_context_vector;
1502 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1503 const_cast<AccessContext *>(&cb_access_context_));
John Zulauf355e49b2020-04-24 15:11:15 -06001504
John Zulauf86356ca2020-10-19 11:46:41 -06001505 // Create a view list
1506 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1507 assert(fb_state);
1508 if (nullptr == fb_state) return skip;
1509 // NOTE: Must not use COMMAND_BUFFER_STATE variant of this as RecordCmdBeginRenderPass hasn't run and thus
1510 // the activeRenderPass.* fields haven't been set.
1511 const auto views = sync_state_->GetAttachmentViews(*pRenderPassBegin, *fb_state);
1512
1513 // Validate transitions
1514 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
1515
1516 // Validate load operations if there were no layout transition hazards
1517 if (!skip) {
1518 temp_context.RecordLayoutTransitions(rp_state, subpass, views, kCurrentCommandTag);
1519 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001520 }
John Zulauf86356ca2020-10-19 11:46:41 -06001521
John Zulauf355e49b2020-04-24 15:11:15 -06001522 return skip;
1523}
1524
locke-lunarg61870c22020-06-09 14:51:50 -06001525bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1526 const char *func_name) const {
1527 bool skip = false;
1528 const PIPELINE_STATE *pPipe = nullptr;
1529 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1530 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1531 if (!pPipe || !per_sets) {
1532 return skip;
1533 }
1534
1535 using DescriptorClass = cvdescriptorset::DescriptorClass;
1536 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1537 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1538 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1539 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1540
1541 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarg37047832020-06-12 13:44:45 -06001542 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001543 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1544 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001545 for (const auto &set_binding : stage_state.descriptor_uses) {
1546 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1547 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1548 set_binding.first.second);
1549 const auto descriptor_type = binding_it.GetType();
1550 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1551 auto array_idx = 0;
1552
1553 if (binding_it.IsVariableDescriptorCount()) {
1554 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1555 }
1556 SyncStageAccessIndex sync_index =
1557 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1558
1559 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1560 uint32_t index = i - index_range.start;
1561 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1562 switch (descriptor->GetClass()) {
1563 case DescriptorClass::ImageSampler:
1564 case DescriptorClass::Image: {
1565 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001566 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001567 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001568 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1569 img_view_state = image_sampler_descriptor->GetImageViewState();
1570 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001571 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001572 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1573 img_view_state = image_descriptor->GetImageViewState();
1574 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001575 }
1576 if (!img_view_state) continue;
1577 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1578 VkExtent3D extent = {};
1579 VkOffset3D offset = {};
1580 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1581 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1582 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1583 } else {
1584 extent = img_state->createInfo.extent;
1585 }
John Zulauf361fb532020-07-22 10:45:39 -06001586 HazardResult hazard;
1587 const auto &subresource_range = img_view_state->normalized_subresource_range;
1588 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1589 // Input attachments are subject to raster ordering rules
1590 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
1591 kAttachmentRasterOrder, offset, extent);
1592 } else {
1593 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1594 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001595 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001596 skip |= sync_state_->LogError(
1597 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001598 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1599 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001600 func_name, string_SyncHazard(hazard.hazard),
1601 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1602 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1603 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001604 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1605 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1606 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001607 }
1608 break;
1609 }
1610 case DescriptorClass::TexelBuffer: {
1611 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1612 if (!buf_view_state) continue;
1613 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001614 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001615 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001616 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001617 skip |= sync_state_->LogError(
1618 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001619 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1620 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001621 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1622 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1623 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001624 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1625 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1626 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001627 }
1628 break;
1629 }
1630 case DescriptorClass::GeneralBuffer: {
1631 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1632 auto buf_state = buffer_descriptor->GetBufferState();
1633 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001634 const ResourceAccessRange range =
1635 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001636 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001637 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001638 skip |= sync_state_->LogError(
1639 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001640 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1641 func_name, string_SyncHazard(hazard.hazard),
1642 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001643 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1644 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001645 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1646 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1647 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001648 }
1649 break;
1650 }
1651 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1652 default:
1653 break;
1654 }
1655 }
1656 }
1657 }
1658 return skip;
1659}
1660
1661void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1662 const ResourceUsageTag &tag) {
1663 const PIPELINE_STATE *pPipe = nullptr;
1664 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1665 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1666 if (!pPipe || !per_sets) {
1667 return;
1668 }
1669
1670 using DescriptorClass = cvdescriptorset::DescriptorClass;
1671 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1672 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1673 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1674 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1675
1676 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001677 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
1678 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1679 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001680 for (const auto &set_binding : stage_state.descriptor_uses) {
1681 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1682 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1683 set_binding.first.second);
1684 const auto descriptor_type = binding_it.GetType();
1685 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1686 auto array_idx = 0;
1687
1688 if (binding_it.IsVariableDescriptorCount()) {
1689 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1690 }
1691 SyncStageAccessIndex sync_index =
1692 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1693
1694 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1695 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1696 switch (descriptor->GetClass()) {
1697 case DescriptorClass::ImageSampler:
1698 case DescriptorClass::Image: {
1699 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1700 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1701 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1702 } else {
1703 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1704 }
1705 if (!img_view_state) continue;
1706 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1707 VkExtent3D extent = {};
1708 VkOffset3D offset = {};
1709 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1710 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1711 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1712 } else {
1713 extent = img_state->createInfo.extent;
1714 }
1715 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1716 offset, extent, tag);
1717 break;
1718 }
1719 case DescriptorClass::TexelBuffer: {
1720 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1721 if (!buf_view_state) continue;
1722 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001723 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001724 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1725 break;
1726 }
1727 case DescriptorClass::GeneralBuffer: {
1728 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1729 auto buf_state = buffer_descriptor->GetBufferState();
1730 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001731 const ResourceAccessRange range =
1732 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001733 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1734 break;
1735 }
1736 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1737 default:
1738 break;
1739 }
1740 }
1741 }
1742 }
1743}
1744
1745bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1746 bool skip = false;
1747 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1748 if (!pPipe) {
1749 return skip;
1750 }
1751
1752 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1753 const auto &binding_buffers_size = binding_buffers.size();
1754 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1755
1756 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1757 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1758 if (binding_description.binding < binding_buffers_size) {
1759 const auto &binding_buffer = binding_buffers[binding_description.binding];
1760 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1761
1762 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06001763 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1764 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001765 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1766 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001767 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001768 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 -06001769 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001770 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001771 }
1772 }
1773 }
1774 return skip;
1775}
1776
1777void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
1778 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1779 if (!pPipe) {
1780 return;
1781 }
1782 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1783 const auto &binding_buffers_size = binding_buffers.size();
1784 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1785
1786 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1787 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1788 if (binding_description.binding < binding_buffers_size) {
1789 const auto &binding_buffer = binding_buffers[binding_description.binding];
1790 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1791
1792 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06001793 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1794 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001795 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1796 }
1797 }
1798}
1799
1800bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1801 bool skip = false;
1802 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
1803
1804 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1805 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001806 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1807 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001808 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1809 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001810 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001811 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 -06001812 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001813 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001814 }
1815
1816 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1817 // We will detect more accurate range in the future.
1818 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1819 return skip;
1820}
1821
1822void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
1823 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return;
1824
1825 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1826 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001827 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1828 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001829 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1830
1831 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1832 // We will detect more accurate range in the future.
1833 RecordDrawVertex(UINT32_MAX, 0, tag);
1834}
1835
1836bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06001837 bool skip = false;
1838 if (!current_renderpass_context_) return skip;
1839 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1840 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1841 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001842}
1843
1844void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
locke-lunarg7077d502020-06-18 21:37:26 -06001845 if (current_renderpass_context_)
1846 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1847 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001848}
1849
John Zulauf355e49b2020-04-24 15:11:15 -06001850bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001851 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001852 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06001853 skip |=
1854 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001855
1856 return skip;
1857}
1858
1859bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1860 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001861 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001862 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001863 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06001864 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1865 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001866
1867 return skip;
1868}
1869
1870void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1871 assert(sync_state_);
1872 if (!cb_state_) return;
1873
1874 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06001875 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06001876 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06001877 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001878 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001879}
1880
John Zulauf355e49b2020-04-24 15:11:15 -06001881void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001882 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001883 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001884 current_context_ = &current_renderpass_context_->CurrentContext();
1885}
1886
John Zulauf355e49b2020-04-24 15:11:15 -06001887void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001888 assert(current_renderpass_context_);
1889 if (!current_renderpass_context_) return;
1890
John Zulauf1a224292020-06-30 14:52:13 -06001891 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001892 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001893 current_renderpass_context_ = nullptr;
1894}
1895
locke-lunarg61870c22020-06-09 14:51:50 -06001896bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1897 const VkRect2D &render_area, const char *func_name) const {
1898 bool skip = false;
locke-lunarg96dc9632020-06-10 17:22:18 -06001899 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001900 if (!pPipe ||
1901 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001902 return skip;
1903 }
1904 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001905 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1906 VkExtent3D extent = CastTo3D(render_area.extent);
1907 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001908
John Zulauf1a224292020-06-30 14:52:13 -06001909 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001910 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001911 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1912 for (const auto location : list) {
1913 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1914 continue;
1915 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001916 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1917 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06001918 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001919 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001920 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001921 func_name, string_SyncHazard(hazard.hazard),
1922 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1923 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001924 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001925 }
1926 }
1927 }
locke-lunarg37047832020-06-12 13:44:45 -06001928
1929 // PHASE1 TODO: Add layout based read/vs. write selection.
1930 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1931 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1932 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001933 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001934 bool depth_write = false, stencil_write = false;
1935
1936 // PHASE1 TODO: These validation should be in core_checks.
1937 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1938 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1939 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1940 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1941 depth_write = true;
1942 }
1943 // PHASE1 TODO: It needs to check if stencil is writable.
1944 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1945 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1946 // PHASE1 TODO: These validation should be in core_checks.
1947 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1948 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1949 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1950 stencil_write = true;
1951 }
1952
1953 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1954 if (depth_write) {
1955 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001956 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1957 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001958 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001959 skip |= sync_state.LogError(
1960 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001961 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001962 func_name, string_SyncHazard(hazard.hazard),
1963 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1964 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001965 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001966 }
1967 }
1968 if (stencil_write) {
1969 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001970 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1971 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001972 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001973 skip |= sync_state.LogError(
1974 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001975 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001976 func_name, string_SyncHazard(hazard.hazard),
1977 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1978 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001979 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001980 }
locke-lunarg61870c22020-06-09 14:51:50 -06001981 }
1982 }
1983 return skip;
1984}
1985
locke-lunarg96dc9632020-06-10 17:22:18 -06001986void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1987 const ResourceUsageTag &tag) {
1988 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001989 if (!pPipe ||
1990 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001991 return;
1992 }
1993 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001994 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1995 VkExtent3D extent = CastTo3D(render_area.extent);
1996 VkOffset3D offset = CastTo3D(render_area.offset);
1997
John Zulauf1a224292020-06-30 14:52:13 -06001998 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001999 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002000 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2001 for (const auto location : list) {
2002 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
2003 continue;
2004 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002005 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
2006 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002007 }
2008 }
locke-lunarg37047832020-06-12 13:44:45 -06002009
2010 // PHASE1 TODO: Add layout based read/vs. write selection.
2011 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
2012 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
2013 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002014 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002015 bool depth_write = false, stencil_write = false;
2016
2017 // PHASE1 TODO: These validation should be in core_checks.
2018 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
2019 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2020 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
2021 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2022 depth_write = true;
2023 }
2024 // PHASE1 TODO: It needs to check if stencil is writable.
2025 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2026 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2027 // PHASE1 TODO: These validation should be in core_checks.
2028 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
2029 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
2030 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2031 stencil_write = true;
2032 }
2033
2034 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2035 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002036 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2037 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002038 }
2039 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002040 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2041 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002042 }
locke-lunarg61870c22020-06-09 14:51:50 -06002043 }
2044}
2045
John Zulauf1507ee42020-05-18 11:33:09 -06002046bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
2047 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002048 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002049 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06002050 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2051 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002052 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2053 func_name);
2054
John Zulauf355e49b2020-04-24 15:11:15 -06002055 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002056 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06002057 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002058 if (!skip) {
2059 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2060 // on a copy of the (empty) next context.
2061 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2062 AccessContext temp_context(next_context);
2063 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
2064 skip |= temp_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
2065 }
John Zulauf7635de32020-05-29 17:14:15 -06002066 return skip;
2067}
2068bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
2069 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002070 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002071 bool skip = false;
2072 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2073 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002074 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2075 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06002076 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002077 return skip;
2078}
2079
John Zulauf7635de32020-05-29 17:14:15 -06002080AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2081 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2082}
2083
2084bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2085 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002086 bool skip = false;
2087
John Zulauf7635de32020-05-29 17:14:15 -06002088 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2089 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2090 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2091 // to apply and only copy then, if this proves a hot spot.
2092 std::unique_ptr<AccessContext> proxy_for_current;
2093
John Zulauf355e49b2020-04-24 15:11:15 -06002094 // Validate the "finalLayout" transitions to external
2095 // Get them from where there we're hidding in the extra entry.
2096 const auto &final_transitions = rp_state_->subpass_transitions.back();
2097 for (const auto &transition : final_transitions) {
2098 const auto &attach_view = attachment_views_[transition.attachment];
2099 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2100 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002101 auto *context = trackback.context;
2102
2103 if (transition.prev_pass == current_subpass_) {
2104 if (!proxy_for_current) {
2105 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2106 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2107 }
2108 context = proxy_for_current.get();
2109 }
2110
John Zulaufa0a98292020-09-18 09:30:10 -06002111 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2112 const auto merged_barrier = MergeBarriers(trackback.barriers);
2113 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope,
2114 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2115 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002116 if (hazard.hazard) {
2117 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2118 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf389c34b2020-07-28 11:19:35 -06002119 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002120 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf389c34b2020-07-28 11:19:35 -06002121 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf37ceaed2020-07-03 16:18:15 -06002122 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002123 }
2124 }
2125 return skip;
2126}
2127
2128void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2129 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002130 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002131}
2132
John Zulauf1507ee42020-05-18 11:33:09 -06002133void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2134 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2135 auto &subpass_context = subpass_contexts_[current_subpass_];
2136 VkExtent3D extent = CastTo3D(render_area.extent);
2137 VkOffset3D offset = CastTo3D(render_area.offset);
2138
2139 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2140 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2141 if (attachment_views_[i] == nullptr) continue; // UNUSED
2142 const auto &view = *attachment_views_[i];
2143 const IMAGE_STATE *image = view.image_state.get();
2144 if (image == nullptr) continue;
2145
2146 const auto &ci = attachment_ci[i];
2147 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002148 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002149 const bool is_color = !(has_depth || has_stencil);
2150
2151 if (is_color) {
2152 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2153 extent, tag);
2154 } else {
2155 auto update_range = view.normalized_subresource_range;
2156 if (has_depth) {
2157 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2158 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2159 }
2160 if (has_stencil) {
2161 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2162 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2163 tag);
2164 }
2165 }
2166 }
2167 }
2168}
2169
John Zulauf355e49b2020-04-24 15:11:15 -06002170void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002171 const AccessContext *external_context, VkQueueFlags queue_flags,
2172 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002173 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002174 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002175 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2176 // Add this for all subpasses here so that they exsist during next subpass validation
2177 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002178 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002179 }
2180 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2181
2182 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002183 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002184}
John Zulauf1507ee42020-05-18 11:33:09 -06002185
2186void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002187 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2188 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002189 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002190
John Zulauf355e49b2020-04-24 15:11:15 -06002191 current_subpass_++;
2192 assert(current_subpass_ < subpass_contexts_.size());
2193 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002194 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002195}
2196
John Zulauf1a224292020-06-30 14:52:13 -06002197void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2198 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002199 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002200 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002201 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002202
John Zulauf355e49b2020-04-24 15:11:15 -06002203 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002204 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002205
2206 // Add the "finalLayout" transitions to external
2207 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002208 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2209 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2210 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002211 const auto &final_transitions = rp_state_->subpass_transitions.back();
2212 for (const auto &transition : final_transitions) {
2213 const auto &attachment = attachment_views_[transition.attachment];
2214 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002215 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf89311b42020-09-29 16:28:47 -06002216 ApplyBarrierOpsFunctor barrier_ops(true /* resolve */, last_trackback.barriers, true /* layout transition */, tag);
2217 external_context->UpdateResourceAccess(*attachment->image_state, attachment->normalized_subresource_range, barrier_ops);
John Zulauf355e49b2020-04-24 15:11:15 -06002218 }
2219}
2220
John Zulauf3d84f1b2020-03-09 13:33:25 -06002221SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2222 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2223 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2224 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2225 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2226 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2227 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2228}
2229
John Zulaufb02c1eb2020-10-06 16:33:36 -06002230// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2231void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2232 for (const auto &barrier : barriers) {
2233 ApplyBarrier(barrier, layout_transition);
2234 }
2235}
2236
John Zulauf89311b42020-09-29 16:28:47 -06002237// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2238// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2239// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002240void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2241 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
2242 assert(!pending_write_barriers);
2243 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002244 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002245 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002246 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002247 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002248}
John Zulauf9cb530d2019-09-30 14:14:10 -06002249HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2250 HazardResult hazard;
2251 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002252 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002253 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002254 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002255 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002256 }
2257 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002258 // Write operation:
2259 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2260 // If reads exists -- test only against them because either:
2261 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2262 // * 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
2263 // the current write happens after the reads, so just test the write against the reades
2264 // Otherwise test against last_write
2265 //
2266 // Look for casus belli for WAR
2267 if (last_read_count) {
2268 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2269 const auto &read_access = last_reads[read_index];
2270 if (IsReadHazard(usage_stage, read_access)) {
2271 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2272 break;
2273 }
2274 }
John Zulauf361fb532020-07-22 10:45:39 -06002275 } else if (last_write && IsWriteHazard(usage)) {
2276 // Write-After-Write check -- if we have a previous write to test against
2277 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002278 }
2279 }
2280 return hazard;
2281}
2282
John Zulauf69133422020-05-20 14:55:53 -06002283HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2284 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2285 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002286 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002287 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002288 const bool input_attachment_ordering = 0 != (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
2289 const bool last_write_is_ordered = 0 != (last_write & ordering.access_scope);
2290 if (IsRead(usage_bit)) {
2291 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2292 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2293 if (is_raw_hazard) {
2294 // NOTE: we know last_write is non-zero
2295 // See if the ordering rules save us from the simple RAW check above
2296 // First check to see if the current usage is covered by the ordering rules
2297 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2298 const bool usage_is_ordered =
2299 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2300 if (usage_is_ordered) {
2301 // Now see of the most recent write (or a subsequent read) are ordered
2302 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2303 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002304 }
2305 }
John Zulauf4285ee92020-09-23 10:20:52 -06002306 if (is_raw_hazard) {
2307 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2308 }
John Zulauf361fb532020-07-22 10:45:39 -06002309 } else {
2310 // Only check for WAW if there are no reads since last_write
John Zulauf4285ee92020-09-23 10:20:52 -06002311 bool usage_write_is_ordered = 0 != (usage_bit & ordering.access_scope);
John Zulauf361fb532020-07-22 10:45:39 -06002312 if (last_read_count) {
John Zulauf361fb532020-07-22 10:45:39 -06002313 // Look for any WAR hazards outside the ordered set of stages
John Zulauf4285ee92020-09-23 10:20:52 -06002314 VkPipelineStageFlags ordered_stages = 0;
2315 if (usage_write_is_ordered) {
2316 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2317 ordered_stages = GetOrderedStages(ordering);
2318 }
2319 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2320 if ((ordered_stages & last_read_stages) != last_read_stages) {
2321 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2322 const auto &read_access = last_reads[read_index];
2323 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2324 if (IsReadHazard(usage_stage, read_access)) {
2325 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2326 break;
2327 }
John Zulaufd14743a2020-07-03 09:42:39 -06002328 }
2329 }
John Zulauf4285ee92020-09-23 10:20:52 -06002330 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
2331 if (last_write && IsWriteHazard(usage_bit)) {
2332 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002333 }
John Zulauf69133422020-05-20 14:55:53 -06002334 }
2335 }
2336 return hazard;
2337}
2338
John Zulauf2f952d22020-02-10 11:34:51 -07002339// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06002340HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002341 HazardResult hazard;
2342 auto usage = FlagBit(usage_index);
2343 if (IsRead(usage)) {
2344 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002345 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002346 }
2347 } else {
2348 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002349 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002350 } else if (last_read_count > 0) {
John Zulauf4285ee92020-09-23 10:20:52 -06002351 // Any read could be reported, so we'll just pick the first one arbitrarily
John Zulauf59e25072020-07-17 10:55:21 -06002352 hazard.Set(this, usage_index, WRITE_RACING_READ, last_reads[0].access, last_reads[0].tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002353 }
2354 }
2355 return hazard;
2356}
2357
John Zulauf36bcf6a2020-02-03 15:12:52 -07002358HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
2359 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002360 // Only supporting image layout transitions for now
2361 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2362 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002363 // only test for WAW if there no intervening read operations.
2364 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2365 if (last_read_count) {
John Zulauf355e49b2020-04-24 15:11:15 -06002366 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002367 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002368 const auto &read_access = last_reads[read_index];
2369 // If the read stage is not in the src sync sync
2370 // *AND* not execution chained with an existing sync barrier (that's the or)
2371 // then the barrier access is unsafe (R/W after R)
2372 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002373 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002374 break;
2375 }
2376 }
John Zulauf361fb532020-07-22 10:45:39 -06002377 } else if (last_write) {
2378 // If the previous write is *not* in the 1st access scope
2379 // *AND* the current barrier is not in the dependency chain
2380 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2381 // then the barrier access is unsafe (R/W after W)
2382 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
2383 // TODO: Do we need a difference hazard name for this?
2384 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2385 }
John Zulaufd14743a2020-07-03 09:42:39 -06002386 }
John Zulauf361fb532020-07-22 10:45:39 -06002387
John Zulauf0cb5be22020-01-23 12:18:22 -07002388 return hazard;
2389}
2390
John Zulauf5f13a792020-03-10 07:31:21 -06002391// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2392// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2393// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2394void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2395 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002396 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2397 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002398 *this = other;
2399 } else if (!other.write_tag.IsBefore(write_tag)) {
2400 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2401 // dependency chaining logic or any stage expansion)
2402 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002403 pending_write_barriers |= other.pending_write_barriers;
2404 pending_layout_transition |= other.pending_layout_transition;
2405 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002406
John Zulaufd14743a2020-07-03 09:42:39 -06002407 // Merge the read states
John Zulauf4285ee92020-09-23 10:20:52 -06002408 const auto pre_merge_count = last_read_count;
2409 const auto pre_merge_stages = last_read_stages;
John Zulauf5f13a792020-03-10 07:31:21 -06002410 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2411 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002412 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002413 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002414 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2415 // but we should wait on profiling data for that.
2416 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002417 auto &my_read = last_reads[my_read_index];
2418 if (other_read.stage == my_read.stage) {
2419 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002420 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002421 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002422 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002423 my_read.pending_dep_chain = other_read.pending_dep_chain;
2424 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
2425 // May require tracking more than one access per stage.
2426 my_read.barriers = other_read.barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002427 if (my_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
2428 // Since I'm overwriting the fragement stage read, also update the input attachment info
2429 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06002430 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002431 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002432 } else if (other_read.tag.IsBefore(my_read.tag)) {
2433 // The read tags match so merge the barriers
2434 my_read.barriers |= other_read.barriers;
2435 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002436 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002437
John Zulauf5f13a792020-03-10 07:31:21 -06002438 break;
2439 }
2440 }
2441 } else {
2442 // The other read stage doesn't exist in this, so add it.
2443 last_reads[last_read_count] = other_read;
2444 last_read_count++;
2445 last_read_stages |= other_read.stage;
John Zulauf4285ee92020-09-23 10:20:52 -06002446 if (other_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06002447 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002448 }
John Zulauf5f13a792020-03-10 07:31:21 -06002449 }
2450 }
John Zulauf361fb532020-07-22 10:45:39 -06002451 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002452 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2453 // ignore it.
John Zulauf5f13a792020-03-10 07:31:21 -06002454}
2455
John Zulauf9cb530d2019-09-30 14:14:10 -06002456void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2457 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2458 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002459 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002460 // Mulitple outstanding reads may be of interest and do dependency chains independently
2461 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2462 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002463 uint32_t update_index = kStageCount;
John Zulauf9cb530d2019-09-30 14:14:10 -06002464 if (usage_stage & last_read_stages) {
2465 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf4285ee92020-09-23 10:20:52 -06002466 if (last_reads[read_index].stage == usage_stage) {
2467 update_index = read_index;
John Zulauf9cb530d2019-09-30 14:14:10 -06002468 break;
2469 }
2470 }
John Zulauf4285ee92020-09-23 10:20:52 -06002471 assert(update_index < last_read_count);
John Zulauf9cb530d2019-09-30 14:14:10 -06002472 } else {
John Zulauf9cb530d2019-09-30 14:14:10 -06002473 assert(last_read_count < last_reads.size());
John Zulauf4285ee92020-09-23 10:20:52 -06002474 update_index = last_read_count++;
John Zulauf9cb530d2019-09-30 14:14:10 -06002475 last_read_stages |= usage_stage;
2476 }
John Zulauf4285ee92020-09-23 10:20:52 -06002477 last_reads[update_index].Set(usage_stage, usage_bit, 0, tag);
2478
2479 // Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
2480 if (usage_stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06002481 // TODO Revisit re: multiple reads for a given stage
2482 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002483 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002484 } else {
2485 // Assume write
2486 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002487 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002488 }
2489}
John Zulauf5f13a792020-03-10 07:31:21 -06002490
John Zulauf89311b42020-09-29 16:28:47 -06002491// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2492// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2493// We can overwrite them as *this* write is now after them.
2494//
2495// Note: intentionally ignore pending barriers and chains (i.e. don't apply or clear them), let ApplyPendingBarriers handle them.
2496void ResourceAccessState::SetWrite(SyncStageAccessFlagBits usage_bit, const ResourceUsageTag &tag) {
2497 last_read_count = 0;
2498 last_read_stages = 0;
2499 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06002500 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06002501
2502 write_barriers = 0;
2503 write_dependency_chain = 0;
2504 write_tag = tag;
2505 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002506}
2507
John Zulauf89311b42020-09-29 16:28:47 -06002508// Apply the memory barrier without updating the existing barriers. The execution barrier
2509// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2510// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2511// replace the current write barriers or add to them, so accumulate to pending as well.
2512void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2513 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2514 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06002515 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
2516 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
2517 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
2518 // transistion *as* a write and in scope with the barrier (it's before visibility).
2519 if (layout_transition || InSourceScopeOrChain(barrier.src_exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06002520 pending_write_barriers |= barrier.dst_access_scope;
2521 pending_write_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002522 }
John Zulauf89311b42020-09-29 16:28:47 -06002523 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2524 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06002525
John Zulauf89311b42020-09-29 16:28:47 -06002526 if (!pending_layout_transition) {
2527 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2528 // don't need to be tracked as we're just going to zero them.
John Zulaufa0a98292020-09-18 09:30:10 -06002529 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf89311b42020-09-29 16:28:47 -06002530 ReadState &access = last_reads[read_index];
2531 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2532 if (barrier.src_exec_scope & (access.stage | access.barriers)) {
2533 access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002534 }
2535 }
John Zulaufa0a98292020-09-18 09:30:10 -06002536 }
John Zulaufa0a98292020-09-18 09:30:10 -06002537}
2538
John Zulauf89311b42020-09-29 16:28:47 -06002539void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
2540 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06002541 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
2542 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
2543 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06002544 }
John Zulauf89311b42020-09-29 16:28:47 -06002545
2546 // Apply the accumulate execution barriers (and thus update chaining information)
2547 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
2548 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2549 ReadState &access = last_reads[read_index];
2550 access.barriers |= access.pending_dep_chain;
2551 read_execution_barriers |= access.barriers;
2552 access.pending_dep_chain = 0;
2553 }
2554
2555 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
2556 write_dependency_chain |= pending_write_dep_chain;
2557 write_barriers |= pending_write_barriers;
2558 pending_write_dep_chain = 0;
2559 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002560}
2561
John Zulauf59e25072020-07-17 10:55:21 -06002562// This should be just Bits or Index, but we don't have an invalid state for Index
2563VkPipelineStageFlags ResourceAccessState::GetReadBarriers(SyncStageAccessFlags usage_bit) const {
2564 VkPipelineStageFlags barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06002565
2566 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2567 const auto &read_access = last_reads[read_index];
2568 if (read_access.access & usage_bit) {
2569 barriers = read_access.barriers;
2570 break;
John Zulauf59e25072020-07-17 10:55:21 -06002571 }
2572 }
John Zulauf4285ee92020-09-23 10:20:52 -06002573
John Zulauf59e25072020-07-17 10:55:21 -06002574 return barriers;
2575}
2576
John Zulauf4285ee92020-09-23 10:20:52 -06002577inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlagBits usage_stage, SyncStageAccessFlagBits usage) const {
2578 assert(IsRead(usage));
2579 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
2580 // * the previous reads are not hazards, and thus last_write must be visible and available to
2581 // any reads that happen after.
2582 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2583 // the current read will be also not be a hazard, thus reporting a hazard here adds no needed information.
2584 return (0 != last_write) && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
2585}
2586
John Zulauf4285ee92020-09-23 10:20:52 -06002587VkPipelineStageFlags ResourceAccessState::GetOrderedStages(const SyncOrderingBarrier &ordering) const {
2588 // Whether the stage are in the ordering scope only matters if the current write is ordered
2589 VkPipelineStageFlags ordered_stages = last_read_stages & ordering.exec_scope;
2590 // Special input attachment handling as always (not encoded in exec_scop)
John Zulauf89311b42020-09-29 16:28:47 -06002591 const bool input_attachment_ordering = 0 != (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauff51fbb62020-10-02 14:43:24 -06002592 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06002593 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
2594 ordered_stages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
2595 }
2596
2597 return ordered_stages;
2598}
2599
2600inline ResourceAccessState::ReadState *ResourceAccessState::GetReadStateForStage(VkPipelineStageFlagBits stage,
2601 uint32_t search_limit) {
2602 ReadState *read_state = nullptr;
2603 search_limit = std::min(search_limit, last_read_count);
2604 for (uint32_t i = 0; i < search_limit; i++) {
2605 if (last_reads[i].stage == stage) {
2606 read_state = &last_reads[i];
2607 break;
2608 }
2609 }
2610 return read_state;
2611}
2612
John Zulaufd1f85d42020-04-15 12:23:15 -06002613void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002614 auto *access_context = GetAccessContextNoInsert(command_buffer);
2615 if (access_context) {
2616 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002617 }
2618}
2619
John Zulaufd1f85d42020-04-15 12:23:15 -06002620void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2621 auto access_found = cb_access_state.find(command_buffer);
2622 if (access_found != cb_access_state.end()) {
2623 access_found->second->Reset();
2624 cb_access_state.erase(access_found);
2625 }
2626}
2627
John Zulauf89311b42020-09-29 16:28:47 -06002628void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2629 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags src_access_scope,
2630 SyncStageAccessFlags dst_access_scope, uint32_t memory_barrier_count,
2631 const VkMemoryBarrier *pMemoryBarriers, const ResourceUsageTag &tag) {
2632 ApplyBarrierOpsFunctor barriers_functor(true /* resolve */, std::min<uint32_t>(1, memory_barrier_count), tag);
2633 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
2634 const auto &barrier = pMemoryBarriers[barrier_index];
2635 SyncBarrier sync_barrier(src_exec_scope, SyncStageAccess::AccessScope(src_access_scope, barrier.srcAccessMask),
2636 dst_exec_scope, SyncStageAccess::AccessScope(dst_access_scope, barrier.dstAccessMask));
2637 barriers_functor.PushBack(sync_barrier, false);
2638 }
2639 if (0 == memory_barrier_count) {
2640 // If there are no global memory barriers, force an exec barrier
2641 barriers_functor.PushBack(SyncBarrier(src_exec_scope, 0, dst_exec_scope, 0), false);
2642 }
John Zulauf540266b2020-04-06 18:54:53 -06002643 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002644}
2645
John Zulauf540266b2020-04-06 18:54:53 -06002646void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002647 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2648 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002649 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002650 for (uint32_t index = 0; index < barrier_count; index++) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002651 auto barrier = barriers[index]; // barrier is a copy
John Zulauf9cb530d2019-09-30 14:14:10 -06002652 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2653 if (!buffer) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002654 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
2655 const ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002656 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2657 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06002658 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2659 const ApplyBarrierFunctor update_action(sync_barrier, false /* layout_transition */);
2660 context->UpdateResourceAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002661 }
2662}
2663
John Zulauf540266b2020-04-06 18:54:53 -06002664void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2665 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2666 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002667 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002668 for (uint32_t index = 0; index < barrier_count; index++) {
2669 const auto &barrier = barriers[index];
2670 const auto *image = Get<IMAGE_STATE>(barrier.image);
2671 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002672 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002673 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2674 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2675 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06002676 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2677 const ApplyBarrierFunctor barrier_action(sync_barrier, layout_transition);
2678 context->UpdateResourceAccess(*image, subresource_range, barrier_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002679 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002680}
2681
2682bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2683 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2684 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002685 const auto *cb_context = GetAccessContext(commandBuffer);
2686 assert(cb_context);
2687 if (!cb_context) return skip;
2688 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002689
John Zulauf3d84f1b2020-03-09 13:33:25 -06002690 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002691 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002692 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002693
2694 for (uint32_t region = 0; region < regionCount; region++) {
2695 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002696 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002697 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002698 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002699 if (hazard.hazard) {
2700 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002701 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002702 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002703 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002704 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002705 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002706 }
John Zulauf16adfc92020-04-08 10:28:33 -06002707 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002708 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06002709 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002710 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002711 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002712 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002713 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002714 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002715 }
2716 }
2717 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002718 }
2719 return skip;
2720}
2721
2722void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2723 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002724 auto *cb_context = GetAccessContext(commandBuffer);
2725 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002726 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002727 auto *context = cb_context->GetCurrentAccessContext();
2728
John Zulauf9cb530d2019-09-30 14:14:10 -06002729 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002730 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002731
2732 for (uint32_t region = 0; region < regionCount; region++) {
2733 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002734 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002735 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002736 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002737 }
John Zulauf16adfc92020-04-08 10:28:33 -06002738 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002739 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002740 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002741 }
2742 }
2743}
2744
Jeff Leger178b1e52020-10-05 12:22:23 -04002745bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
2746 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
2747 bool skip = false;
2748 const auto *cb_context = GetAccessContext(commandBuffer);
2749 assert(cb_context);
2750 if (!cb_context) return skip;
2751 const auto *context = cb_context->GetCurrentAccessContext();
2752
2753 // If we have no previous accesses, we have no hazards
2754 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2755 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2756
2757 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2758 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2759 if (src_buffer) {
2760 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2761 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
2762 if (hazard.hazard) {
2763 // TODO -- add tag information to log msg when useful.
2764 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
2765 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
2766 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
2767 region, string_UsageTag(hazard).c_str());
2768 }
2769 }
2770 if (dst_buffer && !skip) {
2771 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2772 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
2773 if (hazard.hazard) {
2774 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
2775 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
2776 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
2777 region, string_UsageTag(hazard).c_str());
2778 }
2779 }
2780 if (skip) break;
2781 }
2782 return skip;
2783}
2784
2785void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
2786 auto *cb_context = GetAccessContext(commandBuffer);
2787 assert(cb_context);
2788 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
2789 auto *context = cb_context->GetCurrentAccessContext();
2790
2791 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2792 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2793
2794 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2795 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2796 if (src_buffer) {
2797 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2798 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
2799 }
2800 if (dst_buffer) {
2801 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2802 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
2803 }
2804 }
2805}
2806
John Zulauf5c5e88d2019-12-26 11:22:02 -07002807bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2808 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2809 const VkImageCopy *pRegions) const {
2810 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002811 const auto *cb_access_context = GetAccessContext(commandBuffer);
2812 assert(cb_access_context);
2813 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002814
John Zulauf3d84f1b2020-03-09 13:33:25 -06002815 const auto *context = cb_access_context->GetCurrentAccessContext();
2816 assert(context);
2817 if (!context) return skip;
2818
2819 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2820 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002821 for (uint32_t region = 0; region < regionCount; region++) {
2822 const auto &copy_region = pRegions[region];
2823 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002824 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002825 copy_region.srcOffset, copy_region.extent);
2826 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002827 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002828 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002829 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002830 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002831 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002832 }
2833
2834 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002835 VkExtent3D dst_copy_extent =
2836 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002837 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002838 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002839 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002840 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002841 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002842 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002843 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002844 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002845 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002846 }
2847 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002848
John Zulauf5c5e88d2019-12-26 11:22:02 -07002849 return skip;
2850}
2851
2852void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2853 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2854 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002855 auto *cb_access_context = GetAccessContext(commandBuffer);
2856 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002857 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002858 auto *context = cb_access_context->GetCurrentAccessContext();
2859 assert(context);
2860
John Zulauf5c5e88d2019-12-26 11:22:02 -07002861 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002862 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002863
2864 for (uint32_t region = 0; region < regionCount; region++) {
2865 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002866 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002867 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2868 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002869 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002870 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002871 VkExtent3D dst_copy_extent =
2872 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002873 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2874 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002875 }
2876 }
2877}
2878
Jeff Leger178b1e52020-10-05 12:22:23 -04002879bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
2880 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
2881 bool skip = false;
2882 const auto *cb_access_context = GetAccessContext(commandBuffer);
2883 assert(cb_access_context);
2884 if (!cb_access_context) return skip;
2885
2886 const auto *context = cb_access_context->GetCurrentAccessContext();
2887 assert(context);
2888 if (!context) return skip;
2889
2890 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2891 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2892 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2893 const auto &copy_region = pCopyImageInfo->pRegions[region];
2894 if (src_image) {
2895 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
2896 copy_region.srcOffset, copy_region.extent);
2897 if (hazard.hazard) {
2898 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
2899 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
2900 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
2901 region, string_UsageTag(hazard).c_str());
2902 }
2903 }
2904
2905 if (dst_image) {
2906 VkExtent3D dst_copy_extent =
2907 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2908 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
2909 copy_region.dstOffset, dst_copy_extent);
2910 if (hazard.hazard) {
2911 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
2912 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
2913 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
2914 region, string_UsageTag(hazard).c_str());
2915 }
2916 if (skip) break;
2917 }
2918 }
2919
2920 return skip;
2921}
2922
2923void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
2924 auto *cb_access_context = GetAccessContext(commandBuffer);
2925 assert(cb_access_context);
2926 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
2927 auto *context = cb_access_context->GetCurrentAccessContext();
2928 assert(context);
2929
2930 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2931 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2932
2933 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2934 const auto &copy_region = pCopyImageInfo->pRegions[region];
2935 if (src_image) {
2936 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2937 copy_region.extent, tag);
2938 }
2939 if (dst_image) {
2940 VkExtent3D dst_copy_extent =
2941 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2942 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2943 dst_copy_extent, tag);
2944 }
2945 }
2946}
2947
John Zulauf9cb530d2019-09-30 14:14:10 -06002948bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2949 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2950 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2951 uint32_t bufferMemoryBarrierCount,
2952 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2953 uint32_t imageMemoryBarrierCount,
2954 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2955 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002956 const auto *cb_access_context = GetAccessContext(commandBuffer);
2957 assert(cb_access_context);
2958 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002959
John Zulauf3d84f1b2020-03-09 13:33:25 -06002960 const auto *context = cb_access_context->GetCurrentAccessContext();
2961 assert(context);
2962 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002963
John Zulauf3d84f1b2020-03-09 13:33:25 -06002964 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002965 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2966 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002967 // Validate Image Layout transitions
2968 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2969 const auto &barrier = pImageMemoryBarriers[index];
2970 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2971 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2972 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002973 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002974 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002975 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002976 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002977 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002978 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002979 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002980 }
2981 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002982
2983 return skip;
2984}
2985
2986void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2987 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2988 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2989 uint32_t bufferMemoryBarrierCount,
2990 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2991 uint32_t imageMemoryBarrierCount,
2992 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002993 auto *cb_access_context = GetAccessContext(commandBuffer);
2994 assert(cb_access_context);
2995 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002996 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002997 auto access_context = cb_access_context->GetCurrentAccessContext();
2998 assert(access_context);
2999 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003000
John Zulauf3d84f1b2020-03-09 13:33:25 -06003001 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003002 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003003 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003004 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
3005 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
3006 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf89311b42020-09-29 16:28:47 -06003007
3008 // These two apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
3009 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
3010 // of the barriers is maintained.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003011 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
3012 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06003013 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06003014 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003015
John Zulauf89311b42020-09-29 16:28:47 -06003016 // Apply the global barriers last as is it walks all memory, it can also clean up the "pending" state without requiring an
3017 // additional pass, updating the dependency chains *last* as it goes along.
3018 // This is needed to guarantee order independence of the three lists.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003019 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf89311b42020-09-29 16:28:47 -06003020 pMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003021}
3022
3023void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3024 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3025 // The state tracker sets up the device state
3026 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3027
John Zulauf5f13a792020-03-10 07:31:21 -06003028 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3029 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003030 // TODO: Find a good way to do this hooklessly.
3031 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3032 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3033 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3034
John Zulaufd1f85d42020-04-15 12:23:15 -06003035 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3036 sync_device_state->ResetCommandBufferCallback(command_buffer);
3037 });
3038 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3039 sync_device_state->FreeCommandBufferCallback(command_buffer);
3040 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003041}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003042
John Zulauf355e49b2020-04-24 15:11:15 -06003043bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3044 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
3045 bool skip = false;
3046 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
3047 auto cb_context = GetAccessContext(commandBuffer);
3048
3049 if (rp_state && cb_context) {
3050 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
3051 }
3052
3053 return skip;
3054}
3055
3056bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3057 VkSubpassContents contents) const {
3058 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3059 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3060 subpass_begin_info.contents = contents;
3061 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
3062 return skip;
3063}
3064
3065bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3066 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
3067 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3068 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
3069 return skip;
3070}
3071
3072bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3073 const VkRenderPassBeginInfo *pRenderPassBegin,
3074 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
3075 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3076 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
3077 return skip;
3078}
3079
John Zulauf3d84f1b2020-03-09 13:33:25 -06003080void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3081 VkResult result) {
3082 // The state tracker sets up the command buffer state
3083 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3084
3085 // Create/initialize the structure that trackers accesses at the command buffer scope.
3086 auto cb_access_context = GetAccessContext(commandBuffer);
3087 assert(cb_access_context);
3088 cb_access_context->Reset();
3089}
3090
3091void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06003092 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003093 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003094 if (cb_context) {
3095 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003096 }
3097}
3098
3099void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3100 VkSubpassContents contents) {
3101 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3102 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3103 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003104 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003105}
3106
3107void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3108 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3109 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003110 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003111}
3112
3113void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3114 const VkRenderPassBeginInfo *pRenderPassBegin,
3115 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3116 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003117 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
3118}
3119
3120bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
3121 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
3122 bool skip = false;
3123
3124 auto cb_context = GetAccessContext(commandBuffer);
3125 assert(cb_context);
3126 auto cb_state = cb_context->GetCommandBufferState();
3127 if (!cb_state) return skip;
3128
3129 auto rp_state = cb_state->activeRenderPass;
3130 if (!rp_state) return skip;
3131
3132 skip |= cb_context->ValidateNextSubpass(func_name);
3133
3134 return skip;
3135}
3136
3137bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3138 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
3139 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3140 subpass_begin_info.contents = contents;
3141 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
3142 return skip;
3143}
3144
3145bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
3146 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3147 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3148 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
3149 return skip;
3150}
3151
3152bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3153 const VkSubpassEndInfo *pSubpassEndInfo) const {
3154 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3155 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
3156 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003157}
3158
3159void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003160 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003161 auto cb_context = GetAccessContext(commandBuffer);
3162 assert(cb_context);
3163 auto cb_state = cb_context->GetCommandBufferState();
3164 if (!cb_state) return;
3165
3166 auto rp_state = cb_state->activeRenderPass;
3167 if (!rp_state) return;
3168
John Zulauf355e49b2020-04-24 15:11:15 -06003169 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003170}
3171
3172void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3173 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
3174 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3175 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003176 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003177}
3178
3179void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3180 const VkSubpassEndInfo *pSubpassEndInfo) {
3181 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003182 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003183}
3184
3185void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3186 const VkSubpassEndInfo *pSubpassEndInfo) {
3187 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003188 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003189}
3190
John Zulauf355e49b2020-04-24 15:11:15 -06003191bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
3192 const char *func_name) const {
3193 bool skip = false;
3194
3195 auto cb_context = GetAccessContext(commandBuffer);
3196 assert(cb_context);
3197 auto cb_state = cb_context->GetCommandBufferState();
3198 if (!cb_state) return skip;
3199
3200 auto rp_state = cb_state->activeRenderPass;
3201 if (!rp_state) return skip;
3202
3203 skip |= cb_context->ValidateEndRenderpass(func_name);
3204 return skip;
3205}
3206
3207bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3208 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
3209 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
3210 return skip;
3211}
3212
3213bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
3214 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3215 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
3216 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
3217 return skip;
3218}
3219
3220bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
3221 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3222 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
3223 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
3224 return skip;
3225}
3226
3227void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3228 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003229 // Resolve the all subpass contexts to the command buffer contexts
3230 auto cb_context = GetAccessContext(commandBuffer);
3231 assert(cb_context);
3232 auto cb_state = cb_context->GetCommandBufferState();
3233 if (!cb_state) return;
3234
locke-lunargaecf2152020-05-12 17:15:41 -06003235 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06003236 if (!rp_state) return;
3237
John Zulauf355e49b2020-04-24 15:11:15 -06003238 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06003239}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003240
John Zulauf33fc1d52020-07-17 11:01:10 -06003241// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3242// updates to a resource which do not conflict at the byte level.
3243// TODO: Revisit this rule to see if it needs to be tighter or looser
3244// TODO: Add programatic control over suppression heuristics
3245bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3246 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3247}
3248
John Zulauf3d84f1b2020-03-09 13:33:25 -06003249void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003250 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003251 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003252}
3253
3254void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003255 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003256 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003257}
3258
3259void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003260 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003261 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003262}
locke-lunarga19c71d2020-03-02 18:17:04 -07003263
Jeff Leger178b1e52020-10-05 12:22:23 -04003264template <typename BufferImageCopyRegionType>
3265bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3266 VkImageLayout dstImageLayout, uint32_t regionCount,
3267 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003268 bool skip = false;
3269 const auto *cb_access_context = GetAccessContext(commandBuffer);
3270 assert(cb_access_context);
3271 if (!cb_access_context) return skip;
3272
Jeff Leger178b1e52020-10-05 12:22:23 -04003273 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3274 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3275
locke-lunarga19c71d2020-03-02 18:17:04 -07003276 const auto *context = cb_access_context->GetCurrentAccessContext();
3277 assert(context);
3278 if (!context) return skip;
3279
3280 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003281 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3282
3283 for (uint32_t region = 0; region < regionCount; region++) {
3284 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003285 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003286 ResourceAccessRange src_range =
3287 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003288 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003289 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003290 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003291 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003292 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003293 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003294 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003295 }
3296 }
3297 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003298 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003299 copy_region.imageOffset, copy_region.imageExtent);
3300 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003301 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003302 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003303 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003304 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003305 }
3306 if (skip) break;
3307 }
3308 if (skip) break;
3309 }
3310 return skip;
3311}
3312
Jeff Leger178b1e52020-10-05 12:22:23 -04003313bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3314 VkImageLayout dstImageLayout, uint32_t regionCount,
3315 const VkBufferImageCopy *pRegions) const {
3316 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3317 COPY_COMMAND_VERSION_1);
3318}
3319
3320bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3321 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3322 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3323 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3324 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3325}
3326
3327template <typename BufferImageCopyRegionType>
3328void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3329 VkImageLayout dstImageLayout, uint32_t regionCount,
3330 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003331 auto *cb_access_context = GetAccessContext(commandBuffer);
3332 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003333
3334 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3335 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3336
3337 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003338 auto *context = cb_access_context->GetCurrentAccessContext();
3339 assert(context);
3340
3341 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003342 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003343
3344 for (uint32_t region = 0; region < regionCount; region++) {
3345 const auto &copy_region = pRegions[region];
3346 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003347 ResourceAccessRange src_range =
3348 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003349 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003350 }
3351 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003352 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003353 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003354 }
3355 }
3356}
3357
Jeff Leger178b1e52020-10-05 12:22:23 -04003358void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3359 VkImageLayout dstImageLayout, uint32_t regionCount,
3360 const VkBufferImageCopy *pRegions) {
3361 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3362 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3363}
3364
3365void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3366 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3367 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3368 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3369 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3370 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3371}
3372
3373template <typename BufferImageCopyRegionType>
3374bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3375 VkBuffer dstBuffer, uint32_t regionCount,
3376 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003377 bool skip = false;
3378 const auto *cb_access_context = GetAccessContext(commandBuffer);
3379 assert(cb_access_context);
3380 if (!cb_access_context) return skip;
3381
Jeff Leger178b1e52020-10-05 12:22:23 -04003382 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3383 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3384
locke-lunarga19c71d2020-03-02 18:17:04 -07003385 const auto *context = cb_access_context->GetCurrentAccessContext();
3386 assert(context);
3387 if (!context) return skip;
3388
3389 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3390 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3391 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3392 for (uint32_t region = 0; region < regionCount; region++) {
3393 const auto &copy_region = pRegions[region];
3394 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003395 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003396 copy_region.imageOffset, copy_region.imageExtent);
3397 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003398 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003399 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003400 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003401 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003402 }
3403 }
3404 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06003405 ResourceAccessRange dst_range =
3406 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003407 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003408 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003409 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003410 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003411 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003412 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003413 }
3414 }
3415 if (skip) break;
3416 }
3417 return skip;
3418}
3419
Jeff Leger178b1e52020-10-05 12:22:23 -04003420bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3421 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3422 const VkBufferImageCopy *pRegions) const {
3423 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3424 COPY_COMMAND_VERSION_1);
3425}
3426
3427bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3428 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3429 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3430 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3431 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3432}
3433
3434template <typename BufferImageCopyRegionType>
3435void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3436 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3437 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003438 auto *cb_access_context = GetAccessContext(commandBuffer);
3439 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003440
3441 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3442 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3443
3444 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003445 auto *context = cb_access_context->GetCurrentAccessContext();
3446 assert(context);
3447
3448 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003449 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3450 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 -06003451 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003452
3453 for (uint32_t region = 0; region < regionCount; region++) {
3454 const auto &copy_region = pRegions[region];
3455 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003456 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003457 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003458 }
3459 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003460 ResourceAccessRange dst_range =
3461 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003462 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003463 }
3464 }
3465}
3466
Jeff Leger178b1e52020-10-05 12:22:23 -04003467void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3468 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3469 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3470 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3471}
3472
3473void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3474 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3475 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3476 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3477 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3478 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3479}
3480
3481template <typename RegionType>
3482bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3483 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3484 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003485 bool skip = false;
3486 const auto *cb_access_context = GetAccessContext(commandBuffer);
3487 assert(cb_access_context);
3488 if (!cb_access_context) return skip;
3489
3490 const auto *context = cb_access_context->GetCurrentAccessContext();
3491 assert(context);
3492 if (!context) return skip;
3493
3494 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3495 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3496
3497 for (uint32_t region = 0; region < regionCount; region++) {
3498 const auto &blit_region = pRegions[region];
3499 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003500 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3501 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3502 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3503 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3504 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3505 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3506 auto hazard =
3507 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003508 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003509 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003510 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003511 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003512 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003513 }
3514 }
3515
3516 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003517 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3518 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3519 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3520 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3521 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3522 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3523 auto hazard =
3524 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003525 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003526 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003527 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003528 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003529 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003530 }
3531 if (skip) break;
3532 }
3533 }
3534
3535 return skip;
3536}
3537
Jeff Leger178b1e52020-10-05 12:22:23 -04003538bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3539 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3540 const VkImageBlit *pRegions, VkFilter filter) const {
3541 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3542 "vkCmdBlitImage");
3543}
3544
3545bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3546 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3547 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3548 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3549 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3550}
3551
3552template <typename RegionType>
3553void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3554 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3555 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003556 auto *cb_access_context = GetAccessContext(commandBuffer);
3557 assert(cb_access_context);
3558 auto *context = cb_access_context->GetCurrentAccessContext();
3559 assert(context);
3560
3561 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003562 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003563
3564 for (uint32_t region = 0; region < regionCount; region++) {
3565 const auto &blit_region = pRegions[region];
3566 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003567 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3568 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3569 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3570 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3571 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3572 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3573 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003574 }
3575 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003576 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3577 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3578 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3579 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3580 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3581 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3582 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003583 }
3584 }
3585}
locke-lunarg36ba2592020-04-03 09:42:04 -06003586
Jeff Leger178b1e52020-10-05 12:22:23 -04003587void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3588 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3589 const VkImageBlit *pRegions, VkFilter filter) {
3590 auto *cb_access_context = GetAccessContext(commandBuffer);
3591 assert(cb_access_context);
3592 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3593 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3594 pRegions, filter);
3595 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3596}
3597
3598void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3599 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3600 auto *cb_access_context = GetAccessContext(commandBuffer);
3601 assert(cb_access_context);
3602 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3603 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3604 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3605 pBlitImageInfo->filter, tag);
3606}
3607
locke-lunarg61870c22020-06-09 14:51:50 -06003608bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
3609 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
3610 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003611 bool skip = false;
3612 if (drawCount == 0) return skip;
3613
3614 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3615 VkDeviceSize size = struct_size;
3616 if (drawCount == 1 || stride == size) {
3617 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003618 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003619 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3620 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003621 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003622 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003623 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003624 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003625 }
3626 } else {
3627 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003628 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003629 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3630 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003631 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003632 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3633 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3634 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003635 break;
3636 }
3637 }
3638 }
3639 return skip;
3640}
3641
locke-lunarg61870c22020-06-09 14:51:50 -06003642void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3643 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3644 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003645 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3646 VkDeviceSize size = struct_size;
3647 if (drawCount == 1 || stride == size) {
3648 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003649 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003650 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3651 } else {
3652 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003653 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003654 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3655 }
3656 }
3657}
3658
locke-lunarg61870c22020-06-09 14:51:50 -06003659bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3660 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003661 bool skip = false;
3662
3663 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003664 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003665 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3666 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003667 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003668 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003669 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003670 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003671 }
3672 return skip;
3673}
3674
locke-lunarg61870c22020-06-09 14:51:50 -06003675void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003676 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003677 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003678 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3679}
3680
locke-lunarg36ba2592020-04-03 09:42:04 -06003681bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003682 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003683 const auto *cb_access_context = GetAccessContext(commandBuffer);
3684 assert(cb_access_context);
3685 if (!cb_access_context) return skip;
3686
locke-lunarg61870c22020-06-09 14:51:50 -06003687 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003688 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003689}
3690
3691void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003692 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003693 auto *cb_access_context = GetAccessContext(commandBuffer);
3694 assert(cb_access_context);
3695 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003696
locke-lunarg61870c22020-06-09 14:51:50 -06003697 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003698}
locke-lunarge1a67022020-04-29 00:15:36 -06003699
3700bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003701 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003702 const auto *cb_access_context = GetAccessContext(commandBuffer);
3703 assert(cb_access_context);
3704 if (!cb_access_context) return skip;
3705
3706 const auto *context = cb_access_context->GetCurrentAccessContext();
3707 assert(context);
3708 if (!context) return skip;
3709
locke-lunarg61870c22020-06-09 14:51:50 -06003710 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3711 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3712 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003713 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003714}
3715
3716void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003717 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003718 auto *cb_access_context = GetAccessContext(commandBuffer);
3719 assert(cb_access_context);
3720 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3721 auto *context = cb_access_context->GetCurrentAccessContext();
3722 assert(context);
3723
locke-lunarg61870c22020-06-09 14:51:50 -06003724 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3725 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003726}
3727
3728bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3729 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003730 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003731 const auto *cb_access_context = GetAccessContext(commandBuffer);
3732 assert(cb_access_context);
3733 if (!cb_access_context) return skip;
3734
locke-lunarg61870c22020-06-09 14:51:50 -06003735 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3736 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3737 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003738 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003739}
3740
3741void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3742 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003743 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003744 auto *cb_access_context = GetAccessContext(commandBuffer);
3745 assert(cb_access_context);
3746 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003747
locke-lunarg61870c22020-06-09 14:51:50 -06003748 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3749 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3750 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003751}
3752
3753bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3754 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003755 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003756 const auto *cb_access_context = GetAccessContext(commandBuffer);
3757 assert(cb_access_context);
3758 if (!cb_access_context) return skip;
3759
locke-lunarg61870c22020-06-09 14:51:50 -06003760 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3761 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3762 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003763 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003764}
3765
3766void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3767 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003768 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003769 auto *cb_access_context = GetAccessContext(commandBuffer);
3770 assert(cb_access_context);
3771 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003772
locke-lunarg61870c22020-06-09 14:51:50 -06003773 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3774 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3775 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003776}
3777
3778bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3779 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003780 bool skip = false;
3781 if (drawCount == 0) return skip;
3782
locke-lunargff255f92020-05-13 18:53:52 -06003783 const auto *cb_access_context = GetAccessContext(commandBuffer);
3784 assert(cb_access_context);
3785 if (!cb_access_context) return skip;
3786
3787 const auto *context = cb_access_context->GetCurrentAccessContext();
3788 assert(context);
3789 if (!context) return skip;
3790
locke-lunarg61870c22020-06-09 14:51:50 -06003791 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3792 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3793 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3794 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003795
3796 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3797 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3798 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003799 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003800 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003801}
3802
3803void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3804 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003805 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003806 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003807 auto *cb_access_context = GetAccessContext(commandBuffer);
3808 assert(cb_access_context);
3809 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3810 auto *context = cb_access_context->GetCurrentAccessContext();
3811 assert(context);
3812
locke-lunarg61870c22020-06-09 14:51:50 -06003813 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3814 cb_access_context->RecordDrawSubpassAttachment(tag);
3815 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003816
3817 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3818 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3819 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003820 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003821}
3822
3823bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3824 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003825 bool skip = false;
3826 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003827 const auto *cb_access_context = GetAccessContext(commandBuffer);
3828 assert(cb_access_context);
3829 if (!cb_access_context) return skip;
3830
3831 const auto *context = cb_access_context->GetCurrentAccessContext();
3832 assert(context);
3833 if (!context) return skip;
3834
locke-lunarg61870c22020-06-09 14:51:50 -06003835 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3836 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3837 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3838 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003839
3840 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3841 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3842 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003843 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003844 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003845}
3846
3847void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3848 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003849 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003850 auto *cb_access_context = GetAccessContext(commandBuffer);
3851 assert(cb_access_context);
3852 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3853 auto *context = cb_access_context->GetCurrentAccessContext();
3854 assert(context);
3855
locke-lunarg61870c22020-06-09 14:51:50 -06003856 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3857 cb_access_context->RecordDrawSubpassAttachment(tag);
3858 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003859
3860 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3861 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3862 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003863 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003864}
3865
3866bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3867 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3868 uint32_t stride, const char *function) const {
3869 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003870 const auto *cb_access_context = GetAccessContext(commandBuffer);
3871 assert(cb_access_context);
3872 if (!cb_access_context) return skip;
3873
3874 const auto *context = cb_access_context->GetCurrentAccessContext();
3875 assert(context);
3876 if (!context) return skip;
3877
locke-lunarg61870c22020-06-09 14:51:50 -06003878 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3879 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3880 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3881 function);
3882 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003883
3884 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3885 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3886 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003887 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003888 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003889}
3890
3891bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3892 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3893 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003894 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3895 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003896}
3897
3898void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3899 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3900 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003901 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3902 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003903 auto *cb_access_context = GetAccessContext(commandBuffer);
3904 assert(cb_access_context);
3905 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3906 auto *context = cb_access_context->GetCurrentAccessContext();
3907 assert(context);
3908
locke-lunarg61870c22020-06-09 14:51:50 -06003909 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3910 cb_access_context->RecordDrawSubpassAttachment(tag);
3911 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3912 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003913
3914 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3915 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3916 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003917 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003918}
3919
3920bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3921 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3922 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003923 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3924 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003925}
3926
3927void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3928 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3929 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003930 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3931 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003932 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003933}
3934
3935bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3936 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3937 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003938 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3939 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003940}
3941
3942void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3943 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3944 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003945 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3946 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003947 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3948}
3949
3950bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3951 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3952 uint32_t stride, const char *function) const {
3953 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003954 const auto *cb_access_context = GetAccessContext(commandBuffer);
3955 assert(cb_access_context);
3956 if (!cb_access_context) return skip;
3957
3958 const auto *context = cb_access_context->GetCurrentAccessContext();
3959 assert(context);
3960 if (!context) return skip;
3961
locke-lunarg61870c22020-06-09 14:51:50 -06003962 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3963 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3964 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3965 stride, function);
3966 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003967
3968 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3969 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3970 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003971 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003972 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003973}
3974
3975bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3976 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3977 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003978 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3979 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003980}
3981
3982void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3983 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3984 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003985 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3986 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003987 auto *cb_access_context = GetAccessContext(commandBuffer);
3988 assert(cb_access_context);
3989 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3990 auto *context = cb_access_context->GetCurrentAccessContext();
3991 assert(context);
3992
locke-lunarg61870c22020-06-09 14:51:50 -06003993 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3994 cb_access_context->RecordDrawSubpassAttachment(tag);
3995 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3996 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003997
3998 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3999 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004000 // We will update the index and vertex buffer in SubmitQueue in the future.
4001 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004002}
4003
4004bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4005 VkDeviceSize offset, VkBuffer countBuffer,
4006 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4007 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004008 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4009 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004010}
4011
4012void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4013 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4014 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004015 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4016 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004017 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4018}
4019
4020bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4021 VkDeviceSize offset, VkBuffer countBuffer,
4022 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4023 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004024 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4025 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004026}
4027
4028void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4029 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4030 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004031 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4032 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004033 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4034}
4035
4036bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4037 const VkClearColorValue *pColor, uint32_t rangeCount,
4038 const VkImageSubresourceRange *pRanges) const {
4039 bool skip = false;
4040 const auto *cb_access_context = GetAccessContext(commandBuffer);
4041 assert(cb_access_context);
4042 if (!cb_access_context) return skip;
4043
4044 const auto *context = cb_access_context->GetCurrentAccessContext();
4045 assert(context);
4046 if (!context) return skip;
4047
4048 const auto *image_state = Get<IMAGE_STATE>(image);
4049
4050 for (uint32_t index = 0; index < rangeCount; index++) {
4051 const auto &range = pRanges[index];
4052 if (image_state) {
4053 auto hazard =
4054 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4055 if (hazard.hazard) {
4056 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004057 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004058 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004059 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004060 }
4061 }
4062 }
4063 return skip;
4064}
4065
4066void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4067 const VkClearColorValue *pColor, uint32_t rangeCount,
4068 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004069 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004070 auto *cb_access_context = GetAccessContext(commandBuffer);
4071 assert(cb_access_context);
4072 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4073 auto *context = cb_access_context->GetCurrentAccessContext();
4074 assert(context);
4075
4076 const auto *image_state = Get<IMAGE_STATE>(image);
4077
4078 for (uint32_t index = 0; index < rangeCount; index++) {
4079 const auto &range = pRanges[index];
4080 if (image_state) {
4081 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4082 tag);
4083 }
4084 }
4085}
4086
4087bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4088 VkImageLayout imageLayout,
4089 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4090 const VkImageSubresourceRange *pRanges) const {
4091 bool skip = false;
4092 const auto *cb_access_context = GetAccessContext(commandBuffer);
4093 assert(cb_access_context);
4094 if (!cb_access_context) return skip;
4095
4096 const auto *context = cb_access_context->GetCurrentAccessContext();
4097 assert(context);
4098 if (!context) return skip;
4099
4100 const auto *image_state = Get<IMAGE_STATE>(image);
4101
4102 for (uint32_t index = 0; index < rangeCount; index++) {
4103 const auto &range = pRanges[index];
4104 if (image_state) {
4105 auto hazard =
4106 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4107 if (hazard.hazard) {
4108 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004109 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004110 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004111 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004112 }
4113 }
4114 }
4115 return skip;
4116}
4117
4118void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4119 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4120 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004121 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004122 auto *cb_access_context = GetAccessContext(commandBuffer);
4123 assert(cb_access_context);
4124 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4125 auto *context = cb_access_context->GetCurrentAccessContext();
4126 assert(context);
4127
4128 const auto *image_state = Get<IMAGE_STATE>(image);
4129
4130 for (uint32_t index = 0; index < rangeCount; index++) {
4131 const auto &range = pRanges[index];
4132 if (image_state) {
4133 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4134 tag);
4135 }
4136 }
4137}
4138
4139bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4140 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4141 VkDeviceSize dstOffset, VkDeviceSize stride,
4142 VkQueryResultFlags flags) const {
4143 bool skip = false;
4144 const auto *cb_access_context = GetAccessContext(commandBuffer);
4145 assert(cb_access_context);
4146 if (!cb_access_context) return skip;
4147
4148 const auto *context = cb_access_context->GetCurrentAccessContext();
4149 assert(context);
4150 if (!context) return skip;
4151
4152 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4153
4154 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004155 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004156 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4157 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004158 skip |=
4159 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4160 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4161 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004162 }
4163 }
locke-lunargff255f92020-05-13 18:53:52 -06004164
4165 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004166 return skip;
4167}
4168
4169void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4170 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4171 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004172 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4173 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004174 auto *cb_access_context = GetAccessContext(commandBuffer);
4175 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004176 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004177 auto *context = cb_access_context->GetCurrentAccessContext();
4178 assert(context);
4179
4180 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4181
4182 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004183 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004184 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4185 }
locke-lunargff255f92020-05-13 18:53:52 -06004186
4187 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004188}
4189
4190bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4191 VkDeviceSize size, uint32_t data) const {
4192 bool skip = false;
4193 const auto *cb_access_context = GetAccessContext(commandBuffer);
4194 assert(cb_access_context);
4195 if (!cb_access_context) return skip;
4196
4197 const auto *context = cb_access_context->GetCurrentAccessContext();
4198 assert(context);
4199 if (!context) return skip;
4200
4201 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4202
4203 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004204 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004205 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4206 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004207 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004208 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004209 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004210 }
4211 }
4212 return skip;
4213}
4214
4215void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4216 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004217 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004218 auto *cb_access_context = GetAccessContext(commandBuffer);
4219 assert(cb_access_context);
4220 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4221 auto *context = cb_access_context->GetCurrentAccessContext();
4222 assert(context);
4223
4224 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4225
4226 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004227 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004228 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4229 }
4230}
4231
4232bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4233 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4234 const VkImageResolve *pRegions) const {
4235 bool skip = false;
4236 const auto *cb_access_context = GetAccessContext(commandBuffer);
4237 assert(cb_access_context);
4238 if (!cb_access_context) return skip;
4239
4240 const auto *context = cb_access_context->GetCurrentAccessContext();
4241 assert(context);
4242 if (!context) return skip;
4243
4244 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4245 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4246
4247 for (uint32_t region = 0; region < regionCount; region++) {
4248 const auto &resolve_region = pRegions[region];
4249 if (src_image) {
4250 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4251 resolve_region.srcOffset, resolve_region.extent);
4252 if (hazard.hazard) {
4253 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004254 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004255 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004256 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004257 }
4258 }
4259
4260 if (dst_image) {
4261 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4262 resolve_region.dstOffset, resolve_region.extent);
4263 if (hazard.hazard) {
4264 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004265 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004266 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004267 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004268 }
4269 if (skip) break;
4270 }
4271 }
4272
4273 return skip;
4274}
4275
4276void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4277 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4278 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004279 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4280 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004281 auto *cb_access_context = GetAccessContext(commandBuffer);
4282 assert(cb_access_context);
4283 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4284 auto *context = cb_access_context->GetCurrentAccessContext();
4285 assert(context);
4286
4287 auto *src_image = Get<IMAGE_STATE>(srcImage);
4288 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4289
4290 for (uint32_t region = 0; region < regionCount; region++) {
4291 const auto &resolve_region = pRegions[region];
4292 if (src_image) {
4293 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4294 resolve_region.srcOffset, resolve_region.extent, tag);
4295 }
4296 if (dst_image) {
4297 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4298 resolve_region.dstOffset, resolve_region.extent, tag);
4299 }
4300 }
4301}
4302
Jeff Leger178b1e52020-10-05 12:22:23 -04004303bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4304 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4305 bool skip = false;
4306 const auto *cb_access_context = GetAccessContext(commandBuffer);
4307 assert(cb_access_context);
4308 if (!cb_access_context) return skip;
4309
4310 const auto *context = cb_access_context->GetCurrentAccessContext();
4311 assert(context);
4312 if (!context) return skip;
4313
4314 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4315 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4316
4317 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4318 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4319 if (src_image) {
4320 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4321 resolve_region.srcOffset, resolve_region.extent);
4322 if (hazard.hazard) {
4323 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4324 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4325 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
4326 region, string_UsageTag(hazard).c_str());
4327 }
4328 }
4329
4330 if (dst_image) {
4331 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4332 resolve_region.dstOffset, resolve_region.extent);
4333 if (hazard.hazard) {
4334 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4335 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4336 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
4337 region, string_UsageTag(hazard).c_str());
4338 }
4339 if (skip) break;
4340 }
4341 }
4342
4343 return skip;
4344}
4345
4346void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4347 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4348 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4349 auto *cb_access_context = GetAccessContext(commandBuffer);
4350 assert(cb_access_context);
4351 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4352 auto *context = cb_access_context->GetCurrentAccessContext();
4353 assert(context);
4354
4355 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4356 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4357
4358 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4359 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4360 if (src_image) {
4361 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4362 resolve_region.srcOffset, resolve_region.extent, tag);
4363 }
4364 if (dst_image) {
4365 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4366 resolve_region.dstOffset, resolve_region.extent, tag);
4367 }
4368 }
4369}
4370
locke-lunarge1a67022020-04-29 00:15:36 -06004371bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4372 VkDeviceSize dataSize, const void *pData) const {
4373 bool skip = false;
4374 const auto *cb_access_context = GetAccessContext(commandBuffer);
4375 assert(cb_access_context);
4376 if (!cb_access_context) return skip;
4377
4378 const auto *context = cb_access_context->GetCurrentAccessContext();
4379 assert(context);
4380 if (!context) return skip;
4381
4382 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4383
4384 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004385 // VK_WHOLE_SIZE not allowed
4386 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004387 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4388 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004389 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004390 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004391 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004392 }
4393 }
4394 return skip;
4395}
4396
4397void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4398 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004399 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004400 auto *cb_access_context = GetAccessContext(commandBuffer);
4401 assert(cb_access_context);
4402 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4403 auto *context = cb_access_context->GetCurrentAccessContext();
4404 assert(context);
4405
4406 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4407
4408 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004409 // VK_WHOLE_SIZE not allowed
4410 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004411 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4412 }
4413}
locke-lunargff255f92020-05-13 18:53:52 -06004414
4415bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4416 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4417 bool skip = false;
4418 const auto *cb_access_context = GetAccessContext(commandBuffer);
4419 assert(cb_access_context);
4420 if (!cb_access_context) return skip;
4421
4422 const auto *context = cb_access_context->GetCurrentAccessContext();
4423 assert(context);
4424 if (!context) return skip;
4425
4426 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4427
4428 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004429 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004430 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4431 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004432 skip |=
4433 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4434 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4435 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004436 }
4437 }
4438 return skip;
4439}
4440
4441void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4442 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004443 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004444 auto *cb_access_context = GetAccessContext(commandBuffer);
4445 assert(cb_access_context);
4446 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4447 auto *context = cb_access_context->GetCurrentAccessContext();
4448 assert(context);
4449
4450 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4451
4452 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004453 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004454 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4455 }
4456}