blob: 3f206492fc313f4f7c901a177618496f6fa609d2 [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
1181struct ApplyMemoryAccessBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001182 using Iterator = ResourceAccessRangeMap::iterator;
1183 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001184
John Zulauf5c5e88d2019-12-26 11:22:02 -07001185 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001186 auto &access_state = pos->second;
John Zulaufa0a98292020-09-18 09:30:10 -06001187 access_state.ApplyMemoryAccessBarrier(false, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001188 return pos;
1189 }
1190
John Zulauf36bcf6a2020-02-03 15:12:52 -07001191 ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_,
1192 VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_)
1193 : src_exec_scope(src_exec_scope_),
1194 src_access_scope(src_access_scope_),
1195 dst_exec_scope(dst_exec_scope_),
1196 dst_access_scope(dst_access_scope_) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001197
John Zulauf36bcf6a2020-02-03 15:12:52 -07001198 VkPipelineStageFlags src_exec_scope;
1199 SyncStageAccessFlags src_access_scope;
1200 VkPipelineStageFlags dst_exec_scope;
1201 SyncStageAccessFlags dst_access_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001202};
1203
1204struct ApplyGlobalBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001205 using Iterator = ResourceAccessRangeMap::iterator;
1206 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001207
John Zulauf5c5e88d2019-12-26 11:22:02 -07001208 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001209 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001210 access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001211
1212 for (const auto &functor : barrier_functor) {
1213 functor(accesses, pos);
1214 }
1215 return pos;
1216 }
1217
John Zulauf36bcf6a2020-02-03 15:12:52 -07001218 ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope,
1219 SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses,
John Zulauf9cb530d2019-09-30 14:14:10 -06001220 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001221 : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001222 // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier...
1223 barrier_functor.reserve(memoryBarrierCount);
1224 for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) {
1225 const auto &barrier = pMemoryBarriers[barrier_index];
John Zulauf36bcf6a2020-02-03 15:12:52 -07001226 barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask),
1227 dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
John Zulauf9cb530d2019-09-30 14:14:10 -06001228 }
1229 }
1230
John Zulauf36bcf6a2020-02-03 15:12:52 -07001231 const VkPipelineStageFlags src_exec_scope;
1232 const VkPipelineStageFlags dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001233 std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor;
1234};
1235
John Zulauf355e49b2020-04-24 15:11:15 -06001236void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1237 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001238 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1239 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001240}
1241
John Zulauf16adfc92020-04-08 10:28:33 -06001242void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001243 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001244 if (!SimpleBinding(buffer)) return;
1245 const auto base_address = ResourceBaseAddress(buffer);
1246 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1247}
John Zulauf355e49b2020-04-24 15:11:15 -06001248
John Zulauf540266b2020-04-06 18:54:53 -06001249void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001250 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001251 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001252 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001253 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001254 const auto address_type = ImageAddressType(image);
1255 const auto base_address = ResourceBaseAddress(image);
1256 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001257 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001258 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001259 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001260}
John Zulauf7635de32020-05-29 17:14:15 -06001261void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1262 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1263 if (view != nullptr) {
1264 const IMAGE_STATE *image = view->image_state.get();
1265 if (image != nullptr) {
1266 auto *update_range = &view->normalized_subresource_range;
1267 VkImageSubresourceRange masked_range;
1268 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1269 masked_range = view->normalized_subresource_range;
1270 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1271 update_range = &masked_range;
1272 }
1273 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1274 }
1275 }
1276}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001277
John Zulauf355e49b2020-04-24 15:11:15 -06001278void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1279 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1280 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001281 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1282 subresource.layerCount};
1283 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1284}
1285
John Zulauf540266b2020-04-06 18:54:53 -06001286template <typename Action>
1287void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001288 if (!SimpleBinding(buffer)) return;
1289 const auto base_address = ResourceBaseAddress(buffer);
1290 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001291}
1292
1293template <typename Action>
1294void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1295 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001296 if (!SimpleBinding(image)) return;
1297 const auto address_type = ImageAddressType(image);
1298 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001299
locke-lunargae26eac2020-04-16 15:29:05 -06001300 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001301 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001302
John Zulauf16adfc92020-04-08 10:28:33 -06001303 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001304 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001305 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001306 }
1307}
1308
John Zulauf7635de32020-05-29 17:14:15 -06001309void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1310 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1311 const ResourceUsageTag &tag) {
1312 UpdateStateResolveAction update(*this, tag);
1313 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1314}
1315
John Zulaufaff20662020-06-01 14:07:58 -06001316void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1317 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1318 const ResourceUsageTag &tag) {
1319 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1320 VkExtent3D extent = CastTo3D(render_area.extent);
1321 VkOffset3D offset = CastTo3D(render_area.offset);
1322
1323 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1324 if (rp_state.attachment_last_subpass[i] == subpass) {
1325 if (attachment_views[i] == nullptr) continue; // UNUSED
1326 const auto &view = *attachment_views[i];
1327 const IMAGE_STATE *image = view.image_state.get();
1328 if (image == nullptr) continue;
1329
1330 const auto &ci = attachment_ci[i];
1331 const bool has_depth = FormatHasDepth(ci.format);
1332 const bool has_stencil = FormatHasStencil(ci.format);
1333 const bool is_color = !(has_depth || has_stencil);
1334 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1335
1336 if (is_color && store_op_stores) {
1337 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1338 offset, extent, tag);
1339 } else {
1340 auto update_range = view.normalized_subresource_range;
1341 if (has_depth && store_op_stores) {
1342 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1343 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1344 tag);
1345 }
1346 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1347 if (has_stencil && stencil_op_stores) {
1348 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1349 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1350 tag);
1351 }
1352 }
1353 }
1354 }
1355}
1356
John Zulauf540266b2020-04-06 18:54:53 -06001357template <typename Action>
1358void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1359 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001360 for (const auto address_type : kAddressTypes) {
1361 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001362 }
1363}
1364
1365void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001366 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1367 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -06001368 for (const auto address_type : kAddressTypes) {
John Zulaufa0a98292020-09-18 09:30:10 -06001369 context.ResolveAccessRange(address_type, full_range, context.GetDstExternalTrackBack().barriers,
John Zulauf355e49b2020-04-24 15:11:15 -06001370 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001371 }
1372 }
1373}
1374
John Zulauf355e49b2020-04-24 15:11:15 -06001375void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1376 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1377 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) {
1378 const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1379 UpdateMemoryAccess(image, subresource_range, barrier_action);
1380}
1381
John Zulauf7635de32020-05-29 17:14:15 -06001382// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001383void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1384 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1385 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range,
1386 bool layout_transition, const ResourceUsageTag &tag) {
1387 if (layout_transition) {
1388 UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent,
1389 tag);
1390 ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope,
1391 subresource_range);
John Zulaufc9201222020-05-13 15:13:03 -06001392 } else {
1393 ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range);
John Zulauf355e49b2020-04-24 15:11:15 -06001394 }
John Zulauf355e49b2020-04-24 15:11:15 -06001395}
1396
John Zulauf7635de32020-05-29 17:14:15 -06001397// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001398void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier,
1399 const VkImageSubresourceRange &subresource_range, bool layout_transition,
1400 const ResourceUsageTag &tag) {
1401 ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope,
1402 subresource_range, layout_transition, tag);
1403}
1404
1405// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001406HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001407 if (!attach_view) return HazardResult();
1408 const auto image_state = attach_view->image_state.get();
1409 if (!image_state) return HazardResult();
1410
John Zulauf355e49b2020-04-24 15:11:15 -06001411 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001412 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001413
1414 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001415 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1416 const auto merged_barrier = MergeBarriers(track_back.barriers);
1417 HazardResult hazard =
1418 track_back.context->DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
1419 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001420 if (!hazard.hazard) {
1421 // The Async hazard check is against the current context's async set.
John Zulaufa0a98292020-09-18 09:30:10 -06001422 hazard = DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001423 attach_view->normalized_subresource_range, kDetectAsync);
1424 }
John Zulaufa0a98292020-09-18 09:30:10 -06001425
John Zulauf355e49b2020-04-24 15:11:15 -06001426 return hazard;
1427}
1428
1429// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1430bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1431
1432 const VkRenderPassBeginInfo *pRenderPassBegin,
1433 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1434 const char *func_name) const {
1435 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1436 bool skip = false;
1437 uint32_t subpass = 0;
1438 const auto &transitions = rp_state.subpass_transitions[subpass];
1439 if (transitions.size()) {
1440 const std::vector<AccessContext> empty_context_vector;
1441 // Create context we can use to validate against...
1442 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1443 const_cast<AccessContext *>(&cb_access_context_));
1444
1445 assert(pRenderPassBegin);
1446 if (nullptr == pRenderPassBegin) return skip;
1447
1448 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1449 assert(fb_state);
1450 if (nullptr == fb_state) return skip;
1451
1452 // Create a limited array of views (which we'll need to toss
1453 std::vector<const IMAGE_VIEW_STATE *> views;
1454 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
1455 const auto attachment_count = count_attachment.first;
1456 const auto *attachments = count_attachment.second;
1457 views.resize(attachment_count, nullptr);
1458 for (const auto &transition : transitions) {
1459 assert(transition.attachment < attachment_count);
1460 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
1461 }
1462
John Zulauf7635de32020-05-29 17:14:15 -06001463 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
1464 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001465 }
1466 return skip;
1467}
1468
locke-lunarg61870c22020-06-09 14:51:50 -06001469bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1470 const char *func_name) const {
1471 bool skip = false;
1472 const PIPELINE_STATE *pPipe = nullptr;
1473 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1474 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1475 if (!pPipe || !per_sets) {
1476 return skip;
1477 }
1478
1479 using DescriptorClass = cvdescriptorset::DescriptorClass;
1480 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1481 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1482 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1483 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1484
1485 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarg37047832020-06-12 13:44:45 -06001486 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001487 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1488 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001489 for (const auto &set_binding : stage_state.descriptor_uses) {
1490 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1491 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1492 set_binding.first.second);
1493 const auto descriptor_type = binding_it.GetType();
1494 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1495 auto array_idx = 0;
1496
1497 if (binding_it.IsVariableDescriptorCount()) {
1498 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1499 }
1500 SyncStageAccessIndex sync_index =
1501 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1502
1503 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1504 uint32_t index = i - index_range.start;
1505 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1506 switch (descriptor->GetClass()) {
1507 case DescriptorClass::ImageSampler:
1508 case DescriptorClass::Image: {
1509 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001510 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001511 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001512 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1513 img_view_state = image_sampler_descriptor->GetImageViewState();
1514 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001515 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001516 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1517 img_view_state = image_descriptor->GetImageViewState();
1518 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001519 }
1520 if (!img_view_state) continue;
1521 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1522 VkExtent3D extent = {};
1523 VkOffset3D offset = {};
1524 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1525 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1526 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1527 } else {
1528 extent = img_state->createInfo.extent;
1529 }
John Zulauf361fb532020-07-22 10:45:39 -06001530 HazardResult hazard;
1531 const auto &subresource_range = img_view_state->normalized_subresource_range;
1532 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1533 // Input attachments are subject to raster ordering rules
1534 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
1535 kAttachmentRasterOrder, offset, extent);
1536 } else {
1537 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1538 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001539 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001540 skip |= sync_state_->LogError(
1541 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001542 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1543 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001544 func_name, string_SyncHazard(hazard.hazard),
1545 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1546 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1547 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001548 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1549 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1550 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001551 }
1552 break;
1553 }
1554 case DescriptorClass::TexelBuffer: {
1555 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1556 if (!buf_view_state) continue;
1557 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001558 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001559 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001560 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001561 skip |= sync_state_->LogError(
1562 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001563 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1564 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001565 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1566 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1567 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001568 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1569 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1570 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001571 }
1572 break;
1573 }
1574 case DescriptorClass::GeneralBuffer: {
1575 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1576 auto buf_state = buffer_descriptor->GetBufferState();
1577 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001578 const ResourceAccessRange range =
1579 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001580 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001581 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001582 skip |= sync_state_->LogError(
1583 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001584 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1585 func_name, string_SyncHazard(hazard.hazard),
1586 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001587 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1588 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001589 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1590 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1591 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001592 }
1593 break;
1594 }
1595 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1596 default:
1597 break;
1598 }
1599 }
1600 }
1601 }
1602 return skip;
1603}
1604
1605void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1606 const ResourceUsageTag &tag) {
1607 const PIPELINE_STATE *pPipe = nullptr;
1608 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1609 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1610 if (!pPipe || !per_sets) {
1611 return;
1612 }
1613
1614 using DescriptorClass = cvdescriptorset::DescriptorClass;
1615 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1616 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1617 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1618 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1619
1620 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001621 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
1622 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1623 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001624 for (const auto &set_binding : stage_state.descriptor_uses) {
1625 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1626 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1627 set_binding.first.second);
1628 const auto descriptor_type = binding_it.GetType();
1629 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1630 auto array_idx = 0;
1631
1632 if (binding_it.IsVariableDescriptorCount()) {
1633 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1634 }
1635 SyncStageAccessIndex sync_index =
1636 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1637
1638 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1639 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1640 switch (descriptor->GetClass()) {
1641 case DescriptorClass::ImageSampler:
1642 case DescriptorClass::Image: {
1643 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1644 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1645 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1646 } else {
1647 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1648 }
1649 if (!img_view_state) continue;
1650 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1651 VkExtent3D extent = {};
1652 VkOffset3D offset = {};
1653 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1654 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1655 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1656 } else {
1657 extent = img_state->createInfo.extent;
1658 }
1659 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1660 offset, extent, tag);
1661 break;
1662 }
1663 case DescriptorClass::TexelBuffer: {
1664 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1665 if (!buf_view_state) continue;
1666 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001667 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001668 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1669 break;
1670 }
1671 case DescriptorClass::GeneralBuffer: {
1672 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1673 auto buf_state = buffer_descriptor->GetBufferState();
1674 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001675 const ResourceAccessRange range =
1676 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001677 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1678 break;
1679 }
1680 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1681 default:
1682 break;
1683 }
1684 }
1685 }
1686 }
1687}
1688
1689bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1690 bool skip = false;
1691 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1692 if (!pPipe) {
1693 return skip;
1694 }
1695
1696 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1697 const auto &binding_buffers_size = binding_buffers.size();
1698 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1699
1700 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1701 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1702 if (binding_description.binding < binding_buffers_size) {
1703 const auto &binding_buffer = binding_buffers[binding_description.binding];
1704 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1705
1706 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06001707 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1708 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001709 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1710 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001711 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001712 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 -06001713 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001714 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001715 }
1716 }
1717 }
1718 return skip;
1719}
1720
1721void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
1722 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1723 if (!pPipe) {
1724 return;
1725 }
1726 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1727 const auto &binding_buffers_size = binding_buffers.size();
1728 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1729
1730 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1731 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1732 if (binding_description.binding < binding_buffers_size) {
1733 const auto &binding_buffer = binding_buffers[binding_description.binding];
1734 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1735
1736 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06001737 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1738 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001739 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1740 }
1741 }
1742}
1743
1744bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1745 bool skip = false;
1746 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
1747
1748 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1749 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001750 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1751 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001752 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1753 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001754 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001755 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 -06001756 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001757 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001758 }
1759
1760 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1761 // We will detect more accurate range in the future.
1762 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1763 return skip;
1764}
1765
1766void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
1767 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return;
1768
1769 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1770 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001771 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1772 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001773 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1774
1775 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1776 // We will detect more accurate range in the future.
1777 RecordDrawVertex(UINT32_MAX, 0, tag);
1778}
1779
1780bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06001781 bool skip = false;
1782 if (!current_renderpass_context_) return skip;
1783 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1784 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1785 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001786}
1787
1788void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
locke-lunarg7077d502020-06-18 21:37:26 -06001789 if (current_renderpass_context_)
1790 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1791 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001792}
1793
John Zulauf355e49b2020-04-24 15:11:15 -06001794bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001795 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001796 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06001797 skip |=
1798 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001799
1800 return skip;
1801}
1802
1803bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1804 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001805 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001806 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001807 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06001808 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1809 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001810
1811 return skip;
1812}
1813
1814void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1815 assert(sync_state_);
1816 if (!cb_state_) return;
1817
1818 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06001819 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06001820 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06001821 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001822 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001823}
1824
John Zulauf355e49b2020-04-24 15:11:15 -06001825void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001826 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001827 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001828 current_context_ = &current_renderpass_context_->CurrentContext();
1829}
1830
John Zulauf355e49b2020-04-24 15:11:15 -06001831void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001832 assert(current_renderpass_context_);
1833 if (!current_renderpass_context_) return;
1834
John Zulauf1a224292020-06-30 14:52:13 -06001835 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001836 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001837 current_renderpass_context_ = nullptr;
1838}
1839
locke-lunarg61870c22020-06-09 14:51:50 -06001840bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1841 const VkRect2D &render_area, const char *func_name) const {
1842 bool skip = false;
locke-lunarg96dc9632020-06-10 17:22:18 -06001843 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001844 if (!pPipe ||
1845 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001846 return skip;
1847 }
1848 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001849 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1850 VkExtent3D extent = CastTo3D(render_area.extent);
1851 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001852
John Zulauf1a224292020-06-30 14:52:13 -06001853 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001854 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001855 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1856 for (const auto location : list) {
1857 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1858 continue;
1859 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001860 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1861 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06001862 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001863 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001864 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001865 func_name, string_SyncHazard(hazard.hazard),
1866 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1867 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001868 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001869 }
1870 }
1871 }
locke-lunarg37047832020-06-12 13:44:45 -06001872
1873 // PHASE1 TODO: Add layout based read/vs. write selection.
1874 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1875 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1876 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001877 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001878 bool depth_write = false, stencil_write = false;
1879
1880 // PHASE1 TODO: These validation should be in core_checks.
1881 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1882 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1883 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1884 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1885 depth_write = true;
1886 }
1887 // PHASE1 TODO: It needs to check if stencil is writable.
1888 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1889 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1890 // PHASE1 TODO: These validation should be in core_checks.
1891 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1892 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1893 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1894 stencil_write = true;
1895 }
1896
1897 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1898 if (depth_write) {
1899 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001900 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1901 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001902 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001903 skip |= sync_state.LogError(
1904 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001905 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001906 func_name, string_SyncHazard(hazard.hazard),
1907 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1908 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001909 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001910 }
1911 }
1912 if (stencil_write) {
1913 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001914 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1915 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001916 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001917 skip |= sync_state.LogError(
1918 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001919 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001920 func_name, string_SyncHazard(hazard.hazard),
1921 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1922 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001923 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001924 }
locke-lunarg61870c22020-06-09 14:51:50 -06001925 }
1926 }
1927 return skip;
1928}
1929
locke-lunarg96dc9632020-06-10 17:22:18 -06001930void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1931 const ResourceUsageTag &tag) {
1932 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001933 if (!pPipe ||
1934 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001935 return;
1936 }
1937 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001938 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1939 VkExtent3D extent = CastTo3D(render_area.extent);
1940 VkOffset3D offset = CastTo3D(render_area.offset);
1941
John Zulauf1a224292020-06-30 14:52:13 -06001942 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001943 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001944 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1945 for (const auto location : list) {
1946 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1947 continue;
1948 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001949 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
1950 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001951 }
1952 }
locke-lunarg37047832020-06-12 13:44:45 -06001953
1954 // PHASE1 TODO: Add layout based read/vs. write selection.
1955 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1956 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1957 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001958 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001959 bool depth_write = false, stencil_write = false;
1960
1961 // PHASE1 TODO: These validation should be in core_checks.
1962 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1963 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1964 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1965 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1966 depth_write = true;
1967 }
1968 // PHASE1 TODO: It needs to check if stencil is writable.
1969 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1970 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1971 // PHASE1 TODO: These validation should be in core_checks.
1972 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1973 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1974 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1975 stencil_write = true;
1976 }
1977
1978 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1979 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001980 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1981 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001982 }
1983 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001984 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1985 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001986 }
locke-lunarg61870c22020-06-09 14:51:50 -06001987 }
1988}
1989
John Zulauf1507ee42020-05-18 11:33:09 -06001990bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
1991 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001992 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001993 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06001994 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1995 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001996 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1997 func_name);
1998
John Zulauf355e49b2020-04-24 15:11:15 -06001999 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002000 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06002001 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
2002 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
2003 return skip;
2004}
2005bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
2006 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002007 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002008 bool skip = false;
2009 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2010 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002011 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2012 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06002013 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002014 return skip;
2015}
2016
John Zulauf7635de32020-05-29 17:14:15 -06002017AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2018 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2019}
2020
2021bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2022 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002023 bool skip = false;
2024
John Zulauf7635de32020-05-29 17:14:15 -06002025 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2026 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2027 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2028 // to apply and only copy then, if this proves a hot spot.
2029 std::unique_ptr<AccessContext> proxy_for_current;
2030
John Zulauf355e49b2020-04-24 15:11:15 -06002031 // Validate the "finalLayout" transitions to external
2032 // Get them from where there we're hidding in the extra entry.
2033 const auto &final_transitions = rp_state_->subpass_transitions.back();
2034 for (const auto &transition : final_transitions) {
2035 const auto &attach_view = attachment_views_[transition.attachment];
2036 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2037 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002038 auto *context = trackback.context;
2039
2040 if (transition.prev_pass == current_subpass_) {
2041 if (!proxy_for_current) {
2042 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2043 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2044 }
2045 context = proxy_for_current.get();
2046 }
2047
John Zulaufa0a98292020-09-18 09:30:10 -06002048 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2049 const auto merged_barrier = MergeBarriers(trackback.barriers);
2050 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope,
2051 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2052 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002053 if (hazard.hazard) {
2054 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2055 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf389c34b2020-07-28 11:19:35 -06002056 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002057 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf389c34b2020-07-28 11:19:35 -06002058 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf37ceaed2020-07-03 16:18:15 -06002059 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002060 }
2061 }
2062 return skip;
2063}
2064
2065void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2066 // Add layout transitions...
2067 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
2068 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -06002069 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -06002070 for (const auto &transition : transitions) {
2071 const auto attachment_view = attachment_views_[transition.attachment];
2072 if (!attachment_view) continue;
2073 const auto image = attachment_view->image_state.get();
2074 if (!image) continue;
2075
John Zulaufa0a98292020-09-18 09:30:10 -06002076 const auto *trackback = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
2077 assert(trackback);
2078 const auto merged_barrier = MergeBarriers(trackback->barriers);
John Zulaufc9201222020-05-13 15:13:03 -06002079 auto insert_pair = view_seen.insert(attachment_view);
2080 if (insert_pair.second) {
2081 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
John Zulaufa0a98292020-09-18 09:30:10 -06002082 subpass_context.ApplyImageBarrier(*image, merged_barrier, attachment_view->normalized_subresource_range, true, tag);
John Zulaufc9201222020-05-13 15:13:03 -06002083
2084 } else {
2085 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
2086 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
John Zulaufa0a98292020-09-18 09:30:10 -06002087 auto barrier_to_transition = merged_barrier;
John Zulaufc9201222020-05-13 15:13:03 -06002088 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
John Zulaufa0a98292020-09-18 09:30:10 -06002089 subpass_context.ApplyImageBarrier(*image, barrier_to_transition, attachment_view->normalized_subresource_range, false,
2090 tag);
John Zulaufc9201222020-05-13 15:13:03 -06002091 }
John Zulauf355e49b2020-04-24 15:11:15 -06002092 }
2093}
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.
2170 const auto &final_transitions = rp_state_->subpass_transitions.back();
2171 for (const auto &transition : final_transitions) {
2172 const auto &attachment = attachment_views_[transition.attachment];
2173 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002174 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufa0a98292020-09-18 09:30:10 -06002175 // Since this is a layout transition vs. a subpass, we can safely merge the barriers, as there are no
2176 // chaining effects after the layout transition (write operation) nukes the prior depenency chains
2177 external_context->ApplyImageBarrier(*attachment->image_state, MergeBarriers(last_trackback.barriers),
John Zulauf1a224292020-06-30 14:52:13 -06002178 attachment->normalized_subresource_range, true, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002179 }
2180}
2181
John Zulauf3d84f1b2020-03-09 13:33:25 -06002182SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2183 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2184 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2185 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2186 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2187 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2188 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2189}
2190
John Zulaufa0a98292020-09-18 09:30:10 -06002191void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers) {
2192 for (const auto &barrier : barriers) {
2193 ApplyMemoryAccessBarrier(true, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope,
2194 barrier.dst_access_scope);
2195 }
2196 ApplyExecutionBarriers(barriers);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002197}
2198
John Zulauf9cb530d2019-09-30 14:14:10 -06002199HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2200 HazardResult hazard;
2201 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002202 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002203 if (IsRead(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002204 // Only check reads vs. last_write if it doesn't happen-after any other read because either:
2205 // * the previous reads are not hazards, and thus last_write must be visible and available to
2206 // any reads that happen after.
2207 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2208 // the current read will be also not be a hazard, thus reporting a hazard here adds no needed information.
2209 if (((usage_stage & read_execution_barriers) == 0) && last_write && IsWriteHazard(usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002210 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002211 }
2212 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002213 // Write operation:
2214 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2215 // If reads exists -- test only against them because either:
2216 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2217 // * 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
2218 // the current write happens after the reads, so just test the write against the reades
2219 // Otherwise test against last_write
2220 //
2221 // Look for casus belli for WAR
2222 if (last_read_count) {
2223 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2224 const auto &read_access = last_reads[read_index];
2225 if (IsReadHazard(usage_stage, read_access)) {
2226 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2227 break;
2228 }
2229 }
2230 } else if (input_attachment_barriers != kNoAttachmentRead) {
John Zulaufd14743a2020-07-03 09:42:39 -06002231 if (IsReadHazard(usage_stage, input_attachment_barriers)) {
John Zulauf59e25072020-07-17 10:55:21 -06002232 hazard.Set(this, usage_index, WRITE_AFTER_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002233 }
John Zulauf361fb532020-07-22 10:45:39 -06002234 } else if (last_write && IsWriteHazard(usage)) {
2235 // Write-After-Write check -- if we have a previous write to test against
2236 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002237 }
2238 }
2239 return hazard;
2240}
2241
John Zulauf69133422020-05-20 14:55:53 -06002242HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2243 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2244 HazardResult hazard;
2245 const auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002246 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf69133422020-05-20 14:55:53 -06002247 const bool write_is_ordered = (last_write & ordering.access_scope) == last_write; // Is true if no write, and that's good.
2248 if (IsRead(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002249 if (!write_is_ordered) {
2250 // Only check for RAW if the write is unordered, and there are no reads ordered before the current read since last_write
2251 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2252 // We need to assemble the effect read_execution barriers from the union of the state barriers and the ordering rules
2253 // Check to see if there are any reads ordered before usage, including ordering rules and barriers.
2254 bool ordered_read = 0 != ((last_read_stages & ordering.exec_scope) | (read_execution_barriers & usage_stage));
2255 // Noting the "special* encoding of the input attachment ordering rule (in access, but not exec)
2256 if ((ordering.access_scope & SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) &&
2257 (input_attachment_barriers != kNoAttachmentRead)) {
2258 ordered_read = true;
John Zulauf69133422020-05-20 14:55:53 -06002259 }
John Zulaufd14743a2020-07-03 09:42:39 -06002260
John Zulauf361fb532020-07-22 10:45:39 -06002261 if (!ordered_read && IsWriteHazard(usage)) {
2262 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2263 }
2264 }
2265
2266 } else {
2267 // Only check for WAW if there are no reads since last_write
2268 if (last_read_count) {
2269 // Ignore ordered read stages (which represent frame-buffer local operations, except input attachment
2270 const auto unordered_reads = last_read_stages & ~ordering.exec_scope;
2271 // Look for any WAR hazards outside the ordered set of stages
2272 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2273 const auto &read_access = last_reads[read_index];
2274 if ((read_access.stage & unordered_reads) && IsReadHazard(usage_stage, read_access)) {
2275 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2276 break;
John Zulaufd14743a2020-07-03 09:42:39 -06002277 }
2278 }
John Zulauf361fb532020-07-22 10:45:39 -06002279 } else if (input_attachment_barriers != kNoAttachmentRead) {
2280 // This is special case code for the fragment shader input attachment, which unlike all other fragment shader operations
2281 // is framebuffer local, and thus subject to raster ordering guarantees
2282 if (0 == (ordering.access_scope & SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT)) {
2283 // NOTE: Currently all ordering barriers include this bit, so this code may never be reached, but it's
2284 // here s.t. if we need to change the ordering barrier/rules we needn't change the code.
2285 hazard.Set(this, usage_index, WRITE_AFTER_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
2286 }
2287 } else if (!write_is_ordered && IsWriteHazard(usage)) {
2288 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf69133422020-05-20 14:55:53 -06002289 }
2290 }
2291 return hazard;
2292}
2293
John Zulauf2f952d22020-02-10 11:34:51 -07002294// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06002295HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002296 HazardResult hazard;
2297 auto usage = FlagBit(usage_index);
2298 if (IsRead(usage)) {
2299 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002300 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002301 }
2302 } else {
2303 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002304 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002305 } else if (last_read_count > 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002306 hazard.Set(this, usage_index, WRITE_RACING_READ, last_reads[0].access, last_reads[0].tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002307 } else if (input_attachment_barriers != kNoAttachmentRead) {
John Zulauf59e25072020-07-17 10:55:21 -06002308 hazard.Set(this, usage_index, WRITE_RACING_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002309 }
2310 }
2311 return hazard;
2312}
2313
John Zulauf36bcf6a2020-02-03 15:12:52 -07002314HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
2315 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002316 // Only supporting image layout transitions for now
2317 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2318 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002319 // only test for WAW if there no intervening read operations.
2320 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2321 if (last_read_count) {
John Zulauf355e49b2020-04-24 15:11:15 -06002322 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002323 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002324 const auto &read_access = last_reads[read_index];
2325 // If the read stage is not in the src sync sync
2326 // *AND* not execution chained with an existing sync barrier (that's the or)
2327 // then the barrier access is unsafe (R/W after R)
2328 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002329 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002330 break;
2331 }
2332 }
John Zulauf361fb532020-07-22 10:45:39 -06002333 } else if (input_attachment_barriers != kNoAttachmentRead) {
John Zulaufd14743a2020-07-03 09:42:39 -06002334 // Same logic as read acces above for the special case of input attachment read
John Zulaufd14743a2020-07-03 09:42:39 -06002335 if ((src_exec_scope & (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | input_attachment_barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002336 hazard.Set(this, usage_index, WRITE_AFTER_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT, input_attachment_tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002337 }
John Zulauf361fb532020-07-22 10:45:39 -06002338 } else if (last_write) {
2339 // If the previous write is *not* in the 1st access scope
2340 // *AND* the current barrier is not in the dependency chain
2341 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2342 // then the barrier access is unsafe (R/W after W)
2343 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
2344 // TODO: Do we need a difference hazard name for this?
2345 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2346 }
John Zulaufd14743a2020-07-03 09:42:39 -06002347 }
John Zulauf361fb532020-07-22 10:45:39 -06002348
John Zulauf0cb5be22020-01-23 12:18:22 -07002349 return hazard;
2350}
2351
John Zulauf5f13a792020-03-10 07:31:21 -06002352// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2353// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2354// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2355void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2356 if (write_tag.IsBefore(other.write_tag)) {
2357 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
2358 *this = other;
2359 } else if (!other.write_tag.IsBefore(write_tag)) {
2360 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2361 // dependency chaining logic or any stage expansion)
2362 write_barriers |= other.write_barriers;
2363
John Zulaufd14743a2020-07-03 09:42:39 -06002364 // Merge the read states
2365 if (input_attachment_barriers == kNoAttachmentRead) {
2366 // this doesn't have an input attachment read, so we'll take other, unconditionally (even if it's kNoAttachmentRead)
2367 input_attachment_barriers = other.input_attachment_barriers;
2368 input_attachment_tag = other.input_attachment_tag;
2369 } else if (other.input_attachment_barriers != kNoAttachmentRead) {
2370 // Both states have an input attachment read, pick the newest tag and merge barriers.
2371 if (input_attachment_tag.IsBefore(other.input_attachment_tag)) {
2372 input_attachment_tag = other.input_attachment_tag;
2373 }
2374 input_attachment_barriers |= other.input_attachment_barriers;
2375 }
2376 // The else clause is that only this has an attachment read and no merge is needed
2377
John Zulauf5f13a792020-03-10 07:31:21 -06002378 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2379 auto &other_read = other.last_reads[other_read_index];
2380 if (last_read_stages & other_read.stage) {
2381 // Merge in the barriers for read stages that exist in *both* this and other
2382 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
2383 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
2384 auto &my_read = last_reads[my_read_index];
2385 if (other_read.stage == my_read.stage) {
2386 if (my_read.tag.IsBefore(other_read.tag)) {
2387 my_read.tag = other_read.tag;
John Zulauf37ceaed2020-07-03 16:18:15 -06002388 my_read.access = other_read.access;
John Zulauf5f13a792020-03-10 07:31:21 -06002389 }
2390 my_read.barriers |= other_read.barriers;
2391 break;
2392 }
2393 }
2394 } else {
2395 // The other read stage doesn't exist in this, so add it.
2396 last_reads[last_read_count] = other_read;
2397 last_read_count++;
2398 last_read_stages |= other_read.stage;
2399 }
2400 }
John Zulauf361fb532020-07-22 10:45:39 -06002401 read_execution_barriers |= other.read_execution_barriers;
John Zulauf5f13a792020-03-10 07:31:21 -06002402 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
2403 // it.
2404}
2405
John Zulauf9cb530d2019-09-30 14:14:10 -06002406void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2407 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2408 const auto usage_bit = FlagBit(usage_index);
John Zulaufd14743a2020-07-03 09:42:39 -06002409 if (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) {
2410 // Input attachment requires special treatment for raster/load/store ordering guarantees
2411 input_attachment_barriers = 0;
2412 input_attachment_tag = tag;
2413 } else if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002414 // Mulitple outstanding reads may be of interest and do dependency chains independently
2415 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2416 const auto usage_stage = PipelineStageBit(usage_index);
2417 if (usage_stage & last_read_stages) {
2418 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2419 ReadState &access = last_reads[read_index];
2420 if (access.stage == usage_stage) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002421 access.access = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002422 access.barriers = 0;
2423 access.tag = tag;
2424 break;
2425 }
2426 }
2427 } else {
2428 // We don't have this stage in the list yet...
2429 assert(last_read_count < last_reads.size());
2430 ReadState &access = last_reads[last_read_count++];
2431 access.stage = usage_stage;
John Zulauf37ceaed2020-07-03 16:18:15 -06002432 access.access = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002433 access.barriers = 0;
2434 access.tag = tag;
2435 last_read_stages |= usage_stage;
2436 }
2437 } else {
2438 // Assume write
2439 // TODO determine what to do with READ-WRITE operations if any
John Zulaufd14743a2020-07-03 09:42:39 -06002440 // Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
John Zulauf9cb530d2019-09-30 14:14:10 -06002441 // if the last_reads/last_write were unsafe, we've reported them,
2442 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
2443 last_read_count = 0;
2444 last_read_stages = 0;
John Zulauf361fb532020-07-22 10:45:39 -06002445 read_execution_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002446
John Zulaufd14743a2020-07-03 09:42:39 -06002447 input_attachment_barriers = kNoAttachmentRead; // Denotes no outstanding input attachment read after the last write.
2448 // NOTE: we don't reset the tag, as the equality check ignores it when kNoAttachmentRead is set.
2449
John Zulauf9cb530d2019-09-30 14:14:10 -06002450 write_barriers = 0;
2451 write_dependency_chain = 0;
2452 write_tag = tag;
2453 last_write = usage_bit;
2454 }
2455}
John Zulauf5f13a792020-03-10 07:31:21 -06002456
John Zulaufa0a98292020-09-18 09:30:10 -06002457// TODO: Leave "old style" execution barrier code until PipelineBarrier is corrected for true unordered barrier application
John Zulauf9cb530d2019-09-30 14:14:10 -06002458void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
2459 // Execution Barriers only protect read operations
2460 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2461 ReadState &access = last_reads[read_index];
2462 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2463 if (srcStageMask & (access.stage | access.barriers)) {
2464 access.barriers |= dstStageMask;
John Zulauf361fb532020-07-22 10:45:39 -06002465 read_execution_barriers |= dstStageMask;
John Zulauf9cb530d2019-09-30 14:14:10 -06002466 }
2467 }
John Zulauf361fb532020-07-22 10:45:39 -06002468 if ((input_attachment_barriers != kNoAttachmentRead) &&
2469 (srcStageMask & (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | input_attachment_barriers))) {
John Zulaufd14743a2020-07-03 09:42:39 -06002470 input_attachment_barriers |= dstStageMask;
John Zulauf361fb532020-07-22 10:45:39 -06002471 read_execution_barriers |= dstStageMask;
John Zulaufd14743a2020-07-03 09:42:39 -06002472 }
John Zulauf361fb532020-07-22 10:45:39 -06002473 if (write_dependency_chain & srcStageMask) {
2474 write_dependency_chain |= dstStageMask;
2475 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002476}
2477
John Zulaufa0a98292020-09-18 09:30:10 -06002478void ResourceAccessState::ApplyExecutionBarriers(const std::vector<SyncBarrier> &barriers) {
2479 // Since all execution chains are unordered, apply before update, accumulating update in pending_
2480 // s.t. we don't create unintended dependency chaings.
2481 // For reads the barriers function as the dependency chain
2482 VkPipelineStageFlags pending_write_chain = 0;
2483 VkPipelineStageFlags pending_input_attacment_barriers = 0;
2484 std::array<VkPipelineStageFlags, kStageCount> pending_read_barriers;
2485 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2486 pending_read_barriers[read_index] = 0;
2487 }
2488
2489 for (const auto &barrier : barriers) {
2490 const VkPipelineStageFlags src_scope = barrier.src_exec_scope;
2491 const VkPipelineStageFlags dst_scope = barrier.dst_exec_scope;
2492 // Execution Barriers only protect read operations
2493 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2494 const ReadState &access = last_reads[read_index];
2495 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2496 if (src_scope & (access.stage | access.barriers)) {
2497 pending_read_barriers[read_index] |= dst_scope;
2498 }
2499 }
2500 if ((input_attachment_barriers != kNoAttachmentRead) &&
2501 (src_scope & (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | input_attachment_barriers))) {
2502 pending_input_attacment_barriers |= dst_scope;
2503 }
2504 if (InSourceScopeOrChain(src_scope, barrier.src_access_scope)) {
2505 pending_write_chain |= dst_scope;
2506 }
2507 }
2508
2509 // Apply accumulated pending changes to the depenendency chains and barriers
2510 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2511 last_reads[read_index].barriers |= pending_read_barriers[read_index];
2512 read_execution_barriers |= pending_read_barriers[read_index];
2513 }
2514 input_attachment_barriers |= pending_input_attacment_barriers;
2515 read_execution_barriers |= pending_input_attacment_barriers;
2516 write_dependency_chain |= pending_write_chain;
2517}
2518
2519void ResourceAccessState::ApplyMemoryAccessBarrier(bool multi_dep, VkPipelineStageFlags src_exec_scope,
2520 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
2521 SyncStageAccessFlags dst_access_scope) {
2522 // We update just the write barrier side of the execution and memory barrier. Updating the dependency chain would
2523 // create chaining between batches of barriers, which isn't wasn't intended.
2524 // The callers is responsible for coming back and updating the dependency chain information after the memory access
2525 // batch is complete.
John Zulauf9cb530d2019-09-30 14:14:10 -06002526 // The || implements the "dependency chain" logic for this barrier
John Zulaufa0a98292020-09-18 09:30:10 -06002527 if (InSourceScopeOrChain(src_exec_scope, src_access_scope)) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002528 write_barriers |= dst_access_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002529 if (!multi_dep) {
2530 write_dependency_chain |= dst_exec_scope;
2531 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002532 }
2533}
2534
John Zulauf59e25072020-07-17 10:55:21 -06002535// This should be just Bits or Index, but we don't have an invalid state for Index
2536VkPipelineStageFlags ResourceAccessState::GetReadBarriers(SyncStageAccessFlags usage_bit) const {
2537 VkPipelineStageFlags barriers = 0U;
2538 if (usage_bit & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) {
2539 barriers = input_attachment_barriers;
2540 } else {
2541 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2542 const auto &read_access = last_reads[read_index];
2543 if (read_access.access & usage_bit) {
2544 barriers = read_access.barriers;
2545 break;
2546 }
2547 }
2548 }
2549 return barriers;
2550}
2551
John Zulaufd1f85d42020-04-15 12:23:15 -06002552void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002553 auto *access_context = GetAccessContextNoInsert(command_buffer);
2554 if (access_context) {
2555 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002556 }
2557}
2558
John Zulaufd1f85d42020-04-15 12:23:15 -06002559void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2560 auto access_found = cb_access_state.find(command_buffer);
2561 if (access_found != cb_access_state.end()) {
2562 access_found->second->Reset();
2563 cb_access_state.erase(access_found);
2564 }
2565}
2566
John Zulauf540266b2020-04-06 18:54:53 -06002567void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002568 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
2569 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002570 const VkMemoryBarrier *pMemoryBarriers) {
2571 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07002572 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002573 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002574 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002575}
2576
John Zulauf540266b2020-04-06 18:54:53 -06002577void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002578 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2579 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002580 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002581 for (uint32_t index = 0; index < barrier_count; index++) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002582 auto barrier = barriers[index]; // barrier is a copy
John Zulauf9cb530d2019-09-30 14:14:10 -06002583 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2584 if (!buffer) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002585 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
2586 const ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002587 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2588 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2589 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2590 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002591 }
2592}
2593
John Zulauf540266b2020-04-06 18:54:53 -06002594void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2595 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2596 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002597 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002598 for (uint32_t index = 0; index < barrier_count; index++) {
2599 const auto &barrier = barriers[index];
2600 const auto *image = Get<IMAGE_STATE>(barrier.image);
2601 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002602 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002603 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2604 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2605 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2606 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
2607 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002608 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002609}
2610
2611bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2612 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2613 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002614 const auto *cb_context = GetAccessContext(commandBuffer);
2615 assert(cb_context);
2616 if (!cb_context) return skip;
2617 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002618
John Zulauf3d84f1b2020-03-09 13:33:25 -06002619 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002620 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002621 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002622
2623 for (uint32_t region = 0; region < regionCount; region++) {
2624 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002625 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002626 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002627 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002628 if (hazard.hazard) {
2629 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002630 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002631 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002632 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002633 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002634 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002635 }
John Zulauf16adfc92020-04-08 10:28:33 -06002636 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002637 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06002638 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002639 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002640 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002641 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002642 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002643 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002644 }
2645 }
2646 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002647 }
2648 return skip;
2649}
2650
2651void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2652 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002653 auto *cb_context = GetAccessContext(commandBuffer);
2654 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002655 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002656 auto *context = cb_context->GetCurrentAccessContext();
2657
John Zulauf9cb530d2019-09-30 14:14:10 -06002658 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002659 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002660
2661 for (uint32_t region = 0; region < regionCount; region++) {
2662 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002663 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002664 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002665 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002666 }
John Zulauf16adfc92020-04-08 10:28:33 -06002667 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002668 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002669 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002670 }
2671 }
2672}
2673
Jeff Leger178b1e52020-10-05 12:22:23 -04002674bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
2675 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
2676 bool skip = false;
2677 const auto *cb_context = GetAccessContext(commandBuffer);
2678 assert(cb_context);
2679 if (!cb_context) return skip;
2680 const auto *context = cb_context->GetCurrentAccessContext();
2681
2682 // If we have no previous accesses, we have no hazards
2683 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2684 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2685
2686 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2687 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2688 if (src_buffer) {
2689 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2690 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
2691 if (hazard.hazard) {
2692 // TODO -- add tag information to log msg when useful.
2693 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
2694 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
2695 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
2696 region, string_UsageTag(hazard).c_str());
2697 }
2698 }
2699 if (dst_buffer && !skip) {
2700 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2701 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
2702 if (hazard.hazard) {
2703 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
2704 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
2705 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
2706 region, string_UsageTag(hazard).c_str());
2707 }
2708 }
2709 if (skip) break;
2710 }
2711 return skip;
2712}
2713
2714void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
2715 auto *cb_context = GetAccessContext(commandBuffer);
2716 assert(cb_context);
2717 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
2718 auto *context = cb_context->GetCurrentAccessContext();
2719
2720 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2721 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2722
2723 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2724 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2725 if (src_buffer) {
2726 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2727 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
2728 }
2729 if (dst_buffer) {
2730 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2731 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
2732 }
2733 }
2734}
2735
John Zulauf5c5e88d2019-12-26 11:22:02 -07002736bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2737 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2738 const VkImageCopy *pRegions) const {
2739 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002740 const auto *cb_access_context = GetAccessContext(commandBuffer);
2741 assert(cb_access_context);
2742 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002743
John Zulauf3d84f1b2020-03-09 13:33:25 -06002744 const auto *context = cb_access_context->GetCurrentAccessContext();
2745 assert(context);
2746 if (!context) return skip;
2747
2748 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2749 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002750 for (uint32_t region = 0; region < regionCount; region++) {
2751 const auto &copy_region = pRegions[region];
2752 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002753 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002754 copy_region.srcOffset, copy_region.extent);
2755 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002756 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002757 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002758 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002759 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002760 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002761 }
2762
2763 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002764 VkExtent3D dst_copy_extent =
2765 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002766 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002767 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002768 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002769 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002770 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002771 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002772 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002773 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002774 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002775 }
2776 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002777
John Zulauf5c5e88d2019-12-26 11:22:02 -07002778 return skip;
2779}
2780
2781void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2782 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2783 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002784 auto *cb_access_context = GetAccessContext(commandBuffer);
2785 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002786 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002787 auto *context = cb_access_context->GetCurrentAccessContext();
2788 assert(context);
2789
John Zulauf5c5e88d2019-12-26 11:22:02 -07002790 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002791 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002792
2793 for (uint32_t region = 0; region < regionCount; region++) {
2794 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002795 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002796 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2797 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002798 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002799 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002800 VkExtent3D dst_copy_extent =
2801 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002802 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2803 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002804 }
2805 }
2806}
2807
Jeff Leger178b1e52020-10-05 12:22:23 -04002808bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
2809 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
2810 bool skip = false;
2811 const auto *cb_access_context = GetAccessContext(commandBuffer);
2812 assert(cb_access_context);
2813 if (!cb_access_context) return skip;
2814
2815 const auto *context = cb_access_context->GetCurrentAccessContext();
2816 assert(context);
2817 if (!context) return skip;
2818
2819 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2820 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2821 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2822 const auto &copy_region = pCopyImageInfo->pRegions[region];
2823 if (src_image) {
2824 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
2825 copy_region.srcOffset, copy_region.extent);
2826 if (hazard.hazard) {
2827 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
2828 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
2829 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
2830 region, string_UsageTag(hazard).c_str());
2831 }
2832 }
2833
2834 if (dst_image) {
2835 VkExtent3D dst_copy_extent =
2836 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2837 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
2838 copy_region.dstOffset, dst_copy_extent);
2839 if (hazard.hazard) {
2840 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
2841 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
2842 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
2843 region, string_UsageTag(hazard).c_str());
2844 }
2845 if (skip) break;
2846 }
2847 }
2848
2849 return skip;
2850}
2851
2852void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
2853 auto *cb_access_context = GetAccessContext(commandBuffer);
2854 assert(cb_access_context);
2855 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
2856 auto *context = cb_access_context->GetCurrentAccessContext();
2857 assert(context);
2858
2859 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2860 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2861
2862 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2863 const auto &copy_region = pCopyImageInfo->pRegions[region];
2864 if (src_image) {
2865 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2866 copy_region.extent, tag);
2867 }
2868 if (dst_image) {
2869 VkExtent3D dst_copy_extent =
2870 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2871 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2872 dst_copy_extent, tag);
2873 }
2874 }
2875}
2876
John Zulauf9cb530d2019-09-30 14:14:10 -06002877bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2878 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2879 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2880 uint32_t bufferMemoryBarrierCount,
2881 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2882 uint32_t imageMemoryBarrierCount,
2883 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2884 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002885 const auto *cb_access_context = GetAccessContext(commandBuffer);
2886 assert(cb_access_context);
2887 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002888
John Zulauf3d84f1b2020-03-09 13:33:25 -06002889 const auto *context = cb_access_context->GetCurrentAccessContext();
2890 assert(context);
2891 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002892
John Zulauf3d84f1b2020-03-09 13:33:25 -06002893 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002894 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2895 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002896 // Validate Image Layout transitions
2897 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2898 const auto &barrier = pImageMemoryBarriers[index];
2899 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2900 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2901 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002902 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002903 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002904 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002905 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002906 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002907 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002908 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002909 }
2910 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002911
2912 return skip;
2913}
2914
2915void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2916 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2917 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2918 uint32_t bufferMemoryBarrierCount,
2919 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2920 uint32_t imageMemoryBarrierCount,
2921 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002922 auto *cb_access_context = GetAccessContext(commandBuffer);
2923 assert(cb_access_context);
2924 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002925 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002926 auto access_context = cb_access_context->GetCurrentAccessContext();
2927 assert(access_context);
2928 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06002929
John Zulauf3d84f1b2020-03-09 13:33:25 -06002930 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002931 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002932 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002933 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
2934 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2935 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002936 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
2937 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002938 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06002939 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002940
2941 // Apply these last in-case there operation is a superset of the other two and would clean them up...
John Zulauf3d84f1b2020-03-09 13:33:25 -06002942 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07002943 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06002944}
2945
2946void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
2947 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
2948 // The state tracker sets up the device state
2949 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
2950
John Zulauf5f13a792020-03-10 07:31:21 -06002951 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
2952 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06002953 // TODO: Find a good way to do this hooklessly.
2954 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
2955 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
2956 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
2957
John Zulaufd1f85d42020-04-15 12:23:15 -06002958 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2959 sync_device_state->ResetCommandBufferCallback(command_buffer);
2960 });
2961 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2962 sync_device_state->FreeCommandBufferCallback(command_buffer);
2963 });
John Zulauf9cb530d2019-09-30 14:14:10 -06002964}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002965
John Zulauf355e49b2020-04-24 15:11:15 -06002966bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2967 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
2968 bool skip = false;
2969 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
2970 auto cb_context = GetAccessContext(commandBuffer);
2971
2972 if (rp_state && cb_context) {
2973 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
2974 }
2975
2976 return skip;
2977}
2978
2979bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2980 VkSubpassContents contents) const {
2981 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2982 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2983 subpass_begin_info.contents = contents;
2984 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
2985 return skip;
2986}
2987
2988bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2989 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2990 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2991 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
2992 return skip;
2993}
2994
2995bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2996 const VkRenderPassBeginInfo *pRenderPassBegin,
2997 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2998 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2999 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
3000 return skip;
3001}
3002
John Zulauf3d84f1b2020-03-09 13:33:25 -06003003void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3004 VkResult result) {
3005 // The state tracker sets up the command buffer state
3006 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3007
3008 // Create/initialize the structure that trackers accesses at the command buffer scope.
3009 auto cb_access_context = GetAccessContext(commandBuffer);
3010 assert(cb_access_context);
3011 cb_access_context->Reset();
3012}
3013
3014void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06003015 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003016 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003017 if (cb_context) {
3018 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003019 }
3020}
3021
3022void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3023 VkSubpassContents contents) {
3024 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3025 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3026 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003027 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003028}
3029
3030void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3031 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3032 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003033 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003034}
3035
3036void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3037 const VkRenderPassBeginInfo *pRenderPassBegin,
3038 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3039 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003040 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
3041}
3042
3043bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
3044 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
3045 bool skip = false;
3046
3047 auto cb_context = GetAccessContext(commandBuffer);
3048 assert(cb_context);
3049 auto cb_state = cb_context->GetCommandBufferState();
3050 if (!cb_state) return skip;
3051
3052 auto rp_state = cb_state->activeRenderPass;
3053 if (!rp_state) return skip;
3054
3055 skip |= cb_context->ValidateNextSubpass(func_name);
3056
3057 return skip;
3058}
3059
3060bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3061 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
3062 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3063 subpass_begin_info.contents = contents;
3064 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
3065 return skip;
3066}
3067
3068bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
3069 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3070 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3071 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
3072 return skip;
3073}
3074
3075bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3076 const VkSubpassEndInfo *pSubpassEndInfo) const {
3077 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3078 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
3079 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003080}
3081
3082void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003083 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003084 auto cb_context = GetAccessContext(commandBuffer);
3085 assert(cb_context);
3086 auto cb_state = cb_context->GetCommandBufferState();
3087 if (!cb_state) return;
3088
3089 auto rp_state = cb_state->activeRenderPass;
3090 if (!rp_state) return;
3091
John Zulauf355e49b2020-04-24 15:11:15 -06003092 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003093}
3094
3095void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3096 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
3097 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3098 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003099 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003100}
3101
3102void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3103 const VkSubpassEndInfo *pSubpassEndInfo) {
3104 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003105 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003106}
3107
3108void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3109 const VkSubpassEndInfo *pSubpassEndInfo) {
3110 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003111 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003112}
3113
John Zulauf355e49b2020-04-24 15:11:15 -06003114bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
3115 const char *func_name) const {
3116 bool skip = false;
3117
3118 auto cb_context = GetAccessContext(commandBuffer);
3119 assert(cb_context);
3120 auto cb_state = cb_context->GetCommandBufferState();
3121 if (!cb_state) return skip;
3122
3123 auto rp_state = cb_state->activeRenderPass;
3124 if (!rp_state) return skip;
3125
3126 skip |= cb_context->ValidateEndRenderpass(func_name);
3127 return skip;
3128}
3129
3130bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3131 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
3132 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
3133 return skip;
3134}
3135
3136bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
3137 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3138 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
3139 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
3140 return skip;
3141}
3142
3143bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
3144 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3145 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
3146 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
3147 return skip;
3148}
3149
3150void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3151 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003152 // Resolve the all subpass contexts to the command buffer contexts
3153 auto cb_context = GetAccessContext(commandBuffer);
3154 assert(cb_context);
3155 auto cb_state = cb_context->GetCommandBufferState();
3156 if (!cb_state) return;
3157
locke-lunargaecf2152020-05-12 17:15:41 -06003158 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06003159 if (!rp_state) return;
3160
John Zulauf355e49b2020-04-24 15:11:15 -06003161 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06003162}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003163
John Zulauf33fc1d52020-07-17 11:01:10 -06003164// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3165// updates to a resource which do not conflict at the byte level.
3166// TODO: Revisit this rule to see if it needs to be tighter or looser
3167// TODO: Add programatic control over suppression heuristics
3168bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3169 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3170}
3171
John Zulauf3d84f1b2020-03-09 13:33:25 -06003172void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003173 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003174 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003175}
3176
3177void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003178 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003179 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003180}
3181
3182void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003183 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003184 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003185}
locke-lunarga19c71d2020-03-02 18:17:04 -07003186
Jeff Leger178b1e52020-10-05 12:22:23 -04003187template <typename BufferImageCopyRegionType>
3188bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3189 VkImageLayout dstImageLayout, uint32_t regionCount,
3190 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003191 bool skip = false;
3192 const auto *cb_access_context = GetAccessContext(commandBuffer);
3193 assert(cb_access_context);
3194 if (!cb_access_context) return skip;
3195
Jeff Leger178b1e52020-10-05 12:22:23 -04003196 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3197 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3198
locke-lunarga19c71d2020-03-02 18:17:04 -07003199 const auto *context = cb_access_context->GetCurrentAccessContext();
3200 assert(context);
3201 if (!context) return skip;
3202
3203 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003204 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3205
3206 for (uint32_t region = 0; region < regionCount; region++) {
3207 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003208 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003209 ResourceAccessRange src_range =
3210 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003211 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003212 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003213 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003214 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003215 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003216 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003217 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003218 }
3219 }
3220 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003221 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003222 copy_region.imageOffset, copy_region.imageExtent);
3223 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003224 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003225 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003226 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003227 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003228 }
3229 if (skip) break;
3230 }
3231 if (skip) break;
3232 }
3233 return skip;
3234}
3235
Jeff Leger178b1e52020-10-05 12:22:23 -04003236bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3237 VkImageLayout dstImageLayout, uint32_t regionCount,
3238 const VkBufferImageCopy *pRegions) const {
3239 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3240 COPY_COMMAND_VERSION_1);
3241}
3242
3243bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3244 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3245 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3246 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3247 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3248}
3249
3250template <typename BufferImageCopyRegionType>
3251void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3252 VkImageLayout dstImageLayout, uint32_t regionCount,
3253 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003254 auto *cb_access_context = GetAccessContext(commandBuffer);
3255 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003256
3257 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3258 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3259
3260 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003261 auto *context = cb_access_context->GetCurrentAccessContext();
3262 assert(context);
3263
3264 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003265 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003266
3267 for (uint32_t region = 0; region < regionCount; region++) {
3268 const auto &copy_region = pRegions[region];
3269 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003270 ResourceAccessRange src_range =
3271 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003272 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003273 }
3274 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003275 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003276 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003277 }
3278 }
3279}
3280
Jeff Leger178b1e52020-10-05 12:22:23 -04003281void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3282 VkImageLayout dstImageLayout, uint32_t regionCount,
3283 const VkBufferImageCopy *pRegions) {
3284 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3285 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3286}
3287
3288void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3289 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3290 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3291 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3292 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3293 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3294}
3295
3296template <typename BufferImageCopyRegionType>
3297bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3298 VkBuffer dstBuffer, uint32_t regionCount,
3299 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003300 bool skip = false;
3301 const auto *cb_access_context = GetAccessContext(commandBuffer);
3302 assert(cb_access_context);
3303 if (!cb_access_context) return skip;
3304
Jeff Leger178b1e52020-10-05 12:22:23 -04003305 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3306 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3307
locke-lunarga19c71d2020-03-02 18:17:04 -07003308 const auto *context = cb_access_context->GetCurrentAccessContext();
3309 assert(context);
3310 if (!context) return skip;
3311
3312 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3313 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3314 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3315 for (uint32_t region = 0; region < regionCount; region++) {
3316 const auto &copy_region = pRegions[region];
3317 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003318 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003319 copy_region.imageOffset, copy_region.imageExtent);
3320 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003321 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003322 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003323 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003324 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003325 }
3326 }
3327 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06003328 ResourceAccessRange dst_range =
3329 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003330 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003331 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003332 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003333 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003334 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003335 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003336 }
3337 }
3338 if (skip) break;
3339 }
3340 return skip;
3341}
3342
Jeff Leger178b1e52020-10-05 12:22:23 -04003343bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3344 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3345 const VkBufferImageCopy *pRegions) const {
3346 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3347 COPY_COMMAND_VERSION_1);
3348}
3349
3350bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3351 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3352 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3353 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3354 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3355}
3356
3357template <typename BufferImageCopyRegionType>
3358void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3359 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3360 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003361 auto *cb_access_context = GetAccessContext(commandBuffer);
3362 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003363
3364 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3365 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3366
3367 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003368 auto *context = cb_access_context->GetCurrentAccessContext();
3369 assert(context);
3370
3371 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003372 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3373 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 -06003374 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003375
3376 for (uint32_t region = 0; region < regionCount; region++) {
3377 const auto &copy_region = pRegions[region];
3378 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003379 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003380 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003381 }
3382 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003383 ResourceAccessRange dst_range =
3384 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003385 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003386 }
3387 }
3388}
3389
Jeff Leger178b1e52020-10-05 12:22:23 -04003390void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3391 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3392 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3393 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3394}
3395
3396void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3397 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3398 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3399 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3400 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3401 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3402}
3403
3404template <typename RegionType>
3405bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3406 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3407 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003408 bool skip = false;
3409 const auto *cb_access_context = GetAccessContext(commandBuffer);
3410 assert(cb_access_context);
3411 if (!cb_access_context) return skip;
3412
3413 const auto *context = cb_access_context->GetCurrentAccessContext();
3414 assert(context);
3415 if (!context) return skip;
3416
3417 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3418 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3419
3420 for (uint32_t region = 0; region < regionCount; region++) {
3421 const auto &blit_region = pRegions[region];
3422 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003423 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3424 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3425 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3426 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3427 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3428 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3429 auto hazard =
3430 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003431 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003432 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003433 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003434 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003435 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003436 }
3437 }
3438
3439 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003440 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3441 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3442 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3443 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3444 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3445 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3446 auto hazard =
3447 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003448 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003449 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003450 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003451 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003452 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003453 }
3454 if (skip) break;
3455 }
3456 }
3457
3458 return skip;
3459}
3460
Jeff Leger178b1e52020-10-05 12:22:23 -04003461bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3462 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3463 const VkImageBlit *pRegions, VkFilter filter) const {
3464 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3465 "vkCmdBlitImage");
3466}
3467
3468bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3469 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3470 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3471 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3472 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3473}
3474
3475template <typename RegionType>
3476void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3477 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3478 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003479 auto *cb_access_context = GetAccessContext(commandBuffer);
3480 assert(cb_access_context);
3481 auto *context = cb_access_context->GetCurrentAccessContext();
3482 assert(context);
3483
3484 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003485 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003486
3487 for (uint32_t region = 0; region < regionCount; region++) {
3488 const auto &blit_region = pRegions[region];
3489 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003490 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3491 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3492 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3493 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3494 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3495 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3496 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003497 }
3498 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003499 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3500 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3501 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3502 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3503 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3504 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3505 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003506 }
3507 }
3508}
locke-lunarg36ba2592020-04-03 09:42:04 -06003509
Jeff Leger178b1e52020-10-05 12:22:23 -04003510void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3511 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3512 const VkImageBlit *pRegions, VkFilter filter) {
3513 auto *cb_access_context = GetAccessContext(commandBuffer);
3514 assert(cb_access_context);
3515 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3516 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3517 pRegions, filter);
3518 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3519}
3520
3521void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3522 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3523 auto *cb_access_context = GetAccessContext(commandBuffer);
3524 assert(cb_access_context);
3525 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3526 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3527 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3528 pBlitImageInfo->filter, tag);
3529}
3530
locke-lunarg61870c22020-06-09 14:51:50 -06003531bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
3532 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
3533 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003534 bool skip = false;
3535 if (drawCount == 0) return skip;
3536
3537 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3538 VkDeviceSize size = struct_size;
3539 if (drawCount == 1 || stride == size) {
3540 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003541 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003542 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3543 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003544 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003545 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003546 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003547 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003548 }
3549 } else {
3550 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003551 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003552 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3553 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003554 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003555 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3556 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3557 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003558 break;
3559 }
3560 }
3561 }
3562 return skip;
3563}
3564
locke-lunarg61870c22020-06-09 14:51:50 -06003565void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3566 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3567 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003568 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3569 VkDeviceSize size = struct_size;
3570 if (drawCount == 1 || stride == size) {
3571 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003572 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003573 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3574 } else {
3575 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003576 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003577 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3578 }
3579 }
3580}
3581
locke-lunarg61870c22020-06-09 14:51:50 -06003582bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3583 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003584 bool skip = false;
3585
3586 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003587 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003588 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3589 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003590 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003591 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003592 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003593 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003594 }
3595 return skip;
3596}
3597
locke-lunarg61870c22020-06-09 14:51:50 -06003598void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003599 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003600 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003601 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3602}
3603
locke-lunarg36ba2592020-04-03 09:42:04 -06003604bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003605 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003606 const auto *cb_access_context = GetAccessContext(commandBuffer);
3607 assert(cb_access_context);
3608 if (!cb_access_context) return skip;
3609
locke-lunarg61870c22020-06-09 14:51:50 -06003610 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003611 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003612}
3613
3614void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003615 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003616 auto *cb_access_context = GetAccessContext(commandBuffer);
3617 assert(cb_access_context);
3618 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003619
locke-lunarg61870c22020-06-09 14:51:50 -06003620 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003621}
locke-lunarge1a67022020-04-29 00:15:36 -06003622
3623bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003624 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003625 const auto *cb_access_context = GetAccessContext(commandBuffer);
3626 assert(cb_access_context);
3627 if (!cb_access_context) return skip;
3628
3629 const auto *context = cb_access_context->GetCurrentAccessContext();
3630 assert(context);
3631 if (!context) return skip;
3632
locke-lunarg61870c22020-06-09 14:51:50 -06003633 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3634 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3635 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003636 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003637}
3638
3639void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003640 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003641 auto *cb_access_context = GetAccessContext(commandBuffer);
3642 assert(cb_access_context);
3643 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3644 auto *context = cb_access_context->GetCurrentAccessContext();
3645 assert(context);
3646
locke-lunarg61870c22020-06-09 14:51:50 -06003647 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3648 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003649}
3650
3651bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3652 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003653 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003654 const auto *cb_access_context = GetAccessContext(commandBuffer);
3655 assert(cb_access_context);
3656 if (!cb_access_context) return skip;
3657
locke-lunarg61870c22020-06-09 14:51:50 -06003658 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3659 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3660 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003661 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003662}
3663
3664void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3665 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003666 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003667 auto *cb_access_context = GetAccessContext(commandBuffer);
3668 assert(cb_access_context);
3669 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003670
locke-lunarg61870c22020-06-09 14:51:50 -06003671 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3672 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3673 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003674}
3675
3676bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3677 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003678 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003679 const auto *cb_access_context = GetAccessContext(commandBuffer);
3680 assert(cb_access_context);
3681 if (!cb_access_context) return skip;
3682
locke-lunarg61870c22020-06-09 14:51:50 -06003683 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3684 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3685 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003686 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003687}
3688
3689void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3690 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003691 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003692 auto *cb_access_context = GetAccessContext(commandBuffer);
3693 assert(cb_access_context);
3694 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003695
locke-lunarg61870c22020-06-09 14:51:50 -06003696 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3697 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3698 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003699}
3700
3701bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3702 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003703 bool skip = false;
3704 if (drawCount == 0) return skip;
3705
locke-lunargff255f92020-05-13 18:53:52 -06003706 const auto *cb_access_context = GetAccessContext(commandBuffer);
3707 assert(cb_access_context);
3708 if (!cb_access_context) return skip;
3709
3710 const auto *context = cb_access_context->GetCurrentAccessContext();
3711 assert(context);
3712 if (!context) return skip;
3713
locke-lunarg61870c22020-06-09 14:51:50 -06003714 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3715 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3716 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3717 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003718
3719 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3720 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3721 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003722 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003723 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003724}
3725
3726void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3727 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003728 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003729 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003730 auto *cb_access_context = GetAccessContext(commandBuffer);
3731 assert(cb_access_context);
3732 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3733 auto *context = cb_access_context->GetCurrentAccessContext();
3734 assert(context);
3735
locke-lunarg61870c22020-06-09 14:51:50 -06003736 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3737 cb_access_context->RecordDrawSubpassAttachment(tag);
3738 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003739
3740 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3741 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3742 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003743 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003744}
3745
3746bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3747 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003748 bool skip = false;
3749 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003750 const auto *cb_access_context = GetAccessContext(commandBuffer);
3751 assert(cb_access_context);
3752 if (!cb_access_context) return skip;
3753
3754 const auto *context = cb_access_context->GetCurrentAccessContext();
3755 assert(context);
3756 if (!context) return skip;
3757
locke-lunarg61870c22020-06-09 14:51:50 -06003758 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3759 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3760 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3761 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003762
3763 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3764 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3765 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003766 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003767 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003768}
3769
3770void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3771 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003772 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003773 auto *cb_access_context = GetAccessContext(commandBuffer);
3774 assert(cb_access_context);
3775 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3776 auto *context = cb_access_context->GetCurrentAccessContext();
3777 assert(context);
3778
locke-lunarg61870c22020-06-09 14:51:50 -06003779 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3780 cb_access_context->RecordDrawSubpassAttachment(tag);
3781 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003782
3783 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3784 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3785 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003786 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003787}
3788
3789bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3790 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3791 uint32_t stride, const char *function) const {
3792 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003793 const auto *cb_access_context = GetAccessContext(commandBuffer);
3794 assert(cb_access_context);
3795 if (!cb_access_context) return skip;
3796
3797 const auto *context = cb_access_context->GetCurrentAccessContext();
3798 assert(context);
3799 if (!context) return skip;
3800
locke-lunarg61870c22020-06-09 14:51:50 -06003801 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3802 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3803 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3804 function);
3805 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003806
3807 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3808 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3809 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003810 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003811 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003812}
3813
3814bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3815 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3816 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003817 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3818 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003819}
3820
3821void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3822 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3823 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003824 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3825 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003826 auto *cb_access_context = GetAccessContext(commandBuffer);
3827 assert(cb_access_context);
3828 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3829 auto *context = cb_access_context->GetCurrentAccessContext();
3830 assert(context);
3831
locke-lunarg61870c22020-06-09 14:51:50 -06003832 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3833 cb_access_context->RecordDrawSubpassAttachment(tag);
3834 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3835 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003836
3837 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3838 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3839 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003840 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003841}
3842
3843bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3844 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3845 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003846 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3847 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003848}
3849
3850void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3851 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3852 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003853 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3854 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003855 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003856}
3857
3858bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3859 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3860 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003861 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3862 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003863}
3864
3865void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3866 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3867 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003868 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3869 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003870 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3871}
3872
3873bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3874 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3875 uint32_t stride, const char *function) const {
3876 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003877 const auto *cb_access_context = GetAccessContext(commandBuffer);
3878 assert(cb_access_context);
3879 if (!cb_access_context) return skip;
3880
3881 const auto *context = cb_access_context->GetCurrentAccessContext();
3882 assert(context);
3883 if (!context) return skip;
3884
locke-lunarg61870c22020-06-09 14:51:50 -06003885 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3886 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3887 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3888 stride, function);
3889 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003890
3891 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3892 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3893 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003894 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003895 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003896}
3897
3898bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3899 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3900 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003901 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3902 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003903}
3904
3905void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3906 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3907 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003908 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3909 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003910 auto *cb_access_context = GetAccessContext(commandBuffer);
3911 assert(cb_access_context);
3912 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3913 auto *context = cb_access_context->GetCurrentAccessContext();
3914 assert(context);
3915
locke-lunarg61870c22020-06-09 14:51:50 -06003916 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3917 cb_access_context->RecordDrawSubpassAttachment(tag);
3918 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3919 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003920
3921 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3922 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06003923 // We will update the index and vertex buffer in SubmitQueue in the future.
3924 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003925}
3926
3927bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3928 VkDeviceSize offset, VkBuffer countBuffer,
3929 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3930 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003931 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3932 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003933}
3934
3935void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3936 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3937 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003938 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3939 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003940 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3941}
3942
3943bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3944 VkDeviceSize offset, VkBuffer countBuffer,
3945 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3946 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003947 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3948 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003949}
3950
3951void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3952 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3953 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003954 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3955 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003956 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3957}
3958
3959bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3960 const VkClearColorValue *pColor, uint32_t rangeCount,
3961 const VkImageSubresourceRange *pRanges) const {
3962 bool skip = false;
3963 const auto *cb_access_context = GetAccessContext(commandBuffer);
3964 assert(cb_access_context);
3965 if (!cb_access_context) return skip;
3966
3967 const auto *context = cb_access_context->GetCurrentAccessContext();
3968 assert(context);
3969 if (!context) return skip;
3970
3971 const auto *image_state = Get<IMAGE_STATE>(image);
3972
3973 for (uint32_t index = 0; index < rangeCount; index++) {
3974 const auto &range = pRanges[index];
3975 if (image_state) {
3976 auto hazard =
3977 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3978 if (hazard.hazard) {
3979 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003980 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003981 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06003982 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003983 }
3984 }
3985 }
3986 return skip;
3987}
3988
3989void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3990 const VkClearColorValue *pColor, uint32_t rangeCount,
3991 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003992 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003993 auto *cb_access_context = GetAccessContext(commandBuffer);
3994 assert(cb_access_context);
3995 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
3996 auto *context = cb_access_context->GetCurrentAccessContext();
3997 assert(context);
3998
3999 const auto *image_state = Get<IMAGE_STATE>(image);
4000
4001 for (uint32_t index = 0; index < rangeCount; index++) {
4002 const auto &range = pRanges[index];
4003 if (image_state) {
4004 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4005 tag);
4006 }
4007 }
4008}
4009
4010bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4011 VkImageLayout imageLayout,
4012 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4013 const VkImageSubresourceRange *pRanges) const {
4014 bool skip = false;
4015 const auto *cb_access_context = GetAccessContext(commandBuffer);
4016 assert(cb_access_context);
4017 if (!cb_access_context) return skip;
4018
4019 const auto *context = cb_access_context->GetCurrentAccessContext();
4020 assert(context);
4021 if (!context) return skip;
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 auto hazard =
4029 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4030 if (hazard.hazard) {
4031 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004032 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004033 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004034 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004035 }
4036 }
4037 }
4038 return skip;
4039}
4040
4041void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4042 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4043 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004044 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004045 auto *cb_access_context = GetAccessContext(commandBuffer);
4046 assert(cb_access_context);
4047 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4048 auto *context = cb_access_context->GetCurrentAccessContext();
4049 assert(context);
4050
4051 const auto *image_state = Get<IMAGE_STATE>(image);
4052
4053 for (uint32_t index = 0; index < rangeCount; index++) {
4054 const auto &range = pRanges[index];
4055 if (image_state) {
4056 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4057 tag);
4058 }
4059 }
4060}
4061
4062bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4063 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4064 VkDeviceSize dstOffset, VkDeviceSize stride,
4065 VkQueryResultFlags flags) const {
4066 bool skip = false;
4067 const auto *cb_access_context = GetAccessContext(commandBuffer);
4068 assert(cb_access_context);
4069 if (!cb_access_context) return skip;
4070
4071 const auto *context = cb_access_context->GetCurrentAccessContext();
4072 assert(context);
4073 if (!context) return skip;
4074
4075 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4076
4077 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004078 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004079 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4080 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004081 skip |=
4082 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4083 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4084 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004085 }
4086 }
locke-lunargff255f92020-05-13 18:53:52 -06004087
4088 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004089 return skip;
4090}
4091
4092void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4093 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4094 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004095 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4096 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004097 auto *cb_access_context = GetAccessContext(commandBuffer);
4098 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004099 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004100 auto *context = cb_access_context->GetCurrentAccessContext();
4101 assert(context);
4102
4103 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4104
4105 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004106 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004107 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4108 }
locke-lunargff255f92020-05-13 18:53:52 -06004109
4110 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004111}
4112
4113bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4114 VkDeviceSize size, uint32_t data) const {
4115 bool skip = false;
4116 const auto *cb_access_context = GetAccessContext(commandBuffer);
4117 assert(cb_access_context);
4118 if (!cb_access_context) return skip;
4119
4120 const auto *context = cb_access_context->GetCurrentAccessContext();
4121 assert(context);
4122 if (!context) return skip;
4123
4124 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4125
4126 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004127 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004128 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4129 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004130 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004131 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004132 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004133 }
4134 }
4135 return skip;
4136}
4137
4138void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4139 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004140 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004141 auto *cb_access_context = GetAccessContext(commandBuffer);
4142 assert(cb_access_context);
4143 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4144 auto *context = cb_access_context->GetCurrentAccessContext();
4145 assert(context);
4146
4147 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4148
4149 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004150 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004151 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4152 }
4153}
4154
4155bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4156 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4157 const VkImageResolve *pRegions) const {
4158 bool skip = false;
4159 const auto *cb_access_context = GetAccessContext(commandBuffer);
4160 assert(cb_access_context);
4161 if (!cb_access_context) return skip;
4162
4163 const auto *context = cb_access_context->GetCurrentAccessContext();
4164 assert(context);
4165 if (!context) return skip;
4166
4167 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4168 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4169
4170 for (uint32_t region = 0; region < regionCount; region++) {
4171 const auto &resolve_region = pRegions[region];
4172 if (src_image) {
4173 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4174 resolve_region.srcOffset, resolve_region.extent);
4175 if (hazard.hazard) {
4176 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004177 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004178 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004179 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004180 }
4181 }
4182
4183 if (dst_image) {
4184 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4185 resolve_region.dstOffset, resolve_region.extent);
4186 if (hazard.hazard) {
4187 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004188 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004189 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004190 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004191 }
4192 if (skip) break;
4193 }
4194 }
4195
4196 return skip;
4197}
4198
4199void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4200 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4201 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004202 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4203 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004204 auto *cb_access_context = GetAccessContext(commandBuffer);
4205 assert(cb_access_context);
4206 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4207 auto *context = cb_access_context->GetCurrentAccessContext();
4208 assert(context);
4209
4210 auto *src_image = Get<IMAGE_STATE>(srcImage);
4211 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4212
4213 for (uint32_t region = 0; region < regionCount; region++) {
4214 const auto &resolve_region = pRegions[region];
4215 if (src_image) {
4216 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4217 resolve_region.srcOffset, resolve_region.extent, tag);
4218 }
4219 if (dst_image) {
4220 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4221 resolve_region.dstOffset, resolve_region.extent, tag);
4222 }
4223 }
4224}
4225
Jeff Leger178b1e52020-10-05 12:22:23 -04004226bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4227 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4228 bool skip = false;
4229 const auto *cb_access_context = GetAccessContext(commandBuffer);
4230 assert(cb_access_context);
4231 if (!cb_access_context) return skip;
4232
4233 const auto *context = cb_access_context->GetCurrentAccessContext();
4234 assert(context);
4235 if (!context) return skip;
4236
4237 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4238 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4239
4240 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4241 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4242 if (src_image) {
4243 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4244 resolve_region.srcOffset, resolve_region.extent);
4245 if (hazard.hazard) {
4246 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4247 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4248 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
4249 region, string_UsageTag(hazard).c_str());
4250 }
4251 }
4252
4253 if (dst_image) {
4254 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4255 resolve_region.dstOffset, resolve_region.extent);
4256 if (hazard.hazard) {
4257 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4258 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4259 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
4260 region, string_UsageTag(hazard).c_str());
4261 }
4262 if (skip) break;
4263 }
4264 }
4265
4266 return skip;
4267}
4268
4269void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4270 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4271 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4272 auto *cb_access_context = GetAccessContext(commandBuffer);
4273 assert(cb_access_context);
4274 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4275 auto *context = cb_access_context->GetCurrentAccessContext();
4276 assert(context);
4277
4278 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4279 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4280
4281 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4282 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4283 if (src_image) {
4284 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4285 resolve_region.srcOffset, resolve_region.extent, tag);
4286 }
4287 if (dst_image) {
4288 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4289 resolve_region.dstOffset, resolve_region.extent, tag);
4290 }
4291 }
4292}
4293
locke-lunarge1a67022020-04-29 00:15:36 -06004294bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4295 VkDeviceSize dataSize, const void *pData) const {
4296 bool skip = false;
4297 const auto *cb_access_context = GetAccessContext(commandBuffer);
4298 assert(cb_access_context);
4299 if (!cb_access_context) return skip;
4300
4301 const auto *context = cb_access_context->GetCurrentAccessContext();
4302 assert(context);
4303 if (!context) return skip;
4304
4305 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4306
4307 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004308 // VK_WHOLE_SIZE not allowed
4309 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004310 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4311 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004312 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004313 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004314 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004315 }
4316 }
4317 return skip;
4318}
4319
4320void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4321 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004322 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004323 auto *cb_access_context = GetAccessContext(commandBuffer);
4324 assert(cb_access_context);
4325 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4326 auto *context = cb_access_context->GetCurrentAccessContext();
4327 assert(context);
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 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4335 }
4336}
locke-lunargff255f92020-05-13 18:53:52 -06004337
4338bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4339 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4340 bool skip = false;
4341 const auto *cb_access_context = GetAccessContext(commandBuffer);
4342 assert(cb_access_context);
4343 if (!cb_access_context) return skip;
4344
4345 const auto *context = cb_access_context->GetCurrentAccessContext();
4346 assert(context);
4347 if (!context) return skip;
4348
4349 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4350
4351 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004352 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004353 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4354 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004355 skip |=
4356 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4357 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4358 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004359 }
4360 }
4361 return skip;
4362}
4363
4364void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4365 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004366 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004367 auto *cb_access_context = GetAccessContext(commandBuffer);
4368 assert(cb_access_context);
4369 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4370 auto *context = cb_access_context->GetCurrentAccessContext();
4371 assert(context);
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 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4378 }
4379}