blob: fb87ebfa0f1828fb98d928ceedb1d0fdd324082b [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
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700146static std::string string_UsageTag(const ResourceUsageTag &tag) {
147 std::stringstream out;
148
149 out << "command: " << CommandTypeString(tag.command);
150 out << ", seq_no: " << ((tag.index >> 1) & UINT32_MAX) << ", reset_no: " << (tag.index >> 33);
151 if (tag.index & 1) {
152 out << ", subcmd: " << (tag.index & 1);
153 }
154 return out.str();
155}
156
John Zulauf37ceaed2020-07-03 16:18:15 -0600157static std::string string_UsageTag(const HazardResult &hazard) {
158 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600159 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
160 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600161 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600162 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
163 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf59e25072020-07-17 10:55:21 -0600164 out << "(usage: " << usage_info.name << ", prior_usage: " << stage_access_name;
165 if (IsHazardVsRead(hazard.hazard)) {
166 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
167 out << ", read_barriers: " << string_VkPipelineStageFlags(barriers);
168 } else {
169 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
170 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
171 }
172
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700173 out << ", " << string_UsageTag(tag) << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600174 return out.str();
175}
176
John Zulaufd14743a2020-07-03 09:42:39 -0600177// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
178// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
179// also reflects this special case for read hazard detection (using access instead of exec scope)
John Zulaufb027cdb2020-05-21 14:25:22 -0600180static constexpr VkPipelineStageFlags kColorAttachmentExecScope = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700181static const SyncStageAccessFlags kColorAttachmentAccessScope =
182 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
183 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
184 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
185 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600186static constexpr VkPipelineStageFlags kDepthStencilAttachmentExecScope =
187 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700188static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
189 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
190 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
191 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600192
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700193static const SyncOrderingBarrier kColorAttachmentRasterOrder = {kColorAttachmentExecScope, kColorAttachmentAccessScope};
194static const SyncOrderingBarrier kDepthStencilAttachmentRasterOrder = {kDepthStencilAttachmentExecScope,
195 kDepthStencilAttachmentAccessScope};
196static const SyncOrderingBarrier kAttachmentRasterOrder = {kDepthStencilAttachmentExecScope | kColorAttachmentExecScope,
197 kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope};
John Zulauf7635de32020-05-29 17:14:15 -0600198// 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 -0600199static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600200
John Zulaufb02c1eb2020-10-06 16:33:36 -0600201static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) {
202 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
203}
204
205static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
206
locke-lunarg3c038002020-04-30 23:08:08 -0600207inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
208 if (size == VK_WHOLE_SIZE) {
209 return (whole_size - offset);
210 }
211 return size;
212}
213
John Zulauf3e86bf02020-09-12 10:47:57 -0600214static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
215 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
216}
217
John Zulauf16adfc92020-04-08 10:28:33 -0600218template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600219static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600220 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
221}
222
John Zulauf355e49b2020-04-24 15:11:15 -0600223static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600224
John Zulauf3e86bf02020-09-12 10:47:57 -0600225static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
226 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
227}
228
229static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
230 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
231}
232
John Zulauf0cb5be22020-01-23 12:18:22 -0700233// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
234VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
235 VkPipelineStageFlags expanded = stage_mask;
236 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
237 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
238 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
239 if (all_commands.first & queue_flags) {
240 expanded |= all_commands.second;
241 }
242 }
243 }
244 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
245 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
246 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
247 }
248 return expanded;
249}
250
John Zulauf36bcf6a2020-02-03 15:12:52 -0700251VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
Jeremy Gebben91c36902020-11-09 08:17:08 -0700252 const std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
John Zulauf36bcf6a2020-02-03 15:12:52 -0700253 VkPipelineStageFlags unscanned = stage_mask;
254 VkPipelineStageFlags related = 0;
Jonah Ryan-Davis185189c2020-07-14 10:28:52 -0400255 for (const auto &entry : map) {
256 const auto &stage = entry.first;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700257 if (stage & unscanned) {
258 related = related | entry.second;
259 unscanned = unscanned & ~stage;
260 if (!unscanned) break;
261 }
262 }
263 return related;
264}
265
266VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
267 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
268}
269
270VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
271 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
272}
273
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700274static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700275
John Zulauf3e86bf02020-09-12 10:47:57 -0600276ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
277 VkDeviceSize stride) {
278 VkDeviceSize range_start = offset + first_index * stride;
279 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600280 if (count == UINT32_MAX) {
281 range_size = buf_whole_size - range_start;
282 } else {
283 range_size = count * stride;
284 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600285 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600286}
287
locke-lunarg654e3692020-06-04 17:19:15 -0600288SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
289 VkShaderStageFlagBits stage_flag) {
290 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
291 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
292 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
293 }
294 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
295 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
296 assert(0);
297 }
298 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
299 return stage_access->second.uniform_read;
300 }
301
302 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
303 // Because if write hazard happens, read hazard might or might not happen.
304 // But if write hazard doesn't happen, read hazard is impossible to happen.
305 if (descriptor_data.is_writable) {
306 return stage_access->second.shader_write;
307 }
308 return stage_access->second.shader_read;
309}
310
locke-lunarg37047832020-06-12 13:44:45 -0600311bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
312 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
313 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
314 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
315 ? true
316 : false;
317}
318
319bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
320 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
321 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
322 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
323 ? true
324 : false;
325}
326
John Zulauf355e49b2020-04-24 15:11:15 -0600327// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
328const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = {
329 AccessContext::AddressType::kLinearAddress, AccessContext::AddressType::kIdealizedAddress};
330
John Zulaufb02c1eb2020-10-06 16:33:36 -0600331template <typename Action>
332static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
333 Action &action) {
334 // At this point the "apply over range" logic only supports a single memory binding
335 if (!SimpleBinding(image_state)) return;
336 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
337 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
338 image_state.createInfo.extent);
339 const auto base_address = ResourceBaseAddress(image_state);
340 for (; range_gen->non_empty(); ++range_gen) {
341 action((*range_gen + base_address));
342 }
343}
344
John Zulauf7635de32020-05-29 17:14:15 -0600345// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
346// Used by both validation and record operations
347//
348// The signature for Action() reflect the needs of both uses.
349template <typename Action>
350void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
351 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
352 VkExtent3D extent = CastTo3D(render_area.extent);
353 VkOffset3D offset = CastTo3D(render_area.offset);
354 const auto &rp_ci = rp_state.createInfo;
355 const auto *attachment_ci = rp_ci.pAttachments;
356 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
357
358 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
359 const auto *color_attachments = subpass_ci.pColorAttachments;
360 const auto *color_resolve = subpass_ci.pResolveAttachments;
361 if (color_resolve && color_attachments) {
362 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
363 const auto &color_attach = color_attachments[i].attachment;
364 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
365 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
366 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
367 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
368 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
369 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
370 }
371 }
372 }
373
374 // Depth stencil resolve only if the extension is present
375 const auto ds_resolve = lvl_find_in_chain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
376 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
377 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
378 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
379 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
380 const auto src_ci = attachment_ci[src_at];
381 // The formats are required to match so we can pick either
382 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
383 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
384 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
385 VkImageAspectFlags aspect_mask = 0u;
386
387 // Figure out which aspects are actually touched during resolve operations
388 const char *aspect_string = nullptr;
389 if (resolve_depth && resolve_stencil) {
390 // Validate all aspects together
391 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
392 aspect_string = "depth/stencil";
393 } else if (resolve_depth) {
394 // Validate depth only
395 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
396 aspect_string = "depth";
397 } else if (resolve_stencil) {
398 // Validate all stencil only
399 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
400 aspect_string = "stencil";
401 }
402
403 if (aspect_mask) {
404 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
Jeremy Gebbenec5cd382020-11-16 15:53:45 -0700405 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kAttachmentRasterOrder, offset, extent,
John Zulauf7635de32020-05-29 17:14:15 -0600406 aspect_mask);
407 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
408 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
409 }
410 }
411}
412
413// Action for validating resolve operations
414class ValidateResolveAction {
415 public:
416 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
417 const char *func_name)
418 : render_pass_(render_pass),
419 subpass_(subpass),
420 context_(context),
421 sync_state_(sync_state),
422 func_name_(func_name),
423 skip_(false) {}
424 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
425 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
426 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
427 HazardResult hazard;
428 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
429 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600430 skip_ |= sync_state_.LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
431 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600432 " to resolve attachment %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -0600433 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name,
John Zulauf37ceaed2020-07-03 16:18:15 -0600434 src_at, dst_at, string_UsageTag(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600435 }
436 }
437 // Providing a mechanism for the constructing caller to get the result of the validation
438 bool GetSkip() const { return skip_; }
439
440 private:
441 VkRenderPass render_pass_;
442 const uint32_t subpass_;
443 const AccessContext &context_;
444 const SyncValidator &sync_state_;
445 const char *func_name_;
446 bool skip_;
447};
448
449// Update action for resolve operations
450class UpdateStateResolveAction {
451 public:
452 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
453 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
454 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
455 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
456 // Ignores validation only arguments...
457 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
458 }
459
460 private:
461 AccessContext &context_;
462 const ResourceUsageTag &tag_;
463};
464
John Zulauf59e25072020-07-17 10:55:21 -0600465void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700466 const SyncStageAccessFlags &prior_, const ResourceUsageTag &tag_) {
John Zulauf59e25072020-07-17 10:55:21 -0600467 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
468 usage_index = usage_index_;
469 hazard = hazard_;
470 prior_access = prior_;
471 tag = tag_;
472}
473
John Zulauf540266b2020-04-06 18:54:53 -0600474AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
475 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600476 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600477 Reset();
478 const auto &subpass_dep = dependencies[subpass];
479 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600480 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600481 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600482 const auto prev_pass = prev_dep.first->pass;
483 const auto &prev_barriers = prev_dep.second;
484 assert(prev_dep.second.size());
485 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
486 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700487 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600488
489 async_.reserve(subpass_dep.async.size());
490 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700491 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600492 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600493 if (subpass_dep.barrier_from_external.size()) {
494 src_external_ = TrackBack(external_context, queue_flags, subpass_dep.barrier_from_external);
John Zulaufe5da6e52020-03-18 15:32:18 -0600495 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600496 if (subpass_dep.barrier_to_external.size()) {
497 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600498 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700499}
500
John Zulauf5f13a792020-03-10 07:31:21 -0600501template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600502HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600503 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600504 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600505 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600506
507 HazardResult hazard;
508 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
509 hazard = detector.Detect(prev);
510 }
511 return hazard;
512}
513
John Zulauf3d84f1b2020-03-09 13:33:25 -0600514// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
515// the DAG of the contexts (for example subpasses)
516template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600517HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
518 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600519 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600520
John Zulauf1a224292020-06-30 14:52:13 -0600521 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600522 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
523 // so we'll check these first
524 for (const auto &async_context : async_) {
525 hazard = async_context->DetectAsyncHazard(type, detector, range);
526 if (hazard.hazard) return hazard;
527 }
John Zulauf5f13a792020-03-10 07:31:21 -0600528 }
529
John Zulauf1a224292020-06-30 14:52:13 -0600530 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600531
John Zulauf69133422020-05-20 14:55:53 -0600532 const auto &accesses = GetAccessStateMap(type);
533 const auto from = accesses.lower_bound(range);
534 const auto to = accesses.upper_bound(range);
535 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600536
John Zulauf69133422020-05-20 14:55:53 -0600537 for (auto pos = from; pos != to; ++pos) {
538 // Cover any leading gap, or gap between entries
539 if (detect_prev) {
540 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
541 // Cover any leading gap, or gap between entries
542 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600543 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600544 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600545 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600546 if (hazard.hazard) return hazard;
547 }
John Zulauf69133422020-05-20 14:55:53 -0600548 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
549 gap.begin = pos->first.end;
550 }
551
552 hazard = detector.Detect(pos);
553 if (hazard.hazard) return hazard;
554 }
555
556 if (detect_prev) {
557 // Detect in the trailing empty as needed
558 gap.end = range.end;
559 if (gap.non_empty()) {
560 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600561 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600562 }
563
564 return hazard;
565}
566
567// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
568template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600569HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600570 auto &accesses = GetAccessStateMap(type);
571 const auto from = accesses.lower_bound(range);
572 const auto to = accesses.upper_bound(range);
573
John Zulauf3d84f1b2020-03-09 13:33:25 -0600574 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600575 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700576 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600577 }
John Zulauf16adfc92020-04-08 10:28:33 -0600578
John Zulauf3d84f1b2020-03-09 13:33:25 -0600579 return hazard;
580}
581
John Zulaufb02c1eb2020-10-06 16:33:36 -0600582struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700583 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600584 void operator()(ResourceAccessState *access) const {
585 assert(access);
586 access->ApplyBarriers(barriers, true);
587 }
588 const std::vector<SyncBarrier> &barriers;
589};
590
591struct ApplyTrackbackBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700592 explicit ApplyTrackbackBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600593 void operator()(ResourceAccessState *access) const {
594 assert(access);
595 assert(!access->HasPendingState());
596 access->ApplyBarriers(barriers, false);
597 access->ApplyPendingBarriers(kCurrentCommandTag);
598 }
599 const std::vector<SyncBarrier> &barriers;
600};
601
602// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
603// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
604// *different* map from dest.
605// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
606// range [first, last)
607template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600608static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
609 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600610 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600611 auto at = entry;
612 for (auto pos = first; pos != last; ++pos) {
613 // Every member of the input iterator range must fit within the remaining portion of entry
614 assert(at->first.includes(pos->first));
615 assert(at != dest->end());
616 // Trim up at to the same size as the entry to resolve
617 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600618 auto access = pos->second; // intentional copy
619 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600620 at->second.Resolve(access);
621 ++at; // Go to the remaining unused section of entry
622 }
623}
624
John Zulaufa0a98292020-09-18 09:30:10 -0600625static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
626 SyncBarrier merged = {};
627 for (const auto &barrier : barriers) {
628 merged.Merge(barrier);
629 }
630 return merged;
631}
632
John Zulaufb02c1eb2020-10-06 16:33:36 -0600633template <typename BarrierAction>
634void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600635 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
636 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600637 if (!range.non_empty()) return;
638
John Zulauf355e49b2020-04-24 15:11:15 -0600639 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
640 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600641 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600642 if (current->pos_B->valid) {
643 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600644 auto access = src_pos->second; // intentional copy
645 barrier_action(&access);
646
John Zulauf16adfc92020-04-08 10:28:33 -0600647 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600648 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
649 trimmed->second.Resolve(access);
650 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600651 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600652 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600653 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600654 }
John Zulauf16adfc92020-04-08 10:28:33 -0600655 } else {
656 // we have to descend to fill this gap
657 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600658 if (current->pos_A->valid) {
659 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
660 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600661 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600662 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -0600663 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600664 // There isn't anything in dest in current)range, so we can accumulate directly into it.
665 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600666 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
667 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
668 barrier_action(&pos->second);
John Zulauf355e49b2020-04-24 15:11:15 -0600669 }
670 }
671 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
672 // iterator of the outer while.
673
674 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
675 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
676 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600677 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
678 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600679 current.seek(seek_to);
680 } else if (!current->pos_A->valid && infill_state) {
681 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
682 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
683 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600684 }
John Zulauf5f13a792020-03-10 07:31:21 -0600685 }
John Zulauf16adfc92020-04-08 10:28:33 -0600686 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600687 }
John Zulauf1a224292020-06-30 14:52:13 -0600688
689 // Infill if range goes passed both the current and resolve map prior contents
690 if (recur_to_infill && (current->range.end < range.end)) {
691 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
692 ResourceAccessRangeMap gap_map;
693 const auto the_end = resolve_map->end();
694 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
695 for (auto &access : gap_map) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600696 barrier_action(&access.second);
John Zulauf1a224292020-06-30 14:52:13 -0600697 resolve_map->insert(the_end, access);
698 }
699 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600700}
701
John Zulauf355e49b2020-04-24 15:11:15 -0600702void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
703 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600704 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600705 if (range.non_empty() && infill_state) {
706 descent_map->insert(std::make_pair(range, *infill_state));
707 }
708 } else {
709 // Look for something to fill the gap further along.
710 for (const auto &prev_dep : prev_) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600711 const ApplyTrackbackBarriersAction barrier_action(prev_dep.barriers);
712 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600713 }
714
John Zulaufe5da6e52020-03-18 15:32:18 -0600715 if (src_external_.context) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600716 const ApplyTrackbackBarriersAction barrier_action(src_external_.barriers);
717 src_external_.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600718 }
719 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600720}
721
John Zulauf16adfc92020-04-08 10:28:33 -0600722AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600723 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600724}
725
John Zulauf16adfc92020-04-08 10:28:33 -0600726
John Zulauf1507ee42020-05-18 11:33:09 -0600727static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
728 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
729 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
730 return stage_access;
731}
732static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
733 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
734 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
735 return stage_access;
736}
737
John Zulauf7635de32020-05-29 17:14:15 -0600738// Caller must manage returned pointer
739static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
740 uint32_t subpass, const VkRect2D &render_area,
741 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
742 auto *proxy = new AccessContext(context);
743 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600744 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600745 return proxy;
746}
747
John Zulaufb02c1eb2020-10-06 16:33:36 -0600748template <typename BarrierAction>
John Zulauf52446eb2020-10-22 16:40:08 -0600749class ResolveAccessRangeFunctor {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600750 public:
751 ResolveAccessRangeFunctor(const AccessContext &context, AccessContext::AddressType address_type,
752 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
753 BarrierAction &barrier_action)
John Zulauf52446eb2020-10-22 16:40:08 -0600754 : context_(context),
755 address_type_(address_type),
756 descent_map_(descent_map),
757 infill_state_(infill_state),
758 barrier_action_(barrier_action) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600759 ResolveAccessRangeFunctor() = delete;
760 void operator()(const ResourceAccessRange &range) const {
761 context_.ResolveAccessRange(address_type_, range, barrier_action_, descent_map_, infill_state_);
762 }
763
764 private:
John Zulauf52446eb2020-10-22 16:40:08 -0600765 const AccessContext &context_;
766 const AccessContext::AddressType address_type_;
767 ResourceAccessRangeMap *const descent_map_;
768 const ResourceAccessState *infill_state_;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600769 BarrierAction &barrier_action_;
770};
771
John Zulaufb02c1eb2020-10-06 16:33:36 -0600772template <typename BarrierAction>
773void AccessContext::ResolveAccessRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range,
774 BarrierAction &barrier_action, AddressType address_type, ResourceAccessRangeMap *descent_map,
775 const ResourceAccessState *infill_state) const {
776 const ResolveAccessRangeFunctor<BarrierAction> action(*this, address_type, descent_map, infill_state, barrier_action);
777 ApplyOverImageRange(image_state, subresource_range, action);
John Zulauf62f10592020-04-03 12:20:02 -0600778}
779
John Zulauf7635de32020-05-29 17:14:15 -0600780// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600781bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600782 const VkRect2D &render_area, uint32_t subpass,
783 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
784 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600785 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600786 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
787 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
788 // those affects have not been recorded yet.
789 //
790 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
791 // to apply and only copy then, if this proves a hot spot.
792 std::unique_ptr<AccessContext> proxy_for_prev;
793 TrackBack proxy_track_back;
794
John Zulauf355e49b2020-04-24 15:11:15 -0600795 const auto &transitions = rp_state.subpass_transitions[subpass];
796 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600797 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
798
799 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
800 if (prev_needs_proxy) {
801 if (!proxy_for_prev) {
802 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
803 render_area, attachment_views));
804 proxy_track_back = *track_back;
805 proxy_track_back.context = proxy_for_prev.get();
806 }
807 track_back = &proxy_track_back;
808 }
809 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600810 if (hazard.hazard) {
John Zulauf389c34b2020-07-28 11:19:35 -0600811 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
812 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
813 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
814 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
815 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
816 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600817 }
818 }
819 return skip;
820}
821
John Zulauf1507ee42020-05-18 11:33:09 -0600822bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600823 const VkRect2D &render_area, uint32_t subpass,
824 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
825 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600826 bool skip = false;
827 const auto *attachment_ci = rp_state.createInfo.pAttachments;
828 VkExtent3D extent = CastTo3D(render_area.extent);
829 VkOffset3D offset = CastTo3D(render_area.offset);
John Zulaufa0a98292020-09-18 09:30:10 -0600830
John Zulauf1507ee42020-05-18 11:33:09 -0600831 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
832 if (subpass == rp_state.attachment_first_subpass[i]) {
833 if (attachment_views[i] == nullptr) continue;
834 const IMAGE_VIEW_STATE &view = *attachment_views[i];
835 const IMAGE_STATE *image = view.image_state.get();
836 if (image == nullptr) continue;
837 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -0600838
839 // Need check in the following way
840 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
841 // vs. transition
842 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
843 // for each aspect loaded.
844
845 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600846 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600847 const bool is_color = !(has_depth || has_stencil);
848
849 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -0600850 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -0600851
John Zulaufaff20662020-06-01 14:07:58 -0600852 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600853 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -0600854
John Zulaufb02c1eb2020-10-06 16:33:36 -0600855 auto hazard_range = view.normalized_subresource_range;
856 bool checked_stencil = false;
857 if (is_color) {
John Zulauf859089b2020-10-29 17:37:03 -0600858 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, kColorAttachmentRasterOrder, offset,
859 extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600860 aspect = "color";
861 } else {
862 if (has_depth) {
863 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf859089b2020-10-29 17:37:03 -0600864 hazard = DetectHazard(*image, load_index, hazard_range, kDepthStencilAttachmentRasterOrder, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600865 aspect = "depth";
866 }
867 if (!hazard.hazard && has_stencil) {
868 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf859089b2020-10-29 17:37:03 -0600869 hazard =
870 DetectHazard(*image, stencil_load_index, hazard_range, kDepthStencilAttachmentRasterOrder, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600871 aspect = "stencil";
872 checked_stencil = true;
873 }
874 }
875
876 if (hazard.hazard) {
877 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
878 if (hazard.tag == kCurrentCommandTag) {
879 // Hazard vs. ILT
880 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
881 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
882 " aspect %s during load with loadOp %s.",
883 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
884 } else {
John Zulauf1507ee42020-05-18 11:33:09 -0600885 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
886 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600887 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -0600888 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600889 string_UsageTag(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -0600890 }
891 }
892 }
893 }
894 return skip;
895}
896
John Zulaufaff20662020-06-01 14:07:58 -0600897// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
898// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
899// store is part of the same Next/End operation.
900// The latter is handled in layout transistion validation directly
901bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
902 const VkRect2D &render_area, uint32_t subpass,
903 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
904 const char *func_name) const {
905 bool skip = false;
906 const auto *attachment_ci = rp_state.createInfo.pAttachments;
907 VkExtent3D extent = CastTo3D(render_area.extent);
908 VkOffset3D offset = CastTo3D(render_area.offset);
909
910 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
911 if (subpass == rp_state.attachment_last_subpass[i]) {
912 if (attachment_views[i] == nullptr) continue;
913 const IMAGE_VIEW_STATE &view = *attachment_views[i];
914 const IMAGE_STATE *image = view.image_state.get();
915 if (image == nullptr) continue;
916 const auto &ci = attachment_ci[i];
917
918 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
919 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
920 // sake, we treat DONT_CARE as writing.
921 const bool has_depth = FormatHasDepth(ci.format);
922 const bool has_stencil = FormatHasStencil(ci.format);
923 const bool is_color = !(has_depth || has_stencil);
924 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
925 if (!has_stencil && !store_op_stores) continue;
926
927 HazardResult hazard;
928 const char *aspect = nullptr;
929 bool checked_stencil = false;
930 if (is_color) {
931 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
932 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
933 aspect = "color";
934 } else {
935 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
936 auto hazard_range = view.normalized_subresource_range;
937 if (has_depth && store_op_stores) {
938 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
939 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
940 kAttachmentRasterOrder, offset, extent);
941 aspect = "depth";
942 }
943 if (!hazard.hazard && has_stencil && stencil_op_stores) {
944 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
945 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
946 kAttachmentRasterOrder, offset, extent);
947 aspect = "stencil";
948 checked_stencil = true;
949 }
950 }
951
952 if (hazard.hazard) {
953 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
954 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -0600955 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
956 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600957 " %s aspect during store with %s %s. Access info %s",
John Zulauf1dae9192020-06-16 15:46:44 -0600958 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600959 store_op_string, string_UsageTag(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -0600960 }
961 }
962 }
963 return skip;
964}
965
John Zulaufb027cdb2020-05-21 14:25:22 -0600966bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
967 const VkRect2D &render_area,
968 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
969 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600970 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
971 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
972 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600973}
974
John Zulauf3d84f1b2020-03-09 13:33:25 -0600975class HazardDetector {
976 SyncStageAccessIndex usage_index_;
977
978 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600979 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700980 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
981 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600982 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700983 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -0600984};
985
John Zulauf69133422020-05-20 14:55:53 -0600986class HazardDetectorWithOrdering {
987 const SyncStageAccessIndex usage_index_;
988 const SyncOrderingBarrier &ordering_;
989
990 public:
991 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
992 return pos->second.DetectHazard(usage_index_, ordering_);
993 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700994 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
995 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -0600996 }
997 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
998 : usage_index_(usage), ordering_(ordering) {}
999};
1000
John Zulauf16adfc92020-04-08 10:28:33 -06001001HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -06001002 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001003 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -06001004 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001005}
1006
John Zulauf16adfc92020-04-08 10:28:33 -06001007HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001008 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001009 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001010 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -06001011}
1012
John Zulauf69133422020-05-20 14:55:53 -06001013template <typename Detector>
1014HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1015 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1016 const VkExtent3D &extent, DetectOptions options) const {
1017 if (!SimpleBinding(image)) return HazardResult();
1018 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
1019 const auto address_type = ImageAddressType(image);
1020 const auto base_address = ResourceBaseAddress(image);
1021 for (; range_gen->non_empty(); ++range_gen) {
1022 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
1023 if (hazard.hazard) return hazard;
1024 }
1025 return HazardResult();
1026}
1027
John Zulauf540266b2020-04-06 18:54:53 -06001028HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1029 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1030 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001031 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1032 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -06001033 return DetectHazard(image, current_usage, subresource_range, offset, extent);
1034}
1035
1036HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1037 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1038 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -06001039 HazardDetector detector(current_usage);
1040 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
1041}
1042
1043HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1044 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
1045 const VkOffset3D &offset, const VkExtent3D &extent) const {
1046 HazardDetectorWithOrdering detector(current_usage, ordering);
1047 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001048}
1049
John Zulaufb027cdb2020-05-21 14:25:22 -06001050// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
1051// should have reported the issue regarding an invalid attachment entry
1052HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
1053 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
1054 VkImageAspectFlags aspect_mask) const {
1055 if (view != nullptr) {
1056 const IMAGE_STATE *image = view->image_state.get();
1057 if (image != nullptr) {
1058 auto *detect_range = &view->normalized_subresource_range;
1059 VkImageSubresourceRange masked_range;
1060 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1061 masked_range = view->normalized_subresource_range;
1062 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1063 detect_range = &masked_range;
1064 }
1065
1066 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1067 if (detect_range->aspectMask) {
1068 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
1069 }
1070 }
1071 }
1072 return HazardResult();
1073}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001074class BarrierHazardDetector {
1075 public:
1076 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1077 SyncStageAccessFlags src_access_scope)
1078 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1079
John Zulauf5f13a792020-03-10 07:31:21 -06001080 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1081 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001082 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001083 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001084 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001085 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001086 }
1087
1088 private:
1089 SyncStageAccessIndex usage_index_;
1090 VkPipelineStageFlags src_exec_scope_;
1091 SyncStageAccessFlags src_access_scope_;
1092};
1093
John Zulauf16adfc92020-04-08 10:28:33 -06001094HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001095 VkPipelineStageFlags src_exec_scope, const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001096 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001097 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -06001098 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001099}
1100
John Zulauf16adfc92020-04-08 10:28:33 -06001101HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001102 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001103 const VkImageSubresourceRange &subresource_range,
1104 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001105 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1106 VkOffset3D zero_offset = {0, 0, 0};
1107 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001108}
1109
John Zulauf355e49b2020-04-24 15:11:15 -06001110HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001111 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001112 const VkImageMemoryBarrier &barrier) const {
1113 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1114 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1115 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1116}
1117
John Zulauf9cb530d2019-09-30 14:14:10 -06001118template <typename Flags, typename Map>
1119SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1120 SyncStageAccessFlags scope = 0;
1121 for (const auto &bit_scope : map) {
1122 if (flag_mask < bit_scope.first) break;
1123
1124 if (flag_mask & bit_scope.first) {
1125 scope |= bit_scope.second;
1126 }
1127 }
1128 return scope;
1129}
1130
1131SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
1132 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1133}
1134
1135SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
1136 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
1137}
1138
1139// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
1140SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001141 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1142 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1143 // 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 -06001144 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1145}
1146
1147template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001148void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001149 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1150 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -06001151 auto pos = accesses->lower_bound(range);
1152 if (pos == accesses->end() || !pos->first.intersects(range)) {
1153 // The range is empty, fill it with a default value.
1154 pos = action.Infill(accesses, pos, range);
1155 } else if (range.begin < pos->first.begin) {
1156 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001157 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001158 } else if (pos->first.begin < range.begin) {
1159 // Trim the beginning if needed
1160 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1161 ++pos;
1162 }
1163
1164 const auto the_end = accesses->end();
1165 while ((pos != the_end) && pos->first.intersects(range)) {
1166 if (pos->first.end > range.end) {
1167 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1168 }
1169
1170 pos = action(accesses, pos);
1171 if (pos == the_end) break;
1172
1173 auto next = pos;
1174 ++next;
1175 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1176 // Need to infill if next is disjoint
1177 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001178 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001179 next = action.Infill(accesses, next, new_range);
1180 }
1181 pos = next;
1182 }
1183}
1184
1185struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001186 using Iterator = ResourceAccessRangeMap::iterator;
1187 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001188 // this is only called on gaps, and never returns a gap.
1189 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001190 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001191 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001192 }
John Zulauf5f13a792020-03-10 07:31:21 -06001193
John Zulauf5c5e88d2019-12-26 11:22:02 -07001194 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001195 auto &access_state = pos->second;
1196 access_state.Update(usage, tag);
1197 return pos;
1198 }
1199
John Zulauf16adfc92020-04-08 10:28:33 -06001200 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001201 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001202 : type(type_), context(context_), usage(usage_), tag(tag_) {}
1203 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001204 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001205 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001206 const ResourceUsageTag &tag;
1207};
1208
John Zulauf89311b42020-09-29 16:28:47 -06001209// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1210// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1211class ApplyBarrierFunctor {
1212 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001213 using Iterator = ResourceAccessRangeMap::iterator;
1214 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001215
John Zulauf5c5e88d2019-12-26 11:22:02 -07001216 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001217 auto &access_state = pos->second;
John Zulauf89311b42020-09-29 16:28:47 -06001218 access_state.ApplyBarrier(barrier_, layout_transition_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001219 return pos;
1220 }
1221
John Zulauf89311b42020-09-29 16:28:47 -06001222 ApplyBarrierFunctor(const SyncBarrier &barrier, bool layout_transition)
1223 : barrier_(barrier), layout_transition_(layout_transition) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001224
John Zulauf89311b42020-09-29 16:28:47 -06001225 private:
1226 const SyncBarrier barrier_;
1227 const bool layout_transition_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001228};
1229
John Zulauf89311b42020-09-29 16:28:47 -06001230// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1231// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1232// of a collection is known/present.
1233class ApplyBarrierOpsFunctor {
1234 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001235 using Iterator = ResourceAccessRangeMap::iterator;
1236 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001237
John Zulauf89311b42020-09-29 16:28:47 -06001238 struct BarrierOp {
1239 SyncBarrier barrier;
1240 bool layout_transition;
1241 BarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1242 : barrier(barrier_), layout_transition(layout_transition_) {}
1243 BarrierOp() = default;
1244 };
John Zulauf5c5e88d2019-12-26 11:22:02 -07001245 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001246 auto &access_state = pos->second;
John Zulauf89311b42020-09-29 16:28:47 -06001247 for (const auto op : barrier_ops_) {
1248 access_state.ApplyBarrier(op.barrier, op.layout_transition);
1249 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001250
John Zulauf89311b42020-09-29 16:28:47 -06001251 if (resolve_) {
1252 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1253 // another walk
1254 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001255 }
1256 return pos;
1257 }
1258
John Zulauf89311b42020-09-29 16:28:47 -06001259 // A valid tag is required IFF any of the barriers ops are a layout transition, as transitions are write ops
1260 ApplyBarrierOpsFunctor(bool resolve, size_t size_hint, const ResourceUsageTag &tag)
1261 : resolve_(resolve), barrier_ops_(), tag_(tag) {
1262 if (size_hint) {
1263 barrier_ops_.reserve(size_hint);
1264 }
1265 };
1266
1267 // A valid tag is required IFF layout_transition is true, as transitions are write ops
1268 ApplyBarrierOpsFunctor(bool resolve, const std::vector<SyncBarrier> &barriers, bool layout_transition,
1269 const ResourceUsageTag &tag)
John Zulaufb02c1eb2020-10-06 16:33:36 -06001270 : resolve_(resolve), barrier_ops_(), tag_(tag) {
1271 barrier_ops_.reserve(barriers.size());
John Zulauf89311b42020-09-29 16:28:47 -06001272 for (const auto &barrier : barriers) {
1273 barrier_ops_.emplace_back(barrier, layout_transition);
John Zulauf9cb530d2019-09-30 14:14:10 -06001274 }
1275 }
1276
John Zulauf89311b42020-09-29 16:28:47 -06001277 void PushBack(const SyncBarrier &barrier, bool layout_transition) { barrier_ops_.emplace_back(barrier, layout_transition); }
1278
1279 void PushBack(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
1280 barrier_ops_.reserve(barrier_ops_.size() + barriers.size());
1281 for (const auto &barrier : barriers) {
1282 barrier_ops_.emplace_back(barrier, layout_transition);
1283 }
1284 }
1285
1286 private:
1287 bool resolve_;
1288 std::vector<BarrierOp> barrier_ops_;
1289 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001290};
1291
John Zulauf355e49b2020-04-24 15:11:15 -06001292void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1293 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001294 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1295 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001296}
1297
John Zulauf16adfc92020-04-08 10:28:33 -06001298void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001299 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001300 if (!SimpleBinding(buffer)) return;
1301 const auto base_address = ResourceBaseAddress(buffer);
1302 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1303}
John Zulauf355e49b2020-04-24 15:11:15 -06001304
John Zulauf540266b2020-04-06 18:54:53 -06001305void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001306 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001307 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001308 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001309 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001310 const auto address_type = ImageAddressType(image);
1311 const auto base_address = ResourceBaseAddress(image);
1312 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001313 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001314 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001315 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001316}
John Zulauf7635de32020-05-29 17:14:15 -06001317void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1318 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1319 if (view != nullptr) {
1320 const IMAGE_STATE *image = view->image_state.get();
1321 if (image != nullptr) {
1322 auto *update_range = &view->normalized_subresource_range;
1323 VkImageSubresourceRange masked_range;
1324 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1325 masked_range = view->normalized_subresource_range;
1326 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1327 update_range = &masked_range;
1328 }
1329 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1330 }
1331 }
1332}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001333
John Zulauf355e49b2020-04-24 15:11:15 -06001334void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1335 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1336 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001337 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1338 subresource.layerCount};
1339 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1340}
1341
John Zulauf540266b2020-04-06 18:54:53 -06001342template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001343void AccessContext::UpdateResourceAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001344 if (!SimpleBinding(buffer)) return;
1345 const auto base_address = ResourceBaseAddress(buffer);
1346 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001347}
1348
1349template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001350void AccessContext::UpdateResourceAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1351 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001352 if (!SimpleBinding(image)) return;
1353 const auto address_type = ImageAddressType(image);
1354 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001355
locke-lunargae26eac2020-04-16 15:29:05 -06001356 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001357 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001358
John Zulauf16adfc92020-04-08 10:28:33 -06001359 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001360 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001361 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001362 }
1363}
1364
John Zulauf7635de32020-05-29 17:14:15 -06001365void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1366 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1367 const ResourceUsageTag &tag) {
1368 UpdateStateResolveAction update(*this, tag);
1369 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1370}
1371
John Zulaufaff20662020-06-01 14:07:58 -06001372void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1373 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1374 const ResourceUsageTag &tag) {
1375 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1376 VkExtent3D extent = CastTo3D(render_area.extent);
1377 VkOffset3D offset = CastTo3D(render_area.offset);
1378
1379 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1380 if (rp_state.attachment_last_subpass[i] == subpass) {
1381 if (attachment_views[i] == nullptr) continue; // UNUSED
1382 const auto &view = *attachment_views[i];
1383 const IMAGE_STATE *image = view.image_state.get();
1384 if (image == nullptr) continue;
1385
1386 const auto &ci = attachment_ci[i];
1387 const bool has_depth = FormatHasDepth(ci.format);
1388 const bool has_stencil = FormatHasStencil(ci.format);
1389 const bool is_color = !(has_depth || has_stencil);
1390 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1391
1392 if (is_color && store_op_stores) {
1393 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1394 offset, extent, tag);
1395 } else {
1396 auto update_range = view.normalized_subresource_range;
1397 if (has_depth && store_op_stores) {
1398 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1399 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1400 tag);
1401 }
1402 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1403 if (has_stencil && stencil_op_stores) {
1404 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1405 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1406 tag);
1407 }
1408 }
1409 }
1410 }
1411}
1412
John Zulauf540266b2020-04-06 18:54:53 -06001413template <typename Action>
1414void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1415 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001416 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001417 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001418 }
1419}
1420
1421void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001422 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1423 auto &context = contexts[subpass_index];
John Zulaufb02c1eb2020-10-06 16:33:36 -06001424 ApplyTrackbackBarriersAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001425 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001426 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001427 }
1428 }
1429}
1430
John Zulauf355e49b2020-04-24 15:11:15 -06001431// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001432HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001433 if (!attach_view) return HazardResult();
1434 const auto image_state = attach_view->image_state.get();
1435 if (!image_state) return HazardResult();
1436
John Zulauf355e49b2020-04-24 15:11:15 -06001437 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001438 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001439
1440 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001441 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1442 const auto merged_barrier = MergeBarriers(track_back.barriers);
1443 HazardResult hazard =
1444 track_back.context->DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
1445 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001446 if (!hazard.hazard) {
1447 // The Async hazard check is against the current context's async set.
John Zulaufa0a98292020-09-18 09:30:10 -06001448 hazard = DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001449 attach_view->normalized_subresource_range, kDetectAsync);
1450 }
John Zulaufa0a98292020-09-18 09:30:10 -06001451
John Zulauf355e49b2020-04-24 15:11:15 -06001452 return hazard;
1453}
1454
John Zulaufb02c1eb2020-10-06 16:33:36 -06001455void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
1456 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1457 const ResourceUsageTag &tag) {
1458 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001459 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001460 for (const auto &transition : transitions) {
1461 const auto prev_pass = transition.prev_pass;
1462 const auto attachment_view = attachment_views[transition.attachment];
1463 if (!attachment_view) continue;
1464 const auto *image = attachment_view->image_state.get();
1465 if (!image) continue;
1466 if (!SimpleBinding(*image)) continue;
1467
1468 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1469 assert(trackback);
1470
1471 // Import the attachments into the current context
1472 const auto *prev_context = trackback->context;
1473 assert(prev_context);
1474 const auto address_type = ImageAddressType(*image);
1475 auto &target_map = GetAccessStateMap(address_type);
1476 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
1477 prev_context->ResolveAccessRange(*image, attachment_view->normalized_subresource_range, barrier_action, address_type,
John Zulauf646cc292020-10-23 09:16:45 -06001478 &target_map, &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001479 }
1480
John Zulauf86356ca2020-10-19 11:46:41 -06001481 // If there were no transitions skip this global map walk
1482 if (transitions.size()) {
1483 ApplyBarrierOpsFunctor apply_pending_action(true /* resolve */, 0, tag);
1484 ApplyGlobalBarriers(apply_pending_action);
1485 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001486}
1487
John Zulauf355e49b2020-04-24 15:11:15 -06001488// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1489bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1490
1491 const VkRenderPassBeginInfo *pRenderPassBegin,
1492 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1493 const char *func_name) const {
1494 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1495 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06001496
John Zulauf86356ca2020-10-19 11:46:41 -06001497 assert(pRenderPassBegin);
1498 if (nullptr == pRenderPassBegin) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06001499
John Zulauf86356ca2020-10-19 11:46:41 -06001500 const uint32_t subpass = 0;
John Zulauf355e49b2020-04-24 15:11:15 -06001501
John Zulauf86356ca2020-10-19 11:46:41 -06001502 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
1503 // hasn't happened yet)
1504 const std::vector<AccessContext> empty_context_vector;
1505 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1506 const_cast<AccessContext *>(&cb_access_context_));
John Zulauf355e49b2020-04-24 15:11:15 -06001507
John Zulauf86356ca2020-10-19 11:46:41 -06001508 // Create a view list
1509 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1510 assert(fb_state);
1511 if (nullptr == fb_state) return skip;
1512 // NOTE: Must not use COMMAND_BUFFER_STATE variant of this as RecordCmdBeginRenderPass hasn't run and thus
1513 // the activeRenderPass.* fields haven't been set.
1514 const auto views = sync_state_->GetAttachmentViews(*pRenderPassBegin, *fb_state);
1515
1516 // Validate transitions
1517 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
1518
1519 // Validate load operations if there were no layout transition hazards
1520 if (!skip) {
1521 temp_context.RecordLayoutTransitions(rp_state, subpass, views, kCurrentCommandTag);
1522 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001523 }
John Zulauf86356ca2020-10-19 11:46:41 -06001524
John Zulauf355e49b2020-04-24 15:11:15 -06001525 return skip;
1526}
1527
locke-lunarg61870c22020-06-09 14:51:50 -06001528bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1529 const char *func_name) const {
1530 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001531 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001532 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001533 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1534 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001535 return skip;
1536 }
1537
1538 using DescriptorClass = cvdescriptorset::DescriptorClass;
1539 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1540 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1541 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1542 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1543
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001544 for (const auto &stage_state : pipe->stage_state) {
1545 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1546 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001547 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001548 }
locke-lunarg61870c22020-06-09 14:51:50 -06001549 for (const auto &set_binding : stage_state.descriptor_uses) {
1550 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1551 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1552 set_binding.first.second);
1553 const auto descriptor_type = binding_it.GetType();
1554 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1555 auto array_idx = 0;
1556
1557 if (binding_it.IsVariableDescriptorCount()) {
1558 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1559 }
1560 SyncStageAccessIndex sync_index =
1561 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1562
1563 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1564 uint32_t index = i - index_range.start;
1565 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1566 switch (descriptor->GetClass()) {
1567 case DescriptorClass::ImageSampler:
1568 case DescriptorClass::Image: {
1569 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001570 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001571 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001572 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1573 img_view_state = image_sampler_descriptor->GetImageViewState();
1574 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001575 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001576 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1577 img_view_state = image_descriptor->GetImageViewState();
1578 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001579 }
1580 if (!img_view_state) continue;
1581 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1582 VkExtent3D extent = {};
1583 VkOffset3D offset = {};
1584 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1585 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1586 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1587 } else {
1588 extent = img_state->createInfo.extent;
1589 }
John Zulauf361fb532020-07-22 10:45:39 -06001590 HazardResult hazard;
1591 const auto &subresource_range = img_view_state->normalized_subresource_range;
1592 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1593 // Input attachments are subject to raster ordering rules
1594 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
1595 kAttachmentRasterOrder, offset, extent);
1596 } else {
1597 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1598 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001599 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001600 skip |= sync_state_->LogError(
1601 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001602 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1603 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001604 func_name, string_SyncHazard(hazard.hazard),
1605 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1606 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001607 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001608 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1609 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1610 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001611 }
1612 break;
1613 }
1614 case DescriptorClass::TexelBuffer: {
1615 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1616 if (!buf_view_state) continue;
1617 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001618 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001619 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001620 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001621 skip |= sync_state_->LogError(
1622 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001623 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1624 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001625 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1626 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001627 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001628 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1629 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1630 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001631 }
1632 break;
1633 }
1634 case DescriptorClass::GeneralBuffer: {
1635 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1636 auto buf_state = buffer_descriptor->GetBufferState();
1637 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001638 const ResourceAccessRange range =
1639 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001640 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001641 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001642 skip |= sync_state_->LogError(
1643 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001644 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1645 func_name, string_SyncHazard(hazard.hazard),
1646 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001647 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001648 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001649 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1650 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1651 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001652 }
1653 break;
1654 }
1655 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1656 default:
1657 break;
1658 }
1659 }
1660 }
1661 }
1662 return skip;
1663}
1664
1665void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1666 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001667 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001668 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001669 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1670 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001671 return;
1672 }
1673
1674 using DescriptorClass = cvdescriptorset::DescriptorClass;
1675 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1676 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1677 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1678 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1679
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001680 for (const auto &stage_state : pipe->stage_state) {
1681 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1682 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001683 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001684 }
locke-lunarg61870c22020-06-09 14:51:50 -06001685 for (const auto &set_binding : stage_state.descriptor_uses) {
1686 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1687 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1688 set_binding.first.second);
1689 const auto descriptor_type = binding_it.GetType();
1690 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1691 auto array_idx = 0;
1692
1693 if (binding_it.IsVariableDescriptorCount()) {
1694 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1695 }
1696 SyncStageAccessIndex sync_index =
1697 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1698
1699 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1700 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1701 switch (descriptor->GetClass()) {
1702 case DescriptorClass::ImageSampler:
1703 case DescriptorClass::Image: {
1704 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1705 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1706 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1707 } else {
1708 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1709 }
1710 if (!img_view_state) continue;
1711 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1712 VkExtent3D extent = {};
1713 VkOffset3D offset = {};
1714 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1715 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1716 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1717 } else {
1718 extent = img_state->createInfo.extent;
1719 }
1720 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1721 offset, extent, tag);
1722 break;
1723 }
1724 case DescriptorClass::TexelBuffer: {
1725 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1726 if (!buf_view_state) continue;
1727 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001728 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001729 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1730 break;
1731 }
1732 case DescriptorClass::GeneralBuffer: {
1733 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1734 auto buf_state = buffer_descriptor->GetBufferState();
1735 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001736 const ResourceAccessRange range =
1737 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001738 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1739 break;
1740 }
1741 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1742 default:
1743 break;
1744 }
1745 }
1746 }
1747 }
1748}
1749
1750bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1751 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001752 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1753 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06001754 return skip;
1755 }
1756
1757 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1758 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001759 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06001760
1761 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001762 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06001763 if (binding_description.binding < binding_buffers_size) {
1764 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07001765 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001766
locke-lunarg1ae57d62020-11-18 10:49:19 -07001767 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001768 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1769 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001770 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1771 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001772 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001773 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 -06001774 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001775 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001776 }
1777 }
1778 }
1779 return skip;
1780}
1781
1782void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001783 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1784 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06001785 return;
1786 }
1787 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1788 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001789 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06001790
1791 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001792 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06001793 if (binding_description.binding < binding_buffers_size) {
1794 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07001795 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001796
locke-lunarg1ae57d62020-11-18 10:49:19 -07001797 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001798 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1799 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06001800 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1801 }
1802 }
1803}
1804
1805bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1806 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001807 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07001808 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001809 }
locke-lunarg61870c22020-06-09 14:51:50 -06001810
locke-lunarg1ae57d62020-11-18 10:49:19 -07001811 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001812 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001813 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1814 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001815 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1816 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001817 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001818 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 -06001819 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001820 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001821 }
1822
1823 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1824 // We will detect more accurate range in the future.
1825 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1826 return skip;
1827}
1828
1829void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07001830 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) return;
locke-lunarg61870c22020-06-09 14:51:50 -06001831
locke-lunarg1ae57d62020-11-18 10:49:19 -07001832 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001833 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06001834 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
1835 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06001836 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1837
1838 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1839 // We will detect more accurate range in the future.
1840 RecordDrawVertex(UINT32_MAX, 0, tag);
1841}
1842
1843bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06001844 bool skip = false;
1845 if (!current_renderpass_context_) return skip;
1846 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1847 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1848 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001849}
1850
1851void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001852 if (current_renderpass_context_) {
locke-lunarg7077d502020-06-18 21:37:26 -06001853 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1854 tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001855 }
locke-lunarg61870c22020-06-09 14:51:50 -06001856}
1857
John Zulauf355e49b2020-04-24 15:11:15 -06001858bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001859 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001860 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06001861 skip |=
1862 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001863
1864 return skip;
1865}
1866
1867bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1868 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001869 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001870 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001871 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06001872 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1873 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001874
1875 return skip;
1876}
1877
1878void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1879 assert(sync_state_);
1880 if (!cb_state_) return;
1881
1882 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06001883 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06001884 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06001885 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001886 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001887}
1888
John Zulauf355e49b2020-04-24 15:11:15 -06001889void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001890 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001891 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001892 current_context_ = &current_renderpass_context_->CurrentContext();
1893}
1894
John Zulauf355e49b2020-04-24 15:11:15 -06001895void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001896 assert(current_renderpass_context_);
1897 if (!current_renderpass_context_) return;
1898
John Zulauf1a224292020-06-30 14:52:13 -06001899 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001900 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001901 current_renderpass_context_ = nullptr;
1902}
1903
locke-lunarg61870c22020-06-09 14:51:50 -06001904bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1905 const VkRect2D &render_area, const char *func_name) const {
1906 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001907 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
1908 if (!pipe ||
1909 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001910 return skip;
1911 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001912 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001913 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1914 VkExtent3D extent = CastTo3D(render_area.extent);
1915 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001916
John Zulauf1a224292020-06-30 14:52:13 -06001917 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001918 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001919 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1920 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001921 if (location >= subpass.colorAttachmentCount ||
1922 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001923 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001924 }
locke-lunarg96dc9632020-06-10 17:22:18 -06001925 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001926 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1927 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06001928 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001929 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001930 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001931 func_name, string_SyncHazard(hazard.hazard),
1932 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1933 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001934 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001935 }
1936 }
1937 }
locke-lunarg37047832020-06-12 13:44:45 -06001938
1939 // PHASE1 TODO: Add layout based read/vs. write selection.
1940 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001941 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06001942 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001943 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001944 bool depth_write = false, stencil_write = false;
1945
1946 // PHASE1 TODO: These validation should be in core_checks.
1947 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001948 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1949 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06001950 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1951 depth_write = true;
1952 }
1953 // PHASE1 TODO: It needs to check if stencil is writable.
1954 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1955 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1956 // PHASE1 TODO: These validation should be in core_checks.
1957 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001958 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06001959 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1960 stencil_write = true;
1961 }
1962
1963 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1964 if (depth_write) {
1965 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001966 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1967 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001968 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001969 skip |= sync_state.LogError(
1970 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001971 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001972 func_name, string_SyncHazard(hazard.hazard),
1973 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1974 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001975 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001976 }
1977 }
1978 if (stencil_write) {
1979 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001980 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1981 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001982 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001983 skip |= sync_state.LogError(
1984 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001985 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001986 func_name, string_SyncHazard(hazard.hazard),
1987 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1988 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001989 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001990 }
locke-lunarg61870c22020-06-09 14:51:50 -06001991 }
1992 }
1993 return skip;
1994}
1995
locke-lunarg96dc9632020-06-10 17:22:18 -06001996void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1997 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001998 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
1999 if (!pipe ||
2000 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002001 return;
2002 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002003 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002004 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
2005 VkExtent3D extent = CastTo3D(render_area.extent);
2006 VkOffset3D offset = CastTo3D(render_area.offset);
2007
John Zulauf1a224292020-06-30 14:52:13 -06002008 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002009 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002010 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2011 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002012 if (location >= subpass.colorAttachmentCount ||
2013 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002014 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002015 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002016 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002017 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
2018 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002019 }
2020 }
locke-lunarg37047832020-06-12 13:44:45 -06002021
2022 // PHASE1 TODO: Add layout based read/vs. write selection.
2023 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002024 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002025 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002026 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002027 bool depth_write = false, stencil_write = false;
2028
2029 // PHASE1 TODO: These validation should be in core_checks.
2030 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002031 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2032 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002033 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2034 depth_write = true;
2035 }
2036 // PHASE1 TODO: It needs to check if stencil is writable.
2037 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2038 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2039 // PHASE1 TODO: These validation should be in core_checks.
2040 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002041 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002042 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2043 stencil_write = true;
2044 }
2045
2046 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2047 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002048 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2049 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002050 }
2051 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002052 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2053 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002054 }
locke-lunarg61870c22020-06-09 14:51:50 -06002055 }
2056}
2057
John Zulauf1507ee42020-05-18 11:33:09 -06002058bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
2059 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002060 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002061 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06002062 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2063 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002064 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2065 func_name);
2066
John Zulauf355e49b2020-04-24 15:11:15 -06002067 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002068 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06002069 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002070 if (!skip) {
2071 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2072 // on a copy of the (empty) next context.
2073 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2074 AccessContext temp_context(next_context);
2075 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
2076 skip |= temp_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
2077 }
John Zulauf7635de32020-05-29 17:14:15 -06002078 return skip;
2079}
2080bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
2081 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002082 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002083 bool skip = false;
2084 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2085 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002086 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2087 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06002088 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002089 return skip;
2090}
2091
John Zulauf7635de32020-05-29 17:14:15 -06002092AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2093 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2094}
2095
2096bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2097 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002098 bool skip = false;
2099
John Zulauf7635de32020-05-29 17:14:15 -06002100 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2101 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2102 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2103 // to apply and only copy then, if this proves a hot spot.
2104 std::unique_ptr<AccessContext> proxy_for_current;
2105
John Zulauf355e49b2020-04-24 15:11:15 -06002106 // Validate the "finalLayout" transitions to external
2107 // Get them from where there we're hidding in the extra entry.
2108 const auto &final_transitions = rp_state_->subpass_transitions.back();
2109 for (const auto &transition : final_transitions) {
2110 const auto &attach_view = attachment_views_[transition.attachment];
2111 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2112 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002113 auto *context = trackback.context;
2114
2115 if (transition.prev_pass == current_subpass_) {
2116 if (!proxy_for_current) {
2117 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2118 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2119 }
2120 context = proxy_for_current.get();
2121 }
2122
John Zulaufa0a98292020-09-18 09:30:10 -06002123 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2124 const auto merged_barrier = MergeBarriers(trackback.barriers);
2125 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope,
2126 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2127 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002128 if (hazard.hazard) {
2129 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2130 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf389c34b2020-07-28 11:19:35 -06002131 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002132 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf389c34b2020-07-28 11:19:35 -06002133 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf37ceaed2020-07-03 16:18:15 -06002134 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002135 }
2136 }
2137 return skip;
2138}
2139
2140void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2141 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002142 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002143}
2144
John Zulauf1507ee42020-05-18 11:33:09 -06002145void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2146 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2147 auto &subpass_context = subpass_contexts_[current_subpass_];
2148 VkExtent3D extent = CastTo3D(render_area.extent);
2149 VkOffset3D offset = CastTo3D(render_area.offset);
2150
2151 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2152 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2153 if (attachment_views_[i] == nullptr) continue; // UNUSED
2154 const auto &view = *attachment_views_[i];
2155 const IMAGE_STATE *image = view.image_state.get();
2156 if (image == nullptr) continue;
2157
2158 const auto &ci = attachment_ci[i];
2159 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002160 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002161 const bool is_color = !(has_depth || has_stencil);
2162
2163 if (is_color) {
2164 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2165 extent, tag);
2166 } else {
2167 auto update_range = view.normalized_subresource_range;
2168 if (has_depth) {
2169 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2170 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2171 }
2172 if (has_stencil) {
2173 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2174 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2175 tag);
2176 }
2177 }
2178 }
2179 }
2180}
2181
John Zulauf355e49b2020-04-24 15:11:15 -06002182void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002183 const AccessContext *external_context, VkQueueFlags queue_flags,
2184 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002185 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002186 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002187 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2188 // Add this for all subpasses here so that they exsist during next subpass validation
2189 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002190 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002191 }
2192 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2193
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002194 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002195 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002196 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002197}
John Zulauf1507ee42020-05-18 11:33:09 -06002198
2199void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002200 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2201 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002202 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002203
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002204 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2205 // subpass, so their tag needs to be different from the layout and load operations below.
2206 ResourceUsageTag next_tag = tag;
2207 next_tag.index++;
John Zulauf355e49b2020-04-24 15:11:15 -06002208 current_subpass_++;
2209 assert(current_subpass_ < subpass_contexts_.size());
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002210 subpass_contexts_[current_subpass_].SetStartTag(next_tag);
2211 RecordLayoutTransitions(next_tag);
2212 RecordLoadOperations(render_area, next_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002213}
2214
John Zulauf1a224292020-06-30 14:52:13 -06002215void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2216 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002217 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002218 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002219 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002220
John Zulauf355e49b2020-04-24 15:11:15 -06002221 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002222 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002223
2224 // Add the "finalLayout" transitions to external
2225 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002226 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2227 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2228 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002229 const auto &final_transitions = rp_state_->subpass_transitions.back();
2230 for (const auto &transition : final_transitions) {
2231 const auto &attachment = attachment_views_[transition.attachment];
2232 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002233 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf89311b42020-09-29 16:28:47 -06002234 ApplyBarrierOpsFunctor barrier_ops(true /* resolve */, last_trackback.barriers, true /* layout transition */, tag);
2235 external_context->UpdateResourceAccess(*attachment->image_state, attachment->normalized_subresource_range, barrier_ops);
John Zulauf355e49b2020-04-24 15:11:15 -06002236 }
2237}
2238
John Zulauf3d84f1b2020-03-09 13:33:25 -06002239SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2240 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2241 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2242 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2243 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2244 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2245 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2246}
2247
John Zulaufb02c1eb2020-10-06 16:33:36 -06002248// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2249void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2250 for (const auto &barrier : barriers) {
2251 ApplyBarrier(barrier, layout_transition);
2252 }
2253}
2254
John Zulauf89311b42020-09-29 16:28:47 -06002255// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2256// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2257// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002258void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2259 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002260 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002261 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002262 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002263 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002264 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002265 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002266}
John Zulauf9cb530d2019-09-30 14:14:10 -06002267HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2268 HazardResult hazard;
2269 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002270 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002271 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002272 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002273 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002274 }
2275 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002276 // Write operation:
2277 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2278 // If reads exists -- test only against them because either:
2279 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2280 // * 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
2281 // the current write happens after the reads, so just test the write against the reades
2282 // Otherwise test against last_write
2283 //
2284 // Look for casus belli for WAR
2285 if (last_read_count) {
2286 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2287 const auto &read_access = last_reads[read_index];
2288 if (IsReadHazard(usage_stage, read_access)) {
2289 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2290 break;
2291 }
2292 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002293 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002294 // Write-After-Write check -- if we have a previous write to test against
2295 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002296 }
2297 }
2298 return hazard;
2299}
2300
John Zulauf69133422020-05-20 14:55:53 -06002301HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2302 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2303 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002304 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002305 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002306 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2307 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002308 if (IsRead(usage_bit)) {
2309 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2310 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2311 if (is_raw_hazard) {
2312 // NOTE: we know last_write is non-zero
2313 // See if the ordering rules save us from the simple RAW check above
2314 // First check to see if the current usage is covered by the ordering rules
2315 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2316 const bool usage_is_ordered =
2317 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2318 if (usage_is_ordered) {
2319 // Now see of the most recent write (or a subsequent read) are ordered
2320 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2321 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002322 }
2323 }
John Zulauf4285ee92020-09-23 10:20:52 -06002324 if (is_raw_hazard) {
2325 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2326 }
John Zulauf361fb532020-07-22 10:45:39 -06002327 } else {
2328 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002329 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulauf361fb532020-07-22 10:45:39 -06002330 if (last_read_count) {
John Zulauf361fb532020-07-22 10:45:39 -06002331 // Look for any WAR hazards outside the ordered set of stages
John Zulauf4285ee92020-09-23 10:20:52 -06002332 VkPipelineStageFlags ordered_stages = 0;
2333 if (usage_write_is_ordered) {
2334 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2335 ordered_stages = GetOrderedStages(ordering);
2336 }
2337 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2338 if ((ordered_stages & last_read_stages) != last_read_stages) {
2339 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2340 const auto &read_access = last_reads[read_index];
2341 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2342 if (IsReadHazard(usage_stage, read_access)) {
2343 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2344 break;
2345 }
John Zulaufd14743a2020-07-03 09:42:39 -06002346 }
2347 }
John Zulauf4285ee92020-09-23 10:20:52 -06002348 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002349 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002350 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002351 }
John Zulauf69133422020-05-20 14:55:53 -06002352 }
2353 }
2354 return hazard;
2355}
2356
John Zulauf2f952d22020-02-10 11:34:51 -07002357// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002358HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002359 HazardResult hazard;
2360 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002361 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2362 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2363 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002364 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002365 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002366 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002367 }
2368 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002369 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002370 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002371 } else if (last_read_count > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002372 // Any reads during the other subpass will conflict with this write, so we need to check them all.
2373 for (uint32_t i = 0; i < last_read_count; i++) {
2374 if (last_reads[i].tag.index >= start_tag.index) {
2375 hazard.Set(this, usage_index, WRITE_RACING_READ, last_reads[i].access, last_reads[i].tag);
2376 break;
2377 }
2378 }
John Zulauf2f952d22020-02-10 11:34:51 -07002379 }
2380 }
2381 return hazard;
2382}
2383
John Zulauf36bcf6a2020-02-03 15:12:52 -07002384HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002385 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002386 // Only supporting image layout transitions for now
2387 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2388 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002389 // only test for WAW if there no intervening read operations.
2390 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2391 if (last_read_count) {
John Zulauf355e49b2020-04-24 15:11:15 -06002392 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002393 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002394 const auto &read_access = last_reads[read_index];
2395 // If the read stage is not in the src sync sync
2396 // *AND* not execution chained with an existing sync barrier (that's the or)
2397 // then the barrier access is unsafe (R/W after R)
2398 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002399 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002400 break;
2401 }
2402 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002403 } else if (last_write.any()) {
John Zulauf361fb532020-07-22 10:45:39 -06002404 // If the previous write is *not* in the 1st access scope
2405 // *AND* the current barrier is not in the dependency chain
2406 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2407 // then the barrier access is unsafe (R/W after W)
2408 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
2409 // TODO: Do we need a difference hazard name for this?
2410 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2411 }
John Zulaufd14743a2020-07-03 09:42:39 -06002412 }
John Zulauf361fb532020-07-22 10:45:39 -06002413
John Zulauf0cb5be22020-01-23 12:18:22 -07002414 return hazard;
2415}
2416
John Zulauf5f13a792020-03-10 07:31:21 -06002417// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2418// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2419// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2420void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2421 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002422 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2423 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002424 *this = other;
2425 } else if (!other.write_tag.IsBefore(write_tag)) {
2426 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2427 // dependency chaining logic or any stage expansion)
2428 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002429 pending_write_barriers |= other.pending_write_barriers;
2430 pending_layout_transition |= other.pending_layout_transition;
2431 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002432
John Zulaufd14743a2020-07-03 09:42:39 -06002433 // Merge the read states
John Zulauf4285ee92020-09-23 10:20:52 -06002434 const auto pre_merge_count = last_read_count;
2435 const auto pre_merge_stages = last_read_stages;
John Zulauf5f13a792020-03-10 07:31:21 -06002436 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2437 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002438 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002439 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002440 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2441 // but we should wait on profiling data for that.
2442 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002443 auto &my_read = last_reads[my_read_index];
2444 if (other_read.stage == my_read.stage) {
2445 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002446 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002447 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002448 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002449 my_read.pending_dep_chain = other_read.pending_dep_chain;
2450 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
2451 // May require tracking more than one access per stage.
2452 my_read.barriers = other_read.barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002453 if (my_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
2454 // Since I'm overwriting the fragement stage read, also update the input attachment info
2455 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06002456 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002457 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002458 } else if (other_read.tag.IsBefore(my_read.tag)) {
2459 // The read tags match so merge the barriers
2460 my_read.barriers |= other_read.barriers;
2461 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002462 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002463
John Zulauf5f13a792020-03-10 07:31:21 -06002464 break;
2465 }
2466 }
2467 } else {
2468 // The other read stage doesn't exist in this, so add it.
2469 last_reads[last_read_count] = other_read;
2470 last_read_count++;
2471 last_read_stages |= other_read.stage;
John Zulauf4285ee92020-09-23 10:20:52 -06002472 if (other_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06002473 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002474 }
John Zulauf5f13a792020-03-10 07:31:21 -06002475 }
2476 }
John Zulauf361fb532020-07-22 10:45:39 -06002477 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002478 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2479 // ignore it.
John Zulauf5f13a792020-03-10 07:31:21 -06002480}
2481
John Zulauf9cb530d2019-09-30 14:14:10 -06002482void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2483 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2484 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002485 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002486 // Mulitple outstanding reads may be of interest and do dependency chains independently
2487 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2488 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002489 uint32_t update_index = kStageCount;
John Zulauf9cb530d2019-09-30 14:14:10 -06002490 if (usage_stage & last_read_stages) {
2491 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf4285ee92020-09-23 10:20:52 -06002492 if (last_reads[read_index].stage == usage_stage) {
2493 update_index = read_index;
John Zulauf9cb530d2019-09-30 14:14:10 -06002494 break;
2495 }
2496 }
John Zulauf4285ee92020-09-23 10:20:52 -06002497 assert(update_index < last_read_count);
John Zulauf9cb530d2019-09-30 14:14:10 -06002498 } else {
John Zulauf9cb530d2019-09-30 14:14:10 -06002499 assert(last_read_count < last_reads.size());
John Zulauf4285ee92020-09-23 10:20:52 -06002500 update_index = last_read_count++;
John Zulauf9cb530d2019-09-30 14:14:10 -06002501 last_read_stages |= usage_stage;
2502 }
John Zulauf4285ee92020-09-23 10:20:52 -06002503 last_reads[update_index].Set(usage_stage, usage_bit, 0, tag);
2504
2505 // Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
2506 if (usage_stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06002507 // TODO Revisit re: multiple reads for a given stage
2508 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002509 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002510 } else {
2511 // Assume write
2512 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002513 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002514 }
2515}
John Zulauf5f13a792020-03-10 07:31:21 -06002516
John Zulauf89311b42020-09-29 16:28:47 -06002517// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2518// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2519// We can overwrite them as *this* write is now after them.
2520//
2521// 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 -07002522void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulauf89311b42020-09-29 16:28:47 -06002523 last_read_count = 0;
2524 last_read_stages = 0;
2525 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06002526 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06002527
2528 write_barriers = 0;
2529 write_dependency_chain = 0;
2530 write_tag = tag;
2531 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002532}
2533
John Zulauf89311b42020-09-29 16:28:47 -06002534// Apply the memory barrier without updating the existing barriers. The execution barrier
2535// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2536// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2537// replace the current write barriers or add to them, so accumulate to pending as well.
2538void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2539 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2540 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06002541 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
2542 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
2543 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
2544 // transistion *as* a write and in scope with the barrier (it's before visibility).
2545 if (layout_transition || InSourceScopeOrChain(barrier.src_exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06002546 pending_write_barriers |= barrier.dst_access_scope;
2547 pending_write_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002548 }
John Zulauf89311b42020-09-29 16:28:47 -06002549 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2550 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06002551
John Zulauf89311b42020-09-29 16:28:47 -06002552 if (!pending_layout_transition) {
2553 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2554 // don't need to be tracked as we're just going to zero them.
John Zulaufa0a98292020-09-18 09:30:10 -06002555 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf89311b42020-09-29 16:28:47 -06002556 ReadState &access = last_reads[read_index];
2557 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2558 if (barrier.src_exec_scope & (access.stage | access.barriers)) {
2559 access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002560 }
2561 }
John Zulaufa0a98292020-09-18 09:30:10 -06002562 }
John Zulaufa0a98292020-09-18 09:30:10 -06002563}
2564
John Zulauf89311b42020-09-29 16:28:47 -06002565void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
2566 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06002567 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
2568 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
2569 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06002570 }
John Zulauf89311b42020-09-29 16:28:47 -06002571
2572 // Apply the accumulate execution barriers (and thus update chaining information)
2573 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
2574 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2575 ReadState &access = last_reads[read_index];
2576 access.barriers |= access.pending_dep_chain;
2577 read_execution_barriers |= access.barriers;
2578 access.pending_dep_chain = 0;
2579 }
2580
2581 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
2582 write_dependency_chain |= pending_write_dep_chain;
2583 write_barriers |= pending_write_barriers;
2584 pending_write_dep_chain = 0;
2585 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002586}
2587
John Zulauf59e25072020-07-17 10:55:21 -06002588// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002589VkPipelineStageFlags ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
John Zulauf59e25072020-07-17 10:55:21 -06002590 VkPipelineStageFlags barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06002591
2592 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2593 const auto &read_access = last_reads[read_index];
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002594 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06002595 barriers = read_access.barriers;
2596 break;
John Zulauf59e25072020-07-17 10:55:21 -06002597 }
2598 }
John Zulauf4285ee92020-09-23 10:20:52 -06002599
John Zulauf59e25072020-07-17 10:55:21 -06002600 return barriers;
2601}
2602
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002603inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlagBits usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06002604 assert(IsRead(usage));
2605 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
2606 // * the previous reads are not hazards, and thus last_write must be visible and available to
2607 // any reads that happen after.
2608 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2609 // 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 -07002610 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06002611}
2612
John Zulauf4285ee92020-09-23 10:20:52 -06002613VkPipelineStageFlags ResourceAccessState::GetOrderedStages(const SyncOrderingBarrier &ordering) const {
2614 // Whether the stage are in the ordering scope only matters if the current write is ordered
2615 VkPipelineStageFlags ordered_stages = last_read_stages & ordering.exec_scope;
2616 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002617 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06002618 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06002619 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
2620 ordered_stages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
2621 }
2622
2623 return ordered_stages;
2624}
2625
2626inline ResourceAccessState::ReadState *ResourceAccessState::GetReadStateForStage(VkPipelineStageFlagBits stage,
2627 uint32_t search_limit) {
2628 ReadState *read_state = nullptr;
2629 search_limit = std::min(search_limit, last_read_count);
2630 for (uint32_t i = 0; i < search_limit; i++) {
2631 if (last_reads[i].stage == stage) {
2632 read_state = &last_reads[i];
2633 break;
2634 }
2635 }
2636 return read_state;
2637}
2638
John Zulaufd1f85d42020-04-15 12:23:15 -06002639void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002640 auto *access_context = GetAccessContextNoInsert(command_buffer);
2641 if (access_context) {
2642 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002643 }
2644}
2645
John Zulaufd1f85d42020-04-15 12:23:15 -06002646void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2647 auto access_found = cb_access_state.find(command_buffer);
2648 if (access_found != cb_access_state.end()) {
2649 access_found->second->Reset();
2650 cb_access_state.erase(access_found);
2651 }
2652}
2653
John Zulauf89311b42020-09-29 16:28:47 -06002654void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2655 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags src_access_scope,
2656 SyncStageAccessFlags dst_access_scope, uint32_t memory_barrier_count,
2657 const VkMemoryBarrier *pMemoryBarriers, const ResourceUsageTag &tag) {
2658 ApplyBarrierOpsFunctor barriers_functor(true /* resolve */, std::min<uint32_t>(1, memory_barrier_count), tag);
2659 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
2660 const auto &barrier = pMemoryBarriers[barrier_index];
2661 SyncBarrier sync_barrier(src_exec_scope, SyncStageAccess::AccessScope(src_access_scope, barrier.srcAccessMask),
2662 dst_exec_scope, SyncStageAccess::AccessScope(dst_access_scope, barrier.dstAccessMask));
2663 barriers_functor.PushBack(sync_barrier, false);
2664 }
2665 if (0 == memory_barrier_count) {
2666 // If there are no global memory barriers, force an exec barrier
2667 barriers_functor.PushBack(SyncBarrier(src_exec_scope, 0, dst_exec_scope, 0), false);
2668 }
John Zulauf540266b2020-04-06 18:54:53 -06002669 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002670}
2671
John Zulauf540266b2020-04-06 18:54:53 -06002672void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002673 const SyncStageAccessFlags &src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2674 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002675 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002676 for (uint32_t index = 0; index < barrier_count; index++) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002677 auto barrier = barriers[index]; // barrier is a copy
John Zulauf9cb530d2019-09-30 14:14:10 -06002678 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2679 if (!buffer) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002680 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
2681 const ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002682 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2683 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06002684 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2685 const ApplyBarrierFunctor update_action(sync_barrier, false /* layout_transition */);
2686 context->UpdateResourceAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002687 }
2688}
2689
John Zulauf540266b2020-04-06 18:54:53 -06002690void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002691 const SyncStageAccessFlags &src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2692 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002693 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002694 for (uint32_t index = 0; index < barrier_count; index++) {
2695 const auto &barrier = barriers[index];
2696 const auto *image = Get<IMAGE_STATE>(barrier.image);
2697 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002698 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002699 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2700 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2701 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06002702 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2703 const ApplyBarrierFunctor barrier_action(sync_barrier, layout_transition);
2704 context->UpdateResourceAccess(*image, subresource_range, barrier_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002705 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002706}
2707
2708bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2709 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2710 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002711 const auto *cb_context = GetAccessContext(commandBuffer);
2712 assert(cb_context);
2713 if (!cb_context) return skip;
2714 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002715
John Zulauf3d84f1b2020-03-09 13:33:25 -06002716 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002717 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002718 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002719
2720 for (uint32_t region = 0; region < regionCount; region++) {
2721 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002722 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002723 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002724 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002725 if (hazard.hazard) {
2726 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002727 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002728 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002729 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002730 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002731 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002732 }
John Zulauf16adfc92020-04-08 10:28:33 -06002733 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002734 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06002735 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002736 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002737 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002738 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002739 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002740 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002741 }
2742 }
2743 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002744 }
2745 return skip;
2746}
2747
2748void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2749 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002750 auto *cb_context = GetAccessContext(commandBuffer);
2751 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002752 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002753 auto *context = cb_context->GetCurrentAccessContext();
2754
John Zulauf9cb530d2019-09-30 14:14:10 -06002755 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002756 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002757
2758 for (uint32_t region = 0; region < regionCount; region++) {
2759 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002760 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002761 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002762 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002763 }
John Zulauf16adfc92020-04-08 10:28:33 -06002764 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06002765 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002766 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002767 }
2768 }
2769}
2770
Jeff Leger178b1e52020-10-05 12:22:23 -04002771bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
2772 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
2773 bool skip = false;
2774 const auto *cb_context = GetAccessContext(commandBuffer);
2775 assert(cb_context);
2776 if (!cb_context) return skip;
2777 const auto *context = cb_context->GetCurrentAccessContext();
2778
2779 // If we have no previous accesses, we have no hazards
2780 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2781 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2782
2783 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2784 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2785 if (src_buffer) {
2786 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2787 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
2788 if (hazard.hazard) {
2789 // TODO -- add tag information to log msg when useful.
2790 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
2791 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
2792 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
2793 region, string_UsageTag(hazard).c_str());
2794 }
2795 }
2796 if (dst_buffer && !skip) {
2797 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2798 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
2799 if (hazard.hazard) {
2800 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
2801 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
2802 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
2803 region, string_UsageTag(hazard).c_str());
2804 }
2805 }
2806 if (skip) break;
2807 }
2808 return skip;
2809}
2810
2811void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
2812 auto *cb_context = GetAccessContext(commandBuffer);
2813 assert(cb_context);
2814 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
2815 auto *context = cb_context->GetCurrentAccessContext();
2816
2817 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
2818 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
2819
2820 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
2821 const auto &copy_region = pCopyBufferInfos->pRegions[region];
2822 if (src_buffer) {
2823 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
2824 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
2825 }
2826 if (dst_buffer) {
2827 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
2828 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
2829 }
2830 }
2831}
2832
John Zulauf5c5e88d2019-12-26 11:22:02 -07002833bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2834 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2835 const VkImageCopy *pRegions) const {
2836 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002837 const auto *cb_access_context = GetAccessContext(commandBuffer);
2838 assert(cb_access_context);
2839 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002840
John Zulauf3d84f1b2020-03-09 13:33:25 -06002841 const auto *context = cb_access_context->GetCurrentAccessContext();
2842 assert(context);
2843 if (!context) return skip;
2844
2845 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2846 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002847 for (uint32_t region = 0; region < regionCount; region++) {
2848 const auto &copy_region = pRegions[region];
2849 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002850 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002851 copy_region.srcOffset, copy_region.extent);
2852 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002853 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002854 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002855 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002856 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002857 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002858 }
2859
2860 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002861 VkExtent3D dst_copy_extent =
2862 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002863 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002864 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002865 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002866 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002867 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002868 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002869 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002870 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002871 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002872 }
2873 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002874
John Zulauf5c5e88d2019-12-26 11:22:02 -07002875 return skip;
2876}
2877
2878void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2879 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2880 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002881 auto *cb_access_context = GetAccessContext(commandBuffer);
2882 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002883 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002884 auto *context = cb_access_context->GetCurrentAccessContext();
2885 assert(context);
2886
John Zulauf5c5e88d2019-12-26 11:22:02 -07002887 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002888 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002889
2890 for (uint32_t region = 0; region < regionCount; region++) {
2891 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002892 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002893 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2894 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002895 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002896 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002897 VkExtent3D dst_copy_extent =
2898 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002899 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2900 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002901 }
2902 }
2903}
2904
Jeff Leger178b1e52020-10-05 12:22:23 -04002905bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
2906 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
2907 bool skip = false;
2908 const auto *cb_access_context = GetAccessContext(commandBuffer);
2909 assert(cb_access_context);
2910 if (!cb_access_context) return skip;
2911
2912 const auto *context = cb_access_context->GetCurrentAccessContext();
2913 assert(context);
2914 if (!context) return skip;
2915
2916 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2917 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2918 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2919 const auto &copy_region = pCopyImageInfo->pRegions[region];
2920 if (src_image) {
2921 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
2922 copy_region.srcOffset, copy_region.extent);
2923 if (hazard.hazard) {
2924 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
2925 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
2926 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
2927 region, string_UsageTag(hazard).c_str());
2928 }
2929 }
2930
2931 if (dst_image) {
2932 VkExtent3D dst_copy_extent =
2933 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2934 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
2935 copy_region.dstOffset, dst_copy_extent);
2936 if (hazard.hazard) {
2937 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
2938 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
2939 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
2940 region, string_UsageTag(hazard).c_str());
2941 }
2942 if (skip) break;
2943 }
2944 }
2945
2946 return skip;
2947}
2948
2949void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
2950 auto *cb_access_context = GetAccessContext(commandBuffer);
2951 assert(cb_access_context);
2952 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
2953 auto *context = cb_access_context->GetCurrentAccessContext();
2954 assert(context);
2955
2956 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
2957 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
2958
2959 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
2960 const auto &copy_region = pCopyImageInfo->pRegions[region];
2961 if (src_image) {
2962 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2963 copy_region.extent, tag);
2964 }
2965 if (dst_image) {
2966 VkExtent3D dst_copy_extent =
2967 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
2968 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2969 dst_copy_extent, tag);
2970 }
2971 }
2972}
2973
John Zulauf9cb530d2019-09-30 14:14:10 -06002974bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2975 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2976 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2977 uint32_t bufferMemoryBarrierCount,
2978 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2979 uint32_t imageMemoryBarrierCount,
2980 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2981 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002982 const auto *cb_access_context = GetAccessContext(commandBuffer);
2983 assert(cb_access_context);
2984 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002985
John Zulauf3d84f1b2020-03-09 13:33:25 -06002986 const auto *context = cb_access_context->GetCurrentAccessContext();
2987 assert(context);
2988 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002989
John Zulauf3d84f1b2020-03-09 13:33:25 -06002990 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002991 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2992 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002993 // Validate Image Layout transitions
2994 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2995 const auto &barrier = pImageMemoryBarriers[index];
2996 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2997 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2998 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002999 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07003000 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003001 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003002 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003003 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003004 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003005 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07003006 }
3007 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003008
3009 return skip;
3010}
3011
3012void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3013 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3014 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3015 uint32_t bufferMemoryBarrierCount,
3016 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3017 uint32_t imageMemoryBarrierCount,
3018 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003019 auto *cb_access_context = GetAccessContext(commandBuffer);
3020 assert(cb_access_context);
3021 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06003022 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003023 auto access_context = cb_access_context->GetCurrentAccessContext();
3024 assert(access_context);
3025 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003026
John Zulauf3d84f1b2020-03-09 13:33:25 -06003027 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003028 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003029 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003030 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
3031 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
3032 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf89311b42020-09-29 16:28:47 -06003033
3034 // These two apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
3035 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
3036 // of the barriers is maintained.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003037 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
3038 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06003039 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06003040 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003041
John Zulauf89311b42020-09-29 16:28:47 -06003042 // Apply the global barriers last as is it walks all memory, it can also clean up the "pending" state without requiring an
3043 // additional pass, updating the dependency chains *last* as it goes along.
3044 // This is needed to guarantee order independence of the three lists.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003045 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf89311b42020-09-29 16:28:47 -06003046 pMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003047}
3048
3049void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3050 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3051 // The state tracker sets up the device state
3052 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3053
John Zulauf5f13a792020-03-10 07:31:21 -06003054 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3055 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003056 // TODO: Find a good way to do this hooklessly.
3057 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3058 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3059 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3060
John Zulaufd1f85d42020-04-15 12:23:15 -06003061 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3062 sync_device_state->ResetCommandBufferCallback(command_buffer);
3063 });
3064 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3065 sync_device_state->FreeCommandBufferCallback(command_buffer);
3066 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003067}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003068
John Zulauf355e49b2020-04-24 15:11:15 -06003069bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3070 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
3071 bool skip = false;
3072 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
3073 auto cb_context = GetAccessContext(commandBuffer);
3074
3075 if (rp_state && cb_context) {
3076 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
3077 }
3078
3079 return skip;
3080}
3081
3082bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3083 VkSubpassContents contents) const {
3084 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3085 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3086 subpass_begin_info.contents = contents;
3087 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
3088 return skip;
3089}
3090
3091bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3092 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
3093 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3094 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
3095 return skip;
3096}
3097
3098bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3099 const VkRenderPassBeginInfo *pRenderPassBegin,
3100 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
3101 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3102 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
3103 return skip;
3104}
3105
John Zulauf3d84f1b2020-03-09 13:33:25 -06003106void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3107 VkResult result) {
3108 // The state tracker sets up the command buffer state
3109 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3110
3111 // Create/initialize the structure that trackers accesses at the command buffer scope.
3112 auto cb_access_context = GetAccessContext(commandBuffer);
3113 assert(cb_access_context);
3114 cb_access_context->Reset();
3115}
3116
3117void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06003118 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003119 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003120 if (cb_context) {
3121 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003122 }
3123}
3124
3125void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3126 VkSubpassContents contents) {
3127 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3128 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3129 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003130 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003131}
3132
3133void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3134 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3135 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003136 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003137}
3138
3139void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3140 const VkRenderPassBeginInfo *pRenderPassBegin,
3141 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3142 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003143 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
3144}
3145
3146bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
3147 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
3148 bool skip = false;
3149
3150 auto cb_context = GetAccessContext(commandBuffer);
3151 assert(cb_context);
3152 auto cb_state = cb_context->GetCommandBufferState();
3153 if (!cb_state) return skip;
3154
3155 auto rp_state = cb_state->activeRenderPass;
3156 if (!rp_state) return skip;
3157
3158 skip |= cb_context->ValidateNextSubpass(func_name);
3159
3160 return skip;
3161}
3162
3163bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3164 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
3165 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3166 subpass_begin_info.contents = contents;
3167 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
3168 return skip;
3169}
3170
3171bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
3172 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3173 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3174 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
3175 return skip;
3176}
3177
3178bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3179 const VkSubpassEndInfo *pSubpassEndInfo) const {
3180 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3181 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
3182 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003183}
3184
3185void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003186 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003187 auto cb_context = GetAccessContext(commandBuffer);
3188 assert(cb_context);
3189 auto cb_state = cb_context->GetCommandBufferState();
3190 if (!cb_state) return;
3191
3192 auto rp_state = cb_state->activeRenderPass;
3193 if (!rp_state) return;
3194
John Zulauf355e49b2020-04-24 15:11:15 -06003195 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003196}
3197
3198void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3199 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
3200 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3201 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003202 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003203}
3204
3205void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3206 const VkSubpassEndInfo *pSubpassEndInfo) {
3207 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003208 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003209}
3210
3211void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3212 const VkSubpassEndInfo *pSubpassEndInfo) {
3213 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003214 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003215}
3216
John Zulauf355e49b2020-04-24 15:11:15 -06003217bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
3218 const char *func_name) const {
3219 bool skip = false;
3220
3221 auto cb_context = GetAccessContext(commandBuffer);
3222 assert(cb_context);
3223 auto cb_state = cb_context->GetCommandBufferState();
3224 if (!cb_state) return skip;
3225
3226 auto rp_state = cb_state->activeRenderPass;
3227 if (!rp_state) return skip;
3228
3229 skip |= cb_context->ValidateEndRenderpass(func_name);
3230 return skip;
3231}
3232
3233bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3234 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
3235 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
3236 return skip;
3237}
3238
3239bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
3240 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3241 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
3242 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
3243 return skip;
3244}
3245
3246bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
3247 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
3248 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
3249 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
3250 return skip;
3251}
3252
3253void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3254 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003255 // Resolve the all subpass contexts to the command buffer contexts
3256 auto cb_context = GetAccessContext(commandBuffer);
3257 assert(cb_context);
3258 auto cb_state = cb_context->GetCommandBufferState();
3259 if (!cb_state) return;
3260
locke-lunargaecf2152020-05-12 17:15:41 -06003261 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06003262 if (!rp_state) return;
3263
John Zulauf355e49b2020-04-24 15:11:15 -06003264 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06003265}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003266
John Zulauf33fc1d52020-07-17 11:01:10 -06003267// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3268// updates to a resource which do not conflict at the byte level.
3269// TODO: Revisit this rule to see if it needs to be tighter or looser
3270// TODO: Add programatic control over suppression heuristics
3271bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3272 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3273}
3274
John Zulauf3d84f1b2020-03-09 13:33:25 -06003275void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003276 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003277 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003278}
3279
3280void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003281 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003282 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003283}
3284
3285void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003286 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003287 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003288}
locke-lunarga19c71d2020-03-02 18:17:04 -07003289
Jeff Leger178b1e52020-10-05 12:22:23 -04003290template <typename BufferImageCopyRegionType>
3291bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3292 VkImageLayout dstImageLayout, uint32_t regionCount,
3293 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003294 bool skip = false;
3295 const auto *cb_access_context = GetAccessContext(commandBuffer);
3296 assert(cb_access_context);
3297 if (!cb_access_context) return skip;
3298
Jeff Leger178b1e52020-10-05 12:22:23 -04003299 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3300 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3301
locke-lunarga19c71d2020-03-02 18:17:04 -07003302 const auto *context = cb_access_context->GetCurrentAccessContext();
3303 assert(context);
3304 if (!context) return skip;
3305
3306 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003307 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3308
3309 for (uint32_t region = 0; region < regionCount; region++) {
3310 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003311 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003312 ResourceAccessRange src_range =
3313 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003314 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003315 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003316 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003317 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003318 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003319 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003320 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003321 }
3322 }
3323 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003324 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003325 copy_region.imageOffset, copy_region.imageExtent);
3326 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003327 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003328 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003329 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003330 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003331 }
3332 if (skip) break;
3333 }
3334 if (skip) break;
3335 }
3336 return skip;
3337}
3338
Jeff Leger178b1e52020-10-05 12:22:23 -04003339bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3340 VkImageLayout dstImageLayout, uint32_t regionCount,
3341 const VkBufferImageCopy *pRegions) const {
3342 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3343 COPY_COMMAND_VERSION_1);
3344}
3345
3346bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3347 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3348 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3349 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3350 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3351}
3352
3353template <typename BufferImageCopyRegionType>
3354void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3355 VkImageLayout dstImageLayout, uint32_t regionCount,
3356 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003357 auto *cb_access_context = GetAccessContext(commandBuffer);
3358 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003359
3360 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3361 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3362
3363 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003364 auto *context = cb_access_context->GetCurrentAccessContext();
3365 assert(context);
3366
3367 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003368 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003369
3370 for (uint32_t region = 0; region < regionCount; region++) {
3371 const auto &copy_region = pRegions[region];
3372 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003373 ResourceAccessRange src_range =
3374 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003375 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003376 }
3377 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003378 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003379 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003380 }
3381 }
3382}
3383
Jeff Leger178b1e52020-10-05 12:22:23 -04003384void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3385 VkImageLayout dstImageLayout, uint32_t regionCount,
3386 const VkBufferImageCopy *pRegions) {
3387 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3388 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3389}
3390
3391void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3392 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3393 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3394 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3395 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3396 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3397}
3398
3399template <typename BufferImageCopyRegionType>
3400bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3401 VkBuffer dstBuffer, uint32_t regionCount,
3402 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003403 bool skip = false;
3404 const auto *cb_access_context = GetAccessContext(commandBuffer);
3405 assert(cb_access_context);
3406 if (!cb_access_context) return skip;
3407
Jeff Leger178b1e52020-10-05 12:22:23 -04003408 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3409 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3410
locke-lunarga19c71d2020-03-02 18:17:04 -07003411 const auto *context = cb_access_context->GetCurrentAccessContext();
3412 assert(context);
3413 if (!context) return skip;
3414
3415 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3416 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3417 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3418 for (uint32_t region = 0; region < regionCount; region++) {
3419 const auto &copy_region = pRegions[region];
3420 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003421 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003422 copy_region.imageOffset, copy_region.imageExtent);
3423 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003424 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003425 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003426 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003427 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003428 }
3429 }
3430 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06003431 ResourceAccessRange dst_range =
3432 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003433 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003434 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003435 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003436 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003437 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003438 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003439 }
3440 }
3441 if (skip) break;
3442 }
3443 return skip;
3444}
3445
Jeff Leger178b1e52020-10-05 12:22:23 -04003446bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3447 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3448 const VkBufferImageCopy *pRegions) const {
3449 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3450 COPY_COMMAND_VERSION_1);
3451}
3452
3453bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3454 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3455 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3456 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3457 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3458}
3459
3460template <typename BufferImageCopyRegionType>
3461void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3462 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3463 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003464 auto *cb_access_context = GetAccessContext(commandBuffer);
3465 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003466
3467 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3468 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3469
3470 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003471 auto *context = cb_access_context->GetCurrentAccessContext();
3472 assert(context);
3473
3474 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003475 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3476 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 -06003477 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003478
3479 for (uint32_t region = 0; region < regionCount; region++) {
3480 const auto &copy_region = pRegions[region];
3481 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003482 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003483 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003484 }
3485 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003486 ResourceAccessRange dst_range =
3487 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003488 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003489 }
3490 }
3491}
3492
Jeff Leger178b1e52020-10-05 12:22:23 -04003493void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3494 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3495 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3496 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3497}
3498
3499void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3500 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3501 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3502 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3503 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3504 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3505}
3506
3507template <typename RegionType>
3508bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3509 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3510 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003511 bool skip = false;
3512 const auto *cb_access_context = GetAccessContext(commandBuffer);
3513 assert(cb_access_context);
3514 if (!cb_access_context) return skip;
3515
3516 const auto *context = cb_access_context->GetCurrentAccessContext();
3517 assert(context);
3518 if (!context) return skip;
3519
3520 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3521 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3522
3523 for (uint32_t region = 0; region < regionCount; region++) {
3524 const auto &blit_region = pRegions[region];
3525 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003526 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3527 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3528 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3529 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3530 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3531 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3532 auto hazard =
3533 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003534 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003535 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003536 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003537 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003538 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003539 }
3540 }
3541
3542 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003543 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3544 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3545 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3546 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3547 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3548 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3549 auto hazard =
3550 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003551 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003552 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003553 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003554 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003555 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003556 }
3557 if (skip) break;
3558 }
3559 }
3560
3561 return skip;
3562}
3563
Jeff Leger178b1e52020-10-05 12:22:23 -04003564bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3565 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3566 const VkImageBlit *pRegions, VkFilter filter) const {
3567 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3568 "vkCmdBlitImage");
3569}
3570
3571bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3572 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3573 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3574 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3575 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3576}
3577
3578template <typename RegionType>
3579void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3580 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3581 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003582 auto *cb_access_context = GetAccessContext(commandBuffer);
3583 assert(cb_access_context);
3584 auto *context = cb_access_context->GetCurrentAccessContext();
3585 assert(context);
3586
3587 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003588 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003589
3590 for (uint32_t region = 0; region < regionCount; region++) {
3591 const auto &blit_region = pRegions[region];
3592 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003593 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3594 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3595 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3596 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3597 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3598 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3599 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003600 }
3601 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003602 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3603 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3604 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3605 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3606 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3607 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3608 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003609 }
3610 }
3611}
locke-lunarg36ba2592020-04-03 09:42:04 -06003612
Jeff Leger178b1e52020-10-05 12:22:23 -04003613void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3614 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3615 const VkImageBlit *pRegions, VkFilter filter) {
3616 auto *cb_access_context = GetAccessContext(commandBuffer);
3617 assert(cb_access_context);
3618 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3619 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3620 pRegions, filter);
3621 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3622}
3623
3624void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3625 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3626 auto *cb_access_context = GetAccessContext(commandBuffer);
3627 assert(cb_access_context);
3628 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3629 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3630 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3631 pBlitImageInfo->filter, tag);
3632}
3633
locke-lunarg61870c22020-06-09 14:51:50 -06003634bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
3635 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
3636 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003637 bool skip = false;
3638 if (drawCount == 0) return skip;
3639
3640 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3641 VkDeviceSize size = struct_size;
3642 if (drawCount == 1 || stride == size) {
3643 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003644 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003645 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3646 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003647 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003648 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003649 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003650 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003651 }
3652 } else {
3653 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003654 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003655 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3656 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003657 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003658 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3659 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3660 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003661 break;
3662 }
3663 }
3664 }
3665 return skip;
3666}
3667
locke-lunarg61870c22020-06-09 14:51:50 -06003668void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3669 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3670 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003671 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3672 VkDeviceSize size = struct_size;
3673 if (drawCount == 1 || stride == size) {
3674 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003675 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003676 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3677 } else {
3678 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003679 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003680 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3681 }
3682 }
3683}
3684
locke-lunarg61870c22020-06-09 14:51:50 -06003685bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3686 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003687 bool skip = false;
3688
3689 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003690 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003691 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3692 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003693 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003694 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003695 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003696 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003697 }
3698 return skip;
3699}
3700
locke-lunarg61870c22020-06-09 14:51:50 -06003701void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003702 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003703 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003704 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3705}
3706
locke-lunarg36ba2592020-04-03 09:42:04 -06003707bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003708 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003709 const auto *cb_access_context = GetAccessContext(commandBuffer);
3710 assert(cb_access_context);
3711 if (!cb_access_context) return skip;
3712
locke-lunarg61870c22020-06-09 14:51:50 -06003713 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003714 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003715}
3716
3717void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003718 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003719 auto *cb_access_context = GetAccessContext(commandBuffer);
3720 assert(cb_access_context);
3721 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003722
locke-lunarg61870c22020-06-09 14:51:50 -06003723 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003724}
locke-lunarge1a67022020-04-29 00:15:36 -06003725
3726bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003727 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003728 const auto *cb_access_context = GetAccessContext(commandBuffer);
3729 assert(cb_access_context);
3730 if (!cb_access_context) return skip;
3731
3732 const auto *context = cb_access_context->GetCurrentAccessContext();
3733 assert(context);
3734 if (!context) return skip;
3735
locke-lunarg61870c22020-06-09 14:51:50 -06003736 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3737 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3738 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003739 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003740}
3741
3742void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003743 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003744 auto *cb_access_context = GetAccessContext(commandBuffer);
3745 assert(cb_access_context);
3746 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3747 auto *context = cb_access_context->GetCurrentAccessContext();
3748 assert(context);
3749
locke-lunarg61870c22020-06-09 14:51:50 -06003750 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3751 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003752}
3753
3754bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3755 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003756 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003757 const auto *cb_access_context = GetAccessContext(commandBuffer);
3758 assert(cb_access_context);
3759 if (!cb_access_context) return skip;
3760
locke-lunarg61870c22020-06-09 14:51:50 -06003761 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3762 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3763 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003764 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003765}
3766
3767void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3768 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003769 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003770 auto *cb_access_context = GetAccessContext(commandBuffer);
3771 assert(cb_access_context);
3772 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003773
locke-lunarg61870c22020-06-09 14:51:50 -06003774 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3775 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3776 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003777}
3778
3779bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3780 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003781 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003782 const auto *cb_access_context = GetAccessContext(commandBuffer);
3783 assert(cb_access_context);
3784 if (!cb_access_context) return skip;
3785
locke-lunarg61870c22020-06-09 14:51:50 -06003786 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3787 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3788 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003789 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003790}
3791
3792void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3793 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003794 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003795 auto *cb_access_context = GetAccessContext(commandBuffer);
3796 assert(cb_access_context);
3797 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003798
locke-lunarg61870c22020-06-09 14:51:50 -06003799 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3800 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3801 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003802}
3803
3804bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3805 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003806 bool skip = false;
3807 if (drawCount == 0) return skip;
3808
locke-lunargff255f92020-05-13 18:53:52 -06003809 const auto *cb_access_context = GetAccessContext(commandBuffer);
3810 assert(cb_access_context);
3811 if (!cb_access_context) return skip;
3812
3813 const auto *context = cb_access_context->GetCurrentAccessContext();
3814 assert(context);
3815 if (!context) return skip;
3816
locke-lunarg61870c22020-06-09 14:51:50 -06003817 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3818 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3819 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3820 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003821
3822 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3823 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3824 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003825 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003826 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003827}
3828
3829void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3830 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003831 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003832 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003833 auto *cb_access_context = GetAccessContext(commandBuffer);
3834 assert(cb_access_context);
3835 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3836 auto *context = cb_access_context->GetCurrentAccessContext();
3837 assert(context);
3838
locke-lunarg61870c22020-06-09 14:51:50 -06003839 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3840 cb_access_context->RecordDrawSubpassAttachment(tag);
3841 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003842
3843 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3844 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3845 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003846 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003847}
3848
3849bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3850 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003851 bool skip = false;
3852 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003853 const auto *cb_access_context = GetAccessContext(commandBuffer);
3854 assert(cb_access_context);
3855 if (!cb_access_context) return skip;
3856
3857 const auto *context = cb_access_context->GetCurrentAccessContext();
3858 assert(context);
3859 if (!context) return skip;
3860
locke-lunarg61870c22020-06-09 14:51:50 -06003861 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3862 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3863 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3864 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003865
3866 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3867 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3868 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003869 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003870 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003871}
3872
3873void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3874 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003875 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003876 auto *cb_access_context = GetAccessContext(commandBuffer);
3877 assert(cb_access_context);
3878 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3879 auto *context = cb_access_context->GetCurrentAccessContext();
3880 assert(context);
3881
locke-lunarg61870c22020-06-09 14:51:50 -06003882 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3883 cb_access_context->RecordDrawSubpassAttachment(tag);
3884 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003885
3886 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3887 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3888 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003889 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003890}
3891
3892bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3893 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3894 uint32_t stride, const char *function) const {
3895 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003896 const auto *cb_access_context = GetAccessContext(commandBuffer);
3897 assert(cb_access_context);
3898 if (!cb_access_context) return skip;
3899
3900 const auto *context = cb_access_context->GetCurrentAccessContext();
3901 assert(context);
3902 if (!context) return skip;
3903
locke-lunarg61870c22020-06-09 14:51:50 -06003904 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3905 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3906 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3907 function);
3908 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003909
3910 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3911 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3912 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003913 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003914 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003915}
3916
3917bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3918 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3919 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003920 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3921 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003922}
3923
3924void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3925 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3926 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003927 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3928 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003929 auto *cb_access_context = GetAccessContext(commandBuffer);
3930 assert(cb_access_context);
3931 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3932 auto *context = cb_access_context->GetCurrentAccessContext();
3933 assert(context);
3934
locke-lunarg61870c22020-06-09 14:51:50 -06003935 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3936 cb_access_context->RecordDrawSubpassAttachment(tag);
3937 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3938 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003939
3940 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3941 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3942 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003943 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003944}
3945
3946bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3947 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3948 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003949 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3950 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003951}
3952
3953void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3954 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3955 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003956 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3957 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003958 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003959}
3960
3961bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3962 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3963 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003964 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3965 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003966}
3967
3968void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3969 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3970 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003971 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3972 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003973 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3974}
3975
3976bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3977 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3978 uint32_t stride, const char *function) const {
3979 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003980 const auto *cb_access_context = GetAccessContext(commandBuffer);
3981 assert(cb_access_context);
3982 if (!cb_access_context) return skip;
3983
3984 const auto *context = cb_access_context->GetCurrentAccessContext();
3985 assert(context);
3986 if (!context) return skip;
3987
locke-lunarg61870c22020-06-09 14:51:50 -06003988 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3989 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3990 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3991 stride, function);
3992 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003993
3994 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3995 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3996 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003997 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003998 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003999}
4000
4001bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4002 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4003 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004004 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4005 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004006}
4007
4008void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4009 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4010 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004011 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4012 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004013 auto *cb_access_context = GetAccessContext(commandBuffer);
4014 assert(cb_access_context);
4015 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
4016 auto *context = cb_access_context->GetCurrentAccessContext();
4017 assert(context);
4018
locke-lunarg61870c22020-06-09 14:51:50 -06004019 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4020 cb_access_context->RecordDrawSubpassAttachment(tag);
4021 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4022 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004023
4024 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4025 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004026 // We will update the index and vertex buffer in SubmitQueue in the future.
4027 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004028}
4029
4030bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4031 VkDeviceSize offset, VkBuffer countBuffer,
4032 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4033 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004034 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4035 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004036}
4037
4038void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4039 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4040 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004041 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4042 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004043 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4044}
4045
4046bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4047 VkDeviceSize offset, VkBuffer countBuffer,
4048 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4049 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004050 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4051 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004052}
4053
4054void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4055 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4056 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004057 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4058 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004059 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4060}
4061
4062bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4063 const VkClearColorValue *pColor, uint32_t rangeCount,
4064 const VkImageSubresourceRange *pRanges) const {
4065 bool skip = false;
4066 const auto *cb_access_context = GetAccessContext(commandBuffer);
4067 assert(cb_access_context);
4068 if (!cb_access_context) return skip;
4069
4070 const auto *context = cb_access_context->GetCurrentAccessContext();
4071 assert(context);
4072 if (!context) return skip;
4073
4074 const auto *image_state = Get<IMAGE_STATE>(image);
4075
4076 for (uint32_t index = 0; index < rangeCount; index++) {
4077 const auto &range = pRanges[index];
4078 if (image_state) {
4079 auto hazard =
4080 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4081 if (hazard.hazard) {
4082 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004083 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004084 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004085 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004086 }
4087 }
4088 }
4089 return skip;
4090}
4091
4092void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4093 const VkClearColorValue *pColor, uint32_t rangeCount,
4094 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004095 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004096 auto *cb_access_context = GetAccessContext(commandBuffer);
4097 assert(cb_access_context);
4098 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4099 auto *context = cb_access_context->GetCurrentAccessContext();
4100 assert(context);
4101
4102 const auto *image_state = Get<IMAGE_STATE>(image);
4103
4104 for (uint32_t index = 0; index < rangeCount; index++) {
4105 const auto &range = pRanges[index];
4106 if (image_state) {
4107 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4108 tag);
4109 }
4110 }
4111}
4112
4113bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4114 VkImageLayout imageLayout,
4115 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4116 const VkImageSubresourceRange *pRanges) const {
4117 bool skip = false;
4118 const auto *cb_access_context = GetAccessContext(commandBuffer);
4119 assert(cb_access_context);
4120 if (!cb_access_context) return skip;
4121
4122 const auto *context = cb_access_context->GetCurrentAccessContext();
4123 assert(context);
4124 if (!context) return skip;
4125
4126 const auto *image_state = Get<IMAGE_STATE>(image);
4127
4128 for (uint32_t index = 0; index < rangeCount; index++) {
4129 const auto &range = pRanges[index];
4130 if (image_state) {
4131 auto hazard =
4132 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4133 if (hazard.hazard) {
4134 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004135 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004136 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004137 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004138 }
4139 }
4140 }
4141 return skip;
4142}
4143
4144void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4145 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4146 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004147 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004148 auto *cb_access_context = GetAccessContext(commandBuffer);
4149 assert(cb_access_context);
4150 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4151 auto *context = cb_access_context->GetCurrentAccessContext();
4152 assert(context);
4153
4154 const auto *image_state = Get<IMAGE_STATE>(image);
4155
4156 for (uint32_t index = 0; index < rangeCount; index++) {
4157 const auto &range = pRanges[index];
4158 if (image_state) {
4159 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4160 tag);
4161 }
4162 }
4163}
4164
4165bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4166 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4167 VkDeviceSize dstOffset, VkDeviceSize stride,
4168 VkQueryResultFlags flags) const {
4169 bool skip = false;
4170 const auto *cb_access_context = GetAccessContext(commandBuffer);
4171 assert(cb_access_context);
4172 if (!cb_access_context) return skip;
4173
4174 const auto *context = cb_access_context->GetCurrentAccessContext();
4175 assert(context);
4176 if (!context) return skip;
4177
4178 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4179
4180 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004181 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004182 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4183 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004184 skip |=
4185 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4186 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4187 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004188 }
4189 }
locke-lunargff255f92020-05-13 18:53:52 -06004190
4191 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004192 return skip;
4193}
4194
4195void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4196 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4197 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004198 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4199 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004200 auto *cb_access_context = GetAccessContext(commandBuffer);
4201 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004202 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004203 auto *context = cb_access_context->GetCurrentAccessContext();
4204 assert(context);
4205
4206 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4207
4208 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004209 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004210 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4211 }
locke-lunargff255f92020-05-13 18:53:52 -06004212
4213 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004214}
4215
4216bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4217 VkDeviceSize size, uint32_t data) const {
4218 bool skip = false;
4219 const auto *cb_access_context = GetAccessContext(commandBuffer);
4220 assert(cb_access_context);
4221 if (!cb_access_context) return skip;
4222
4223 const auto *context = cb_access_context->GetCurrentAccessContext();
4224 assert(context);
4225 if (!context) return skip;
4226
4227 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4228
4229 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004230 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004231 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4232 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004233 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004234 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004235 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004236 }
4237 }
4238 return skip;
4239}
4240
4241void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4242 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004243 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004244 auto *cb_access_context = GetAccessContext(commandBuffer);
4245 assert(cb_access_context);
4246 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4247 auto *context = cb_access_context->GetCurrentAccessContext();
4248 assert(context);
4249
4250 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4251
4252 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004253 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004254 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4255 }
4256}
4257
4258bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4259 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4260 const VkImageResolve *pRegions) const {
4261 bool skip = false;
4262 const auto *cb_access_context = GetAccessContext(commandBuffer);
4263 assert(cb_access_context);
4264 if (!cb_access_context) return skip;
4265
4266 const auto *context = cb_access_context->GetCurrentAccessContext();
4267 assert(context);
4268 if (!context) return skip;
4269
4270 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4271 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4272
4273 for (uint32_t region = 0; region < regionCount; region++) {
4274 const auto &resolve_region = pRegions[region];
4275 if (src_image) {
4276 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4277 resolve_region.srcOffset, resolve_region.extent);
4278 if (hazard.hazard) {
4279 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004280 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004281 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004282 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004283 }
4284 }
4285
4286 if (dst_image) {
4287 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4288 resolve_region.dstOffset, resolve_region.extent);
4289 if (hazard.hazard) {
4290 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004291 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004292 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004293 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004294 }
4295 if (skip) break;
4296 }
4297 }
4298
4299 return skip;
4300}
4301
4302void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4303 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4304 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004305 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4306 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004307 auto *cb_access_context = GetAccessContext(commandBuffer);
4308 assert(cb_access_context);
4309 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4310 auto *context = cb_access_context->GetCurrentAccessContext();
4311 assert(context);
4312
4313 auto *src_image = Get<IMAGE_STATE>(srcImage);
4314 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4315
4316 for (uint32_t region = 0; region < regionCount; region++) {
4317 const auto &resolve_region = pRegions[region];
4318 if (src_image) {
4319 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4320 resolve_region.srcOffset, resolve_region.extent, tag);
4321 }
4322 if (dst_image) {
4323 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4324 resolve_region.dstOffset, resolve_region.extent, tag);
4325 }
4326 }
4327}
4328
Jeff Leger178b1e52020-10-05 12:22:23 -04004329bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4330 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4331 bool skip = false;
4332 const auto *cb_access_context = GetAccessContext(commandBuffer);
4333 assert(cb_access_context);
4334 if (!cb_access_context) return skip;
4335
4336 const auto *context = cb_access_context->GetCurrentAccessContext();
4337 assert(context);
4338 if (!context) return skip;
4339
4340 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4341 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4342
4343 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4344 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4345 if (src_image) {
4346 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4347 resolve_region.srcOffset, resolve_region.extent);
4348 if (hazard.hazard) {
4349 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4350 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4351 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
4352 region, string_UsageTag(hazard).c_str());
4353 }
4354 }
4355
4356 if (dst_image) {
4357 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4358 resolve_region.dstOffset, resolve_region.extent);
4359 if (hazard.hazard) {
4360 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4361 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4362 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
4363 region, string_UsageTag(hazard).c_str());
4364 }
4365 if (skip) break;
4366 }
4367 }
4368
4369 return skip;
4370}
4371
4372void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4373 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4374 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4375 auto *cb_access_context = GetAccessContext(commandBuffer);
4376 assert(cb_access_context);
4377 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4378 auto *context = cb_access_context->GetCurrentAccessContext();
4379 assert(context);
4380
4381 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4382 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4383
4384 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4385 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4386 if (src_image) {
4387 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4388 resolve_region.srcOffset, resolve_region.extent, tag);
4389 }
4390 if (dst_image) {
4391 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4392 resolve_region.dstOffset, resolve_region.extent, tag);
4393 }
4394 }
4395}
4396
locke-lunarge1a67022020-04-29 00:15:36 -06004397bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4398 VkDeviceSize dataSize, const void *pData) const {
4399 bool skip = false;
4400 const auto *cb_access_context = GetAccessContext(commandBuffer);
4401 assert(cb_access_context);
4402 if (!cb_access_context) return skip;
4403
4404 const auto *context = cb_access_context->GetCurrentAccessContext();
4405 assert(context);
4406 if (!context) return skip;
4407
4408 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4409
4410 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004411 // VK_WHOLE_SIZE not allowed
4412 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004413 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4414 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004415 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004416 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004417 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004418 }
4419 }
4420 return skip;
4421}
4422
4423void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4424 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004425 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004426 auto *cb_access_context = GetAccessContext(commandBuffer);
4427 assert(cb_access_context);
4428 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4429 auto *context = cb_access_context->GetCurrentAccessContext();
4430 assert(context);
4431
4432 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4433
4434 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004435 // VK_WHOLE_SIZE not allowed
4436 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004437 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4438 }
4439}
locke-lunargff255f92020-05-13 18:53:52 -06004440
4441bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4442 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4443 bool skip = false;
4444 const auto *cb_access_context = GetAccessContext(commandBuffer);
4445 assert(cb_access_context);
4446 if (!cb_access_context) return skip;
4447
4448 const auto *context = cb_access_context->GetCurrentAccessContext();
4449 assert(context);
4450 if (!context) return skip;
4451
4452 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4453
4454 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004455 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004456 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4457 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004458 skip |=
4459 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4460 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4461 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004462 }
4463 }
4464 return skip;
4465}
4466
4467void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4468 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004469 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004470 auto *cb_access_context = GetAccessContext(commandBuffer);
4471 assert(cb_access_context);
4472 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4473 auto *context = cb_access_context->GetCurrentAccessContext();
4474 assert(context);
4475
4476 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4477
4478 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004479 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004480 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4481 }
4482}