blob: ac1755328c654f6e6a136e4f34ce18374a35c610 [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) {
461 assert(prev_dep.dependency);
462 const auto dep = *prev_dep.dependency;
John Zulauf540266b2020-04-06 18:54:53 -0600463 prev_.emplace_back(const_cast<AccessContext *>(&contexts[dep.srcSubpass]), queue_flags, dep);
John Zulauf355e49b2020-04-24 15:11:15 -0600464 prev_by_subpass_[dep.srcSubpass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700465 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600466
467 async_.reserve(subpass_dep.async.size());
468 for (const auto async_subpass : subpass_dep.async) {
John Zulauf540266b2020-04-06 18:54:53 -0600469 async_.emplace_back(const_cast<AccessContext *>(&contexts[async_subpass]));
John Zulauf3d84f1b2020-03-09 13:33:25 -0600470 }
John Zulaufe5da6e52020-03-18 15:32:18 -0600471 if (subpass_dep.barrier_from_external) {
472 src_external_ = TrackBack(external_context, queue_flags, *subpass_dep.barrier_from_external);
473 } else {
474 src_external_ = TrackBack();
475 }
476 if (subpass_dep.barrier_to_external) {
477 dst_external_ = TrackBack(this, queue_flags, *subpass_dep.barrier_to_external);
478 } else {
479 dst_external_ = TrackBack();
John Zulauf3d84f1b2020-03-09 13:33:25 -0600480 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700481}
482
John Zulauf5f13a792020-03-10 07:31:21 -0600483template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600484HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600485 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600486 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600487 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600488
489 HazardResult hazard;
490 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
491 hazard = detector.Detect(prev);
492 }
493 return hazard;
494}
495
John Zulauf3d84f1b2020-03-09 13:33:25 -0600496// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
497// the DAG of the contexts (for example subpasses)
498template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600499HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
500 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600501 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600502
John Zulauf1a224292020-06-30 14:52:13 -0600503 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600504 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
505 // so we'll check these first
506 for (const auto &async_context : async_) {
507 hazard = async_context->DetectAsyncHazard(type, detector, range);
508 if (hazard.hazard) return hazard;
509 }
John Zulauf5f13a792020-03-10 07:31:21 -0600510 }
511
John Zulauf1a224292020-06-30 14:52:13 -0600512 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600513
John Zulauf69133422020-05-20 14:55:53 -0600514 const auto &accesses = GetAccessStateMap(type);
515 const auto from = accesses.lower_bound(range);
516 const auto to = accesses.upper_bound(range);
517 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600518
John Zulauf69133422020-05-20 14:55:53 -0600519 for (auto pos = from; pos != to; ++pos) {
520 // Cover any leading gap, or gap between entries
521 if (detect_prev) {
522 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
523 // Cover any leading gap, or gap between entries
524 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600525 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600526 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600527 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600528 if (hazard.hazard) return hazard;
529 }
John Zulauf69133422020-05-20 14:55:53 -0600530 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
531 gap.begin = pos->first.end;
532 }
533
534 hazard = detector.Detect(pos);
535 if (hazard.hazard) return hazard;
536 }
537
538 if (detect_prev) {
539 // Detect in the trailing empty as needed
540 gap.end = range.end;
541 if (gap.non_empty()) {
542 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600543 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600544 }
545
546 return hazard;
547}
548
549// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
550template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600551HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600552 auto &accesses = GetAccessStateMap(type);
553 const auto from = accesses.lower_bound(range);
554 const auto to = accesses.upper_bound(range);
555
John Zulauf3d84f1b2020-03-09 13:33:25 -0600556 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600557 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
558 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600559 }
John Zulauf16adfc92020-04-08 10:28:33 -0600560
John Zulauf3d84f1b2020-03-09 13:33:25 -0600561 return hazard;
562}
563
John Zulauf355e49b2020-04-24 15:11:15 -0600564// Returns the last resolved entry
565static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
566 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
567 const SyncBarrier *barrier) {
568 auto at = entry;
569 for (auto pos = first; pos != last; ++pos) {
570 // Every member of the input iterator range must fit within the remaining portion of entry
571 assert(at->first.includes(pos->first));
572 assert(at != dest->end());
573 // Trim up at to the same size as the entry to resolve
574 at = sparse_container::split(at, *dest, pos->first);
575 auto access = pos->second;
576 if (barrier) {
577 access.ApplyBarrier(*barrier);
578 }
579 at->second.Resolve(access);
580 ++at; // Go to the remaining unused section of entry
581 }
582}
583
584void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, const SyncBarrier *barrier,
585 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
586 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600587 if (!range.non_empty()) return;
588
John Zulauf355e49b2020-04-24 15:11:15 -0600589 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
590 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600591 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600592 if (current->pos_B->valid) {
593 const auto &src_pos = current->pos_B->lower_bound;
John Zulauf355e49b2020-04-24 15:11:15 -0600594 auto access = src_pos->second;
595 if (barrier) {
596 access.ApplyBarrier(*barrier);
597 }
John Zulauf16adfc92020-04-08 10:28:33 -0600598 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600599 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
600 trimmed->second.Resolve(access);
601 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600602 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600603 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600604 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600605 }
John Zulauf16adfc92020-04-08 10:28:33 -0600606 } else {
607 // we have to descend to fill this gap
608 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600609 if (current->pos_A->valid) {
610 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
611 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600612 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulauf355e49b2020-04-24 15:11:15 -0600613 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier);
614 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600615 // There isn't anything in dest in current)range, so we can accumulate directly into it.
616 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulauf355e49b2020-04-24 15:11:15 -0600617 if (barrier) {
618 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
John Zulauf3bcab5e2020-06-19 14:42:32 -0600619 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
John Zulauf355e49b2020-04-24 15:11:15 -0600620 pos->second.ApplyBarrier(*barrier);
621 }
622 }
623 }
624 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
625 // iterator of the outer while.
626
627 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
628 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
629 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600630 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
631 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600632 current.seek(seek_to);
633 } else if (!current->pos_A->valid && infill_state) {
634 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
635 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
636 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600637 }
John Zulauf5f13a792020-03-10 07:31:21 -0600638 }
John Zulauf16adfc92020-04-08 10:28:33 -0600639 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600640 }
John Zulauf1a224292020-06-30 14:52:13 -0600641
642 // Infill if range goes passed both the current and resolve map prior contents
643 if (recur_to_infill && (current->range.end < range.end)) {
644 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
645 ResourceAccessRangeMap gap_map;
646 const auto the_end = resolve_map->end();
647 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
648 for (auto &access : gap_map) {
649 access.second.ApplyBarrier(*barrier);
650 resolve_map->insert(the_end, access);
651 }
652 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600653}
654
John Zulauf355e49b2020-04-24 15:11:15 -0600655void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
656 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600657 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600658 if (range.non_empty() && infill_state) {
659 descent_map->insert(std::make_pair(range, *infill_state));
660 }
661 } else {
662 // Look for something to fill the gap further along.
663 for (const auto &prev_dep : prev_) {
John Zulauf355e49b2020-04-24 15:11:15 -0600664 prev_dep.context->ResolveAccessRange(type, range, &prev_dep.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600665 }
666
John Zulaufe5da6e52020-03-18 15:32:18 -0600667 if (src_external_.context) {
John Zulauf355e49b2020-04-24 15:11:15 -0600668 src_external_.context->ResolveAccessRange(type, range, &src_external_.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600669 }
670 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600671}
672
John Zulauf16adfc92020-04-08 10:28:33 -0600673AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600674 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600675}
676
677VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) {
678 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
679}
680
John Zulauf355e49b2020-04-24 15:11:15 -0600681static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
John Zulauf16adfc92020-04-08 10:28:33 -0600682
John Zulauf1507ee42020-05-18 11:33:09 -0600683static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
684 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
685 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
686 return stage_access;
687}
688static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
689 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
690 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
691 return stage_access;
692}
693
John Zulauf7635de32020-05-29 17:14:15 -0600694// Caller must manage returned pointer
695static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
696 uint32_t subpass, const VkRect2D &render_area,
697 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
698 auto *proxy = new AccessContext(context);
699 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600700 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600701 return proxy;
702}
703
John Zulauf540266b2020-04-06 18:54:53 -0600704void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
John Zulauf355e49b2020-04-24 15:11:15 -0600705 AddressType address_type, ResourceAccessRangeMap *descent_map,
706 const ResourceAccessState *infill_state) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600707 if (!SimpleBinding(image_state)) return;
708
John Zulauf62f10592020-04-03 12:20:02 -0600709 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
locke-lunargae26eac2020-04-16 15:29:05 -0600710 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600711 image_state.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600712 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf62f10592020-04-03 12:20:02 -0600713 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600714 ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state);
John Zulauf62f10592020-04-03 12:20:02 -0600715 }
716}
717
John Zulauf7635de32020-05-29 17:14:15 -0600718// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600719bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600720 const VkRect2D &render_area, uint32_t subpass,
721 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
722 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600723 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600724 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
725 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
726 // those affects have not been recorded yet.
727 //
728 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
729 // to apply and only copy then, if this proves a hot spot.
730 std::unique_ptr<AccessContext> proxy_for_prev;
731 TrackBack proxy_track_back;
732
John Zulauf355e49b2020-04-24 15:11:15 -0600733 const auto &transitions = rp_state.subpass_transitions[subpass];
734 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600735 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
736
737 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
738 if (prev_needs_proxy) {
739 if (!proxy_for_prev) {
740 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
741 render_area, attachment_views));
742 proxy_track_back = *track_back;
743 proxy_track_back.context = proxy_for_prev.get();
744 }
745 track_back = &proxy_track_back;
746 }
747 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600748 if (hazard.hazard) {
John Zulauf389c34b2020-07-28 11:19:35 -0600749 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
750 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
751 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
752 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
753 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
754 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600755 }
756 }
757 return skip;
758}
759
John Zulauf1507ee42020-05-18 11:33:09 -0600760bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600761 const VkRect2D &render_area, uint32_t subpass,
762 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
763 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600764 bool skip = false;
765 const auto *attachment_ci = rp_state.createInfo.pAttachments;
766 VkExtent3D extent = CastTo3D(render_area.extent);
767 VkOffset3D offset = CastTo3D(render_area.offset);
768 const auto external_access_scope = src_external_.barrier.dst_access_scope;
John Zulauf1507ee42020-05-18 11:33:09 -0600769
770 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
771 if (subpass == rp_state.attachment_first_subpass[i]) {
772 if (attachment_views[i] == nullptr) continue;
773 const IMAGE_VIEW_STATE &view = *attachment_views[i];
774 const IMAGE_STATE *image = view.image_state.get();
775 if (image == nullptr) continue;
776 const auto &ci = attachment_ci[i];
777 const bool is_transition = rp_state.attachment_first_is_transition[i];
778
779 // Need check in the following way
780 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
781 // vs. transition
782 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
783 // for each aspect loaded.
784
785 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600786 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600787 const bool is_color = !(has_depth || has_stencil);
788
789 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
790 const SyncStageAccessFlags load_mask = (has_depth || is_color) ? SyncStageAccess::Flags(load_index) : 0U;
791 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
792 const SyncStageAccessFlags stencil_mask = has_stencil ? SyncStageAccess::Flags(stencil_load_index) : 0U;
793
John Zulaufaff20662020-06-01 14:07:58 -0600794 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600795 const char *aspect = nullptr;
796 if (is_transition) {
797 // For transition w
798 SyncHazard transition_hazard = SyncHazard::NONE;
799 bool checked_stencil = false;
800 if (load_mask) {
801 if ((load_mask & external_access_scope) != load_mask) {
802 transition_hazard =
803 SyncStageAccess::HasWrite(load_mask) ? SyncHazard::WRITE_AFTER_WRITE : SyncHazard::READ_AFTER_WRITE;
804 aspect = is_color ? "color" : "depth";
805 }
806 if (!transition_hazard && stencil_mask) {
807 if ((stencil_mask & external_access_scope) != stencil_mask) {
808 transition_hazard = SyncStageAccess::HasWrite(stencil_mask) ? SyncHazard::WRITE_AFTER_WRITE
809 : SyncHazard::READ_AFTER_WRITE;
810 aspect = "stencil";
811 checked_stencil = true;
812 }
813 }
814 }
815 if (transition_hazard) {
816 // Hazard vs. ILT
817 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
818 skip |=
locke-lunarg54379cf2020-08-05 12:25:29 -0600819 sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(transition_hazard),
John Zulauf1507ee42020-05-18 11:33:09 -0600820 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
821 " aspect %s during load with loadOp %s.",
822 func_name, string_SyncHazard(transition_hazard), subpass, i, aspect, load_op_string);
823 }
824 } else {
825 auto hazard_range = view.normalized_subresource_range;
826 bool checked_stencil = false;
827 if (is_color) {
828 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, offset, extent);
829 aspect = "color";
830 } else {
831 if (has_depth) {
832 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
833 hazard = DetectHazard(*image, load_index, hazard_range, offset, extent);
834 aspect = "depth";
835 }
836 if (!hazard.hazard && has_stencil) {
837 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
838 hazard = DetectHazard(*image, stencil_load_index, hazard_range, offset, extent);
839 aspect = "stencil";
840 checked_stencil = true;
841 }
842 }
843
844 if (hazard.hazard) {
845 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
846 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
847 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600848 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -0600849 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600850 string_UsageTag(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -0600851 }
852 }
853 }
854 }
855 return skip;
856}
857
John Zulaufaff20662020-06-01 14:07:58 -0600858// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
859// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
860// store is part of the same Next/End operation.
861// The latter is handled in layout transistion validation directly
862bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
863 const VkRect2D &render_area, uint32_t subpass,
864 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
865 const char *func_name) const {
866 bool skip = false;
867 const auto *attachment_ci = rp_state.createInfo.pAttachments;
868 VkExtent3D extent = CastTo3D(render_area.extent);
869 VkOffset3D offset = CastTo3D(render_area.offset);
870
871 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
872 if (subpass == rp_state.attachment_last_subpass[i]) {
873 if (attachment_views[i] == nullptr) continue;
874 const IMAGE_VIEW_STATE &view = *attachment_views[i];
875 const IMAGE_STATE *image = view.image_state.get();
876 if (image == nullptr) continue;
877 const auto &ci = attachment_ci[i];
878
879 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
880 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
881 // sake, we treat DONT_CARE as writing.
882 const bool has_depth = FormatHasDepth(ci.format);
883 const bool has_stencil = FormatHasStencil(ci.format);
884 const bool is_color = !(has_depth || has_stencil);
885 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
886 if (!has_stencil && !store_op_stores) continue;
887
888 HazardResult hazard;
889 const char *aspect = nullptr;
890 bool checked_stencil = false;
891 if (is_color) {
892 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
893 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
894 aspect = "color";
895 } else {
896 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
897 auto hazard_range = view.normalized_subresource_range;
898 if (has_depth && store_op_stores) {
899 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
900 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
901 kAttachmentRasterOrder, offset, extent);
902 aspect = "depth";
903 }
904 if (!hazard.hazard && has_stencil && stencil_op_stores) {
905 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
906 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
907 kAttachmentRasterOrder, offset, extent);
908 aspect = "stencil";
909 checked_stencil = true;
910 }
911 }
912
913 if (hazard.hazard) {
914 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
915 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -0600916 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
917 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600918 " %s aspect during store with %s %s. Access info %s",
John Zulauf1dae9192020-06-16 15:46:44 -0600919 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600920 store_op_string, string_UsageTag(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -0600921 }
922 }
923 }
924 return skip;
925}
926
John Zulaufb027cdb2020-05-21 14:25:22 -0600927bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
928 const VkRect2D &render_area,
929 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
930 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600931 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
932 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
933 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600934}
935
John Zulauf3d84f1b2020-03-09 13:33:25 -0600936class HazardDetector {
937 SyncStageAccessIndex usage_index_;
938
939 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600940 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600941 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
942 return pos->second.DetectAsyncHazard(usage_index_);
943 }
944 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
945};
946
John Zulauf69133422020-05-20 14:55:53 -0600947class HazardDetectorWithOrdering {
948 const SyncStageAccessIndex usage_index_;
949 const SyncOrderingBarrier &ordering_;
950
951 public:
952 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
953 return pos->second.DetectHazard(usage_index_, ordering_);
954 }
955 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
956 return pos->second.DetectAsyncHazard(usage_index_);
957 }
958 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
959 : usage_index_(usage), ordering_(ordering) {}
960};
961
John Zulauf16adfc92020-04-08 10:28:33 -0600962HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600963 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600964 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600965 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600966}
967
John Zulauf16adfc92020-04-08 10:28:33 -0600968HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600969 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600970 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -0600971 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -0600972}
973
John Zulauf69133422020-05-20 14:55:53 -0600974template <typename Detector>
975HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
976 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
977 const VkExtent3D &extent, DetectOptions options) const {
978 if (!SimpleBinding(image)) return HazardResult();
979 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
980 const auto address_type = ImageAddressType(image);
981 const auto base_address = ResourceBaseAddress(image);
982 for (; range_gen->non_empty(); ++range_gen) {
983 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
984 if (hazard.hazard) return hazard;
985 }
986 return HazardResult();
987}
988
John Zulauf540266b2020-04-06 18:54:53 -0600989HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
990 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
991 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700992 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
993 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -0600994 return DetectHazard(image, current_usage, subresource_range, offset, extent);
995}
996
997HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
998 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
999 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -06001000 HazardDetector detector(current_usage);
1001 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
1002}
1003
1004HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1005 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
1006 const VkOffset3D &offset, const VkExtent3D &extent) const {
1007 HazardDetectorWithOrdering detector(current_usage, ordering);
1008 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001009}
1010
John Zulaufb027cdb2020-05-21 14:25:22 -06001011// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
1012// should have reported the issue regarding an invalid attachment entry
1013HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
1014 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
1015 VkImageAspectFlags aspect_mask) const {
1016 if (view != nullptr) {
1017 const IMAGE_STATE *image = view->image_state.get();
1018 if (image != nullptr) {
1019 auto *detect_range = &view->normalized_subresource_range;
1020 VkImageSubresourceRange masked_range;
1021 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1022 masked_range = view->normalized_subresource_range;
1023 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1024 detect_range = &masked_range;
1025 }
1026
1027 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1028 if (detect_range->aspectMask) {
1029 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
1030 }
1031 }
1032 }
1033 return HazardResult();
1034}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001035class BarrierHazardDetector {
1036 public:
1037 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1038 SyncStageAccessFlags src_access_scope)
1039 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1040
John Zulauf5f13a792020-03-10 07:31:21 -06001041 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1042 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001043 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001044 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
1045 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1046 return pos->second.DetectAsyncHazard(usage_index_);
1047 }
1048
1049 private:
1050 SyncStageAccessIndex usage_index_;
1051 VkPipelineStageFlags src_exec_scope_;
1052 SyncStageAccessFlags src_access_scope_;
1053};
1054
John Zulauf16adfc92020-04-08 10:28:33 -06001055HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -06001056 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001057 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001058 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -06001059 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001060}
1061
John Zulauf16adfc92020-04-08 10:28:33 -06001062HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001063 SyncStageAccessFlags src_access_scope,
1064 const VkImageSubresourceRange &subresource_range,
1065 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001066 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1067 VkOffset3D zero_offset = {0, 0, 0};
1068 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001069}
1070
John Zulauf355e49b2020-04-24 15:11:15 -06001071HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1072 SyncStageAccessFlags src_stage_accesses,
1073 const VkImageMemoryBarrier &barrier) const {
1074 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1075 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1076 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1077}
1078
John Zulauf9cb530d2019-09-30 14:14:10 -06001079template <typename Flags, typename Map>
1080SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1081 SyncStageAccessFlags scope = 0;
1082 for (const auto &bit_scope : map) {
1083 if (flag_mask < bit_scope.first) break;
1084
1085 if (flag_mask & bit_scope.first) {
1086 scope |= bit_scope.second;
1087 }
1088 }
1089 return scope;
1090}
1091
1092SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
1093 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1094}
1095
1096SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
1097 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
1098}
1099
1100// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
1101SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001102 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1103 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1104 // 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 -06001105 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1106}
1107
1108template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001109void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001110 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1111 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -06001112 auto pos = accesses->lower_bound(range);
1113 if (pos == accesses->end() || !pos->first.intersects(range)) {
1114 // The range is empty, fill it with a default value.
1115 pos = action.Infill(accesses, pos, range);
1116 } else if (range.begin < pos->first.begin) {
1117 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001118 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001119 } else if (pos->first.begin < range.begin) {
1120 // Trim the beginning if needed
1121 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1122 ++pos;
1123 }
1124
1125 const auto the_end = accesses->end();
1126 while ((pos != the_end) && pos->first.intersects(range)) {
1127 if (pos->first.end > range.end) {
1128 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1129 }
1130
1131 pos = action(accesses, pos);
1132 if (pos == the_end) break;
1133
1134 auto next = pos;
1135 ++next;
1136 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1137 // Need to infill if next is disjoint
1138 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001139 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001140 next = action.Infill(accesses, next, new_range);
1141 }
1142 pos = next;
1143 }
1144}
1145
1146struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001147 using Iterator = ResourceAccessRangeMap::iterator;
1148 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001149 // this is only called on gaps, and never returns a gap.
1150 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001151 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001152 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001153 }
John Zulauf5f13a792020-03-10 07:31:21 -06001154
John Zulauf5c5e88d2019-12-26 11:22:02 -07001155 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001156 auto &access_state = pos->second;
1157 access_state.Update(usage, tag);
1158 return pos;
1159 }
1160
John Zulauf16adfc92020-04-08 10:28:33 -06001161 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001162 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001163 : type(type_), context(context_), usage(usage_), tag(tag_) {}
1164 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001165 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001166 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001167 const ResourceUsageTag &tag;
1168};
1169
1170struct ApplyMemoryAccessBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001171 using Iterator = ResourceAccessRangeMap::iterator;
1172 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001173
John Zulauf5c5e88d2019-12-26 11:22:02 -07001174 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001175 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001176 access_state.ApplyMemoryAccessBarrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001177 return pos;
1178 }
1179
John Zulauf36bcf6a2020-02-03 15:12:52 -07001180 ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_,
1181 VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_)
1182 : src_exec_scope(src_exec_scope_),
1183 src_access_scope(src_access_scope_),
1184 dst_exec_scope(dst_exec_scope_),
1185 dst_access_scope(dst_access_scope_) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001186
John Zulauf36bcf6a2020-02-03 15:12:52 -07001187 VkPipelineStageFlags src_exec_scope;
1188 SyncStageAccessFlags src_access_scope;
1189 VkPipelineStageFlags dst_exec_scope;
1190 SyncStageAccessFlags dst_access_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001191};
1192
1193struct ApplyGlobalBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001194 using Iterator = ResourceAccessRangeMap::iterator;
1195 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001196
John Zulauf5c5e88d2019-12-26 11:22:02 -07001197 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001198 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001199 access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001200
1201 for (const auto &functor : barrier_functor) {
1202 functor(accesses, pos);
1203 }
1204 return pos;
1205 }
1206
John Zulauf36bcf6a2020-02-03 15:12:52 -07001207 ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope,
1208 SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses,
John Zulauf9cb530d2019-09-30 14:14:10 -06001209 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001210 : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001211 // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier...
1212 barrier_functor.reserve(memoryBarrierCount);
1213 for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) {
1214 const auto &barrier = pMemoryBarriers[barrier_index];
John Zulauf36bcf6a2020-02-03 15:12:52 -07001215 barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask),
1216 dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
John Zulauf9cb530d2019-09-30 14:14:10 -06001217 }
1218 }
1219
John Zulauf36bcf6a2020-02-03 15:12:52 -07001220 const VkPipelineStageFlags src_exec_scope;
1221 const VkPipelineStageFlags dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001222 std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor;
1223};
1224
John Zulauf355e49b2020-04-24 15:11:15 -06001225void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1226 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001227 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1228 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001229}
1230
John Zulauf16adfc92020-04-08 10:28:33 -06001231void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001232 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001233 if (!SimpleBinding(buffer)) return;
1234 const auto base_address = ResourceBaseAddress(buffer);
1235 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1236}
John Zulauf355e49b2020-04-24 15:11:15 -06001237
John Zulauf540266b2020-04-06 18:54:53 -06001238void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001239 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001240 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001241 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001242 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001243 const auto address_type = ImageAddressType(image);
1244 const auto base_address = ResourceBaseAddress(image);
1245 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001246 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001247 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001248 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001249}
John Zulauf7635de32020-05-29 17:14:15 -06001250void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1251 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1252 if (view != nullptr) {
1253 const IMAGE_STATE *image = view->image_state.get();
1254 if (image != nullptr) {
1255 auto *update_range = &view->normalized_subresource_range;
1256 VkImageSubresourceRange masked_range;
1257 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1258 masked_range = view->normalized_subresource_range;
1259 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1260 update_range = &masked_range;
1261 }
1262 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1263 }
1264 }
1265}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001266
John Zulauf355e49b2020-04-24 15:11:15 -06001267void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1268 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1269 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001270 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1271 subresource.layerCount};
1272 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1273}
1274
John Zulauf540266b2020-04-06 18:54:53 -06001275template <typename Action>
1276void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001277 if (!SimpleBinding(buffer)) return;
1278 const auto base_address = ResourceBaseAddress(buffer);
1279 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001280}
1281
1282template <typename Action>
1283void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1284 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001285 if (!SimpleBinding(image)) return;
1286 const auto address_type = ImageAddressType(image);
1287 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001288
locke-lunargae26eac2020-04-16 15:29:05 -06001289 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001290 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001291
John Zulauf16adfc92020-04-08 10:28:33 -06001292 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001293 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001294 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001295 }
1296}
1297
John Zulauf7635de32020-05-29 17:14:15 -06001298void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1299 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1300 const ResourceUsageTag &tag) {
1301 UpdateStateResolveAction update(*this, tag);
1302 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1303}
1304
John Zulaufaff20662020-06-01 14:07:58 -06001305void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1306 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1307 const ResourceUsageTag &tag) {
1308 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1309 VkExtent3D extent = CastTo3D(render_area.extent);
1310 VkOffset3D offset = CastTo3D(render_area.offset);
1311
1312 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1313 if (rp_state.attachment_last_subpass[i] == subpass) {
1314 if (attachment_views[i] == nullptr) continue; // UNUSED
1315 const auto &view = *attachment_views[i];
1316 const IMAGE_STATE *image = view.image_state.get();
1317 if (image == nullptr) continue;
1318
1319 const auto &ci = attachment_ci[i];
1320 const bool has_depth = FormatHasDepth(ci.format);
1321 const bool has_stencil = FormatHasStencil(ci.format);
1322 const bool is_color = !(has_depth || has_stencil);
1323 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1324
1325 if (is_color && store_op_stores) {
1326 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1327 offset, extent, tag);
1328 } else {
1329 auto update_range = view.normalized_subresource_range;
1330 if (has_depth && store_op_stores) {
1331 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1332 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1333 tag);
1334 }
1335 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1336 if (has_stencil && stencil_op_stores) {
1337 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1338 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1339 tag);
1340 }
1341 }
1342 }
1343 }
1344}
1345
John Zulauf540266b2020-04-06 18:54:53 -06001346template <typename Action>
1347void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1348 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001349 for (const auto address_type : kAddressTypes) {
1350 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001351 }
1352}
1353
1354void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001355 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1356 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -06001357 for (const auto address_type : kAddressTypes) {
John Zulauf355e49b2020-04-24 15:11:15 -06001358 context.ResolveAccessRange(address_type, full_range, &context.GetDstExternalTrackBack().barrier,
1359 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001360 }
1361 }
1362}
1363
John Zulauf355e49b2020-04-24 15:11:15 -06001364void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1365 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1366 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) {
1367 const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1368 UpdateMemoryAccess(image, subresource_range, barrier_action);
1369}
1370
John Zulauf7635de32020-05-29 17:14:15 -06001371// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001372void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1373 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1374 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range,
1375 bool layout_transition, const ResourceUsageTag &tag) {
1376 if (layout_transition) {
1377 UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent,
1378 tag);
1379 ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope,
1380 subresource_range);
John Zulaufc9201222020-05-13 15:13:03 -06001381 } else {
1382 ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range);
John Zulauf355e49b2020-04-24 15:11:15 -06001383 }
John Zulauf355e49b2020-04-24 15:11:15 -06001384}
1385
John Zulauf7635de32020-05-29 17:14:15 -06001386// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001387void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier,
1388 const VkImageSubresourceRange &subresource_range, bool layout_transition,
1389 const ResourceUsageTag &tag) {
1390 ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope,
1391 subresource_range, layout_transition, tag);
1392}
1393
1394// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001395HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001396 if (!attach_view) return HazardResult();
1397 const auto image_state = attach_view->image_state.get();
1398 if (!image_state) return HazardResult();
1399
John Zulauf355e49b2020-04-24 15:11:15 -06001400 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001401 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001402
1403 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulauf7635de32020-05-29 17:14:15 -06001404 auto hazard = track_back.context->DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope,
1405 track_back.barrier.src_access_scope,
1406 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001407 if (!hazard.hazard) {
1408 // The Async hazard check is against the current context's async set.
John Zulauf7635de32020-05-29 17:14:15 -06001409 hazard = DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope, track_back.barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001410 attach_view->normalized_subresource_range, kDetectAsync);
1411 }
1412 return hazard;
1413}
1414
1415// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1416bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1417
1418 const VkRenderPassBeginInfo *pRenderPassBegin,
1419 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1420 const char *func_name) const {
1421 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1422 bool skip = false;
1423 uint32_t subpass = 0;
1424 const auto &transitions = rp_state.subpass_transitions[subpass];
1425 if (transitions.size()) {
1426 const std::vector<AccessContext> empty_context_vector;
1427 // Create context we can use to validate against...
1428 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1429 const_cast<AccessContext *>(&cb_access_context_));
1430
1431 assert(pRenderPassBegin);
1432 if (nullptr == pRenderPassBegin) return skip;
1433
1434 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1435 assert(fb_state);
1436 if (nullptr == fb_state) return skip;
1437
1438 // Create a limited array of views (which we'll need to toss
1439 std::vector<const IMAGE_VIEW_STATE *> views;
1440 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
1441 const auto attachment_count = count_attachment.first;
1442 const auto *attachments = count_attachment.second;
1443 views.resize(attachment_count, nullptr);
1444 for (const auto &transition : transitions) {
1445 assert(transition.attachment < attachment_count);
1446 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
1447 }
1448
John Zulauf7635de32020-05-29 17:14:15 -06001449 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
1450 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001451 }
1452 return skip;
1453}
1454
locke-lunarg61870c22020-06-09 14:51:50 -06001455bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1456 const char *func_name) const {
1457 bool skip = false;
1458 const PIPELINE_STATE *pPipe = nullptr;
1459 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1460 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1461 if (!pPipe || !per_sets) {
1462 return skip;
1463 }
1464
1465 using DescriptorClass = cvdescriptorset::DescriptorClass;
1466 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1467 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1468 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1469 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1470
1471 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarg37047832020-06-12 13:44:45 -06001472 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001473 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1474 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001475 for (const auto &set_binding : stage_state.descriptor_uses) {
1476 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1477 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1478 set_binding.first.second);
1479 const auto descriptor_type = binding_it.GetType();
1480 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1481 auto array_idx = 0;
1482
1483 if (binding_it.IsVariableDescriptorCount()) {
1484 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1485 }
1486 SyncStageAccessIndex sync_index =
1487 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1488
1489 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1490 uint32_t index = i - index_range.start;
1491 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1492 switch (descriptor->GetClass()) {
1493 case DescriptorClass::ImageSampler:
1494 case DescriptorClass::Image: {
1495 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001496 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001497 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001498 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1499 img_view_state = image_sampler_descriptor->GetImageViewState();
1500 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001501 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001502 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1503 img_view_state = image_descriptor->GetImageViewState();
1504 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001505 }
1506 if (!img_view_state) continue;
1507 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1508 VkExtent3D extent = {};
1509 VkOffset3D offset = {};
1510 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1511 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1512 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1513 } else {
1514 extent = img_state->createInfo.extent;
1515 }
John Zulauf361fb532020-07-22 10:45:39 -06001516 HazardResult hazard;
1517 const auto &subresource_range = img_view_state->normalized_subresource_range;
1518 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1519 // Input attachments are subject to raster ordering rules
1520 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
1521 kAttachmentRasterOrder, offset, extent);
1522 } else {
1523 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1524 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001525 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001526 skip |= sync_state_->LogError(
1527 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001528 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1529 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001530 func_name, string_SyncHazard(hazard.hazard),
1531 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1532 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1533 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001534 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1535 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1536 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001537 }
1538 break;
1539 }
1540 case DescriptorClass::TexelBuffer: {
1541 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1542 if (!buf_view_state) continue;
1543 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001544 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001545 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001546 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001547 skip |= sync_state_->LogError(
1548 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001549 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1550 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001551 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1552 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1553 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001554 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1555 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1556 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001557 }
1558 break;
1559 }
1560 case DescriptorClass::GeneralBuffer: {
1561 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1562 auto buf_state = buffer_descriptor->GetBufferState();
1563 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001564 const ResourceAccessRange range =
1565 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001566 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001567 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001568 skip |= sync_state_->LogError(
1569 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001570 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1571 func_name, string_SyncHazard(hazard.hazard),
1572 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001573 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1574 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001575 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1576 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1577 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001578 }
1579 break;
1580 }
1581 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1582 default:
1583 break;
1584 }
1585 }
1586 }
1587 }
1588 return skip;
1589}
1590
1591void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1592 const ResourceUsageTag &tag) {
1593 const PIPELINE_STATE *pPipe = nullptr;
1594 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1595 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1596 if (!pPipe || !per_sets) {
1597 return;
1598 }
1599
1600 using DescriptorClass = cvdescriptorset::DescriptorClass;
1601 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1602 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1603 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1604 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1605
1606 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001607 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
1608 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1609 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001610 for (const auto &set_binding : stage_state.descriptor_uses) {
1611 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1612 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1613 set_binding.first.second);
1614 const auto descriptor_type = binding_it.GetType();
1615 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1616 auto array_idx = 0;
1617
1618 if (binding_it.IsVariableDescriptorCount()) {
1619 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1620 }
1621 SyncStageAccessIndex sync_index =
1622 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1623
1624 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1625 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1626 switch (descriptor->GetClass()) {
1627 case DescriptorClass::ImageSampler:
1628 case DescriptorClass::Image: {
1629 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1630 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1631 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1632 } else {
1633 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1634 }
1635 if (!img_view_state) continue;
1636 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1637 VkExtent3D extent = {};
1638 VkOffset3D offset = {};
1639 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1640 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1641 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1642 } else {
1643 extent = img_state->createInfo.extent;
1644 }
1645 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1646 offset, extent, tag);
1647 break;
1648 }
1649 case DescriptorClass::TexelBuffer: {
1650 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1651 if (!buf_view_state) continue;
1652 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001653 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001654 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1655 break;
1656 }
1657 case DescriptorClass::GeneralBuffer: {
1658 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1659 auto buf_state = buffer_descriptor->GetBufferState();
1660 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001661 const ResourceAccessRange range =
1662 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001663 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1664 break;
1665 }
1666 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1667 default:
1668 break;
1669 }
1670 }
1671 }
1672 }
1673}
1674
1675bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1676 bool skip = false;
1677 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1678 if (!pPipe) {
1679 return skip;
1680 }
1681
1682 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1683 const auto &binding_buffers_size = binding_buffers.size();
1684 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1685
1686 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1687 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1688 if (binding_description.binding < binding_buffers_size) {
1689 const auto &binding_buffer = binding_buffers[binding_description.binding];
1690 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1691
1692 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06001693 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1694 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001695 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1696 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001697 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001698 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 -06001699 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001700 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001701 }
1702 }
1703 }
1704 return skip;
1705}
1706
1707void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
1708 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1709 if (!pPipe) {
1710 return;
1711 }
1712 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1713 const auto &binding_buffers_size = binding_buffers.size();
1714 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1715
1716 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1717 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1718 if (binding_description.binding < binding_buffers_size) {
1719 const auto &binding_buffer = binding_buffers[binding_description.binding];
1720 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1721
1722 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06001723 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1724 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001725 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1726 }
1727 }
1728}
1729
1730bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1731 bool skip = false;
1732 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
1733
1734 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1735 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001736 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1737 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001738 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1739 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001740 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001741 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 -06001742 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001743 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001744 }
1745
1746 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1747 // We will detect more accurate range in the future.
1748 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1749 return skip;
1750}
1751
1752void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
1753 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return;
1754
1755 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1756 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001757 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1758 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001759 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1760
1761 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1762 // We will detect more accurate range in the future.
1763 RecordDrawVertex(UINT32_MAX, 0, tag);
1764}
1765
1766bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06001767 bool skip = false;
1768 if (!current_renderpass_context_) return skip;
1769 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1770 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1771 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001772}
1773
1774void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
locke-lunarg7077d502020-06-18 21:37:26 -06001775 if (current_renderpass_context_)
1776 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1777 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001778}
1779
John Zulauf355e49b2020-04-24 15:11:15 -06001780bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001781 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001782 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06001783 skip |=
1784 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001785
1786 return skip;
1787}
1788
1789bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1790 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001791 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001792 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001793 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06001794 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1795 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001796
1797 return skip;
1798}
1799
1800void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1801 assert(sync_state_);
1802 if (!cb_state_) return;
1803
1804 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06001805 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06001806 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06001807 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001808 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001809}
1810
John Zulauf355e49b2020-04-24 15:11:15 -06001811void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001812 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001813 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001814 current_context_ = &current_renderpass_context_->CurrentContext();
1815}
1816
John Zulauf355e49b2020-04-24 15:11:15 -06001817void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001818 assert(current_renderpass_context_);
1819 if (!current_renderpass_context_) return;
1820
John Zulauf1a224292020-06-30 14:52:13 -06001821 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001822 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001823 current_renderpass_context_ = nullptr;
1824}
1825
locke-lunarg61870c22020-06-09 14:51:50 -06001826bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1827 const VkRect2D &render_area, const char *func_name) const {
1828 bool skip = false;
locke-lunarg96dc9632020-06-10 17:22:18 -06001829 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001830 if (!pPipe ||
1831 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001832 return skip;
1833 }
1834 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001835 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1836 VkExtent3D extent = CastTo3D(render_area.extent);
1837 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001838
John Zulauf1a224292020-06-30 14:52:13 -06001839 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001840 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001841 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1842 for (const auto location : list) {
1843 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1844 continue;
1845 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001846 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1847 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06001848 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001849 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001850 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001851 func_name, string_SyncHazard(hazard.hazard),
1852 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1853 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001854 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001855 }
1856 }
1857 }
locke-lunarg37047832020-06-12 13:44:45 -06001858
1859 // PHASE1 TODO: Add layout based read/vs. write selection.
1860 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1861 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1862 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001863 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001864 bool depth_write = false, stencil_write = false;
1865
1866 // PHASE1 TODO: These validation should be in core_checks.
1867 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1868 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1869 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1870 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1871 depth_write = true;
1872 }
1873 // PHASE1 TODO: It needs to check if stencil is writable.
1874 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1875 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1876 // PHASE1 TODO: These validation should be in core_checks.
1877 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1878 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1879 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1880 stencil_write = true;
1881 }
1882
1883 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1884 if (depth_write) {
1885 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001886 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1887 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001888 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001889 skip |= sync_state.LogError(
1890 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001891 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001892 func_name, string_SyncHazard(hazard.hazard),
1893 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1894 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001895 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001896 }
1897 }
1898 if (stencil_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_STENCIL_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 stencil 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 }
locke-lunarg61870c22020-06-09 14:51:50 -06001911 }
1912 }
1913 return skip;
1914}
1915
locke-lunarg96dc9632020-06-10 17:22:18 -06001916void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1917 const ResourceUsageTag &tag) {
1918 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001919 if (!pPipe ||
1920 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001921 return;
1922 }
1923 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001924 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1925 VkExtent3D extent = CastTo3D(render_area.extent);
1926 VkOffset3D offset = CastTo3D(render_area.offset);
1927
John Zulauf1a224292020-06-30 14:52:13 -06001928 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001929 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001930 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1931 for (const auto location : list) {
1932 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1933 continue;
1934 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001935 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
1936 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001937 }
1938 }
locke-lunarg37047832020-06-12 13:44:45 -06001939
1940 // PHASE1 TODO: Add layout based read/vs. write selection.
1941 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1942 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1943 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001944 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001945 bool depth_write = false, stencil_write = false;
1946
1947 // PHASE1 TODO: These validation should be in core_checks.
1948 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1949 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1950 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1951 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1952 depth_write = true;
1953 }
1954 // PHASE1 TODO: It needs to check if stencil is writable.
1955 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1956 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1957 // PHASE1 TODO: These validation should be in core_checks.
1958 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1959 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1960 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1961 stencil_write = true;
1962 }
1963
1964 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1965 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001966 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1967 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001968 }
1969 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001970 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1971 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001972 }
locke-lunarg61870c22020-06-09 14:51:50 -06001973 }
1974}
1975
John Zulauf1507ee42020-05-18 11:33:09 -06001976bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
1977 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001978 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001979 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06001980 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1981 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001982 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1983 func_name);
1984
John Zulauf355e49b2020-04-24 15:11:15 -06001985 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06001986 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06001987 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1988 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1989 return skip;
1990}
1991bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
1992 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001993 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06001994 bool skip = false;
1995 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1996 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001997 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1998 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06001999 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002000 return skip;
2001}
2002
John Zulauf7635de32020-05-29 17:14:15 -06002003AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2004 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2005}
2006
2007bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2008 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002009 bool skip = false;
2010
John Zulauf7635de32020-05-29 17:14:15 -06002011 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2012 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2013 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2014 // to apply and only copy then, if this proves a hot spot.
2015 std::unique_ptr<AccessContext> proxy_for_current;
2016
John Zulauf355e49b2020-04-24 15:11:15 -06002017 // Validate the "finalLayout" transitions to external
2018 // Get them from where there we're hidding in the extra entry.
2019 const auto &final_transitions = rp_state_->subpass_transitions.back();
2020 for (const auto &transition : final_transitions) {
2021 const auto &attach_view = attachment_views_[transition.attachment];
2022 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2023 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002024 auto *context = trackback.context;
2025
2026 if (transition.prev_pass == current_subpass_) {
2027 if (!proxy_for_current) {
2028 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2029 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2030 }
2031 context = proxy_for_current.get();
2032 }
2033
2034 auto hazard = context->DetectImageBarrierHazard(
John Zulauf355e49b2020-04-24 15:11:15 -06002035 *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope,
2036 attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious);
2037 if (hazard.hazard) {
2038 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2039 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf389c34b2020-07-28 11:19:35 -06002040 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002041 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf389c34b2020-07-28 11:19:35 -06002042 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf37ceaed2020-07-03 16:18:15 -06002043 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002044 }
2045 }
2046 return skip;
2047}
2048
2049void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2050 // Add layout transitions...
2051 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
2052 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -06002053 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -06002054 for (const auto &transition : transitions) {
2055 const auto attachment_view = attachment_views_[transition.attachment];
2056 if (!attachment_view) continue;
2057 const auto image = attachment_view->image_state.get();
2058 if (!image) continue;
2059
2060 const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
John Zulaufc9201222020-05-13 15:13:03 -06002061 auto insert_pair = view_seen.insert(attachment_view);
2062 if (insert_pair.second) {
2063 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
2064 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag);
2065
2066 } else {
2067 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
2068 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
2069 auto barrier_to_transition = barrier->barrier;
2070 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
2071 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag);
2072 }
John Zulauf355e49b2020-04-24 15:11:15 -06002073 }
2074}
2075
John Zulauf1507ee42020-05-18 11:33:09 -06002076void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2077 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2078 auto &subpass_context = subpass_contexts_[current_subpass_];
2079 VkExtent3D extent = CastTo3D(render_area.extent);
2080 VkOffset3D offset = CastTo3D(render_area.offset);
2081
2082 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2083 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2084 if (attachment_views_[i] == nullptr) continue; // UNUSED
2085 const auto &view = *attachment_views_[i];
2086 const IMAGE_STATE *image = view.image_state.get();
2087 if (image == nullptr) continue;
2088
2089 const auto &ci = attachment_ci[i];
2090 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002091 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002092 const bool is_color = !(has_depth || has_stencil);
2093
2094 if (is_color) {
2095 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2096 extent, tag);
2097 } else {
2098 auto update_range = view.normalized_subresource_range;
2099 if (has_depth) {
2100 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2101 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2102 }
2103 if (has_stencil) {
2104 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2105 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2106 tag);
2107 }
2108 }
2109 }
2110 }
2111}
2112
John Zulauf355e49b2020-04-24 15:11:15 -06002113void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002114 const AccessContext *external_context, VkQueueFlags queue_flags,
2115 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002116 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002117 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002118 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2119 // Add this for all subpasses here so that they exsist during next subpass validation
2120 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002121 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002122 }
2123 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2124
2125 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002126 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002127}
John Zulauf1507ee42020-05-18 11:33:09 -06002128
2129void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002130 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2131 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002132 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002133
John Zulauf355e49b2020-04-24 15:11:15 -06002134 current_subpass_++;
2135 assert(current_subpass_ < subpass_contexts_.size());
2136 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002137 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002138}
2139
John Zulauf1a224292020-06-30 14:52:13 -06002140void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2141 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002142 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002143 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002144 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002145
John Zulauf355e49b2020-04-24 15:11:15 -06002146 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002147 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002148
2149 // Add the "finalLayout" transitions to external
2150 // Get them from where there we're hidding in the extra entry.
2151 const auto &final_transitions = rp_state_->subpass_transitions.back();
2152 for (const auto &transition : final_transitions) {
2153 const auto &attachment = attachment_views_[transition.attachment];
2154 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002155 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf1a224292020-06-30 14:52:13 -06002156 external_context->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier,
2157 attachment->normalized_subresource_range, true, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002158 }
2159}
2160
John Zulauf3d84f1b2020-03-09 13:33:25 -06002161SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2162 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2163 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2164 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2165 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2166 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2167 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2168}
2169
2170void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) {
2171 ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
2172 ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope);
2173}
2174
John Zulauf9cb530d2019-09-30 14:14:10 -06002175HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2176 HazardResult hazard;
2177 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002178 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002179 if (IsRead(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002180 // Only check reads vs. last_write if it doesn't happen-after any other read because either:
2181 // * the previous reads are not hazards, and thus last_write must be visible and available to
2182 // any reads that happen after.
2183 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2184 // the current read will be also not be a hazard, thus reporting a hazard here adds no needed information.
2185 if (((usage_stage & read_execution_barriers) == 0) && last_write && IsWriteHazard(usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002186 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002187 }
2188 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002189 // Write operation:
2190 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2191 // If reads exists -- test only against them because either:
2192 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2193 // * 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
2194 // the current write happens after the reads, so just test the write against the reades
2195 // Otherwise test against last_write
2196 //
2197 // Look for casus belli for WAR
2198 if (last_read_count) {
2199 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2200 const auto &read_access = last_reads[read_index];
2201 if (IsReadHazard(usage_stage, read_access)) {
2202 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2203 break;
2204 }
2205 }
2206 } else if (input_attachment_barriers != kNoAttachmentRead) {
John Zulaufd14743a2020-07-03 09:42:39 -06002207 if (IsReadHazard(usage_stage, input_attachment_barriers)) {
John Zulauf59e25072020-07-17 10:55:21 -06002208 hazard.Set(this, usage_index, WRITE_AFTER_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002209 }
John Zulauf361fb532020-07-22 10:45:39 -06002210 } else if (last_write && IsWriteHazard(usage)) {
2211 // Write-After-Write check -- if we have a previous write to test against
2212 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002213 }
2214 }
2215 return hazard;
2216}
2217
John Zulauf69133422020-05-20 14:55:53 -06002218HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2219 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2220 HazardResult hazard;
2221 const auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002222 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf69133422020-05-20 14:55:53 -06002223 const bool write_is_ordered = (last_write & ordering.access_scope) == last_write; // Is true if no write, and that's good.
2224 if (IsRead(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002225 if (!write_is_ordered) {
2226 // Only check for RAW if the write is unordered, and there are no reads ordered before the current read since last_write
2227 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2228 // We need to assemble the effect read_execution barriers from the union of the state barriers and the ordering rules
2229 // Check to see if there are any reads ordered before usage, including ordering rules and barriers.
2230 bool ordered_read = 0 != ((last_read_stages & ordering.exec_scope) | (read_execution_barriers & usage_stage));
2231 // Noting the "special* encoding of the input attachment ordering rule (in access, but not exec)
2232 if ((ordering.access_scope & SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) &&
2233 (input_attachment_barriers != kNoAttachmentRead)) {
2234 ordered_read = true;
John Zulauf69133422020-05-20 14:55:53 -06002235 }
John Zulaufd14743a2020-07-03 09:42:39 -06002236
John Zulauf361fb532020-07-22 10:45:39 -06002237 if (!ordered_read && IsWriteHazard(usage)) {
2238 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2239 }
2240 }
2241
2242 } else {
2243 // Only check for WAW if there are no reads since last_write
2244 if (last_read_count) {
2245 // Ignore ordered read stages (which represent frame-buffer local operations, except input attachment
2246 const auto unordered_reads = last_read_stages & ~ordering.exec_scope;
2247 // Look for any WAR hazards outside the ordered set of stages
2248 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2249 const auto &read_access = last_reads[read_index];
2250 if ((read_access.stage & unordered_reads) && IsReadHazard(usage_stage, read_access)) {
2251 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2252 break;
John Zulaufd14743a2020-07-03 09:42:39 -06002253 }
2254 }
John Zulauf361fb532020-07-22 10:45:39 -06002255 } else if (input_attachment_barriers != kNoAttachmentRead) {
2256 // This is special case code for the fragment shader input attachment, which unlike all other fragment shader operations
2257 // is framebuffer local, and thus subject to raster ordering guarantees
2258 if (0 == (ordering.access_scope & SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT)) {
2259 // NOTE: Currently all ordering barriers include this bit, so this code may never be reached, but it's
2260 // here s.t. if we need to change the ordering barrier/rules we needn't change the code.
2261 hazard.Set(this, usage_index, WRITE_AFTER_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
2262 }
2263 } else if (!write_is_ordered && IsWriteHazard(usage)) {
2264 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf69133422020-05-20 14:55:53 -06002265 }
2266 }
2267 return hazard;
2268}
2269
John Zulauf2f952d22020-02-10 11:34:51 -07002270// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06002271HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002272 HazardResult hazard;
2273 auto usage = FlagBit(usage_index);
2274 if (IsRead(usage)) {
2275 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002276 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002277 }
2278 } else {
2279 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002280 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002281 } else if (last_read_count > 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002282 hazard.Set(this, usage_index, WRITE_RACING_READ, last_reads[0].access, last_reads[0].tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002283 } else if (input_attachment_barriers != kNoAttachmentRead) {
John Zulauf59e25072020-07-17 10:55:21 -06002284 hazard.Set(this, usage_index, WRITE_RACING_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002285 }
2286 }
2287 return hazard;
2288}
2289
John Zulauf36bcf6a2020-02-03 15:12:52 -07002290HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
2291 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002292 // Only supporting image layout transitions for now
2293 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2294 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002295 // only test for WAW if there no intervening read operations.
2296 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2297 if (last_read_count) {
John Zulauf355e49b2020-04-24 15:11:15 -06002298 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002299 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002300 const auto &read_access = last_reads[read_index];
2301 // If the read stage is not in the src sync sync
2302 // *AND* not execution chained with an existing sync barrier (that's the or)
2303 // then the barrier access is unsafe (R/W after R)
2304 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002305 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002306 break;
2307 }
2308 }
John Zulauf361fb532020-07-22 10:45:39 -06002309 } else if (input_attachment_barriers != kNoAttachmentRead) {
John Zulaufd14743a2020-07-03 09:42:39 -06002310 // Same logic as read acces above for the special case of input attachment read
John Zulaufd14743a2020-07-03 09:42:39 -06002311 if ((src_exec_scope & (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | input_attachment_barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002312 hazard.Set(this, usage_index, WRITE_AFTER_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT, input_attachment_tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002313 }
John Zulauf361fb532020-07-22 10:45:39 -06002314 } else if (last_write) {
2315 // If the previous write is *not* in the 1st access scope
2316 // *AND* the current barrier is not in the dependency chain
2317 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2318 // then the barrier access is unsafe (R/W after W)
2319 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
2320 // TODO: Do we need a difference hazard name for this?
2321 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2322 }
John Zulaufd14743a2020-07-03 09:42:39 -06002323 }
John Zulauf361fb532020-07-22 10:45:39 -06002324
John Zulauf0cb5be22020-01-23 12:18:22 -07002325 return hazard;
2326}
2327
John Zulauf5f13a792020-03-10 07:31:21 -06002328// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2329// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2330// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2331void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2332 if (write_tag.IsBefore(other.write_tag)) {
2333 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
2334 *this = other;
2335 } else if (!other.write_tag.IsBefore(write_tag)) {
2336 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2337 // dependency chaining logic or any stage expansion)
2338 write_barriers |= other.write_barriers;
2339
John Zulaufd14743a2020-07-03 09:42:39 -06002340 // Merge the read states
2341 if (input_attachment_barriers == kNoAttachmentRead) {
2342 // this doesn't have an input attachment read, so we'll take other, unconditionally (even if it's kNoAttachmentRead)
2343 input_attachment_barriers = other.input_attachment_barriers;
2344 input_attachment_tag = other.input_attachment_tag;
2345 } else if (other.input_attachment_barriers != kNoAttachmentRead) {
2346 // Both states have an input attachment read, pick the newest tag and merge barriers.
2347 if (input_attachment_tag.IsBefore(other.input_attachment_tag)) {
2348 input_attachment_tag = other.input_attachment_tag;
2349 }
2350 input_attachment_barriers |= other.input_attachment_barriers;
2351 }
2352 // The else clause is that only this has an attachment read and no merge is needed
2353
John Zulauf5f13a792020-03-10 07:31:21 -06002354 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2355 auto &other_read = other.last_reads[other_read_index];
2356 if (last_read_stages & other_read.stage) {
2357 // Merge in the barriers for read stages that exist in *both* this and other
2358 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
2359 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
2360 auto &my_read = last_reads[my_read_index];
2361 if (other_read.stage == my_read.stage) {
2362 if (my_read.tag.IsBefore(other_read.tag)) {
2363 my_read.tag = other_read.tag;
John Zulauf37ceaed2020-07-03 16:18:15 -06002364 my_read.access = other_read.access;
John Zulauf5f13a792020-03-10 07:31:21 -06002365 }
2366 my_read.barriers |= other_read.barriers;
2367 break;
2368 }
2369 }
2370 } else {
2371 // The other read stage doesn't exist in this, so add it.
2372 last_reads[last_read_count] = other_read;
2373 last_read_count++;
2374 last_read_stages |= other_read.stage;
2375 }
2376 }
John Zulauf361fb532020-07-22 10:45:39 -06002377 read_execution_barriers |= other.read_execution_barriers;
John Zulauf5f13a792020-03-10 07:31:21 -06002378 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
2379 // it.
2380}
2381
John Zulauf9cb530d2019-09-30 14:14:10 -06002382void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2383 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2384 const auto usage_bit = FlagBit(usage_index);
John Zulaufd14743a2020-07-03 09:42:39 -06002385 if (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) {
2386 // Input attachment requires special treatment for raster/load/store ordering guarantees
2387 input_attachment_barriers = 0;
2388 input_attachment_tag = tag;
2389 } else if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002390 // Mulitple outstanding reads may be of interest and do dependency chains independently
2391 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2392 const auto usage_stage = PipelineStageBit(usage_index);
2393 if (usage_stage & last_read_stages) {
2394 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2395 ReadState &access = last_reads[read_index];
2396 if (access.stage == usage_stage) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002397 access.access = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002398 access.barriers = 0;
2399 access.tag = tag;
2400 break;
2401 }
2402 }
2403 } else {
2404 // We don't have this stage in the list yet...
2405 assert(last_read_count < last_reads.size());
2406 ReadState &access = last_reads[last_read_count++];
2407 access.stage = usage_stage;
John Zulauf37ceaed2020-07-03 16:18:15 -06002408 access.access = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002409 access.barriers = 0;
2410 access.tag = tag;
2411 last_read_stages |= usage_stage;
2412 }
2413 } else {
2414 // Assume write
2415 // TODO determine what to do with READ-WRITE operations if any
John Zulaufd14743a2020-07-03 09:42:39 -06002416 // Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
John Zulauf9cb530d2019-09-30 14:14:10 -06002417 // if the last_reads/last_write were unsafe, we've reported them,
2418 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
2419 last_read_count = 0;
2420 last_read_stages = 0;
John Zulauf361fb532020-07-22 10:45:39 -06002421 read_execution_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002422
John Zulaufd14743a2020-07-03 09:42:39 -06002423 input_attachment_barriers = kNoAttachmentRead; // Denotes no outstanding input attachment read after the last write.
2424 // NOTE: we don't reset the tag, as the equality check ignores it when kNoAttachmentRead is set.
2425
John Zulauf9cb530d2019-09-30 14:14:10 -06002426 write_barriers = 0;
2427 write_dependency_chain = 0;
2428 write_tag = tag;
2429 last_write = usage_bit;
2430 }
2431}
John Zulauf5f13a792020-03-10 07:31:21 -06002432
John Zulauf9cb530d2019-09-30 14:14:10 -06002433void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
2434 // Execution Barriers only protect read operations
2435 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2436 ReadState &access = last_reads[read_index];
2437 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2438 if (srcStageMask & (access.stage | access.barriers)) {
2439 access.barriers |= dstStageMask;
John Zulauf361fb532020-07-22 10:45:39 -06002440 read_execution_barriers |= dstStageMask;
John Zulauf9cb530d2019-09-30 14:14:10 -06002441 }
2442 }
John Zulauf361fb532020-07-22 10:45:39 -06002443 if ((input_attachment_barriers != kNoAttachmentRead) &&
2444 (srcStageMask & (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | input_attachment_barriers))) {
John Zulaufd14743a2020-07-03 09:42:39 -06002445 input_attachment_barriers |= dstStageMask;
John Zulauf361fb532020-07-22 10:45:39 -06002446 read_execution_barriers |= dstStageMask;
John Zulaufd14743a2020-07-03 09:42:39 -06002447 }
John Zulauf361fb532020-07-22 10:45:39 -06002448 if (write_dependency_chain & srcStageMask) {
2449 write_dependency_chain |= dstStageMask;
2450 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002451}
2452
John Zulauf36bcf6a2020-02-03 15:12:52 -07002453void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
2454 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002455 // Assuming we've applied the execution side of this barrier, we update just the write
2456 // The || implements the "dependency chain" logic for this barrier
John Zulauf36bcf6a2020-02-03 15:12:52 -07002457 if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) {
2458 write_barriers |= dst_access_scope;
2459 write_dependency_chain |= dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06002460 }
2461}
2462
John Zulauf59e25072020-07-17 10:55:21 -06002463// This should be just Bits or Index, but we don't have an invalid state for Index
2464VkPipelineStageFlags ResourceAccessState::GetReadBarriers(SyncStageAccessFlags usage_bit) const {
2465 VkPipelineStageFlags barriers = 0U;
2466 if (usage_bit & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) {
2467 barriers = input_attachment_barriers;
2468 } else {
2469 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2470 const auto &read_access = last_reads[read_index];
2471 if (read_access.access & usage_bit) {
2472 barriers = read_access.barriers;
2473 break;
2474 }
2475 }
2476 }
2477 return barriers;
2478}
2479
John Zulaufd1f85d42020-04-15 12:23:15 -06002480void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002481 auto *access_context = GetAccessContextNoInsert(command_buffer);
2482 if (access_context) {
2483 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002484 }
2485}
2486
John Zulaufd1f85d42020-04-15 12:23:15 -06002487void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2488 auto access_found = cb_access_state.find(command_buffer);
2489 if (access_found != cb_access_state.end()) {
2490 access_found->second->Reset();
2491 cb_access_state.erase(access_found);
2492 }
2493}
2494
John Zulauf540266b2020-04-06 18:54:53 -06002495void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002496 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
2497 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002498 const VkMemoryBarrier *pMemoryBarriers) {
2499 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07002500 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002501 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002502 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002503}
2504
John Zulauf540266b2020-04-06 18:54:53 -06002505void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002506 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2507 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002508 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002509 for (uint32_t index = 0; index < barrier_count; index++) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002510 auto barrier = barriers[index]; // barrier is a copy
John Zulauf9cb530d2019-09-30 14:14:10 -06002511 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2512 if (!buffer) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002513 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
2514 const ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002515 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2516 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2517 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2518 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002519 }
2520}
2521
John Zulauf540266b2020-04-06 18:54:53 -06002522void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2523 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2524 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002525 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002526 for (uint32_t index = 0; index < barrier_count; index++) {
2527 const auto &barrier = barriers[index];
2528 const auto *image = Get<IMAGE_STATE>(barrier.image);
2529 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002530 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002531 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2532 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2533 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2534 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
2535 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002536 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002537}
2538
2539bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2540 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2541 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002542 const auto *cb_context = GetAccessContext(commandBuffer);
2543 assert(cb_context);
2544 if (!cb_context) return skip;
2545 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002546
John Zulauf3d84f1b2020-03-09 13:33:25 -06002547 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002548 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002549 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002550
2551 for (uint32_t region = 0; region < regionCount; region++) {
2552 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002553 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002554 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002555 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002556 if (hazard.hazard) {
2557 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002558 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002559 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002560 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002561 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002562 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002563 }
John Zulauf16adfc92020-04-08 10:28:33 -06002564 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002565 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06002566 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002567 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002568 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002569 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002570 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002571 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002572 }
2573 }
2574 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002575 }
2576 return skip;
2577}
2578
2579void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2580 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002581 auto *cb_context = GetAccessContext(commandBuffer);
2582 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002583 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002584 auto *context = cb_context->GetCurrentAccessContext();
2585
John Zulauf9cb530d2019-09-30 14:14:10 -06002586 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002587 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002588
2589 for (uint32_t region = 0; region < regionCount; region++) {
2590 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002591 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002592 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002593 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002594 }
John Zulauf16adfc92020-04-08 10:28:33 -06002595 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002596 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002597 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002598 }
2599 }
2600}
2601
2602bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2603 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2604 const VkImageCopy *pRegions) const {
2605 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002606 const auto *cb_access_context = GetAccessContext(commandBuffer);
2607 assert(cb_access_context);
2608 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002609
John Zulauf3d84f1b2020-03-09 13:33:25 -06002610 const auto *context = cb_access_context->GetCurrentAccessContext();
2611 assert(context);
2612 if (!context) return skip;
2613
2614 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2615 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002616 for (uint32_t region = 0; region < regionCount; region++) {
2617 const auto &copy_region = pRegions[region];
2618 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002619 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002620 copy_region.srcOffset, copy_region.extent);
2621 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002622 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002623 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002624 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002625 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002626 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002627 }
2628
2629 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002630 VkExtent3D dst_copy_extent =
2631 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002632 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002633 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002634 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002635 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002636 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002637 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002638 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002639 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002640 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002641 }
2642 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002643
John Zulauf5c5e88d2019-12-26 11:22:02 -07002644 return skip;
2645}
2646
2647void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2648 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2649 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002650 auto *cb_access_context = GetAccessContext(commandBuffer);
2651 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002652 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002653 auto *context = cb_access_context->GetCurrentAccessContext();
2654 assert(context);
2655
John Zulauf5c5e88d2019-12-26 11:22:02 -07002656 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002657 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002658
2659 for (uint32_t region = 0; region < regionCount; region++) {
2660 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002661 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002662 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2663 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002664 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002665 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002666 VkExtent3D dst_copy_extent =
2667 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002668 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2669 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002670 }
2671 }
2672}
2673
2674bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2675 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2676 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2677 uint32_t bufferMemoryBarrierCount,
2678 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2679 uint32_t imageMemoryBarrierCount,
2680 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2681 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002682 const auto *cb_access_context = GetAccessContext(commandBuffer);
2683 assert(cb_access_context);
2684 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002685
John Zulauf3d84f1b2020-03-09 13:33:25 -06002686 const auto *context = cb_access_context->GetCurrentAccessContext();
2687 assert(context);
2688 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002689
John Zulauf3d84f1b2020-03-09 13:33:25 -06002690 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002691 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2692 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002693 // Validate Image Layout transitions
2694 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2695 const auto &barrier = pImageMemoryBarriers[index];
2696 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2697 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2698 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002699 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002700 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002701 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002702 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002703 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002704 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002705 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002706 }
2707 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002708
2709 return skip;
2710}
2711
2712void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2713 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2714 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2715 uint32_t bufferMemoryBarrierCount,
2716 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2717 uint32_t imageMemoryBarrierCount,
2718 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002719 auto *cb_access_context = GetAccessContext(commandBuffer);
2720 assert(cb_access_context);
2721 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002722 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002723 auto access_context = cb_access_context->GetCurrentAccessContext();
2724 assert(access_context);
2725 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06002726
John Zulauf3d84f1b2020-03-09 13:33:25 -06002727 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002728 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002729 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002730 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
2731 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2732 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002733 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
2734 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002735 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06002736 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002737
2738 // 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 -06002739 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07002740 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06002741}
2742
2743void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
2744 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
2745 // The state tracker sets up the device state
2746 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
2747
John Zulauf5f13a792020-03-10 07:31:21 -06002748 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
2749 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06002750 // TODO: Find a good way to do this hooklessly.
2751 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
2752 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
2753 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
2754
John Zulaufd1f85d42020-04-15 12:23:15 -06002755 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2756 sync_device_state->ResetCommandBufferCallback(command_buffer);
2757 });
2758 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2759 sync_device_state->FreeCommandBufferCallback(command_buffer);
2760 });
John Zulauf9cb530d2019-09-30 14:14:10 -06002761}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002762
John Zulauf355e49b2020-04-24 15:11:15 -06002763bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2764 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
2765 bool skip = false;
2766 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
2767 auto cb_context = GetAccessContext(commandBuffer);
2768
2769 if (rp_state && cb_context) {
2770 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
2771 }
2772
2773 return skip;
2774}
2775
2776bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2777 VkSubpassContents contents) const {
2778 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2779 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2780 subpass_begin_info.contents = contents;
2781 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
2782 return skip;
2783}
2784
2785bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2786 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2787 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2788 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
2789 return skip;
2790}
2791
2792bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2793 const VkRenderPassBeginInfo *pRenderPassBegin,
2794 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2795 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2796 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
2797 return skip;
2798}
2799
John Zulauf3d84f1b2020-03-09 13:33:25 -06002800void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
2801 VkResult result) {
2802 // The state tracker sets up the command buffer state
2803 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
2804
2805 // Create/initialize the structure that trackers accesses at the command buffer scope.
2806 auto cb_access_context = GetAccessContext(commandBuffer);
2807 assert(cb_access_context);
2808 cb_access_context->Reset();
2809}
2810
2811void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06002812 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002813 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002814 if (cb_context) {
2815 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002816 }
2817}
2818
2819void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2820 VkSubpassContents contents) {
2821 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2822 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2823 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002824 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002825}
2826
2827void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2828 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2829 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002830 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002831}
2832
2833void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2834 const VkRenderPassBeginInfo *pRenderPassBegin,
2835 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2836 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002837 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
2838}
2839
2840bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2841 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
2842 bool skip = false;
2843
2844 auto cb_context = GetAccessContext(commandBuffer);
2845 assert(cb_context);
2846 auto cb_state = cb_context->GetCommandBufferState();
2847 if (!cb_state) return skip;
2848
2849 auto rp_state = cb_state->activeRenderPass;
2850 if (!rp_state) return skip;
2851
2852 skip |= cb_context->ValidateNextSubpass(func_name);
2853
2854 return skip;
2855}
2856
2857bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
2858 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
2859 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2860 subpass_begin_info.contents = contents;
2861 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
2862 return skip;
2863}
2864
2865bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2866 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2867 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2868 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
2869 return skip;
2870}
2871
2872bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2873 const VkSubpassEndInfo *pSubpassEndInfo) const {
2874 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2875 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
2876 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002877}
2878
2879void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06002880 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002881 auto cb_context = GetAccessContext(commandBuffer);
2882 assert(cb_context);
2883 auto cb_state = cb_context->GetCommandBufferState();
2884 if (!cb_state) return;
2885
2886 auto rp_state = cb_state->activeRenderPass;
2887 if (!rp_state) return;
2888
John Zulauf355e49b2020-04-24 15:11:15 -06002889 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002890}
2891
2892void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
2893 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
2894 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2895 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002896 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002897}
2898
2899void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2900 const VkSubpassEndInfo *pSubpassEndInfo) {
2901 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002902 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002903}
2904
2905void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2906 const VkSubpassEndInfo *pSubpassEndInfo) {
2907 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002908 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002909}
2910
John Zulauf355e49b2020-04-24 15:11:15 -06002911bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
2912 const char *func_name) const {
2913 bool skip = false;
2914
2915 auto cb_context = GetAccessContext(commandBuffer);
2916 assert(cb_context);
2917 auto cb_state = cb_context->GetCommandBufferState();
2918 if (!cb_state) return skip;
2919
2920 auto rp_state = cb_state->activeRenderPass;
2921 if (!rp_state) return skip;
2922
2923 skip |= cb_context->ValidateEndRenderpass(func_name);
2924 return skip;
2925}
2926
2927bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2928 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
2929 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
2930 return skip;
2931}
2932
2933bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
2934 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2935 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2936 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
2937 return skip;
2938}
2939
2940bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
2941 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2942 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2943 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
2944 return skip;
2945}
2946
2947void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
2948 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06002949 // Resolve the all subpass contexts to the command buffer contexts
2950 auto cb_context = GetAccessContext(commandBuffer);
2951 assert(cb_context);
2952 auto cb_state = cb_context->GetCommandBufferState();
2953 if (!cb_state) return;
2954
locke-lunargaecf2152020-05-12 17:15:41 -06002955 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06002956 if (!rp_state) return;
2957
John Zulauf355e49b2020-04-24 15:11:15 -06002958 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06002959}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002960
John Zulauf33fc1d52020-07-17 11:01:10 -06002961// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
2962// updates to a resource which do not conflict at the byte level.
2963// TODO: Revisit this rule to see if it needs to be tighter or looser
2964// TODO: Add programatic control over suppression heuristics
2965bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
2966 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
2967}
2968
John Zulauf3d84f1b2020-03-09 13:33:25 -06002969void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002970 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06002971 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002972}
2973
2974void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06002975 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06002976 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002977}
2978
2979void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06002980 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06002981 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002982}
locke-lunarga19c71d2020-03-02 18:17:04 -07002983
2984bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2985 VkImageLayout dstImageLayout, uint32_t regionCount,
2986 const VkBufferImageCopy *pRegions) const {
2987 bool skip = false;
2988 const auto *cb_access_context = GetAccessContext(commandBuffer);
2989 assert(cb_access_context);
2990 if (!cb_access_context) return skip;
2991
2992 const auto *context = cb_access_context->GetCurrentAccessContext();
2993 assert(context);
2994 if (!context) return skip;
2995
2996 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07002997 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2998
2999 for (uint32_t region = 0; region < regionCount; region++) {
3000 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003001 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003002 ResourceAccessRange src_range =
3003 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003004 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003005 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003006 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003007 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003008 "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003009 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003010 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003011 }
3012 }
3013 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003014 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003015 copy_region.imageOffset, copy_region.imageExtent);
3016 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003017 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003018 "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003019 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003020 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003021 }
3022 if (skip) break;
3023 }
3024 if (skip) break;
3025 }
3026 return skip;
3027}
3028
3029void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3030 VkImageLayout dstImageLayout, uint32_t regionCount,
3031 const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003032 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07003033 auto *cb_access_context = GetAccessContext(commandBuffer);
3034 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003035 const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07003036 auto *context = cb_access_context->GetCurrentAccessContext();
3037 assert(context);
3038
3039 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003040 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003041
3042 for (uint32_t region = 0; region < regionCount; region++) {
3043 const auto &copy_region = pRegions[region];
3044 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003045 ResourceAccessRange src_range =
3046 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003047 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003048 }
3049 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003050 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003051 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003052 }
3053 }
3054}
3055
3056bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3057 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3058 const VkBufferImageCopy *pRegions) const {
3059 bool skip = false;
3060 const auto *cb_access_context = GetAccessContext(commandBuffer);
3061 assert(cb_access_context);
3062 if (!cb_access_context) return skip;
3063
3064 const auto *context = cb_access_context->GetCurrentAccessContext();
3065 assert(context);
3066 if (!context) return skip;
3067
3068 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3069 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3070 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3071 for (uint32_t region = 0; region < regionCount; region++) {
3072 const auto &copy_region = pRegions[region];
3073 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003074 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003075 copy_region.imageOffset, copy_region.imageExtent);
3076 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003077 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003078 "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003079 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003080 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003081 }
3082 }
3083 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06003084 ResourceAccessRange dst_range =
3085 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003086 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003087 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003088 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003089 "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003090 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003091 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003092 }
3093 }
3094 if (skip) break;
3095 }
3096 return skip;
3097}
3098
3099void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3100 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003101 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07003102 auto *cb_access_context = GetAccessContext(commandBuffer);
3103 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003104 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER);
locke-lunarga19c71d2020-03-02 18:17:04 -07003105 auto *context = cb_access_context->GetCurrentAccessContext();
3106 assert(context);
3107
3108 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003109 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3110 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 -06003111 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003112
3113 for (uint32_t region = 0; region < regionCount; region++) {
3114 const auto &copy_region = pRegions[region];
3115 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003116 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003117 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003118 }
3119 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003120 ResourceAccessRange dst_range =
3121 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003122 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003123 }
3124 }
3125}
3126
3127bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3128 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3129 const VkImageBlit *pRegions, VkFilter filter) const {
3130 bool skip = false;
3131 const auto *cb_access_context = GetAccessContext(commandBuffer);
3132 assert(cb_access_context);
3133 if (!cb_access_context) return skip;
3134
3135 const auto *context = cb_access_context->GetCurrentAccessContext();
3136 assert(context);
3137 if (!context) return skip;
3138
3139 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3140 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3141
3142 for (uint32_t region = 0; region < regionCount; region++) {
3143 const auto &blit_region = pRegions[region];
3144 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003145 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3146 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3147 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3148 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3149 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3150 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3151 auto hazard =
3152 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003153 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003154 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003155 "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003156 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003157 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003158 }
3159 }
3160
3161 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003162 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3163 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3164 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3165 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3166 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3167 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3168 auto hazard =
3169 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003170 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003171 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003172 "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003173 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003174 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003175 }
3176 if (skip) break;
3177 }
3178 }
3179
3180 return skip;
3181}
3182
3183void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3184 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3185 const VkImageBlit *pRegions, VkFilter filter) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003186 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3187 pRegions, filter);
locke-lunarga19c71d2020-03-02 18:17:04 -07003188 auto *cb_access_context = GetAccessContext(commandBuffer);
3189 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003190 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07003191 auto *context = cb_access_context->GetCurrentAccessContext();
3192 assert(context);
3193
3194 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003195 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003196
3197 for (uint32_t region = 0; region < regionCount; region++) {
3198 const auto &blit_region = pRegions[region];
3199 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003200 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3201 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3202 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3203 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3204 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3205 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3206 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003207 }
3208 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003209 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3210 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3211 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3212 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3213 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3214 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3215 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003216 }
3217 }
3218}
locke-lunarg36ba2592020-04-03 09:42:04 -06003219
locke-lunarg61870c22020-06-09 14:51:50 -06003220bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
3221 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
3222 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003223 bool skip = false;
3224 if (drawCount == 0) return skip;
3225
3226 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3227 VkDeviceSize size = struct_size;
3228 if (drawCount == 1 || stride == size) {
3229 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003230 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003231 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3232 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003233 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003234 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003235 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003236 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003237 }
3238 } else {
3239 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003240 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003241 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3242 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003243 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003244 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3245 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3246 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003247 break;
3248 }
3249 }
3250 }
3251 return skip;
3252}
3253
locke-lunarg61870c22020-06-09 14:51:50 -06003254void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3255 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3256 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003257 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3258 VkDeviceSize size = struct_size;
3259 if (drawCount == 1 || stride == size) {
3260 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003261 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003262 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3263 } else {
3264 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003265 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003266 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3267 }
3268 }
3269}
3270
locke-lunarg61870c22020-06-09 14:51:50 -06003271bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3272 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003273 bool skip = false;
3274
3275 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003276 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003277 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3278 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003279 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003280 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003281 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003282 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003283 }
3284 return skip;
3285}
3286
locke-lunarg61870c22020-06-09 14:51:50 -06003287void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003288 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003289 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003290 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3291}
3292
locke-lunarg36ba2592020-04-03 09:42:04 -06003293bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003294 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003295 const auto *cb_access_context = GetAccessContext(commandBuffer);
3296 assert(cb_access_context);
3297 if (!cb_access_context) return skip;
3298
locke-lunarg61870c22020-06-09 14:51:50 -06003299 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003300 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003301}
3302
3303void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003304 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003305 auto *cb_access_context = GetAccessContext(commandBuffer);
3306 assert(cb_access_context);
3307 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003308
locke-lunarg61870c22020-06-09 14:51:50 -06003309 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003310}
locke-lunarge1a67022020-04-29 00:15:36 -06003311
3312bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003313 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003314 const auto *cb_access_context = GetAccessContext(commandBuffer);
3315 assert(cb_access_context);
3316 if (!cb_access_context) return skip;
3317
3318 const auto *context = cb_access_context->GetCurrentAccessContext();
3319 assert(context);
3320 if (!context) return skip;
3321
locke-lunarg61870c22020-06-09 14:51:50 -06003322 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3323 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3324 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003325 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003326}
3327
3328void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003329 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003330 auto *cb_access_context = GetAccessContext(commandBuffer);
3331 assert(cb_access_context);
3332 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3333 auto *context = cb_access_context->GetCurrentAccessContext();
3334 assert(context);
3335
locke-lunarg61870c22020-06-09 14:51:50 -06003336 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3337 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003338}
3339
3340bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3341 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003342 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003343 const auto *cb_access_context = GetAccessContext(commandBuffer);
3344 assert(cb_access_context);
3345 if (!cb_access_context) return skip;
3346
locke-lunarg61870c22020-06-09 14:51:50 -06003347 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3348 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3349 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003350 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003351}
3352
3353void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3354 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003355 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003356 auto *cb_access_context = GetAccessContext(commandBuffer);
3357 assert(cb_access_context);
3358 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003359
locke-lunarg61870c22020-06-09 14:51:50 -06003360 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3361 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3362 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003363}
3364
3365bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3366 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003367 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003368 const auto *cb_access_context = GetAccessContext(commandBuffer);
3369 assert(cb_access_context);
3370 if (!cb_access_context) return skip;
3371
locke-lunarg61870c22020-06-09 14:51:50 -06003372 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3373 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3374 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003375 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003376}
3377
3378void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3379 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003380 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003381 auto *cb_access_context = GetAccessContext(commandBuffer);
3382 assert(cb_access_context);
3383 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003384
locke-lunarg61870c22020-06-09 14:51:50 -06003385 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3386 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3387 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003388}
3389
3390bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3391 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003392 bool skip = false;
3393 if (drawCount == 0) return skip;
3394
locke-lunargff255f92020-05-13 18:53:52 -06003395 const auto *cb_access_context = GetAccessContext(commandBuffer);
3396 assert(cb_access_context);
3397 if (!cb_access_context) return skip;
3398
3399 const auto *context = cb_access_context->GetCurrentAccessContext();
3400 assert(context);
3401 if (!context) return skip;
3402
locke-lunarg61870c22020-06-09 14:51:50 -06003403 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3404 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3405 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3406 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003407
3408 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3409 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3410 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003411 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003412 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003413}
3414
3415void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3416 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003417 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003418 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003419 auto *cb_access_context = GetAccessContext(commandBuffer);
3420 assert(cb_access_context);
3421 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3422 auto *context = cb_access_context->GetCurrentAccessContext();
3423 assert(context);
3424
locke-lunarg61870c22020-06-09 14:51:50 -06003425 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3426 cb_access_context->RecordDrawSubpassAttachment(tag);
3427 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003428
3429 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3430 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3431 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003432 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003433}
3434
3435bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3436 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003437 bool skip = false;
3438 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003439 const auto *cb_access_context = GetAccessContext(commandBuffer);
3440 assert(cb_access_context);
3441 if (!cb_access_context) return skip;
3442
3443 const auto *context = cb_access_context->GetCurrentAccessContext();
3444 assert(context);
3445 if (!context) return skip;
3446
locke-lunarg61870c22020-06-09 14:51:50 -06003447 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3448 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3449 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3450 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003451
3452 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3453 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3454 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003455 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003456 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003457}
3458
3459void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3460 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003461 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003462 auto *cb_access_context = GetAccessContext(commandBuffer);
3463 assert(cb_access_context);
3464 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3465 auto *context = cb_access_context->GetCurrentAccessContext();
3466 assert(context);
3467
locke-lunarg61870c22020-06-09 14:51:50 -06003468 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3469 cb_access_context->RecordDrawSubpassAttachment(tag);
3470 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003471
3472 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3473 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3474 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003475 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003476}
3477
3478bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3479 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3480 uint32_t stride, const char *function) const {
3481 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003482 const auto *cb_access_context = GetAccessContext(commandBuffer);
3483 assert(cb_access_context);
3484 if (!cb_access_context) return skip;
3485
3486 const auto *context = cb_access_context->GetCurrentAccessContext();
3487 assert(context);
3488 if (!context) return skip;
3489
locke-lunarg61870c22020-06-09 14:51:50 -06003490 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3491 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3492 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3493 function);
3494 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003495
3496 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3497 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3498 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003499 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003500 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003501}
3502
3503bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3504 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3505 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003506 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3507 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003508}
3509
3510void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3511 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3512 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003513 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3514 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003515 auto *cb_access_context = GetAccessContext(commandBuffer);
3516 assert(cb_access_context);
3517 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3518 auto *context = cb_access_context->GetCurrentAccessContext();
3519 assert(context);
3520
locke-lunarg61870c22020-06-09 14:51:50 -06003521 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3522 cb_access_context->RecordDrawSubpassAttachment(tag);
3523 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3524 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003525
3526 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3527 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3528 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003529 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003530}
3531
3532bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3533 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3534 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003535 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3536 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003537}
3538
3539void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3540 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3541 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003542 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3543 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003544 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003545}
3546
3547bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3548 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3549 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003550 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3551 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003552}
3553
3554void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3555 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3556 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003557 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3558 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003559 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3560}
3561
3562bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3563 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3564 uint32_t stride, const char *function) const {
3565 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003566 const auto *cb_access_context = GetAccessContext(commandBuffer);
3567 assert(cb_access_context);
3568 if (!cb_access_context) return skip;
3569
3570 const auto *context = cb_access_context->GetCurrentAccessContext();
3571 assert(context);
3572 if (!context) return skip;
3573
locke-lunarg61870c22020-06-09 14:51:50 -06003574 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3575 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3576 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3577 stride, function);
3578 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003579
3580 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3581 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3582 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003583 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003584 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003585}
3586
3587bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3588 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3589 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003590 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3591 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003592}
3593
3594void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3595 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3596 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003597 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3598 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003599 auto *cb_access_context = GetAccessContext(commandBuffer);
3600 assert(cb_access_context);
3601 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3602 auto *context = cb_access_context->GetCurrentAccessContext();
3603 assert(context);
3604
locke-lunarg61870c22020-06-09 14:51:50 -06003605 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3606 cb_access_context->RecordDrawSubpassAttachment(tag);
3607 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3608 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003609
3610 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3611 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06003612 // We will update the index and vertex buffer in SubmitQueue in the future.
3613 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003614}
3615
3616bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3617 VkDeviceSize offset, VkBuffer countBuffer,
3618 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3619 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003620 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3621 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003622}
3623
3624void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3625 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3626 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003627 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3628 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003629 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3630}
3631
3632bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3633 VkDeviceSize offset, VkBuffer countBuffer,
3634 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3635 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003636 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3637 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003638}
3639
3640void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3641 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3642 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003643 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3644 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003645 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3646}
3647
3648bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3649 const VkClearColorValue *pColor, uint32_t rangeCount,
3650 const VkImageSubresourceRange *pRanges) const {
3651 bool skip = false;
3652 const auto *cb_access_context = GetAccessContext(commandBuffer);
3653 assert(cb_access_context);
3654 if (!cb_access_context) return skip;
3655
3656 const auto *context = cb_access_context->GetCurrentAccessContext();
3657 assert(context);
3658 if (!context) return skip;
3659
3660 const auto *image_state = Get<IMAGE_STATE>(image);
3661
3662 for (uint32_t index = 0; index < rangeCount; index++) {
3663 const auto &range = pRanges[index];
3664 if (image_state) {
3665 auto hazard =
3666 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3667 if (hazard.hazard) {
3668 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003669 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003670 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06003671 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003672 }
3673 }
3674 }
3675 return skip;
3676}
3677
3678void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3679 const VkClearColorValue *pColor, uint32_t rangeCount,
3680 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003681 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003682 auto *cb_access_context = GetAccessContext(commandBuffer);
3683 assert(cb_access_context);
3684 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
3685 auto *context = cb_access_context->GetCurrentAccessContext();
3686 assert(context);
3687
3688 const auto *image_state = Get<IMAGE_STATE>(image);
3689
3690 for (uint32_t index = 0; index < rangeCount; index++) {
3691 const auto &range = pRanges[index];
3692 if (image_state) {
3693 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3694 tag);
3695 }
3696 }
3697}
3698
3699bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3700 VkImageLayout imageLayout,
3701 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3702 const VkImageSubresourceRange *pRanges) const {
3703 bool skip = false;
3704 const auto *cb_access_context = GetAccessContext(commandBuffer);
3705 assert(cb_access_context);
3706 if (!cb_access_context) return skip;
3707
3708 const auto *context = cb_access_context->GetCurrentAccessContext();
3709 assert(context);
3710 if (!context) return skip;
3711
3712 const auto *image_state = Get<IMAGE_STATE>(image);
3713
3714 for (uint32_t index = 0; index < rangeCount; index++) {
3715 const auto &range = pRanges[index];
3716 if (image_state) {
3717 auto hazard =
3718 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3719 if (hazard.hazard) {
3720 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003721 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003722 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06003723 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003724 }
3725 }
3726 }
3727 return skip;
3728}
3729
3730void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3731 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3732 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003733 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003734 auto *cb_access_context = GetAccessContext(commandBuffer);
3735 assert(cb_access_context);
3736 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
3737 auto *context = cb_access_context->GetCurrentAccessContext();
3738 assert(context);
3739
3740 const auto *image_state = Get<IMAGE_STATE>(image);
3741
3742 for (uint32_t index = 0; index < rangeCount; index++) {
3743 const auto &range = pRanges[index];
3744 if (image_state) {
3745 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3746 tag);
3747 }
3748 }
3749}
3750
3751bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
3752 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
3753 VkDeviceSize dstOffset, VkDeviceSize stride,
3754 VkQueryResultFlags flags) const {
3755 bool skip = false;
3756 const auto *cb_access_context = GetAccessContext(commandBuffer);
3757 assert(cb_access_context);
3758 if (!cb_access_context) return skip;
3759
3760 const auto *context = cb_access_context->GetCurrentAccessContext();
3761 assert(context);
3762 if (!context) return skip;
3763
3764 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3765
3766 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003767 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003768 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3769 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06003770 skip |=
3771 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3772 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
3773 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003774 }
3775 }
locke-lunargff255f92020-05-13 18:53:52 -06003776
3777 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003778 return skip;
3779}
3780
3781void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
3782 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3783 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003784 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
3785 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06003786 auto *cb_access_context = GetAccessContext(commandBuffer);
3787 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06003788 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06003789 auto *context = cb_access_context->GetCurrentAccessContext();
3790 assert(context);
3791
3792 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3793
3794 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003795 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003796 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3797 }
locke-lunargff255f92020-05-13 18:53:52 -06003798
3799 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003800}
3801
3802bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3803 VkDeviceSize size, uint32_t data) const {
3804 bool skip = false;
3805 const auto *cb_access_context = GetAccessContext(commandBuffer);
3806 assert(cb_access_context);
3807 if (!cb_access_context) return skip;
3808
3809 const auto *context = cb_access_context->GetCurrentAccessContext();
3810 assert(context);
3811 if (!context) return skip;
3812
3813 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3814
3815 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003816 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06003817 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3818 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003819 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003820 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06003821 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003822 }
3823 }
3824 return skip;
3825}
3826
3827void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3828 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003829 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06003830 auto *cb_access_context = GetAccessContext(commandBuffer);
3831 assert(cb_access_context);
3832 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
3833 auto *context = cb_access_context->GetCurrentAccessContext();
3834 assert(context);
3835
3836 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3837
3838 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003839 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06003840 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3841 }
3842}
3843
3844bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3845 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3846 const VkImageResolve *pRegions) const {
3847 bool skip = false;
3848 const auto *cb_access_context = GetAccessContext(commandBuffer);
3849 assert(cb_access_context);
3850 if (!cb_access_context) return skip;
3851
3852 const auto *context = cb_access_context->GetCurrentAccessContext();
3853 assert(context);
3854 if (!context) return skip;
3855
3856 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3857 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3858
3859 for (uint32_t region = 0; region < regionCount; region++) {
3860 const auto &resolve_region = pRegions[region];
3861 if (src_image) {
3862 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3863 resolve_region.srcOffset, resolve_region.extent);
3864 if (hazard.hazard) {
3865 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003866 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003867 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003868 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003869 }
3870 }
3871
3872 if (dst_image) {
3873 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3874 resolve_region.dstOffset, resolve_region.extent);
3875 if (hazard.hazard) {
3876 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003877 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003878 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003879 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003880 }
3881 if (skip) break;
3882 }
3883 }
3884
3885 return skip;
3886}
3887
3888void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3889 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3890 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003891 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3892 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06003893 auto *cb_access_context = GetAccessContext(commandBuffer);
3894 assert(cb_access_context);
3895 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
3896 auto *context = cb_access_context->GetCurrentAccessContext();
3897 assert(context);
3898
3899 auto *src_image = Get<IMAGE_STATE>(srcImage);
3900 auto *dst_image = Get<IMAGE_STATE>(dstImage);
3901
3902 for (uint32_t region = 0; region < regionCount; region++) {
3903 const auto &resolve_region = pRegions[region];
3904 if (src_image) {
3905 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3906 resolve_region.srcOffset, resolve_region.extent, tag);
3907 }
3908 if (dst_image) {
3909 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3910 resolve_region.dstOffset, resolve_region.extent, tag);
3911 }
3912 }
3913}
3914
3915bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3916 VkDeviceSize dataSize, const void *pData) const {
3917 bool skip = false;
3918 const auto *cb_access_context = GetAccessContext(commandBuffer);
3919 assert(cb_access_context);
3920 if (!cb_access_context) return skip;
3921
3922 const auto *context = cb_access_context->GetCurrentAccessContext();
3923 assert(context);
3924 if (!context) return skip;
3925
3926 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3927
3928 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003929 // VK_WHOLE_SIZE not allowed
3930 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06003931 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3932 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003933 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003934 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06003935 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003936 }
3937 }
3938 return skip;
3939}
3940
3941void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3942 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003943 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06003944 auto *cb_access_context = GetAccessContext(commandBuffer);
3945 assert(cb_access_context);
3946 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
3947 auto *context = cb_access_context->GetCurrentAccessContext();
3948 assert(context);
3949
3950 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3951
3952 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003953 // VK_WHOLE_SIZE not allowed
3954 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06003955 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3956 }
3957}
locke-lunargff255f92020-05-13 18:53:52 -06003958
3959bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3960 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
3961 bool skip = false;
3962 const auto *cb_access_context = GetAccessContext(commandBuffer);
3963 assert(cb_access_context);
3964 if (!cb_access_context) return skip;
3965
3966 const auto *context = cb_access_context->GetCurrentAccessContext();
3967 assert(context);
3968 if (!context) return skip;
3969
3970 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3971
3972 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003973 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003974 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3975 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06003976 skip |=
3977 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3978 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
3979 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003980 }
3981 }
3982 return skip;
3983}
3984
3985void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3986 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003987 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06003988 auto *cb_access_context = GetAccessContext(commandBuffer);
3989 assert(cb_access_context);
3990 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
3991 auto *context = cb_access_context->GetCurrentAccessContext();
3992 assert(context);
3993
3994 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3995
3996 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003997 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003998 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3999 }
4000}