blob: 3790e3047f3c99dc6eb0602c48466d4e4197b13a [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 } else {
476 src_external_ = TrackBack();
477 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600478 if (subpass_dep.barrier_to_external.size()) {
479 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulaufe5da6e52020-03-18 15:32:18 -0600480 } else {
481 dst_external_ = TrackBack();
John Zulauf3d84f1b2020-03-09 13:33:25 -0600482 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700483}
484
John Zulauf5f13a792020-03-10 07:31:21 -0600485template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600486HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600487 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600488 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600489 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600490
491 HazardResult hazard;
492 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
493 hazard = detector.Detect(prev);
494 }
495 return hazard;
496}
497
John Zulauf3d84f1b2020-03-09 13:33:25 -0600498// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
499// the DAG of the contexts (for example subpasses)
500template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600501HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
502 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600503 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600504
John Zulauf1a224292020-06-30 14:52:13 -0600505 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600506 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
507 // so we'll check these first
508 for (const auto &async_context : async_) {
509 hazard = async_context->DetectAsyncHazard(type, detector, range);
510 if (hazard.hazard) return hazard;
511 }
John Zulauf5f13a792020-03-10 07:31:21 -0600512 }
513
John Zulauf1a224292020-06-30 14:52:13 -0600514 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600515
John Zulauf69133422020-05-20 14:55:53 -0600516 const auto &accesses = GetAccessStateMap(type);
517 const auto from = accesses.lower_bound(range);
518 const auto to = accesses.upper_bound(range);
519 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600520
John Zulauf69133422020-05-20 14:55:53 -0600521 for (auto pos = from; pos != to; ++pos) {
522 // Cover any leading gap, or gap between entries
523 if (detect_prev) {
524 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
525 // Cover any leading gap, or gap between entries
526 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600527 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600528 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600529 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600530 if (hazard.hazard) return hazard;
531 }
John Zulauf69133422020-05-20 14:55:53 -0600532 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
533 gap.begin = pos->first.end;
534 }
535
536 hazard = detector.Detect(pos);
537 if (hazard.hazard) return hazard;
538 }
539
540 if (detect_prev) {
541 // Detect in the trailing empty as needed
542 gap.end = range.end;
543 if (gap.non_empty()) {
544 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600545 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600546 }
547
548 return hazard;
549}
550
551// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
552template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600553HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600554 auto &accesses = GetAccessStateMap(type);
555 const auto from = accesses.lower_bound(range);
556 const auto to = accesses.upper_bound(range);
557
John Zulauf3d84f1b2020-03-09 13:33:25 -0600558 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600559 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
560 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600561 }
John Zulauf16adfc92020-04-08 10:28:33 -0600562
John Zulauf3d84f1b2020-03-09 13:33:25 -0600563 return hazard;
564}
565
John Zulauf355e49b2020-04-24 15:11:15 -0600566// Returns the last resolved entry
567static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
568 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
569 const SyncBarrier *barrier) {
570 auto at = entry;
571 for (auto pos = first; pos != last; ++pos) {
572 // Every member of the input iterator range must fit within the remaining portion of entry
573 assert(at->first.includes(pos->first));
574 assert(at != dest->end());
575 // Trim up at to the same size as the entry to resolve
576 at = sparse_container::split(at, *dest, pos->first);
577 auto access = pos->second;
578 if (barrier) {
579 access.ApplyBarrier(*barrier);
580 }
581 at->second.Resolve(access);
582 ++at; // Go to the remaining unused section of entry
583 }
584}
585
586void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, const SyncBarrier *barrier,
587 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
588 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600589 if (!range.non_empty()) return;
590
John Zulauf355e49b2020-04-24 15:11:15 -0600591 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
592 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600593 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600594 if (current->pos_B->valid) {
595 const auto &src_pos = current->pos_B->lower_bound;
John Zulauf355e49b2020-04-24 15:11:15 -0600596 auto access = src_pos->second;
597 if (barrier) {
598 access.ApplyBarrier(*barrier);
599 }
John Zulauf16adfc92020-04-08 10:28:33 -0600600 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600601 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
602 trimmed->second.Resolve(access);
603 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600604 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600605 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600606 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600607 }
John Zulauf16adfc92020-04-08 10:28:33 -0600608 } else {
609 // we have to descend to fill this gap
610 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600611 if (current->pos_A->valid) {
612 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
613 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600614 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulauf355e49b2020-04-24 15:11:15 -0600615 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier);
616 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600617 // There isn't anything in dest in current)range, so we can accumulate directly into it.
618 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulauf355e49b2020-04-24 15:11:15 -0600619 if (barrier) {
620 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
John Zulauf3bcab5e2020-06-19 14:42:32 -0600621 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
John Zulauf355e49b2020-04-24 15:11:15 -0600622 pos->second.ApplyBarrier(*barrier);
623 }
624 }
625 }
626 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
627 // iterator of the outer while.
628
629 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
630 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
631 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600632 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
633 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600634 current.seek(seek_to);
635 } else if (!current->pos_A->valid && infill_state) {
636 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
637 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
638 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600639 }
John Zulauf5f13a792020-03-10 07:31:21 -0600640 }
John Zulauf16adfc92020-04-08 10:28:33 -0600641 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600642 }
John Zulauf1a224292020-06-30 14:52:13 -0600643
644 // Infill if range goes passed both the current and resolve map prior contents
645 if (recur_to_infill && (current->range.end < range.end)) {
646 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
647 ResourceAccessRangeMap gap_map;
648 const auto the_end = resolve_map->end();
649 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
650 for (auto &access : gap_map) {
651 access.second.ApplyBarrier(*barrier);
652 resolve_map->insert(the_end, access);
653 }
654 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600655}
656
John Zulauf355e49b2020-04-24 15:11:15 -0600657void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
658 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600659 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600660 if (range.non_empty() && infill_state) {
661 descent_map->insert(std::make_pair(range, *infill_state));
662 }
663 } else {
664 // Look for something to fill the gap further along.
665 for (const auto &prev_dep : prev_) {
John Zulauf355e49b2020-04-24 15:11:15 -0600666 prev_dep.context->ResolveAccessRange(type, range, &prev_dep.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600667 }
668
John Zulaufe5da6e52020-03-18 15:32:18 -0600669 if (src_external_.context) {
John Zulauf355e49b2020-04-24 15:11:15 -0600670 src_external_.context->ResolveAccessRange(type, range, &src_external_.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600671 }
672 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600673}
674
John Zulauf16adfc92020-04-08 10:28:33 -0600675AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600676 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600677}
678
679VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) {
680 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
681}
682
John Zulauf355e49b2020-04-24 15:11:15 -0600683static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
John Zulauf16adfc92020-04-08 10:28:33 -0600684
John Zulauf1507ee42020-05-18 11:33:09 -0600685static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
686 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
687 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
688 return stage_access;
689}
690static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
691 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
692 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
693 return stage_access;
694}
695
John Zulauf7635de32020-05-29 17:14:15 -0600696// Caller must manage returned pointer
697static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
698 uint32_t subpass, const VkRect2D &render_area,
699 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
700 auto *proxy = new AccessContext(context);
701 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600702 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600703 return proxy;
704}
705
John Zulauf540266b2020-04-06 18:54:53 -0600706void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
John Zulauf355e49b2020-04-24 15:11:15 -0600707 AddressType address_type, ResourceAccessRangeMap *descent_map,
708 const ResourceAccessState *infill_state) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600709 if (!SimpleBinding(image_state)) return;
710
John Zulauf62f10592020-04-03 12:20:02 -0600711 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
locke-lunargae26eac2020-04-16 15:29:05 -0600712 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600713 image_state.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600714 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf62f10592020-04-03 12:20:02 -0600715 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600716 ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state);
John Zulauf62f10592020-04-03 12:20:02 -0600717 }
718}
719
John Zulauf7635de32020-05-29 17:14:15 -0600720// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600721bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600722 const VkRect2D &render_area, uint32_t subpass,
723 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
724 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600725 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600726 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
727 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
728 // those affects have not been recorded yet.
729 //
730 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
731 // to apply and only copy then, if this proves a hot spot.
732 std::unique_ptr<AccessContext> proxy_for_prev;
733 TrackBack proxy_track_back;
734
John Zulauf355e49b2020-04-24 15:11:15 -0600735 const auto &transitions = rp_state.subpass_transitions[subpass];
736 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600737 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
738
739 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
740 if (prev_needs_proxy) {
741 if (!proxy_for_prev) {
742 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
743 render_area, attachment_views));
744 proxy_track_back = *track_back;
745 proxy_track_back.context = proxy_for_prev.get();
746 }
747 track_back = &proxy_track_back;
748 }
749 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600750 if (hazard.hazard) {
John Zulauf389c34b2020-07-28 11:19:35 -0600751 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
752 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
753 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
754 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
755 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
756 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600757 }
758 }
759 return skip;
760}
761
John Zulauf1507ee42020-05-18 11:33:09 -0600762bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600763 const VkRect2D &render_area, uint32_t subpass,
764 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
765 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600766 bool skip = false;
767 const auto *attachment_ci = rp_state.createInfo.pAttachments;
768 VkExtent3D extent = CastTo3D(render_area.extent);
769 VkOffset3D offset = CastTo3D(render_area.offset);
770 const auto external_access_scope = src_external_.barrier.dst_access_scope;
John Zulauf1507ee42020-05-18 11:33:09 -0600771
772 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
773 if (subpass == rp_state.attachment_first_subpass[i]) {
774 if (attachment_views[i] == nullptr) continue;
775 const IMAGE_VIEW_STATE &view = *attachment_views[i];
776 const IMAGE_STATE *image = view.image_state.get();
777 if (image == nullptr) continue;
778 const auto &ci = attachment_ci[i];
779 const bool is_transition = rp_state.attachment_first_is_transition[i];
780
781 // Need check in the following way
782 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
783 // vs. transition
784 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
785 // for each aspect loaded.
786
787 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600788 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600789 const bool is_color = !(has_depth || has_stencil);
790
791 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
792 const SyncStageAccessFlags load_mask = (has_depth || is_color) ? SyncStageAccess::Flags(load_index) : 0U;
793 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
794 const SyncStageAccessFlags stencil_mask = has_stencil ? SyncStageAccess::Flags(stencil_load_index) : 0U;
795
John Zulaufaff20662020-06-01 14:07:58 -0600796 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600797 const char *aspect = nullptr;
798 if (is_transition) {
799 // For transition w
800 SyncHazard transition_hazard = SyncHazard::NONE;
801 bool checked_stencil = false;
802 if (load_mask) {
803 if ((load_mask & external_access_scope) != load_mask) {
804 transition_hazard =
805 SyncStageAccess::HasWrite(load_mask) ? SyncHazard::WRITE_AFTER_WRITE : SyncHazard::READ_AFTER_WRITE;
806 aspect = is_color ? "color" : "depth";
807 }
808 if (!transition_hazard && stencil_mask) {
809 if ((stencil_mask & external_access_scope) != stencil_mask) {
810 transition_hazard = SyncStageAccess::HasWrite(stencil_mask) ? SyncHazard::WRITE_AFTER_WRITE
811 : SyncHazard::READ_AFTER_WRITE;
812 aspect = "stencil";
813 checked_stencil = true;
814 }
815 }
816 }
817 if (transition_hazard) {
818 // Hazard vs. ILT
819 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
820 skip |=
locke-lunarg54379cf2020-08-05 12:25:29 -0600821 sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(transition_hazard),
John Zulauf1507ee42020-05-18 11:33:09 -0600822 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
823 " aspect %s during load with loadOp %s.",
824 func_name, string_SyncHazard(transition_hazard), subpass, i, aspect, load_op_string);
825 }
826 } else {
827 auto hazard_range = view.normalized_subresource_range;
828 bool checked_stencil = false;
829 if (is_color) {
830 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, offset, extent);
831 aspect = "color";
832 } else {
833 if (has_depth) {
834 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
835 hazard = DetectHazard(*image, load_index, hazard_range, offset, extent);
836 aspect = "depth";
837 }
838 if (!hazard.hazard && has_stencil) {
839 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
840 hazard = DetectHazard(*image, stencil_load_index, hazard_range, offset, extent);
841 aspect = "stencil";
842 checked_stencil = true;
843 }
844 }
845
846 if (hazard.hazard) {
847 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
848 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
849 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600850 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -0600851 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600852 string_UsageTag(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -0600853 }
854 }
855 }
856 }
857 return skip;
858}
859
John Zulaufaff20662020-06-01 14:07:58 -0600860// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
861// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
862// store is part of the same Next/End operation.
863// The latter is handled in layout transistion validation directly
864bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
865 const VkRect2D &render_area, uint32_t subpass,
866 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
867 const char *func_name) const {
868 bool skip = false;
869 const auto *attachment_ci = rp_state.createInfo.pAttachments;
870 VkExtent3D extent = CastTo3D(render_area.extent);
871 VkOffset3D offset = CastTo3D(render_area.offset);
872
873 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
874 if (subpass == rp_state.attachment_last_subpass[i]) {
875 if (attachment_views[i] == nullptr) continue;
876 const IMAGE_VIEW_STATE &view = *attachment_views[i];
877 const IMAGE_STATE *image = view.image_state.get();
878 if (image == nullptr) continue;
879 const auto &ci = attachment_ci[i];
880
881 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
882 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
883 // sake, we treat DONT_CARE as writing.
884 const bool has_depth = FormatHasDepth(ci.format);
885 const bool has_stencil = FormatHasStencil(ci.format);
886 const bool is_color = !(has_depth || has_stencil);
887 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
888 if (!has_stencil && !store_op_stores) continue;
889
890 HazardResult hazard;
891 const char *aspect = nullptr;
892 bool checked_stencil = false;
893 if (is_color) {
894 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
895 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
896 aspect = "color";
897 } else {
898 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
899 auto hazard_range = view.normalized_subresource_range;
900 if (has_depth && store_op_stores) {
901 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
902 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
903 kAttachmentRasterOrder, offset, extent);
904 aspect = "depth";
905 }
906 if (!hazard.hazard && has_stencil && stencil_op_stores) {
907 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
908 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
909 kAttachmentRasterOrder, offset, extent);
910 aspect = "stencil";
911 checked_stencil = true;
912 }
913 }
914
915 if (hazard.hazard) {
916 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
917 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -0600918 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
919 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600920 " %s aspect during store with %s %s. Access info %s",
John Zulauf1dae9192020-06-16 15:46:44 -0600921 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600922 store_op_string, string_UsageTag(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -0600923 }
924 }
925 }
926 return skip;
927}
928
John Zulaufb027cdb2020-05-21 14:25:22 -0600929bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
930 const VkRect2D &render_area,
931 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
932 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600933 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
934 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
935 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600936}
937
John Zulauf3d84f1b2020-03-09 13:33:25 -0600938class HazardDetector {
939 SyncStageAccessIndex usage_index_;
940
941 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600942 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600943 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
944 return pos->second.DetectAsyncHazard(usage_index_);
945 }
946 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
947};
948
John Zulauf69133422020-05-20 14:55:53 -0600949class HazardDetectorWithOrdering {
950 const SyncStageAccessIndex usage_index_;
951 const SyncOrderingBarrier &ordering_;
952
953 public:
954 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
955 return pos->second.DetectHazard(usage_index_, ordering_);
956 }
957 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
958 return pos->second.DetectAsyncHazard(usage_index_);
959 }
960 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
961 : usage_index_(usage), ordering_(ordering) {}
962};
963
John Zulauf16adfc92020-04-08 10:28:33 -0600964HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600965 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600966 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600967 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600968}
969
John Zulauf16adfc92020-04-08 10:28:33 -0600970HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600971 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600972 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -0600973 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -0600974}
975
John Zulauf69133422020-05-20 14:55:53 -0600976template <typename Detector>
977HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
978 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
979 const VkExtent3D &extent, DetectOptions options) const {
980 if (!SimpleBinding(image)) return HazardResult();
981 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
982 const auto address_type = ImageAddressType(image);
983 const auto base_address = ResourceBaseAddress(image);
984 for (; range_gen->non_empty(); ++range_gen) {
985 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
986 if (hazard.hazard) return hazard;
987 }
988 return HazardResult();
989}
990
John Zulauf540266b2020-04-06 18:54:53 -0600991HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
992 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
993 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700994 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
995 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -0600996 return DetectHazard(image, current_usage, subresource_range, offset, extent);
997}
998
999HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1000 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1001 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -06001002 HazardDetector detector(current_usage);
1003 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
1004}
1005
1006HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1007 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
1008 const VkOffset3D &offset, const VkExtent3D &extent) const {
1009 HazardDetectorWithOrdering detector(current_usage, ordering);
1010 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001011}
1012
John Zulaufb027cdb2020-05-21 14:25:22 -06001013// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
1014// should have reported the issue regarding an invalid attachment entry
1015HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
1016 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
1017 VkImageAspectFlags aspect_mask) const {
1018 if (view != nullptr) {
1019 const IMAGE_STATE *image = view->image_state.get();
1020 if (image != nullptr) {
1021 auto *detect_range = &view->normalized_subresource_range;
1022 VkImageSubresourceRange masked_range;
1023 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1024 masked_range = view->normalized_subresource_range;
1025 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1026 detect_range = &masked_range;
1027 }
1028
1029 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1030 if (detect_range->aspectMask) {
1031 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
1032 }
1033 }
1034 }
1035 return HazardResult();
1036}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001037class BarrierHazardDetector {
1038 public:
1039 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1040 SyncStageAccessFlags src_access_scope)
1041 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1042
John Zulauf5f13a792020-03-10 07:31:21 -06001043 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1044 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001045 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001046 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
1047 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1048 return pos->second.DetectAsyncHazard(usage_index_);
1049 }
1050
1051 private:
1052 SyncStageAccessIndex usage_index_;
1053 VkPipelineStageFlags src_exec_scope_;
1054 SyncStageAccessFlags src_access_scope_;
1055};
1056
John Zulauf16adfc92020-04-08 10:28:33 -06001057HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -06001058 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001059 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001060 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -06001061 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001062}
1063
John Zulauf16adfc92020-04-08 10:28:33 -06001064HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001065 SyncStageAccessFlags src_access_scope,
1066 const VkImageSubresourceRange &subresource_range,
1067 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001068 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1069 VkOffset3D zero_offset = {0, 0, 0};
1070 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001071}
1072
John Zulauf355e49b2020-04-24 15:11:15 -06001073HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1074 SyncStageAccessFlags src_stage_accesses,
1075 const VkImageMemoryBarrier &barrier) const {
1076 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1077 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1078 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1079}
1080
John Zulauf9cb530d2019-09-30 14:14:10 -06001081template <typename Flags, typename Map>
1082SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1083 SyncStageAccessFlags scope = 0;
1084 for (const auto &bit_scope : map) {
1085 if (flag_mask < bit_scope.first) break;
1086
1087 if (flag_mask & bit_scope.first) {
1088 scope |= bit_scope.second;
1089 }
1090 }
1091 return scope;
1092}
1093
1094SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
1095 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1096}
1097
1098SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
1099 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
1100}
1101
1102// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
1103SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001104 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1105 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1106 // 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 -06001107 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1108}
1109
1110template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001111void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001112 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1113 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -06001114 auto pos = accesses->lower_bound(range);
1115 if (pos == accesses->end() || !pos->first.intersects(range)) {
1116 // The range is empty, fill it with a default value.
1117 pos = action.Infill(accesses, pos, range);
1118 } else if (range.begin < pos->first.begin) {
1119 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001120 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001121 } else if (pos->first.begin < range.begin) {
1122 // Trim the beginning if needed
1123 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1124 ++pos;
1125 }
1126
1127 const auto the_end = accesses->end();
1128 while ((pos != the_end) && pos->first.intersects(range)) {
1129 if (pos->first.end > range.end) {
1130 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1131 }
1132
1133 pos = action(accesses, pos);
1134 if (pos == the_end) break;
1135
1136 auto next = pos;
1137 ++next;
1138 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1139 // Need to infill if next is disjoint
1140 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001141 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001142 next = action.Infill(accesses, next, new_range);
1143 }
1144 pos = next;
1145 }
1146}
1147
1148struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001149 using Iterator = ResourceAccessRangeMap::iterator;
1150 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001151 // this is only called on gaps, and never returns a gap.
1152 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001153 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001154 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001155 }
John Zulauf5f13a792020-03-10 07:31:21 -06001156
John Zulauf5c5e88d2019-12-26 11:22:02 -07001157 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001158 auto &access_state = pos->second;
1159 access_state.Update(usage, tag);
1160 return pos;
1161 }
1162
John Zulauf16adfc92020-04-08 10:28:33 -06001163 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001164 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001165 : type(type_), context(context_), usage(usage_), tag(tag_) {}
1166 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001167 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001168 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001169 const ResourceUsageTag &tag;
1170};
1171
1172struct ApplyMemoryAccessBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001173 using Iterator = ResourceAccessRangeMap::iterator;
1174 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001175
John Zulauf5c5e88d2019-12-26 11:22:02 -07001176 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001177 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001178 access_state.ApplyMemoryAccessBarrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001179 return pos;
1180 }
1181
John Zulauf36bcf6a2020-02-03 15:12:52 -07001182 ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_,
1183 VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_)
1184 : src_exec_scope(src_exec_scope_),
1185 src_access_scope(src_access_scope_),
1186 dst_exec_scope(dst_exec_scope_),
1187 dst_access_scope(dst_access_scope_) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001188
John Zulauf36bcf6a2020-02-03 15:12:52 -07001189 VkPipelineStageFlags src_exec_scope;
1190 SyncStageAccessFlags src_access_scope;
1191 VkPipelineStageFlags dst_exec_scope;
1192 SyncStageAccessFlags dst_access_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001193};
1194
1195struct ApplyGlobalBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001196 using Iterator = ResourceAccessRangeMap::iterator;
1197 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001198
John Zulauf5c5e88d2019-12-26 11:22:02 -07001199 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001200 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001201 access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001202
1203 for (const auto &functor : barrier_functor) {
1204 functor(accesses, pos);
1205 }
1206 return pos;
1207 }
1208
John Zulauf36bcf6a2020-02-03 15:12:52 -07001209 ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope,
1210 SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses,
John Zulauf9cb530d2019-09-30 14:14:10 -06001211 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001212 : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001213 // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier...
1214 barrier_functor.reserve(memoryBarrierCount);
1215 for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) {
1216 const auto &barrier = pMemoryBarriers[barrier_index];
John Zulauf36bcf6a2020-02-03 15:12:52 -07001217 barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask),
1218 dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
John Zulauf9cb530d2019-09-30 14:14:10 -06001219 }
1220 }
1221
John Zulauf36bcf6a2020-02-03 15:12:52 -07001222 const VkPipelineStageFlags src_exec_scope;
1223 const VkPipelineStageFlags dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001224 std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor;
1225};
1226
John Zulauf355e49b2020-04-24 15:11:15 -06001227void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1228 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001229 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1230 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001231}
1232
John Zulauf16adfc92020-04-08 10:28:33 -06001233void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001234 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001235 if (!SimpleBinding(buffer)) return;
1236 const auto base_address = ResourceBaseAddress(buffer);
1237 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1238}
John Zulauf355e49b2020-04-24 15:11:15 -06001239
John Zulauf540266b2020-04-06 18:54:53 -06001240void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001241 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001242 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001243 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001244 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001245 const auto address_type = ImageAddressType(image);
1246 const auto base_address = ResourceBaseAddress(image);
1247 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001248 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001249 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001250 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001251}
John Zulauf7635de32020-05-29 17:14:15 -06001252void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1253 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1254 if (view != nullptr) {
1255 const IMAGE_STATE *image = view->image_state.get();
1256 if (image != nullptr) {
1257 auto *update_range = &view->normalized_subresource_range;
1258 VkImageSubresourceRange masked_range;
1259 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1260 masked_range = view->normalized_subresource_range;
1261 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1262 update_range = &masked_range;
1263 }
1264 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1265 }
1266 }
1267}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001268
John Zulauf355e49b2020-04-24 15:11:15 -06001269void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1270 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1271 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001272 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1273 subresource.layerCount};
1274 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1275}
1276
John Zulauf540266b2020-04-06 18:54:53 -06001277template <typename Action>
1278void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001279 if (!SimpleBinding(buffer)) return;
1280 const auto base_address = ResourceBaseAddress(buffer);
1281 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001282}
1283
1284template <typename Action>
1285void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1286 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001287 if (!SimpleBinding(image)) return;
1288 const auto address_type = ImageAddressType(image);
1289 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001290
locke-lunargae26eac2020-04-16 15:29:05 -06001291 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001292 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001293
John Zulauf16adfc92020-04-08 10:28:33 -06001294 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001295 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001296 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001297 }
1298}
1299
John Zulauf7635de32020-05-29 17:14:15 -06001300void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1301 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1302 const ResourceUsageTag &tag) {
1303 UpdateStateResolveAction update(*this, tag);
1304 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1305}
1306
John Zulaufaff20662020-06-01 14:07:58 -06001307void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1308 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1309 const ResourceUsageTag &tag) {
1310 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1311 VkExtent3D extent = CastTo3D(render_area.extent);
1312 VkOffset3D offset = CastTo3D(render_area.offset);
1313
1314 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1315 if (rp_state.attachment_last_subpass[i] == subpass) {
1316 if (attachment_views[i] == nullptr) continue; // UNUSED
1317 const auto &view = *attachment_views[i];
1318 const IMAGE_STATE *image = view.image_state.get();
1319 if (image == nullptr) continue;
1320
1321 const auto &ci = attachment_ci[i];
1322 const bool has_depth = FormatHasDepth(ci.format);
1323 const bool has_stencil = FormatHasStencil(ci.format);
1324 const bool is_color = !(has_depth || has_stencil);
1325 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1326
1327 if (is_color && store_op_stores) {
1328 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1329 offset, extent, tag);
1330 } else {
1331 auto update_range = view.normalized_subresource_range;
1332 if (has_depth && store_op_stores) {
1333 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1334 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1335 tag);
1336 }
1337 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1338 if (has_stencil && stencil_op_stores) {
1339 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1340 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1341 tag);
1342 }
1343 }
1344 }
1345 }
1346}
1347
John Zulauf540266b2020-04-06 18:54:53 -06001348template <typename Action>
1349void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1350 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001351 for (const auto address_type : kAddressTypes) {
1352 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001353 }
1354}
1355
1356void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001357 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1358 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -06001359 for (const auto address_type : kAddressTypes) {
John Zulauf355e49b2020-04-24 15:11:15 -06001360 context.ResolveAccessRange(address_type, full_range, &context.GetDstExternalTrackBack().barrier,
1361 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001362 }
1363 }
1364}
1365
John Zulauf355e49b2020-04-24 15:11:15 -06001366void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1367 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1368 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) {
1369 const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1370 UpdateMemoryAccess(image, subresource_range, barrier_action);
1371}
1372
John Zulauf7635de32020-05-29 17:14:15 -06001373// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001374void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1375 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1376 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range,
1377 bool layout_transition, const ResourceUsageTag &tag) {
1378 if (layout_transition) {
1379 UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent,
1380 tag);
1381 ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope,
1382 subresource_range);
John Zulaufc9201222020-05-13 15:13:03 -06001383 } else {
1384 ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range);
John Zulauf355e49b2020-04-24 15:11:15 -06001385 }
John Zulauf355e49b2020-04-24 15:11:15 -06001386}
1387
John Zulauf7635de32020-05-29 17:14:15 -06001388// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001389void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier,
1390 const VkImageSubresourceRange &subresource_range, bool layout_transition,
1391 const ResourceUsageTag &tag) {
1392 ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope,
1393 subresource_range, layout_transition, tag);
1394}
1395
1396// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001397HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001398 if (!attach_view) return HazardResult();
1399 const auto image_state = attach_view->image_state.get();
1400 if (!image_state) return HazardResult();
1401
John Zulauf355e49b2020-04-24 15:11:15 -06001402 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001403 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001404
1405 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulauf7635de32020-05-29 17:14:15 -06001406 auto hazard = track_back.context->DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope,
1407 track_back.barrier.src_access_scope,
1408 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001409 if (!hazard.hazard) {
1410 // The Async hazard check is against the current context's async set.
John Zulauf7635de32020-05-29 17:14:15 -06001411 hazard = DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope, track_back.barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001412 attach_view->normalized_subresource_range, kDetectAsync);
1413 }
1414 return hazard;
1415}
1416
1417// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1418bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1419
1420 const VkRenderPassBeginInfo *pRenderPassBegin,
1421 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1422 const char *func_name) const {
1423 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1424 bool skip = false;
1425 uint32_t subpass = 0;
1426 const auto &transitions = rp_state.subpass_transitions[subpass];
1427 if (transitions.size()) {
1428 const std::vector<AccessContext> empty_context_vector;
1429 // Create context we can use to validate against...
1430 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1431 const_cast<AccessContext *>(&cb_access_context_));
1432
1433 assert(pRenderPassBegin);
1434 if (nullptr == pRenderPassBegin) return skip;
1435
1436 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1437 assert(fb_state);
1438 if (nullptr == fb_state) return skip;
1439
1440 // Create a limited array of views (which we'll need to toss
1441 std::vector<const IMAGE_VIEW_STATE *> views;
1442 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
1443 const auto attachment_count = count_attachment.first;
1444 const auto *attachments = count_attachment.second;
1445 views.resize(attachment_count, nullptr);
1446 for (const auto &transition : transitions) {
1447 assert(transition.attachment < attachment_count);
1448 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
1449 }
1450
John Zulauf7635de32020-05-29 17:14:15 -06001451 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
1452 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001453 }
1454 return skip;
1455}
1456
locke-lunarg61870c22020-06-09 14:51:50 -06001457bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1458 const char *func_name) const {
1459 bool skip = false;
1460 const PIPELINE_STATE *pPipe = nullptr;
1461 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1462 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1463 if (!pPipe || !per_sets) {
1464 return skip;
1465 }
1466
1467 using DescriptorClass = cvdescriptorset::DescriptorClass;
1468 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1469 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1470 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1471 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1472
1473 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarg37047832020-06-12 13:44:45 -06001474 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001475 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1476 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001477 for (const auto &set_binding : stage_state.descriptor_uses) {
1478 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1479 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1480 set_binding.first.second);
1481 const auto descriptor_type = binding_it.GetType();
1482 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1483 auto array_idx = 0;
1484
1485 if (binding_it.IsVariableDescriptorCount()) {
1486 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1487 }
1488 SyncStageAccessIndex sync_index =
1489 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1490
1491 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1492 uint32_t index = i - index_range.start;
1493 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1494 switch (descriptor->GetClass()) {
1495 case DescriptorClass::ImageSampler:
1496 case DescriptorClass::Image: {
1497 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001498 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001499 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001500 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1501 img_view_state = image_sampler_descriptor->GetImageViewState();
1502 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001503 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001504 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1505 img_view_state = image_descriptor->GetImageViewState();
1506 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001507 }
1508 if (!img_view_state) continue;
1509 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1510 VkExtent3D extent = {};
1511 VkOffset3D offset = {};
1512 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1513 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1514 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1515 } else {
1516 extent = img_state->createInfo.extent;
1517 }
John Zulauf361fb532020-07-22 10:45:39 -06001518 HazardResult hazard;
1519 const auto &subresource_range = img_view_state->normalized_subresource_range;
1520 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1521 // Input attachments are subject to raster ordering rules
1522 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
1523 kAttachmentRasterOrder, offset, extent);
1524 } else {
1525 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1526 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001527 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001528 skip |= sync_state_->LogError(
1529 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001530 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1531 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001532 func_name, string_SyncHazard(hazard.hazard),
1533 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1534 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1535 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001536 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1537 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1538 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001539 }
1540 break;
1541 }
1542 case DescriptorClass::TexelBuffer: {
1543 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1544 if (!buf_view_state) continue;
1545 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001546 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001547 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001548 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001549 skip |= sync_state_->LogError(
1550 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001551 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1552 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001553 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1554 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1555 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001556 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1557 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1558 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001559 }
1560 break;
1561 }
1562 case DescriptorClass::GeneralBuffer: {
1563 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1564 auto buf_state = buffer_descriptor->GetBufferState();
1565 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001566 const ResourceAccessRange range =
1567 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001568 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001569 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001570 skip |= sync_state_->LogError(
1571 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001572 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1573 func_name, string_SyncHazard(hazard.hazard),
1574 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001575 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1576 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001577 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1578 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1579 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001580 }
1581 break;
1582 }
1583 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1584 default:
1585 break;
1586 }
1587 }
1588 }
1589 }
1590 return skip;
1591}
1592
1593void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1594 const ResourceUsageTag &tag) {
1595 const PIPELINE_STATE *pPipe = nullptr;
1596 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1597 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1598 if (!pPipe || !per_sets) {
1599 return;
1600 }
1601
1602 using DescriptorClass = cvdescriptorset::DescriptorClass;
1603 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1604 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1605 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1606 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1607
1608 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001609 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
1610 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1611 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001612 for (const auto &set_binding : stage_state.descriptor_uses) {
1613 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1614 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1615 set_binding.first.second);
1616 const auto descriptor_type = binding_it.GetType();
1617 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1618 auto array_idx = 0;
1619
1620 if (binding_it.IsVariableDescriptorCount()) {
1621 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1622 }
1623 SyncStageAccessIndex sync_index =
1624 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1625
1626 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1627 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1628 switch (descriptor->GetClass()) {
1629 case DescriptorClass::ImageSampler:
1630 case DescriptorClass::Image: {
1631 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1632 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1633 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1634 } else {
1635 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1636 }
1637 if (!img_view_state) continue;
1638 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1639 VkExtent3D extent = {};
1640 VkOffset3D offset = {};
1641 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1642 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1643 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1644 } else {
1645 extent = img_state->createInfo.extent;
1646 }
1647 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1648 offset, extent, tag);
1649 break;
1650 }
1651 case DescriptorClass::TexelBuffer: {
1652 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1653 if (!buf_view_state) continue;
1654 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001655 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001656 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1657 break;
1658 }
1659 case DescriptorClass::GeneralBuffer: {
1660 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1661 auto buf_state = buffer_descriptor->GetBufferState();
1662 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001663 const ResourceAccessRange range =
1664 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001665 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1666 break;
1667 }
1668 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1669 default:
1670 break;
1671 }
1672 }
1673 }
1674 }
1675}
1676
1677bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1678 bool skip = false;
1679 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1680 if (!pPipe) {
1681 return skip;
1682 }
1683
1684 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1685 const auto &binding_buffers_size = binding_buffers.size();
1686 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1687
1688 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1689 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1690 if (binding_description.binding < binding_buffers_size) {
1691 const auto &binding_buffer = binding_buffers[binding_description.binding];
1692 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1693
1694 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06001695 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1696 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001697 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1698 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001699 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001700 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 -06001701 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001702 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001703 }
1704 }
1705 }
1706 return skip;
1707}
1708
1709void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
1710 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1711 if (!pPipe) {
1712 return;
1713 }
1714 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1715 const auto &binding_buffers_size = binding_buffers.size();
1716 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1717
1718 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1719 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1720 if (binding_description.binding < binding_buffers_size) {
1721 const auto &binding_buffer = binding_buffers[binding_description.binding];
1722 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1723
1724 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06001725 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1726 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001727 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1728 }
1729 }
1730}
1731
1732bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1733 bool skip = false;
1734 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
1735
1736 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1737 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001738 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1739 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001740 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1741 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001742 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001743 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 -06001744 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001745 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001746 }
1747
1748 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1749 // We will detect more accurate range in the future.
1750 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1751 return skip;
1752}
1753
1754void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
1755 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return;
1756
1757 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1758 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001759 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1760 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001761 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1762
1763 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1764 // We will detect more accurate range in the future.
1765 RecordDrawVertex(UINT32_MAX, 0, tag);
1766}
1767
1768bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06001769 bool skip = false;
1770 if (!current_renderpass_context_) return skip;
1771 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1772 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1773 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001774}
1775
1776void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
locke-lunarg7077d502020-06-18 21:37:26 -06001777 if (current_renderpass_context_)
1778 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1779 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001780}
1781
John Zulauf355e49b2020-04-24 15:11:15 -06001782bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001783 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001784 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06001785 skip |=
1786 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001787
1788 return skip;
1789}
1790
1791bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1792 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001793 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001794 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001795 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06001796 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1797 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001798
1799 return skip;
1800}
1801
1802void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1803 assert(sync_state_);
1804 if (!cb_state_) return;
1805
1806 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06001807 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06001808 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06001809 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001810 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001811}
1812
John Zulauf355e49b2020-04-24 15:11:15 -06001813void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001814 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001815 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001816 current_context_ = &current_renderpass_context_->CurrentContext();
1817}
1818
John Zulauf355e49b2020-04-24 15:11:15 -06001819void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001820 assert(current_renderpass_context_);
1821 if (!current_renderpass_context_) return;
1822
John Zulauf1a224292020-06-30 14:52:13 -06001823 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001824 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001825 current_renderpass_context_ = nullptr;
1826}
1827
locke-lunarg61870c22020-06-09 14:51:50 -06001828bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1829 const VkRect2D &render_area, const char *func_name) const {
1830 bool skip = false;
locke-lunarg96dc9632020-06-10 17:22:18 -06001831 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001832 if (!pPipe ||
1833 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001834 return skip;
1835 }
1836 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001837 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1838 VkExtent3D extent = CastTo3D(render_area.extent);
1839 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001840
John Zulauf1a224292020-06-30 14:52:13 -06001841 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001842 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001843 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1844 for (const auto location : list) {
1845 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1846 continue;
1847 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001848 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1849 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06001850 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001851 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001852 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001853 func_name, string_SyncHazard(hazard.hazard),
1854 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1855 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001856 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001857 }
1858 }
1859 }
locke-lunarg37047832020-06-12 13:44:45 -06001860
1861 // PHASE1 TODO: Add layout based read/vs. write selection.
1862 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1863 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1864 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001865 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001866 bool depth_write = false, stencil_write = false;
1867
1868 // PHASE1 TODO: These validation should be in core_checks.
1869 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1870 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1871 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1872 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1873 depth_write = true;
1874 }
1875 // PHASE1 TODO: It needs to check if stencil is writable.
1876 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1877 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1878 // PHASE1 TODO: These validation should be in core_checks.
1879 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1880 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1881 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1882 stencil_write = true;
1883 }
1884
1885 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1886 if (depth_write) {
1887 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001888 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1889 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001890 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001891 skip |= sync_state.LogError(
1892 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001893 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001894 func_name, string_SyncHazard(hazard.hazard),
1895 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1896 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001897 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001898 }
1899 }
1900 if (stencil_write) {
1901 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001902 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1903 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001904 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001905 skip |= sync_state.LogError(
1906 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001907 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001908 func_name, string_SyncHazard(hazard.hazard),
1909 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1910 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001911 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001912 }
locke-lunarg61870c22020-06-09 14:51:50 -06001913 }
1914 }
1915 return skip;
1916}
1917
locke-lunarg96dc9632020-06-10 17:22:18 -06001918void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1919 const ResourceUsageTag &tag) {
1920 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001921 if (!pPipe ||
1922 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001923 return;
1924 }
1925 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001926 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1927 VkExtent3D extent = CastTo3D(render_area.extent);
1928 VkOffset3D offset = CastTo3D(render_area.offset);
1929
John Zulauf1a224292020-06-30 14:52:13 -06001930 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001931 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001932 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1933 for (const auto location : list) {
1934 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1935 continue;
1936 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001937 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
1938 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001939 }
1940 }
locke-lunarg37047832020-06-12 13:44:45 -06001941
1942 // PHASE1 TODO: Add layout based read/vs. write selection.
1943 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1944 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1945 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001946 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001947 bool depth_write = false, stencil_write = false;
1948
1949 // PHASE1 TODO: These validation should be in core_checks.
1950 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1951 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1952 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1953 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1954 depth_write = true;
1955 }
1956 // PHASE1 TODO: It needs to check if stencil is writable.
1957 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1958 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1959 // PHASE1 TODO: These validation should be in core_checks.
1960 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1961 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1962 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1963 stencil_write = true;
1964 }
1965
1966 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1967 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001968 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1969 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001970 }
1971 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001972 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1973 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001974 }
locke-lunarg61870c22020-06-09 14:51:50 -06001975 }
1976}
1977
John Zulauf1507ee42020-05-18 11:33:09 -06001978bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
1979 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001980 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001981 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06001982 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1983 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001984 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1985 func_name);
1986
John Zulauf355e49b2020-04-24 15:11:15 -06001987 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06001988 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06001989 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1990 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1991 return skip;
1992}
1993bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
1994 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001995 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06001996 bool skip = false;
1997 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1998 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001999 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2000 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06002001 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002002 return skip;
2003}
2004
John Zulauf7635de32020-05-29 17:14:15 -06002005AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2006 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2007}
2008
2009bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2010 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002011 bool skip = false;
2012
John Zulauf7635de32020-05-29 17:14:15 -06002013 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2014 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2015 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2016 // to apply and only copy then, if this proves a hot spot.
2017 std::unique_ptr<AccessContext> proxy_for_current;
2018
John Zulauf355e49b2020-04-24 15:11:15 -06002019 // Validate the "finalLayout" transitions to external
2020 // Get them from where there we're hidding in the extra entry.
2021 const auto &final_transitions = rp_state_->subpass_transitions.back();
2022 for (const auto &transition : final_transitions) {
2023 const auto &attach_view = attachment_views_[transition.attachment];
2024 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2025 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002026 auto *context = trackback.context;
2027
2028 if (transition.prev_pass == current_subpass_) {
2029 if (!proxy_for_current) {
2030 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2031 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2032 }
2033 context = proxy_for_current.get();
2034 }
2035
2036 auto hazard = context->DetectImageBarrierHazard(
John Zulauf355e49b2020-04-24 15:11:15 -06002037 *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope,
2038 attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious);
2039 if (hazard.hazard) {
2040 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2041 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf389c34b2020-07-28 11:19:35 -06002042 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002043 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf389c34b2020-07-28 11:19:35 -06002044 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf37ceaed2020-07-03 16:18:15 -06002045 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002046 }
2047 }
2048 return skip;
2049}
2050
2051void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2052 // Add layout transitions...
2053 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
2054 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -06002055 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -06002056 for (const auto &transition : transitions) {
2057 const auto attachment_view = attachment_views_[transition.attachment];
2058 if (!attachment_view) continue;
2059 const auto image = attachment_view->image_state.get();
2060 if (!image) continue;
2061
2062 const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
John Zulaufc9201222020-05-13 15:13:03 -06002063 auto insert_pair = view_seen.insert(attachment_view);
2064 if (insert_pair.second) {
2065 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
2066 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag);
2067
2068 } else {
2069 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
2070 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
2071 auto barrier_to_transition = barrier->barrier;
2072 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
2073 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag);
2074 }
John Zulauf355e49b2020-04-24 15:11:15 -06002075 }
2076}
2077
John Zulauf1507ee42020-05-18 11:33:09 -06002078void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2079 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2080 auto &subpass_context = subpass_contexts_[current_subpass_];
2081 VkExtent3D extent = CastTo3D(render_area.extent);
2082 VkOffset3D offset = CastTo3D(render_area.offset);
2083
2084 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2085 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2086 if (attachment_views_[i] == nullptr) continue; // UNUSED
2087 const auto &view = *attachment_views_[i];
2088 const IMAGE_STATE *image = view.image_state.get();
2089 if (image == nullptr) continue;
2090
2091 const auto &ci = attachment_ci[i];
2092 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002093 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002094 const bool is_color = !(has_depth || has_stencil);
2095
2096 if (is_color) {
2097 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2098 extent, tag);
2099 } else {
2100 auto update_range = view.normalized_subresource_range;
2101 if (has_depth) {
2102 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2103 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2104 }
2105 if (has_stencil) {
2106 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2107 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2108 tag);
2109 }
2110 }
2111 }
2112 }
2113}
2114
John Zulauf355e49b2020-04-24 15:11:15 -06002115void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002116 const AccessContext *external_context, VkQueueFlags queue_flags,
2117 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002118 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002119 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002120 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2121 // Add this for all subpasses here so that they exsist during next subpass validation
2122 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002123 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002124 }
2125 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2126
2127 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002128 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002129}
John Zulauf1507ee42020-05-18 11:33:09 -06002130
2131void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002132 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2133 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002134 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002135
John Zulauf355e49b2020-04-24 15:11:15 -06002136 current_subpass_++;
2137 assert(current_subpass_ < subpass_contexts_.size());
2138 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002139 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002140}
2141
John Zulauf1a224292020-06-30 14:52:13 -06002142void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2143 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002144 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002145 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002146 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002147
John Zulauf355e49b2020-04-24 15:11:15 -06002148 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002149 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002150
2151 // Add the "finalLayout" transitions to external
2152 // Get them from where there we're hidding in the extra entry.
2153 const auto &final_transitions = rp_state_->subpass_transitions.back();
2154 for (const auto &transition : final_transitions) {
2155 const auto &attachment = attachment_views_[transition.attachment];
2156 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002157 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf1a224292020-06-30 14:52:13 -06002158 external_context->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier,
2159 attachment->normalized_subresource_range, true, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002160 }
2161}
2162
John Zulauf3d84f1b2020-03-09 13:33:25 -06002163SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2164 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2165 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2166 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2167 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2168 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2169 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2170}
2171
2172void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) {
2173 ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
2174 ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope);
2175}
2176
John Zulauf9cb530d2019-09-30 14:14:10 -06002177HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2178 HazardResult hazard;
2179 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002180 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002181 if (IsRead(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002182 // Only check reads vs. last_write if it doesn't happen-after any other read because either:
2183 // * the previous reads are not hazards, and thus last_write must be visible and available to
2184 // any reads that happen after.
2185 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2186 // the current read will be also not be a hazard, thus reporting a hazard here adds no needed information.
2187 if (((usage_stage & read_execution_barriers) == 0) && last_write && IsWriteHazard(usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002188 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002189 }
2190 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002191 // Write operation:
2192 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2193 // If reads exists -- test only against them because either:
2194 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2195 // * 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
2196 // the current write happens after the reads, so just test the write against the reades
2197 // Otherwise test against last_write
2198 //
2199 // Look for casus belli for WAR
2200 if (last_read_count) {
2201 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2202 const auto &read_access = last_reads[read_index];
2203 if (IsReadHazard(usage_stage, read_access)) {
2204 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2205 break;
2206 }
2207 }
2208 } else if (input_attachment_barriers != kNoAttachmentRead) {
John Zulaufd14743a2020-07-03 09:42:39 -06002209 if (IsReadHazard(usage_stage, input_attachment_barriers)) {
John Zulauf59e25072020-07-17 10:55:21 -06002210 hazard.Set(this, usage_index, WRITE_AFTER_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002211 }
John Zulauf361fb532020-07-22 10:45:39 -06002212 } else if (last_write && IsWriteHazard(usage)) {
2213 // Write-After-Write check -- if we have a previous write to test against
2214 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002215 }
2216 }
2217 return hazard;
2218}
2219
John Zulauf69133422020-05-20 14:55:53 -06002220HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2221 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2222 HazardResult hazard;
2223 const auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002224 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf69133422020-05-20 14:55:53 -06002225 const bool write_is_ordered = (last_write & ordering.access_scope) == last_write; // Is true if no write, and that's good.
2226 if (IsRead(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002227 if (!write_is_ordered) {
2228 // Only check for RAW if the write is unordered, and there are no reads ordered before the current read since last_write
2229 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2230 // We need to assemble the effect read_execution barriers from the union of the state barriers and the ordering rules
2231 // Check to see if there are any reads ordered before usage, including ordering rules and barriers.
2232 bool ordered_read = 0 != ((last_read_stages & ordering.exec_scope) | (read_execution_barriers & usage_stage));
2233 // Noting the "special* encoding of the input attachment ordering rule (in access, but not exec)
2234 if ((ordering.access_scope & SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) &&
2235 (input_attachment_barriers != kNoAttachmentRead)) {
2236 ordered_read = true;
John Zulauf69133422020-05-20 14:55:53 -06002237 }
John Zulaufd14743a2020-07-03 09:42:39 -06002238
John Zulauf361fb532020-07-22 10:45:39 -06002239 if (!ordered_read && IsWriteHazard(usage)) {
2240 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2241 }
2242 }
2243
2244 } else {
2245 // Only check for WAW if there are no reads since last_write
2246 if (last_read_count) {
2247 // Ignore ordered read stages (which represent frame-buffer local operations, except input attachment
2248 const auto unordered_reads = last_read_stages & ~ordering.exec_scope;
2249 // Look for any WAR hazards outside the ordered set of stages
2250 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2251 const auto &read_access = last_reads[read_index];
2252 if ((read_access.stage & unordered_reads) && IsReadHazard(usage_stage, read_access)) {
2253 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2254 break;
John Zulaufd14743a2020-07-03 09:42:39 -06002255 }
2256 }
John Zulauf361fb532020-07-22 10:45:39 -06002257 } else if (input_attachment_barriers != kNoAttachmentRead) {
2258 // This is special case code for the fragment shader input attachment, which unlike all other fragment shader operations
2259 // is framebuffer local, and thus subject to raster ordering guarantees
2260 if (0 == (ordering.access_scope & SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT)) {
2261 // NOTE: Currently all ordering barriers include this bit, so this code may never be reached, but it's
2262 // here s.t. if we need to change the ordering barrier/rules we needn't change the code.
2263 hazard.Set(this, usage_index, WRITE_AFTER_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
2264 }
2265 } else if (!write_is_ordered && IsWriteHazard(usage)) {
2266 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf69133422020-05-20 14:55:53 -06002267 }
2268 }
2269 return hazard;
2270}
2271
John Zulauf2f952d22020-02-10 11:34:51 -07002272// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06002273HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002274 HazardResult hazard;
2275 auto usage = FlagBit(usage_index);
2276 if (IsRead(usage)) {
2277 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002278 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002279 }
2280 } else {
2281 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002282 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002283 } else if (last_read_count > 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002284 hazard.Set(this, usage_index, WRITE_RACING_READ, last_reads[0].access, last_reads[0].tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002285 } else if (input_attachment_barriers != kNoAttachmentRead) {
John Zulauf59e25072020-07-17 10:55:21 -06002286 hazard.Set(this, usage_index, WRITE_RACING_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002287 }
2288 }
2289 return hazard;
2290}
2291
John Zulauf36bcf6a2020-02-03 15:12:52 -07002292HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
2293 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002294 // Only supporting image layout transitions for now
2295 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2296 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002297 // only test for WAW if there no intervening read operations.
2298 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2299 if (last_read_count) {
John Zulauf355e49b2020-04-24 15:11:15 -06002300 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002301 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002302 const auto &read_access = last_reads[read_index];
2303 // If the read stage is not in the src sync sync
2304 // *AND* not execution chained with an existing sync barrier (that's the or)
2305 // then the barrier access is unsafe (R/W after R)
2306 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002307 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002308 break;
2309 }
2310 }
John Zulauf361fb532020-07-22 10:45:39 -06002311 } else if (input_attachment_barriers != kNoAttachmentRead) {
John Zulaufd14743a2020-07-03 09:42:39 -06002312 // Same logic as read acces above for the special case of input attachment read
John Zulaufd14743a2020-07-03 09:42:39 -06002313 if ((src_exec_scope & (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | input_attachment_barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002314 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 -06002315 }
John Zulauf361fb532020-07-22 10:45:39 -06002316 } else if (last_write) {
2317 // If the previous write is *not* in the 1st access scope
2318 // *AND* the current barrier is not in the dependency chain
2319 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2320 // then the barrier access is unsafe (R/W after W)
2321 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
2322 // TODO: Do we need a difference hazard name for this?
2323 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2324 }
John Zulaufd14743a2020-07-03 09:42:39 -06002325 }
John Zulauf361fb532020-07-22 10:45:39 -06002326
John Zulauf0cb5be22020-01-23 12:18:22 -07002327 return hazard;
2328}
2329
John Zulauf5f13a792020-03-10 07:31:21 -06002330// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2331// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2332// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2333void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2334 if (write_tag.IsBefore(other.write_tag)) {
2335 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
2336 *this = other;
2337 } else if (!other.write_tag.IsBefore(write_tag)) {
2338 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2339 // dependency chaining logic or any stage expansion)
2340 write_barriers |= other.write_barriers;
2341
John Zulaufd14743a2020-07-03 09:42:39 -06002342 // Merge the read states
2343 if (input_attachment_barriers == kNoAttachmentRead) {
2344 // this doesn't have an input attachment read, so we'll take other, unconditionally (even if it's kNoAttachmentRead)
2345 input_attachment_barriers = other.input_attachment_barriers;
2346 input_attachment_tag = other.input_attachment_tag;
2347 } else if (other.input_attachment_barriers != kNoAttachmentRead) {
2348 // Both states have an input attachment read, pick the newest tag and merge barriers.
2349 if (input_attachment_tag.IsBefore(other.input_attachment_tag)) {
2350 input_attachment_tag = other.input_attachment_tag;
2351 }
2352 input_attachment_barriers |= other.input_attachment_barriers;
2353 }
2354 // The else clause is that only this has an attachment read and no merge is needed
2355
John Zulauf5f13a792020-03-10 07:31:21 -06002356 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2357 auto &other_read = other.last_reads[other_read_index];
2358 if (last_read_stages & other_read.stage) {
2359 // Merge in the barriers for read stages that exist in *both* this and other
2360 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
2361 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
2362 auto &my_read = last_reads[my_read_index];
2363 if (other_read.stage == my_read.stage) {
2364 if (my_read.tag.IsBefore(other_read.tag)) {
2365 my_read.tag = other_read.tag;
John Zulauf37ceaed2020-07-03 16:18:15 -06002366 my_read.access = other_read.access;
John Zulauf5f13a792020-03-10 07:31:21 -06002367 }
2368 my_read.barriers |= other_read.barriers;
2369 break;
2370 }
2371 }
2372 } else {
2373 // The other read stage doesn't exist in this, so add it.
2374 last_reads[last_read_count] = other_read;
2375 last_read_count++;
2376 last_read_stages |= other_read.stage;
2377 }
2378 }
John Zulauf361fb532020-07-22 10:45:39 -06002379 read_execution_barriers |= other.read_execution_barriers;
John Zulauf5f13a792020-03-10 07:31:21 -06002380 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
2381 // it.
2382}
2383
John Zulauf9cb530d2019-09-30 14:14:10 -06002384void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2385 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2386 const auto usage_bit = FlagBit(usage_index);
John Zulaufd14743a2020-07-03 09:42:39 -06002387 if (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) {
2388 // Input attachment requires special treatment for raster/load/store ordering guarantees
2389 input_attachment_barriers = 0;
2390 input_attachment_tag = tag;
2391 } else if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002392 // Mulitple outstanding reads may be of interest and do dependency chains independently
2393 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2394 const auto usage_stage = PipelineStageBit(usage_index);
2395 if (usage_stage & last_read_stages) {
2396 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2397 ReadState &access = last_reads[read_index];
2398 if (access.stage == usage_stage) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002399 access.access = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002400 access.barriers = 0;
2401 access.tag = tag;
2402 break;
2403 }
2404 }
2405 } else {
2406 // We don't have this stage in the list yet...
2407 assert(last_read_count < last_reads.size());
2408 ReadState &access = last_reads[last_read_count++];
2409 access.stage = usage_stage;
John Zulauf37ceaed2020-07-03 16:18:15 -06002410 access.access = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002411 access.barriers = 0;
2412 access.tag = tag;
2413 last_read_stages |= usage_stage;
2414 }
2415 } else {
2416 // Assume write
2417 // TODO determine what to do with READ-WRITE operations if any
John Zulaufd14743a2020-07-03 09:42:39 -06002418 // Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
John Zulauf9cb530d2019-09-30 14:14:10 -06002419 // if the last_reads/last_write were unsafe, we've reported them,
2420 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
2421 last_read_count = 0;
2422 last_read_stages = 0;
John Zulauf361fb532020-07-22 10:45:39 -06002423 read_execution_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002424
John Zulaufd14743a2020-07-03 09:42:39 -06002425 input_attachment_barriers = kNoAttachmentRead; // Denotes no outstanding input attachment read after the last write.
2426 // NOTE: we don't reset the tag, as the equality check ignores it when kNoAttachmentRead is set.
2427
John Zulauf9cb530d2019-09-30 14:14:10 -06002428 write_barriers = 0;
2429 write_dependency_chain = 0;
2430 write_tag = tag;
2431 last_write = usage_bit;
2432 }
2433}
John Zulauf5f13a792020-03-10 07:31:21 -06002434
John Zulauf9cb530d2019-09-30 14:14:10 -06002435void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
2436 // Execution Barriers only protect read operations
2437 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2438 ReadState &access = last_reads[read_index];
2439 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2440 if (srcStageMask & (access.stage | access.barriers)) {
2441 access.barriers |= dstStageMask;
John Zulauf361fb532020-07-22 10:45:39 -06002442 read_execution_barriers |= dstStageMask;
John Zulauf9cb530d2019-09-30 14:14:10 -06002443 }
2444 }
John Zulauf361fb532020-07-22 10:45:39 -06002445 if ((input_attachment_barriers != kNoAttachmentRead) &&
2446 (srcStageMask & (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | input_attachment_barriers))) {
John Zulaufd14743a2020-07-03 09:42:39 -06002447 input_attachment_barriers |= dstStageMask;
John Zulauf361fb532020-07-22 10:45:39 -06002448 read_execution_barriers |= dstStageMask;
John Zulaufd14743a2020-07-03 09:42:39 -06002449 }
John Zulauf361fb532020-07-22 10:45:39 -06002450 if (write_dependency_chain & srcStageMask) {
2451 write_dependency_chain |= dstStageMask;
2452 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002453}
2454
John Zulauf36bcf6a2020-02-03 15:12:52 -07002455void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
2456 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002457 // Assuming we've applied the execution side of this barrier, we update just the write
2458 // The || implements the "dependency chain" logic for this barrier
John Zulauf36bcf6a2020-02-03 15:12:52 -07002459 if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) {
2460 write_barriers |= dst_access_scope;
2461 write_dependency_chain |= dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06002462 }
2463}
2464
John Zulauf59e25072020-07-17 10:55:21 -06002465// This should be just Bits or Index, but we don't have an invalid state for Index
2466VkPipelineStageFlags ResourceAccessState::GetReadBarriers(SyncStageAccessFlags usage_bit) const {
2467 VkPipelineStageFlags barriers = 0U;
2468 if (usage_bit & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) {
2469 barriers = input_attachment_barriers;
2470 } else {
2471 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2472 const auto &read_access = last_reads[read_index];
2473 if (read_access.access & usage_bit) {
2474 barriers = read_access.barriers;
2475 break;
2476 }
2477 }
2478 }
2479 return barriers;
2480}
2481
John Zulaufd1f85d42020-04-15 12:23:15 -06002482void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002483 auto *access_context = GetAccessContextNoInsert(command_buffer);
2484 if (access_context) {
2485 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002486 }
2487}
2488
John Zulaufd1f85d42020-04-15 12:23:15 -06002489void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2490 auto access_found = cb_access_state.find(command_buffer);
2491 if (access_found != cb_access_state.end()) {
2492 access_found->second->Reset();
2493 cb_access_state.erase(access_found);
2494 }
2495}
2496
John Zulauf540266b2020-04-06 18:54:53 -06002497void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002498 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
2499 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002500 const VkMemoryBarrier *pMemoryBarriers) {
2501 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07002502 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002503 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002504 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002505}
2506
John Zulauf540266b2020-04-06 18:54:53 -06002507void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002508 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2509 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002510 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002511 for (uint32_t index = 0; index < barrier_count; index++) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002512 auto barrier = barriers[index]; // barrier is a copy
John Zulauf9cb530d2019-09-30 14:14:10 -06002513 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2514 if (!buffer) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002515 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
2516 const ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002517 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2518 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2519 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2520 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002521 }
2522}
2523
John Zulauf540266b2020-04-06 18:54:53 -06002524void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2525 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2526 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002527 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002528 for (uint32_t index = 0; index < barrier_count; index++) {
2529 const auto &barrier = barriers[index];
2530 const auto *image = Get<IMAGE_STATE>(barrier.image);
2531 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002532 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002533 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2534 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2535 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2536 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
2537 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002538 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002539}
2540
2541bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2542 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2543 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002544 const auto *cb_context = GetAccessContext(commandBuffer);
2545 assert(cb_context);
2546 if (!cb_context) return skip;
2547 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002548
John Zulauf3d84f1b2020-03-09 13:33:25 -06002549 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002550 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002551 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002552
2553 for (uint32_t region = 0; region < regionCount; region++) {
2554 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002555 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002556 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002557 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002558 if (hazard.hazard) {
2559 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002560 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002561 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002562 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002563 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002564 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002565 }
John Zulauf16adfc92020-04-08 10:28:33 -06002566 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002567 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06002568 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002569 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002570 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002571 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002572 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002573 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002574 }
2575 }
2576 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002577 }
2578 return skip;
2579}
2580
2581void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2582 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002583 auto *cb_context = GetAccessContext(commandBuffer);
2584 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002585 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002586 auto *context = cb_context->GetCurrentAccessContext();
2587
John Zulauf9cb530d2019-09-30 14:14:10 -06002588 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002589 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002590
2591 for (uint32_t region = 0; region < regionCount; region++) {
2592 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002593 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002594 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002595 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002596 }
John Zulauf16adfc92020-04-08 10:28:33 -06002597 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002598 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002599 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002600 }
2601 }
2602}
2603
2604bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2605 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2606 const VkImageCopy *pRegions) const {
2607 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002608 const auto *cb_access_context = GetAccessContext(commandBuffer);
2609 assert(cb_access_context);
2610 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002611
John Zulauf3d84f1b2020-03-09 13:33:25 -06002612 const auto *context = cb_access_context->GetCurrentAccessContext();
2613 assert(context);
2614 if (!context) return skip;
2615
2616 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2617 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002618 for (uint32_t region = 0; region < regionCount; region++) {
2619 const auto &copy_region = pRegions[region];
2620 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002621 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002622 copy_region.srcOffset, copy_region.extent);
2623 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002624 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002625 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002626 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002627 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002628 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002629 }
2630
2631 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002632 VkExtent3D dst_copy_extent =
2633 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002634 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002635 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002636 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002637 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002638 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002639 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002640 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002641 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002642 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002643 }
2644 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002645
John Zulauf5c5e88d2019-12-26 11:22:02 -07002646 return skip;
2647}
2648
2649void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2650 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2651 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002652 auto *cb_access_context = GetAccessContext(commandBuffer);
2653 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002654 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002655 auto *context = cb_access_context->GetCurrentAccessContext();
2656 assert(context);
2657
John Zulauf5c5e88d2019-12-26 11:22:02 -07002658 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002659 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002660
2661 for (uint32_t region = 0; region < regionCount; region++) {
2662 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002663 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002664 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2665 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002666 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002667 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002668 VkExtent3D dst_copy_extent =
2669 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002670 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2671 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002672 }
2673 }
2674}
2675
2676bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2677 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2678 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2679 uint32_t bufferMemoryBarrierCount,
2680 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2681 uint32_t imageMemoryBarrierCount,
2682 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2683 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002684 const auto *cb_access_context = GetAccessContext(commandBuffer);
2685 assert(cb_access_context);
2686 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002687
John Zulauf3d84f1b2020-03-09 13:33:25 -06002688 const auto *context = cb_access_context->GetCurrentAccessContext();
2689 assert(context);
2690 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002691
John Zulauf3d84f1b2020-03-09 13:33:25 -06002692 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002693 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2694 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002695 // Validate Image Layout transitions
2696 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2697 const auto &barrier = pImageMemoryBarriers[index];
2698 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2699 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2700 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002701 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002702 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002703 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002704 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002705 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002706 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002707 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002708 }
2709 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002710
2711 return skip;
2712}
2713
2714void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2715 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2716 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2717 uint32_t bufferMemoryBarrierCount,
2718 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2719 uint32_t imageMemoryBarrierCount,
2720 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002721 auto *cb_access_context = GetAccessContext(commandBuffer);
2722 assert(cb_access_context);
2723 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002724 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002725 auto access_context = cb_access_context->GetCurrentAccessContext();
2726 assert(access_context);
2727 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06002728
John Zulauf3d84f1b2020-03-09 13:33:25 -06002729 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002730 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002731 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002732 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
2733 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2734 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002735 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
2736 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002737 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06002738 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002739
2740 // 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 -06002741 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07002742 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06002743}
2744
2745void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
2746 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
2747 // The state tracker sets up the device state
2748 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
2749
John Zulauf5f13a792020-03-10 07:31:21 -06002750 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
2751 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06002752 // TODO: Find a good way to do this hooklessly.
2753 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
2754 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
2755 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
2756
John Zulaufd1f85d42020-04-15 12:23:15 -06002757 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2758 sync_device_state->ResetCommandBufferCallback(command_buffer);
2759 });
2760 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2761 sync_device_state->FreeCommandBufferCallback(command_buffer);
2762 });
John Zulauf9cb530d2019-09-30 14:14:10 -06002763}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002764
John Zulauf355e49b2020-04-24 15:11:15 -06002765bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2766 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
2767 bool skip = false;
2768 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
2769 auto cb_context = GetAccessContext(commandBuffer);
2770
2771 if (rp_state && cb_context) {
2772 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
2773 }
2774
2775 return skip;
2776}
2777
2778bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2779 VkSubpassContents contents) const {
2780 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2781 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2782 subpass_begin_info.contents = contents;
2783 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
2784 return skip;
2785}
2786
2787bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2788 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2789 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2790 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
2791 return skip;
2792}
2793
2794bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2795 const VkRenderPassBeginInfo *pRenderPassBegin,
2796 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2797 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2798 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
2799 return skip;
2800}
2801
John Zulauf3d84f1b2020-03-09 13:33:25 -06002802void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
2803 VkResult result) {
2804 // The state tracker sets up the command buffer state
2805 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
2806
2807 // Create/initialize the structure that trackers accesses at the command buffer scope.
2808 auto cb_access_context = GetAccessContext(commandBuffer);
2809 assert(cb_access_context);
2810 cb_access_context->Reset();
2811}
2812
2813void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06002814 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002815 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002816 if (cb_context) {
2817 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002818 }
2819}
2820
2821void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2822 VkSubpassContents contents) {
2823 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2824 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2825 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002826 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002827}
2828
2829void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2830 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2831 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002832 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002833}
2834
2835void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2836 const VkRenderPassBeginInfo *pRenderPassBegin,
2837 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2838 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002839 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
2840}
2841
2842bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2843 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
2844 bool skip = false;
2845
2846 auto cb_context = GetAccessContext(commandBuffer);
2847 assert(cb_context);
2848 auto cb_state = cb_context->GetCommandBufferState();
2849 if (!cb_state) return skip;
2850
2851 auto rp_state = cb_state->activeRenderPass;
2852 if (!rp_state) return skip;
2853
2854 skip |= cb_context->ValidateNextSubpass(func_name);
2855
2856 return skip;
2857}
2858
2859bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
2860 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
2861 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2862 subpass_begin_info.contents = contents;
2863 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
2864 return skip;
2865}
2866
2867bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2868 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2869 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2870 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
2871 return skip;
2872}
2873
2874bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2875 const VkSubpassEndInfo *pSubpassEndInfo) const {
2876 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2877 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
2878 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002879}
2880
2881void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06002882 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002883 auto cb_context = GetAccessContext(commandBuffer);
2884 assert(cb_context);
2885 auto cb_state = cb_context->GetCommandBufferState();
2886 if (!cb_state) return;
2887
2888 auto rp_state = cb_state->activeRenderPass;
2889 if (!rp_state) return;
2890
John Zulauf355e49b2020-04-24 15:11:15 -06002891 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002892}
2893
2894void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
2895 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
2896 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2897 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002898 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002899}
2900
2901void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2902 const VkSubpassEndInfo *pSubpassEndInfo) {
2903 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002904 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002905}
2906
2907void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2908 const VkSubpassEndInfo *pSubpassEndInfo) {
2909 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002910 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002911}
2912
John Zulauf355e49b2020-04-24 15:11:15 -06002913bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
2914 const char *func_name) const {
2915 bool skip = false;
2916
2917 auto cb_context = GetAccessContext(commandBuffer);
2918 assert(cb_context);
2919 auto cb_state = cb_context->GetCommandBufferState();
2920 if (!cb_state) return skip;
2921
2922 auto rp_state = cb_state->activeRenderPass;
2923 if (!rp_state) return skip;
2924
2925 skip |= cb_context->ValidateEndRenderpass(func_name);
2926 return skip;
2927}
2928
2929bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2930 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
2931 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
2932 return skip;
2933}
2934
2935bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
2936 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2937 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2938 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
2939 return skip;
2940}
2941
2942bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
2943 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2944 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2945 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
2946 return skip;
2947}
2948
2949void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
2950 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06002951 // Resolve the all subpass contexts to the command buffer contexts
2952 auto cb_context = GetAccessContext(commandBuffer);
2953 assert(cb_context);
2954 auto cb_state = cb_context->GetCommandBufferState();
2955 if (!cb_state) return;
2956
locke-lunargaecf2152020-05-12 17:15:41 -06002957 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06002958 if (!rp_state) return;
2959
John Zulauf355e49b2020-04-24 15:11:15 -06002960 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06002961}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002962
John Zulauf33fc1d52020-07-17 11:01:10 -06002963// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
2964// updates to a resource which do not conflict at the byte level.
2965// TODO: Revisit this rule to see if it needs to be tighter or looser
2966// TODO: Add programatic control over suppression heuristics
2967bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
2968 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
2969}
2970
John Zulauf3d84f1b2020-03-09 13:33:25 -06002971void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002972 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06002973 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002974}
2975
2976void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06002977 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06002978 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002979}
2980
2981void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06002982 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06002983 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002984}
locke-lunarga19c71d2020-03-02 18:17:04 -07002985
2986bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2987 VkImageLayout dstImageLayout, uint32_t regionCount,
2988 const VkBufferImageCopy *pRegions) const {
2989 bool skip = false;
2990 const auto *cb_access_context = GetAccessContext(commandBuffer);
2991 assert(cb_access_context);
2992 if (!cb_access_context) return skip;
2993
2994 const auto *context = cb_access_context->GetCurrentAccessContext();
2995 assert(context);
2996 if (!context) return skip;
2997
2998 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07002999 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3000
3001 for (uint32_t region = 0; region < regionCount; region++) {
3002 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003003 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003004 ResourceAccessRange src_range =
3005 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003006 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003007 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003008 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003009 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003010 "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003011 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003012 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003013 }
3014 }
3015 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003016 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003017 copy_region.imageOffset, copy_region.imageExtent);
3018 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003019 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003020 "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003021 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003022 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003023 }
3024 if (skip) break;
3025 }
3026 if (skip) break;
3027 }
3028 return skip;
3029}
3030
3031void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3032 VkImageLayout dstImageLayout, uint32_t regionCount,
3033 const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003034 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07003035 auto *cb_access_context = GetAccessContext(commandBuffer);
3036 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003037 const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07003038 auto *context = cb_access_context->GetCurrentAccessContext();
3039 assert(context);
3040
3041 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003042 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003043
3044 for (uint32_t region = 0; region < regionCount; region++) {
3045 const auto &copy_region = pRegions[region];
3046 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003047 ResourceAccessRange src_range =
3048 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003049 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003050 }
3051 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003052 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003053 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003054 }
3055 }
3056}
3057
3058bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3059 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3060 const VkBufferImageCopy *pRegions) const {
3061 bool skip = false;
3062 const auto *cb_access_context = GetAccessContext(commandBuffer);
3063 assert(cb_access_context);
3064 if (!cb_access_context) return skip;
3065
3066 const auto *context = cb_access_context->GetCurrentAccessContext();
3067 assert(context);
3068 if (!context) return skip;
3069
3070 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3071 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3072 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3073 for (uint32_t region = 0; region < regionCount; region++) {
3074 const auto &copy_region = pRegions[region];
3075 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003076 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003077 copy_region.imageOffset, copy_region.imageExtent);
3078 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003079 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003080 "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003081 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003082 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003083 }
3084 }
3085 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06003086 ResourceAccessRange dst_range =
3087 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003088 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003089 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003090 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003091 "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003092 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003093 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003094 }
3095 }
3096 if (skip) break;
3097 }
3098 return skip;
3099}
3100
3101void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3102 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003103 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07003104 auto *cb_access_context = GetAccessContext(commandBuffer);
3105 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003106 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER);
locke-lunarga19c71d2020-03-02 18:17:04 -07003107 auto *context = cb_access_context->GetCurrentAccessContext();
3108 assert(context);
3109
3110 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003111 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3112 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 -06003113 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003114
3115 for (uint32_t region = 0; region < regionCount; region++) {
3116 const auto &copy_region = pRegions[region];
3117 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003118 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003119 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003120 }
3121 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003122 ResourceAccessRange dst_range =
3123 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003124 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003125 }
3126 }
3127}
3128
3129bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3130 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3131 const VkImageBlit *pRegions, VkFilter filter) const {
3132 bool skip = false;
3133 const auto *cb_access_context = GetAccessContext(commandBuffer);
3134 assert(cb_access_context);
3135 if (!cb_access_context) return skip;
3136
3137 const auto *context = cb_access_context->GetCurrentAccessContext();
3138 assert(context);
3139 if (!context) return skip;
3140
3141 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3142 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3143
3144 for (uint32_t region = 0; region < regionCount; region++) {
3145 const auto &blit_region = pRegions[region];
3146 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003147 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3148 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3149 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3150 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3151 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3152 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3153 auto hazard =
3154 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003155 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003156 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003157 "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003158 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003159 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003160 }
3161 }
3162
3163 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003164 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3165 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3166 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3167 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3168 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3169 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3170 auto hazard =
3171 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003172 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003173 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003174 "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003175 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003176 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003177 }
3178 if (skip) break;
3179 }
3180 }
3181
3182 return skip;
3183}
3184
3185void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3186 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3187 const VkImageBlit *pRegions, VkFilter filter) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003188 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3189 pRegions, filter);
locke-lunarga19c71d2020-03-02 18:17:04 -07003190 auto *cb_access_context = GetAccessContext(commandBuffer);
3191 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003192 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07003193 auto *context = cb_access_context->GetCurrentAccessContext();
3194 assert(context);
3195
3196 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003197 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003198
3199 for (uint32_t region = 0; region < regionCount; region++) {
3200 const auto &blit_region = pRegions[region];
3201 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003202 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3203 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3204 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3205 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3206 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3207 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3208 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003209 }
3210 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003211 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3212 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3213 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3214 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3215 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3216 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3217 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003218 }
3219 }
3220}
locke-lunarg36ba2592020-04-03 09:42:04 -06003221
locke-lunarg61870c22020-06-09 14:51:50 -06003222bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
3223 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
3224 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003225 bool skip = false;
3226 if (drawCount == 0) return skip;
3227
3228 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3229 VkDeviceSize size = struct_size;
3230 if (drawCount == 1 || stride == size) {
3231 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003232 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003233 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3234 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003235 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003236 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003237 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003238 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003239 }
3240 } else {
3241 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003242 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003243 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3244 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003245 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003246 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3247 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3248 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003249 break;
3250 }
3251 }
3252 }
3253 return skip;
3254}
3255
locke-lunarg61870c22020-06-09 14:51:50 -06003256void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3257 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3258 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003259 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3260 VkDeviceSize size = struct_size;
3261 if (drawCount == 1 || stride == size) {
3262 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003263 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003264 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3265 } else {
3266 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003267 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003268 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3269 }
3270 }
3271}
3272
locke-lunarg61870c22020-06-09 14:51:50 -06003273bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3274 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003275 bool skip = false;
3276
3277 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003278 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003279 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3280 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003281 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003282 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003283 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003284 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003285 }
3286 return skip;
3287}
3288
locke-lunarg61870c22020-06-09 14:51:50 -06003289void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003290 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003291 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003292 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3293}
3294
locke-lunarg36ba2592020-04-03 09:42:04 -06003295bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003296 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003297 const auto *cb_access_context = GetAccessContext(commandBuffer);
3298 assert(cb_access_context);
3299 if (!cb_access_context) return skip;
3300
locke-lunarg61870c22020-06-09 14:51:50 -06003301 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003302 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003303}
3304
3305void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003306 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003307 auto *cb_access_context = GetAccessContext(commandBuffer);
3308 assert(cb_access_context);
3309 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003310
locke-lunarg61870c22020-06-09 14:51:50 -06003311 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003312}
locke-lunarge1a67022020-04-29 00:15:36 -06003313
3314bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003315 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003316 const auto *cb_access_context = GetAccessContext(commandBuffer);
3317 assert(cb_access_context);
3318 if (!cb_access_context) return skip;
3319
3320 const auto *context = cb_access_context->GetCurrentAccessContext();
3321 assert(context);
3322 if (!context) return skip;
3323
locke-lunarg61870c22020-06-09 14:51:50 -06003324 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3325 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3326 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003327 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003328}
3329
3330void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003331 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003332 auto *cb_access_context = GetAccessContext(commandBuffer);
3333 assert(cb_access_context);
3334 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3335 auto *context = cb_access_context->GetCurrentAccessContext();
3336 assert(context);
3337
locke-lunarg61870c22020-06-09 14:51:50 -06003338 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3339 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003340}
3341
3342bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3343 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003344 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003345 const auto *cb_access_context = GetAccessContext(commandBuffer);
3346 assert(cb_access_context);
3347 if (!cb_access_context) return skip;
3348
locke-lunarg61870c22020-06-09 14:51:50 -06003349 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3350 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3351 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003352 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003353}
3354
3355void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3356 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003357 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003358 auto *cb_access_context = GetAccessContext(commandBuffer);
3359 assert(cb_access_context);
3360 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003361
locke-lunarg61870c22020-06-09 14:51:50 -06003362 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3363 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3364 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003365}
3366
3367bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3368 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003369 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003370 const auto *cb_access_context = GetAccessContext(commandBuffer);
3371 assert(cb_access_context);
3372 if (!cb_access_context) return skip;
3373
locke-lunarg61870c22020-06-09 14:51:50 -06003374 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3375 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3376 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003377 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003378}
3379
3380void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3381 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003382 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003383 auto *cb_access_context = GetAccessContext(commandBuffer);
3384 assert(cb_access_context);
3385 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003386
locke-lunarg61870c22020-06-09 14:51:50 -06003387 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3388 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3389 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003390}
3391
3392bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3393 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003394 bool skip = false;
3395 if (drawCount == 0) return skip;
3396
locke-lunargff255f92020-05-13 18:53:52 -06003397 const auto *cb_access_context = GetAccessContext(commandBuffer);
3398 assert(cb_access_context);
3399 if (!cb_access_context) return skip;
3400
3401 const auto *context = cb_access_context->GetCurrentAccessContext();
3402 assert(context);
3403 if (!context) return skip;
3404
locke-lunarg61870c22020-06-09 14:51:50 -06003405 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3406 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3407 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3408 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003409
3410 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3411 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3412 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003413 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003414 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003415}
3416
3417void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3418 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003419 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003420 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003421 auto *cb_access_context = GetAccessContext(commandBuffer);
3422 assert(cb_access_context);
3423 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3424 auto *context = cb_access_context->GetCurrentAccessContext();
3425 assert(context);
3426
locke-lunarg61870c22020-06-09 14:51:50 -06003427 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3428 cb_access_context->RecordDrawSubpassAttachment(tag);
3429 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003430
3431 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3432 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3433 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003434 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003435}
3436
3437bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3438 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003439 bool skip = false;
3440 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003441 const auto *cb_access_context = GetAccessContext(commandBuffer);
3442 assert(cb_access_context);
3443 if (!cb_access_context) return skip;
3444
3445 const auto *context = cb_access_context->GetCurrentAccessContext();
3446 assert(context);
3447 if (!context) return skip;
3448
locke-lunarg61870c22020-06-09 14:51:50 -06003449 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3450 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3451 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3452 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003453
3454 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3455 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3456 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003457 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003458 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003459}
3460
3461void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3462 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003463 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003464 auto *cb_access_context = GetAccessContext(commandBuffer);
3465 assert(cb_access_context);
3466 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3467 auto *context = cb_access_context->GetCurrentAccessContext();
3468 assert(context);
3469
locke-lunarg61870c22020-06-09 14:51:50 -06003470 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3471 cb_access_context->RecordDrawSubpassAttachment(tag);
3472 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003473
3474 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3475 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3476 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003477 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003478}
3479
3480bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3481 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3482 uint32_t stride, const char *function) const {
3483 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003484 const auto *cb_access_context = GetAccessContext(commandBuffer);
3485 assert(cb_access_context);
3486 if (!cb_access_context) return skip;
3487
3488 const auto *context = cb_access_context->GetCurrentAccessContext();
3489 assert(context);
3490 if (!context) return skip;
3491
locke-lunarg61870c22020-06-09 14:51:50 -06003492 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3493 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3494 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3495 function);
3496 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003497
3498 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3499 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3500 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003501 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003502 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003503}
3504
3505bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3506 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3507 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003508 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3509 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003510}
3511
3512void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3513 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3514 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003515 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3516 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003517 auto *cb_access_context = GetAccessContext(commandBuffer);
3518 assert(cb_access_context);
3519 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3520 auto *context = cb_access_context->GetCurrentAccessContext();
3521 assert(context);
3522
locke-lunarg61870c22020-06-09 14:51:50 -06003523 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3524 cb_access_context->RecordDrawSubpassAttachment(tag);
3525 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3526 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003527
3528 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3529 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3530 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003531 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003532}
3533
3534bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3535 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3536 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003537 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3538 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003539}
3540
3541void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3542 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3543 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003544 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3545 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003546 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003547}
3548
3549bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3550 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3551 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003552 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3553 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003554}
3555
3556void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3557 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3558 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003559 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3560 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003561 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3562}
3563
3564bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3565 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3566 uint32_t stride, const char *function) const {
3567 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003568 const auto *cb_access_context = GetAccessContext(commandBuffer);
3569 assert(cb_access_context);
3570 if (!cb_access_context) return skip;
3571
3572 const auto *context = cb_access_context->GetCurrentAccessContext();
3573 assert(context);
3574 if (!context) return skip;
3575
locke-lunarg61870c22020-06-09 14:51:50 -06003576 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3577 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3578 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3579 stride, function);
3580 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003581
3582 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3583 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3584 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003585 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003586 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003587}
3588
3589bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3590 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3591 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003592 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3593 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003594}
3595
3596void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3597 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3598 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003599 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3600 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003601 auto *cb_access_context = GetAccessContext(commandBuffer);
3602 assert(cb_access_context);
3603 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3604 auto *context = cb_access_context->GetCurrentAccessContext();
3605 assert(context);
3606
locke-lunarg61870c22020-06-09 14:51:50 -06003607 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3608 cb_access_context->RecordDrawSubpassAttachment(tag);
3609 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3610 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003611
3612 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3613 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06003614 // We will update the index and vertex buffer in SubmitQueue in the future.
3615 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003616}
3617
3618bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3619 VkDeviceSize offset, VkBuffer countBuffer,
3620 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3621 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003622 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3623 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003624}
3625
3626void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3627 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3628 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003629 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3630 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003631 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3632}
3633
3634bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3635 VkDeviceSize offset, VkBuffer countBuffer,
3636 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3637 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003638 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3639 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003640}
3641
3642void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3643 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3644 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003645 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3646 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003647 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3648}
3649
3650bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3651 const VkClearColorValue *pColor, uint32_t rangeCount,
3652 const VkImageSubresourceRange *pRanges) const {
3653 bool skip = false;
3654 const auto *cb_access_context = GetAccessContext(commandBuffer);
3655 assert(cb_access_context);
3656 if (!cb_access_context) return skip;
3657
3658 const auto *context = cb_access_context->GetCurrentAccessContext();
3659 assert(context);
3660 if (!context) return skip;
3661
3662 const auto *image_state = Get<IMAGE_STATE>(image);
3663
3664 for (uint32_t index = 0; index < rangeCount; index++) {
3665 const auto &range = pRanges[index];
3666 if (image_state) {
3667 auto hazard =
3668 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3669 if (hazard.hazard) {
3670 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003671 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003672 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06003673 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003674 }
3675 }
3676 }
3677 return skip;
3678}
3679
3680void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3681 const VkClearColorValue *pColor, uint32_t rangeCount,
3682 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003683 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003684 auto *cb_access_context = GetAccessContext(commandBuffer);
3685 assert(cb_access_context);
3686 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
3687 auto *context = cb_access_context->GetCurrentAccessContext();
3688 assert(context);
3689
3690 const auto *image_state = Get<IMAGE_STATE>(image);
3691
3692 for (uint32_t index = 0; index < rangeCount; index++) {
3693 const auto &range = pRanges[index];
3694 if (image_state) {
3695 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3696 tag);
3697 }
3698 }
3699}
3700
3701bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3702 VkImageLayout imageLayout,
3703 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3704 const VkImageSubresourceRange *pRanges) const {
3705 bool skip = false;
3706 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
3714 const auto *image_state = Get<IMAGE_STATE>(image);
3715
3716 for (uint32_t index = 0; index < rangeCount; index++) {
3717 const auto &range = pRanges[index];
3718 if (image_state) {
3719 auto hazard =
3720 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3721 if (hazard.hazard) {
3722 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003723 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003724 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06003725 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003726 }
3727 }
3728 }
3729 return skip;
3730}
3731
3732void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3733 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3734 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003735 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003736 auto *cb_access_context = GetAccessContext(commandBuffer);
3737 assert(cb_access_context);
3738 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
3739 auto *context = cb_access_context->GetCurrentAccessContext();
3740 assert(context);
3741
3742 const auto *image_state = Get<IMAGE_STATE>(image);
3743
3744 for (uint32_t index = 0; index < rangeCount; index++) {
3745 const auto &range = pRanges[index];
3746 if (image_state) {
3747 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3748 tag);
3749 }
3750 }
3751}
3752
3753bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
3754 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
3755 VkDeviceSize dstOffset, VkDeviceSize stride,
3756 VkQueryResultFlags flags) const {
3757 bool skip = false;
3758 const auto *cb_access_context = GetAccessContext(commandBuffer);
3759 assert(cb_access_context);
3760 if (!cb_access_context) return skip;
3761
3762 const auto *context = cb_access_context->GetCurrentAccessContext();
3763 assert(context);
3764 if (!context) return skip;
3765
3766 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3767
3768 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003769 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003770 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3771 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06003772 skip |=
3773 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3774 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
3775 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003776 }
3777 }
locke-lunargff255f92020-05-13 18:53:52 -06003778
3779 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003780 return skip;
3781}
3782
3783void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
3784 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3785 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003786 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
3787 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06003788 auto *cb_access_context = GetAccessContext(commandBuffer);
3789 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06003790 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06003791 auto *context = cb_access_context->GetCurrentAccessContext();
3792 assert(context);
3793
3794 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3795
3796 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003797 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003798 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3799 }
locke-lunargff255f92020-05-13 18:53:52 -06003800
3801 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003802}
3803
3804bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3805 VkDeviceSize size, uint32_t data) const {
3806 bool skip = false;
3807 const auto *cb_access_context = GetAccessContext(commandBuffer);
3808 assert(cb_access_context);
3809 if (!cb_access_context) return skip;
3810
3811 const auto *context = cb_access_context->GetCurrentAccessContext();
3812 assert(context);
3813 if (!context) return skip;
3814
3815 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3816
3817 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003818 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06003819 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3820 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003821 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003822 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06003823 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003824 }
3825 }
3826 return skip;
3827}
3828
3829void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3830 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003831 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06003832 auto *cb_access_context = GetAccessContext(commandBuffer);
3833 assert(cb_access_context);
3834 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
3835 auto *context = cb_access_context->GetCurrentAccessContext();
3836 assert(context);
3837
3838 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3839
3840 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003841 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06003842 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3843 }
3844}
3845
3846bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3847 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3848 const VkImageResolve *pRegions) const {
3849 bool skip = false;
3850 const auto *cb_access_context = GetAccessContext(commandBuffer);
3851 assert(cb_access_context);
3852 if (!cb_access_context) return skip;
3853
3854 const auto *context = cb_access_context->GetCurrentAccessContext();
3855 assert(context);
3856 if (!context) return skip;
3857
3858 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3859 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3860
3861 for (uint32_t region = 0; region < regionCount; region++) {
3862 const auto &resolve_region = pRegions[region];
3863 if (src_image) {
3864 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3865 resolve_region.srcOffset, resolve_region.extent);
3866 if (hazard.hazard) {
3867 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003868 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003869 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003870 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003871 }
3872 }
3873
3874 if (dst_image) {
3875 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3876 resolve_region.dstOffset, resolve_region.extent);
3877 if (hazard.hazard) {
3878 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003879 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003880 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003881 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003882 }
3883 if (skip) break;
3884 }
3885 }
3886
3887 return skip;
3888}
3889
3890void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3891 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3892 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003893 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3894 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06003895 auto *cb_access_context = GetAccessContext(commandBuffer);
3896 assert(cb_access_context);
3897 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
3898 auto *context = cb_access_context->GetCurrentAccessContext();
3899 assert(context);
3900
3901 auto *src_image = Get<IMAGE_STATE>(srcImage);
3902 auto *dst_image = Get<IMAGE_STATE>(dstImage);
3903
3904 for (uint32_t region = 0; region < regionCount; region++) {
3905 const auto &resolve_region = pRegions[region];
3906 if (src_image) {
3907 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3908 resolve_region.srcOffset, resolve_region.extent, tag);
3909 }
3910 if (dst_image) {
3911 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3912 resolve_region.dstOffset, resolve_region.extent, tag);
3913 }
3914 }
3915}
3916
3917bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3918 VkDeviceSize dataSize, const void *pData) const {
3919 bool skip = false;
3920 const auto *cb_access_context = GetAccessContext(commandBuffer);
3921 assert(cb_access_context);
3922 if (!cb_access_context) return skip;
3923
3924 const auto *context = cb_access_context->GetCurrentAccessContext();
3925 assert(context);
3926 if (!context) return skip;
3927
3928 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3929
3930 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003931 // VK_WHOLE_SIZE not allowed
3932 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06003933 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3934 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003935 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003936 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06003937 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003938 }
3939 }
3940 return skip;
3941}
3942
3943void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3944 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003945 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06003946 auto *cb_access_context = GetAccessContext(commandBuffer);
3947 assert(cb_access_context);
3948 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
3949 auto *context = cb_access_context->GetCurrentAccessContext();
3950 assert(context);
3951
3952 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3953
3954 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003955 // VK_WHOLE_SIZE not allowed
3956 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06003957 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3958 }
3959}
locke-lunargff255f92020-05-13 18:53:52 -06003960
3961bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3962 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
3963 bool skip = false;
3964 const auto *cb_access_context = GetAccessContext(commandBuffer);
3965 assert(cb_access_context);
3966 if (!cb_access_context) return skip;
3967
3968 const auto *context = cb_access_context->GetCurrentAccessContext();
3969 assert(context);
3970 if (!context) return skip;
3971
3972 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3973
3974 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003975 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003976 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3977 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06003978 skip |=
3979 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3980 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
3981 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003982 }
3983 }
3984 return skip;
3985}
3986
3987void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3988 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003989 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06003990 auto *cb_access_context = GetAccessContext(commandBuffer);
3991 assert(cb_access_context);
3992 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
3993 auto *context = cb_access_context->GetCurrentAccessContext();
3994 assert(context);
3995
3996 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3997
3998 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003999 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004000 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4001 }
4002}