blob: 02efc326cfa4fd87b6ce1a1eefb1fccfeb4de0c0 [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;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700116 for (size_t i = 0; i < flags.size(); i++) {
117 if (flags.test(i)) {
118 info = &syncStageAccessInfoByStageAccessIndex[i];
119 break;
John Zulauf37ceaed2020-07-03 16:18:15 -0600120 }
121 }
122 return info;
123}
124
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700125static std::string string_SyncStageAccessFlags(const SyncStageAccessFlags &flags, const char *sep = "|") {
John Zulauf59e25072020-07-17 10:55:21 -0600126 std::string out_str;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700127 if (flags.none()) {
John Zulauf389c34b2020-07-28 11:19:35 -0600128 out_str = "0";
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700129 } else {
130 for (size_t i = 0; i < syncStageAccessInfoByStageAccessIndex.size(); i++) {
131 const auto &info = syncStageAccessInfoByStageAccessIndex[i];
132 if ((flags & info.stage_access_bit).any()) {
133 if (!out_str.empty()) {
134 out_str.append(sep);
135 }
136 out_str.append(info.name);
John Zulauf59e25072020-07-17 10:55:21 -0600137 }
John Zulauf59e25072020-07-17 10:55:21 -0600138 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700139 if (out_str.length() == 0) {
140 out_str.append("Unhandled SyncStageAccess");
141 }
John Zulauf59e25072020-07-17 10:55:21 -0600142 }
143 return out_str;
144}
145
John Zulauf37ceaed2020-07-03 16:18:15 -0600146static std::string string_UsageTag(const HazardResult &hazard) {
147 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600148 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
149 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600150 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600151 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
152 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf59e25072020-07-17 10:55:21 -0600153 out << "(usage: " << usage_info.name << ", prior_usage: " << stage_access_name;
154 if (IsHazardVsRead(hazard.hazard)) {
155 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
156 out << ", read_barriers: " << string_VkPipelineStageFlags(barriers);
157 } else {
158 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
159 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
160 }
161
162 out << ", command: " << CommandTypeString(tag.command);
163 out << ", seq_no: " << (tag.index & 0xFFFFFFFF) << ", reset_no: " << (tag.index >> 32) << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600164 return out.str();
165}
166
John Zulaufd14743a2020-07-03 09:42:39 -0600167// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
168// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
169// also reflects this special case for read hazard detection (using access instead of exec scope)
John Zulaufb027cdb2020-05-21 14:25:22 -0600170static constexpr VkPipelineStageFlags kColorAttachmentExecScope = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700171static const SyncStageAccessFlags kColorAttachmentAccessScope =
172 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
173 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
174 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
175 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600176static constexpr VkPipelineStageFlags kDepthStencilAttachmentExecScope =
177 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700178static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
179 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
180 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
181 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600182
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700183static const SyncOrderingBarrier kColorAttachmentRasterOrder = {kColorAttachmentExecScope, kColorAttachmentAccessScope};
184static const SyncOrderingBarrier kDepthStencilAttachmentRasterOrder = {kDepthStencilAttachmentExecScope,
185 kDepthStencilAttachmentAccessScope};
186static const SyncOrderingBarrier kAttachmentRasterOrder = {kDepthStencilAttachmentExecScope | kColorAttachmentExecScope,
187 kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope};
John Zulauf7635de32020-05-29 17:14:15 -0600188// 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 -0600189static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600190
John Zulaufb02c1eb2020-10-06 16:33:36 -0600191static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) {
192 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
193}
194
195static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
196
locke-lunarg3c038002020-04-30 23:08:08 -0600197inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
198 if (size == VK_WHOLE_SIZE) {
199 return (whole_size - offset);
200 }
201 return size;
202}
203
John Zulauf3e86bf02020-09-12 10:47:57 -0600204static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
205 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
206}
207
John Zulauf16adfc92020-04-08 10:28:33 -0600208template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600209static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600210 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
211}
212
John Zulauf355e49b2020-04-24 15:11:15 -0600213static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600214
John Zulauf3e86bf02020-09-12 10:47:57 -0600215static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
216 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
217}
218
219static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
220 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
221}
222
John Zulauf0cb5be22020-01-23 12:18:22 -0700223// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
224VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
225 VkPipelineStageFlags expanded = stage_mask;
226 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
227 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
228 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
229 if (all_commands.first & queue_flags) {
230 expanded |= all_commands.second;
231 }
232 }
233 }
234 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
235 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
236 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
237 }
238 return expanded;
239}
240
John Zulauf36bcf6a2020-02-03 15:12:52 -0700241VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
Jeremy Gebben91c36902020-11-09 08:17:08 -0700242 const std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
John Zulauf36bcf6a2020-02-03 15:12:52 -0700243 VkPipelineStageFlags unscanned = stage_mask;
244 VkPipelineStageFlags related = 0;
Jonah Ryan-Davis185189c2020-07-14 10:28:52 -0400245 for (const auto &entry : map) {
246 const auto &stage = entry.first;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700247 if (stage & unscanned) {
248 related = related | entry.second;
249 unscanned = unscanned & ~stage;
250 if (!unscanned) break;
251 }
252 }
253 return related;
254}
255
256VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
257 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
258}
259
260VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
261 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
262}
263
John Zulauf5c5e88d2019-12-26 11:22:02 -0700264static const ResourceAccessRange full_range(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700265
John Zulauf3e86bf02020-09-12 10:47:57 -0600266ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
267 VkDeviceSize stride) {
268 VkDeviceSize range_start = offset + first_index * stride;
269 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600270 if (count == UINT32_MAX) {
271 range_size = buf_whole_size - range_start;
272 } else {
273 range_size = count * stride;
274 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600275 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600276}
277
locke-lunarg654e3692020-06-04 17:19:15 -0600278SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
279 VkShaderStageFlagBits stage_flag) {
280 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
281 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
282 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
283 }
284 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
285 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
286 assert(0);
287 }
288 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
289 return stage_access->second.uniform_read;
290 }
291
292 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
293 // Because if write hazard happens, read hazard might or might not happen.
294 // But if write hazard doesn't happen, read hazard is impossible to happen.
295 if (descriptor_data.is_writable) {
296 return stage_access->second.shader_write;
297 }
298 return stage_access->second.shader_read;
299}
300
locke-lunarg37047832020-06-12 13:44:45 -0600301bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
302 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
303 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
304 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
305 ? true
306 : false;
307}
308
309bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
310 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
311 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
312 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
313 ? true
314 : false;
315}
316
John Zulauf355e49b2020-04-24 15:11:15 -0600317// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
318const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = {
319 AccessContext::AddressType::kLinearAddress, AccessContext::AddressType::kIdealizedAddress};
320
John Zulaufb02c1eb2020-10-06 16:33:36 -0600321template <typename Action>
322static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
323 Action &action) {
324 // At this point the "apply over range" logic only supports a single memory binding
325 if (!SimpleBinding(image_state)) return;
326 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
327 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
328 image_state.createInfo.extent);
329 const auto base_address = ResourceBaseAddress(image_state);
330 for (; range_gen->non_empty(); ++range_gen) {
331 action((*range_gen + base_address));
332 }
333}
334
John Zulauf7635de32020-05-29 17:14:15 -0600335// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
336// Used by both validation and record operations
337//
338// The signature for Action() reflect the needs of both uses.
339template <typename Action>
340void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
341 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
342 VkExtent3D extent = CastTo3D(render_area.extent);
343 VkOffset3D offset = CastTo3D(render_area.offset);
344 const auto &rp_ci = rp_state.createInfo;
345 const auto *attachment_ci = rp_ci.pAttachments;
346 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
347
348 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
349 const auto *color_attachments = subpass_ci.pColorAttachments;
350 const auto *color_resolve = subpass_ci.pResolveAttachments;
351 if (color_resolve && color_attachments) {
352 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
353 const auto &color_attach = color_attachments[i].attachment;
354 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
355 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
356 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
357 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
358 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
359 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
360 }
361 }
362 }
363
364 // Depth stencil resolve only if the extension is present
365 const auto ds_resolve = lvl_find_in_chain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
366 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
367 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
368 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
369 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
370 const auto src_ci = attachment_ci[src_at];
371 // The formats are required to match so we can pick either
372 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
373 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
374 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
375 VkImageAspectFlags aspect_mask = 0u;
376
377 // Figure out which aspects are actually touched during resolve operations
378 const char *aspect_string = nullptr;
379 if (resolve_depth && resolve_stencil) {
380 // Validate all aspects together
381 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
382 aspect_string = "depth/stencil";
383 } else if (resolve_depth) {
384 // Validate depth only
385 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
386 aspect_string = "depth";
387 } else if (resolve_stencil) {
388 // Validate all stencil only
389 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
390 aspect_string = "stencil";
391 }
392
393 if (aspect_mask) {
394 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
Jeremy Gebbenec5cd382020-11-16 15:53:45 -0700395 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kAttachmentRasterOrder, offset, extent,
John Zulauf7635de32020-05-29 17:14:15 -0600396 aspect_mask);
397 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
398 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
399 }
400 }
401}
402
403// Action for validating resolve operations
404class ValidateResolveAction {
405 public:
406 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
407 const char *func_name)
408 : render_pass_(render_pass),
409 subpass_(subpass),
410 context_(context),
411 sync_state_(sync_state),
412 func_name_(func_name),
413 skip_(false) {}
414 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
415 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
416 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
417 HazardResult hazard;
418 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
419 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600420 skip_ |= sync_state_.LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
421 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600422 " to resolve attachment %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -0600423 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name,
John Zulauf37ceaed2020-07-03 16:18:15 -0600424 src_at, dst_at, string_UsageTag(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600425 }
426 }
427 // Providing a mechanism for the constructing caller to get the result of the validation
428 bool GetSkip() const { return skip_; }
429
430 private:
431 VkRenderPass render_pass_;
432 const uint32_t subpass_;
433 const AccessContext &context_;
434 const SyncValidator &sync_state_;
435 const char *func_name_;
436 bool skip_;
437};
438
439// Update action for resolve operations
440class UpdateStateResolveAction {
441 public:
442 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
443 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
444 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
445 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
446 // Ignores validation only arguments...
447 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
448 }
449
450 private:
451 AccessContext &context_;
452 const ResourceUsageTag &tag_;
453};
454
John Zulauf59e25072020-07-17 10:55:21 -0600455void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700456 const SyncStageAccessFlags &prior_, const ResourceUsageTag &tag_) {
John Zulauf59e25072020-07-17 10:55:21 -0600457 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
458 usage_index = usage_index_;
459 hazard = hazard_;
460 prior_access = prior_;
461 tag = tag_;
462}
463
John Zulauf540266b2020-04-06 18:54:53 -0600464AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
465 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600466 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600467 Reset();
468 const auto &subpass_dep = dependencies[subpass];
469 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600470 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600471 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600472 const auto prev_pass = prev_dep.first->pass;
473 const auto &prev_barriers = prev_dep.second;
474 assert(prev_dep.second.size());
475 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
476 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700477 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600478
479 async_.reserve(subpass_dep.async.size());
480 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700481 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600482 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600483 if (subpass_dep.barrier_from_external.size()) {
484 src_external_ = TrackBack(external_context, queue_flags, subpass_dep.barrier_from_external);
John Zulaufe5da6e52020-03-18 15:32:18 -0600485 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600486 if (subpass_dep.barrier_to_external.size()) {
487 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600488 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700489}
490
John Zulauf5f13a792020-03-10 07:31:21 -0600491template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600492HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600493 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600494 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600495 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600496
497 HazardResult hazard;
498 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
499 hazard = detector.Detect(prev);
500 }
501 return hazard;
502}
503
John Zulauf3d84f1b2020-03-09 13:33:25 -0600504// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
505// the DAG of the contexts (for example subpasses)
506template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600507HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
508 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600509 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600510
John Zulauf1a224292020-06-30 14:52:13 -0600511 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600512 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
513 // so we'll check these first
514 for (const auto &async_context : async_) {
515 hazard = async_context->DetectAsyncHazard(type, detector, range);
516 if (hazard.hazard) return hazard;
517 }
John Zulauf5f13a792020-03-10 07:31:21 -0600518 }
519
John Zulauf1a224292020-06-30 14:52:13 -0600520 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600521
John Zulauf69133422020-05-20 14:55:53 -0600522 const auto &accesses = GetAccessStateMap(type);
523 const auto from = accesses.lower_bound(range);
524 const auto to = accesses.upper_bound(range);
525 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600526
John Zulauf69133422020-05-20 14:55:53 -0600527 for (auto pos = from; pos != to; ++pos) {
528 // Cover any leading gap, or gap between entries
529 if (detect_prev) {
530 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
531 // Cover any leading gap, or gap between entries
532 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600533 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600534 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600535 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600536 if (hazard.hazard) return hazard;
537 }
John Zulauf69133422020-05-20 14:55:53 -0600538 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
539 gap.begin = pos->first.end;
540 }
541
542 hazard = detector.Detect(pos);
543 if (hazard.hazard) return hazard;
544 }
545
546 if (detect_prev) {
547 // Detect in the trailing empty as needed
548 gap.end = range.end;
549 if (gap.non_empty()) {
550 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600551 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600552 }
553
554 return hazard;
555}
556
557// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
558template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600559HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600560 auto &accesses = GetAccessStateMap(type);
561 const auto from = accesses.lower_bound(range);
562 const auto to = accesses.upper_bound(range);
563
John Zulauf3d84f1b2020-03-09 13:33:25 -0600564 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600565 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700566 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600567 }
John Zulauf16adfc92020-04-08 10:28:33 -0600568
John Zulauf3d84f1b2020-03-09 13:33:25 -0600569 return hazard;
570}
571
John Zulaufb02c1eb2020-10-06 16:33:36 -0600572struct ApplySubpassTransitionBarriersAction {
573 ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
574 void operator()(ResourceAccessState *access) const {
575 assert(access);
576 access->ApplyBarriers(barriers, true);
577 }
578 const std::vector<SyncBarrier> &barriers;
579};
580
581struct ApplyTrackbackBarriersAction {
582 ApplyTrackbackBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
583 void operator()(ResourceAccessState *access) const {
584 assert(access);
585 assert(!access->HasPendingState());
586 access->ApplyBarriers(barriers, false);
587 access->ApplyPendingBarriers(kCurrentCommandTag);
588 }
589 const std::vector<SyncBarrier> &barriers;
590};
591
592// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
593// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
594// *different* map from dest.
595// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
596// range [first, last)
597template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600598static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
599 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600600 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600601 auto at = entry;
602 for (auto pos = first; pos != last; ++pos) {
603 // Every member of the input iterator range must fit within the remaining portion of entry
604 assert(at->first.includes(pos->first));
605 assert(at != dest->end());
606 // Trim up at to the same size as the entry to resolve
607 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600608 auto access = pos->second; // intentional copy
609 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600610 at->second.Resolve(access);
611 ++at; // Go to the remaining unused section of entry
612 }
613}
614
John Zulaufa0a98292020-09-18 09:30:10 -0600615static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
616 SyncBarrier merged = {};
617 for (const auto &barrier : barriers) {
618 merged.Merge(barrier);
619 }
620 return merged;
621}
622
John Zulaufb02c1eb2020-10-06 16:33:36 -0600623template <typename BarrierAction>
624void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600625 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
626 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600627 if (!range.non_empty()) return;
628
John Zulauf355e49b2020-04-24 15:11:15 -0600629 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
630 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600631 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600632 if (current->pos_B->valid) {
633 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600634 auto access = src_pos->second; // intentional copy
635 barrier_action(&access);
636
John Zulauf16adfc92020-04-08 10:28:33 -0600637 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600638 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
639 trimmed->second.Resolve(access);
640 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600641 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600642 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600643 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600644 }
John Zulauf16adfc92020-04-08 10:28:33 -0600645 } else {
646 // we have to descend to fill this gap
647 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600648 if (current->pos_A->valid) {
649 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
650 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600651 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600652 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -0600653 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600654 // There isn't anything in dest in current)range, so we can accumulate directly into it.
655 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600656 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
657 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
658 barrier_action(&pos->second);
John Zulauf355e49b2020-04-24 15:11:15 -0600659 }
660 }
661 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
662 // iterator of the outer while.
663
664 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
665 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
666 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600667 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
668 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600669 current.seek(seek_to);
670 } else if (!current->pos_A->valid && infill_state) {
671 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
672 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
673 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600674 }
John Zulauf5f13a792020-03-10 07:31:21 -0600675 }
John Zulauf16adfc92020-04-08 10:28:33 -0600676 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600677 }
John Zulauf1a224292020-06-30 14:52:13 -0600678
679 // Infill if range goes passed both the current and resolve map prior contents
680 if (recur_to_infill && (current->range.end < range.end)) {
681 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
682 ResourceAccessRangeMap gap_map;
683 const auto the_end = resolve_map->end();
684 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
685 for (auto &access : gap_map) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600686 barrier_action(&access.second);
John Zulauf1a224292020-06-30 14:52:13 -0600687 resolve_map->insert(the_end, access);
688 }
689 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600690}
691
John Zulauf355e49b2020-04-24 15:11:15 -0600692void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
693 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600694 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600695 if (range.non_empty() && infill_state) {
696 descent_map->insert(std::make_pair(range, *infill_state));
697 }
698 } else {
699 // Look for something to fill the gap further along.
700 for (const auto &prev_dep : prev_) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600701 const ApplyTrackbackBarriersAction barrier_action(prev_dep.barriers);
702 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600703 }
704
John Zulaufe5da6e52020-03-18 15:32:18 -0600705 if (src_external_.context) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600706 const ApplyTrackbackBarriersAction barrier_action(src_external_.barriers);
707 src_external_.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600708 }
709 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600710}
711
John Zulauf16adfc92020-04-08 10:28:33 -0600712AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600713 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600714}
715
John Zulauf16adfc92020-04-08 10:28:33 -0600716
John Zulauf1507ee42020-05-18 11:33:09 -0600717static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
718 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
719 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
720 return stage_access;
721}
722static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
723 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
724 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
725 return stage_access;
726}
727
John Zulauf7635de32020-05-29 17:14:15 -0600728// Caller must manage returned pointer
729static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
730 uint32_t subpass, const VkRect2D &render_area,
731 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
732 auto *proxy = new AccessContext(context);
733 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600734 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600735 return proxy;
736}
737
John Zulaufb02c1eb2020-10-06 16:33:36 -0600738template <typename BarrierAction>
John Zulauf52446eb2020-10-22 16:40:08 -0600739class ResolveAccessRangeFunctor {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600740 public:
741 ResolveAccessRangeFunctor(const AccessContext &context, AccessContext::AddressType address_type,
742 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
743 BarrierAction &barrier_action)
John Zulauf52446eb2020-10-22 16:40:08 -0600744 : context_(context),
745 address_type_(address_type),
746 descent_map_(descent_map),
747 infill_state_(infill_state),
748 barrier_action_(barrier_action) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600749 ResolveAccessRangeFunctor() = delete;
750 void operator()(const ResourceAccessRange &range) const {
751 context_.ResolveAccessRange(address_type_, range, barrier_action_, descent_map_, infill_state_);
752 }
753
754 private:
John Zulauf52446eb2020-10-22 16:40:08 -0600755 const AccessContext &context_;
756 const AccessContext::AddressType address_type_;
757 ResourceAccessRangeMap *const descent_map_;
758 const ResourceAccessState *infill_state_;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600759 BarrierAction &barrier_action_;
760};
761
John Zulaufb02c1eb2020-10-06 16:33:36 -0600762template <typename BarrierAction>
763void AccessContext::ResolveAccessRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range,
764 BarrierAction &barrier_action, AddressType address_type, ResourceAccessRangeMap *descent_map,
765 const ResourceAccessState *infill_state) const {
766 const ResolveAccessRangeFunctor<BarrierAction> action(*this, address_type, descent_map, infill_state, barrier_action);
767 ApplyOverImageRange(image_state, subresource_range, action);
John Zulauf62f10592020-04-03 12:20:02 -0600768}
769
John Zulauf7635de32020-05-29 17:14:15 -0600770// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600771bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600772 const VkRect2D &render_area, uint32_t subpass,
773 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
774 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600775 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600776 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
777 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
778 // those affects have not been recorded yet.
779 //
780 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
781 // to apply and only copy then, if this proves a hot spot.
782 std::unique_ptr<AccessContext> proxy_for_prev;
783 TrackBack proxy_track_back;
784
John Zulauf355e49b2020-04-24 15:11:15 -0600785 const auto &transitions = rp_state.subpass_transitions[subpass];
786 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600787 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
788
789 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
790 if (prev_needs_proxy) {
791 if (!proxy_for_prev) {
792 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
793 render_area, attachment_views));
794 proxy_track_back = *track_back;
795 proxy_track_back.context = proxy_for_prev.get();
796 }
797 track_back = &proxy_track_back;
798 }
799 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600800 if (hazard.hazard) {
John Zulauf389c34b2020-07-28 11:19:35 -0600801 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
802 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
803 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
804 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
805 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
806 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600807 }
808 }
809 return skip;
810}
811
John Zulauf1507ee42020-05-18 11:33:09 -0600812bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600813 const VkRect2D &render_area, uint32_t subpass,
814 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
815 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600816 bool skip = false;
817 const auto *attachment_ci = rp_state.createInfo.pAttachments;
818 VkExtent3D extent = CastTo3D(render_area.extent);
819 VkOffset3D offset = CastTo3D(render_area.offset);
John Zulaufa0a98292020-09-18 09:30:10 -0600820
John Zulauf1507ee42020-05-18 11:33:09 -0600821 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
822 if (subpass == rp_state.attachment_first_subpass[i]) {
823 if (attachment_views[i] == nullptr) continue;
824 const IMAGE_VIEW_STATE &view = *attachment_views[i];
825 const IMAGE_STATE *image = view.image_state.get();
826 if (image == nullptr) continue;
827 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -0600828
829 // Need check in the following way
830 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
831 // vs. transition
832 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
833 // for each aspect loaded.
834
835 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600836 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600837 const bool is_color = !(has_depth || has_stencil);
838
839 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -0600840 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -0600841
John Zulaufaff20662020-06-01 14:07:58 -0600842 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600843 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -0600844
John Zulaufb02c1eb2020-10-06 16:33:36 -0600845 auto hazard_range = view.normalized_subresource_range;
846 bool checked_stencil = false;
847 if (is_color) {
John Zulauf859089b2020-10-29 17:37:03 -0600848 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, kColorAttachmentRasterOrder, offset,
849 extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600850 aspect = "color";
851 } else {
852 if (has_depth) {
853 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf859089b2020-10-29 17:37:03 -0600854 hazard = DetectHazard(*image, load_index, hazard_range, kDepthStencilAttachmentRasterOrder, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600855 aspect = "depth";
856 }
857 if (!hazard.hazard && has_stencil) {
858 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf859089b2020-10-29 17:37:03 -0600859 hazard =
860 DetectHazard(*image, stencil_load_index, hazard_range, kDepthStencilAttachmentRasterOrder, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600861 aspect = "stencil";
862 checked_stencil = true;
863 }
864 }
865
866 if (hazard.hazard) {
867 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
868 if (hazard.tag == kCurrentCommandTag) {
869 // Hazard vs. ILT
870 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
871 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
872 " aspect %s during load with loadOp %s.",
873 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
874 } else {
John Zulauf1507ee42020-05-18 11:33:09 -0600875 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
876 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600877 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -0600878 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600879 string_UsageTag(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -0600880 }
881 }
882 }
883 }
884 return skip;
885}
886
John Zulaufaff20662020-06-01 14:07:58 -0600887// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
888// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
889// store is part of the same Next/End operation.
890// The latter is handled in layout transistion validation directly
891bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
892 const VkRect2D &render_area, uint32_t subpass,
893 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
894 const char *func_name) const {
895 bool skip = false;
896 const auto *attachment_ci = rp_state.createInfo.pAttachments;
897 VkExtent3D extent = CastTo3D(render_area.extent);
898 VkOffset3D offset = CastTo3D(render_area.offset);
899
900 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
901 if (subpass == rp_state.attachment_last_subpass[i]) {
902 if (attachment_views[i] == nullptr) continue;
903 const IMAGE_VIEW_STATE &view = *attachment_views[i];
904 const IMAGE_STATE *image = view.image_state.get();
905 if (image == nullptr) continue;
906 const auto &ci = attachment_ci[i];
907
908 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
909 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
910 // sake, we treat DONT_CARE as writing.
911 const bool has_depth = FormatHasDepth(ci.format);
912 const bool has_stencil = FormatHasStencil(ci.format);
913 const bool is_color = !(has_depth || has_stencil);
914 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
915 if (!has_stencil && !store_op_stores) continue;
916
917 HazardResult hazard;
918 const char *aspect = nullptr;
919 bool checked_stencil = false;
920 if (is_color) {
921 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
922 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
923 aspect = "color";
924 } else {
925 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
926 auto hazard_range = view.normalized_subresource_range;
927 if (has_depth && store_op_stores) {
928 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
929 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
930 kAttachmentRasterOrder, offset, extent);
931 aspect = "depth";
932 }
933 if (!hazard.hazard && has_stencil && stencil_op_stores) {
934 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
935 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
936 kAttachmentRasterOrder, offset, extent);
937 aspect = "stencil";
938 checked_stencil = true;
939 }
940 }
941
942 if (hazard.hazard) {
943 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
944 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -0600945 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
946 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600947 " %s aspect during store with %s %s. Access info %s",
John Zulauf1dae9192020-06-16 15:46:44 -0600948 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600949 store_op_string, string_UsageTag(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -0600950 }
951 }
952 }
953 return skip;
954}
955
John Zulaufb027cdb2020-05-21 14:25:22 -0600956bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
957 const VkRect2D &render_area,
958 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
959 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600960 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
961 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
962 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600963}
964
John Zulauf3d84f1b2020-03-09 13:33:25 -0600965class HazardDetector {
966 SyncStageAccessIndex usage_index_;
967
968 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600969 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700970 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
971 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600972 }
973 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
974};
975
John Zulauf69133422020-05-20 14:55:53 -0600976class HazardDetectorWithOrdering {
977 const SyncStageAccessIndex usage_index_;
978 const SyncOrderingBarrier &ordering_;
979
980 public:
981 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
982 return pos->second.DetectHazard(usage_index_, ordering_);
983 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700984 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
985 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -0600986 }
987 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
988 : usage_index_(usage), ordering_(ordering) {}
989};
990
John Zulauf16adfc92020-04-08 10:28:33 -0600991HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600992 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600993 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600994 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600995}
996
John Zulauf16adfc92020-04-08 10:28:33 -0600997HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600998 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600999 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001000 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -06001001}
1002
John Zulauf69133422020-05-20 14:55:53 -06001003template <typename Detector>
1004HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1005 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1006 const VkExtent3D &extent, DetectOptions options) const {
1007 if (!SimpleBinding(image)) return HazardResult();
1008 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
1009 const auto address_type = ImageAddressType(image);
1010 const auto base_address = ResourceBaseAddress(image);
1011 for (; range_gen->non_empty(); ++range_gen) {
1012 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
1013 if (hazard.hazard) return hazard;
1014 }
1015 return HazardResult();
1016}
1017
John Zulauf540266b2020-04-06 18:54:53 -06001018HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1019 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1020 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001021 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1022 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -06001023 return DetectHazard(image, current_usage, subresource_range, offset, extent);
1024}
1025
1026HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1027 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1028 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -06001029 HazardDetector detector(current_usage);
1030 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
1031}
1032
1033HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1034 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
1035 const VkOffset3D &offset, const VkExtent3D &extent) const {
1036 HazardDetectorWithOrdering detector(current_usage, ordering);
1037 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001038}
1039
John Zulaufb027cdb2020-05-21 14:25:22 -06001040// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
1041// should have reported the issue regarding an invalid attachment entry
1042HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
1043 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
1044 VkImageAspectFlags aspect_mask) const {
1045 if (view != nullptr) {
1046 const IMAGE_STATE *image = view->image_state.get();
1047 if (image != nullptr) {
1048 auto *detect_range = &view->normalized_subresource_range;
1049 VkImageSubresourceRange masked_range;
1050 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1051 masked_range = view->normalized_subresource_range;
1052 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1053 detect_range = &masked_range;
1054 }
1055
1056 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1057 if (detect_range->aspectMask) {
1058 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
1059 }
1060 }
1061 }
1062 return HazardResult();
1063}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001064class BarrierHazardDetector {
1065 public:
1066 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1067 SyncStageAccessFlags src_access_scope)
1068 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1069
John Zulauf5f13a792020-03-10 07:31:21 -06001070 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1071 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001072 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001073 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001074 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001075 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001076 }
1077
1078 private:
1079 SyncStageAccessIndex usage_index_;
1080 VkPipelineStageFlags src_exec_scope_;
1081 SyncStageAccessFlags src_access_scope_;
1082};
1083
John Zulauf16adfc92020-04-08 10:28:33 -06001084HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001085 VkPipelineStageFlags src_exec_scope, const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001086 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001087 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -06001088 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001089}
1090
John Zulauf16adfc92020-04-08 10:28:33 -06001091HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001092 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001093 const VkImageSubresourceRange &subresource_range,
1094 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001095 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1096 VkOffset3D zero_offset = {0, 0, 0};
1097 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001098}
1099
John Zulauf355e49b2020-04-24 15:11:15 -06001100HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001101 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001102 const VkImageMemoryBarrier &barrier) const {
1103 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1104 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1105 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1106}
1107
John Zulauf9cb530d2019-09-30 14:14:10 -06001108template <typename Flags, typename Map>
1109SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1110 SyncStageAccessFlags scope = 0;
1111 for (const auto &bit_scope : map) {
1112 if (flag_mask < bit_scope.first) break;
1113
1114 if (flag_mask & bit_scope.first) {
1115 scope |= bit_scope.second;
1116 }
1117 }
1118 return scope;
1119}
1120
1121SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
1122 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1123}
1124
1125SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
1126 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
1127}
1128
1129// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
1130SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001131 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1132 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1133 // 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 -06001134 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1135}
1136
1137template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001138void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001139 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1140 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -06001141 auto pos = accesses->lower_bound(range);
1142 if (pos == accesses->end() || !pos->first.intersects(range)) {
1143 // The range is empty, fill it with a default value.
1144 pos = action.Infill(accesses, pos, range);
1145 } else if (range.begin < pos->first.begin) {
1146 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001147 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001148 } else if (pos->first.begin < range.begin) {
1149 // Trim the beginning if needed
1150 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1151 ++pos;
1152 }
1153
1154 const auto the_end = accesses->end();
1155 while ((pos != the_end) && pos->first.intersects(range)) {
1156 if (pos->first.end > range.end) {
1157 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1158 }
1159
1160 pos = action(accesses, pos);
1161 if (pos == the_end) break;
1162
1163 auto next = pos;
1164 ++next;
1165 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1166 // Need to infill if next is disjoint
1167 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001168 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001169 next = action.Infill(accesses, next, new_range);
1170 }
1171 pos = next;
1172 }
1173}
1174
1175struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001176 using Iterator = ResourceAccessRangeMap::iterator;
1177 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001178 // this is only called on gaps, and never returns a gap.
1179 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001180 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001181 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001182 }
John Zulauf5f13a792020-03-10 07:31:21 -06001183
John Zulauf5c5e88d2019-12-26 11:22:02 -07001184 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001185 auto &access_state = pos->second;
1186 access_state.Update(usage, tag);
1187 return pos;
1188 }
1189
John Zulauf16adfc92020-04-08 10:28:33 -06001190 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001191 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001192 : type(type_), context(context_), usage(usage_), tag(tag_) {}
1193 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001194 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001195 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001196 const ResourceUsageTag &tag;
1197};
1198
John Zulauf89311b42020-09-29 16:28:47 -06001199// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1200// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1201class ApplyBarrierFunctor {
1202 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001203 using Iterator = ResourceAccessRangeMap::iterator;
1204 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001205
John Zulauf5c5e88d2019-12-26 11:22:02 -07001206 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001207 auto &access_state = pos->second;
John Zulauf89311b42020-09-29 16:28:47 -06001208 access_state.ApplyBarrier(barrier_, layout_transition_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001209 return pos;
1210 }
1211
John Zulauf89311b42020-09-29 16:28:47 -06001212 ApplyBarrierFunctor(const SyncBarrier &barrier, bool layout_transition)
1213 : barrier_(barrier), layout_transition_(layout_transition) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001214
John Zulauf89311b42020-09-29 16:28:47 -06001215 private:
1216 const SyncBarrier barrier_;
1217 const bool layout_transition_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001218};
1219
John Zulauf89311b42020-09-29 16:28:47 -06001220// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1221// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1222// of a collection is known/present.
1223class ApplyBarrierOpsFunctor {
1224 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001225 using Iterator = ResourceAccessRangeMap::iterator;
1226 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001227
John Zulauf89311b42020-09-29 16:28:47 -06001228 struct BarrierOp {
1229 SyncBarrier barrier;
1230 bool layout_transition;
1231 BarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1232 : barrier(barrier_), layout_transition(layout_transition_) {}
1233 BarrierOp() = default;
1234 };
John Zulauf5c5e88d2019-12-26 11:22:02 -07001235 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001236 auto &access_state = pos->second;
John Zulauf89311b42020-09-29 16:28:47 -06001237 for (const auto op : barrier_ops_) {
1238 access_state.ApplyBarrier(op.barrier, op.layout_transition);
1239 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001240
John Zulauf89311b42020-09-29 16:28:47 -06001241 if (resolve_) {
1242 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1243 // another walk
1244 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001245 }
1246 return pos;
1247 }
1248
John Zulauf89311b42020-09-29 16:28:47 -06001249 // A valid tag is required IFF any of the barriers ops are a layout transition, as transitions are write ops
1250 ApplyBarrierOpsFunctor(bool resolve, size_t size_hint, const ResourceUsageTag &tag)
1251 : resolve_(resolve), barrier_ops_(), tag_(tag) {
1252 if (size_hint) {
1253 barrier_ops_.reserve(size_hint);
1254 }
1255 };
1256
1257 // A valid tag is required IFF layout_transition is true, as transitions are write ops
1258 ApplyBarrierOpsFunctor(bool resolve, const std::vector<SyncBarrier> &barriers, bool layout_transition,
1259 const ResourceUsageTag &tag)
John Zulaufb02c1eb2020-10-06 16:33:36 -06001260 : resolve_(resolve), barrier_ops_(), tag_(tag) {
1261 barrier_ops_.reserve(barriers.size());
John Zulauf89311b42020-09-29 16:28:47 -06001262 for (const auto &barrier : barriers) {
1263 barrier_ops_.emplace_back(barrier, layout_transition);
John Zulauf9cb530d2019-09-30 14:14:10 -06001264 }
1265 }
1266
John Zulauf89311b42020-09-29 16:28:47 -06001267 void PushBack(const SyncBarrier &barrier, bool layout_transition) { barrier_ops_.emplace_back(barrier, layout_transition); }
1268
1269 void PushBack(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
1270 barrier_ops_.reserve(barrier_ops_.size() + barriers.size());
1271 for (const auto &barrier : barriers) {
1272 barrier_ops_.emplace_back(barrier, layout_transition);
1273 }
1274 }
1275
1276 private:
1277 bool resolve_;
1278 std::vector<BarrierOp> barrier_ops_;
1279 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001280};
1281
John Zulauf355e49b2020-04-24 15:11:15 -06001282void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1283 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001284 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1285 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001286}
1287
John Zulauf16adfc92020-04-08 10:28:33 -06001288void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001289 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001290 if (!SimpleBinding(buffer)) return;
1291 const auto base_address = ResourceBaseAddress(buffer);
1292 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1293}
John Zulauf355e49b2020-04-24 15:11:15 -06001294
John Zulauf540266b2020-04-06 18:54:53 -06001295void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001296 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001297 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001298 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001299 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001300 const auto address_type = ImageAddressType(image);
1301 const auto base_address = ResourceBaseAddress(image);
1302 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001303 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001304 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001305 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001306}
John Zulauf7635de32020-05-29 17:14:15 -06001307void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1308 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1309 if (view != nullptr) {
1310 const IMAGE_STATE *image = view->image_state.get();
1311 if (image != nullptr) {
1312 auto *update_range = &view->normalized_subresource_range;
1313 VkImageSubresourceRange masked_range;
1314 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1315 masked_range = view->normalized_subresource_range;
1316 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1317 update_range = &masked_range;
1318 }
1319 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1320 }
1321 }
1322}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001323
John Zulauf355e49b2020-04-24 15:11:15 -06001324void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1325 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1326 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001327 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1328 subresource.layerCount};
1329 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1330}
1331
John Zulauf540266b2020-04-06 18:54:53 -06001332template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001333void AccessContext::UpdateResourceAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001334 if (!SimpleBinding(buffer)) return;
1335 const auto base_address = ResourceBaseAddress(buffer);
1336 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001337}
1338
1339template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001340void AccessContext::UpdateResourceAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1341 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001342 if (!SimpleBinding(image)) return;
1343 const auto address_type = ImageAddressType(image);
1344 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001345
locke-lunargae26eac2020-04-16 15:29:05 -06001346 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001347 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001348
John Zulauf16adfc92020-04-08 10:28:33 -06001349 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001350 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001351 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001352 }
1353}
1354
John Zulauf7635de32020-05-29 17:14:15 -06001355void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1356 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1357 const ResourceUsageTag &tag) {
1358 UpdateStateResolveAction update(*this, tag);
1359 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1360}
1361
John Zulaufaff20662020-06-01 14:07:58 -06001362void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1363 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1364 const ResourceUsageTag &tag) {
1365 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1366 VkExtent3D extent = CastTo3D(render_area.extent);
1367 VkOffset3D offset = CastTo3D(render_area.offset);
1368
1369 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1370 if (rp_state.attachment_last_subpass[i] == subpass) {
1371 if (attachment_views[i] == nullptr) continue; // UNUSED
1372 const auto &view = *attachment_views[i];
1373 const IMAGE_STATE *image = view.image_state.get();
1374 if (image == nullptr) continue;
1375
1376 const auto &ci = attachment_ci[i];
1377 const bool has_depth = FormatHasDepth(ci.format);
1378 const bool has_stencil = FormatHasStencil(ci.format);
1379 const bool is_color = !(has_depth || has_stencil);
1380 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1381
1382 if (is_color && store_op_stores) {
1383 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1384 offset, extent, tag);
1385 } else {
1386 auto update_range = view.normalized_subresource_range;
1387 if (has_depth && store_op_stores) {
1388 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1389 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1390 tag);
1391 }
1392 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1393 if (has_stencil && stencil_op_stores) {
1394 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1395 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1396 tag);
1397 }
1398 }
1399 }
1400 }
1401}
1402
John Zulauf540266b2020-04-06 18:54:53 -06001403template <typename Action>
1404void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1405 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001406 for (const auto address_type : kAddressTypes) {
1407 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001408 }
1409}
1410
1411void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001412 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1413 auto &context = contexts[subpass_index];
John Zulaufb02c1eb2020-10-06 16:33:36 -06001414 ApplyTrackbackBarriersAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001415 for (const auto address_type : kAddressTypes) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001416 context.ResolveAccessRange(address_type, full_range, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001417 }
1418 }
1419}
1420
John Zulauf355e49b2020-04-24 15:11:15 -06001421// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001422HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001423 if (!attach_view) return HazardResult();
1424 const auto image_state = attach_view->image_state.get();
1425 if (!image_state) return HazardResult();
1426
John Zulauf355e49b2020-04-24 15:11:15 -06001427 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001428 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001429
1430 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001431 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1432 const auto merged_barrier = MergeBarriers(track_back.barriers);
1433 HazardResult hazard =
1434 track_back.context->DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
1435 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001436 if (!hazard.hazard) {
1437 // The Async hazard check is against the current context's async set.
John Zulaufa0a98292020-09-18 09:30:10 -06001438 hazard = DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001439 attach_view->normalized_subresource_range, kDetectAsync);
1440 }
John Zulaufa0a98292020-09-18 09:30:10 -06001441
John Zulauf355e49b2020-04-24 15:11:15 -06001442 return hazard;
1443}
1444
John Zulaufb02c1eb2020-10-06 16:33:36 -06001445void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
1446 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1447 const ResourceUsageTag &tag) {
1448 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001449 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001450 for (const auto &transition : transitions) {
1451 const auto prev_pass = transition.prev_pass;
1452 const auto attachment_view = attachment_views[transition.attachment];
1453 if (!attachment_view) continue;
1454 const auto *image = attachment_view->image_state.get();
1455 if (!image) continue;
1456 if (!SimpleBinding(*image)) continue;
1457
1458 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1459 assert(trackback);
1460
1461 // Import the attachments into the current context
1462 const auto *prev_context = trackback->context;
1463 assert(prev_context);
1464 const auto address_type = ImageAddressType(*image);
1465 auto &target_map = GetAccessStateMap(address_type);
1466 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
1467 prev_context->ResolveAccessRange(*image, attachment_view->normalized_subresource_range, barrier_action, address_type,
John Zulauf646cc292020-10-23 09:16:45 -06001468 &target_map, &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001469 }
1470
John Zulauf86356ca2020-10-19 11:46:41 -06001471 // If there were no transitions skip this global map walk
1472 if (transitions.size()) {
1473 ApplyBarrierOpsFunctor apply_pending_action(true /* resolve */, 0, tag);
1474 ApplyGlobalBarriers(apply_pending_action);
1475 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001476}
1477
John Zulauf355e49b2020-04-24 15:11:15 -06001478// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1479bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1480
1481 const VkRenderPassBeginInfo *pRenderPassBegin,
1482 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1483 const char *func_name) const {
1484 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1485 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06001486
John Zulauf86356ca2020-10-19 11:46:41 -06001487 assert(pRenderPassBegin);
1488 if (nullptr == pRenderPassBegin) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06001489
John Zulauf86356ca2020-10-19 11:46:41 -06001490 const uint32_t subpass = 0;
John Zulauf355e49b2020-04-24 15:11:15 -06001491
John Zulauf86356ca2020-10-19 11:46:41 -06001492 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
1493 // hasn't happened yet)
1494 const std::vector<AccessContext> empty_context_vector;
1495 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1496 const_cast<AccessContext *>(&cb_access_context_));
John Zulauf355e49b2020-04-24 15:11:15 -06001497
John Zulauf86356ca2020-10-19 11:46:41 -06001498 // Create a view list
1499 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1500 assert(fb_state);
1501 if (nullptr == fb_state) return skip;
1502 // NOTE: Must not use COMMAND_BUFFER_STATE variant of this as RecordCmdBeginRenderPass hasn't run and thus
1503 // the activeRenderPass.* fields haven't been set.
1504 const auto views = sync_state_->GetAttachmentViews(*pRenderPassBegin, *fb_state);
1505
1506 // Validate transitions
1507 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
1508
1509 // Validate load operations if there were no layout transition hazards
1510 if (!skip) {
1511 temp_context.RecordLayoutTransitions(rp_state, subpass, views, kCurrentCommandTag);
1512 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001513 }
John Zulauf86356ca2020-10-19 11:46:41 -06001514
John Zulauf355e49b2020-04-24 15:11:15 -06001515 return skip;
1516}
1517
locke-lunarg61870c22020-06-09 14:51:50 -06001518bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1519 const char *func_name) const {
1520 bool skip = false;
1521 const PIPELINE_STATE *pPipe = nullptr;
1522 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1523 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1524 if (!pPipe || !per_sets) {
1525 return skip;
1526 }
1527
1528 using DescriptorClass = cvdescriptorset::DescriptorClass;
1529 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1530 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1531 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1532 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1533
1534 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarg37047832020-06-12 13:44:45 -06001535 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001536 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1537 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001538 for (const auto &set_binding : stage_state.descriptor_uses) {
1539 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1540 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1541 set_binding.first.second);
1542 const auto descriptor_type = binding_it.GetType();
1543 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1544 auto array_idx = 0;
1545
1546 if (binding_it.IsVariableDescriptorCount()) {
1547 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1548 }
1549 SyncStageAccessIndex sync_index =
1550 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1551
1552 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1553 uint32_t index = i - index_range.start;
1554 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1555 switch (descriptor->GetClass()) {
1556 case DescriptorClass::ImageSampler:
1557 case DescriptorClass::Image: {
1558 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001559 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001560 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001561 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1562 img_view_state = image_sampler_descriptor->GetImageViewState();
1563 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001564 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001565 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1566 img_view_state = image_descriptor->GetImageViewState();
1567 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001568 }
1569 if (!img_view_state) continue;
1570 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1571 VkExtent3D extent = {};
1572 VkOffset3D offset = {};
1573 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1574 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1575 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1576 } else {
1577 extent = img_state->createInfo.extent;
1578 }
John Zulauf361fb532020-07-22 10:45:39 -06001579 HazardResult hazard;
1580 const auto &subresource_range = img_view_state->normalized_subresource_range;
1581 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1582 // Input attachments are subject to raster ordering rules
1583 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
1584 kAttachmentRasterOrder, offset, extent);
1585 } else {
1586 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1587 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001588 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001589 skip |= sync_state_->LogError(
1590 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001591 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1592 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001593 func_name, string_SyncHazard(hazard.hazard),
1594 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1595 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1596 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001597 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1598 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1599 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001600 }
1601 break;
1602 }
1603 case DescriptorClass::TexelBuffer: {
1604 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1605 if (!buf_view_state) continue;
1606 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001607 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001608 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001609 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001610 skip |= sync_state_->LogError(
1611 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001612 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1613 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001614 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1615 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1616 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001617 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1618 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1619 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001620 }
1621 break;
1622 }
1623 case DescriptorClass::GeneralBuffer: {
1624 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1625 auto buf_state = buffer_descriptor->GetBufferState();
1626 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001627 const ResourceAccessRange range =
1628 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001629 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001630 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001631 skip |= sync_state_->LogError(
1632 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001633 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1634 func_name, string_SyncHazard(hazard.hazard),
1635 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001636 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1637 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001638 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1639 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1640 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001641 }
1642 break;
1643 }
1644 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1645 default:
1646 break;
1647 }
1648 }
1649 }
1650 }
1651 return skip;
1652}
1653
1654void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1655 const ResourceUsageTag &tag) {
1656 const PIPELINE_STATE *pPipe = nullptr;
1657 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1658 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1659 if (!pPipe || !per_sets) {
1660 return;
1661 }
1662
1663 using DescriptorClass = cvdescriptorset::DescriptorClass;
1664 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1665 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1666 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1667 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1668
1669 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001670 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
1671 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1672 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001673 for (const auto &set_binding : stage_state.descriptor_uses) {
1674 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1675 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1676 set_binding.first.second);
1677 const auto descriptor_type = binding_it.GetType();
1678 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1679 auto array_idx = 0;
1680
1681 if (binding_it.IsVariableDescriptorCount()) {
1682 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1683 }
1684 SyncStageAccessIndex sync_index =
1685 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1686
1687 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1688 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1689 switch (descriptor->GetClass()) {
1690 case DescriptorClass::ImageSampler:
1691 case DescriptorClass::Image: {
1692 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1693 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1694 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1695 } else {
1696 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1697 }
1698 if (!img_view_state) continue;
1699 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1700 VkExtent3D extent = {};
1701 VkOffset3D offset = {};
1702 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1703 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1704 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1705 } else {
1706 extent = img_state->createInfo.extent;
1707 }
1708 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1709 offset, extent, tag);
1710 break;
1711 }
1712 case DescriptorClass::TexelBuffer: {
1713 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1714 if (!buf_view_state) continue;
1715 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001716 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001717 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1718 break;
1719 }
1720 case DescriptorClass::GeneralBuffer: {
1721 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1722 auto buf_state = buffer_descriptor->GetBufferState();
1723 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001724 const ResourceAccessRange range =
1725 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001726 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1727 break;
1728 }
1729 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1730 default:
1731 break;
1732 }
1733 }
1734 }
1735 }
1736}
1737
1738bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1739 bool skip = false;
1740 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1741 if (!pPipe) {
1742 return skip;
1743 }
1744
1745 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1746 const auto &binding_buffers_size = binding_buffers.size();
1747 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1748
1749 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1750 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1751 if (binding_description.binding < binding_buffers_size) {
1752 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07001753 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001754
locke-lunarg1ae57d62020-11-18 10:49:19 -07001755 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001756 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1757 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001758 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1759 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001760 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001761 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 -06001762 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001763 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001764 }
1765 }
1766 }
1767 return skip;
1768}
1769
1770void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
1771 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1772 if (!pPipe) {
1773 return;
1774 }
1775 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1776 const auto &binding_buffers_size = binding_buffers.size();
1777 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1778
1779 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1780 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1781 if (binding_description.binding < binding_buffers_size) {
1782 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07001783 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001784
locke-lunarg1ae57d62020-11-18 10:49:19 -07001785 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001786 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1787 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001788 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1789 }
1790 }
1791}
1792
1793bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1794 bool skip = false;
locke-lunarg1ae57d62020-11-18 10:49:19 -07001795 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed)
1796 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001797
locke-lunarg1ae57d62020-11-18 10:49:19 -07001798 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001799 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001800 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1801 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001802 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1803 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001804 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001805 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 -06001806 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001807 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001808 }
1809
1810 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1811 // We will detect more accurate range in the future.
1812 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1813 return skip;
1814}
1815
1816void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07001817 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) return;
locke-lunarg61870c22020-06-09 14:51:50 -06001818
locke-lunarg1ae57d62020-11-18 10:49:19 -07001819 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001820 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001821 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1822 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001823 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1824
1825 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1826 // We will detect more accurate range in the future.
1827 RecordDrawVertex(UINT32_MAX, 0, tag);
1828}
1829
1830bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06001831 bool skip = false;
1832 if (!current_renderpass_context_) return skip;
1833 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1834 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1835 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001836}
1837
1838void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
locke-lunarg7077d502020-06-18 21:37:26 -06001839 if (current_renderpass_context_)
1840 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1841 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001842}
1843
John Zulauf355e49b2020-04-24 15:11:15 -06001844bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001845 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001846 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06001847 skip |=
1848 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001849
1850 return skip;
1851}
1852
1853bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1854 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001855 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001856 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001857 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06001858 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1859 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001860
1861 return skip;
1862}
1863
1864void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1865 assert(sync_state_);
1866 if (!cb_state_) return;
1867
1868 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06001869 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06001870 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06001871 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001872 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001873}
1874
John Zulauf355e49b2020-04-24 15:11:15 -06001875void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001876 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001877 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001878 current_context_ = &current_renderpass_context_->CurrentContext();
1879}
1880
John Zulauf355e49b2020-04-24 15:11:15 -06001881void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001882 assert(current_renderpass_context_);
1883 if (!current_renderpass_context_) return;
1884
John Zulauf1a224292020-06-30 14:52:13 -06001885 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001886 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001887 current_renderpass_context_ = nullptr;
1888}
1889
locke-lunarg61870c22020-06-09 14:51:50 -06001890bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1891 const VkRect2D &render_area, const char *func_name) const {
1892 bool skip = false;
locke-lunarg96dc9632020-06-10 17:22:18 -06001893 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001894 if (!pPipe ||
1895 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001896 return skip;
1897 }
1898 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001899 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1900 VkExtent3D extent = CastTo3D(render_area.extent);
1901 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001902
John Zulauf1a224292020-06-30 14:52:13 -06001903 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001904 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001905 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1906 for (const auto location : list) {
1907 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1908 continue;
1909 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001910 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1911 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06001912 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001913 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001914 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001915 func_name, string_SyncHazard(hazard.hazard),
1916 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1917 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001918 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001919 }
1920 }
1921 }
locke-lunarg37047832020-06-12 13:44:45 -06001922
1923 // PHASE1 TODO: Add layout based read/vs. write selection.
1924 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1925 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1926 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001927 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001928 bool depth_write = false, stencil_write = false;
1929
1930 // PHASE1 TODO: These validation should be in core_checks.
1931 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1932 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1933 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1934 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1935 depth_write = true;
1936 }
1937 // PHASE1 TODO: It needs to check if stencil is writable.
1938 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1939 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1940 // PHASE1 TODO: These validation should be in core_checks.
1941 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1942 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1943 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1944 stencil_write = true;
1945 }
1946
1947 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1948 if (depth_write) {
1949 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001950 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1951 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001952 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001953 skip |= sync_state.LogError(
1954 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001955 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001956 func_name, string_SyncHazard(hazard.hazard),
1957 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1958 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001959 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001960 }
1961 }
1962 if (stencil_write) {
1963 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001964 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1965 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001966 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001967 skip |= sync_state.LogError(
1968 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001969 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001970 func_name, string_SyncHazard(hazard.hazard),
1971 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1972 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001973 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001974 }
locke-lunarg61870c22020-06-09 14:51:50 -06001975 }
1976 }
1977 return skip;
1978}
1979
locke-lunarg96dc9632020-06-10 17:22:18 -06001980void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1981 const ResourceUsageTag &tag) {
1982 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001983 if (!pPipe ||
1984 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001985 return;
1986 }
1987 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001988 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1989 VkExtent3D extent = CastTo3D(render_area.extent);
1990 VkOffset3D offset = CastTo3D(render_area.offset);
1991
John Zulauf1a224292020-06-30 14:52:13 -06001992 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001993 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001994 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1995 for (const auto location : list) {
1996 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1997 continue;
1998 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001999 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
2000 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002001 }
2002 }
locke-lunarg37047832020-06-12 13:44:45 -06002003
2004 // PHASE1 TODO: Add layout based read/vs. write selection.
2005 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
2006 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
2007 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002008 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002009 bool depth_write = false, stencil_write = false;
2010
2011 // PHASE1 TODO: These validation should be in core_checks.
2012 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
2013 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2014 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
2015 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2016 depth_write = true;
2017 }
2018 // PHASE1 TODO: It needs to check if stencil is writable.
2019 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2020 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2021 // PHASE1 TODO: These validation should be in core_checks.
2022 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
2023 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
2024 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2025 stencil_write = true;
2026 }
2027
2028 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2029 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002030 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2031 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002032 }
2033 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002034 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2035 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002036 }
locke-lunarg61870c22020-06-09 14:51:50 -06002037 }
2038}
2039
John Zulauf1507ee42020-05-18 11:33:09 -06002040bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
2041 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002042 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002043 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06002044 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2045 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002046 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2047 func_name);
2048
John Zulauf355e49b2020-04-24 15:11:15 -06002049 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002050 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06002051 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002052 if (!skip) {
2053 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2054 // on a copy of the (empty) next context.
2055 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2056 AccessContext temp_context(next_context);
2057 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
2058 skip |= temp_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
2059 }
John Zulauf7635de32020-05-29 17:14:15 -06002060 return skip;
2061}
2062bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
2063 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002064 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002065 bool skip = false;
2066 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2067 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002068 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2069 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06002070 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002071 return skip;
2072}
2073
John Zulauf7635de32020-05-29 17:14:15 -06002074AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2075 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2076}
2077
2078bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2079 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002080 bool skip = false;
2081
John Zulauf7635de32020-05-29 17:14:15 -06002082 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2083 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2084 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2085 // to apply and only copy then, if this proves a hot spot.
2086 std::unique_ptr<AccessContext> proxy_for_current;
2087
John Zulauf355e49b2020-04-24 15:11:15 -06002088 // Validate the "finalLayout" transitions to external
2089 // Get them from where there we're hidding in the extra entry.
2090 const auto &final_transitions = rp_state_->subpass_transitions.back();
2091 for (const auto &transition : final_transitions) {
2092 const auto &attach_view = attachment_views_[transition.attachment];
2093 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2094 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002095 auto *context = trackback.context;
2096
2097 if (transition.prev_pass == current_subpass_) {
2098 if (!proxy_for_current) {
2099 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2100 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2101 }
2102 context = proxy_for_current.get();
2103 }
2104
John Zulaufa0a98292020-09-18 09:30:10 -06002105 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2106 const auto merged_barrier = MergeBarriers(trackback.barriers);
2107 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope,
2108 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2109 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002110 if (hazard.hazard) {
2111 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2112 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf389c34b2020-07-28 11:19:35 -06002113 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002114 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf389c34b2020-07-28 11:19:35 -06002115 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf37ceaed2020-07-03 16:18:15 -06002116 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002117 }
2118 }
2119 return skip;
2120}
2121
2122void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2123 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002124 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002125}
2126
John Zulauf1507ee42020-05-18 11:33:09 -06002127void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2128 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2129 auto &subpass_context = subpass_contexts_[current_subpass_];
2130 VkExtent3D extent = CastTo3D(render_area.extent);
2131 VkOffset3D offset = CastTo3D(render_area.offset);
2132
2133 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2134 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2135 if (attachment_views_[i] == nullptr) continue; // UNUSED
2136 const auto &view = *attachment_views_[i];
2137 const IMAGE_STATE *image = view.image_state.get();
2138 if (image == nullptr) continue;
2139
2140 const auto &ci = attachment_ci[i];
2141 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002142 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002143 const bool is_color = !(has_depth || has_stencil);
2144
2145 if (is_color) {
2146 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2147 extent, tag);
2148 } else {
2149 auto update_range = view.normalized_subresource_range;
2150 if (has_depth) {
2151 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2152 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2153 }
2154 if (has_stencil) {
2155 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2156 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2157 tag);
2158 }
2159 }
2160 }
2161 }
2162}
2163
John Zulauf355e49b2020-04-24 15:11:15 -06002164void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002165 const AccessContext *external_context, VkQueueFlags queue_flags,
2166 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002167 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002168 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002169 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2170 // Add this for all subpasses here so that they exsist during next subpass validation
2171 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002172 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002173 }
2174 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2175
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002176 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002177 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002178 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002179}
John Zulauf1507ee42020-05-18 11:33:09 -06002180
2181void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002182 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2183 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002184 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002185
John Zulauf355e49b2020-04-24 15:11:15 -06002186 current_subpass_++;
2187 assert(current_subpass_ < subpass_contexts_.size());
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002188 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002189 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002190 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002191}
2192
John Zulauf1a224292020-06-30 14:52:13 -06002193void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2194 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002195 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002196 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002197 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002198
John Zulauf355e49b2020-04-24 15:11:15 -06002199 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002200 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002201
2202 // Add the "finalLayout" transitions to external
2203 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002204 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2205 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2206 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002207 const auto &final_transitions = rp_state_->subpass_transitions.back();
2208 for (const auto &transition : final_transitions) {
2209 const auto &attachment = attachment_views_[transition.attachment];
2210 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002211 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf89311b42020-09-29 16:28:47 -06002212 ApplyBarrierOpsFunctor barrier_ops(true /* resolve */, last_trackback.barriers, true /* layout transition */, tag);
2213 external_context->UpdateResourceAccess(*attachment->image_state, attachment->normalized_subresource_range, barrier_ops);
John Zulauf355e49b2020-04-24 15:11:15 -06002214 }
2215}
2216
John Zulauf3d84f1b2020-03-09 13:33:25 -06002217SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2218 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2219 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2220 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2221 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2222 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2223 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2224}
2225
John Zulaufb02c1eb2020-10-06 16:33:36 -06002226// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2227void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2228 for (const auto &barrier : barriers) {
2229 ApplyBarrier(barrier, layout_transition);
2230 }
2231}
2232
John Zulauf89311b42020-09-29 16:28:47 -06002233// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2234// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2235// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002236void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2237 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002238 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002239 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002240 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002241 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002242 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002243 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002244}
John Zulauf9cb530d2019-09-30 14:14:10 -06002245HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2246 HazardResult hazard;
2247 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002248 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002249 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002250 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002251 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002252 }
2253 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002254 // Write operation:
2255 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2256 // If reads exists -- test only against them because either:
2257 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2258 // * 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
2259 // the current write happens after the reads, so just test the write against the reades
2260 // Otherwise test against last_write
2261 //
2262 // Look for casus belli for WAR
2263 if (last_read_count) {
2264 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2265 const auto &read_access = last_reads[read_index];
2266 if (IsReadHazard(usage_stage, read_access)) {
2267 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2268 break;
2269 }
2270 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002271 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002272 // Write-After-Write check -- if we have a previous write to test against
2273 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002274 }
2275 }
2276 return hazard;
2277}
2278
John Zulauf69133422020-05-20 14:55:53 -06002279HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2280 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2281 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002282 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002283 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002284 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2285 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002286 if (IsRead(usage_bit)) {
2287 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2288 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2289 if (is_raw_hazard) {
2290 // NOTE: we know last_write is non-zero
2291 // See if the ordering rules save us from the simple RAW check above
2292 // First check to see if the current usage is covered by the ordering rules
2293 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2294 const bool usage_is_ordered =
2295 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2296 if (usage_is_ordered) {
2297 // Now see of the most recent write (or a subsequent read) are ordered
2298 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2299 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002300 }
2301 }
John Zulauf4285ee92020-09-23 10:20:52 -06002302 if (is_raw_hazard) {
2303 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2304 }
John Zulauf361fb532020-07-22 10:45:39 -06002305 } else {
2306 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002307 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulauf361fb532020-07-22 10:45:39 -06002308 if (last_read_count) {
John Zulauf361fb532020-07-22 10:45:39 -06002309 // Look for any WAR hazards outside the ordered set of stages
John Zulauf4285ee92020-09-23 10:20:52 -06002310 VkPipelineStageFlags ordered_stages = 0;
2311 if (usage_write_is_ordered) {
2312 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2313 ordered_stages = GetOrderedStages(ordering);
2314 }
2315 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2316 if ((ordered_stages & last_read_stages) != last_read_stages) {
2317 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2318 const auto &read_access = last_reads[read_index];
2319 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2320 if (IsReadHazard(usage_stage, read_access)) {
2321 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2322 break;
2323 }
John Zulaufd14743a2020-07-03 09:42:39 -06002324 }
2325 }
John Zulauf4285ee92020-09-23 10:20:52 -06002326 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002327 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002328 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002329 }
John Zulauf69133422020-05-20 14:55:53 -06002330 }
2331 }
2332 return hazard;
2333}
2334
John Zulauf2f952d22020-02-10 11:34:51 -07002335// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002336HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002337 HazardResult hazard;
2338 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002339 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2340 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2341 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002342 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002343 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002344 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002345 }
2346 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002347 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002348 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002349 } else if (last_read_count > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002350 // Any reads during the other subpass will conflict with this write, so we need to check them all.
2351 for (uint32_t i = 0; i < last_read_count; i++) {
2352 if (last_reads[i].tag.index >= start_tag.index) {
2353 hazard.Set(this, usage_index, WRITE_RACING_READ, last_reads[i].access, last_reads[i].tag);
2354 break;
2355 }
2356 }
John Zulauf2f952d22020-02-10 11:34:51 -07002357 }
2358 }
2359 return hazard;
2360}
2361
John Zulauf36bcf6a2020-02-03 15:12:52 -07002362HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002363 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002364 // Only supporting image layout transitions for now
2365 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2366 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002367 // only test for WAW if there no intervening read operations.
2368 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2369 if (last_read_count) {
John Zulauf355e49b2020-04-24 15:11:15 -06002370 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002371 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002372 const auto &read_access = last_reads[read_index];
2373 // If the read stage is not in the src sync sync
2374 // *AND* not execution chained with an existing sync barrier (that's the or)
2375 // then the barrier access is unsafe (R/W after R)
2376 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002377 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002378 break;
2379 }
2380 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002381 } else if (last_write.any()) {
John Zulauf361fb532020-07-22 10:45:39 -06002382 // If the previous write is *not* in the 1st access scope
2383 // *AND* the current barrier is not in the dependency chain
2384 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2385 // then the barrier access is unsafe (R/W after W)
2386 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
2387 // TODO: Do we need a difference hazard name for this?
2388 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2389 }
John Zulaufd14743a2020-07-03 09:42:39 -06002390 }
John Zulauf361fb532020-07-22 10:45:39 -06002391
John Zulauf0cb5be22020-01-23 12:18:22 -07002392 return hazard;
2393}
2394
John Zulauf5f13a792020-03-10 07:31:21 -06002395// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2396// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2397// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2398void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2399 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002400 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2401 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002402 *this = other;
2403 } else if (!other.write_tag.IsBefore(write_tag)) {
2404 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2405 // dependency chaining logic or any stage expansion)
2406 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002407 pending_write_barriers |= other.pending_write_barriers;
2408 pending_layout_transition |= other.pending_layout_transition;
2409 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002410
John Zulaufd14743a2020-07-03 09:42:39 -06002411 // Merge the read states
John Zulauf4285ee92020-09-23 10:20:52 -06002412 const auto pre_merge_count = last_read_count;
2413 const auto pre_merge_stages = last_read_stages;
John Zulauf5f13a792020-03-10 07:31:21 -06002414 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2415 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002416 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002417 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002418 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2419 // but we should wait on profiling data for that.
2420 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002421 auto &my_read = last_reads[my_read_index];
2422 if (other_read.stage == my_read.stage) {
2423 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002424 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002425 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002426 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002427 my_read.pending_dep_chain = other_read.pending_dep_chain;
2428 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
2429 // May require tracking more than one access per stage.
2430 my_read.barriers = other_read.barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002431 if (my_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
2432 // Since I'm overwriting the fragement stage read, also update the input attachment info
2433 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06002434 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002435 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002436 } else if (other_read.tag.IsBefore(my_read.tag)) {
2437 // The read tags match so merge the barriers
2438 my_read.barriers |= other_read.barriers;
2439 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002440 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002441
John Zulauf5f13a792020-03-10 07:31:21 -06002442 break;
2443 }
2444 }
2445 } else {
2446 // The other read stage doesn't exist in this, so add it.
2447 last_reads[last_read_count] = other_read;
2448 last_read_count++;
2449 last_read_stages |= other_read.stage;
John Zulauf4285ee92020-09-23 10:20:52 -06002450 if (other_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06002451 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002452 }
John Zulauf5f13a792020-03-10 07:31:21 -06002453 }
2454 }
John Zulauf361fb532020-07-22 10:45:39 -06002455 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002456 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2457 // ignore it.
John Zulauf5f13a792020-03-10 07:31:21 -06002458}
2459
John Zulauf9cb530d2019-09-30 14:14:10 -06002460void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2461 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2462 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002463 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002464 // Mulitple outstanding reads may be of interest and do dependency chains independently
2465 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2466 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002467 uint32_t update_index = kStageCount;
John Zulauf9cb530d2019-09-30 14:14:10 -06002468 if (usage_stage & last_read_stages) {
2469 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf4285ee92020-09-23 10:20:52 -06002470 if (last_reads[read_index].stage == usage_stage) {
2471 update_index = read_index;
John Zulauf9cb530d2019-09-30 14:14:10 -06002472 break;
2473 }
2474 }
John Zulauf4285ee92020-09-23 10:20:52 -06002475 assert(update_index < last_read_count);
John Zulauf9cb530d2019-09-30 14:14:10 -06002476 } else {
John Zulauf9cb530d2019-09-30 14:14:10 -06002477 assert(last_read_count < last_reads.size());
John Zulauf4285ee92020-09-23 10:20:52 -06002478 update_index = last_read_count++;
John Zulauf9cb530d2019-09-30 14:14:10 -06002479 last_read_stages |= usage_stage;
2480 }
John Zulauf4285ee92020-09-23 10:20:52 -06002481 last_reads[update_index].Set(usage_stage, usage_bit, 0, tag);
2482
2483 // Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
2484 if (usage_stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06002485 // TODO Revisit re: multiple reads for a given stage
2486 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002487 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002488 } else {
2489 // Assume write
2490 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002491 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002492 }
2493}
John Zulauf5f13a792020-03-10 07:31:21 -06002494
John Zulauf89311b42020-09-29 16:28:47 -06002495// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2496// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2497// We can overwrite them as *this* write is now after them.
2498//
2499// Note: intentionally ignore pending barriers and chains (i.e. don't apply or clear them), let ApplyPendingBarriers handle them.
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002500void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulauf89311b42020-09-29 16:28:47 -06002501 last_read_count = 0;
2502 last_read_stages = 0;
2503 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06002504 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06002505
2506 write_barriers = 0;
2507 write_dependency_chain = 0;
2508 write_tag = tag;
2509 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002510}
2511
John Zulauf89311b42020-09-29 16:28:47 -06002512// Apply the memory barrier without updating the existing barriers. The execution barrier
2513// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2514// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2515// replace the current write barriers or add to them, so accumulate to pending as well.
2516void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2517 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2518 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06002519 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
2520 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
2521 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
2522 // transistion *as* a write and in scope with the barrier (it's before visibility).
2523 if (layout_transition || InSourceScopeOrChain(barrier.src_exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06002524 pending_write_barriers |= barrier.dst_access_scope;
2525 pending_write_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002526 }
John Zulauf89311b42020-09-29 16:28:47 -06002527 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2528 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06002529
John Zulauf89311b42020-09-29 16:28:47 -06002530 if (!pending_layout_transition) {
2531 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2532 // don't need to be tracked as we're just going to zero them.
John Zulaufa0a98292020-09-18 09:30:10 -06002533 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf89311b42020-09-29 16:28:47 -06002534 ReadState &access = last_reads[read_index];
2535 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2536 if (barrier.src_exec_scope & (access.stage | access.barriers)) {
2537 access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002538 }
2539 }
John Zulaufa0a98292020-09-18 09:30:10 -06002540 }
John Zulaufa0a98292020-09-18 09:30:10 -06002541}
2542
John Zulauf89311b42020-09-29 16:28:47 -06002543void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
2544 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06002545 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
2546 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
2547 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06002548 }
John Zulauf89311b42020-09-29 16:28:47 -06002549
2550 // Apply the accumulate execution barriers (and thus update chaining information)
2551 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
2552 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2553 ReadState &access = last_reads[read_index];
2554 access.barriers |= access.pending_dep_chain;
2555 read_execution_barriers |= access.barriers;
2556 access.pending_dep_chain = 0;
2557 }
2558
2559 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
2560 write_dependency_chain |= pending_write_dep_chain;
2561 write_barriers |= pending_write_barriers;
2562 pending_write_dep_chain = 0;
2563 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002564}
2565
John Zulauf59e25072020-07-17 10:55:21 -06002566// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002567VkPipelineStageFlags ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
John Zulauf59e25072020-07-17 10:55:21 -06002568 VkPipelineStageFlags barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06002569
2570 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2571 const auto &read_access = last_reads[read_index];
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002572 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06002573 barriers = read_access.barriers;
2574 break;
John Zulauf59e25072020-07-17 10:55:21 -06002575 }
2576 }
John Zulauf4285ee92020-09-23 10:20:52 -06002577
John Zulauf59e25072020-07-17 10:55:21 -06002578 return barriers;
2579}
2580
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002581inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlagBits usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06002582 assert(IsRead(usage));
2583 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
2584 // * the previous reads are not hazards, and thus last_write must be visible and available to
2585 // any reads that happen after.
2586 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2587 // the current read will be also not be a hazard, thus reporting a hazard here adds no needed information.
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002588 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06002589}
2590
John Zulauf4285ee92020-09-23 10:20:52 -06002591VkPipelineStageFlags ResourceAccessState::GetOrderedStages(const SyncOrderingBarrier &ordering) const {
2592 // Whether the stage are in the ordering scope only matters if the current write is ordered
2593 VkPipelineStageFlags ordered_stages = last_read_stages & ordering.exec_scope;
2594 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002595 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06002596 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06002597 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
2598 ordered_stages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
2599 }
2600
2601 return ordered_stages;
2602}
2603
2604inline ResourceAccessState::ReadState *ResourceAccessState::GetReadStateForStage(VkPipelineStageFlagBits stage,
2605 uint32_t search_limit) {
2606 ReadState *read_state = nullptr;
2607 search_limit = std::min(search_limit, last_read_count);
2608 for (uint32_t i = 0; i < search_limit; i++) {
2609 if (last_reads[i].stage == stage) {
2610 read_state = &last_reads[i];
2611 break;
2612 }
2613 }
2614 return read_state;
2615}
2616
John Zulaufd1f85d42020-04-15 12:23:15 -06002617void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002618 auto *access_context = GetAccessContextNoInsert(command_buffer);
2619 if (access_context) {
2620 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002621 }
2622}
2623
John Zulaufd1f85d42020-04-15 12:23:15 -06002624void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2625 auto access_found = cb_access_state.find(command_buffer);
2626 if (access_found != cb_access_state.end()) {
2627 access_found->second->Reset();
2628 cb_access_state.erase(access_found);
2629 }
2630}
2631
John Zulauf89311b42020-09-29 16:28:47 -06002632void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2633 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags src_access_scope,
2634 SyncStageAccessFlags dst_access_scope, uint32_t memory_barrier_count,
2635 const VkMemoryBarrier *pMemoryBarriers, const ResourceUsageTag &tag) {
2636 ApplyBarrierOpsFunctor barriers_functor(true /* resolve */, std::min<uint32_t>(1, memory_barrier_count), tag);
2637 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
2638 const auto &barrier = pMemoryBarriers[barrier_index];
2639 SyncBarrier sync_barrier(src_exec_scope, SyncStageAccess::AccessScope(src_access_scope, barrier.srcAccessMask),
2640 dst_exec_scope, SyncStageAccess::AccessScope(dst_access_scope, barrier.dstAccessMask));
2641 barriers_functor.PushBack(sync_barrier, false);
2642 }
2643 if (0 == memory_barrier_count) {
2644 // If there are no global memory barriers, force an exec barrier
2645 barriers_functor.PushBack(SyncBarrier(src_exec_scope, 0, dst_exec_scope, 0), false);
2646 }
John Zulauf540266b2020-04-06 18:54:53 -06002647 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002648}
2649
John Zulauf540266b2020-04-06 18:54:53 -06002650void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002651 const SyncStageAccessFlags &src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2652 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002653 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002654 for (uint32_t index = 0; index < barrier_count; index++) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002655 auto barrier = barriers[index]; // barrier is a copy
John Zulauf9cb530d2019-09-30 14:14:10 -06002656 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2657 if (!buffer) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002658 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
2659 const ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002660 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2661 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06002662 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2663 const ApplyBarrierFunctor update_action(sync_barrier, false /* layout_transition */);
2664 context->UpdateResourceAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002665 }
2666}
2667
John Zulauf540266b2020-04-06 18:54:53 -06002668void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002669 const SyncStageAccessFlags &src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2670 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002671 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002672 for (uint32_t index = 0; index < barrier_count; index++) {
2673 const auto &barrier = barriers[index];
2674 const auto *image = Get<IMAGE_STATE>(barrier.image);
2675 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002676 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002677 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2678 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2679 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06002680 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2681 const ApplyBarrierFunctor barrier_action(sync_barrier, layout_transition);
2682 context->UpdateResourceAccess(*image, subresource_range, barrier_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002683 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002684}
2685
2686bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2687 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2688 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002689 const auto *cb_context = GetAccessContext(commandBuffer);
2690 assert(cb_context);
2691 if (!cb_context) return skip;
2692 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002693
John Zulauf3d84f1b2020-03-09 13:33:25 -06002694 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002695 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002696 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002697
2698 for (uint32_t region = 0; region < regionCount; region++) {
2699 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002700 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002701 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002702 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002703 if (hazard.hazard) {
2704 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002705 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002706 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002707 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002708 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002709 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002710 }
John Zulauf16adfc92020-04-08 10:28:33 -06002711 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002712 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06002713 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002714 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002715 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002716 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002717 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002718 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002719 }
2720 }
2721 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002722 }
2723 return skip;
2724}
2725
2726void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2727 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002728 auto *cb_context = GetAccessContext(commandBuffer);
2729 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002730 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002731 auto *context = cb_context->GetCurrentAccessContext();
2732
John Zulauf9cb530d2019-09-30 14:14:10 -06002733 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002734 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002735
2736 for (uint32_t region = 0; region < regionCount; region++) {
2737 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002738 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002739 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002740 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002741 }
John Zulauf16adfc92020-04-08 10:28:33 -06002742 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002743 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002744 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002745 }
2746 }
2747}
2748
Jeff Leger178b1e52020-10-05 12:22:23 -04002749bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
2750 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
2751 bool skip = false;
2752 const auto *cb_context = GetAccessContext(commandBuffer);
2753 assert(cb_context);
2754 if (!cb_context) return skip;
2755 const auto *context = cb_context->GetCurrentAccessContext();
2756
2757 // If we have no previous accesses, we have no hazards
2758 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2759 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2760
2761 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2762 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2763 if (src_buffer) {
2764 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2765 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
2766 if (hazard.hazard) {
2767 // TODO -- add tag information to log msg when useful.
2768 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
2769 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
2770 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
2771 region, string_UsageTag(hazard).c_str());
2772 }
2773 }
2774 if (dst_buffer && !skip) {
2775 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2776 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
2777 if (hazard.hazard) {
2778 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
2779 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
2780 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
2781 region, string_UsageTag(hazard).c_str());
2782 }
2783 }
2784 if (skip) break;
2785 }
2786 return skip;
2787}
2788
2789void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
2790 auto *cb_context = GetAccessContext(commandBuffer);
2791 assert(cb_context);
2792 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
2793 auto *context = cb_context->GetCurrentAccessContext();
2794
2795 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2796 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2797
2798 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2799 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2800 if (src_buffer) {
2801 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2802 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
2803 }
2804 if (dst_buffer) {
2805 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2806 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
2807 }
2808 }
2809}
2810
John Zulauf5c5e88d2019-12-26 11:22:02 -07002811bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2812 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2813 const VkImageCopy *pRegions) const {
2814 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002815 const auto *cb_access_context = GetAccessContext(commandBuffer);
2816 assert(cb_access_context);
2817 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002818
John Zulauf3d84f1b2020-03-09 13:33:25 -06002819 const auto *context = cb_access_context->GetCurrentAccessContext();
2820 assert(context);
2821 if (!context) return skip;
2822
2823 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2824 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002825 for (uint32_t region = 0; region < regionCount; region++) {
2826 const auto &copy_region = pRegions[region];
2827 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002828 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002829 copy_region.srcOffset, copy_region.extent);
2830 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002831 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002832 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002833 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002834 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002835 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002836 }
2837
2838 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002839 VkExtent3D dst_copy_extent =
2840 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002841 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002842 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002843 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002844 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002845 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002846 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002847 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002848 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002849 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002850 }
2851 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002852
John Zulauf5c5e88d2019-12-26 11:22:02 -07002853 return skip;
2854}
2855
2856void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2857 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2858 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002859 auto *cb_access_context = GetAccessContext(commandBuffer);
2860 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002861 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002862 auto *context = cb_access_context->GetCurrentAccessContext();
2863 assert(context);
2864
John Zulauf5c5e88d2019-12-26 11:22:02 -07002865 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002866 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002867
2868 for (uint32_t region = 0; region < regionCount; region++) {
2869 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002870 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002871 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2872 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002873 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002874 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002875 VkExtent3D dst_copy_extent =
2876 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002877 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2878 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002879 }
2880 }
2881}
2882
Jeff Leger178b1e52020-10-05 12:22:23 -04002883bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
2884 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
2885 bool skip = false;
2886 const auto *cb_access_context = GetAccessContext(commandBuffer);
2887 assert(cb_access_context);
2888 if (!cb_access_context) return skip;
2889
2890 const auto *context = cb_access_context->GetCurrentAccessContext();
2891 assert(context);
2892 if (!context) return skip;
2893
2894 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2895 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2896 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2897 const auto &copy_region = pCopyImageInfo->pRegions[region];
2898 if (src_image) {
2899 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
2900 copy_region.srcOffset, copy_region.extent);
2901 if (hazard.hazard) {
2902 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
2903 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
2904 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
2905 region, string_UsageTag(hazard).c_str());
2906 }
2907 }
2908
2909 if (dst_image) {
2910 VkExtent3D dst_copy_extent =
2911 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2912 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
2913 copy_region.dstOffset, dst_copy_extent);
2914 if (hazard.hazard) {
2915 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
2916 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
2917 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
2918 region, string_UsageTag(hazard).c_str());
2919 }
2920 if (skip) break;
2921 }
2922 }
2923
2924 return skip;
2925}
2926
2927void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
2928 auto *cb_access_context = GetAccessContext(commandBuffer);
2929 assert(cb_access_context);
2930 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
2931 auto *context = cb_access_context->GetCurrentAccessContext();
2932 assert(context);
2933
2934 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2935 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2936
2937 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2938 const auto &copy_region = pCopyImageInfo->pRegions[region];
2939 if (src_image) {
2940 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2941 copy_region.extent, tag);
2942 }
2943 if (dst_image) {
2944 VkExtent3D dst_copy_extent =
2945 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2946 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2947 dst_copy_extent, tag);
2948 }
2949 }
2950}
2951
John Zulauf9cb530d2019-09-30 14:14:10 -06002952bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2953 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2954 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2955 uint32_t bufferMemoryBarrierCount,
2956 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2957 uint32_t imageMemoryBarrierCount,
2958 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2959 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002960 const auto *cb_access_context = GetAccessContext(commandBuffer);
2961 assert(cb_access_context);
2962 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002963
John Zulauf3d84f1b2020-03-09 13:33:25 -06002964 const auto *context = cb_access_context->GetCurrentAccessContext();
2965 assert(context);
2966 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002967
John Zulauf3d84f1b2020-03-09 13:33:25 -06002968 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002969 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2970 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002971 // Validate Image Layout transitions
2972 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2973 const auto &barrier = pImageMemoryBarriers[index];
2974 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2975 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2976 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002977 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002978 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002979 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002980 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002981 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002982 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002983 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002984 }
2985 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002986
2987 return skip;
2988}
2989
2990void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2991 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2992 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2993 uint32_t bufferMemoryBarrierCount,
2994 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2995 uint32_t imageMemoryBarrierCount,
2996 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002997 auto *cb_access_context = GetAccessContext(commandBuffer);
2998 assert(cb_access_context);
2999 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06003000 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003001 auto access_context = cb_access_context->GetCurrentAccessContext();
3002 assert(access_context);
3003 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003004
John Zulauf3d84f1b2020-03-09 13:33:25 -06003005 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003006 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003007 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003008 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
3009 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
3010 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf89311b42020-09-29 16:28:47 -06003011
3012 // These two apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
3013 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
3014 // of the barriers is maintained.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003015 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
3016 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06003017 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06003018 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003019
John Zulauf89311b42020-09-29 16:28:47 -06003020 // Apply the global barriers last as is it walks all memory, it can also clean up the "pending" state without requiring an
3021 // additional pass, updating the dependency chains *last* as it goes along.
3022 // This is needed to guarantee order independence of the three lists.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003023 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf89311b42020-09-29 16:28:47 -06003024 pMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003025}
3026
3027void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3028 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3029 // The state tracker sets up the device state
3030 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3031
John Zulauf5f13a792020-03-10 07:31:21 -06003032 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3033 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003034 // TODO: Find a good way to do this hooklessly.
3035 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3036 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3037 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3038
John Zulaufd1f85d42020-04-15 12:23:15 -06003039 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3040 sync_device_state->ResetCommandBufferCallback(command_buffer);
3041 });
3042 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3043 sync_device_state->FreeCommandBufferCallback(command_buffer);
3044 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003045}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003046
John Zulauf355e49b2020-04-24 15:11:15 -06003047bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3048 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
3049 bool skip = false;
3050 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
3051 auto cb_context = GetAccessContext(commandBuffer);
3052
3053 if (rp_state && cb_context) {
3054 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
3055 }
3056
3057 return skip;
3058}
3059
3060bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3061 VkSubpassContents contents) const {
3062 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3063 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3064 subpass_begin_info.contents = contents;
3065 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
3066 return skip;
3067}
3068
3069bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3070 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
3071 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3072 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
3073 return skip;
3074}
3075
3076bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3077 const VkRenderPassBeginInfo *pRenderPassBegin,
3078 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
3079 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3080 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
3081 return skip;
3082}
3083
John Zulauf3d84f1b2020-03-09 13:33:25 -06003084void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3085 VkResult result) {
3086 // The state tracker sets up the command buffer state
3087 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3088
3089 // Create/initialize the structure that trackers accesses at the command buffer scope.
3090 auto cb_access_context = GetAccessContext(commandBuffer);
3091 assert(cb_access_context);
3092 cb_access_context->Reset();
3093}
3094
3095void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06003096 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003097 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003098 if (cb_context) {
3099 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003100 }
3101}
3102
3103void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3104 VkSubpassContents contents) {
3105 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3106 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3107 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003108 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003109}
3110
3111void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3112 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3113 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003114 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003115}
3116
3117void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3118 const VkRenderPassBeginInfo *pRenderPassBegin,
3119 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3120 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003121 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
3122}
3123
3124bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
3125 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
3126 bool skip = false;
3127
3128 auto cb_context = GetAccessContext(commandBuffer);
3129 assert(cb_context);
3130 auto cb_state = cb_context->GetCommandBufferState();
3131 if (!cb_state) return skip;
3132
3133 auto rp_state = cb_state->activeRenderPass;
3134 if (!rp_state) return skip;
3135
3136 skip |= cb_context->ValidateNextSubpass(func_name);
3137
3138 return skip;
3139}
3140
3141bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3142 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
3143 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3144 subpass_begin_info.contents = contents;
3145 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
3146 return skip;
3147}
3148
3149bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
3150 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3151 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3152 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
3153 return skip;
3154}
3155
3156bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3157 const VkSubpassEndInfo *pSubpassEndInfo) const {
3158 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3159 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
3160 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003161}
3162
3163void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003164 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003165 auto cb_context = GetAccessContext(commandBuffer);
3166 assert(cb_context);
3167 auto cb_state = cb_context->GetCommandBufferState();
3168 if (!cb_state) return;
3169
3170 auto rp_state = cb_state->activeRenderPass;
3171 if (!rp_state) return;
3172
John Zulauf355e49b2020-04-24 15:11:15 -06003173 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003174}
3175
3176void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3177 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
3178 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3179 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003180 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003181}
3182
3183void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3184 const VkSubpassEndInfo *pSubpassEndInfo) {
3185 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003186 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003187}
3188
3189void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3190 const VkSubpassEndInfo *pSubpassEndInfo) {
3191 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003192 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003193}
3194
John Zulauf355e49b2020-04-24 15:11:15 -06003195bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
3196 const char *func_name) const {
3197 bool skip = false;
3198
3199 auto cb_context = GetAccessContext(commandBuffer);
3200 assert(cb_context);
3201 auto cb_state = cb_context->GetCommandBufferState();
3202 if (!cb_state) return skip;
3203
3204 auto rp_state = cb_state->activeRenderPass;
3205 if (!rp_state) return skip;
3206
3207 skip |= cb_context->ValidateEndRenderpass(func_name);
3208 return skip;
3209}
3210
3211bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3212 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
3213 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
3214 return skip;
3215}
3216
3217bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
3218 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3219 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
3220 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
3221 return skip;
3222}
3223
3224bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
3225 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3226 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
3227 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
3228 return skip;
3229}
3230
3231void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3232 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003233 // Resolve the all subpass contexts to the command buffer contexts
3234 auto cb_context = GetAccessContext(commandBuffer);
3235 assert(cb_context);
3236 auto cb_state = cb_context->GetCommandBufferState();
3237 if (!cb_state) return;
3238
locke-lunargaecf2152020-05-12 17:15:41 -06003239 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06003240 if (!rp_state) return;
3241
John Zulauf355e49b2020-04-24 15:11:15 -06003242 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06003243}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003244
John Zulauf33fc1d52020-07-17 11:01:10 -06003245// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3246// updates to a resource which do not conflict at the byte level.
3247// TODO: Revisit this rule to see if it needs to be tighter or looser
3248// TODO: Add programatic control over suppression heuristics
3249bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3250 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3251}
3252
John Zulauf3d84f1b2020-03-09 13:33:25 -06003253void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003254 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003255 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003256}
3257
3258void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003259 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003260 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003261}
3262
3263void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003264 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003265 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003266}
locke-lunarga19c71d2020-03-02 18:17:04 -07003267
Jeff Leger178b1e52020-10-05 12:22:23 -04003268template <typename BufferImageCopyRegionType>
3269bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3270 VkImageLayout dstImageLayout, uint32_t regionCount,
3271 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003272 bool skip = false;
3273 const auto *cb_access_context = GetAccessContext(commandBuffer);
3274 assert(cb_access_context);
3275 if (!cb_access_context) return skip;
3276
Jeff Leger178b1e52020-10-05 12:22:23 -04003277 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3278 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3279
locke-lunarga19c71d2020-03-02 18:17:04 -07003280 const auto *context = cb_access_context->GetCurrentAccessContext();
3281 assert(context);
3282 if (!context) return skip;
3283
3284 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003285 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3286
3287 for (uint32_t region = 0; region < regionCount; region++) {
3288 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003289 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003290 ResourceAccessRange src_range =
3291 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003292 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003293 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003294 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003295 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003296 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003297 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003298 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003299 }
3300 }
3301 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003302 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003303 copy_region.imageOffset, copy_region.imageExtent);
3304 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003305 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003306 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003307 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003308 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003309 }
3310 if (skip) break;
3311 }
3312 if (skip) break;
3313 }
3314 return skip;
3315}
3316
Jeff Leger178b1e52020-10-05 12:22:23 -04003317bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3318 VkImageLayout dstImageLayout, uint32_t regionCount,
3319 const VkBufferImageCopy *pRegions) const {
3320 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3321 COPY_COMMAND_VERSION_1);
3322}
3323
3324bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3325 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3326 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3327 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3328 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3329}
3330
3331template <typename BufferImageCopyRegionType>
3332void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3333 VkImageLayout dstImageLayout, uint32_t regionCount,
3334 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003335 auto *cb_access_context = GetAccessContext(commandBuffer);
3336 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003337
3338 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3339 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3340
3341 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003342 auto *context = cb_access_context->GetCurrentAccessContext();
3343 assert(context);
3344
3345 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003346 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003347
3348 for (uint32_t region = 0; region < regionCount; region++) {
3349 const auto &copy_region = pRegions[region];
3350 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003351 ResourceAccessRange src_range =
3352 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003353 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003354 }
3355 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003356 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003357 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003358 }
3359 }
3360}
3361
Jeff Leger178b1e52020-10-05 12:22:23 -04003362void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3363 VkImageLayout dstImageLayout, uint32_t regionCount,
3364 const VkBufferImageCopy *pRegions) {
3365 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3366 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3367}
3368
3369void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3370 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3371 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3372 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3373 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3374 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3375}
3376
3377template <typename BufferImageCopyRegionType>
3378bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3379 VkBuffer dstBuffer, uint32_t regionCount,
3380 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003381 bool skip = false;
3382 const auto *cb_access_context = GetAccessContext(commandBuffer);
3383 assert(cb_access_context);
3384 if (!cb_access_context) return skip;
3385
Jeff Leger178b1e52020-10-05 12:22:23 -04003386 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3387 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3388
locke-lunarga19c71d2020-03-02 18:17:04 -07003389 const auto *context = cb_access_context->GetCurrentAccessContext();
3390 assert(context);
3391 if (!context) return skip;
3392
3393 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3394 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3395 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3396 for (uint32_t region = 0; region < regionCount; region++) {
3397 const auto &copy_region = pRegions[region];
3398 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003399 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003400 copy_region.imageOffset, copy_region.imageExtent);
3401 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003402 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003403 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003404 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003405 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003406 }
3407 }
3408 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06003409 ResourceAccessRange dst_range =
3410 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003411 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003412 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003413 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003414 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003415 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003416 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003417 }
3418 }
3419 if (skip) break;
3420 }
3421 return skip;
3422}
3423
Jeff Leger178b1e52020-10-05 12:22:23 -04003424bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3425 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3426 const VkBufferImageCopy *pRegions) const {
3427 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3428 COPY_COMMAND_VERSION_1);
3429}
3430
3431bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3432 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3433 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3434 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3435 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3436}
3437
3438template <typename BufferImageCopyRegionType>
3439void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3440 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3441 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003442 auto *cb_access_context = GetAccessContext(commandBuffer);
3443 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003444
3445 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3446 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3447
3448 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003449 auto *context = cb_access_context->GetCurrentAccessContext();
3450 assert(context);
3451
3452 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003453 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3454 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 -06003455 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003456
3457 for (uint32_t region = 0; region < regionCount; region++) {
3458 const auto &copy_region = pRegions[region];
3459 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003460 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003461 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003462 }
3463 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003464 ResourceAccessRange dst_range =
3465 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003466 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003467 }
3468 }
3469}
3470
Jeff Leger178b1e52020-10-05 12:22:23 -04003471void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3472 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3473 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3474 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3475}
3476
3477void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3478 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3479 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3480 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3481 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3482 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3483}
3484
3485template <typename RegionType>
3486bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3487 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3488 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003489 bool skip = false;
3490 const auto *cb_access_context = GetAccessContext(commandBuffer);
3491 assert(cb_access_context);
3492 if (!cb_access_context) return skip;
3493
3494 const auto *context = cb_access_context->GetCurrentAccessContext();
3495 assert(context);
3496 if (!context) return skip;
3497
3498 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3499 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3500
3501 for (uint32_t region = 0; region < regionCount; region++) {
3502 const auto &blit_region = pRegions[region];
3503 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003504 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3505 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3506 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3507 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3508 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3509 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3510 auto hazard =
3511 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003512 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003513 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003514 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003515 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003516 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003517 }
3518 }
3519
3520 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003521 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3522 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3523 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3524 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3525 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3526 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3527 auto hazard =
3528 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003529 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003530 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003531 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003532 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003533 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003534 }
3535 if (skip) break;
3536 }
3537 }
3538
3539 return skip;
3540}
3541
Jeff Leger178b1e52020-10-05 12:22:23 -04003542bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3543 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3544 const VkImageBlit *pRegions, VkFilter filter) const {
3545 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3546 "vkCmdBlitImage");
3547}
3548
3549bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3550 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3551 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3552 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3553 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3554}
3555
3556template <typename RegionType>
3557void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3558 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3559 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003560 auto *cb_access_context = GetAccessContext(commandBuffer);
3561 assert(cb_access_context);
3562 auto *context = cb_access_context->GetCurrentAccessContext();
3563 assert(context);
3564
3565 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003566 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003567
3568 for (uint32_t region = 0; region < regionCount; region++) {
3569 const auto &blit_region = pRegions[region];
3570 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003571 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3572 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3573 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3574 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3575 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3576 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3577 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003578 }
3579 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003580 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3581 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3582 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3583 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3584 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3585 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3586 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003587 }
3588 }
3589}
locke-lunarg36ba2592020-04-03 09:42:04 -06003590
Jeff Leger178b1e52020-10-05 12:22:23 -04003591void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3592 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3593 const VkImageBlit *pRegions, VkFilter filter) {
3594 auto *cb_access_context = GetAccessContext(commandBuffer);
3595 assert(cb_access_context);
3596 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3597 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3598 pRegions, filter);
3599 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3600}
3601
3602void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3603 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3604 auto *cb_access_context = GetAccessContext(commandBuffer);
3605 assert(cb_access_context);
3606 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3607 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3608 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3609 pBlitImageInfo->filter, tag);
3610}
3611
locke-lunarg61870c22020-06-09 14:51:50 -06003612bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
3613 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
3614 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003615 bool skip = false;
3616 if (drawCount == 0) return skip;
3617
3618 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3619 VkDeviceSize size = struct_size;
3620 if (drawCount == 1 || stride == size) {
3621 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003622 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003623 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3624 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003625 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003626 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003627 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003628 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003629 }
3630 } else {
3631 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003632 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003633 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3634 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003635 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003636 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3637 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3638 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003639 break;
3640 }
3641 }
3642 }
3643 return skip;
3644}
3645
locke-lunarg61870c22020-06-09 14:51:50 -06003646void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3647 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3648 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003649 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3650 VkDeviceSize size = struct_size;
3651 if (drawCount == 1 || stride == size) {
3652 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003653 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003654 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3655 } else {
3656 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003657 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003658 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3659 }
3660 }
3661}
3662
locke-lunarg61870c22020-06-09 14:51:50 -06003663bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3664 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003665 bool skip = false;
3666
3667 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003668 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003669 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3670 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003671 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003672 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003673 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003674 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003675 }
3676 return skip;
3677}
3678
locke-lunarg61870c22020-06-09 14:51:50 -06003679void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003680 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003681 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003682 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3683}
3684
locke-lunarg36ba2592020-04-03 09:42:04 -06003685bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003686 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003687 const auto *cb_access_context = GetAccessContext(commandBuffer);
3688 assert(cb_access_context);
3689 if (!cb_access_context) return skip;
3690
locke-lunarg61870c22020-06-09 14:51:50 -06003691 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003692 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003693}
3694
3695void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003696 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003697 auto *cb_access_context = GetAccessContext(commandBuffer);
3698 assert(cb_access_context);
3699 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003700
locke-lunarg61870c22020-06-09 14:51:50 -06003701 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003702}
locke-lunarge1a67022020-04-29 00:15:36 -06003703
3704bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003705 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003706 const auto *cb_access_context = GetAccessContext(commandBuffer);
3707 assert(cb_access_context);
3708 if (!cb_access_context) return skip;
3709
3710 const auto *context = cb_access_context->GetCurrentAccessContext();
3711 assert(context);
3712 if (!context) return skip;
3713
locke-lunarg61870c22020-06-09 14:51:50 -06003714 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3715 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3716 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003717 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003718}
3719
3720void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003721 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003722 auto *cb_access_context = GetAccessContext(commandBuffer);
3723 assert(cb_access_context);
3724 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3725 auto *context = cb_access_context->GetCurrentAccessContext();
3726 assert(context);
3727
locke-lunarg61870c22020-06-09 14:51:50 -06003728 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3729 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003730}
3731
3732bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3733 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003734 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003735 const auto *cb_access_context = GetAccessContext(commandBuffer);
3736 assert(cb_access_context);
3737 if (!cb_access_context) return skip;
3738
locke-lunarg61870c22020-06-09 14:51:50 -06003739 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3740 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3741 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003742 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003743}
3744
3745void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3746 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003747 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003748 auto *cb_access_context = GetAccessContext(commandBuffer);
3749 assert(cb_access_context);
3750 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003751
locke-lunarg61870c22020-06-09 14:51:50 -06003752 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3753 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3754 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003755}
3756
3757bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3758 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003759 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003760 const auto *cb_access_context = GetAccessContext(commandBuffer);
3761 assert(cb_access_context);
3762 if (!cb_access_context) return skip;
3763
locke-lunarg61870c22020-06-09 14:51:50 -06003764 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3765 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3766 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003767 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003768}
3769
3770void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3771 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003772 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003773 auto *cb_access_context = GetAccessContext(commandBuffer);
3774 assert(cb_access_context);
3775 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003776
locke-lunarg61870c22020-06-09 14:51:50 -06003777 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3778 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3779 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003780}
3781
3782bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3783 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003784 bool skip = false;
3785 if (drawCount == 0) return skip;
3786
locke-lunargff255f92020-05-13 18:53:52 -06003787 const auto *cb_access_context = GetAccessContext(commandBuffer);
3788 assert(cb_access_context);
3789 if (!cb_access_context) return skip;
3790
3791 const auto *context = cb_access_context->GetCurrentAccessContext();
3792 assert(context);
3793 if (!context) return skip;
3794
locke-lunarg61870c22020-06-09 14:51:50 -06003795 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3796 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3797 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3798 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003799
3800 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3801 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3802 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003803 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003804 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003805}
3806
3807void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3808 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003809 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003810 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003811 auto *cb_access_context = GetAccessContext(commandBuffer);
3812 assert(cb_access_context);
3813 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3814 auto *context = cb_access_context->GetCurrentAccessContext();
3815 assert(context);
3816
locke-lunarg61870c22020-06-09 14:51:50 -06003817 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3818 cb_access_context->RecordDrawSubpassAttachment(tag);
3819 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003820
3821 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3822 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3823 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003824 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003825}
3826
3827bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3828 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003829 bool skip = false;
3830 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003831 const auto *cb_access_context = GetAccessContext(commandBuffer);
3832 assert(cb_access_context);
3833 if (!cb_access_context) return skip;
3834
3835 const auto *context = cb_access_context->GetCurrentAccessContext();
3836 assert(context);
3837 if (!context) return skip;
3838
locke-lunarg61870c22020-06-09 14:51:50 -06003839 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3840 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3841 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3842 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003843
3844 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3845 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3846 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003847 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003848 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003849}
3850
3851void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3852 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003853 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003854 auto *cb_access_context = GetAccessContext(commandBuffer);
3855 assert(cb_access_context);
3856 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3857 auto *context = cb_access_context->GetCurrentAccessContext();
3858 assert(context);
3859
locke-lunarg61870c22020-06-09 14:51:50 -06003860 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3861 cb_access_context->RecordDrawSubpassAttachment(tag);
3862 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003863
3864 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3865 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3866 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003867 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003868}
3869
3870bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3871 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3872 uint32_t stride, const char *function) const {
3873 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003874 const auto *cb_access_context = GetAccessContext(commandBuffer);
3875 assert(cb_access_context);
3876 if (!cb_access_context) return skip;
3877
3878 const auto *context = cb_access_context->GetCurrentAccessContext();
3879 assert(context);
3880 if (!context) return skip;
3881
locke-lunarg61870c22020-06-09 14:51:50 -06003882 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3883 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3884 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3885 function);
3886 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003887
3888 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3889 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3890 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003891 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003892 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003893}
3894
3895bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3896 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3897 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003898 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3899 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003900}
3901
3902void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3903 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3904 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003905 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3906 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003907 auto *cb_access_context = GetAccessContext(commandBuffer);
3908 assert(cb_access_context);
3909 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3910 auto *context = cb_access_context->GetCurrentAccessContext();
3911 assert(context);
3912
locke-lunarg61870c22020-06-09 14:51:50 -06003913 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3914 cb_access_context->RecordDrawSubpassAttachment(tag);
3915 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3916 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003917
3918 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3919 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3920 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003921 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003922}
3923
3924bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3925 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3926 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003927 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3928 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003929}
3930
3931void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3932 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3933 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003934 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3935 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003936 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003937}
3938
3939bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3940 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3941 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003942 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3943 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003944}
3945
3946void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3947 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3948 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003949 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3950 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003951 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3952}
3953
3954bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3955 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3956 uint32_t stride, const char *function) const {
3957 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003958 const auto *cb_access_context = GetAccessContext(commandBuffer);
3959 assert(cb_access_context);
3960 if (!cb_access_context) return skip;
3961
3962 const auto *context = cb_access_context->GetCurrentAccessContext();
3963 assert(context);
3964 if (!context) return skip;
3965
locke-lunarg61870c22020-06-09 14:51:50 -06003966 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3967 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3968 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3969 stride, function);
3970 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003971
3972 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3973 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3974 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003975 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003976 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003977}
3978
3979bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3980 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3981 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003982 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3983 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003984}
3985
3986void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3987 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3988 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003989 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3990 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003991 auto *cb_access_context = GetAccessContext(commandBuffer);
3992 assert(cb_access_context);
3993 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3994 auto *context = cb_access_context->GetCurrentAccessContext();
3995 assert(context);
3996
locke-lunarg61870c22020-06-09 14:51:50 -06003997 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3998 cb_access_context->RecordDrawSubpassAttachment(tag);
3999 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4000 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004001
4002 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4003 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004004 // We will update the index and vertex buffer in SubmitQueue in the future.
4005 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004006}
4007
4008bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4009 VkDeviceSize offset, VkBuffer countBuffer,
4010 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4011 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004012 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4013 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004014}
4015
4016void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4017 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4018 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004019 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4020 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004021 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4022}
4023
4024bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4025 VkDeviceSize offset, VkBuffer countBuffer,
4026 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4027 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004028 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4029 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004030}
4031
4032void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4033 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4034 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004035 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4036 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004037 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4038}
4039
4040bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4041 const VkClearColorValue *pColor, uint32_t rangeCount,
4042 const VkImageSubresourceRange *pRanges) const {
4043 bool skip = false;
4044 const auto *cb_access_context = GetAccessContext(commandBuffer);
4045 assert(cb_access_context);
4046 if (!cb_access_context) return skip;
4047
4048 const auto *context = cb_access_context->GetCurrentAccessContext();
4049 assert(context);
4050 if (!context) return skip;
4051
4052 const auto *image_state = Get<IMAGE_STATE>(image);
4053
4054 for (uint32_t index = 0; index < rangeCount; index++) {
4055 const auto &range = pRanges[index];
4056 if (image_state) {
4057 auto hazard =
4058 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4059 if (hazard.hazard) {
4060 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004061 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004062 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004063 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004064 }
4065 }
4066 }
4067 return skip;
4068}
4069
4070void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4071 const VkClearColorValue *pColor, uint32_t rangeCount,
4072 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004073 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004074 auto *cb_access_context = GetAccessContext(commandBuffer);
4075 assert(cb_access_context);
4076 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4077 auto *context = cb_access_context->GetCurrentAccessContext();
4078 assert(context);
4079
4080 const auto *image_state = Get<IMAGE_STATE>(image);
4081
4082 for (uint32_t index = 0; index < rangeCount; index++) {
4083 const auto &range = pRanges[index];
4084 if (image_state) {
4085 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4086 tag);
4087 }
4088 }
4089}
4090
4091bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4092 VkImageLayout imageLayout,
4093 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4094 const VkImageSubresourceRange *pRanges) const {
4095 bool skip = false;
4096 const auto *cb_access_context = GetAccessContext(commandBuffer);
4097 assert(cb_access_context);
4098 if (!cb_access_context) return skip;
4099
4100 const auto *context = cb_access_context->GetCurrentAccessContext();
4101 assert(context);
4102 if (!context) return skip;
4103
4104 const auto *image_state = Get<IMAGE_STATE>(image);
4105
4106 for (uint32_t index = 0; index < rangeCount; index++) {
4107 const auto &range = pRanges[index];
4108 if (image_state) {
4109 auto hazard =
4110 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4111 if (hazard.hazard) {
4112 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004113 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004114 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004115 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004116 }
4117 }
4118 }
4119 return skip;
4120}
4121
4122void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4123 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4124 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004125 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004126 auto *cb_access_context = GetAccessContext(commandBuffer);
4127 assert(cb_access_context);
4128 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4129 auto *context = cb_access_context->GetCurrentAccessContext();
4130 assert(context);
4131
4132 const auto *image_state = Get<IMAGE_STATE>(image);
4133
4134 for (uint32_t index = 0; index < rangeCount; index++) {
4135 const auto &range = pRanges[index];
4136 if (image_state) {
4137 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4138 tag);
4139 }
4140 }
4141}
4142
4143bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4144 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4145 VkDeviceSize dstOffset, VkDeviceSize stride,
4146 VkQueryResultFlags flags) const {
4147 bool skip = false;
4148 const auto *cb_access_context = GetAccessContext(commandBuffer);
4149 assert(cb_access_context);
4150 if (!cb_access_context) return skip;
4151
4152 const auto *context = cb_access_context->GetCurrentAccessContext();
4153 assert(context);
4154 if (!context) return skip;
4155
4156 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4157
4158 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004159 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004160 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4161 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004162 skip |=
4163 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4164 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4165 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004166 }
4167 }
locke-lunargff255f92020-05-13 18:53:52 -06004168
4169 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004170 return skip;
4171}
4172
4173void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4174 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4175 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004176 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4177 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004178 auto *cb_access_context = GetAccessContext(commandBuffer);
4179 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004180 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004181 auto *context = cb_access_context->GetCurrentAccessContext();
4182 assert(context);
4183
4184 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4185
4186 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004187 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004188 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4189 }
locke-lunargff255f92020-05-13 18:53:52 -06004190
4191 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004192}
4193
4194bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4195 VkDeviceSize size, uint32_t data) const {
4196 bool skip = false;
4197 const auto *cb_access_context = GetAccessContext(commandBuffer);
4198 assert(cb_access_context);
4199 if (!cb_access_context) return skip;
4200
4201 const auto *context = cb_access_context->GetCurrentAccessContext();
4202 assert(context);
4203 if (!context) return skip;
4204
4205 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4206
4207 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004208 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004209 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4210 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004211 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004212 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004213 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004214 }
4215 }
4216 return skip;
4217}
4218
4219void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4220 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004221 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004222 auto *cb_access_context = GetAccessContext(commandBuffer);
4223 assert(cb_access_context);
4224 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4225 auto *context = cb_access_context->GetCurrentAccessContext();
4226 assert(context);
4227
4228 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4229
4230 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004231 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004232 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4233 }
4234}
4235
4236bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4237 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4238 const VkImageResolve *pRegions) const {
4239 bool skip = false;
4240 const auto *cb_access_context = GetAccessContext(commandBuffer);
4241 assert(cb_access_context);
4242 if (!cb_access_context) return skip;
4243
4244 const auto *context = cb_access_context->GetCurrentAccessContext();
4245 assert(context);
4246 if (!context) return skip;
4247
4248 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4249 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4250
4251 for (uint32_t region = 0; region < regionCount; region++) {
4252 const auto &resolve_region = pRegions[region];
4253 if (src_image) {
4254 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4255 resolve_region.srcOffset, resolve_region.extent);
4256 if (hazard.hazard) {
4257 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004258 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004259 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004260 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004261 }
4262 }
4263
4264 if (dst_image) {
4265 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4266 resolve_region.dstOffset, resolve_region.extent);
4267 if (hazard.hazard) {
4268 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004269 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004270 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004271 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004272 }
4273 if (skip) break;
4274 }
4275 }
4276
4277 return skip;
4278}
4279
4280void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4281 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4282 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004283 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4284 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004285 auto *cb_access_context = GetAccessContext(commandBuffer);
4286 assert(cb_access_context);
4287 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4288 auto *context = cb_access_context->GetCurrentAccessContext();
4289 assert(context);
4290
4291 auto *src_image = Get<IMAGE_STATE>(srcImage);
4292 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4293
4294 for (uint32_t region = 0; region < regionCount; region++) {
4295 const auto &resolve_region = pRegions[region];
4296 if (src_image) {
4297 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4298 resolve_region.srcOffset, resolve_region.extent, tag);
4299 }
4300 if (dst_image) {
4301 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4302 resolve_region.dstOffset, resolve_region.extent, tag);
4303 }
4304 }
4305}
4306
Jeff Leger178b1e52020-10-05 12:22:23 -04004307bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4308 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4309 bool skip = false;
4310 const auto *cb_access_context = GetAccessContext(commandBuffer);
4311 assert(cb_access_context);
4312 if (!cb_access_context) return skip;
4313
4314 const auto *context = cb_access_context->GetCurrentAccessContext();
4315 assert(context);
4316 if (!context) return skip;
4317
4318 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4319 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4320
4321 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4322 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4323 if (src_image) {
4324 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4325 resolve_region.srcOffset, resolve_region.extent);
4326 if (hazard.hazard) {
4327 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4328 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4329 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
4330 region, string_UsageTag(hazard).c_str());
4331 }
4332 }
4333
4334 if (dst_image) {
4335 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4336 resolve_region.dstOffset, resolve_region.extent);
4337 if (hazard.hazard) {
4338 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4339 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4340 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
4341 region, string_UsageTag(hazard).c_str());
4342 }
4343 if (skip) break;
4344 }
4345 }
4346
4347 return skip;
4348}
4349
4350void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4351 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4352 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4353 auto *cb_access_context = GetAccessContext(commandBuffer);
4354 assert(cb_access_context);
4355 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4356 auto *context = cb_access_context->GetCurrentAccessContext();
4357 assert(context);
4358
4359 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4360 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4361
4362 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4363 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4364 if (src_image) {
4365 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4366 resolve_region.srcOffset, resolve_region.extent, tag);
4367 }
4368 if (dst_image) {
4369 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4370 resolve_region.dstOffset, resolve_region.extent, tag);
4371 }
4372 }
4373}
4374
locke-lunarge1a67022020-04-29 00:15:36 -06004375bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4376 VkDeviceSize dataSize, const void *pData) const {
4377 bool skip = false;
4378 const auto *cb_access_context = GetAccessContext(commandBuffer);
4379 assert(cb_access_context);
4380 if (!cb_access_context) return skip;
4381
4382 const auto *context = cb_access_context->GetCurrentAccessContext();
4383 assert(context);
4384 if (!context) return skip;
4385
4386 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4387
4388 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004389 // VK_WHOLE_SIZE not allowed
4390 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004391 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4392 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004393 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004394 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004395 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004396 }
4397 }
4398 return skip;
4399}
4400
4401void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4402 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004403 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004404 auto *cb_access_context = GetAccessContext(commandBuffer);
4405 assert(cb_access_context);
4406 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4407 auto *context = cb_access_context->GetCurrentAccessContext();
4408 assert(context);
4409
4410 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4411
4412 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004413 // VK_WHOLE_SIZE not allowed
4414 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004415 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4416 }
4417}
locke-lunargff255f92020-05-13 18:53:52 -06004418
4419bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4420 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4421 bool skip = false;
4422 const auto *cb_access_context = GetAccessContext(commandBuffer);
4423 assert(cb_access_context);
4424 if (!cb_access_context) return skip;
4425
4426 const auto *context = cb_access_context->GetCurrentAccessContext();
4427 assert(context);
4428 if (!context) return skip;
4429
4430 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4431
4432 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004433 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004434 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4435 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004436 skip |=
4437 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4438 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4439 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004440 }
4441 }
4442 return skip;
4443}
4444
4445void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4446 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004447 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004448 auto *cb_access_context = GetAccessContext(commandBuffer);
4449 assert(cb_access_context);
4450 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4451 auto *context = cb_access_context->GetCurrentAccessContext();
4452 assert(context);
4453
4454 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4455
4456 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004457 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004458 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4459 }
4460}