blob: f696ad656be341e1d0b95b581d78e260bcd6a4e2 [file] [log] [blame]
locke-lunarg8ec19162020-06-16 18:48:34 -06001/* Copyright (c) 2019-2020 The Khronos Group Inc.
2 * Copyright (c) 2019-2020 Valve Corporation
3 * Copyright (c) 2019-2020 LunarG, Inc.
John Zulauf9cb530d2019-09-30 14:14:10 -06004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Author: John Zulauf <jzulauf@lunarg.com>
18 */
19
20#include <limits>
21#include <vector>
locke-lunarg296a3c92020-03-25 01:04:29 -060022#include <memory>
23#include <bitset>
John Zulauf9cb530d2019-09-30 14:14:10 -060024#include "synchronization_validation.h"
25
26static const char *string_SyncHazardVUID(SyncHazard hazard) {
27 switch (hazard) {
28 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070029 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060030 break;
31 case SyncHazard::READ_AFTER_WRITE:
32 return "SYNC-HAZARD-READ_AFTER_WRITE";
33 break;
34 case SyncHazard::WRITE_AFTER_READ:
35 return "SYNC-HAZARD-WRITE_AFTER_READ";
36 break;
37 case SyncHazard::WRITE_AFTER_WRITE:
38 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
39 break;
John Zulauf2f952d22020-02-10 11:34:51 -070040 case SyncHazard::READ_RACING_WRITE:
41 return "SYNC-HAZARD-READ-RACING-WRITE";
42 break;
43 case SyncHazard::WRITE_RACING_WRITE:
44 return "SYNC-HAZARD-WRITE-RACING-WRITE";
45 break;
46 case SyncHazard::WRITE_RACING_READ:
47 return "SYNC-HAZARD-WRITE-RACING-READ";
48 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060049 default:
50 assert(0);
51 }
52 return "SYNC-HAZARD-INVALID";
53}
54
John Zulauf59e25072020-07-17 10:55:21 -060055static bool IsHazardVsRead(SyncHazard hazard) {
56 switch (hazard) {
57 case SyncHazard::NONE:
58 return false;
59 break;
60 case SyncHazard::READ_AFTER_WRITE:
61 return false;
62 break;
63 case SyncHazard::WRITE_AFTER_READ:
64 return true;
65 break;
66 case SyncHazard::WRITE_AFTER_WRITE:
67 return false;
68 break;
69 case SyncHazard::READ_RACING_WRITE:
70 return false;
71 break;
72 case SyncHazard::WRITE_RACING_WRITE:
73 return false;
74 break;
75 case SyncHazard::WRITE_RACING_READ:
76 return true;
77 break;
78 default:
79 assert(0);
80 }
81 return false;
82}
83
John Zulauf9cb530d2019-09-30 14:14:10 -060084static const char *string_SyncHazard(SyncHazard hazard) {
85 switch (hazard) {
86 case SyncHazard::NONE:
87 return "NONR";
88 break;
89 case SyncHazard::READ_AFTER_WRITE:
90 return "READ_AFTER_WRITE";
91 break;
92 case SyncHazard::WRITE_AFTER_READ:
93 return "WRITE_AFTER_READ";
94 break;
95 case SyncHazard::WRITE_AFTER_WRITE:
96 return "WRITE_AFTER_WRITE";
97 break;
John Zulauf2f952d22020-02-10 11:34:51 -070098 case SyncHazard::READ_RACING_WRITE:
99 return "READ_RACING_WRITE";
100 break;
101 case SyncHazard::WRITE_RACING_WRITE:
102 return "WRITE_RACING_WRITE";
103 break;
104 case SyncHazard::WRITE_RACING_READ:
105 return "WRITE_RACING_READ";
106 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600107 default:
108 assert(0);
109 }
110 return "INVALID HAZARD";
111}
112
John Zulauf37ceaed2020-07-03 16:18:15 -0600113static const SyncStageAccessInfoType *SyncStageAccessInfoFromMask(SyncStageAccessFlags flags) {
114 // Return the info for the first bit found
115 const SyncStageAccessInfoType *info = nullptr;
116 uint32_t index = 0;
117 while (flags) {
118 if (flags & 0x1) {
119 flags = 0;
120 info = &syncStageAccessInfoByStageAccessIndex[index];
121 } else {
122 flags = flags >> 1;
123 index++;
124 }
125 }
126 return info;
127}
128
John Zulauf59e25072020-07-17 10:55:21 -0600129static std::string string_SyncStageAccessFlags(SyncStageAccessFlags flags, const char *sep = "|") {
130 std::string out_str;
131 uint32_t index = 0;
132 while (flags) {
133 const auto &info = syncStageAccessInfoByStageAccessIndex[index];
134 if (flags & info.stage_access_bit) {
135 if (!out_str.empty()) {
136 out_str.append(sep);
137 }
138 out_str.append(info.name);
139 flags = flags & ~info.stage_access_bit;
140 }
141 index++;
142 assert(index < syncStageAccessInfoByStageAccessIndex.size());
143 }
144 if (out_str.length() == 0) {
145 out_str.append("Unhandled SyncStageAccess");
146 }
147 return out_str;
148}
149
John Zulauf37ceaed2020-07-03 16:18:15 -0600150static std::string string_UsageTag(const HazardResult &hazard) {
151 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600152 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
153 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600154 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600155 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
156 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf59e25072020-07-17 10:55:21 -0600157 out << "(usage: " << usage_info.name << ", prior_usage: " << stage_access_name;
158 if (IsHazardVsRead(hazard.hazard)) {
159 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
160 out << ", read_barriers: " << string_VkPipelineStageFlags(barriers);
161 } else {
162 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
163 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
164 }
165
166 out << ", command: " << CommandTypeString(tag.command);
167 out << ", seq_no: " << (tag.index & 0xFFFFFFFF) << ", reset_no: " << (tag.index >> 32) << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600168 return out.str();
169}
170
John Zulaufd14743a2020-07-03 09:42:39 -0600171// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
172// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
173// also reflects this special case for read hazard detection (using access instead of exec scope)
John Zulaufb027cdb2020-05-21 14:25:22 -0600174static constexpr VkPipelineStageFlags kColorAttachmentExecScope = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
175static constexpr SyncStageAccessFlags kColorAttachmentAccessScope =
176 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
177 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
John Zulaufd14743a2020-07-03 09:42:39 -0600178 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
179 SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600180static constexpr VkPipelineStageFlags kDepthStencilAttachmentExecScope =
181 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
182static constexpr SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
183 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
184 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
185 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
John Zulaufd14743a2020-07-03 09:42:39 -0600186 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
187 SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600188
189static constexpr SyncOrderingBarrier kColorAttachmentRasterOrder = {kColorAttachmentExecScope, kColorAttachmentAccessScope};
190static constexpr SyncOrderingBarrier kDepthStencilAttachmentRasterOrder = {kDepthStencilAttachmentExecScope,
191 kDepthStencilAttachmentAccessScope};
192static constexpr SyncOrderingBarrier kAttachmentRasterOrder = {kDepthStencilAttachmentExecScope | kColorAttachmentExecScope,
193 kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope};
John Zulauf7635de32020-05-29 17:14:15 -0600194// Sometimes we have an internal access conflict, and we using the kCurrentCommandTag to set and detect in temporary/proxy contexts
John Zulaufcc6fecb2020-06-17 15:24:54 -0600195static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600196
locke-lunarg3c038002020-04-30 23:08:08 -0600197inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
198 if (size == VK_WHOLE_SIZE) {
199 return (whole_size - offset);
200 }
201 return size;
202}
203
John Zulauf16adfc92020-04-08 10:28:33 -0600204template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600205static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600206 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
207}
208
John Zulauf355e49b2020-04-24 15:11:15 -0600209static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600210
John Zulauf0cb5be22020-01-23 12:18:22 -0700211// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
212VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
213 VkPipelineStageFlags expanded = stage_mask;
214 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
215 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
216 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
217 if (all_commands.first & queue_flags) {
218 expanded |= all_commands.second;
219 }
220 }
221 }
222 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
223 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
224 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
225 }
226 return expanded;
227}
228
John Zulauf36bcf6a2020-02-03 15:12:52 -0700229VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
230 std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
231 VkPipelineStageFlags unscanned = stage_mask;
232 VkPipelineStageFlags related = 0;
Jonah Ryan-Davis185189c2020-07-14 10:28:52 -0400233 for (const auto &entry : map) {
234 const auto &stage = entry.first;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700235 if (stage & unscanned) {
236 related = related | entry.second;
237 unscanned = unscanned & ~stage;
238 if (!unscanned) break;
239 }
240 }
241 return related;
242}
243
244VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
245 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
246}
247
248VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
249 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
250}
251
John Zulauf5c5e88d2019-12-26 11:22:02 -0700252static const ResourceAccessRange full_range(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700253
locke-lunargff255f92020-05-13 18:53:52 -0600254void GetBufferRange(VkDeviceSize &range_start, VkDeviceSize &range_size, VkDeviceSize offset, VkDeviceSize buf_whole_size,
255 uint32_t first_index, uint32_t count, VkDeviceSize stride) {
256 range_start = offset + first_index * stride;
257 range_size = 0;
258 if (count == UINT32_MAX) {
259 range_size = buf_whole_size - range_start;
260 } else {
261 range_size = count * stride;
262 }
263}
264
locke-lunarg654e3692020-06-04 17:19:15 -0600265SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
266 VkShaderStageFlagBits stage_flag) {
267 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
268 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
269 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
270 }
271 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
272 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
273 assert(0);
274 }
275 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
276 return stage_access->second.uniform_read;
277 }
278
279 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
280 // Because if write hazard happens, read hazard might or might not happen.
281 // But if write hazard doesn't happen, read hazard is impossible to happen.
282 if (descriptor_data.is_writable) {
283 return stage_access->second.shader_write;
284 }
285 return stage_access->second.shader_read;
286}
287
locke-lunarg37047832020-06-12 13:44:45 -0600288bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
289 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
290 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
291 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
292 ? true
293 : false;
294}
295
296bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
297 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
298 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
299 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
300 ? true
301 : false;
302}
303
John Zulauf355e49b2020-04-24 15:11:15 -0600304// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
305const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = {
306 AccessContext::AddressType::kLinearAddress, AccessContext::AddressType::kIdealizedAddress};
307
John Zulauf7635de32020-05-29 17:14:15 -0600308// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
309// Used by both validation and record operations
310//
311// The signature for Action() reflect the needs of both uses.
312template <typename Action>
313void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
314 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
315 VkExtent3D extent = CastTo3D(render_area.extent);
316 VkOffset3D offset = CastTo3D(render_area.offset);
317 const auto &rp_ci = rp_state.createInfo;
318 const auto *attachment_ci = rp_ci.pAttachments;
319 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
320
321 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
322 const auto *color_attachments = subpass_ci.pColorAttachments;
323 const auto *color_resolve = subpass_ci.pResolveAttachments;
324 if (color_resolve && color_attachments) {
325 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
326 const auto &color_attach = color_attachments[i].attachment;
327 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
328 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
329 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
330 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
331 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
332 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
333 }
334 }
335 }
336
337 // Depth stencil resolve only if the extension is present
338 const auto ds_resolve = lvl_find_in_chain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
339 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
340 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
341 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
342 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
343 const auto src_ci = attachment_ci[src_at];
344 // The formats are required to match so we can pick either
345 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
346 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
347 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
348 VkImageAspectFlags aspect_mask = 0u;
349
350 // Figure out which aspects are actually touched during resolve operations
351 const char *aspect_string = nullptr;
352 if (resolve_depth && resolve_stencil) {
353 // Validate all aspects together
354 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
355 aspect_string = "depth/stencil";
356 } else if (resolve_depth) {
357 // Validate depth only
358 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
359 aspect_string = "depth";
360 } else if (resolve_stencil) {
361 // Validate all stencil only
362 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
363 aspect_string = "stencil";
364 }
365
366 if (aspect_mask) {
367 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
368 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kDepthStencilAttachmentRasterOrder, offset, extent,
369 aspect_mask);
370 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
371 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
372 }
373 }
374}
375
376// Action for validating resolve operations
377class ValidateResolveAction {
378 public:
379 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
380 const char *func_name)
381 : render_pass_(render_pass),
382 subpass_(subpass),
383 context_(context),
384 sync_state_(sync_state),
385 func_name_(func_name),
386 skip_(false) {}
387 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
388 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
389 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
390 HazardResult hazard;
391 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
392 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600393 skip_ |= sync_state_.LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
394 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600395 " to resolve attachment %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -0600396 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name,
John Zulauf37ceaed2020-07-03 16:18:15 -0600397 src_at, dst_at, string_UsageTag(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600398 }
399 }
400 // Providing a mechanism for the constructing caller to get the result of the validation
401 bool GetSkip() const { return skip_; }
402
403 private:
404 VkRenderPass render_pass_;
405 const uint32_t subpass_;
406 const AccessContext &context_;
407 const SyncValidator &sync_state_;
408 const char *func_name_;
409 bool skip_;
410};
411
412// Update action for resolve operations
413class UpdateStateResolveAction {
414 public:
415 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
416 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
417 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
418 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
419 // Ignores validation only arguments...
420 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
421 }
422
423 private:
424 AccessContext &context_;
425 const ResourceUsageTag &tag_;
426};
427
John Zulauf59e25072020-07-17 10:55:21 -0600428void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
429 SyncStageAccessFlags prior_, const ResourceUsageTag &tag_) {
430 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
431 usage_index = usage_index_;
432 hazard = hazard_;
433 prior_access = prior_;
434 tag = tag_;
435}
436
John Zulauf540266b2020-04-06 18:54:53 -0600437AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
438 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600439 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600440 Reset();
441 const auto &subpass_dep = dependencies[subpass];
442 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600443 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600444 for (const auto &prev_dep : subpass_dep.prev) {
445 assert(prev_dep.dependency);
446 const auto dep = *prev_dep.dependency;
John Zulauf540266b2020-04-06 18:54:53 -0600447 prev_.emplace_back(const_cast<AccessContext *>(&contexts[dep.srcSubpass]), queue_flags, dep);
John Zulauf355e49b2020-04-24 15:11:15 -0600448 prev_by_subpass_[dep.srcSubpass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700449 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600450
451 async_.reserve(subpass_dep.async.size());
452 for (const auto async_subpass : subpass_dep.async) {
John Zulauf540266b2020-04-06 18:54:53 -0600453 async_.emplace_back(const_cast<AccessContext *>(&contexts[async_subpass]));
John Zulauf3d84f1b2020-03-09 13:33:25 -0600454 }
John Zulaufe5da6e52020-03-18 15:32:18 -0600455 if (subpass_dep.barrier_from_external) {
456 src_external_ = TrackBack(external_context, queue_flags, *subpass_dep.barrier_from_external);
457 } else {
458 src_external_ = TrackBack();
459 }
460 if (subpass_dep.barrier_to_external) {
461 dst_external_ = TrackBack(this, queue_flags, *subpass_dep.barrier_to_external);
462 } else {
463 dst_external_ = TrackBack();
John Zulauf3d84f1b2020-03-09 13:33:25 -0600464 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700465}
466
John Zulauf5f13a792020-03-10 07:31:21 -0600467template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600468HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600469 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600470 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600471 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600472
473 HazardResult hazard;
474 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
475 hazard = detector.Detect(prev);
476 }
477 return hazard;
478}
479
John Zulauf3d84f1b2020-03-09 13:33:25 -0600480// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
481// the DAG of the contexts (for example subpasses)
482template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600483HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
484 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600485 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600486
John Zulauf1a224292020-06-30 14:52:13 -0600487 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600488 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
489 // so we'll check these first
490 for (const auto &async_context : async_) {
491 hazard = async_context->DetectAsyncHazard(type, detector, range);
492 if (hazard.hazard) return hazard;
493 }
John Zulauf5f13a792020-03-10 07:31:21 -0600494 }
495
John Zulauf1a224292020-06-30 14:52:13 -0600496 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600497
John Zulauf69133422020-05-20 14:55:53 -0600498 const auto &accesses = GetAccessStateMap(type);
499 const auto from = accesses.lower_bound(range);
500 const auto to = accesses.upper_bound(range);
501 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600502
John Zulauf69133422020-05-20 14:55:53 -0600503 for (auto pos = from; pos != to; ++pos) {
504 // Cover any leading gap, or gap between entries
505 if (detect_prev) {
506 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
507 // Cover any leading gap, or gap between entries
508 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600509 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600510 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600511 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600512 if (hazard.hazard) return hazard;
513 }
John Zulauf69133422020-05-20 14:55:53 -0600514 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
515 gap.begin = pos->first.end;
516 }
517
518 hazard = detector.Detect(pos);
519 if (hazard.hazard) return hazard;
520 }
521
522 if (detect_prev) {
523 // Detect in the trailing empty as needed
524 gap.end = range.end;
525 if (gap.non_empty()) {
526 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600527 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600528 }
529
530 return hazard;
531}
532
533// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
534template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600535HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600536 auto &accesses = GetAccessStateMap(type);
537 const auto from = accesses.lower_bound(range);
538 const auto to = accesses.upper_bound(range);
539
John Zulauf3d84f1b2020-03-09 13:33:25 -0600540 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600541 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
542 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600543 }
John Zulauf16adfc92020-04-08 10:28:33 -0600544
John Zulauf3d84f1b2020-03-09 13:33:25 -0600545 return hazard;
546}
547
John Zulauf355e49b2020-04-24 15:11:15 -0600548// Returns the last resolved entry
549static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
550 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
551 const SyncBarrier *barrier) {
552 auto at = entry;
553 for (auto pos = first; pos != last; ++pos) {
554 // Every member of the input iterator range must fit within the remaining portion of entry
555 assert(at->first.includes(pos->first));
556 assert(at != dest->end());
557 // Trim up at to the same size as the entry to resolve
558 at = sparse_container::split(at, *dest, pos->first);
559 auto access = pos->second;
560 if (barrier) {
561 access.ApplyBarrier(*barrier);
562 }
563 at->second.Resolve(access);
564 ++at; // Go to the remaining unused section of entry
565 }
566}
567
568void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, const SyncBarrier *barrier,
569 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
570 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600571 if (!range.non_empty()) return;
572
John Zulauf355e49b2020-04-24 15:11:15 -0600573 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
574 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600575 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600576 if (current->pos_B->valid) {
577 const auto &src_pos = current->pos_B->lower_bound;
John Zulauf355e49b2020-04-24 15:11:15 -0600578 auto access = src_pos->second;
579 if (barrier) {
580 access.ApplyBarrier(*barrier);
581 }
John Zulauf16adfc92020-04-08 10:28:33 -0600582 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600583 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
584 trimmed->second.Resolve(access);
585 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600586 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600587 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600588 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600589 }
John Zulauf16adfc92020-04-08 10:28:33 -0600590 } else {
591 // we have to descend to fill this gap
592 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600593 if (current->pos_A->valid) {
594 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
595 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600596 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulauf355e49b2020-04-24 15:11:15 -0600597 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier);
598 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600599 // There isn't anything in dest in current)range, so we can accumulate directly into it.
600 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulauf355e49b2020-04-24 15:11:15 -0600601 if (barrier) {
602 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
John Zulauf3bcab5e2020-06-19 14:42:32 -0600603 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
John Zulauf355e49b2020-04-24 15:11:15 -0600604 pos->second.ApplyBarrier(*barrier);
605 }
606 }
607 }
608 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
609 // iterator of the outer while.
610
611 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
612 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
613 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600614 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
615 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600616 current.seek(seek_to);
617 } else if (!current->pos_A->valid && infill_state) {
618 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
619 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
620 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600621 }
John Zulauf5f13a792020-03-10 07:31:21 -0600622 }
John Zulauf16adfc92020-04-08 10:28:33 -0600623 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600624 }
John Zulauf1a224292020-06-30 14:52:13 -0600625
626 // Infill if range goes passed both the current and resolve map prior contents
627 if (recur_to_infill && (current->range.end < range.end)) {
628 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
629 ResourceAccessRangeMap gap_map;
630 const auto the_end = resolve_map->end();
631 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
632 for (auto &access : gap_map) {
633 access.second.ApplyBarrier(*barrier);
634 resolve_map->insert(the_end, access);
635 }
636 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600637}
638
John Zulauf355e49b2020-04-24 15:11:15 -0600639void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
640 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600641 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600642 if (range.non_empty() && infill_state) {
643 descent_map->insert(std::make_pair(range, *infill_state));
644 }
645 } else {
646 // Look for something to fill the gap further along.
647 for (const auto &prev_dep : prev_) {
John Zulauf355e49b2020-04-24 15:11:15 -0600648 prev_dep.context->ResolveAccessRange(type, range, &prev_dep.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600649 }
650
John Zulaufe5da6e52020-03-18 15:32:18 -0600651 if (src_external_.context) {
John Zulauf355e49b2020-04-24 15:11:15 -0600652 src_external_.context->ResolveAccessRange(type, range, &src_external_.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600653 }
654 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600655}
656
John Zulauf16adfc92020-04-08 10:28:33 -0600657AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600658 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600659}
660
661VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) {
662 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
663}
664
John Zulauf355e49b2020-04-24 15:11:15 -0600665static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
John Zulauf16adfc92020-04-08 10:28:33 -0600666
John Zulauf1507ee42020-05-18 11:33:09 -0600667static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
668 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
669 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
670 return stage_access;
671}
672static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
673 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
674 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
675 return stage_access;
676}
677
John Zulauf7635de32020-05-29 17:14:15 -0600678// Caller must manage returned pointer
679static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
680 uint32_t subpass, const VkRect2D &render_area,
681 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
682 auto *proxy = new AccessContext(context);
683 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600684 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600685 return proxy;
686}
687
John Zulauf540266b2020-04-06 18:54:53 -0600688void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
John Zulauf355e49b2020-04-24 15:11:15 -0600689 AddressType address_type, ResourceAccessRangeMap *descent_map,
690 const ResourceAccessState *infill_state) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600691 if (!SimpleBinding(image_state)) return;
692
John Zulauf62f10592020-04-03 12:20:02 -0600693 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
locke-lunargae26eac2020-04-16 15:29:05 -0600694 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600695 image_state.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600696 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf62f10592020-04-03 12:20:02 -0600697 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600698 ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state);
John Zulauf62f10592020-04-03 12:20:02 -0600699 }
700}
701
John Zulauf7635de32020-05-29 17:14:15 -0600702// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600703bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600704 const VkRect2D &render_area, uint32_t subpass,
705 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
706 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600707 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600708 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
709 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
710 // those affects have not been recorded yet.
711 //
712 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
713 // to apply and only copy then, if this proves a hot spot.
714 std::unique_ptr<AccessContext> proxy_for_prev;
715 TrackBack proxy_track_back;
716
John Zulauf355e49b2020-04-24 15:11:15 -0600717 const auto &transitions = rp_state.subpass_transitions[subpass];
718 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600719 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
720
721 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
722 if (prev_needs_proxy) {
723 if (!proxy_for_prev) {
724 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
725 render_area, attachment_views));
726 proxy_track_back = *track_back;
727 proxy_track_back.context = proxy_for_prev.get();
728 }
729 track_back = &proxy_track_back;
730 }
731 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600732 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600733 skip |= sync_state.LogError(
734 rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -0600735 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " image layout transition. Access info %s.",
John Zulauf37ceaed2020-07-03 16:18:15 -0600736 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment, string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600737 }
738 }
739 return skip;
740}
741
John Zulauf1507ee42020-05-18 11:33:09 -0600742bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600743 const VkRect2D &render_area, uint32_t subpass,
744 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
745 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600746 bool skip = false;
747 const auto *attachment_ci = rp_state.createInfo.pAttachments;
748 VkExtent3D extent = CastTo3D(render_area.extent);
749 VkOffset3D offset = CastTo3D(render_area.offset);
750 const auto external_access_scope = src_external_.barrier.dst_access_scope;
John Zulauf1507ee42020-05-18 11:33:09 -0600751
752 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
753 if (subpass == rp_state.attachment_first_subpass[i]) {
754 if (attachment_views[i] == nullptr) continue;
755 const IMAGE_VIEW_STATE &view = *attachment_views[i];
756 const IMAGE_STATE *image = view.image_state.get();
757 if (image == nullptr) continue;
758 const auto &ci = attachment_ci[i];
759 const bool is_transition = rp_state.attachment_first_is_transition[i];
760
761 // Need check in the following way
762 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
763 // vs. transition
764 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
765 // for each aspect loaded.
766
767 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600768 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600769 const bool is_color = !(has_depth || has_stencil);
770
771 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
772 const SyncStageAccessFlags load_mask = (has_depth || is_color) ? SyncStageAccess::Flags(load_index) : 0U;
773 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
774 const SyncStageAccessFlags stencil_mask = has_stencil ? SyncStageAccess::Flags(stencil_load_index) : 0U;
775
John Zulaufaff20662020-06-01 14:07:58 -0600776 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600777 const char *aspect = nullptr;
778 if (is_transition) {
779 // For transition w
780 SyncHazard transition_hazard = SyncHazard::NONE;
781 bool checked_stencil = false;
782 if (load_mask) {
783 if ((load_mask & external_access_scope) != load_mask) {
784 transition_hazard =
785 SyncStageAccess::HasWrite(load_mask) ? SyncHazard::WRITE_AFTER_WRITE : SyncHazard::READ_AFTER_WRITE;
786 aspect = is_color ? "color" : "depth";
787 }
788 if (!transition_hazard && stencil_mask) {
789 if ((stencil_mask & external_access_scope) != stencil_mask) {
790 transition_hazard = SyncStageAccess::HasWrite(stencil_mask) ? SyncHazard::WRITE_AFTER_WRITE
791 : SyncHazard::READ_AFTER_WRITE;
792 aspect = "stencil";
793 checked_stencil = true;
794 }
795 }
796 }
797 if (transition_hazard) {
798 // Hazard vs. ILT
799 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
800 skip |=
801 sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
802 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
803 " aspect %s during load with loadOp %s.",
804 func_name, string_SyncHazard(transition_hazard), subpass, i, aspect, load_op_string);
805 }
806 } else {
807 auto hazard_range = view.normalized_subresource_range;
808 bool checked_stencil = false;
809 if (is_color) {
810 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, offset, extent);
811 aspect = "color";
812 } else {
813 if (has_depth) {
814 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
815 hazard = DetectHazard(*image, load_index, hazard_range, offset, extent);
816 aspect = "depth";
817 }
818 if (!hazard.hazard && has_stencil) {
819 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
820 hazard = DetectHazard(*image, stencil_load_index, hazard_range, offset, extent);
821 aspect = "stencil";
822 checked_stencil = true;
823 }
824 }
825
826 if (hazard.hazard) {
827 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
828 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
829 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600830 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -0600831 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600832 string_UsageTag(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -0600833 }
834 }
835 }
836 }
837 return skip;
838}
839
John Zulaufaff20662020-06-01 14:07:58 -0600840// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
841// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
842// store is part of the same Next/End operation.
843// The latter is handled in layout transistion validation directly
844bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
845 const VkRect2D &render_area, uint32_t subpass,
846 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
847 const char *func_name) const {
848 bool skip = false;
849 const auto *attachment_ci = rp_state.createInfo.pAttachments;
850 VkExtent3D extent = CastTo3D(render_area.extent);
851 VkOffset3D offset = CastTo3D(render_area.offset);
852
853 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
854 if (subpass == rp_state.attachment_last_subpass[i]) {
855 if (attachment_views[i] == nullptr) continue;
856 const IMAGE_VIEW_STATE &view = *attachment_views[i];
857 const IMAGE_STATE *image = view.image_state.get();
858 if (image == nullptr) continue;
859 const auto &ci = attachment_ci[i];
860
861 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
862 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
863 // sake, we treat DONT_CARE as writing.
864 const bool has_depth = FormatHasDepth(ci.format);
865 const bool has_stencil = FormatHasStencil(ci.format);
866 const bool is_color = !(has_depth || has_stencil);
867 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
868 if (!has_stencil && !store_op_stores) continue;
869
870 HazardResult hazard;
871 const char *aspect = nullptr;
872 bool checked_stencil = false;
873 if (is_color) {
874 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
875 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
876 aspect = "color";
877 } else {
878 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
879 auto hazard_range = view.normalized_subresource_range;
880 if (has_depth && store_op_stores) {
881 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
882 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
883 kAttachmentRasterOrder, offset, extent);
884 aspect = "depth";
885 }
886 if (!hazard.hazard && has_stencil && stencil_op_stores) {
887 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
888 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
889 kAttachmentRasterOrder, offset, extent);
890 aspect = "stencil";
891 checked_stencil = true;
892 }
893 }
894
895 if (hazard.hazard) {
896 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
897 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -0600898 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
899 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600900 " %s aspect during store with %s %s. Access info %s",
John Zulauf1dae9192020-06-16 15:46:44 -0600901 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600902 store_op_string, string_UsageTag(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -0600903 }
904 }
905 }
906 return skip;
907}
908
John Zulaufb027cdb2020-05-21 14:25:22 -0600909bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
910 const VkRect2D &render_area,
911 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
912 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600913 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
914 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
915 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600916}
917
John Zulauf3d84f1b2020-03-09 13:33:25 -0600918class HazardDetector {
919 SyncStageAccessIndex usage_index_;
920
921 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600922 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600923 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
924 return pos->second.DetectAsyncHazard(usage_index_);
925 }
926 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
927};
928
John Zulauf69133422020-05-20 14:55:53 -0600929class HazardDetectorWithOrdering {
930 const SyncStageAccessIndex usage_index_;
931 const SyncOrderingBarrier &ordering_;
932
933 public:
934 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
935 return pos->second.DetectHazard(usage_index_, ordering_);
936 }
937 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
938 return pos->second.DetectAsyncHazard(usage_index_);
939 }
940 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
941 : usage_index_(usage), ordering_(ordering) {}
942};
943
John Zulauf16adfc92020-04-08 10:28:33 -0600944HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600945 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600946 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600947 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600948}
949
John Zulauf16adfc92020-04-08 10:28:33 -0600950HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600951 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600952 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -0600953 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -0600954}
955
John Zulauf69133422020-05-20 14:55:53 -0600956template <typename Detector>
957HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
958 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
959 const VkExtent3D &extent, DetectOptions options) const {
960 if (!SimpleBinding(image)) return HazardResult();
961 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
962 const auto address_type = ImageAddressType(image);
963 const auto base_address = ResourceBaseAddress(image);
964 for (; range_gen->non_empty(); ++range_gen) {
965 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
966 if (hazard.hazard) return hazard;
967 }
968 return HazardResult();
969}
970
John Zulauf540266b2020-04-06 18:54:53 -0600971HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
972 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
973 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700974 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
975 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -0600976 return DetectHazard(image, current_usage, subresource_range, offset, extent);
977}
978
979HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
980 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
981 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -0600982 HazardDetector detector(current_usage);
983 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
984}
985
986HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
987 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
988 const VkOffset3D &offset, const VkExtent3D &extent) const {
989 HazardDetectorWithOrdering detector(current_usage, ordering);
990 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -0600991}
992
John Zulaufb027cdb2020-05-21 14:25:22 -0600993// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
994// should have reported the issue regarding an invalid attachment entry
995HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
996 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
997 VkImageAspectFlags aspect_mask) const {
998 if (view != nullptr) {
999 const IMAGE_STATE *image = view->image_state.get();
1000 if (image != nullptr) {
1001 auto *detect_range = &view->normalized_subresource_range;
1002 VkImageSubresourceRange masked_range;
1003 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1004 masked_range = view->normalized_subresource_range;
1005 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1006 detect_range = &masked_range;
1007 }
1008
1009 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1010 if (detect_range->aspectMask) {
1011 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
1012 }
1013 }
1014 }
1015 return HazardResult();
1016}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001017class BarrierHazardDetector {
1018 public:
1019 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1020 SyncStageAccessFlags src_access_scope)
1021 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1022
John Zulauf5f13a792020-03-10 07:31:21 -06001023 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1024 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001025 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001026 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
1027 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1028 return pos->second.DetectAsyncHazard(usage_index_);
1029 }
1030
1031 private:
1032 SyncStageAccessIndex usage_index_;
1033 VkPipelineStageFlags src_exec_scope_;
1034 SyncStageAccessFlags src_access_scope_;
1035};
1036
John Zulauf16adfc92020-04-08 10:28:33 -06001037HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -06001038 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001039 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001040 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -06001041 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001042}
1043
John Zulauf16adfc92020-04-08 10:28:33 -06001044HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001045 SyncStageAccessFlags src_access_scope,
1046 const VkImageSubresourceRange &subresource_range,
1047 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001048 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1049 VkOffset3D zero_offset = {0, 0, 0};
1050 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001051}
1052
John Zulauf355e49b2020-04-24 15:11:15 -06001053HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1054 SyncStageAccessFlags src_stage_accesses,
1055 const VkImageMemoryBarrier &barrier) const {
1056 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1057 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1058 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1059}
1060
John Zulauf9cb530d2019-09-30 14:14:10 -06001061template <typename Flags, typename Map>
1062SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1063 SyncStageAccessFlags scope = 0;
1064 for (const auto &bit_scope : map) {
1065 if (flag_mask < bit_scope.first) break;
1066
1067 if (flag_mask & bit_scope.first) {
1068 scope |= bit_scope.second;
1069 }
1070 }
1071 return scope;
1072}
1073
1074SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
1075 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1076}
1077
1078SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
1079 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
1080}
1081
1082// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
1083SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001084 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1085 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1086 // of the union of all stage/access types for all the stages and the same unions for the access mask...
John Zulauf9cb530d2019-09-30 14:14:10 -06001087 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1088}
1089
1090template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001091void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001092 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1093 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -06001094 auto pos = accesses->lower_bound(range);
1095 if (pos == accesses->end() || !pos->first.intersects(range)) {
1096 // The range is empty, fill it with a default value.
1097 pos = action.Infill(accesses, pos, range);
1098 } else if (range.begin < pos->first.begin) {
1099 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001100 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001101 } else if (pos->first.begin < range.begin) {
1102 // Trim the beginning if needed
1103 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1104 ++pos;
1105 }
1106
1107 const auto the_end = accesses->end();
1108 while ((pos != the_end) && pos->first.intersects(range)) {
1109 if (pos->first.end > range.end) {
1110 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1111 }
1112
1113 pos = action(accesses, pos);
1114 if (pos == the_end) break;
1115
1116 auto next = pos;
1117 ++next;
1118 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1119 // Need to infill if next is disjoint
1120 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001121 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001122 next = action.Infill(accesses, next, new_range);
1123 }
1124 pos = next;
1125 }
1126}
1127
1128struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001129 using Iterator = ResourceAccessRangeMap::iterator;
1130 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001131 // this is only called on gaps, and never returns a gap.
1132 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001133 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001134 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001135 }
John Zulauf5f13a792020-03-10 07:31:21 -06001136
John Zulauf5c5e88d2019-12-26 11:22:02 -07001137 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001138 auto &access_state = pos->second;
1139 access_state.Update(usage, tag);
1140 return pos;
1141 }
1142
John Zulauf16adfc92020-04-08 10:28:33 -06001143 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001144 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001145 : type(type_), context(context_), usage(usage_), tag(tag_) {}
1146 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001147 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001148 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001149 const ResourceUsageTag &tag;
1150};
1151
1152struct ApplyMemoryAccessBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001153 using Iterator = ResourceAccessRangeMap::iterator;
1154 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001155
John Zulauf5c5e88d2019-12-26 11:22:02 -07001156 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001157 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001158 access_state.ApplyMemoryAccessBarrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001159 return pos;
1160 }
1161
John Zulauf36bcf6a2020-02-03 15:12:52 -07001162 ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_,
1163 VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_)
1164 : src_exec_scope(src_exec_scope_),
1165 src_access_scope(src_access_scope_),
1166 dst_exec_scope(dst_exec_scope_),
1167 dst_access_scope(dst_access_scope_) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001168
John Zulauf36bcf6a2020-02-03 15:12:52 -07001169 VkPipelineStageFlags src_exec_scope;
1170 SyncStageAccessFlags src_access_scope;
1171 VkPipelineStageFlags dst_exec_scope;
1172 SyncStageAccessFlags dst_access_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001173};
1174
1175struct ApplyGlobalBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001176 using Iterator = ResourceAccessRangeMap::iterator;
1177 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001178
John Zulauf5c5e88d2019-12-26 11:22:02 -07001179 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001180 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001181 access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001182
1183 for (const auto &functor : barrier_functor) {
1184 functor(accesses, pos);
1185 }
1186 return pos;
1187 }
1188
John Zulauf36bcf6a2020-02-03 15:12:52 -07001189 ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope,
1190 SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses,
John Zulauf9cb530d2019-09-30 14:14:10 -06001191 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001192 : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001193 // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier...
1194 barrier_functor.reserve(memoryBarrierCount);
1195 for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) {
1196 const auto &barrier = pMemoryBarriers[barrier_index];
John Zulauf36bcf6a2020-02-03 15:12:52 -07001197 barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask),
1198 dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
John Zulauf9cb530d2019-09-30 14:14:10 -06001199 }
1200 }
1201
John Zulauf36bcf6a2020-02-03 15:12:52 -07001202 const VkPipelineStageFlags src_exec_scope;
1203 const VkPipelineStageFlags dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001204 std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor;
1205};
1206
John Zulauf355e49b2020-04-24 15:11:15 -06001207void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1208 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001209 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1210 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001211}
1212
John Zulauf16adfc92020-04-08 10:28:33 -06001213void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001214 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001215 if (!SimpleBinding(buffer)) return;
1216 const auto base_address = ResourceBaseAddress(buffer);
1217 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1218}
John Zulauf355e49b2020-04-24 15:11:15 -06001219
John Zulauf540266b2020-04-06 18:54:53 -06001220void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001221 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001222 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001223 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001224 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001225 const auto address_type = ImageAddressType(image);
1226 const auto base_address = ResourceBaseAddress(image);
1227 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001228 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001229 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001230 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001231}
John Zulauf7635de32020-05-29 17:14:15 -06001232void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1233 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1234 if (view != nullptr) {
1235 const IMAGE_STATE *image = view->image_state.get();
1236 if (image != nullptr) {
1237 auto *update_range = &view->normalized_subresource_range;
1238 VkImageSubresourceRange masked_range;
1239 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1240 masked_range = view->normalized_subresource_range;
1241 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1242 update_range = &masked_range;
1243 }
1244 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1245 }
1246 }
1247}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001248
John Zulauf355e49b2020-04-24 15:11:15 -06001249void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1250 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1251 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001252 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1253 subresource.layerCount};
1254 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1255}
1256
John Zulauf540266b2020-04-06 18:54:53 -06001257template <typename Action>
1258void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001259 if (!SimpleBinding(buffer)) return;
1260 const auto base_address = ResourceBaseAddress(buffer);
1261 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001262}
1263
1264template <typename Action>
1265void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1266 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001267 if (!SimpleBinding(image)) return;
1268 const auto address_type = ImageAddressType(image);
1269 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001270
locke-lunargae26eac2020-04-16 15:29:05 -06001271 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001272 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001273
John Zulauf16adfc92020-04-08 10:28:33 -06001274 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001275 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001276 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001277 }
1278}
1279
John Zulauf7635de32020-05-29 17:14:15 -06001280void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1281 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1282 const ResourceUsageTag &tag) {
1283 UpdateStateResolveAction update(*this, tag);
1284 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1285}
1286
John Zulaufaff20662020-06-01 14:07:58 -06001287void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1288 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1289 const ResourceUsageTag &tag) {
1290 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1291 VkExtent3D extent = CastTo3D(render_area.extent);
1292 VkOffset3D offset = CastTo3D(render_area.offset);
1293
1294 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1295 if (rp_state.attachment_last_subpass[i] == subpass) {
1296 if (attachment_views[i] == nullptr) continue; // UNUSED
1297 const auto &view = *attachment_views[i];
1298 const IMAGE_STATE *image = view.image_state.get();
1299 if (image == nullptr) continue;
1300
1301 const auto &ci = attachment_ci[i];
1302 const bool has_depth = FormatHasDepth(ci.format);
1303 const bool has_stencil = FormatHasStencil(ci.format);
1304 const bool is_color = !(has_depth || has_stencil);
1305 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1306
1307 if (is_color && store_op_stores) {
1308 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1309 offset, extent, tag);
1310 } else {
1311 auto update_range = view.normalized_subresource_range;
1312 if (has_depth && store_op_stores) {
1313 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1314 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1315 tag);
1316 }
1317 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1318 if (has_stencil && stencil_op_stores) {
1319 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1320 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1321 tag);
1322 }
1323 }
1324 }
1325 }
1326}
1327
John Zulauf540266b2020-04-06 18:54:53 -06001328template <typename Action>
1329void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1330 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001331 for (const auto address_type : kAddressTypes) {
1332 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001333 }
1334}
1335
1336void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001337 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1338 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -06001339 for (const auto address_type : kAddressTypes) {
John Zulauf355e49b2020-04-24 15:11:15 -06001340 context.ResolveAccessRange(address_type, full_range, &context.GetDstExternalTrackBack().barrier,
1341 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001342 }
1343 }
1344}
1345
John Zulauf355e49b2020-04-24 15:11:15 -06001346void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1347 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1348 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) {
1349 const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1350 UpdateMemoryAccess(image, subresource_range, barrier_action);
1351}
1352
John Zulauf7635de32020-05-29 17:14:15 -06001353// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001354void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1355 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1356 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range,
1357 bool layout_transition, const ResourceUsageTag &tag) {
1358 if (layout_transition) {
1359 UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent,
1360 tag);
1361 ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope,
1362 subresource_range);
John Zulaufc9201222020-05-13 15:13:03 -06001363 } else {
1364 ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range);
John Zulauf355e49b2020-04-24 15:11:15 -06001365 }
John Zulauf355e49b2020-04-24 15:11:15 -06001366}
1367
John Zulauf7635de32020-05-29 17:14:15 -06001368// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001369void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier,
1370 const VkImageSubresourceRange &subresource_range, bool layout_transition,
1371 const ResourceUsageTag &tag) {
1372 ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope,
1373 subresource_range, layout_transition, tag);
1374}
1375
1376// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001377HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001378 if (!attach_view) return HazardResult();
1379 const auto image_state = attach_view->image_state.get();
1380 if (!image_state) return HazardResult();
1381
John Zulauf355e49b2020-04-24 15:11:15 -06001382 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001383 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001384
1385 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulauf7635de32020-05-29 17:14:15 -06001386 auto hazard = track_back.context->DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope,
1387 track_back.barrier.src_access_scope,
1388 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001389 if (!hazard.hazard) {
1390 // The Async hazard check is against the current context's async set.
John Zulauf7635de32020-05-29 17:14:15 -06001391 hazard = DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope, track_back.barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001392 attach_view->normalized_subresource_range, kDetectAsync);
1393 }
1394 return hazard;
1395}
1396
1397// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1398bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1399
1400 const VkRenderPassBeginInfo *pRenderPassBegin,
1401 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1402 const char *func_name) const {
1403 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1404 bool skip = false;
1405 uint32_t subpass = 0;
1406 const auto &transitions = rp_state.subpass_transitions[subpass];
1407 if (transitions.size()) {
1408 const std::vector<AccessContext> empty_context_vector;
1409 // Create context we can use to validate against...
1410 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1411 const_cast<AccessContext *>(&cb_access_context_));
1412
1413 assert(pRenderPassBegin);
1414 if (nullptr == pRenderPassBegin) return skip;
1415
1416 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1417 assert(fb_state);
1418 if (nullptr == fb_state) return skip;
1419
1420 // Create a limited array of views (which we'll need to toss
1421 std::vector<const IMAGE_VIEW_STATE *> views;
1422 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
1423 const auto attachment_count = count_attachment.first;
1424 const auto *attachments = count_attachment.second;
1425 views.resize(attachment_count, nullptr);
1426 for (const auto &transition : transitions) {
1427 assert(transition.attachment < attachment_count);
1428 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
1429 }
1430
John Zulauf7635de32020-05-29 17:14:15 -06001431 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
1432 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001433 }
1434 return skip;
1435}
1436
locke-lunarg61870c22020-06-09 14:51:50 -06001437bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1438 const char *func_name) const {
1439 bool skip = false;
1440 const PIPELINE_STATE *pPipe = nullptr;
1441 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1442 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1443 if (!pPipe || !per_sets) {
1444 return skip;
1445 }
1446
1447 using DescriptorClass = cvdescriptorset::DescriptorClass;
1448 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1449 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1450 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1451 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1452
1453 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarg37047832020-06-12 13:44:45 -06001454 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001455 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1456 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001457 for (const auto &set_binding : stage_state.descriptor_uses) {
1458 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1459 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1460 set_binding.first.second);
1461 const auto descriptor_type = binding_it.GetType();
1462 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1463 auto array_idx = 0;
1464
1465 if (binding_it.IsVariableDescriptorCount()) {
1466 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1467 }
1468 SyncStageAccessIndex sync_index =
1469 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1470
1471 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1472 uint32_t index = i - index_range.start;
1473 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1474 switch (descriptor->GetClass()) {
1475 case DescriptorClass::ImageSampler:
1476 case DescriptorClass::Image: {
1477 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001478 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001479 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001480 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1481 img_view_state = image_sampler_descriptor->GetImageViewState();
1482 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001483 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001484 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1485 img_view_state = image_descriptor->GetImageViewState();
1486 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001487 }
1488 if (!img_view_state) continue;
1489 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1490 VkExtent3D extent = {};
1491 VkOffset3D offset = {};
1492 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1493 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1494 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1495 } else {
1496 extent = img_state->createInfo.extent;
1497 }
John Zulauf361fb532020-07-22 10:45:39 -06001498 HazardResult hazard;
1499 const auto &subresource_range = img_view_state->normalized_subresource_range;
1500 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1501 // Input attachments are subject to raster ordering rules
1502 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
1503 kAttachmentRasterOrder, offset, extent);
1504 } else {
1505 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1506 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001507 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001508 skip |= sync_state_->LogError(
1509 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001510 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1511 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001512 func_name, string_SyncHazard(hazard.hazard),
1513 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1514 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1515 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001516 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1517 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1518 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001519 }
1520 break;
1521 }
1522 case DescriptorClass::TexelBuffer: {
1523 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1524 if (!buf_view_state) continue;
1525 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1526 ResourceAccessRange range =
1527 MakeRange(buf_view_state->create_info.offset,
1528 GetRealWholeSize(buf_view_state->create_info.offset, buf_view_state->create_info.range,
1529 buf_state->createInfo.size));
1530 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001531 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001532 skip |= sync_state_->LogError(
1533 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001534 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1535 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001536 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1537 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1538 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001539 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1540 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1541 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001542 }
1543 break;
1544 }
1545 case DescriptorClass::GeneralBuffer: {
1546 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1547 auto buf_state = buffer_descriptor->GetBufferState();
1548 if (!buf_state) continue;
1549 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1550 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
1551 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001552 skip |= sync_state_->LogError(
1553 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001554 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1555 func_name, string_SyncHazard(hazard.hazard),
1556 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001557 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1558 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001559 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1560 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1561 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001562 }
1563 break;
1564 }
1565 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1566 default:
1567 break;
1568 }
1569 }
1570 }
1571 }
1572 return skip;
1573}
1574
1575void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1576 const ResourceUsageTag &tag) {
1577 const PIPELINE_STATE *pPipe = nullptr;
1578 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1579 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1580 if (!pPipe || !per_sets) {
1581 return;
1582 }
1583
1584 using DescriptorClass = cvdescriptorset::DescriptorClass;
1585 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1586 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1587 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1588 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1589
1590 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001591 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
1592 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1593 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001594 for (const auto &set_binding : stage_state.descriptor_uses) {
1595 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1596 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1597 set_binding.first.second);
1598 const auto descriptor_type = binding_it.GetType();
1599 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1600 auto array_idx = 0;
1601
1602 if (binding_it.IsVariableDescriptorCount()) {
1603 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1604 }
1605 SyncStageAccessIndex sync_index =
1606 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1607
1608 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1609 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1610 switch (descriptor->GetClass()) {
1611 case DescriptorClass::ImageSampler:
1612 case DescriptorClass::Image: {
1613 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1614 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1615 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1616 } else {
1617 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1618 }
1619 if (!img_view_state) continue;
1620 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1621 VkExtent3D extent = {};
1622 VkOffset3D offset = {};
1623 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1624 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1625 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1626 } else {
1627 extent = img_state->createInfo.extent;
1628 }
1629 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1630 offset, extent, tag);
1631 break;
1632 }
1633 case DescriptorClass::TexelBuffer: {
1634 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1635 if (!buf_view_state) continue;
1636 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1637 ResourceAccessRange range =
1638 MakeRange(buf_view_state->create_info.offset, buf_view_state->create_info.range);
1639 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1640 break;
1641 }
1642 case DescriptorClass::GeneralBuffer: {
1643 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1644 auto buf_state = buffer_descriptor->GetBufferState();
1645 if (!buf_state) continue;
1646 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1647 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1648 break;
1649 }
1650 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1651 default:
1652 break;
1653 }
1654 }
1655 }
1656 }
1657}
1658
1659bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1660 bool skip = false;
1661 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1662 if (!pPipe) {
1663 return skip;
1664 }
1665
1666 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1667 const auto &binding_buffers_size = binding_buffers.size();
1668 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1669
1670 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1671 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1672 if (binding_description.binding < binding_buffers_size) {
1673 const auto &binding_buffer = binding_buffers[binding_description.binding];
1674 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1675
1676 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1677 VkDeviceSize range_start = 0;
1678 VkDeviceSize range_size = 0;
1679 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1680 binding_description.stride);
1681 ResourceAccessRange range = MakeRange(range_start, range_size);
1682 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1683 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001684 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001685 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 -06001686 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001687 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001688 }
1689 }
1690 }
1691 return skip;
1692}
1693
1694void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
1695 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1696 if (!pPipe) {
1697 return;
1698 }
1699 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1700 const auto &binding_buffers_size = binding_buffers.size();
1701 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1702
1703 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1704 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1705 if (binding_description.binding < binding_buffers_size) {
1706 const auto &binding_buffer = binding_buffers[binding_description.binding];
1707 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1708
1709 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1710 VkDeviceSize range_start = 0;
1711 VkDeviceSize range_size = 0;
1712 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1713 binding_description.stride);
1714 ResourceAccessRange range = MakeRange(range_start, range_size);
1715 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1716 }
1717 }
1718}
1719
1720bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1721 bool skip = false;
1722 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
1723
1724 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1725 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1726 VkDeviceSize range_start = 0;
1727 VkDeviceSize range_size = 0;
1728 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1729 indexCount, index_size);
1730 ResourceAccessRange range = MakeRange(range_start, range_size);
1731 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1732 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001733 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001734 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 -06001735 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001736 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001737 }
1738
1739 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1740 // We will detect more accurate range in the future.
1741 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1742 return skip;
1743}
1744
1745void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
1746 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return;
1747
1748 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1749 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1750 VkDeviceSize range_start = 0;
1751 VkDeviceSize range_size = 0;
1752 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1753 indexCount, index_size);
1754 ResourceAccessRange range = MakeRange(range_start, range_size);
1755 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1756
1757 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1758 // We will detect more accurate range in the future.
1759 RecordDrawVertex(UINT32_MAX, 0, tag);
1760}
1761
1762bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06001763 bool skip = false;
1764 if (!current_renderpass_context_) return skip;
1765 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1766 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1767 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001768}
1769
1770void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
locke-lunarg7077d502020-06-18 21:37:26 -06001771 if (current_renderpass_context_)
1772 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1773 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001774}
1775
John Zulauf355e49b2020-04-24 15:11:15 -06001776bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001777 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001778 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06001779 skip |=
1780 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001781
1782 return skip;
1783}
1784
1785bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1786 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001787 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001788 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001789 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06001790 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1791 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001792
1793 return skip;
1794}
1795
1796void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1797 assert(sync_state_);
1798 if (!cb_state_) return;
1799
1800 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06001801 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06001802 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06001803 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001804 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001805}
1806
John Zulauf355e49b2020-04-24 15:11:15 -06001807void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001808 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001809 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001810 current_context_ = &current_renderpass_context_->CurrentContext();
1811}
1812
John Zulauf355e49b2020-04-24 15:11:15 -06001813void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001814 assert(current_renderpass_context_);
1815 if (!current_renderpass_context_) return;
1816
John Zulauf1a224292020-06-30 14:52:13 -06001817 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001818 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001819 current_renderpass_context_ = nullptr;
1820}
1821
locke-lunarg61870c22020-06-09 14:51:50 -06001822bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1823 const VkRect2D &render_area, const char *func_name) const {
1824 bool skip = false;
locke-lunarg96dc9632020-06-10 17:22:18 -06001825 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001826 if (!pPipe ||
1827 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001828 return skip;
1829 }
1830 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001831 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1832 VkExtent3D extent = CastTo3D(render_area.extent);
1833 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001834
John Zulauf1a224292020-06-30 14:52:13 -06001835 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001836 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001837 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1838 for (const auto location : list) {
1839 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1840 continue;
1841 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001842 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1843 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06001844 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001845 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001846 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001847 func_name, string_SyncHazard(hazard.hazard),
1848 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1849 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001850 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001851 }
1852 }
1853 }
locke-lunarg37047832020-06-12 13:44:45 -06001854
1855 // PHASE1 TODO: Add layout based read/vs. write selection.
1856 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1857 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1858 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001859 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001860 bool depth_write = false, stencil_write = false;
1861
1862 // PHASE1 TODO: These validation should be in core_checks.
1863 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1864 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1865 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1866 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1867 depth_write = true;
1868 }
1869 // PHASE1 TODO: It needs to check if stencil is writable.
1870 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1871 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1872 // PHASE1 TODO: These validation should be in core_checks.
1873 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1874 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1875 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1876 stencil_write = true;
1877 }
1878
1879 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1880 if (depth_write) {
1881 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001882 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1883 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001884 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001885 skip |= sync_state.LogError(
1886 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001887 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001888 func_name, string_SyncHazard(hazard.hazard),
1889 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1890 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001891 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001892 }
1893 }
1894 if (stencil_write) {
1895 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001896 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1897 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001898 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001899 skip |= sync_state.LogError(
1900 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001901 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001902 func_name, string_SyncHazard(hazard.hazard),
1903 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1904 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001905 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001906 }
locke-lunarg61870c22020-06-09 14:51:50 -06001907 }
1908 }
1909 return skip;
1910}
1911
locke-lunarg96dc9632020-06-10 17:22:18 -06001912void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1913 const ResourceUsageTag &tag) {
1914 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001915 if (!pPipe ||
1916 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001917 return;
1918 }
1919 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001920 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1921 VkExtent3D extent = CastTo3D(render_area.extent);
1922 VkOffset3D offset = CastTo3D(render_area.offset);
1923
John Zulauf1a224292020-06-30 14:52:13 -06001924 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001925 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001926 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1927 for (const auto location : list) {
1928 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1929 continue;
1930 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001931 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
1932 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001933 }
1934 }
locke-lunarg37047832020-06-12 13:44:45 -06001935
1936 // PHASE1 TODO: Add layout based read/vs. write selection.
1937 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1938 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1939 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001940 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001941 bool depth_write = false, stencil_write = false;
1942
1943 // PHASE1 TODO: These validation should be in core_checks.
1944 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1945 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1946 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1947 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1948 depth_write = true;
1949 }
1950 // PHASE1 TODO: It needs to check if stencil is writable.
1951 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1952 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1953 // PHASE1 TODO: These validation should be in core_checks.
1954 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1955 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1956 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1957 stencil_write = true;
1958 }
1959
1960 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1961 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001962 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1963 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001964 }
1965 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001966 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1967 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001968 }
locke-lunarg61870c22020-06-09 14:51:50 -06001969 }
1970}
1971
John Zulauf1507ee42020-05-18 11:33:09 -06001972bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
1973 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001974 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001975 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06001976 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1977 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001978 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1979 func_name);
1980
John Zulauf355e49b2020-04-24 15:11:15 -06001981 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06001982 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06001983 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1984 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1985 return skip;
1986}
1987bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
1988 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001989 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06001990 bool skip = false;
1991 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1992 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001993 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1994 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06001995 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001996 return skip;
1997}
1998
John Zulauf7635de32020-05-29 17:14:15 -06001999AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2000 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2001}
2002
2003bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2004 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002005 bool skip = false;
2006
John Zulauf7635de32020-05-29 17:14:15 -06002007 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2008 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2009 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2010 // to apply and only copy then, if this proves a hot spot.
2011 std::unique_ptr<AccessContext> proxy_for_current;
2012
John Zulauf355e49b2020-04-24 15:11:15 -06002013 // Validate the "finalLayout" transitions to external
2014 // Get them from where there we're hidding in the extra entry.
2015 const auto &final_transitions = rp_state_->subpass_transitions.back();
2016 for (const auto &transition : final_transitions) {
2017 const auto &attach_view = attachment_views_[transition.attachment];
2018 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2019 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002020 auto *context = trackback.context;
2021
2022 if (transition.prev_pass == current_subpass_) {
2023 if (!proxy_for_current) {
2024 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2025 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2026 }
2027 context = proxy_for_current.get();
2028 }
2029
2030 auto hazard = context->DetectImageBarrierHazard(
John Zulauf355e49b2020-04-24 15:11:15 -06002031 *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope,
2032 attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious);
2033 if (hazard.hazard) {
2034 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2035 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06002036 " final image layout transition. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002037 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf37ceaed2020-07-03 16:18:15 -06002038 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002039 }
2040 }
2041 return skip;
2042}
2043
2044void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2045 // Add layout transitions...
2046 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
2047 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -06002048 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -06002049 for (const auto &transition : transitions) {
2050 const auto attachment_view = attachment_views_[transition.attachment];
2051 if (!attachment_view) continue;
2052 const auto image = attachment_view->image_state.get();
2053 if (!image) continue;
2054
2055 const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
John Zulaufc9201222020-05-13 15:13:03 -06002056 auto insert_pair = view_seen.insert(attachment_view);
2057 if (insert_pair.second) {
2058 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
2059 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag);
2060
2061 } else {
2062 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
2063 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
2064 auto barrier_to_transition = barrier->barrier;
2065 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
2066 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag);
2067 }
John Zulauf355e49b2020-04-24 15:11:15 -06002068 }
2069}
2070
John Zulauf1507ee42020-05-18 11:33:09 -06002071void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2072 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2073 auto &subpass_context = subpass_contexts_[current_subpass_];
2074 VkExtent3D extent = CastTo3D(render_area.extent);
2075 VkOffset3D offset = CastTo3D(render_area.offset);
2076
2077 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2078 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2079 if (attachment_views_[i] == nullptr) continue; // UNUSED
2080 const auto &view = *attachment_views_[i];
2081 const IMAGE_STATE *image = view.image_state.get();
2082 if (image == nullptr) continue;
2083
2084 const auto &ci = attachment_ci[i];
2085 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002086 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002087 const bool is_color = !(has_depth || has_stencil);
2088
2089 if (is_color) {
2090 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2091 extent, tag);
2092 } else {
2093 auto update_range = view.normalized_subresource_range;
2094 if (has_depth) {
2095 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2096 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2097 }
2098 if (has_stencil) {
2099 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2100 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2101 tag);
2102 }
2103 }
2104 }
2105 }
2106}
2107
John Zulauf355e49b2020-04-24 15:11:15 -06002108void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002109 const AccessContext *external_context, VkQueueFlags queue_flags,
2110 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002111 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002112 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002113 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2114 // Add this for all subpasses here so that they exsist during next subpass validation
2115 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002116 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002117 }
2118 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2119
2120 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002121 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002122}
John Zulauf1507ee42020-05-18 11:33:09 -06002123
2124void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002125 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2126 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002127 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002128
John Zulauf355e49b2020-04-24 15:11:15 -06002129 current_subpass_++;
2130 assert(current_subpass_ < subpass_contexts_.size());
2131 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002132 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002133}
2134
John Zulauf1a224292020-06-30 14:52:13 -06002135void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2136 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002137 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002138 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002139 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002140
John Zulauf355e49b2020-04-24 15:11:15 -06002141 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002142 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002143
2144 // Add the "finalLayout" transitions to external
2145 // Get them from where there we're hidding in the extra entry.
2146 const auto &final_transitions = rp_state_->subpass_transitions.back();
2147 for (const auto &transition : final_transitions) {
2148 const auto &attachment = attachment_views_[transition.attachment];
2149 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002150 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf1a224292020-06-30 14:52:13 -06002151 external_context->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier,
2152 attachment->normalized_subresource_range, true, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002153 }
2154}
2155
John Zulauf3d84f1b2020-03-09 13:33:25 -06002156SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2157 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2158 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2159 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2160 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2161 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2162 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2163}
2164
2165void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) {
2166 ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
2167 ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope);
2168}
2169
John Zulauf9cb530d2019-09-30 14:14:10 -06002170HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2171 HazardResult hazard;
2172 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002173 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002174 if (IsRead(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002175 // Only check reads vs. last_write if it doesn't happen-after any other read because either:
2176 // * the previous reads are not hazards, and thus last_write must be visible and available to
2177 // any reads that happen after.
2178 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2179 // the current read will be also not be a hazard, thus reporting a hazard here adds no needed information.
2180 if (((usage_stage & read_execution_barriers) == 0) && last_write && IsWriteHazard(usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002181 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002182 }
2183 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002184 // Write operation:
2185 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2186 // If reads exists -- test only against them because either:
2187 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2188 // * the read weren't hazards, and thus if the write is safe w.r.t. the reads, no hazard vs. last_write is possible if
2189 // the current write happens after the reads, so just test the write against the reades
2190 // Otherwise test against last_write
2191 //
2192 // Look for casus belli for WAR
2193 if (last_read_count) {
2194 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2195 const auto &read_access = last_reads[read_index];
2196 if (IsReadHazard(usage_stage, read_access)) {
2197 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2198 break;
2199 }
2200 }
2201 } else if (input_attachment_barriers != kNoAttachmentRead) {
John Zulaufd14743a2020-07-03 09:42:39 -06002202 if (IsReadHazard(usage_stage, input_attachment_barriers)) {
John Zulauf59e25072020-07-17 10:55:21 -06002203 hazard.Set(this, usage_index, WRITE_AFTER_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002204 }
John Zulauf361fb532020-07-22 10:45:39 -06002205 } else if (last_write && IsWriteHazard(usage)) {
2206 // Write-After-Write check -- if we have a previous write to test against
2207 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002208 }
2209 }
2210 return hazard;
2211}
2212
John Zulauf69133422020-05-20 14:55:53 -06002213HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2214 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2215 HazardResult hazard;
2216 const auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002217 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf69133422020-05-20 14:55:53 -06002218 const bool write_is_ordered = (last_write & ordering.access_scope) == last_write; // Is true if no write, and that's good.
2219 if (IsRead(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002220 if (!write_is_ordered) {
2221 // Only check for RAW if the write is unordered, and there are no reads ordered before the current read since last_write
2222 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2223 // We need to assemble the effect read_execution barriers from the union of the state barriers and the ordering rules
2224 // Check to see if there are any reads ordered before usage, including ordering rules and barriers.
2225 bool ordered_read = 0 != ((last_read_stages & ordering.exec_scope) | (read_execution_barriers & usage_stage));
2226 // Noting the "special* encoding of the input attachment ordering rule (in access, but not exec)
2227 if ((ordering.access_scope & SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) &&
2228 (input_attachment_barriers != kNoAttachmentRead)) {
2229 ordered_read = true;
John Zulauf69133422020-05-20 14:55:53 -06002230 }
John Zulaufd14743a2020-07-03 09:42:39 -06002231
John Zulauf361fb532020-07-22 10:45:39 -06002232 if (!ordered_read && IsWriteHazard(usage)) {
2233 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2234 }
2235 }
2236
2237 } else {
2238 // Only check for WAW if there are no reads since last_write
2239 if (last_read_count) {
2240 // Ignore ordered read stages (which represent frame-buffer local operations, except input attachment
2241 const auto unordered_reads = last_read_stages & ~ordering.exec_scope;
2242 // Look for any WAR hazards outside the ordered set of stages
2243 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2244 const auto &read_access = last_reads[read_index];
2245 if ((read_access.stage & unordered_reads) && IsReadHazard(usage_stage, read_access)) {
2246 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2247 break;
John Zulaufd14743a2020-07-03 09:42:39 -06002248 }
2249 }
John Zulauf361fb532020-07-22 10:45:39 -06002250 } else if (input_attachment_barriers != kNoAttachmentRead) {
2251 // This is special case code for the fragment shader input attachment, which unlike all other fragment shader operations
2252 // is framebuffer local, and thus subject to raster ordering guarantees
2253 if (0 == (ordering.access_scope & SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT)) {
2254 // NOTE: Currently all ordering barriers include this bit, so this code may never be reached, but it's
2255 // here s.t. if we need to change the ordering barrier/rules we needn't change the code.
2256 hazard.Set(this, usage_index, WRITE_AFTER_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
2257 }
2258 } else if (!write_is_ordered && IsWriteHazard(usage)) {
2259 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf69133422020-05-20 14:55:53 -06002260 }
2261 }
2262 return hazard;
2263}
2264
John Zulauf2f952d22020-02-10 11:34:51 -07002265// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06002266HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002267 HazardResult hazard;
2268 auto usage = FlagBit(usage_index);
2269 if (IsRead(usage)) {
2270 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002271 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002272 }
2273 } else {
2274 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002275 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002276 } else if (last_read_count > 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002277 hazard.Set(this, usage_index, WRITE_RACING_READ, last_reads[0].access, last_reads[0].tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002278 } else if (input_attachment_barriers != kNoAttachmentRead) {
John Zulauf59e25072020-07-17 10:55:21 -06002279 hazard.Set(this, usage_index, WRITE_RACING_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002280 }
2281 }
2282 return hazard;
2283}
2284
John Zulauf36bcf6a2020-02-03 15:12:52 -07002285HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
2286 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002287 // Only supporting image layout transitions for now
2288 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2289 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002290 // only test for WAW if there no intervening read operations.
2291 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2292 if (last_read_count) {
John Zulauf355e49b2020-04-24 15:11:15 -06002293 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002294 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002295 const auto &read_access = last_reads[read_index];
2296 // If the read stage is not in the src sync sync
2297 // *AND* not execution chained with an existing sync barrier (that's the or)
2298 // then the barrier access is unsafe (R/W after R)
2299 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002300 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002301 break;
2302 }
2303 }
John Zulauf361fb532020-07-22 10:45:39 -06002304 } else if (input_attachment_barriers != kNoAttachmentRead) {
John Zulaufd14743a2020-07-03 09:42:39 -06002305 // Same logic as read acces above for the special case of input attachment read
John Zulaufd14743a2020-07-03 09:42:39 -06002306 if ((src_exec_scope & (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | input_attachment_barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002307 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 -06002308 }
John Zulauf361fb532020-07-22 10:45:39 -06002309 } else if (last_write) {
2310 // If the previous write is *not* in the 1st access scope
2311 // *AND* the current barrier is not in the dependency chain
2312 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2313 // then the barrier access is unsafe (R/W after W)
2314 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
2315 // TODO: Do we need a difference hazard name for this?
2316 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2317 }
John Zulaufd14743a2020-07-03 09:42:39 -06002318 }
John Zulauf361fb532020-07-22 10:45:39 -06002319
John Zulauf0cb5be22020-01-23 12:18:22 -07002320 return hazard;
2321}
2322
John Zulauf5f13a792020-03-10 07:31:21 -06002323// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2324// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2325// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2326void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2327 if (write_tag.IsBefore(other.write_tag)) {
2328 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
2329 *this = other;
2330 } else if (!other.write_tag.IsBefore(write_tag)) {
2331 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2332 // dependency chaining logic or any stage expansion)
2333 write_barriers |= other.write_barriers;
2334
John Zulaufd14743a2020-07-03 09:42:39 -06002335 // Merge the read states
2336 if (input_attachment_barriers == kNoAttachmentRead) {
2337 // this doesn't have an input attachment read, so we'll take other, unconditionally (even if it's kNoAttachmentRead)
2338 input_attachment_barriers = other.input_attachment_barriers;
2339 input_attachment_tag = other.input_attachment_tag;
2340 } else if (other.input_attachment_barriers != kNoAttachmentRead) {
2341 // Both states have an input attachment read, pick the newest tag and merge barriers.
2342 if (input_attachment_tag.IsBefore(other.input_attachment_tag)) {
2343 input_attachment_tag = other.input_attachment_tag;
2344 }
2345 input_attachment_barriers |= other.input_attachment_barriers;
2346 }
2347 // The else clause is that only this has an attachment read and no merge is needed
2348
John Zulauf5f13a792020-03-10 07:31:21 -06002349 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2350 auto &other_read = other.last_reads[other_read_index];
2351 if (last_read_stages & other_read.stage) {
2352 // Merge in the barriers for read stages that exist in *both* this and other
2353 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
2354 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
2355 auto &my_read = last_reads[my_read_index];
2356 if (other_read.stage == my_read.stage) {
2357 if (my_read.tag.IsBefore(other_read.tag)) {
2358 my_read.tag = other_read.tag;
John Zulauf37ceaed2020-07-03 16:18:15 -06002359 my_read.access = other_read.access;
John Zulauf5f13a792020-03-10 07:31:21 -06002360 }
2361 my_read.barriers |= other_read.barriers;
2362 break;
2363 }
2364 }
2365 } else {
2366 // The other read stage doesn't exist in this, so add it.
2367 last_reads[last_read_count] = other_read;
2368 last_read_count++;
2369 last_read_stages |= other_read.stage;
2370 }
2371 }
John Zulauf361fb532020-07-22 10:45:39 -06002372 read_execution_barriers |= other.read_execution_barriers;
John Zulauf5f13a792020-03-10 07:31:21 -06002373 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
2374 // it.
2375}
2376
John Zulauf9cb530d2019-09-30 14:14:10 -06002377void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2378 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2379 const auto usage_bit = FlagBit(usage_index);
John Zulaufd14743a2020-07-03 09:42:39 -06002380 if (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) {
2381 // Input attachment requires special treatment for raster/load/store ordering guarantees
2382 input_attachment_barriers = 0;
2383 input_attachment_tag = tag;
2384 } else if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002385 // Mulitple outstanding reads may be of interest and do dependency chains independently
2386 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2387 const auto usage_stage = PipelineStageBit(usage_index);
2388 if (usage_stage & last_read_stages) {
2389 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2390 ReadState &access = last_reads[read_index];
2391 if (access.stage == usage_stage) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002392 access.access = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002393 access.barriers = 0;
2394 access.tag = tag;
2395 break;
2396 }
2397 }
2398 } else {
2399 // We don't have this stage in the list yet...
2400 assert(last_read_count < last_reads.size());
2401 ReadState &access = last_reads[last_read_count++];
2402 access.stage = usage_stage;
John Zulauf37ceaed2020-07-03 16:18:15 -06002403 access.access = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002404 access.barriers = 0;
2405 access.tag = tag;
2406 last_read_stages |= usage_stage;
2407 }
2408 } else {
2409 // Assume write
2410 // TODO determine what to do with READ-WRITE operations if any
John Zulaufd14743a2020-07-03 09:42:39 -06002411 // Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
John Zulauf9cb530d2019-09-30 14:14:10 -06002412 // if the last_reads/last_write were unsafe, we've reported them,
2413 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
2414 last_read_count = 0;
2415 last_read_stages = 0;
John Zulauf361fb532020-07-22 10:45:39 -06002416 read_execution_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002417
John Zulaufd14743a2020-07-03 09:42:39 -06002418 input_attachment_barriers = kNoAttachmentRead; // Denotes no outstanding input attachment read after the last write.
2419 // NOTE: we don't reset the tag, as the equality check ignores it when kNoAttachmentRead is set.
2420
John Zulauf9cb530d2019-09-30 14:14:10 -06002421 write_barriers = 0;
2422 write_dependency_chain = 0;
2423 write_tag = tag;
2424 last_write = usage_bit;
2425 }
2426}
John Zulauf5f13a792020-03-10 07:31:21 -06002427
John Zulauf9cb530d2019-09-30 14:14:10 -06002428void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
2429 // Execution Barriers only protect read operations
2430 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2431 ReadState &access = last_reads[read_index];
2432 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2433 if (srcStageMask & (access.stage | access.barriers)) {
2434 access.barriers |= dstStageMask;
John Zulauf361fb532020-07-22 10:45:39 -06002435 read_execution_barriers |= dstStageMask;
John Zulauf9cb530d2019-09-30 14:14:10 -06002436 }
2437 }
John Zulauf361fb532020-07-22 10:45:39 -06002438 if ((input_attachment_barriers != kNoAttachmentRead) &&
2439 (srcStageMask & (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | input_attachment_barriers))) {
John Zulaufd14743a2020-07-03 09:42:39 -06002440 input_attachment_barriers |= dstStageMask;
John Zulauf361fb532020-07-22 10:45:39 -06002441 read_execution_barriers |= dstStageMask;
John Zulaufd14743a2020-07-03 09:42:39 -06002442 }
John Zulauf361fb532020-07-22 10:45:39 -06002443 if (write_dependency_chain & srcStageMask) {
2444 write_dependency_chain |= dstStageMask;
2445 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002446}
2447
John Zulauf36bcf6a2020-02-03 15:12:52 -07002448void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
2449 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002450 // Assuming we've applied the execution side of this barrier, we update just the write
2451 // The || implements the "dependency chain" logic for this barrier
John Zulauf36bcf6a2020-02-03 15:12:52 -07002452 if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) {
2453 write_barriers |= dst_access_scope;
2454 write_dependency_chain |= dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06002455 }
2456}
2457
John Zulauf59e25072020-07-17 10:55:21 -06002458// This should be just Bits or Index, but we don't have an invalid state for Index
2459VkPipelineStageFlags ResourceAccessState::GetReadBarriers(SyncStageAccessFlags usage_bit) const {
2460 VkPipelineStageFlags barriers = 0U;
2461 if (usage_bit & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) {
2462 barriers = input_attachment_barriers;
2463 } else {
2464 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2465 const auto &read_access = last_reads[read_index];
2466 if (read_access.access & usage_bit) {
2467 barriers = read_access.barriers;
2468 break;
2469 }
2470 }
2471 }
2472 return barriers;
2473}
2474
John Zulaufd1f85d42020-04-15 12:23:15 -06002475void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002476 auto *access_context = GetAccessContextNoInsert(command_buffer);
2477 if (access_context) {
2478 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002479 }
2480}
2481
John Zulaufd1f85d42020-04-15 12:23:15 -06002482void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2483 auto access_found = cb_access_state.find(command_buffer);
2484 if (access_found != cb_access_state.end()) {
2485 access_found->second->Reset();
2486 cb_access_state.erase(access_found);
2487 }
2488}
2489
John Zulauf540266b2020-04-06 18:54:53 -06002490void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002491 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
2492 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002493 const VkMemoryBarrier *pMemoryBarriers) {
2494 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07002495 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002496 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002497 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002498}
2499
John Zulauf540266b2020-04-06 18:54:53 -06002500void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002501 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2502 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002503 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002504 for (uint32_t index = 0; index < barrier_count; index++) {
locke-lunarg3c038002020-04-30 23:08:08 -06002505 auto barrier = barriers[index];
John Zulauf9cb530d2019-09-30 14:14:10 -06002506 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2507 if (!buffer) continue;
locke-lunarg3c038002020-04-30 23:08:08 -06002508 barrier.size = GetRealWholeSize(barrier.offset, barrier.size, buffer->createInfo.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002509 ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002510 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2511 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2512 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2513 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002514 }
2515}
2516
John Zulauf540266b2020-04-06 18:54:53 -06002517void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2518 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2519 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002520 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002521 for (uint32_t index = 0; index < barrier_count; index++) {
2522 const auto &barrier = barriers[index];
2523 const auto *image = Get<IMAGE_STATE>(barrier.image);
2524 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002525 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002526 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2527 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2528 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2529 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
2530 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002531 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002532}
2533
2534bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2535 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2536 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002537 const auto *cb_context = GetAccessContext(commandBuffer);
2538 assert(cb_context);
2539 if (!cb_context) return skip;
2540 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002541
John Zulauf3d84f1b2020-03-09 13:33:25 -06002542 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002543 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002544 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002545
2546 for (uint32_t region = 0; region < regionCount; region++) {
2547 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002548 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002549 ResourceAccessRange src_range = MakeRange(
2550 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002551 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002552 if (hazard.hazard) {
2553 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002554 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002555 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002556 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002557 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002558 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002559 }
John Zulauf16adfc92020-04-08 10:28:33 -06002560 if (dst_buffer && !skip) {
locke-lunargff255f92020-05-13 18:53:52 -06002561 ResourceAccessRange dst_range = MakeRange(
2562 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf355e49b2020-04-24 15:11:15 -06002563 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002564 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002565 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002566 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002567 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002568 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002569 }
2570 }
2571 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002572 }
2573 return skip;
2574}
2575
2576void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2577 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002578 auto *cb_context = GetAccessContext(commandBuffer);
2579 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002580 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002581 auto *context = cb_context->GetCurrentAccessContext();
2582
John Zulauf9cb530d2019-09-30 14:14:10 -06002583 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002584 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002585
2586 for (uint32_t region = 0; region < regionCount; region++) {
2587 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002588 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002589 ResourceAccessRange src_range = MakeRange(
2590 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002591 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002592 }
John Zulauf16adfc92020-04-08 10:28:33 -06002593 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002594 ResourceAccessRange dst_range = MakeRange(
2595 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002596 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002597 }
2598 }
2599}
2600
2601bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2602 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2603 const VkImageCopy *pRegions) const {
2604 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002605 const auto *cb_access_context = GetAccessContext(commandBuffer);
2606 assert(cb_access_context);
2607 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002608
John Zulauf3d84f1b2020-03-09 13:33:25 -06002609 const auto *context = cb_access_context->GetCurrentAccessContext();
2610 assert(context);
2611 if (!context) return skip;
2612
2613 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2614 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002615 for (uint32_t region = 0; region < regionCount; region++) {
2616 const auto &copy_region = pRegions[region];
2617 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002618 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002619 copy_region.srcOffset, copy_region.extent);
2620 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002621 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002622 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002623 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002624 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002625 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002626 }
2627
2628 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002629 VkExtent3D dst_copy_extent =
2630 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002631 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002632 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002633 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002634 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002635 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002636 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002637 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002638 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002639 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002640 }
2641 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002642
John Zulauf5c5e88d2019-12-26 11:22:02 -07002643 return skip;
2644}
2645
2646void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2647 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2648 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002649 auto *cb_access_context = GetAccessContext(commandBuffer);
2650 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002651 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002652 auto *context = cb_access_context->GetCurrentAccessContext();
2653 assert(context);
2654
John Zulauf5c5e88d2019-12-26 11:22:02 -07002655 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002656 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002657
2658 for (uint32_t region = 0; region < regionCount; region++) {
2659 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002660 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002661 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2662 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002663 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002664 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002665 VkExtent3D dst_copy_extent =
2666 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002667 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2668 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002669 }
2670 }
2671}
2672
2673bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2674 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2675 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2676 uint32_t bufferMemoryBarrierCount,
2677 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2678 uint32_t imageMemoryBarrierCount,
2679 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2680 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002681 const auto *cb_access_context = GetAccessContext(commandBuffer);
2682 assert(cb_access_context);
2683 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002684
John Zulauf3d84f1b2020-03-09 13:33:25 -06002685 const auto *context = cb_access_context->GetCurrentAccessContext();
2686 assert(context);
2687 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002688
John Zulauf3d84f1b2020-03-09 13:33:25 -06002689 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002690 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2691 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002692 // Validate Image Layout transitions
2693 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2694 const auto &barrier = pImageMemoryBarriers[index];
2695 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2696 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2697 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002698 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002699 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002700 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002701 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002702 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002703 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002704 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002705 }
2706 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002707
2708 return skip;
2709}
2710
2711void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2712 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2713 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2714 uint32_t bufferMemoryBarrierCount,
2715 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2716 uint32_t imageMemoryBarrierCount,
2717 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002718 auto *cb_access_context = GetAccessContext(commandBuffer);
2719 assert(cb_access_context);
2720 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002721 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002722 auto access_context = cb_access_context->GetCurrentAccessContext();
2723 assert(access_context);
2724 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06002725
John Zulauf3d84f1b2020-03-09 13:33:25 -06002726 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002727 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002728 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002729 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
2730 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2731 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002732 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
2733 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002734 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06002735 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002736
2737 // 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 -06002738 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07002739 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06002740}
2741
2742void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
2743 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
2744 // The state tracker sets up the device state
2745 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
2746
John Zulauf5f13a792020-03-10 07:31:21 -06002747 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
2748 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06002749 // TODO: Find a good way to do this hooklessly.
2750 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
2751 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
2752 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
2753
John Zulaufd1f85d42020-04-15 12:23:15 -06002754 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2755 sync_device_state->ResetCommandBufferCallback(command_buffer);
2756 });
2757 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2758 sync_device_state->FreeCommandBufferCallback(command_buffer);
2759 });
John Zulauf9cb530d2019-09-30 14:14:10 -06002760}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002761
John Zulauf355e49b2020-04-24 15:11:15 -06002762bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2763 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
2764 bool skip = false;
2765 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
2766 auto cb_context = GetAccessContext(commandBuffer);
2767
2768 if (rp_state && cb_context) {
2769 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
2770 }
2771
2772 return skip;
2773}
2774
2775bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2776 VkSubpassContents contents) const {
2777 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2778 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2779 subpass_begin_info.contents = contents;
2780 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
2781 return skip;
2782}
2783
2784bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2785 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2786 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2787 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
2788 return skip;
2789}
2790
2791bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2792 const VkRenderPassBeginInfo *pRenderPassBegin,
2793 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2794 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2795 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
2796 return skip;
2797}
2798
John Zulauf3d84f1b2020-03-09 13:33:25 -06002799void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
2800 VkResult result) {
2801 // The state tracker sets up the command buffer state
2802 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
2803
2804 // Create/initialize the structure that trackers accesses at the command buffer scope.
2805 auto cb_access_context = GetAccessContext(commandBuffer);
2806 assert(cb_access_context);
2807 cb_access_context->Reset();
2808}
2809
2810void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06002811 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002812 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002813 if (cb_context) {
2814 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002815 }
2816}
2817
2818void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2819 VkSubpassContents contents) {
2820 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2821 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2822 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002823 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002824}
2825
2826void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2827 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2828 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002829 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002830}
2831
2832void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2833 const VkRenderPassBeginInfo *pRenderPassBegin,
2834 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2835 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002836 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
2837}
2838
2839bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2840 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
2841 bool skip = false;
2842
2843 auto cb_context = GetAccessContext(commandBuffer);
2844 assert(cb_context);
2845 auto cb_state = cb_context->GetCommandBufferState();
2846 if (!cb_state) return skip;
2847
2848 auto rp_state = cb_state->activeRenderPass;
2849 if (!rp_state) return skip;
2850
2851 skip |= cb_context->ValidateNextSubpass(func_name);
2852
2853 return skip;
2854}
2855
2856bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
2857 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
2858 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2859 subpass_begin_info.contents = contents;
2860 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
2861 return skip;
2862}
2863
2864bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2865 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2866 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2867 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
2868 return skip;
2869}
2870
2871bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2872 const VkSubpassEndInfo *pSubpassEndInfo) const {
2873 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2874 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
2875 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002876}
2877
2878void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06002879 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002880 auto cb_context = GetAccessContext(commandBuffer);
2881 assert(cb_context);
2882 auto cb_state = cb_context->GetCommandBufferState();
2883 if (!cb_state) return;
2884
2885 auto rp_state = cb_state->activeRenderPass;
2886 if (!rp_state) return;
2887
John Zulauf355e49b2020-04-24 15:11:15 -06002888 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002889}
2890
2891void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
2892 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
2893 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2894 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002895 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002896}
2897
2898void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2899 const VkSubpassEndInfo *pSubpassEndInfo) {
2900 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002901 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002902}
2903
2904void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2905 const VkSubpassEndInfo *pSubpassEndInfo) {
2906 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002907 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002908}
2909
John Zulauf355e49b2020-04-24 15:11:15 -06002910bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
2911 const char *func_name) const {
2912 bool skip = false;
2913
2914 auto cb_context = GetAccessContext(commandBuffer);
2915 assert(cb_context);
2916 auto cb_state = cb_context->GetCommandBufferState();
2917 if (!cb_state) return skip;
2918
2919 auto rp_state = cb_state->activeRenderPass;
2920 if (!rp_state) return skip;
2921
2922 skip |= cb_context->ValidateEndRenderpass(func_name);
2923 return skip;
2924}
2925
2926bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2927 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
2928 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
2929 return skip;
2930}
2931
2932bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
2933 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2934 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2935 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
2936 return skip;
2937}
2938
2939bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
2940 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2941 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2942 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
2943 return skip;
2944}
2945
2946void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
2947 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06002948 // Resolve the all subpass contexts to the command buffer contexts
2949 auto cb_context = GetAccessContext(commandBuffer);
2950 assert(cb_context);
2951 auto cb_state = cb_context->GetCommandBufferState();
2952 if (!cb_state) return;
2953
locke-lunargaecf2152020-05-12 17:15:41 -06002954 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06002955 if (!rp_state) return;
2956
John Zulauf355e49b2020-04-24 15:11:15 -06002957 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06002958}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002959
John Zulauf33fc1d52020-07-17 11:01:10 -06002960// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
2961// updates to a resource which do not conflict at the byte level.
2962// TODO: Revisit this rule to see if it needs to be tighter or looser
2963// TODO: Add programatic control over suppression heuristics
2964bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
2965 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
2966}
2967
John Zulauf3d84f1b2020-03-09 13:33:25 -06002968void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002969 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06002970 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002971}
2972
2973void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06002974 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06002975 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002976}
2977
2978void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06002979 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06002980 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002981}
locke-lunarga19c71d2020-03-02 18:17:04 -07002982
2983bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2984 VkImageLayout dstImageLayout, uint32_t regionCount,
2985 const VkBufferImageCopy *pRegions) const {
2986 bool skip = false;
2987 const auto *cb_access_context = GetAccessContext(commandBuffer);
2988 assert(cb_access_context);
2989 if (!cb_access_context) return skip;
2990
2991 const auto *context = cb_access_context->GetCurrentAccessContext();
2992 assert(context);
2993 if (!context) return skip;
2994
2995 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07002996 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2997
2998 for (uint32_t region = 0; region < regionCount; region++) {
2999 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003000 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003001 ResourceAccessRange src_range =
3002 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003003 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003004 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003005 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003006 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003007 "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003008 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003009 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003010 }
3011 }
3012 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003013 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003014 copy_region.imageOffset, copy_region.imageExtent);
3015 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003016 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003017 "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003018 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003019 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003020 }
3021 if (skip) break;
3022 }
3023 if (skip) break;
3024 }
3025 return skip;
3026}
3027
3028void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3029 VkImageLayout dstImageLayout, uint32_t regionCount,
3030 const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003031 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07003032 auto *cb_access_context = GetAccessContext(commandBuffer);
3033 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003034 const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07003035 auto *context = cb_access_context->GetCurrentAccessContext();
3036 assert(context);
3037
3038 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003039 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003040
3041 for (uint32_t region = 0; region < regionCount; region++) {
3042 const auto &copy_region = pRegions[region];
3043 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003044 ResourceAccessRange src_range =
3045 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003046 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003047 }
3048 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003049 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003050 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003051 }
3052 }
3053}
3054
3055bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3056 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3057 const VkBufferImageCopy *pRegions) const {
3058 bool skip = false;
3059 const auto *cb_access_context = GetAccessContext(commandBuffer);
3060 assert(cb_access_context);
3061 if (!cb_access_context) return skip;
3062
3063 const auto *context = cb_access_context->GetCurrentAccessContext();
3064 assert(context);
3065 if (!context) return skip;
3066
3067 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3068 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3069 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3070 for (uint32_t region = 0; region < regionCount; region++) {
3071 const auto &copy_region = pRegions[region];
3072 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003073 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003074 copy_region.imageOffset, copy_region.imageExtent);
3075 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003076 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003077 "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003078 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003079 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003080 }
3081 }
3082 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06003083 ResourceAccessRange dst_range =
3084 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003085 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003086 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003087 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003088 "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003089 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003090 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003091 }
3092 }
3093 if (skip) break;
3094 }
3095 return skip;
3096}
3097
3098void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3099 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003100 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07003101 auto *cb_access_context = GetAccessContext(commandBuffer);
3102 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003103 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER);
locke-lunarga19c71d2020-03-02 18:17:04 -07003104 auto *context = cb_access_context->GetCurrentAccessContext();
3105 assert(context);
3106
3107 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003108 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3109 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 -06003110 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003111
3112 for (uint32_t region = 0; region < regionCount; region++) {
3113 const auto &copy_region = pRegions[region];
3114 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003115 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003116 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003117 }
3118 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003119 ResourceAccessRange dst_range =
3120 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003121 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003122 }
3123 }
3124}
3125
3126bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3127 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3128 const VkImageBlit *pRegions, VkFilter filter) const {
3129 bool skip = false;
3130 const auto *cb_access_context = GetAccessContext(commandBuffer);
3131 assert(cb_access_context);
3132 if (!cb_access_context) return skip;
3133
3134 const auto *context = cb_access_context->GetCurrentAccessContext();
3135 assert(context);
3136 if (!context) return skip;
3137
3138 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3139 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3140
3141 for (uint32_t region = 0; region < regionCount; region++) {
3142 const auto &blit_region = pRegions[region];
3143 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003144 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3145 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3146 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3147 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3148 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3149 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3150 auto hazard =
3151 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003152 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003153 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003154 "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003155 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003156 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003157 }
3158 }
3159
3160 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003161 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3162 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3163 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3164 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3165 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3166 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3167 auto hazard =
3168 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003169 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003170 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003171 "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003172 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003173 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003174 }
3175 if (skip) break;
3176 }
3177 }
3178
3179 return skip;
3180}
3181
3182void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3183 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3184 const VkImageBlit *pRegions, VkFilter filter) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003185 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3186 pRegions, filter);
locke-lunarga19c71d2020-03-02 18:17:04 -07003187 auto *cb_access_context = GetAccessContext(commandBuffer);
3188 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003189 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07003190 auto *context = cb_access_context->GetCurrentAccessContext();
3191 assert(context);
3192
3193 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003194 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003195
3196 for (uint32_t region = 0; region < regionCount; region++) {
3197 const auto &blit_region = pRegions[region];
3198 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003199 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3200 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3201 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3202 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3203 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3204 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3205 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003206 }
3207 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003208 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3209 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3210 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3211 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3212 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3213 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3214 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003215 }
3216 }
3217}
locke-lunarg36ba2592020-04-03 09:42:04 -06003218
locke-lunarg61870c22020-06-09 14:51:50 -06003219bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
3220 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
3221 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003222 bool skip = false;
3223 if (drawCount == 0) return skip;
3224
3225 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3226 VkDeviceSize size = struct_size;
3227 if (drawCount == 1 || stride == size) {
3228 if (drawCount > 1) size *= drawCount;
3229 ResourceAccessRange range = MakeRange(offset, size);
3230 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3231 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003232 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003233 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003234 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003235 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003236 }
3237 } else {
3238 for (uint32_t i = 0; i < drawCount; ++i) {
3239 ResourceAccessRange range = MakeRange(offset + i * stride, size);
3240 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3241 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003242 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003243 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3244 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3245 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003246 break;
3247 }
3248 }
3249 }
3250 return skip;
3251}
3252
locke-lunarg61870c22020-06-09 14:51:50 -06003253void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3254 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3255 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003256 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3257 VkDeviceSize size = struct_size;
3258 if (drawCount == 1 || stride == size) {
3259 if (drawCount > 1) size *= drawCount;
3260 ResourceAccessRange range = MakeRange(offset, size);
3261 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3262 } else {
3263 for (uint32_t i = 0; i < drawCount; ++i) {
3264 ResourceAccessRange range = MakeRange(offset + i * stride, size);
3265 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3266 }
3267 }
3268}
3269
locke-lunarg61870c22020-06-09 14:51:50 -06003270bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3271 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003272 bool skip = false;
3273
3274 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3275 ResourceAccessRange range = MakeRange(offset, 4);
3276 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3277 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003278 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003279 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003280 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003281 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003282 }
3283 return skip;
3284}
3285
locke-lunarg61870c22020-06-09 14:51:50 -06003286void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003287 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3288 ResourceAccessRange range = MakeRange(offset, 4);
3289 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3290}
3291
locke-lunarg36ba2592020-04-03 09:42:04 -06003292bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003293 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003294 const auto *cb_access_context = GetAccessContext(commandBuffer);
3295 assert(cb_access_context);
3296 if (!cb_access_context) return skip;
3297
locke-lunarg61870c22020-06-09 14:51:50 -06003298 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003299 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003300}
3301
3302void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003303 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003304 auto *cb_access_context = GetAccessContext(commandBuffer);
3305 assert(cb_access_context);
3306 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003307
locke-lunarg61870c22020-06-09 14:51:50 -06003308 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003309}
locke-lunarge1a67022020-04-29 00:15:36 -06003310
3311bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003312 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003313 const auto *cb_access_context = GetAccessContext(commandBuffer);
3314 assert(cb_access_context);
3315 if (!cb_access_context) return skip;
3316
3317 const auto *context = cb_access_context->GetCurrentAccessContext();
3318 assert(context);
3319 if (!context) return skip;
3320
locke-lunarg61870c22020-06-09 14:51:50 -06003321 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3322 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3323 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003324 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003325}
3326
3327void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003328 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003329 auto *cb_access_context = GetAccessContext(commandBuffer);
3330 assert(cb_access_context);
3331 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3332 auto *context = cb_access_context->GetCurrentAccessContext();
3333 assert(context);
3334
locke-lunarg61870c22020-06-09 14:51:50 -06003335 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3336 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003337}
3338
3339bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3340 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003341 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003342 const auto *cb_access_context = GetAccessContext(commandBuffer);
3343 assert(cb_access_context);
3344 if (!cb_access_context) return skip;
3345
locke-lunarg61870c22020-06-09 14:51:50 -06003346 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3347 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3348 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003349 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003350}
3351
3352void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3353 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003354 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003355 auto *cb_access_context = GetAccessContext(commandBuffer);
3356 assert(cb_access_context);
3357 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003358
locke-lunarg61870c22020-06-09 14:51:50 -06003359 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3360 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3361 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003362}
3363
3364bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3365 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003366 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003367 const auto *cb_access_context = GetAccessContext(commandBuffer);
3368 assert(cb_access_context);
3369 if (!cb_access_context) return skip;
3370
locke-lunarg61870c22020-06-09 14:51:50 -06003371 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3372 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3373 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003374 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003375}
3376
3377void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3378 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003379 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003380 auto *cb_access_context = GetAccessContext(commandBuffer);
3381 assert(cb_access_context);
3382 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003383
locke-lunarg61870c22020-06-09 14:51:50 -06003384 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3385 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3386 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003387}
3388
3389bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3390 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003391 bool skip = false;
3392 if (drawCount == 0) return skip;
3393
locke-lunargff255f92020-05-13 18:53:52 -06003394 const auto *cb_access_context = GetAccessContext(commandBuffer);
3395 assert(cb_access_context);
3396 if (!cb_access_context) return skip;
3397
3398 const auto *context = cb_access_context->GetCurrentAccessContext();
3399 assert(context);
3400 if (!context) return skip;
3401
locke-lunarg61870c22020-06-09 14:51:50 -06003402 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3403 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3404 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3405 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003406
3407 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3408 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3409 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003410 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003411 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003412}
3413
3414void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3415 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003416 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003417 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003418 auto *cb_access_context = GetAccessContext(commandBuffer);
3419 assert(cb_access_context);
3420 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3421 auto *context = cb_access_context->GetCurrentAccessContext();
3422 assert(context);
3423
locke-lunarg61870c22020-06-09 14:51:50 -06003424 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3425 cb_access_context->RecordDrawSubpassAttachment(tag);
3426 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003427
3428 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3429 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3430 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003431 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003432}
3433
3434bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3435 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003436 bool skip = false;
3437 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003438 const auto *cb_access_context = GetAccessContext(commandBuffer);
3439 assert(cb_access_context);
3440 if (!cb_access_context) return skip;
3441
3442 const auto *context = cb_access_context->GetCurrentAccessContext();
3443 assert(context);
3444 if (!context) return skip;
3445
locke-lunarg61870c22020-06-09 14:51:50 -06003446 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3447 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3448 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3449 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003450
3451 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3452 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3453 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003454 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003455 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003456}
3457
3458void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3459 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003460 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, 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_DRAWINDEXEDINDIRECT);
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(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003470
3471 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3472 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3473 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003474 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003475}
3476
3477bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3478 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3479 uint32_t stride, const char *function) const {
3480 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003481 const auto *cb_access_context = GetAccessContext(commandBuffer);
3482 assert(cb_access_context);
3483 if (!cb_access_context) return skip;
3484
3485 const auto *context = cb_access_context->GetCurrentAccessContext();
3486 assert(context);
3487 if (!context) return skip;
3488
locke-lunarg61870c22020-06-09 14:51:50 -06003489 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3490 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3491 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3492 function);
3493 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003494
3495 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3496 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3497 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003498 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003499 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003500}
3501
3502bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3503 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3504 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003505 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3506 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003507}
3508
3509void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3510 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3511 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003512 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3513 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003514 auto *cb_access_context = GetAccessContext(commandBuffer);
3515 assert(cb_access_context);
3516 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3517 auto *context = cb_access_context->GetCurrentAccessContext();
3518 assert(context);
3519
locke-lunarg61870c22020-06-09 14:51:50 -06003520 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3521 cb_access_context->RecordDrawSubpassAttachment(tag);
3522 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3523 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003524
3525 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3526 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3527 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003528 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003529}
3530
3531bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3532 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3533 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003534 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3535 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003536}
3537
3538void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3539 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3540 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003541 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3542 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003543 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003544}
3545
3546bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3547 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3548 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003549 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3550 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003551}
3552
3553void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3554 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3555 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003556 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3557 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003558 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3559}
3560
3561bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3562 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3563 uint32_t stride, const char *function) const {
3564 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003565 const auto *cb_access_context = GetAccessContext(commandBuffer);
3566 assert(cb_access_context);
3567 if (!cb_access_context) return skip;
3568
3569 const auto *context = cb_access_context->GetCurrentAccessContext();
3570 assert(context);
3571 if (!context) return skip;
3572
locke-lunarg61870c22020-06-09 14:51:50 -06003573 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3574 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3575 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3576 stride, function);
3577 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003578
3579 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3580 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3581 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003582 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003583 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003584}
3585
3586bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3587 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3588 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003589 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3590 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003591}
3592
3593void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3594 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3595 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003596 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3597 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003598 auto *cb_access_context = GetAccessContext(commandBuffer);
3599 assert(cb_access_context);
3600 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3601 auto *context = cb_access_context->GetCurrentAccessContext();
3602 assert(context);
3603
locke-lunarg61870c22020-06-09 14:51:50 -06003604 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3605 cb_access_context->RecordDrawSubpassAttachment(tag);
3606 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3607 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003608
3609 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3610 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06003611 // We will update the index and vertex buffer in SubmitQueue in the future.
3612 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003613}
3614
3615bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3616 VkDeviceSize offset, VkBuffer countBuffer,
3617 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3618 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003619 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3620 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003621}
3622
3623void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3624 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3625 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003626 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3627 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003628 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3629}
3630
3631bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3632 VkDeviceSize offset, VkBuffer countBuffer,
3633 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3634 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003635 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3636 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003637}
3638
3639void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3640 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3641 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003642 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3643 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003644 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3645}
3646
3647bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3648 const VkClearColorValue *pColor, uint32_t rangeCount,
3649 const VkImageSubresourceRange *pRanges) const {
3650 bool skip = false;
3651 const auto *cb_access_context = GetAccessContext(commandBuffer);
3652 assert(cb_access_context);
3653 if (!cb_access_context) return skip;
3654
3655 const auto *context = cb_access_context->GetCurrentAccessContext();
3656 assert(context);
3657 if (!context) return skip;
3658
3659 const auto *image_state = Get<IMAGE_STATE>(image);
3660
3661 for (uint32_t index = 0; index < rangeCount; index++) {
3662 const auto &range = pRanges[index];
3663 if (image_state) {
3664 auto hazard =
3665 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3666 if (hazard.hazard) {
3667 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003668 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003669 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06003670 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003671 }
3672 }
3673 }
3674 return skip;
3675}
3676
3677void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3678 const VkClearColorValue *pColor, uint32_t rangeCount,
3679 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003680 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003681 auto *cb_access_context = GetAccessContext(commandBuffer);
3682 assert(cb_access_context);
3683 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
3684 auto *context = cb_access_context->GetCurrentAccessContext();
3685 assert(context);
3686
3687 const auto *image_state = Get<IMAGE_STATE>(image);
3688
3689 for (uint32_t index = 0; index < rangeCount; index++) {
3690 const auto &range = pRanges[index];
3691 if (image_state) {
3692 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3693 tag);
3694 }
3695 }
3696}
3697
3698bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3699 VkImageLayout imageLayout,
3700 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3701 const VkImageSubresourceRange *pRanges) const {
3702 bool skip = false;
3703 const auto *cb_access_context = GetAccessContext(commandBuffer);
3704 assert(cb_access_context);
3705 if (!cb_access_context) return skip;
3706
3707 const auto *context = cb_access_context->GetCurrentAccessContext();
3708 assert(context);
3709 if (!context) return skip;
3710
3711 const auto *image_state = Get<IMAGE_STATE>(image);
3712
3713 for (uint32_t index = 0; index < rangeCount; index++) {
3714 const auto &range = pRanges[index];
3715 if (image_state) {
3716 auto hazard =
3717 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3718 if (hazard.hazard) {
3719 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003720 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003721 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06003722 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003723 }
3724 }
3725 }
3726 return skip;
3727}
3728
3729void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3730 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3731 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003732 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003733 auto *cb_access_context = GetAccessContext(commandBuffer);
3734 assert(cb_access_context);
3735 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
3736 auto *context = cb_access_context->GetCurrentAccessContext();
3737 assert(context);
3738
3739 const auto *image_state = Get<IMAGE_STATE>(image);
3740
3741 for (uint32_t index = 0; index < rangeCount; index++) {
3742 const auto &range = pRanges[index];
3743 if (image_state) {
3744 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3745 tag);
3746 }
3747 }
3748}
3749
3750bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
3751 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
3752 VkDeviceSize dstOffset, VkDeviceSize stride,
3753 VkQueryResultFlags flags) const {
3754 bool skip = false;
3755 const auto *cb_access_context = GetAccessContext(commandBuffer);
3756 assert(cb_access_context);
3757 if (!cb_access_context) return skip;
3758
3759 const auto *context = cb_access_context->GetCurrentAccessContext();
3760 assert(context);
3761 if (!context) return skip;
3762
3763 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3764
3765 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003766 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003767 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3768 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06003769 skip |=
3770 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3771 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
3772 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003773 }
3774 }
locke-lunargff255f92020-05-13 18:53:52 -06003775
3776 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003777 return skip;
3778}
3779
3780void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
3781 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3782 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003783 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
3784 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06003785 auto *cb_access_context = GetAccessContext(commandBuffer);
3786 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06003787 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06003788 auto *context = cb_access_context->GetCurrentAccessContext();
3789 assert(context);
3790
3791 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3792
3793 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003794 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003795 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3796 }
locke-lunargff255f92020-05-13 18:53:52 -06003797
3798 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003799}
3800
3801bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3802 VkDeviceSize size, uint32_t data) const {
3803 bool skip = false;
3804 const auto *cb_access_context = GetAccessContext(commandBuffer);
3805 assert(cb_access_context);
3806 if (!cb_access_context) return skip;
3807
3808 const auto *context = cb_access_context->GetCurrentAccessContext();
3809 assert(context);
3810 if (!context) return skip;
3811
3812 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3813
3814 if (dst_buffer) {
3815 ResourceAccessRange range = MakeRange(dstOffset, size);
3816 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3817 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003818 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003819 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06003820 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003821 }
3822 }
3823 return skip;
3824}
3825
3826void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3827 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003828 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06003829 auto *cb_access_context = GetAccessContext(commandBuffer);
3830 assert(cb_access_context);
3831 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
3832 auto *context = cb_access_context->GetCurrentAccessContext();
3833 assert(context);
3834
3835 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3836
3837 if (dst_buffer) {
3838 ResourceAccessRange range = MakeRange(dstOffset, size);
3839 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3840 }
3841}
3842
3843bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3844 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3845 const VkImageResolve *pRegions) const {
3846 bool skip = false;
3847 const auto *cb_access_context = GetAccessContext(commandBuffer);
3848 assert(cb_access_context);
3849 if (!cb_access_context) return skip;
3850
3851 const auto *context = cb_access_context->GetCurrentAccessContext();
3852 assert(context);
3853 if (!context) return skip;
3854
3855 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3856 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3857
3858 for (uint32_t region = 0; region < regionCount; region++) {
3859 const auto &resolve_region = pRegions[region];
3860 if (src_image) {
3861 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3862 resolve_region.srcOffset, resolve_region.extent);
3863 if (hazard.hazard) {
3864 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003865 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003866 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003867 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003868 }
3869 }
3870
3871 if (dst_image) {
3872 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3873 resolve_region.dstOffset, resolve_region.extent);
3874 if (hazard.hazard) {
3875 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003876 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003877 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003878 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003879 }
3880 if (skip) break;
3881 }
3882 }
3883
3884 return skip;
3885}
3886
3887void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3888 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3889 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003890 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3891 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06003892 auto *cb_access_context = GetAccessContext(commandBuffer);
3893 assert(cb_access_context);
3894 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
3895 auto *context = cb_access_context->GetCurrentAccessContext();
3896 assert(context);
3897
3898 auto *src_image = Get<IMAGE_STATE>(srcImage);
3899 auto *dst_image = Get<IMAGE_STATE>(dstImage);
3900
3901 for (uint32_t region = 0; region < regionCount; region++) {
3902 const auto &resolve_region = pRegions[region];
3903 if (src_image) {
3904 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3905 resolve_region.srcOffset, resolve_region.extent, tag);
3906 }
3907 if (dst_image) {
3908 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3909 resolve_region.dstOffset, resolve_region.extent, tag);
3910 }
3911 }
3912}
3913
3914bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3915 VkDeviceSize dataSize, const void *pData) const {
3916 bool skip = false;
3917 const auto *cb_access_context = GetAccessContext(commandBuffer);
3918 assert(cb_access_context);
3919 if (!cb_access_context) return skip;
3920
3921 const auto *context = cb_access_context->GetCurrentAccessContext();
3922 assert(context);
3923 if (!context) return skip;
3924
3925 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3926
3927 if (dst_buffer) {
3928 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3929 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3930 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003931 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003932 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06003933 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003934 }
3935 }
3936 return skip;
3937}
3938
3939void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3940 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003941 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06003942 auto *cb_access_context = GetAccessContext(commandBuffer);
3943 assert(cb_access_context);
3944 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
3945 auto *context = cb_access_context->GetCurrentAccessContext();
3946 assert(context);
3947
3948 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3949
3950 if (dst_buffer) {
3951 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3952 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3953 }
3954}
locke-lunargff255f92020-05-13 18:53:52 -06003955
3956bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3957 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
3958 bool skip = false;
3959 const auto *cb_access_context = GetAccessContext(commandBuffer);
3960 assert(cb_access_context);
3961 if (!cb_access_context) return skip;
3962
3963 const auto *context = cb_access_context->GetCurrentAccessContext();
3964 assert(context);
3965 if (!context) return skip;
3966
3967 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3968
3969 if (dst_buffer) {
3970 ResourceAccessRange range = MakeRange(dstOffset, 4);
3971 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3972 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06003973 skip |=
3974 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3975 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
3976 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003977 }
3978 }
3979 return skip;
3980}
3981
3982void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3983 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003984 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06003985 auto *cb_access_context = GetAccessContext(commandBuffer);
3986 assert(cb_access_context);
3987 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
3988 auto *context = cb_access_context->GetCurrentAccessContext();
3989 assert(context);
3990
3991 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3992
3993 if (dst_buffer) {
3994 ResourceAccessRange range = MakeRange(dstOffset, 4);
3995 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3996 }
3997}