blob: 650168cbd35068b4f4cd0b1e9d39f7cd80d73cd4 [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];
1754 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1755
1756 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
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];
1784 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1785
1786 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
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;
1796 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
1797
1798 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1799 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001800 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1801 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001802 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1803 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001804 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001805 index_buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001806 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001807 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001808 }
1809
1810 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1811 // We will detect more accurate range in the future.
1812 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1813 return skip;
1814}
1815
1816void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
1817 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return;
1818
1819 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1820 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001821 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1822 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001823 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1824
1825 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1826 // We will detect more accurate range in the future.
1827 RecordDrawVertex(UINT32_MAX, 0, tag);
1828}
1829
1830bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06001831 bool skip = false;
1832 if (!current_renderpass_context_) return skip;
1833 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1834 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1835 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001836}
1837
1838void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
locke-lunarg7077d502020-06-18 21:37:26 -06001839 if (current_renderpass_context_)
1840 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1841 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001842}
1843
John Zulauf355e49b2020-04-24 15:11:15 -06001844bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001845 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001846 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06001847 skip |=
1848 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001849
1850 return skip;
1851}
1852
1853bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1854 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001855 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001856 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001857 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06001858 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1859 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001860
1861 return skip;
1862}
1863
1864void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1865 assert(sync_state_);
1866 if (!cb_state_) return;
1867
1868 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06001869 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06001870 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06001871 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001872 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001873}
1874
John Zulauf355e49b2020-04-24 15:11:15 -06001875void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001876 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001877 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001878 current_context_ = &current_renderpass_context_->CurrentContext();
1879}
1880
John Zulauf355e49b2020-04-24 15:11:15 -06001881void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001882 assert(current_renderpass_context_);
1883 if (!current_renderpass_context_) return;
1884
John Zulauf1a224292020-06-30 14:52:13 -06001885 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001886 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001887 current_renderpass_context_ = nullptr;
1888}
1889
locke-lunarg61870c22020-06-09 14:51:50 -06001890bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1891 const VkRect2D &render_area, const char *func_name) const {
1892 bool skip = false;
locke-lunarg96dc9632020-06-10 17:22:18 -06001893 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001894 if (!pPipe ||
1895 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001896 return skip;
1897 }
1898 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001899 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1900 VkExtent3D extent = CastTo3D(render_area.extent);
1901 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001902
John Zulauf1a224292020-06-30 14:52:13 -06001903 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001904 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001905 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1906 for (const auto location : list) {
1907 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1908 continue;
1909 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001910 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1911 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06001912 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001913 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001914 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001915 func_name, string_SyncHazard(hazard.hazard),
1916 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1917 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001918 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001919 }
1920 }
1921 }
locke-lunarg37047832020-06-12 13:44:45 -06001922
1923 // PHASE1 TODO: Add layout based read/vs. write selection.
1924 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1925 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1926 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001927 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001928 bool depth_write = false, stencil_write = false;
1929
1930 // PHASE1 TODO: These validation should be in core_checks.
1931 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1932 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1933 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1934 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1935 depth_write = true;
1936 }
1937 // PHASE1 TODO: It needs to check if stencil is writable.
1938 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1939 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1940 // PHASE1 TODO: These validation should be in core_checks.
1941 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1942 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1943 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1944 stencil_write = true;
1945 }
1946
1947 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1948 if (depth_write) {
1949 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001950 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1951 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001952 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001953 skip |= sync_state.LogError(
1954 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001955 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001956 func_name, string_SyncHazard(hazard.hazard),
1957 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1958 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001959 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001960 }
1961 }
1962 if (stencil_write) {
1963 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001964 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1965 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001966 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001967 skip |= sync_state.LogError(
1968 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001969 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001970 func_name, string_SyncHazard(hazard.hazard),
1971 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1972 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001973 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001974 }
locke-lunarg61870c22020-06-09 14:51:50 -06001975 }
1976 }
1977 return skip;
1978}
1979
locke-lunarg96dc9632020-06-10 17:22:18 -06001980void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1981 const ResourceUsageTag &tag) {
1982 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001983 if (!pPipe ||
1984 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001985 return;
1986 }
1987 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001988 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1989 VkExtent3D extent = CastTo3D(render_area.extent);
1990 VkOffset3D offset = CastTo3D(render_area.offset);
1991
John Zulauf1a224292020-06-30 14:52:13 -06001992 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001993 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001994 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1995 for (const auto location : list) {
1996 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1997 continue;
1998 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001999 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
2000 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002001 }
2002 }
locke-lunarg37047832020-06-12 13:44:45 -06002003
2004 // PHASE1 TODO: Add layout based read/vs. write selection.
2005 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
2006 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
2007 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002008 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002009 bool depth_write = false, stencil_write = false;
2010
2011 // PHASE1 TODO: These validation should be in core_checks.
2012 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
2013 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2014 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
2015 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2016 depth_write = true;
2017 }
2018 // PHASE1 TODO: It needs to check if stencil is writable.
2019 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2020 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2021 // PHASE1 TODO: These validation should be in core_checks.
2022 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
2023 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
2024 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2025 stencil_write = true;
2026 }
2027
2028 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2029 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002030 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2031 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002032 }
2033 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002034 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2035 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002036 }
locke-lunarg61870c22020-06-09 14:51:50 -06002037 }
2038}
2039
John Zulauf1507ee42020-05-18 11:33:09 -06002040bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
2041 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002042 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002043 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06002044 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2045 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002046 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2047 func_name);
2048
John Zulauf355e49b2020-04-24 15:11:15 -06002049 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002050 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06002051 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002052 if (!skip) {
2053 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2054 // on a copy of the (empty) next context.
2055 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2056 AccessContext temp_context(next_context);
2057 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
2058 skip |= temp_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
2059 }
John Zulauf7635de32020-05-29 17:14:15 -06002060 return skip;
2061}
2062bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
2063 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002064 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002065 bool skip = false;
2066 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2067 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002068 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2069 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06002070 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002071 return skip;
2072}
2073
John Zulauf7635de32020-05-29 17:14:15 -06002074AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2075 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2076}
2077
2078bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2079 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002080 bool skip = false;
2081
John Zulauf7635de32020-05-29 17:14:15 -06002082 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2083 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2084 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2085 // to apply and only copy then, if this proves a hot spot.
2086 std::unique_ptr<AccessContext> proxy_for_current;
2087
John Zulauf355e49b2020-04-24 15:11:15 -06002088 // Validate the "finalLayout" transitions to external
2089 // Get them from where there we're hidding in the extra entry.
2090 const auto &final_transitions = rp_state_->subpass_transitions.back();
2091 for (const auto &transition : final_transitions) {
2092 const auto &attach_view = attachment_views_[transition.attachment];
2093 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2094 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002095 auto *context = trackback.context;
2096
2097 if (transition.prev_pass == current_subpass_) {
2098 if (!proxy_for_current) {
2099 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2100 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2101 }
2102 context = proxy_for_current.get();
2103 }
2104
John Zulaufa0a98292020-09-18 09:30:10 -06002105 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2106 const auto merged_barrier = MergeBarriers(trackback.barriers);
2107 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope,
2108 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2109 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002110 if (hazard.hazard) {
2111 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2112 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf389c34b2020-07-28 11:19:35 -06002113 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002114 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf389c34b2020-07-28 11:19:35 -06002115 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf37ceaed2020-07-03 16:18:15 -06002116 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002117 }
2118 }
2119 return skip;
2120}
2121
2122void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2123 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002124 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002125}
2126
John Zulauf1507ee42020-05-18 11:33:09 -06002127void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2128 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2129 auto &subpass_context = subpass_contexts_[current_subpass_];
2130 VkExtent3D extent = CastTo3D(render_area.extent);
2131 VkOffset3D offset = CastTo3D(render_area.offset);
2132
2133 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2134 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2135 if (attachment_views_[i] == nullptr) continue; // UNUSED
2136 const auto &view = *attachment_views_[i];
2137 const IMAGE_STATE *image = view.image_state.get();
2138 if (image == nullptr) continue;
2139
2140 const auto &ci = attachment_ci[i];
2141 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002142 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002143 const bool is_color = !(has_depth || has_stencil);
2144
2145 if (is_color) {
2146 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2147 extent, tag);
2148 } else {
2149 auto update_range = view.normalized_subresource_range;
2150 if (has_depth) {
2151 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2152 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2153 }
2154 if (has_stencil) {
2155 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2156 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2157 tag);
2158 }
2159 }
2160 }
2161 }
2162}
2163
John Zulauf355e49b2020-04-24 15:11:15 -06002164void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002165 const AccessContext *external_context, VkQueueFlags queue_flags,
2166 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002167 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002168 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002169 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2170 // Add this for all subpasses here so that they exsist during next subpass validation
2171 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002172 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002173 }
2174 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2175
2176 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002177 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002178}
John Zulauf1507ee42020-05-18 11:33:09 -06002179
2180void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002181 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2182 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002183 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002184
John Zulauf355e49b2020-04-24 15:11:15 -06002185 current_subpass_++;
2186 assert(current_subpass_ < subpass_contexts_.size());
2187 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002188 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002189}
2190
John Zulauf1a224292020-06-30 14:52:13 -06002191void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2192 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002193 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002194 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002195 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002196
John Zulauf355e49b2020-04-24 15:11:15 -06002197 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002198 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002199
2200 // Add the "finalLayout" transitions to external
2201 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002202 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2203 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2204 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002205 const auto &final_transitions = rp_state_->subpass_transitions.back();
2206 for (const auto &transition : final_transitions) {
2207 const auto &attachment = attachment_views_[transition.attachment];
2208 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002209 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf89311b42020-09-29 16:28:47 -06002210 ApplyBarrierOpsFunctor barrier_ops(true /* resolve */, last_trackback.barriers, true /* layout transition */, tag);
2211 external_context->UpdateResourceAccess(*attachment->image_state, attachment->normalized_subresource_range, barrier_ops);
John Zulauf355e49b2020-04-24 15:11:15 -06002212 }
2213}
2214
John Zulauf3d84f1b2020-03-09 13:33:25 -06002215SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2216 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2217 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2218 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2219 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2220 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2221 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2222}
2223
John Zulaufb02c1eb2020-10-06 16:33:36 -06002224// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2225void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2226 for (const auto &barrier : barriers) {
2227 ApplyBarrier(barrier, layout_transition);
2228 }
2229}
2230
John Zulauf89311b42020-09-29 16:28:47 -06002231// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2232// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2233// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002234void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2235 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002236 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002237 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002238 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002239 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002240 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002241 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002242}
John Zulauf9cb530d2019-09-30 14:14:10 -06002243HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2244 HazardResult hazard;
2245 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002246 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002247 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002248 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002249 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002250 }
2251 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002252 // Write operation:
2253 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2254 // If reads exists -- test only against them because either:
2255 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2256 // * 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
2257 // the current write happens after the reads, so just test the write against the reades
2258 // Otherwise test against last_write
2259 //
2260 // Look for casus belli for WAR
2261 if (last_read_count) {
2262 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2263 const auto &read_access = last_reads[read_index];
2264 if (IsReadHazard(usage_stage, read_access)) {
2265 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2266 break;
2267 }
2268 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002269 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002270 // Write-After-Write check -- if we have a previous write to test against
2271 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002272 }
2273 }
2274 return hazard;
2275}
2276
John Zulauf69133422020-05-20 14:55:53 -06002277HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2278 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2279 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002280 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002281 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002282 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2283 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002284 if (IsRead(usage_bit)) {
2285 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2286 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2287 if (is_raw_hazard) {
2288 // NOTE: we know last_write is non-zero
2289 // See if the ordering rules save us from the simple RAW check above
2290 // First check to see if the current usage is covered by the ordering rules
2291 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2292 const bool usage_is_ordered =
2293 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2294 if (usage_is_ordered) {
2295 // Now see of the most recent write (or a subsequent read) are ordered
2296 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2297 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002298 }
2299 }
John Zulauf4285ee92020-09-23 10:20:52 -06002300 if (is_raw_hazard) {
2301 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2302 }
John Zulauf361fb532020-07-22 10:45:39 -06002303 } else {
2304 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002305 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulauf361fb532020-07-22 10:45:39 -06002306 if (last_read_count) {
John Zulauf361fb532020-07-22 10:45:39 -06002307 // Look for any WAR hazards outside the ordered set of stages
John Zulauf4285ee92020-09-23 10:20:52 -06002308 VkPipelineStageFlags ordered_stages = 0;
2309 if (usage_write_is_ordered) {
2310 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2311 ordered_stages = GetOrderedStages(ordering);
2312 }
2313 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2314 if ((ordered_stages & last_read_stages) != last_read_stages) {
2315 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2316 const auto &read_access = last_reads[read_index];
2317 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2318 if (IsReadHazard(usage_stage, read_access)) {
2319 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2320 break;
2321 }
John Zulaufd14743a2020-07-03 09:42:39 -06002322 }
2323 }
John Zulauf4285ee92020-09-23 10:20:52 -06002324 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002325 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002326 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002327 }
John Zulauf69133422020-05-20 14:55:53 -06002328 }
2329 }
2330 return hazard;
2331}
2332
John Zulauf2f952d22020-02-10 11:34:51 -07002333// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06002334HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002335 HazardResult hazard;
2336 auto usage = FlagBit(usage_index);
2337 if (IsRead(usage)) {
2338 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002339 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002340 }
2341 } else {
2342 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002343 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002344 } else if (last_read_count > 0) {
John Zulauf4285ee92020-09-23 10:20:52 -06002345 // Any read could be reported, so we'll just pick the first one arbitrarily
John Zulauf59e25072020-07-17 10:55:21 -06002346 hazard.Set(this, usage_index, WRITE_RACING_READ, last_reads[0].access, last_reads[0].tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002347 }
2348 }
2349 return hazard;
2350}
2351
John Zulauf36bcf6a2020-02-03 15:12:52 -07002352HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002353 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002354 // Only supporting image layout transitions for now
2355 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2356 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002357 // only test for WAW if there no intervening read operations.
2358 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2359 if (last_read_count) {
John Zulauf355e49b2020-04-24 15:11:15 -06002360 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002361 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002362 const auto &read_access = last_reads[read_index];
2363 // If the read stage is not in the src sync sync
2364 // *AND* not execution chained with an existing sync barrier (that's the or)
2365 // then the barrier access is unsafe (R/W after R)
2366 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002367 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002368 break;
2369 }
2370 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002371 } else if (last_write.any()) {
John Zulauf361fb532020-07-22 10:45:39 -06002372 // If the previous write is *not* in the 1st access scope
2373 // *AND* the current barrier is not in the dependency chain
2374 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2375 // then the barrier access is unsafe (R/W after W)
2376 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
2377 // TODO: Do we need a difference hazard name for this?
2378 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2379 }
John Zulaufd14743a2020-07-03 09:42:39 -06002380 }
John Zulauf361fb532020-07-22 10:45:39 -06002381
John Zulauf0cb5be22020-01-23 12:18:22 -07002382 return hazard;
2383}
2384
John Zulauf5f13a792020-03-10 07:31:21 -06002385// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2386// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2387// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2388void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2389 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002390 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2391 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002392 *this = other;
2393 } else if (!other.write_tag.IsBefore(write_tag)) {
2394 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2395 // dependency chaining logic or any stage expansion)
2396 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002397 pending_write_barriers |= other.pending_write_barriers;
2398 pending_layout_transition |= other.pending_layout_transition;
2399 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002400
John Zulaufd14743a2020-07-03 09:42:39 -06002401 // Merge the read states
John Zulauf4285ee92020-09-23 10:20:52 -06002402 const auto pre_merge_count = last_read_count;
2403 const auto pre_merge_stages = last_read_stages;
John Zulauf5f13a792020-03-10 07:31:21 -06002404 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2405 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002406 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002407 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002408 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2409 // but we should wait on profiling data for that.
2410 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002411 auto &my_read = last_reads[my_read_index];
2412 if (other_read.stage == my_read.stage) {
2413 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002414 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002415 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002416 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002417 my_read.pending_dep_chain = other_read.pending_dep_chain;
2418 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
2419 // May require tracking more than one access per stage.
2420 my_read.barriers = other_read.barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002421 if (my_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
2422 // Since I'm overwriting the fragement stage read, also update the input attachment info
2423 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06002424 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002425 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002426 } else if (other_read.tag.IsBefore(my_read.tag)) {
2427 // The read tags match so merge the barriers
2428 my_read.barriers |= other_read.barriers;
2429 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002430 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002431
John Zulauf5f13a792020-03-10 07:31:21 -06002432 break;
2433 }
2434 }
2435 } else {
2436 // The other read stage doesn't exist in this, so add it.
2437 last_reads[last_read_count] = other_read;
2438 last_read_count++;
2439 last_read_stages |= other_read.stage;
John Zulauf4285ee92020-09-23 10:20:52 -06002440 if (other_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06002441 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002442 }
John Zulauf5f13a792020-03-10 07:31:21 -06002443 }
2444 }
John Zulauf361fb532020-07-22 10:45:39 -06002445 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002446 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2447 // ignore it.
John Zulauf5f13a792020-03-10 07:31:21 -06002448}
2449
John Zulauf9cb530d2019-09-30 14:14:10 -06002450void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2451 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2452 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002453 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002454 // Mulitple outstanding reads may be of interest and do dependency chains independently
2455 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2456 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002457 uint32_t update_index = kStageCount;
John Zulauf9cb530d2019-09-30 14:14:10 -06002458 if (usage_stage & last_read_stages) {
2459 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf4285ee92020-09-23 10:20:52 -06002460 if (last_reads[read_index].stage == usage_stage) {
2461 update_index = read_index;
John Zulauf9cb530d2019-09-30 14:14:10 -06002462 break;
2463 }
2464 }
John Zulauf4285ee92020-09-23 10:20:52 -06002465 assert(update_index < last_read_count);
John Zulauf9cb530d2019-09-30 14:14:10 -06002466 } else {
John Zulauf9cb530d2019-09-30 14:14:10 -06002467 assert(last_read_count < last_reads.size());
John Zulauf4285ee92020-09-23 10:20:52 -06002468 update_index = last_read_count++;
John Zulauf9cb530d2019-09-30 14:14:10 -06002469 last_read_stages |= usage_stage;
2470 }
John Zulauf4285ee92020-09-23 10:20:52 -06002471 last_reads[update_index].Set(usage_stage, usage_bit, 0, tag);
2472
2473 // Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
2474 if (usage_stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06002475 // TODO Revisit re: multiple reads for a given stage
2476 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002477 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002478 } else {
2479 // Assume write
2480 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002481 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002482 }
2483}
John Zulauf5f13a792020-03-10 07:31:21 -06002484
John Zulauf89311b42020-09-29 16:28:47 -06002485// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2486// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2487// We can overwrite them as *this* write is now after them.
2488//
2489// 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 -07002490void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulauf89311b42020-09-29 16:28:47 -06002491 last_read_count = 0;
2492 last_read_stages = 0;
2493 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06002494 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06002495
2496 write_barriers = 0;
2497 write_dependency_chain = 0;
2498 write_tag = tag;
2499 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002500}
2501
John Zulauf89311b42020-09-29 16:28:47 -06002502// Apply the memory barrier without updating the existing barriers. The execution barrier
2503// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2504// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2505// replace the current write barriers or add to them, so accumulate to pending as well.
2506void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2507 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2508 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06002509 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
2510 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
2511 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
2512 // transistion *as* a write and in scope with the barrier (it's before visibility).
2513 if (layout_transition || InSourceScopeOrChain(barrier.src_exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06002514 pending_write_barriers |= barrier.dst_access_scope;
2515 pending_write_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002516 }
John Zulauf89311b42020-09-29 16:28:47 -06002517 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2518 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06002519
John Zulauf89311b42020-09-29 16:28:47 -06002520 if (!pending_layout_transition) {
2521 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2522 // don't need to be tracked as we're just going to zero them.
John Zulaufa0a98292020-09-18 09:30:10 -06002523 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf89311b42020-09-29 16:28:47 -06002524 ReadState &access = last_reads[read_index];
2525 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2526 if (barrier.src_exec_scope & (access.stage | access.barriers)) {
2527 access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002528 }
2529 }
John Zulaufa0a98292020-09-18 09:30:10 -06002530 }
John Zulaufa0a98292020-09-18 09:30:10 -06002531}
2532
John Zulauf89311b42020-09-29 16:28:47 -06002533void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
2534 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06002535 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
2536 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
2537 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06002538 }
John Zulauf89311b42020-09-29 16:28:47 -06002539
2540 // Apply the accumulate execution barriers (and thus update chaining information)
2541 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
2542 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2543 ReadState &access = last_reads[read_index];
2544 access.barriers |= access.pending_dep_chain;
2545 read_execution_barriers |= access.barriers;
2546 access.pending_dep_chain = 0;
2547 }
2548
2549 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
2550 write_dependency_chain |= pending_write_dep_chain;
2551 write_barriers |= pending_write_barriers;
2552 pending_write_dep_chain = 0;
2553 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002554}
2555
John Zulauf59e25072020-07-17 10:55:21 -06002556// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002557VkPipelineStageFlags ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
John Zulauf59e25072020-07-17 10:55:21 -06002558 VkPipelineStageFlags barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06002559
2560 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2561 const auto &read_access = last_reads[read_index];
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002562 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06002563 barriers = read_access.barriers;
2564 break;
John Zulauf59e25072020-07-17 10:55:21 -06002565 }
2566 }
John Zulauf4285ee92020-09-23 10:20:52 -06002567
John Zulauf59e25072020-07-17 10:55:21 -06002568 return barriers;
2569}
2570
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002571inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlagBits usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06002572 assert(IsRead(usage));
2573 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
2574 // * the previous reads are not hazards, and thus last_write must be visible and available to
2575 // any reads that happen after.
2576 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2577 // 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 -07002578 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06002579}
2580
John Zulauf4285ee92020-09-23 10:20:52 -06002581VkPipelineStageFlags ResourceAccessState::GetOrderedStages(const SyncOrderingBarrier &ordering) const {
2582 // Whether the stage are in the ordering scope only matters if the current write is ordered
2583 VkPipelineStageFlags ordered_stages = last_read_stages & ordering.exec_scope;
2584 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002585 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06002586 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06002587 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
2588 ordered_stages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
2589 }
2590
2591 return ordered_stages;
2592}
2593
2594inline ResourceAccessState::ReadState *ResourceAccessState::GetReadStateForStage(VkPipelineStageFlagBits stage,
2595 uint32_t search_limit) {
2596 ReadState *read_state = nullptr;
2597 search_limit = std::min(search_limit, last_read_count);
2598 for (uint32_t i = 0; i < search_limit; i++) {
2599 if (last_reads[i].stage == stage) {
2600 read_state = &last_reads[i];
2601 break;
2602 }
2603 }
2604 return read_state;
2605}
2606
John Zulaufd1f85d42020-04-15 12:23:15 -06002607void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002608 auto *access_context = GetAccessContextNoInsert(command_buffer);
2609 if (access_context) {
2610 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002611 }
2612}
2613
John Zulaufd1f85d42020-04-15 12:23:15 -06002614void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2615 auto access_found = cb_access_state.find(command_buffer);
2616 if (access_found != cb_access_state.end()) {
2617 access_found->second->Reset();
2618 cb_access_state.erase(access_found);
2619 }
2620}
2621
John Zulauf89311b42020-09-29 16:28:47 -06002622void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2623 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags src_access_scope,
2624 SyncStageAccessFlags dst_access_scope, uint32_t memory_barrier_count,
2625 const VkMemoryBarrier *pMemoryBarriers, const ResourceUsageTag &tag) {
2626 ApplyBarrierOpsFunctor barriers_functor(true /* resolve */, std::min<uint32_t>(1, memory_barrier_count), tag);
2627 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
2628 const auto &barrier = pMemoryBarriers[barrier_index];
2629 SyncBarrier sync_barrier(src_exec_scope, SyncStageAccess::AccessScope(src_access_scope, barrier.srcAccessMask),
2630 dst_exec_scope, SyncStageAccess::AccessScope(dst_access_scope, barrier.dstAccessMask));
2631 barriers_functor.PushBack(sync_barrier, false);
2632 }
2633 if (0 == memory_barrier_count) {
2634 // If there are no global memory barriers, force an exec barrier
2635 barriers_functor.PushBack(SyncBarrier(src_exec_scope, 0, dst_exec_scope, 0), false);
2636 }
John Zulauf540266b2020-04-06 18:54:53 -06002637 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002638}
2639
John Zulauf540266b2020-04-06 18:54:53 -06002640void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002641 const SyncStageAccessFlags &src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2642 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002643 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002644 for (uint32_t index = 0; index < barrier_count; index++) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002645 auto barrier = barriers[index]; // barrier is a copy
John Zulauf9cb530d2019-09-30 14:14:10 -06002646 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2647 if (!buffer) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002648 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
2649 const ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002650 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2651 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06002652 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2653 const ApplyBarrierFunctor update_action(sync_barrier, false /* layout_transition */);
2654 context->UpdateResourceAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002655 }
2656}
2657
John Zulauf540266b2020-04-06 18:54:53 -06002658void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002659 const SyncStageAccessFlags &src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2660 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002661 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002662 for (uint32_t index = 0; index < barrier_count; index++) {
2663 const auto &barrier = barriers[index];
2664 const auto *image = Get<IMAGE_STATE>(barrier.image);
2665 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002666 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002667 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2668 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2669 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06002670 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2671 const ApplyBarrierFunctor barrier_action(sync_barrier, layout_transition);
2672 context->UpdateResourceAccess(*image, subresource_range, barrier_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002673 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002674}
2675
2676bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2677 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2678 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002679 const auto *cb_context = GetAccessContext(commandBuffer);
2680 assert(cb_context);
2681 if (!cb_context) return skip;
2682 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002683
John Zulauf3d84f1b2020-03-09 13:33:25 -06002684 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002685 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002686 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002687
2688 for (uint32_t region = 0; region < regionCount; region++) {
2689 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002690 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002691 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002692 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002693 if (hazard.hazard) {
2694 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002695 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002696 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002697 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002698 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002699 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002700 }
John Zulauf16adfc92020-04-08 10:28:33 -06002701 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002702 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06002703 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002704 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002705 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002706 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002707 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002708 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002709 }
2710 }
2711 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002712 }
2713 return skip;
2714}
2715
2716void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2717 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002718 auto *cb_context = GetAccessContext(commandBuffer);
2719 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002720 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002721 auto *context = cb_context->GetCurrentAccessContext();
2722
John Zulauf9cb530d2019-09-30 14:14:10 -06002723 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002724 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002725
2726 for (uint32_t region = 0; region < regionCount; region++) {
2727 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002728 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002729 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002730 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002731 }
John Zulauf16adfc92020-04-08 10:28:33 -06002732 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002733 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002734 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002735 }
2736 }
2737}
2738
Jeff Leger178b1e52020-10-05 12:22:23 -04002739bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
2740 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
2741 bool skip = false;
2742 const auto *cb_context = GetAccessContext(commandBuffer);
2743 assert(cb_context);
2744 if (!cb_context) return skip;
2745 const auto *context = cb_context->GetCurrentAccessContext();
2746
2747 // If we have no previous accesses, we have no hazards
2748 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2749 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2750
2751 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2752 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2753 if (src_buffer) {
2754 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2755 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
2756 if (hazard.hazard) {
2757 // TODO -- add tag information to log msg when useful.
2758 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
2759 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
2760 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
2761 region, string_UsageTag(hazard).c_str());
2762 }
2763 }
2764 if (dst_buffer && !skip) {
2765 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2766 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
2767 if (hazard.hazard) {
2768 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
2769 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
2770 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
2771 region, string_UsageTag(hazard).c_str());
2772 }
2773 }
2774 if (skip) break;
2775 }
2776 return skip;
2777}
2778
2779void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
2780 auto *cb_context = GetAccessContext(commandBuffer);
2781 assert(cb_context);
2782 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
2783 auto *context = cb_context->GetCurrentAccessContext();
2784
2785 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2786 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2787
2788 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2789 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2790 if (src_buffer) {
2791 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2792 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
2793 }
2794 if (dst_buffer) {
2795 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2796 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
2797 }
2798 }
2799}
2800
John Zulauf5c5e88d2019-12-26 11:22:02 -07002801bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2802 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2803 const VkImageCopy *pRegions) const {
2804 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002805 const auto *cb_access_context = GetAccessContext(commandBuffer);
2806 assert(cb_access_context);
2807 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002808
John Zulauf3d84f1b2020-03-09 13:33:25 -06002809 const auto *context = cb_access_context->GetCurrentAccessContext();
2810 assert(context);
2811 if (!context) return skip;
2812
2813 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2814 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002815 for (uint32_t region = 0; region < regionCount; region++) {
2816 const auto &copy_region = pRegions[region];
2817 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002818 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002819 copy_region.srcOffset, copy_region.extent);
2820 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002821 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002822 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002823 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002824 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002825 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002826 }
2827
2828 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002829 VkExtent3D dst_copy_extent =
2830 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002831 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002832 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002833 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002834 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002835 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002836 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002837 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002838 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002839 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002840 }
2841 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002842
John Zulauf5c5e88d2019-12-26 11:22:02 -07002843 return skip;
2844}
2845
2846void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2847 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2848 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002849 auto *cb_access_context = GetAccessContext(commandBuffer);
2850 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002851 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002852 auto *context = cb_access_context->GetCurrentAccessContext();
2853 assert(context);
2854
John Zulauf5c5e88d2019-12-26 11:22:02 -07002855 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002856 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002857
2858 for (uint32_t region = 0; region < regionCount; region++) {
2859 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002860 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002861 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2862 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002863 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002864 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002865 VkExtent3D dst_copy_extent =
2866 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002867 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2868 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002869 }
2870 }
2871}
2872
Jeff Leger178b1e52020-10-05 12:22:23 -04002873bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
2874 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
2875 bool skip = false;
2876 const auto *cb_access_context = GetAccessContext(commandBuffer);
2877 assert(cb_access_context);
2878 if (!cb_access_context) return skip;
2879
2880 const auto *context = cb_access_context->GetCurrentAccessContext();
2881 assert(context);
2882 if (!context) return skip;
2883
2884 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2885 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2886 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2887 const auto &copy_region = pCopyImageInfo->pRegions[region];
2888 if (src_image) {
2889 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
2890 copy_region.srcOffset, copy_region.extent);
2891 if (hazard.hazard) {
2892 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
2893 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
2894 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
2895 region, string_UsageTag(hazard).c_str());
2896 }
2897 }
2898
2899 if (dst_image) {
2900 VkExtent3D dst_copy_extent =
2901 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2902 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
2903 copy_region.dstOffset, dst_copy_extent);
2904 if (hazard.hazard) {
2905 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
2906 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
2907 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
2908 region, string_UsageTag(hazard).c_str());
2909 }
2910 if (skip) break;
2911 }
2912 }
2913
2914 return skip;
2915}
2916
2917void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
2918 auto *cb_access_context = GetAccessContext(commandBuffer);
2919 assert(cb_access_context);
2920 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
2921 auto *context = cb_access_context->GetCurrentAccessContext();
2922 assert(context);
2923
2924 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2925 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2926
2927 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2928 const auto &copy_region = pCopyImageInfo->pRegions[region];
2929 if (src_image) {
2930 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2931 copy_region.extent, tag);
2932 }
2933 if (dst_image) {
2934 VkExtent3D dst_copy_extent =
2935 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2936 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2937 dst_copy_extent, tag);
2938 }
2939 }
2940}
2941
John Zulauf9cb530d2019-09-30 14:14:10 -06002942bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2943 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2944 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2945 uint32_t bufferMemoryBarrierCount,
2946 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2947 uint32_t imageMemoryBarrierCount,
2948 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2949 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002950 const auto *cb_access_context = GetAccessContext(commandBuffer);
2951 assert(cb_access_context);
2952 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002953
John Zulauf3d84f1b2020-03-09 13:33:25 -06002954 const auto *context = cb_access_context->GetCurrentAccessContext();
2955 assert(context);
2956 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002957
John Zulauf3d84f1b2020-03-09 13:33:25 -06002958 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002959 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2960 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002961 // Validate Image Layout transitions
2962 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2963 const auto &barrier = pImageMemoryBarriers[index];
2964 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2965 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2966 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002967 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002968 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002969 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002970 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002971 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002972 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002973 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002974 }
2975 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002976
2977 return skip;
2978}
2979
2980void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2981 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2982 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2983 uint32_t bufferMemoryBarrierCount,
2984 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2985 uint32_t imageMemoryBarrierCount,
2986 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002987 auto *cb_access_context = GetAccessContext(commandBuffer);
2988 assert(cb_access_context);
2989 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002990 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002991 auto access_context = cb_access_context->GetCurrentAccessContext();
2992 assert(access_context);
2993 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06002994
John Zulauf3d84f1b2020-03-09 13:33:25 -06002995 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002996 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002997 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002998 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
2999 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
3000 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf89311b42020-09-29 16:28:47 -06003001
3002 // These two apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
3003 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
3004 // of the barriers is maintained.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003005 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
3006 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06003007 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06003008 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003009
John Zulauf89311b42020-09-29 16:28:47 -06003010 // Apply the global barriers last as is it walks all memory, it can also clean up the "pending" state without requiring an
3011 // additional pass, updating the dependency chains *last* as it goes along.
3012 // This is needed to guarantee order independence of the three lists.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003013 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf89311b42020-09-29 16:28:47 -06003014 pMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003015}
3016
3017void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3018 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3019 // The state tracker sets up the device state
3020 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3021
John Zulauf5f13a792020-03-10 07:31:21 -06003022 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3023 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003024 // TODO: Find a good way to do this hooklessly.
3025 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3026 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3027 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3028
John Zulaufd1f85d42020-04-15 12:23:15 -06003029 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3030 sync_device_state->ResetCommandBufferCallback(command_buffer);
3031 });
3032 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3033 sync_device_state->FreeCommandBufferCallback(command_buffer);
3034 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003035}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003036
John Zulauf355e49b2020-04-24 15:11:15 -06003037bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3038 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
3039 bool skip = false;
3040 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
3041 auto cb_context = GetAccessContext(commandBuffer);
3042
3043 if (rp_state && cb_context) {
3044 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
3045 }
3046
3047 return skip;
3048}
3049
3050bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3051 VkSubpassContents contents) const {
3052 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3053 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3054 subpass_begin_info.contents = contents;
3055 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
3056 return skip;
3057}
3058
3059bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3060 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
3061 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3062 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
3063 return skip;
3064}
3065
3066bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3067 const VkRenderPassBeginInfo *pRenderPassBegin,
3068 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
3069 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3070 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
3071 return skip;
3072}
3073
John Zulauf3d84f1b2020-03-09 13:33:25 -06003074void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3075 VkResult result) {
3076 // The state tracker sets up the command buffer state
3077 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3078
3079 // Create/initialize the structure that trackers accesses at the command buffer scope.
3080 auto cb_access_context = GetAccessContext(commandBuffer);
3081 assert(cb_access_context);
3082 cb_access_context->Reset();
3083}
3084
3085void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06003086 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003087 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003088 if (cb_context) {
3089 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003090 }
3091}
3092
3093void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3094 VkSubpassContents contents) {
3095 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3096 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3097 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003098 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003099}
3100
3101void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3102 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3103 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003104 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003105}
3106
3107void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3108 const VkRenderPassBeginInfo *pRenderPassBegin,
3109 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3110 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003111 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
3112}
3113
3114bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
3115 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
3116 bool skip = false;
3117
3118 auto cb_context = GetAccessContext(commandBuffer);
3119 assert(cb_context);
3120 auto cb_state = cb_context->GetCommandBufferState();
3121 if (!cb_state) return skip;
3122
3123 auto rp_state = cb_state->activeRenderPass;
3124 if (!rp_state) return skip;
3125
3126 skip |= cb_context->ValidateNextSubpass(func_name);
3127
3128 return skip;
3129}
3130
3131bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3132 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
3133 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3134 subpass_begin_info.contents = contents;
3135 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
3136 return skip;
3137}
3138
3139bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
3140 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3141 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3142 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
3143 return skip;
3144}
3145
3146bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3147 const VkSubpassEndInfo *pSubpassEndInfo) const {
3148 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3149 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
3150 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003151}
3152
3153void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003154 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003155 auto cb_context = GetAccessContext(commandBuffer);
3156 assert(cb_context);
3157 auto cb_state = cb_context->GetCommandBufferState();
3158 if (!cb_state) return;
3159
3160 auto rp_state = cb_state->activeRenderPass;
3161 if (!rp_state) return;
3162
John Zulauf355e49b2020-04-24 15:11:15 -06003163 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003164}
3165
3166void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3167 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
3168 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3169 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003170 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003171}
3172
3173void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3174 const VkSubpassEndInfo *pSubpassEndInfo) {
3175 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003176 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003177}
3178
3179void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3180 const VkSubpassEndInfo *pSubpassEndInfo) {
3181 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003182 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003183}
3184
John Zulauf355e49b2020-04-24 15:11:15 -06003185bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
3186 const char *func_name) const {
3187 bool skip = false;
3188
3189 auto cb_context = GetAccessContext(commandBuffer);
3190 assert(cb_context);
3191 auto cb_state = cb_context->GetCommandBufferState();
3192 if (!cb_state) return skip;
3193
3194 auto rp_state = cb_state->activeRenderPass;
3195 if (!rp_state) return skip;
3196
3197 skip |= cb_context->ValidateEndRenderpass(func_name);
3198 return skip;
3199}
3200
3201bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3202 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
3203 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
3204 return skip;
3205}
3206
3207bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
3208 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3209 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
3210 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
3211 return skip;
3212}
3213
3214bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
3215 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3216 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
3217 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
3218 return skip;
3219}
3220
3221void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3222 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003223 // Resolve the all subpass contexts to the command buffer contexts
3224 auto cb_context = GetAccessContext(commandBuffer);
3225 assert(cb_context);
3226 auto cb_state = cb_context->GetCommandBufferState();
3227 if (!cb_state) return;
3228
locke-lunargaecf2152020-05-12 17:15:41 -06003229 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06003230 if (!rp_state) return;
3231
John Zulauf355e49b2020-04-24 15:11:15 -06003232 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06003233}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003234
John Zulauf33fc1d52020-07-17 11:01:10 -06003235// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3236// updates to a resource which do not conflict at the byte level.
3237// TODO: Revisit this rule to see if it needs to be tighter or looser
3238// TODO: Add programatic control over suppression heuristics
3239bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3240 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3241}
3242
John Zulauf3d84f1b2020-03-09 13:33:25 -06003243void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003244 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003245 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003246}
3247
3248void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003249 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003250 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003251}
3252
3253void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003254 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003255 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003256}
locke-lunarga19c71d2020-03-02 18:17:04 -07003257
Jeff Leger178b1e52020-10-05 12:22:23 -04003258template <typename BufferImageCopyRegionType>
3259bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3260 VkImageLayout dstImageLayout, uint32_t regionCount,
3261 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003262 bool skip = false;
3263 const auto *cb_access_context = GetAccessContext(commandBuffer);
3264 assert(cb_access_context);
3265 if (!cb_access_context) return skip;
3266
Jeff Leger178b1e52020-10-05 12:22:23 -04003267 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3268 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3269
locke-lunarga19c71d2020-03-02 18:17:04 -07003270 const auto *context = cb_access_context->GetCurrentAccessContext();
3271 assert(context);
3272 if (!context) return skip;
3273
3274 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003275 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3276
3277 for (uint32_t region = 0; region < regionCount; region++) {
3278 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003279 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003280 ResourceAccessRange src_range =
3281 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003282 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003283 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003284 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003285 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003286 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003287 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003288 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003289 }
3290 }
3291 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003292 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003293 copy_region.imageOffset, copy_region.imageExtent);
3294 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003295 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003296 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003297 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003298 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003299 }
3300 if (skip) break;
3301 }
3302 if (skip) break;
3303 }
3304 return skip;
3305}
3306
Jeff Leger178b1e52020-10-05 12:22:23 -04003307bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3308 VkImageLayout dstImageLayout, uint32_t regionCount,
3309 const VkBufferImageCopy *pRegions) const {
3310 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3311 COPY_COMMAND_VERSION_1);
3312}
3313
3314bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3315 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3316 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3317 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3318 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3319}
3320
3321template <typename BufferImageCopyRegionType>
3322void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3323 VkImageLayout dstImageLayout, uint32_t regionCount,
3324 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003325 auto *cb_access_context = GetAccessContext(commandBuffer);
3326 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003327
3328 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3329 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3330
3331 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003332 auto *context = cb_access_context->GetCurrentAccessContext();
3333 assert(context);
3334
3335 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003336 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003337
3338 for (uint32_t region = 0; region < regionCount; region++) {
3339 const auto &copy_region = pRegions[region];
3340 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003341 ResourceAccessRange src_range =
3342 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003343 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003344 }
3345 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003346 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003347 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003348 }
3349 }
3350}
3351
Jeff Leger178b1e52020-10-05 12:22:23 -04003352void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3353 VkImageLayout dstImageLayout, uint32_t regionCount,
3354 const VkBufferImageCopy *pRegions) {
3355 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3356 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3357}
3358
3359void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3360 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3361 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3362 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3363 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3364 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3365}
3366
3367template <typename BufferImageCopyRegionType>
3368bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3369 VkBuffer dstBuffer, uint32_t regionCount,
3370 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003371 bool skip = false;
3372 const auto *cb_access_context = GetAccessContext(commandBuffer);
3373 assert(cb_access_context);
3374 if (!cb_access_context) return skip;
3375
Jeff Leger178b1e52020-10-05 12:22:23 -04003376 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3377 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3378
locke-lunarga19c71d2020-03-02 18:17:04 -07003379 const auto *context = cb_access_context->GetCurrentAccessContext();
3380 assert(context);
3381 if (!context) return skip;
3382
3383 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3384 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3385 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3386 for (uint32_t region = 0; region < regionCount; region++) {
3387 const auto &copy_region = pRegions[region];
3388 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003389 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003390 copy_region.imageOffset, copy_region.imageExtent);
3391 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003392 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003393 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003394 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003395 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003396 }
3397 }
3398 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06003399 ResourceAccessRange dst_range =
3400 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003401 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003402 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003403 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003404 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003405 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003406 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003407 }
3408 }
3409 if (skip) break;
3410 }
3411 return skip;
3412}
3413
Jeff Leger178b1e52020-10-05 12:22:23 -04003414bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3415 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3416 const VkBufferImageCopy *pRegions) const {
3417 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3418 COPY_COMMAND_VERSION_1);
3419}
3420
3421bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3422 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3423 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3424 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3425 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3426}
3427
3428template <typename BufferImageCopyRegionType>
3429void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3430 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3431 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003432 auto *cb_access_context = GetAccessContext(commandBuffer);
3433 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003434
3435 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3436 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3437
3438 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003439 auto *context = cb_access_context->GetCurrentAccessContext();
3440 assert(context);
3441
3442 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003443 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3444 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 -06003445 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003446
3447 for (uint32_t region = 0; region < regionCount; region++) {
3448 const auto &copy_region = pRegions[region];
3449 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003450 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003451 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003452 }
3453 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003454 ResourceAccessRange dst_range =
3455 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003456 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003457 }
3458 }
3459}
3460
Jeff Leger178b1e52020-10-05 12:22:23 -04003461void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3462 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3463 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3464 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3465}
3466
3467void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3468 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3469 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3470 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3471 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3472 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3473}
3474
3475template <typename RegionType>
3476bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3477 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3478 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003479 bool skip = false;
3480 const auto *cb_access_context = GetAccessContext(commandBuffer);
3481 assert(cb_access_context);
3482 if (!cb_access_context) return skip;
3483
3484 const auto *context = cb_access_context->GetCurrentAccessContext();
3485 assert(context);
3486 if (!context) return skip;
3487
3488 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3489 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3490
3491 for (uint32_t region = 0; region < regionCount; region++) {
3492 const auto &blit_region = pRegions[region];
3493 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003494 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3495 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3496 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3497 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3498 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3499 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3500 auto hazard =
3501 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003502 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003503 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003504 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003505 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003506 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003507 }
3508 }
3509
3510 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003511 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3512 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3513 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3514 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3515 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3516 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3517 auto hazard =
3518 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003519 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003520 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003521 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003522 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003523 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003524 }
3525 if (skip) break;
3526 }
3527 }
3528
3529 return skip;
3530}
3531
Jeff Leger178b1e52020-10-05 12:22:23 -04003532bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3533 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3534 const VkImageBlit *pRegions, VkFilter filter) const {
3535 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3536 "vkCmdBlitImage");
3537}
3538
3539bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3540 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3541 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3542 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3543 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3544}
3545
3546template <typename RegionType>
3547void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3548 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3549 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003550 auto *cb_access_context = GetAccessContext(commandBuffer);
3551 assert(cb_access_context);
3552 auto *context = cb_access_context->GetCurrentAccessContext();
3553 assert(context);
3554
3555 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003556 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003557
3558 for (uint32_t region = 0; region < regionCount; region++) {
3559 const auto &blit_region = pRegions[region];
3560 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003561 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3562 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3563 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3564 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3565 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3566 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3567 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003568 }
3569 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003570 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3571 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3572 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3573 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3574 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3575 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3576 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003577 }
3578 }
3579}
locke-lunarg36ba2592020-04-03 09:42:04 -06003580
Jeff Leger178b1e52020-10-05 12:22:23 -04003581void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3582 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3583 const VkImageBlit *pRegions, VkFilter filter) {
3584 auto *cb_access_context = GetAccessContext(commandBuffer);
3585 assert(cb_access_context);
3586 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3587 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3588 pRegions, filter);
3589 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3590}
3591
3592void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3593 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3594 auto *cb_access_context = GetAccessContext(commandBuffer);
3595 assert(cb_access_context);
3596 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3597 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3598 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3599 pBlitImageInfo->filter, tag);
3600}
3601
locke-lunarg61870c22020-06-09 14:51:50 -06003602bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
3603 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
3604 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003605 bool skip = false;
3606 if (drawCount == 0) return skip;
3607
3608 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3609 VkDeviceSize size = struct_size;
3610 if (drawCount == 1 || stride == size) {
3611 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003612 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003613 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3614 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003615 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003616 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003617 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003618 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003619 }
3620 } else {
3621 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003622 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003623 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3624 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003625 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003626 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3627 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3628 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003629 break;
3630 }
3631 }
3632 }
3633 return skip;
3634}
3635
locke-lunarg61870c22020-06-09 14:51:50 -06003636void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3637 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3638 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003639 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3640 VkDeviceSize size = struct_size;
3641 if (drawCount == 1 || stride == size) {
3642 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003643 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003644 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3645 } else {
3646 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003647 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003648 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3649 }
3650 }
3651}
3652
locke-lunarg61870c22020-06-09 14:51:50 -06003653bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3654 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003655 bool skip = false;
3656
3657 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003658 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003659 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3660 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003661 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003662 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003663 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003664 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003665 }
3666 return skip;
3667}
3668
locke-lunarg61870c22020-06-09 14:51:50 -06003669void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003670 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003671 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003672 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3673}
3674
locke-lunarg36ba2592020-04-03 09:42:04 -06003675bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003676 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003677 const auto *cb_access_context = GetAccessContext(commandBuffer);
3678 assert(cb_access_context);
3679 if (!cb_access_context) return skip;
3680
locke-lunarg61870c22020-06-09 14:51:50 -06003681 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003682 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003683}
3684
3685void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003686 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003687 auto *cb_access_context = GetAccessContext(commandBuffer);
3688 assert(cb_access_context);
3689 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003690
locke-lunarg61870c22020-06-09 14:51:50 -06003691 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003692}
locke-lunarge1a67022020-04-29 00:15:36 -06003693
3694bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003695 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003696 const auto *cb_access_context = GetAccessContext(commandBuffer);
3697 assert(cb_access_context);
3698 if (!cb_access_context) return skip;
3699
3700 const auto *context = cb_access_context->GetCurrentAccessContext();
3701 assert(context);
3702 if (!context) return skip;
3703
locke-lunarg61870c22020-06-09 14:51:50 -06003704 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3705 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3706 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003707 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003708}
3709
3710void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003711 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003712 auto *cb_access_context = GetAccessContext(commandBuffer);
3713 assert(cb_access_context);
3714 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3715 auto *context = cb_access_context->GetCurrentAccessContext();
3716 assert(context);
3717
locke-lunarg61870c22020-06-09 14:51:50 -06003718 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3719 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003720}
3721
3722bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3723 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003724 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003725 const auto *cb_access_context = GetAccessContext(commandBuffer);
3726 assert(cb_access_context);
3727 if (!cb_access_context) return skip;
3728
locke-lunarg61870c22020-06-09 14:51:50 -06003729 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3730 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3731 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003732 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003733}
3734
3735void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3736 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003737 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003738 auto *cb_access_context = GetAccessContext(commandBuffer);
3739 assert(cb_access_context);
3740 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003741
locke-lunarg61870c22020-06-09 14:51:50 -06003742 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3743 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3744 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003745}
3746
3747bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3748 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003749 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003750 const auto *cb_access_context = GetAccessContext(commandBuffer);
3751 assert(cb_access_context);
3752 if (!cb_access_context) return skip;
3753
locke-lunarg61870c22020-06-09 14:51:50 -06003754 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3755 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3756 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003757 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003758}
3759
3760void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3761 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003762 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003763 auto *cb_access_context = GetAccessContext(commandBuffer);
3764 assert(cb_access_context);
3765 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003766
locke-lunarg61870c22020-06-09 14:51:50 -06003767 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3768 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3769 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003770}
3771
3772bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3773 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003774 bool skip = false;
3775 if (drawCount == 0) return skip;
3776
locke-lunargff255f92020-05-13 18:53:52 -06003777 const auto *cb_access_context = GetAccessContext(commandBuffer);
3778 assert(cb_access_context);
3779 if (!cb_access_context) return skip;
3780
3781 const auto *context = cb_access_context->GetCurrentAccessContext();
3782 assert(context);
3783 if (!context) return skip;
3784
locke-lunarg61870c22020-06-09 14:51:50 -06003785 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3786 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3787 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3788 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003789
3790 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3791 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3792 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003793 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003794 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003795}
3796
3797void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3798 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003799 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003800 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003801 auto *cb_access_context = GetAccessContext(commandBuffer);
3802 assert(cb_access_context);
3803 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3804 auto *context = cb_access_context->GetCurrentAccessContext();
3805 assert(context);
3806
locke-lunarg61870c22020-06-09 14:51:50 -06003807 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3808 cb_access_context->RecordDrawSubpassAttachment(tag);
3809 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003810
3811 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3812 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3813 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003814 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003815}
3816
3817bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3818 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003819 bool skip = false;
3820 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003821 const auto *cb_access_context = GetAccessContext(commandBuffer);
3822 assert(cb_access_context);
3823 if (!cb_access_context) return skip;
3824
3825 const auto *context = cb_access_context->GetCurrentAccessContext();
3826 assert(context);
3827 if (!context) return skip;
3828
locke-lunarg61870c22020-06-09 14:51:50 -06003829 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3830 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3831 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3832 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003833
3834 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3835 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3836 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003837 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003838 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003839}
3840
3841void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3842 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003843 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003844 auto *cb_access_context = GetAccessContext(commandBuffer);
3845 assert(cb_access_context);
3846 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3847 auto *context = cb_access_context->GetCurrentAccessContext();
3848 assert(context);
3849
locke-lunarg61870c22020-06-09 14:51:50 -06003850 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3851 cb_access_context->RecordDrawSubpassAttachment(tag);
3852 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003853
3854 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3855 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3856 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003857 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003858}
3859
3860bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3861 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3862 uint32_t stride, const char *function) const {
3863 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003864 const auto *cb_access_context = GetAccessContext(commandBuffer);
3865 assert(cb_access_context);
3866 if (!cb_access_context) return skip;
3867
3868 const auto *context = cb_access_context->GetCurrentAccessContext();
3869 assert(context);
3870 if (!context) return skip;
3871
locke-lunarg61870c22020-06-09 14:51:50 -06003872 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3873 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3874 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3875 function);
3876 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003877
3878 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3879 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3880 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003881 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003882 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003883}
3884
3885bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3886 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3887 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003888 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3889 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003890}
3891
3892void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3893 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3894 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003895 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3896 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003897 auto *cb_access_context = GetAccessContext(commandBuffer);
3898 assert(cb_access_context);
3899 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3900 auto *context = cb_access_context->GetCurrentAccessContext();
3901 assert(context);
3902
locke-lunarg61870c22020-06-09 14:51:50 -06003903 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3904 cb_access_context->RecordDrawSubpassAttachment(tag);
3905 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3906 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003907
3908 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3909 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3910 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003911 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003912}
3913
3914bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3915 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3916 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003917 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3918 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003919}
3920
3921void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3922 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3923 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003924 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3925 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003926 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003927}
3928
3929bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3930 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3931 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003932 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3933 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003934}
3935
3936void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3937 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3938 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003939 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3940 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003941 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3942}
3943
3944bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3945 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3946 uint32_t stride, const char *function) const {
3947 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003948 const auto *cb_access_context = GetAccessContext(commandBuffer);
3949 assert(cb_access_context);
3950 if (!cb_access_context) return skip;
3951
3952 const auto *context = cb_access_context->GetCurrentAccessContext();
3953 assert(context);
3954 if (!context) return skip;
3955
locke-lunarg61870c22020-06-09 14:51:50 -06003956 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3957 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3958 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3959 stride, function);
3960 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003961
3962 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3963 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3964 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003965 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003966 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003967}
3968
3969bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3970 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3971 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003972 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3973 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003974}
3975
3976void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3977 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3978 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003979 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3980 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003981 auto *cb_access_context = GetAccessContext(commandBuffer);
3982 assert(cb_access_context);
3983 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3984 auto *context = cb_access_context->GetCurrentAccessContext();
3985 assert(context);
3986
locke-lunarg61870c22020-06-09 14:51:50 -06003987 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3988 cb_access_context->RecordDrawSubpassAttachment(tag);
3989 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3990 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003991
3992 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3993 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06003994 // We will update the index and vertex buffer in SubmitQueue in the future.
3995 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003996}
3997
3998bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3999 VkDeviceSize offset, VkBuffer countBuffer,
4000 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4001 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004002 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4003 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004004}
4005
4006void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4007 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4008 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004009 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4010 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004011 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4012}
4013
4014bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4015 VkDeviceSize offset, VkBuffer countBuffer,
4016 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4017 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004018 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4019 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004020}
4021
4022void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4023 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4024 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004025 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4026 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004027 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4028}
4029
4030bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4031 const VkClearColorValue *pColor, uint32_t rangeCount,
4032 const VkImageSubresourceRange *pRanges) const {
4033 bool skip = false;
4034 const auto *cb_access_context = GetAccessContext(commandBuffer);
4035 assert(cb_access_context);
4036 if (!cb_access_context) return skip;
4037
4038 const auto *context = cb_access_context->GetCurrentAccessContext();
4039 assert(context);
4040 if (!context) return skip;
4041
4042 const auto *image_state = Get<IMAGE_STATE>(image);
4043
4044 for (uint32_t index = 0; index < rangeCount; index++) {
4045 const auto &range = pRanges[index];
4046 if (image_state) {
4047 auto hazard =
4048 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4049 if (hazard.hazard) {
4050 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004051 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004052 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004053 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004054 }
4055 }
4056 }
4057 return skip;
4058}
4059
4060void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4061 const VkClearColorValue *pColor, uint32_t rangeCount,
4062 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004063 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004064 auto *cb_access_context = GetAccessContext(commandBuffer);
4065 assert(cb_access_context);
4066 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4067 auto *context = cb_access_context->GetCurrentAccessContext();
4068 assert(context);
4069
4070 const auto *image_state = Get<IMAGE_STATE>(image);
4071
4072 for (uint32_t index = 0; index < rangeCount; index++) {
4073 const auto &range = pRanges[index];
4074 if (image_state) {
4075 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4076 tag);
4077 }
4078 }
4079}
4080
4081bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4082 VkImageLayout imageLayout,
4083 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4084 const VkImageSubresourceRange *pRanges) const {
4085 bool skip = false;
4086 const auto *cb_access_context = GetAccessContext(commandBuffer);
4087 assert(cb_access_context);
4088 if (!cb_access_context) return skip;
4089
4090 const auto *context = cb_access_context->GetCurrentAccessContext();
4091 assert(context);
4092 if (!context) return skip;
4093
4094 const auto *image_state = Get<IMAGE_STATE>(image);
4095
4096 for (uint32_t index = 0; index < rangeCount; index++) {
4097 const auto &range = pRanges[index];
4098 if (image_state) {
4099 auto hazard =
4100 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4101 if (hazard.hazard) {
4102 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004103 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004104 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004105 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004106 }
4107 }
4108 }
4109 return skip;
4110}
4111
4112void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4113 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4114 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004115 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004116 auto *cb_access_context = GetAccessContext(commandBuffer);
4117 assert(cb_access_context);
4118 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4119 auto *context = cb_access_context->GetCurrentAccessContext();
4120 assert(context);
4121
4122 const auto *image_state = Get<IMAGE_STATE>(image);
4123
4124 for (uint32_t index = 0; index < rangeCount; index++) {
4125 const auto &range = pRanges[index];
4126 if (image_state) {
4127 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4128 tag);
4129 }
4130 }
4131}
4132
4133bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4134 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4135 VkDeviceSize dstOffset, VkDeviceSize stride,
4136 VkQueryResultFlags flags) const {
4137 bool skip = false;
4138 const auto *cb_access_context = GetAccessContext(commandBuffer);
4139 assert(cb_access_context);
4140 if (!cb_access_context) return skip;
4141
4142 const auto *context = cb_access_context->GetCurrentAccessContext();
4143 assert(context);
4144 if (!context) return skip;
4145
4146 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4147
4148 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004149 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004150 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4151 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004152 skip |=
4153 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4154 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4155 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004156 }
4157 }
locke-lunargff255f92020-05-13 18:53:52 -06004158
4159 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004160 return skip;
4161}
4162
4163void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4164 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4165 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004166 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4167 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004168 auto *cb_access_context = GetAccessContext(commandBuffer);
4169 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004170 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004171 auto *context = cb_access_context->GetCurrentAccessContext();
4172 assert(context);
4173
4174 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4175
4176 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004177 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004178 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4179 }
locke-lunargff255f92020-05-13 18:53:52 -06004180
4181 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004182}
4183
4184bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4185 VkDeviceSize size, uint32_t data) const {
4186 bool skip = false;
4187 const auto *cb_access_context = GetAccessContext(commandBuffer);
4188 assert(cb_access_context);
4189 if (!cb_access_context) return skip;
4190
4191 const auto *context = cb_access_context->GetCurrentAccessContext();
4192 assert(context);
4193 if (!context) return skip;
4194
4195 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4196
4197 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004198 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004199 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4200 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004201 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004202 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004203 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004204 }
4205 }
4206 return skip;
4207}
4208
4209void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4210 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004211 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004212 auto *cb_access_context = GetAccessContext(commandBuffer);
4213 assert(cb_access_context);
4214 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4215 auto *context = cb_access_context->GetCurrentAccessContext();
4216 assert(context);
4217
4218 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4219
4220 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004221 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004222 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4223 }
4224}
4225
4226bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4227 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4228 const VkImageResolve *pRegions) const {
4229 bool skip = false;
4230 const auto *cb_access_context = GetAccessContext(commandBuffer);
4231 assert(cb_access_context);
4232 if (!cb_access_context) return skip;
4233
4234 const auto *context = cb_access_context->GetCurrentAccessContext();
4235 assert(context);
4236 if (!context) return skip;
4237
4238 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4239 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4240
4241 for (uint32_t region = 0; region < regionCount; region++) {
4242 const auto &resolve_region = pRegions[region];
4243 if (src_image) {
4244 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4245 resolve_region.srcOffset, resolve_region.extent);
4246 if (hazard.hazard) {
4247 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004248 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004249 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004250 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004251 }
4252 }
4253
4254 if (dst_image) {
4255 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4256 resolve_region.dstOffset, resolve_region.extent);
4257 if (hazard.hazard) {
4258 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004259 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004260 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004261 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004262 }
4263 if (skip) break;
4264 }
4265 }
4266
4267 return skip;
4268}
4269
4270void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4271 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4272 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004273 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4274 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004275 auto *cb_access_context = GetAccessContext(commandBuffer);
4276 assert(cb_access_context);
4277 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4278 auto *context = cb_access_context->GetCurrentAccessContext();
4279 assert(context);
4280
4281 auto *src_image = Get<IMAGE_STATE>(srcImage);
4282 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4283
4284 for (uint32_t region = 0; region < regionCount; region++) {
4285 const auto &resolve_region = pRegions[region];
4286 if (src_image) {
4287 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4288 resolve_region.srcOffset, resolve_region.extent, tag);
4289 }
4290 if (dst_image) {
4291 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4292 resolve_region.dstOffset, resolve_region.extent, tag);
4293 }
4294 }
4295}
4296
Jeff Leger178b1e52020-10-05 12:22:23 -04004297bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4298 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4299 bool skip = false;
4300 const auto *cb_access_context = GetAccessContext(commandBuffer);
4301 assert(cb_access_context);
4302 if (!cb_access_context) return skip;
4303
4304 const auto *context = cb_access_context->GetCurrentAccessContext();
4305 assert(context);
4306 if (!context) return skip;
4307
4308 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4309 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4310
4311 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4312 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4313 if (src_image) {
4314 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4315 resolve_region.srcOffset, resolve_region.extent);
4316 if (hazard.hazard) {
4317 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4318 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4319 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
4320 region, string_UsageTag(hazard).c_str());
4321 }
4322 }
4323
4324 if (dst_image) {
4325 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4326 resolve_region.dstOffset, resolve_region.extent);
4327 if (hazard.hazard) {
4328 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4329 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4330 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
4331 region, string_UsageTag(hazard).c_str());
4332 }
4333 if (skip) break;
4334 }
4335 }
4336
4337 return skip;
4338}
4339
4340void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4341 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4342 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4343 auto *cb_access_context = GetAccessContext(commandBuffer);
4344 assert(cb_access_context);
4345 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4346 auto *context = cb_access_context->GetCurrentAccessContext();
4347 assert(context);
4348
4349 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4350 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4351
4352 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4353 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4354 if (src_image) {
4355 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4356 resolve_region.srcOffset, resolve_region.extent, tag);
4357 }
4358 if (dst_image) {
4359 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4360 resolve_region.dstOffset, resolve_region.extent, tag);
4361 }
4362 }
4363}
4364
locke-lunarge1a67022020-04-29 00:15:36 -06004365bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4366 VkDeviceSize dataSize, const void *pData) const {
4367 bool skip = false;
4368 const auto *cb_access_context = GetAccessContext(commandBuffer);
4369 assert(cb_access_context);
4370 if (!cb_access_context) return skip;
4371
4372 const auto *context = cb_access_context->GetCurrentAccessContext();
4373 assert(context);
4374 if (!context) return skip;
4375
4376 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4377
4378 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004379 // VK_WHOLE_SIZE not allowed
4380 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004381 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4382 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004383 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004384 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004385 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004386 }
4387 }
4388 return skip;
4389}
4390
4391void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4392 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004393 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004394 auto *cb_access_context = GetAccessContext(commandBuffer);
4395 assert(cb_access_context);
4396 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4397 auto *context = cb_access_context->GetCurrentAccessContext();
4398 assert(context);
4399
4400 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4401
4402 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004403 // VK_WHOLE_SIZE not allowed
4404 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004405 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4406 }
4407}
locke-lunargff255f92020-05-13 18:53:52 -06004408
4409bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4410 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4411 bool skip = false;
4412 const auto *cb_access_context = GetAccessContext(commandBuffer);
4413 assert(cb_access_context);
4414 if (!cb_access_context) return skip;
4415
4416 const auto *context = cb_access_context->GetCurrentAccessContext();
4417 assert(context);
4418 if (!context) return skip;
4419
4420 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4421
4422 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004423 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004424 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4425 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004426 skip |=
4427 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4428 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4429 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004430 }
4431 }
4432 return skip;
4433}
4434
4435void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4436 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004437 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004438 auto *cb_access_context = GetAccessContext(commandBuffer);
4439 assert(cb_access_context);
4440 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4441 auto *context = cb_access_context->GetCurrentAccessContext();
4442 assert(context);
4443
4444 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4445
4446 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004447 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004448 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4449 }
4450}