blob: 769a1122482485a83deef1208bee7b9c782ca4b6 [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
locke-lunarg3c038002020-04-30 23:08:08 -0600200inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
201 if (size == VK_WHOLE_SIZE) {
202 return (whole_size - offset);
203 }
204 return size;
205}
206
John Zulauf3e86bf02020-09-12 10:47:57 -0600207static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
208 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
209}
210
John Zulauf16adfc92020-04-08 10:28:33 -0600211template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600212static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600213 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
214}
215
John Zulauf355e49b2020-04-24 15:11:15 -0600216static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600217
John Zulauf3e86bf02020-09-12 10:47:57 -0600218static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
219 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
220}
221
222static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
223 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
224}
225
John Zulauf0cb5be22020-01-23 12:18:22 -0700226// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
227VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
228 VkPipelineStageFlags expanded = stage_mask;
229 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
230 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
231 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
232 if (all_commands.first & queue_flags) {
233 expanded |= all_commands.second;
234 }
235 }
236 }
237 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
238 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
239 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
240 }
241 return expanded;
242}
243
John Zulauf36bcf6a2020-02-03 15:12:52 -0700244VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
245 std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
246 VkPipelineStageFlags unscanned = stage_mask;
247 VkPipelineStageFlags related = 0;
Jonah Ryan-Davis185189c2020-07-14 10:28:52 -0400248 for (const auto &entry : map) {
249 const auto &stage = entry.first;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700250 if (stage & unscanned) {
251 related = related | entry.second;
252 unscanned = unscanned & ~stage;
253 if (!unscanned) break;
254 }
255 }
256 return related;
257}
258
259VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
260 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
261}
262
263VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
264 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
265}
266
John Zulauf5c5e88d2019-12-26 11:22:02 -0700267static const ResourceAccessRange full_range(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700268
John Zulauf3e86bf02020-09-12 10:47:57 -0600269ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
270 VkDeviceSize stride) {
271 VkDeviceSize range_start = offset + first_index * stride;
272 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600273 if (count == UINT32_MAX) {
274 range_size = buf_whole_size - range_start;
275 } else {
276 range_size = count * stride;
277 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600278 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600279}
280
locke-lunarg654e3692020-06-04 17:19:15 -0600281SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
282 VkShaderStageFlagBits stage_flag) {
283 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
284 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
285 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
286 }
287 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
288 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
289 assert(0);
290 }
291 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
292 return stage_access->second.uniform_read;
293 }
294
295 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
296 // Because if write hazard happens, read hazard might or might not happen.
297 // But if write hazard doesn't happen, read hazard is impossible to happen.
298 if (descriptor_data.is_writable) {
299 return stage_access->second.shader_write;
300 }
301 return stage_access->second.shader_read;
302}
303
locke-lunarg37047832020-06-12 13:44:45 -0600304bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
305 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
306 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
307 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
308 ? true
309 : false;
310}
311
312bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
313 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
314 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
315 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
316 ? true
317 : false;
318}
319
John Zulauf355e49b2020-04-24 15:11:15 -0600320// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
321const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = {
322 AccessContext::AddressType::kLinearAddress, AccessContext::AddressType::kIdealizedAddress};
323
John Zulauf7635de32020-05-29 17:14:15 -0600324// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
325// Used by both validation and record operations
326//
327// The signature for Action() reflect the needs of both uses.
328template <typename Action>
329void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
330 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
331 VkExtent3D extent = CastTo3D(render_area.extent);
332 VkOffset3D offset = CastTo3D(render_area.offset);
333 const auto &rp_ci = rp_state.createInfo;
334 const auto *attachment_ci = rp_ci.pAttachments;
335 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
336
337 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
338 const auto *color_attachments = subpass_ci.pColorAttachments;
339 const auto *color_resolve = subpass_ci.pResolveAttachments;
340 if (color_resolve && color_attachments) {
341 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
342 const auto &color_attach = color_attachments[i].attachment;
343 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
344 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
345 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
346 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
347 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
348 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
349 }
350 }
351 }
352
353 // Depth stencil resolve only if the extension is present
354 const auto ds_resolve = lvl_find_in_chain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
355 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
356 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
357 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
358 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
359 const auto src_ci = attachment_ci[src_at];
360 // The formats are required to match so we can pick either
361 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
362 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
363 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
364 VkImageAspectFlags aspect_mask = 0u;
365
366 // Figure out which aspects are actually touched during resolve operations
367 const char *aspect_string = nullptr;
368 if (resolve_depth && resolve_stencil) {
369 // Validate all aspects together
370 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
371 aspect_string = "depth/stencil";
372 } else if (resolve_depth) {
373 // Validate depth only
374 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
375 aspect_string = "depth";
376 } else if (resolve_stencil) {
377 // Validate all stencil only
378 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
379 aspect_string = "stencil";
380 }
381
382 if (aspect_mask) {
383 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
384 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kDepthStencilAttachmentRasterOrder, offset, extent,
385 aspect_mask);
386 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
387 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
388 }
389 }
390}
391
392// Action for validating resolve operations
393class ValidateResolveAction {
394 public:
395 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
396 const char *func_name)
397 : render_pass_(render_pass),
398 subpass_(subpass),
399 context_(context),
400 sync_state_(sync_state),
401 func_name_(func_name),
402 skip_(false) {}
403 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
404 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
405 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
406 HazardResult hazard;
407 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
408 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600409 skip_ |= sync_state_.LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
410 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600411 " to resolve attachment %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -0600412 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name,
John Zulauf37ceaed2020-07-03 16:18:15 -0600413 src_at, dst_at, string_UsageTag(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600414 }
415 }
416 // Providing a mechanism for the constructing caller to get the result of the validation
417 bool GetSkip() const { return skip_; }
418
419 private:
420 VkRenderPass render_pass_;
421 const uint32_t subpass_;
422 const AccessContext &context_;
423 const SyncValidator &sync_state_;
424 const char *func_name_;
425 bool skip_;
426};
427
428// Update action for resolve operations
429class UpdateStateResolveAction {
430 public:
431 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
432 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
433 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
434 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
435 // Ignores validation only arguments...
436 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
437 }
438
439 private:
440 AccessContext &context_;
441 const ResourceUsageTag &tag_;
442};
443
John Zulauf59e25072020-07-17 10:55:21 -0600444void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
445 SyncStageAccessFlags prior_, const ResourceUsageTag &tag_) {
446 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
447 usage_index = usage_index_;
448 hazard = hazard_;
449 prior_access = prior_;
450 tag = tag_;
451}
452
John Zulauf540266b2020-04-06 18:54:53 -0600453AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
454 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600455 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600456 Reset();
457 const auto &subpass_dep = dependencies[subpass];
458 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600459 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600460 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600461 const auto prev_pass = prev_dep.first->pass;
462 const auto &prev_barriers = prev_dep.second;
463 assert(prev_dep.second.size());
464 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
465 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700466 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600467
468 async_.reserve(subpass_dep.async.size());
469 for (const auto async_subpass : subpass_dep.async) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600470 // TODO -- review why async is storing non-const
John Zulauf540266b2020-04-06 18:54:53 -0600471 async_.emplace_back(const_cast<AccessContext *>(&contexts[async_subpass]));
John Zulauf3d84f1b2020-03-09 13:33:25 -0600472 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600473 if (subpass_dep.barrier_from_external.size()) {
474 src_external_ = TrackBack(external_context, queue_flags, subpass_dep.barrier_from_external);
John Zulaufe5da6e52020-03-18 15:32:18 -0600475 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600476 if (subpass_dep.barrier_to_external.size()) {
477 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600478 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700479}
480
John Zulauf5f13a792020-03-10 07:31:21 -0600481template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600482HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600483 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600484 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600485 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600486
487 HazardResult hazard;
488 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
489 hazard = detector.Detect(prev);
490 }
491 return hazard;
492}
493
John Zulauf3d84f1b2020-03-09 13:33:25 -0600494// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
495// the DAG of the contexts (for example subpasses)
496template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600497HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
498 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600499 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600500
John Zulauf1a224292020-06-30 14:52:13 -0600501 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600502 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
503 // so we'll check these first
504 for (const auto &async_context : async_) {
505 hazard = async_context->DetectAsyncHazard(type, detector, range);
506 if (hazard.hazard) return hazard;
507 }
John Zulauf5f13a792020-03-10 07:31:21 -0600508 }
509
John Zulauf1a224292020-06-30 14:52:13 -0600510 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600511
John Zulauf69133422020-05-20 14:55:53 -0600512 const auto &accesses = GetAccessStateMap(type);
513 const auto from = accesses.lower_bound(range);
514 const auto to = accesses.upper_bound(range);
515 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600516
John Zulauf69133422020-05-20 14:55:53 -0600517 for (auto pos = from; pos != to; ++pos) {
518 // Cover any leading gap, or gap between entries
519 if (detect_prev) {
520 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
521 // Cover any leading gap, or gap between entries
522 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600523 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600524 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600525 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600526 if (hazard.hazard) return hazard;
527 }
John Zulauf69133422020-05-20 14:55:53 -0600528 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
529 gap.begin = pos->first.end;
530 }
531
532 hazard = detector.Detect(pos);
533 if (hazard.hazard) return hazard;
534 }
535
536 if (detect_prev) {
537 // Detect in the trailing empty as needed
538 gap.end = range.end;
539 if (gap.non_empty()) {
540 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600541 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600542 }
543
544 return hazard;
545}
546
547// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
548template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600549HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600550 auto &accesses = GetAccessStateMap(type);
551 const auto from = accesses.lower_bound(range);
552 const auto to = accesses.upper_bound(range);
553
John Zulauf3d84f1b2020-03-09 13:33:25 -0600554 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600555 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
556 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600557 }
John Zulauf16adfc92020-04-08 10:28:33 -0600558
John Zulauf3d84f1b2020-03-09 13:33:25 -0600559 return hazard;
560}
561
John Zulauf355e49b2020-04-24 15:11:15 -0600562// Returns the last resolved entry
563static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
564 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufa0a98292020-09-18 09:30:10 -0600565 const std::vector<SyncBarrier> &barriers) {
John Zulauf355e49b2020-04-24 15:11:15 -0600566 auto at = entry;
567 for (auto pos = first; pos != last; ++pos) {
568 // Every member of the input iterator range must fit within the remaining portion of entry
569 assert(at->first.includes(pos->first));
570 assert(at != dest->end());
571 // Trim up at to the same size as the entry to resolve
572 at = sparse_container::split(at, *dest, pos->first);
573 auto access = pos->second;
John Zulaufa0a98292020-09-18 09:30:10 -0600574 if (barriers.size()) {
575 access.ApplyBarriers(barriers);
John Zulauf355e49b2020-04-24 15:11:15 -0600576 }
577 at->second.Resolve(access);
578 ++at; // Go to the remaining unused section of entry
579 }
580}
581
John Zulaufa0a98292020-09-18 09:30:10 -0600582static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
583 SyncBarrier merged = {};
584 for (const auto &barrier : barriers) {
585 merged.Merge(barrier);
586 }
587 return merged;
588}
589
590void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, const std::vector<SyncBarrier> &barriers,
John Zulauf355e49b2020-04-24 15:11:15 -0600591 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
592 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600593 if (!range.non_empty()) return;
594
John Zulauf355e49b2020-04-24 15:11:15 -0600595 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
596 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600597 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600598 if (current->pos_B->valid) {
599 const auto &src_pos = current->pos_B->lower_bound;
John Zulauf355e49b2020-04-24 15:11:15 -0600600 auto access = src_pos->second;
John Zulaufa0a98292020-09-18 09:30:10 -0600601 if (barriers.size()) {
602 access.ApplyBarriers(barriers);
John Zulauf355e49b2020-04-24 15:11:15 -0600603 }
John Zulauf16adfc92020-04-08 10:28:33 -0600604 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600605 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
606 trimmed->second.Resolve(access);
607 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600608 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600609 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600610 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600611 }
John Zulauf16adfc92020-04-08 10:28:33 -0600612 } else {
613 // we have to descend to fill this gap
614 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600615 if (current->pos_A->valid) {
616 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
617 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600618 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulaufa0a98292020-09-18 09:30:10 -0600619 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barriers);
John Zulauf355e49b2020-04-24 15:11:15 -0600620 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600621 // There isn't anything in dest in current)range, so we can accumulate directly into it.
622 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulaufa0a98292020-09-18 09:30:10 -0600623 if (barriers.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -0600624 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
John Zulauf3bcab5e2020-06-19 14:42:32 -0600625 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
John Zulaufa0a98292020-09-18 09:30:10 -0600626 pos->second.ApplyBarriers(barriers);
John Zulauf355e49b2020-04-24 15:11:15 -0600627 }
628 }
629 }
630 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
631 // iterator of the outer while.
632
633 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
634 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
635 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600636 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
637 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600638 current.seek(seek_to);
639 } else if (!current->pos_A->valid && infill_state) {
640 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
641 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
642 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600643 }
John Zulauf5f13a792020-03-10 07:31:21 -0600644 }
John Zulauf16adfc92020-04-08 10:28:33 -0600645 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600646 }
John Zulauf1a224292020-06-30 14:52:13 -0600647
648 // Infill if range goes passed both the current and resolve map prior contents
649 if (recur_to_infill && (current->range.end < range.end)) {
650 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
651 ResourceAccessRangeMap gap_map;
652 const auto the_end = resolve_map->end();
653 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
654 for (auto &access : gap_map) {
John Zulaufa0a98292020-09-18 09:30:10 -0600655 access.second.ApplyBarriers(barriers);
John Zulauf1a224292020-06-30 14:52:13 -0600656 resolve_map->insert(the_end, access);
657 }
658 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600659}
660
John Zulauf355e49b2020-04-24 15:11:15 -0600661void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
662 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600663 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600664 if (range.non_empty() && infill_state) {
665 descent_map->insert(std::make_pair(range, *infill_state));
666 }
667 } else {
668 // Look for something to fill the gap further along.
669 for (const auto &prev_dep : prev_) {
John Zulaufa0a98292020-09-18 09:30:10 -0600670 prev_dep.context->ResolveAccessRange(type, range, prev_dep.barriers, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600671 }
672
John Zulaufe5da6e52020-03-18 15:32:18 -0600673 if (src_external_.context) {
John Zulaufa0a98292020-09-18 09:30:10 -0600674 src_external_.context->ResolveAccessRange(type, range, src_external_.barriers, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600675 }
676 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600677}
678
John Zulauf16adfc92020-04-08 10:28:33 -0600679AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600680 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600681}
682
683VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) {
684 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
685}
686
John Zulauf355e49b2020-04-24 15:11:15 -0600687static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
John Zulauf16adfc92020-04-08 10:28:33 -0600688
John Zulauf1507ee42020-05-18 11:33:09 -0600689static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
690 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
691 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
692 return stage_access;
693}
694static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
695 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
696 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
697 return stage_access;
698}
699
John Zulauf7635de32020-05-29 17:14:15 -0600700// Caller must manage returned pointer
701static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
702 uint32_t subpass, const VkRect2D &render_area,
703 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
704 auto *proxy = new AccessContext(context);
705 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600706 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600707 return proxy;
708}
709
John Zulauf540266b2020-04-06 18:54:53 -0600710void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
John Zulauf355e49b2020-04-24 15:11:15 -0600711 AddressType address_type, ResourceAccessRangeMap *descent_map,
712 const ResourceAccessState *infill_state) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600713 if (!SimpleBinding(image_state)) return;
714
John Zulauf62f10592020-04-03 12:20:02 -0600715 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
locke-lunargae26eac2020-04-16 15:29:05 -0600716 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600717 image_state.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600718 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf62f10592020-04-03 12:20:02 -0600719 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600720 ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state);
John Zulauf62f10592020-04-03 12:20:02 -0600721 }
722}
723
John Zulauf7635de32020-05-29 17:14:15 -0600724// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600725bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600726 const VkRect2D &render_area, uint32_t subpass,
727 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
728 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600729 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600730 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
731 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
732 // those affects have not been recorded yet.
733 //
734 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
735 // to apply and only copy then, if this proves a hot spot.
736 std::unique_ptr<AccessContext> proxy_for_prev;
737 TrackBack proxy_track_back;
738
John Zulauf355e49b2020-04-24 15:11:15 -0600739 const auto &transitions = rp_state.subpass_transitions[subpass];
740 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600741 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
742
743 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
744 if (prev_needs_proxy) {
745 if (!proxy_for_prev) {
746 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
747 render_area, attachment_views));
748 proxy_track_back = *track_back;
749 proxy_track_back.context = proxy_for_prev.get();
750 }
751 track_back = &proxy_track_back;
752 }
753 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600754 if (hazard.hazard) {
John Zulauf389c34b2020-07-28 11:19:35 -0600755 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
756 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
757 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
758 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
759 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
760 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600761 }
762 }
763 return skip;
764}
765
John Zulauf1507ee42020-05-18 11:33:09 -0600766bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600767 const VkRect2D &render_area, uint32_t subpass,
768 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
769 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600770 bool skip = false;
771 const auto *attachment_ci = rp_state.createInfo.pAttachments;
772 VkExtent3D extent = CastTo3D(render_area.extent);
773 VkOffset3D offset = CastTo3D(render_area.offset);
John Zulaufa0a98292020-09-18 09:30:10 -0600774
775 SyncStageAccessFlags accumulate_access_scope = 0;
776 for (const auto &barrier : src_external_.barriers) {
777 accumulate_access_scope |= barrier.dst_access_scope;
778 }
779 const auto external_access_scope = accumulate_access_scope;
John Zulauf1507ee42020-05-18 11:33:09 -0600780
781 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
782 if (subpass == rp_state.attachment_first_subpass[i]) {
783 if (attachment_views[i] == nullptr) continue;
784 const IMAGE_VIEW_STATE &view = *attachment_views[i];
785 const IMAGE_STATE *image = view.image_state.get();
786 if (image == nullptr) continue;
787 const auto &ci = attachment_ci[i];
788 const bool is_transition = rp_state.attachment_first_is_transition[i];
789
790 // Need check in the following way
791 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
792 // vs. transition
793 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
794 // for each aspect loaded.
795
796 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600797 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600798 const bool is_color = !(has_depth || has_stencil);
799
800 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
801 const SyncStageAccessFlags load_mask = (has_depth || is_color) ? SyncStageAccess::Flags(load_index) : 0U;
802 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
803 const SyncStageAccessFlags stencil_mask = has_stencil ? SyncStageAccess::Flags(stencil_load_index) : 0U;
804
John Zulaufaff20662020-06-01 14:07:58 -0600805 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600806 const char *aspect = nullptr;
807 if (is_transition) {
808 // For transition w
809 SyncHazard transition_hazard = SyncHazard::NONE;
810 bool checked_stencil = false;
811 if (load_mask) {
812 if ((load_mask & external_access_scope) != load_mask) {
813 transition_hazard =
814 SyncStageAccess::HasWrite(load_mask) ? SyncHazard::WRITE_AFTER_WRITE : SyncHazard::READ_AFTER_WRITE;
815 aspect = is_color ? "color" : "depth";
816 }
817 if (!transition_hazard && stencil_mask) {
818 if ((stencil_mask & external_access_scope) != stencil_mask) {
819 transition_hazard = SyncStageAccess::HasWrite(stencil_mask) ? SyncHazard::WRITE_AFTER_WRITE
820 : SyncHazard::READ_AFTER_WRITE;
821 aspect = "stencil";
822 checked_stencil = true;
823 }
824 }
825 }
826 if (transition_hazard) {
827 // Hazard vs. ILT
828 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
829 skip |=
locke-lunarg54379cf2020-08-05 12:25:29 -0600830 sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(transition_hazard),
John Zulauf1507ee42020-05-18 11:33:09 -0600831 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
832 " aspect %s during load with loadOp %s.",
833 func_name, string_SyncHazard(transition_hazard), subpass, i, aspect, load_op_string);
834 }
835 } else {
836 auto hazard_range = view.normalized_subresource_range;
837 bool checked_stencil = false;
838 if (is_color) {
839 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, offset, extent);
840 aspect = "color";
841 } else {
842 if (has_depth) {
843 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
844 hazard = DetectHazard(*image, load_index, hazard_range, offset, extent);
845 aspect = "depth";
846 }
847 if (!hazard.hazard && has_stencil) {
848 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
849 hazard = DetectHazard(*image, stencil_load_index, hazard_range, offset, extent);
850 aspect = "stencil";
851 checked_stencil = true;
852 }
853 }
854
855 if (hazard.hazard) {
856 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
857 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
858 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600859 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -0600860 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600861 string_UsageTag(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -0600862 }
863 }
864 }
865 }
866 return skip;
867}
868
John Zulaufaff20662020-06-01 14:07:58 -0600869// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
870// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
871// store is part of the same Next/End operation.
872// The latter is handled in layout transistion validation directly
873bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
874 const VkRect2D &render_area, uint32_t subpass,
875 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
876 const char *func_name) const {
877 bool skip = false;
878 const auto *attachment_ci = rp_state.createInfo.pAttachments;
879 VkExtent3D extent = CastTo3D(render_area.extent);
880 VkOffset3D offset = CastTo3D(render_area.offset);
881
882 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
883 if (subpass == rp_state.attachment_last_subpass[i]) {
884 if (attachment_views[i] == nullptr) continue;
885 const IMAGE_VIEW_STATE &view = *attachment_views[i];
886 const IMAGE_STATE *image = view.image_state.get();
887 if (image == nullptr) continue;
888 const auto &ci = attachment_ci[i];
889
890 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
891 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
892 // sake, we treat DONT_CARE as writing.
893 const bool has_depth = FormatHasDepth(ci.format);
894 const bool has_stencil = FormatHasStencil(ci.format);
895 const bool is_color = !(has_depth || has_stencil);
896 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
897 if (!has_stencil && !store_op_stores) continue;
898
899 HazardResult hazard;
900 const char *aspect = nullptr;
901 bool checked_stencil = false;
902 if (is_color) {
903 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
904 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
905 aspect = "color";
906 } else {
907 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
908 auto hazard_range = view.normalized_subresource_range;
909 if (has_depth && store_op_stores) {
910 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
911 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
912 kAttachmentRasterOrder, offset, extent);
913 aspect = "depth";
914 }
915 if (!hazard.hazard && has_stencil && stencil_op_stores) {
916 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
917 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
918 kAttachmentRasterOrder, offset, extent);
919 aspect = "stencil";
920 checked_stencil = true;
921 }
922 }
923
924 if (hazard.hazard) {
925 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
926 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -0600927 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
928 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600929 " %s aspect during store with %s %s. Access info %s",
John Zulauf1dae9192020-06-16 15:46:44 -0600930 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600931 store_op_string, string_UsageTag(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -0600932 }
933 }
934 }
935 return skip;
936}
937
John Zulaufb027cdb2020-05-21 14:25:22 -0600938bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
939 const VkRect2D &render_area,
940 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
941 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600942 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
943 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
944 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600945}
946
John Zulauf3d84f1b2020-03-09 13:33:25 -0600947class HazardDetector {
948 SyncStageAccessIndex usage_index_;
949
950 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600951 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600952 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
953 return pos->second.DetectAsyncHazard(usage_index_);
954 }
955 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
956};
957
John Zulauf69133422020-05-20 14:55:53 -0600958class HazardDetectorWithOrdering {
959 const SyncStageAccessIndex usage_index_;
960 const SyncOrderingBarrier &ordering_;
961
962 public:
963 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
964 return pos->second.DetectHazard(usage_index_, ordering_);
965 }
966 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
967 return pos->second.DetectAsyncHazard(usage_index_);
968 }
969 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
970 : usage_index_(usage), ordering_(ordering) {}
971};
972
John Zulauf16adfc92020-04-08 10:28:33 -0600973HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600974 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600975 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600976 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600977}
978
John Zulauf16adfc92020-04-08 10:28:33 -0600979HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600980 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600981 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -0600982 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -0600983}
984
John Zulauf69133422020-05-20 14:55:53 -0600985template <typename Detector>
986HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
987 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
988 const VkExtent3D &extent, DetectOptions options) const {
989 if (!SimpleBinding(image)) return HazardResult();
990 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
991 const auto address_type = ImageAddressType(image);
992 const auto base_address = ResourceBaseAddress(image);
993 for (; range_gen->non_empty(); ++range_gen) {
994 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
995 if (hazard.hazard) return hazard;
996 }
997 return HazardResult();
998}
999
John Zulauf540266b2020-04-06 18:54:53 -06001000HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1001 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1002 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001003 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1004 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -06001005 return DetectHazard(image, current_usage, subresource_range, offset, extent);
1006}
1007
1008HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1009 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1010 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -06001011 HazardDetector detector(current_usage);
1012 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
1013}
1014
1015HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1016 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
1017 const VkOffset3D &offset, const VkExtent3D &extent) const {
1018 HazardDetectorWithOrdering detector(current_usage, ordering);
1019 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001020}
1021
John Zulaufb027cdb2020-05-21 14:25:22 -06001022// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
1023// should have reported the issue regarding an invalid attachment entry
1024HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
1025 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
1026 VkImageAspectFlags aspect_mask) const {
1027 if (view != nullptr) {
1028 const IMAGE_STATE *image = view->image_state.get();
1029 if (image != nullptr) {
1030 auto *detect_range = &view->normalized_subresource_range;
1031 VkImageSubresourceRange masked_range;
1032 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1033 masked_range = view->normalized_subresource_range;
1034 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1035 detect_range = &masked_range;
1036 }
1037
1038 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1039 if (detect_range->aspectMask) {
1040 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
1041 }
1042 }
1043 }
1044 return HazardResult();
1045}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001046class BarrierHazardDetector {
1047 public:
1048 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1049 SyncStageAccessFlags src_access_scope)
1050 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1051
John Zulauf5f13a792020-03-10 07:31:21 -06001052 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1053 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001054 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001055 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
1056 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1057 return pos->second.DetectAsyncHazard(usage_index_);
1058 }
1059
1060 private:
1061 SyncStageAccessIndex usage_index_;
1062 VkPipelineStageFlags src_exec_scope_;
1063 SyncStageAccessFlags src_access_scope_;
1064};
1065
John Zulauf16adfc92020-04-08 10:28:33 -06001066HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -06001067 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001068 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001069 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -06001070 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001071}
1072
John Zulauf16adfc92020-04-08 10:28:33 -06001073HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001074 SyncStageAccessFlags src_access_scope,
1075 const VkImageSubresourceRange &subresource_range,
1076 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001077 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1078 VkOffset3D zero_offset = {0, 0, 0};
1079 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001080}
1081
John Zulauf355e49b2020-04-24 15:11:15 -06001082HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1083 SyncStageAccessFlags src_stage_accesses,
1084 const VkImageMemoryBarrier &barrier) const {
1085 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1086 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1087 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1088}
1089
John Zulauf9cb530d2019-09-30 14:14:10 -06001090template <typename Flags, typename Map>
1091SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1092 SyncStageAccessFlags scope = 0;
1093 for (const auto &bit_scope : map) {
1094 if (flag_mask < bit_scope.first) break;
1095
1096 if (flag_mask & bit_scope.first) {
1097 scope |= bit_scope.second;
1098 }
1099 }
1100 return scope;
1101}
1102
1103SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
1104 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1105}
1106
1107SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
1108 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
1109}
1110
1111// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
1112SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001113 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1114 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1115 // 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 -06001116 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1117}
1118
1119template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001120void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001121 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1122 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -06001123 auto pos = accesses->lower_bound(range);
1124 if (pos == accesses->end() || !pos->first.intersects(range)) {
1125 // The range is empty, fill it with a default value.
1126 pos = action.Infill(accesses, pos, range);
1127 } else if (range.begin < pos->first.begin) {
1128 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001129 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001130 } else if (pos->first.begin < range.begin) {
1131 // Trim the beginning if needed
1132 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1133 ++pos;
1134 }
1135
1136 const auto the_end = accesses->end();
1137 while ((pos != the_end) && pos->first.intersects(range)) {
1138 if (pos->first.end > range.end) {
1139 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1140 }
1141
1142 pos = action(accesses, pos);
1143 if (pos == the_end) break;
1144
1145 auto next = pos;
1146 ++next;
1147 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1148 // Need to infill if next is disjoint
1149 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001150 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001151 next = action.Infill(accesses, next, new_range);
1152 }
1153 pos = next;
1154 }
1155}
1156
1157struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001158 using Iterator = ResourceAccessRangeMap::iterator;
1159 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001160 // this is only called on gaps, and never returns a gap.
1161 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001162 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001163 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001164 }
John Zulauf5f13a792020-03-10 07:31:21 -06001165
John Zulauf5c5e88d2019-12-26 11:22:02 -07001166 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001167 auto &access_state = pos->second;
1168 access_state.Update(usage, tag);
1169 return pos;
1170 }
1171
John Zulauf16adfc92020-04-08 10:28:33 -06001172 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001173 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001174 : type(type_), context(context_), usage(usage_), tag(tag_) {}
1175 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001176 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001177 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001178 const ResourceUsageTag &tag;
1179};
1180
John Zulauf89311b42020-09-29 16:28:47 -06001181// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1182// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1183class ApplyBarrierFunctor {
1184 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001185 using Iterator = ResourceAccessRangeMap::iterator;
1186 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001187
John Zulauf5c5e88d2019-12-26 11:22:02 -07001188 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001189 auto &access_state = pos->second;
John Zulauf89311b42020-09-29 16:28:47 -06001190 access_state.ApplyBarrier(barrier_, layout_transition_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001191 return pos;
1192 }
1193
John Zulauf89311b42020-09-29 16:28:47 -06001194 ApplyBarrierFunctor(const SyncBarrier &barrier, bool layout_transition)
1195 : barrier_(barrier), layout_transition_(layout_transition) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001196
John Zulauf89311b42020-09-29 16:28:47 -06001197 private:
1198 const SyncBarrier barrier_;
1199 const bool layout_transition_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001200};
1201
John Zulauf89311b42020-09-29 16:28:47 -06001202// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1203// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1204// of a collection is known/present.
1205class ApplyBarrierOpsFunctor {
1206 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001207 using Iterator = ResourceAccessRangeMap::iterator;
1208 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001209
John Zulauf89311b42020-09-29 16:28:47 -06001210 struct BarrierOp {
1211 SyncBarrier barrier;
1212 bool layout_transition;
1213 BarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1214 : barrier(barrier_), layout_transition(layout_transition_) {}
1215 BarrierOp() = default;
1216 };
John Zulauf5c5e88d2019-12-26 11:22:02 -07001217 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001218 auto &access_state = pos->second;
John Zulauf89311b42020-09-29 16:28:47 -06001219 for (const auto op : barrier_ops_) {
1220 access_state.ApplyBarrier(op.barrier, op.layout_transition);
1221 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001222
John Zulauf89311b42020-09-29 16:28:47 -06001223 if (resolve_) {
1224 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1225 // another walk
1226 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001227 }
1228 return pos;
1229 }
1230
John Zulauf89311b42020-09-29 16:28:47 -06001231 // A valid tag is required IFF any of the barriers ops are a layout transition, as transitions are write ops
1232 ApplyBarrierOpsFunctor(bool resolve, size_t size_hint, const ResourceUsageTag &tag)
1233 : resolve_(resolve), barrier_ops_(), tag_(tag) {
1234 if (size_hint) {
1235 barrier_ops_.reserve(size_hint);
1236 }
1237 };
1238
1239 // A valid tag is required IFF layout_transition is true, as transitions are write ops
1240 ApplyBarrierOpsFunctor(bool resolve, const std::vector<SyncBarrier> &barriers, bool layout_transition,
1241 const ResourceUsageTag &tag)
1242 : resolve_(resolve), barrier_ops_(barriers.size()), tag_(tag) {
1243 for (const auto &barrier : barriers) {
1244 barrier_ops_.emplace_back(barrier, layout_transition);
John Zulauf9cb530d2019-09-30 14:14:10 -06001245 }
1246 }
1247
John Zulauf89311b42020-09-29 16:28:47 -06001248 void PushBack(const SyncBarrier &barrier, bool layout_transition) { barrier_ops_.emplace_back(barrier, layout_transition); }
1249
1250 void PushBack(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
1251 barrier_ops_.reserve(barrier_ops_.size() + barriers.size());
1252 for (const auto &barrier : barriers) {
1253 barrier_ops_.emplace_back(barrier, layout_transition);
1254 }
1255 }
1256
1257 private:
1258 bool resolve_;
1259 std::vector<BarrierOp> barrier_ops_;
1260 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001261};
1262
John Zulauf355e49b2020-04-24 15:11:15 -06001263void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1264 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001265 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1266 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001267}
1268
John Zulauf16adfc92020-04-08 10:28:33 -06001269void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001270 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001271 if (!SimpleBinding(buffer)) return;
1272 const auto base_address = ResourceBaseAddress(buffer);
1273 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1274}
John Zulauf355e49b2020-04-24 15:11:15 -06001275
John Zulauf540266b2020-04-06 18:54:53 -06001276void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001277 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001278 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001279 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001280 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001281 const auto address_type = ImageAddressType(image);
1282 const auto base_address = ResourceBaseAddress(image);
1283 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001284 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001285 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001286 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001287}
John Zulauf7635de32020-05-29 17:14:15 -06001288void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1289 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1290 if (view != nullptr) {
1291 const IMAGE_STATE *image = view->image_state.get();
1292 if (image != nullptr) {
1293 auto *update_range = &view->normalized_subresource_range;
1294 VkImageSubresourceRange masked_range;
1295 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1296 masked_range = view->normalized_subresource_range;
1297 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1298 update_range = &masked_range;
1299 }
1300 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1301 }
1302 }
1303}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001304
John Zulauf355e49b2020-04-24 15:11:15 -06001305void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1306 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1307 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001308 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1309 subresource.layerCount};
1310 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1311}
1312
John Zulauf540266b2020-04-06 18:54:53 -06001313template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001314void AccessContext::UpdateResourceAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001315 if (!SimpleBinding(buffer)) return;
1316 const auto base_address = ResourceBaseAddress(buffer);
1317 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001318}
1319
1320template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001321void AccessContext::UpdateResourceAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1322 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001323 if (!SimpleBinding(image)) return;
1324 const auto address_type = ImageAddressType(image);
1325 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001326
locke-lunargae26eac2020-04-16 15:29:05 -06001327 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001328 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001329
John Zulauf16adfc92020-04-08 10:28:33 -06001330 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001331 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001332 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001333 }
1334}
1335
John Zulauf7635de32020-05-29 17:14:15 -06001336void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1337 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1338 const ResourceUsageTag &tag) {
1339 UpdateStateResolveAction update(*this, tag);
1340 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1341}
1342
John Zulaufaff20662020-06-01 14:07:58 -06001343void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1344 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1345 const ResourceUsageTag &tag) {
1346 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1347 VkExtent3D extent = CastTo3D(render_area.extent);
1348 VkOffset3D offset = CastTo3D(render_area.offset);
1349
1350 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1351 if (rp_state.attachment_last_subpass[i] == subpass) {
1352 if (attachment_views[i] == nullptr) continue; // UNUSED
1353 const auto &view = *attachment_views[i];
1354 const IMAGE_STATE *image = view.image_state.get();
1355 if (image == nullptr) continue;
1356
1357 const auto &ci = attachment_ci[i];
1358 const bool has_depth = FormatHasDepth(ci.format);
1359 const bool has_stencil = FormatHasStencil(ci.format);
1360 const bool is_color = !(has_depth || has_stencil);
1361 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1362
1363 if (is_color && store_op_stores) {
1364 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1365 offset, extent, tag);
1366 } else {
1367 auto update_range = view.normalized_subresource_range;
1368 if (has_depth && store_op_stores) {
1369 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1370 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1371 tag);
1372 }
1373 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1374 if (has_stencil && stencil_op_stores) {
1375 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1376 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1377 tag);
1378 }
1379 }
1380 }
1381 }
1382}
1383
John Zulauf540266b2020-04-06 18:54:53 -06001384template <typename Action>
1385void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1386 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001387 for (const auto address_type : kAddressTypes) {
1388 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001389 }
1390}
1391
1392void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001393 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1394 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -06001395 for (const auto address_type : kAddressTypes) {
John Zulaufa0a98292020-09-18 09:30:10 -06001396 context.ResolveAccessRange(address_type, full_range, context.GetDstExternalTrackBack().barriers,
John Zulauf355e49b2020-04-24 15:11:15 -06001397 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001398 }
1399 }
1400}
1401
John Zulauf355e49b2020-04-24 15:11:15 -06001402// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001403HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001404 if (!attach_view) return HazardResult();
1405 const auto image_state = attach_view->image_state.get();
1406 if (!image_state) return HazardResult();
1407
John Zulauf355e49b2020-04-24 15:11:15 -06001408 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001409 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001410
1411 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001412 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1413 const auto merged_barrier = MergeBarriers(track_back.barriers);
1414 HazardResult hazard =
1415 track_back.context->DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
1416 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001417 if (!hazard.hazard) {
1418 // The Async hazard check is against the current context's async set.
John Zulaufa0a98292020-09-18 09:30:10 -06001419 hazard = DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001420 attach_view->normalized_subresource_range, kDetectAsync);
1421 }
John Zulaufa0a98292020-09-18 09:30:10 -06001422
John Zulauf355e49b2020-04-24 15:11:15 -06001423 return hazard;
1424}
1425
1426// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1427bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1428
1429 const VkRenderPassBeginInfo *pRenderPassBegin,
1430 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1431 const char *func_name) const {
1432 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1433 bool skip = false;
1434 uint32_t subpass = 0;
1435 const auto &transitions = rp_state.subpass_transitions[subpass];
1436 if (transitions.size()) {
1437 const std::vector<AccessContext> empty_context_vector;
1438 // Create context we can use to validate against...
1439 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1440 const_cast<AccessContext *>(&cb_access_context_));
1441
1442 assert(pRenderPassBegin);
1443 if (nullptr == pRenderPassBegin) return skip;
1444
1445 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1446 assert(fb_state);
1447 if (nullptr == fb_state) return skip;
1448
1449 // Create a limited array of views (which we'll need to toss
1450 std::vector<const IMAGE_VIEW_STATE *> views;
1451 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
1452 const auto attachment_count = count_attachment.first;
1453 const auto *attachments = count_attachment.second;
1454 views.resize(attachment_count, nullptr);
1455 for (const auto &transition : transitions) {
1456 assert(transition.attachment < attachment_count);
1457 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
1458 }
1459
John Zulauf7635de32020-05-29 17:14:15 -06001460 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
1461 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001462 }
1463 return skip;
1464}
1465
locke-lunarg61870c22020-06-09 14:51:50 -06001466bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1467 const char *func_name) const {
1468 bool skip = false;
1469 const PIPELINE_STATE *pPipe = nullptr;
1470 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1471 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1472 if (!pPipe || !per_sets) {
1473 return skip;
1474 }
1475
1476 using DescriptorClass = cvdescriptorset::DescriptorClass;
1477 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1478 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1479 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1480 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1481
1482 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarg37047832020-06-12 13:44:45 -06001483 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001484 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1485 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001486 for (const auto &set_binding : stage_state.descriptor_uses) {
1487 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1488 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1489 set_binding.first.second);
1490 const auto descriptor_type = binding_it.GetType();
1491 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1492 auto array_idx = 0;
1493
1494 if (binding_it.IsVariableDescriptorCount()) {
1495 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1496 }
1497 SyncStageAccessIndex sync_index =
1498 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1499
1500 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1501 uint32_t index = i - index_range.start;
1502 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1503 switch (descriptor->GetClass()) {
1504 case DescriptorClass::ImageSampler:
1505 case DescriptorClass::Image: {
1506 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001507 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001508 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001509 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1510 img_view_state = image_sampler_descriptor->GetImageViewState();
1511 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001512 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001513 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1514 img_view_state = image_descriptor->GetImageViewState();
1515 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001516 }
1517 if (!img_view_state) continue;
1518 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1519 VkExtent3D extent = {};
1520 VkOffset3D offset = {};
1521 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1522 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1523 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1524 } else {
1525 extent = img_state->createInfo.extent;
1526 }
John Zulauf361fb532020-07-22 10:45:39 -06001527 HazardResult hazard;
1528 const auto &subresource_range = img_view_state->normalized_subresource_range;
1529 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1530 // Input attachments are subject to raster ordering rules
1531 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
1532 kAttachmentRasterOrder, offset, extent);
1533 } else {
1534 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1535 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001536 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001537 skip |= sync_state_->LogError(
1538 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001539 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1540 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001541 func_name, string_SyncHazard(hazard.hazard),
1542 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1543 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1544 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001545 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1546 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1547 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001548 }
1549 break;
1550 }
1551 case DescriptorClass::TexelBuffer: {
1552 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1553 if (!buf_view_state) continue;
1554 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001555 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001556 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001557 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001558 skip |= sync_state_->LogError(
1559 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001560 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1561 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001562 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1563 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1564 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001565 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1566 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1567 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001568 }
1569 break;
1570 }
1571 case DescriptorClass::GeneralBuffer: {
1572 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1573 auto buf_state = buffer_descriptor->GetBufferState();
1574 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001575 const ResourceAccessRange range =
1576 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001577 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001578 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001579 skip |= sync_state_->LogError(
1580 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001581 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1582 func_name, string_SyncHazard(hazard.hazard),
1583 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001584 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1585 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001586 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1587 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1588 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001589 }
1590 break;
1591 }
1592 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1593 default:
1594 break;
1595 }
1596 }
1597 }
1598 }
1599 return skip;
1600}
1601
1602void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1603 const ResourceUsageTag &tag) {
1604 const PIPELINE_STATE *pPipe = nullptr;
1605 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1606 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1607 if (!pPipe || !per_sets) {
1608 return;
1609 }
1610
1611 using DescriptorClass = cvdescriptorset::DescriptorClass;
1612 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1613 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1614 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1615 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1616
1617 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001618 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
1619 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1620 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001621 for (const auto &set_binding : stage_state.descriptor_uses) {
1622 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1623 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1624 set_binding.first.second);
1625 const auto descriptor_type = binding_it.GetType();
1626 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1627 auto array_idx = 0;
1628
1629 if (binding_it.IsVariableDescriptorCount()) {
1630 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1631 }
1632 SyncStageAccessIndex sync_index =
1633 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1634
1635 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1636 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1637 switch (descriptor->GetClass()) {
1638 case DescriptorClass::ImageSampler:
1639 case DescriptorClass::Image: {
1640 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1641 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1642 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1643 } else {
1644 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1645 }
1646 if (!img_view_state) continue;
1647 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1648 VkExtent3D extent = {};
1649 VkOffset3D offset = {};
1650 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1651 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1652 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1653 } else {
1654 extent = img_state->createInfo.extent;
1655 }
1656 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1657 offset, extent, tag);
1658 break;
1659 }
1660 case DescriptorClass::TexelBuffer: {
1661 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1662 if (!buf_view_state) continue;
1663 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001664 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001665 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1666 break;
1667 }
1668 case DescriptorClass::GeneralBuffer: {
1669 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1670 auto buf_state = buffer_descriptor->GetBufferState();
1671 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001672 const ResourceAccessRange range =
1673 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001674 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1675 break;
1676 }
1677 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1678 default:
1679 break;
1680 }
1681 }
1682 }
1683 }
1684}
1685
1686bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1687 bool skip = false;
1688 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1689 if (!pPipe) {
1690 return skip;
1691 }
1692
1693 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1694 const auto &binding_buffers_size = binding_buffers.size();
1695 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1696
1697 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1698 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1699 if (binding_description.binding < binding_buffers_size) {
1700 const auto &binding_buffer = binding_buffers[binding_description.binding];
1701 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1702
1703 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06001704 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1705 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001706 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1707 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001708 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001709 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 -06001710 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001711 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001712 }
1713 }
1714 }
1715 return skip;
1716}
1717
1718void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
1719 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1720 if (!pPipe) {
1721 return;
1722 }
1723 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1724 const auto &binding_buffers_size = binding_buffers.size();
1725 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1726
1727 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1728 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1729 if (binding_description.binding < binding_buffers_size) {
1730 const auto &binding_buffer = binding_buffers[binding_description.binding];
1731 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1732
1733 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06001734 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1735 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001736 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1737 }
1738 }
1739}
1740
1741bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1742 bool skip = false;
1743 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
1744
1745 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1746 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001747 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1748 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001749 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1750 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001751 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001752 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 -06001753 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001754 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001755 }
1756
1757 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1758 // We will detect more accurate range in the future.
1759 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1760 return skip;
1761}
1762
1763void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
1764 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return;
1765
1766 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1767 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001768 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1769 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001770 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1771
1772 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1773 // We will detect more accurate range in the future.
1774 RecordDrawVertex(UINT32_MAX, 0, tag);
1775}
1776
1777bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06001778 bool skip = false;
1779 if (!current_renderpass_context_) return skip;
1780 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1781 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1782 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001783}
1784
1785void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
locke-lunarg7077d502020-06-18 21:37:26 -06001786 if (current_renderpass_context_)
1787 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1788 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001789}
1790
John Zulauf355e49b2020-04-24 15:11:15 -06001791bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001792 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001793 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06001794 skip |=
1795 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001796
1797 return skip;
1798}
1799
1800bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1801 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001802 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001803 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001804 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06001805 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1806 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001807
1808 return skip;
1809}
1810
1811void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1812 assert(sync_state_);
1813 if (!cb_state_) return;
1814
1815 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06001816 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06001817 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06001818 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001819 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001820}
1821
John Zulauf355e49b2020-04-24 15:11:15 -06001822void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001823 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001824 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001825 current_context_ = &current_renderpass_context_->CurrentContext();
1826}
1827
John Zulauf355e49b2020-04-24 15:11:15 -06001828void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001829 assert(current_renderpass_context_);
1830 if (!current_renderpass_context_) return;
1831
John Zulauf1a224292020-06-30 14:52:13 -06001832 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001833 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001834 current_renderpass_context_ = nullptr;
1835}
1836
locke-lunarg61870c22020-06-09 14:51:50 -06001837bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1838 const VkRect2D &render_area, const char *func_name) const {
1839 bool skip = false;
locke-lunarg96dc9632020-06-10 17:22:18 -06001840 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001841 if (!pPipe ||
1842 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001843 return skip;
1844 }
1845 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001846 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1847 VkExtent3D extent = CastTo3D(render_area.extent);
1848 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001849
John Zulauf1a224292020-06-30 14:52:13 -06001850 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001851 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001852 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1853 for (const auto location : list) {
1854 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1855 continue;
1856 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001857 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1858 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06001859 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001860 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001861 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001862 func_name, string_SyncHazard(hazard.hazard),
1863 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1864 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001865 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001866 }
1867 }
1868 }
locke-lunarg37047832020-06-12 13:44:45 -06001869
1870 // PHASE1 TODO: Add layout based read/vs. write selection.
1871 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1872 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1873 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001874 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001875 bool depth_write = false, stencil_write = false;
1876
1877 // PHASE1 TODO: These validation should be in core_checks.
1878 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1879 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1880 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1881 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1882 depth_write = true;
1883 }
1884 // PHASE1 TODO: It needs to check if stencil is writable.
1885 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1886 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1887 // PHASE1 TODO: These validation should be in core_checks.
1888 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1889 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1890 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1891 stencil_write = true;
1892 }
1893
1894 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1895 if (depth_write) {
1896 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001897 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1898 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001899 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001900 skip |= sync_state.LogError(
1901 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001902 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001903 func_name, string_SyncHazard(hazard.hazard),
1904 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1905 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001906 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001907 }
1908 }
1909 if (stencil_write) {
1910 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001911 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1912 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001913 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001914 skip |= sync_state.LogError(
1915 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001916 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001917 func_name, string_SyncHazard(hazard.hazard),
1918 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1919 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001920 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001921 }
locke-lunarg61870c22020-06-09 14:51:50 -06001922 }
1923 }
1924 return skip;
1925}
1926
locke-lunarg96dc9632020-06-10 17:22:18 -06001927void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1928 const ResourceUsageTag &tag) {
1929 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001930 if (!pPipe ||
1931 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001932 return;
1933 }
1934 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001935 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1936 VkExtent3D extent = CastTo3D(render_area.extent);
1937 VkOffset3D offset = CastTo3D(render_area.offset);
1938
John Zulauf1a224292020-06-30 14:52:13 -06001939 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001940 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001941 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1942 for (const auto location : list) {
1943 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1944 continue;
1945 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001946 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
1947 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001948 }
1949 }
locke-lunarg37047832020-06-12 13:44:45 -06001950
1951 // PHASE1 TODO: Add layout based read/vs. write selection.
1952 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1953 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1954 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001955 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001956 bool depth_write = false, stencil_write = false;
1957
1958 // PHASE1 TODO: These validation should be in core_checks.
1959 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1960 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1961 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1962 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1963 depth_write = true;
1964 }
1965 // PHASE1 TODO: It needs to check if stencil is writable.
1966 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1967 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1968 // PHASE1 TODO: These validation should be in core_checks.
1969 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1970 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1971 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1972 stencil_write = true;
1973 }
1974
1975 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1976 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001977 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1978 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001979 }
1980 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001981 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1982 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001983 }
locke-lunarg61870c22020-06-09 14:51:50 -06001984 }
1985}
1986
John Zulauf1507ee42020-05-18 11:33:09 -06001987bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
1988 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001989 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001990 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06001991 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1992 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001993 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1994 func_name);
1995
John Zulauf355e49b2020-04-24 15:11:15 -06001996 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06001997 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06001998 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1999 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
2000 return skip;
2001}
2002bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
2003 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002004 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002005 bool skip = false;
2006 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2007 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002008 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2009 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06002010 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002011 return skip;
2012}
2013
John Zulauf7635de32020-05-29 17:14:15 -06002014AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2015 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2016}
2017
2018bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2019 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002020 bool skip = false;
2021
John Zulauf7635de32020-05-29 17:14:15 -06002022 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2023 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2024 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2025 // to apply and only copy then, if this proves a hot spot.
2026 std::unique_ptr<AccessContext> proxy_for_current;
2027
John Zulauf355e49b2020-04-24 15:11:15 -06002028 // Validate the "finalLayout" transitions to external
2029 // Get them from where there we're hidding in the extra entry.
2030 const auto &final_transitions = rp_state_->subpass_transitions.back();
2031 for (const auto &transition : final_transitions) {
2032 const auto &attach_view = attachment_views_[transition.attachment];
2033 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2034 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002035 auto *context = trackback.context;
2036
2037 if (transition.prev_pass == current_subpass_) {
2038 if (!proxy_for_current) {
2039 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2040 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2041 }
2042 context = proxy_for_current.get();
2043 }
2044
John Zulaufa0a98292020-09-18 09:30:10 -06002045 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2046 const auto merged_barrier = MergeBarriers(trackback.barriers);
2047 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope,
2048 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2049 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002050 if (hazard.hazard) {
2051 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2052 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf389c34b2020-07-28 11:19:35 -06002053 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002054 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf389c34b2020-07-28 11:19:35 -06002055 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf37ceaed2020-07-03 16:18:15 -06002056 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002057 }
2058 }
2059 return skip;
2060}
2061
2062void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2063 // Add layout transitions...
2064 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
2065 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf89311b42020-09-29 16:28:47 -06002066 std::unordered_map<const IMAGE_VIEW_STATE *, ApplyBarrierOpsFunctor> view_ops;
John Zulauf355e49b2020-04-24 15:11:15 -06002067 for (const auto &transition : transitions) {
2068 const auto attachment_view = attachment_views_[transition.attachment];
2069 if (!attachment_view) continue;
2070 const auto image = attachment_view->image_state.get();
2071 if (!image) continue;
2072
John Zulaufa0a98292020-09-18 09:30:10 -06002073 const auto *trackback = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
2074 assert(trackback);
John Zulaufc9201222020-05-13 15:13:03 -06002075
John Zulauf89311b42020-09-29 16:28:47 -06002076 // And the trackback barriers to the to the barriers for this view
2077 auto found = view_ops.find(attachment_view);
2078 if (found != view_ops.end()) {
2079 // Add this set of barriers to the list for this view
2080 found->second.PushBack(trackback->barriers, true /* layout transition */);
John Zulaufc9201222020-05-13 15:13:03 -06002081 } else {
John Zulauf89311b42020-09-29 16:28:47 -06002082 // TODO: Aliasing. When we clean up for aliasing support, may need to reassess using true -> resolve, below.
2083 view_ops.insert(std::make_pair(attachment_view, ApplyBarrierOpsFunctor(true /* resolve */, trackback->barriers,
2084 true /* layout transition */, tag)));
John Zulaufc9201222020-05-13 15:13:03 -06002085 }
John Zulauf355e49b2020-04-24 15:11:15 -06002086 }
John Zulauf89311b42020-09-29 16:28:47 -06002087 // For each view with a layout transition, apply the barriers
2088 for (const auto &op : view_ops) {
2089 const auto view = op.first;
2090 const auto image = view->image_state.get();
2091 subpass_context.UpdateResourceAccess(*image, view->normalized_subresource_range, op.second);
2092 }
John Zulauf355e49b2020-04-24 15:11:15 -06002093}
2094
John Zulauf1507ee42020-05-18 11:33:09 -06002095void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2096 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2097 auto &subpass_context = subpass_contexts_[current_subpass_];
2098 VkExtent3D extent = CastTo3D(render_area.extent);
2099 VkOffset3D offset = CastTo3D(render_area.offset);
2100
2101 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2102 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2103 if (attachment_views_[i] == nullptr) continue; // UNUSED
2104 const auto &view = *attachment_views_[i];
2105 const IMAGE_STATE *image = view.image_state.get();
2106 if (image == nullptr) continue;
2107
2108 const auto &ci = attachment_ci[i];
2109 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002110 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002111 const bool is_color = !(has_depth || has_stencil);
2112
2113 if (is_color) {
2114 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2115 extent, tag);
2116 } else {
2117 auto update_range = view.normalized_subresource_range;
2118 if (has_depth) {
2119 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2120 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2121 }
2122 if (has_stencil) {
2123 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2124 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2125 tag);
2126 }
2127 }
2128 }
2129 }
2130}
2131
John Zulauf355e49b2020-04-24 15:11:15 -06002132void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002133 const AccessContext *external_context, VkQueueFlags queue_flags,
2134 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002135 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002136 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002137 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2138 // Add this for all subpasses here so that they exsist during next subpass validation
2139 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002140 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002141 }
2142 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2143
2144 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002145 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002146}
John Zulauf1507ee42020-05-18 11:33:09 -06002147
2148void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002149 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2150 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002151 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002152
John Zulauf355e49b2020-04-24 15:11:15 -06002153 current_subpass_++;
2154 assert(current_subpass_ < subpass_contexts_.size());
2155 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002156 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002157}
2158
John Zulauf1a224292020-06-30 14:52:13 -06002159void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2160 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002161 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002162 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002163 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002164
John Zulauf355e49b2020-04-24 15:11:15 -06002165 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002166 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002167
2168 // Add the "finalLayout" transitions to external
2169 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002170 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2171 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2172 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002173 const auto &final_transitions = rp_state_->subpass_transitions.back();
2174 for (const auto &transition : final_transitions) {
2175 const auto &attachment = attachment_views_[transition.attachment];
2176 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002177 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf89311b42020-09-29 16:28:47 -06002178 ApplyBarrierOpsFunctor barrier_ops(true /* resolve */, last_trackback.barriers, true /* layout transition */, tag);
2179 external_context->UpdateResourceAccess(*attachment->image_state, attachment->normalized_subresource_range, barrier_ops);
John Zulauf355e49b2020-04-24 15:11:15 -06002180 }
2181}
2182
John Zulauf3d84f1b2020-03-09 13:33:25 -06002183SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2184 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2185 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2186 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2187 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2188 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2189 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2190}
2191
John Zulauf89311b42020-09-29 16:28:47 -06002192// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2193// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2194// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufa0a98292020-09-18 09:30:10 -06002195void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers) {
2196 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002197 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002198 }
John Zulauf89311b42020-09-29 16:28:47 -06002199 ApplyPendingBarriers(kCurrentCommandTag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002200}
2201
John Zulauf9cb530d2019-09-30 14:14:10 -06002202HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2203 HazardResult hazard;
2204 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002205 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002206 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002207 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002208 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002209 }
2210 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002211 // Write operation:
2212 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2213 // If reads exists -- test only against them because either:
2214 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2215 // * 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
2216 // the current write happens after the reads, so just test the write against the reades
2217 // Otherwise test against last_write
2218 //
2219 // Look for casus belli for WAR
2220 if (last_read_count) {
2221 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2222 const auto &read_access = last_reads[read_index];
2223 if (IsReadHazard(usage_stage, read_access)) {
2224 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2225 break;
2226 }
2227 }
John Zulauf361fb532020-07-22 10:45:39 -06002228 } else if (last_write && IsWriteHazard(usage)) {
2229 // Write-After-Write check -- if we have a previous write to test against
2230 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002231 }
2232 }
2233 return hazard;
2234}
2235
John Zulauf69133422020-05-20 14:55:53 -06002236HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2237 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2238 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002239 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002240 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002241 const bool input_attachment_ordering = 0 != (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
2242 const bool last_write_is_ordered = 0 != (last_write & ordering.access_scope);
2243 if (IsRead(usage_bit)) {
2244 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2245 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2246 if (is_raw_hazard) {
2247 // NOTE: we know last_write is non-zero
2248 // See if the ordering rules save us from the simple RAW check above
2249 // First check to see if the current usage is covered by the ordering rules
2250 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2251 const bool usage_is_ordered =
2252 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2253 if (usage_is_ordered) {
2254 // Now see of the most recent write (or a subsequent read) are ordered
2255 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2256 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002257 }
2258 }
John Zulauf4285ee92020-09-23 10:20:52 -06002259 if (is_raw_hazard) {
2260 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2261 }
John Zulauf361fb532020-07-22 10:45:39 -06002262 } else {
2263 // Only check for WAW if there are no reads since last_write
John Zulauf4285ee92020-09-23 10:20:52 -06002264 bool usage_write_is_ordered = 0 != (usage_bit & ordering.access_scope);
John Zulauf361fb532020-07-22 10:45:39 -06002265 if (last_read_count) {
John Zulauf361fb532020-07-22 10:45:39 -06002266 // Look for any WAR hazards outside the ordered set of stages
John Zulauf4285ee92020-09-23 10:20:52 -06002267 VkPipelineStageFlags ordered_stages = 0;
2268 if (usage_write_is_ordered) {
2269 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2270 ordered_stages = GetOrderedStages(ordering);
2271 }
2272 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2273 if ((ordered_stages & last_read_stages) != last_read_stages) {
2274 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2275 const auto &read_access = last_reads[read_index];
2276 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2277 if (IsReadHazard(usage_stage, read_access)) {
2278 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2279 break;
2280 }
John Zulaufd14743a2020-07-03 09:42:39 -06002281 }
2282 }
John Zulauf4285ee92020-09-23 10:20:52 -06002283 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
2284 if (last_write && IsWriteHazard(usage_bit)) {
2285 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002286 }
John Zulauf69133422020-05-20 14:55:53 -06002287 }
2288 }
2289 return hazard;
2290}
2291
John Zulauf2f952d22020-02-10 11:34:51 -07002292// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06002293HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002294 HazardResult hazard;
2295 auto usage = FlagBit(usage_index);
2296 if (IsRead(usage)) {
2297 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002298 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002299 }
2300 } else {
2301 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002302 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002303 } else if (last_read_count > 0) {
John Zulauf4285ee92020-09-23 10:20:52 -06002304 // Any read could be reported, so we'll just pick the first one arbitrarily
John Zulauf59e25072020-07-17 10:55:21 -06002305 hazard.Set(this, usage_index, WRITE_RACING_READ, last_reads[0].access, last_reads[0].tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002306 }
2307 }
2308 return hazard;
2309}
2310
John Zulauf36bcf6a2020-02-03 15:12:52 -07002311HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
2312 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002313 // Only supporting image layout transitions for now
2314 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2315 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002316 // only test for WAW if there no intervening read operations.
2317 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2318 if (last_read_count) {
John Zulauf355e49b2020-04-24 15:11:15 -06002319 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002320 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002321 const auto &read_access = last_reads[read_index];
2322 // If the read stage is not in the src sync sync
2323 // *AND* not execution chained with an existing sync barrier (that's the or)
2324 // then the barrier access is unsafe (R/W after R)
2325 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002326 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002327 break;
2328 }
2329 }
John Zulauf361fb532020-07-22 10:45:39 -06002330 } else if (last_write) {
2331 // If the previous write is *not* in the 1st access scope
2332 // *AND* the current barrier is not in the dependency chain
2333 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2334 // then the barrier access is unsafe (R/W after W)
2335 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
2336 // TODO: Do we need a difference hazard name for this?
2337 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2338 }
John Zulaufd14743a2020-07-03 09:42:39 -06002339 }
John Zulauf361fb532020-07-22 10:45:39 -06002340
John Zulauf0cb5be22020-01-23 12:18:22 -07002341 return hazard;
2342}
2343
John Zulauf5f13a792020-03-10 07:31:21 -06002344// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2345// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2346// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2347void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2348 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002349 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2350 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002351 *this = other;
2352 } else if (!other.write_tag.IsBefore(write_tag)) {
2353 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2354 // dependency chaining logic or any stage expansion)
2355 write_barriers |= other.write_barriers;
2356
John Zulaufd14743a2020-07-03 09:42:39 -06002357 // Merge the read states
John Zulauf4285ee92020-09-23 10:20:52 -06002358 const auto pre_merge_count = last_read_count;
2359 const auto pre_merge_stages = last_read_stages;
John Zulauf5f13a792020-03-10 07:31:21 -06002360 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2361 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002362 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002363 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002364 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2365 // but we should wait on profiling data for that.
2366 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002367 auto &my_read = last_reads[my_read_index];
2368 if (other_read.stage == my_read.stage) {
2369 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002370 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002371 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002372 my_read.tag = other_read.tag;
2373 if (my_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
2374 // Since I'm overwriting the fragement stage read, also update the input attachment info
2375 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06002376 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002377 }
John Zulauf5f13a792020-03-10 07:31:21 -06002378 }
John Zulauf4285ee92020-09-23 10:20:52 -06002379 // TODO: Phase 2 -- review the state merge logic to avoid false negative from the OR'ing of the barriers
2380 // May require tracking more than one access per stage.
John Zulauf5f13a792020-03-10 07:31:21 -06002381 my_read.barriers |= other_read.barriers;
2382 break;
2383 }
2384 }
2385 } else {
2386 // The other read stage doesn't exist in this, so add it.
2387 last_reads[last_read_count] = other_read;
2388 last_read_count++;
2389 last_read_stages |= other_read.stage;
John Zulauf4285ee92020-09-23 10:20:52 -06002390 if (other_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06002391 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002392 }
John Zulauf5f13a792020-03-10 07:31:21 -06002393 }
2394 }
John Zulauf361fb532020-07-22 10:45:39 -06002395 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002396 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2397 // ignore it.
John Zulauf5f13a792020-03-10 07:31:21 -06002398}
2399
John Zulauf9cb530d2019-09-30 14:14:10 -06002400void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2401 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2402 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002403 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002404 // Mulitple outstanding reads may be of interest and do dependency chains independently
2405 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2406 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002407 uint32_t update_index = kStageCount;
John Zulauf9cb530d2019-09-30 14:14:10 -06002408 if (usage_stage & last_read_stages) {
2409 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf4285ee92020-09-23 10:20:52 -06002410 if (last_reads[read_index].stage == usage_stage) {
2411 update_index = read_index;
John Zulauf9cb530d2019-09-30 14:14:10 -06002412 break;
2413 }
2414 }
John Zulauf4285ee92020-09-23 10:20:52 -06002415 assert(update_index < last_read_count);
John Zulauf9cb530d2019-09-30 14:14:10 -06002416 } else {
John Zulauf9cb530d2019-09-30 14:14:10 -06002417 assert(last_read_count < last_reads.size());
John Zulauf4285ee92020-09-23 10:20:52 -06002418 update_index = last_read_count++;
John Zulauf9cb530d2019-09-30 14:14:10 -06002419 last_read_stages |= usage_stage;
2420 }
John Zulauf4285ee92020-09-23 10:20:52 -06002421 last_reads[update_index].Set(usage_stage, usage_bit, 0, tag);
2422
2423 // Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
2424 if (usage_stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06002425 // TODO Revisit re: multiple reads for a given stage
2426 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002427 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002428 } else {
2429 // Assume write
2430 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002431 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002432 }
2433}
John Zulauf5f13a792020-03-10 07:31:21 -06002434
John Zulauf89311b42020-09-29 16:28:47 -06002435// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2436// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2437// We can overwrite them as *this* write is now after them.
2438//
2439// Note: intentionally ignore pending barriers and chains (i.e. don't apply or clear them), let ApplyPendingBarriers handle them.
2440void ResourceAccessState::SetWrite(SyncStageAccessFlagBits usage_bit, const ResourceUsageTag &tag) {
2441 last_read_count = 0;
2442 last_read_stages = 0;
2443 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06002444 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06002445
2446 write_barriers = 0;
2447 write_dependency_chain = 0;
2448 write_tag = tag;
2449 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002450}
2451
John Zulauf89311b42020-09-29 16:28:47 -06002452// Apply the memory barrier without updating the existing barriers. The execution barrier
2453// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2454// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2455// replace the current write barriers or add to them, so accumulate to pending as well.
2456void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2457 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2458 // applying the memory barriers
2459 if (InSourceScopeOrChain(barrier.src_exec_scope, barrier.src_access_scope)) {
2460 pending_write_barriers |= barrier.dst_access_scope;
2461 pending_write_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002462 }
John Zulauf89311b42020-09-29 16:28:47 -06002463 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2464 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06002465
John Zulauf89311b42020-09-29 16:28:47 -06002466 if (!pending_layout_transition) {
2467 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2468 // don't need to be tracked as we're just going to zero them.
John Zulaufa0a98292020-09-18 09:30:10 -06002469 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf89311b42020-09-29 16:28:47 -06002470 ReadState &access = last_reads[read_index];
2471 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2472 if (barrier.src_exec_scope & (access.stage | access.barriers)) {
2473 access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002474 }
2475 }
John Zulaufa0a98292020-09-18 09:30:10 -06002476 }
John Zulaufa0a98292020-09-18 09:30:10 -06002477}
2478
John Zulauf89311b42020-09-29 16:28:47 -06002479void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
2480 if (pending_layout_transition) {
2481 // Should only be true for valid tags -- barrier/subpass record not "resolve previous" traversals...
2482 assert(tag != kCurrentCommandTag);
2483 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
2484 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
2485 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06002486 }
John Zulauf89311b42020-09-29 16:28:47 -06002487
2488 // Apply the accumulate execution barriers (and thus update chaining information)
2489 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
2490 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2491 ReadState &access = last_reads[read_index];
2492 access.barriers |= access.pending_dep_chain;
2493 read_execution_barriers |= access.barriers;
2494 access.pending_dep_chain = 0;
2495 }
2496
2497 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
2498 write_dependency_chain |= pending_write_dep_chain;
2499 write_barriers |= pending_write_barriers;
2500 pending_write_dep_chain = 0;
2501 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002502}
2503
John Zulauf59e25072020-07-17 10:55:21 -06002504// This should be just Bits or Index, but we don't have an invalid state for Index
2505VkPipelineStageFlags ResourceAccessState::GetReadBarriers(SyncStageAccessFlags usage_bit) const {
2506 VkPipelineStageFlags barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06002507
2508 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2509 const auto &read_access = last_reads[read_index];
2510 if (read_access.access & usage_bit) {
2511 barriers = read_access.barriers;
2512 break;
John Zulauf59e25072020-07-17 10:55:21 -06002513 }
2514 }
John Zulauf4285ee92020-09-23 10:20:52 -06002515
John Zulauf59e25072020-07-17 10:55:21 -06002516 return barriers;
2517}
2518
John Zulauf4285ee92020-09-23 10:20:52 -06002519inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlagBits usage_stage, SyncStageAccessFlagBits usage) const {
2520 assert(IsRead(usage));
2521 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
2522 // * the previous reads are not hazards, and thus last_write must be visible and available to
2523 // any reads that happen after.
2524 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2525 // the current read will be also not be a hazard, thus reporting a hazard here adds no needed information.
2526 return (0 != last_write) && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
2527}
2528
2529bool ResourceAccessState::IsWARHazard(VkPipelineStageFlagBits usage_stage, SyncStageAccessFlagBits usage) const { return false; }
2530
2531VkPipelineStageFlags ResourceAccessState::GetOrderedStages(const SyncOrderingBarrier &ordering) const {
2532 // Whether the stage are in the ordering scope only matters if the current write is ordered
2533 VkPipelineStageFlags ordered_stages = last_read_stages & ordering.exec_scope;
2534 // Special input attachment handling as always (not encoded in exec_scop)
John Zulauf89311b42020-09-29 16:28:47 -06002535 const bool input_attachment_ordering = 0 != (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauff51fbb62020-10-02 14:43:24 -06002536 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06002537 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
2538 ordered_stages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
2539 }
2540
2541 return ordered_stages;
2542}
2543
2544inline ResourceAccessState::ReadState *ResourceAccessState::GetReadStateForStage(VkPipelineStageFlagBits stage,
2545 uint32_t search_limit) {
2546 ReadState *read_state = nullptr;
2547 search_limit = std::min(search_limit, last_read_count);
2548 for (uint32_t i = 0; i < search_limit; i++) {
2549 if (last_reads[i].stage == stage) {
2550 read_state = &last_reads[i];
2551 break;
2552 }
2553 }
2554 return read_state;
2555}
2556
John Zulaufd1f85d42020-04-15 12:23:15 -06002557void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002558 auto *access_context = GetAccessContextNoInsert(command_buffer);
2559 if (access_context) {
2560 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002561 }
2562}
2563
John Zulaufd1f85d42020-04-15 12:23:15 -06002564void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2565 auto access_found = cb_access_state.find(command_buffer);
2566 if (access_found != cb_access_state.end()) {
2567 access_found->second->Reset();
2568 cb_access_state.erase(access_found);
2569 }
2570}
2571
John Zulauf89311b42020-09-29 16:28:47 -06002572void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2573 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags src_access_scope,
2574 SyncStageAccessFlags dst_access_scope, uint32_t memory_barrier_count,
2575 const VkMemoryBarrier *pMemoryBarriers, const ResourceUsageTag &tag) {
2576 ApplyBarrierOpsFunctor barriers_functor(true /* resolve */, std::min<uint32_t>(1, memory_barrier_count), tag);
2577 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
2578 const auto &barrier = pMemoryBarriers[barrier_index];
2579 SyncBarrier sync_barrier(src_exec_scope, SyncStageAccess::AccessScope(src_access_scope, barrier.srcAccessMask),
2580 dst_exec_scope, SyncStageAccess::AccessScope(dst_access_scope, barrier.dstAccessMask));
2581 barriers_functor.PushBack(sync_barrier, false);
2582 }
2583 if (0 == memory_barrier_count) {
2584 // If there are no global memory barriers, force an exec barrier
2585 barriers_functor.PushBack(SyncBarrier(src_exec_scope, 0, dst_exec_scope, 0), false);
2586 }
John Zulauf540266b2020-04-06 18:54:53 -06002587 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002588}
2589
John Zulauf540266b2020-04-06 18:54:53 -06002590void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002591 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2592 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002593 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002594 for (uint32_t index = 0; index < barrier_count; index++) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002595 auto barrier = barriers[index]; // barrier is a copy
John Zulauf9cb530d2019-09-30 14:14:10 -06002596 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2597 if (!buffer) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002598 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
2599 const ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002600 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2601 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06002602 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2603 const ApplyBarrierFunctor update_action(sync_barrier, false /* layout_transition */);
2604 context->UpdateResourceAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002605 }
2606}
2607
John Zulauf540266b2020-04-06 18:54:53 -06002608void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2609 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2610 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002611 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002612 for (uint32_t index = 0; index < barrier_count; index++) {
2613 const auto &barrier = barriers[index];
2614 const auto *image = Get<IMAGE_STATE>(barrier.image);
2615 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002616 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002617 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2618 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2619 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06002620 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2621 const ApplyBarrierFunctor barrier_action(sync_barrier, layout_transition);
2622 context->UpdateResourceAccess(*image, subresource_range, barrier_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002623 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002624}
2625
2626bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2627 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2628 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002629 const auto *cb_context = GetAccessContext(commandBuffer);
2630 assert(cb_context);
2631 if (!cb_context) return skip;
2632 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002633
John Zulauf3d84f1b2020-03-09 13:33:25 -06002634 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002635 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002636 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002637
2638 for (uint32_t region = 0; region < regionCount; region++) {
2639 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002640 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002641 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002642 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002643 if (hazard.hazard) {
2644 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002645 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002646 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002647 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002648 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002649 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002650 }
John Zulauf16adfc92020-04-08 10:28:33 -06002651 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002652 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06002653 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002654 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002655 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002656 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002657 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002658 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002659 }
2660 }
2661 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002662 }
2663 return skip;
2664}
2665
2666void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2667 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002668 auto *cb_context = GetAccessContext(commandBuffer);
2669 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002670 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002671 auto *context = cb_context->GetCurrentAccessContext();
2672
John Zulauf9cb530d2019-09-30 14:14:10 -06002673 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002674 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002675
2676 for (uint32_t region = 0; region < regionCount; region++) {
2677 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002678 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002679 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002680 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002681 }
John Zulauf16adfc92020-04-08 10:28:33 -06002682 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002683 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002684 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002685 }
2686 }
2687}
2688
Jeff Leger178b1e52020-10-05 12:22:23 -04002689bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
2690 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
2691 bool skip = false;
2692 const auto *cb_context = GetAccessContext(commandBuffer);
2693 assert(cb_context);
2694 if (!cb_context) return skip;
2695 const auto *context = cb_context->GetCurrentAccessContext();
2696
2697 // If we have no previous accesses, we have no hazards
2698 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2699 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2700
2701 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2702 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2703 if (src_buffer) {
2704 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2705 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
2706 if (hazard.hazard) {
2707 // TODO -- add tag information to log msg when useful.
2708 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
2709 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
2710 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
2711 region, string_UsageTag(hazard).c_str());
2712 }
2713 }
2714 if (dst_buffer && !skip) {
2715 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2716 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
2717 if (hazard.hazard) {
2718 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
2719 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
2720 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
2721 region, string_UsageTag(hazard).c_str());
2722 }
2723 }
2724 if (skip) break;
2725 }
2726 return skip;
2727}
2728
2729void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
2730 auto *cb_context = GetAccessContext(commandBuffer);
2731 assert(cb_context);
2732 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
2733 auto *context = cb_context->GetCurrentAccessContext();
2734
2735 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2736 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2737
2738 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2739 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2740 if (src_buffer) {
2741 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2742 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
2743 }
2744 if (dst_buffer) {
2745 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2746 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
2747 }
2748 }
2749}
2750
John Zulauf5c5e88d2019-12-26 11:22:02 -07002751bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2752 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2753 const VkImageCopy *pRegions) const {
2754 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002755 const auto *cb_access_context = GetAccessContext(commandBuffer);
2756 assert(cb_access_context);
2757 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002758
John Zulauf3d84f1b2020-03-09 13:33:25 -06002759 const auto *context = cb_access_context->GetCurrentAccessContext();
2760 assert(context);
2761 if (!context) return skip;
2762
2763 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2764 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002765 for (uint32_t region = 0; region < regionCount; region++) {
2766 const auto &copy_region = pRegions[region];
2767 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002768 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002769 copy_region.srcOffset, copy_region.extent);
2770 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002771 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002772 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002773 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002774 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002775 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002776 }
2777
2778 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002779 VkExtent3D dst_copy_extent =
2780 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002781 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002782 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002783 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002784 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002785 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002786 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002787 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002788 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002789 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002790 }
2791 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002792
John Zulauf5c5e88d2019-12-26 11:22:02 -07002793 return skip;
2794}
2795
2796void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2797 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2798 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002799 auto *cb_access_context = GetAccessContext(commandBuffer);
2800 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002801 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002802 auto *context = cb_access_context->GetCurrentAccessContext();
2803 assert(context);
2804
John Zulauf5c5e88d2019-12-26 11:22:02 -07002805 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002806 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002807
2808 for (uint32_t region = 0; region < regionCount; region++) {
2809 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002810 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002811 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2812 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002813 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002814 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002815 VkExtent3D dst_copy_extent =
2816 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002817 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2818 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002819 }
2820 }
2821}
2822
Jeff Leger178b1e52020-10-05 12:22:23 -04002823bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
2824 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
2825 bool skip = false;
2826 const auto *cb_access_context = GetAccessContext(commandBuffer);
2827 assert(cb_access_context);
2828 if (!cb_access_context) return skip;
2829
2830 const auto *context = cb_access_context->GetCurrentAccessContext();
2831 assert(context);
2832 if (!context) return skip;
2833
2834 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2835 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2836 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2837 const auto &copy_region = pCopyImageInfo->pRegions[region];
2838 if (src_image) {
2839 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
2840 copy_region.srcOffset, copy_region.extent);
2841 if (hazard.hazard) {
2842 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
2843 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
2844 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
2845 region, string_UsageTag(hazard).c_str());
2846 }
2847 }
2848
2849 if (dst_image) {
2850 VkExtent3D dst_copy_extent =
2851 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2852 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
2853 copy_region.dstOffset, dst_copy_extent);
2854 if (hazard.hazard) {
2855 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
2856 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
2857 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
2858 region, string_UsageTag(hazard).c_str());
2859 }
2860 if (skip) break;
2861 }
2862 }
2863
2864 return skip;
2865}
2866
2867void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
2868 auto *cb_access_context = GetAccessContext(commandBuffer);
2869 assert(cb_access_context);
2870 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
2871 auto *context = cb_access_context->GetCurrentAccessContext();
2872 assert(context);
2873
2874 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2875 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2876
2877 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2878 const auto &copy_region = pCopyImageInfo->pRegions[region];
2879 if (src_image) {
2880 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2881 copy_region.extent, tag);
2882 }
2883 if (dst_image) {
2884 VkExtent3D dst_copy_extent =
2885 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2886 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2887 dst_copy_extent, tag);
2888 }
2889 }
2890}
2891
John Zulauf9cb530d2019-09-30 14:14:10 -06002892bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2893 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2894 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2895 uint32_t bufferMemoryBarrierCount,
2896 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2897 uint32_t imageMemoryBarrierCount,
2898 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2899 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002900 const auto *cb_access_context = GetAccessContext(commandBuffer);
2901 assert(cb_access_context);
2902 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002903
John Zulauf3d84f1b2020-03-09 13:33:25 -06002904 const auto *context = cb_access_context->GetCurrentAccessContext();
2905 assert(context);
2906 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002907
John Zulauf3d84f1b2020-03-09 13:33:25 -06002908 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002909 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2910 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002911 // Validate Image Layout transitions
2912 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2913 const auto &barrier = pImageMemoryBarriers[index];
2914 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2915 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2916 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002917 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002918 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002919 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002920 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002921 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002922 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002923 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002924 }
2925 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002926
2927 return skip;
2928}
2929
2930void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2931 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2932 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2933 uint32_t bufferMemoryBarrierCount,
2934 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2935 uint32_t imageMemoryBarrierCount,
2936 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002937 auto *cb_access_context = GetAccessContext(commandBuffer);
2938 assert(cb_access_context);
2939 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002940 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002941 auto access_context = cb_access_context->GetCurrentAccessContext();
2942 assert(access_context);
2943 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06002944
John Zulauf3d84f1b2020-03-09 13:33:25 -06002945 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002946 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002947 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002948 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
2949 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2950 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf89311b42020-09-29 16:28:47 -06002951
2952 // These two apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
2953 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
2954 // of the barriers is maintained.
John Zulauf3d84f1b2020-03-09 13:33:25 -06002955 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
2956 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002957 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06002958 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002959
John Zulauf89311b42020-09-29 16:28:47 -06002960 // Apply the global barriers last as is it walks all memory, it can also clean up the "pending" state without requiring an
2961 // additional pass, updating the dependency chains *last* as it goes along.
2962 // This is needed to guarantee order independence of the three lists.
John Zulauf3d84f1b2020-03-09 13:33:25 -06002963 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf89311b42020-09-29 16:28:47 -06002964 pMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002965}
2966
2967void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
2968 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
2969 // The state tracker sets up the device state
2970 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
2971
John Zulauf5f13a792020-03-10 07:31:21 -06002972 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
2973 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06002974 // TODO: Find a good way to do this hooklessly.
2975 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
2976 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
2977 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
2978
John Zulaufd1f85d42020-04-15 12:23:15 -06002979 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2980 sync_device_state->ResetCommandBufferCallback(command_buffer);
2981 });
2982 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2983 sync_device_state->FreeCommandBufferCallback(command_buffer);
2984 });
John Zulauf9cb530d2019-09-30 14:14:10 -06002985}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002986
John Zulauf355e49b2020-04-24 15:11:15 -06002987bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2988 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
2989 bool skip = false;
2990 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
2991 auto cb_context = GetAccessContext(commandBuffer);
2992
2993 if (rp_state && cb_context) {
2994 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
2995 }
2996
2997 return skip;
2998}
2999
3000bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3001 VkSubpassContents contents) const {
3002 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3003 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3004 subpass_begin_info.contents = contents;
3005 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
3006 return skip;
3007}
3008
3009bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3010 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
3011 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3012 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
3013 return skip;
3014}
3015
3016bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3017 const VkRenderPassBeginInfo *pRenderPassBegin,
3018 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
3019 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3020 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
3021 return skip;
3022}
3023
John Zulauf3d84f1b2020-03-09 13:33:25 -06003024void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3025 VkResult result) {
3026 // The state tracker sets up the command buffer state
3027 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3028
3029 // Create/initialize the structure that trackers accesses at the command buffer scope.
3030 auto cb_access_context = GetAccessContext(commandBuffer);
3031 assert(cb_access_context);
3032 cb_access_context->Reset();
3033}
3034
3035void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06003036 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003037 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003038 if (cb_context) {
3039 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003040 }
3041}
3042
3043void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3044 VkSubpassContents contents) {
3045 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3046 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3047 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003048 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003049}
3050
3051void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3052 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3053 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003054 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003055}
3056
3057void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3058 const VkRenderPassBeginInfo *pRenderPassBegin,
3059 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3060 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003061 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
3062}
3063
3064bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
3065 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
3066 bool skip = false;
3067
3068 auto cb_context = GetAccessContext(commandBuffer);
3069 assert(cb_context);
3070 auto cb_state = cb_context->GetCommandBufferState();
3071 if (!cb_state) return skip;
3072
3073 auto rp_state = cb_state->activeRenderPass;
3074 if (!rp_state) return skip;
3075
3076 skip |= cb_context->ValidateNextSubpass(func_name);
3077
3078 return skip;
3079}
3080
3081bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3082 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
3083 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3084 subpass_begin_info.contents = contents;
3085 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
3086 return skip;
3087}
3088
3089bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
3090 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3091 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3092 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
3093 return skip;
3094}
3095
3096bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3097 const VkSubpassEndInfo *pSubpassEndInfo) const {
3098 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3099 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
3100 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003101}
3102
3103void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003104 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003105 auto cb_context = GetAccessContext(commandBuffer);
3106 assert(cb_context);
3107 auto cb_state = cb_context->GetCommandBufferState();
3108 if (!cb_state) return;
3109
3110 auto rp_state = cb_state->activeRenderPass;
3111 if (!rp_state) return;
3112
John Zulauf355e49b2020-04-24 15:11:15 -06003113 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003114}
3115
3116void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3117 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
3118 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3119 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003120 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003121}
3122
3123void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3124 const VkSubpassEndInfo *pSubpassEndInfo) {
3125 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003126 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003127}
3128
3129void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3130 const VkSubpassEndInfo *pSubpassEndInfo) {
3131 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003132 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003133}
3134
John Zulauf355e49b2020-04-24 15:11:15 -06003135bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
3136 const char *func_name) const {
3137 bool skip = false;
3138
3139 auto cb_context = GetAccessContext(commandBuffer);
3140 assert(cb_context);
3141 auto cb_state = cb_context->GetCommandBufferState();
3142 if (!cb_state) return skip;
3143
3144 auto rp_state = cb_state->activeRenderPass;
3145 if (!rp_state) return skip;
3146
3147 skip |= cb_context->ValidateEndRenderpass(func_name);
3148 return skip;
3149}
3150
3151bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3152 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
3153 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
3154 return skip;
3155}
3156
3157bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
3158 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3159 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
3160 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
3161 return skip;
3162}
3163
3164bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
3165 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3166 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
3167 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
3168 return skip;
3169}
3170
3171void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3172 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003173 // Resolve the all subpass contexts to the command buffer contexts
3174 auto cb_context = GetAccessContext(commandBuffer);
3175 assert(cb_context);
3176 auto cb_state = cb_context->GetCommandBufferState();
3177 if (!cb_state) return;
3178
locke-lunargaecf2152020-05-12 17:15:41 -06003179 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06003180 if (!rp_state) return;
3181
John Zulauf355e49b2020-04-24 15:11:15 -06003182 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06003183}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003184
John Zulauf33fc1d52020-07-17 11:01:10 -06003185// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3186// updates to a resource which do not conflict at the byte level.
3187// TODO: Revisit this rule to see if it needs to be tighter or looser
3188// TODO: Add programatic control over suppression heuristics
3189bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3190 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3191}
3192
John Zulauf3d84f1b2020-03-09 13:33:25 -06003193void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003194 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003195 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003196}
3197
3198void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003199 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003200 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003201}
3202
3203void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003204 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003205 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003206}
locke-lunarga19c71d2020-03-02 18:17:04 -07003207
Jeff Leger178b1e52020-10-05 12:22:23 -04003208template <typename BufferImageCopyRegionType>
3209bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3210 VkImageLayout dstImageLayout, uint32_t regionCount,
3211 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003212 bool skip = false;
3213 const auto *cb_access_context = GetAccessContext(commandBuffer);
3214 assert(cb_access_context);
3215 if (!cb_access_context) return skip;
3216
Jeff Leger178b1e52020-10-05 12:22:23 -04003217 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3218 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3219
locke-lunarga19c71d2020-03-02 18:17:04 -07003220 const auto *context = cb_access_context->GetCurrentAccessContext();
3221 assert(context);
3222 if (!context) return skip;
3223
3224 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003225 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3226
3227 for (uint32_t region = 0; region < regionCount; region++) {
3228 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003229 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003230 ResourceAccessRange src_range =
3231 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003232 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003233 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003234 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003235 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003236 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003237 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003238 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003239 }
3240 }
3241 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003242 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003243 copy_region.imageOffset, copy_region.imageExtent);
3244 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003245 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003246 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003247 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003248 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003249 }
3250 if (skip) break;
3251 }
3252 if (skip) break;
3253 }
3254 return skip;
3255}
3256
Jeff Leger178b1e52020-10-05 12:22:23 -04003257bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3258 VkImageLayout dstImageLayout, uint32_t regionCount,
3259 const VkBufferImageCopy *pRegions) const {
3260 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3261 COPY_COMMAND_VERSION_1);
3262}
3263
3264bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3265 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3266 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3267 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3268 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3269}
3270
3271template <typename BufferImageCopyRegionType>
3272void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3273 VkImageLayout dstImageLayout, uint32_t regionCount,
3274 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003275 auto *cb_access_context = GetAccessContext(commandBuffer);
3276 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003277
3278 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3279 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3280
3281 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003282 auto *context = cb_access_context->GetCurrentAccessContext();
3283 assert(context);
3284
3285 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003286 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003287
3288 for (uint32_t region = 0; region < regionCount; region++) {
3289 const auto &copy_region = pRegions[region];
3290 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003291 ResourceAccessRange src_range =
3292 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003293 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003294 }
3295 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003296 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003297 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003298 }
3299 }
3300}
3301
Jeff Leger178b1e52020-10-05 12:22:23 -04003302void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3303 VkImageLayout dstImageLayout, uint32_t regionCount,
3304 const VkBufferImageCopy *pRegions) {
3305 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3306 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3307}
3308
3309void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3310 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3311 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3312 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3313 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3314 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3315}
3316
3317template <typename BufferImageCopyRegionType>
3318bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3319 VkBuffer dstBuffer, uint32_t regionCount,
3320 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003321 bool skip = false;
3322 const auto *cb_access_context = GetAccessContext(commandBuffer);
3323 assert(cb_access_context);
3324 if (!cb_access_context) return skip;
3325
Jeff Leger178b1e52020-10-05 12:22:23 -04003326 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3327 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3328
locke-lunarga19c71d2020-03-02 18:17:04 -07003329 const auto *context = cb_access_context->GetCurrentAccessContext();
3330 assert(context);
3331 if (!context) return skip;
3332
3333 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3334 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3335 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3336 for (uint32_t region = 0; region < regionCount; region++) {
3337 const auto &copy_region = pRegions[region];
3338 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003339 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003340 copy_region.imageOffset, copy_region.imageExtent);
3341 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003342 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003343 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003344 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003345 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003346 }
3347 }
3348 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06003349 ResourceAccessRange dst_range =
3350 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003351 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003352 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003353 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003354 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003355 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003356 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003357 }
3358 }
3359 if (skip) break;
3360 }
3361 return skip;
3362}
3363
Jeff Leger178b1e52020-10-05 12:22:23 -04003364bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3365 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3366 const VkBufferImageCopy *pRegions) const {
3367 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3368 COPY_COMMAND_VERSION_1);
3369}
3370
3371bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3372 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3373 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3374 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3375 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3376}
3377
3378template <typename BufferImageCopyRegionType>
3379void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3380 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3381 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003382 auto *cb_access_context = GetAccessContext(commandBuffer);
3383 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003384
3385 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3386 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3387
3388 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003389 auto *context = cb_access_context->GetCurrentAccessContext();
3390 assert(context);
3391
3392 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003393 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3394 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 -06003395 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003396
3397 for (uint32_t region = 0; region < regionCount; region++) {
3398 const auto &copy_region = pRegions[region];
3399 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003400 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003401 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003402 }
3403 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003404 ResourceAccessRange dst_range =
3405 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003406 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003407 }
3408 }
3409}
3410
Jeff Leger178b1e52020-10-05 12:22:23 -04003411void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3412 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3413 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3414 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3415}
3416
3417void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3418 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3419 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3420 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3421 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3422 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3423}
3424
3425template <typename RegionType>
3426bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3427 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3428 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003429 bool skip = false;
3430 const auto *cb_access_context = GetAccessContext(commandBuffer);
3431 assert(cb_access_context);
3432 if (!cb_access_context) return skip;
3433
3434 const auto *context = cb_access_context->GetCurrentAccessContext();
3435 assert(context);
3436 if (!context) return skip;
3437
3438 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3439 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3440
3441 for (uint32_t region = 0; region < regionCount; region++) {
3442 const auto &blit_region = pRegions[region];
3443 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003444 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3445 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3446 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3447 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3448 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3449 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3450 auto hazard =
3451 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003452 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003453 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003454 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003455 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003456 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003457 }
3458 }
3459
3460 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003461 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3462 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3463 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3464 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3465 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3466 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3467 auto hazard =
3468 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003469 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003470 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003471 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003472 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003473 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003474 }
3475 if (skip) break;
3476 }
3477 }
3478
3479 return skip;
3480}
3481
Jeff Leger178b1e52020-10-05 12:22:23 -04003482bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3483 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3484 const VkImageBlit *pRegions, VkFilter filter) const {
3485 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3486 "vkCmdBlitImage");
3487}
3488
3489bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3490 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3491 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3492 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3493 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3494}
3495
3496template <typename RegionType>
3497void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3498 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3499 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003500 auto *cb_access_context = GetAccessContext(commandBuffer);
3501 assert(cb_access_context);
3502 auto *context = cb_access_context->GetCurrentAccessContext();
3503 assert(context);
3504
3505 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003506 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003507
3508 for (uint32_t region = 0; region < regionCount; region++) {
3509 const auto &blit_region = pRegions[region];
3510 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003511 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3512 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3513 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3514 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3515 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3516 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3517 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003518 }
3519 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003520 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3521 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3522 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3523 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3524 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3525 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3526 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003527 }
3528 }
3529}
locke-lunarg36ba2592020-04-03 09:42:04 -06003530
Jeff Leger178b1e52020-10-05 12:22:23 -04003531void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3532 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3533 const VkImageBlit *pRegions, VkFilter filter) {
3534 auto *cb_access_context = GetAccessContext(commandBuffer);
3535 assert(cb_access_context);
3536 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3537 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3538 pRegions, filter);
3539 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3540}
3541
3542void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3543 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3544 auto *cb_access_context = GetAccessContext(commandBuffer);
3545 assert(cb_access_context);
3546 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3547 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3548 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3549 pBlitImageInfo->filter, tag);
3550}
3551
locke-lunarg61870c22020-06-09 14:51:50 -06003552bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
3553 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
3554 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003555 bool skip = false;
3556 if (drawCount == 0) return skip;
3557
3558 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3559 VkDeviceSize size = struct_size;
3560 if (drawCount == 1 || stride == size) {
3561 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003562 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003563 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3564 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003565 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003566 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003567 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003568 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003569 }
3570 } else {
3571 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003572 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003573 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3574 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003575 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003576 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3577 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3578 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003579 break;
3580 }
3581 }
3582 }
3583 return skip;
3584}
3585
locke-lunarg61870c22020-06-09 14:51:50 -06003586void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3587 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3588 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003589 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3590 VkDeviceSize size = struct_size;
3591 if (drawCount == 1 || stride == size) {
3592 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003593 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003594 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3595 } else {
3596 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003597 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003598 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3599 }
3600 }
3601}
3602
locke-lunarg61870c22020-06-09 14:51:50 -06003603bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3604 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003605 bool skip = false;
3606
3607 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003608 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003609 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3610 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003611 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003612 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003613 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003614 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003615 }
3616 return skip;
3617}
3618
locke-lunarg61870c22020-06-09 14:51:50 -06003619void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003620 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003621 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003622 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3623}
3624
locke-lunarg36ba2592020-04-03 09:42:04 -06003625bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003626 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003627 const auto *cb_access_context = GetAccessContext(commandBuffer);
3628 assert(cb_access_context);
3629 if (!cb_access_context) return skip;
3630
locke-lunarg61870c22020-06-09 14:51:50 -06003631 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003632 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003633}
3634
3635void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003636 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003637 auto *cb_access_context = GetAccessContext(commandBuffer);
3638 assert(cb_access_context);
3639 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003640
locke-lunarg61870c22020-06-09 14:51:50 -06003641 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003642}
locke-lunarge1a67022020-04-29 00:15:36 -06003643
3644bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003645 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003646 const auto *cb_access_context = GetAccessContext(commandBuffer);
3647 assert(cb_access_context);
3648 if (!cb_access_context) return skip;
3649
3650 const auto *context = cb_access_context->GetCurrentAccessContext();
3651 assert(context);
3652 if (!context) return skip;
3653
locke-lunarg61870c22020-06-09 14:51:50 -06003654 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3655 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3656 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003657 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003658}
3659
3660void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003661 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003662 auto *cb_access_context = GetAccessContext(commandBuffer);
3663 assert(cb_access_context);
3664 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3665 auto *context = cb_access_context->GetCurrentAccessContext();
3666 assert(context);
3667
locke-lunarg61870c22020-06-09 14:51:50 -06003668 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3669 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003670}
3671
3672bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3673 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003674 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003675 const auto *cb_access_context = GetAccessContext(commandBuffer);
3676 assert(cb_access_context);
3677 if (!cb_access_context) return skip;
3678
locke-lunarg61870c22020-06-09 14:51:50 -06003679 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3680 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3681 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003682 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003683}
3684
3685void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3686 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003687 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003688 auto *cb_access_context = GetAccessContext(commandBuffer);
3689 assert(cb_access_context);
3690 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003691
locke-lunarg61870c22020-06-09 14:51:50 -06003692 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3693 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3694 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003695}
3696
3697bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3698 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003699 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003700 const auto *cb_access_context = GetAccessContext(commandBuffer);
3701 assert(cb_access_context);
3702 if (!cb_access_context) return skip;
3703
locke-lunarg61870c22020-06-09 14:51:50 -06003704 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3705 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3706 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003707 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003708}
3709
3710void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3711 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003712 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003713 auto *cb_access_context = GetAccessContext(commandBuffer);
3714 assert(cb_access_context);
3715 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003716
locke-lunarg61870c22020-06-09 14:51:50 -06003717 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3718 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3719 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003720}
3721
3722bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3723 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003724 bool skip = false;
3725 if (drawCount == 0) return skip;
3726
locke-lunargff255f92020-05-13 18:53:52 -06003727 const auto *cb_access_context = GetAccessContext(commandBuffer);
3728 assert(cb_access_context);
3729 if (!cb_access_context) return skip;
3730
3731 const auto *context = cb_access_context->GetCurrentAccessContext();
3732 assert(context);
3733 if (!context) return skip;
3734
locke-lunarg61870c22020-06-09 14:51:50 -06003735 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3736 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3737 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3738 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003739
3740 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3741 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3742 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003743 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003744 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003745}
3746
3747void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3748 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003749 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003750 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003751 auto *cb_access_context = GetAccessContext(commandBuffer);
3752 assert(cb_access_context);
3753 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3754 auto *context = cb_access_context->GetCurrentAccessContext();
3755 assert(context);
3756
locke-lunarg61870c22020-06-09 14:51:50 -06003757 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3758 cb_access_context->RecordDrawSubpassAttachment(tag);
3759 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003760
3761 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3762 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3763 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003764 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003765}
3766
3767bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3768 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003769 bool skip = false;
3770 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003771 const auto *cb_access_context = GetAccessContext(commandBuffer);
3772 assert(cb_access_context);
3773 if (!cb_access_context) return skip;
3774
3775 const auto *context = cb_access_context->GetCurrentAccessContext();
3776 assert(context);
3777 if (!context) return skip;
3778
locke-lunarg61870c22020-06-09 14:51:50 -06003779 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3780 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3781 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3782 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003783
3784 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3785 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3786 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003787 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003788 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003789}
3790
3791void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3792 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003793 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003794 auto *cb_access_context = GetAccessContext(commandBuffer);
3795 assert(cb_access_context);
3796 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3797 auto *context = cb_access_context->GetCurrentAccessContext();
3798 assert(context);
3799
locke-lunarg61870c22020-06-09 14:51:50 -06003800 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3801 cb_access_context->RecordDrawSubpassAttachment(tag);
3802 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003803
3804 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3805 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3806 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003807 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003808}
3809
3810bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3811 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3812 uint32_t stride, const char *function) const {
3813 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003814 const auto *cb_access_context = GetAccessContext(commandBuffer);
3815 assert(cb_access_context);
3816 if (!cb_access_context) return skip;
3817
3818 const auto *context = cb_access_context->GetCurrentAccessContext();
3819 assert(context);
3820 if (!context) return skip;
3821
locke-lunarg61870c22020-06-09 14:51:50 -06003822 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3823 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3824 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3825 function);
3826 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003827
3828 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3829 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3830 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003831 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003832 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003833}
3834
3835bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3836 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3837 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003838 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3839 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003840}
3841
3842void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3843 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3844 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003845 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3846 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003847 auto *cb_access_context = GetAccessContext(commandBuffer);
3848 assert(cb_access_context);
3849 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3850 auto *context = cb_access_context->GetCurrentAccessContext();
3851 assert(context);
3852
locke-lunarg61870c22020-06-09 14:51:50 -06003853 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3854 cb_access_context->RecordDrawSubpassAttachment(tag);
3855 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3856 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003857
3858 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3859 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3860 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003861 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003862}
3863
3864bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3865 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3866 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003867 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3868 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003869}
3870
3871void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3872 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3873 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003874 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3875 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003876 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003877}
3878
3879bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3880 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3881 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003882 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3883 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003884}
3885
3886void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3887 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3888 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003889 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3890 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003891 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3892}
3893
3894bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3895 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3896 uint32_t stride, const char *function) const {
3897 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003898 const auto *cb_access_context = GetAccessContext(commandBuffer);
3899 assert(cb_access_context);
3900 if (!cb_access_context) return skip;
3901
3902 const auto *context = cb_access_context->GetCurrentAccessContext();
3903 assert(context);
3904 if (!context) return skip;
3905
locke-lunarg61870c22020-06-09 14:51:50 -06003906 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3907 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3908 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3909 stride, function);
3910 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003911
3912 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3913 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3914 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003915 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003916 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003917}
3918
3919bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3920 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3921 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003922 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3923 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003924}
3925
3926void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3927 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3928 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003929 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3930 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003931 auto *cb_access_context = GetAccessContext(commandBuffer);
3932 assert(cb_access_context);
3933 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3934 auto *context = cb_access_context->GetCurrentAccessContext();
3935 assert(context);
3936
locke-lunarg61870c22020-06-09 14:51:50 -06003937 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3938 cb_access_context->RecordDrawSubpassAttachment(tag);
3939 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3940 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003941
3942 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3943 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06003944 // We will update the index and vertex buffer in SubmitQueue in the future.
3945 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003946}
3947
3948bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3949 VkDeviceSize offset, VkBuffer countBuffer,
3950 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3951 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003952 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3953 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003954}
3955
3956void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3957 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3958 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003959 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3960 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003961 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3962}
3963
3964bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3965 VkDeviceSize offset, VkBuffer countBuffer,
3966 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3967 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003968 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3969 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003970}
3971
3972void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3973 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3974 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003975 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3976 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003977 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3978}
3979
3980bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3981 const VkClearColorValue *pColor, uint32_t rangeCount,
3982 const VkImageSubresourceRange *pRanges) const {
3983 bool skip = false;
3984 const auto *cb_access_context = GetAccessContext(commandBuffer);
3985 assert(cb_access_context);
3986 if (!cb_access_context) return skip;
3987
3988 const auto *context = cb_access_context->GetCurrentAccessContext();
3989 assert(context);
3990 if (!context) return skip;
3991
3992 const auto *image_state = Get<IMAGE_STATE>(image);
3993
3994 for (uint32_t index = 0; index < rangeCount; index++) {
3995 const auto &range = pRanges[index];
3996 if (image_state) {
3997 auto hazard =
3998 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3999 if (hazard.hazard) {
4000 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004001 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004002 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004003 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004004 }
4005 }
4006 }
4007 return skip;
4008}
4009
4010void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4011 const VkClearColorValue *pColor, uint32_t rangeCount,
4012 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004013 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004014 auto *cb_access_context = GetAccessContext(commandBuffer);
4015 assert(cb_access_context);
4016 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4017 auto *context = cb_access_context->GetCurrentAccessContext();
4018 assert(context);
4019
4020 const auto *image_state = Get<IMAGE_STATE>(image);
4021
4022 for (uint32_t index = 0; index < rangeCount; index++) {
4023 const auto &range = pRanges[index];
4024 if (image_state) {
4025 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4026 tag);
4027 }
4028 }
4029}
4030
4031bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4032 VkImageLayout imageLayout,
4033 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4034 const VkImageSubresourceRange *pRanges) const {
4035 bool skip = false;
4036 const auto *cb_access_context = GetAccessContext(commandBuffer);
4037 assert(cb_access_context);
4038 if (!cb_access_context) return skip;
4039
4040 const auto *context = cb_access_context->GetCurrentAccessContext();
4041 assert(context);
4042 if (!context) return skip;
4043
4044 const auto *image_state = Get<IMAGE_STATE>(image);
4045
4046 for (uint32_t index = 0; index < rangeCount; index++) {
4047 const auto &range = pRanges[index];
4048 if (image_state) {
4049 auto hazard =
4050 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4051 if (hazard.hazard) {
4052 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004053 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004054 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004055 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004056 }
4057 }
4058 }
4059 return skip;
4060}
4061
4062void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4063 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4064 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004065 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004066 auto *cb_access_context = GetAccessContext(commandBuffer);
4067 assert(cb_access_context);
4068 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4069 auto *context = cb_access_context->GetCurrentAccessContext();
4070 assert(context);
4071
4072 const auto *image_state = Get<IMAGE_STATE>(image);
4073
4074 for (uint32_t index = 0; index < rangeCount; index++) {
4075 const auto &range = pRanges[index];
4076 if (image_state) {
4077 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4078 tag);
4079 }
4080 }
4081}
4082
4083bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4084 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4085 VkDeviceSize dstOffset, VkDeviceSize stride,
4086 VkQueryResultFlags flags) const {
4087 bool skip = false;
4088 const auto *cb_access_context = GetAccessContext(commandBuffer);
4089 assert(cb_access_context);
4090 if (!cb_access_context) return skip;
4091
4092 const auto *context = cb_access_context->GetCurrentAccessContext();
4093 assert(context);
4094 if (!context) return skip;
4095
4096 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4097
4098 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004099 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004100 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4101 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004102 skip |=
4103 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4104 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4105 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004106 }
4107 }
locke-lunargff255f92020-05-13 18:53:52 -06004108
4109 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004110 return skip;
4111}
4112
4113void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4114 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4115 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004116 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4117 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004118 auto *cb_access_context = GetAccessContext(commandBuffer);
4119 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004120 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004121 auto *context = cb_access_context->GetCurrentAccessContext();
4122 assert(context);
4123
4124 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4125
4126 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004127 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004128 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4129 }
locke-lunargff255f92020-05-13 18:53:52 -06004130
4131 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004132}
4133
4134bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4135 VkDeviceSize size, uint32_t data) const {
4136 bool skip = false;
4137 const auto *cb_access_context = GetAccessContext(commandBuffer);
4138 assert(cb_access_context);
4139 if (!cb_access_context) return skip;
4140
4141 const auto *context = cb_access_context->GetCurrentAccessContext();
4142 assert(context);
4143 if (!context) return skip;
4144
4145 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4146
4147 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004148 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004149 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4150 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004151 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004152 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004153 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004154 }
4155 }
4156 return skip;
4157}
4158
4159void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4160 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004161 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004162 auto *cb_access_context = GetAccessContext(commandBuffer);
4163 assert(cb_access_context);
4164 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4165 auto *context = cb_access_context->GetCurrentAccessContext();
4166 assert(context);
4167
4168 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4169
4170 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004171 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004172 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4173 }
4174}
4175
4176bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4177 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4178 const VkImageResolve *pRegions) const {
4179 bool skip = false;
4180 const auto *cb_access_context = GetAccessContext(commandBuffer);
4181 assert(cb_access_context);
4182 if (!cb_access_context) return skip;
4183
4184 const auto *context = cb_access_context->GetCurrentAccessContext();
4185 assert(context);
4186 if (!context) return skip;
4187
4188 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4189 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4190
4191 for (uint32_t region = 0; region < regionCount; region++) {
4192 const auto &resolve_region = pRegions[region];
4193 if (src_image) {
4194 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4195 resolve_region.srcOffset, resolve_region.extent);
4196 if (hazard.hazard) {
4197 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004198 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004199 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004200 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004201 }
4202 }
4203
4204 if (dst_image) {
4205 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4206 resolve_region.dstOffset, resolve_region.extent);
4207 if (hazard.hazard) {
4208 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004209 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004210 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004211 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004212 }
4213 if (skip) break;
4214 }
4215 }
4216
4217 return skip;
4218}
4219
4220void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4221 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4222 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004223 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4224 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004225 auto *cb_access_context = GetAccessContext(commandBuffer);
4226 assert(cb_access_context);
4227 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4228 auto *context = cb_access_context->GetCurrentAccessContext();
4229 assert(context);
4230
4231 auto *src_image = Get<IMAGE_STATE>(srcImage);
4232 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4233
4234 for (uint32_t region = 0; region < regionCount; region++) {
4235 const auto &resolve_region = pRegions[region];
4236 if (src_image) {
4237 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4238 resolve_region.srcOffset, resolve_region.extent, tag);
4239 }
4240 if (dst_image) {
4241 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4242 resolve_region.dstOffset, resolve_region.extent, tag);
4243 }
4244 }
4245}
4246
Jeff Leger178b1e52020-10-05 12:22:23 -04004247bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4248 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4249 bool skip = false;
4250 const auto *cb_access_context = GetAccessContext(commandBuffer);
4251 assert(cb_access_context);
4252 if (!cb_access_context) return skip;
4253
4254 const auto *context = cb_access_context->GetCurrentAccessContext();
4255 assert(context);
4256 if (!context) return skip;
4257
4258 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4259 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4260
4261 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4262 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4263 if (src_image) {
4264 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4265 resolve_region.srcOffset, resolve_region.extent);
4266 if (hazard.hazard) {
4267 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4268 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4269 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
4270 region, string_UsageTag(hazard).c_str());
4271 }
4272 }
4273
4274 if (dst_image) {
4275 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4276 resolve_region.dstOffset, resolve_region.extent);
4277 if (hazard.hazard) {
4278 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4279 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4280 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
4281 region, string_UsageTag(hazard).c_str());
4282 }
4283 if (skip) break;
4284 }
4285 }
4286
4287 return skip;
4288}
4289
4290void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4291 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4292 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4293 auto *cb_access_context = GetAccessContext(commandBuffer);
4294 assert(cb_access_context);
4295 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4296 auto *context = cb_access_context->GetCurrentAccessContext();
4297 assert(context);
4298
4299 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4300 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4301
4302 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4303 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4304 if (src_image) {
4305 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4306 resolve_region.srcOffset, resolve_region.extent, tag);
4307 }
4308 if (dst_image) {
4309 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4310 resolve_region.dstOffset, resolve_region.extent, tag);
4311 }
4312 }
4313}
4314
locke-lunarge1a67022020-04-29 00:15:36 -06004315bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4316 VkDeviceSize dataSize, const void *pData) const {
4317 bool skip = false;
4318 const auto *cb_access_context = GetAccessContext(commandBuffer);
4319 assert(cb_access_context);
4320 if (!cb_access_context) return skip;
4321
4322 const auto *context = cb_access_context->GetCurrentAccessContext();
4323 assert(context);
4324 if (!context) return skip;
4325
4326 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4327
4328 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004329 // VK_WHOLE_SIZE not allowed
4330 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004331 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4332 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004333 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004334 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004335 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004336 }
4337 }
4338 return skip;
4339}
4340
4341void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4342 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004343 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004344 auto *cb_access_context = GetAccessContext(commandBuffer);
4345 assert(cb_access_context);
4346 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4347 auto *context = cb_access_context->GetCurrentAccessContext();
4348 assert(context);
4349
4350 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4351
4352 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004353 // VK_WHOLE_SIZE not allowed
4354 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004355 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4356 }
4357}
locke-lunargff255f92020-05-13 18:53:52 -06004358
4359bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4360 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4361 bool skip = false;
4362 const auto *cb_access_context = GetAccessContext(commandBuffer);
4363 assert(cb_access_context);
4364 if (!cb_access_context) return skip;
4365
4366 const auto *context = cb_access_context->GetCurrentAccessContext();
4367 assert(context);
4368 if (!context) return skip;
4369
4370 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4371
4372 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004373 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004374 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4375 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004376 skip |=
4377 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4378 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4379 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004380 }
4381 }
4382 return skip;
4383}
4384
4385void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4386 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004387 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004388 auto *cb_access_context = GetAccessContext(commandBuffer);
4389 assert(cb_access_context);
4390 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4391 auto *context = cb_access_context->GetCurrentAccessContext();
4392 assert(context);
4393
4394 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4395
4396 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004397 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004398 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4399 }
4400}