blob: bd432e1d25ff9309b86ea41456b3eb114d1577b7 [file] [log] [blame]
locke-lunarg8ec19162020-06-16 18:48:34 -06001/* Copyright (c) 2019-2020 The Khronos Group Inc.
2 * Copyright (c) 2019-2020 Valve Corporation
3 * Copyright (c) 2019-2020 LunarG, Inc.
John Zulauf9cb530d2019-09-30 14:14:10 -06004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Author: John Zulauf <jzulauf@lunarg.com>
18 */
19
20#include <limits>
21#include <vector>
locke-lunarg296a3c92020-03-25 01:04:29 -060022#include <memory>
23#include <bitset>
John Zulauf9cb530d2019-09-30 14:14:10 -060024#include "synchronization_validation.h"
25
26static const char *string_SyncHazardVUID(SyncHazard hazard) {
27 switch (hazard) {
28 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070029 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060030 break;
31 case SyncHazard::READ_AFTER_WRITE:
32 return "SYNC-HAZARD-READ_AFTER_WRITE";
33 break;
34 case SyncHazard::WRITE_AFTER_READ:
35 return "SYNC-HAZARD-WRITE_AFTER_READ";
36 break;
37 case SyncHazard::WRITE_AFTER_WRITE:
38 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
39 break;
John Zulauf2f952d22020-02-10 11:34:51 -070040 case SyncHazard::READ_RACING_WRITE:
41 return "SYNC-HAZARD-READ-RACING-WRITE";
42 break;
43 case SyncHazard::WRITE_RACING_WRITE:
44 return "SYNC-HAZARD-WRITE-RACING-WRITE";
45 break;
46 case SyncHazard::WRITE_RACING_READ:
47 return "SYNC-HAZARD-WRITE-RACING-READ";
48 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060049 default:
50 assert(0);
51 }
52 return "SYNC-HAZARD-INVALID";
53}
54
John Zulauf59e25072020-07-17 10:55:21 -060055static bool IsHazardVsRead(SyncHazard hazard) {
56 switch (hazard) {
57 case SyncHazard::NONE:
58 return false;
59 break;
60 case SyncHazard::READ_AFTER_WRITE:
61 return false;
62 break;
63 case SyncHazard::WRITE_AFTER_READ:
64 return true;
65 break;
66 case SyncHazard::WRITE_AFTER_WRITE:
67 return false;
68 break;
69 case SyncHazard::READ_RACING_WRITE:
70 return false;
71 break;
72 case SyncHazard::WRITE_RACING_WRITE:
73 return false;
74 break;
75 case SyncHazard::WRITE_RACING_READ:
76 return true;
77 break;
78 default:
79 assert(0);
80 }
81 return false;
82}
83
John Zulauf9cb530d2019-09-30 14:14:10 -060084static const char *string_SyncHazard(SyncHazard hazard) {
85 switch (hazard) {
86 case SyncHazard::NONE:
87 return "NONR";
88 break;
89 case SyncHazard::READ_AFTER_WRITE:
90 return "READ_AFTER_WRITE";
91 break;
92 case SyncHazard::WRITE_AFTER_READ:
93 return "WRITE_AFTER_READ";
94 break;
95 case SyncHazard::WRITE_AFTER_WRITE:
96 return "WRITE_AFTER_WRITE";
97 break;
John Zulauf2f952d22020-02-10 11:34:51 -070098 case SyncHazard::READ_RACING_WRITE:
99 return "READ_RACING_WRITE";
100 break;
101 case SyncHazard::WRITE_RACING_WRITE:
102 return "WRITE_RACING_WRITE";
103 break;
104 case SyncHazard::WRITE_RACING_READ:
105 return "WRITE_RACING_READ";
106 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600107 default:
108 assert(0);
109 }
110 return "INVALID HAZARD";
111}
112
John Zulauf37ceaed2020-07-03 16:18:15 -0600113static const SyncStageAccessInfoType *SyncStageAccessInfoFromMask(SyncStageAccessFlags flags) {
114 // Return the info for the first bit found
115 const SyncStageAccessInfoType *info = nullptr;
116 uint32_t index = 0;
117 while (flags) {
118 if (flags & 0x1) {
119 flags = 0;
120 info = &syncStageAccessInfoByStageAccessIndex[index];
121 } else {
122 flags = flags >> 1;
123 index++;
124 }
125 }
126 return info;
127}
128
John Zulauf59e25072020-07-17 10:55:21 -0600129static std::string string_SyncStageAccessFlags(SyncStageAccessFlags flags, const char *sep = "|") {
130 std::string out_str;
131 uint32_t index = 0;
John Zulauf389c34b2020-07-28 11:19:35 -0600132 if (0 == flags) {
133 out_str = "0";
134 }
John Zulauf59e25072020-07-17 10:55:21 -0600135 while (flags) {
136 const auto &info = syncStageAccessInfoByStageAccessIndex[index];
137 if (flags & info.stage_access_bit) {
138 if (!out_str.empty()) {
139 out_str.append(sep);
140 }
141 out_str.append(info.name);
142 flags = flags & ~info.stage_access_bit;
143 }
144 index++;
145 assert(index < syncStageAccessInfoByStageAccessIndex.size());
146 }
147 if (out_str.length() == 0) {
148 out_str.append("Unhandled SyncStageAccess");
149 }
150 return out_str;
151}
152
John Zulauf37ceaed2020-07-03 16:18:15 -0600153static std::string string_UsageTag(const HazardResult &hazard) {
154 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600155 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
156 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600157 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600158 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
159 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf59e25072020-07-17 10:55:21 -0600160 out << "(usage: " << usage_info.name << ", prior_usage: " << stage_access_name;
161 if (IsHazardVsRead(hazard.hazard)) {
162 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
163 out << ", read_barriers: " << string_VkPipelineStageFlags(barriers);
164 } else {
165 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
166 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
167 }
168
169 out << ", command: " << CommandTypeString(tag.command);
170 out << ", seq_no: " << (tag.index & 0xFFFFFFFF) << ", reset_no: " << (tag.index >> 32) << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600171 return out.str();
172}
173
John Zulaufd14743a2020-07-03 09:42:39 -0600174// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
175// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
176// also reflects this special case for read hazard detection (using access instead of exec scope)
John Zulaufb027cdb2020-05-21 14:25:22 -0600177static constexpr VkPipelineStageFlags kColorAttachmentExecScope = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
178static constexpr SyncStageAccessFlags kColorAttachmentAccessScope =
179 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
180 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
John Zulaufd14743a2020-07-03 09:42:39 -0600181 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
182 SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600183static constexpr VkPipelineStageFlags kDepthStencilAttachmentExecScope =
184 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
185static constexpr SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
186 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
187 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
188 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
John Zulaufd14743a2020-07-03 09:42:39 -0600189 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
190 SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600191
192static constexpr SyncOrderingBarrier kColorAttachmentRasterOrder = {kColorAttachmentExecScope, kColorAttachmentAccessScope};
193static constexpr SyncOrderingBarrier kDepthStencilAttachmentRasterOrder = {kDepthStencilAttachmentExecScope,
194 kDepthStencilAttachmentAccessScope};
195static constexpr SyncOrderingBarrier kAttachmentRasterOrder = {kDepthStencilAttachmentExecScope | kColorAttachmentExecScope,
196 kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope};
John Zulauf7635de32020-05-29 17:14:15 -0600197// Sometimes we have an internal access conflict, and we using the kCurrentCommandTag to set and detect in temporary/proxy contexts
John Zulaufcc6fecb2020-06-17 15:24:54 -0600198static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600199
locke-lunarg3c038002020-04-30 23:08:08 -0600200inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
201 if (size == VK_WHOLE_SIZE) {
202 return (whole_size - offset);
203 }
204 return size;
205}
206
John Zulauf3e86bf02020-09-12 10:47:57 -0600207static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
208 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
209}
210
John Zulauf16adfc92020-04-08 10:28:33 -0600211template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600212static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600213 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
214}
215
John Zulauf355e49b2020-04-24 15:11:15 -0600216static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600217
John Zulauf3e86bf02020-09-12 10:47:57 -0600218static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
219 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
220}
221
222static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
223 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
224}
225
John Zulauf0cb5be22020-01-23 12:18:22 -0700226// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
227VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
228 VkPipelineStageFlags expanded = stage_mask;
229 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
230 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
231 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
232 if (all_commands.first & queue_flags) {
233 expanded |= all_commands.second;
234 }
235 }
236 }
237 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
238 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
239 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
240 }
241 return expanded;
242}
243
John Zulauf36bcf6a2020-02-03 15:12:52 -0700244VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
245 std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
246 VkPipelineStageFlags unscanned = stage_mask;
247 VkPipelineStageFlags related = 0;
Jonah Ryan-Davis185189c2020-07-14 10:28:52 -0400248 for (const auto &entry : map) {
249 const auto &stage = entry.first;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700250 if (stage & unscanned) {
251 related = related | entry.second;
252 unscanned = unscanned & ~stage;
253 if (!unscanned) break;
254 }
255 }
256 return related;
257}
258
259VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
260 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
261}
262
263VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
264 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
265}
266
John Zulauf5c5e88d2019-12-26 11:22:02 -0700267static const ResourceAccessRange full_range(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700268
John Zulauf3e86bf02020-09-12 10:47:57 -0600269ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
270 VkDeviceSize stride) {
271 VkDeviceSize range_start = offset + first_index * stride;
272 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600273 if (count == UINT32_MAX) {
274 range_size = buf_whole_size - range_start;
275 } else {
276 range_size = count * stride;
277 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600278 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600279}
280
locke-lunarg654e3692020-06-04 17:19:15 -0600281SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
282 VkShaderStageFlagBits stage_flag) {
283 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
284 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
285 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
286 }
287 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
288 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
289 assert(0);
290 }
291 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
292 return stage_access->second.uniform_read;
293 }
294
295 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
296 // Because if write hazard happens, read hazard might or might not happen.
297 // But if write hazard doesn't happen, read hazard is impossible to happen.
298 if (descriptor_data.is_writable) {
299 return stage_access->second.shader_write;
300 }
301 return stage_access->second.shader_read;
302}
303
locke-lunarg37047832020-06-12 13:44:45 -0600304bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
305 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
306 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
307 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
308 ? true
309 : false;
310}
311
312bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
313 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
314 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
315 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
316 ? true
317 : false;
318}
319
John Zulauf355e49b2020-04-24 15:11:15 -0600320// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
321const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = {
322 AccessContext::AddressType::kLinearAddress, AccessContext::AddressType::kIdealizedAddress};
323
John Zulauf7635de32020-05-29 17:14:15 -0600324// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
325// Used by both validation and record operations
326//
327// The signature for Action() reflect the needs of both uses.
328template <typename Action>
329void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
330 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
331 VkExtent3D extent = CastTo3D(render_area.extent);
332 VkOffset3D offset = CastTo3D(render_area.offset);
333 const auto &rp_ci = rp_state.createInfo;
334 const auto *attachment_ci = rp_ci.pAttachments;
335 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
336
337 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
338 const auto *color_attachments = subpass_ci.pColorAttachments;
339 const auto *color_resolve = subpass_ci.pResolveAttachments;
340 if (color_resolve && color_attachments) {
341 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
342 const auto &color_attach = color_attachments[i].attachment;
343 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
344 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
345 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
346 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
347 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
348 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
349 }
350 }
351 }
352
353 // Depth stencil resolve only if the extension is present
354 const auto ds_resolve = lvl_find_in_chain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
355 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
356 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
357 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
358 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
359 const auto src_ci = attachment_ci[src_at];
360 // The formats are required to match so we can pick either
361 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
362 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
363 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
364 VkImageAspectFlags aspect_mask = 0u;
365
366 // Figure out which aspects are actually touched during resolve operations
367 const char *aspect_string = nullptr;
368 if (resolve_depth && resolve_stencil) {
369 // Validate all aspects together
370 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
371 aspect_string = "depth/stencil";
372 } else if (resolve_depth) {
373 // Validate depth only
374 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
375 aspect_string = "depth";
376 } else if (resolve_stencil) {
377 // Validate all stencil only
378 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
379 aspect_string = "stencil";
380 }
381
382 if (aspect_mask) {
383 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
384 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kDepthStencilAttachmentRasterOrder, offset, extent,
385 aspect_mask);
386 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
387 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
388 }
389 }
390}
391
392// Action for validating resolve operations
393class ValidateResolveAction {
394 public:
395 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
396 const char *func_name)
397 : render_pass_(render_pass),
398 subpass_(subpass),
399 context_(context),
400 sync_state_(sync_state),
401 func_name_(func_name),
402 skip_(false) {}
403 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
404 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
405 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
406 HazardResult hazard;
407 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
408 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600409 skip_ |= sync_state_.LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
410 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600411 " to resolve attachment %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -0600412 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name,
John Zulauf37ceaed2020-07-03 16:18:15 -0600413 src_at, dst_at, string_UsageTag(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600414 }
415 }
416 // Providing a mechanism for the constructing caller to get the result of the validation
417 bool GetSkip() const { return skip_; }
418
419 private:
420 VkRenderPass render_pass_;
421 const uint32_t subpass_;
422 const AccessContext &context_;
423 const SyncValidator &sync_state_;
424 const char *func_name_;
425 bool skip_;
426};
427
428// Update action for resolve operations
429class UpdateStateResolveAction {
430 public:
431 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
432 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
433 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
434 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
435 // Ignores validation only arguments...
436 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
437 }
438
439 private:
440 AccessContext &context_;
441 const ResourceUsageTag &tag_;
442};
443
John Zulauf59e25072020-07-17 10:55:21 -0600444void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
445 SyncStageAccessFlags prior_, const ResourceUsageTag &tag_) {
446 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
447 usage_index = usage_index_;
448 hazard = hazard_;
449 prior_access = prior_;
450 tag = tag_;
451}
452
John Zulauf540266b2020-04-06 18:54:53 -0600453AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
454 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600455 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600456 Reset();
457 const auto &subpass_dep = dependencies[subpass];
458 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600459 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600460 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600461 const auto prev_pass = prev_dep.first->pass;
462 const auto &prev_barriers = prev_dep.second;
463 assert(prev_dep.second.size());
464 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
465 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700466 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600467
468 async_.reserve(subpass_dep.async.size());
469 for (const auto async_subpass : subpass_dep.async) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600470 // TODO -- review why async is storing non-const
John Zulauf540266b2020-04-06 18:54:53 -0600471 async_.emplace_back(const_cast<AccessContext *>(&contexts[async_subpass]));
John Zulauf3d84f1b2020-03-09 13:33:25 -0600472 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600473 if (subpass_dep.barrier_from_external.size()) {
474 src_external_ = TrackBack(external_context, queue_flags, subpass_dep.barrier_from_external);
John Zulaufe5da6e52020-03-18 15:32:18 -0600475 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600476 if (subpass_dep.barrier_to_external.size()) {
477 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600478 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700479}
480
John Zulauf5f13a792020-03-10 07:31:21 -0600481template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600482HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600483 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600484 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600485 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600486
487 HazardResult hazard;
488 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
489 hazard = detector.Detect(prev);
490 }
491 return hazard;
492}
493
John Zulauf3d84f1b2020-03-09 13:33:25 -0600494// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
495// the DAG of the contexts (for example subpasses)
496template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600497HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
498 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600499 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600500
John Zulauf1a224292020-06-30 14:52:13 -0600501 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600502 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
503 // so we'll check these first
504 for (const auto &async_context : async_) {
505 hazard = async_context->DetectAsyncHazard(type, detector, range);
506 if (hazard.hazard) return hazard;
507 }
John Zulauf5f13a792020-03-10 07:31:21 -0600508 }
509
John Zulauf1a224292020-06-30 14:52:13 -0600510 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600511
John Zulauf69133422020-05-20 14:55:53 -0600512 const auto &accesses = GetAccessStateMap(type);
513 const auto from = accesses.lower_bound(range);
514 const auto to = accesses.upper_bound(range);
515 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600516
John Zulauf69133422020-05-20 14:55:53 -0600517 for (auto pos = from; pos != to; ++pos) {
518 // Cover any leading gap, or gap between entries
519 if (detect_prev) {
520 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
521 // Cover any leading gap, or gap between entries
522 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600523 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600524 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600525 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600526 if (hazard.hazard) return hazard;
527 }
John Zulauf69133422020-05-20 14:55:53 -0600528 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
529 gap.begin = pos->first.end;
530 }
531
532 hazard = detector.Detect(pos);
533 if (hazard.hazard) return hazard;
534 }
535
536 if (detect_prev) {
537 // Detect in the trailing empty as needed
538 gap.end = range.end;
539 if (gap.non_empty()) {
540 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600541 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600542 }
543
544 return hazard;
545}
546
547// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
548template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600549HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600550 auto &accesses = GetAccessStateMap(type);
551 const auto from = accesses.lower_bound(range);
552 const auto to = accesses.upper_bound(range);
553
John Zulauf3d84f1b2020-03-09 13:33:25 -0600554 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600555 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
556 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600557 }
John Zulauf16adfc92020-04-08 10:28:33 -0600558
John Zulauf3d84f1b2020-03-09 13:33:25 -0600559 return hazard;
560}
561
John Zulauf355e49b2020-04-24 15:11:15 -0600562// Returns the last resolved entry
563static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
564 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufa0a98292020-09-18 09:30:10 -0600565 const std::vector<SyncBarrier> &barriers) {
John Zulauf355e49b2020-04-24 15:11:15 -0600566 auto at = entry;
567 for (auto pos = first; pos != last; ++pos) {
568 // Every member of the input iterator range must fit within the remaining portion of entry
569 assert(at->first.includes(pos->first));
570 assert(at != dest->end());
571 // Trim up at to the same size as the entry to resolve
572 at = sparse_container::split(at, *dest, pos->first);
573 auto access = pos->second;
John Zulaufa0a98292020-09-18 09:30:10 -0600574 if (barriers.size()) {
575 access.ApplyBarriers(barriers);
John Zulauf355e49b2020-04-24 15:11:15 -0600576 }
577 at->second.Resolve(access);
578 ++at; // Go to the remaining unused section of entry
579 }
580}
581
John Zulaufa0a98292020-09-18 09:30:10 -0600582static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
583 SyncBarrier merged = {};
584 for (const auto &barrier : barriers) {
585 merged.Merge(barrier);
586 }
587 return merged;
588}
589
590void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, const std::vector<SyncBarrier> &barriers,
John Zulauf355e49b2020-04-24 15:11:15 -0600591 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
592 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600593 if (!range.non_empty()) return;
594
John Zulauf355e49b2020-04-24 15:11:15 -0600595 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
596 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600597 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600598 if (current->pos_B->valid) {
599 const auto &src_pos = current->pos_B->lower_bound;
John Zulauf355e49b2020-04-24 15:11:15 -0600600 auto access = src_pos->second;
John Zulaufa0a98292020-09-18 09:30:10 -0600601 if (barriers.size()) {
602 access.ApplyBarriers(barriers);
John Zulauf355e49b2020-04-24 15:11:15 -0600603 }
John Zulauf16adfc92020-04-08 10:28:33 -0600604 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600605 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
606 trimmed->second.Resolve(access);
607 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600608 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600609 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600610 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600611 }
John Zulauf16adfc92020-04-08 10:28:33 -0600612 } else {
613 // we have to descend to fill this gap
614 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600615 if (current->pos_A->valid) {
616 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
617 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600618 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulaufa0a98292020-09-18 09:30:10 -0600619 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barriers);
John Zulauf355e49b2020-04-24 15:11:15 -0600620 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600621 // There isn't anything in dest in current)range, so we can accumulate directly into it.
622 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulaufa0a98292020-09-18 09:30:10 -0600623 if (barriers.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -0600624 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
John Zulauf3bcab5e2020-06-19 14:42:32 -0600625 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
John Zulaufa0a98292020-09-18 09:30:10 -0600626 pos->second.ApplyBarriers(barriers);
John Zulauf355e49b2020-04-24 15:11:15 -0600627 }
628 }
629 }
630 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
631 // iterator of the outer while.
632
633 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
634 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
635 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600636 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
637 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600638 current.seek(seek_to);
639 } else if (!current->pos_A->valid && infill_state) {
640 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
641 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
642 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600643 }
John Zulauf5f13a792020-03-10 07:31:21 -0600644 }
John Zulauf16adfc92020-04-08 10:28:33 -0600645 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600646 }
John Zulauf1a224292020-06-30 14:52:13 -0600647
648 // Infill if range goes passed both the current and resolve map prior contents
649 if (recur_to_infill && (current->range.end < range.end)) {
650 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
651 ResourceAccessRangeMap gap_map;
652 const auto the_end = resolve_map->end();
653 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
654 for (auto &access : gap_map) {
John Zulaufa0a98292020-09-18 09:30:10 -0600655 access.second.ApplyBarriers(barriers);
John Zulauf1a224292020-06-30 14:52:13 -0600656 resolve_map->insert(the_end, access);
657 }
658 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600659}
660
John Zulauf355e49b2020-04-24 15:11:15 -0600661void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
662 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600663 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600664 if (range.non_empty() && infill_state) {
665 descent_map->insert(std::make_pair(range, *infill_state));
666 }
667 } else {
668 // Look for something to fill the gap further along.
669 for (const auto &prev_dep : prev_) {
John Zulaufa0a98292020-09-18 09:30:10 -0600670 prev_dep.context->ResolveAccessRange(type, range, prev_dep.barriers, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600671 }
672
John Zulaufe5da6e52020-03-18 15:32:18 -0600673 if (src_external_.context) {
John Zulaufa0a98292020-09-18 09:30:10 -0600674 src_external_.context->ResolveAccessRange(type, range, src_external_.barriers, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600675 }
676 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600677}
678
John Zulauf16adfc92020-04-08 10:28:33 -0600679AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600680 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600681}
682
683VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) {
684 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
685}
686
John Zulauf355e49b2020-04-24 15:11:15 -0600687static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
John Zulauf16adfc92020-04-08 10:28:33 -0600688
John Zulauf1507ee42020-05-18 11:33:09 -0600689static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
690 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
691 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
692 return stage_access;
693}
694static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
695 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
696 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
697 return stage_access;
698}
699
John Zulauf7635de32020-05-29 17:14:15 -0600700// Caller must manage returned pointer
701static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
702 uint32_t subpass, const VkRect2D &render_area,
703 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
704 auto *proxy = new AccessContext(context);
705 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600706 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600707 return proxy;
708}
709
John Zulauf540266b2020-04-06 18:54:53 -0600710void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
John Zulauf355e49b2020-04-24 15:11:15 -0600711 AddressType address_type, ResourceAccessRangeMap *descent_map,
712 const ResourceAccessState *infill_state) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600713 if (!SimpleBinding(image_state)) return;
714
John Zulauf62f10592020-04-03 12:20:02 -0600715 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
locke-lunargae26eac2020-04-16 15:29:05 -0600716 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600717 image_state.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600718 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf62f10592020-04-03 12:20:02 -0600719 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600720 ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state);
John Zulauf62f10592020-04-03 12:20:02 -0600721 }
722}
723
John Zulauf7635de32020-05-29 17:14:15 -0600724// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600725bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600726 const VkRect2D &render_area, uint32_t subpass,
727 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
728 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600729 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600730 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
731 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
732 // those affects have not been recorded yet.
733 //
734 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
735 // to apply and only copy then, if this proves a hot spot.
736 std::unique_ptr<AccessContext> proxy_for_prev;
737 TrackBack proxy_track_back;
738
John Zulauf355e49b2020-04-24 15:11:15 -0600739 const auto &transitions = rp_state.subpass_transitions[subpass];
740 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600741 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
742
743 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
744 if (prev_needs_proxy) {
745 if (!proxy_for_prev) {
746 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
747 render_area, attachment_views));
748 proxy_track_back = *track_back;
749 proxy_track_back.context = proxy_for_prev.get();
750 }
751 track_back = &proxy_track_back;
752 }
753 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600754 if (hazard.hazard) {
John Zulauf389c34b2020-07-28 11:19:35 -0600755 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
756 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
757 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
758 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
759 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
760 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600761 }
762 }
763 return skip;
764}
765
John Zulauf1507ee42020-05-18 11:33:09 -0600766bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600767 const VkRect2D &render_area, uint32_t subpass,
768 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
769 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600770 bool skip = false;
771 const auto *attachment_ci = rp_state.createInfo.pAttachments;
772 VkExtent3D extent = CastTo3D(render_area.extent);
773 VkOffset3D offset = CastTo3D(render_area.offset);
John Zulaufa0a98292020-09-18 09:30:10 -0600774
775 SyncStageAccessFlags accumulate_access_scope = 0;
776 for (const auto &barrier : src_external_.barriers) {
777 accumulate_access_scope |= barrier.dst_access_scope;
778 }
779 const auto external_access_scope = accumulate_access_scope;
John Zulauf1507ee42020-05-18 11:33:09 -0600780
781 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
782 if (subpass == rp_state.attachment_first_subpass[i]) {
783 if (attachment_views[i] == nullptr) continue;
784 const IMAGE_VIEW_STATE &view = *attachment_views[i];
785 const IMAGE_STATE *image = view.image_state.get();
786 if (image == nullptr) continue;
787 const auto &ci = attachment_ci[i];
788 const bool is_transition = rp_state.attachment_first_is_transition[i];
789
790 // Need check in the following way
791 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
792 // vs. transition
793 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
794 // for each aspect loaded.
795
796 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600797 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600798 const bool is_color = !(has_depth || has_stencil);
799
800 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
801 const SyncStageAccessFlags load_mask = (has_depth || is_color) ? SyncStageAccess::Flags(load_index) : 0U;
802 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
803 const SyncStageAccessFlags stencil_mask = has_stencil ? SyncStageAccess::Flags(stencil_load_index) : 0U;
804
John Zulaufaff20662020-06-01 14:07:58 -0600805 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600806 const char *aspect = nullptr;
807 if (is_transition) {
808 // For transition w
809 SyncHazard transition_hazard = SyncHazard::NONE;
810 bool checked_stencil = false;
811 if (load_mask) {
812 if ((load_mask & external_access_scope) != load_mask) {
813 transition_hazard =
814 SyncStageAccess::HasWrite(load_mask) ? SyncHazard::WRITE_AFTER_WRITE : SyncHazard::READ_AFTER_WRITE;
815 aspect = is_color ? "color" : "depth";
816 }
817 if (!transition_hazard && stencil_mask) {
818 if ((stencil_mask & external_access_scope) != stencil_mask) {
819 transition_hazard = SyncStageAccess::HasWrite(stencil_mask) ? SyncHazard::WRITE_AFTER_WRITE
820 : SyncHazard::READ_AFTER_WRITE;
821 aspect = "stencil";
822 checked_stencil = true;
823 }
824 }
825 }
826 if (transition_hazard) {
827 // Hazard vs. ILT
828 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
829 skip |=
locke-lunarg54379cf2020-08-05 12:25:29 -0600830 sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(transition_hazard),
John Zulauf1507ee42020-05-18 11:33:09 -0600831 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
832 " aspect %s during load with loadOp %s.",
833 func_name, string_SyncHazard(transition_hazard), subpass, i, aspect, load_op_string);
834 }
835 } else {
836 auto hazard_range = view.normalized_subresource_range;
837 bool checked_stencil = false;
838 if (is_color) {
839 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, offset, extent);
840 aspect = "color";
841 } else {
842 if (has_depth) {
843 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
844 hazard = DetectHazard(*image, load_index, hazard_range, offset, extent);
845 aspect = "depth";
846 }
847 if (!hazard.hazard && has_stencil) {
848 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
849 hazard = DetectHazard(*image, stencil_load_index, hazard_range, offset, extent);
850 aspect = "stencil";
851 checked_stencil = true;
852 }
853 }
854
855 if (hazard.hazard) {
856 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
857 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
858 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600859 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -0600860 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600861 string_UsageTag(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -0600862 }
863 }
864 }
865 }
866 return skip;
867}
868
John Zulaufaff20662020-06-01 14:07:58 -0600869// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
870// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
871// store is part of the same Next/End operation.
872// The latter is handled in layout transistion validation directly
873bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
874 const VkRect2D &render_area, uint32_t subpass,
875 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
876 const char *func_name) const {
877 bool skip = false;
878 const auto *attachment_ci = rp_state.createInfo.pAttachments;
879 VkExtent3D extent = CastTo3D(render_area.extent);
880 VkOffset3D offset = CastTo3D(render_area.offset);
881
882 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
883 if (subpass == rp_state.attachment_last_subpass[i]) {
884 if (attachment_views[i] == nullptr) continue;
885 const IMAGE_VIEW_STATE &view = *attachment_views[i];
886 const IMAGE_STATE *image = view.image_state.get();
887 if (image == nullptr) continue;
888 const auto &ci = attachment_ci[i];
889
890 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
891 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
892 // sake, we treat DONT_CARE as writing.
893 const bool has_depth = FormatHasDepth(ci.format);
894 const bool has_stencil = FormatHasStencil(ci.format);
895 const bool is_color = !(has_depth || has_stencil);
896 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
897 if (!has_stencil && !store_op_stores) continue;
898
899 HazardResult hazard;
900 const char *aspect = nullptr;
901 bool checked_stencil = false;
902 if (is_color) {
903 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
904 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
905 aspect = "color";
906 } else {
907 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
908 auto hazard_range = view.normalized_subresource_range;
909 if (has_depth && store_op_stores) {
910 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
911 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
912 kAttachmentRasterOrder, offset, extent);
913 aspect = "depth";
914 }
915 if (!hazard.hazard && has_stencil && stencil_op_stores) {
916 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
917 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
918 kAttachmentRasterOrder, offset, extent);
919 aspect = "stencil";
920 checked_stencil = true;
921 }
922 }
923
924 if (hazard.hazard) {
925 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
926 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -0600927 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
928 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600929 " %s aspect during store with %s %s. Access info %s",
John Zulauf1dae9192020-06-16 15:46:44 -0600930 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600931 store_op_string, string_UsageTag(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -0600932 }
933 }
934 }
935 return skip;
936}
937
John Zulaufb027cdb2020-05-21 14:25:22 -0600938bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
939 const VkRect2D &render_area,
940 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
941 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600942 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
943 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
944 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600945}
946
John Zulauf3d84f1b2020-03-09 13:33:25 -0600947class HazardDetector {
948 SyncStageAccessIndex usage_index_;
949
950 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600951 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600952 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
953 return pos->second.DetectAsyncHazard(usage_index_);
954 }
955 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
956};
957
John Zulauf69133422020-05-20 14:55:53 -0600958class HazardDetectorWithOrdering {
959 const SyncStageAccessIndex usage_index_;
960 const SyncOrderingBarrier &ordering_;
961
962 public:
963 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
964 return pos->second.DetectHazard(usage_index_, ordering_);
965 }
966 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
967 return pos->second.DetectAsyncHazard(usage_index_);
968 }
969 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
970 : usage_index_(usage), ordering_(ordering) {}
971};
972
John Zulauf16adfc92020-04-08 10:28:33 -0600973HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600974 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600975 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600976 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600977}
978
John Zulauf16adfc92020-04-08 10:28:33 -0600979HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600980 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600981 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -0600982 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -0600983}
984
John Zulauf69133422020-05-20 14:55:53 -0600985template <typename Detector>
986HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
987 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
988 const VkExtent3D &extent, DetectOptions options) const {
989 if (!SimpleBinding(image)) return HazardResult();
990 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
991 const auto address_type = ImageAddressType(image);
992 const auto base_address = ResourceBaseAddress(image);
993 for (; range_gen->non_empty(); ++range_gen) {
994 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
995 if (hazard.hazard) return hazard;
996 }
997 return HazardResult();
998}
999
John Zulauf540266b2020-04-06 18:54:53 -06001000HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1001 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1002 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001003 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1004 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -06001005 return DetectHazard(image, current_usage, subresource_range, offset, extent);
1006}
1007
1008HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1009 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1010 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -06001011 HazardDetector detector(current_usage);
1012 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
1013}
1014
1015HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1016 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
1017 const VkOffset3D &offset, const VkExtent3D &extent) const {
1018 HazardDetectorWithOrdering detector(current_usage, ordering);
1019 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001020}
1021
John Zulaufb027cdb2020-05-21 14:25:22 -06001022// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
1023// should have reported the issue regarding an invalid attachment entry
1024HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
1025 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
1026 VkImageAspectFlags aspect_mask) const {
1027 if (view != nullptr) {
1028 const IMAGE_STATE *image = view->image_state.get();
1029 if (image != nullptr) {
1030 auto *detect_range = &view->normalized_subresource_range;
1031 VkImageSubresourceRange masked_range;
1032 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1033 masked_range = view->normalized_subresource_range;
1034 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1035 detect_range = &masked_range;
1036 }
1037
1038 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1039 if (detect_range->aspectMask) {
1040 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
1041 }
1042 }
1043 }
1044 return HazardResult();
1045}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001046class BarrierHazardDetector {
1047 public:
1048 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1049 SyncStageAccessFlags src_access_scope)
1050 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1051
John Zulauf5f13a792020-03-10 07:31:21 -06001052 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1053 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001054 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001055 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
1056 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1057 return pos->second.DetectAsyncHazard(usage_index_);
1058 }
1059
1060 private:
1061 SyncStageAccessIndex usage_index_;
1062 VkPipelineStageFlags src_exec_scope_;
1063 SyncStageAccessFlags src_access_scope_;
1064};
1065
John Zulauf16adfc92020-04-08 10:28:33 -06001066HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -06001067 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001068 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001069 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -06001070 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001071}
1072
John Zulauf16adfc92020-04-08 10:28:33 -06001073HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001074 SyncStageAccessFlags src_access_scope,
1075 const VkImageSubresourceRange &subresource_range,
1076 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001077 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1078 VkOffset3D zero_offset = {0, 0, 0};
1079 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001080}
1081
John Zulauf355e49b2020-04-24 15:11:15 -06001082HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1083 SyncStageAccessFlags src_stage_accesses,
1084 const VkImageMemoryBarrier &barrier) const {
1085 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1086 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1087 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1088}
1089
John Zulauf9cb530d2019-09-30 14:14:10 -06001090template <typename Flags, typename Map>
1091SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1092 SyncStageAccessFlags scope = 0;
1093 for (const auto &bit_scope : map) {
1094 if (flag_mask < bit_scope.first) break;
1095
1096 if (flag_mask & bit_scope.first) {
1097 scope |= bit_scope.second;
1098 }
1099 }
1100 return scope;
1101}
1102
1103SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
1104 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1105}
1106
1107SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
1108 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
1109}
1110
1111// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
1112SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001113 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1114 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1115 // of the union of all stage/access types for all the stages and the same unions for the access mask...
John Zulauf9cb530d2019-09-30 14:14:10 -06001116 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1117}
1118
1119template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001120void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001121 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1122 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -06001123 auto pos = accesses->lower_bound(range);
1124 if (pos == accesses->end() || !pos->first.intersects(range)) {
1125 // The range is empty, fill it with a default value.
1126 pos = action.Infill(accesses, pos, range);
1127 } else if (range.begin < pos->first.begin) {
1128 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001129 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001130 } else if (pos->first.begin < range.begin) {
1131 // Trim the beginning if needed
1132 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1133 ++pos;
1134 }
1135
1136 const auto the_end = accesses->end();
1137 while ((pos != the_end) && pos->first.intersects(range)) {
1138 if (pos->first.end > range.end) {
1139 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1140 }
1141
1142 pos = action(accesses, pos);
1143 if (pos == the_end) break;
1144
1145 auto next = pos;
1146 ++next;
1147 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1148 // Need to infill if next is disjoint
1149 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001150 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001151 next = action.Infill(accesses, next, new_range);
1152 }
1153 pos = next;
1154 }
1155}
1156
1157struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001158 using Iterator = ResourceAccessRangeMap::iterator;
1159 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001160 // this is only called on gaps, and never returns a gap.
1161 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001162 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001163 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001164 }
John Zulauf5f13a792020-03-10 07:31:21 -06001165
John Zulauf5c5e88d2019-12-26 11:22:02 -07001166 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001167 auto &access_state = pos->second;
1168 access_state.Update(usage, tag);
1169 return pos;
1170 }
1171
John Zulauf16adfc92020-04-08 10:28:33 -06001172 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001173 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001174 : type(type_), context(context_), usage(usage_), tag(tag_) {}
1175 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001176 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001177 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001178 const ResourceUsageTag &tag;
1179};
1180
1181struct ApplyMemoryAccessBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001182 using Iterator = ResourceAccessRangeMap::iterator;
1183 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001184
John Zulauf5c5e88d2019-12-26 11:22:02 -07001185 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001186 auto &access_state = pos->second;
John Zulaufa0a98292020-09-18 09:30:10 -06001187 access_state.ApplyMemoryAccessBarrier(false, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001188 return pos;
1189 }
1190
John Zulauf36bcf6a2020-02-03 15:12:52 -07001191 ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_,
1192 VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_)
1193 : src_exec_scope(src_exec_scope_),
1194 src_access_scope(src_access_scope_),
1195 dst_exec_scope(dst_exec_scope_),
1196 dst_access_scope(dst_access_scope_) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001197
John Zulauf36bcf6a2020-02-03 15:12:52 -07001198 VkPipelineStageFlags src_exec_scope;
1199 SyncStageAccessFlags src_access_scope;
1200 VkPipelineStageFlags dst_exec_scope;
1201 SyncStageAccessFlags dst_access_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001202};
1203
1204struct ApplyGlobalBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001205 using Iterator = ResourceAccessRangeMap::iterator;
1206 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001207
John Zulauf5c5e88d2019-12-26 11:22:02 -07001208 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001209 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001210 access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001211
1212 for (const auto &functor : barrier_functor) {
1213 functor(accesses, pos);
1214 }
1215 return pos;
1216 }
1217
John Zulauf36bcf6a2020-02-03 15:12:52 -07001218 ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope,
1219 SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses,
John Zulauf9cb530d2019-09-30 14:14:10 -06001220 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001221 : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001222 // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier...
1223 barrier_functor.reserve(memoryBarrierCount);
1224 for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) {
1225 const auto &barrier = pMemoryBarriers[barrier_index];
John Zulauf36bcf6a2020-02-03 15:12:52 -07001226 barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask),
1227 dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
John Zulauf9cb530d2019-09-30 14:14:10 -06001228 }
1229 }
1230
John Zulauf36bcf6a2020-02-03 15:12:52 -07001231 const VkPipelineStageFlags src_exec_scope;
1232 const VkPipelineStageFlags dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001233 std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor;
1234};
1235
John Zulauf355e49b2020-04-24 15:11:15 -06001236void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1237 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001238 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1239 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001240}
1241
John Zulauf16adfc92020-04-08 10:28:33 -06001242void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001243 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001244 if (!SimpleBinding(buffer)) return;
1245 const auto base_address = ResourceBaseAddress(buffer);
1246 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1247}
John Zulauf355e49b2020-04-24 15:11:15 -06001248
John Zulauf540266b2020-04-06 18:54:53 -06001249void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001250 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001251 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001252 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001253 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001254 const auto address_type = ImageAddressType(image);
1255 const auto base_address = ResourceBaseAddress(image);
1256 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001257 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001258 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001259 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001260}
John Zulauf7635de32020-05-29 17:14:15 -06001261void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1262 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1263 if (view != nullptr) {
1264 const IMAGE_STATE *image = view->image_state.get();
1265 if (image != nullptr) {
1266 auto *update_range = &view->normalized_subresource_range;
1267 VkImageSubresourceRange masked_range;
1268 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1269 masked_range = view->normalized_subresource_range;
1270 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1271 update_range = &masked_range;
1272 }
1273 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1274 }
1275 }
1276}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001277
John Zulauf355e49b2020-04-24 15:11:15 -06001278void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1279 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1280 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001281 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1282 subresource.layerCount};
1283 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1284}
1285
John Zulauf540266b2020-04-06 18:54:53 -06001286template <typename Action>
1287void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001288 if (!SimpleBinding(buffer)) return;
1289 const auto base_address = ResourceBaseAddress(buffer);
1290 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001291}
1292
1293template <typename Action>
1294void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1295 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001296 if (!SimpleBinding(image)) return;
1297 const auto address_type = ImageAddressType(image);
1298 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001299
locke-lunargae26eac2020-04-16 15:29:05 -06001300 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001301 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001302
John Zulauf16adfc92020-04-08 10:28:33 -06001303 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001304 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001305 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001306 }
1307}
1308
John Zulauf7635de32020-05-29 17:14:15 -06001309void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1310 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1311 const ResourceUsageTag &tag) {
1312 UpdateStateResolveAction update(*this, tag);
1313 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1314}
1315
John Zulaufaff20662020-06-01 14:07:58 -06001316void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1317 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1318 const ResourceUsageTag &tag) {
1319 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1320 VkExtent3D extent = CastTo3D(render_area.extent);
1321 VkOffset3D offset = CastTo3D(render_area.offset);
1322
1323 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1324 if (rp_state.attachment_last_subpass[i] == subpass) {
1325 if (attachment_views[i] == nullptr) continue; // UNUSED
1326 const auto &view = *attachment_views[i];
1327 const IMAGE_STATE *image = view.image_state.get();
1328 if (image == nullptr) continue;
1329
1330 const auto &ci = attachment_ci[i];
1331 const bool has_depth = FormatHasDepth(ci.format);
1332 const bool has_stencil = FormatHasStencil(ci.format);
1333 const bool is_color = !(has_depth || has_stencil);
1334 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1335
1336 if (is_color && store_op_stores) {
1337 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1338 offset, extent, tag);
1339 } else {
1340 auto update_range = view.normalized_subresource_range;
1341 if (has_depth && store_op_stores) {
1342 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1343 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1344 tag);
1345 }
1346 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1347 if (has_stencil && stencil_op_stores) {
1348 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1349 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1350 tag);
1351 }
1352 }
1353 }
1354 }
1355}
1356
John Zulauf540266b2020-04-06 18:54:53 -06001357template <typename Action>
1358void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1359 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001360 for (const auto address_type : kAddressTypes) {
1361 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001362 }
1363}
1364
1365void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001366 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1367 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -06001368 for (const auto address_type : kAddressTypes) {
John Zulaufa0a98292020-09-18 09:30:10 -06001369 context.ResolveAccessRange(address_type, full_range, context.GetDstExternalTrackBack().barriers,
John Zulauf355e49b2020-04-24 15:11:15 -06001370 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001371 }
1372 }
1373}
1374
John Zulauf355e49b2020-04-24 15:11:15 -06001375void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1376 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1377 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) {
1378 const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1379 UpdateMemoryAccess(image, subresource_range, barrier_action);
1380}
1381
John Zulauf7635de32020-05-29 17:14:15 -06001382// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001383void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1384 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1385 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range,
1386 bool layout_transition, const ResourceUsageTag &tag) {
1387 if (layout_transition) {
1388 UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent,
1389 tag);
1390 ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope,
1391 subresource_range);
John Zulaufc9201222020-05-13 15:13:03 -06001392 } else {
1393 ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range);
John Zulauf355e49b2020-04-24 15:11:15 -06001394 }
John Zulauf355e49b2020-04-24 15:11:15 -06001395}
1396
John Zulauf7635de32020-05-29 17:14:15 -06001397// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001398void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier,
1399 const VkImageSubresourceRange &subresource_range, bool layout_transition,
1400 const ResourceUsageTag &tag) {
1401 ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope,
1402 subresource_range, layout_transition, tag);
1403}
1404
1405// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001406HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001407 if (!attach_view) return HazardResult();
1408 const auto image_state = attach_view->image_state.get();
1409 if (!image_state) return HazardResult();
1410
John Zulauf355e49b2020-04-24 15:11:15 -06001411 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001412 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001413
1414 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001415 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1416 const auto merged_barrier = MergeBarriers(track_back.barriers);
1417 HazardResult hazard =
1418 track_back.context->DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
1419 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001420 if (!hazard.hazard) {
1421 // The Async hazard check is against the current context's async set.
John Zulaufa0a98292020-09-18 09:30:10 -06001422 hazard = DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001423 attach_view->normalized_subresource_range, kDetectAsync);
1424 }
John Zulaufa0a98292020-09-18 09:30:10 -06001425
John Zulauf355e49b2020-04-24 15:11:15 -06001426 return hazard;
1427}
1428
1429// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1430bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1431
1432 const VkRenderPassBeginInfo *pRenderPassBegin,
1433 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1434 const char *func_name) const {
1435 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1436 bool skip = false;
1437 uint32_t subpass = 0;
1438 const auto &transitions = rp_state.subpass_transitions[subpass];
1439 if (transitions.size()) {
1440 const std::vector<AccessContext> empty_context_vector;
1441 // Create context we can use to validate against...
1442 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1443 const_cast<AccessContext *>(&cb_access_context_));
1444
1445 assert(pRenderPassBegin);
1446 if (nullptr == pRenderPassBegin) return skip;
1447
1448 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1449 assert(fb_state);
1450 if (nullptr == fb_state) return skip;
1451
1452 // Create a limited array of views (which we'll need to toss
1453 std::vector<const IMAGE_VIEW_STATE *> views;
1454 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
1455 const auto attachment_count = count_attachment.first;
1456 const auto *attachments = count_attachment.second;
1457 views.resize(attachment_count, nullptr);
1458 for (const auto &transition : transitions) {
1459 assert(transition.attachment < attachment_count);
1460 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
1461 }
1462
John Zulauf7635de32020-05-29 17:14:15 -06001463 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
1464 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001465 }
1466 return skip;
1467}
1468
locke-lunarg61870c22020-06-09 14:51:50 -06001469bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1470 const char *func_name) const {
1471 bool skip = false;
1472 const PIPELINE_STATE *pPipe = nullptr;
1473 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1474 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1475 if (!pPipe || !per_sets) {
1476 return skip;
1477 }
1478
1479 using DescriptorClass = cvdescriptorset::DescriptorClass;
1480 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1481 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1482 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1483 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1484
1485 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarg37047832020-06-12 13:44:45 -06001486 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001487 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1488 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001489 for (const auto &set_binding : stage_state.descriptor_uses) {
1490 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1491 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1492 set_binding.first.second);
1493 const auto descriptor_type = binding_it.GetType();
1494 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1495 auto array_idx = 0;
1496
1497 if (binding_it.IsVariableDescriptorCount()) {
1498 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1499 }
1500 SyncStageAccessIndex sync_index =
1501 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1502
1503 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1504 uint32_t index = i - index_range.start;
1505 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1506 switch (descriptor->GetClass()) {
1507 case DescriptorClass::ImageSampler:
1508 case DescriptorClass::Image: {
1509 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001510 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001511 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001512 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1513 img_view_state = image_sampler_descriptor->GetImageViewState();
1514 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001515 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001516 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1517 img_view_state = image_descriptor->GetImageViewState();
1518 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001519 }
1520 if (!img_view_state) continue;
1521 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1522 VkExtent3D extent = {};
1523 VkOffset3D offset = {};
1524 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1525 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1526 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1527 } else {
1528 extent = img_state->createInfo.extent;
1529 }
John Zulauf361fb532020-07-22 10:45:39 -06001530 HazardResult hazard;
1531 const auto &subresource_range = img_view_state->normalized_subresource_range;
1532 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1533 // Input attachments are subject to raster ordering rules
1534 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
1535 kAttachmentRasterOrder, offset, extent);
1536 } else {
1537 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1538 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001539 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001540 skip |= sync_state_->LogError(
1541 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001542 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1543 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001544 func_name, string_SyncHazard(hazard.hazard),
1545 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1546 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1547 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001548 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1549 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1550 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001551 }
1552 break;
1553 }
1554 case DescriptorClass::TexelBuffer: {
1555 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1556 if (!buf_view_state) continue;
1557 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001558 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001559 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001560 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001561 skip |= sync_state_->LogError(
1562 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001563 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1564 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001565 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1566 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1567 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001568 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1569 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1570 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001571 }
1572 break;
1573 }
1574 case DescriptorClass::GeneralBuffer: {
1575 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1576 auto buf_state = buffer_descriptor->GetBufferState();
1577 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001578 const ResourceAccessRange range =
1579 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001580 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001581 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001582 skip |= sync_state_->LogError(
1583 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001584 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1585 func_name, string_SyncHazard(hazard.hazard),
1586 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001587 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1588 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001589 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1590 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1591 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001592 }
1593 break;
1594 }
1595 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1596 default:
1597 break;
1598 }
1599 }
1600 }
1601 }
1602 return skip;
1603}
1604
1605void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1606 const ResourceUsageTag &tag) {
1607 const PIPELINE_STATE *pPipe = nullptr;
1608 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1609 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1610 if (!pPipe || !per_sets) {
1611 return;
1612 }
1613
1614 using DescriptorClass = cvdescriptorset::DescriptorClass;
1615 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1616 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1617 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1618 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1619
1620 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001621 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
1622 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1623 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001624 for (const auto &set_binding : stage_state.descriptor_uses) {
1625 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1626 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1627 set_binding.first.second);
1628 const auto descriptor_type = binding_it.GetType();
1629 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1630 auto array_idx = 0;
1631
1632 if (binding_it.IsVariableDescriptorCount()) {
1633 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1634 }
1635 SyncStageAccessIndex sync_index =
1636 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1637
1638 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1639 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1640 switch (descriptor->GetClass()) {
1641 case DescriptorClass::ImageSampler:
1642 case DescriptorClass::Image: {
1643 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1644 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1645 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1646 } else {
1647 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1648 }
1649 if (!img_view_state) continue;
1650 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1651 VkExtent3D extent = {};
1652 VkOffset3D offset = {};
1653 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1654 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1655 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1656 } else {
1657 extent = img_state->createInfo.extent;
1658 }
1659 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1660 offset, extent, tag);
1661 break;
1662 }
1663 case DescriptorClass::TexelBuffer: {
1664 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1665 if (!buf_view_state) continue;
1666 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001667 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001668 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1669 break;
1670 }
1671 case DescriptorClass::GeneralBuffer: {
1672 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1673 auto buf_state = buffer_descriptor->GetBufferState();
1674 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001675 const ResourceAccessRange range =
1676 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001677 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1678 break;
1679 }
1680 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1681 default:
1682 break;
1683 }
1684 }
1685 }
1686 }
1687}
1688
1689bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1690 bool skip = false;
1691 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1692 if (!pPipe) {
1693 return skip;
1694 }
1695
1696 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1697 const auto &binding_buffers_size = binding_buffers.size();
1698 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1699
1700 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1701 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1702 if (binding_description.binding < binding_buffers_size) {
1703 const auto &binding_buffer = binding_buffers[binding_description.binding];
1704 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1705
1706 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06001707 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1708 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001709 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1710 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001711 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001712 buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001713 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001714 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001715 }
1716 }
1717 }
1718 return skip;
1719}
1720
1721void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
1722 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1723 if (!pPipe) {
1724 return;
1725 }
1726 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1727 const auto &binding_buffers_size = binding_buffers.size();
1728 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1729
1730 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1731 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1732 if (binding_description.binding < binding_buffers_size) {
1733 const auto &binding_buffer = binding_buffers[binding_description.binding];
1734 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1735
1736 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06001737 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1738 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001739 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1740 }
1741 }
1742}
1743
1744bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1745 bool skip = false;
1746 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
1747
1748 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1749 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001750 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1751 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001752 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1753 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001754 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001755 index_buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001756 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001757 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001758 }
1759
1760 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1761 // We will detect more accurate range in the future.
1762 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1763 return skip;
1764}
1765
1766void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
1767 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return;
1768
1769 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1770 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001771 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1772 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001773 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1774
1775 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1776 // We will detect more accurate range in the future.
1777 RecordDrawVertex(UINT32_MAX, 0, tag);
1778}
1779
1780bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06001781 bool skip = false;
1782 if (!current_renderpass_context_) return skip;
1783 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1784 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1785 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001786}
1787
1788void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
locke-lunarg7077d502020-06-18 21:37:26 -06001789 if (current_renderpass_context_)
1790 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1791 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001792}
1793
John Zulauf355e49b2020-04-24 15:11:15 -06001794bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001795 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001796 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06001797 skip |=
1798 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001799
1800 return skip;
1801}
1802
1803bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1804 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001805 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001806 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001807 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06001808 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1809 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001810
1811 return skip;
1812}
1813
1814void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1815 assert(sync_state_);
1816 if (!cb_state_) return;
1817
1818 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06001819 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06001820 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06001821 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001822 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001823}
1824
John Zulauf355e49b2020-04-24 15:11:15 -06001825void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001826 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001827 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001828 current_context_ = &current_renderpass_context_->CurrentContext();
1829}
1830
John Zulauf355e49b2020-04-24 15:11:15 -06001831void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001832 assert(current_renderpass_context_);
1833 if (!current_renderpass_context_) return;
1834
John Zulauf1a224292020-06-30 14:52:13 -06001835 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001836 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001837 current_renderpass_context_ = nullptr;
1838}
1839
locke-lunarg61870c22020-06-09 14:51:50 -06001840bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1841 const VkRect2D &render_area, const char *func_name) const {
1842 bool skip = false;
locke-lunarg96dc9632020-06-10 17:22:18 -06001843 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001844 if (!pPipe ||
1845 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001846 return skip;
1847 }
1848 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001849 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1850 VkExtent3D extent = CastTo3D(render_area.extent);
1851 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001852
John Zulauf1a224292020-06-30 14:52:13 -06001853 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001854 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001855 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1856 for (const auto location : list) {
1857 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1858 continue;
1859 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001860 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1861 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06001862 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001863 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001864 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001865 func_name, string_SyncHazard(hazard.hazard),
1866 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1867 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001868 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001869 }
1870 }
1871 }
locke-lunarg37047832020-06-12 13:44:45 -06001872
1873 // PHASE1 TODO: Add layout based read/vs. write selection.
1874 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1875 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1876 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001877 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001878 bool depth_write = false, stencil_write = false;
1879
1880 // PHASE1 TODO: These validation should be in core_checks.
1881 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1882 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1883 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1884 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1885 depth_write = true;
1886 }
1887 // PHASE1 TODO: It needs to check if stencil is writable.
1888 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1889 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1890 // PHASE1 TODO: These validation should be in core_checks.
1891 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1892 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1893 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1894 stencil_write = true;
1895 }
1896
1897 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1898 if (depth_write) {
1899 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001900 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1901 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001902 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001903 skip |= sync_state.LogError(
1904 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001905 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001906 func_name, string_SyncHazard(hazard.hazard),
1907 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1908 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001909 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001910 }
1911 }
1912 if (stencil_write) {
1913 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001914 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1915 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001916 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001917 skip |= sync_state.LogError(
1918 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001919 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001920 func_name, string_SyncHazard(hazard.hazard),
1921 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1922 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001923 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001924 }
locke-lunarg61870c22020-06-09 14:51:50 -06001925 }
1926 }
1927 return skip;
1928}
1929
locke-lunarg96dc9632020-06-10 17:22:18 -06001930void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1931 const ResourceUsageTag &tag) {
1932 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001933 if (!pPipe ||
1934 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001935 return;
1936 }
1937 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001938 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1939 VkExtent3D extent = CastTo3D(render_area.extent);
1940 VkOffset3D offset = CastTo3D(render_area.offset);
1941
John Zulauf1a224292020-06-30 14:52:13 -06001942 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001943 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001944 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1945 for (const auto location : list) {
1946 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1947 continue;
1948 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001949 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
1950 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001951 }
1952 }
locke-lunarg37047832020-06-12 13:44:45 -06001953
1954 // PHASE1 TODO: Add layout based read/vs. write selection.
1955 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1956 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1957 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001958 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001959 bool depth_write = false, stencil_write = false;
1960
1961 // PHASE1 TODO: These validation should be in core_checks.
1962 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1963 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1964 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1965 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1966 depth_write = true;
1967 }
1968 // PHASE1 TODO: It needs to check if stencil is writable.
1969 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1970 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1971 // PHASE1 TODO: These validation should be in core_checks.
1972 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1973 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1974 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1975 stencil_write = true;
1976 }
1977
1978 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1979 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001980 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1981 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001982 }
1983 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001984 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1985 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001986 }
locke-lunarg61870c22020-06-09 14:51:50 -06001987 }
1988}
1989
John Zulauf1507ee42020-05-18 11:33:09 -06001990bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
1991 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001992 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001993 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06001994 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1995 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001996 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1997 func_name);
1998
John Zulauf355e49b2020-04-24 15:11:15 -06001999 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002000 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06002001 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
2002 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
2003 return skip;
2004}
2005bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
2006 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002007 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002008 bool skip = false;
2009 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2010 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002011 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2012 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06002013 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002014 return skip;
2015}
2016
John Zulauf7635de32020-05-29 17:14:15 -06002017AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2018 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2019}
2020
2021bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2022 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002023 bool skip = false;
2024
John Zulauf7635de32020-05-29 17:14:15 -06002025 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2026 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2027 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2028 // to apply and only copy then, if this proves a hot spot.
2029 std::unique_ptr<AccessContext> proxy_for_current;
2030
John Zulauf355e49b2020-04-24 15:11:15 -06002031 // Validate the "finalLayout" transitions to external
2032 // Get them from where there we're hidding in the extra entry.
2033 const auto &final_transitions = rp_state_->subpass_transitions.back();
2034 for (const auto &transition : final_transitions) {
2035 const auto &attach_view = attachment_views_[transition.attachment];
2036 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2037 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002038 auto *context = trackback.context;
2039
2040 if (transition.prev_pass == current_subpass_) {
2041 if (!proxy_for_current) {
2042 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2043 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2044 }
2045 context = proxy_for_current.get();
2046 }
2047
John Zulaufa0a98292020-09-18 09:30:10 -06002048 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2049 const auto merged_barrier = MergeBarriers(trackback.barriers);
2050 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope,
2051 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2052 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002053 if (hazard.hazard) {
2054 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2055 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf389c34b2020-07-28 11:19:35 -06002056 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002057 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf389c34b2020-07-28 11:19:35 -06002058 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf37ceaed2020-07-03 16:18:15 -06002059 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002060 }
2061 }
2062 return skip;
2063}
2064
2065void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2066 // Add layout transitions...
2067 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
2068 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -06002069 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -06002070 for (const auto &transition : transitions) {
2071 const auto attachment_view = attachment_views_[transition.attachment];
2072 if (!attachment_view) continue;
2073 const auto image = attachment_view->image_state.get();
2074 if (!image) continue;
2075
John Zulaufa0a98292020-09-18 09:30:10 -06002076 const auto *trackback = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
2077 assert(trackback);
2078 const auto merged_barrier = MergeBarriers(trackback->barriers);
John Zulaufc9201222020-05-13 15:13:03 -06002079 auto insert_pair = view_seen.insert(attachment_view);
2080 if (insert_pair.second) {
2081 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
John Zulaufa0a98292020-09-18 09:30:10 -06002082 subpass_context.ApplyImageBarrier(*image, merged_barrier, attachment_view->normalized_subresource_range, true, tag);
John Zulaufc9201222020-05-13 15:13:03 -06002083
2084 } else {
2085 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
2086 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
John Zulaufa0a98292020-09-18 09:30:10 -06002087 auto barrier_to_transition = merged_barrier;
John Zulaufc9201222020-05-13 15:13:03 -06002088 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
John Zulaufa0a98292020-09-18 09:30:10 -06002089 subpass_context.ApplyImageBarrier(*image, barrier_to_transition, attachment_view->normalized_subresource_range, false,
2090 tag);
John Zulaufc9201222020-05-13 15:13:03 -06002091 }
John Zulauf355e49b2020-04-24 15:11:15 -06002092 }
2093}
2094
John Zulauf1507ee42020-05-18 11:33:09 -06002095void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2096 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2097 auto &subpass_context = subpass_contexts_[current_subpass_];
2098 VkExtent3D extent = CastTo3D(render_area.extent);
2099 VkOffset3D offset = CastTo3D(render_area.offset);
2100
2101 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2102 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2103 if (attachment_views_[i] == nullptr) continue; // UNUSED
2104 const auto &view = *attachment_views_[i];
2105 const IMAGE_STATE *image = view.image_state.get();
2106 if (image == nullptr) continue;
2107
2108 const auto &ci = attachment_ci[i];
2109 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002110 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002111 const bool is_color = !(has_depth || has_stencil);
2112
2113 if (is_color) {
2114 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2115 extent, tag);
2116 } else {
2117 auto update_range = view.normalized_subresource_range;
2118 if (has_depth) {
2119 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2120 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2121 }
2122 if (has_stencil) {
2123 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2124 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2125 tag);
2126 }
2127 }
2128 }
2129 }
2130}
2131
John Zulauf355e49b2020-04-24 15:11:15 -06002132void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002133 const AccessContext *external_context, VkQueueFlags queue_flags,
2134 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002135 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002136 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002137 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2138 // Add this for all subpasses here so that they exsist during next subpass validation
2139 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002140 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002141 }
2142 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2143
2144 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002145 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002146}
John Zulauf1507ee42020-05-18 11:33:09 -06002147
2148void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002149 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2150 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002151 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002152
John Zulauf355e49b2020-04-24 15:11:15 -06002153 current_subpass_++;
2154 assert(current_subpass_ < subpass_contexts_.size());
2155 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002156 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002157}
2158
John Zulauf1a224292020-06-30 14:52:13 -06002159void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2160 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002161 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002162 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002163 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002164
John Zulauf355e49b2020-04-24 15:11:15 -06002165 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002166 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002167
2168 // Add the "finalLayout" transitions to external
2169 // Get them from where there we're hidding in the extra entry.
2170 const auto &final_transitions = rp_state_->subpass_transitions.back();
2171 for (const auto &transition : final_transitions) {
2172 const auto &attachment = attachment_views_[transition.attachment];
2173 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002174 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufa0a98292020-09-18 09:30:10 -06002175 // Since this is a layout transition vs. a subpass, we can safely merge the barriers, as there are no
2176 // chaining effects after the layout transition (write operation) nukes the prior depenency chains
2177 external_context->ApplyImageBarrier(*attachment->image_state, MergeBarriers(last_trackback.barriers),
John Zulauf1a224292020-06-30 14:52:13 -06002178 attachment->normalized_subresource_range, true, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002179 }
2180}
2181
John Zulauf3d84f1b2020-03-09 13:33:25 -06002182SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2183 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2184 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2185 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2186 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2187 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2188 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2189}
2190
John Zulaufa0a98292020-09-18 09:30:10 -06002191void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers) {
2192 for (const auto &barrier : barriers) {
2193 ApplyMemoryAccessBarrier(true, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope,
2194 barrier.dst_access_scope);
2195 }
2196 ApplyExecutionBarriers(barriers);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002197}
2198
John Zulauf9cb530d2019-09-30 14:14:10 -06002199HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2200 HazardResult hazard;
2201 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002202 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002203 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002204 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002205 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002206 }
2207 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002208 // Write operation:
2209 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2210 // If reads exists -- test only against them because either:
2211 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2212 // * 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
2213 // the current write happens after the reads, so just test the write against the reades
2214 // Otherwise test against last_write
2215 //
2216 // Look for casus belli for WAR
2217 if (last_read_count) {
2218 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2219 const auto &read_access = last_reads[read_index];
2220 if (IsReadHazard(usage_stage, read_access)) {
2221 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2222 break;
2223 }
2224 }
John Zulauf361fb532020-07-22 10:45:39 -06002225 } else if (last_write && IsWriteHazard(usage)) {
2226 // Write-After-Write check -- if we have a previous write to test against
2227 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002228 }
2229 }
2230 return hazard;
2231}
2232
John Zulauf69133422020-05-20 14:55:53 -06002233HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2234 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2235 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002236 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002237 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002238 const bool input_attachment_ordering = 0 != (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
2239 const bool last_write_is_ordered = 0 != (last_write & ordering.access_scope);
2240 if (IsRead(usage_bit)) {
2241 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2242 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2243 if (is_raw_hazard) {
2244 // NOTE: we know last_write is non-zero
2245 // See if the ordering rules save us from the simple RAW check above
2246 // First check to see if the current usage is covered by the ordering rules
2247 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2248 const bool usage_is_ordered =
2249 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2250 if (usage_is_ordered) {
2251 // Now see of the most recent write (or a subsequent read) are ordered
2252 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2253 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002254 }
2255 }
John Zulauf4285ee92020-09-23 10:20:52 -06002256 if (is_raw_hazard) {
2257 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2258 }
John Zulauf361fb532020-07-22 10:45:39 -06002259 } else {
2260 // Only check for WAW if there are no reads since last_write
John Zulauf4285ee92020-09-23 10:20:52 -06002261 bool usage_write_is_ordered = 0 != (usage_bit & ordering.access_scope);
John Zulauf361fb532020-07-22 10:45:39 -06002262 if (last_read_count) {
John Zulauf361fb532020-07-22 10:45:39 -06002263 // Look for any WAR hazards outside the ordered set of stages
John Zulauf4285ee92020-09-23 10:20:52 -06002264 VkPipelineStageFlags ordered_stages = 0;
2265 if (usage_write_is_ordered) {
2266 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2267 ordered_stages = GetOrderedStages(ordering);
2268 }
2269 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2270 if ((ordered_stages & last_read_stages) != last_read_stages) {
2271 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2272 const auto &read_access = last_reads[read_index];
2273 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2274 if (IsReadHazard(usage_stage, read_access)) {
2275 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2276 break;
2277 }
John Zulaufd14743a2020-07-03 09:42:39 -06002278 }
2279 }
John Zulauf4285ee92020-09-23 10:20:52 -06002280 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
2281 if (last_write && IsWriteHazard(usage_bit)) {
2282 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002283 }
John Zulauf69133422020-05-20 14:55:53 -06002284 }
2285 }
2286 return hazard;
2287}
2288
John Zulauf2f952d22020-02-10 11:34:51 -07002289// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06002290HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002291 HazardResult hazard;
2292 auto usage = FlagBit(usage_index);
2293 if (IsRead(usage)) {
2294 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002295 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002296 }
2297 } else {
2298 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002299 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002300 } else if (last_read_count > 0) {
John Zulauf4285ee92020-09-23 10:20:52 -06002301 // Any read could be reported, so we'll just pick the first one arbitrarily
John Zulauf59e25072020-07-17 10:55:21 -06002302 hazard.Set(this, usage_index, WRITE_RACING_READ, last_reads[0].access, last_reads[0].tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002303 }
2304 }
2305 return hazard;
2306}
2307
John Zulauf36bcf6a2020-02-03 15:12:52 -07002308HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
2309 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002310 // Only supporting image layout transitions for now
2311 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2312 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002313 // only test for WAW if there no intervening read operations.
2314 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2315 if (last_read_count) {
John Zulauf355e49b2020-04-24 15:11:15 -06002316 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002317 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002318 const auto &read_access = last_reads[read_index];
2319 // If the read stage is not in the src sync sync
2320 // *AND* not execution chained with an existing sync barrier (that's the or)
2321 // then the barrier access is unsafe (R/W after R)
2322 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002323 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002324 break;
2325 }
2326 }
John Zulauf361fb532020-07-22 10:45:39 -06002327 } else if (last_write) {
2328 // If the previous write is *not* in the 1st access scope
2329 // *AND* the current barrier is not in the dependency chain
2330 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2331 // then the barrier access is unsafe (R/W after W)
2332 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
2333 // TODO: Do we need a difference hazard name for this?
2334 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2335 }
John Zulaufd14743a2020-07-03 09:42:39 -06002336 }
John Zulauf361fb532020-07-22 10:45:39 -06002337
John Zulauf0cb5be22020-01-23 12:18:22 -07002338 return hazard;
2339}
2340
John Zulauf5f13a792020-03-10 07:31:21 -06002341// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2342// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2343// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2344void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2345 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002346 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2347 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002348 *this = other;
2349 } else if (!other.write_tag.IsBefore(write_tag)) {
2350 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2351 // dependency chaining logic or any stage expansion)
2352 write_barriers |= other.write_barriers;
2353
John Zulaufd14743a2020-07-03 09:42:39 -06002354 // Merge the read states
John Zulauf4285ee92020-09-23 10:20:52 -06002355 const auto pre_merge_count = last_read_count;
2356 const auto pre_merge_stages = last_read_stages;
John Zulauf5f13a792020-03-10 07:31:21 -06002357 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2358 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002359 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002360 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002361 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2362 // but we should wait on profiling data for that.
2363 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002364 auto &my_read = last_reads[my_read_index];
2365 if (other_read.stage == my_read.stage) {
2366 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002367 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002368 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002369 my_read.tag = other_read.tag;
2370 if (my_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
2371 // Since I'm overwriting the fragement stage read, also update the input attachment info
2372 // as this is the only stage that affects it.
2373 input_attachment_stage = other.input_attachment_stage;
2374 }
John Zulauf5f13a792020-03-10 07:31:21 -06002375 }
John Zulauf4285ee92020-09-23 10:20:52 -06002376 // TODO: Phase 2 -- review the state merge logic to avoid false negative from the OR'ing of the barriers
2377 // May require tracking more than one access per stage.
John Zulauf5f13a792020-03-10 07:31:21 -06002378 my_read.barriers |= other_read.barriers;
2379 break;
2380 }
2381 }
2382 } else {
2383 // The other read stage doesn't exist in this, so add it.
2384 last_reads[last_read_count] = other_read;
2385 last_read_count++;
2386 last_read_stages |= other_read.stage;
John Zulauf4285ee92020-09-23 10:20:52 -06002387 if (other_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
2388 input_attachment_stage = other.input_attachment_stage;
2389 }
John Zulauf5f13a792020-03-10 07:31:21 -06002390 }
2391 }
John Zulauf361fb532020-07-22 10:45:39 -06002392 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002393 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2394 // ignore it.
John Zulauf5f13a792020-03-10 07:31:21 -06002395}
2396
John Zulauf9cb530d2019-09-30 14:14:10 -06002397void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2398 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2399 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002400 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002401 // Mulitple outstanding reads may be of interest and do dependency chains independently
2402 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2403 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002404 uint32_t update_index = kStageCount;
John Zulauf9cb530d2019-09-30 14:14:10 -06002405 if (usage_stage & last_read_stages) {
2406 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf4285ee92020-09-23 10:20:52 -06002407 if (last_reads[read_index].stage == usage_stage) {
2408 update_index = read_index;
John Zulauf9cb530d2019-09-30 14:14:10 -06002409 break;
2410 }
2411 }
John Zulauf4285ee92020-09-23 10:20:52 -06002412 assert(update_index < last_read_count);
John Zulauf9cb530d2019-09-30 14:14:10 -06002413 } else {
John Zulauf9cb530d2019-09-30 14:14:10 -06002414 assert(last_read_count < last_reads.size());
John Zulauf4285ee92020-09-23 10:20:52 -06002415 update_index = last_read_count++;
John Zulauf9cb530d2019-09-30 14:14:10 -06002416 last_read_stages |= usage_stage;
2417 }
John Zulauf4285ee92020-09-23 10:20:52 -06002418 last_reads[update_index].Set(usage_stage, usage_bit, 0, tag);
2419
2420 // Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
2421 if (usage_stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
2422 if (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) {
2423 input_attachment_stage = usage_stage;
2424 } else {
2425 input_attachment_stage = kInvalidAttachmentStage;
2426 }
2427 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002428 } else {
2429 // Assume write
2430 // TODO determine what to do with READ-WRITE operations if any
John Zulaufd14743a2020-07-03 09:42:39 -06002431 // Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
John Zulauf9cb530d2019-09-30 14:14:10 -06002432 // if the last_reads/last_write were unsafe, we've reported them,
2433 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
2434 last_read_count = 0;
2435 last_read_stages = 0;
John Zulauf361fb532020-07-22 10:45:39 -06002436 read_execution_barriers = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002437 input_attachment_stage = kInvalidAttachmentStage; // Denotes no outstanding input attachment read after the last write.
John Zulaufd14743a2020-07-03 09:42:39 -06002438
John Zulauf9cb530d2019-09-30 14:14:10 -06002439 write_barriers = 0;
2440 write_dependency_chain = 0;
2441 write_tag = tag;
2442 last_write = usage_bit;
2443 }
2444}
John Zulauf5f13a792020-03-10 07:31:21 -06002445
John Zulaufa0a98292020-09-18 09:30:10 -06002446// TODO: Leave "old style" execution barrier code until PipelineBarrier is corrected for true unordered barrier application
John Zulauf9cb530d2019-09-30 14:14:10 -06002447void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
2448 // Execution Barriers only protect read operations
2449 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2450 ReadState &access = last_reads[read_index];
2451 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2452 if (srcStageMask & (access.stage | access.barriers)) {
2453 access.barriers |= dstStageMask;
John Zulauf361fb532020-07-22 10:45:39 -06002454 read_execution_barriers |= dstStageMask;
John Zulauf9cb530d2019-09-30 14:14:10 -06002455 }
2456 }
John Zulauf361fb532020-07-22 10:45:39 -06002457 if (write_dependency_chain & srcStageMask) {
2458 write_dependency_chain |= dstStageMask;
2459 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002460}
2461
John Zulaufa0a98292020-09-18 09:30:10 -06002462void ResourceAccessState::ApplyExecutionBarriers(const std::vector<SyncBarrier> &barriers) {
2463 // Since all execution chains are unordered, apply before update, accumulating update in pending_
2464 // s.t. we don't create unintended dependency chaings.
2465 // For reads the barriers function as the dependency chain
2466 VkPipelineStageFlags pending_write_chain = 0;
2467 VkPipelineStageFlags pending_input_attacment_barriers = 0;
2468 std::array<VkPipelineStageFlags, kStageCount> pending_read_barriers;
2469 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2470 pending_read_barriers[read_index] = 0;
2471 }
2472
2473 for (const auto &barrier : barriers) {
2474 const VkPipelineStageFlags src_scope = barrier.src_exec_scope;
2475 const VkPipelineStageFlags dst_scope = barrier.dst_exec_scope;
2476 // Execution Barriers only protect read operations
2477 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2478 const ReadState &access = last_reads[read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002479 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync
2480 // scope
John Zulaufa0a98292020-09-18 09:30:10 -06002481 if (src_scope & (access.stage | access.barriers)) {
2482 pending_read_barriers[read_index] |= dst_scope;
2483 }
2484 }
John Zulaufa0a98292020-09-18 09:30:10 -06002485 if (InSourceScopeOrChain(src_scope, barrier.src_access_scope)) {
2486 pending_write_chain |= dst_scope;
2487 }
2488 }
2489
2490 // Apply accumulated pending changes to the depenendency chains and barriers
2491 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2492 last_reads[read_index].barriers |= pending_read_barriers[read_index];
2493 read_execution_barriers |= pending_read_barriers[read_index];
2494 }
John Zulaufa0a98292020-09-18 09:30:10 -06002495 read_execution_barriers |= pending_input_attacment_barriers;
2496 write_dependency_chain |= pending_write_chain;
2497}
2498
2499void ResourceAccessState::ApplyMemoryAccessBarrier(bool multi_dep, VkPipelineStageFlags src_exec_scope,
2500 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
2501 SyncStageAccessFlags dst_access_scope) {
2502 // We update just the write barrier side of the execution and memory barrier. Updating the dependency chain would
2503 // create chaining between batches of barriers, which isn't wasn't intended.
2504 // The callers is responsible for coming back and updating the dependency chain information after the memory access
2505 // batch is complete.
John Zulauf9cb530d2019-09-30 14:14:10 -06002506 // The || implements the "dependency chain" logic for this barrier
John Zulaufa0a98292020-09-18 09:30:10 -06002507 if (InSourceScopeOrChain(src_exec_scope, src_access_scope)) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002508 write_barriers |= dst_access_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002509 if (!multi_dep) {
2510 write_dependency_chain |= dst_exec_scope;
2511 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002512 }
2513}
2514
John Zulauf59e25072020-07-17 10:55:21 -06002515// This should be just Bits or Index, but we don't have an invalid state for Index
2516VkPipelineStageFlags ResourceAccessState::GetReadBarriers(SyncStageAccessFlags usage_bit) const {
2517 VkPipelineStageFlags barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06002518
2519 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2520 const auto &read_access = last_reads[read_index];
2521 if (read_access.access & usage_bit) {
2522 barriers = read_access.barriers;
2523 break;
John Zulauf59e25072020-07-17 10:55:21 -06002524 }
2525 }
John Zulauf4285ee92020-09-23 10:20:52 -06002526
John Zulauf59e25072020-07-17 10:55:21 -06002527 return barriers;
2528}
2529
John Zulauf4285ee92020-09-23 10:20:52 -06002530inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlagBits usage_stage, SyncStageAccessFlagBits usage) const {
2531 assert(IsRead(usage));
2532 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
2533 // * the previous reads are not hazards, and thus last_write must be visible and available to
2534 // any reads that happen after.
2535 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2536 // the current read will be also not be a hazard, thus reporting a hazard here adds no needed information.
2537 return (0 != last_write) && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
2538}
2539
2540bool ResourceAccessState::IsWARHazard(VkPipelineStageFlagBits usage_stage, SyncStageAccessFlagBits usage) const { return false; }
2541
2542VkPipelineStageFlags ResourceAccessState::GetOrderedStages(const SyncOrderingBarrier &ordering) const {
2543 // Whether the stage are in the ordering scope only matters if the current write is ordered
2544 VkPipelineStageFlags ordered_stages = last_read_stages & ordering.exec_scope;
2545 // Special input attachment handling as always (not encoded in exec_scop)
2546 const bool input_attachment_ordering = ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT;
2547 if (input_attachment_ordering && (input_attachment_stage != kInvalidAttachmentStage)) {
2548 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
2549 ordered_stages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
2550 }
2551
2552 return ordered_stages;
2553}
2554
2555inline ResourceAccessState::ReadState *ResourceAccessState::GetReadStateForStage(VkPipelineStageFlagBits stage,
2556 uint32_t search_limit) {
2557 ReadState *read_state = nullptr;
2558 search_limit = std::min(search_limit, last_read_count);
2559 for (uint32_t i = 0; i < search_limit; i++) {
2560 if (last_reads[i].stage == stage) {
2561 read_state = &last_reads[i];
2562 break;
2563 }
2564 }
2565 return read_state;
2566}
2567
John Zulaufd1f85d42020-04-15 12:23:15 -06002568void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002569 auto *access_context = GetAccessContextNoInsert(command_buffer);
2570 if (access_context) {
2571 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002572 }
2573}
2574
John Zulaufd1f85d42020-04-15 12:23:15 -06002575void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2576 auto access_found = cb_access_state.find(command_buffer);
2577 if (access_found != cb_access_state.end()) {
2578 access_found->second->Reset();
2579 cb_access_state.erase(access_found);
2580 }
2581}
2582
John Zulauf540266b2020-04-06 18:54:53 -06002583void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002584 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
2585 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002586 const VkMemoryBarrier *pMemoryBarriers) {
2587 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07002588 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002589 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002590 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002591}
2592
John Zulauf540266b2020-04-06 18:54:53 -06002593void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002594 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2595 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002596 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002597 for (uint32_t index = 0; index < barrier_count; index++) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002598 auto barrier = barriers[index]; // barrier is a copy
John Zulauf9cb530d2019-09-30 14:14:10 -06002599 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2600 if (!buffer) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002601 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
2602 const ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002603 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2604 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2605 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2606 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002607 }
2608}
2609
John Zulauf540266b2020-04-06 18:54:53 -06002610void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2611 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2612 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002613 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002614 for (uint32_t index = 0; index < barrier_count; index++) {
2615 const auto &barrier = barriers[index];
2616 const auto *image = Get<IMAGE_STATE>(barrier.image);
2617 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002618 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002619 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2620 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2621 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2622 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
2623 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002624 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002625}
2626
2627bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2628 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2629 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002630 const auto *cb_context = GetAccessContext(commandBuffer);
2631 assert(cb_context);
2632 if (!cb_context) return skip;
2633 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002634
John Zulauf3d84f1b2020-03-09 13:33:25 -06002635 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002636 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002637 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002638
2639 for (uint32_t region = 0; region < regionCount; region++) {
2640 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002641 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002642 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002643 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002644 if (hazard.hazard) {
2645 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002646 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002647 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002648 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002649 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002650 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002651 }
John Zulauf16adfc92020-04-08 10:28:33 -06002652 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002653 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06002654 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002655 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002656 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002657 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002658 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002659 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002660 }
2661 }
2662 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002663 }
2664 return skip;
2665}
2666
2667void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2668 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002669 auto *cb_context = GetAccessContext(commandBuffer);
2670 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002671 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002672 auto *context = cb_context->GetCurrentAccessContext();
2673
John Zulauf9cb530d2019-09-30 14:14:10 -06002674 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002675 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002676
2677 for (uint32_t region = 0; region < regionCount; region++) {
2678 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002679 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002680 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002681 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002682 }
John Zulauf16adfc92020-04-08 10:28:33 -06002683 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002684 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002685 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002686 }
2687 }
2688}
2689
Jeff Leger178b1e52020-10-05 12:22:23 -04002690bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
2691 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
2692 bool skip = false;
2693 const auto *cb_context = GetAccessContext(commandBuffer);
2694 assert(cb_context);
2695 if (!cb_context) return skip;
2696 const auto *context = cb_context->GetCurrentAccessContext();
2697
2698 // If we have no previous accesses, we have no hazards
2699 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2700 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2701
2702 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2703 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2704 if (src_buffer) {
2705 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2706 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
2707 if (hazard.hazard) {
2708 // TODO -- add tag information to log msg when useful.
2709 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
2710 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
2711 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
2712 region, string_UsageTag(hazard).c_str());
2713 }
2714 }
2715 if (dst_buffer && !skip) {
2716 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2717 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
2718 if (hazard.hazard) {
2719 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
2720 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
2721 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
2722 region, string_UsageTag(hazard).c_str());
2723 }
2724 }
2725 if (skip) break;
2726 }
2727 return skip;
2728}
2729
2730void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
2731 auto *cb_context = GetAccessContext(commandBuffer);
2732 assert(cb_context);
2733 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
2734 auto *context = cb_context->GetCurrentAccessContext();
2735
2736 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2737 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2738
2739 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2740 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2741 if (src_buffer) {
2742 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2743 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
2744 }
2745 if (dst_buffer) {
2746 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2747 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
2748 }
2749 }
2750}
2751
John Zulauf5c5e88d2019-12-26 11:22:02 -07002752bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2753 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2754 const VkImageCopy *pRegions) const {
2755 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002756 const auto *cb_access_context = GetAccessContext(commandBuffer);
2757 assert(cb_access_context);
2758 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002759
John Zulauf3d84f1b2020-03-09 13:33:25 -06002760 const auto *context = cb_access_context->GetCurrentAccessContext();
2761 assert(context);
2762 if (!context) return skip;
2763
2764 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2765 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002766 for (uint32_t region = 0; region < regionCount; region++) {
2767 const auto &copy_region = pRegions[region];
2768 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002769 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002770 copy_region.srcOffset, copy_region.extent);
2771 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002772 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002773 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002774 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002775 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002776 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002777 }
2778
2779 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002780 VkExtent3D dst_copy_extent =
2781 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002782 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002783 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002784 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002785 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002786 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002787 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002788 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002789 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002790 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002791 }
2792 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002793
John Zulauf5c5e88d2019-12-26 11:22:02 -07002794 return skip;
2795}
2796
2797void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2798 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2799 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002800 auto *cb_access_context = GetAccessContext(commandBuffer);
2801 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002802 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002803 auto *context = cb_access_context->GetCurrentAccessContext();
2804 assert(context);
2805
John Zulauf5c5e88d2019-12-26 11:22:02 -07002806 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002807 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002808
2809 for (uint32_t region = 0; region < regionCount; region++) {
2810 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002811 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002812 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2813 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002814 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002815 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002816 VkExtent3D dst_copy_extent =
2817 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002818 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2819 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002820 }
2821 }
2822}
2823
Jeff Leger178b1e52020-10-05 12:22:23 -04002824bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
2825 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
2826 bool skip = false;
2827 const auto *cb_access_context = GetAccessContext(commandBuffer);
2828 assert(cb_access_context);
2829 if (!cb_access_context) return skip;
2830
2831 const auto *context = cb_access_context->GetCurrentAccessContext();
2832 assert(context);
2833 if (!context) return skip;
2834
2835 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2836 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2837 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2838 const auto &copy_region = pCopyImageInfo->pRegions[region];
2839 if (src_image) {
2840 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
2841 copy_region.srcOffset, copy_region.extent);
2842 if (hazard.hazard) {
2843 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
2844 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
2845 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
2846 region, string_UsageTag(hazard).c_str());
2847 }
2848 }
2849
2850 if (dst_image) {
2851 VkExtent3D dst_copy_extent =
2852 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2853 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
2854 copy_region.dstOffset, dst_copy_extent);
2855 if (hazard.hazard) {
2856 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
2857 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
2858 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
2859 region, string_UsageTag(hazard).c_str());
2860 }
2861 if (skip) break;
2862 }
2863 }
2864
2865 return skip;
2866}
2867
2868void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
2869 auto *cb_access_context = GetAccessContext(commandBuffer);
2870 assert(cb_access_context);
2871 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
2872 auto *context = cb_access_context->GetCurrentAccessContext();
2873 assert(context);
2874
2875 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2876 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2877
2878 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2879 const auto &copy_region = pCopyImageInfo->pRegions[region];
2880 if (src_image) {
2881 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2882 copy_region.extent, tag);
2883 }
2884 if (dst_image) {
2885 VkExtent3D dst_copy_extent =
2886 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2887 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2888 dst_copy_extent, tag);
2889 }
2890 }
2891}
2892
John Zulauf9cb530d2019-09-30 14:14:10 -06002893bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2894 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2895 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2896 uint32_t bufferMemoryBarrierCount,
2897 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2898 uint32_t imageMemoryBarrierCount,
2899 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2900 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002901 const auto *cb_access_context = GetAccessContext(commandBuffer);
2902 assert(cb_access_context);
2903 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002904
John Zulauf3d84f1b2020-03-09 13:33:25 -06002905 const auto *context = cb_access_context->GetCurrentAccessContext();
2906 assert(context);
2907 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002908
John Zulauf3d84f1b2020-03-09 13:33:25 -06002909 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002910 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2911 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002912 // Validate Image Layout transitions
2913 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2914 const auto &barrier = pImageMemoryBarriers[index];
2915 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2916 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2917 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002918 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002919 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002920 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002921 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002922 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002923 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002924 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002925 }
2926 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002927
2928 return skip;
2929}
2930
2931void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2932 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2933 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2934 uint32_t bufferMemoryBarrierCount,
2935 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2936 uint32_t imageMemoryBarrierCount,
2937 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002938 auto *cb_access_context = GetAccessContext(commandBuffer);
2939 assert(cb_access_context);
2940 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002941 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002942 auto access_context = cb_access_context->GetCurrentAccessContext();
2943 assert(access_context);
2944 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06002945
John Zulauf3d84f1b2020-03-09 13:33:25 -06002946 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002947 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002948 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002949 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
2950 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2951 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002952 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
2953 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002954 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06002955 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002956
2957 // 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 -06002958 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07002959 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06002960}
2961
2962void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
2963 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
2964 // The state tracker sets up the device state
2965 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
2966
John Zulauf5f13a792020-03-10 07:31:21 -06002967 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
2968 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06002969 // TODO: Find a good way to do this hooklessly.
2970 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
2971 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
2972 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
2973
John Zulaufd1f85d42020-04-15 12:23:15 -06002974 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2975 sync_device_state->ResetCommandBufferCallback(command_buffer);
2976 });
2977 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2978 sync_device_state->FreeCommandBufferCallback(command_buffer);
2979 });
John Zulauf9cb530d2019-09-30 14:14:10 -06002980}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002981
John Zulauf355e49b2020-04-24 15:11:15 -06002982bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2983 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
2984 bool skip = false;
2985 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
2986 auto cb_context = GetAccessContext(commandBuffer);
2987
2988 if (rp_state && cb_context) {
2989 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
2990 }
2991
2992 return skip;
2993}
2994
2995bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2996 VkSubpassContents contents) const {
2997 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2998 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2999 subpass_begin_info.contents = contents;
3000 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
3001 return skip;
3002}
3003
3004bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3005 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
3006 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3007 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
3008 return skip;
3009}
3010
3011bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3012 const VkRenderPassBeginInfo *pRenderPassBegin,
3013 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
3014 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3015 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
3016 return skip;
3017}
3018
John Zulauf3d84f1b2020-03-09 13:33:25 -06003019void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3020 VkResult result) {
3021 // The state tracker sets up the command buffer state
3022 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3023
3024 // Create/initialize the structure that trackers accesses at the command buffer scope.
3025 auto cb_access_context = GetAccessContext(commandBuffer);
3026 assert(cb_access_context);
3027 cb_access_context->Reset();
3028}
3029
3030void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06003031 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003032 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003033 if (cb_context) {
3034 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003035 }
3036}
3037
3038void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3039 VkSubpassContents contents) {
3040 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3041 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3042 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003043 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003044}
3045
3046void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3047 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3048 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003049 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003050}
3051
3052void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3053 const VkRenderPassBeginInfo *pRenderPassBegin,
3054 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3055 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003056 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
3057}
3058
3059bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
3060 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
3061 bool skip = false;
3062
3063 auto cb_context = GetAccessContext(commandBuffer);
3064 assert(cb_context);
3065 auto cb_state = cb_context->GetCommandBufferState();
3066 if (!cb_state) return skip;
3067
3068 auto rp_state = cb_state->activeRenderPass;
3069 if (!rp_state) return skip;
3070
3071 skip |= cb_context->ValidateNextSubpass(func_name);
3072
3073 return skip;
3074}
3075
3076bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3077 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
3078 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3079 subpass_begin_info.contents = contents;
3080 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
3081 return skip;
3082}
3083
3084bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
3085 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3086 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3087 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
3088 return skip;
3089}
3090
3091bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3092 const VkSubpassEndInfo *pSubpassEndInfo) const {
3093 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3094 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
3095 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003096}
3097
3098void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003099 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003100 auto cb_context = GetAccessContext(commandBuffer);
3101 assert(cb_context);
3102 auto cb_state = cb_context->GetCommandBufferState();
3103 if (!cb_state) return;
3104
3105 auto rp_state = cb_state->activeRenderPass;
3106 if (!rp_state) return;
3107
John Zulauf355e49b2020-04-24 15:11:15 -06003108 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003109}
3110
3111void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3112 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
3113 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3114 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003115 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003116}
3117
3118void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3119 const VkSubpassEndInfo *pSubpassEndInfo) {
3120 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003121 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003122}
3123
3124void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3125 const VkSubpassEndInfo *pSubpassEndInfo) {
3126 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003127 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003128}
3129
John Zulauf355e49b2020-04-24 15:11:15 -06003130bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
3131 const char *func_name) const {
3132 bool skip = false;
3133
3134 auto cb_context = GetAccessContext(commandBuffer);
3135 assert(cb_context);
3136 auto cb_state = cb_context->GetCommandBufferState();
3137 if (!cb_state) return skip;
3138
3139 auto rp_state = cb_state->activeRenderPass;
3140 if (!rp_state) return skip;
3141
3142 skip |= cb_context->ValidateEndRenderpass(func_name);
3143 return skip;
3144}
3145
3146bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3147 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
3148 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
3149 return skip;
3150}
3151
3152bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
3153 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3154 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
3155 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
3156 return skip;
3157}
3158
3159bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
3160 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3161 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
3162 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
3163 return skip;
3164}
3165
3166void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3167 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003168 // Resolve the all subpass contexts to the command buffer contexts
3169 auto cb_context = GetAccessContext(commandBuffer);
3170 assert(cb_context);
3171 auto cb_state = cb_context->GetCommandBufferState();
3172 if (!cb_state) return;
3173
locke-lunargaecf2152020-05-12 17:15:41 -06003174 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06003175 if (!rp_state) return;
3176
John Zulauf355e49b2020-04-24 15:11:15 -06003177 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06003178}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003179
John Zulauf33fc1d52020-07-17 11:01:10 -06003180// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3181// updates to a resource which do not conflict at the byte level.
3182// TODO: Revisit this rule to see if it needs to be tighter or looser
3183// TODO: Add programatic control over suppression heuristics
3184bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3185 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3186}
3187
John Zulauf3d84f1b2020-03-09 13:33:25 -06003188void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003189 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003190 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003191}
3192
3193void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003194 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003195 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003196}
3197
3198void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003199 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003200 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003201}
locke-lunarga19c71d2020-03-02 18:17:04 -07003202
Jeff Leger178b1e52020-10-05 12:22:23 -04003203template <typename BufferImageCopyRegionType>
3204bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3205 VkImageLayout dstImageLayout, uint32_t regionCount,
3206 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003207 bool skip = false;
3208 const auto *cb_access_context = GetAccessContext(commandBuffer);
3209 assert(cb_access_context);
3210 if (!cb_access_context) return skip;
3211
Jeff Leger178b1e52020-10-05 12:22:23 -04003212 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3213 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3214
locke-lunarga19c71d2020-03-02 18:17:04 -07003215 const auto *context = cb_access_context->GetCurrentAccessContext();
3216 assert(context);
3217 if (!context) return skip;
3218
3219 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003220 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3221
3222 for (uint32_t region = 0; region < regionCount; region++) {
3223 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003224 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003225 ResourceAccessRange src_range =
3226 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003227 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003228 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003229 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003230 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003231 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003232 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003233 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003234 }
3235 }
3236 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003237 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003238 copy_region.imageOffset, copy_region.imageExtent);
3239 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003240 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003241 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003242 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003243 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003244 }
3245 if (skip) break;
3246 }
3247 if (skip) break;
3248 }
3249 return skip;
3250}
3251
Jeff Leger178b1e52020-10-05 12:22:23 -04003252bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3253 VkImageLayout dstImageLayout, uint32_t regionCount,
3254 const VkBufferImageCopy *pRegions) const {
3255 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3256 COPY_COMMAND_VERSION_1);
3257}
3258
3259bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3260 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3261 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3262 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3263 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3264}
3265
3266template <typename BufferImageCopyRegionType>
3267void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3268 VkImageLayout dstImageLayout, uint32_t regionCount,
3269 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003270 auto *cb_access_context = GetAccessContext(commandBuffer);
3271 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003272
3273 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3274 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3275
3276 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003277 auto *context = cb_access_context->GetCurrentAccessContext();
3278 assert(context);
3279
3280 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003281 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003282
3283 for (uint32_t region = 0; region < regionCount; region++) {
3284 const auto &copy_region = pRegions[region];
3285 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003286 ResourceAccessRange src_range =
3287 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003288 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003289 }
3290 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003291 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003292 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003293 }
3294 }
3295}
3296
Jeff Leger178b1e52020-10-05 12:22:23 -04003297void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3298 VkImageLayout dstImageLayout, uint32_t regionCount,
3299 const VkBufferImageCopy *pRegions) {
3300 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3301 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3302}
3303
3304void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3305 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3306 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3307 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3308 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3309 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3310}
3311
3312template <typename BufferImageCopyRegionType>
3313bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3314 VkBuffer dstBuffer, uint32_t regionCount,
3315 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003316 bool skip = false;
3317 const auto *cb_access_context = GetAccessContext(commandBuffer);
3318 assert(cb_access_context);
3319 if (!cb_access_context) return skip;
3320
Jeff Leger178b1e52020-10-05 12:22:23 -04003321 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3322 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3323
locke-lunarga19c71d2020-03-02 18:17:04 -07003324 const auto *context = cb_access_context->GetCurrentAccessContext();
3325 assert(context);
3326 if (!context) return skip;
3327
3328 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3329 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3330 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3331 for (uint32_t region = 0; region < regionCount; region++) {
3332 const auto &copy_region = pRegions[region];
3333 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003334 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003335 copy_region.imageOffset, copy_region.imageExtent);
3336 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003337 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003338 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003339 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003340 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003341 }
3342 }
3343 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06003344 ResourceAccessRange dst_range =
3345 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003346 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003347 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003348 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003349 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003350 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003351 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003352 }
3353 }
3354 if (skip) break;
3355 }
3356 return skip;
3357}
3358
Jeff Leger178b1e52020-10-05 12:22:23 -04003359bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3360 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3361 const VkBufferImageCopy *pRegions) const {
3362 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3363 COPY_COMMAND_VERSION_1);
3364}
3365
3366bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3367 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3368 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3369 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3370 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3371}
3372
3373template <typename BufferImageCopyRegionType>
3374void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3375 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3376 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003377 auto *cb_access_context = GetAccessContext(commandBuffer);
3378 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003379
3380 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3381 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3382
3383 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003384 auto *context = cb_access_context->GetCurrentAccessContext();
3385 assert(context);
3386
3387 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003388 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3389 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 -06003390 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003391
3392 for (uint32_t region = 0; region < regionCount; region++) {
3393 const auto &copy_region = pRegions[region];
3394 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003395 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003396 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003397 }
3398 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003399 ResourceAccessRange dst_range =
3400 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003401 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003402 }
3403 }
3404}
3405
Jeff Leger178b1e52020-10-05 12:22:23 -04003406void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3407 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3408 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3409 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3410}
3411
3412void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3413 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3414 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3415 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3416 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3417 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3418}
3419
3420template <typename RegionType>
3421bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3422 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3423 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003424 bool skip = false;
3425 const auto *cb_access_context = GetAccessContext(commandBuffer);
3426 assert(cb_access_context);
3427 if (!cb_access_context) return skip;
3428
3429 const auto *context = cb_access_context->GetCurrentAccessContext();
3430 assert(context);
3431 if (!context) return skip;
3432
3433 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3434 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3435
3436 for (uint32_t region = 0; region < regionCount; region++) {
3437 const auto &blit_region = pRegions[region];
3438 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003439 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3440 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3441 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3442 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3443 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3444 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3445 auto hazard =
3446 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003447 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003448 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003449 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003450 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003451 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003452 }
3453 }
3454
3455 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003456 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3457 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3458 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3459 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3460 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3461 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3462 auto hazard =
3463 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003464 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003465 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003466 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003467 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003468 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003469 }
3470 if (skip) break;
3471 }
3472 }
3473
3474 return skip;
3475}
3476
Jeff Leger178b1e52020-10-05 12:22:23 -04003477bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3478 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3479 const VkImageBlit *pRegions, VkFilter filter) const {
3480 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3481 "vkCmdBlitImage");
3482}
3483
3484bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3485 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3486 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3487 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3488 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3489}
3490
3491template <typename RegionType>
3492void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3493 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3494 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003495 auto *cb_access_context = GetAccessContext(commandBuffer);
3496 assert(cb_access_context);
3497 auto *context = cb_access_context->GetCurrentAccessContext();
3498 assert(context);
3499
3500 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003501 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003502
3503 for (uint32_t region = 0; region < regionCount; region++) {
3504 const auto &blit_region = pRegions[region];
3505 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003506 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3507 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3508 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3509 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3510 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3511 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3512 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003513 }
3514 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003515 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3516 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3517 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3518 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3519 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3520 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3521 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003522 }
3523 }
3524}
locke-lunarg36ba2592020-04-03 09:42:04 -06003525
Jeff Leger178b1e52020-10-05 12:22:23 -04003526void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3527 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3528 const VkImageBlit *pRegions, VkFilter filter) {
3529 auto *cb_access_context = GetAccessContext(commandBuffer);
3530 assert(cb_access_context);
3531 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3532 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3533 pRegions, filter);
3534 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3535}
3536
3537void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3538 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3539 auto *cb_access_context = GetAccessContext(commandBuffer);
3540 assert(cb_access_context);
3541 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3542 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3543 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3544 pBlitImageInfo->filter, tag);
3545}
3546
locke-lunarg61870c22020-06-09 14:51:50 -06003547bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
3548 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
3549 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003550 bool skip = false;
3551 if (drawCount == 0) return skip;
3552
3553 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3554 VkDeviceSize size = struct_size;
3555 if (drawCount == 1 || stride == size) {
3556 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003557 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003558 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3559 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003560 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003561 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003562 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003563 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003564 }
3565 } else {
3566 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003567 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003568 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3569 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003570 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003571 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3572 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3573 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003574 break;
3575 }
3576 }
3577 }
3578 return skip;
3579}
3580
locke-lunarg61870c22020-06-09 14:51:50 -06003581void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3582 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3583 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003584 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3585 VkDeviceSize size = struct_size;
3586 if (drawCount == 1 || stride == size) {
3587 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003588 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003589 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3590 } else {
3591 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003592 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003593 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3594 }
3595 }
3596}
3597
locke-lunarg61870c22020-06-09 14:51:50 -06003598bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3599 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003600 bool skip = false;
3601
3602 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003603 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003604 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3605 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003606 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003607 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003608 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003609 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003610 }
3611 return skip;
3612}
3613
locke-lunarg61870c22020-06-09 14:51:50 -06003614void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003615 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003616 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003617 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3618}
3619
locke-lunarg36ba2592020-04-03 09:42:04 -06003620bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003621 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003622 const auto *cb_access_context = GetAccessContext(commandBuffer);
3623 assert(cb_access_context);
3624 if (!cb_access_context) return skip;
3625
locke-lunarg61870c22020-06-09 14:51:50 -06003626 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003627 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003628}
3629
3630void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003631 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003632 auto *cb_access_context = GetAccessContext(commandBuffer);
3633 assert(cb_access_context);
3634 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003635
locke-lunarg61870c22020-06-09 14:51:50 -06003636 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003637}
locke-lunarge1a67022020-04-29 00:15:36 -06003638
3639bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003640 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003641 const auto *cb_access_context = GetAccessContext(commandBuffer);
3642 assert(cb_access_context);
3643 if (!cb_access_context) return skip;
3644
3645 const auto *context = cb_access_context->GetCurrentAccessContext();
3646 assert(context);
3647 if (!context) return skip;
3648
locke-lunarg61870c22020-06-09 14:51:50 -06003649 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3650 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3651 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003652 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003653}
3654
3655void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003656 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003657 auto *cb_access_context = GetAccessContext(commandBuffer);
3658 assert(cb_access_context);
3659 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3660 auto *context = cb_access_context->GetCurrentAccessContext();
3661 assert(context);
3662
locke-lunarg61870c22020-06-09 14:51:50 -06003663 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3664 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003665}
3666
3667bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3668 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003669 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003670 const auto *cb_access_context = GetAccessContext(commandBuffer);
3671 assert(cb_access_context);
3672 if (!cb_access_context) return skip;
3673
locke-lunarg61870c22020-06-09 14:51:50 -06003674 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3675 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3676 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003677 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003678}
3679
3680void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3681 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003682 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003683 auto *cb_access_context = GetAccessContext(commandBuffer);
3684 assert(cb_access_context);
3685 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003686
locke-lunarg61870c22020-06-09 14:51:50 -06003687 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3688 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3689 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003690}
3691
3692bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3693 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003694 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003695 const auto *cb_access_context = GetAccessContext(commandBuffer);
3696 assert(cb_access_context);
3697 if (!cb_access_context) return skip;
3698
locke-lunarg61870c22020-06-09 14:51:50 -06003699 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3700 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3701 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003702 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003703}
3704
3705void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3706 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003707 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003708 auto *cb_access_context = GetAccessContext(commandBuffer);
3709 assert(cb_access_context);
3710 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003711
locke-lunarg61870c22020-06-09 14:51:50 -06003712 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3713 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3714 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003715}
3716
3717bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3718 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003719 bool skip = false;
3720 if (drawCount == 0) return skip;
3721
locke-lunargff255f92020-05-13 18:53:52 -06003722 const auto *cb_access_context = GetAccessContext(commandBuffer);
3723 assert(cb_access_context);
3724 if (!cb_access_context) return skip;
3725
3726 const auto *context = cb_access_context->GetCurrentAccessContext();
3727 assert(context);
3728 if (!context) return skip;
3729
locke-lunarg61870c22020-06-09 14:51:50 -06003730 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3731 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3732 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3733 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003734
3735 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3736 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3737 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003738 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003739 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003740}
3741
3742void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3743 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003744 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003745 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003746 auto *cb_access_context = GetAccessContext(commandBuffer);
3747 assert(cb_access_context);
3748 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3749 auto *context = cb_access_context->GetCurrentAccessContext();
3750 assert(context);
3751
locke-lunarg61870c22020-06-09 14:51:50 -06003752 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3753 cb_access_context->RecordDrawSubpassAttachment(tag);
3754 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003755
3756 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3757 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3758 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003759 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003760}
3761
3762bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3763 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003764 bool skip = false;
3765 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003766 const auto *cb_access_context = GetAccessContext(commandBuffer);
3767 assert(cb_access_context);
3768 if (!cb_access_context) return skip;
3769
3770 const auto *context = cb_access_context->GetCurrentAccessContext();
3771 assert(context);
3772 if (!context) return skip;
3773
locke-lunarg61870c22020-06-09 14:51:50 -06003774 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3775 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3776 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3777 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003778
3779 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3780 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3781 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003782 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003783 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003784}
3785
3786void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3787 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003788 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003789 auto *cb_access_context = GetAccessContext(commandBuffer);
3790 assert(cb_access_context);
3791 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3792 auto *context = cb_access_context->GetCurrentAccessContext();
3793 assert(context);
3794
locke-lunarg61870c22020-06-09 14:51:50 -06003795 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3796 cb_access_context->RecordDrawSubpassAttachment(tag);
3797 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003798
3799 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3800 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3801 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003802 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003803}
3804
3805bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3806 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3807 uint32_t stride, const char *function) const {
3808 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003809 const auto *cb_access_context = GetAccessContext(commandBuffer);
3810 assert(cb_access_context);
3811 if (!cb_access_context) return skip;
3812
3813 const auto *context = cb_access_context->GetCurrentAccessContext();
3814 assert(context);
3815 if (!context) return skip;
3816
locke-lunarg61870c22020-06-09 14:51:50 -06003817 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3818 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3819 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3820 function);
3821 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003822
3823 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3824 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3825 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003826 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003827 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003828}
3829
3830bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3831 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3832 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003833 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3834 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003835}
3836
3837void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3838 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3839 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003840 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3841 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003842 auto *cb_access_context = GetAccessContext(commandBuffer);
3843 assert(cb_access_context);
3844 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3845 auto *context = cb_access_context->GetCurrentAccessContext();
3846 assert(context);
3847
locke-lunarg61870c22020-06-09 14:51:50 -06003848 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3849 cb_access_context->RecordDrawSubpassAttachment(tag);
3850 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3851 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003852
3853 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3854 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3855 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003856 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003857}
3858
3859bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3860 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3861 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003862 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3863 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003864}
3865
3866void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3867 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3868 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003869 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3870 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003871 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003872}
3873
3874bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3875 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3876 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003877 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3878 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003879}
3880
3881void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3882 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3883 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003884 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3885 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003886 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3887}
3888
3889bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3890 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3891 uint32_t stride, const char *function) const {
3892 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003893 const auto *cb_access_context = GetAccessContext(commandBuffer);
3894 assert(cb_access_context);
3895 if (!cb_access_context) return skip;
3896
3897 const auto *context = cb_access_context->GetCurrentAccessContext();
3898 assert(context);
3899 if (!context) return skip;
3900
locke-lunarg61870c22020-06-09 14:51:50 -06003901 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3902 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3903 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3904 stride, function);
3905 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003906
3907 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3908 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3909 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003910 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003911 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003912}
3913
3914bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3915 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3916 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003917 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3918 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003919}
3920
3921void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3922 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3923 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003924 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3925 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003926 auto *cb_access_context = GetAccessContext(commandBuffer);
3927 assert(cb_access_context);
3928 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3929 auto *context = cb_access_context->GetCurrentAccessContext();
3930 assert(context);
3931
locke-lunarg61870c22020-06-09 14:51:50 -06003932 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3933 cb_access_context->RecordDrawSubpassAttachment(tag);
3934 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3935 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003936
3937 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3938 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06003939 // We will update the index and vertex buffer in SubmitQueue in the future.
3940 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003941}
3942
3943bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3944 VkDeviceSize offset, VkBuffer countBuffer,
3945 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3946 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003947 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3948 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003949}
3950
3951void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3952 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3953 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003954 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3955 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003956 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3957}
3958
3959bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3960 VkDeviceSize offset, VkBuffer countBuffer,
3961 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3962 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003963 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3964 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003965}
3966
3967void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3968 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3969 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003970 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3971 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003972 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3973}
3974
3975bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3976 const VkClearColorValue *pColor, uint32_t rangeCount,
3977 const VkImageSubresourceRange *pRanges) const {
3978 bool skip = false;
3979 const auto *cb_access_context = GetAccessContext(commandBuffer);
3980 assert(cb_access_context);
3981 if (!cb_access_context) return skip;
3982
3983 const auto *context = cb_access_context->GetCurrentAccessContext();
3984 assert(context);
3985 if (!context) return skip;
3986
3987 const auto *image_state = Get<IMAGE_STATE>(image);
3988
3989 for (uint32_t index = 0; index < rangeCount; index++) {
3990 const auto &range = pRanges[index];
3991 if (image_state) {
3992 auto hazard =
3993 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3994 if (hazard.hazard) {
3995 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003996 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003997 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06003998 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003999 }
4000 }
4001 }
4002 return skip;
4003}
4004
4005void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4006 const VkClearColorValue *pColor, uint32_t rangeCount,
4007 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004008 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004009 auto *cb_access_context = GetAccessContext(commandBuffer);
4010 assert(cb_access_context);
4011 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4012 auto *context = cb_access_context->GetCurrentAccessContext();
4013 assert(context);
4014
4015 const auto *image_state = Get<IMAGE_STATE>(image);
4016
4017 for (uint32_t index = 0; index < rangeCount; index++) {
4018 const auto &range = pRanges[index];
4019 if (image_state) {
4020 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4021 tag);
4022 }
4023 }
4024}
4025
4026bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4027 VkImageLayout imageLayout,
4028 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4029 const VkImageSubresourceRange *pRanges) const {
4030 bool skip = false;
4031 const auto *cb_access_context = GetAccessContext(commandBuffer);
4032 assert(cb_access_context);
4033 if (!cb_access_context) return skip;
4034
4035 const auto *context = cb_access_context->GetCurrentAccessContext();
4036 assert(context);
4037 if (!context) return skip;
4038
4039 const auto *image_state = Get<IMAGE_STATE>(image);
4040
4041 for (uint32_t index = 0; index < rangeCount; index++) {
4042 const auto &range = pRanges[index];
4043 if (image_state) {
4044 auto hazard =
4045 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4046 if (hazard.hazard) {
4047 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004048 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004049 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004050 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004051 }
4052 }
4053 }
4054 return skip;
4055}
4056
4057void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4058 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4059 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004060 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004061 auto *cb_access_context = GetAccessContext(commandBuffer);
4062 assert(cb_access_context);
4063 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4064 auto *context = cb_access_context->GetCurrentAccessContext();
4065 assert(context);
4066
4067 const auto *image_state = Get<IMAGE_STATE>(image);
4068
4069 for (uint32_t index = 0; index < rangeCount; index++) {
4070 const auto &range = pRanges[index];
4071 if (image_state) {
4072 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4073 tag);
4074 }
4075 }
4076}
4077
4078bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4079 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4080 VkDeviceSize dstOffset, VkDeviceSize stride,
4081 VkQueryResultFlags flags) const {
4082 bool skip = false;
4083 const auto *cb_access_context = GetAccessContext(commandBuffer);
4084 assert(cb_access_context);
4085 if (!cb_access_context) return skip;
4086
4087 const auto *context = cb_access_context->GetCurrentAccessContext();
4088 assert(context);
4089 if (!context) return skip;
4090
4091 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4092
4093 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004094 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004095 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4096 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004097 skip |=
4098 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4099 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4100 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004101 }
4102 }
locke-lunargff255f92020-05-13 18:53:52 -06004103
4104 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004105 return skip;
4106}
4107
4108void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4109 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4110 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004111 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4112 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004113 auto *cb_access_context = GetAccessContext(commandBuffer);
4114 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004115 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004116 auto *context = cb_access_context->GetCurrentAccessContext();
4117 assert(context);
4118
4119 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4120
4121 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004122 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004123 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4124 }
locke-lunargff255f92020-05-13 18:53:52 -06004125
4126 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004127}
4128
4129bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4130 VkDeviceSize size, uint32_t data) const {
4131 bool skip = false;
4132 const auto *cb_access_context = GetAccessContext(commandBuffer);
4133 assert(cb_access_context);
4134 if (!cb_access_context) return skip;
4135
4136 const auto *context = cb_access_context->GetCurrentAccessContext();
4137 assert(context);
4138 if (!context) return skip;
4139
4140 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4141
4142 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004143 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004144 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4145 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004146 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004147 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004148 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004149 }
4150 }
4151 return skip;
4152}
4153
4154void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4155 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004156 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004157 auto *cb_access_context = GetAccessContext(commandBuffer);
4158 assert(cb_access_context);
4159 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4160 auto *context = cb_access_context->GetCurrentAccessContext();
4161 assert(context);
4162
4163 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4164
4165 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004166 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004167 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4168 }
4169}
4170
4171bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4172 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4173 const VkImageResolve *pRegions) const {
4174 bool skip = false;
4175 const auto *cb_access_context = GetAccessContext(commandBuffer);
4176 assert(cb_access_context);
4177 if (!cb_access_context) return skip;
4178
4179 const auto *context = cb_access_context->GetCurrentAccessContext();
4180 assert(context);
4181 if (!context) return skip;
4182
4183 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4184 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4185
4186 for (uint32_t region = 0; region < regionCount; region++) {
4187 const auto &resolve_region = pRegions[region];
4188 if (src_image) {
4189 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4190 resolve_region.srcOffset, resolve_region.extent);
4191 if (hazard.hazard) {
4192 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004193 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004194 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004195 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004196 }
4197 }
4198
4199 if (dst_image) {
4200 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4201 resolve_region.dstOffset, resolve_region.extent);
4202 if (hazard.hazard) {
4203 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004204 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004205 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004206 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004207 }
4208 if (skip) break;
4209 }
4210 }
4211
4212 return skip;
4213}
4214
4215void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4216 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4217 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004218 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4219 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004220 auto *cb_access_context = GetAccessContext(commandBuffer);
4221 assert(cb_access_context);
4222 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4223 auto *context = cb_access_context->GetCurrentAccessContext();
4224 assert(context);
4225
4226 auto *src_image = Get<IMAGE_STATE>(srcImage);
4227 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4228
4229 for (uint32_t region = 0; region < regionCount; region++) {
4230 const auto &resolve_region = pRegions[region];
4231 if (src_image) {
4232 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4233 resolve_region.srcOffset, resolve_region.extent, tag);
4234 }
4235 if (dst_image) {
4236 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4237 resolve_region.dstOffset, resolve_region.extent, tag);
4238 }
4239 }
4240}
4241
Jeff Leger178b1e52020-10-05 12:22:23 -04004242bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4243 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4244 bool skip = false;
4245 const auto *cb_access_context = GetAccessContext(commandBuffer);
4246 assert(cb_access_context);
4247 if (!cb_access_context) return skip;
4248
4249 const auto *context = cb_access_context->GetCurrentAccessContext();
4250 assert(context);
4251 if (!context) return skip;
4252
4253 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4254 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4255
4256 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4257 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4258 if (src_image) {
4259 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4260 resolve_region.srcOffset, resolve_region.extent);
4261 if (hazard.hazard) {
4262 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4263 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4264 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
4265 region, string_UsageTag(hazard).c_str());
4266 }
4267 }
4268
4269 if (dst_image) {
4270 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4271 resolve_region.dstOffset, resolve_region.extent);
4272 if (hazard.hazard) {
4273 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4274 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4275 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
4276 region, string_UsageTag(hazard).c_str());
4277 }
4278 if (skip) break;
4279 }
4280 }
4281
4282 return skip;
4283}
4284
4285void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4286 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4287 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4288 auto *cb_access_context = GetAccessContext(commandBuffer);
4289 assert(cb_access_context);
4290 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4291 auto *context = cb_access_context->GetCurrentAccessContext();
4292 assert(context);
4293
4294 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4295 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4296
4297 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4298 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4299 if (src_image) {
4300 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4301 resolve_region.srcOffset, resolve_region.extent, tag);
4302 }
4303 if (dst_image) {
4304 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4305 resolve_region.dstOffset, resolve_region.extent, tag);
4306 }
4307 }
4308}
4309
locke-lunarge1a67022020-04-29 00:15:36 -06004310bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4311 VkDeviceSize dataSize, const void *pData) const {
4312 bool skip = false;
4313 const auto *cb_access_context = GetAccessContext(commandBuffer);
4314 assert(cb_access_context);
4315 if (!cb_access_context) return skip;
4316
4317 const auto *context = cb_access_context->GetCurrentAccessContext();
4318 assert(context);
4319 if (!context) return skip;
4320
4321 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4322
4323 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004324 // VK_WHOLE_SIZE not allowed
4325 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004326 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4327 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004328 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004329 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004330 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004331 }
4332 }
4333 return skip;
4334}
4335
4336void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4337 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004338 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004339 auto *cb_access_context = GetAccessContext(commandBuffer);
4340 assert(cb_access_context);
4341 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4342 auto *context = cb_access_context->GetCurrentAccessContext();
4343 assert(context);
4344
4345 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4346
4347 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004348 // VK_WHOLE_SIZE not allowed
4349 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004350 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4351 }
4352}
locke-lunargff255f92020-05-13 18:53:52 -06004353
4354bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4355 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4356 bool skip = false;
4357 const auto *cb_access_context = GetAccessContext(commandBuffer);
4358 assert(cb_access_context);
4359 if (!cb_access_context) return skip;
4360
4361 const auto *context = cb_access_context->GetCurrentAccessContext();
4362 assert(context);
4363 if (!context) return skip;
4364
4365 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4366
4367 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004368 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004369 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4370 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004371 skip |=
4372 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4373 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4374 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004375 }
4376 }
4377 return skip;
4378}
4379
4380void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4381 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004382 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004383 auto *cb_access_context = GetAccessContext(commandBuffer);
4384 assert(cb_access_context);
4385 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4386 auto *context = cb_access_context->GetCurrentAccessContext();
4387 assert(context);
4388
4389 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4390
4391 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004392 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004393 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4394 }
4395}