blob: 0a758412f955d4535565a8d42cffd11313b74903 [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;
John Zulauf389c34b2020-07-28 11:19:35 -0600132 if (0 == flags) {
133 out_str = "0";
134 }
John Zulauf59e25072020-07-17 10:55:21 -0600135 while (flags) {
136 const auto &info = syncStageAccessInfoByStageAccessIndex[index];
137 if (flags & info.stage_access_bit) {
138 if (!out_str.empty()) {
139 out_str.append(sep);
140 }
141 out_str.append(info.name);
142 flags = flags & ~info.stage_access_bit;
143 }
144 index++;
145 assert(index < syncStageAccessInfoByStageAccessIndex.size());
146 }
147 if (out_str.length() == 0) {
148 out_str.append("Unhandled SyncStageAccess");
149 }
150 return out_str;
151}
152
John Zulauf37ceaed2020-07-03 16:18:15 -0600153static std::string string_UsageTag(const HazardResult &hazard) {
154 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600155 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
156 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600157 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600158 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
159 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf59e25072020-07-17 10:55:21 -0600160 out << "(usage: " << usage_info.name << ", prior_usage: " << stage_access_name;
161 if (IsHazardVsRead(hazard.hazard)) {
162 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
163 out << ", read_barriers: " << string_VkPipelineStageFlags(barriers);
164 } else {
165 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
166 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
167 }
168
169 out << ", command: " << CommandTypeString(tag.command);
170 out << ", seq_no: " << (tag.index & 0xFFFFFFFF) << ", reset_no: " << (tag.index >> 32) << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600171 return out.str();
172}
173
John Zulaufd14743a2020-07-03 09:42:39 -0600174// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
175// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
176// also reflects this special case for read hazard detection (using access instead of exec scope)
John Zulaufb027cdb2020-05-21 14:25:22 -0600177static constexpr VkPipelineStageFlags kColorAttachmentExecScope = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
178static constexpr SyncStageAccessFlags kColorAttachmentAccessScope =
179 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
180 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
John Zulaufd14743a2020-07-03 09:42:39 -0600181 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
182 SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600183static constexpr VkPipelineStageFlags kDepthStencilAttachmentExecScope =
184 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
185static constexpr SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
186 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
187 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
188 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
John Zulaufd14743a2020-07-03 09:42:39 -0600189 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
190 SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600191
192static constexpr SyncOrderingBarrier kColorAttachmentRasterOrder = {kColorAttachmentExecScope, kColorAttachmentAccessScope};
193static constexpr SyncOrderingBarrier kDepthStencilAttachmentRasterOrder = {kDepthStencilAttachmentExecScope,
194 kDepthStencilAttachmentAccessScope};
195static constexpr SyncOrderingBarrier kAttachmentRasterOrder = {kDepthStencilAttachmentExecScope | kColorAttachmentExecScope,
196 kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope};
John Zulauf7635de32020-05-29 17:14:15 -0600197// 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 -0600198static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600199
locke-lunarg3c038002020-04-30 23:08:08 -0600200inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
201 if (size == VK_WHOLE_SIZE) {
202 return (whole_size - offset);
203 }
204 return size;
205}
206
John Zulauf16adfc92020-04-08 10:28:33 -0600207template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600208static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600209 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
210}
211
John Zulauf355e49b2020-04-24 15:11:15 -0600212static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600213
John Zulauf0cb5be22020-01-23 12:18:22 -0700214// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
215VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
216 VkPipelineStageFlags expanded = stage_mask;
217 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
218 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
219 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
220 if (all_commands.first & queue_flags) {
221 expanded |= all_commands.second;
222 }
223 }
224 }
225 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
226 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
227 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
228 }
229 return expanded;
230}
231
John Zulauf36bcf6a2020-02-03 15:12:52 -0700232VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
233 std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
234 VkPipelineStageFlags unscanned = stage_mask;
235 VkPipelineStageFlags related = 0;
Jonah Ryan-Davis185189c2020-07-14 10:28:52 -0400236 for (const auto &entry : map) {
237 const auto &stage = entry.first;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700238 if (stage & unscanned) {
239 related = related | entry.second;
240 unscanned = unscanned & ~stage;
241 if (!unscanned) break;
242 }
243 }
244 return related;
245}
246
247VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
248 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
249}
250
251VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
252 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
253}
254
John Zulauf5c5e88d2019-12-26 11:22:02 -0700255static const ResourceAccessRange full_range(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700256
locke-lunargff255f92020-05-13 18:53:52 -0600257void GetBufferRange(VkDeviceSize &range_start, VkDeviceSize &range_size, VkDeviceSize offset, VkDeviceSize buf_whole_size,
258 uint32_t first_index, uint32_t count, VkDeviceSize stride) {
259 range_start = offset + first_index * stride;
260 range_size = 0;
261 if (count == UINT32_MAX) {
262 range_size = buf_whole_size - range_start;
263 } else {
264 range_size = count * stride;
265 }
266}
267
locke-lunarg654e3692020-06-04 17:19:15 -0600268SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
269 VkShaderStageFlagBits stage_flag) {
270 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
271 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
272 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
273 }
274 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
275 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
276 assert(0);
277 }
278 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
279 return stage_access->second.uniform_read;
280 }
281
282 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
283 // Because if write hazard happens, read hazard might or might not happen.
284 // But if write hazard doesn't happen, read hazard is impossible to happen.
285 if (descriptor_data.is_writable) {
286 return stage_access->second.shader_write;
287 }
288 return stage_access->second.shader_read;
289}
290
locke-lunarg37047832020-06-12 13:44:45 -0600291bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
292 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
293 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
294 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
295 ? true
296 : false;
297}
298
299bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
300 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
301 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
302 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
303 ? true
304 : false;
305}
306
John Zulauf355e49b2020-04-24 15:11:15 -0600307// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
308const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = {
309 AccessContext::AddressType::kLinearAddress, AccessContext::AddressType::kIdealizedAddress};
310
John Zulauf7635de32020-05-29 17:14:15 -0600311// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
312// Used by both validation and record operations
313//
314// The signature for Action() reflect the needs of both uses.
315template <typename Action>
316void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
317 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
318 VkExtent3D extent = CastTo3D(render_area.extent);
319 VkOffset3D offset = CastTo3D(render_area.offset);
320 const auto &rp_ci = rp_state.createInfo;
321 const auto *attachment_ci = rp_ci.pAttachments;
322 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
323
324 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
325 const auto *color_attachments = subpass_ci.pColorAttachments;
326 const auto *color_resolve = subpass_ci.pResolveAttachments;
327 if (color_resolve && color_attachments) {
328 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
329 const auto &color_attach = color_attachments[i].attachment;
330 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
331 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
332 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
333 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
334 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
335 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
336 }
337 }
338 }
339
340 // Depth stencil resolve only if the extension is present
341 const auto ds_resolve = lvl_find_in_chain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
342 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
343 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
344 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
345 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
346 const auto src_ci = attachment_ci[src_at];
347 // The formats are required to match so we can pick either
348 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
349 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
350 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
351 VkImageAspectFlags aspect_mask = 0u;
352
353 // Figure out which aspects are actually touched during resolve operations
354 const char *aspect_string = nullptr;
355 if (resolve_depth && resolve_stencil) {
356 // Validate all aspects together
357 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
358 aspect_string = "depth/stencil";
359 } else if (resolve_depth) {
360 // Validate depth only
361 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
362 aspect_string = "depth";
363 } else if (resolve_stencil) {
364 // Validate all stencil only
365 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
366 aspect_string = "stencil";
367 }
368
369 if (aspect_mask) {
370 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
371 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kDepthStencilAttachmentRasterOrder, offset, extent,
372 aspect_mask);
373 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
374 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
375 }
376 }
377}
378
379// Action for validating resolve operations
380class ValidateResolveAction {
381 public:
382 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
383 const char *func_name)
384 : render_pass_(render_pass),
385 subpass_(subpass),
386 context_(context),
387 sync_state_(sync_state),
388 func_name_(func_name),
389 skip_(false) {}
390 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
391 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
392 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
393 HazardResult hazard;
394 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
395 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600396 skip_ |= sync_state_.LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
397 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600398 " to resolve attachment %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -0600399 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name,
John Zulauf37ceaed2020-07-03 16:18:15 -0600400 src_at, dst_at, string_UsageTag(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600401 }
402 }
403 // Providing a mechanism for the constructing caller to get the result of the validation
404 bool GetSkip() const { return skip_; }
405
406 private:
407 VkRenderPass render_pass_;
408 const uint32_t subpass_;
409 const AccessContext &context_;
410 const SyncValidator &sync_state_;
411 const char *func_name_;
412 bool skip_;
413};
414
415// Update action for resolve operations
416class UpdateStateResolveAction {
417 public:
418 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
419 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
420 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
421 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
422 // Ignores validation only arguments...
423 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
424 }
425
426 private:
427 AccessContext &context_;
428 const ResourceUsageTag &tag_;
429};
430
John Zulauf59e25072020-07-17 10:55:21 -0600431void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
432 SyncStageAccessFlags prior_, const ResourceUsageTag &tag_) {
433 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
434 usage_index = usage_index_;
435 hazard = hazard_;
436 prior_access = prior_;
437 tag = tag_;
438}
439
John Zulauf540266b2020-04-06 18:54:53 -0600440AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
441 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600442 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600443 Reset();
444 const auto &subpass_dep = dependencies[subpass];
445 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600446 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600447 for (const auto &prev_dep : subpass_dep.prev) {
448 assert(prev_dep.dependency);
449 const auto dep = *prev_dep.dependency;
John Zulauf540266b2020-04-06 18:54:53 -0600450 prev_.emplace_back(const_cast<AccessContext *>(&contexts[dep.srcSubpass]), queue_flags, dep);
John Zulauf355e49b2020-04-24 15:11:15 -0600451 prev_by_subpass_[dep.srcSubpass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700452 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600453
454 async_.reserve(subpass_dep.async.size());
455 for (const auto async_subpass : subpass_dep.async) {
John Zulauf540266b2020-04-06 18:54:53 -0600456 async_.emplace_back(const_cast<AccessContext *>(&contexts[async_subpass]));
John Zulauf3d84f1b2020-03-09 13:33:25 -0600457 }
John Zulaufe5da6e52020-03-18 15:32:18 -0600458 if (subpass_dep.barrier_from_external) {
459 src_external_ = TrackBack(external_context, queue_flags, *subpass_dep.barrier_from_external);
460 } else {
461 src_external_ = TrackBack();
462 }
463 if (subpass_dep.barrier_to_external) {
464 dst_external_ = TrackBack(this, queue_flags, *subpass_dep.barrier_to_external);
465 } else {
466 dst_external_ = TrackBack();
John Zulauf3d84f1b2020-03-09 13:33:25 -0600467 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700468}
469
John Zulauf5f13a792020-03-10 07:31:21 -0600470template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600471HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600472 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600473 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600474 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600475
476 HazardResult hazard;
477 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
478 hazard = detector.Detect(prev);
479 }
480 return hazard;
481}
482
John Zulauf3d84f1b2020-03-09 13:33:25 -0600483// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
484// the DAG of the contexts (for example subpasses)
485template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600486HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
487 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600488 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600489
John Zulauf1a224292020-06-30 14:52:13 -0600490 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600491 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
492 // so we'll check these first
493 for (const auto &async_context : async_) {
494 hazard = async_context->DetectAsyncHazard(type, detector, range);
495 if (hazard.hazard) return hazard;
496 }
John Zulauf5f13a792020-03-10 07:31:21 -0600497 }
498
John Zulauf1a224292020-06-30 14:52:13 -0600499 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600500
John Zulauf69133422020-05-20 14:55:53 -0600501 const auto &accesses = GetAccessStateMap(type);
502 const auto from = accesses.lower_bound(range);
503 const auto to = accesses.upper_bound(range);
504 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600505
John Zulauf69133422020-05-20 14:55:53 -0600506 for (auto pos = from; pos != to; ++pos) {
507 // Cover any leading gap, or gap between entries
508 if (detect_prev) {
509 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
510 // Cover any leading gap, or gap between entries
511 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600512 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600513 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600514 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600515 if (hazard.hazard) return hazard;
516 }
John Zulauf69133422020-05-20 14:55:53 -0600517 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
518 gap.begin = pos->first.end;
519 }
520
521 hazard = detector.Detect(pos);
522 if (hazard.hazard) return hazard;
523 }
524
525 if (detect_prev) {
526 // Detect in the trailing empty as needed
527 gap.end = range.end;
528 if (gap.non_empty()) {
529 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600530 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600531 }
532
533 return hazard;
534}
535
536// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
537template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600538HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600539 auto &accesses = GetAccessStateMap(type);
540 const auto from = accesses.lower_bound(range);
541 const auto to = accesses.upper_bound(range);
542
John Zulauf3d84f1b2020-03-09 13:33:25 -0600543 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600544 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
545 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600546 }
John Zulauf16adfc92020-04-08 10:28:33 -0600547
John Zulauf3d84f1b2020-03-09 13:33:25 -0600548 return hazard;
549}
550
John Zulauf355e49b2020-04-24 15:11:15 -0600551// Returns the last resolved entry
552static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
553 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
554 const SyncBarrier *barrier) {
555 auto at = entry;
556 for (auto pos = first; pos != last; ++pos) {
557 // Every member of the input iterator range must fit within the remaining portion of entry
558 assert(at->first.includes(pos->first));
559 assert(at != dest->end());
560 // Trim up at to the same size as the entry to resolve
561 at = sparse_container::split(at, *dest, pos->first);
562 auto access = pos->second;
563 if (barrier) {
564 access.ApplyBarrier(*barrier);
565 }
566 at->second.Resolve(access);
567 ++at; // Go to the remaining unused section of entry
568 }
569}
570
571void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, const SyncBarrier *barrier,
572 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
573 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600574 if (!range.non_empty()) return;
575
John Zulauf355e49b2020-04-24 15:11:15 -0600576 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
577 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600578 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600579 if (current->pos_B->valid) {
580 const auto &src_pos = current->pos_B->lower_bound;
John Zulauf355e49b2020-04-24 15:11:15 -0600581 auto access = src_pos->second;
582 if (barrier) {
583 access.ApplyBarrier(*barrier);
584 }
John Zulauf16adfc92020-04-08 10:28:33 -0600585 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600586 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
587 trimmed->second.Resolve(access);
588 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600589 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600590 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600591 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600592 }
John Zulauf16adfc92020-04-08 10:28:33 -0600593 } else {
594 // we have to descend to fill this gap
595 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600596 if (current->pos_A->valid) {
597 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
598 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600599 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulauf355e49b2020-04-24 15:11:15 -0600600 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier);
601 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600602 // There isn't anything in dest in current)range, so we can accumulate directly into it.
603 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulauf355e49b2020-04-24 15:11:15 -0600604 if (barrier) {
605 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
John Zulauf3bcab5e2020-06-19 14:42:32 -0600606 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
John Zulauf355e49b2020-04-24 15:11:15 -0600607 pos->second.ApplyBarrier(*barrier);
608 }
609 }
610 }
611 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
612 // iterator of the outer while.
613
614 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
615 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
616 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600617 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
618 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600619 current.seek(seek_to);
620 } else if (!current->pos_A->valid && infill_state) {
621 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
622 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
623 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600624 }
John Zulauf5f13a792020-03-10 07:31:21 -0600625 }
John Zulauf16adfc92020-04-08 10:28:33 -0600626 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600627 }
John Zulauf1a224292020-06-30 14:52:13 -0600628
629 // Infill if range goes passed both the current and resolve map prior contents
630 if (recur_to_infill && (current->range.end < range.end)) {
631 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
632 ResourceAccessRangeMap gap_map;
633 const auto the_end = resolve_map->end();
634 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
635 for (auto &access : gap_map) {
636 access.second.ApplyBarrier(*barrier);
637 resolve_map->insert(the_end, access);
638 }
639 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600640}
641
John Zulauf355e49b2020-04-24 15:11:15 -0600642void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
643 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600644 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600645 if (range.non_empty() && infill_state) {
646 descent_map->insert(std::make_pair(range, *infill_state));
647 }
648 } else {
649 // Look for something to fill the gap further along.
650 for (const auto &prev_dep : prev_) {
John Zulauf355e49b2020-04-24 15:11:15 -0600651 prev_dep.context->ResolveAccessRange(type, range, &prev_dep.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600652 }
653
John Zulaufe5da6e52020-03-18 15:32:18 -0600654 if (src_external_.context) {
John Zulauf355e49b2020-04-24 15:11:15 -0600655 src_external_.context->ResolveAccessRange(type, range, &src_external_.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600656 }
657 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600658}
659
John Zulauf16adfc92020-04-08 10:28:33 -0600660AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600661 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600662}
663
664VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) {
665 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
666}
667
John Zulauf355e49b2020-04-24 15:11:15 -0600668static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
John Zulauf16adfc92020-04-08 10:28:33 -0600669
John Zulauf1507ee42020-05-18 11:33:09 -0600670static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
671 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
672 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
673 return stage_access;
674}
675static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
676 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
677 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
678 return stage_access;
679}
680
John Zulauf7635de32020-05-29 17:14:15 -0600681// Caller must manage returned pointer
682static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
683 uint32_t subpass, const VkRect2D &render_area,
684 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
685 auto *proxy = new AccessContext(context);
686 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600687 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600688 return proxy;
689}
690
John Zulauf540266b2020-04-06 18:54:53 -0600691void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
John Zulauf355e49b2020-04-24 15:11:15 -0600692 AddressType address_type, ResourceAccessRangeMap *descent_map,
693 const ResourceAccessState *infill_state) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600694 if (!SimpleBinding(image_state)) return;
695
John Zulauf62f10592020-04-03 12:20:02 -0600696 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
locke-lunargae26eac2020-04-16 15:29:05 -0600697 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600698 image_state.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600699 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf62f10592020-04-03 12:20:02 -0600700 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600701 ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state);
John Zulauf62f10592020-04-03 12:20:02 -0600702 }
703}
704
John Zulauf7635de32020-05-29 17:14:15 -0600705// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600706bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600707 const VkRect2D &render_area, uint32_t subpass,
708 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
709 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600710 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600711 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
712 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
713 // those affects have not been recorded yet.
714 //
715 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
716 // to apply and only copy then, if this proves a hot spot.
717 std::unique_ptr<AccessContext> proxy_for_prev;
718 TrackBack proxy_track_back;
719
John Zulauf355e49b2020-04-24 15:11:15 -0600720 const auto &transitions = rp_state.subpass_transitions[subpass];
721 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600722 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
723
724 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
725 if (prev_needs_proxy) {
726 if (!proxy_for_prev) {
727 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
728 render_area, attachment_views));
729 proxy_track_back = *track_back;
730 proxy_track_back.context = proxy_for_prev.get();
731 }
732 track_back = &proxy_track_back;
733 }
734 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600735 if (hazard.hazard) {
John Zulauf389c34b2020-07-28 11:19:35 -0600736 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
737 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
738 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
739 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
740 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
741 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600742 }
743 }
744 return skip;
745}
746
John Zulauf1507ee42020-05-18 11:33:09 -0600747bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600748 const VkRect2D &render_area, uint32_t subpass,
749 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
750 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600751 bool skip = false;
752 const auto *attachment_ci = rp_state.createInfo.pAttachments;
753 VkExtent3D extent = CastTo3D(render_area.extent);
754 VkOffset3D offset = CastTo3D(render_area.offset);
755 const auto external_access_scope = src_external_.barrier.dst_access_scope;
John Zulauf1507ee42020-05-18 11:33:09 -0600756
757 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
758 if (subpass == rp_state.attachment_first_subpass[i]) {
759 if (attachment_views[i] == nullptr) continue;
760 const IMAGE_VIEW_STATE &view = *attachment_views[i];
761 const IMAGE_STATE *image = view.image_state.get();
762 if (image == nullptr) continue;
763 const auto &ci = attachment_ci[i];
764 const bool is_transition = rp_state.attachment_first_is_transition[i];
765
766 // Need check in the following way
767 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
768 // vs. transition
769 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
770 // for each aspect loaded.
771
772 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600773 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600774 const bool is_color = !(has_depth || has_stencil);
775
776 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
777 const SyncStageAccessFlags load_mask = (has_depth || is_color) ? SyncStageAccess::Flags(load_index) : 0U;
778 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
779 const SyncStageAccessFlags stencil_mask = has_stencil ? SyncStageAccess::Flags(stencil_load_index) : 0U;
780
John Zulaufaff20662020-06-01 14:07:58 -0600781 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600782 const char *aspect = nullptr;
783 if (is_transition) {
784 // For transition w
785 SyncHazard transition_hazard = SyncHazard::NONE;
786 bool checked_stencil = false;
787 if (load_mask) {
788 if ((load_mask & external_access_scope) != load_mask) {
789 transition_hazard =
790 SyncStageAccess::HasWrite(load_mask) ? SyncHazard::WRITE_AFTER_WRITE : SyncHazard::READ_AFTER_WRITE;
791 aspect = is_color ? "color" : "depth";
792 }
793 if (!transition_hazard && stencil_mask) {
794 if ((stencil_mask & external_access_scope) != stencil_mask) {
795 transition_hazard = SyncStageAccess::HasWrite(stencil_mask) ? SyncHazard::WRITE_AFTER_WRITE
796 : SyncHazard::READ_AFTER_WRITE;
797 aspect = "stencil";
798 checked_stencil = true;
799 }
800 }
801 }
802 if (transition_hazard) {
803 // Hazard vs. ILT
804 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
805 skip |=
locke-lunarg54379cf2020-08-05 12:25:29 -0600806 sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(transition_hazard),
John Zulauf1507ee42020-05-18 11:33:09 -0600807 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
808 " aspect %s during load with loadOp %s.",
809 func_name, string_SyncHazard(transition_hazard), subpass, i, aspect, load_op_string);
810 }
811 } else {
812 auto hazard_range = view.normalized_subresource_range;
813 bool checked_stencil = false;
814 if (is_color) {
815 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, offset, extent);
816 aspect = "color";
817 } else {
818 if (has_depth) {
819 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
820 hazard = DetectHazard(*image, load_index, hazard_range, offset, extent);
821 aspect = "depth";
822 }
823 if (!hazard.hazard && has_stencil) {
824 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
825 hazard = DetectHazard(*image, stencil_load_index, hazard_range, offset, extent);
826 aspect = "stencil";
827 checked_stencil = true;
828 }
829 }
830
831 if (hazard.hazard) {
832 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
833 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
834 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600835 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -0600836 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600837 string_UsageTag(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -0600838 }
839 }
840 }
841 }
842 return skip;
843}
844
John Zulaufaff20662020-06-01 14:07:58 -0600845// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
846// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
847// store is part of the same Next/End operation.
848// The latter is handled in layout transistion validation directly
849bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
850 const VkRect2D &render_area, uint32_t subpass,
851 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
852 const char *func_name) const {
853 bool skip = false;
854 const auto *attachment_ci = rp_state.createInfo.pAttachments;
855 VkExtent3D extent = CastTo3D(render_area.extent);
856 VkOffset3D offset = CastTo3D(render_area.offset);
857
858 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
859 if (subpass == rp_state.attachment_last_subpass[i]) {
860 if (attachment_views[i] == nullptr) continue;
861 const IMAGE_VIEW_STATE &view = *attachment_views[i];
862 const IMAGE_STATE *image = view.image_state.get();
863 if (image == nullptr) continue;
864 const auto &ci = attachment_ci[i];
865
866 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
867 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
868 // sake, we treat DONT_CARE as writing.
869 const bool has_depth = FormatHasDepth(ci.format);
870 const bool has_stencil = FormatHasStencil(ci.format);
871 const bool is_color = !(has_depth || has_stencil);
872 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
873 if (!has_stencil && !store_op_stores) continue;
874
875 HazardResult hazard;
876 const char *aspect = nullptr;
877 bool checked_stencil = false;
878 if (is_color) {
879 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
880 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
881 aspect = "color";
882 } else {
883 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
884 auto hazard_range = view.normalized_subresource_range;
885 if (has_depth && store_op_stores) {
886 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
887 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
888 kAttachmentRasterOrder, offset, extent);
889 aspect = "depth";
890 }
891 if (!hazard.hazard && has_stencil && stencil_op_stores) {
892 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
893 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
894 kAttachmentRasterOrder, offset, extent);
895 aspect = "stencil";
896 checked_stencil = true;
897 }
898 }
899
900 if (hazard.hazard) {
901 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
902 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -0600903 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
904 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600905 " %s aspect during store with %s %s. Access info %s",
John Zulauf1dae9192020-06-16 15:46:44 -0600906 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600907 store_op_string, string_UsageTag(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -0600908 }
909 }
910 }
911 return skip;
912}
913
John Zulaufb027cdb2020-05-21 14:25:22 -0600914bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
915 const VkRect2D &render_area,
916 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
917 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600918 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
919 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
920 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600921}
922
John Zulauf3d84f1b2020-03-09 13:33:25 -0600923class HazardDetector {
924 SyncStageAccessIndex usage_index_;
925
926 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600927 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600928 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
929 return pos->second.DetectAsyncHazard(usage_index_);
930 }
931 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
932};
933
John Zulauf69133422020-05-20 14:55:53 -0600934class HazardDetectorWithOrdering {
935 const SyncStageAccessIndex usage_index_;
936 const SyncOrderingBarrier &ordering_;
937
938 public:
939 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
940 return pos->second.DetectHazard(usage_index_, ordering_);
941 }
942 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
943 return pos->second.DetectAsyncHazard(usage_index_);
944 }
945 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
946 : usage_index_(usage), ordering_(ordering) {}
947};
948
John Zulauf16adfc92020-04-08 10:28:33 -0600949HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600950 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600951 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600952 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600953}
954
John Zulauf16adfc92020-04-08 10:28:33 -0600955HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600956 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600957 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -0600958 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -0600959}
960
John Zulauf69133422020-05-20 14:55:53 -0600961template <typename Detector>
962HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
963 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
964 const VkExtent3D &extent, DetectOptions options) const {
965 if (!SimpleBinding(image)) return HazardResult();
966 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
967 const auto address_type = ImageAddressType(image);
968 const auto base_address = ResourceBaseAddress(image);
969 for (; range_gen->non_empty(); ++range_gen) {
970 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
971 if (hazard.hazard) return hazard;
972 }
973 return HazardResult();
974}
975
John Zulauf540266b2020-04-06 18:54:53 -0600976HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
977 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
978 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700979 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
980 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -0600981 return DetectHazard(image, current_usage, subresource_range, offset, extent);
982}
983
984HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
985 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
986 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -0600987 HazardDetector detector(current_usage);
988 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
989}
990
991HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
992 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
993 const VkOffset3D &offset, const VkExtent3D &extent) const {
994 HazardDetectorWithOrdering detector(current_usage, ordering);
995 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -0600996}
997
John Zulaufb027cdb2020-05-21 14:25:22 -0600998// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
999// should have reported the issue regarding an invalid attachment entry
1000HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
1001 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
1002 VkImageAspectFlags aspect_mask) const {
1003 if (view != nullptr) {
1004 const IMAGE_STATE *image = view->image_state.get();
1005 if (image != nullptr) {
1006 auto *detect_range = &view->normalized_subresource_range;
1007 VkImageSubresourceRange masked_range;
1008 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1009 masked_range = view->normalized_subresource_range;
1010 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1011 detect_range = &masked_range;
1012 }
1013
1014 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1015 if (detect_range->aspectMask) {
1016 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
1017 }
1018 }
1019 }
1020 return HazardResult();
1021}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001022class BarrierHazardDetector {
1023 public:
1024 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1025 SyncStageAccessFlags src_access_scope)
1026 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1027
John Zulauf5f13a792020-03-10 07:31:21 -06001028 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1029 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001030 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001031 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
1032 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1033 return pos->second.DetectAsyncHazard(usage_index_);
1034 }
1035
1036 private:
1037 SyncStageAccessIndex usage_index_;
1038 VkPipelineStageFlags src_exec_scope_;
1039 SyncStageAccessFlags src_access_scope_;
1040};
1041
John Zulauf16adfc92020-04-08 10:28:33 -06001042HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -06001043 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001044 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001045 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -06001046 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001047}
1048
John Zulauf16adfc92020-04-08 10:28:33 -06001049HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001050 SyncStageAccessFlags src_access_scope,
1051 const VkImageSubresourceRange &subresource_range,
1052 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001053 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1054 VkOffset3D zero_offset = {0, 0, 0};
1055 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001056}
1057
John Zulauf355e49b2020-04-24 15:11:15 -06001058HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1059 SyncStageAccessFlags src_stage_accesses,
1060 const VkImageMemoryBarrier &barrier) const {
1061 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1062 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1063 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1064}
1065
John Zulauf9cb530d2019-09-30 14:14:10 -06001066template <typename Flags, typename Map>
1067SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1068 SyncStageAccessFlags scope = 0;
1069 for (const auto &bit_scope : map) {
1070 if (flag_mask < bit_scope.first) break;
1071
1072 if (flag_mask & bit_scope.first) {
1073 scope |= bit_scope.second;
1074 }
1075 }
1076 return scope;
1077}
1078
1079SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
1080 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1081}
1082
1083SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
1084 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
1085}
1086
1087// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
1088SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001089 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1090 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1091 // 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 -06001092 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1093}
1094
1095template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001096void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001097 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1098 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -06001099 auto pos = accesses->lower_bound(range);
1100 if (pos == accesses->end() || !pos->first.intersects(range)) {
1101 // The range is empty, fill it with a default value.
1102 pos = action.Infill(accesses, pos, range);
1103 } else if (range.begin < pos->first.begin) {
1104 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001105 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001106 } else if (pos->first.begin < range.begin) {
1107 // Trim the beginning if needed
1108 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1109 ++pos;
1110 }
1111
1112 const auto the_end = accesses->end();
1113 while ((pos != the_end) && pos->first.intersects(range)) {
1114 if (pos->first.end > range.end) {
1115 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1116 }
1117
1118 pos = action(accesses, pos);
1119 if (pos == the_end) break;
1120
1121 auto next = pos;
1122 ++next;
1123 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1124 // Need to infill if next is disjoint
1125 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001126 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001127 next = action.Infill(accesses, next, new_range);
1128 }
1129 pos = next;
1130 }
1131}
1132
1133struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001134 using Iterator = ResourceAccessRangeMap::iterator;
1135 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001136 // this is only called on gaps, and never returns a gap.
1137 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001138 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001139 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001140 }
John Zulauf5f13a792020-03-10 07:31:21 -06001141
John Zulauf5c5e88d2019-12-26 11:22:02 -07001142 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001143 auto &access_state = pos->second;
1144 access_state.Update(usage, tag);
1145 return pos;
1146 }
1147
John Zulauf16adfc92020-04-08 10:28:33 -06001148 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001149 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001150 : type(type_), context(context_), usage(usage_), tag(tag_) {}
1151 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001152 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001153 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001154 const ResourceUsageTag &tag;
1155};
1156
1157struct ApplyMemoryAccessBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001158 using Iterator = ResourceAccessRangeMap::iterator;
1159 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001160
John Zulauf5c5e88d2019-12-26 11:22:02 -07001161 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001162 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001163 access_state.ApplyMemoryAccessBarrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001164 return pos;
1165 }
1166
John Zulauf36bcf6a2020-02-03 15:12:52 -07001167 ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_,
1168 VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_)
1169 : src_exec_scope(src_exec_scope_),
1170 src_access_scope(src_access_scope_),
1171 dst_exec_scope(dst_exec_scope_),
1172 dst_access_scope(dst_access_scope_) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001173
John Zulauf36bcf6a2020-02-03 15:12:52 -07001174 VkPipelineStageFlags src_exec_scope;
1175 SyncStageAccessFlags src_access_scope;
1176 VkPipelineStageFlags dst_exec_scope;
1177 SyncStageAccessFlags dst_access_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001178};
1179
1180struct ApplyGlobalBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001181 using Iterator = ResourceAccessRangeMap::iterator;
1182 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001183
John Zulauf5c5e88d2019-12-26 11:22:02 -07001184 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001185 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001186 access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001187
1188 for (const auto &functor : barrier_functor) {
1189 functor(accesses, pos);
1190 }
1191 return pos;
1192 }
1193
John Zulauf36bcf6a2020-02-03 15:12:52 -07001194 ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope,
1195 SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses,
John Zulauf9cb530d2019-09-30 14:14:10 -06001196 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001197 : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001198 // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier...
1199 barrier_functor.reserve(memoryBarrierCount);
1200 for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) {
1201 const auto &barrier = pMemoryBarriers[barrier_index];
John Zulauf36bcf6a2020-02-03 15:12:52 -07001202 barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask),
1203 dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
John Zulauf9cb530d2019-09-30 14:14:10 -06001204 }
1205 }
1206
John Zulauf36bcf6a2020-02-03 15:12:52 -07001207 const VkPipelineStageFlags src_exec_scope;
1208 const VkPipelineStageFlags dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001209 std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor;
1210};
1211
John Zulauf355e49b2020-04-24 15:11:15 -06001212void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1213 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001214 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1215 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001216}
1217
John Zulauf16adfc92020-04-08 10:28:33 -06001218void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001219 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001220 if (!SimpleBinding(buffer)) return;
1221 const auto base_address = ResourceBaseAddress(buffer);
1222 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1223}
John Zulauf355e49b2020-04-24 15:11:15 -06001224
John Zulauf540266b2020-04-06 18:54:53 -06001225void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001226 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001227 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001228 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001229 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001230 const auto address_type = ImageAddressType(image);
1231 const auto base_address = ResourceBaseAddress(image);
1232 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001233 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001234 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001235 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001236}
John Zulauf7635de32020-05-29 17:14:15 -06001237void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1238 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1239 if (view != nullptr) {
1240 const IMAGE_STATE *image = view->image_state.get();
1241 if (image != nullptr) {
1242 auto *update_range = &view->normalized_subresource_range;
1243 VkImageSubresourceRange masked_range;
1244 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1245 masked_range = view->normalized_subresource_range;
1246 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1247 update_range = &masked_range;
1248 }
1249 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1250 }
1251 }
1252}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001253
John Zulauf355e49b2020-04-24 15:11:15 -06001254void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1255 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1256 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001257 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1258 subresource.layerCount};
1259 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1260}
1261
John Zulauf540266b2020-04-06 18:54:53 -06001262template <typename Action>
1263void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001264 if (!SimpleBinding(buffer)) return;
1265 const auto base_address = ResourceBaseAddress(buffer);
1266 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001267}
1268
1269template <typename Action>
1270void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1271 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001272 if (!SimpleBinding(image)) return;
1273 const auto address_type = ImageAddressType(image);
1274 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001275
locke-lunargae26eac2020-04-16 15:29:05 -06001276 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001277 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001278
John Zulauf16adfc92020-04-08 10:28:33 -06001279 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001280 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001281 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001282 }
1283}
1284
John Zulauf7635de32020-05-29 17:14:15 -06001285void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1286 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1287 const ResourceUsageTag &tag) {
1288 UpdateStateResolveAction update(*this, tag);
1289 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1290}
1291
John Zulaufaff20662020-06-01 14:07:58 -06001292void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1293 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1294 const ResourceUsageTag &tag) {
1295 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1296 VkExtent3D extent = CastTo3D(render_area.extent);
1297 VkOffset3D offset = CastTo3D(render_area.offset);
1298
1299 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1300 if (rp_state.attachment_last_subpass[i] == subpass) {
1301 if (attachment_views[i] == nullptr) continue; // UNUSED
1302 const auto &view = *attachment_views[i];
1303 const IMAGE_STATE *image = view.image_state.get();
1304 if (image == nullptr) continue;
1305
1306 const auto &ci = attachment_ci[i];
1307 const bool has_depth = FormatHasDepth(ci.format);
1308 const bool has_stencil = FormatHasStencil(ci.format);
1309 const bool is_color = !(has_depth || has_stencil);
1310 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1311
1312 if (is_color && store_op_stores) {
1313 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1314 offset, extent, tag);
1315 } else {
1316 auto update_range = view.normalized_subresource_range;
1317 if (has_depth && store_op_stores) {
1318 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1319 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1320 tag);
1321 }
1322 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1323 if (has_stencil && stencil_op_stores) {
1324 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1325 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1326 tag);
1327 }
1328 }
1329 }
1330 }
1331}
1332
John Zulauf540266b2020-04-06 18:54:53 -06001333template <typename Action>
1334void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1335 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001336 for (const auto address_type : kAddressTypes) {
1337 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001338 }
1339}
1340
1341void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001342 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1343 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -06001344 for (const auto address_type : kAddressTypes) {
John Zulauf355e49b2020-04-24 15:11:15 -06001345 context.ResolveAccessRange(address_type, full_range, &context.GetDstExternalTrackBack().barrier,
1346 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001347 }
1348 }
1349}
1350
John Zulauf355e49b2020-04-24 15:11:15 -06001351void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1352 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1353 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) {
1354 const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1355 UpdateMemoryAccess(image, subresource_range, barrier_action);
1356}
1357
John Zulauf7635de32020-05-29 17:14:15 -06001358// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001359void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1360 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1361 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range,
1362 bool layout_transition, const ResourceUsageTag &tag) {
1363 if (layout_transition) {
1364 UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent,
1365 tag);
1366 ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope,
1367 subresource_range);
John Zulaufc9201222020-05-13 15:13:03 -06001368 } else {
1369 ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range);
John Zulauf355e49b2020-04-24 15:11:15 -06001370 }
John Zulauf355e49b2020-04-24 15:11:15 -06001371}
1372
John Zulauf7635de32020-05-29 17:14:15 -06001373// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001374void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier,
1375 const VkImageSubresourceRange &subresource_range, bool layout_transition,
1376 const ResourceUsageTag &tag) {
1377 ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope,
1378 subresource_range, layout_transition, tag);
1379}
1380
1381// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001382HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001383 if (!attach_view) return HazardResult();
1384 const auto image_state = attach_view->image_state.get();
1385 if (!image_state) return HazardResult();
1386
John Zulauf355e49b2020-04-24 15:11:15 -06001387 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001388 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001389
1390 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulauf7635de32020-05-29 17:14:15 -06001391 auto hazard = track_back.context->DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope,
1392 track_back.barrier.src_access_scope,
1393 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001394 if (!hazard.hazard) {
1395 // The Async hazard check is against the current context's async set.
John Zulauf7635de32020-05-29 17:14:15 -06001396 hazard = DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope, track_back.barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001397 attach_view->normalized_subresource_range, kDetectAsync);
1398 }
1399 return hazard;
1400}
1401
1402// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1403bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1404
1405 const VkRenderPassBeginInfo *pRenderPassBegin,
1406 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1407 const char *func_name) const {
1408 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1409 bool skip = false;
1410 uint32_t subpass = 0;
1411 const auto &transitions = rp_state.subpass_transitions[subpass];
1412 if (transitions.size()) {
1413 const std::vector<AccessContext> empty_context_vector;
1414 // Create context we can use to validate against...
1415 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1416 const_cast<AccessContext *>(&cb_access_context_));
1417
1418 assert(pRenderPassBegin);
1419 if (nullptr == pRenderPassBegin) return skip;
1420
1421 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1422 assert(fb_state);
1423 if (nullptr == fb_state) return skip;
1424
1425 // Create a limited array of views (which we'll need to toss
1426 std::vector<const IMAGE_VIEW_STATE *> views;
1427 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
1428 const auto attachment_count = count_attachment.first;
1429 const auto *attachments = count_attachment.second;
1430 views.resize(attachment_count, nullptr);
1431 for (const auto &transition : transitions) {
1432 assert(transition.attachment < attachment_count);
1433 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
1434 }
1435
John Zulauf7635de32020-05-29 17:14:15 -06001436 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
1437 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001438 }
1439 return skip;
1440}
1441
locke-lunarg61870c22020-06-09 14:51:50 -06001442bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1443 const char *func_name) const {
1444 bool skip = false;
1445 const PIPELINE_STATE *pPipe = nullptr;
1446 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1447 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1448 if (!pPipe || !per_sets) {
1449 return skip;
1450 }
1451
1452 using DescriptorClass = cvdescriptorset::DescriptorClass;
1453 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1454 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1455 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1456 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1457
1458 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarg37047832020-06-12 13:44:45 -06001459 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001460 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1461 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001462 for (const auto &set_binding : stage_state.descriptor_uses) {
1463 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1464 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1465 set_binding.first.second);
1466 const auto descriptor_type = binding_it.GetType();
1467 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1468 auto array_idx = 0;
1469
1470 if (binding_it.IsVariableDescriptorCount()) {
1471 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1472 }
1473 SyncStageAccessIndex sync_index =
1474 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1475
1476 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1477 uint32_t index = i - index_range.start;
1478 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1479 switch (descriptor->GetClass()) {
1480 case DescriptorClass::ImageSampler:
1481 case DescriptorClass::Image: {
1482 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001483 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001484 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001485 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1486 img_view_state = image_sampler_descriptor->GetImageViewState();
1487 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001488 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001489 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1490 img_view_state = image_descriptor->GetImageViewState();
1491 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001492 }
1493 if (!img_view_state) continue;
1494 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1495 VkExtent3D extent = {};
1496 VkOffset3D offset = {};
1497 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1498 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1499 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1500 } else {
1501 extent = img_state->createInfo.extent;
1502 }
John Zulauf361fb532020-07-22 10:45:39 -06001503 HazardResult hazard;
1504 const auto &subresource_range = img_view_state->normalized_subresource_range;
1505 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1506 // Input attachments are subject to raster ordering rules
1507 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
1508 kAttachmentRasterOrder, offset, extent);
1509 } else {
1510 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1511 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001512 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001513 skip |= sync_state_->LogError(
1514 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001515 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1516 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001517 func_name, string_SyncHazard(hazard.hazard),
1518 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1519 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1520 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001521 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1522 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1523 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001524 }
1525 break;
1526 }
1527 case DescriptorClass::TexelBuffer: {
1528 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1529 if (!buf_view_state) continue;
1530 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1531 ResourceAccessRange range =
1532 MakeRange(buf_view_state->create_info.offset,
1533 GetRealWholeSize(buf_view_state->create_info.offset, buf_view_state->create_info.range,
1534 buf_state->createInfo.size));
1535 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001536 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001537 skip |= sync_state_->LogError(
1538 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001539 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1540 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001541 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1542 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1543 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001544 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1545 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1546 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001547 }
1548 break;
1549 }
1550 case DescriptorClass::GeneralBuffer: {
1551 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1552 auto buf_state = buffer_descriptor->GetBufferState();
1553 if (!buf_state) continue;
1554 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1555 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001556 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001557 skip |= sync_state_->LogError(
1558 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001559 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1560 func_name, string_SyncHazard(hazard.hazard),
1561 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001562 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1563 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001564 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1565 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1566 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001567 }
1568 break;
1569 }
1570 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1571 default:
1572 break;
1573 }
1574 }
1575 }
1576 }
1577 return skip;
1578}
1579
1580void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1581 const ResourceUsageTag &tag) {
1582 const PIPELINE_STATE *pPipe = nullptr;
1583 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1584 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1585 if (!pPipe || !per_sets) {
1586 return;
1587 }
1588
1589 using DescriptorClass = cvdescriptorset::DescriptorClass;
1590 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1591 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1592 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1593 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1594
1595 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001596 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
1597 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1598 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001599 for (const auto &set_binding : stage_state.descriptor_uses) {
1600 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1601 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1602 set_binding.first.second);
1603 const auto descriptor_type = binding_it.GetType();
1604 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1605 auto array_idx = 0;
1606
1607 if (binding_it.IsVariableDescriptorCount()) {
1608 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1609 }
1610 SyncStageAccessIndex sync_index =
1611 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1612
1613 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1614 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1615 switch (descriptor->GetClass()) {
1616 case DescriptorClass::ImageSampler:
1617 case DescriptorClass::Image: {
1618 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1619 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1620 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1621 } else {
1622 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1623 }
1624 if (!img_view_state) continue;
1625 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1626 VkExtent3D extent = {};
1627 VkOffset3D offset = {};
1628 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1629 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1630 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1631 } else {
1632 extent = img_state->createInfo.extent;
1633 }
1634 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1635 offset, extent, tag);
1636 break;
1637 }
1638 case DescriptorClass::TexelBuffer: {
1639 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1640 if (!buf_view_state) continue;
1641 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1642 ResourceAccessRange range =
1643 MakeRange(buf_view_state->create_info.offset, buf_view_state->create_info.range);
1644 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1645 break;
1646 }
1647 case DescriptorClass::GeneralBuffer: {
1648 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1649 auto buf_state = buffer_descriptor->GetBufferState();
1650 if (!buf_state) continue;
1651 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1652 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1653 break;
1654 }
1655 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1656 default:
1657 break;
1658 }
1659 }
1660 }
1661 }
1662}
1663
1664bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1665 bool skip = false;
1666 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1667 if (!pPipe) {
1668 return skip;
1669 }
1670
1671 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1672 const auto &binding_buffers_size = binding_buffers.size();
1673 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1674
1675 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1676 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1677 if (binding_description.binding < binding_buffers_size) {
1678 const auto &binding_buffer = binding_buffers[binding_description.binding];
1679 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1680
1681 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1682 VkDeviceSize range_start = 0;
1683 VkDeviceSize range_size = 0;
1684 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1685 binding_description.stride);
1686 ResourceAccessRange range = MakeRange(range_start, range_size);
1687 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1688 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001689 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001690 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 -06001691 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001692 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001693 }
1694 }
1695 }
1696 return skip;
1697}
1698
1699void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
1700 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1701 if (!pPipe) {
1702 return;
1703 }
1704 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1705 const auto &binding_buffers_size = binding_buffers.size();
1706 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1707
1708 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1709 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1710 if (binding_description.binding < binding_buffers_size) {
1711 const auto &binding_buffer = binding_buffers[binding_description.binding];
1712 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1713
1714 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1715 VkDeviceSize range_start = 0;
1716 VkDeviceSize range_size = 0;
1717 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1718 binding_description.stride);
1719 ResourceAccessRange range = MakeRange(range_start, range_size);
1720 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1721 }
1722 }
1723}
1724
1725bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1726 bool skip = false;
1727 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
1728
1729 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1730 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1731 VkDeviceSize range_start = 0;
1732 VkDeviceSize range_size = 0;
1733 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1734 indexCount, index_size);
1735 ResourceAccessRange range = MakeRange(range_start, range_size);
1736 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1737 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001738 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001739 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 -06001740 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001741 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001742 }
1743
1744 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1745 // We will detect more accurate range in the future.
1746 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1747 return skip;
1748}
1749
1750void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
1751 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return;
1752
1753 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1754 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1755 VkDeviceSize range_start = 0;
1756 VkDeviceSize range_size = 0;
1757 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1758 indexCount, index_size);
1759 ResourceAccessRange range = MakeRange(range_start, range_size);
1760 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1761
1762 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1763 // We will detect more accurate range in the future.
1764 RecordDrawVertex(UINT32_MAX, 0, tag);
1765}
1766
1767bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06001768 bool skip = false;
1769 if (!current_renderpass_context_) return skip;
1770 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1771 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1772 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001773}
1774
1775void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
locke-lunarg7077d502020-06-18 21:37:26 -06001776 if (current_renderpass_context_)
1777 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1778 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001779}
1780
John Zulauf355e49b2020-04-24 15:11:15 -06001781bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001782 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001783 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06001784 skip |=
1785 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001786
1787 return skip;
1788}
1789
1790bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1791 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001792 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001793 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001794 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06001795 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1796 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001797
1798 return skip;
1799}
1800
1801void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1802 assert(sync_state_);
1803 if (!cb_state_) return;
1804
1805 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06001806 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06001807 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06001808 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001809 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001810}
1811
John Zulauf355e49b2020-04-24 15:11:15 -06001812void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001813 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001814 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001815 current_context_ = &current_renderpass_context_->CurrentContext();
1816}
1817
John Zulauf355e49b2020-04-24 15:11:15 -06001818void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001819 assert(current_renderpass_context_);
1820 if (!current_renderpass_context_) return;
1821
John Zulauf1a224292020-06-30 14:52:13 -06001822 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001823 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001824 current_renderpass_context_ = nullptr;
1825}
1826
locke-lunarg61870c22020-06-09 14:51:50 -06001827bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1828 const VkRect2D &render_area, const char *func_name) const {
1829 bool skip = false;
locke-lunarg96dc9632020-06-10 17:22:18 -06001830 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001831 if (!pPipe ||
1832 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001833 return skip;
1834 }
1835 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001836 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1837 VkExtent3D extent = CastTo3D(render_area.extent);
1838 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001839
John Zulauf1a224292020-06-30 14:52:13 -06001840 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001841 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001842 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1843 for (const auto location : list) {
1844 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1845 continue;
1846 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001847 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1848 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06001849 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001850 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001851 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001852 func_name, string_SyncHazard(hazard.hazard),
1853 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1854 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001855 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001856 }
1857 }
1858 }
locke-lunarg37047832020-06-12 13:44:45 -06001859
1860 // PHASE1 TODO: Add layout based read/vs. write selection.
1861 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1862 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1863 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001864 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001865 bool depth_write = false, stencil_write = false;
1866
1867 // PHASE1 TODO: These validation should be in core_checks.
1868 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1869 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1870 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1871 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1872 depth_write = true;
1873 }
1874 // PHASE1 TODO: It needs to check if stencil is writable.
1875 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1876 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1877 // PHASE1 TODO: These validation should be in core_checks.
1878 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1879 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1880 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1881 stencil_write = true;
1882 }
1883
1884 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1885 if (depth_write) {
1886 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001887 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1888 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001889 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001890 skip |= sync_state.LogError(
1891 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001892 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001893 func_name, string_SyncHazard(hazard.hazard),
1894 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1895 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001896 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001897 }
1898 }
1899 if (stencil_write) {
1900 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001901 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1902 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001903 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001904 skip |= sync_state.LogError(
1905 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06001906 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001907 func_name, string_SyncHazard(hazard.hazard),
1908 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1909 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001910 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001911 }
locke-lunarg61870c22020-06-09 14:51:50 -06001912 }
1913 }
1914 return skip;
1915}
1916
locke-lunarg96dc9632020-06-10 17:22:18 -06001917void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1918 const ResourceUsageTag &tag) {
1919 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001920 if (!pPipe ||
1921 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001922 return;
1923 }
1924 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001925 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1926 VkExtent3D extent = CastTo3D(render_area.extent);
1927 VkOffset3D offset = CastTo3D(render_area.offset);
1928
John Zulauf1a224292020-06-30 14:52:13 -06001929 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001930 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001931 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1932 for (const auto location : list) {
1933 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1934 continue;
1935 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001936 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
1937 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001938 }
1939 }
locke-lunarg37047832020-06-12 13:44:45 -06001940
1941 // PHASE1 TODO: Add layout based read/vs. write selection.
1942 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1943 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1944 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001945 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001946 bool depth_write = false, stencil_write = false;
1947
1948 // PHASE1 TODO: These validation should be in core_checks.
1949 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1950 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1951 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1952 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1953 depth_write = true;
1954 }
1955 // PHASE1 TODO: It needs to check if stencil is writable.
1956 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1957 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1958 // PHASE1 TODO: These validation should be in core_checks.
1959 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1960 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1961 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1962 stencil_write = true;
1963 }
1964
1965 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1966 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001967 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1968 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001969 }
1970 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001971 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1972 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001973 }
locke-lunarg61870c22020-06-09 14:51:50 -06001974 }
1975}
1976
John Zulauf1507ee42020-05-18 11:33:09 -06001977bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
1978 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001979 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001980 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06001981 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1982 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001983 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1984 func_name);
1985
John Zulauf355e49b2020-04-24 15:11:15 -06001986 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06001987 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06001988 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1989 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1990 return skip;
1991}
1992bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
1993 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001994 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06001995 bool skip = false;
1996 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1997 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001998 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1999 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06002000 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002001 return skip;
2002}
2003
John Zulauf7635de32020-05-29 17:14:15 -06002004AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2005 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2006}
2007
2008bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2009 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002010 bool skip = false;
2011
John Zulauf7635de32020-05-29 17:14:15 -06002012 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2013 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2014 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2015 // to apply and only copy then, if this proves a hot spot.
2016 std::unique_ptr<AccessContext> proxy_for_current;
2017
John Zulauf355e49b2020-04-24 15:11:15 -06002018 // Validate the "finalLayout" transitions to external
2019 // Get them from where there we're hidding in the extra entry.
2020 const auto &final_transitions = rp_state_->subpass_transitions.back();
2021 for (const auto &transition : final_transitions) {
2022 const auto &attach_view = attachment_views_[transition.attachment];
2023 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2024 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002025 auto *context = trackback.context;
2026
2027 if (transition.prev_pass == current_subpass_) {
2028 if (!proxy_for_current) {
2029 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2030 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2031 }
2032 context = proxy_for_current.get();
2033 }
2034
2035 auto hazard = context->DetectImageBarrierHazard(
John Zulauf355e49b2020-04-24 15:11:15 -06002036 *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope,
2037 attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious);
2038 if (hazard.hazard) {
2039 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2040 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf389c34b2020-07-28 11:19:35 -06002041 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002042 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf389c34b2020-07-28 11:19:35 -06002043 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf37ceaed2020-07-03 16:18:15 -06002044 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002045 }
2046 }
2047 return skip;
2048}
2049
2050void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2051 // Add layout transitions...
2052 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
2053 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -06002054 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -06002055 for (const auto &transition : transitions) {
2056 const auto attachment_view = attachment_views_[transition.attachment];
2057 if (!attachment_view) continue;
2058 const auto image = attachment_view->image_state.get();
2059 if (!image) continue;
2060
2061 const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
John Zulaufc9201222020-05-13 15:13:03 -06002062 auto insert_pair = view_seen.insert(attachment_view);
2063 if (insert_pair.second) {
2064 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
2065 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag);
2066
2067 } else {
2068 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
2069 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
2070 auto barrier_to_transition = barrier->barrier;
2071 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
2072 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag);
2073 }
John Zulauf355e49b2020-04-24 15:11:15 -06002074 }
2075}
2076
John Zulauf1507ee42020-05-18 11:33:09 -06002077void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2078 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2079 auto &subpass_context = subpass_contexts_[current_subpass_];
2080 VkExtent3D extent = CastTo3D(render_area.extent);
2081 VkOffset3D offset = CastTo3D(render_area.offset);
2082
2083 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2084 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2085 if (attachment_views_[i] == nullptr) continue; // UNUSED
2086 const auto &view = *attachment_views_[i];
2087 const IMAGE_STATE *image = view.image_state.get();
2088 if (image == nullptr) continue;
2089
2090 const auto &ci = attachment_ci[i];
2091 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002092 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002093 const bool is_color = !(has_depth || has_stencil);
2094
2095 if (is_color) {
2096 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2097 extent, tag);
2098 } else {
2099 auto update_range = view.normalized_subresource_range;
2100 if (has_depth) {
2101 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2102 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2103 }
2104 if (has_stencil) {
2105 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2106 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2107 tag);
2108 }
2109 }
2110 }
2111 }
2112}
2113
John Zulauf355e49b2020-04-24 15:11:15 -06002114void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002115 const AccessContext *external_context, VkQueueFlags queue_flags,
2116 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002117 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002118 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002119 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2120 // Add this for all subpasses here so that they exsist during next subpass validation
2121 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002122 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002123 }
2124 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2125
2126 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002127 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002128}
John Zulauf1507ee42020-05-18 11:33:09 -06002129
2130void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002131 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2132 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002133 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002134
John Zulauf355e49b2020-04-24 15:11:15 -06002135 current_subpass_++;
2136 assert(current_subpass_ < subpass_contexts_.size());
2137 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002138 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002139}
2140
John Zulauf1a224292020-06-30 14:52:13 -06002141void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2142 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002143 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002144 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002145 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002146
John Zulauf355e49b2020-04-24 15:11:15 -06002147 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002148 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002149
2150 // Add the "finalLayout" transitions to external
2151 // Get them from where there we're hidding in the extra entry.
2152 const auto &final_transitions = rp_state_->subpass_transitions.back();
2153 for (const auto &transition : final_transitions) {
2154 const auto &attachment = attachment_views_[transition.attachment];
2155 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002156 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf1a224292020-06-30 14:52:13 -06002157 external_context->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier,
2158 attachment->normalized_subresource_range, true, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002159 }
2160}
2161
John Zulauf3d84f1b2020-03-09 13:33:25 -06002162SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2163 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2164 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2165 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2166 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2167 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2168 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2169}
2170
2171void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) {
2172 ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
2173 ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope);
2174}
2175
John Zulauf9cb530d2019-09-30 14:14:10 -06002176HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2177 HazardResult hazard;
2178 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002179 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002180 if (IsRead(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002181 // Only check reads vs. last_write if it doesn't happen-after any other read because either:
2182 // * the previous reads are not hazards, and thus last_write must be visible and available to
2183 // any reads that happen after.
2184 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2185 // the current read will be also not be a hazard, thus reporting a hazard here adds no needed information.
2186 if (((usage_stage & read_execution_barriers) == 0) && last_write && IsWriteHazard(usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002187 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002188 }
2189 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002190 // Write operation:
2191 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2192 // If reads exists -- test only against them because either:
2193 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2194 // * 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
2195 // the current write happens after the reads, so just test the write against the reades
2196 // Otherwise test against last_write
2197 //
2198 // Look for casus belli for WAR
2199 if (last_read_count) {
2200 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2201 const auto &read_access = last_reads[read_index];
2202 if (IsReadHazard(usage_stage, read_access)) {
2203 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2204 break;
2205 }
2206 }
2207 } else if (input_attachment_barriers != kNoAttachmentRead) {
John Zulaufd14743a2020-07-03 09:42:39 -06002208 if (IsReadHazard(usage_stage, input_attachment_barriers)) {
John Zulauf59e25072020-07-17 10:55:21 -06002209 hazard.Set(this, usage_index, WRITE_AFTER_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002210 }
John Zulauf361fb532020-07-22 10:45:39 -06002211 } else if (last_write && IsWriteHazard(usage)) {
2212 // Write-After-Write check -- if we have a previous write to test against
2213 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002214 }
2215 }
2216 return hazard;
2217}
2218
John Zulauf69133422020-05-20 14:55:53 -06002219HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2220 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2221 HazardResult hazard;
2222 const auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002223 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf69133422020-05-20 14:55:53 -06002224 const bool write_is_ordered = (last_write & ordering.access_scope) == last_write; // Is true if no write, and that's good.
2225 if (IsRead(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002226 if (!write_is_ordered) {
2227 // Only check for RAW if the write is unordered, and there are no reads ordered before the current read since last_write
2228 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2229 // We need to assemble the effect read_execution barriers from the union of the state barriers and the ordering rules
2230 // Check to see if there are any reads ordered before usage, including ordering rules and barriers.
2231 bool ordered_read = 0 != ((last_read_stages & ordering.exec_scope) | (read_execution_barriers & usage_stage));
2232 // Noting the "special* encoding of the input attachment ordering rule (in access, but not exec)
2233 if ((ordering.access_scope & SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) &&
2234 (input_attachment_barriers != kNoAttachmentRead)) {
2235 ordered_read = true;
John Zulauf69133422020-05-20 14:55:53 -06002236 }
John Zulaufd14743a2020-07-03 09:42:39 -06002237
John Zulauf361fb532020-07-22 10:45:39 -06002238 if (!ordered_read && IsWriteHazard(usage)) {
2239 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2240 }
2241 }
2242
2243 } else {
2244 // Only check for WAW if there are no reads since last_write
2245 if (last_read_count) {
2246 // Ignore ordered read stages (which represent frame-buffer local operations, except input attachment
2247 const auto unordered_reads = last_read_stages & ~ordering.exec_scope;
2248 // Look for any WAR hazards outside the ordered set of stages
2249 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2250 const auto &read_access = last_reads[read_index];
2251 if ((read_access.stage & unordered_reads) && IsReadHazard(usage_stage, read_access)) {
2252 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2253 break;
John Zulaufd14743a2020-07-03 09:42:39 -06002254 }
2255 }
John Zulauf361fb532020-07-22 10:45:39 -06002256 } else if (input_attachment_barriers != kNoAttachmentRead) {
2257 // This is special case code for the fragment shader input attachment, which unlike all other fragment shader operations
2258 // is framebuffer local, and thus subject to raster ordering guarantees
2259 if (0 == (ordering.access_scope & SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT)) {
2260 // NOTE: Currently all ordering barriers include this bit, so this code may never be reached, but it's
2261 // here s.t. if we need to change the ordering barrier/rules we needn't change the code.
2262 hazard.Set(this, usage_index, WRITE_AFTER_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
2263 }
2264 } else if (!write_is_ordered && IsWriteHazard(usage)) {
2265 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf69133422020-05-20 14:55:53 -06002266 }
2267 }
2268 return hazard;
2269}
2270
John Zulauf2f952d22020-02-10 11:34:51 -07002271// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06002272HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002273 HazardResult hazard;
2274 auto usage = FlagBit(usage_index);
2275 if (IsRead(usage)) {
2276 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002277 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002278 }
2279 } else {
2280 if (last_write != 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002281 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002282 } else if (last_read_count > 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002283 hazard.Set(this, usage_index, WRITE_RACING_READ, last_reads[0].access, last_reads[0].tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002284 } else if (input_attachment_barriers != kNoAttachmentRead) {
John Zulauf59e25072020-07-17 10:55:21 -06002285 hazard.Set(this, usage_index, WRITE_RACING_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002286 }
2287 }
2288 return hazard;
2289}
2290
John Zulauf36bcf6a2020-02-03 15:12:52 -07002291HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
2292 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002293 // Only supporting image layout transitions for now
2294 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2295 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002296 // only test for WAW if there no intervening read operations.
2297 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2298 if (last_read_count) {
John Zulauf355e49b2020-04-24 15:11:15 -06002299 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002300 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002301 const auto &read_access = last_reads[read_index];
2302 // If the read stage is not in the src sync sync
2303 // *AND* not execution chained with an existing sync barrier (that's the or)
2304 // then the barrier access is unsafe (R/W after R)
2305 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002306 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002307 break;
2308 }
2309 }
John Zulauf361fb532020-07-22 10:45:39 -06002310 } else if (input_attachment_barriers != kNoAttachmentRead) {
John Zulaufd14743a2020-07-03 09:42:39 -06002311 // Same logic as read acces above for the special case of input attachment read
John Zulaufd14743a2020-07-03 09:42:39 -06002312 if ((src_exec_scope & (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | input_attachment_barriers)) == 0) {
John Zulauf59e25072020-07-17 10:55:21 -06002313 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 -06002314 }
John Zulauf361fb532020-07-22 10:45:39 -06002315 } else if (last_write) {
2316 // If the previous write is *not* in the 1st access scope
2317 // *AND* the current barrier is not in the dependency chain
2318 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2319 // then the barrier access is unsafe (R/W after W)
2320 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
2321 // TODO: Do we need a difference hazard name for this?
2322 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2323 }
John Zulaufd14743a2020-07-03 09:42:39 -06002324 }
John Zulauf361fb532020-07-22 10:45:39 -06002325
John Zulauf0cb5be22020-01-23 12:18:22 -07002326 return hazard;
2327}
2328
John Zulauf5f13a792020-03-10 07:31:21 -06002329// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2330// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2331// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2332void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2333 if (write_tag.IsBefore(other.write_tag)) {
2334 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
2335 *this = other;
2336 } else if (!other.write_tag.IsBefore(write_tag)) {
2337 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2338 // dependency chaining logic or any stage expansion)
2339 write_barriers |= other.write_barriers;
2340
John Zulaufd14743a2020-07-03 09:42:39 -06002341 // Merge the read states
2342 if (input_attachment_barriers == kNoAttachmentRead) {
2343 // this doesn't have an input attachment read, so we'll take other, unconditionally (even if it's kNoAttachmentRead)
2344 input_attachment_barriers = other.input_attachment_barriers;
2345 input_attachment_tag = other.input_attachment_tag;
2346 } else if (other.input_attachment_barriers != kNoAttachmentRead) {
2347 // Both states have an input attachment read, pick the newest tag and merge barriers.
2348 if (input_attachment_tag.IsBefore(other.input_attachment_tag)) {
2349 input_attachment_tag = other.input_attachment_tag;
2350 }
2351 input_attachment_barriers |= other.input_attachment_barriers;
2352 }
2353 // The else clause is that only this has an attachment read and no merge is needed
2354
John Zulauf5f13a792020-03-10 07:31:21 -06002355 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2356 auto &other_read = other.last_reads[other_read_index];
2357 if (last_read_stages & other_read.stage) {
2358 // Merge in the barriers for read stages that exist in *both* this and other
2359 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
2360 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
2361 auto &my_read = last_reads[my_read_index];
2362 if (other_read.stage == my_read.stage) {
2363 if (my_read.tag.IsBefore(other_read.tag)) {
2364 my_read.tag = other_read.tag;
John Zulauf37ceaed2020-07-03 16:18:15 -06002365 my_read.access = other_read.access;
John Zulauf5f13a792020-03-10 07:31:21 -06002366 }
2367 my_read.barriers |= other_read.barriers;
2368 break;
2369 }
2370 }
2371 } else {
2372 // The other read stage doesn't exist in this, so add it.
2373 last_reads[last_read_count] = other_read;
2374 last_read_count++;
2375 last_read_stages |= other_read.stage;
2376 }
2377 }
John Zulauf361fb532020-07-22 10:45:39 -06002378 read_execution_barriers |= other.read_execution_barriers;
John Zulauf5f13a792020-03-10 07:31:21 -06002379 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
2380 // it.
2381}
2382
John Zulauf9cb530d2019-09-30 14:14:10 -06002383void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2384 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2385 const auto usage_bit = FlagBit(usage_index);
John Zulaufd14743a2020-07-03 09:42:39 -06002386 if (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) {
2387 // Input attachment requires special treatment for raster/load/store ordering guarantees
2388 input_attachment_barriers = 0;
2389 input_attachment_tag = tag;
2390 } else if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002391 // Mulitple outstanding reads may be of interest and do dependency chains independently
2392 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2393 const auto usage_stage = PipelineStageBit(usage_index);
2394 if (usage_stage & last_read_stages) {
2395 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2396 ReadState &access = last_reads[read_index];
2397 if (access.stage == usage_stage) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002398 access.access = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002399 access.barriers = 0;
2400 access.tag = tag;
2401 break;
2402 }
2403 }
2404 } else {
2405 // We don't have this stage in the list yet...
2406 assert(last_read_count < last_reads.size());
2407 ReadState &access = last_reads[last_read_count++];
2408 access.stage = usage_stage;
John Zulauf37ceaed2020-07-03 16:18:15 -06002409 access.access = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002410 access.barriers = 0;
2411 access.tag = tag;
2412 last_read_stages |= usage_stage;
2413 }
2414 } else {
2415 // Assume write
2416 // TODO determine what to do with READ-WRITE operations if any
John Zulaufd14743a2020-07-03 09:42:39 -06002417 // Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
John Zulauf9cb530d2019-09-30 14:14:10 -06002418 // if the last_reads/last_write were unsafe, we've reported them,
2419 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
2420 last_read_count = 0;
2421 last_read_stages = 0;
John Zulauf361fb532020-07-22 10:45:39 -06002422 read_execution_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002423
John Zulaufd14743a2020-07-03 09:42:39 -06002424 input_attachment_barriers = kNoAttachmentRead; // Denotes no outstanding input attachment read after the last write.
2425 // NOTE: we don't reset the tag, as the equality check ignores it when kNoAttachmentRead is set.
2426
John Zulauf9cb530d2019-09-30 14:14:10 -06002427 write_barriers = 0;
2428 write_dependency_chain = 0;
2429 write_tag = tag;
2430 last_write = usage_bit;
2431 }
2432}
John Zulauf5f13a792020-03-10 07:31:21 -06002433
John Zulauf9cb530d2019-09-30 14:14:10 -06002434void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
2435 // Execution Barriers only protect read operations
2436 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2437 ReadState &access = last_reads[read_index];
2438 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2439 if (srcStageMask & (access.stage | access.barriers)) {
2440 access.barriers |= dstStageMask;
John Zulauf361fb532020-07-22 10:45:39 -06002441 read_execution_barriers |= dstStageMask;
John Zulauf9cb530d2019-09-30 14:14:10 -06002442 }
2443 }
John Zulauf361fb532020-07-22 10:45:39 -06002444 if ((input_attachment_barriers != kNoAttachmentRead) &&
2445 (srcStageMask & (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | input_attachment_barriers))) {
John Zulaufd14743a2020-07-03 09:42:39 -06002446 input_attachment_barriers |= dstStageMask;
John Zulauf361fb532020-07-22 10:45:39 -06002447 read_execution_barriers |= dstStageMask;
John Zulaufd14743a2020-07-03 09:42:39 -06002448 }
John Zulauf361fb532020-07-22 10:45:39 -06002449 if (write_dependency_chain & srcStageMask) {
2450 write_dependency_chain |= dstStageMask;
2451 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002452}
2453
John Zulauf36bcf6a2020-02-03 15:12:52 -07002454void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
2455 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002456 // Assuming we've applied the execution side of this barrier, we update just the write
2457 // The || implements the "dependency chain" logic for this barrier
John Zulauf36bcf6a2020-02-03 15:12:52 -07002458 if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) {
2459 write_barriers |= dst_access_scope;
2460 write_dependency_chain |= dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06002461 }
2462}
2463
John Zulauf59e25072020-07-17 10:55:21 -06002464// This should be just Bits or Index, but we don't have an invalid state for Index
2465VkPipelineStageFlags ResourceAccessState::GetReadBarriers(SyncStageAccessFlags usage_bit) const {
2466 VkPipelineStageFlags barriers = 0U;
2467 if (usage_bit & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) {
2468 barriers = input_attachment_barriers;
2469 } else {
2470 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2471 const auto &read_access = last_reads[read_index];
2472 if (read_access.access & usage_bit) {
2473 barriers = read_access.barriers;
2474 break;
2475 }
2476 }
2477 }
2478 return barriers;
2479}
2480
John Zulaufd1f85d42020-04-15 12:23:15 -06002481void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002482 auto *access_context = GetAccessContextNoInsert(command_buffer);
2483 if (access_context) {
2484 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002485 }
2486}
2487
John Zulaufd1f85d42020-04-15 12:23:15 -06002488void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2489 auto access_found = cb_access_state.find(command_buffer);
2490 if (access_found != cb_access_state.end()) {
2491 access_found->second->Reset();
2492 cb_access_state.erase(access_found);
2493 }
2494}
2495
John Zulauf540266b2020-04-06 18:54:53 -06002496void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002497 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
2498 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002499 const VkMemoryBarrier *pMemoryBarriers) {
2500 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07002501 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002502 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002503 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002504}
2505
John Zulauf540266b2020-04-06 18:54:53 -06002506void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002507 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2508 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002509 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002510 for (uint32_t index = 0; index < barrier_count; index++) {
locke-lunarg3c038002020-04-30 23:08:08 -06002511 auto barrier = barriers[index];
John Zulauf9cb530d2019-09-30 14:14:10 -06002512 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2513 if (!buffer) continue;
locke-lunarg3c038002020-04-30 23:08:08 -06002514 barrier.size = GetRealWholeSize(barrier.offset, barrier.size, buffer->createInfo.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002515 ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002516 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2517 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2518 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2519 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002520 }
2521}
2522
John Zulauf540266b2020-04-06 18:54:53 -06002523void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2524 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2525 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002526 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002527 for (uint32_t index = 0; index < barrier_count; index++) {
2528 const auto &barrier = barriers[index];
2529 const auto *image = Get<IMAGE_STATE>(barrier.image);
2530 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002531 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002532 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2533 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2534 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2535 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
2536 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002537 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002538}
2539
2540bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2541 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2542 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002543 const auto *cb_context = GetAccessContext(commandBuffer);
2544 assert(cb_context);
2545 if (!cb_context) return skip;
2546 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002547
John Zulauf3d84f1b2020-03-09 13:33:25 -06002548 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002549 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002550 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002551
2552 for (uint32_t region = 0; region < regionCount; region++) {
2553 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002554 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002555 ResourceAccessRange src_range = MakeRange(
2556 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002557 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002558 if (hazard.hazard) {
2559 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002560 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002561 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002562 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002563 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002564 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002565 }
John Zulauf16adfc92020-04-08 10:28:33 -06002566 if (dst_buffer && !skip) {
locke-lunargff255f92020-05-13 18:53:52 -06002567 ResourceAccessRange dst_range = MakeRange(
2568 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf355e49b2020-04-24 15:11:15 -06002569 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002570 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002571 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002572 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002573 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002574 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002575 }
2576 }
2577 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002578 }
2579 return skip;
2580}
2581
2582void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2583 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002584 auto *cb_context = GetAccessContext(commandBuffer);
2585 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002586 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002587 auto *context = cb_context->GetCurrentAccessContext();
2588
John Zulauf9cb530d2019-09-30 14:14:10 -06002589 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002590 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002591
2592 for (uint32_t region = 0; region < regionCount; region++) {
2593 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002594 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002595 ResourceAccessRange src_range = MakeRange(
2596 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002597 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002598 }
John Zulauf16adfc92020-04-08 10:28:33 -06002599 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002600 ResourceAccessRange dst_range = MakeRange(
2601 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002602 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002603 }
2604 }
2605}
2606
2607bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2608 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2609 const VkImageCopy *pRegions) const {
2610 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002611 const auto *cb_access_context = GetAccessContext(commandBuffer);
2612 assert(cb_access_context);
2613 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002614
John Zulauf3d84f1b2020-03-09 13:33:25 -06002615 const auto *context = cb_access_context->GetCurrentAccessContext();
2616 assert(context);
2617 if (!context) return skip;
2618
2619 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2620 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002621 for (uint32_t region = 0; region < regionCount; region++) {
2622 const auto &copy_region = pRegions[region];
2623 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002624 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002625 copy_region.srcOffset, copy_region.extent);
2626 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002627 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002628 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002629 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002630 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002631 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002632 }
2633
2634 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002635 VkExtent3D dst_copy_extent =
2636 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002637 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002638 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002639 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002640 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002641 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002642 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002643 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002644 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002645 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002646 }
2647 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002648
John Zulauf5c5e88d2019-12-26 11:22:02 -07002649 return skip;
2650}
2651
2652void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2653 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2654 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002655 auto *cb_access_context = GetAccessContext(commandBuffer);
2656 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002657 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002658 auto *context = cb_access_context->GetCurrentAccessContext();
2659 assert(context);
2660
John Zulauf5c5e88d2019-12-26 11:22:02 -07002661 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002662 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002663
2664 for (uint32_t region = 0; region < regionCount; region++) {
2665 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002666 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002667 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2668 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002669 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002670 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002671 VkExtent3D dst_copy_extent =
2672 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002673 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2674 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002675 }
2676 }
2677}
2678
2679bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2680 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2681 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2682 uint32_t bufferMemoryBarrierCount,
2683 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2684 uint32_t imageMemoryBarrierCount,
2685 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2686 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002687 const auto *cb_access_context = GetAccessContext(commandBuffer);
2688 assert(cb_access_context);
2689 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002690
John Zulauf3d84f1b2020-03-09 13:33:25 -06002691 const auto *context = cb_access_context->GetCurrentAccessContext();
2692 assert(context);
2693 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002694
John Zulauf3d84f1b2020-03-09 13:33:25 -06002695 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002696 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2697 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002698 // Validate Image Layout transitions
2699 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2700 const auto &barrier = pImageMemoryBarriers[index];
2701 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2702 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2703 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002704 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002705 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002706 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002707 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002708 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002709 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002710 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002711 }
2712 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002713
2714 return skip;
2715}
2716
2717void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2718 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2719 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2720 uint32_t bufferMemoryBarrierCount,
2721 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2722 uint32_t imageMemoryBarrierCount,
2723 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002724 auto *cb_access_context = GetAccessContext(commandBuffer);
2725 assert(cb_access_context);
2726 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002727 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002728 auto access_context = cb_access_context->GetCurrentAccessContext();
2729 assert(access_context);
2730 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06002731
John Zulauf3d84f1b2020-03-09 13:33:25 -06002732 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002733 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002734 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002735 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
2736 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2737 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002738 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
2739 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002740 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06002741 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002742
2743 // 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 -06002744 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07002745 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06002746}
2747
2748void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
2749 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
2750 // The state tracker sets up the device state
2751 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
2752
John Zulauf5f13a792020-03-10 07:31:21 -06002753 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
2754 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06002755 // TODO: Find a good way to do this hooklessly.
2756 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
2757 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
2758 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
2759
John Zulaufd1f85d42020-04-15 12:23:15 -06002760 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2761 sync_device_state->ResetCommandBufferCallback(command_buffer);
2762 });
2763 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2764 sync_device_state->FreeCommandBufferCallback(command_buffer);
2765 });
John Zulauf9cb530d2019-09-30 14:14:10 -06002766}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002767
John Zulauf355e49b2020-04-24 15:11:15 -06002768bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2769 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
2770 bool skip = false;
2771 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
2772 auto cb_context = GetAccessContext(commandBuffer);
2773
2774 if (rp_state && cb_context) {
2775 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
2776 }
2777
2778 return skip;
2779}
2780
2781bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2782 VkSubpassContents contents) const {
2783 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2784 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2785 subpass_begin_info.contents = contents;
2786 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
2787 return skip;
2788}
2789
2790bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2791 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2792 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2793 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
2794 return skip;
2795}
2796
2797bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2798 const VkRenderPassBeginInfo *pRenderPassBegin,
2799 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2800 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2801 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
2802 return skip;
2803}
2804
John Zulauf3d84f1b2020-03-09 13:33:25 -06002805void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
2806 VkResult result) {
2807 // The state tracker sets up the command buffer state
2808 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
2809
2810 // Create/initialize the structure that trackers accesses at the command buffer scope.
2811 auto cb_access_context = GetAccessContext(commandBuffer);
2812 assert(cb_access_context);
2813 cb_access_context->Reset();
2814}
2815
2816void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06002817 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002818 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002819 if (cb_context) {
2820 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002821 }
2822}
2823
2824void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2825 VkSubpassContents contents) {
2826 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2827 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2828 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002829 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002830}
2831
2832void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2833 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2834 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002835 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002836}
2837
2838void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2839 const VkRenderPassBeginInfo *pRenderPassBegin,
2840 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2841 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002842 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
2843}
2844
2845bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2846 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
2847 bool skip = false;
2848
2849 auto cb_context = GetAccessContext(commandBuffer);
2850 assert(cb_context);
2851 auto cb_state = cb_context->GetCommandBufferState();
2852 if (!cb_state) return skip;
2853
2854 auto rp_state = cb_state->activeRenderPass;
2855 if (!rp_state) return skip;
2856
2857 skip |= cb_context->ValidateNextSubpass(func_name);
2858
2859 return skip;
2860}
2861
2862bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
2863 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
2864 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2865 subpass_begin_info.contents = contents;
2866 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
2867 return skip;
2868}
2869
2870bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2871 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2872 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2873 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
2874 return skip;
2875}
2876
2877bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2878 const VkSubpassEndInfo *pSubpassEndInfo) const {
2879 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2880 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
2881 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002882}
2883
2884void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06002885 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002886 auto cb_context = GetAccessContext(commandBuffer);
2887 assert(cb_context);
2888 auto cb_state = cb_context->GetCommandBufferState();
2889 if (!cb_state) return;
2890
2891 auto rp_state = cb_state->activeRenderPass;
2892 if (!rp_state) return;
2893
John Zulauf355e49b2020-04-24 15:11:15 -06002894 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002895}
2896
2897void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
2898 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
2899 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2900 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002901 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002902}
2903
2904void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2905 const VkSubpassEndInfo *pSubpassEndInfo) {
2906 StateTracker::PostCallRecordCmdNextSubpass2(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
2910void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2911 const VkSubpassEndInfo *pSubpassEndInfo) {
2912 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002913 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002914}
2915
John Zulauf355e49b2020-04-24 15:11:15 -06002916bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
2917 const char *func_name) const {
2918 bool skip = false;
2919
2920 auto cb_context = GetAccessContext(commandBuffer);
2921 assert(cb_context);
2922 auto cb_state = cb_context->GetCommandBufferState();
2923 if (!cb_state) return skip;
2924
2925 auto rp_state = cb_state->activeRenderPass;
2926 if (!rp_state) return skip;
2927
2928 skip |= cb_context->ValidateEndRenderpass(func_name);
2929 return skip;
2930}
2931
2932bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2933 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
2934 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
2935 return skip;
2936}
2937
2938bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
2939 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2940 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2941 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
2942 return skip;
2943}
2944
2945bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
2946 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2947 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2948 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
2949 return skip;
2950}
2951
2952void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
2953 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06002954 // Resolve the all subpass contexts to the command buffer contexts
2955 auto cb_context = GetAccessContext(commandBuffer);
2956 assert(cb_context);
2957 auto cb_state = cb_context->GetCommandBufferState();
2958 if (!cb_state) return;
2959
locke-lunargaecf2152020-05-12 17:15:41 -06002960 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06002961 if (!rp_state) return;
2962
John Zulauf355e49b2020-04-24 15:11:15 -06002963 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06002964}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002965
John Zulauf33fc1d52020-07-17 11:01:10 -06002966// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
2967// updates to a resource which do not conflict at the byte level.
2968// TODO: Revisit this rule to see if it needs to be tighter or looser
2969// TODO: Add programatic control over suppression heuristics
2970bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
2971 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
2972}
2973
John Zulauf3d84f1b2020-03-09 13:33:25 -06002974void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002975 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06002976 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002977}
2978
2979void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06002980 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06002981 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002982}
2983
2984void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06002985 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06002986 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002987}
locke-lunarga19c71d2020-03-02 18:17:04 -07002988
2989bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2990 VkImageLayout dstImageLayout, uint32_t regionCount,
2991 const VkBufferImageCopy *pRegions) const {
2992 bool skip = false;
2993 const auto *cb_access_context = GetAccessContext(commandBuffer);
2994 assert(cb_access_context);
2995 if (!cb_access_context) return skip;
2996
2997 const auto *context = cb_access_context->GetCurrentAccessContext();
2998 assert(context);
2999 if (!context) return skip;
3000
3001 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003002 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3003
3004 for (uint32_t region = 0; region < regionCount; region++) {
3005 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003006 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003007 ResourceAccessRange src_range =
3008 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003009 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003010 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003011 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003012 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003013 "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003014 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003015 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003016 }
3017 }
3018 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003019 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003020 copy_region.imageOffset, copy_region.imageExtent);
3021 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003022 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003023 "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003024 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003025 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003026 }
3027 if (skip) break;
3028 }
3029 if (skip) break;
3030 }
3031 return skip;
3032}
3033
3034void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3035 VkImageLayout dstImageLayout, uint32_t regionCount,
3036 const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003037 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07003038 auto *cb_access_context = GetAccessContext(commandBuffer);
3039 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003040 const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07003041 auto *context = cb_access_context->GetCurrentAccessContext();
3042 assert(context);
3043
3044 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003045 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003046
3047 for (uint32_t region = 0; region < regionCount; region++) {
3048 const auto &copy_region = pRegions[region];
3049 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003050 ResourceAccessRange src_range =
3051 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003052 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003053 }
3054 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003055 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003056 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003057 }
3058 }
3059}
3060
3061bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3062 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3063 const VkBufferImageCopy *pRegions) const {
3064 bool skip = false;
3065 const auto *cb_access_context = GetAccessContext(commandBuffer);
3066 assert(cb_access_context);
3067 if (!cb_access_context) return skip;
3068
3069 const auto *context = cb_access_context->GetCurrentAccessContext();
3070 assert(context);
3071 if (!context) return skip;
3072
3073 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3074 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3075 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3076 for (uint32_t region = 0; region < regionCount; region++) {
3077 const auto &copy_region = pRegions[region];
3078 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003079 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003080 copy_region.imageOffset, copy_region.imageExtent);
3081 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003082 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003083 "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003084 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003085 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003086 }
3087 }
3088 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06003089 ResourceAccessRange dst_range =
3090 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003091 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07003092 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003093 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003094 "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003095 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003096 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003097 }
3098 }
3099 if (skip) break;
3100 }
3101 return skip;
3102}
3103
3104void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3105 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003106 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07003107 auto *cb_access_context = GetAccessContext(commandBuffer);
3108 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003109 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER);
locke-lunarga19c71d2020-03-02 18:17:04 -07003110 auto *context = cb_access_context->GetCurrentAccessContext();
3111 assert(context);
3112
3113 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003114 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3115 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 -06003116 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003117
3118 for (uint32_t region = 0; region < regionCount; region++) {
3119 const auto &copy_region = pRegions[region];
3120 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003121 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06003122 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003123 }
3124 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003125 ResourceAccessRange dst_range =
3126 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06003127 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003128 }
3129 }
3130}
3131
3132bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3133 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3134 const VkImageBlit *pRegions, VkFilter filter) const {
3135 bool skip = false;
3136 const auto *cb_access_context = GetAccessContext(commandBuffer);
3137 assert(cb_access_context);
3138 if (!cb_access_context) return skip;
3139
3140 const auto *context = cb_access_context->GetCurrentAccessContext();
3141 assert(context);
3142 if (!context) return skip;
3143
3144 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3145 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3146
3147 for (uint32_t region = 0; region < regionCount; region++) {
3148 const auto &blit_region = pRegions[region];
3149 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003150 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3151 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3152 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3153 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3154 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3155 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3156 auto hazard =
3157 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003158 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003159 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003160 "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003161 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003162 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003163 }
3164 }
3165
3166 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003167 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3168 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3169 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3170 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3171 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3172 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3173 auto hazard =
3174 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003175 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003176 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003177 "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003178 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003179 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003180 }
3181 if (skip) break;
3182 }
3183 }
3184
3185 return skip;
3186}
3187
3188void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3189 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3190 const VkImageBlit *pRegions, VkFilter filter) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003191 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3192 pRegions, filter);
locke-lunarga19c71d2020-03-02 18:17:04 -07003193 auto *cb_access_context = GetAccessContext(commandBuffer);
3194 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003195 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07003196 auto *context = cb_access_context->GetCurrentAccessContext();
3197 assert(context);
3198
3199 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003200 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003201
3202 for (uint32_t region = 0; region < regionCount; region++) {
3203 const auto &blit_region = pRegions[region];
3204 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003205 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3206 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3207 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3208 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3209 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3210 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3211 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003212 }
3213 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003214 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3215 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3216 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3217 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3218 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3219 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3220 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003221 }
3222 }
3223}
locke-lunarg36ba2592020-04-03 09:42:04 -06003224
locke-lunarg61870c22020-06-09 14:51:50 -06003225bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
3226 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
3227 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003228 bool skip = false;
3229 if (drawCount == 0) return skip;
3230
3231 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3232 VkDeviceSize size = struct_size;
3233 if (drawCount == 1 || stride == size) {
3234 if (drawCount > 1) size *= drawCount;
3235 ResourceAccessRange range = MakeRange(offset, size);
3236 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3237 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003238 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003239 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003240 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003241 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003242 }
3243 } else {
3244 for (uint32_t i = 0; i < drawCount; ++i) {
3245 ResourceAccessRange range = MakeRange(offset + i * stride, size);
3246 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3247 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003248 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003249 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3250 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3251 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003252 break;
3253 }
3254 }
3255 }
3256 return skip;
3257}
3258
locke-lunarg61870c22020-06-09 14:51:50 -06003259void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3260 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3261 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003262 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3263 VkDeviceSize size = struct_size;
3264 if (drawCount == 1 || stride == size) {
3265 if (drawCount > 1) size *= drawCount;
3266 ResourceAccessRange range = MakeRange(offset, size);
3267 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3268 } else {
3269 for (uint32_t i = 0; i < drawCount; ++i) {
3270 ResourceAccessRange range = MakeRange(offset + i * stride, size);
3271 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3272 }
3273 }
3274}
3275
locke-lunarg61870c22020-06-09 14:51:50 -06003276bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3277 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003278 bool skip = false;
3279
3280 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3281 ResourceAccessRange range = MakeRange(offset, 4);
3282 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3283 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003284 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003285 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003286 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003287 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003288 }
3289 return skip;
3290}
3291
locke-lunarg61870c22020-06-09 14:51:50 -06003292void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003293 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3294 ResourceAccessRange range = MakeRange(offset, 4);
3295 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3296}
3297
locke-lunarg36ba2592020-04-03 09:42:04 -06003298bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003299 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003300 const auto *cb_access_context = GetAccessContext(commandBuffer);
3301 assert(cb_access_context);
3302 if (!cb_access_context) return skip;
3303
locke-lunarg61870c22020-06-09 14:51:50 -06003304 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003305 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003306}
3307
3308void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003309 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003310 auto *cb_access_context = GetAccessContext(commandBuffer);
3311 assert(cb_access_context);
3312 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003313
locke-lunarg61870c22020-06-09 14:51:50 -06003314 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003315}
locke-lunarge1a67022020-04-29 00:15:36 -06003316
3317bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003318 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003319 const auto *cb_access_context = GetAccessContext(commandBuffer);
3320 assert(cb_access_context);
3321 if (!cb_access_context) return skip;
3322
3323 const auto *context = cb_access_context->GetCurrentAccessContext();
3324 assert(context);
3325 if (!context) return skip;
3326
locke-lunarg61870c22020-06-09 14:51:50 -06003327 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3328 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3329 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003330 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003331}
3332
3333void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003334 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003335 auto *cb_access_context = GetAccessContext(commandBuffer);
3336 assert(cb_access_context);
3337 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3338 auto *context = cb_access_context->GetCurrentAccessContext();
3339 assert(context);
3340
locke-lunarg61870c22020-06-09 14:51:50 -06003341 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3342 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003343}
3344
3345bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3346 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003347 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003348 const auto *cb_access_context = GetAccessContext(commandBuffer);
3349 assert(cb_access_context);
3350 if (!cb_access_context) return skip;
3351
locke-lunarg61870c22020-06-09 14:51:50 -06003352 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3353 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3354 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003355 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003356}
3357
3358void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3359 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003360 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003361 auto *cb_access_context = GetAccessContext(commandBuffer);
3362 assert(cb_access_context);
3363 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003364
locke-lunarg61870c22020-06-09 14:51:50 -06003365 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3366 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3367 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003368}
3369
3370bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3371 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003372 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003373 const auto *cb_access_context = GetAccessContext(commandBuffer);
3374 assert(cb_access_context);
3375 if (!cb_access_context) return skip;
3376
locke-lunarg61870c22020-06-09 14:51:50 -06003377 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3378 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3379 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003380 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003381}
3382
3383void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3384 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003385 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003386 auto *cb_access_context = GetAccessContext(commandBuffer);
3387 assert(cb_access_context);
3388 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003389
locke-lunarg61870c22020-06-09 14:51:50 -06003390 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3391 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3392 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003393}
3394
3395bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3396 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003397 bool skip = false;
3398 if (drawCount == 0) return skip;
3399
locke-lunargff255f92020-05-13 18:53:52 -06003400 const auto *cb_access_context = GetAccessContext(commandBuffer);
3401 assert(cb_access_context);
3402 if (!cb_access_context) return skip;
3403
3404 const auto *context = cb_access_context->GetCurrentAccessContext();
3405 assert(context);
3406 if (!context) return skip;
3407
locke-lunarg61870c22020-06-09 14:51:50 -06003408 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3409 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3410 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3411 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003412
3413 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3414 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3415 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003416 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003417 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003418}
3419
3420void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3421 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003422 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003423 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003424 auto *cb_access_context = GetAccessContext(commandBuffer);
3425 assert(cb_access_context);
3426 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3427 auto *context = cb_access_context->GetCurrentAccessContext();
3428 assert(context);
3429
locke-lunarg61870c22020-06-09 14:51:50 -06003430 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3431 cb_access_context->RecordDrawSubpassAttachment(tag);
3432 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003433
3434 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3435 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3436 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003437 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003438}
3439
3440bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3441 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003442 bool skip = false;
3443 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003444 const auto *cb_access_context = GetAccessContext(commandBuffer);
3445 assert(cb_access_context);
3446 if (!cb_access_context) return skip;
3447
3448 const auto *context = cb_access_context->GetCurrentAccessContext();
3449 assert(context);
3450 if (!context) return skip;
3451
locke-lunarg61870c22020-06-09 14:51:50 -06003452 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3453 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3454 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3455 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003456
3457 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3458 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3459 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003460 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003461 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003462}
3463
3464void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3465 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003466 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003467 auto *cb_access_context = GetAccessContext(commandBuffer);
3468 assert(cb_access_context);
3469 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3470 auto *context = cb_access_context->GetCurrentAccessContext();
3471 assert(context);
3472
locke-lunarg61870c22020-06-09 14:51:50 -06003473 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3474 cb_access_context->RecordDrawSubpassAttachment(tag);
3475 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003476
3477 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3478 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3479 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003480 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003481}
3482
3483bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3484 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3485 uint32_t stride, const char *function) const {
3486 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003487 const auto *cb_access_context = GetAccessContext(commandBuffer);
3488 assert(cb_access_context);
3489 if (!cb_access_context) return skip;
3490
3491 const auto *context = cb_access_context->GetCurrentAccessContext();
3492 assert(context);
3493 if (!context) return skip;
3494
locke-lunarg61870c22020-06-09 14:51:50 -06003495 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3496 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3497 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3498 function);
3499 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003500
3501 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3502 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3503 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003504 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003505 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003506}
3507
3508bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3509 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3510 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003511 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3512 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003513}
3514
3515void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3516 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3517 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003518 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3519 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003520 auto *cb_access_context = GetAccessContext(commandBuffer);
3521 assert(cb_access_context);
3522 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3523 auto *context = cb_access_context->GetCurrentAccessContext();
3524 assert(context);
3525
locke-lunarg61870c22020-06-09 14:51:50 -06003526 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3527 cb_access_context->RecordDrawSubpassAttachment(tag);
3528 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3529 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003530
3531 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3532 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3533 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003534 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003535}
3536
3537bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3538 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3539 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003540 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3541 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003542}
3543
3544void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3545 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3546 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003547 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3548 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003549 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003550}
3551
3552bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3553 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3554 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003555 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3556 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003557}
3558
3559void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3560 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3561 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003562 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3563 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003564 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3565}
3566
3567bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3568 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3569 uint32_t stride, const char *function) const {
3570 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003571 const auto *cb_access_context = GetAccessContext(commandBuffer);
3572 assert(cb_access_context);
3573 if (!cb_access_context) return skip;
3574
3575 const auto *context = cb_access_context->GetCurrentAccessContext();
3576 assert(context);
3577 if (!context) return skip;
3578
locke-lunarg61870c22020-06-09 14:51:50 -06003579 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3580 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3581 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3582 stride, function);
3583 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003584
3585 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3586 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3587 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003588 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003589 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003590}
3591
3592bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3593 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3594 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003595 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3596 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003597}
3598
3599void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3600 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3601 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003602 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3603 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003604 auto *cb_access_context = GetAccessContext(commandBuffer);
3605 assert(cb_access_context);
3606 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3607 auto *context = cb_access_context->GetCurrentAccessContext();
3608 assert(context);
3609
locke-lunarg61870c22020-06-09 14:51:50 -06003610 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3611 cb_access_context->RecordDrawSubpassAttachment(tag);
3612 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3613 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003614
3615 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3616 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06003617 // We will update the index and vertex buffer in SubmitQueue in the future.
3618 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003619}
3620
3621bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3622 VkDeviceSize offset, VkBuffer countBuffer,
3623 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3624 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003625 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3626 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003627}
3628
3629void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3630 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3631 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003632 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3633 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003634 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3635}
3636
3637bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3638 VkDeviceSize offset, VkBuffer countBuffer,
3639 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3640 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003641 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3642 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003643}
3644
3645void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3646 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3647 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003648 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3649 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003650 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3651}
3652
3653bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3654 const VkClearColorValue *pColor, uint32_t rangeCount,
3655 const VkImageSubresourceRange *pRanges) const {
3656 bool skip = false;
3657 const auto *cb_access_context = GetAccessContext(commandBuffer);
3658 assert(cb_access_context);
3659 if (!cb_access_context) return skip;
3660
3661 const auto *context = cb_access_context->GetCurrentAccessContext();
3662 assert(context);
3663 if (!context) return skip;
3664
3665 const auto *image_state = Get<IMAGE_STATE>(image);
3666
3667 for (uint32_t index = 0; index < rangeCount; index++) {
3668 const auto &range = pRanges[index];
3669 if (image_state) {
3670 auto hazard =
3671 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3672 if (hazard.hazard) {
3673 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003674 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003675 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06003676 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003677 }
3678 }
3679 }
3680 return skip;
3681}
3682
3683void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3684 const VkClearColorValue *pColor, uint32_t rangeCount,
3685 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003686 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003687 auto *cb_access_context = GetAccessContext(commandBuffer);
3688 assert(cb_access_context);
3689 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
3690 auto *context = cb_access_context->GetCurrentAccessContext();
3691 assert(context);
3692
3693 const auto *image_state = Get<IMAGE_STATE>(image);
3694
3695 for (uint32_t index = 0; index < rangeCount; index++) {
3696 const auto &range = pRanges[index];
3697 if (image_state) {
3698 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3699 tag);
3700 }
3701 }
3702}
3703
3704bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3705 VkImageLayout imageLayout,
3706 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3707 const VkImageSubresourceRange *pRanges) const {
3708 bool skip = false;
3709 const auto *cb_access_context = GetAccessContext(commandBuffer);
3710 assert(cb_access_context);
3711 if (!cb_access_context) return skip;
3712
3713 const auto *context = cb_access_context->GetCurrentAccessContext();
3714 assert(context);
3715 if (!context) return skip;
3716
3717 const auto *image_state = Get<IMAGE_STATE>(image);
3718
3719 for (uint32_t index = 0; index < rangeCount; index++) {
3720 const auto &range = pRanges[index];
3721 if (image_state) {
3722 auto hazard =
3723 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3724 if (hazard.hazard) {
3725 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003726 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003727 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06003728 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003729 }
3730 }
3731 }
3732 return skip;
3733}
3734
3735void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3736 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3737 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003738 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003739 auto *cb_access_context = GetAccessContext(commandBuffer);
3740 assert(cb_access_context);
3741 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
3742 auto *context = cb_access_context->GetCurrentAccessContext();
3743 assert(context);
3744
3745 const auto *image_state = Get<IMAGE_STATE>(image);
3746
3747 for (uint32_t index = 0; index < rangeCount; index++) {
3748 const auto &range = pRanges[index];
3749 if (image_state) {
3750 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3751 tag);
3752 }
3753 }
3754}
3755
3756bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
3757 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
3758 VkDeviceSize dstOffset, VkDeviceSize stride,
3759 VkQueryResultFlags flags) const {
3760 bool skip = false;
3761 const auto *cb_access_context = GetAccessContext(commandBuffer);
3762 assert(cb_access_context);
3763 if (!cb_access_context) return skip;
3764
3765 const auto *context = cb_access_context->GetCurrentAccessContext();
3766 assert(context);
3767 if (!context) return skip;
3768
3769 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3770
3771 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003772 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003773 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3774 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06003775 skip |=
3776 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3777 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
3778 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003779 }
3780 }
locke-lunargff255f92020-05-13 18:53:52 -06003781
3782 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003783 return skip;
3784}
3785
3786void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
3787 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3788 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003789 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
3790 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06003791 auto *cb_access_context = GetAccessContext(commandBuffer);
3792 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06003793 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06003794 auto *context = cb_access_context->GetCurrentAccessContext();
3795 assert(context);
3796
3797 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3798
3799 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003800 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003801 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3802 }
locke-lunargff255f92020-05-13 18:53:52 -06003803
3804 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003805}
3806
3807bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3808 VkDeviceSize size, uint32_t data) const {
3809 bool skip = false;
3810 const auto *cb_access_context = GetAccessContext(commandBuffer);
3811 assert(cb_access_context);
3812 if (!cb_access_context) return skip;
3813
3814 const auto *context = cb_access_context->GetCurrentAccessContext();
3815 assert(context);
3816 if (!context) return skip;
3817
3818 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3819
3820 if (dst_buffer) {
3821 ResourceAccessRange range = MakeRange(dstOffset, size);
3822 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3823 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003824 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003825 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06003826 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003827 }
3828 }
3829 return skip;
3830}
3831
3832void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3833 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003834 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06003835 auto *cb_access_context = GetAccessContext(commandBuffer);
3836 assert(cb_access_context);
3837 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
3838 auto *context = cb_access_context->GetCurrentAccessContext();
3839 assert(context);
3840
3841 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3842
3843 if (dst_buffer) {
3844 ResourceAccessRange range = MakeRange(dstOffset, size);
3845 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3846 }
3847}
3848
3849bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3850 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3851 const VkImageResolve *pRegions) const {
3852 bool skip = false;
3853 const auto *cb_access_context = GetAccessContext(commandBuffer);
3854 assert(cb_access_context);
3855 if (!cb_access_context) return skip;
3856
3857 const auto *context = cb_access_context->GetCurrentAccessContext();
3858 assert(context);
3859 if (!context) return skip;
3860
3861 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3862 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3863
3864 for (uint32_t region = 0; region < regionCount; region++) {
3865 const auto &resolve_region = pRegions[region];
3866 if (src_image) {
3867 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3868 resolve_region.srcOffset, resolve_region.extent);
3869 if (hazard.hazard) {
3870 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003871 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003872 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003873 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003874 }
3875 }
3876
3877 if (dst_image) {
3878 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3879 resolve_region.dstOffset, resolve_region.extent);
3880 if (hazard.hazard) {
3881 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003882 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003883 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003884 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003885 }
3886 if (skip) break;
3887 }
3888 }
3889
3890 return skip;
3891}
3892
3893void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3894 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3895 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003896 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3897 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06003898 auto *cb_access_context = GetAccessContext(commandBuffer);
3899 assert(cb_access_context);
3900 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
3901 auto *context = cb_access_context->GetCurrentAccessContext();
3902 assert(context);
3903
3904 auto *src_image = Get<IMAGE_STATE>(srcImage);
3905 auto *dst_image = Get<IMAGE_STATE>(dstImage);
3906
3907 for (uint32_t region = 0; region < regionCount; region++) {
3908 const auto &resolve_region = pRegions[region];
3909 if (src_image) {
3910 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3911 resolve_region.srcOffset, resolve_region.extent, tag);
3912 }
3913 if (dst_image) {
3914 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3915 resolve_region.dstOffset, resolve_region.extent, tag);
3916 }
3917 }
3918}
3919
3920bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3921 VkDeviceSize dataSize, const void *pData) const {
3922 bool skip = false;
3923 const auto *cb_access_context = GetAccessContext(commandBuffer);
3924 assert(cb_access_context);
3925 if (!cb_access_context) return skip;
3926
3927 const auto *context = cb_access_context->GetCurrentAccessContext();
3928 assert(context);
3929 if (!context) return skip;
3930
3931 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3932
3933 if (dst_buffer) {
3934 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3935 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3936 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003937 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003938 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06003939 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003940 }
3941 }
3942 return skip;
3943}
3944
3945void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3946 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003947 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06003948 auto *cb_access_context = GetAccessContext(commandBuffer);
3949 assert(cb_access_context);
3950 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
3951 auto *context = cb_access_context->GetCurrentAccessContext();
3952 assert(context);
3953
3954 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3955
3956 if (dst_buffer) {
3957 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3958 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3959 }
3960}
locke-lunargff255f92020-05-13 18:53:52 -06003961
3962bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3963 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
3964 bool skip = false;
3965 const auto *cb_access_context = GetAccessContext(commandBuffer);
3966 assert(cb_access_context);
3967 if (!cb_access_context) return skip;
3968
3969 const auto *context = cb_access_context->GetCurrentAccessContext();
3970 assert(context);
3971 if (!context) return skip;
3972
3973 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3974
3975 if (dst_buffer) {
3976 ResourceAccessRange range = MakeRange(dstOffset, 4);
3977 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3978 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06003979 skip |=
3980 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3981 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
3982 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003983 }
3984 }
3985 return skip;
3986}
3987
3988void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3989 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003990 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06003991 auto *cb_access_context = GetAccessContext(commandBuffer);
3992 assert(cb_access_context);
3993 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
3994 auto *context = cb_access_context->GetCurrentAccessContext();
3995 assert(context);
3996
3997 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3998
3999 if (dst_buffer) {
4000 ResourceAccessRange range = MakeRange(dstOffset, 4);
4001 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4002 }
4003}