blob: a8d491922291e6401f4aab1b38500652e84c8592 [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;
1478 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1479 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1480 } else {
1481 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1482 }
1483 if (!img_view_state) continue;
1484 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1485 VkExtent3D extent = {};
1486 VkOffset3D offset = {};
1487 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1488 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1489 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1490 } else {
1491 extent = img_state->createInfo.extent;
1492 }
1493 auto hazard = current_context_->DetectHazard(*img_state, sync_index,
1494 img_view_state->normalized_subresource_range, offset, extent);
1495 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06001496 skip |= sync_state_->LogError(
1497 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001498 "%s: Hazard %s for %s in %s, %s, and %s binding #%" PRIu32 " index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001499 func_name, string_SyncHazard(hazard.hazard),
1500 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1501 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1502 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1503 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(), set_binding.first.second,
John Zulauf37ceaed2020-07-03 16:18:15 -06001504 index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001505 }
1506 break;
1507 }
1508 case DescriptorClass::TexelBuffer: {
1509 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1510 if (!buf_view_state) continue;
1511 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1512 ResourceAccessRange range =
1513 MakeRange(buf_view_state->create_info.offset,
1514 GetRealWholeSize(buf_view_state->create_info.offset, buf_view_state->create_info.range,
1515 buf_state->createInfo.size));
1516 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
1517 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001518 skip |= sync_state_->LogError(
1519 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001520 "%s: Hazard %s for %s in %s, %s, and %s binding #%d index %d. Access info %s.", func_name,
locke-lunarg88dbb542020-06-23 22:05:42 -06001521 string_SyncHazard(hazard.hazard),
1522 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1523 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1524 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1525 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(), set_binding.first.second,
John Zulauf37ceaed2020-07-03 16:18:15 -06001526 index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001527 }
1528 break;
1529 }
1530 case DescriptorClass::GeneralBuffer: {
1531 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1532 auto buf_state = buffer_descriptor->GetBufferState();
1533 if (!buf_state) continue;
1534 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1535 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
1536 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001537 skip |= sync_state_->LogError(
1538 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001539 "%s: Hazard %s for %s in %s, %s, and %s binding #%d index %d. Access info %s.", func_name,
locke-lunarg88dbb542020-06-23 22:05:42 -06001540 string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
1541 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1542 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1543 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(), set_binding.first.second,
John Zulauf37ceaed2020-07-03 16:18:15 -06001544 index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001545 }
1546 break;
1547 }
1548 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1549 default:
1550 break;
1551 }
1552 }
1553 }
1554 }
1555 return skip;
1556}
1557
1558void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1559 const ResourceUsageTag &tag) {
1560 const PIPELINE_STATE *pPipe = nullptr;
1561 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1562 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1563 if (!pPipe || !per_sets) {
1564 return;
1565 }
1566
1567 using DescriptorClass = cvdescriptorset::DescriptorClass;
1568 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1569 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1570 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1571 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1572
1573 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001574 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
1575 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1576 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001577 for (const auto &set_binding : stage_state.descriptor_uses) {
1578 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1579 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1580 set_binding.first.second);
1581 const auto descriptor_type = binding_it.GetType();
1582 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1583 auto array_idx = 0;
1584
1585 if (binding_it.IsVariableDescriptorCount()) {
1586 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1587 }
1588 SyncStageAccessIndex sync_index =
1589 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1590
1591 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1592 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1593 switch (descriptor->GetClass()) {
1594 case DescriptorClass::ImageSampler:
1595 case DescriptorClass::Image: {
1596 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1597 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1598 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1599 } else {
1600 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1601 }
1602 if (!img_view_state) continue;
1603 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1604 VkExtent3D extent = {};
1605 VkOffset3D offset = {};
1606 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1607 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1608 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1609 } else {
1610 extent = img_state->createInfo.extent;
1611 }
1612 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1613 offset, extent, tag);
1614 break;
1615 }
1616 case DescriptorClass::TexelBuffer: {
1617 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1618 if (!buf_view_state) continue;
1619 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1620 ResourceAccessRange range =
1621 MakeRange(buf_view_state->create_info.offset, buf_view_state->create_info.range);
1622 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1623 break;
1624 }
1625 case DescriptorClass::GeneralBuffer: {
1626 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1627 auto buf_state = buffer_descriptor->GetBufferState();
1628 if (!buf_state) continue;
1629 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1630 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1631 break;
1632 }
1633 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1634 default:
1635 break;
1636 }
1637 }
1638 }
1639 }
1640}
1641
1642bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1643 bool skip = false;
1644 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1645 if (!pPipe) {
1646 return skip;
1647 }
1648
1649 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1650 const auto &binding_buffers_size = binding_buffers.size();
1651 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1652
1653 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1654 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1655 if (binding_description.binding < binding_buffers_size) {
1656 const auto &binding_buffer = binding_buffers[binding_description.binding];
1657 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1658
1659 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1660 VkDeviceSize range_start = 0;
1661 VkDeviceSize range_size = 0;
1662 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1663 binding_description.stride);
1664 ResourceAccessRange range = MakeRange(range_start, range_size);
1665 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1666 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001667 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001668 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 -06001669 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001670 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001671 }
1672 }
1673 }
1674 return skip;
1675}
1676
1677void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
1678 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1679 if (!pPipe) {
1680 return;
1681 }
1682 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1683 const auto &binding_buffers_size = binding_buffers.size();
1684 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1685
1686 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1687 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1688 if (binding_description.binding < binding_buffers_size) {
1689 const auto &binding_buffer = binding_buffers[binding_description.binding];
1690 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1691
1692 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1693 VkDeviceSize range_start = 0;
1694 VkDeviceSize range_size = 0;
1695 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1696 binding_description.stride);
1697 ResourceAccessRange range = MakeRange(range_start, range_size);
1698 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1699 }
1700 }
1701}
1702
1703bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1704 bool skip = false;
1705 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
1706
1707 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1708 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1709 VkDeviceSize range_start = 0;
1710 VkDeviceSize range_size = 0;
1711 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1712 indexCount, index_size);
1713 ResourceAccessRange range = MakeRange(range_start, range_size);
1714 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1715 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001716 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001717 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 -06001718 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001719 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001720 }
1721
1722 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1723 // We will detect more accurate range in the future.
1724 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1725 return skip;
1726}
1727
1728void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
1729 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return;
1730
1731 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1732 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1733 VkDeviceSize range_start = 0;
1734 VkDeviceSize range_size = 0;
1735 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1736 indexCount, index_size);
1737 ResourceAccessRange range = MakeRange(range_start, range_size);
1738 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1739
1740 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1741 // We will detect more accurate range in the future.
1742 RecordDrawVertex(UINT32_MAX, 0, tag);
1743}
1744
1745bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06001746 bool skip = false;
1747 if (!current_renderpass_context_) return skip;
1748 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1749 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1750 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001751}
1752
1753void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
locke-lunarg7077d502020-06-18 21:37:26 -06001754 if (current_renderpass_context_)
1755 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1756 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001757}
1758
John Zulauf355e49b2020-04-24 15:11:15 -06001759bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001760 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001761 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06001762 skip |=
1763 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001764
1765 return skip;
1766}
1767
1768bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1769 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001770 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001771 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001772 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06001773 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1774 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001775
1776 return skip;
1777}
1778
1779void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1780 assert(sync_state_);
1781 if (!cb_state_) return;
1782
1783 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06001784 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06001785 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06001786 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001787 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001788}
1789
John Zulauf355e49b2020-04-24 15:11:15 -06001790void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001791 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001792 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001793 current_context_ = &current_renderpass_context_->CurrentContext();
1794}
1795
John Zulauf355e49b2020-04-24 15:11:15 -06001796void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001797 assert(current_renderpass_context_);
1798 if (!current_renderpass_context_) return;
1799
John Zulauf1a224292020-06-30 14:52:13 -06001800 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001801 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001802 current_renderpass_context_ = nullptr;
1803}
1804
locke-lunarg61870c22020-06-09 14:51:50 -06001805bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1806 const VkRect2D &render_area, const char *func_name) const {
1807 bool skip = false;
locke-lunarg96dc9632020-06-10 17:22:18 -06001808 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001809 if (!pPipe ||
1810 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001811 return skip;
1812 }
1813 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001814 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1815 VkExtent3D extent = CastTo3D(render_area.extent);
1816 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001817
John Zulauf1a224292020-06-30 14:52:13 -06001818 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001819 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001820 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1821 for (const auto location : list) {
1822 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1823 continue;
1824 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001825 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1826 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06001827 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001828 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001829 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001830 func_name, string_SyncHazard(hazard.hazard),
1831 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1832 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001833 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001834 }
1835 }
1836 }
locke-lunarg37047832020-06-12 13:44:45 -06001837
1838 // PHASE1 TODO: Add layout based read/vs. write selection.
1839 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1840 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1841 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001842 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001843 bool depth_write = false, stencil_write = false;
1844
1845 // PHASE1 TODO: These validation should be in core_checks.
1846 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1847 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1848 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1849 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1850 depth_write = true;
1851 }
1852 // PHASE1 TODO: It needs to check if stencil is writable.
1853 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1854 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1855 // PHASE1 TODO: These validation should be in core_checks.
1856 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1857 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1858 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1859 stencil_write = true;
1860 }
1861
1862 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1863 if (depth_write) {
1864 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001865 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1866 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001867 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001868 skip |= sync_state.LogError(
1869 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001870 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001871 func_name, string_SyncHazard(hazard.hazard),
1872 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1873 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001874 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001875 }
1876 }
1877 if (stencil_write) {
1878 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001879 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1880 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001881 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001882 skip |= sync_state.LogError(
1883 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001884 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001885 func_name, string_SyncHazard(hazard.hazard),
1886 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1887 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001888 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001889 }
locke-lunarg61870c22020-06-09 14:51:50 -06001890 }
1891 }
1892 return skip;
1893}
1894
locke-lunarg96dc9632020-06-10 17:22:18 -06001895void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1896 const ResourceUsageTag &tag) {
1897 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001898 if (!pPipe ||
1899 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001900 return;
1901 }
1902 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001903 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1904 VkExtent3D extent = CastTo3D(render_area.extent);
1905 VkOffset3D offset = CastTo3D(render_area.offset);
1906
John Zulauf1a224292020-06-30 14:52:13 -06001907 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001908 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001909 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1910 for (const auto location : list) {
1911 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1912 continue;
1913 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001914 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
1915 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001916 }
1917 }
locke-lunarg37047832020-06-12 13:44:45 -06001918
1919 // PHASE1 TODO: Add layout based read/vs. write selection.
1920 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1921 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1922 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001923 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001924 bool depth_write = false, stencil_write = false;
1925
1926 // PHASE1 TODO: These validation should be in core_checks.
1927 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1928 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1929 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1930 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1931 depth_write = true;
1932 }
1933 // PHASE1 TODO: It needs to check if stencil is writable.
1934 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1935 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1936 // PHASE1 TODO: These validation should be in core_checks.
1937 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1938 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1939 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1940 stencil_write = true;
1941 }
1942
1943 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1944 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001945 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1946 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001947 }
1948 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001949 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1950 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001951 }
locke-lunarg61870c22020-06-09 14:51:50 -06001952 }
1953}
1954
John Zulauf1507ee42020-05-18 11:33:09 -06001955bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
1956 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001957 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001958 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06001959 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1960 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001961 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1962 func_name);
1963
John Zulauf355e49b2020-04-24 15:11:15 -06001964 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06001965 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06001966 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1967 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1968 return skip;
1969}
1970bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
1971 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001972 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06001973 bool skip = false;
1974 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1975 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001976 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1977 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06001978 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001979 return skip;
1980}
1981
John Zulauf7635de32020-05-29 17:14:15 -06001982AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
1983 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
1984}
1985
1986bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
1987 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001988 bool skip = false;
1989
John Zulauf7635de32020-05-29 17:14:15 -06001990 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
1991 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
1992 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1993 // to apply and only copy then, if this proves a hot spot.
1994 std::unique_ptr<AccessContext> proxy_for_current;
1995
John Zulauf355e49b2020-04-24 15:11:15 -06001996 // Validate the "finalLayout" transitions to external
1997 // Get them from where there we're hidding in the extra entry.
1998 const auto &final_transitions = rp_state_->subpass_transitions.back();
1999 for (const auto &transition : final_transitions) {
2000 const auto &attach_view = attachment_views_[transition.attachment];
2001 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2002 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002003 auto *context = trackback.context;
2004
2005 if (transition.prev_pass == current_subpass_) {
2006 if (!proxy_for_current) {
2007 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2008 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2009 }
2010 context = proxy_for_current.get();
2011 }
2012
2013 auto hazard = context->DetectImageBarrierHazard(
John Zulauf355e49b2020-04-24 15:11:15 -06002014 *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope,
2015 attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious);
2016 if (hazard.hazard) {
2017 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2018 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06002019 " final image layout transition. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002020 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf37ceaed2020-07-03 16:18:15 -06002021 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002022 }
2023 }
2024 return skip;
2025}
2026
2027void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2028 // Add layout transitions...
2029 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
2030 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -06002031 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -06002032 for (const auto &transition : transitions) {
2033 const auto attachment_view = attachment_views_[transition.attachment];
2034 if (!attachment_view) continue;
2035 const auto image = attachment_view->image_state.get();
2036 if (!image) continue;
2037
2038 const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
John Zulaufc9201222020-05-13 15:13:03 -06002039 auto insert_pair = view_seen.insert(attachment_view);
2040 if (insert_pair.second) {
2041 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
2042 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag);
2043
2044 } else {
2045 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
2046 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
2047 auto barrier_to_transition = barrier->barrier;
2048 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
2049 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag);
2050 }
John Zulauf355e49b2020-04-24 15:11:15 -06002051 }
2052}
2053
John Zulauf1507ee42020-05-18 11:33:09 -06002054void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2055 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2056 auto &subpass_context = subpass_contexts_[current_subpass_];
2057 VkExtent3D extent = CastTo3D(render_area.extent);
2058 VkOffset3D offset = CastTo3D(render_area.offset);
2059
2060 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2061 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2062 if (attachment_views_[i] == nullptr) continue; // UNUSED
2063 const auto &view = *attachment_views_[i];
2064 const IMAGE_STATE *image = view.image_state.get();
2065 if (image == nullptr) continue;
2066
2067 const auto &ci = attachment_ci[i];
2068 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002069 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002070 const bool is_color = !(has_depth || has_stencil);
2071
2072 if (is_color) {
2073 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2074 extent, tag);
2075 } else {
2076 auto update_range = view.normalized_subresource_range;
2077 if (has_depth) {
2078 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2079 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2080 }
2081 if (has_stencil) {
2082 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2083 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2084 tag);
2085 }
2086 }
2087 }
2088 }
2089}
2090
John Zulauf355e49b2020-04-24 15:11:15 -06002091void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002092 const AccessContext *external_context, VkQueueFlags queue_flags,
2093 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002094 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002095 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002096 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2097 // Add this for all subpasses here so that they exsist during next subpass validation
2098 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002099 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002100 }
2101 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2102
2103 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002104 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002105}
John Zulauf1507ee42020-05-18 11:33:09 -06002106
2107void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002108 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2109 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002110 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002111
John Zulauf355e49b2020-04-24 15:11:15 -06002112 current_subpass_++;
2113 assert(current_subpass_ < subpass_contexts_.size());
2114 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002115 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002116}
2117
John Zulauf1a224292020-06-30 14:52:13 -06002118void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2119 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002120 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002121 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002122 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002123
John Zulauf355e49b2020-04-24 15:11:15 -06002124 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002125 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002126
2127 // Add the "finalLayout" transitions to external
2128 // Get them from where there we're hidding in the extra entry.
2129 const auto &final_transitions = rp_state_->subpass_transitions.back();
2130 for (const auto &transition : final_transitions) {
2131 const auto &attachment = attachment_views_[transition.attachment];
2132 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002133 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf1a224292020-06-30 14:52:13 -06002134 external_context->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier,
2135 attachment->normalized_subresource_range, true, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002136 }
2137}
2138
John Zulauf3d84f1b2020-03-09 13:33:25 -06002139SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2140 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2141 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2142 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2143 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2144 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2145 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2146}
2147
2148void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) {
2149 ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
2150 ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope);
2151}
2152
John Zulauf9cb530d2019-09-30 14:14:10 -06002153HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2154 HazardResult hazard;
2155 auto usage = FlagBit(usage_index);
2156 if (IsRead(usage)) {
John Zulaufc9201222020-05-13 15:13:03 -06002157 if (last_write && IsWriteHazard(usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002158 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002159 }
2160 } else {
2161 // Assume write
2162 // TODO determine what to do with READ-WRITE usage states if any
2163 // Write-After-Write check -- if we have a previous write to test against
2164 if (last_write && IsWriteHazard(usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002165 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002166 } else {
John Zulauf69133422020-05-20 14:55:53 -06002167 // Look for casus belli for WAR
John Zulauf9cb530d2019-09-30 14:14:10 -06002168 const auto usage_stage = PipelineStageBit(usage_index);
John Zulaufd14743a2020-07-03 09:42:39 -06002169 // Note: kNoAttachmentRead is ~0, and thus the no attachment read hazard check doesn't need a separate path.
2170 if (IsReadHazard(usage_stage, input_attachment_barriers)) {
John Zulauf59e25072020-07-17 10:55:21 -06002171 hazard.Set(this, usage_index, WRITE_AFTER_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002172 }
2173 if (!hazard.hazard) {
2174 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002175 const auto &read_access = last_reads[read_index];
2176 if (IsReadHazard(usage_stage, read_access)) {
John Zulauf59e25072020-07-17 10:55:21 -06002177 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002178 break;
2179 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002180 }
2181 }
2182 }
2183 }
2184 return hazard;
2185}
2186
John Zulauf69133422020-05-20 14:55:53 -06002187HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2188 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2189 HazardResult hazard;
2190 const auto usage = FlagBit(usage_index);
2191 const bool write_is_ordered = (last_write & ordering.access_scope) == last_write; // Is true if no write, and that's good.
2192 if (IsRead(usage)) {
2193 if (!write_is_ordered && IsWriteHazard(usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002194 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf69133422020-05-20 14:55:53 -06002195 }
2196 } else {
2197 if (!write_is_ordered && IsWriteHazard(usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002198 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf69133422020-05-20 14:55:53 -06002199 } else {
2200 const auto usage_stage = PipelineStageBit(usage_index);
2201 const auto unordered_reads = last_read_stages & ~ordering.exec_scope;
2202 if (unordered_reads) {
2203 // Look for any WAR hazards outside the ordered set of stages
2204 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002205 const auto &read_access = last_reads[read_index];
2206 if ((read_access.stage & unordered_reads) && IsReadHazard(usage_stage, read_access)) {
John Zulauf59e25072020-07-17 10:55:21 -06002207 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf69133422020-05-20 14:55:53 -06002208 }
2209 }
2210 }
John Zulaufd14743a2020-07-03 09:42:39 -06002211
2212 // This is special case code for the fragment shader input attachment, which unlike all other fragment shader operations
2213 // is framebuffer local, and thus subject to raster ordering guarantees
2214 if (!hazard.hazard && (input_attachment_barriers != kNoAttachmentRead)) {
2215 if (0 == (ordering.access_scope & SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT)) {
2216 // NOTE: Currently all ordering barriers include this bit, so this code may never be reached, but it's
2217 // 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 -06002218 hazard.Set(this, usage_index, WRITE_AFTER_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ,
2219 input_attachment_tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002220 }
2221 }
John Zulauf69133422020-05-20 14:55:53 -06002222 }
2223 }
2224 return hazard;
2225}
2226
John Zulauf2f952d22020-02-10 11:34:51 -07002227// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06002228HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002229 HazardResult hazard;
2230 auto usage = FlagBit(usage_index);
2231 if (IsRead(usage)) {
2232 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002233 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002234 }
2235 } else {
2236 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002237 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002238 } else if (last_read_count > 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002239 hazard.Set(this, usage_index, WRITE_RACING_READ, last_reads[0].access, last_reads[0].tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002240 } else if (input_attachment_barriers != kNoAttachmentRead) {
John Zulauf59e25072020-07-17 10:55:21 -06002241 hazard.Set(this, usage_index, WRITE_RACING_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002242 }
2243 }
2244 return hazard;
2245}
2246
John Zulauf36bcf6a2020-02-03 15:12:52 -07002247HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
2248 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002249 // Only supporting image layout transitions for now
2250 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2251 HazardResult hazard;
2252 if (last_write) {
2253 // If the previous write is *not* in the 1st access scope
2254 // *AND* the current barrier is not in the dependency chain
2255 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2256 // then the barrier access is unsafe (R/W after W)
John Zulauf36bcf6a2020-02-03 15:12:52 -07002257 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
John Zulauf0cb5be22020-01-23 12:18:22 -07002258 // TODO: Do we need a difference hazard name for this?
John Zulauf59e25072020-07-17 10:55:21 -06002259 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002260 }
John Zulauf355e49b2020-04-24 15:11:15 -06002261 }
2262 if (!hazard.hazard) {
2263 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002264 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002265 const auto &read_access = last_reads[read_index];
2266 // If the read stage is not in the src sync sync
2267 // *AND* not execution chained with an existing sync barrier (that's the or)
2268 // then the barrier access is unsafe (R/W after R)
2269 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002270 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002271 break;
2272 }
2273 }
2274 }
John Zulaufd14743a2020-07-03 09:42:39 -06002275 if (!hazard.hazard) {
2276 // Same logic as read acces above for the special case of input attachment read
2277 // Note: kNoReadAttachment is ~0 and thus cannot cause a hazard return.
2278 if ((src_exec_scope & (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | input_attachment_barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002279 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 -06002280 }
2281 }
John Zulauf0cb5be22020-01-23 12:18:22 -07002282 return hazard;
2283}
2284
John Zulauf5f13a792020-03-10 07:31:21 -06002285// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2286// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2287// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2288void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2289 if (write_tag.IsBefore(other.write_tag)) {
2290 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
2291 *this = other;
2292 } else if (!other.write_tag.IsBefore(write_tag)) {
2293 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2294 // dependency chaining logic or any stage expansion)
2295 write_barriers |= other.write_barriers;
2296
John Zulaufd14743a2020-07-03 09:42:39 -06002297 // Merge the read states
2298 if (input_attachment_barriers == kNoAttachmentRead) {
2299 // this doesn't have an input attachment read, so we'll take other, unconditionally (even if it's kNoAttachmentRead)
2300 input_attachment_barriers = other.input_attachment_barriers;
2301 input_attachment_tag = other.input_attachment_tag;
2302 } else if (other.input_attachment_barriers != kNoAttachmentRead) {
2303 // Both states have an input attachment read, pick the newest tag and merge barriers.
2304 if (input_attachment_tag.IsBefore(other.input_attachment_tag)) {
2305 input_attachment_tag = other.input_attachment_tag;
2306 }
2307 input_attachment_barriers |= other.input_attachment_barriers;
2308 }
2309 // The else clause is that only this has an attachment read and no merge is needed
2310
John Zulauf5f13a792020-03-10 07:31:21 -06002311 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2312 auto &other_read = other.last_reads[other_read_index];
2313 if (last_read_stages & other_read.stage) {
2314 // Merge in the barriers for read stages that exist in *both* this and other
2315 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
2316 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
2317 auto &my_read = last_reads[my_read_index];
2318 if (other_read.stage == my_read.stage) {
2319 if (my_read.tag.IsBefore(other_read.tag)) {
2320 my_read.tag = other_read.tag;
John Zulauf37ceaed2020-07-03 16:18:15 -06002321 my_read.access = other_read.access;
John Zulauf5f13a792020-03-10 07:31:21 -06002322 }
2323 my_read.barriers |= other_read.barriers;
2324 break;
2325 }
2326 }
2327 } else {
2328 // The other read stage doesn't exist in this, so add it.
2329 last_reads[last_read_count] = other_read;
2330 last_read_count++;
2331 last_read_stages |= other_read.stage;
2332 }
2333 }
2334 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
2335 // it.
2336}
2337
John Zulauf9cb530d2019-09-30 14:14:10 -06002338void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2339 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2340 const auto usage_bit = FlagBit(usage_index);
John Zulaufd14743a2020-07-03 09:42:39 -06002341 if (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) {
2342 // Input attachment requires special treatment for raster/load/store ordering guarantees
2343 input_attachment_barriers = 0;
2344 input_attachment_tag = tag;
2345 } else if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002346 // Mulitple outstanding reads may be of interest and do dependency chains independently
2347 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2348 const auto usage_stage = PipelineStageBit(usage_index);
2349 if (usage_stage & last_read_stages) {
2350 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2351 ReadState &access = last_reads[read_index];
2352 if (access.stage == usage_stage) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002353 access.access = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002354 access.barriers = 0;
2355 access.tag = tag;
2356 break;
2357 }
2358 }
2359 } else {
2360 // We don't have this stage in the list yet...
2361 assert(last_read_count < last_reads.size());
2362 ReadState &access = last_reads[last_read_count++];
2363 access.stage = usage_stage;
John Zulauf37ceaed2020-07-03 16:18:15 -06002364 access.access = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002365 access.barriers = 0;
2366 access.tag = tag;
2367 last_read_stages |= usage_stage;
2368 }
2369 } else {
2370 // Assume write
2371 // TODO determine what to do with READ-WRITE operations if any
John Zulaufd14743a2020-07-03 09:42:39 -06002372 // Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
John Zulauf9cb530d2019-09-30 14:14:10 -06002373 // if the last_reads/last_write were unsafe, we've reported them,
2374 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
2375 last_read_count = 0;
2376 last_read_stages = 0;
2377
John Zulaufd14743a2020-07-03 09:42:39 -06002378 input_attachment_barriers = kNoAttachmentRead; // Denotes no outstanding input attachment read after the last write.
2379 // NOTE: we don't reset the tag, as the equality check ignores it when kNoAttachmentRead is set.
2380
John Zulauf9cb530d2019-09-30 14:14:10 -06002381 write_barriers = 0;
2382 write_dependency_chain = 0;
2383 write_tag = tag;
2384 last_write = usage_bit;
2385 }
2386}
John Zulauf5f13a792020-03-10 07:31:21 -06002387
John Zulauf9cb530d2019-09-30 14:14:10 -06002388void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
2389 // Execution Barriers only protect read operations
2390 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2391 ReadState &access = last_reads[read_index];
2392 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2393 if (srcStageMask & (access.stage | access.barriers)) {
2394 access.barriers |= dstStageMask;
2395 }
2396 }
John Zulaufd14743a2020-07-03 09:42:39 -06002397 if (srcStageMask & (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | input_attachment_barriers)) {
2398 input_attachment_barriers |= dstStageMask;
2399 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002400 if (write_dependency_chain & srcStageMask) write_dependency_chain |= dstStageMask;
2401}
2402
John Zulauf36bcf6a2020-02-03 15:12:52 -07002403void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
2404 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002405 // Assuming we've applied the execution side of this barrier, we update just the write
2406 // The || implements the "dependency chain" logic for this barrier
John Zulauf36bcf6a2020-02-03 15:12:52 -07002407 if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) {
2408 write_barriers |= dst_access_scope;
2409 write_dependency_chain |= dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06002410 }
2411}
2412
John Zulauf59e25072020-07-17 10:55:21 -06002413// This should be just Bits or Index, but we don't have an invalid state for Index
2414VkPipelineStageFlags ResourceAccessState::GetReadBarriers(SyncStageAccessFlags usage_bit) const {
2415 VkPipelineStageFlags barriers = 0U;
2416 if (usage_bit & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) {
2417 barriers = input_attachment_barriers;
2418 } else {
2419 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2420 const auto &read_access = last_reads[read_index];
2421 if (read_access.access & usage_bit) {
2422 barriers = read_access.barriers;
2423 break;
2424 }
2425 }
2426 }
2427 return barriers;
2428}
2429
John Zulaufd1f85d42020-04-15 12:23:15 -06002430void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002431 auto *access_context = GetAccessContextNoInsert(command_buffer);
2432 if (access_context) {
2433 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002434 }
2435}
2436
John Zulaufd1f85d42020-04-15 12:23:15 -06002437void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2438 auto access_found = cb_access_state.find(command_buffer);
2439 if (access_found != cb_access_state.end()) {
2440 access_found->second->Reset();
2441 cb_access_state.erase(access_found);
2442 }
2443}
2444
John Zulauf540266b2020-04-06 18:54:53 -06002445void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002446 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
2447 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002448 const VkMemoryBarrier *pMemoryBarriers) {
2449 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07002450 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002451 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002452 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002453}
2454
John Zulauf540266b2020-04-06 18:54:53 -06002455void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002456 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2457 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002458 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002459 for (uint32_t index = 0; index < barrier_count; index++) {
locke-lunarg3c038002020-04-30 23:08:08 -06002460 auto barrier = barriers[index];
John Zulauf9cb530d2019-09-30 14:14:10 -06002461 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2462 if (!buffer) continue;
locke-lunarg3c038002020-04-30 23:08:08 -06002463 barrier.size = GetRealWholeSize(barrier.offset, barrier.size, buffer->createInfo.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002464 ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002465 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2466 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2467 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2468 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002469 }
2470}
2471
John Zulauf540266b2020-04-06 18:54:53 -06002472void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2473 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2474 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002475 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002476 for (uint32_t index = 0; index < barrier_count; index++) {
2477 const auto &barrier = barriers[index];
2478 const auto *image = Get<IMAGE_STATE>(barrier.image);
2479 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002480 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002481 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2482 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2483 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2484 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
2485 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002486 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002487}
2488
2489bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2490 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2491 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002492 const auto *cb_context = GetAccessContext(commandBuffer);
2493 assert(cb_context);
2494 if (!cb_context) return skip;
2495 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002496
John Zulauf3d84f1b2020-03-09 13:33:25 -06002497 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002498 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002499 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002500
2501 for (uint32_t region = 0; region < regionCount; region++) {
2502 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002503 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002504 ResourceAccessRange src_range = MakeRange(
2505 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002506 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002507 if (hazard.hazard) {
2508 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002509 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002510 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002511 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002512 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002513 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002514 }
John Zulauf16adfc92020-04-08 10:28:33 -06002515 if (dst_buffer && !skip) {
locke-lunargff255f92020-05-13 18:53:52 -06002516 ResourceAccessRange dst_range = MakeRange(
2517 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf355e49b2020-04-24 15:11:15 -06002518 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002519 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002520 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002521 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002522 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002523 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002524 }
2525 }
2526 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002527 }
2528 return skip;
2529}
2530
2531void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2532 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002533 auto *cb_context = GetAccessContext(commandBuffer);
2534 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002535 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002536 auto *context = cb_context->GetCurrentAccessContext();
2537
John Zulauf9cb530d2019-09-30 14:14:10 -06002538 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002539 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002540
2541 for (uint32_t region = 0; region < regionCount; region++) {
2542 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002543 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002544 ResourceAccessRange src_range = MakeRange(
2545 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002546 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002547 }
John Zulauf16adfc92020-04-08 10:28:33 -06002548 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002549 ResourceAccessRange dst_range = MakeRange(
2550 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002551 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002552 }
2553 }
2554}
2555
2556bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2557 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2558 const VkImageCopy *pRegions) const {
2559 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002560 const auto *cb_access_context = GetAccessContext(commandBuffer);
2561 assert(cb_access_context);
2562 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002563
John Zulauf3d84f1b2020-03-09 13:33:25 -06002564 const auto *context = cb_access_context->GetCurrentAccessContext();
2565 assert(context);
2566 if (!context) return skip;
2567
2568 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2569 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002570 for (uint32_t region = 0; region < regionCount; region++) {
2571 const auto &copy_region = pRegions[region];
2572 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002573 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002574 copy_region.srcOffset, copy_region.extent);
2575 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002576 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002577 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002578 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002579 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002580 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002581 }
2582
2583 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002584 VkExtent3D dst_copy_extent =
2585 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002586 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002587 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002588 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002589 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002590 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002591 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002592 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002593 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002594 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002595 }
2596 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002597
John Zulauf5c5e88d2019-12-26 11:22:02 -07002598 return skip;
2599}
2600
2601void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2602 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2603 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002604 auto *cb_access_context = GetAccessContext(commandBuffer);
2605 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002606 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002607 auto *context = cb_access_context->GetCurrentAccessContext();
2608 assert(context);
2609
John Zulauf5c5e88d2019-12-26 11:22:02 -07002610 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002611 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002612
2613 for (uint32_t region = 0; region < regionCount; region++) {
2614 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002615 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002616 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2617 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002618 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002619 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002620 VkExtent3D dst_copy_extent =
2621 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002622 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2623 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002624 }
2625 }
2626}
2627
2628bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2629 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2630 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2631 uint32_t bufferMemoryBarrierCount,
2632 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2633 uint32_t imageMemoryBarrierCount,
2634 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2635 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002636 const auto *cb_access_context = GetAccessContext(commandBuffer);
2637 assert(cb_access_context);
2638 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002639
John Zulauf3d84f1b2020-03-09 13:33:25 -06002640 const auto *context = cb_access_context->GetCurrentAccessContext();
2641 assert(context);
2642 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002643
John Zulauf3d84f1b2020-03-09 13:33:25 -06002644 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002645 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2646 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002647 // Validate Image Layout transitions
2648 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2649 const auto &barrier = pImageMemoryBarriers[index];
2650 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2651 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2652 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002653 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002654 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002655 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002656 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002657 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002658 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002659 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002660 }
2661 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002662
2663 return skip;
2664}
2665
2666void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2667 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2668 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2669 uint32_t bufferMemoryBarrierCount,
2670 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2671 uint32_t imageMemoryBarrierCount,
2672 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002673 auto *cb_access_context = GetAccessContext(commandBuffer);
2674 assert(cb_access_context);
2675 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002676 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002677 auto access_context = cb_access_context->GetCurrentAccessContext();
2678 assert(access_context);
2679 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06002680
John Zulauf3d84f1b2020-03-09 13:33:25 -06002681 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002682 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002683 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002684 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
2685 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2686 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002687 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
2688 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002689 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06002690 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002691
2692 // 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 -06002693 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07002694 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06002695}
2696
2697void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
2698 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
2699 // The state tracker sets up the device state
2700 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
2701
John Zulauf5f13a792020-03-10 07:31:21 -06002702 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
2703 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06002704 // TODO: Find a good way to do this hooklessly.
2705 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
2706 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
2707 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
2708
John Zulaufd1f85d42020-04-15 12:23:15 -06002709 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2710 sync_device_state->ResetCommandBufferCallback(command_buffer);
2711 });
2712 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2713 sync_device_state->FreeCommandBufferCallback(command_buffer);
2714 });
John Zulauf9cb530d2019-09-30 14:14:10 -06002715}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002716
John Zulauf355e49b2020-04-24 15:11:15 -06002717bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2718 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
2719 bool skip = false;
2720 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
2721 auto cb_context = GetAccessContext(commandBuffer);
2722
2723 if (rp_state && cb_context) {
2724 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
2725 }
2726
2727 return skip;
2728}
2729
2730bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2731 VkSubpassContents contents) const {
2732 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2733 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2734 subpass_begin_info.contents = contents;
2735 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
2736 return skip;
2737}
2738
2739bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2740 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2741 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2742 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
2743 return skip;
2744}
2745
2746bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2747 const VkRenderPassBeginInfo *pRenderPassBegin,
2748 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2749 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2750 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
2751 return skip;
2752}
2753
John Zulauf3d84f1b2020-03-09 13:33:25 -06002754void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
2755 VkResult result) {
2756 // The state tracker sets up the command buffer state
2757 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
2758
2759 // Create/initialize the structure that trackers accesses at the command buffer scope.
2760 auto cb_access_context = GetAccessContext(commandBuffer);
2761 assert(cb_access_context);
2762 cb_access_context->Reset();
2763}
2764
2765void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06002766 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002767 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002768 if (cb_context) {
2769 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002770 }
2771}
2772
2773void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2774 VkSubpassContents contents) {
2775 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2776 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2777 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002778 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002779}
2780
2781void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2782 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2783 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002784 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002785}
2786
2787void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2788 const VkRenderPassBeginInfo *pRenderPassBegin,
2789 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2790 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002791 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
2792}
2793
2794bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2795 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
2796 bool skip = false;
2797
2798 auto cb_context = GetAccessContext(commandBuffer);
2799 assert(cb_context);
2800 auto cb_state = cb_context->GetCommandBufferState();
2801 if (!cb_state) return skip;
2802
2803 auto rp_state = cb_state->activeRenderPass;
2804 if (!rp_state) return skip;
2805
2806 skip |= cb_context->ValidateNextSubpass(func_name);
2807
2808 return skip;
2809}
2810
2811bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
2812 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
2813 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2814 subpass_begin_info.contents = contents;
2815 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
2816 return skip;
2817}
2818
2819bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2820 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2821 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2822 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
2823 return skip;
2824}
2825
2826bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2827 const VkSubpassEndInfo *pSubpassEndInfo) const {
2828 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2829 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
2830 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002831}
2832
2833void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06002834 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002835 auto cb_context = GetAccessContext(commandBuffer);
2836 assert(cb_context);
2837 auto cb_state = cb_context->GetCommandBufferState();
2838 if (!cb_state) return;
2839
2840 auto rp_state = cb_state->activeRenderPass;
2841 if (!rp_state) return;
2842
John Zulauf355e49b2020-04-24 15:11:15 -06002843 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002844}
2845
2846void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
2847 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
2848 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2849 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002850 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002851}
2852
2853void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2854 const VkSubpassEndInfo *pSubpassEndInfo) {
2855 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002856 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002857}
2858
2859void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2860 const VkSubpassEndInfo *pSubpassEndInfo) {
2861 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002862 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002863}
2864
John Zulauf355e49b2020-04-24 15:11:15 -06002865bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
2866 const char *func_name) const {
2867 bool skip = false;
2868
2869 auto cb_context = GetAccessContext(commandBuffer);
2870 assert(cb_context);
2871 auto cb_state = cb_context->GetCommandBufferState();
2872 if (!cb_state) return skip;
2873
2874 auto rp_state = cb_state->activeRenderPass;
2875 if (!rp_state) return skip;
2876
2877 skip |= cb_context->ValidateEndRenderpass(func_name);
2878 return skip;
2879}
2880
2881bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2882 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
2883 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
2884 return skip;
2885}
2886
2887bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
2888 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2889 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2890 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
2891 return skip;
2892}
2893
2894bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
2895 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2896 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2897 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
2898 return skip;
2899}
2900
2901void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
2902 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06002903 // Resolve the all subpass contexts to the command buffer contexts
2904 auto cb_context = GetAccessContext(commandBuffer);
2905 assert(cb_context);
2906 auto cb_state = cb_context->GetCommandBufferState();
2907 if (!cb_state) return;
2908
locke-lunargaecf2152020-05-12 17:15:41 -06002909 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06002910 if (!rp_state) return;
2911
John Zulauf355e49b2020-04-24 15:11:15 -06002912 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06002913}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002914
2915void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002916 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06002917 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002918}
2919
2920void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06002921 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06002922 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002923}
2924
2925void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06002926 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06002927 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002928}
locke-lunarga19c71d2020-03-02 18:17:04 -07002929
2930bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2931 VkImageLayout dstImageLayout, uint32_t regionCount,
2932 const VkBufferImageCopy *pRegions) const {
2933 bool skip = false;
2934 const auto *cb_access_context = GetAccessContext(commandBuffer);
2935 assert(cb_access_context);
2936 if (!cb_access_context) return skip;
2937
2938 const auto *context = cb_access_context->GetCurrentAccessContext();
2939 assert(context);
2940 if (!context) return skip;
2941
2942 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07002943 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2944
2945 for (uint32_t region = 0; region < regionCount; region++) {
2946 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002947 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002948 ResourceAccessRange src_range =
2949 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002950 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002951 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002952 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002953 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002954 "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002955 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002956 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002957 }
2958 }
2959 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002960 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002961 copy_region.imageOffset, copy_region.imageExtent);
2962 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002963 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002964 "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002965 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002966 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002967 }
2968 if (skip) break;
2969 }
2970 if (skip) break;
2971 }
2972 return skip;
2973}
2974
2975void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2976 VkImageLayout dstImageLayout, uint32_t regionCount,
2977 const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06002978 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07002979 auto *cb_access_context = GetAccessContext(commandBuffer);
2980 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002981 const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07002982 auto *context = cb_access_context->GetCurrentAccessContext();
2983 assert(context);
2984
2985 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06002986 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002987
2988 for (uint32_t region = 0; region < regionCount; region++) {
2989 const auto &copy_region = pRegions[region];
2990 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002991 ResourceAccessRange src_range =
2992 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002993 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002994 }
2995 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002996 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002997 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002998 }
2999 }
3000}
3001
3002bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3003 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3004 const VkBufferImageCopy *pRegions) const {
3005 bool skip = false;
3006 const auto *cb_access_context = GetAccessContext(commandBuffer);
3007 assert(cb_access_context);
3008 if (!cb_access_context) return skip;
3009
3010 const auto *context = cb_access_context->GetCurrentAccessContext();
3011 assert(context);
3012 if (!context) return skip;
3013
3014 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3015 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3016 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3017 for (uint32_t region = 0; region < regionCount; region++) {
3018 const auto &copy_region = pRegions[region];
3019 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003020 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003021 copy_region.imageOffset, copy_region.imageExtent);
3022 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003023 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003024 "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003025 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003026 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003027 }
3028 }
3029 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06003030 ResourceAccessRange dst_range =
3031 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003032 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003033 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003034 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003035 "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003036 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003037 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003038 }
3039 }
3040 if (skip) break;
3041 }
3042 return skip;
3043}
3044
3045void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3046 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003047 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07003048 auto *cb_access_context = GetAccessContext(commandBuffer);
3049 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003050 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER);
locke-lunarga19c71d2020-03-02 18:17:04 -07003051 auto *context = cb_access_context->GetCurrentAccessContext();
3052 assert(context);
3053
3054 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003055 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3056 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 -06003057 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003058
3059 for (uint32_t region = 0; region < regionCount; region++) {
3060 const auto &copy_region = pRegions[region];
3061 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003062 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003063 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003064 }
3065 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003066 ResourceAccessRange dst_range =
3067 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003068 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003069 }
3070 }
3071}
3072
3073bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3074 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3075 const VkImageBlit *pRegions, VkFilter filter) const {
3076 bool skip = false;
3077 const auto *cb_access_context = GetAccessContext(commandBuffer);
3078 assert(cb_access_context);
3079 if (!cb_access_context) return skip;
3080
3081 const auto *context = cb_access_context->GetCurrentAccessContext();
3082 assert(context);
3083 if (!context) return skip;
3084
3085 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3086 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3087
3088 for (uint32_t region = 0; region < regionCount; region++) {
3089 const auto &blit_region = pRegions[region];
3090 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003091 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3092 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3093 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3094 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3095 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3096 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3097 auto hazard =
3098 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003099 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003100 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003101 "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003102 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003103 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003104 }
3105 }
3106
3107 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003108 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3109 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3110 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3111 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3112 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3113 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3114 auto hazard =
3115 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003116 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003117 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003118 "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003119 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003120 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003121 }
3122 if (skip) break;
3123 }
3124 }
3125
3126 return skip;
3127}
3128
3129void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3130 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3131 const VkImageBlit *pRegions, VkFilter filter) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003132 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3133 pRegions, filter);
locke-lunarga19c71d2020-03-02 18:17:04 -07003134 auto *cb_access_context = GetAccessContext(commandBuffer);
3135 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003136 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07003137 auto *context = cb_access_context->GetCurrentAccessContext();
3138 assert(context);
3139
3140 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003141 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003142
3143 for (uint32_t region = 0; region < regionCount; region++) {
3144 const auto &blit_region = pRegions[region];
3145 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003146 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3147 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3148 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3149 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3150 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3151 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3152 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003153 }
3154 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003155 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3156 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3157 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3158 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3159 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3160 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3161 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003162 }
3163 }
3164}
locke-lunarg36ba2592020-04-03 09:42:04 -06003165
locke-lunarg61870c22020-06-09 14:51:50 -06003166bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
3167 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
3168 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003169 bool skip = false;
3170 if (drawCount == 0) return skip;
3171
3172 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3173 VkDeviceSize size = struct_size;
3174 if (drawCount == 1 || stride == size) {
3175 if (drawCount > 1) size *= drawCount;
3176 ResourceAccessRange range = MakeRange(offset, size);
3177 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3178 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003179 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003180 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003181 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003182 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003183 }
3184 } else {
3185 for (uint32_t i = 0; i < drawCount; ++i) {
3186 ResourceAccessRange range = MakeRange(offset + i * stride, size);
3187 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3188 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003189 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003190 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3191 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3192 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003193 break;
3194 }
3195 }
3196 }
3197 return skip;
3198}
3199
locke-lunarg61870c22020-06-09 14:51:50 -06003200void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3201 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3202 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003203 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3204 VkDeviceSize size = struct_size;
3205 if (drawCount == 1 || stride == size) {
3206 if (drawCount > 1) size *= drawCount;
3207 ResourceAccessRange range = MakeRange(offset, size);
3208 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3209 } else {
3210 for (uint32_t i = 0; i < drawCount; ++i) {
3211 ResourceAccessRange range = MakeRange(offset + i * stride, size);
3212 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3213 }
3214 }
3215}
3216
locke-lunarg61870c22020-06-09 14:51:50 -06003217bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3218 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003219 bool skip = false;
3220
3221 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3222 ResourceAccessRange range = MakeRange(offset, 4);
3223 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3224 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003225 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003226 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003227 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003228 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003229 }
3230 return skip;
3231}
3232
locke-lunarg61870c22020-06-09 14:51:50 -06003233void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003234 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3235 ResourceAccessRange range = MakeRange(offset, 4);
3236 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3237}
3238
locke-lunarg36ba2592020-04-03 09:42:04 -06003239bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003240 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003241 const auto *cb_access_context = GetAccessContext(commandBuffer);
3242 assert(cb_access_context);
3243 if (!cb_access_context) return skip;
3244
locke-lunarg61870c22020-06-09 14:51:50 -06003245 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003246 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003247}
3248
3249void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003250 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003251 auto *cb_access_context = GetAccessContext(commandBuffer);
3252 assert(cb_access_context);
3253 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003254
locke-lunarg61870c22020-06-09 14:51:50 -06003255 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003256}
locke-lunarge1a67022020-04-29 00:15:36 -06003257
3258bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003259 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003260 const auto *cb_access_context = GetAccessContext(commandBuffer);
3261 assert(cb_access_context);
3262 if (!cb_access_context) return skip;
3263
3264 const auto *context = cb_access_context->GetCurrentAccessContext();
3265 assert(context);
3266 if (!context) return skip;
3267
locke-lunarg61870c22020-06-09 14:51:50 -06003268 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3269 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3270 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003271 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003272}
3273
3274void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003275 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003276 auto *cb_access_context = GetAccessContext(commandBuffer);
3277 assert(cb_access_context);
3278 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3279 auto *context = cb_access_context->GetCurrentAccessContext();
3280 assert(context);
3281
locke-lunarg61870c22020-06-09 14:51:50 -06003282 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3283 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003284}
3285
3286bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3287 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003288 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003289 const auto *cb_access_context = GetAccessContext(commandBuffer);
3290 assert(cb_access_context);
3291 if (!cb_access_context) return skip;
3292
locke-lunarg61870c22020-06-09 14:51:50 -06003293 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3294 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3295 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003296 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003297}
3298
3299void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3300 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003301 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003302 auto *cb_access_context = GetAccessContext(commandBuffer);
3303 assert(cb_access_context);
3304 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003305
locke-lunarg61870c22020-06-09 14:51:50 -06003306 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3307 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3308 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003309}
3310
3311bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3312 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003313 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003314 const auto *cb_access_context = GetAccessContext(commandBuffer);
3315 assert(cb_access_context);
3316 if (!cb_access_context) return skip;
3317
locke-lunarg61870c22020-06-09 14:51:50 -06003318 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3319 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3320 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003321 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003322}
3323
3324void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3325 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003326 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003327 auto *cb_access_context = GetAccessContext(commandBuffer);
3328 assert(cb_access_context);
3329 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003330
locke-lunarg61870c22020-06-09 14:51:50 -06003331 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3332 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3333 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003334}
3335
3336bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3337 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003338 bool skip = false;
3339 if (drawCount == 0) return skip;
3340
locke-lunargff255f92020-05-13 18:53:52 -06003341 const auto *cb_access_context = GetAccessContext(commandBuffer);
3342 assert(cb_access_context);
3343 if (!cb_access_context) return skip;
3344
3345 const auto *context = cb_access_context->GetCurrentAccessContext();
3346 assert(context);
3347 if (!context) return skip;
3348
locke-lunarg61870c22020-06-09 14:51:50 -06003349 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3350 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3351 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3352 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003353
3354 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3355 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3356 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003357 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003358 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003359}
3360
3361void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3362 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003363 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003364 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003365 auto *cb_access_context = GetAccessContext(commandBuffer);
3366 assert(cb_access_context);
3367 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3368 auto *context = cb_access_context->GetCurrentAccessContext();
3369 assert(context);
3370
locke-lunarg61870c22020-06-09 14:51:50 -06003371 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3372 cb_access_context->RecordDrawSubpassAttachment(tag);
3373 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003374
3375 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3376 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3377 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003378 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003379}
3380
3381bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3382 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003383 bool skip = false;
3384 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003385 const auto *cb_access_context = GetAccessContext(commandBuffer);
3386 assert(cb_access_context);
3387 if (!cb_access_context) return skip;
3388
3389 const auto *context = cb_access_context->GetCurrentAccessContext();
3390 assert(context);
3391 if (!context) return skip;
3392
locke-lunarg61870c22020-06-09 14:51:50 -06003393 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3394 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3395 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3396 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003397
3398 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3399 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3400 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003401 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003402 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003403}
3404
3405void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3406 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003407 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003408 auto *cb_access_context = GetAccessContext(commandBuffer);
3409 assert(cb_access_context);
3410 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3411 auto *context = cb_access_context->GetCurrentAccessContext();
3412 assert(context);
3413
locke-lunarg61870c22020-06-09 14:51:50 -06003414 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3415 cb_access_context->RecordDrawSubpassAttachment(tag);
3416 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003417
3418 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3419 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3420 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003421 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003422}
3423
3424bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3425 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3426 uint32_t stride, const char *function) const {
3427 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003428 const auto *cb_access_context = GetAccessContext(commandBuffer);
3429 assert(cb_access_context);
3430 if (!cb_access_context) return skip;
3431
3432 const auto *context = cb_access_context->GetCurrentAccessContext();
3433 assert(context);
3434 if (!context) return skip;
3435
locke-lunarg61870c22020-06-09 14:51:50 -06003436 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3437 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3438 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3439 function);
3440 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003441
3442 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3443 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3444 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003445 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003446 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003447}
3448
3449bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3450 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3451 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003452 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3453 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003454}
3455
3456void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3457 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3458 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003459 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3460 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003461 auto *cb_access_context = GetAccessContext(commandBuffer);
3462 assert(cb_access_context);
3463 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3464 auto *context = cb_access_context->GetCurrentAccessContext();
3465 assert(context);
3466
locke-lunarg61870c22020-06-09 14:51:50 -06003467 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3468 cb_access_context->RecordDrawSubpassAttachment(tag);
3469 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3470 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003471
3472 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3473 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3474 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003475 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003476}
3477
3478bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3479 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3480 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003481 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3482 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003483}
3484
3485void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3486 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3487 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003488 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3489 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003490 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003491}
3492
3493bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3494 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3495 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003496 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3497 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003498}
3499
3500void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3501 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3502 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003503 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3504 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003505 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3506}
3507
3508bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3509 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3510 uint32_t stride, const char *function) const {
3511 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003512 const auto *cb_access_context = GetAccessContext(commandBuffer);
3513 assert(cb_access_context);
3514 if (!cb_access_context) return skip;
3515
3516 const auto *context = cb_access_context->GetCurrentAccessContext();
3517 assert(context);
3518 if (!context) return skip;
3519
locke-lunarg61870c22020-06-09 14:51:50 -06003520 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3521 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3522 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3523 stride, function);
3524 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003525
3526 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3527 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3528 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003529 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003530 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003531}
3532
3533bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3534 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3535 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003536 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3537 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003538}
3539
3540void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3541 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3542 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003543 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3544 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003545 auto *cb_access_context = GetAccessContext(commandBuffer);
3546 assert(cb_access_context);
3547 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3548 auto *context = cb_access_context->GetCurrentAccessContext();
3549 assert(context);
3550
locke-lunarg61870c22020-06-09 14:51:50 -06003551 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3552 cb_access_context->RecordDrawSubpassAttachment(tag);
3553 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3554 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003555
3556 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3557 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06003558 // We will update the index and vertex buffer in SubmitQueue in the future.
3559 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003560}
3561
3562bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3563 VkDeviceSize offset, VkBuffer countBuffer,
3564 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3565 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003566 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3567 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003568}
3569
3570void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3571 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3572 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003573 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3574 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003575 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3576}
3577
3578bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3579 VkDeviceSize offset, VkBuffer countBuffer,
3580 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3581 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003582 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3583 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003584}
3585
3586void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3587 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3588 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003589 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3590 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003591 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3592}
3593
3594bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3595 const VkClearColorValue *pColor, uint32_t rangeCount,
3596 const VkImageSubresourceRange *pRanges) const {
3597 bool skip = false;
3598 const auto *cb_access_context = GetAccessContext(commandBuffer);
3599 assert(cb_access_context);
3600 if (!cb_access_context) return skip;
3601
3602 const auto *context = cb_access_context->GetCurrentAccessContext();
3603 assert(context);
3604 if (!context) return skip;
3605
3606 const auto *image_state = Get<IMAGE_STATE>(image);
3607
3608 for (uint32_t index = 0; index < rangeCount; index++) {
3609 const auto &range = pRanges[index];
3610 if (image_state) {
3611 auto hazard =
3612 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3613 if (hazard.hazard) {
3614 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003615 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003616 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06003617 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003618 }
3619 }
3620 }
3621 return skip;
3622}
3623
3624void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3625 const VkClearColorValue *pColor, uint32_t rangeCount,
3626 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003627 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003628 auto *cb_access_context = GetAccessContext(commandBuffer);
3629 assert(cb_access_context);
3630 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
3631 auto *context = cb_access_context->GetCurrentAccessContext();
3632 assert(context);
3633
3634 const auto *image_state = Get<IMAGE_STATE>(image);
3635
3636 for (uint32_t index = 0; index < rangeCount; index++) {
3637 const auto &range = pRanges[index];
3638 if (image_state) {
3639 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3640 tag);
3641 }
3642 }
3643}
3644
3645bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3646 VkImageLayout imageLayout,
3647 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3648 const VkImageSubresourceRange *pRanges) const {
3649 bool skip = false;
3650 const auto *cb_access_context = GetAccessContext(commandBuffer);
3651 assert(cb_access_context);
3652 if (!cb_access_context) return skip;
3653
3654 const auto *context = cb_access_context->GetCurrentAccessContext();
3655 assert(context);
3656 if (!context) return skip;
3657
3658 const auto *image_state = Get<IMAGE_STATE>(image);
3659
3660 for (uint32_t index = 0; index < rangeCount; index++) {
3661 const auto &range = pRanges[index];
3662 if (image_state) {
3663 auto hazard =
3664 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3665 if (hazard.hazard) {
3666 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003667 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003668 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06003669 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003670 }
3671 }
3672 }
3673 return skip;
3674}
3675
3676void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3677 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3678 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003679 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003680 auto *cb_access_context = GetAccessContext(commandBuffer);
3681 assert(cb_access_context);
3682 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
3683 auto *context = cb_access_context->GetCurrentAccessContext();
3684 assert(context);
3685
3686 const auto *image_state = Get<IMAGE_STATE>(image);
3687
3688 for (uint32_t index = 0; index < rangeCount; index++) {
3689 const auto &range = pRanges[index];
3690 if (image_state) {
3691 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3692 tag);
3693 }
3694 }
3695}
3696
3697bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
3698 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
3699 VkDeviceSize dstOffset, VkDeviceSize stride,
3700 VkQueryResultFlags flags) const {
3701 bool skip = false;
3702 const auto *cb_access_context = GetAccessContext(commandBuffer);
3703 assert(cb_access_context);
3704 if (!cb_access_context) return skip;
3705
3706 const auto *context = cb_access_context->GetCurrentAccessContext();
3707 assert(context);
3708 if (!context) return skip;
3709
3710 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3711
3712 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003713 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003714 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3715 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06003716 skip |=
3717 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3718 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
3719 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003720 }
3721 }
locke-lunargff255f92020-05-13 18:53:52 -06003722
3723 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003724 return skip;
3725}
3726
3727void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
3728 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3729 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003730 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
3731 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06003732 auto *cb_access_context = GetAccessContext(commandBuffer);
3733 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06003734 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06003735 auto *context = cb_access_context->GetCurrentAccessContext();
3736 assert(context);
3737
3738 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3739
3740 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003741 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003742 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3743 }
locke-lunargff255f92020-05-13 18:53:52 -06003744
3745 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003746}
3747
3748bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3749 VkDeviceSize size, uint32_t data) const {
3750 bool skip = false;
3751 const auto *cb_access_context = GetAccessContext(commandBuffer);
3752 assert(cb_access_context);
3753 if (!cb_access_context) return skip;
3754
3755 const auto *context = cb_access_context->GetCurrentAccessContext();
3756 assert(context);
3757 if (!context) return skip;
3758
3759 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3760
3761 if (dst_buffer) {
3762 ResourceAccessRange range = MakeRange(dstOffset, size);
3763 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3764 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003765 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003766 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06003767 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003768 }
3769 }
3770 return skip;
3771}
3772
3773void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3774 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003775 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06003776 auto *cb_access_context = GetAccessContext(commandBuffer);
3777 assert(cb_access_context);
3778 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
3779 auto *context = cb_access_context->GetCurrentAccessContext();
3780 assert(context);
3781
3782 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3783
3784 if (dst_buffer) {
3785 ResourceAccessRange range = MakeRange(dstOffset, size);
3786 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3787 }
3788}
3789
3790bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3791 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3792 const VkImageResolve *pRegions) const {
3793 bool skip = false;
3794 const auto *cb_access_context = GetAccessContext(commandBuffer);
3795 assert(cb_access_context);
3796 if (!cb_access_context) return skip;
3797
3798 const auto *context = cb_access_context->GetCurrentAccessContext();
3799 assert(context);
3800 if (!context) return skip;
3801
3802 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3803 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3804
3805 for (uint32_t region = 0; region < regionCount; region++) {
3806 const auto &resolve_region = pRegions[region];
3807 if (src_image) {
3808 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3809 resolve_region.srcOffset, resolve_region.extent);
3810 if (hazard.hazard) {
3811 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003812 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003813 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003814 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003815 }
3816 }
3817
3818 if (dst_image) {
3819 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3820 resolve_region.dstOffset, resolve_region.extent);
3821 if (hazard.hazard) {
3822 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003823 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003824 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003825 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003826 }
3827 if (skip) break;
3828 }
3829 }
3830
3831 return skip;
3832}
3833
3834void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3835 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3836 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003837 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3838 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06003839 auto *cb_access_context = GetAccessContext(commandBuffer);
3840 assert(cb_access_context);
3841 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
3842 auto *context = cb_access_context->GetCurrentAccessContext();
3843 assert(context);
3844
3845 auto *src_image = Get<IMAGE_STATE>(srcImage);
3846 auto *dst_image = Get<IMAGE_STATE>(dstImage);
3847
3848 for (uint32_t region = 0; region < regionCount; region++) {
3849 const auto &resolve_region = pRegions[region];
3850 if (src_image) {
3851 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3852 resolve_region.srcOffset, resolve_region.extent, tag);
3853 }
3854 if (dst_image) {
3855 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3856 resolve_region.dstOffset, resolve_region.extent, tag);
3857 }
3858 }
3859}
3860
3861bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3862 VkDeviceSize dataSize, const void *pData) const {
3863 bool skip = false;
3864 const auto *cb_access_context = GetAccessContext(commandBuffer);
3865 assert(cb_access_context);
3866 if (!cb_access_context) return skip;
3867
3868 const auto *context = cb_access_context->GetCurrentAccessContext();
3869 assert(context);
3870 if (!context) return skip;
3871
3872 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3873
3874 if (dst_buffer) {
3875 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3876 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3877 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003878 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003879 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06003880 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003881 }
3882 }
3883 return skip;
3884}
3885
3886void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3887 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003888 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06003889 auto *cb_access_context = GetAccessContext(commandBuffer);
3890 assert(cb_access_context);
3891 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
3892 auto *context = cb_access_context->GetCurrentAccessContext();
3893 assert(context);
3894
3895 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3896
3897 if (dst_buffer) {
3898 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3899 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3900 }
3901}
locke-lunargff255f92020-05-13 18:53:52 -06003902
3903bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3904 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
3905 bool skip = false;
3906 const auto *cb_access_context = GetAccessContext(commandBuffer);
3907 assert(cb_access_context);
3908 if (!cb_access_context) return skip;
3909
3910 const auto *context = cb_access_context->GetCurrentAccessContext();
3911 assert(context);
3912 if (!context) return skip;
3913
3914 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3915
3916 if (dst_buffer) {
3917 ResourceAccessRange range = MakeRange(dstOffset, 4);
3918 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3919 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06003920 skip |=
3921 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3922 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
3923 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003924 }
3925 }
3926 return skip;
3927}
3928
3929void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3930 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003931 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06003932 auto *cb_access_context = GetAccessContext(commandBuffer);
3933 assert(cb_access_context);
3934 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
3935 auto *context = cb_access_context->GetCurrentAccessContext();
3936 assert(context);
3937
3938 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3939
3940 if (dst_buffer) {
3941 ResourceAccessRange range = MakeRange(dstOffset, 4);
3942 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3943 }
3944}