blob: 8ba009a2eaac033b3b4481af5bdbf98f534cc2f7 [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) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600481 // TODO -- review why async is storing non-const
John Zulauf540266b2020-04-06 18:54:53 -0600482 async_.emplace_back(const_cast<AccessContext *>(&contexts[async_subpass]));
John Zulauf3d84f1b2020-03-09 13:33:25 -0600483 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600484 if (subpass_dep.barrier_from_external.size()) {
485 src_external_ = TrackBack(external_context, queue_flags, subpass_dep.barrier_from_external);
John Zulaufe5da6e52020-03-18 15:32:18 -0600486 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600487 if (subpass_dep.barrier_to_external.size()) {
488 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600489 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700490}
491
John Zulauf5f13a792020-03-10 07:31:21 -0600492template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600493HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600494 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600495 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600496 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600497
498 HazardResult hazard;
499 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
500 hazard = detector.Detect(prev);
501 }
502 return hazard;
503}
504
John Zulauf3d84f1b2020-03-09 13:33:25 -0600505// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
506// the DAG of the contexts (for example subpasses)
507template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600508HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
509 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600510 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600511
John Zulauf1a224292020-06-30 14:52:13 -0600512 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600513 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
514 // so we'll check these first
515 for (const auto &async_context : async_) {
516 hazard = async_context->DetectAsyncHazard(type, detector, range);
517 if (hazard.hazard) return hazard;
518 }
John Zulauf5f13a792020-03-10 07:31:21 -0600519 }
520
John Zulauf1a224292020-06-30 14:52:13 -0600521 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600522
John Zulauf69133422020-05-20 14:55:53 -0600523 const auto &accesses = GetAccessStateMap(type);
524 const auto from = accesses.lower_bound(range);
525 const auto to = accesses.upper_bound(range);
526 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600527
John Zulauf69133422020-05-20 14:55:53 -0600528 for (auto pos = from; pos != to; ++pos) {
529 // Cover any leading gap, or gap between entries
530 if (detect_prev) {
531 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
532 // Cover any leading gap, or gap between entries
533 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600534 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600535 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600536 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600537 if (hazard.hazard) return hazard;
538 }
John Zulauf69133422020-05-20 14:55:53 -0600539 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
540 gap.begin = pos->first.end;
541 }
542
543 hazard = detector.Detect(pos);
544 if (hazard.hazard) return hazard;
545 }
546
547 if (detect_prev) {
548 // Detect in the trailing empty as needed
549 gap.end = range.end;
550 if (gap.non_empty()) {
551 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600552 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600553 }
554
555 return hazard;
556}
557
558// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
559template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600560HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600561 auto &accesses = GetAccessStateMap(type);
562 const auto from = accesses.lower_bound(range);
563 const auto to = accesses.upper_bound(range);
564
John Zulauf3d84f1b2020-03-09 13:33:25 -0600565 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600566 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
567 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600568 }
John Zulauf16adfc92020-04-08 10:28:33 -0600569
John Zulauf3d84f1b2020-03-09 13:33:25 -0600570 return hazard;
571}
572
John Zulaufb02c1eb2020-10-06 16:33:36 -0600573struct ApplySubpassTransitionBarriersAction {
574 ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
575 void operator()(ResourceAccessState *access) const {
576 assert(access);
577 access->ApplyBarriers(barriers, true);
578 }
579 const std::vector<SyncBarrier> &barriers;
580};
581
582struct ApplyTrackbackBarriersAction {
583 ApplyTrackbackBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
584 void operator()(ResourceAccessState *access) const {
585 assert(access);
586 assert(!access->HasPendingState());
587 access->ApplyBarriers(barriers, false);
588 access->ApplyPendingBarriers(kCurrentCommandTag);
589 }
590 const std::vector<SyncBarrier> &barriers;
591};
592
593// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
594// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
595// *different* map from dest.
596// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
597// range [first, last)
598template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600599static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
600 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600601 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600602 auto at = entry;
603 for (auto pos = first; pos != last; ++pos) {
604 // Every member of the input iterator range must fit within the remaining portion of entry
605 assert(at->first.includes(pos->first));
606 assert(at != dest->end());
607 // Trim up at to the same size as the entry to resolve
608 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600609 auto access = pos->second; // intentional copy
610 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600611 at->second.Resolve(access);
612 ++at; // Go to the remaining unused section of entry
613 }
614}
615
John Zulaufa0a98292020-09-18 09:30:10 -0600616static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
617 SyncBarrier merged = {};
618 for (const auto &barrier : barriers) {
619 merged.Merge(barrier);
620 }
621 return merged;
622}
623
John Zulaufb02c1eb2020-10-06 16:33:36 -0600624template <typename BarrierAction>
625void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600626 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
627 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600628 if (!range.non_empty()) return;
629
John Zulauf355e49b2020-04-24 15:11:15 -0600630 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
631 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600632 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600633 if (current->pos_B->valid) {
634 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600635 auto access = src_pos->second; // intentional copy
636 barrier_action(&access);
637
John Zulauf16adfc92020-04-08 10:28:33 -0600638 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600639 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
640 trimmed->second.Resolve(access);
641 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600642 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600643 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600644 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600645 }
John Zulauf16adfc92020-04-08 10:28:33 -0600646 } else {
647 // we have to descend to fill this gap
648 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600649 if (current->pos_A->valid) {
650 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
651 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600652 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600653 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -0600654 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600655 // There isn't anything in dest in current)range, so we can accumulate directly into it.
656 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600657 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
658 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
659 barrier_action(&pos->second);
John Zulauf355e49b2020-04-24 15:11:15 -0600660 }
661 }
662 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
663 // iterator of the outer while.
664
665 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
666 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
667 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600668 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
669 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600670 current.seek(seek_to);
671 } else if (!current->pos_A->valid && infill_state) {
672 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
673 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
674 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600675 }
John Zulauf5f13a792020-03-10 07:31:21 -0600676 }
John Zulauf16adfc92020-04-08 10:28:33 -0600677 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600678 }
John Zulauf1a224292020-06-30 14:52:13 -0600679
680 // Infill if range goes passed both the current and resolve map prior contents
681 if (recur_to_infill && (current->range.end < range.end)) {
682 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
683 ResourceAccessRangeMap gap_map;
684 const auto the_end = resolve_map->end();
685 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
686 for (auto &access : gap_map) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600687 barrier_action(&access.second);
John Zulauf1a224292020-06-30 14:52:13 -0600688 resolve_map->insert(the_end, access);
689 }
690 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600691}
692
John Zulauf355e49b2020-04-24 15:11:15 -0600693void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
694 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600695 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600696 if (range.non_empty() && infill_state) {
697 descent_map->insert(std::make_pair(range, *infill_state));
698 }
699 } else {
700 // Look for something to fill the gap further along.
701 for (const auto &prev_dep : prev_) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600702 const ApplyTrackbackBarriersAction barrier_action(prev_dep.barriers);
703 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600704 }
705
John Zulaufe5da6e52020-03-18 15:32:18 -0600706 if (src_external_.context) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600707 const ApplyTrackbackBarriersAction barrier_action(src_external_.barriers);
708 src_external_.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600709 }
710 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600711}
712
John Zulauf16adfc92020-04-08 10:28:33 -0600713AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600714 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600715}
716
John Zulauf16adfc92020-04-08 10:28:33 -0600717
John Zulauf1507ee42020-05-18 11:33:09 -0600718static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
719 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
720 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
721 return stage_access;
722}
723static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
724 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
725 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
726 return stage_access;
727}
728
John Zulauf7635de32020-05-29 17:14:15 -0600729// Caller must manage returned pointer
730static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
731 uint32_t subpass, const VkRect2D &render_area,
732 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
733 auto *proxy = new AccessContext(context);
734 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600735 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600736 return proxy;
737}
738
John Zulaufb02c1eb2020-10-06 16:33:36 -0600739template <typename BarrierAction>
John Zulauf52446eb2020-10-22 16:40:08 -0600740class ResolveAccessRangeFunctor {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600741 public:
742 ResolveAccessRangeFunctor(const AccessContext &context, AccessContext::AddressType address_type,
743 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
744 BarrierAction &barrier_action)
John Zulauf52446eb2020-10-22 16:40:08 -0600745 : context_(context),
746 address_type_(address_type),
747 descent_map_(descent_map),
748 infill_state_(infill_state),
749 barrier_action_(barrier_action) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600750 ResolveAccessRangeFunctor() = delete;
751 void operator()(const ResourceAccessRange &range) const {
752 context_.ResolveAccessRange(address_type_, range, barrier_action_, descent_map_, infill_state_);
753 }
754
755 private:
John Zulauf52446eb2020-10-22 16:40:08 -0600756 const AccessContext &context_;
757 const AccessContext::AddressType address_type_;
758 ResourceAccessRangeMap *const descent_map_;
759 const ResourceAccessState *infill_state_;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600760 BarrierAction &barrier_action_;
761};
762
John Zulaufb02c1eb2020-10-06 16:33:36 -0600763template <typename BarrierAction>
764void AccessContext::ResolveAccessRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range,
765 BarrierAction &barrier_action, AddressType address_type, ResourceAccessRangeMap *descent_map,
766 const ResourceAccessState *infill_state) const {
767 const ResolveAccessRangeFunctor<BarrierAction> action(*this, address_type, descent_map, infill_state, barrier_action);
768 ApplyOverImageRange(image_state, subresource_range, action);
John Zulauf62f10592020-04-03 12:20:02 -0600769}
770
John Zulauf7635de32020-05-29 17:14:15 -0600771// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600772bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600773 const VkRect2D &render_area, uint32_t subpass,
774 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
775 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600776 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600777 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
778 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
779 // those affects have not been recorded yet.
780 //
781 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
782 // to apply and only copy then, if this proves a hot spot.
783 std::unique_ptr<AccessContext> proxy_for_prev;
784 TrackBack proxy_track_back;
785
John Zulauf355e49b2020-04-24 15:11:15 -0600786 const auto &transitions = rp_state.subpass_transitions[subpass];
787 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600788 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
789
790 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
791 if (prev_needs_proxy) {
792 if (!proxy_for_prev) {
793 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
794 render_area, attachment_views));
795 proxy_track_back = *track_back;
796 proxy_track_back.context = proxy_for_prev.get();
797 }
798 track_back = &proxy_track_back;
799 }
800 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600801 if (hazard.hazard) {
John Zulauf389c34b2020-07-28 11:19:35 -0600802 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
803 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
804 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
805 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
806 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
807 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600808 }
809 }
810 return skip;
811}
812
John Zulauf1507ee42020-05-18 11:33:09 -0600813bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600814 const VkRect2D &render_area, uint32_t subpass,
815 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
816 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600817 bool skip = false;
818 const auto *attachment_ci = rp_state.createInfo.pAttachments;
819 VkExtent3D extent = CastTo3D(render_area.extent);
820 VkOffset3D offset = CastTo3D(render_area.offset);
John Zulaufa0a98292020-09-18 09:30:10 -0600821
John Zulauf1507ee42020-05-18 11:33:09 -0600822 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
823 if (subpass == rp_state.attachment_first_subpass[i]) {
824 if (attachment_views[i] == nullptr) continue;
825 const IMAGE_VIEW_STATE &view = *attachment_views[i];
826 const IMAGE_STATE *image = view.image_state.get();
827 if (image == nullptr) continue;
828 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -0600829
830 // Need check in the following way
831 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
832 // vs. transition
833 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
834 // for each aspect loaded.
835
836 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600837 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600838 const bool is_color = !(has_depth || has_stencil);
839
840 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -0600841 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -0600842
John Zulaufaff20662020-06-01 14:07:58 -0600843 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600844 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -0600845
John Zulaufb02c1eb2020-10-06 16:33:36 -0600846 auto hazard_range = view.normalized_subresource_range;
847 bool checked_stencil = false;
848 if (is_color) {
John Zulauf859089b2020-10-29 17:37:03 -0600849 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, kColorAttachmentRasterOrder, offset,
850 extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600851 aspect = "color";
852 } else {
853 if (has_depth) {
854 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf859089b2020-10-29 17:37:03 -0600855 hazard = DetectHazard(*image, load_index, hazard_range, kDepthStencilAttachmentRasterOrder, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600856 aspect = "depth";
857 }
858 if (!hazard.hazard && has_stencil) {
859 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf859089b2020-10-29 17:37:03 -0600860 hazard =
861 DetectHazard(*image, stencil_load_index, hazard_range, kDepthStencilAttachmentRasterOrder, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600862 aspect = "stencil";
863 checked_stencil = true;
864 }
865 }
866
867 if (hazard.hazard) {
868 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
869 if (hazard.tag == kCurrentCommandTag) {
870 // Hazard vs. ILT
871 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
872 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
873 " aspect %s during load with loadOp %s.",
874 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
875 } else {
John Zulauf1507ee42020-05-18 11:33:09 -0600876 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
877 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600878 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -0600879 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600880 string_UsageTag(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -0600881 }
882 }
883 }
884 }
885 return skip;
886}
887
John Zulaufaff20662020-06-01 14:07:58 -0600888// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
889// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
890// store is part of the same Next/End operation.
891// The latter is handled in layout transistion validation directly
892bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
893 const VkRect2D &render_area, uint32_t subpass,
894 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
895 const char *func_name) const {
896 bool skip = false;
897 const auto *attachment_ci = rp_state.createInfo.pAttachments;
898 VkExtent3D extent = CastTo3D(render_area.extent);
899 VkOffset3D offset = CastTo3D(render_area.offset);
900
901 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
902 if (subpass == rp_state.attachment_last_subpass[i]) {
903 if (attachment_views[i] == nullptr) continue;
904 const IMAGE_VIEW_STATE &view = *attachment_views[i];
905 const IMAGE_STATE *image = view.image_state.get();
906 if (image == nullptr) continue;
907 const auto &ci = attachment_ci[i];
908
909 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
910 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
911 // sake, we treat DONT_CARE as writing.
912 const bool has_depth = FormatHasDepth(ci.format);
913 const bool has_stencil = FormatHasStencil(ci.format);
914 const bool is_color = !(has_depth || has_stencil);
915 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
916 if (!has_stencil && !store_op_stores) continue;
917
918 HazardResult hazard;
919 const char *aspect = nullptr;
920 bool checked_stencil = false;
921 if (is_color) {
922 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
923 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
924 aspect = "color";
925 } else {
926 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
927 auto hazard_range = view.normalized_subresource_range;
928 if (has_depth && store_op_stores) {
929 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
930 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
931 kAttachmentRasterOrder, offset, extent);
932 aspect = "depth";
933 }
934 if (!hazard.hazard && has_stencil && stencil_op_stores) {
935 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
936 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
937 kAttachmentRasterOrder, offset, extent);
938 aspect = "stencil";
939 checked_stencil = true;
940 }
941 }
942
943 if (hazard.hazard) {
944 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
945 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -0600946 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
947 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600948 " %s aspect during store with %s %s. Access info %s",
John Zulauf1dae9192020-06-16 15:46:44 -0600949 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600950 store_op_string, string_UsageTag(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -0600951 }
952 }
953 }
954 return skip;
955}
956
John Zulaufb027cdb2020-05-21 14:25:22 -0600957bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
958 const VkRect2D &render_area,
959 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
960 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600961 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
962 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
963 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600964}
965
John Zulauf3d84f1b2020-03-09 13:33:25 -0600966class HazardDetector {
967 SyncStageAccessIndex usage_index_;
968
969 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600970 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600971 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
972 return pos->second.DetectAsyncHazard(usage_index_);
973 }
974 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
975};
976
John Zulauf69133422020-05-20 14:55:53 -0600977class HazardDetectorWithOrdering {
978 const SyncStageAccessIndex usage_index_;
979 const SyncOrderingBarrier &ordering_;
980
981 public:
982 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
983 return pos->second.DetectHazard(usage_index_, ordering_);
984 }
985 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
986 return pos->second.DetectAsyncHazard(usage_index_);
987 }
988 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
989 : usage_index_(usage), ordering_(ordering) {}
990};
991
John Zulauf16adfc92020-04-08 10:28:33 -0600992HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600993 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600994 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600995 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600996}
997
John Zulauf16adfc92020-04-08 10:28:33 -0600998HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600999 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001000 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001001 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -06001002}
1003
John Zulauf69133422020-05-20 14:55:53 -06001004template <typename Detector>
1005HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1006 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1007 const VkExtent3D &extent, DetectOptions options) const {
1008 if (!SimpleBinding(image)) return HazardResult();
1009 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
1010 const auto address_type = ImageAddressType(image);
1011 const auto base_address = ResourceBaseAddress(image);
1012 for (; range_gen->non_empty(); ++range_gen) {
1013 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
1014 if (hazard.hazard) return hazard;
1015 }
1016 return HazardResult();
1017}
1018
John Zulauf540266b2020-04-06 18:54:53 -06001019HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1020 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1021 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001022 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1023 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -06001024 return DetectHazard(image, current_usage, subresource_range, offset, extent);
1025}
1026
1027HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1028 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1029 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -06001030 HazardDetector detector(current_usage);
1031 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
1032}
1033
1034HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1035 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
1036 const VkOffset3D &offset, const VkExtent3D &extent) const {
1037 HazardDetectorWithOrdering detector(current_usage, ordering);
1038 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001039}
1040
John Zulaufb027cdb2020-05-21 14:25:22 -06001041// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
1042// should have reported the issue regarding an invalid attachment entry
1043HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
1044 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
1045 VkImageAspectFlags aspect_mask) const {
1046 if (view != nullptr) {
1047 const IMAGE_STATE *image = view->image_state.get();
1048 if (image != nullptr) {
1049 auto *detect_range = &view->normalized_subresource_range;
1050 VkImageSubresourceRange masked_range;
1051 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1052 masked_range = view->normalized_subresource_range;
1053 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1054 detect_range = &masked_range;
1055 }
1056
1057 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1058 if (detect_range->aspectMask) {
1059 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
1060 }
1061 }
1062 }
1063 return HazardResult();
1064}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001065class BarrierHazardDetector {
1066 public:
1067 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1068 SyncStageAccessFlags src_access_scope)
1069 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1070
John Zulauf5f13a792020-03-10 07:31:21 -06001071 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1072 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001073 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001074 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
1075 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1076 return pos->second.DetectAsyncHazard(usage_index_);
1077 }
1078
1079 private:
1080 SyncStageAccessIndex usage_index_;
1081 VkPipelineStageFlags src_exec_scope_;
1082 SyncStageAccessFlags src_access_scope_;
1083};
1084
John Zulauf16adfc92020-04-08 10:28:33 -06001085HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001086 VkPipelineStageFlags src_exec_scope, const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001087 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001088 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -06001089 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001090}
1091
John Zulauf16adfc92020-04-08 10:28:33 -06001092HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001093 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001094 const VkImageSubresourceRange &subresource_range,
1095 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001096 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1097 VkOffset3D zero_offset = {0, 0, 0};
1098 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001099}
1100
John Zulauf355e49b2020-04-24 15:11:15 -06001101HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001102 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001103 const VkImageMemoryBarrier &barrier) const {
1104 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1105 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1106 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1107}
1108
John Zulauf9cb530d2019-09-30 14:14:10 -06001109template <typename Flags, typename Map>
1110SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1111 SyncStageAccessFlags scope = 0;
1112 for (const auto &bit_scope : map) {
1113 if (flag_mask < bit_scope.first) break;
1114
1115 if (flag_mask & bit_scope.first) {
1116 scope |= bit_scope.second;
1117 }
1118 }
1119 return scope;
1120}
1121
1122SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
1123 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1124}
1125
1126SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
1127 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
1128}
1129
1130// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
1131SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001132 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1133 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1134 // 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 -06001135 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1136}
1137
1138template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001139void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001140 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1141 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -06001142 auto pos = accesses->lower_bound(range);
1143 if (pos == accesses->end() || !pos->first.intersects(range)) {
1144 // The range is empty, fill it with a default value.
1145 pos = action.Infill(accesses, pos, range);
1146 } else if (range.begin < pos->first.begin) {
1147 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001148 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001149 } else if (pos->first.begin < range.begin) {
1150 // Trim the beginning if needed
1151 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1152 ++pos;
1153 }
1154
1155 const auto the_end = accesses->end();
1156 while ((pos != the_end) && pos->first.intersects(range)) {
1157 if (pos->first.end > range.end) {
1158 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1159 }
1160
1161 pos = action(accesses, pos);
1162 if (pos == the_end) break;
1163
1164 auto next = pos;
1165 ++next;
1166 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1167 // Need to infill if next is disjoint
1168 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001169 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001170 next = action.Infill(accesses, next, new_range);
1171 }
1172 pos = next;
1173 }
1174}
1175
1176struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001177 using Iterator = ResourceAccessRangeMap::iterator;
1178 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001179 // this is only called on gaps, and never returns a gap.
1180 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001181 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001182 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001183 }
John Zulauf5f13a792020-03-10 07:31:21 -06001184
John Zulauf5c5e88d2019-12-26 11:22:02 -07001185 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001186 auto &access_state = pos->second;
1187 access_state.Update(usage, tag);
1188 return pos;
1189 }
1190
John Zulauf16adfc92020-04-08 10:28:33 -06001191 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001192 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001193 : type(type_), context(context_), usage(usage_), tag(tag_) {}
1194 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001195 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001196 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001197 const ResourceUsageTag &tag;
1198};
1199
John Zulauf89311b42020-09-29 16:28:47 -06001200// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1201// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1202class ApplyBarrierFunctor {
1203 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001204 using Iterator = ResourceAccessRangeMap::iterator;
1205 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001206
John Zulauf5c5e88d2019-12-26 11:22:02 -07001207 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001208 auto &access_state = pos->second;
John Zulauf89311b42020-09-29 16:28:47 -06001209 access_state.ApplyBarrier(barrier_, layout_transition_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001210 return pos;
1211 }
1212
John Zulauf89311b42020-09-29 16:28:47 -06001213 ApplyBarrierFunctor(const SyncBarrier &barrier, bool layout_transition)
1214 : barrier_(barrier), layout_transition_(layout_transition) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001215
John Zulauf89311b42020-09-29 16:28:47 -06001216 private:
1217 const SyncBarrier barrier_;
1218 const bool layout_transition_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001219};
1220
John Zulauf89311b42020-09-29 16:28:47 -06001221// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1222// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1223// of a collection is known/present.
1224class ApplyBarrierOpsFunctor {
1225 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001226 using Iterator = ResourceAccessRangeMap::iterator;
1227 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001228
John Zulauf89311b42020-09-29 16:28:47 -06001229 struct BarrierOp {
1230 SyncBarrier barrier;
1231 bool layout_transition;
1232 BarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1233 : barrier(barrier_), layout_transition(layout_transition_) {}
1234 BarrierOp() = default;
1235 };
John Zulauf5c5e88d2019-12-26 11:22:02 -07001236 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001237 auto &access_state = pos->second;
John Zulauf89311b42020-09-29 16:28:47 -06001238 for (const auto op : barrier_ops_) {
1239 access_state.ApplyBarrier(op.barrier, op.layout_transition);
1240 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001241
John Zulauf89311b42020-09-29 16:28:47 -06001242 if (resolve_) {
1243 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1244 // another walk
1245 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001246 }
1247 return pos;
1248 }
1249
John Zulauf89311b42020-09-29 16:28:47 -06001250 // A valid tag is required IFF any of the barriers ops are a layout transition, as transitions are write ops
1251 ApplyBarrierOpsFunctor(bool resolve, size_t size_hint, const ResourceUsageTag &tag)
1252 : resolve_(resolve), barrier_ops_(), tag_(tag) {
1253 if (size_hint) {
1254 barrier_ops_.reserve(size_hint);
1255 }
1256 };
1257
1258 // A valid tag is required IFF layout_transition is true, as transitions are write ops
1259 ApplyBarrierOpsFunctor(bool resolve, const std::vector<SyncBarrier> &barriers, bool layout_transition,
1260 const ResourceUsageTag &tag)
John Zulaufb02c1eb2020-10-06 16:33:36 -06001261 : resolve_(resolve), barrier_ops_(), tag_(tag) {
1262 barrier_ops_.reserve(barriers.size());
John Zulauf89311b42020-09-29 16:28:47 -06001263 for (const auto &barrier : barriers) {
1264 barrier_ops_.emplace_back(barrier, layout_transition);
John Zulauf9cb530d2019-09-30 14:14:10 -06001265 }
1266 }
1267
John Zulauf89311b42020-09-29 16:28:47 -06001268 void PushBack(const SyncBarrier &barrier, bool layout_transition) { barrier_ops_.emplace_back(barrier, layout_transition); }
1269
1270 void PushBack(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
1271 barrier_ops_.reserve(barrier_ops_.size() + barriers.size());
1272 for (const auto &barrier : barriers) {
1273 barrier_ops_.emplace_back(barrier, layout_transition);
1274 }
1275 }
1276
1277 private:
1278 bool resolve_;
1279 std::vector<BarrierOp> barrier_ops_;
1280 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001281};
1282
John Zulauf355e49b2020-04-24 15:11:15 -06001283void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1284 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001285 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1286 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001287}
1288
John Zulauf16adfc92020-04-08 10:28:33 -06001289void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001290 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001291 if (!SimpleBinding(buffer)) return;
1292 const auto base_address = ResourceBaseAddress(buffer);
1293 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1294}
John Zulauf355e49b2020-04-24 15:11:15 -06001295
John Zulauf540266b2020-04-06 18:54:53 -06001296void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001297 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001298 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001299 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001300 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001301 const auto address_type = ImageAddressType(image);
1302 const auto base_address = ResourceBaseAddress(image);
1303 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001304 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001305 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001306 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001307}
John Zulauf7635de32020-05-29 17:14:15 -06001308void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1309 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1310 if (view != nullptr) {
1311 const IMAGE_STATE *image = view->image_state.get();
1312 if (image != nullptr) {
1313 auto *update_range = &view->normalized_subresource_range;
1314 VkImageSubresourceRange masked_range;
1315 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1316 masked_range = view->normalized_subresource_range;
1317 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1318 update_range = &masked_range;
1319 }
1320 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1321 }
1322 }
1323}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001324
John Zulauf355e49b2020-04-24 15:11:15 -06001325void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1326 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1327 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001328 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1329 subresource.layerCount};
1330 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1331}
1332
John Zulauf540266b2020-04-06 18:54:53 -06001333template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001334void AccessContext::UpdateResourceAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001335 if (!SimpleBinding(buffer)) return;
1336 const auto base_address = ResourceBaseAddress(buffer);
1337 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001338}
1339
1340template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001341void AccessContext::UpdateResourceAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1342 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001343 if (!SimpleBinding(image)) return;
1344 const auto address_type = ImageAddressType(image);
1345 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001346
locke-lunargae26eac2020-04-16 15:29:05 -06001347 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001348 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001349
John Zulauf16adfc92020-04-08 10:28:33 -06001350 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001351 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001352 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001353 }
1354}
1355
John Zulauf7635de32020-05-29 17:14:15 -06001356void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1357 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1358 const ResourceUsageTag &tag) {
1359 UpdateStateResolveAction update(*this, tag);
1360 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1361}
1362
John Zulaufaff20662020-06-01 14:07:58 -06001363void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1364 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1365 const ResourceUsageTag &tag) {
1366 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1367 VkExtent3D extent = CastTo3D(render_area.extent);
1368 VkOffset3D offset = CastTo3D(render_area.offset);
1369
1370 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1371 if (rp_state.attachment_last_subpass[i] == subpass) {
1372 if (attachment_views[i] == nullptr) continue; // UNUSED
1373 const auto &view = *attachment_views[i];
1374 const IMAGE_STATE *image = view.image_state.get();
1375 if (image == nullptr) continue;
1376
1377 const auto &ci = attachment_ci[i];
1378 const bool has_depth = FormatHasDepth(ci.format);
1379 const bool has_stencil = FormatHasStencil(ci.format);
1380 const bool is_color = !(has_depth || has_stencil);
1381 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1382
1383 if (is_color && store_op_stores) {
1384 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1385 offset, extent, tag);
1386 } else {
1387 auto update_range = view.normalized_subresource_range;
1388 if (has_depth && store_op_stores) {
1389 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1390 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1391 tag);
1392 }
1393 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1394 if (has_stencil && stencil_op_stores) {
1395 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1396 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1397 tag);
1398 }
1399 }
1400 }
1401 }
1402}
1403
John Zulauf540266b2020-04-06 18:54:53 -06001404template <typename Action>
1405void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1406 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001407 for (const auto address_type : kAddressTypes) {
1408 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001409 }
1410}
1411
1412void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001413 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1414 auto &context = contexts[subpass_index];
John Zulaufb02c1eb2020-10-06 16:33:36 -06001415 ApplyTrackbackBarriersAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001416 for (const auto address_type : kAddressTypes) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001417 context.ResolveAccessRange(address_type, full_range, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001418 }
1419 }
1420}
1421
John Zulauf355e49b2020-04-24 15:11:15 -06001422// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001423HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001424 if (!attach_view) return HazardResult();
1425 const auto image_state = attach_view->image_state.get();
1426 if (!image_state) return HazardResult();
1427
John Zulauf355e49b2020-04-24 15:11:15 -06001428 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001429 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001430
1431 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001432 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1433 const auto merged_barrier = MergeBarriers(track_back.barriers);
1434 HazardResult hazard =
1435 track_back.context->DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
1436 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001437 if (!hazard.hazard) {
1438 // The Async hazard check is against the current context's async set.
John Zulaufa0a98292020-09-18 09:30:10 -06001439 hazard = DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001440 attach_view->normalized_subresource_range, kDetectAsync);
1441 }
John Zulaufa0a98292020-09-18 09:30:10 -06001442
John Zulauf355e49b2020-04-24 15:11:15 -06001443 return hazard;
1444}
1445
John Zulaufb02c1eb2020-10-06 16:33:36 -06001446void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
1447 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1448 const ResourceUsageTag &tag) {
1449 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001450 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001451 for (const auto &transition : transitions) {
1452 const auto prev_pass = transition.prev_pass;
1453 const auto attachment_view = attachment_views[transition.attachment];
1454 if (!attachment_view) continue;
1455 const auto *image = attachment_view->image_state.get();
1456 if (!image) continue;
1457 if (!SimpleBinding(*image)) continue;
1458
1459 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1460 assert(trackback);
1461
1462 // Import the attachments into the current context
1463 const auto *prev_context = trackback->context;
1464 assert(prev_context);
1465 const auto address_type = ImageAddressType(*image);
1466 auto &target_map = GetAccessStateMap(address_type);
1467 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
1468 prev_context->ResolveAccessRange(*image, attachment_view->normalized_subresource_range, barrier_action, address_type,
John Zulauf646cc292020-10-23 09:16:45 -06001469 &target_map, &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001470 }
1471
John Zulauf86356ca2020-10-19 11:46:41 -06001472 // If there were no transitions skip this global map walk
1473 if (transitions.size()) {
1474 ApplyBarrierOpsFunctor apply_pending_action(true /* resolve */, 0, tag);
1475 ApplyGlobalBarriers(apply_pending_action);
1476 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001477}
1478
John Zulauf355e49b2020-04-24 15:11:15 -06001479// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1480bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1481
1482 const VkRenderPassBeginInfo *pRenderPassBegin,
1483 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1484 const char *func_name) const {
1485 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1486 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06001487
John Zulauf86356ca2020-10-19 11:46:41 -06001488 assert(pRenderPassBegin);
1489 if (nullptr == pRenderPassBegin) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06001490
John Zulauf86356ca2020-10-19 11:46:41 -06001491 const uint32_t subpass = 0;
John Zulauf355e49b2020-04-24 15:11:15 -06001492
John Zulauf86356ca2020-10-19 11:46:41 -06001493 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
1494 // hasn't happened yet)
1495 const std::vector<AccessContext> empty_context_vector;
1496 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1497 const_cast<AccessContext *>(&cb_access_context_));
John Zulauf355e49b2020-04-24 15:11:15 -06001498
John Zulauf86356ca2020-10-19 11:46:41 -06001499 // Create a view list
1500 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1501 assert(fb_state);
1502 if (nullptr == fb_state) return skip;
1503 // NOTE: Must not use COMMAND_BUFFER_STATE variant of this as RecordCmdBeginRenderPass hasn't run and thus
1504 // the activeRenderPass.* fields haven't been set.
1505 const auto views = sync_state_->GetAttachmentViews(*pRenderPassBegin, *fb_state);
1506
1507 // Validate transitions
1508 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
1509
1510 // Validate load operations if there were no layout transition hazards
1511 if (!skip) {
1512 temp_context.RecordLayoutTransitions(rp_state, subpass, views, kCurrentCommandTag);
1513 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001514 }
John Zulauf86356ca2020-10-19 11:46:41 -06001515
John Zulauf355e49b2020-04-24 15:11:15 -06001516 return skip;
1517}
1518
locke-lunarg61870c22020-06-09 14:51:50 -06001519bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1520 const char *func_name) const {
1521 bool skip = false;
1522 const PIPELINE_STATE *pPipe = nullptr;
1523 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1524 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1525 if (!pPipe || !per_sets) {
1526 return skip;
1527 }
1528
1529 using DescriptorClass = cvdescriptorset::DescriptorClass;
1530 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1531 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1532 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1533 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1534
1535 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarg37047832020-06-12 13:44:45 -06001536 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001537 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1538 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001539 for (const auto &set_binding : stage_state.descriptor_uses) {
1540 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1541 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1542 set_binding.first.second);
1543 const auto descriptor_type = binding_it.GetType();
1544 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1545 auto array_idx = 0;
1546
1547 if (binding_it.IsVariableDescriptorCount()) {
1548 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1549 }
1550 SyncStageAccessIndex sync_index =
1551 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1552
1553 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1554 uint32_t index = i - index_range.start;
1555 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1556 switch (descriptor->GetClass()) {
1557 case DescriptorClass::ImageSampler:
1558 case DescriptorClass::Image: {
1559 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001560 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001561 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001562 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1563 img_view_state = image_sampler_descriptor->GetImageViewState();
1564 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001565 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001566 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1567 img_view_state = image_descriptor->GetImageViewState();
1568 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001569 }
1570 if (!img_view_state) continue;
1571 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1572 VkExtent3D extent = {};
1573 VkOffset3D offset = {};
1574 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1575 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1576 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1577 } else {
1578 extent = img_state->createInfo.extent;
1579 }
John Zulauf361fb532020-07-22 10:45:39 -06001580 HazardResult hazard;
1581 const auto &subresource_range = img_view_state->normalized_subresource_range;
1582 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1583 // Input attachments are subject to raster ordering rules
1584 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
1585 kAttachmentRasterOrder, offset, extent);
1586 } else {
1587 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1588 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001589 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001590 skip |= sync_state_->LogError(
1591 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001592 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1593 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001594 func_name, string_SyncHazard(hazard.hazard),
1595 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1596 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1597 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001598 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1599 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1600 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001601 }
1602 break;
1603 }
1604 case DescriptorClass::TexelBuffer: {
1605 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1606 if (!buf_view_state) continue;
1607 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001608 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001609 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001610 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001611 skip |= sync_state_->LogError(
1612 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001613 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1614 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001615 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1616 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1617 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001618 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1619 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1620 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001621 }
1622 break;
1623 }
1624 case DescriptorClass::GeneralBuffer: {
1625 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1626 auto buf_state = buffer_descriptor->GetBufferState();
1627 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001628 const ResourceAccessRange range =
1629 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001630 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001631 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001632 skip |= sync_state_->LogError(
1633 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001634 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1635 func_name, string_SyncHazard(hazard.hazard),
1636 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001637 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1638 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001639 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1640 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1641 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001642 }
1643 break;
1644 }
1645 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1646 default:
1647 break;
1648 }
1649 }
1650 }
1651 }
1652 return skip;
1653}
1654
1655void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1656 const ResourceUsageTag &tag) {
1657 const PIPELINE_STATE *pPipe = nullptr;
1658 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1659 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1660 if (!pPipe || !per_sets) {
1661 return;
1662 }
1663
1664 using DescriptorClass = cvdescriptorset::DescriptorClass;
1665 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1666 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1667 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1668 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1669
1670 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001671 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
1672 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1673 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001674 for (const auto &set_binding : stage_state.descriptor_uses) {
1675 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1676 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1677 set_binding.first.second);
1678 const auto descriptor_type = binding_it.GetType();
1679 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1680 auto array_idx = 0;
1681
1682 if (binding_it.IsVariableDescriptorCount()) {
1683 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1684 }
1685 SyncStageAccessIndex sync_index =
1686 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1687
1688 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1689 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1690 switch (descriptor->GetClass()) {
1691 case DescriptorClass::ImageSampler:
1692 case DescriptorClass::Image: {
1693 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1694 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1695 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1696 } else {
1697 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1698 }
1699 if (!img_view_state) continue;
1700 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1701 VkExtent3D extent = {};
1702 VkOffset3D offset = {};
1703 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1704 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1705 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1706 } else {
1707 extent = img_state->createInfo.extent;
1708 }
1709 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1710 offset, extent, tag);
1711 break;
1712 }
1713 case DescriptorClass::TexelBuffer: {
1714 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1715 if (!buf_view_state) continue;
1716 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001717 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001718 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1719 break;
1720 }
1721 case DescriptorClass::GeneralBuffer: {
1722 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1723 auto buf_state = buffer_descriptor->GetBufferState();
1724 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001725 const ResourceAccessRange range =
1726 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001727 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1728 break;
1729 }
1730 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1731 default:
1732 break;
1733 }
1734 }
1735 }
1736 }
1737}
1738
1739bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1740 bool skip = false;
1741 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1742 if (!pPipe) {
1743 return skip;
1744 }
1745
1746 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1747 const auto &binding_buffers_size = binding_buffers.size();
1748 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1749
1750 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1751 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1752 if (binding_description.binding < binding_buffers_size) {
1753 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07001754 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001755
locke-lunarg1ae57d62020-11-18 10:49:19 -07001756 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001757 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1758 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001759 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1760 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001761 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001762 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 -06001763 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001764 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001765 }
1766 }
1767 }
1768 return skip;
1769}
1770
1771void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
1772 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1773 if (!pPipe) {
1774 return;
1775 }
1776 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1777 const auto &binding_buffers_size = binding_buffers.size();
1778 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1779
1780 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1781 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1782 if (binding_description.binding < binding_buffers_size) {
1783 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07001784 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001785
locke-lunarg1ae57d62020-11-18 10:49:19 -07001786 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001787 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1788 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001789 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1790 }
1791 }
1792}
1793
1794bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1795 bool skip = false;
locke-lunarg1ae57d62020-11-18 10:49:19 -07001796 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed)
1797 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001798
locke-lunarg1ae57d62020-11-18 10:49:19 -07001799 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001800 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001801 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1802 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001803 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1804 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001805 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001806 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 -06001807 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001808 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001809 }
1810
1811 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1812 // We will detect more accurate range in the future.
1813 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1814 return skip;
1815}
1816
1817void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07001818 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 -06001819
locke-lunarg1ae57d62020-11-18 10:49:19 -07001820 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001821 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001822 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1823 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001824 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1825
1826 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1827 // We will detect more accurate range in the future.
1828 RecordDrawVertex(UINT32_MAX, 0, tag);
1829}
1830
1831bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06001832 bool skip = false;
1833 if (!current_renderpass_context_) return skip;
1834 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1835 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1836 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001837}
1838
1839void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
locke-lunarg7077d502020-06-18 21:37:26 -06001840 if (current_renderpass_context_)
1841 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1842 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001843}
1844
John Zulauf355e49b2020-04-24 15:11:15 -06001845bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001846 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001847 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06001848 skip |=
1849 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001850
1851 return skip;
1852}
1853
1854bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1855 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001856 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001857 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001858 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06001859 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1860 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001861
1862 return skip;
1863}
1864
1865void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1866 assert(sync_state_);
1867 if (!cb_state_) return;
1868
1869 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06001870 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06001871 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06001872 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001873 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001874}
1875
John Zulauf355e49b2020-04-24 15:11:15 -06001876void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001877 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001878 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001879 current_context_ = &current_renderpass_context_->CurrentContext();
1880}
1881
John Zulauf355e49b2020-04-24 15:11:15 -06001882void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001883 assert(current_renderpass_context_);
1884 if (!current_renderpass_context_) return;
1885
John Zulauf1a224292020-06-30 14:52:13 -06001886 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001887 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001888 current_renderpass_context_ = nullptr;
1889}
1890
locke-lunarg61870c22020-06-09 14:51:50 -06001891bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1892 const VkRect2D &render_area, const char *func_name) const {
1893 bool skip = false;
locke-lunarg96dc9632020-06-10 17:22:18 -06001894 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001895 if (!pPipe ||
1896 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001897 return skip;
1898 }
1899 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001900 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1901 VkExtent3D extent = CastTo3D(render_area.extent);
1902 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001903
John Zulauf1a224292020-06-30 14:52:13 -06001904 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001905 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001906 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1907 for (const auto location : list) {
1908 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1909 continue;
1910 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001911 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1912 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06001913 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001914 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001915 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001916 func_name, string_SyncHazard(hazard.hazard),
1917 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1918 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001919 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001920 }
1921 }
1922 }
locke-lunarg37047832020-06-12 13:44:45 -06001923
1924 // PHASE1 TODO: Add layout based read/vs. write selection.
1925 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1926 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1927 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001928 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001929 bool depth_write = false, stencil_write = false;
1930
1931 // PHASE1 TODO: These validation should be in core_checks.
1932 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1933 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1934 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1935 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1936 depth_write = true;
1937 }
1938 // PHASE1 TODO: It needs to check if stencil is writable.
1939 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1940 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1941 // PHASE1 TODO: These validation should be in core_checks.
1942 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1943 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1944 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1945 stencil_write = true;
1946 }
1947
1948 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1949 if (depth_write) {
1950 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001951 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1952 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001953 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001954 skip |= sync_state.LogError(
1955 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001956 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001957 func_name, string_SyncHazard(hazard.hazard),
1958 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1959 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001960 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001961 }
1962 }
1963 if (stencil_write) {
1964 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001965 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1966 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001967 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001968 skip |= sync_state.LogError(
1969 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001970 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001971 func_name, string_SyncHazard(hazard.hazard),
1972 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1973 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001974 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001975 }
locke-lunarg61870c22020-06-09 14:51:50 -06001976 }
1977 }
1978 return skip;
1979}
1980
locke-lunarg96dc9632020-06-10 17:22:18 -06001981void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1982 const ResourceUsageTag &tag) {
1983 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001984 if (!pPipe ||
1985 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001986 return;
1987 }
1988 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001989 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1990 VkExtent3D extent = CastTo3D(render_area.extent);
1991 VkOffset3D offset = CastTo3D(render_area.offset);
1992
John Zulauf1a224292020-06-30 14:52:13 -06001993 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001994 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001995 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1996 for (const auto location : list) {
1997 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1998 continue;
1999 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002000 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
2001 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002002 }
2003 }
locke-lunarg37047832020-06-12 13:44:45 -06002004
2005 // PHASE1 TODO: Add layout based read/vs. write selection.
2006 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
2007 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
2008 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002009 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002010 bool depth_write = false, stencil_write = false;
2011
2012 // PHASE1 TODO: These validation should be in core_checks.
2013 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
2014 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2015 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
2016 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2017 depth_write = true;
2018 }
2019 // PHASE1 TODO: It needs to check if stencil is writable.
2020 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2021 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2022 // PHASE1 TODO: These validation should be in core_checks.
2023 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
2024 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
2025 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2026 stencil_write = true;
2027 }
2028
2029 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2030 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002031 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2032 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002033 }
2034 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002035 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2036 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002037 }
locke-lunarg61870c22020-06-09 14:51:50 -06002038 }
2039}
2040
John Zulauf1507ee42020-05-18 11:33:09 -06002041bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
2042 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002043 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002044 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06002045 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2046 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002047 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2048 func_name);
2049
John Zulauf355e49b2020-04-24 15:11:15 -06002050 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002051 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06002052 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002053 if (!skip) {
2054 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2055 // on a copy of the (empty) next context.
2056 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2057 AccessContext temp_context(next_context);
2058 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
2059 skip |= temp_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
2060 }
John Zulauf7635de32020-05-29 17:14:15 -06002061 return skip;
2062}
2063bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
2064 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002065 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002066 bool skip = false;
2067 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2068 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002069 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2070 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06002071 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002072 return skip;
2073}
2074
John Zulauf7635de32020-05-29 17:14:15 -06002075AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2076 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2077}
2078
2079bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2080 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002081 bool skip = false;
2082
John Zulauf7635de32020-05-29 17:14:15 -06002083 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2084 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2085 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2086 // to apply and only copy then, if this proves a hot spot.
2087 std::unique_ptr<AccessContext> proxy_for_current;
2088
John Zulauf355e49b2020-04-24 15:11:15 -06002089 // Validate the "finalLayout" transitions to external
2090 // Get them from where there we're hidding in the extra entry.
2091 const auto &final_transitions = rp_state_->subpass_transitions.back();
2092 for (const auto &transition : final_transitions) {
2093 const auto &attach_view = attachment_views_[transition.attachment];
2094 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2095 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002096 auto *context = trackback.context;
2097
2098 if (transition.prev_pass == current_subpass_) {
2099 if (!proxy_for_current) {
2100 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2101 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2102 }
2103 context = proxy_for_current.get();
2104 }
2105
John Zulaufa0a98292020-09-18 09:30:10 -06002106 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2107 const auto merged_barrier = MergeBarriers(trackback.barriers);
2108 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope,
2109 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2110 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002111 if (hazard.hazard) {
2112 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2113 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf389c34b2020-07-28 11:19:35 -06002114 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002115 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf389c34b2020-07-28 11:19:35 -06002116 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf37ceaed2020-07-03 16:18:15 -06002117 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002118 }
2119 }
2120 return skip;
2121}
2122
2123void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2124 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002125 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002126}
2127
John Zulauf1507ee42020-05-18 11:33:09 -06002128void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2129 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2130 auto &subpass_context = subpass_contexts_[current_subpass_];
2131 VkExtent3D extent = CastTo3D(render_area.extent);
2132 VkOffset3D offset = CastTo3D(render_area.offset);
2133
2134 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2135 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2136 if (attachment_views_[i] == nullptr) continue; // UNUSED
2137 const auto &view = *attachment_views_[i];
2138 const IMAGE_STATE *image = view.image_state.get();
2139 if (image == nullptr) continue;
2140
2141 const auto &ci = attachment_ci[i];
2142 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002143 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002144 const bool is_color = !(has_depth || has_stencil);
2145
2146 if (is_color) {
2147 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2148 extent, tag);
2149 } else {
2150 auto update_range = view.normalized_subresource_range;
2151 if (has_depth) {
2152 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2153 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2154 }
2155 if (has_stencil) {
2156 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2157 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2158 tag);
2159 }
2160 }
2161 }
2162 }
2163}
2164
John Zulauf355e49b2020-04-24 15:11:15 -06002165void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002166 const AccessContext *external_context, VkQueueFlags queue_flags,
2167 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002168 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002169 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002170 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2171 // Add this for all subpasses here so that they exsist during next subpass validation
2172 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002173 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002174 }
2175 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2176
2177 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());
2188 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002189 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002190}
2191
John Zulauf1a224292020-06-30 14:52:13 -06002192void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2193 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002194 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002195 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002196 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002197
John Zulauf355e49b2020-04-24 15:11:15 -06002198 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002199 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002200
2201 // Add the "finalLayout" transitions to external
2202 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002203 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2204 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2205 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002206 const auto &final_transitions = rp_state_->subpass_transitions.back();
2207 for (const auto &transition : final_transitions) {
2208 const auto &attachment = attachment_views_[transition.attachment];
2209 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002210 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf89311b42020-09-29 16:28:47 -06002211 ApplyBarrierOpsFunctor barrier_ops(true /* resolve */, last_trackback.barriers, true /* layout transition */, tag);
2212 external_context->UpdateResourceAccess(*attachment->image_state, attachment->normalized_subresource_range, barrier_ops);
John Zulauf355e49b2020-04-24 15:11:15 -06002213 }
2214}
2215
John Zulauf3d84f1b2020-03-09 13:33:25 -06002216SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2217 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2218 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2219 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2220 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2221 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2222 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2223}
2224
John Zulaufb02c1eb2020-10-06 16:33:36 -06002225// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2226void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2227 for (const auto &barrier : barriers) {
2228 ApplyBarrier(barrier, layout_transition);
2229 }
2230}
2231
John Zulauf89311b42020-09-29 16:28:47 -06002232// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2233// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2234// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002235void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2236 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002237 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002238 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002239 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002240 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002241 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002242 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002243}
John Zulauf9cb530d2019-09-30 14:14:10 -06002244HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2245 HazardResult hazard;
2246 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002247 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002248 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002249 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002250 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002251 }
2252 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002253 // Write operation:
2254 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2255 // If reads exists -- test only against them because either:
2256 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2257 // * 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
2258 // the current write happens after the reads, so just test the write against the reades
2259 // Otherwise test against last_write
2260 //
2261 // Look for casus belli for WAR
2262 if (last_read_count) {
2263 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2264 const auto &read_access = last_reads[read_index];
2265 if (IsReadHazard(usage_stage, read_access)) {
2266 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2267 break;
2268 }
2269 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002270 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002271 // Write-After-Write check -- if we have a previous write to test against
2272 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002273 }
2274 }
2275 return hazard;
2276}
2277
John Zulauf69133422020-05-20 14:55:53 -06002278HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2279 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2280 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002281 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002282 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002283 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2284 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002285 if (IsRead(usage_bit)) {
2286 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2287 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2288 if (is_raw_hazard) {
2289 // NOTE: we know last_write is non-zero
2290 // See if the ordering rules save us from the simple RAW check above
2291 // First check to see if the current usage is covered by the ordering rules
2292 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2293 const bool usage_is_ordered =
2294 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2295 if (usage_is_ordered) {
2296 // Now see of the most recent write (or a subsequent read) are ordered
2297 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2298 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002299 }
2300 }
John Zulauf4285ee92020-09-23 10:20:52 -06002301 if (is_raw_hazard) {
2302 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2303 }
John Zulauf361fb532020-07-22 10:45:39 -06002304 } else {
2305 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002306 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulauf361fb532020-07-22 10:45:39 -06002307 if (last_read_count) {
John Zulauf361fb532020-07-22 10:45:39 -06002308 // Look for any WAR hazards outside the ordered set of stages
John Zulauf4285ee92020-09-23 10:20:52 -06002309 VkPipelineStageFlags ordered_stages = 0;
2310 if (usage_write_is_ordered) {
2311 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2312 ordered_stages = GetOrderedStages(ordering);
2313 }
2314 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2315 if ((ordered_stages & last_read_stages) != last_read_stages) {
2316 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2317 const auto &read_access = last_reads[read_index];
2318 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2319 if (IsReadHazard(usage_stage, read_access)) {
2320 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2321 break;
2322 }
John Zulaufd14743a2020-07-03 09:42:39 -06002323 }
2324 }
John Zulauf4285ee92020-09-23 10:20:52 -06002325 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002326 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002327 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002328 }
John Zulauf69133422020-05-20 14:55:53 -06002329 }
2330 }
2331 return hazard;
2332}
2333
John Zulauf2f952d22020-02-10 11:34:51 -07002334// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06002335HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002336 HazardResult hazard;
2337 auto usage = FlagBit(usage_index);
2338 if (IsRead(usage)) {
2339 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002340 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002341 }
2342 } else {
2343 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002344 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002345 } else if (last_read_count > 0) {
John Zulauf4285ee92020-09-23 10:20:52 -06002346 // Any read could be reported, so we'll just pick the first one arbitrarily
John Zulauf59e25072020-07-17 10:55:21 -06002347 hazard.Set(this, usage_index, WRITE_RACING_READ, last_reads[0].access, last_reads[0].tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002348 }
2349 }
2350 return hazard;
2351}
2352
John Zulauf36bcf6a2020-02-03 15:12:52 -07002353HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002354 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002355 // Only supporting image layout transitions for now
2356 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2357 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002358 // only test for WAW if there no intervening read operations.
2359 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2360 if (last_read_count) {
John Zulauf355e49b2020-04-24 15:11:15 -06002361 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002362 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002363 const auto &read_access = last_reads[read_index];
2364 // If the read stage is not in the src sync sync
2365 // *AND* not execution chained with an existing sync barrier (that's the or)
2366 // then the barrier access is unsafe (R/W after R)
2367 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002368 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002369 break;
2370 }
2371 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002372 } else if (last_write.any()) {
John Zulauf361fb532020-07-22 10:45:39 -06002373 // If the previous write is *not* in the 1st access scope
2374 // *AND* the current barrier is not in the dependency chain
2375 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2376 // then the barrier access is unsafe (R/W after W)
2377 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
2378 // TODO: Do we need a difference hazard name for this?
2379 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2380 }
John Zulaufd14743a2020-07-03 09:42:39 -06002381 }
John Zulauf361fb532020-07-22 10:45:39 -06002382
John Zulauf0cb5be22020-01-23 12:18:22 -07002383 return hazard;
2384}
2385
John Zulauf5f13a792020-03-10 07:31:21 -06002386// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2387// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2388// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2389void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2390 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002391 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2392 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002393 *this = other;
2394 } else if (!other.write_tag.IsBefore(write_tag)) {
2395 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2396 // dependency chaining logic or any stage expansion)
2397 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002398 pending_write_barriers |= other.pending_write_barriers;
2399 pending_layout_transition |= other.pending_layout_transition;
2400 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002401
John Zulaufd14743a2020-07-03 09:42:39 -06002402 // Merge the read states
John Zulauf4285ee92020-09-23 10:20:52 -06002403 const auto pre_merge_count = last_read_count;
2404 const auto pre_merge_stages = last_read_stages;
John Zulauf5f13a792020-03-10 07:31:21 -06002405 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2406 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002407 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002408 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002409 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2410 // but we should wait on profiling data for that.
2411 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002412 auto &my_read = last_reads[my_read_index];
2413 if (other_read.stage == my_read.stage) {
2414 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002415 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002416 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002417 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002418 my_read.pending_dep_chain = other_read.pending_dep_chain;
2419 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
2420 // May require tracking more than one access per stage.
2421 my_read.barriers = other_read.barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002422 if (my_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
2423 // Since I'm overwriting the fragement stage read, also update the input attachment info
2424 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06002425 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002426 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002427 } else if (other_read.tag.IsBefore(my_read.tag)) {
2428 // The read tags match so merge the barriers
2429 my_read.barriers |= other_read.barriers;
2430 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002431 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002432
John Zulauf5f13a792020-03-10 07:31:21 -06002433 break;
2434 }
2435 }
2436 } else {
2437 // The other read stage doesn't exist in this, so add it.
2438 last_reads[last_read_count] = other_read;
2439 last_read_count++;
2440 last_read_stages |= other_read.stage;
John Zulauf4285ee92020-09-23 10:20:52 -06002441 if (other_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06002442 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002443 }
John Zulauf5f13a792020-03-10 07:31:21 -06002444 }
2445 }
John Zulauf361fb532020-07-22 10:45:39 -06002446 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002447 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2448 // ignore it.
John Zulauf5f13a792020-03-10 07:31:21 -06002449}
2450
John Zulauf9cb530d2019-09-30 14:14:10 -06002451void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2452 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2453 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002454 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002455 // Mulitple outstanding reads may be of interest and do dependency chains independently
2456 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2457 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002458 uint32_t update_index = kStageCount;
John Zulauf9cb530d2019-09-30 14:14:10 -06002459 if (usage_stage & last_read_stages) {
2460 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf4285ee92020-09-23 10:20:52 -06002461 if (last_reads[read_index].stage == usage_stage) {
2462 update_index = read_index;
John Zulauf9cb530d2019-09-30 14:14:10 -06002463 break;
2464 }
2465 }
John Zulauf4285ee92020-09-23 10:20:52 -06002466 assert(update_index < last_read_count);
John Zulauf9cb530d2019-09-30 14:14:10 -06002467 } else {
John Zulauf9cb530d2019-09-30 14:14:10 -06002468 assert(last_read_count < last_reads.size());
John Zulauf4285ee92020-09-23 10:20:52 -06002469 update_index = last_read_count++;
John Zulauf9cb530d2019-09-30 14:14:10 -06002470 last_read_stages |= usage_stage;
2471 }
John Zulauf4285ee92020-09-23 10:20:52 -06002472 last_reads[update_index].Set(usage_stage, usage_bit, 0, tag);
2473
2474 // Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
2475 if (usage_stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06002476 // TODO Revisit re: multiple reads for a given stage
2477 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002478 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002479 } else {
2480 // Assume write
2481 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002482 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002483 }
2484}
John Zulauf5f13a792020-03-10 07:31:21 -06002485
John Zulauf89311b42020-09-29 16:28:47 -06002486// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2487// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2488// We can overwrite them as *this* write is now after them.
2489//
2490// 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 -07002491void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulauf89311b42020-09-29 16:28:47 -06002492 last_read_count = 0;
2493 last_read_stages = 0;
2494 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06002495 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06002496
2497 write_barriers = 0;
2498 write_dependency_chain = 0;
2499 write_tag = tag;
2500 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002501}
2502
John Zulauf89311b42020-09-29 16:28:47 -06002503// Apply the memory barrier without updating the existing barriers. The execution barrier
2504// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2505// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2506// replace the current write barriers or add to them, so accumulate to pending as well.
2507void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2508 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2509 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06002510 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
2511 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
2512 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
2513 // transistion *as* a write and in scope with the barrier (it's before visibility).
2514 if (layout_transition || InSourceScopeOrChain(barrier.src_exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06002515 pending_write_barriers |= barrier.dst_access_scope;
2516 pending_write_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002517 }
John Zulauf89311b42020-09-29 16:28:47 -06002518 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2519 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06002520
John Zulauf89311b42020-09-29 16:28:47 -06002521 if (!pending_layout_transition) {
2522 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2523 // don't need to be tracked as we're just going to zero them.
John Zulaufa0a98292020-09-18 09:30:10 -06002524 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf89311b42020-09-29 16:28:47 -06002525 ReadState &access = last_reads[read_index];
2526 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2527 if (barrier.src_exec_scope & (access.stage | access.barriers)) {
2528 access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002529 }
2530 }
John Zulaufa0a98292020-09-18 09:30:10 -06002531 }
John Zulaufa0a98292020-09-18 09:30:10 -06002532}
2533
John Zulauf89311b42020-09-29 16:28:47 -06002534void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
2535 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06002536 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
2537 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
2538 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06002539 }
John Zulauf89311b42020-09-29 16:28:47 -06002540
2541 // Apply the accumulate execution barriers (and thus update chaining information)
2542 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
2543 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2544 ReadState &access = last_reads[read_index];
2545 access.barriers |= access.pending_dep_chain;
2546 read_execution_barriers |= access.barriers;
2547 access.pending_dep_chain = 0;
2548 }
2549
2550 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
2551 write_dependency_chain |= pending_write_dep_chain;
2552 write_barriers |= pending_write_barriers;
2553 pending_write_dep_chain = 0;
2554 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002555}
2556
John Zulauf59e25072020-07-17 10:55:21 -06002557// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002558VkPipelineStageFlags ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
John Zulauf59e25072020-07-17 10:55:21 -06002559 VkPipelineStageFlags barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06002560
2561 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2562 const auto &read_access = last_reads[read_index];
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002563 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06002564 barriers = read_access.barriers;
2565 break;
John Zulauf59e25072020-07-17 10:55:21 -06002566 }
2567 }
John Zulauf4285ee92020-09-23 10:20:52 -06002568
John Zulauf59e25072020-07-17 10:55:21 -06002569 return barriers;
2570}
2571
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002572inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlagBits usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06002573 assert(IsRead(usage));
2574 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
2575 // * the previous reads are not hazards, and thus last_write must be visible and available to
2576 // any reads that happen after.
2577 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2578 // 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 -07002579 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06002580}
2581
John Zulauf4285ee92020-09-23 10:20:52 -06002582VkPipelineStageFlags ResourceAccessState::GetOrderedStages(const SyncOrderingBarrier &ordering) const {
2583 // Whether the stage are in the ordering scope only matters if the current write is ordered
2584 VkPipelineStageFlags ordered_stages = last_read_stages & ordering.exec_scope;
2585 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002586 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06002587 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06002588 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
2589 ordered_stages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
2590 }
2591
2592 return ordered_stages;
2593}
2594
2595inline ResourceAccessState::ReadState *ResourceAccessState::GetReadStateForStage(VkPipelineStageFlagBits stage,
2596 uint32_t search_limit) {
2597 ReadState *read_state = nullptr;
2598 search_limit = std::min(search_limit, last_read_count);
2599 for (uint32_t i = 0; i < search_limit; i++) {
2600 if (last_reads[i].stage == stage) {
2601 read_state = &last_reads[i];
2602 break;
2603 }
2604 }
2605 return read_state;
2606}
2607
John Zulaufd1f85d42020-04-15 12:23:15 -06002608void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002609 auto *access_context = GetAccessContextNoInsert(command_buffer);
2610 if (access_context) {
2611 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002612 }
2613}
2614
John Zulaufd1f85d42020-04-15 12:23:15 -06002615void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2616 auto access_found = cb_access_state.find(command_buffer);
2617 if (access_found != cb_access_state.end()) {
2618 access_found->second->Reset();
2619 cb_access_state.erase(access_found);
2620 }
2621}
2622
John Zulauf89311b42020-09-29 16:28:47 -06002623void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2624 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags src_access_scope,
2625 SyncStageAccessFlags dst_access_scope, uint32_t memory_barrier_count,
2626 const VkMemoryBarrier *pMemoryBarriers, const ResourceUsageTag &tag) {
2627 ApplyBarrierOpsFunctor barriers_functor(true /* resolve */, std::min<uint32_t>(1, memory_barrier_count), tag);
2628 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
2629 const auto &barrier = pMemoryBarriers[barrier_index];
2630 SyncBarrier sync_barrier(src_exec_scope, SyncStageAccess::AccessScope(src_access_scope, barrier.srcAccessMask),
2631 dst_exec_scope, SyncStageAccess::AccessScope(dst_access_scope, barrier.dstAccessMask));
2632 barriers_functor.PushBack(sync_barrier, false);
2633 }
2634 if (0 == memory_barrier_count) {
2635 // If there are no global memory barriers, force an exec barrier
2636 barriers_functor.PushBack(SyncBarrier(src_exec_scope, 0, dst_exec_scope, 0), false);
2637 }
John Zulauf540266b2020-04-06 18:54:53 -06002638 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002639}
2640
John Zulauf540266b2020-04-06 18:54:53 -06002641void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002642 const SyncStageAccessFlags &src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2643 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002644 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002645 for (uint32_t index = 0; index < barrier_count; index++) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002646 auto barrier = barriers[index]; // barrier is a copy
John Zulauf9cb530d2019-09-30 14:14:10 -06002647 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2648 if (!buffer) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002649 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
2650 const ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002651 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2652 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06002653 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2654 const ApplyBarrierFunctor update_action(sync_barrier, false /* layout_transition */);
2655 context->UpdateResourceAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002656 }
2657}
2658
John Zulauf540266b2020-04-06 18:54:53 -06002659void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002660 const SyncStageAccessFlags &src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2661 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002662 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002663 for (uint32_t index = 0; index < barrier_count; index++) {
2664 const auto &barrier = barriers[index];
2665 const auto *image = Get<IMAGE_STATE>(barrier.image);
2666 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002667 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002668 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2669 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2670 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06002671 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2672 const ApplyBarrierFunctor barrier_action(sync_barrier, layout_transition);
2673 context->UpdateResourceAccess(*image, subresource_range, barrier_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002674 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002675}
2676
2677bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2678 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2679 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002680 const auto *cb_context = GetAccessContext(commandBuffer);
2681 assert(cb_context);
2682 if (!cb_context) return skip;
2683 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002684
John Zulauf3d84f1b2020-03-09 13:33:25 -06002685 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002686 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002687 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002688
2689 for (uint32_t region = 0; region < regionCount; region++) {
2690 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002691 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002692 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002693 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002694 if (hazard.hazard) {
2695 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002696 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002697 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002698 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002699 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002700 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002701 }
John Zulauf16adfc92020-04-08 10:28:33 -06002702 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002703 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06002704 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002705 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002706 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002707 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002708 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002709 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002710 }
2711 }
2712 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002713 }
2714 return skip;
2715}
2716
2717void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2718 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002719 auto *cb_context = GetAccessContext(commandBuffer);
2720 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002721 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002722 auto *context = cb_context->GetCurrentAccessContext();
2723
John Zulauf9cb530d2019-09-30 14:14:10 -06002724 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002725 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002726
2727 for (uint32_t region = 0; region < regionCount; region++) {
2728 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002729 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002730 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002731 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002732 }
John Zulauf16adfc92020-04-08 10:28:33 -06002733 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002734 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002735 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002736 }
2737 }
2738}
2739
Jeff Leger178b1e52020-10-05 12:22:23 -04002740bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
2741 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
2742 bool skip = false;
2743 const auto *cb_context = GetAccessContext(commandBuffer);
2744 assert(cb_context);
2745 if (!cb_context) return skip;
2746 const auto *context = cb_context->GetCurrentAccessContext();
2747
2748 // If we have no previous accesses, we have no hazards
2749 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2750 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2751
2752 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2753 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2754 if (src_buffer) {
2755 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2756 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
2757 if (hazard.hazard) {
2758 // TODO -- add tag information to log msg when useful.
2759 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
2760 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
2761 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
2762 region, string_UsageTag(hazard).c_str());
2763 }
2764 }
2765 if (dst_buffer && !skip) {
2766 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2767 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
2768 if (hazard.hazard) {
2769 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
2770 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
2771 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
2772 region, string_UsageTag(hazard).c_str());
2773 }
2774 }
2775 if (skip) break;
2776 }
2777 return skip;
2778}
2779
2780void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
2781 auto *cb_context = GetAccessContext(commandBuffer);
2782 assert(cb_context);
2783 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
2784 auto *context = cb_context->GetCurrentAccessContext();
2785
2786 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2787 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2788
2789 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2790 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2791 if (src_buffer) {
2792 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2793 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
2794 }
2795 if (dst_buffer) {
2796 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2797 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
2798 }
2799 }
2800}
2801
John Zulauf5c5e88d2019-12-26 11:22:02 -07002802bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2803 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2804 const VkImageCopy *pRegions) const {
2805 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002806 const auto *cb_access_context = GetAccessContext(commandBuffer);
2807 assert(cb_access_context);
2808 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002809
John Zulauf3d84f1b2020-03-09 13:33:25 -06002810 const auto *context = cb_access_context->GetCurrentAccessContext();
2811 assert(context);
2812 if (!context) return skip;
2813
2814 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2815 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002816 for (uint32_t region = 0; region < regionCount; region++) {
2817 const auto &copy_region = pRegions[region];
2818 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002819 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002820 copy_region.srcOffset, copy_region.extent);
2821 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002822 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002823 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002824 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002825 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002826 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002827 }
2828
2829 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002830 VkExtent3D dst_copy_extent =
2831 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002832 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002833 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002834 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002835 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002836 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002837 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002838 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002839 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002840 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002841 }
2842 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002843
John Zulauf5c5e88d2019-12-26 11:22:02 -07002844 return skip;
2845}
2846
2847void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2848 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2849 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002850 auto *cb_access_context = GetAccessContext(commandBuffer);
2851 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002852 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002853 auto *context = cb_access_context->GetCurrentAccessContext();
2854 assert(context);
2855
John Zulauf5c5e88d2019-12-26 11:22:02 -07002856 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002857 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002858
2859 for (uint32_t region = 0; region < regionCount; region++) {
2860 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002861 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002862 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2863 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002864 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002865 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002866 VkExtent3D dst_copy_extent =
2867 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002868 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2869 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002870 }
2871 }
2872}
2873
Jeff Leger178b1e52020-10-05 12:22:23 -04002874bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
2875 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
2876 bool skip = false;
2877 const auto *cb_access_context = GetAccessContext(commandBuffer);
2878 assert(cb_access_context);
2879 if (!cb_access_context) return skip;
2880
2881 const auto *context = cb_access_context->GetCurrentAccessContext();
2882 assert(context);
2883 if (!context) return skip;
2884
2885 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2886 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2887 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2888 const auto &copy_region = pCopyImageInfo->pRegions[region];
2889 if (src_image) {
2890 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
2891 copy_region.srcOffset, copy_region.extent);
2892 if (hazard.hazard) {
2893 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
2894 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
2895 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
2896 region, string_UsageTag(hazard).c_str());
2897 }
2898 }
2899
2900 if (dst_image) {
2901 VkExtent3D dst_copy_extent =
2902 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2903 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
2904 copy_region.dstOffset, dst_copy_extent);
2905 if (hazard.hazard) {
2906 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
2907 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
2908 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
2909 region, string_UsageTag(hazard).c_str());
2910 }
2911 if (skip) break;
2912 }
2913 }
2914
2915 return skip;
2916}
2917
2918void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
2919 auto *cb_access_context = GetAccessContext(commandBuffer);
2920 assert(cb_access_context);
2921 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
2922 auto *context = cb_access_context->GetCurrentAccessContext();
2923 assert(context);
2924
2925 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2926 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2927
2928 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2929 const auto &copy_region = pCopyImageInfo->pRegions[region];
2930 if (src_image) {
2931 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2932 copy_region.extent, tag);
2933 }
2934 if (dst_image) {
2935 VkExtent3D dst_copy_extent =
2936 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2937 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2938 dst_copy_extent, tag);
2939 }
2940 }
2941}
2942
John Zulauf9cb530d2019-09-30 14:14:10 -06002943bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2944 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2945 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2946 uint32_t bufferMemoryBarrierCount,
2947 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2948 uint32_t imageMemoryBarrierCount,
2949 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2950 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002951 const auto *cb_access_context = GetAccessContext(commandBuffer);
2952 assert(cb_access_context);
2953 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002954
John Zulauf3d84f1b2020-03-09 13:33:25 -06002955 const auto *context = cb_access_context->GetCurrentAccessContext();
2956 assert(context);
2957 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002958
John Zulauf3d84f1b2020-03-09 13:33:25 -06002959 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002960 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2961 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002962 // Validate Image Layout transitions
2963 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2964 const auto &barrier = pImageMemoryBarriers[index];
2965 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2966 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2967 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002968 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002969 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002970 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002971 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002972 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002973 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002974 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002975 }
2976 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002977
2978 return skip;
2979}
2980
2981void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2982 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2983 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2984 uint32_t bufferMemoryBarrierCount,
2985 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2986 uint32_t imageMemoryBarrierCount,
2987 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002988 auto *cb_access_context = GetAccessContext(commandBuffer);
2989 assert(cb_access_context);
2990 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002991 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002992 auto access_context = cb_access_context->GetCurrentAccessContext();
2993 assert(access_context);
2994 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06002995
John Zulauf3d84f1b2020-03-09 13:33:25 -06002996 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002997 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002998 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002999 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
3000 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
3001 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf89311b42020-09-29 16:28:47 -06003002
3003 // These two apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
3004 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
3005 // of the barriers is maintained.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003006 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
3007 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06003008 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06003009 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003010
John Zulauf89311b42020-09-29 16:28:47 -06003011 // Apply the global barriers last as is it walks all memory, it can also clean up the "pending" state without requiring an
3012 // additional pass, updating the dependency chains *last* as it goes along.
3013 // This is needed to guarantee order independence of the three lists.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003014 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf89311b42020-09-29 16:28:47 -06003015 pMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003016}
3017
3018void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3019 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3020 // The state tracker sets up the device state
3021 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3022
John Zulauf5f13a792020-03-10 07:31:21 -06003023 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3024 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003025 // TODO: Find a good way to do this hooklessly.
3026 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3027 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3028 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3029
John Zulaufd1f85d42020-04-15 12:23:15 -06003030 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3031 sync_device_state->ResetCommandBufferCallback(command_buffer);
3032 });
3033 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3034 sync_device_state->FreeCommandBufferCallback(command_buffer);
3035 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003036}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003037
John Zulauf355e49b2020-04-24 15:11:15 -06003038bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3039 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
3040 bool skip = false;
3041 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
3042 auto cb_context = GetAccessContext(commandBuffer);
3043
3044 if (rp_state && cb_context) {
3045 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
3046 }
3047
3048 return skip;
3049}
3050
3051bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3052 VkSubpassContents contents) const {
3053 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3054 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3055 subpass_begin_info.contents = contents;
3056 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
3057 return skip;
3058}
3059
3060bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3061 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
3062 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3063 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
3064 return skip;
3065}
3066
3067bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3068 const VkRenderPassBeginInfo *pRenderPassBegin,
3069 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
3070 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3071 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
3072 return skip;
3073}
3074
John Zulauf3d84f1b2020-03-09 13:33:25 -06003075void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3076 VkResult result) {
3077 // The state tracker sets up the command buffer state
3078 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3079
3080 // Create/initialize the structure that trackers accesses at the command buffer scope.
3081 auto cb_access_context = GetAccessContext(commandBuffer);
3082 assert(cb_access_context);
3083 cb_access_context->Reset();
3084}
3085
3086void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06003087 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003088 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003089 if (cb_context) {
3090 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003091 }
3092}
3093
3094void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3095 VkSubpassContents contents) {
3096 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3097 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3098 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003099 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003100}
3101
3102void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3103 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3104 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003105 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003106}
3107
3108void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3109 const VkRenderPassBeginInfo *pRenderPassBegin,
3110 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3111 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003112 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
3113}
3114
3115bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
3116 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
3117 bool skip = false;
3118
3119 auto cb_context = GetAccessContext(commandBuffer);
3120 assert(cb_context);
3121 auto cb_state = cb_context->GetCommandBufferState();
3122 if (!cb_state) return skip;
3123
3124 auto rp_state = cb_state->activeRenderPass;
3125 if (!rp_state) return skip;
3126
3127 skip |= cb_context->ValidateNextSubpass(func_name);
3128
3129 return skip;
3130}
3131
3132bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3133 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
3134 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3135 subpass_begin_info.contents = contents;
3136 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
3137 return skip;
3138}
3139
3140bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
3141 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3142 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3143 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
3144 return skip;
3145}
3146
3147bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3148 const VkSubpassEndInfo *pSubpassEndInfo) const {
3149 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3150 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
3151 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003152}
3153
3154void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003155 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003156 auto cb_context = GetAccessContext(commandBuffer);
3157 assert(cb_context);
3158 auto cb_state = cb_context->GetCommandBufferState();
3159 if (!cb_state) return;
3160
3161 auto rp_state = cb_state->activeRenderPass;
3162 if (!rp_state) return;
3163
John Zulauf355e49b2020-04-24 15:11:15 -06003164 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003165}
3166
3167void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3168 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
3169 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3170 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003171 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003172}
3173
3174void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3175 const VkSubpassEndInfo *pSubpassEndInfo) {
3176 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003177 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003178}
3179
3180void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3181 const VkSubpassEndInfo *pSubpassEndInfo) {
3182 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003183 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003184}
3185
John Zulauf355e49b2020-04-24 15:11:15 -06003186bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
3187 const char *func_name) const {
3188 bool skip = false;
3189
3190 auto cb_context = GetAccessContext(commandBuffer);
3191 assert(cb_context);
3192 auto cb_state = cb_context->GetCommandBufferState();
3193 if (!cb_state) return skip;
3194
3195 auto rp_state = cb_state->activeRenderPass;
3196 if (!rp_state) return skip;
3197
3198 skip |= cb_context->ValidateEndRenderpass(func_name);
3199 return skip;
3200}
3201
3202bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3203 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
3204 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
3205 return skip;
3206}
3207
3208bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
3209 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3210 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
3211 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
3212 return skip;
3213}
3214
3215bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
3216 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3217 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
3218 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
3219 return skip;
3220}
3221
3222void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3223 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003224 // Resolve the all subpass contexts to the command buffer contexts
3225 auto cb_context = GetAccessContext(commandBuffer);
3226 assert(cb_context);
3227 auto cb_state = cb_context->GetCommandBufferState();
3228 if (!cb_state) return;
3229
locke-lunargaecf2152020-05-12 17:15:41 -06003230 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06003231 if (!rp_state) return;
3232
John Zulauf355e49b2020-04-24 15:11:15 -06003233 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06003234}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003235
John Zulauf33fc1d52020-07-17 11:01:10 -06003236// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3237// updates to a resource which do not conflict at the byte level.
3238// TODO: Revisit this rule to see if it needs to be tighter or looser
3239// TODO: Add programatic control over suppression heuristics
3240bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3241 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3242}
3243
John Zulauf3d84f1b2020-03-09 13:33:25 -06003244void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003245 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003246 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003247}
3248
3249void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003250 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003251 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003252}
3253
3254void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003255 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003256 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003257}
locke-lunarga19c71d2020-03-02 18:17:04 -07003258
Jeff Leger178b1e52020-10-05 12:22:23 -04003259template <typename BufferImageCopyRegionType>
3260bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3261 VkImageLayout dstImageLayout, uint32_t regionCount,
3262 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003263 bool skip = false;
3264 const auto *cb_access_context = GetAccessContext(commandBuffer);
3265 assert(cb_access_context);
3266 if (!cb_access_context) return skip;
3267
Jeff Leger178b1e52020-10-05 12:22:23 -04003268 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3269 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3270
locke-lunarga19c71d2020-03-02 18:17:04 -07003271 const auto *context = cb_access_context->GetCurrentAccessContext();
3272 assert(context);
3273 if (!context) return skip;
3274
3275 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003276 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3277
3278 for (uint32_t region = 0; region < regionCount; region++) {
3279 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003280 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003281 ResourceAccessRange src_range =
3282 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003283 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003284 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003285 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003286 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003287 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003288 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003289 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003290 }
3291 }
3292 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003293 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003294 copy_region.imageOffset, copy_region.imageExtent);
3295 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003296 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003297 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003298 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003299 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003300 }
3301 if (skip) break;
3302 }
3303 if (skip) break;
3304 }
3305 return skip;
3306}
3307
Jeff Leger178b1e52020-10-05 12:22:23 -04003308bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3309 VkImageLayout dstImageLayout, uint32_t regionCount,
3310 const VkBufferImageCopy *pRegions) const {
3311 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3312 COPY_COMMAND_VERSION_1);
3313}
3314
3315bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3316 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3317 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3318 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3319 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3320}
3321
3322template <typename BufferImageCopyRegionType>
3323void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3324 VkImageLayout dstImageLayout, uint32_t regionCount,
3325 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003326 auto *cb_access_context = GetAccessContext(commandBuffer);
3327 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003328
3329 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3330 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3331
3332 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003333 auto *context = cb_access_context->GetCurrentAccessContext();
3334 assert(context);
3335
3336 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003337 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003338
3339 for (uint32_t region = 0; region < regionCount; region++) {
3340 const auto &copy_region = pRegions[region];
3341 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003342 ResourceAccessRange src_range =
3343 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003344 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003345 }
3346 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003347 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003348 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003349 }
3350 }
3351}
3352
Jeff Leger178b1e52020-10-05 12:22:23 -04003353void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3354 VkImageLayout dstImageLayout, uint32_t regionCount,
3355 const VkBufferImageCopy *pRegions) {
3356 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3357 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3358}
3359
3360void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3361 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3362 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3363 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3364 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3365 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3366}
3367
3368template <typename BufferImageCopyRegionType>
3369bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3370 VkBuffer dstBuffer, uint32_t regionCount,
3371 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003372 bool skip = false;
3373 const auto *cb_access_context = GetAccessContext(commandBuffer);
3374 assert(cb_access_context);
3375 if (!cb_access_context) return skip;
3376
Jeff Leger178b1e52020-10-05 12:22:23 -04003377 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3378 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3379
locke-lunarga19c71d2020-03-02 18:17:04 -07003380 const auto *context = cb_access_context->GetCurrentAccessContext();
3381 assert(context);
3382 if (!context) return skip;
3383
3384 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3385 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3386 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3387 for (uint32_t region = 0; region < regionCount; region++) {
3388 const auto &copy_region = pRegions[region];
3389 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003390 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003391 copy_region.imageOffset, copy_region.imageExtent);
3392 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003393 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003394 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003395 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003396 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003397 }
3398 }
3399 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06003400 ResourceAccessRange dst_range =
3401 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003402 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003403 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003404 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003405 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003406 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003407 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003408 }
3409 }
3410 if (skip) break;
3411 }
3412 return skip;
3413}
3414
Jeff Leger178b1e52020-10-05 12:22:23 -04003415bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3416 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3417 const VkBufferImageCopy *pRegions) const {
3418 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3419 COPY_COMMAND_VERSION_1);
3420}
3421
3422bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3423 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3424 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3425 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3426 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3427}
3428
3429template <typename BufferImageCopyRegionType>
3430void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3431 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3432 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003433 auto *cb_access_context = GetAccessContext(commandBuffer);
3434 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003435
3436 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3437 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3438
3439 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003440 auto *context = cb_access_context->GetCurrentAccessContext();
3441 assert(context);
3442
3443 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003444 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3445 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 -06003446 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003447
3448 for (uint32_t region = 0; region < regionCount; region++) {
3449 const auto &copy_region = pRegions[region];
3450 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003451 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003452 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003453 }
3454 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003455 ResourceAccessRange dst_range =
3456 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003457 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003458 }
3459 }
3460}
3461
Jeff Leger178b1e52020-10-05 12:22:23 -04003462void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3463 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3464 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3465 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3466}
3467
3468void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3469 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3470 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3471 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3472 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3473 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3474}
3475
3476template <typename RegionType>
3477bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3478 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3479 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003480 bool skip = false;
3481 const auto *cb_access_context = GetAccessContext(commandBuffer);
3482 assert(cb_access_context);
3483 if (!cb_access_context) return skip;
3484
3485 const auto *context = cb_access_context->GetCurrentAccessContext();
3486 assert(context);
3487 if (!context) return skip;
3488
3489 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3490 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3491
3492 for (uint32_t region = 0; region < regionCount; region++) {
3493 const auto &blit_region = pRegions[region];
3494 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003495 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3496 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3497 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3498 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3499 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3500 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3501 auto hazard =
3502 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003503 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003504 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003505 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003506 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003507 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003508 }
3509 }
3510
3511 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003512 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3513 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3514 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3515 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3516 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3517 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3518 auto hazard =
3519 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003520 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003521 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003522 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003523 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003524 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003525 }
3526 if (skip) break;
3527 }
3528 }
3529
3530 return skip;
3531}
3532
Jeff Leger178b1e52020-10-05 12:22:23 -04003533bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3534 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3535 const VkImageBlit *pRegions, VkFilter filter) const {
3536 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3537 "vkCmdBlitImage");
3538}
3539
3540bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3541 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3542 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3543 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3544 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3545}
3546
3547template <typename RegionType>
3548void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3549 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3550 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003551 auto *cb_access_context = GetAccessContext(commandBuffer);
3552 assert(cb_access_context);
3553 auto *context = cb_access_context->GetCurrentAccessContext();
3554 assert(context);
3555
3556 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003557 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003558
3559 for (uint32_t region = 0; region < regionCount; region++) {
3560 const auto &blit_region = pRegions[region];
3561 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003562 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3563 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3564 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3565 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3566 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3567 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3568 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003569 }
3570 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003571 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3572 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3573 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3574 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3575 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3576 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3577 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003578 }
3579 }
3580}
locke-lunarg36ba2592020-04-03 09:42:04 -06003581
Jeff Leger178b1e52020-10-05 12:22:23 -04003582void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3583 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3584 const VkImageBlit *pRegions, VkFilter filter) {
3585 auto *cb_access_context = GetAccessContext(commandBuffer);
3586 assert(cb_access_context);
3587 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3588 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3589 pRegions, filter);
3590 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3591}
3592
3593void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3594 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3595 auto *cb_access_context = GetAccessContext(commandBuffer);
3596 assert(cb_access_context);
3597 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3598 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3599 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3600 pBlitImageInfo->filter, tag);
3601}
3602
locke-lunarg61870c22020-06-09 14:51:50 -06003603bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
3604 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
3605 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003606 bool skip = false;
3607 if (drawCount == 0) return skip;
3608
3609 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3610 VkDeviceSize size = struct_size;
3611 if (drawCount == 1 || stride == size) {
3612 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003613 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003614 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3615 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003616 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003617 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003618 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003619 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003620 }
3621 } else {
3622 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003623 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003624 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3625 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003626 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003627 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3628 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3629 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003630 break;
3631 }
3632 }
3633 }
3634 return skip;
3635}
3636
locke-lunarg61870c22020-06-09 14:51:50 -06003637void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3638 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3639 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003640 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3641 VkDeviceSize size = struct_size;
3642 if (drawCount == 1 || stride == size) {
3643 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003644 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003645 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3646 } else {
3647 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003648 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003649 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3650 }
3651 }
3652}
3653
locke-lunarg61870c22020-06-09 14:51:50 -06003654bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3655 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003656 bool skip = false;
3657
3658 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003659 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003660 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3661 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003662 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003663 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003664 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003665 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003666 }
3667 return skip;
3668}
3669
locke-lunarg61870c22020-06-09 14:51:50 -06003670void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003671 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003672 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003673 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3674}
3675
locke-lunarg36ba2592020-04-03 09:42:04 -06003676bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003677 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003678 const auto *cb_access_context = GetAccessContext(commandBuffer);
3679 assert(cb_access_context);
3680 if (!cb_access_context) return skip;
3681
locke-lunarg61870c22020-06-09 14:51:50 -06003682 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003683 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003684}
3685
3686void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003687 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003688 auto *cb_access_context = GetAccessContext(commandBuffer);
3689 assert(cb_access_context);
3690 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003691
locke-lunarg61870c22020-06-09 14:51:50 -06003692 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003693}
locke-lunarge1a67022020-04-29 00:15:36 -06003694
3695bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003696 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003697 const auto *cb_access_context = GetAccessContext(commandBuffer);
3698 assert(cb_access_context);
3699 if (!cb_access_context) return skip;
3700
3701 const auto *context = cb_access_context->GetCurrentAccessContext();
3702 assert(context);
3703 if (!context) return skip;
3704
locke-lunarg61870c22020-06-09 14:51:50 -06003705 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3706 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3707 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003708 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003709}
3710
3711void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003712 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003713 auto *cb_access_context = GetAccessContext(commandBuffer);
3714 assert(cb_access_context);
3715 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3716 auto *context = cb_access_context->GetCurrentAccessContext();
3717 assert(context);
3718
locke-lunarg61870c22020-06-09 14:51:50 -06003719 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3720 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003721}
3722
3723bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3724 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003725 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003726 const auto *cb_access_context = GetAccessContext(commandBuffer);
3727 assert(cb_access_context);
3728 if (!cb_access_context) return skip;
3729
locke-lunarg61870c22020-06-09 14:51:50 -06003730 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3731 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3732 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003733 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003734}
3735
3736void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3737 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003738 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003739 auto *cb_access_context = GetAccessContext(commandBuffer);
3740 assert(cb_access_context);
3741 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003742
locke-lunarg61870c22020-06-09 14:51:50 -06003743 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3744 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3745 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003746}
3747
3748bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3749 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003750 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003751 const auto *cb_access_context = GetAccessContext(commandBuffer);
3752 assert(cb_access_context);
3753 if (!cb_access_context) return skip;
3754
locke-lunarg61870c22020-06-09 14:51:50 -06003755 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3756 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3757 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003758 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003759}
3760
3761void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3762 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003763 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003764 auto *cb_access_context = GetAccessContext(commandBuffer);
3765 assert(cb_access_context);
3766 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003767
locke-lunarg61870c22020-06-09 14:51:50 -06003768 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3769 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3770 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003771}
3772
3773bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3774 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003775 bool skip = false;
3776 if (drawCount == 0) return skip;
3777
locke-lunargff255f92020-05-13 18:53:52 -06003778 const auto *cb_access_context = GetAccessContext(commandBuffer);
3779 assert(cb_access_context);
3780 if (!cb_access_context) return skip;
3781
3782 const auto *context = cb_access_context->GetCurrentAccessContext();
3783 assert(context);
3784 if (!context) return skip;
3785
locke-lunarg61870c22020-06-09 14:51:50 -06003786 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3787 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3788 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3789 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003790
3791 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3792 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3793 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003794 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003795 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003796}
3797
3798void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3799 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003800 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003801 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003802 auto *cb_access_context = GetAccessContext(commandBuffer);
3803 assert(cb_access_context);
3804 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3805 auto *context = cb_access_context->GetCurrentAccessContext();
3806 assert(context);
3807
locke-lunarg61870c22020-06-09 14:51:50 -06003808 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3809 cb_access_context->RecordDrawSubpassAttachment(tag);
3810 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003811
3812 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3813 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3814 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003815 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003816}
3817
3818bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3819 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003820 bool skip = false;
3821 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003822 const auto *cb_access_context = GetAccessContext(commandBuffer);
3823 assert(cb_access_context);
3824 if (!cb_access_context) return skip;
3825
3826 const auto *context = cb_access_context->GetCurrentAccessContext();
3827 assert(context);
3828 if (!context) return skip;
3829
locke-lunarg61870c22020-06-09 14:51:50 -06003830 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3831 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3832 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3833 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003834
3835 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3836 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3837 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003838 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003839 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003840}
3841
3842void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3843 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003844 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003845 auto *cb_access_context = GetAccessContext(commandBuffer);
3846 assert(cb_access_context);
3847 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3848 auto *context = cb_access_context->GetCurrentAccessContext();
3849 assert(context);
3850
locke-lunarg61870c22020-06-09 14:51:50 -06003851 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3852 cb_access_context->RecordDrawSubpassAttachment(tag);
3853 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003854
3855 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3856 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3857 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003858 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003859}
3860
3861bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3862 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3863 uint32_t stride, const char *function) const {
3864 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003865 const auto *cb_access_context = GetAccessContext(commandBuffer);
3866 assert(cb_access_context);
3867 if (!cb_access_context) return skip;
3868
3869 const auto *context = cb_access_context->GetCurrentAccessContext();
3870 assert(context);
3871 if (!context) return skip;
3872
locke-lunarg61870c22020-06-09 14:51:50 -06003873 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3874 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3875 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3876 function);
3877 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003878
3879 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3880 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3881 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003882 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003883 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003884}
3885
3886bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3887 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3888 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003889 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3890 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003891}
3892
3893void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3894 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3895 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003896 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3897 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003898 auto *cb_access_context = GetAccessContext(commandBuffer);
3899 assert(cb_access_context);
3900 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3901 auto *context = cb_access_context->GetCurrentAccessContext();
3902 assert(context);
3903
locke-lunarg61870c22020-06-09 14:51:50 -06003904 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3905 cb_access_context->RecordDrawSubpassAttachment(tag);
3906 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3907 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003908
3909 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3910 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3911 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003912 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003913}
3914
3915bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3916 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3917 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003918 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3919 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003920}
3921
3922void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3923 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3924 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003925 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3926 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003927 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003928}
3929
3930bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3931 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3932 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003933 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3934 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003935}
3936
3937void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3938 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3939 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003940 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3941 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003942 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3943}
3944
3945bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3946 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3947 uint32_t stride, const char *function) const {
3948 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003949 const auto *cb_access_context = GetAccessContext(commandBuffer);
3950 assert(cb_access_context);
3951 if (!cb_access_context) return skip;
3952
3953 const auto *context = cb_access_context->GetCurrentAccessContext();
3954 assert(context);
3955 if (!context) return skip;
3956
locke-lunarg61870c22020-06-09 14:51:50 -06003957 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3958 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3959 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3960 stride, function);
3961 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003962
3963 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3964 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3965 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003966 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003967 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003968}
3969
3970bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3971 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3972 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003973 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3974 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003975}
3976
3977void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3978 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3979 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003980 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3981 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003982 auto *cb_access_context = GetAccessContext(commandBuffer);
3983 assert(cb_access_context);
3984 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3985 auto *context = cb_access_context->GetCurrentAccessContext();
3986 assert(context);
3987
locke-lunarg61870c22020-06-09 14:51:50 -06003988 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3989 cb_access_context->RecordDrawSubpassAttachment(tag);
3990 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3991 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003992
3993 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3994 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06003995 // We will update the index and vertex buffer in SubmitQueue in the future.
3996 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003997}
3998
3999bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4000 VkDeviceSize offset, VkBuffer countBuffer,
4001 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4002 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004003 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4004 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004005}
4006
4007void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4008 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4009 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004010 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4011 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004012 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4013}
4014
4015bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4016 VkDeviceSize offset, VkBuffer countBuffer,
4017 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4018 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004019 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4020 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004021}
4022
4023void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4024 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4025 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004026 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4027 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004028 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4029}
4030
4031bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4032 const VkClearColorValue *pColor, uint32_t rangeCount,
4033 const VkImageSubresourceRange *pRanges) const {
4034 bool skip = false;
4035 const auto *cb_access_context = GetAccessContext(commandBuffer);
4036 assert(cb_access_context);
4037 if (!cb_access_context) return skip;
4038
4039 const auto *context = cb_access_context->GetCurrentAccessContext();
4040 assert(context);
4041 if (!context) return skip;
4042
4043 const auto *image_state = Get<IMAGE_STATE>(image);
4044
4045 for (uint32_t index = 0; index < rangeCount; index++) {
4046 const auto &range = pRanges[index];
4047 if (image_state) {
4048 auto hazard =
4049 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4050 if (hazard.hazard) {
4051 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004052 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004053 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004054 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004055 }
4056 }
4057 }
4058 return skip;
4059}
4060
4061void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4062 const VkClearColorValue *pColor, uint32_t rangeCount,
4063 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004064 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004065 auto *cb_access_context = GetAccessContext(commandBuffer);
4066 assert(cb_access_context);
4067 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4068 auto *context = cb_access_context->GetCurrentAccessContext();
4069 assert(context);
4070
4071 const auto *image_state = Get<IMAGE_STATE>(image);
4072
4073 for (uint32_t index = 0; index < rangeCount; index++) {
4074 const auto &range = pRanges[index];
4075 if (image_state) {
4076 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4077 tag);
4078 }
4079 }
4080}
4081
4082bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4083 VkImageLayout imageLayout,
4084 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4085 const VkImageSubresourceRange *pRanges) const {
4086 bool skip = false;
4087 const auto *cb_access_context = GetAccessContext(commandBuffer);
4088 assert(cb_access_context);
4089 if (!cb_access_context) return skip;
4090
4091 const auto *context = cb_access_context->GetCurrentAccessContext();
4092 assert(context);
4093 if (!context) return skip;
4094
4095 const auto *image_state = Get<IMAGE_STATE>(image);
4096
4097 for (uint32_t index = 0; index < rangeCount; index++) {
4098 const auto &range = pRanges[index];
4099 if (image_state) {
4100 auto hazard =
4101 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4102 if (hazard.hazard) {
4103 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004104 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004105 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004106 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004107 }
4108 }
4109 }
4110 return skip;
4111}
4112
4113void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4114 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4115 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004116 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004117 auto *cb_access_context = GetAccessContext(commandBuffer);
4118 assert(cb_access_context);
4119 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4120 auto *context = cb_access_context->GetCurrentAccessContext();
4121 assert(context);
4122
4123 const auto *image_state = Get<IMAGE_STATE>(image);
4124
4125 for (uint32_t index = 0; index < rangeCount; index++) {
4126 const auto &range = pRanges[index];
4127 if (image_state) {
4128 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4129 tag);
4130 }
4131 }
4132}
4133
4134bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4135 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4136 VkDeviceSize dstOffset, VkDeviceSize stride,
4137 VkQueryResultFlags flags) const {
4138 bool skip = false;
4139 const auto *cb_access_context = GetAccessContext(commandBuffer);
4140 assert(cb_access_context);
4141 if (!cb_access_context) return skip;
4142
4143 const auto *context = cb_access_context->GetCurrentAccessContext();
4144 assert(context);
4145 if (!context) return skip;
4146
4147 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4148
4149 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004150 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004151 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4152 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004153 skip |=
4154 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4155 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4156 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004157 }
4158 }
locke-lunargff255f92020-05-13 18:53:52 -06004159
4160 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004161 return skip;
4162}
4163
4164void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4165 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4166 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004167 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4168 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004169 auto *cb_access_context = GetAccessContext(commandBuffer);
4170 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004171 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004172 auto *context = cb_access_context->GetCurrentAccessContext();
4173 assert(context);
4174
4175 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4176
4177 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004178 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004179 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4180 }
locke-lunargff255f92020-05-13 18:53:52 -06004181
4182 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004183}
4184
4185bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4186 VkDeviceSize size, uint32_t data) const {
4187 bool skip = false;
4188 const auto *cb_access_context = GetAccessContext(commandBuffer);
4189 assert(cb_access_context);
4190 if (!cb_access_context) return skip;
4191
4192 const auto *context = cb_access_context->GetCurrentAccessContext();
4193 assert(context);
4194 if (!context) return skip;
4195
4196 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4197
4198 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004199 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004200 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4201 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004202 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004203 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004204 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004205 }
4206 }
4207 return skip;
4208}
4209
4210void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4211 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004212 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004213 auto *cb_access_context = GetAccessContext(commandBuffer);
4214 assert(cb_access_context);
4215 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4216 auto *context = cb_access_context->GetCurrentAccessContext();
4217 assert(context);
4218
4219 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4220
4221 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004222 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004223 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4224 }
4225}
4226
4227bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4228 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4229 const VkImageResolve *pRegions) const {
4230 bool skip = false;
4231 const auto *cb_access_context = GetAccessContext(commandBuffer);
4232 assert(cb_access_context);
4233 if (!cb_access_context) return skip;
4234
4235 const auto *context = cb_access_context->GetCurrentAccessContext();
4236 assert(context);
4237 if (!context) return skip;
4238
4239 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4240 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4241
4242 for (uint32_t region = 0; region < regionCount; region++) {
4243 const auto &resolve_region = pRegions[region];
4244 if (src_image) {
4245 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4246 resolve_region.srcOffset, resolve_region.extent);
4247 if (hazard.hazard) {
4248 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004249 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004250 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004251 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004252 }
4253 }
4254
4255 if (dst_image) {
4256 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4257 resolve_region.dstOffset, resolve_region.extent);
4258 if (hazard.hazard) {
4259 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004260 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004261 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004262 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004263 }
4264 if (skip) break;
4265 }
4266 }
4267
4268 return skip;
4269}
4270
4271void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4272 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4273 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004274 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4275 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004276 auto *cb_access_context = GetAccessContext(commandBuffer);
4277 assert(cb_access_context);
4278 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4279 auto *context = cb_access_context->GetCurrentAccessContext();
4280 assert(context);
4281
4282 auto *src_image = Get<IMAGE_STATE>(srcImage);
4283 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4284
4285 for (uint32_t region = 0; region < regionCount; region++) {
4286 const auto &resolve_region = pRegions[region];
4287 if (src_image) {
4288 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4289 resolve_region.srcOffset, resolve_region.extent, tag);
4290 }
4291 if (dst_image) {
4292 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4293 resolve_region.dstOffset, resolve_region.extent, tag);
4294 }
4295 }
4296}
4297
Jeff Leger178b1e52020-10-05 12:22:23 -04004298bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4299 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4300 bool skip = false;
4301 const auto *cb_access_context = GetAccessContext(commandBuffer);
4302 assert(cb_access_context);
4303 if (!cb_access_context) return skip;
4304
4305 const auto *context = cb_access_context->GetCurrentAccessContext();
4306 assert(context);
4307 if (!context) return skip;
4308
4309 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4310 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4311
4312 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4313 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4314 if (src_image) {
4315 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4316 resolve_region.srcOffset, resolve_region.extent);
4317 if (hazard.hazard) {
4318 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4319 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4320 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
4321 region, string_UsageTag(hazard).c_str());
4322 }
4323 }
4324
4325 if (dst_image) {
4326 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4327 resolve_region.dstOffset, resolve_region.extent);
4328 if (hazard.hazard) {
4329 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4330 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4331 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
4332 region, string_UsageTag(hazard).c_str());
4333 }
4334 if (skip) break;
4335 }
4336 }
4337
4338 return skip;
4339}
4340
4341void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4342 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4343 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4344 auto *cb_access_context = GetAccessContext(commandBuffer);
4345 assert(cb_access_context);
4346 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4347 auto *context = cb_access_context->GetCurrentAccessContext();
4348 assert(context);
4349
4350 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4351 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4352
4353 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4354 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4355 if (src_image) {
4356 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4357 resolve_region.srcOffset, resolve_region.extent, tag);
4358 }
4359 if (dst_image) {
4360 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4361 resolve_region.dstOffset, resolve_region.extent, tag);
4362 }
4363 }
4364}
4365
locke-lunarge1a67022020-04-29 00:15:36 -06004366bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4367 VkDeviceSize dataSize, const void *pData) const {
4368 bool skip = false;
4369 const auto *cb_access_context = GetAccessContext(commandBuffer);
4370 assert(cb_access_context);
4371 if (!cb_access_context) return skip;
4372
4373 const auto *context = cb_access_context->GetCurrentAccessContext();
4374 assert(context);
4375 if (!context) return skip;
4376
4377 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4378
4379 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004380 // VK_WHOLE_SIZE not allowed
4381 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004382 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4383 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004384 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004385 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004386 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004387 }
4388 }
4389 return skip;
4390}
4391
4392void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4393 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004394 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004395 auto *cb_access_context = GetAccessContext(commandBuffer);
4396 assert(cb_access_context);
4397 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4398 auto *context = cb_access_context->GetCurrentAccessContext();
4399 assert(context);
4400
4401 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4402
4403 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004404 // VK_WHOLE_SIZE not allowed
4405 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004406 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4407 }
4408}
locke-lunargff255f92020-05-13 18:53:52 -06004409
4410bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4411 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4412 bool skip = false;
4413 const auto *cb_access_context = GetAccessContext(commandBuffer);
4414 assert(cb_access_context);
4415 if (!cb_access_context) return skip;
4416
4417 const auto *context = cb_access_context->GetCurrentAccessContext();
4418 assert(context);
4419 if (!context) return skip;
4420
4421 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4422
4423 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004424 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004425 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4426 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004427 skip |=
4428 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4429 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4430 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004431 }
4432 }
4433 return skip;
4434}
4435
4436void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4437 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004438 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004439 auto *cb_access_context = GetAccessContext(commandBuffer);
4440 assert(cb_access_context);
4441 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4442 auto *context = cb_access_context->GetCurrentAccessContext();
4443 assert(context);
4444
4445 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4446
4447 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004448 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004449 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4450 }
4451}