blob: 93f4e4115fe5198e4bb2fca8cba3d271fced3008 [file] [log] [blame]
locke-lunarg8ec19162020-06-16 18:48:34 -06001/* Copyright (c) 2019-2020 The Khronos Group Inc.
2 * Copyright (c) 2019-2020 Valve Corporation
3 * Copyright (c) 2019-2020 LunarG, Inc.
John Zulauf9cb530d2019-09-30 14:14:10 -06004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Author: John Zulauf <jzulauf@lunarg.com>
18 */
19
20#include <limits>
21#include <vector>
locke-lunarg296a3c92020-03-25 01:04:29 -060022#include <memory>
23#include <bitset>
John Zulauf9cb530d2019-09-30 14:14:10 -060024#include "synchronization_validation.h"
25
26static const char *string_SyncHazardVUID(SyncHazard hazard) {
27 switch (hazard) {
28 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070029 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060030 break;
31 case SyncHazard::READ_AFTER_WRITE:
32 return "SYNC-HAZARD-READ_AFTER_WRITE";
33 break;
34 case SyncHazard::WRITE_AFTER_READ:
35 return "SYNC-HAZARD-WRITE_AFTER_READ";
36 break;
37 case SyncHazard::WRITE_AFTER_WRITE:
38 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
39 break;
John Zulauf2f952d22020-02-10 11:34:51 -070040 case SyncHazard::READ_RACING_WRITE:
41 return "SYNC-HAZARD-READ-RACING-WRITE";
42 break;
43 case SyncHazard::WRITE_RACING_WRITE:
44 return "SYNC-HAZARD-WRITE-RACING-WRITE";
45 break;
46 case SyncHazard::WRITE_RACING_READ:
47 return "SYNC-HAZARD-WRITE-RACING-READ";
48 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060049 default:
50 assert(0);
51 }
52 return "SYNC-HAZARD-INVALID";
53}
54
John Zulauf59e25072020-07-17 10:55:21 -060055static bool IsHazardVsRead(SyncHazard hazard) {
56 switch (hazard) {
57 case SyncHazard::NONE:
58 return false;
59 break;
60 case SyncHazard::READ_AFTER_WRITE:
61 return false;
62 break;
63 case SyncHazard::WRITE_AFTER_READ:
64 return true;
65 break;
66 case SyncHazard::WRITE_AFTER_WRITE:
67 return false;
68 break;
69 case SyncHazard::READ_RACING_WRITE:
70 return false;
71 break;
72 case SyncHazard::WRITE_RACING_WRITE:
73 return false;
74 break;
75 case SyncHazard::WRITE_RACING_READ:
76 return true;
77 break;
78 default:
79 assert(0);
80 }
81 return false;
82}
83
John Zulauf9cb530d2019-09-30 14:14:10 -060084static const char *string_SyncHazard(SyncHazard hazard) {
85 switch (hazard) {
86 case SyncHazard::NONE:
87 return "NONR";
88 break;
89 case SyncHazard::READ_AFTER_WRITE:
90 return "READ_AFTER_WRITE";
91 break;
92 case SyncHazard::WRITE_AFTER_READ:
93 return "WRITE_AFTER_READ";
94 break;
95 case SyncHazard::WRITE_AFTER_WRITE:
96 return "WRITE_AFTER_WRITE";
97 break;
John Zulauf2f952d22020-02-10 11:34:51 -070098 case SyncHazard::READ_RACING_WRITE:
99 return "READ_RACING_WRITE";
100 break;
101 case SyncHazard::WRITE_RACING_WRITE:
102 return "WRITE_RACING_WRITE";
103 break;
104 case SyncHazard::WRITE_RACING_READ:
105 return "WRITE_RACING_READ";
106 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600107 default:
108 assert(0);
109 }
110 return "INVALID HAZARD";
111}
112
John Zulauf37ceaed2020-07-03 16:18:15 -0600113static const SyncStageAccessInfoType *SyncStageAccessInfoFromMask(SyncStageAccessFlags flags) {
114 // Return the info for the first bit found
115 const SyncStageAccessInfoType *info = nullptr;
116 uint32_t index = 0;
117 while (flags) {
118 if (flags & 0x1) {
119 flags = 0;
120 info = &syncStageAccessInfoByStageAccessIndex[index];
121 } else {
122 flags = flags >> 1;
123 index++;
124 }
125 }
126 return info;
127}
128
John Zulauf59e25072020-07-17 10:55:21 -0600129static std::string string_SyncStageAccessFlags(SyncStageAccessFlags flags, const char *sep = "|") {
130 std::string out_str;
131 uint32_t index = 0;
132 while (flags) {
133 const auto &info = syncStageAccessInfoByStageAccessIndex[index];
134 if (flags & info.stage_access_bit) {
135 if (!out_str.empty()) {
136 out_str.append(sep);
137 }
138 out_str.append(info.name);
139 flags = flags & ~info.stage_access_bit;
140 }
141 index++;
142 assert(index < syncStageAccessInfoByStageAccessIndex.size());
143 }
144 if (out_str.length() == 0) {
145 out_str.append("Unhandled SyncStageAccess");
146 }
147 return out_str;
148}
149
John Zulauf37ceaed2020-07-03 16:18:15 -0600150static std::string string_UsageTag(const HazardResult &hazard) {
151 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600152 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
153 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600154 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600155 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
156 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf59e25072020-07-17 10:55:21 -0600157 out << "(usage: " << usage_info.name << ", prior_usage: " << stage_access_name;
158 if (IsHazardVsRead(hazard.hazard)) {
159 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
160 out << ", read_barriers: " << string_VkPipelineStageFlags(barriers);
161 } else {
162 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
163 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
164 }
165
166 out << ", command: " << CommandTypeString(tag.command);
167 out << ", seq_no: " << (tag.index & 0xFFFFFFFF) << ", reset_no: " << (tag.index >> 32) << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600168 return out.str();
169}
170
John Zulaufd14743a2020-07-03 09:42:39 -0600171// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
172// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
173// also reflects this special case for read hazard detection (using access instead of exec scope)
John Zulaufb027cdb2020-05-21 14:25:22 -0600174static constexpr VkPipelineStageFlags kColorAttachmentExecScope = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
175static constexpr SyncStageAccessFlags kColorAttachmentAccessScope =
176 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
177 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
John Zulaufd14743a2020-07-03 09:42:39 -0600178 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
179 SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600180static constexpr VkPipelineStageFlags kDepthStencilAttachmentExecScope =
181 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
182static constexpr SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
183 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
184 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
185 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
John Zulaufd14743a2020-07-03 09:42:39 -0600186 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
187 SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600188
189static constexpr SyncOrderingBarrier kColorAttachmentRasterOrder = {kColorAttachmentExecScope, kColorAttachmentAccessScope};
190static constexpr SyncOrderingBarrier kDepthStencilAttachmentRasterOrder = {kDepthStencilAttachmentExecScope,
191 kDepthStencilAttachmentAccessScope};
192static constexpr SyncOrderingBarrier kAttachmentRasterOrder = {kDepthStencilAttachmentExecScope | kColorAttachmentExecScope,
193 kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope};
John Zulauf7635de32020-05-29 17:14:15 -0600194// 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 -0600195static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600196
locke-lunarg3c038002020-04-30 23:08:08 -0600197inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
198 if (size == VK_WHOLE_SIZE) {
199 return (whole_size - offset);
200 }
201 return size;
202}
203
John Zulauf16adfc92020-04-08 10:28:33 -0600204template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600205static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600206 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
207}
208
John Zulauf355e49b2020-04-24 15:11:15 -0600209static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600210
John Zulauf0cb5be22020-01-23 12:18:22 -0700211// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
212VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
213 VkPipelineStageFlags expanded = stage_mask;
214 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
215 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
216 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
217 if (all_commands.first & queue_flags) {
218 expanded |= all_commands.second;
219 }
220 }
221 }
222 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
223 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
224 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
225 }
226 return expanded;
227}
228
John Zulauf36bcf6a2020-02-03 15:12:52 -0700229VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
230 std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
231 VkPipelineStageFlags unscanned = stage_mask;
232 VkPipelineStageFlags related = 0;
Jonah Ryan-Davis185189c2020-07-14 10:28:52 -0400233 for (const auto &entry : map) {
234 const auto &stage = entry.first;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700235 if (stage & unscanned) {
236 related = related | entry.second;
237 unscanned = unscanned & ~stage;
238 if (!unscanned) break;
239 }
240 }
241 return related;
242}
243
244VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
245 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
246}
247
248VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
249 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
250}
251
John Zulauf5c5e88d2019-12-26 11:22:02 -0700252static const ResourceAccessRange full_range(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700253
locke-lunargff255f92020-05-13 18:53:52 -0600254void GetBufferRange(VkDeviceSize &range_start, VkDeviceSize &range_size, VkDeviceSize offset, VkDeviceSize buf_whole_size,
255 uint32_t first_index, uint32_t count, VkDeviceSize stride) {
256 range_start = offset + first_index * stride;
257 range_size = 0;
258 if (count == UINT32_MAX) {
259 range_size = buf_whole_size - range_start;
260 } else {
261 range_size = count * stride;
262 }
263}
264
locke-lunarg654e3692020-06-04 17:19:15 -0600265SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
266 VkShaderStageFlagBits stage_flag) {
267 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
268 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
269 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
270 }
271 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
272 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
273 assert(0);
274 }
275 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
276 return stage_access->second.uniform_read;
277 }
278
279 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
280 // Because if write hazard happens, read hazard might or might not happen.
281 // But if write hazard doesn't happen, read hazard is impossible to happen.
282 if (descriptor_data.is_writable) {
283 return stage_access->second.shader_write;
284 }
285 return stage_access->second.shader_read;
286}
287
locke-lunarg37047832020-06-12 13:44:45 -0600288bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
289 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
290 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
291 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
292 ? true
293 : false;
294}
295
296bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
297 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
298 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
299 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
300 ? true
301 : false;
302}
303
John Zulauf355e49b2020-04-24 15:11:15 -0600304// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
305const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = {
306 AccessContext::AddressType::kLinearAddress, AccessContext::AddressType::kIdealizedAddress};
307
John Zulauf7635de32020-05-29 17:14:15 -0600308// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
309// Used by both validation and record operations
310//
311// The signature for Action() reflect the needs of both uses.
312template <typename Action>
313void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
314 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
315 VkExtent3D extent = CastTo3D(render_area.extent);
316 VkOffset3D offset = CastTo3D(render_area.offset);
317 const auto &rp_ci = rp_state.createInfo;
318 const auto *attachment_ci = rp_ci.pAttachments;
319 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
320
321 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
322 const auto *color_attachments = subpass_ci.pColorAttachments;
323 const auto *color_resolve = subpass_ci.pResolveAttachments;
324 if (color_resolve && color_attachments) {
325 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
326 const auto &color_attach = color_attachments[i].attachment;
327 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
328 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
329 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
330 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
331 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
332 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
333 }
334 }
335 }
336
337 // Depth stencil resolve only if the extension is present
338 const auto ds_resolve = lvl_find_in_chain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
339 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
340 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
341 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
342 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
343 const auto src_ci = attachment_ci[src_at];
344 // The formats are required to match so we can pick either
345 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
346 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
347 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
348 VkImageAspectFlags aspect_mask = 0u;
349
350 // Figure out which aspects are actually touched during resolve operations
351 const char *aspect_string = nullptr;
352 if (resolve_depth && resolve_stencil) {
353 // Validate all aspects together
354 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
355 aspect_string = "depth/stencil";
356 } else if (resolve_depth) {
357 // Validate depth only
358 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
359 aspect_string = "depth";
360 } else if (resolve_stencil) {
361 // Validate all stencil only
362 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
363 aspect_string = "stencil";
364 }
365
366 if (aspect_mask) {
367 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
368 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kDepthStencilAttachmentRasterOrder, offset, extent,
369 aspect_mask);
370 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
371 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
372 }
373 }
374}
375
376// Action for validating resolve operations
377class ValidateResolveAction {
378 public:
379 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
380 const char *func_name)
381 : render_pass_(render_pass),
382 subpass_(subpass),
383 context_(context),
384 sync_state_(sync_state),
385 func_name_(func_name),
386 skip_(false) {}
387 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
388 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
389 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
390 HazardResult hazard;
391 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
392 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600393 skip_ |= sync_state_.LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
394 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600395 " to resolve attachment %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -0600396 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name,
John Zulauf37ceaed2020-07-03 16:18:15 -0600397 src_at, dst_at, string_UsageTag(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600398 }
399 }
400 // Providing a mechanism for the constructing caller to get the result of the validation
401 bool GetSkip() const { return skip_; }
402
403 private:
404 VkRenderPass render_pass_;
405 const uint32_t subpass_;
406 const AccessContext &context_;
407 const SyncValidator &sync_state_;
408 const char *func_name_;
409 bool skip_;
410};
411
412// Update action for resolve operations
413class UpdateStateResolveAction {
414 public:
415 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
416 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
417 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
418 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
419 // Ignores validation only arguments...
420 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
421 }
422
423 private:
424 AccessContext &context_;
425 const ResourceUsageTag &tag_;
426};
427
John Zulauf59e25072020-07-17 10:55:21 -0600428void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
429 SyncStageAccessFlags prior_, const ResourceUsageTag &tag_) {
430 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
431 usage_index = usage_index_;
432 hazard = hazard_;
433 prior_access = prior_;
434 tag = tag_;
435}
436
John Zulauf540266b2020-04-06 18:54:53 -0600437AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
438 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600439 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600440 Reset();
441 const auto &subpass_dep = dependencies[subpass];
442 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600443 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600444 for (const auto &prev_dep : subpass_dep.prev) {
445 assert(prev_dep.dependency);
446 const auto dep = *prev_dep.dependency;
John Zulauf540266b2020-04-06 18:54:53 -0600447 prev_.emplace_back(const_cast<AccessContext *>(&contexts[dep.srcSubpass]), queue_flags, dep);
John Zulauf355e49b2020-04-24 15:11:15 -0600448 prev_by_subpass_[dep.srcSubpass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700449 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600450
451 async_.reserve(subpass_dep.async.size());
452 for (const auto async_subpass : subpass_dep.async) {
John Zulauf540266b2020-04-06 18:54:53 -0600453 async_.emplace_back(const_cast<AccessContext *>(&contexts[async_subpass]));
John Zulauf3d84f1b2020-03-09 13:33:25 -0600454 }
John Zulaufe5da6e52020-03-18 15:32:18 -0600455 if (subpass_dep.barrier_from_external) {
456 src_external_ = TrackBack(external_context, queue_flags, *subpass_dep.barrier_from_external);
457 } else {
458 src_external_ = TrackBack();
459 }
460 if (subpass_dep.barrier_to_external) {
461 dst_external_ = TrackBack(this, queue_flags, *subpass_dep.barrier_to_external);
462 } else {
463 dst_external_ = TrackBack();
John Zulauf3d84f1b2020-03-09 13:33:25 -0600464 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700465}
466
John Zulauf5f13a792020-03-10 07:31:21 -0600467template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600468HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600469 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600470 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600471 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600472
473 HazardResult hazard;
474 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
475 hazard = detector.Detect(prev);
476 }
477 return hazard;
478}
479
John Zulauf3d84f1b2020-03-09 13:33:25 -0600480// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
481// the DAG of the contexts (for example subpasses)
482template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600483HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
484 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600485 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600486
John Zulauf1a224292020-06-30 14:52:13 -0600487 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600488 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
489 // so we'll check these first
490 for (const auto &async_context : async_) {
491 hazard = async_context->DetectAsyncHazard(type, detector, range);
492 if (hazard.hazard) return hazard;
493 }
John Zulauf5f13a792020-03-10 07:31:21 -0600494 }
495
John Zulauf1a224292020-06-30 14:52:13 -0600496 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600497
John Zulauf69133422020-05-20 14:55:53 -0600498 const auto &accesses = GetAccessStateMap(type);
499 const auto from = accesses.lower_bound(range);
500 const auto to = accesses.upper_bound(range);
501 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600502
John Zulauf69133422020-05-20 14:55:53 -0600503 for (auto pos = from; pos != to; ++pos) {
504 // Cover any leading gap, or gap between entries
505 if (detect_prev) {
506 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
507 // Cover any leading gap, or gap between entries
508 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600509 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600510 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600511 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600512 if (hazard.hazard) return hazard;
513 }
John Zulauf69133422020-05-20 14:55:53 -0600514 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
515 gap.begin = pos->first.end;
516 }
517
518 hazard = detector.Detect(pos);
519 if (hazard.hazard) return hazard;
520 }
521
522 if (detect_prev) {
523 // Detect in the trailing empty as needed
524 gap.end = range.end;
525 if (gap.non_empty()) {
526 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600527 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600528 }
529
530 return hazard;
531}
532
533// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
534template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600535HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600536 auto &accesses = GetAccessStateMap(type);
537 const auto from = accesses.lower_bound(range);
538 const auto to = accesses.upper_bound(range);
539
John Zulauf3d84f1b2020-03-09 13:33:25 -0600540 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600541 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
542 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600543 }
John Zulauf16adfc92020-04-08 10:28:33 -0600544
John Zulauf3d84f1b2020-03-09 13:33:25 -0600545 return hazard;
546}
547
John Zulauf355e49b2020-04-24 15:11:15 -0600548// Returns the last resolved entry
549static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
550 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
551 const SyncBarrier *barrier) {
552 auto at = entry;
553 for (auto pos = first; pos != last; ++pos) {
554 // Every member of the input iterator range must fit within the remaining portion of entry
555 assert(at->first.includes(pos->first));
556 assert(at != dest->end());
557 // Trim up at to the same size as the entry to resolve
558 at = sparse_container::split(at, *dest, pos->first);
559 auto access = pos->second;
560 if (barrier) {
561 access.ApplyBarrier(*barrier);
562 }
563 at->second.Resolve(access);
564 ++at; // Go to the remaining unused section of entry
565 }
566}
567
568void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, const SyncBarrier *barrier,
569 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
570 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600571 if (!range.non_empty()) return;
572
John Zulauf355e49b2020-04-24 15:11:15 -0600573 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
574 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600575 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600576 if (current->pos_B->valid) {
577 const auto &src_pos = current->pos_B->lower_bound;
John Zulauf355e49b2020-04-24 15:11:15 -0600578 auto access = src_pos->second;
579 if (barrier) {
580 access.ApplyBarrier(*barrier);
581 }
John Zulauf16adfc92020-04-08 10:28:33 -0600582 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600583 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
584 trimmed->second.Resolve(access);
585 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600586 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600587 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600588 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600589 }
John Zulauf16adfc92020-04-08 10:28:33 -0600590 } else {
591 // we have to descend to fill this gap
592 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600593 if (current->pos_A->valid) {
594 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
595 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600596 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulauf355e49b2020-04-24 15:11:15 -0600597 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier);
598 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600599 // There isn't anything in dest in current)range, so we can accumulate directly into it.
600 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulauf355e49b2020-04-24 15:11:15 -0600601 if (barrier) {
602 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
John Zulauf3bcab5e2020-06-19 14:42:32 -0600603 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
John Zulauf355e49b2020-04-24 15:11:15 -0600604 pos->second.ApplyBarrier(*barrier);
605 }
606 }
607 }
608 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
609 // iterator of the outer while.
610
611 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
612 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
613 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600614 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
615 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600616 current.seek(seek_to);
617 } else if (!current->pos_A->valid && infill_state) {
618 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
619 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
620 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600621 }
John Zulauf5f13a792020-03-10 07:31:21 -0600622 }
John Zulauf16adfc92020-04-08 10:28:33 -0600623 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600624 }
John Zulauf1a224292020-06-30 14:52:13 -0600625
626 // Infill if range goes passed both the current and resolve map prior contents
627 if (recur_to_infill && (current->range.end < range.end)) {
628 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
629 ResourceAccessRangeMap gap_map;
630 const auto the_end = resolve_map->end();
631 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
632 for (auto &access : gap_map) {
633 access.second.ApplyBarrier(*barrier);
634 resolve_map->insert(the_end, access);
635 }
636 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600637}
638
John Zulauf355e49b2020-04-24 15:11:15 -0600639void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
640 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600641 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600642 if (range.non_empty() && infill_state) {
643 descent_map->insert(std::make_pair(range, *infill_state));
644 }
645 } else {
646 // Look for something to fill the gap further along.
647 for (const auto &prev_dep : prev_) {
John Zulauf355e49b2020-04-24 15:11:15 -0600648 prev_dep.context->ResolveAccessRange(type, range, &prev_dep.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600649 }
650
John Zulaufe5da6e52020-03-18 15:32:18 -0600651 if (src_external_.context) {
John Zulauf355e49b2020-04-24 15:11:15 -0600652 src_external_.context->ResolveAccessRange(type, range, &src_external_.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600653 }
654 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600655}
656
John Zulauf16adfc92020-04-08 10:28:33 -0600657AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600658 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600659}
660
661VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) {
662 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
663}
664
John Zulauf355e49b2020-04-24 15:11:15 -0600665static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
John Zulauf16adfc92020-04-08 10:28:33 -0600666
John Zulauf1507ee42020-05-18 11:33:09 -0600667static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
668 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
669 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
670 return stage_access;
671}
672static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
673 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
674 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
675 return stage_access;
676}
677
John Zulauf7635de32020-05-29 17:14:15 -0600678// Caller must manage returned pointer
679static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
680 uint32_t subpass, const VkRect2D &render_area,
681 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
682 auto *proxy = new AccessContext(context);
683 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600684 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600685 return proxy;
686}
687
John Zulauf540266b2020-04-06 18:54:53 -0600688void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
John Zulauf355e49b2020-04-24 15:11:15 -0600689 AddressType address_type, ResourceAccessRangeMap *descent_map,
690 const ResourceAccessState *infill_state) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600691 if (!SimpleBinding(image_state)) return;
692
John Zulauf62f10592020-04-03 12:20:02 -0600693 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
locke-lunargae26eac2020-04-16 15:29:05 -0600694 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600695 image_state.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600696 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf62f10592020-04-03 12:20:02 -0600697 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600698 ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state);
John Zulauf62f10592020-04-03 12:20:02 -0600699 }
700}
701
John Zulauf7635de32020-05-29 17:14:15 -0600702// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600703bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600704 const VkRect2D &render_area, uint32_t subpass,
705 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
706 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600707 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600708 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
709 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
710 // those affects have not been recorded yet.
711 //
712 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
713 // to apply and only copy then, if this proves a hot spot.
714 std::unique_ptr<AccessContext> proxy_for_prev;
715 TrackBack proxy_track_back;
716
John Zulauf355e49b2020-04-24 15:11:15 -0600717 const auto &transitions = rp_state.subpass_transitions[subpass];
718 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600719 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
720
721 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
722 if (prev_needs_proxy) {
723 if (!proxy_for_prev) {
724 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
725 render_area, attachment_views));
726 proxy_track_back = *track_back;
727 proxy_track_back.context = proxy_for_prev.get();
728 }
729 track_back = &proxy_track_back;
730 }
731 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600732 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600733 skip |= sync_state.LogError(
734 rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -0600735 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " image layout transition. Access info %s.",
John Zulauf37ceaed2020-07-03 16:18:15 -0600736 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment, string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600737 }
738 }
739 return skip;
740}
741
John Zulauf1507ee42020-05-18 11:33:09 -0600742bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600743 const VkRect2D &render_area, uint32_t subpass,
744 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
745 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600746 bool skip = false;
747 const auto *attachment_ci = rp_state.createInfo.pAttachments;
748 VkExtent3D extent = CastTo3D(render_area.extent);
749 VkOffset3D offset = CastTo3D(render_area.offset);
750 const auto external_access_scope = src_external_.barrier.dst_access_scope;
John Zulauf1507ee42020-05-18 11:33:09 -0600751
752 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
753 if (subpass == rp_state.attachment_first_subpass[i]) {
754 if (attachment_views[i] == nullptr) continue;
755 const IMAGE_VIEW_STATE &view = *attachment_views[i];
756 const IMAGE_STATE *image = view.image_state.get();
757 if (image == nullptr) continue;
758 const auto &ci = attachment_ci[i];
759 const bool is_transition = rp_state.attachment_first_is_transition[i];
760
761 // Need check in the following way
762 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
763 // vs. transition
764 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
765 // for each aspect loaded.
766
767 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600768 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600769 const bool is_color = !(has_depth || has_stencil);
770
771 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
772 const SyncStageAccessFlags load_mask = (has_depth || is_color) ? SyncStageAccess::Flags(load_index) : 0U;
773 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
774 const SyncStageAccessFlags stencil_mask = has_stencil ? SyncStageAccess::Flags(stencil_load_index) : 0U;
775
John Zulaufaff20662020-06-01 14:07:58 -0600776 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600777 const char *aspect = nullptr;
778 if (is_transition) {
779 // For transition w
780 SyncHazard transition_hazard = SyncHazard::NONE;
781 bool checked_stencil = false;
782 if (load_mask) {
783 if ((load_mask & external_access_scope) != load_mask) {
784 transition_hazard =
785 SyncStageAccess::HasWrite(load_mask) ? SyncHazard::WRITE_AFTER_WRITE : SyncHazard::READ_AFTER_WRITE;
786 aspect = is_color ? "color" : "depth";
787 }
788 if (!transition_hazard && stencil_mask) {
789 if ((stencil_mask & external_access_scope) != stencil_mask) {
790 transition_hazard = SyncStageAccess::HasWrite(stencil_mask) ? SyncHazard::WRITE_AFTER_WRITE
791 : SyncHazard::READ_AFTER_WRITE;
792 aspect = "stencil";
793 checked_stencil = true;
794 }
795 }
796 }
797 if (transition_hazard) {
798 // Hazard vs. ILT
799 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
800 skip |=
801 sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
802 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
803 " aspect %s during load with loadOp %s.",
804 func_name, string_SyncHazard(transition_hazard), subpass, i, aspect, load_op_string);
805 }
806 } else {
807 auto hazard_range = view.normalized_subresource_range;
808 bool checked_stencil = false;
809 if (is_color) {
810 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, offset, extent);
811 aspect = "color";
812 } else {
813 if (has_depth) {
814 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
815 hazard = DetectHazard(*image, load_index, hazard_range, offset, extent);
816 aspect = "depth";
817 }
818 if (!hazard.hazard && has_stencil) {
819 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
820 hazard = DetectHazard(*image, stencil_load_index, hazard_range, offset, extent);
821 aspect = "stencil";
822 checked_stencil = true;
823 }
824 }
825
826 if (hazard.hazard) {
827 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
828 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
829 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600830 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -0600831 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600832 string_UsageTag(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -0600833 }
834 }
835 }
836 }
837 return skip;
838}
839
John Zulaufaff20662020-06-01 14:07:58 -0600840// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
841// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
842// store is part of the same Next/End operation.
843// The latter is handled in layout transistion validation directly
844bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
845 const VkRect2D &render_area, uint32_t subpass,
846 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
847 const char *func_name) const {
848 bool skip = false;
849 const auto *attachment_ci = rp_state.createInfo.pAttachments;
850 VkExtent3D extent = CastTo3D(render_area.extent);
851 VkOffset3D offset = CastTo3D(render_area.offset);
852
853 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
854 if (subpass == rp_state.attachment_last_subpass[i]) {
855 if (attachment_views[i] == nullptr) continue;
856 const IMAGE_VIEW_STATE &view = *attachment_views[i];
857 const IMAGE_STATE *image = view.image_state.get();
858 if (image == nullptr) continue;
859 const auto &ci = attachment_ci[i];
860
861 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
862 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
863 // sake, we treat DONT_CARE as writing.
864 const bool has_depth = FormatHasDepth(ci.format);
865 const bool has_stencil = FormatHasStencil(ci.format);
866 const bool is_color = !(has_depth || has_stencil);
867 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
868 if (!has_stencil && !store_op_stores) continue;
869
870 HazardResult hazard;
871 const char *aspect = nullptr;
872 bool checked_stencil = false;
873 if (is_color) {
874 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
875 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
876 aspect = "color";
877 } else {
878 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
879 auto hazard_range = view.normalized_subresource_range;
880 if (has_depth && store_op_stores) {
881 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
882 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
883 kAttachmentRasterOrder, offset, extent);
884 aspect = "depth";
885 }
886 if (!hazard.hazard && has_stencil && stencil_op_stores) {
887 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
888 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
889 kAttachmentRasterOrder, offset, extent);
890 aspect = "stencil";
891 checked_stencil = true;
892 }
893 }
894
895 if (hazard.hazard) {
896 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
897 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -0600898 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
899 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600900 " %s aspect during store with %s %s. Access info %s",
John Zulauf1dae9192020-06-16 15:46:44 -0600901 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600902 store_op_string, string_UsageTag(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -0600903 }
904 }
905 }
906 return skip;
907}
908
John Zulaufb027cdb2020-05-21 14:25:22 -0600909bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
910 const VkRect2D &render_area,
911 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
912 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600913 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
914 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
915 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600916}
917
John Zulauf3d84f1b2020-03-09 13:33:25 -0600918class HazardDetector {
919 SyncStageAccessIndex usage_index_;
920
921 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600922 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600923 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
924 return pos->second.DetectAsyncHazard(usage_index_);
925 }
926 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
927};
928
John Zulauf69133422020-05-20 14:55:53 -0600929class HazardDetectorWithOrdering {
930 const SyncStageAccessIndex usage_index_;
931 const SyncOrderingBarrier &ordering_;
932
933 public:
934 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
935 return pos->second.DetectHazard(usage_index_, ordering_);
936 }
937 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
938 return pos->second.DetectAsyncHazard(usage_index_);
939 }
940 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
941 : usage_index_(usage), ordering_(ordering) {}
942};
943
John Zulauf16adfc92020-04-08 10:28:33 -0600944HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600945 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600946 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600947 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600948}
949
John Zulauf16adfc92020-04-08 10:28:33 -0600950HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600951 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600952 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -0600953 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -0600954}
955
John Zulauf69133422020-05-20 14:55:53 -0600956template <typename Detector>
957HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
958 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
959 const VkExtent3D &extent, DetectOptions options) const {
960 if (!SimpleBinding(image)) return HazardResult();
961 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
962 const auto address_type = ImageAddressType(image);
963 const auto base_address = ResourceBaseAddress(image);
964 for (; range_gen->non_empty(); ++range_gen) {
965 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
966 if (hazard.hazard) return hazard;
967 }
968 return HazardResult();
969}
970
John Zulauf540266b2020-04-06 18:54:53 -0600971HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
972 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
973 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700974 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
975 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -0600976 return DetectHazard(image, current_usage, subresource_range, offset, extent);
977}
978
979HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
980 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
981 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -0600982 HazardDetector detector(current_usage);
983 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
984}
985
986HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
987 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
988 const VkOffset3D &offset, const VkExtent3D &extent) const {
989 HazardDetectorWithOrdering detector(current_usage, ordering);
990 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -0600991}
992
John Zulaufb027cdb2020-05-21 14:25:22 -0600993// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
994// should have reported the issue regarding an invalid attachment entry
995HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
996 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
997 VkImageAspectFlags aspect_mask) const {
998 if (view != nullptr) {
999 const IMAGE_STATE *image = view->image_state.get();
1000 if (image != nullptr) {
1001 auto *detect_range = &view->normalized_subresource_range;
1002 VkImageSubresourceRange masked_range;
1003 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1004 masked_range = view->normalized_subresource_range;
1005 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1006 detect_range = &masked_range;
1007 }
1008
1009 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1010 if (detect_range->aspectMask) {
1011 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
1012 }
1013 }
1014 }
1015 return HazardResult();
1016}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001017class BarrierHazardDetector {
1018 public:
1019 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1020 SyncStageAccessFlags src_access_scope)
1021 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1022
John Zulauf5f13a792020-03-10 07:31:21 -06001023 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1024 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001025 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001026 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
1027 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1028 return pos->second.DetectAsyncHazard(usage_index_);
1029 }
1030
1031 private:
1032 SyncStageAccessIndex usage_index_;
1033 VkPipelineStageFlags src_exec_scope_;
1034 SyncStageAccessFlags src_access_scope_;
1035};
1036
John Zulauf16adfc92020-04-08 10:28:33 -06001037HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -06001038 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001039 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001040 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -06001041 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001042}
1043
John Zulauf16adfc92020-04-08 10:28:33 -06001044HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001045 SyncStageAccessFlags src_access_scope,
1046 const VkImageSubresourceRange &subresource_range,
1047 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001048 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1049 VkOffset3D zero_offset = {0, 0, 0};
1050 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001051}
1052
John Zulauf355e49b2020-04-24 15:11:15 -06001053HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1054 SyncStageAccessFlags src_stage_accesses,
1055 const VkImageMemoryBarrier &barrier) const {
1056 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1057 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1058 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1059}
1060
John Zulauf9cb530d2019-09-30 14:14:10 -06001061template <typename Flags, typename Map>
1062SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1063 SyncStageAccessFlags scope = 0;
1064 for (const auto &bit_scope : map) {
1065 if (flag_mask < bit_scope.first) break;
1066
1067 if (flag_mask & bit_scope.first) {
1068 scope |= bit_scope.second;
1069 }
1070 }
1071 return scope;
1072}
1073
1074SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
1075 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1076}
1077
1078SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
1079 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
1080}
1081
1082// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
1083SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001084 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1085 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1086 // 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 -06001087 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1088}
1089
1090template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001091void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001092 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1093 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -06001094 auto pos = accesses->lower_bound(range);
1095 if (pos == accesses->end() || !pos->first.intersects(range)) {
1096 // The range is empty, fill it with a default value.
1097 pos = action.Infill(accesses, pos, range);
1098 } else if (range.begin < pos->first.begin) {
1099 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001100 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001101 } else if (pos->first.begin < range.begin) {
1102 // Trim the beginning if needed
1103 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1104 ++pos;
1105 }
1106
1107 const auto the_end = accesses->end();
1108 while ((pos != the_end) && pos->first.intersects(range)) {
1109 if (pos->first.end > range.end) {
1110 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1111 }
1112
1113 pos = action(accesses, pos);
1114 if (pos == the_end) break;
1115
1116 auto next = pos;
1117 ++next;
1118 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1119 // Need to infill if next is disjoint
1120 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001121 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001122 next = action.Infill(accesses, next, new_range);
1123 }
1124 pos = next;
1125 }
1126}
1127
1128struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001129 using Iterator = ResourceAccessRangeMap::iterator;
1130 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001131 // this is only called on gaps, and never returns a gap.
1132 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001133 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001134 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001135 }
John Zulauf5f13a792020-03-10 07:31:21 -06001136
John Zulauf5c5e88d2019-12-26 11:22:02 -07001137 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001138 auto &access_state = pos->second;
1139 access_state.Update(usage, tag);
1140 return pos;
1141 }
1142
John Zulauf16adfc92020-04-08 10:28:33 -06001143 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001144 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001145 : type(type_), context(context_), usage(usage_), tag(tag_) {}
1146 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001147 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001148 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001149 const ResourceUsageTag &tag;
1150};
1151
1152struct ApplyMemoryAccessBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001153 using Iterator = ResourceAccessRangeMap::iterator;
1154 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001155
John Zulauf5c5e88d2019-12-26 11:22:02 -07001156 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001157 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001158 access_state.ApplyMemoryAccessBarrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001159 return pos;
1160 }
1161
John Zulauf36bcf6a2020-02-03 15:12:52 -07001162 ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_,
1163 VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_)
1164 : src_exec_scope(src_exec_scope_),
1165 src_access_scope(src_access_scope_),
1166 dst_exec_scope(dst_exec_scope_),
1167 dst_access_scope(dst_access_scope_) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001168
John Zulauf36bcf6a2020-02-03 15:12:52 -07001169 VkPipelineStageFlags src_exec_scope;
1170 SyncStageAccessFlags src_access_scope;
1171 VkPipelineStageFlags dst_exec_scope;
1172 SyncStageAccessFlags dst_access_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001173};
1174
1175struct ApplyGlobalBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001176 using Iterator = ResourceAccessRangeMap::iterator;
1177 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001178
John Zulauf5c5e88d2019-12-26 11:22:02 -07001179 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001180 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001181 access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001182
1183 for (const auto &functor : barrier_functor) {
1184 functor(accesses, pos);
1185 }
1186 return pos;
1187 }
1188
John Zulauf36bcf6a2020-02-03 15:12:52 -07001189 ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope,
1190 SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses,
John Zulauf9cb530d2019-09-30 14:14:10 -06001191 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001192 : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001193 // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier...
1194 barrier_functor.reserve(memoryBarrierCount);
1195 for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) {
1196 const auto &barrier = pMemoryBarriers[barrier_index];
John Zulauf36bcf6a2020-02-03 15:12:52 -07001197 barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask),
1198 dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
John Zulauf9cb530d2019-09-30 14:14:10 -06001199 }
1200 }
1201
John Zulauf36bcf6a2020-02-03 15:12:52 -07001202 const VkPipelineStageFlags src_exec_scope;
1203 const VkPipelineStageFlags dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001204 std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor;
1205};
1206
John Zulauf355e49b2020-04-24 15:11:15 -06001207void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1208 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001209 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1210 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001211}
1212
John Zulauf16adfc92020-04-08 10:28:33 -06001213void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001214 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001215 if (!SimpleBinding(buffer)) return;
1216 const auto base_address = ResourceBaseAddress(buffer);
1217 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1218}
John Zulauf355e49b2020-04-24 15:11:15 -06001219
John Zulauf540266b2020-04-06 18:54:53 -06001220void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001221 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001222 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001223 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001224 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001225 const auto address_type = ImageAddressType(image);
1226 const auto base_address = ResourceBaseAddress(image);
1227 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001228 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001229 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001230 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001231}
John Zulauf7635de32020-05-29 17:14:15 -06001232void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1233 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1234 if (view != nullptr) {
1235 const IMAGE_STATE *image = view->image_state.get();
1236 if (image != nullptr) {
1237 auto *update_range = &view->normalized_subresource_range;
1238 VkImageSubresourceRange masked_range;
1239 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1240 masked_range = view->normalized_subresource_range;
1241 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1242 update_range = &masked_range;
1243 }
1244 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1245 }
1246 }
1247}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001248
John Zulauf355e49b2020-04-24 15:11:15 -06001249void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1250 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1251 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001252 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1253 subresource.layerCount};
1254 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1255}
1256
John Zulauf540266b2020-04-06 18:54:53 -06001257template <typename Action>
1258void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001259 if (!SimpleBinding(buffer)) return;
1260 const auto base_address = ResourceBaseAddress(buffer);
1261 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001262}
1263
1264template <typename Action>
1265void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1266 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001267 if (!SimpleBinding(image)) return;
1268 const auto address_type = ImageAddressType(image);
1269 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001270
locke-lunargae26eac2020-04-16 15:29:05 -06001271 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001272 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001273
John Zulauf16adfc92020-04-08 10:28:33 -06001274 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001275 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001276 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001277 }
1278}
1279
John Zulauf7635de32020-05-29 17:14:15 -06001280void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1281 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1282 const ResourceUsageTag &tag) {
1283 UpdateStateResolveAction update(*this, tag);
1284 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1285}
1286
John Zulaufaff20662020-06-01 14:07:58 -06001287void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1288 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1289 const ResourceUsageTag &tag) {
1290 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1291 VkExtent3D extent = CastTo3D(render_area.extent);
1292 VkOffset3D offset = CastTo3D(render_area.offset);
1293
1294 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1295 if (rp_state.attachment_last_subpass[i] == subpass) {
1296 if (attachment_views[i] == nullptr) continue; // UNUSED
1297 const auto &view = *attachment_views[i];
1298 const IMAGE_STATE *image = view.image_state.get();
1299 if (image == nullptr) continue;
1300
1301 const auto &ci = attachment_ci[i];
1302 const bool has_depth = FormatHasDepth(ci.format);
1303 const bool has_stencil = FormatHasStencil(ci.format);
1304 const bool is_color = !(has_depth || has_stencil);
1305 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1306
1307 if (is_color && store_op_stores) {
1308 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1309 offset, extent, tag);
1310 } else {
1311 auto update_range = view.normalized_subresource_range;
1312 if (has_depth && store_op_stores) {
1313 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1314 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1315 tag);
1316 }
1317 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1318 if (has_stencil && stencil_op_stores) {
1319 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1320 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1321 tag);
1322 }
1323 }
1324 }
1325 }
1326}
1327
John Zulauf540266b2020-04-06 18:54:53 -06001328template <typename Action>
1329void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1330 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001331 for (const auto address_type : kAddressTypes) {
1332 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001333 }
1334}
1335
1336void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001337 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1338 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -06001339 for (const auto address_type : kAddressTypes) {
John Zulauf355e49b2020-04-24 15:11:15 -06001340 context.ResolveAccessRange(address_type, full_range, &context.GetDstExternalTrackBack().barrier,
1341 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001342 }
1343 }
1344}
1345
John Zulauf355e49b2020-04-24 15:11:15 -06001346void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1347 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1348 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) {
1349 const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1350 UpdateMemoryAccess(image, subresource_range, barrier_action);
1351}
1352
John Zulauf7635de32020-05-29 17:14:15 -06001353// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001354void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1355 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1356 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range,
1357 bool layout_transition, const ResourceUsageTag &tag) {
1358 if (layout_transition) {
1359 UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent,
1360 tag);
1361 ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope,
1362 subresource_range);
John Zulaufc9201222020-05-13 15:13:03 -06001363 } else {
1364 ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range);
John Zulauf355e49b2020-04-24 15:11:15 -06001365 }
John Zulauf355e49b2020-04-24 15:11:15 -06001366}
1367
John Zulauf7635de32020-05-29 17:14:15 -06001368// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001369void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier,
1370 const VkImageSubresourceRange &subresource_range, bool layout_transition,
1371 const ResourceUsageTag &tag) {
1372 ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope,
1373 subresource_range, layout_transition, tag);
1374}
1375
1376// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001377HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001378 if (!attach_view) return HazardResult();
1379 const auto image_state = attach_view->image_state.get();
1380 if (!image_state) return HazardResult();
1381
John Zulauf355e49b2020-04-24 15:11:15 -06001382 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001383 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001384
1385 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulauf7635de32020-05-29 17:14:15 -06001386 auto hazard = track_back.context->DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope,
1387 track_back.barrier.src_access_scope,
1388 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001389 if (!hazard.hazard) {
1390 // The Async hazard check is against the current context's async set.
John Zulauf7635de32020-05-29 17:14:15 -06001391 hazard = DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope, track_back.barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001392 attach_view->normalized_subresource_range, kDetectAsync);
1393 }
1394 return hazard;
1395}
1396
1397// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1398bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1399
1400 const VkRenderPassBeginInfo *pRenderPassBegin,
1401 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1402 const char *func_name) const {
1403 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1404 bool skip = false;
1405 uint32_t subpass = 0;
1406 const auto &transitions = rp_state.subpass_transitions[subpass];
1407 if (transitions.size()) {
1408 const std::vector<AccessContext> empty_context_vector;
1409 // Create context we can use to validate against...
1410 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1411 const_cast<AccessContext *>(&cb_access_context_));
1412
1413 assert(pRenderPassBegin);
1414 if (nullptr == pRenderPassBegin) return skip;
1415
1416 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1417 assert(fb_state);
1418 if (nullptr == fb_state) return skip;
1419
1420 // Create a limited array of views (which we'll need to toss
1421 std::vector<const IMAGE_VIEW_STATE *> views;
1422 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
1423 const auto attachment_count = count_attachment.first;
1424 const auto *attachments = count_attachment.second;
1425 views.resize(attachment_count, nullptr);
1426 for (const auto &transition : transitions) {
1427 assert(transition.attachment < attachment_count);
1428 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
1429 }
1430
John Zulauf7635de32020-05-29 17:14:15 -06001431 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
1432 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001433 }
1434 return skip;
1435}
1436
locke-lunarg61870c22020-06-09 14:51:50 -06001437bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1438 const char *func_name) const {
1439 bool skip = false;
1440 const PIPELINE_STATE *pPipe = nullptr;
1441 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1442 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1443 if (!pPipe || !per_sets) {
1444 return skip;
1445 }
1446
1447 using DescriptorClass = cvdescriptorset::DescriptorClass;
1448 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1449 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1450 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1451 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1452
1453 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarg37047832020-06-12 13:44:45 -06001454 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001455 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1456 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001457 for (const auto &set_binding : stage_state.descriptor_uses) {
1458 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1459 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1460 set_binding.first.second);
1461 const auto descriptor_type = binding_it.GetType();
1462 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1463 auto array_idx = 0;
1464
1465 if (binding_it.IsVariableDescriptorCount()) {
1466 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1467 }
1468 SyncStageAccessIndex sync_index =
1469 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1470
1471 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1472 uint32_t index = i - index_range.start;
1473 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1474 switch (descriptor->GetClass()) {
1475 case DescriptorClass::ImageSampler:
1476 case DescriptorClass::Image: {
1477 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001478 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001479 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001480 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1481 img_view_state = image_sampler_descriptor->GetImageViewState();
1482 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001483 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001484 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1485 img_view_state = image_descriptor->GetImageViewState();
1486 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001487 }
1488 if (!img_view_state) continue;
1489 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1490 VkExtent3D extent = {};
1491 VkOffset3D offset = {};
1492 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1493 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1494 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1495 } else {
1496 extent = img_state->createInfo.extent;
1497 }
1498 auto hazard = current_context_->DetectHazard(*img_state, sync_index,
1499 img_view_state->normalized_subresource_range, offset, extent);
John Zulauf33fc1d52020-07-17 11:01:10 -06001500 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001501 skip |= sync_state_->LogError(
1502 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001503 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1504 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001505 func_name, string_SyncHazard(hazard.hazard),
1506 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1507 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1508 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001509 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1510 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1511 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001512 }
1513 break;
1514 }
1515 case DescriptorClass::TexelBuffer: {
1516 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1517 if (!buf_view_state) continue;
1518 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1519 ResourceAccessRange range =
1520 MakeRange(buf_view_state->create_info.offset,
1521 GetRealWholeSize(buf_view_state->create_info.offset, buf_view_state->create_info.range,
1522 buf_state->createInfo.size));
1523 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001524 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001525 skip |= sync_state_->LogError(
1526 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001527 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1528 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001529 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1530 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1531 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001532 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1533 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1534 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001535 }
1536 break;
1537 }
1538 case DescriptorClass::GeneralBuffer: {
1539 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1540 auto buf_state = buffer_descriptor->GetBufferState();
1541 if (!buf_state) continue;
1542 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1543 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
1544 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001545 skip |= sync_state_->LogError(
1546 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001547 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1548 func_name, string_SyncHazard(hazard.hazard),
1549 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001550 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1551 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001552 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1553 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1554 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001555 }
1556 break;
1557 }
1558 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1559 default:
1560 break;
1561 }
1562 }
1563 }
1564 }
1565 return skip;
1566}
1567
1568void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1569 const ResourceUsageTag &tag) {
1570 const PIPELINE_STATE *pPipe = nullptr;
1571 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1572 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1573 if (!pPipe || !per_sets) {
1574 return;
1575 }
1576
1577 using DescriptorClass = cvdescriptorset::DescriptorClass;
1578 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1579 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1580 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1581 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1582
1583 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001584 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
1585 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1586 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001587 for (const auto &set_binding : stage_state.descriptor_uses) {
1588 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1589 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1590 set_binding.first.second);
1591 const auto descriptor_type = binding_it.GetType();
1592 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1593 auto array_idx = 0;
1594
1595 if (binding_it.IsVariableDescriptorCount()) {
1596 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1597 }
1598 SyncStageAccessIndex sync_index =
1599 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1600
1601 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1602 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1603 switch (descriptor->GetClass()) {
1604 case DescriptorClass::ImageSampler:
1605 case DescriptorClass::Image: {
1606 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1607 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1608 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1609 } else {
1610 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1611 }
1612 if (!img_view_state) continue;
1613 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1614 VkExtent3D extent = {};
1615 VkOffset3D offset = {};
1616 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1617 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1618 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1619 } else {
1620 extent = img_state->createInfo.extent;
1621 }
1622 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1623 offset, extent, tag);
1624 break;
1625 }
1626 case DescriptorClass::TexelBuffer: {
1627 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1628 if (!buf_view_state) continue;
1629 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1630 ResourceAccessRange range =
1631 MakeRange(buf_view_state->create_info.offset, buf_view_state->create_info.range);
1632 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1633 break;
1634 }
1635 case DescriptorClass::GeneralBuffer: {
1636 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1637 auto buf_state = buffer_descriptor->GetBufferState();
1638 if (!buf_state) continue;
1639 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1640 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1641 break;
1642 }
1643 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1644 default:
1645 break;
1646 }
1647 }
1648 }
1649 }
1650}
1651
1652bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1653 bool skip = false;
1654 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1655 if (!pPipe) {
1656 return skip;
1657 }
1658
1659 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1660 const auto &binding_buffers_size = binding_buffers.size();
1661 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1662
1663 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1664 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1665 if (binding_description.binding < binding_buffers_size) {
1666 const auto &binding_buffer = binding_buffers[binding_description.binding];
1667 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1668
1669 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1670 VkDeviceSize range_start = 0;
1671 VkDeviceSize range_size = 0;
1672 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1673 binding_description.stride);
1674 ResourceAccessRange range = MakeRange(range_start, range_size);
1675 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1676 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001677 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001678 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 -06001679 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001680 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001681 }
1682 }
1683 }
1684 return skip;
1685}
1686
1687void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
1688 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1689 if (!pPipe) {
1690 return;
1691 }
1692 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1693 const auto &binding_buffers_size = binding_buffers.size();
1694 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1695
1696 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1697 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1698 if (binding_description.binding < binding_buffers_size) {
1699 const auto &binding_buffer = binding_buffers[binding_description.binding];
1700 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1701
1702 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1703 VkDeviceSize range_start = 0;
1704 VkDeviceSize range_size = 0;
1705 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1706 binding_description.stride);
1707 ResourceAccessRange range = MakeRange(range_start, range_size);
1708 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1709 }
1710 }
1711}
1712
1713bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1714 bool skip = false;
1715 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
1716
1717 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1718 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1719 VkDeviceSize range_start = 0;
1720 VkDeviceSize range_size = 0;
1721 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1722 indexCount, index_size);
1723 ResourceAccessRange range = MakeRange(range_start, range_size);
1724 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1725 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001726 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001727 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 -06001728 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001729 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001730 }
1731
1732 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1733 // We will detect more accurate range in the future.
1734 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1735 return skip;
1736}
1737
1738void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
1739 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return;
1740
1741 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1742 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1743 VkDeviceSize range_start = 0;
1744 VkDeviceSize range_size = 0;
1745 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1746 indexCount, index_size);
1747 ResourceAccessRange range = MakeRange(range_start, range_size);
1748 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1749
1750 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1751 // We will detect more accurate range in the future.
1752 RecordDrawVertex(UINT32_MAX, 0, tag);
1753}
1754
1755bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06001756 bool skip = false;
1757 if (!current_renderpass_context_) return skip;
1758 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1759 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1760 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001761}
1762
1763void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
locke-lunarg7077d502020-06-18 21:37:26 -06001764 if (current_renderpass_context_)
1765 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1766 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001767}
1768
John Zulauf355e49b2020-04-24 15:11:15 -06001769bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001770 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001771 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06001772 skip |=
1773 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001774
1775 return skip;
1776}
1777
1778bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1779 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001780 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001781 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001782 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06001783 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1784 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001785
1786 return skip;
1787}
1788
1789void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1790 assert(sync_state_);
1791 if (!cb_state_) return;
1792
1793 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06001794 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06001795 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06001796 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001797 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001798}
1799
John Zulauf355e49b2020-04-24 15:11:15 -06001800void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001801 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001802 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001803 current_context_ = &current_renderpass_context_->CurrentContext();
1804}
1805
John Zulauf355e49b2020-04-24 15:11:15 -06001806void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001807 assert(current_renderpass_context_);
1808 if (!current_renderpass_context_) return;
1809
John Zulauf1a224292020-06-30 14:52:13 -06001810 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001811 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001812 current_renderpass_context_ = nullptr;
1813}
1814
locke-lunarg61870c22020-06-09 14:51:50 -06001815bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1816 const VkRect2D &render_area, const char *func_name) const {
1817 bool skip = false;
locke-lunarg96dc9632020-06-10 17:22:18 -06001818 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001819 if (!pPipe ||
1820 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001821 return skip;
1822 }
1823 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001824 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1825 VkExtent3D extent = CastTo3D(render_area.extent);
1826 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001827
John Zulauf1a224292020-06-30 14:52:13 -06001828 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001829 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001830 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1831 for (const auto location : list) {
1832 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1833 continue;
1834 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001835 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1836 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06001837 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001838 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001839 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001840 func_name, string_SyncHazard(hazard.hazard),
1841 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1842 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001843 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001844 }
1845 }
1846 }
locke-lunarg37047832020-06-12 13:44:45 -06001847
1848 // PHASE1 TODO: Add layout based read/vs. write selection.
1849 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1850 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1851 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001852 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001853 bool depth_write = false, stencil_write = false;
1854
1855 // PHASE1 TODO: These validation should be in core_checks.
1856 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1857 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1858 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1859 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1860 depth_write = true;
1861 }
1862 // PHASE1 TODO: It needs to check if stencil is writable.
1863 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1864 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1865 // PHASE1 TODO: These validation should be in core_checks.
1866 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1867 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1868 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1869 stencil_write = true;
1870 }
1871
1872 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1873 if (depth_write) {
1874 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001875 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1876 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001877 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001878 skip |= sync_state.LogError(
1879 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001880 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001881 func_name, string_SyncHazard(hazard.hazard),
1882 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1883 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001884 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001885 }
1886 }
1887 if (stencil_write) {
1888 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001889 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1890 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001891 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001892 skip |= sync_state.LogError(
1893 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001894 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001895 func_name, string_SyncHazard(hazard.hazard),
1896 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1897 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001898 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001899 }
locke-lunarg61870c22020-06-09 14:51:50 -06001900 }
1901 }
1902 return skip;
1903}
1904
locke-lunarg96dc9632020-06-10 17:22:18 -06001905void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1906 const ResourceUsageTag &tag) {
1907 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001908 if (!pPipe ||
1909 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001910 return;
1911 }
1912 const auto &list = pPipe->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);
1916
John Zulauf1a224292020-06-30 14:52:13 -06001917 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001918 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001919 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1920 for (const auto location : list) {
1921 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1922 continue;
1923 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001924 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
1925 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001926 }
1927 }
locke-lunarg37047832020-06-12 13:44:45 -06001928
1929 // PHASE1 TODO: Add layout based read/vs. write selection.
1930 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1931 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1932 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001933 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001934 bool depth_write = false, stencil_write = false;
1935
1936 // PHASE1 TODO: These validation should be in core_checks.
1937 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1938 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1939 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1940 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1941 depth_write = true;
1942 }
1943 // PHASE1 TODO: It needs to check if stencil is writable.
1944 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1945 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1946 // PHASE1 TODO: These validation should be in core_checks.
1947 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1948 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1949 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1950 stencil_write = true;
1951 }
1952
1953 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1954 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001955 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1956 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001957 }
1958 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001959 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1960 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001961 }
locke-lunarg61870c22020-06-09 14:51:50 -06001962 }
1963}
1964
John Zulauf1507ee42020-05-18 11:33:09 -06001965bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
1966 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001967 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001968 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06001969 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1970 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001971 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1972 func_name);
1973
John Zulauf355e49b2020-04-24 15:11:15 -06001974 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06001975 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06001976 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1977 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1978 return skip;
1979}
1980bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
1981 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001982 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06001983 bool skip = false;
1984 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1985 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001986 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1987 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06001988 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001989 return skip;
1990}
1991
John Zulauf7635de32020-05-29 17:14:15 -06001992AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
1993 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
1994}
1995
1996bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
1997 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001998 bool skip = false;
1999
John Zulauf7635de32020-05-29 17:14:15 -06002000 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2001 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2002 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2003 // to apply and only copy then, if this proves a hot spot.
2004 std::unique_ptr<AccessContext> proxy_for_current;
2005
John Zulauf355e49b2020-04-24 15:11:15 -06002006 // Validate the "finalLayout" transitions to external
2007 // Get them from where there we're hidding in the extra entry.
2008 const auto &final_transitions = rp_state_->subpass_transitions.back();
2009 for (const auto &transition : final_transitions) {
2010 const auto &attach_view = attachment_views_[transition.attachment];
2011 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2012 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002013 auto *context = trackback.context;
2014
2015 if (transition.prev_pass == current_subpass_) {
2016 if (!proxy_for_current) {
2017 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2018 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2019 }
2020 context = proxy_for_current.get();
2021 }
2022
2023 auto hazard = context->DetectImageBarrierHazard(
John Zulauf355e49b2020-04-24 15:11:15 -06002024 *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope,
2025 attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious);
2026 if (hazard.hazard) {
2027 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2028 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06002029 " final image layout transition. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002030 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf37ceaed2020-07-03 16:18:15 -06002031 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002032 }
2033 }
2034 return skip;
2035}
2036
2037void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2038 // Add layout transitions...
2039 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
2040 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -06002041 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -06002042 for (const auto &transition : transitions) {
2043 const auto attachment_view = attachment_views_[transition.attachment];
2044 if (!attachment_view) continue;
2045 const auto image = attachment_view->image_state.get();
2046 if (!image) continue;
2047
2048 const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
John Zulaufc9201222020-05-13 15:13:03 -06002049 auto insert_pair = view_seen.insert(attachment_view);
2050 if (insert_pair.second) {
2051 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
2052 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag);
2053
2054 } else {
2055 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
2056 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
2057 auto barrier_to_transition = barrier->barrier;
2058 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
2059 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag);
2060 }
John Zulauf355e49b2020-04-24 15:11:15 -06002061 }
2062}
2063
John Zulauf1507ee42020-05-18 11:33:09 -06002064void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2065 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2066 auto &subpass_context = subpass_contexts_[current_subpass_];
2067 VkExtent3D extent = CastTo3D(render_area.extent);
2068 VkOffset3D offset = CastTo3D(render_area.offset);
2069
2070 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2071 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2072 if (attachment_views_[i] == nullptr) continue; // UNUSED
2073 const auto &view = *attachment_views_[i];
2074 const IMAGE_STATE *image = view.image_state.get();
2075 if (image == nullptr) continue;
2076
2077 const auto &ci = attachment_ci[i];
2078 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002079 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002080 const bool is_color = !(has_depth || has_stencil);
2081
2082 if (is_color) {
2083 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2084 extent, tag);
2085 } else {
2086 auto update_range = view.normalized_subresource_range;
2087 if (has_depth) {
2088 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2089 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2090 }
2091 if (has_stencil) {
2092 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2093 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2094 tag);
2095 }
2096 }
2097 }
2098 }
2099}
2100
John Zulauf355e49b2020-04-24 15:11:15 -06002101void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002102 const AccessContext *external_context, VkQueueFlags queue_flags,
2103 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002104 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002105 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002106 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2107 // Add this for all subpasses here so that they exsist during next subpass validation
2108 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002109 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002110 }
2111 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2112
2113 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002114 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002115}
John Zulauf1507ee42020-05-18 11:33:09 -06002116
2117void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002118 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2119 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002120 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002121
John Zulauf355e49b2020-04-24 15:11:15 -06002122 current_subpass_++;
2123 assert(current_subpass_ < subpass_contexts_.size());
2124 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002125 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002126}
2127
John Zulauf1a224292020-06-30 14:52:13 -06002128void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2129 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002130 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002131 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002132 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002133
John Zulauf355e49b2020-04-24 15:11:15 -06002134 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002135 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002136
2137 // Add the "finalLayout" transitions to external
2138 // Get them from where there we're hidding in the extra entry.
2139 const auto &final_transitions = rp_state_->subpass_transitions.back();
2140 for (const auto &transition : final_transitions) {
2141 const auto &attachment = attachment_views_[transition.attachment];
2142 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002143 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf1a224292020-06-30 14:52:13 -06002144 external_context->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier,
2145 attachment->normalized_subresource_range, true, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002146 }
2147}
2148
John Zulauf3d84f1b2020-03-09 13:33:25 -06002149SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2150 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2151 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2152 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2153 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2154 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2155 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2156}
2157
2158void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) {
2159 ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
2160 ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope);
2161}
2162
John Zulauf9cb530d2019-09-30 14:14:10 -06002163HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2164 HazardResult hazard;
2165 auto usage = FlagBit(usage_index);
2166 if (IsRead(usage)) {
John Zulaufc9201222020-05-13 15:13:03 -06002167 if (last_write && IsWriteHazard(usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002168 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002169 }
2170 } else {
2171 // Assume write
2172 // TODO determine what to do with READ-WRITE usage states if any
2173 // Write-After-Write check -- if we have a previous write to test against
2174 if (last_write && IsWriteHazard(usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002175 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002176 } else {
John Zulauf69133422020-05-20 14:55:53 -06002177 // Look for casus belli for WAR
John Zulauf9cb530d2019-09-30 14:14:10 -06002178 const auto usage_stage = PipelineStageBit(usage_index);
John Zulaufd14743a2020-07-03 09:42:39 -06002179 // Note: kNoAttachmentRead is ~0, and thus the no attachment read hazard check doesn't need a separate path.
2180 if (IsReadHazard(usage_stage, input_attachment_barriers)) {
John Zulauf59e25072020-07-17 10:55:21 -06002181 hazard.Set(this, usage_index, WRITE_AFTER_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002182 }
2183 if (!hazard.hazard) {
2184 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002185 const auto &read_access = last_reads[read_index];
2186 if (IsReadHazard(usage_stage, read_access)) {
John Zulauf59e25072020-07-17 10:55:21 -06002187 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002188 break;
2189 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002190 }
2191 }
2192 }
2193 }
2194 return hazard;
2195}
2196
John Zulauf69133422020-05-20 14:55:53 -06002197HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2198 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2199 HazardResult hazard;
2200 const auto usage = FlagBit(usage_index);
2201 const bool write_is_ordered = (last_write & ordering.access_scope) == last_write; // Is true if no write, and that's good.
2202 if (IsRead(usage)) {
2203 if (!write_is_ordered && IsWriteHazard(usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002204 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf69133422020-05-20 14:55:53 -06002205 }
2206 } else {
2207 if (!write_is_ordered && IsWriteHazard(usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002208 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf69133422020-05-20 14:55:53 -06002209 } else {
2210 const auto usage_stage = PipelineStageBit(usage_index);
2211 const auto unordered_reads = last_read_stages & ~ordering.exec_scope;
2212 if (unordered_reads) {
2213 // Look for any WAR hazards outside the ordered set of stages
2214 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002215 const auto &read_access = last_reads[read_index];
2216 if ((read_access.stage & unordered_reads) && IsReadHazard(usage_stage, read_access)) {
John Zulauf59e25072020-07-17 10:55:21 -06002217 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf69133422020-05-20 14:55:53 -06002218 }
2219 }
2220 }
John Zulaufd14743a2020-07-03 09:42:39 -06002221
2222 // This is special case code for the fragment shader input attachment, which unlike all other fragment shader operations
2223 // is framebuffer local, and thus subject to raster ordering guarantees
2224 if (!hazard.hazard && (input_attachment_barriers != kNoAttachmentRead)) {
2225 if (0 == (ordering.access_scope & SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT)) {
2226 // NOTE: Currently all ordering barriers include this bit, so this code may never be reached, but it's
2227 // here s.t. if we need to change the ordering barrier/rules we needn't change the code.
John Zulauf59e25072020-07-17 10:55:21 -06002228 hazard.Set(this, usage_index, WRITE_AFTER_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ,
2229 input_attachment_tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002230 }
2231 }
John Zulauf69133422020-05-20 14:55:53 -06002232 }
2233 }
2234 return hazard;
2235}
2236
John Zulauf2f952d22020-02-10 11:34:51 -07002237// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06002238HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002239 HazardResult hazard;
2240 auto usage = FlagBit(usage_index);
2241 if (IsRead(usage)) {
2242 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002243 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002244 }
2245 } else {
2246 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002247 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002248 } else if (last_read_count > 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002249 hazard.Set(this, usage_index, WRITE_RACING_READ, last_reads[0].access, last_reads[0].tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002250 } else if (input_attachment_barriers != kNoAttachmentRead) {
John Zulauf59e25072020-07-17 10:55:21 -06002251 hazard.Set(this, usage_index, WRITE_RACING_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002252 }
2253 }
2254 return hazard;
2255}
2256
John Zulauf36bcf6a2020-02-03 15:12:52 -07002257HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
2258 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002259 // Only supporting image layout transitions for now
2260 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2261 HazardResult hazard;
2262 if (last_write) {
2263 // If the previous write is *not* in the 1st access scope
2264 // *AND* the current barrier is not in the dependency chain
2265 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2266 // then the barrier access is unsafe (R/W after W)
John Zulauf36bcf6a2020-02-03 15:12:52 -07002267 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
John Zulauf0cb5be22020-01-23 12:18:22 -07002268 // TODO: Do we need a difference hazard name for this?
John Zulauf59e25072020-07-17 10:55:21 -06002269 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002270 }
John Zulauf355e49b2020-04-24 15:11:15 -06002271 }
2272 if (!hazard.hazard) {
2273 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002274 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002275 const auto &read_access = last_reads[read_index];
2276 // If the read stage is not in the src sync sync
2277 // *AND* not execution chained with an existing sync barrier (that's the or)
2278 // then the barrier access is unsafe (R/W after R)
2279 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002280 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002281 break;
2282 }
2283 }
2284 }
John Zulaufd14743a2020-07-03 09:42:39 -06002285 if (!hazard.hazard) {
2286 // Same logic as read acces above for the special case of input attachment read
2287 // Note: kNoReadAttachment is ~0 and thus cannot cause a hazard return.
2288 if ((src_exec_scope & (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | input_attachment_barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002289 hazard.Set(this, usage_index, WRITE_AFTER_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT, input_attachment_tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002290 }
2291 }
John Zulauf0cb5be22020-01-23 12:18:22 -07002292 return hazard;
2293}
2294
John Zulauf5f13a792020-03-10 07:31:21 -06002295// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2296// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2297// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2298void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2299 if (write_tag.IsBefore(other.write_tag)) {
2300 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
2301 *this = other;
2302 } else if (!other.write_tag.IsBefore(write_tag)) {
2303 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2304 // dependency chaining logic or any stage expansion)
2305 write_barriers |= other.write_barriers;
2306
John Zulaufd14743a2020-07-03 09:42:39 -06002307 // Merge the read states
2308 if (input_attachment_barriers == kNoAttachmentRead) {
2309 // this doesn't have an input attachment read, so we'll take other, unconditionally (even if it's kNoAttachmentRead)
2310 input_attachment_barriers = other.input_attachment_barriers;
2311 input_attachment_tag = other.input_attachment_tag;
2312 } else if (other.input_attachment_barriers != kNoAttachmentRead) {
2313 // Both states have an input attachment read, pick the newest tag and merge barriers.
2314 if (input_attachment_tag.IsBefore(other.input_attachment_tag)) {
2315 input_attachment_tag = other.input_attachment_tag;
2316 }
2317 input_attachment_barriers |= other.input_attachment_barriers;
2318 }
2319 // The else clause is that only this has an attachment read and no merge is needed
2320
John Zulauf5f13a792020-03-10 07:31:21 -06002321 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2322 auto &other_read = other.last_reads[other_read_index];
2323 if (last_read_stages & other_read.stage) {
2324 // Merge in the barriers for read stages that exist in *both* this and other
2325 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
2326 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
2327 auto &my_read = last_reads[my_read_index];
2328 if (other_read.stage == my_read.stage) {
2329 if (my_read.tag.IsBefore(other_read.tag)) {
2330 my_read.tag = other_read.tag;
John Zulauf37ceaed2020-07-03 16:18:15 -06002331 my_read.access = other_read.access;
John Zulauf5f13a792020-03-10 07:31:21 -06002332 }
2333 my_read.barriers |= other_read.barriers;
2334 break;
2335 }
2336 }
2337 } else {
2338 // The other read stage doesn't exist in this, so add it.
2339 last_reads[last_read_count] = other_read;
2340 last_read_count++;
2341 last_read_stages |= other_read.stage;
2342 }
2343 }
2344 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
2345 // it.
2346}
2347
John Zulauf9cb530d2019-09-30 14:14:10 -06002348void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2349 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2350 const auto usage_bit = FlagBit(usage_index);
John Zulaufd14743a2020-07-03 09:42:39 -06002351 if (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) {
2352 // Input attachment requires special treatment for raster/load/store ordering guarantees
2353 input_attachment_barriers = 0;
2354 input_attachment_tag = tag;
2355 } else if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002356 // Mulitple outstanding reads may be of interest and do dependency chains independently
2357 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2358 const auto usage_stage = PipelineStageBit(usage_index);
2359 if (usage_stage & last_read_stages) {
2360 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2361 ReadState &access = last_reads[read_index];
2362 if (access.stage == usage_stage) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002363 access.access = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002364 access.barriers = 0;
2365 access.tag = tag;
2366 break;
2367 }
2368 }
2369 } else {
2370 // We don't have this stage in the list yet...
2371 assert(last_read_count < last_reads.size());
2372 ReadState &access = last_reads[last_read_count++];
2373 access.stage = usage_stage;
John Zulauf37ceaed2020-07-03 16:18:15 -06002374 access.access = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002375 access.barriers = 0;
2376 access.tag = tag;
2377 last_read_stages |= usage_stage;
2378 }
2379 } else {
2380 // Assume write
2381 // TODO determine what to do with READ-WRITE operations if any
John Zulaufd14743a2020-07-03 09:42:39 -06002382 // Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
John Zulauf9cb530d2019-09-30 14:14:10 -06002383 // if the last_reads/last_write were unsafe, we've reported them,
2384 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
2385 last_read_count = 0;
2386 last_read_stages = 0;
2387
John Zulaufd14743a2020-07-03 09:42:39 -06002388 input_attachment_barriers = kNoAttachmentRead; // Denotes no outstanding input attachment read after the last write.
2389 // NOTE: we don't reset the tag, as the equality check ignores it when kNoAttachmentRead is set.
2390
John Zulauf9cb530d2019-09-30 14:14:10 -06002391 write_barriers = 0;
2392 write_dependency_chain = 0;
2393 write_tag = tag;
2394 last_write = usage_bit;
2395 }
2396}
John Zulauf5f13a792020-03-10 07:31:21 -06002397
John Zulauf9cb530d2019-09-30 14:14:10 -06002398void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
2399 // Execution Barriers only protect read operations
2400 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2401 ReadState &access = last_reads[read_index];
2402 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2403 if (srcStageMask & (access.stage | access.barriers)) {
2404 access.barriers |= dstStageMask;
2405 }
2406 }
John Zulaufd14743a2020-07-03 09:42:39 -06002407 if (srcStageMask & (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | input_attachment_barriers)) {
2408 input_attachment_barriers |= dstStageMask;
2409 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002410 if (write_dependency_chain & srcStageMask) write_dependency_chain |= dstStageMask;
2411}
2412
John Zulauf36bcf6a2020-02-03 15:12:52 -07002413void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
2414 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002415 // Assuming we've applied the execution side of this barrier, we update just the write
2416 // The || implements the "dependency chain" logic for this barrier
John Zulauf36bcf6a2020-02-03 15:12:52 -07002417 if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) {
2418 write_barriers |= dst_access_scope;
2419 write_dependency_chain |= dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06002420 }
2421}
2422
John Zulauf59e25072020-07-17 10:55:21 -06002423// This should be just Bits or Index, but we don't have an invalid state for Index
2424VkPipelineStageFlags ResourceAccessState::GetReadBarriers(SyncStageAccessFlags usage_bit) const {
2425 VkPipelineStageFlags barriers = 0U;
2426 if (usage_bit & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) {
2427 barriers = input_attachment_barriers;
2428 } else {
2429 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2430 const auto &read_access = last_reads[read_index];
2431 if (read_access.access & usage_bit) {
2432 barriers = read_access.barriers;
2433 break;
2434 }
2435 }
2436 }
2437 return barriers;
2438}
2439
John Zulaufd1f85d42020-04-15 12:23:15 -06002440void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002441 auto *access_context = GetAccessContextNoInsert(command_buffer);
2442 if (access_context) {
2443 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002444 }
2445}
2446
John Zulaufd1f85d42020-04-15 12:23:15 -06002447void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2448 auto access_found = cb_access_state.find(command_buffer);
2449 if (access_found != cb_access_state.end()) {
2450 access_found->second->Reset();
2451 cb_access_state.erase(access_found);
2452 }
2453}
2454
John Zulauf540266b2020-04-06 18:54:53 -06002455void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002456 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
2457 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002458 const VkMemoryBarrier *pMemoryBarriers) {
2459 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07002460 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002461 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002462 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002463}
2464
John Zulauf540266b2020-04-06 18:54:53 -06002465void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002466 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2467 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002468 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002469 for (uint32_t index = 0; index < barrier_count; index++) {
locke-lunarg3c038002020-04-30 23:08:08 -06002470 auto barrier = barriers[index];
John Zulauf9cb530d2019-09-30 14:14:10 -06002471 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2472 if (!buffer) continue;
locke-lunarg3c038002020-04-30 23:08:08 -06002473 barrier.size = GetRealWholeSize(barrier.offset, barrier.size, buffer->createInfo.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002474 ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002475 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2476 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2477 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2478 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002479 }
2480}
2481
John Zulauf540266b2020-04-06 18:54:53 -06002482void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2483 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2484 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002485 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002486 for (uint32_t index = 0; index < barrier_count; index++) {
2487 const auto &barrier = barriers[index];
2488 const auto *image = Get<IMAGE_STATE>(barrier.image);
2489 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002490 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002491 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2492 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2493 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2494 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
2495 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002496 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002497}
2498
2499bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2500 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2501 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002502 const auto *cb_context = GetAccessContext(commandBuffer);
2503 assert(cb_context);
2504 if (!cb_context) return skip;
2505 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002506
John Zulauf3d84f1b2020-03-09 13:33:25 -06002507 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002508 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002509 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002510
2511 for (uint32_t region = 0; region < regionCount; region++) {
2512 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002513 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002514 ResourceAccessRange src_range = MakeRange(
2515 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002516 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002517 if (hazard.hazard) {
2518 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002519 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002520 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002521 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002522 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002523 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002524 }
John Zulauf16adfc92020-04-08 10:28:33 -06002525 if (dst_buffer && !skip) {
locke-lunargff255f92020-05-13 18:53:52 -06002526 ResourceAccessRange dst_range = MakeRange(
2527 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf355e49b2020-04-24 15:11:15 -06002528 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002529 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002530 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002531 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002532 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002533 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002534 }
2535 }
2536 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002537 }
2538 return skip;
2539}
2540
2541void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2542 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002543 auto *cb_context = GetAccessContext(commandBuffer);
2544 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002545 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002546 auto *context = cb_context->GetCurrentAccessContext();
2547
John Zulauf9cb530d2019-09-30 14:14:10 -06002548 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002549 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002550
2551 for (uint32_t region = 0; region < regionCount; region++) {
2552 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002553 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002554 ResourceAccessRange src_range = MakeRange(
2555 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002556 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002557 }
John Zulauf16adfc92020-04-08 10:28:33 -06002558 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002559 ResourceAccessRange dst_range = MakeRange(
2560 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002561 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002562 }
2563 }
2564}
2565
2566bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2567 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2568 const VkImageCopy *pRegions) const {
2569 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002570 const auto *cb_access_context = GetAccessContext(commandBuffer);
2571 assert(cb_access_context);
2572 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002573
John Zulauf3d84f1b2020-03-09 13:33:25 -06002574 const auto *context = cb_access_context->GetCurrentAccessContext();
2575 assert(context);
2576 if (!context) return skip;
2577
2578 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2579 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002580 for (uint32_t region = 0; region < regionCount; region++) {
2581 const auto &copy_region = pRegions[region];
2582 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002583 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002584 copy_region.srcOffset, copy_region.extent);
2585 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002586 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002587 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002588 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002589 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002590 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002591 }
2592
2593 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002594 VkExtent3D dst_copy_extent =
2595 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002596 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002597 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002598 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002599 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002600 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002601 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002602 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002603 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002604 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002605 }
2606 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002607
John Zulauf5c5e88d2019-12-26 11:22:02 -07002608 return skip;
2609}
2610
2611void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2612 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2613 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002614 auto *cb_access_context = GetAccessContext(commandBuffer);
2615 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002616 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002617 auto *context = cb_access_context->GetCurrentAccessContext();
2618 assert(context);
2619
John Zulauf5c5e88d2019-12-26 11:22:02 -07002620 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002621 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002622
2623 for (uint32_t region = 0; region < regionCount; region++) {
2624 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002625 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002626 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2627 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002628 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002629 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002630 VkExtent3D dst_copy_extent =
2631 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002632 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2633 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002634 }
2635 }
2636}
2637
2638bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2639 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2640 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2641 uint32_t bufferMemoryBarrierCount,
2642 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2643 uint32_t imageMemoryBarrierCount,
2644 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2645 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002646 const auto *cb_access_context = GetAccessContext(commandBuffer);
2647 assert(cb_access_context);
2648 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002649
John Zulauf3d84f1b2020-03-09 13:33:25 -06002650 const auto *context = cb_access_context->GetCurrentAccessContext();
2651 assert(context);
2652 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002653
John Zulauf3d84f1b2020-03-09 13:33:25 -06002654 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002655 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2656 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002657 // Validate Image Layout transitions
2658 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2659 const auto &barrier = pImageMemoryBarriers[index];
2660 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2661 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2662 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002663 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002664 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002665 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002666 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002667 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002668 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002669 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002670 }
2671 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002672
2673 return skip;
2674}
2675
2676void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2677 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2678 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2679 uint32_t bufferMemoryBarrierCount,
2680 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2681 uint32_t imageMemoryBarrierCount,
2682 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002683 auto *cb_access_context = GetAccessContext(commandBuffer);
2684 assert(cb_access_context);
2685 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002686 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002687 auto access_context = cb_access_context->GetCurrentAccessContext();
2688 assert(access_context);
2689 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06002690
John Zulauf3d84f1b2020-03-09 13:33:25 -06002691 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002692 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002693 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002694 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
2695 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2696 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002697 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
2698 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002699 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06002700 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002701
2702 // Apply these last in-case there operation is a superset of the other two and would clean them up...
John Zulauf3d84f1b2020-03-09 13:33:25 -06002703 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07002704 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06002705}
2706
2707void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
2708 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
2709 // The state tracker sets up the device state
2710 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
2711
John Zulauf5f13a792020-03-10 07:31:21 -06002712 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
2713 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06002714 // TODO: Find a good way to do this hooklessly.
2715 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
2716 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
2717 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
2718
John Zulaufd1f85d42020-04-15 12:23:15 -06002719 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2720 sync_device_state->ResetCommandBufferCallback(command_buffer);
2721 });
2722 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2723 sync_device_state->FreeCommandBufferCallback(command_buffer);
2724 });
John Zulauf9cb530d2019-09-30 14:14:10 -06002725}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002726
John Zulauf355e49b2020-04-24 15:11:15 -06002727bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2728 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
2729 bool skip = false;
2730 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
2731 auto cb_context = GetAccessContext(commandBuffer);
2732
2733 if (rp_state && cb_context) {
2734 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
2735 }
2736
2737 return skip;
2738}
2739
2740bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2741 VkSubpassContents contents) const {
2742 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2743 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2744 subpass_begin_info.contents = contents;
2745 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
2746 return skip;
2747}
2748
2749bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2750 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2751 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2752 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
2753 return skip;
2754}
2755
2756bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2757 const VkRenderPassBeginInfo *pRenderPassBegin,
2758 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2759 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2760 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
2761 return skip;
2762}
2763
John Zulauf3d84f1b2020-03-09 13:33:25 -06002764void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
2765 VkResult result) {
2766 // The state tracker sets up the command buffer state
2767 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
2768
2769 // Create/initialize the structure that trackers accesses at the command buffer scope.
2770 auto cb_access_context = GetAccessContext(commandBuffer);
2771 assert(cb_access_context);
2772 cb_access_context->Reset();
2773}
2774
2775void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06002776 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002777 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002778 if (cb_context) {
2779 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002780 }
2781}
2782
2783void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2784 VkSubpassContents contents) {
2785 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2786 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2787 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002788 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002789}
2790
2791void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2792 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2793 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002794 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002795}
2796
2797void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2798 const VkRenderPassBeginInfo *pRenderPassBegin,
2799 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2800 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002801 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
2802}
2803
2804bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2805 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
2806 bool skip = false;
2807
2808 auto cb_context = GetAccessContext(commandBuffer);
2809 assert(cb_context);
2810 auto cb_state = cb_context->GetCommandBufferState();
2811 if (!cb_state) return skip;
2812
2813 auto rp_state = cb_state->activeRenderPass;
2814 if (!rp_state) return skip;
2815
2816 skip |= cb_context->ValidateNextSubpass(func_name);
2817
2818 return skip;
2819}
2820
2821bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
2822 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
2823 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2824 subpass_begin_info.contents = contents;
2825 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
2826 return skip;
2827}
2828
2829bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2830 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2831 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2832 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
2833 return skip;
2834}
2835
2836bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2837 const VkSubpassEndInfo *pSubpassEndInfo) const {
2838 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2839 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
2840 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002841}
2842
2843void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06002844 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002845 auto cb_context = GetAccessContext(commandBuffer);
2846 assert(cb_context);
2847 auto cb_state = cb_context->GetCommandBufferState();
2848 if (!cb_state) return;
2849
2850 auto rp_state = cb_state->activeRenderPass;
2851 if (!rp_state) return;
2852
John Zulauf355e49b2020-04-24 15:11:15 -06002853 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002854}
2855
2856void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
2857 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
2858 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2859 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002860 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002861}
2862
2863void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2864 const VkSubpassEndInfo *pSubpassEndInfo) {
2865 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002866 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002867}
2868
2869void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2870 const VkSubpassEndInfo *pSubpassEndInfo) {
2871 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002872 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002873}
2874
John Zulauf355e49b2020-04-24 15:11:15 -06002875bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
2876 const char *func_name) const {
2877 bool skip = false;
2878
2879 auto cb_context = GetAccessContext(commandBuffer);
2880 assert(cb_context);
2881 auto cb_state = cb_context->GetCommandBufferState();
2882 if (!cb_state) return skip;
2883
2884 auto rp_state = cb_state->activeRenderPass;
2885 if (!rp_state) return skip;
2886
2887 skip |= cb_context->ValidateEndRenderpass(func_name);
2888 return skip;
2889}
2890
2891bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2892 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
2893 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
2894 return skip;
2895}
2896
2897bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
2898 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2899 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2900 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
2901 return skip;
2902}
2903
2904bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
2905 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2906 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2907 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
2908 return skip;
2909}
2910
2911void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
2912 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06002913 // Resolve the all subpass contexts to the command buffer contexts
2914 auto cb_context = GetAccessContext(commandBuffer);
2915 assert(cb_context);
2916 auto cb_state = cb_context->GetCommandBufferState();
2917 if (!cb_state) return;
2918
locke-lunargaecf2152020-05-12 17:15:41 -06002919 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06002920 if (!rp_state) return;
2921
John Zulauf355e49b2020-04-24 15:11:15 -06002922 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06002923}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002924
John Zulauf33fc1d52020-07-17 11:01:10 -06002925// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
2926// updates to a resource which do not conflict at the byte level.
2927// TODO: Revisit this rule to see if it needs to be tighter or looser
2928// TODO: Add programatic control over suppression heuristics
2929bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
2930 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
2931}
2932
John Zulauf3d84f1b2020-03-09 13:33:25 -06002933void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002934 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06002935 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002936}
2937
2938void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06002939 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06002940 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002941}
2942
2943void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06002944 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06002945 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002946}
locke-lunarga19c71d2020-03-02 18:17:04 -07002947
2948bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2949 VkImageLayout dstImageLayout, uint32_t regionCount,
2950 const VkBufferImageCopy *pRegions) const {
2951 bool skip = false;
2952 const auto *cb_access_context = GetAccessContext(commandBuffer);
2953 assert(cb_access_context);
2954 if (!cb_access_context) return skip;
2955
2956 const auto *context = cb_access_context->GetCurrentAccessContext();
2957 assert(context);
2958 if (!context) return skip;
2959
2960 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07002961 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2962
2963 for (uint32_t region = 0; region < regionCount; region++) {
2964 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002965 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002966 ResourceAccessRange src_range =
2967 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002968 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002969 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002970 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002971 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002972 "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002973 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002974 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002975 }
2976 }
2977 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002978 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002979 copy_region.imageOffset, copy_region.imageExtent);
2980 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002981 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002982 "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002983 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002984 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002985 }
2986 if (skip) break;
2987 }
2988 if (skip) break;
2989 }
2990 return skip;
2991}
2992
2993void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2994 VkImageLayout dstImageLayout, uint32_t regionCount,
2995 const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06002996 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07002997 auto *cb_access_context = GetAccessContext(commandBuffer);
2998 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002999 const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07003000 auto *context = cb_access_context->GetCurrentAccessContext();
3001 assert(context);
3002
3003 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003004 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003005
3006 for (uint32_t region = 0; region < regionCount; region++) {
3007 const auto &copy_region = pRegions[region];
3008 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003009 ResourceAccessRange src_range =
3010 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003011 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003012 }
3013 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003014 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003015 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003016 }
3017 }
3018}
3019
3020bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3021 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3022 const VkBufferImageCopy *pRegions) const {
3023 bool skip = false;
3024 const auto *cb_access_context = GetAccessContext(commandBuffer);
3025 assert(cb_access_context);
3026 if (!cb_access_context) return skip;
3027
3028 const auto *context = cb_access_context->GetCurrentAccessContext();
3029 assert(context);
3030 if (!context) return skip;
3031
3032 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3033 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3034 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3035 for (uint32_t region = 0; region < regionCount; region++) {
3036 const auto &copy_region = pRegions[region];
3037 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003038 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003039 copy_region.imageOffset, copy_region.imageExtent);
3040 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003041 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003042 "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003043 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003044 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003045 }
3046 }
3047 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06003048 ResourceAccessRange dst_range =
3049 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003050 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003051 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003052 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003053 "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003054 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003055 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003056 }
3057 }
3058 if (skip) break;
3059 }
3060 return skip;
3061}
3062
3063void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3064 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003065 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07003066 auto *cb_access_context = GetAccessContext(commandBuffer);
3067 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003068 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER);
locke-lunarga19c71d2020-03-02 18:17:04 -07003069 auto *context = cb_access_context->GetCurrentAccessContext();
3070 assert(context);
3071
3072 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003073 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3074 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 -06003075 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003076
3077 for (uint32_t region = 0; region < regionCount; region++) {
3078 const auto &copy_region = pRegions[region];
3079 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003080 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003081 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003082 }
3083 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003084 ResourceAccessRange dst_range =
3085 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003086 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003087 }
3088 }
3089}
3090
3091bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3092 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3093 const VkImageBlit *pRegions, VkFilter filter) const {
3094 bool skip = false;
3095 const auto *cb_access_context = GetAccessContext(commandBuffer);
3096 assert(cb_access_context);
3097 if (!cb_access_context) return skip;
3098
3099 const auto *context = cb_access_context->GetCurrentAccessContext();
3100 assert(context);
3101 if (!context) return skip;
3102
3103 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3104 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3105
3106 for (uint32_t region = 0; region < regionCount; region++) {
3107 const auto &blit_region = pRegions[region];
3108 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003109 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3110 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3111 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3112 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3113 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3114 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3115 auto hazard =
3116 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003117 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003118 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003119 "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003120 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003121 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003122 }
3123 }
3124
3125 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003126 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3127 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3128 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3129 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3130 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3131 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3132 auto hazard =
3133 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003134 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003135 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003136 "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003137 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003138 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003139 }
3140 if (skip) break;
3141 }
3142 }
3143
3144 return skip;
3145}
3146
3147void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3148 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3149 const VkImageBlit *pRegions, VkFilter filter) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003150 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3151 pRegions, filter);
locke-lunarga19c71d2020-03-02 18:17:04 -07003152 auto *cb_access_context = GetAccessContext(commandBuffer);
3153 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003154 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07003155 auto *context = cb_access_context->GetCurrentAccessContext();
3156 assert(context);
3157
3158 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003159 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003160
3161 for (uint32_t region = 0; region < regionCount; region++) {
3162 const auto &blit_region = pRegions[region];
3163 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003164 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3165 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3166 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3167 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3168 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3169 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3170 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003171 }
3172 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003173 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3174 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3175 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3176 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3177 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3178 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3179 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003180 }
3181 }
3182}
locke-lunarg36ba2592020-04-03 09:42:04 -06003183
locke-lunarg61870c22020-06-09 14:51:50 -06003184bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
3185 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
3186 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003187 bool skip = false;
3188 if (drawCount == 0) return skip;
3189
3190 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3191 VkDeviceSize size = struct_size;
3192 if (drawCount == 1 || stride == size) {
3193 if (drawCount > 1) size *= drawCount;
3194 ResourceAccessRange range = MakeRange(offset, size);
3195 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3196 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003197 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003198 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003199 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003200 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003201 }
3202 } else {
3203 for (uint32_t i = 0; i < drawCount; ++i) {
3204 ResourceAccessRange range = MakeRange(offset + i * stride, size);
3205 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3206 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003207 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003208 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3209 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3210 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003211 break;
3212 }
3213 }
3214 }
3215 return skip;
3216}
3217
locke-lunarg61870c22020-06-09 14:51:50 -06003218void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3219 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3220 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003221 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3222 VkDeviceSize size = struct_size;
3223 if (drawCount == 1 || stride == size) {
3224 if (drawCount > 1) size *= drawCount;
3225 ResourceAccessRange range = MakeRange(offset, size);
3226 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3227 } else {
3228 for (uint32_t i = 0; i < drawCount; ++i) {
3229 ResourceAccessRange range = MakeRange(offset + i * stride, size);
3230 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3231 }
3232 }
3233}
3234
locke-lunarg61870c22020-06-09 14:51:50 -06003235bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3236 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003237 bool skip = false;
3238
3239 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3240 ResourceAccessRange range = MakeRange(offset, 4);
3241 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3242 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003243 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003244 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003245 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003246 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003247 }
3248 return skip;
3249}
3250
locke-lunarg61870c22020-06-09 14:51:50 -06003251void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003252 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3253 ResourceAccessRange range = MakeRange(offset, 4);
3254 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3255}
3256
locke-lunarg36ba2592020-04-03 09:42:04 -06003257bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003258 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003259 const auto *cb_access_context = GetAccessContext(commandBuffer);
3260 assert(cb_access_context);
3261 if (!cb_access_context) return skip;
3262
locke-lunarg61870c22020-06-09 14:51:50 -06003263 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003264 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003265}
3266
3267void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003268 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003269 auto *cb_access_context = GetAccessContext(commandBuffer);
3270 assert(cb_access_context);
3271 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003272
locke-lunarg61870c22020-06-09 14:51:50 -06003273 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003274}
locke-lunarge1a67022020-04-29 00:15:36 -06003275
3276bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003277 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003278 const auto *cb_access_context = GetAccessContext(commandBuffer);
3279 assert(cb_access_context);
3280 if (!cb_access_context) return skip;
3281
3282 const auto *context = cb_access_context->GetCurrentAccessContext();
3283 assert(context);
3284 if (!context) return skip;
3285
locke-lunarg61870c22020-06-09 14:51:50 -06003286 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3287 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3288 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003289 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003290}
3291
3292void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003293 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003294 auto *cb_access_context = GetAccessContext(commandBuffer);
3295 assert(cb_access_context);
3296 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3297 auto *context = cb_access_context->GetCurrentAccessContext();
3298 assert(context);
3299
locke-lunarg61870c22020-06-09 14:51:50 -06003300 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3301 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003302}
3303
3304bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3305 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003306 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003307 const auto *cb_access_context = GetAccessContext(commandBuffer);
3308 assert(cb_access_context);
3309 if (!cb_access_context) return skip;
3310
locke-lunarg61870c22020-06-09 14:51:50 -06003311 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3312 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3313 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003314 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003315}
3316
3317void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3318 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003319 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003320 auto *cb_access_context = GetAccessContext(commandBuffer);
3321 assert(cb_access_context);
3322 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003323
locke-lunarg61870c22020-06-09 14:51:50 -06003324 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3325 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3326 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003327}
3328
3329bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3330 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003331 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003332 const auto *cb_access_context = GetAccessContext(commandBuffer);
3333 assert(cb_access_context);
3334 if (!cb_access_context) return skip;
3335
locke-lunarg61870c22020-06-09 14:51:50 -06003336 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3337 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3338 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003339 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003340}
3341
3342void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3343 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003344 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003345 auto *cb_access_context = GetAccessContext(commandBuffer);
3346 assert(cb_access_context);
3347 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003348
locke-lunarg61870c22020-06-09 14:51:50 -06003349 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3350 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3351 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003352}
3353
3354bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3355 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003356 bool skip = false;
3357 if (drawCount == 0) return skip;
3358
locke-lunargff255f92020-05-13 18:53:52 -06003359 const auto *cb_access_context = GetAccessContext(commandBuffer);
3360 assert(cb_access_context);
3361 if (!cb_access_context) return skip;
3362
3363 const auto *context = cb_access_context->GetCurrentAccessContext();
3364 assert(context);
3365 if (!context) return skip;
3366
locke-lunarg61870c22020-06-09 14:51:50 -06003367 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3368 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3369 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3370 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003371
3372 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3373 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3374 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003375 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003376 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003377}
3378
3379void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3380 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003381 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003382 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003383 auto *cb_access_context = GetAccessContext(commandBuffer);
3384 assert(cb_access_context);
3385 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3386 auto *context = cb_access_context->GetCurrentAccessContext();
3387 assert(context);
3388
locke-lunarg61870c22020-06-09 14:51:50 -06003389 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3390 cb_access_context->RecordDrawSubpassAttachment(tag);
3391 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003392
3393 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3394 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3395 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003396 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003397}
3398
3399bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3400 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003401 bool skip = false;
3402 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003403 const auto *cb_access_context = GetAccessContext(commandBuffer);
3404 assert(cb_access_context);
3405 if (!cb_access_context) return skip;
3406
3407 const auto *context = cb_access_context->GetCurrentAccessContext();
3408 assert(context);
3409 if (!context) return skip;
3410
locke-lunarg61870c22020-06-09 14:51:50 -06003411 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3412 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3413 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3414 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003415
3416 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3417 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3418 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003419 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003420 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003421}
3422
3423void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3424 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003425 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003426 auto *cb_access_context = GetAccessContext(commandBuffer);
3427 assert(cb_access_context);
3428 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3429 auto *context = cb_access_context->GetCurrentAccessContext();
3430 assert(context);
3431
locke-lunarg61870c22020-06-09 14:51:50 -06003432 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3433 cb_access_context->RecordDrawSubpassAttachment(tag);
3434 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003435
3436 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3437 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3438 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003439 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003440}
3441
3442bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3443 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3444 uint32_t stride, const char *function) const {
3445 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003446 const auto *cb_access_context = GetAccessContext(commandBuffer);
3447 assert(cb_access_context);
3448 if (!cb_access_context) return skip;
3449
3450 const auto *context = cb_access_context->GetCurrentAccessContext();
3451 assert(context);
3452 if (!context) return skip;
3453
locke-lunarg61870c22020-06-09 14:51:50 -06003454 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3455 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3456 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3457 function);
3458 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003459
3460 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3461 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3462 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003463 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003464 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003465}
3466
3467bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3468 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3469 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003470 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3471 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003472}
3473
3474void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3475 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3476 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003477 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3478 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003479 auto *cb_access_context = GetAccessContext(commandBuffer);
3480 assert(cb_access_context);
3481 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3482 auto *context = cb_access_context->GetCurrentAccessContext();
3483 assert(context);
3484
locke-lunarg61870c22020-06-09 14:51:50 -06003485 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3486 cb_access_context->RecordDrawSubpassAttachment(tag);
3487 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3488 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003489
3490 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3491 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3492 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003493 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003494}
3495
3496bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3497 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3498 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003499 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3500 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003501}
3502
3503void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3504 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3505 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003506 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3507 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003508 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003509}
3510
3511bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3512 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3513 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003514 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3515 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003516}
3517
3518void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3519 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3520 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003521 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3522 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003523 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3524}
3525
3526bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3527 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3528 uint32_t stride, const char *function) const {
3529 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003530 const auto *cb_access_context = GetAccessContext(commandBuffer);
3531 assert(cb_access_context);
3532 if (!cb_access_context) return skip;
3533
3534 const auto *context = cb_access_context->GetCurrentAccessContext();
3535 assert(context);
3536 if (!context) return skip;
3537
locke-lunarg61870c22020-06-09 14:51:50 -06003538 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3539 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3540 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3541 stride, function);
3542 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003543
3544 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3545 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3546 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003547 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003548 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003549}
3550
3551bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3552 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3553 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003554 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3555 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003556}
3557
3558void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3559 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3560 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003561 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3562 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003563 auto *cb_access_context = GetAccessContext(commandBuffer);
3564 assert(cb_access_context);
3565 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3566 auto *context = cb_access_context->GetCurrentAccessContext();
3567 assert(context);
3568
locke-lunarg61870c22020-06-09 14:51:50 -06003569 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3570 cb_access_context->RecordDrawSubpassAttachment(tag);
3571 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3572 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003573
3574 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3575 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06003576 // We will update the index and vertex buffer in SubmitQueue in the future.
3577 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003578}
3579
3580bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3581 VkDeviceSize offset, VkBuffer countBuffer,
3582 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3583 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003584 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3585 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003586}
3587
3588void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3589 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3590 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003591 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3592 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003593 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3594}
3595
3596bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3597 VkDeviceSize offset, VkBuffer countBuffer,
3598 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3599 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003600 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3601 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003602}
3603
3604void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3605 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3606 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003607 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3608 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003609 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3610}
3611
3612bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3613 const VkClearColorValue *pColor, uint32_t rangeCount,
3614 const VkImageSubresourceRange *pRanges) const {
3615 bool skip = false;
3616 const auto *cb_access_context = GetAccessContext(commandBuffer);
3617 assert(cb_access_context);
3618 if (!cb_access_context) return skip;
3619
3620 const auto *context = cb_access_context->GetCurrentAccessContext();
3621 assert(context);
3622 if (!context) return skip;
3623
3624 const auto *image_state = Get<IMAGE_STATE>(image);
3625
3626 for (uint32_t index = 0; index < rangeCount; index++) {
3627 const auto &range = pRanges[index];
3628 if (image_state) {
3629 auto hazard =
3630 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3631 if (hazard.hazard) {
3632 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003633 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003634 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06003635 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003636 }
3637 }
3638 }
3639 return skip;
3640}
3641
3642void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3643 const VkClearColorValue *pColor, uint32_t rangeCount,
3644 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003645 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003646 auto *cb_access_context = GetAccessContext(commandBuffer);
3647 assert(cb_access_context);
3648 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
3649 auto *context = cb_access_context->GetCurrentAccessContext();
3650 assert(context);
3651
3652 const auto *image_state = Get<IMAGE_STATE>(image);
3653
3654 for (uint32_t index = 0; index < rangeCount; index++) {
3655 const auto &range = pRanges[index];
3656 if (image_state) {
3657 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3658 tag);
3659 }
3660 }
3661}
3662
3663bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3664 VkImageLayout imageLayout,
3665 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3666 const VkImageSubresourceRange *pRanges) const {
3667 bool skip = false;
3668 const auto *cb_access_context = GetAccessContext(commandBuffer);
3669 assert(cb_access_context);
3670 if (!cb_access_context) return skip;
3671
3672 const auto *context = cb_access_context->GetCurrentAccessContext();
3673 assert(context);
3674 if (!context) return skip;
3675
3676 const auto *image_state = Get<IMAGE_STATE>(image);
3677
3678 for (uint32_t index = 0; index < rangeCount; index++) {
3679 const auto &range = pRanges[index];
3680 if (image_state) {
3681 auto hazard =
3682 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3683 if (hazard.hazard) {
3684 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003685 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003686 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06003687 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003688 }
3689 }
3690 }
3691 return skip;
3692}
3693
3694void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3695 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3696 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003697 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003698 auto *cb_access_context = GetAccessContext(commandBuffer);
3699 assert(cb_access_context);
3700 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
3701 auto *context = cb_access_context->GetCurrentAccessContext();
3702 assert(context);
3703
3704 const auto *image_state = Get<IMAGE_STATE>(image);
3705
3706 for (uint32_t index = 0; index < rangeCount; index++) {
3707 const auto &range = pRanges[index];
3708 if (image_state) {
3709 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3710 tag);
3711 }
3712 }
3713}
3714
3715bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
3716 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
3717 VkDeviceSize dstOffset, VkDeviceSize stride,
3718 VkQueryResultFlags flags) const {
3719 bool skip = false;
3720 const auto *cb_access_context = GetAccessContext(commandBuffer);
3721 assert(cb_access_context);
3722 if (!cb_access_context) return skip;
3723
3724 const auto *context = cb_access_context->GetCurrentAccessContext();
3725 assert(context);
3726 if (!context) return skip;
3727
3728 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3729
3730 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003731 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003732 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3733 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06003734 skip |=
3735 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3736 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
3737 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003738 }
3739 }
locke-lunargff255f92020-05-13 18:53:52 -06003740
3741 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003742 return skip;
3743}
3744
3745void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
3746 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3747 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003748 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
3749 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06003750 auto *cb_access_context = GetAccessContext(commandBuffer);
3751 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06003752 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06003753 auto *context = cb_access_context->GetCurrentAccessContext();
3754 assert(context);
3755
3756 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3757
3758 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003759 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003760 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3761 }
locke-lunargff255f92020-05-13 18:53:52 -06003762
3763 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003764}
3765
3766bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3767 VkDeviceSize size, uint32_t data) const {
3768 bool skip = false;
3769 const auto *cb_access_context = GetAccessContext(commandBuffer);
3770 assert(cb_access_context);
3771 if (!cb_access_context) return skip;
3772
3773 const auto *context = cb_access_context->GetCurrentAccessContext();
3774 assert(context);
3775 if (!context) return skip;
3776
3777 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3778
3779 if (dst_buffer) {
3780 ResourceAccessRange range = MakeRange(dstOffset, size);
3781 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3782 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003783 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003784 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06003785 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003786 }
3787 }
3788 return skip;
3789}
3790
3791void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3792 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003793 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06003794 auto *cb_access_context = GetAccessContext(commandBuffer);
3795 assert(cb_access_context);
3796 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
3797 auto *context = cb_access_context->GetCurrentAccessContext();
3798 assert(context);
3799
3800 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3801
3802 if (dst_buffer) {
3803 ResourceAccessRange range = MakeRange(dstOffset, size);
3804 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3805 }
3806}
3807
3808bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3809 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3810 const VkImageResolve *pRegions) const {
3811 bool skip = false;
3812 const auto *cb_access_context = GetAccessContext(commandBuffer);
3813 assert(cb_access_context);
3814 if (!cb_access_context) return skip;
3815
3816 const auto *context = cb_access_context->GetCurrentAccessContext();
3817 assert(context);
3818 if (!context) return skip;
3819
3820 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3821 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3822
3823 for (uint32_t region = 0; region < regionCount; region++) {
3824 const auto &resolve_region = pRegions[region];
3825 if (src_image) {
3826 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3827 resolve_region.srcOffset, resolve_region.extent);
3828 if (hazard.hazard) {
3829 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003830 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003831 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003832 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003833 }
3834 }
3835
3836 if (dst_image) {
3837 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3838 resolve_region.dstOffset, resolve_region.extent);
3839 if (hazard.hazard) {
3840 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003841 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003842 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003843 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003844 }
3845 if (skip) break;
3846 }
3847 }
3848
3849 return skip;
3850}
3851
3852void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3853 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3854 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003855 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3856 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06003857 auto *cb_access_context = GetAccessContext(commandBuffer);
3858 assert(cb_access_context);
3859 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
3860 auto *context = cb_access_context->GetCurrentAccessContext();
3861 assert(context);
3862
3863 auto *src_image = Get<IMAGE_STATE>(srcImage);
3864 auto *dst_image = Get<IMAGE_STATE>(dstImage);
3865
3866 for (uint32_t region = 0; region < regionCount; region++) {
3867 const auto &resolve_region = pRegions[region];
3868 if (src_image) {
3869 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3870 resolve_region.srcOffset, resolve_region.extent, tag);
3871 }
3872 if (dst_image) {
3873 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3874 resolve_region.dstOffset, resolve_region.extent, tag);
3875 }
3876 }
3877}
3878
3879bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3880 VkDeviceSize dataSize, const void *pData) const {
3881 bool skip = false;
3882 const auto *cb_access_context = GetAccessContext(commandBuffer);
3883 assert(cb_access_context);
3884 if (!cb_access_context) return skip;
3885
3886 const auto *context = cb_access_context->GetCurrentAccessContext();
3887 assert(context);
3888 if (!context) return skip;
3889
3890 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3891
3892 if (dst_buffer) {
3893 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3894 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3895 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003896 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003897 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06003898 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003899 }
3900 }
3901 return skip;
3902}
3903
3904void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3905 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003906 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06003907 auto *cb_access_context = GetAccessContext(commandBuffer);
3908 assert(cb_access_context);
3909 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
3910 auto *context = cb_access_context->GetCurrentAccessContext();
3911 assert(context);
3912
3913 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3914
3915 if (dst_buffer) {
3916 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3917 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3918 }
3919}
locke-lunargff255f92020-05-13 18:53:52 -06003920
3921bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3922 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
3923 bool skip = false;
3924 const auto *cb_access_context = GetAccessContext(commandBuffer);
3925 assert(cb_access_context);
3926 if (!cb_access_context) return skip;
3927
3928 const auto *context = cb_access_context->GetCurrentAccessContext();
3929 assert(context);
3930 if (!context) return skip;
3931
3932 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3933
3934 if (dst_buffer) {
3935 ResourceAccessRange range = MakeRange(dstOffset, 4);
3936 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3937 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06003938 skip |=
3939 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3940 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
3941 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003942 }
3943 }
3944 return skip;
3945}
3946
3947void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3948 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003949 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06003950 auto *cb_access_context = GetAccessContext(commandBuffer);
3951 assert(cb_access_context);
3952 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
3953 auto *context = cb_access_context->GetCurrentAccessContext();
3954 assert(context);
3955
3956 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3957
3958 if (dst_buffer) {
3959 ResourceAccessRange range = MakeRange(dstOffset, 4);
3960 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3961 }
3962}