blob: b1f83023595efa3087cb3a17e6679da49836c866 [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.
2376 input_attachment_stage = other.input_attachment_stage;
2377 }
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) {
2391 input_attachment_stage = other.input_attachment_stage;
2392 }
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) {
2425 if (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) {
2426 input_attachment_stage = usage_stage;
2427 } else {
2428 input_attachment_stage = kInvalidAttachmentStage;
2429 }
2430 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002431 } else {
2432 // Assume write
2433 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002434 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002435 }
2436}
John Zulauf5f13a792020-03-10 07:31:21 -06002437
John Zulauf89311b42020-09-29 16:28:47 -06002438// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2439// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2440// We can overwrite them as *this* write is now after them.
2441//
2442// Note: intentionally ignore pending barriers and chains (i.e. don't apply or clear them), let ApplyPendingBarriers handle them.
2443void ResourceAccessState::SetWrite(SyncStageAccessFlagBits usage_bit, const ResourceUsageTag &tag) {
2444 last_read_count = 0;
2445 last_read_stages = 0;
2446 read_execution_barriers = 0;
2447 input_attachment_stage = kInvalidAttachmentStage; // Denotes no outstanding input attachment read after the last write.
2448
2449 write_barriers = 0;
2450 write_dependency_chain = 0;
2451 write_tag = tag;
2452 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002453}
2454
John Zulauf89311b42020-09-29 16:28:47 -06002455// Apply the memory barrier without updating the existing barriers. The execution barrier
2456// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2457// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2458// replace the current write barriers or add to them, so accumulate to pending as well.
2459void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2460 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2461 // applying the memory barriers
2462 if (InSourceScopeOrChain(barrier.src_exec_scope, barrier.src_access_scope)) {
2463 pending_write_barriers |= barrier.dst_access_scope;
2464 pending_write_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002465 }
John Zulauf89311b42020-09-29 16:28:47 -06002466 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2467 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06002468
John Zulauf89311b42020-09-29 16:28:47 -06002469 if (!pending_layout_transition) {
2470 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2471 // don't need to be tracked as we're just going to zero them.
John Zulaufa0a98292020-09-18 09:30:10 -06002472 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf89311b42020-09-29 16:28:47 -06002473 ReadState &access = last_reads[read_index];
2474 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2475 if (barrier.src_exec_scope & (access.stage | access.barriers)) {
2476 access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002477 }
2478 }
John Zulaufa0a98292020-09-18 09:30:10 -06002479 }
John Zulaufa0a98292020-09-18 09:30:10 -06002480}
2481
John Zulauf89311b42020-09-29 16:28:47 -06002482void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
2483 if (pending_layout_transition) {
2484 // Should only be true for valid tags -- barrier/subpass record not "resolve previous" traversals...
2485 assert(tag != kCurrentCommandTag);
2486 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
2487 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
2488 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06002489 }
John Zulauf89311b42020-09-29 16:28:47 -06002490
2491 // Apply the accumulate execution barriers (and thus update chaining information)
2492 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
2493 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2494 ReadState &access = last_reads[read_index];
2495 access.barriers |= access.pending_dep_chain;
2496 read_execution_barriers |= access.barriers;
2497 access.pending_dep_chain = 0;
2498 }
2499
2500 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
2501 write_dependency_chain |= pending_write_dep_chain;
2502 write_barriers |= pending_write_barriers;
2503 pending_write_dep_chain = 0;
2504 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002505}
2506
John Zulauf59e25072020-07-17 10:55:21 -06002507// This should be just Bits or Index, but we don't have an invalid state for Index
2508VkPipelineStageFlags ResourceAccessState::GetReadBarriers(SyncStageAccessFlags usage_bit) const {
2509 VkPipelineStageFlags barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06002510
2511 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2512 const auto &read_access = last_reads[read_index];
2513 if (read_access.access & usage_bit) {
2514 barriers = read_access.barriers;
2515 break;
John Zulauf59e25072020-07-17 10:55:21 -06002516 }
2517 }
John Zulauf4285ee92020-09-23 10:20:52 -06002518
John Zulauf59e25072020-07-17 10:55:21 -06002519 return barriers;
2520}
2521
John Zulauf4285ee92020-09-23 10:20:52 -06002522inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlagBits usage_stage, SyncStageAccessFlagBits usage) const {
2523 assert(IsRead(usage));
2524 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
2525 // * the previous reads are not hazards, and thus last_write must be visible and available to
2526 // any reads that happen after.
2527 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2528 // the current read will be also not be a hazard, thus reporting a hazard here adds no needed information.
2529 return (0 != last_write) && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
2530}
2531
2532bool ResourceAccessState::IsWARHazard(VkPipelineStageFlagBits usage_stage, SyncStageAccessFlagBits usage) const { return false; }
2533
2534VkPipelineStageFlags ResourceAccessState::GetOrderedStages(const SyncOrderingBarrier &ordering) const {
2535 // Whether the stage are in the ordering scope only matters if the current write is ordered
2536 VkPipelineStageFlags ordered_stages = last_read_stages & ordering.exec_scope;
2537 // Special input attachment handling as always (not encoded in exec_scop)
John Zulauf89311b42020-09-29 16:28:47 -06002538 const bool input_attachment_ordering = 0 != (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002539 if (input_attachment_ordering && (input_attachment_stage != kInvalidAttachmentStage)) {
2540 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
2541 ordered_stages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
2542 }
2543
2544 return ordered_stages;
2545}
2546
2547inline ResourceAccessState::ReadState *ResourceAccessState::GetReadStateForStage(VkPipelineStageFlagBits stage,
2548 uint32_t search_limit) {
2549 ReadState *read_state = nullptr;
2550 search_limit = std::min(search_limit, last_read_count);
2551 for (uint32_t i = 0; i < search_limit; i++) {
2552 if (last_reads[i].stage == stage) {
2553 read_state = &last_reads[i];
2554 break;
2555 }
2556 }
2557 return read_state;
2558}
2559
John Zulaufd1f85d42020-04-15 12:23:15 -06002560void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002561 auto *access_context = GetAccessContextNoInsert(command_buffer);
2562 if (access_context) {
2563 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002564 }
2565}
2566
John Zulaufd1f85d42020-04-15 12:23:15 -06002567void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2568 auto access_found = cb_access_state.find(command_buffer);
2569 if (access_found != cb_access_state.end()) {
2570 access_found->second->Reset();
2571 cb_access_state.erase(access_found);
2572 }
2573}
2574
John Zulauf89311b42020-09-29 16:28:47 -06002575void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2576 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags src_access_scope,
2577 SyncStageAccessFlags dst_access_scope, uint32_t memory_barrier_count,
2578 const VkMemoryBarrier *pMemoryBarriers, const ResourceUsageTag &tag) {
2579 ApplyBarrierOpsFunctor barriers_functor(true /* resolve */, std::min<uint32_t>(1, memory_barrier_count), tag);
2580 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
2581 const auto &barrier = pMemoryBarriers[barrier_index];
2582 SyncBarrier sync_barrier(src_exec_scope, SyncStageAccess::AccessScope(src_access_scope, barrier.srcAccessMask),
2583 dst_exec_scope, SyncStageAccess::AccessScope(dst_access_scope, barrier.dstAccessMask));
2584 barriers_functor.PushBack(sync_barrier, false);
2585 }
2586 if (0 == memory_barrier_count) {
2587 // If there are no global memory barriers, force an exec barrier
2588 barriers_functor.PushBack(SyncBarrier(src_exec_scope, 0, dst_exec_scope, 0), false);
2589 }
John Zulauf540266b2020-04-06 18:54:53 -06002590 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002591}
2592
John Zulauf540266b2020-04-06 18:54:53 -06002593void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002594 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2595 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002596 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002597 for (uint32_t index = 0; index < barrier_count; index++) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002598 auto barrier = barriers[index]; // barrier is a copy
John Zulauf9cb530d2019-09-30 14:14:10 -06002599 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2600 if (!buffer) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002601 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
2602 const ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002603 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2604 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06002605 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2606 const ApplyBarrierFunctor update_action(sync_barrier, false /* layout_transition */);
2607 context->UpdateResourceAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002608 }
2609}
2610
John Zulauf540266b2020-04-06 18:54:53 -06002611void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2612 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2613 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002614 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002615 for (uint32_t index = 0; index < barrier_count; index++) {
2616 const auto &barrier = barriers[index];
2617 const auto *image = Get<IMAGE_STATE>(barrier.image);
2618 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002619 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002620 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2621 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2622 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06002623 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2624 const ApplyBarrierFunctor barrier_action(sync_barrier, layout_transition);
2625 context->UpdateResourceAccess(*image, subresource_range, barrier_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002626 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002627}
2628
2629bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2630 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2631 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002632 const auto *cb_context = GetAccessContext(commandBuffer);
2633 assert(cb_context);
2634 if (!cb_context) return skip;
2635 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002636
John Zulauf3d84f1b2020-03-09 13:33:25 -06002637 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002638 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002639 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002640
2641 for (uint32_t region = 0; region < regionCount; region++) {
2642 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002643 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002644 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002645 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002646 if (hazard.hazard) {
2647 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002648 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002649 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002650 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002651 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002652 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002653 }
John Zulauf16adfc92020-04-08 10:28:33 -06002654 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002655 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06002656 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002657 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002658 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002659 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002660 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002661 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002662 }
2663 }
2664 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002665 }
2666 return skip;
2667}
2668
2669void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2670 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002671 auto *cb_context = GetAccessContext(commandBuffer);
2672 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002673 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002674 auto *context = cb_context->GetCurrentAccessContext();
2675
John Zulauf9cb530d2019-09-30 14:14:10 -06002676 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002677 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002678
2679 for (uint32_t region = 0; region < regionCount; region++) {
2680 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002681 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002682 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002683 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002684 }
John Zulauf16adfc92020-04-08 10:28:33 -06002685 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002686 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002687 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002688 }
2689 }
2690}
2691
Jeff Leger178b1e52020-10-05 12:22:23 -04002692bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
2693 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
2694 bool skip = false;
2695 const auto *cb_context = GetAccessContext(commandBuffer);
2696 assert(cb_context);
2697 if (!cb_context) return skip;
2698 const auto *context = cb_context->GetCurrentAccessContext();
2699
2700 // If we have no previous accesses, we have no hazards
2701 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2702 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2703
2704 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2705 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2706 if (src_buffer) {
2707 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2708 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
2709 if (hazard.hazard) {
2710 // TODO -- add tag information to log msg when useful.
2711 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
2712 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
2713 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
2714 region, string_UsageTag(hazard).c_str());
2715 }
2716 }
2717 if (dst_buffer && !skip) {
2718 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2719 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
2720 if (hazard.hazard) {
2721 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
2722 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
2723 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
2724 region, string_UsageTag(hazard).c_str());
2725 }
2726 }
2727 if (skip) break;
2728 }
2729 return skip;
2730}
2731
2732void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
2733 auto *cb_context = GetAccessContext(commandBuffer);
2734 assert(cb_context);
2735 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
2736 auto *context = cb_context->GetCurrentAccessContext();
2737
2738 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2739 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2740
2741 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2742 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2743 if (src_buffer) {
2744 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2745 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
2746 }
2747 if (dst_buffer) {
2748 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2749 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
2750 }
2751 }
2752}
2753
John Zulauf5c5e88d2019-12-26 11:22:02 -07002754bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2755 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2756 const VkImageCopy *pRegions) const {
2757 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002758 const auto *cb_access_context = GetAccessContext(commandBuffer);
2759 assert(cb_access_context);
2760 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002761
John Zulauf3d84f1b2020-03-09 13:33:25 -06002762 const auto *context = cb_access_context->GetCurrentAccessContext();
2763 assert(context);
2764 if (!context) return skip;
2765
2766 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2767 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002768 for (uint32_t region = 0; region < regionCount; region++) {
2769 const auto &copy_region = pRegions[region];
2770 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002771 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002772 copy_region.srcOffset, copy_region.extent);
2773 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002774 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002775 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002776 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002777 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002778 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002779 }
2780
2781 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002782 VkExtent3D dst_copy_extent =
2783 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002784 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002785 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002786 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002787 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002788 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002789 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002790 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002791 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002792 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002793 }
2794 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002795
John Zulauf5c5e88d2019-12-26 11:22:02 -07002796 return skip;
2797}
2798
2799void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2800 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2801 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002802 auto *cb_access_context = GetAccessContext(commandBuffer);
2803 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002804 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002805 auto *context = cb_access_context->GetCurrentAccessContext();
2806 assert(context);
2807
John Zulauf5c5e88d2019-12-26 11:22:02 -07002808 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002809 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002810
2811 for (uint32_t region = 0; region < regionCount; region++) {
2812 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002813 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002814 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2815 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002816 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002817 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002818 VkExtent3D dst_copy_extent =
2819 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002820 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2821 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002822 }
2823 }
2824}
2825
Jeff Leger178b1e52020-10-05 12:22:23 -04002826bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
2827 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
2828 bool skip = false;
2829 const auto *cb_access_context = GetAccessContext(commandBuffer);
2830 assert(cb_access_context);
2831 if (!cb_access_context) return skip;
2832
2833 const auto *context = cb_access_context->GetCurrentAccessContext();
2834 assert(context);
2835 if (!context) return skip;
2836
2837 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2838 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2839 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2840 const auto &copy_region = pCopyImageInfo->pRegions[region];
2841 if (src_image) {
2842 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
2843 copy_region.srcOffset, copy_region.extent);
2844 if (hazard.hazard) {
2845 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
2846 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
2847 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
2848 region, string_UsageTag(hazard).c_str());
2849 }
2850 }
2851
2852 if (dst_image) {
2853 VkExtent3D dst_copy_extent =
2854 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2855 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
2856 copy_region.dstOffset, dst_copy_extent);
2857 if (hazard.hazard) {
2858 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
2859 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
2860 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
2861 region, string_UsageTag(hazard).c_str());
2862 }
2863 if (skip) break;
2864 }
2865 }
2866
2867 return skip;
2868}
2869
2870void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
2871 auto *cb_access_context = GetAccessContext(commandBuffer);
2872 assert(cb_access_context);
2873 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
2874 auto *context = cb_access_context->GetCurrentAccessContext();
2875 assert(context);
2876
2877 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2878 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2879
2880 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2881 const auto &copy_region = pCopyImageInfo->pRegions[region];
2882 if (src_image) {
2883 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2884 copy_region.extent, tag);
2885 }
2886 if (dst_image) {
2887 VkExtent3D dst_copy_extent =
2888 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2889 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2890 dst_copy_extent, tag);
2891 }
2892 }
2893}
2894
John Zulauf9cb530d2019-09-30 14:14:10 -06002895bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2896 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2897 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2898 uint32_t bufferMemoryBarrierCount,
2899 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2900 uint32_t imageMemoryBarrierCount,
2901 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2902 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002903 const auto *cb_access_context = GetAccessContext(commandBuffer);
2904 assert(cb_access_context);
2905 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002906
John Zulauf3d84f1b2020-03-09 13:33:25 -06002907 const auto *context = cb_access_context->GetCurrentAccessContext();
2908 assert(context);
2909 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002910
John Zulauf3d84f1b2020-03-09 13:33:25 -06002911 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002912 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2913 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002914 // Validate Image Layout transitions
2915 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2916 const auto &barrier = pImageMemoryBarriers[index];
2917 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2918 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2919 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002920 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002921 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002922 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002923 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002924 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002925 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002926 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002927 }
2928 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002929
2930 return skip;
2931}
2932
2933void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2934 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2935 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2936 uint32_t bufferMemoryBarrierCount,
2937 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2938 uint32_t imageMemoryBarrierCount,
2939 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002940 auto *cb_access_context = GetAccessContext(commandBuffer);
2941 assert(cb_access_context);
2942 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002943 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002944 auto access_context = cb_access_context->GetCurrentAccessContext();
2945 assert(access_context);
2946 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06002947
John Zulauf3d84f1b2020-03-09 13:33:25 -06002948 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002949 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002950 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002951 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
2952 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2953 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf89311b42020-09-29 16:28:47 -06002954
2955 // These two apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
2956 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
2957 // of the barriers is maintained.
John Zulauf3d84f1b2020-03-09 13:33:25 -06002958 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
2959 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002960 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06002961 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002962
John Zulauf89311b42020-09-29 16:28:47 -06002963 // Apply the global barriers last as is it walks all memory, it can also clean up the "pending" state without requiring an
2964 // additional pass, updating the dependency chains *last* as it goes along.
2965 // This is needed to guarantee order independence of the three lists.
John Zulauf3d84f1b2020-03-09 13:33:25 -06002966 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf89311b42020-09-29 16:28:47 -06002967 pMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002968}
2969
2970void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
2971 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
2972 // The state tracker sets up the device state
2973 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
2974
John Zulauf5f13a792020-03-10 07:31:21 -06002975 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
2976 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06002977 // TODO: Find a good way to do this hooklessly.
2978 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
2979 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
2980 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
2981
John Zulaufd1f85d42020-04-15 12:23:15 -06002982 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2983 sync_device_state->ResetCommandBufferCallback(command_buffer);
2984 });
2985 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2986 sync_device_state->FreeCommandBufferCallback(command_buffer);
2987 });
John Zulauf9cb530d2019-09-30 14:14:10 -06002988}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002989
John Zulauf355e49b2020-04-24 15:11:15 -06002990bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2991 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
2992 bool skip = false;
2993 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
2994 auto cb_context = GetAccessContext(commandBuffer);
2995
2996 if (rp_state && cb_context) {
2997 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
2998 }
2999
3000 return skip;
3001}
3002
3003bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3004 VkSubpassContents contents) const {
3005 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3006 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3007 subpass_begin_info.contents = contents;
3008 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
3009 return skip;
3010}
3011
3012bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3013 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
3014 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3015 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
3016 return skip;
3017}
3018
3019bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3020 const VkRenderPassBeginInfo *pRenderPassBegin,
3021 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
3022 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3023 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
3024 return skip;
3025}
3026
John Zulauf3d84f1b2020-03-09 13:33:25 -06003027void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3028 VkResult result) {
3029 // The state tracker sets up the command buffer state
3030 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3031
3032 // Create/initialize the structure that trackers accesses at the command buffer scope.
3033 auto cb_access_context = GetAccessContext(commandBuffer);
3034 assert(cb_access_context);
3035 cb_access_context->Reset();
3036}
3037
3038void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06003039 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003040 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003041 if (cb_context) {
3042 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003043 }
3044}
3045
3046void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3047 VkSubpassContents contents) {
3048 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3049 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3050 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003051 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003052}
3053
3054void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3055 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3056 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003057 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003058}
3059
3060void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3061 const VkRenderPassBeginInfo *pRenderPassBegin,
3062 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3063 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003064 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
3065}
3066
3067bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
3068 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
3069 bool skip = false;
3070
3071 auto cb_context = GetAccessContext(commandBuffer);
3072 assert(cb_context);
3073 auto cb_state = cb_context->GetCommandBufferState();
3074 if (!cb_state) return skip;
3075
3076 auto rp_state = cb_state->activeRenderPass;
3077 if (!rp_state) return skip;
3078
3079 skip |= cb_context->ValidateNextSubpass(func_name);
3080
3081 return skip;
3082}
3083
3084bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3085 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
3086 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3087 subpass_begin_info.contents = contents;
3088 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
3089 return skip;
3090}
3091
3092bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
3093 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3094 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3095 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
3096 return skip;
3097}
3098
3099bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3100 const VkSubpassEndInfo *pSubpassEndInfo) const {
3101 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3102 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
3103 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003104}
3105
3106void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003107 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003108 auto cb_context = GetAccessContext(commandBuffer);
3109 assert(cb_context);
3110 auto cb_state = cb_context->GetCommandBufferState();
3111 if (!cb_state) return;
3112
3113 auto rp_state = cb_state->activeRenderPass;
3114 if (!rp_state) return;
3115
John Zulauf355e49b2020-04-24 15:11:15 -06003116 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003117}
3118
3119void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3120 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
3121 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3122 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003123 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003124}
3125
3126void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3127 const VkSubpassEndInfo *pSubpassEndInfo) {
3128 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003129 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003130}
3131
3132void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3133 const VkSubpassEndInfo *pSubpassEndInfo) {
3134 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003135 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003136}
3137
John Zulauf355e49b2020-04-24 15:11:15 -06003138bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
3139 const char *func_name) const {
3140 bool skip = false;
3141
3142 auto cb_context = GetAccessContext(commandBuffer);
3143 assert(cb_context);
3144 auto cb_state = cb_context->GetCommandBufferState();
3145 if (!cb_state) return skip;
3146
3147 auto rp_state = cb_state->activeRenderPass;
3148 if (!rp_state) return skip;
3149
3150 skip |= cb_context->ValidateEndRenderpass(func_name);
3151 return skip;
3152}
3153
3154bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3155 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
3156 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
3157 return skip;
3158}
3159
3160bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
3161 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3162 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
3163 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
3164 return skip;
3165}
3166
3167bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
3168 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3169 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
3170 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
3171 return skip;
3172}
3173
3174void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3175 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003176 // Resolve the all subpass contexts to the command buffer contexts
3177 auto cb_context = GetAccessContext(commandBuffer);
3178 assert(cb_context);
3179 auto cb_state = cb_context->GetCommandBufferState();
3180 if (!cb_state) return;
3181
locke-lunargaecf2152020-05-12 17:15:41 -06003182 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06003183 if (!rp_state) return;
3184
John Zulauf355e49b2020-04-24 15:11:15 -06003185 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06003186}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003187
John Zulauf33fc1d52020-07-17 11:01:10 -06003188// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3189// updates to a resource which do not conflict at the byte level.
3190// TODO: Revisit this rule to see if it needs to be tighter or looser
3191// TODO: Add programatic control over suppression heuristics
3192bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3193 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3194}
3195
John Zulauf3d84f1b2020-03-09 13:33:25 -06003196void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003197 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003198 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003199}
3200
3201void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003202 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003203 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003204}
3205
3206void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003207 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003208 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003209}
locke-lunarga19c71d2020-03-02 18:17:04 -07003210
Jeff Leger178b1e52020-10-05 12:22:23 -04003211template <typename BufferImageCopyRegionType>
3212bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3213 VkImageLayout dstImageLayout, uint32_t regionCount,
3214 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003215 bool skip = false;
3216 const auto *cb_access_context = GetAccessContext(commandBuffer);
3217 assert(cb_access_context);
3218 if (!cb_access_context) return skip;
3219
Jeff Leger178b1e52020-10-05 12:22:23 -04003220 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3221 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3222
locke-lunarga19c71d2020-03-02 18:17:04 -07003223 const auto *context = cb_access_context->GetCurrentAccessContext();
3224 assert(context);
3225 if (!context) return skip;
3226
3227 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003228 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3229
3230 for (uint32_t region = 0; region < regionCount; region++) {
3231 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003232 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003233 ResourceAccessRange src_range =
3234 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003235 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003236 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003237 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003238 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003239 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003240 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003241 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003242 }
3243 }
3244 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003245 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003246 copy_region.imageOffset, copy_region.imageExtent);
3247 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003248 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003249 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003250 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003251 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003252 }
3253 if (skip) break;
3254 }
3255 if (skip) break;
3256 }
3257 return skip;
3258}
3259
Jeff Leger178b1e52020-10-05 12:22:23 -04003260bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3261 VkImageLayout dstImageLayout, uint32_t regionCount,
3262 const VkBufferImageCopy *pRegions) const {
3263 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3264 COPY_COMMAND_VERSION_1);
3265}
3266
3267bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3268 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3269 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3270 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3271 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3272}
3273
3274template <typename BufferImageCopyRegionType>
3275void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3276 VkImageLayout dstImageLayout, uint32_t regionCount,
3277 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003278 auto *cb_access_context = GetAccessContext(commandBuffer);
3279 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003280
3281 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3282 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3283
3284 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003285 auto *context = cb_access_context->GetCurrentAccessContext();
3286 assert(context);
3287
3288 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003289 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003290
3291 for (uint32_t region = 0; region < regionCount; region++) {
3292 const auto &copy_region = pRegions[region];
3293 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003294 ResourceAccessRange src_range =
3295 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003296 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003297 }
3298 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003299 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003300 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003301 }
3302 }
3303}
3304
Jeff Leger178b1e52020-10-05 12:22:23 -04003305void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3306 VkImageLayout dstImageLayout, uint32_t regionCount,
3307 const VkBufferImageCopy *pRegions) {
3308 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3309 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3310}
3311
3312void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3313 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3314 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3315 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3316 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3317 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3318}
3319
3320template <typename BufferImageCopyRegionType>
3321bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3322 VkBuffer dstBuffer, uint32_t regionCount,
3323 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003324 bool skip = false;
3325 const auto *cb_access_context = GetAccessContext(commandBuffer);
3326 assert(cb_access_context);
3327 if (!cb_access_context) return skip;
3328
Jeff Leger178b1e52020-10-05 12:22:23 -04003329 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3330 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3331
locke-lunarga19c71d2020-03-02 18:17:04 -07003332 const auto *context = cb_access_context->GetCurrentAccessContext();
3333 assert(context);
3334 if (!context) return skip;
3335
3336 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3337 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3338 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3339 for (uint32_t region = 0; region < regionCount; region++) {
3340 const auto &copy_region = pRegions[region];
3341 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003342 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003343 copy_region.imageOffset, copy_region.imageExtent);
3344 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003345 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003346 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003347 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003348 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003349 }
3350 }
3351 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06003352 ResourceAccessRange dst_range =
3353 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003354 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003355 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003356 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003357 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003358 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003359 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003360 }
3361 }
3362 if (skip) break;
3363 }
3364 return skip;
3365}
3366
Jeff Leger178b1e52020-10-05 12:22:23 -04003367bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3368 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3369 const VkBufferImageCopy *pRegions) const {
3370 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3371 COPY_COMMAND_VERSION_1);
3372}
3373
3374bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3375 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3376 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3377 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3378 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3379}
3380
3381template <typename BufferImageCopyRegionType>
3382void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3383 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3384 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003385 auto *cb_access_context = GetAccessContext(commandBuffer);
3386 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003387
3388 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3389 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3390
3391 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003392 auto *context = cb_access_context->GetCurrentAccessContext();
3393 assert(context);
3394
3395 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003396 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3397 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 -06003398 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003399
3400 for (uint32_t region = 0; region < regionCount; region++) {
3401 const auto &copy_region = pRegions[region];
3402 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003403 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003404 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003405 }
3406 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003407 ResourceAccessRange dst_range =
3408 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003409 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003410 }
3411 }
3412}
3413
Jeff Leger178b1e52020-10-05 12:22:23 -04003414void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3415 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3416 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3417 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3418}
3419
3420void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3421 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3422 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3423 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3424 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3425 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3426}
3427
3428template <typename RegionType>
3429bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3430 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3431 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003432 bool skip = false;
3433 const auto *cb_access_context = GetAccessContext(commandBuffer);
3434 assert(cb_access_context);
3435 if (!cb_access_context) return skip;
3436
3437 const auto *context = cb_access_context->GetCurrentAccessContext();
3438 assert(context);
3439 if (!context) return skip;
3440
3441 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3442 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3443
3444 for (uint32_t region = 0; region < regionCount; region++) {
3445 const auto &blit_region = pRegions[region];
3446 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003447 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3448 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3449 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3450 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3451 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3452 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3453 auto hazard =
3454 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003455 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003456 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003457 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003458 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003459 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003460 }
3461 }
3462
3463 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003464 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3465 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3466 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3467 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3468 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3469 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3470 auto hazard =
3471 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003472 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003473 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003474 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003475 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003476 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003477 }
3478 if (skip) break;
3479 }
3480 }
3481
3482 return skip;
3483}
3484
Jeff Leger178b1e52020-10-05 12:22:23 -04003485bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3486 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3487 const VkImageBlit *pRegions, VkFilter filter) const {
3488 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3489 "vkCmdBlitImage");
3490}
3491
3492bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3493 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3494 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3495 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3496 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3497}
3498
3499template <typename RegionType>
3500void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3501 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3502 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003503 auto *cb_access_context = GetAccessContext(commandBuffer);
3504 assert(cb_access_context);
3505 auto *context = cb_access_context->GetCurrentAccessContext();
3506 assert(context);
3507
3508 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003509 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003510
3511 for (uint32_t region = 0; region < regionCount; region++) {
3512 const auto &blit_region = pRegions[region];
3513 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003514 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3515 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3516 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3517 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3518 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3519 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3520 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003521 }
3522 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003523 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3524 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3525 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3526 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3527 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3528 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3529 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003530 }
3531 }
3532}
locke-lunarg36ba2592020-04-03 09:42:04 -06003533
Jeff Leger178b1e52020-10-05 12:22:23 -04003534void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3535 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3536 const VkImageBlit *pRegions, VkFilter filter) {
3537 auto *cb_access_context = GetAccessContext(commandBuffer);
3538 assert(cb_access_context);
3539 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3540 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3541 pRegions, filter);
3542 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3543}
3544
3545void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3546 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3547 auto *cb_access_context = GetAccessContext(commandBuffer);
3548 assert(cb_access_context);
3549 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3550 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3551 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3552 pBlitImageInfo->filter, tag);
3553}
3554
locke-lunarg61870c22020-06-09 14:51:50 -06003555bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
3556 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
3557 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003558 bool skip = false;
3559 if (drawCount == 0) return skip;
3560
3561 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3562 VkDeviceSize size = struct_size;
3563 if (drawCount == 1 || stride == size) {
3564 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003565 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003566 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3567 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003568 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003569 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003570 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003571 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003572 }
3573 } else {
3574 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003575 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003576 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3577 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003578 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003579 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3580 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3581 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003582 break;
3583 }
3584 }
3585 }
3586 return skip;
3587}
3588
locke-lunarg61870c22020-06-09 14:51:50 -06003589void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3590 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3591 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003592 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3593 VkDeviceSize size = struct_size;
3594 if (drawCount == 1 || stride == size) {
3595 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003596 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003597 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3598 } else {
3599 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003600 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003601 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3602 }
3603 }
3604}
3605
locke-lunarg61870c22020-06-09 14:51:50 -06003606bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3607 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003608 bool skip = false;
3609
3610 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003611 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003612 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3613 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003614 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003615 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003616 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003617 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003618 }
3619 return skip;
3620}
3621
locke-lunarg61870c22020-06-09 14:51:50 -06003622void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003623 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003624 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003625 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3626}
3627
locke-lunarg36ba2592020-04-03 09:42:04 -06003628bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003629 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003630 const auto *cb_access_context = GetAccessContext(commandBuffer);
3631 assert(cb_access_context);
3632 if (!cb_access_context) return skip;
3633
locke-lunarg61870c22020-06-09 14:51:50 -06003634 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003635 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003636}
3637
3638void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003639 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003640 auto *cb_access_context = GetAccessContext(commandBuffer);
3641 assert(cb_access_context);
3642 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003643
locke-lunarg61870c22020-06-09 14:51:50 -06003644 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003645}
locke-lunarge1a67022020-04-29 00:15:36 -06003646
3647bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003648 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003649 const auto *cb_access_context = GetAccessContext(commandBuffer);
3650 assert(cb_access_context);
3651 if (!cb_access_context) return skip;
3652
3653 const auto *context = cb_access_context->GetCurrentAccessContext();
3654 assert(context);
3655 if (!context) return skip;
3656
locke-lunarg61870c22020-06-09 14:51:50 -06003657 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3658 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3659 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003660 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003661}
3662
3663void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003664 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003665 auto *cb_access_context = GetAccessContext(commandBuffer);
3666 assert(cb_access_context);
3667 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3668 auto *context = cb_access_context->GetCurrentAccessContext();
3669 assert(context);
3670
locke-lunarg61870c22020-06-09 14:51:50 -06003671 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3672 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003673}
3674
3675bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3676 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003677 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003678 const auto *cb_access_context = GetAccessContext(commandBuffer);
3679 assert(cb_access_context);
3680 if (!cb_access_context) return skip;
3681
locke-lunarg61870c22020-06-09 14:51:50 -06003682 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3683 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3684 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003685 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003686}
3687
3688void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3689 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003690 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003691 auto *cb_access_context = GetAccessContext(commandBuffer);
3692 assert(cb_access_context);
3693 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003694
locke-lunarg61870c22020-06-09 14:51:50 -06003695 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3696 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3697 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003698}
3699
3700bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3701 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003702 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003703 const auto *cb_access_context = GetAccessContext(commandBuffer);
3704 assert(cb_access_context);
3705 if (!cb_access_context) return skip;
3706
locke-lunarg61870c22020-06-09 14:51:50 -06003707 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3708 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3709 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003710 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003711}
3712
3713void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3714 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003715 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003716 auto *cb_access_context = GetAccessContext(commandBuffer);
3717 assert(cb_access_context);
3718 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003719
locke-lunarg61870c22020-06-09 14:51:50 -06003720 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3721 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3722 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003723}
3724
3725bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3726 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003727 bool skip = false;
3728 if (drawCount == 0) return skip;
3729
locke-lunargff255f92020-05-13 18:53:52 -06003730 const auto *cb_access_context = GetAccessContext(commandBuffer);
3731 assert(cb_access_context);
3732 if (!cb_access_context) return skip;
3733
3734 const auto *context = cb_access_context->GetCurrentAccessContext();
3735 assert(context);
3736 if (!context) return skip;
3737
locke-lunarg61870c22020-06-09 14:51:50 -06003738 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3739 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3740 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3741 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003742
3743 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3744 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3745 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003746 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003747 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003748}
3749
3750void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3751 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003752 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003753 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003754 auto *cb_access_context = GetAccessContext(commandBuffer);
3755 assert(cb_access_context);
3756 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3757 auto *context = cb_access_context->GetCurrentAccessContext();
3758 assert(context);
3759
locke-lunarg61870c22020-06-09 14:51:50 -06003760 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3761 cb_access_context->RecordDrawSubpassAttachment(tag);
3762 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003763
3764 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3765 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3766 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003767 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003768}
3769
3770bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3771 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003772 bool skip = false;
3773 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003774 const auto *cb_access_context = GetAccessContext(commandBuffer);
3775 assert(cb_access_context);
3776 if (!cb_access_context) return skip;
3777
3778 const auto *context = cb_access_context->GetCurrentAccessContext();
3779 assert(context);
3780 if (!context) return skip;
3781
locke-lunarg61870c22020-06-09 14:51:50 -06003782 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3783 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3784 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3785 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003786
3787 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3788 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3789 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003790 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003791 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003792}
3793
3794void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3795 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003796 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003797 auto *cb_access_context = GetAccessContext(commandBuffer);
3798 assert(cb_access_context);
3799 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3800 auto *context = cb_access_context->GetCurrentAccessContext();
3801 assert(context);
3802
locke-lunarg61870c22020-06-09 14:51:50 -06003803 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3804 cb_access_context->RecordDrawSubpassAttachment(tag);
3805 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003806
3807 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3808 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3809 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003810 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003811}
3812
3813bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3814 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3815 uint32_t stride, const char *function) const {
3816 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003817 const auto *cb_access_context = GetAccessContext(commandBuffer);
3818 assert(cb_access_context);
3819 if (!cb_access_context) return skip;
3820
3821 const auto *context = cb_access_context->GetCurrentAccessContext();
3822 assert(context);
3823 if (!context) return skip;
3824
locke-lunarg61870c22020-06-09 14:51:50 -06003825 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3826 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3827 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3828 function);
3829 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003830
3831 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3832 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3833 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003834 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003835 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003836}
3837
3838bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3839 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3840 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003841 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3842 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003843}
3844
3845void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3846 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3847 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003848 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3849 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003850 auto *cb_access_context = GetAccessContext(commandBuffer);
3851 assert(cb_access_context);
3852 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3853 auto *context = cb_access_context->GetCurrentAccessContext();
3854 assert(context);
3855
locke-lunarg61870c22020-06-09 14:51:50 -06003856 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3857 cb_access_context->RecordDrawSubpassAttachment(tag);
3858 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3859 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003860
3861 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3862 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3863 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003864 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003865}
3866
3867bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3868 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3869 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003870 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3871 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003872}
3873
3874void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3875 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3876 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003877 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3878 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003879 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003880}
3881
3882bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3883 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3884 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003885 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3886 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003887}
3888
3889void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3890 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3891 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003892 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3893 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003894 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3895}
3896
3897bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3898 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3899 uint32_t stride, const char *function) const {
3900 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003901 const auto *cb_access_context = GetAccessContext(commandBuffer);
3902 assert(cb_access_context);
3903 if (!cb_access_context) return skip;
3904
3905 const auto *context = cb_access_context->GetCurrentAccessContext();
3906 assert(context);
3907 if (!context) return skip;
3908
locke-lunarg61870c22020-06-09 14:51:50 -06003909 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3910 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3911 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3912 stride, function);
3913 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003914
3915 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3916 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3917 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003918 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003919 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003920}
3921
3922bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3923 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3924 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003925 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3926 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003927}
3928
3929void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3930 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3931 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003932 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3933 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003934 auto *cb_access_context = GetAccessContext(commandBuffer);
3935 assert(cb_access_context);
3936 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3937 auto *context = cb_access_context->GetCurrentAccessContext();
3938 assert(context);
3939
locke-lunarg61870c22020-06-09 14:51:50 -06003940 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3941 cb_access_context->RecordDrawSubpassAttachment(tag);
3942 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3943 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003944
3945 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3946 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06003947 // We will update the index and vertex buffer in SubmitQueue in the future.
3948 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003949}
3950
3951bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3952 VkDeviceSize offset, VkBuffer countBuffer,
3953 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3954 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003955 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3956 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003957}
3958
3959void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3960 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3961 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003962 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3963 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003964 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3965}
3966
3967bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3968 VkDeviceSize offset, VkBuffer countBuffer,
3969 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3970 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003971 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3972 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003973}
3974
3975void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3976 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3977 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003978 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3979 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003980 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3981}
3982
3983bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3984 const VkClearColorValue *pColor, uint32_t rangeCount,
3985 const VkImageSubresourceRange *pRanges) const {
3986 bool skip = false;
3987 const auto *cb_access_context = GetAccessContext(commandBuffer);
3988 assert(cb_access_context);
3989 if (!cb_access_context) return skip;
3990
3991 const auto *context = cb_access_context->GetCurrentAccessContext();
3992 assert(context);
3993 if (!context) return skip;
3994
3995 const auto *image_state = Get<IMAGE_STATE>(image);
3996
3997 for (uint32_t index = 0; index < rangeCount; index++) {
3998 const auto &range = pRanges[index];
3999 if (image_state) {
4000 auto hazard =
4001 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4002 if (hazard.hazard) {
4003 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004004 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004005 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004006 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004007 }
4008 }
4009 }
4010 return skip;
4011}
4012
4013void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4014 const VkClearColorValue *pColor, uint32_t rangeCount,
4015 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004016 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004017 auto *cb_access_context = GetAccessContext(commandBuffer);
4018 assert(cb_access_context);
4019 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4020 auto *context = cb_access_context->GetCurrentAccessContext();
4021 assert(context);
4022
4023 const auto *image_state = Get<IMAGE_STATE>(image);
4024
4025 for (uint32_t index = 0; index < rangeCount; index++) {
4026 const auto &range = pRanges[index];
4027 if (image_state) {
4028 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4029 tag);
4030 }
4031 }
4032}
4033
4034bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4035 VkImageLayout imageLayout,
4036 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4037 const VkImageSubresourceRange *pRanges) const {
4038 bool skip = false;
4039 const auto *cb_access_context = GetAccessContext(commandBuffer);
4040 assert(cb_access_context);
4041 if (!cb_access_context) return skip;
4042
4043 const auto *context = cb_access_context->GetCurrentAccessContext();
4044 assert(context);
4045 if (!context) return skip;
4046
4047 const auto *image_state = Get<IMAGE_STATE>(image);
4048
4049 for (uint32_t index = 0; index < rangeCount; index++) {
4050 const auto &range = pRanges[index];
4051 if (image_state) {
4052 auto hazard =
4053 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4054 if (hazard.hazard) {
4055 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004056 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004057 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004058 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004059 }
4060 }
4061 }
4062 return skip;
4063}
4064
4065void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4066 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4067 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004068 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004069 auto *cb_access_context = GetAccessContext(commandBuffer);
4070 assert(cb_access_context);
4071 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4072 auto *context = cb_access_context->GetCurrentAccessContext();
4073 assert(context);
4074
4075 const auto *image_state = Get<IMAGE_STATE>(image);
4076
4077 for (uint32_t index = 0; index < rangeCount; index++) {
4078 const auto &range = pRanges[index];
4079 if (image_state) {
4080 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4081 tag);
4082 }
4083 }
4084}
4085
4086bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4087 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4088 VkDeviceSize dstOffset, VkDeviceSize stride,
4089 VkQueryResultFlags flags) const {
4090 bool skip = false;
4091 const auto *cb_access_context = GetAccessContext(commandBuffer);
4092 assert(cb_access_context);
4093 if (!cb_access_context) return skip;
4094
4095 const auto *context = cb_access_context->GetCurrentAccessContext();
4096 assert(context);
4097 if (!context) return skip;
4098
4099 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4100
4101 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004102 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004103 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4104 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004105 skip |=
4106 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4107 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4108 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004109 }
4110 }
locke-lunargff255f92020-05-13 18:53:52 -06004111
4112 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004113 return skip;
4114}
4115
4116void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4117 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4118 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004119 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4120 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004121 auto *cb_access_context = GetAccessContext(commandBuffer);
4122 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004123 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004124 auto *context = cb_access_context->GetCurrentAccessContext();
4125 assert(context);
4126
4127 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4128
4129 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004130 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004131 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4132 }
locke-lunargff255f92020-05-13 18:53:52 -06004133
4134 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004135}
4136
4137bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4138 VkDeviceSize size, uint32_t data) const {
4139 bool skip = false;
4140 const auto *cb_access_context = GetAccessContext(commandBuffer);
4141 assert(cb_access_context);
4142 if (!cb_access_context) return skip;
4143
4144 const auto *context = cb_access_context->GetCurrentAccessContext();
4145 assert(context);
4146 if (!context) return skip;
4147
4148 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4149
4150 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004151 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004152 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4153 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004154 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004155 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004156 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004157 }
4158 }
4159 return skip;
4160}
4161
4162void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4163 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004164 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004165 auto *cb_access_context = GetAccessContext(commandBuffer);
4166 assert(cb_access_context);
4167 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4168 auto *context = cb_access_context->GetCurrentAccessContext();
4169 assert(context);
4170
4171 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4172
4173 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004174 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004175 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4176 }
4177}
4178
4179bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4180 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4181 const VkImageResolve *pRegions) const {
4182 bool skip = false;
4183 const auto *cb_access_context = GetAccessContext(commandBuffer);
4184 assert(cb_access_context);
4185 if (!cb_access_context) return skip;
4186
4187 const auto *context = cb_access_context->GetCurrentAccessContext();
4188 assert(context);
4189 if (!context) return skip;
4190
4191 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4192 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4193
4194 for (uint32_t region = 0; region < regionCount; region++) {
4195 const auto &resolve_region = pRegions[region];
4196 if (src_image) {
4197 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4198 resolve_region.srcOffset, resolve_region.extent);
4199 if (hazard.hazard) {
4200 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004201 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004202 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004203 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004204 }
4205 }
4206
4207 if (dst_image) {
4208 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4209 resolve_region.dstOffset, resolve_region.extent);
4210 if (hazard.hazard) {
4211 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004212 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004213 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004214 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004215 }
4216 if (skip) break;
4217 }
4218 }
4219
4220 return skip;
4221}
4222
4223void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4224 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4225 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004226 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4227 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004228 auto *cb_access_context = GetAccessContext(commandBuffer);
4229 assert(cb_access_context);
4230 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4231 auto *context = cb_access_context->GetCurrentAccessContext();
4232 assert(context);
4233
4234 auto *src_image = Get<IMAGE_STATE>(srcImage);
4235 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4236
4237 for (uint32_t region = 0; region < regionCount; region++) {
4238 const auto &resolve_region = pRegions[region];
4239 if (src_image) {
4240 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4241 resolve_region.srcOffset, resolve_region.extent, tag);
4242 }
4243 if (dst_image) {
4244 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4245 resolve_region.dstOffset, resolve_region.extent, tag);
4246 }
4247 }
4248}
4249
Jeff Leger178b1e52020-10-05 12:22:23 -04004250bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4251 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4252 bool skip = false;
4253 const auto *cb_access_context = GetAccessContext(commandBuffer);
4254 assert(cb_access_context);
4255 if (!cb_access_context) return skip;
4256
4257 const auto *context = cb_access_context->GetCurrentAccessContext();
4258 assert(context);
4259 if (!context) return skip;
4260
4261 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4262 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4263
4264 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4265 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4266 if (src_image) {
4267 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4268 resolve_region.srcOffset, resolve_region.extent);
4269 if (hazard.hazard) {
4270 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4271 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4272 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
4273 region, string_UsageTag(hazard).c_str());
4274 }
4275 }
4276
4277 if (dst_image) {
4278 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4279 resolve_region.dstOffset, resolve_region.extent);
4280 if (hazard.hazard) {
4281 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4282 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4283 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
4284 region, string_UsageTag(hazard).c_str());
4285 }
4286 if (skip) break;
4287 }
4288 }
4289
4290 return skip;
4291}
4292
4293void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4294 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4295 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4296 auto *cb_access_context = GetAccessContext(commandBuffer);
4297 assert(cb_access_context);
4298 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4299 auto *context = cb_access_context->GetCurrentAccessContext();
4300 assert(context);
4301
4302 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4303 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4304
4305 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4306 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4307 if (src_image) {
4308 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4309 resolve_region.srcOffset, resolve_region.extent, tag);
4310 }
4311 if (dst_image) {
4312 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4313 resolve_region.dstOffset, resolve_region.extent, tag);
4314 }
4315 }
4316}
4317
locke-lunarge1a67022020-04-29 00:15:36 -06004318bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4319 VkDeviceSize dataSize, const void *pData) const {
4320 bool skip = false;
4321 const auto *cb_access_context = GetAccessContext(commandBuffer);
4322 assert(cb_access_context);
4323 if (!cb_access_context) return skip;
4324
4325 const auto *context = cb_access_context->GetCurrentAccessContext();
4326 assert(context);
4327 if (!context) return skip;
4328
4329 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4330
4331 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004332 // VK_WHOLE_SIZE not allowed
4333 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004334 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4335 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004336 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004337 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004338 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004339 }
4340 }
4341 return skip;
4342}
4343
4344void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4345 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004346 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004347 auto *cb_access_context = GetAccessContext(commandBuffer);
4348 assert(cb_access_context);
4349 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4350 auto *context = cb_access_context->GetCurrentAccessContext();
4351 assert(context);
4352
4353 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4354
4355 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004356 // VK_WHOLE_SIZE not allowed
4357 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004358 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4359 }
4360}
locke-lunargff255f92020-05-13 18:53:52 -06004361
4362bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4363 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4364 bool skip = false;
4365 const auto *cb_access_context = GetAccessContext(commandBuffer);
4366 assert(cb_access_context);
4367 if (!cb_access_context) return skip;
4368
4369 const auto *context = cb_access_context->GetCurrentAccessContext();
4370 assert(context);
4371 if (!context) return skip;
4372
4373 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4374
4375 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004376 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004377 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4378 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004379 skip |=
4380 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4381 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4382 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004383 }
4384 }
4385 return skip;
4386}
4387
4388void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4389 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004390 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004391 auto *cb_access_context = GetAccessContext(commandBuffer);
4392 assert(cb_access_context);
4393 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4394 auto *context = cb_access_context->GetCurrentAccessContext();
4395 assert(context);
4396
4397 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4398
4399 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004400 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004401 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4402 }
4403}