blob: bbaafb103cf199e4fd9e1d8bbdd586cf309b6c61 [file] [log] [blame]
John Zulauf9cb530d2019-09-30 14:14:10 -06001/* Copyright (c) 2019 The Khronos Group Inc.
2 * Copyright (c) 2019 Valve Corporation
3 * Copyright (c) 2019 LunarG, Inc.
4 *
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
55static const char *string_SyncHazard(SyncHazard hazard) {
56 switch (hazard) {
57 case SyncHazard::NONE:
58 return "NONR";
59 break;
60 case SyncHazard::READ_AFTER_WRITE:
61 return "READ_AFTER_WRITE";
62 break;
63 case SyncHazard::WRITE_AFTER_READ:
64 return "WRITE_AFTER_READ";
65 break;
66 case SyncHazard::WRITE_AFTER_WRITE:
67 return "WRITE_AFTER_WRITE";
68 break;
John Zulauf2f952d22020-02-10 11:34:51 -070069 case SyncHazard::READ_RACING_WRITE:
70 return "READ_RACING_WRITE";
71 break;
72 case SyncHazard::WRITE_RACING_WRITE:
73 return "WRITE_RACING_WRITE";
74 break;
75 case SyncHazard::WRITE_RACING_READ:
76 return "WRITE_RACING_READ";
77 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060078 default:
79 assert(0);
80 }
81 return "INVALID HAZARD";
82}
83
locke-lunarg3c038002020-04-30 23:08:08 -060084inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
85 if (size == VK_WHOLE_SIZE) {
86 return (whole_size - offset);
87 }
88 return size;
89}
90
John Zulauf16adfc92020-04-08 10:28:33 -060091template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -060092static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -060093 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
94}
95
John Zulauf355e49b2020-04-24 15:11:15 -060096static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -060097
John Zulauf0cb5be22020-01-23 12:18:22 -070098// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
99VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
100 VkPipelineStageFlags expanded = stage_mask;
101 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
102 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
103 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
104 if (all_commands.first & queue_flags) {
105 expanded |= all_commands.second;
106 }
107 }
108 }
109 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
110 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
111 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
112 }
113 return expanded;
114}
115
John Zulauf36bcf6a2020-02-03 15:12:52 -0700116VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
117 std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
118 VkPipelineStageFlags unscanned = stage_mask;
119 VkPipelineStageFlags related = 0;
120 for (const auto entry : map) {
121 const auto stage = entry.first;
122 if (stage & unscanned) {
123 related = related | entry.second;
124 unscanned = unscanned & ~stage;
125 if (!unscanned) break;
126 }
127 }
128 return related;
129}
130
131VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
132 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
133}
134
135VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
136 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
137}
138
John Zulauf5c5e88d2019-12-26 11:22:02 -0700139static const ResourceAccessRange full_range(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700140
John Zulauf355e49b2020-04-24 15:11:15 -0600141// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
142const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = {
143 AccessContext::AddressType::kLinearAddress, AccessContext::AddressType::kIdealizedAddress};
144
John Zulauf540266b2020-04-06 18:54:53 -0600145AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
146 const std::vector<SubpassDependencyGraphNode> &dependencies,
147 const std::vector<AccessContext> &contexts, AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600148 Reset();
149 const auto &subpass_dep = dependencies[subpass];
150 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600151 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600152 for (const auto &prev_dep : subpass_dep.prev) {
153 assert(prev_dep.dependency);
154 const auto dep = *prev_dep.dependency;
John Zulauf540266b2020-04-06 18:54:53 -0600155 prev_.emplace_back(const_cast<AccessContext *>(&contexts[dep.srcSubpass]), queue_flags, dep);
John Zulauf355e49b2020-04-24 15:11:15 -0600156 prev_by_subpass_[dep.srcSubpass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700157 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600158
159 async_.reserve(subpass_dep.async.size());
160 for (const auto async_subpass : subpass_dep.async) {
John Zulauf540266b2020-04-06 18:54:53 -0600161 async_.emplace_back(const_cast<AccessContext *>(&contexts[async_subpass]));
John Zulauf3d84f1b2020-03-09 13:33:25 -0600162 }
John Zulaufe5da6e52020-03-18 15:32:18 -0600163 if (subpass_dep.barrier_from_external) {
164 src_external_ = TrackBack(external_context, queue_flags, *subpass_dep.barrier_from_external);
165 } else {
166 src_external_ = TrackBack();
167 }
168 if (subpass_dep.barrier_to_external) {
169 dst_external_ = TrackBack(this, queue_flags, *subpass_dep.barrier_to_external);
170 } else {
171 dst_external_ = TrackBack();
John Zulauf3d84f1b2020-03-09 13:33:25 -0600172 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700173}
174
John Zulauf5f13a792020-03-10 07:31:21 -0600175template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600176HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600177 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600178 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600179 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600180
181 HazardResult hazard;
182 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
183 hazard = detector.Detect(prev);
184 }
185 return hazard;
186}
187
John Zulauf3d84f1b2020-03-09 13:33:25 -0600188// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
189// the DAG of the contexts (for example subpasses)
190template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600191HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
192 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600193 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600194
John Zulauf355e49b2020-04-24 15:11:15 -0600195 if (static_cast<uint32_t>(options) | DetectOptions::kDetectAsync) {
196 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
197 // so we'll check these first
198 for (const auto &async_context : async_) {
199 hazard = async_context->DetectAsyncHazard(type, detector, range);
200 if (hazard.hazard) return hazard;
201 }
John Zulauf5f13a792020-03-10 07:31:21 -0600202 }
203
John Zulauf69133422020-05-20 14:55:53 -0600204 const bool detect_prev = (static_cast<uint32_t>(options) | DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600205
John Zulauf69133422020-05-20 14:55:53 -0600206 const auto &accesses = GetAccessStateMap(type);
207 const auto from = accesses.lower_bound(range);
208 const auto to = accesses.upper_bound(range);
209 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600210
John Zulauf69133422020-05-20 14:55:53 -0600211 for (auto pos = from; pos != to; ++pos) {
212 // Cover any leading gap, or gap between entries
213 if (detect_prev) {
214 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
215 // Cover any leading gap, or gap between entries
216 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600217 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600218 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600219 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600220 if (hazard.hazard) return hazard;
221 }
John Zulauf69133422020-05-20 14:55:53 -0600222 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
223 gap.begin = pos->first.end;
224 }
225
226 hazard = detector.Detect(pos);
227 if (hazard.hazard) return hazard;
228 }
229
230 if (detect_prev) {
231 // Detect in the trailing empty as needed
232 gap.end = range.end;
233 if (gap.non_empty()) {
234 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600235 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600236 }
237
238 return hazard;
239}
240
241// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
242template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600243HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600244 auto &accesses = GetAccessStateMap(type);
245 const auto from = accesses.lower_bound(range);
246 const auto to = accesses.upper_bound(range);
247
John Zulauf3d84f1b2020-03-09 13:33:25 -0600248 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600249 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
250 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600251 }
John Zulauf16adfc92020-04-08 10:28:33 -0600252
John Zulauf3d84f1b2020-03-09 13:33:25 -0600253 return hazard;
254}
255
John Zulauf355e49b2020-04-24 15:11:15 -0600256// Returns the last resolved entry
257static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
258 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
259 const SyncBarrier *barrier) {
260 auto at = entry;
261 for (auto pos = first; pos != last; ++pos) {
262 // Every member of the input iterator range must fit within the remaining portion of entry
263 assert(at->first.includes(pos->first));
264 assert(at != dest->end());
265 // Trim up at to the same size as the entry to resolve
266 at = sparse_container::split(at, *dest, pos->first);
267 auto access = pos->second;
268 if (barrier) {
269 access.ApplyBarrier(*barrier);
270 }
271 at->second.Resolve(access);
272 ++at; // Go to the remaining unused section of entry
273 }
274}
275
276void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, const SyncBarrier *barrier,
277 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
278 bool recur_to_infill) const {
279 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
280 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf16adfc92020-04-08 10:28:33 -0600281 if (current->pos_B->valid) {
282 const auto &src_pos = current->pos_B->lower_bound;
John Zulauf355e49b2020-04-24 15:11:15 -0600283 auto access = src_pos->second;
284 if (barrier) {
285 access.ApplyBarrier(*barrier);
286 }
John Zulauf16adfc92020-04-08 10:28:33 -0600287 if (current->pos_A->valid) {
288 current.trim_A();
John Zulauf355e49b2020-04-24 15:11:15 -0600289 current->pos_A->lower_bound->second.Resolve(access);
John Zulauf5f13a792020-03-10 07:31:21 -0600290 } else {
John Zulauf355e49b2020-04-24 15:11:15 -0600291 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, access));
292 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600293 }
John Zulauf16adfc92020-04-08 10:28:33 -0600294 } else {
295 // we have to descend to fill this gap
296 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600297 if (current->pos_A->valid) {
298 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
299 ResourceAccessRangeMap gap_map;
300 ResolvePreviousAccess(type, current->range, &gap_map, infill_state);
301 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier);
302 } else {
303 // There isn't anything in dest in current->range, so we can accumulate directly into it.
304 ResolvePreviousAccess(type, current->range, resolve_map, infill_state);
305 if (barrier) {
306 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
307 for (auto pos = resolve_map->lower_bound(current->range); pos != current->pos_A->lower_bound; ++pos) {
308 pos->second.ApplyBarrier(*barrier);
309 }
310 }
311 }
312 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
313 // iterator of the outer while.
314
315 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
316 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
317 // we stepped on the dest map
318 const auto seek_to = current->range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
319 current.invalidate_A(); // Changes current->range
320 current.seek(seek_to);
321 } else if (!current->pos_A->valid && infill_state) {
322 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
323 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
324 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600325 }
John Zulauf5f13a792020-03-10 07:31:21 -0600326 }
John Zulauf16adfc92020-04-08 10:28:33 -0600327 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600328 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600329}
330
John Zulauf355e49b2020-04-24 15:11:15 -0600331void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
332 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600333 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600334 if (range.non_empty() && infill_state) {
335 descent_map->insert(std::make_pair(range, *infill_state));
336 }
337 } else {
338 // Look for something to fill the gap further along.
339 for (const auto &prev_dep : prev_) {
John Zulauf355e49b2020-04-24 15:11:15 -0600340 prev_dep.context->ResolveAccessRange(type, range, &prev_dep.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600341 }
342
John Zulaufe5da6e52020-03-18 15:32:18 -0600343 if (src_external_.context) {
John Zulauf355e49b2020-04-24 15:11:15 -0600344 src_external_.context->ResolveAccessRange(type, range, &src_external_.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600345 }
346 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600347}
348
John Zulauf16adfc92020-04-08 10:28:33 -0600349AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600350 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600351}
352
353VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) {
354 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
355}
356
John Zulauf355e49b2020-04-24 15:11:15 -0600357static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
John Zulauf16adfc92020-04-08 10:28:33 -0600358
John Zulauf1507ee42020-05-18 11:33:09 -0600359static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
360 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
361 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
362 return stage_access;
363}
364static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
365 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
366 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
367 return stage_access;
368}
369
John Zulauf540266b2020-04-06 18:54:53 -0600370void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
John Zulauf355e49b2020-04-24 15:11:15 -0600371 AddressType address_type, ResourceAccessRangeMap *descent_map,
372 const ResourceAccessState *infill_state) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600373 if (!SimpleBinding(image_state)) return;
374
John Zulauf62f10592020-04-03 12:20:02 -0600375 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
locke-lunargae26eac2020-04-16 15:29:05 -0600376 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600377 image_state.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600378 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf62f10592020-04-03 12:20:02 -0600379 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600380 ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state);
John Zulauf62f10592020-04-03 12:20:02 -0600381 }
382}
383
John Zulauf1507ee42020-05-18 11:33:09 -0600384bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
385 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
386 uint32_t subpass) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600387 bool skip = false;
388 const auto &transitions = rp_state.subpass_transitions[subpass];
389 for (const auto &transition : transitions) {
John Zulauf1507ee42020-05-18 11:33:09 -0600390 auto hazard = DetectSubpassTransitionHazard(transition, attachment_views);
John Zulauf355e49b2020-04-24 15:11:15 -0600391 if (hazard.hazard) {
392 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
393 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " image layout transition.",
394 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment);
395 }
396 }
397 return skip;
398}
399
John Zulauf1507ee42020-05-18 11:33:09 -0600400bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
401 const VkRect2D &render_area,
402 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
403 uint32_t subpass) const {
404 bool skip = false;
405 const auto *attachment_ci = rp_state.createInfo.pAttachments;
406 VkExtent3D extent = CastTo3D(render_area.extent);
407 VkOffset3D offset = CastTo3D(render_area.offset);
408 const auto external_access_scope = src_external_.barrier.dst_access_scope;
409 HazardResult hazard;
410
411 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
412 if (subpass == rp_state.attachment_first_subpass[i]) {
413 if (attachment_views[i] == nullptr) continue;
414 const IMAGE_VIEW_STATE &view = *attachment_views[i];
415 const IMAGE_STATE *image = view.image_state.get();
416 if (image == nullptr) continue;
417 const auto &ci = attachment_ci[i];
418 const bool is_transition = rp_state.attachment_first_is_transition[i];
419
420 // Need check in the following way
421 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
422 // vs. transition
423 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
424 // for each aspect loaded.
425
426 const bool has_depth = FormatHasDepth(ci.format);
427 const bool has_stencil = FormatHasDepth(ci.format);
428 const bool is_color = !(has_depth || has_stencil);
429
430 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
431 const SyncStageAccessFlags load_mask = (has_depth || is_color) ? SyncStageAccess::Flags(load_index) : 0U;
432 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
433 const SyncStageAccessFlags stencil_mask = has_stencil ? SyncStageAccess::Flags(stencil_load_index) : 0U;
434
435 const char *aspect = nullptr;
436 if (is_transition) {
437 // For transition w
438 SyncHazard transition_hazard = SyncHazard::NONE;
439 bool checked_stencil = false;
440 if (load_mask) {
441 if ((load_mask & external_access_scope) != load_mask) {
442 transition_hazard =
443 SyncStageAccess::HasWrite(load_mask) ? SyncHazard::WRITE_AFTER_WRITE : SyncHazard::READ_AFTER_WRITE;
444 aspect = is_color ? "color" : "depth";
445 }
446 if (!transition_hazard && stencil_mask) {
447 if ((stencil_mask & external_access_scope) != stencil_mask) {
448 transition_hazard = SyncStageAccess::HasWrite(stencil_mask) ? SyncHazard::WRITE_AFTER_WRITE
449 : SyncHazard::READ_AFTER_WRITE;
450 aspect = "stencil";
451 checked_stencil = true;
452 }
453 }
454 }
455 if (transition_hazard) {
456 // Hazard vs. ILT
457 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
458 skip |=
459 sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
460 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
461 " aspect %s during load with loadOp %s.",
462 func_name, string_SyncHazard(transition_hazard), subpass, i, aspect, load_op_string);
463 }
464 } else {
465 auto hazard_range = view.normalized_subresource_range;
466 bool checked_stencil = false;
467 if (is_color) {
468 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, offset, extent);
469 aspect = "color";
470 } else {
471 if (has_depth) {
472 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
473 hazard = DetectHazard(*image, load_index, hazard_range, offset, extent);
474 aspect = "depth";
475 }
476 if (!hazard.hazard && has_stencil) {
477 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
478 hazard = DetectHazard(*image, stencil_load_index, hazard_range, offset, extent);
479 aspect = "stencil";
480 checked_stencil = true;
481 }
482 }
483
484 if (hazard.hazard) {
485 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
486 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
487 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
488 " aspect %s during load with loadOp %s.",
489 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
490 }
491 }
492 }
493 }
494 return skip;
495}
496
John Zulauf3d84f1b2020-03-09 13:33:25 -0600497class HazardDetector {
498 SyncStageAccessIndex usage_index_;
499
500 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600501 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600502 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
503 return pos->second.DetectAsyncHazard(usage_index_);
504 }
505 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
506};
507
John Zulauf69133422020-05-20 14:55:53 -0600508class HazardDetectorWithOrdering {
509 const SyncStageAccessIndex usage_index_;
510 const SyncOrderingBarrier &ordering_;
511
512 public:
513 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
514 return pos->second.DetectHazard(usage_index_, ordering_);
515 }
516 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
517 return pos->second.DetectAsyncHazard(usage_index_);
518 }
519 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
520 : usage_index_(usage), ordering_(ordering) {}
521};
522
John Zulauf16adfc92020-04-08 10:28:33 -0600523HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600524 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600525 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600526 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600527}
528
John Zulauf16adfc92020-04-08 10:28:33 -0600529HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600530 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600531 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -0600532 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -0600533}
534
John Zulauf69133422020-05-20 14:55:53 -0600535template <typename Detector>
536HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
537 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
538 const VkExtent3D &extent, DetectOptions options) const {
539 if (!SimpleBinding(image)) return HazardResult();
540 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
541 const auto address_type = ImageAddressType(image);
542 const auto base_address = ResourceBaseAddress(image);
543 for (; range_gen->non_empty(); ++range_gen) {
544 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
545 if (hazard.hazard) return hazard;
546 }
547 return HazardResult();
548}
549
John Zulauf540266b2020-04-06 18:54:53 -0600550HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
551 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
552 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700553 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
554 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -0600555 return DetectHazard(image, current_usage, subresource_range, offset, extent);
556}
557
558HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
559 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
560 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -0600561 HazardDetector detector(current_usage);
562 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
563}
564
565HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
566 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
567 const VkOffset3D &offset, const VkExtent3D &extent) const {
568 HazardDetectorWithOrdering detector(current_usage, ordering);
569 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -0600570}
571
John Zulauf3d84f1b2020-03-09 13:33:25 -0600572class BarrierHazardDetector {
573 public:
574 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
575 SyncStageAccessFlags src_access_scope)
576 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
577
John Zulauf5f13a792020-03-10 07:31:21 -0600578 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
579 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -0700580 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600581 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
582 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
583 return pos->second.DetectAsyncHazard(usage_index_);
584 }
585
586 private:
587 SyncStageAccessIndex usage_index_;
588 VkPipelineStageFlags src_exec_scope_;
589 SyncStageAccessFlags src_access_scope_;
590};
591
John Zulauf16adfc92020-04-08 10:28:33 -0600592HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -0600593 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600594 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600595 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -0600596 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -0700597}
598
John Zulauf16adfc92020-04-08 10:28:33 -0600599HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600600 SyncStageAccessFlags src_access_scope,
601 const VkImageSubresourceRange &subresource_range,
602 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -0600603 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
604 VkOffset3D zero_offset = {0, 0, 0};
605 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -0700606}
607
John Zulauf355e49b2020-04-24 15:11:15 -0600608HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
609 SyncStageAccessFlags src_stage_accesses,
610 const VkImageMemoryBarrier &barrier) const {
611 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
612 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
613 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
614}
615
John Zulauf9cb530d2019-09-30 14:14:10 -0600616template <typename Flags, typename Map>
617SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
618 SyncStageAccessFlags scope = 0;
619 for (const auto &bit_scope : map) {
620 if (flag_mask < bit_scope.first) break;
621
622 if (flag_mask & bit_scope.first) {
623 scope |= bit_scope.second;
624 }
625 }
626 return scope;
627}
628
629SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
630 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
631}
632
633SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
634 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
635}
636
637// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
638SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -0600639 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
640 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
641 // 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 -0600642 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
643}
644
645template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -0700646void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf9cb530d2019-09-30 14:14:10 -0600647 // TODO -- region/mem-range accuracte update
648 auto pos = accesses->lower_bound(range);
649 if (pos == accesses->end() || !pos->first.intersects(range)) {
650 // The range is empty, fill it with a default value.
651 pos = action.Infill(accesses, pos, range);
652 } else if (range.begin < pos->first.begin) {
653 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -0700654 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -0600655 } else if (pos->first.begin < range.begin) {
656 // Trim the beginning if needed
657 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
658 ++pos;
659 }
660
661 const auto the_end = accesses->end();
662 while ((pos != the_end) && pos->first.intersects(range)) {
663 if (pos->first.end > range.end) {
664 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
665 }
666
667 pos = action(accesses, pos);
668 if (pos == the_end) break;
669
670 auto next = pos;
671 ++next;
672 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
673 // Need to infill if next is disjoint
674 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -0700675 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -0600676 next = action.Infill(accesses, next, new_range);
677 }
678 pos = next;
679 }
680}
681
682struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700683 using Iterator = ResourceAccessRangeMap::iterator;
684 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600685 // this is only called on gaps, and never returns a gap.
686 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -0600687 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600688 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -0600689 }
John Zulauf5f13a792020-03-10 07:31:21 -0600690
John Zulauf5c5e88d2019-12-26 11:22:02 -0700691 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -0600692 auto &access_state = pos->second;
693 access_state.Update(usage, tag);
694 return pos;
695 }
696
John Zulauf16adfc92020-04-08 10:28:33 -0600697 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -0600698 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -0600699 : type(type_), context(context_), usage(usage_), tag(tag_) {}
700 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -0600701 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -0600702 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -0600703 const ResourceUsageTag &tag;
704};
705
706struct ApplyMemoryAccessBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700707 using Iterator = ResourceAccessRangeMap::iterator;
708 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -0600709
John Zulauf5c5e88d2019-12-26 11:22:02 -0700710 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -0600711 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700712 access_state.ApplyMemoryAccessBarrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -0600713 return pos;
714 }
715
John Zulauf36bcf6a2020-02-03 15:12:52 -0700716 ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_,
717 VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_)
718 : src_exec_scope(src_exec_scope_),
719 src_access_scope(src_access_scope_),
720 dst_exec_scope(dst_exec_scope_),
721 dst_access_scope(dst_access_scope_) {}
John Zulauf9cb530d2019-09-30 14:14:10 -0600722
John Zulauf36bcf6a2020-02-03 15:12:52 -0700723 VkPipelineStageFlags src_exec_scope;
724 SyncStageAccessFlags src_access_scope;
725 VkPipelineStageFlags dst_exec_scope;
726 SyncStageAccessFlags dst_access_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -0600727};
728
729struct ApplyGlobalBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700730 using Iterator = ResourceAccessRangeMap::iterator;
731 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -0600732
John Zulauf5c5e88d2019-12-26 11:22:02 -0700733 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -0600734 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700735 access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -0600736
737 for (const auto &functor : barrier_functor) {
738 functor(accesses, pos);
739 }
740 return pos;
741 }
742
John Zulauf36bcf6a2020-02-03 15:12:52 -0700743 ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope,
744 SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses,
John Zulauf9cb530d2019-09-30 14:14:10 -0600745 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers)
John Zulauf36bcf6a2020-02-03 15:12:52 -0700746 : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -0600747 // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier...
748 barrier_functor.reserve(memoryBarrierCount);
749 for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) {
750 const auto &barrier = pMemoryBarriers[barrier_index];
John Zulauf36bcf6a2020-02-03 15:12:52 -0700751 barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask),
752 dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
John Zulauf9cb530d2019-09-30 14:14:10 -0600753 }
754 }
755
John Zulauf36bcf6a2020-02-03 15:12:52 -0700756 const VkPipelineStageFlags src_exec_scope;
757 const VkPipelineStageFlags dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -0600758 std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor;
759};
760
John Zulauf355e49b2020-04-24 15:11:15 -0600761void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
762 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -0600763 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
764 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600765}
766
John Zulauf16adfc92020-04-08 10:28:33 -0600767void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -0600768 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -0600769 if (!SimpleBinding(buffer)) return;
770 const auto base_address = ResourceBaseAddress(buffer);
771 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
772}
John Zulauf355e49b2020-04-24 15:11:15 -0600773
John Zulauf540266b2020-04-06 18:54:53 -0600774void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -0600775 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -0600776 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -0600777 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -0600778 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600779 const auto address_type = ImageAddressType(image);
780 const auto base_address = ResourceBaseAddress(image);
781 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -0600782 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600783 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -0600784 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600785}
786
John Zulauf355e49b2020-04-24 15:11:15 -0600787void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
788 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
789 const VkExtent3D &extent, const ResourceUsageTag &tag) {
790 // TODO: replace the encoder/generator with offset3D aware versions
791 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
792 subresource.layerCount};
793 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
794}
795
John Zulauf540266b2020-04-06 18:54:53 -0600796template <typename Action>
797void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -0600798 if (!SimpleBinding(buffer)) return;
799 const auto base_address = ResourceBaseAddress(buffer);
800 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -0600801}
802
803template <typename Action>
804void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
805 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -0600806 if (!SimpleBinding(image)) return;
807 const auto address_type = ImageAddressType(image);
808 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -0600809
locke-lunargae26eac2020-04-16 15:29:05 -0600810 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600811 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -0600812
John Zulauf16adfc92020-04-08 10:28:33 -0600813 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -0600814 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600815 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -0600816 }
817}
818
819template <typename Action>
820void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
821 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -0600822 for (const auto address_type : kAddressTypes) {
823 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -0600824 }
825}
826
827void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -0600828 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
829 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -0600830 for (const auto address_type : kAddressTypes) {
John Zulauf355e49b2020-04-24 15:11:15 -0600831 context.ResolveAccessRange(address_type, full_range, &context.GetDstExternalTrackBack().barrier,
832 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -0600833 }
834 }
835}
836
John Zulauf355e49b2020-04-24 15:11:15 -0600837void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
838 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
839 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) {
840 const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
841 UpdateMemoryAccess(image, subresource_range, barrier_action);
842}
843
844// TODO: Plumb offset/extent throughout the image call stacks, with default injector overloads to preserved backwards compatiblity
845// as needed
846void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
847 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
848 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range,
849 bool layout_transition, const ResourceUsageTag &tag) {
850 if (layout_transition) {
851 UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent,
852 tag);
853 ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope,
854 subresource_range);
John Zulaufc9201222020-05-13 15:13:03 -0600855 } else {
856 ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range);
John Zulauf355e49b2020-04-24 15:11:15 -0600857 }
John Zulauf355e49b2020-04-24 15:11:15 -0600858}
859
860void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier,
861 const VkImageSubresourceRange &subresource_range, bool layout_transition,
862 const ResourceUsageTag &tag) {
863 ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope,
864 subresource_range, layout_transition, tag);
865}
866
867// Suitable only for *subpass* access contexts
868HazardResult AccessContext::DetectSubpassTransitionHazard(const RENDER_PASS_STATE::AttachmentTransition &transition,
869 const std::vector<const IMAGE_VIEW_STATE *> &attachments) const {
870 const auto *attach_view = attachments[transition.attachment];
871 if (!attach_view) return HazardResult();
872 const auto image_state = attach_view->image_state.get();
873 if (!image_state) return HazardResult();
874
875 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
876 // We should never ask for a transition from a context we don't have
877 assert(track_back);
878 assert(track_back->context);
879
880 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
881 auto hazard = track_back->context->DetectImageBarrierHazard(*image_state, track_back->barrier.src_exec_scope,
882 track_back->barrier.src_access_scope,
883 attach_view->normalized_subresource_range, kDetectPrevious);
884 if (!hazard.hazard) {
885 // The Async hazard check is against the current context's async set.
886 hazard = DetectImageBarrierHazard(*image_state, track_back->barrier.src_exec_scope, track_back->barrier.src_access_scope,
887 attach_view->normalized_subresource_range, kDetectAsync);
888 }
889 return hazard;
890}
891
892// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
893bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
894
895 const VkRenderPassBeginInfo *pRenderPassBegin,
896 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
897 const char *func_name) const {
898 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
899 bool skip = false;
900 uint32_t subpass = 0;
901 const auto &transitions = rp_state.subpass_transitions[subpass];
902 if (transitions.size()) {
903 const std::vector<AccessContext> empty_context_vector;
904 // Create context we can use to validate against...
905 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
906 const_cast<AccessContext *>(&cb_access_context_));
907
908 assert(pRenderPassBegin);
909 if (nullptr == pRenderPassBegin) return skip;
910
911 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
912 assert(fb_state);
913 if (nullptr == fb_state) return skip;
914
915 // Create a limited array of views (which we'll need to toss
916 std::vector<const IMAGE_VIEW_STATE *> views;
917 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
918 const auto attachment_count = count_attachment.first;
919 const auto *attachments = count_attachment.second;
920 views.resize(attachment_count, nullptr);
921 for (const auto &transition : transitions) {
922 assert(transition.attachment < attachment_count);
923 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
924 }
925
John Zulauf1507ee42020-05-18 11:33:09 -0600926 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, views, func_name, 0);
927 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, views, func_name, 0);
John Zulauf355e49b2020-04-24 15:11:15 -0600928 }
929 return skip;
930}
931
932bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
933 // TODO: Things to add here.
934 // Validate Preserve/Resolve attachments
935 bool skip = false;
John Zulauf1507ee42020-05-18 11:33:09 -0600936 skip |=
937 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -0600938
939 return skip;
940}
941
942bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
943 // TODO: Things to add here.
944 // Validate Preserve/Resolve attachments
945 bool skip = false;
946 skip |= current_renderpass_context_->ValidateFinalSubpassLayoutTransitions(*sync_state_, func_name);
947
948 return skip;
949}
950
951void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
952 assert(sync_state_);
953 if (!cb_state_) return;
954
955 // Create an access context the current renderpass.
956 render_pass_contexts_.emplace_back(&cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -0600957 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf355e49b2020-04-24 15:11:15 -0600958 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -0600959 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -0600960}
961
John Zulauf355e49b2020-04-24 15:11:15 -0600962void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -0600963 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -0600964 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -0600965 current_context_ = &current_renderpass_context_->CurrentContext();
966}
967
John Zulauf355e49b2020-04-24 15:11:15 -0600968void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
969 // TODO: Add layout load/store/resolve access (here or in RenderPassContext)
John Zulauf16adfc92020-04-08 10:28:33 -0600970 assert(current_renderpass_context_);
971 if (!current_renderpass_context_) return;
972
John Zulauf355e49b2020-04-24 15:11:15 -0600973 current_renderpass_context_->RecordEndRenderPass(tag);
974 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -0600975 current_renderpass_context_ = nullptr;
976}
977
John Zulauf1507ee42020-05-18 11:33:09 -0600978bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
979 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600980 bool skip = false;
981 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -0600982 const auto &next_context = subpass_contexts_[next_subpass];
983 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, attachment_views_, func_name, next_subpass);
984 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, attachment_views_, func_name, next_subpass);
John Zulauf355e49b2020-04-24 15:11:15 -0600985 return skip;
986}
987
988bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const char *func_name) const {
989 bool skip = false;
990
991 // Validate the "finalLayout" transitions to external
992 // Get them from where there we're hidding in the extra entry.
993 const auto &final_transitions = rp_state_->subpass_transitions.back();
994 for (const auto &transition : final_transitions) {
995 const auto &attach_view = attachment_views_[transition.attachment];
996 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
997 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
998 auto hazard = trackback.context->DetectImageBarrierHazard(
999 *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope,
1000 attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious);
1001 if (hazard.hazard) {
1002 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
1003 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
1004 " final image layout transition.",
1005 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment);
1006 }
1007 }
1008 return skip;
1009}
1010
1011void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
1012 // Add layout transitions...
1013 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
1014 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -06001015 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -06001016 for (const auto &transition : transitions) {
1017 const auto attachment_view = attachment_views_[transition.attachment];
1018 if (!attachment_view) continue;
1019 const auto image = attachment_view->image_state.get();
1020 if (!image) continue;
1021
1022 const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
John Zulaufc9201222020-05-13 15:13:03 -06001023 auto insert_pair = view_seen.insert(attachment_view);
1024 if (insert_pair.second) {
1025 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
1026 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag);
1027
1028 } else {
1029 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
1030 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
1031 auto barrier_to_transition = barrier->barrier;
1032 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
1033 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag);
1034 }
John Zulauf355e49b2020-04-24 15:11:15 -06001035 }
1036}
1037
John Zulauf1507ee42020-05-18 11:33:09 -06001038void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
1039 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
1040 auto &subpass_context = subpass_contexts_[current_subpass_];
1041 VkExtent3D extent = CastTo3D(render_area.extent);
1042 VkOffset3D offset = CastTo3D(render_area.offset);
1043
1044 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
1045 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
1046 if (attachment_views_[i] == nullptr) continue; // UNUSED
1047 const auto &view = *attachment_views_[i];
1048 const IMAGE_STATE *image = view.image_state.get();
1049 if (image == nullptr) continue;
1050
1051 const auto &ci = attachment_ci[i];
1052 const bool has_depth = FormatHasDepth(ci.format);
1053 const bool has_stencil = FormatHasDepth(ci.format);
1054 const bool is_color = !(has_depth || has_stencil);
1055
1056 if (is_color) {
1057 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
1058 extent, tag);
1059 } else {
1060 auto update_range = view.normalized_subresource_range;
1061 if (has_depth) {
1062 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1063 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
1064 }
1065 if (has_stencil) {
1066 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1067 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
1068 tag);
1069 }
1070 }
1071 }
1072 }
1073}
1074
John Zulauf355e49b2020-04-24 15:11:15 -06001075void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
1076 VkQueueFlags queue_flags, const ResourceUsageTag &tag) {
1077 current_subpass_ = 0;
1078 rp_state_ = cb_state.activeRenderPass;
1079 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
1080 // Add this for all subpasses here so that they exsist during next subpass validation
1081 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
1082 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context_);
1083 }
1084 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
1085
1086 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06001087 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001088}
John Zulauf1507ee42020-05-18 11:33:09 -06001089
1090void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001091 current_subpass_++;
1092 assert(current_subpass_ < subpass_contexts_.size());
1093 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06001094 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001095}
1096
1097void RenderPassAccessContext::RecordEndRenderPass(const ResourceUsageTag &tag) {
1098 // Export the accesses from the renderpass...
1099 external_context_->ResolveChildContexts(subpass_contexts_);
1100
1101 // Add the "finalLayout" transitions to external
1102 // Get them from where there we're hidding in the extra entry.
1103 const auto &final_transitions = rp_state_->subpass_transitions.back();
1104 for (const auto &transition : final_transitions) {
1105 const auto &attachment = attachment_views_[transition.attachment];
1106 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
1107 assert(external_context_ == last_trackback.context);
1108 external_context_->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier,
1109 attachment->normalized_subresource_range, true, tag);
1110 }
1111}
1112
John Zulauf3d84f1b2020-03-09 13:33:25 -06001113SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
1114 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
1115 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
1116 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
1117 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
1118 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
1119 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
1120}
1121
1122void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) {
1123 ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
1124 ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope);
1125}
1126
John Zulauf9cb530d2019-09-30 14:14:10 -06001127HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
1128 HazardResult hazard;
1129 auto usage = FlagBit(usage_index);
1130 if (IsRead(usage)) {
John Zulaufc9201222020-05-13 15:13:03 -06001131 if (last_write && IsWriteHazard(usage)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001132 hazard.Set(READ_AFTER_WRITE, write_tag);
1133 }
1134 } else {
1135 // Assume write
1136 // TODO determine what to do with READ-WRITE usage states if any
1137 // Write-After-Write check -- if we have a previous write to test against
1138 if (last_write && IsWriteHazard(usage)) {
1139 hazard.Set(WRITE_AFTER_WRITE, write_tag);
1140 } else {
John Zulauf69133422020-05-20 14:55:53 -06001141 // Look for casus belli for WAR
John Zulauf9cb530d2019-09-30 14:14:10 -06001142 const auto usage_stage = PipelineStageBit(usage_index);
1143 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
1144 if (IsReadHazard(usage_stage, last_reads[read_index])) {
1145 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
1146 break;
1147 }
1148 }
1149 }
1150 }
1151 return hazard;
1152}
1153
John Zulauf69133422020-05-20 14:55:53 -06001154HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
1155 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
1156 HazardResult hazard;
1157 const auto usage = FlagBit(usage_index);
1158 const bool write_is_ordered = (last_write & ordering.access_scope) == last_write; // Is true if no write, and that's good.
1159 if (IsRead(usage)) {
1160 if (!write_is_ordered && IsWriteHazard(usage)) {
1161 hazard.Set(READ_AFTER_WRITE, write_tag);
1162 }
1163 } else {
1164 if (!write_is_ordered && IsWriteHazard(usage)) {
1165 hazard.Set(WRITE_AFTER_WRITE, write_tag);
1166 } else {
1167 const auto usage_stage = PipelineStageBit(usage_index);
1168 const auto unordered_reads = last_read_stages & ~ordering.exec_scope;
1169 if (unordered_reads) {
1170 // Look for any WAR hazards outside the ordered set of stages
1171 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
1172 if (last_reads[read_index].stage & unordered_reads) {
1173 if (IsReadHazard(usage_stage, last_reads[read_index])) {
1174 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
1175 break;
1176 }
1177 }
1178 }
1179 }
1180 }
1181 }
1182 return hazard;
1183}
1184
John Zulauf2f952d22020-02-10 11:34:51 -07001185// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06001186HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07001187 HazardResult hazard;
1188 auto usage = FlagBit(usage_index);
1189 if (IsRead(usage)) {
1190 if (last_write != 0) {
1191 hazard.Set(READ_RACING_WRITE, write_tag);
1192 }
1193 } else {
1194 if (last_write != 0) {
1195 hazard.Set(WRITE_RACING_WRITE, write_tag);
1196 } else if (last_read_count > 0) {
1197 hazard.Set(WRITE_RACING_READ, last_reads[0].tag);
1198 }
1199 }
1200 return hazard;
1201}
1202
John Zulauf36bcf6a2020-02-03 15:12:52 -07001203HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1204 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07001205 // Only supporting image layout transitions for now
1206 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
1207 HazardResult hazard;
1208 if (last_write) {
1209 // If the previous write is *not* in the 1st access scope
1210 // *AND* the current barrier is not in the dependency chain
1211 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
1212 // then the barrier access is unsafe (R/W after W)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001213 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
John Zulauf0cb5be22020-01-23 12:18:22 -07001214 // TODO: Do we need a difference hazard name for this?
1215 hazard.Set(WRITE_AFTER_WRITE, write_tag);
1216 }
John Zulauf355e49b2020-04-24 15:11:15 -06001217 }
1218 if (!hazard.hazard) {
1219 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07001220 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07001221 const auto &read_access = last_reads[read_index];
1222 // If the read stage is not in the src sync sync
1223 // *AND* not execution chained with an existing sync barrier (that's the or)
1224 // then the barrier access is unsafe (R/W after R)
1225 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
1226 hazard.Set(WRITE_AFTER_READ, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07001227 break;
1228 }
1229 }
1230 }
1231 return hazard;
1232}
1233
John Zulauf5f13a792020-03-10 07:31:21 -06001234// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
1235// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
1236// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
1237void ResourceAccessState::Resolve(const ResourceAccessState &other) {
1238 if (write_tag.IsBefore(other.write_tag)) {
1239 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
1240 *this = other;
1241 } else if (!other.write_tag.IsBefore(write_tag)) {
1242 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
1243 // dependency chaining logic or any stage expansion)
1244 write_barriers |= other.write_barriers;
1245
1246 // Merge that read states
1247 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
1248 auto &other_read = other.last_reads[other_read_index];
1249 if (last_read_stages & other_read.stage) {
1250 // Merge in the barriers for read stages that exist in *both* this and other
1251 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
1252 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
1253 auto &my_read = last_reads[my_read_index];
1254 if (other_read.stage == my_read.stage) {
1255 if (my_read.tag.IsBefore(other_read.tag)) {
1256 my_read.tag = other_read.tag;
1257 }
1258 my_read.barriers |= other_read.barriers;
1259 break;
1260 }
1261 }
1262 } else {
1263 // The other read stage doesn't exist in this, so add it.
1264 last_reads[last_read_count] = other_read;
1265 last_read_count++;
1266 last_read_stages |= other_read.stage;
1267 }
1268 }
1269 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
1270 // it.
1271}
1272
John Zulauf9cb530d2019-09-30 14:14:10 -06001273void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
1274 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
1275 const auto usage_bit = FlagBit(usage_index);
1276 if (IsRead(usage_index)) {
1277 // Mulitple outstanding reads may be of interest and do dependency chains independently
1278 // However, for purposes of barrier tracking, only one read per pipeline stage matters
1279 const auto usage_stage = PipelineStageBit(usage_index);
1280 if (usage_stage & last_read_stages) {
1281 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
1282 ReadState &access = last_reads[read_index];
1283 if (access.stage == usage_stage) {
1284 access.barriers = 0;
1285 access.tag = tag;
1286 break;
1287 }
1288 }
1289 } else {
1290 // We don't have this stage in the list yet...
1291 assert(last_read_count < last_reads.size());
1292 ReadState &access = last_reads[last_read_count++];
1293 access.stage = usage_stage;
1294 access.barriers = 0;
1295 access.tag = tag;
1296 last_read_stages |= usage_stage;
1297 }
1298 } else {
1299 // Assume write
1300 // TODO determine what to do with READ-WRITE operations if any
1301 // Clobber last read and both sets of barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
1302 // if the last_reads/last_write were unsafe, we've reported them,
1303 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
1304 last_read_count = 0;
1305 last_read_stages = 0;
1306
1307 write_barriers = 0;
1308 write_dependency_chain = 0;
1309 write_tag = tag;
1310 last_write = usage_bit;
1311 }
1312}
John Zulauf5f13a792020-03-10 07:31:21 -06001313
John Zulauf9cb530d2019-09-30 14:14:10 -06001314void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
1315 // Execution Barriers only protect read operations
1316 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
1317 ReadState &access = last_reads[read_index];
1318 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
1319 if (srcStageMask & (access.stage | access.barriers)) {
1320 access.barriers |= dstStageMask;
1321 }
1322 }
1323 if (write_dependency_chain & srcStageMask) write_dependency_chain |= dstStageMask;
1324}
1325
John Zulauf36bcf6a2020-02-03 15:12:52 -07001326void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
1327 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001328 // Assuming we've applied the execution side of this barrier, we update just the write
1329 // The || implements the "dependency chain" logic for this barrier
John Zulauf36bcf6a2020-02-03 15:12:52 -07001330 if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) {
1331 write_barriers |= dst_access_scope;
1332 write_dependency_chain |= dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001333 }
1334}
1335
John Zulaufd1f85d42020-04-15 12:23:15 -06001336void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001337 auto *access_context = GetAccessContextNoInsert(command_buffer);
1338 if (access_context) {
1339 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06001340 }
1341}
1342
John Zulaufd1f85d42020-04-15 12:23:15 -06001343void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
1344 auto access_found = cb_access_state.find(command_buffer);
1345 if (access_found != cb_access_state.end()) {
1346 access_found->second->Reset();
1347 cb_access_state.erase(access_found);
1348 }
1349}
1350
John Zulauf540266b2020-04-06 18:54:53 -06001351void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07001352 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
1353 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06001354 const VkMemoryBarrier *pMemoryBarriers) {
1355 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07001356 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06001357 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06001358 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06001359}
1360
John Zulauf540266b2020-04-06 18:54:53 -06001361void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07001362 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
1363 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06001364 const VkBufferMemoryBarrier *barriers) {
1365 // TODO Implement this at subresource/memory_range accuracy
1366 for (uint32_t index = 0; index < barrier_count; index++) {
locke-lunarg3c038002020-04-30 23:08:08 -06001367 auto barrier = barriers[index];
John Zulauf9cb530d2019-09-30 14:14:10 -06001368 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
1369 if (!buffer) continue;
locke-lunarg3c038002020-04-30 23:08:08 -06001370 barrier.size = GetRealWholeSize(barrier.offset, barrier.size, buffer->createInfo.size);
John Zulauf16adfc92020-04-08 10:28:33 -06001371 ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06001372 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
1373 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
1374 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1375 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06001376 }
1377}
1378
John Zulauf540266b2020-04-06 18:54:53 -06001379void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
1380 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
1381 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06001382 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001383 for (uint32_t index = 0; index < barrier_count; index++) {
1384 const auto &barrier = barriers[index];
1385 const auto *image = Get<IMAGE_STATE>(barrier.image);
1386 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06001387 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06001388 bool layout_transition = barrier.oldLayout != barrier.newLayout;
1389 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
1390 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
1391 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
1392 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001393 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001394}
1395
1396bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
1397 uint32_t regionCount, const VkBufferCopy *pRegions) const {
1398 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001399 const auto *cb_context = GetAccessContext(commandBuffer);
1400 assert(cb_context);
1401 if (!cb_context) return skip;
1402 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06001403
John Zulauf3d84f1b2020-03-09 13:33:25 -06001404 // If we have no previous accesses, we have no hazards
1405 // TODO: make this sub-resource capable
1406 // TODO: make this general, and stuff it into templates/utility functions
1407 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001408 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001409
1410 for (uint32_t region = 0; region < regionCount; region++) {
1411 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06001412 if (src_buffer) {
locke-lunarg3c038002020-04-30 23:08:08 -06001413 ResourceAccessRange src_range =
1414 MakeRange(copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06001415 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001416 if (hazard.hazard) {
1417 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06001418 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
1419 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1420 report_data->FormatHandle(srcBuffer).c_str(), region);
John Zulauf9cb530d2019-09-30 14:14:10 -06001421 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001422 }
John Zulauf16adfc92020-04-08 10:28:33 -06001423 if (dst_buffer && !skip) {
locke-lunarg3c038002020-04-30 23:08:08 -06001424 ResourceAccessRange dst_range =
1425 MakeRange(copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf355e49b2020-04-24 15:11:15 -06001426 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001427 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001428 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
1429 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1430 report_data->FormatHandle(dstBuffer).c_str(), region);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001431 }
1432 }
1433 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06001434 }
1435 return skip;
1436}
1437
1438void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
1439 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001440 auto *cb_context = GetAccessContext(commandBuffer);
1441 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001442 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001443 auto *context = cb_context->GetCurrentAccessContext();
1444
John Zulauf9cb530d2019-09-30 14:14:10 -06001445 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06001446 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06001447
1448 for (uint32_t region = 0; region < regionCount; region++) {
1449 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06001450 if (src_buffer) {
locke-lunarg3c038002020-04-30 23:08:08 -06001451 ResourceAccessRange src_range =
1452 MakeRange(copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06001453 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001454 }
John Zulauf16adfc92020-04-08 10:28:33 -06001455 if (dst_buffer) {
locke-lunarg3c038002020-04-30 23:08:08 -06001456 ResourceAccessRange dst_range =
1457 MakeRange(copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06001458 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001459 }
1460 }
1461}
1462
1463bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1464 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
1465 const VkImageCopy *pRegions) const {
1466 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001467 const auto *cb_access_context = GetAccessContext(commandBuffer);
1468 assert(cb_access_context);
1469 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07001470
John Zulauf3d84f1b2020-03-09 13:33:25 -06001471 const auto *context = cb_access_context->GetCurrentAccessContext();
1472 assert(context);
1473 if (!context) return skip;
1474
1475 const auto *src_image = Get<IMAGE_STATE>(srcImage);
1476 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001477 for (uint32_t region = 0; region < regionCount; region++) {
1478 const auto &copy_region = pRegions[region];
1479 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001480 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001481 copy_region.srcOffset, copy_region.extent);
1482 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001483 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
1484 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1485 report_data->FormatHandle(srcImage).c_str(), region);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001486 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001487 }
1488
1489 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07001490 VkExtent3D dst_copy_extent =
1491 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001492 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07001493 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001494 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001495 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
1496 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1497 report_data->FormatHandle(dstImage).c_str(), region);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001498 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07001499 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07001500 }
1501 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001502
John Zulauf5c5e88d2019-12-26 11:22:02 -07001503 return skip;
1504}
1505
1506void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1507 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
1508 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001509 auto *cb_access_context = GetAccessContext(commandBuffer);
1510 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001511 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001512 auto *context = cb_access_context->GetCurrentAccessContext();
1513 assert(context);
1514
John Zulauf5c5e88d2019-12-26 11:22:02 -07001515 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001516 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001517
1518 for (uint32_t region = 0; region < regionCount; region++) {
1519 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06001520 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001521 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
1522 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001523 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001524 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07001525 VkExtent3D dst_copy_extent =
1526 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001527 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
1528 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001529 }
1530 }
1531}
1532
1533bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1534 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1535 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1536 uint32_t bufferMemoryBarrierCount,
1537 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1538 uint32_t imageMemoryBarrierCount,
1539 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
1540 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001541 const auto *cb_access_context = GetAccessContext(commandBuffer);
1542 assert(cb_access_context);
1543 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07001544
John Zulauf3d84f1b2020-03-09 13:33:25 -06001545 const auto *context = cb_access_context->GetCurrentAccessContext();
1546 assert(context);
1547 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07001548
John Zulauf3d84f1b2020-03-09 13:33:25 -06001549 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07001550 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
1551 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07001552 // Validate Image Layout transitions
1553 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
1554 const auto &barrier = pImageMemoryBarriers[index];
1555 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
1556 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
1557 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06001558 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07001559 if (hazard.hazard) {
1560 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06001561 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
1562 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s", string_SyncHazard(hazard.hazard),
1563 index, report_data->FormatHandle(barrier.image).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07001564 }
1565 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001566
1567 return skip;
1568}
1569
1570void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1571 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1572 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1573 uint32_t bufferMemoryBarrierCount,
1574 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1575 uint32_t imageMemoryBarrierCount,
1576 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001577 auto *cb_access_context = GetAccessContext(commandBuffer);
1578 assert(cb_access_context);
1579 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06001580 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001581 auto access_context = cb_access_context->GetCurrentAccessContext();
1582 assert(access_context);
1583 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06001584
John Zulauf3d84f1b2020-03-09 13:33:25 -06001585 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07001586 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001587 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07001588 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
1589 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
1590 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001591 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
1592 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06001593 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001594 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001595
1596 // 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 -06001597 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07001598 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06001599}
1600
1601void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
1602 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
1603 // The state tracker sets up the device state
1604 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
1605
John Zulauf5f13a792020-03-10 07:31:21 -06001606 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
1607 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06001608 // TODO: Find a good way to do this hooklessly.
1609 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
1610 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
1611 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
1612
John Zulaufd1f85d42020-04-15 12:23:15 -06001613 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
1614 sync_device_state->ResetCommandBufferCallback(command_buffer);
1615 });
1616 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
1617 sync_device_state->FreeCommandBufferCallback(command_buffer);
1618 });
John Zulauf9cb530d2019-09-30 14:14:10 -06001619}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001620
John Zulauf355e49b2020-04-24 15:11:15 -06001621bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1622 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
1623 bool skip = false;
1624 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
1625 auto cb_context = GetAccessContext(commandBuffer);
1626
1627 if (rp_state && cb_context) {
1628 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
1629 }
1630
1631 return skip;
1632}
1633
1634bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1635 VkSubpassContents contents) const {
1636 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
1637 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
1638 subpass_begin_info.contents = contents;
1639 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
1640 return skip;
1641}
1642
1643bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1644 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
1645 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1646 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
1647 return skip;
1648}
1649
1650bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1651 const VkRenderPassBeginInfo *pRenderPassBegin,
1652 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
1653 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1654 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
1655 return skip;
1656}
1657
John Zulauf3d84f1b2020-03-09 13:33:25 -06001658void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
1659 VkResult result) {
1660 // The state tracker sets up the command buffer state
1661 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
1662
1663 // Create/initialize the structure that trackers accesses at the command buffer scope.
1664 auto cb_access_context = GetAccessContext(commandBuffer);
1665 assert(cb_access_context);
1666 cb_access_context->Reset();
1667}
1668
1669void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06001670 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001671 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06001672 if (cb_context) {
1673 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06001674 }
1675}
1676
1677void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1678 VkSubpassContents contents) {
1679 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
1680 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
1681 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06001682 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001683}
1684
1685void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1686 const VkSubpassBeginInfo *pSubpassBeginInfo) {
1687 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001688 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001689}
1690
1691void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1692 const VkRenderPassBeginInfo *pRenderPassBegin,
1693 const VkSubpassBeginInfo *pSubpassBeginInfo) {
1694 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001695 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
1696}
1697
1698bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1699 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
1700 bool skip = false;
1701
1702 auto cb_context = GetAccessContext(commandBuffer);
1703 assert(cb_context);
1704 auto cb_state = cb_context->GetCommandBufferState();
1705 if (!cb_state) return skip;
1706
1707 auto rp_state = cb_state->activeRenderPass;
1708 if (!rp_state) return skip;
1709
1710 skip |= cb_context->ValidateNextSubpass(func_name);
1711
1712 return skip;
1713}
1714
1715bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
1716 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
1717 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
1718 subpass_begin_info.contents = contents;
1719 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
1720 return skip;
1721}
1722
1723bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1724 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
1725 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
1726 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
1727 return skip;
1728}
1729
1730bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
1731 const VkSubpassEndInfo *pSubpassEndInfo) const {
1732 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
1733 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
1734 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001735}
1736
1737void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06001738 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001739 auto cb_context = GetAccessContext(commandBuffer);
1740 assert(cb_context);
1741 auto cb_state = cb_context->GetCommandBufferState();
1742 if (!cb_state) return;
1743
1744 auto rp_state = cb_state->activeRenderPass;
1745 if (!rp_state) return;
1746
John Zulauf355e49b2020-04-24 15:11:15 -06001747 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06001748}
1749
1750void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
1751 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
1752 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
1753 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06001754 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001755}
1756
1757void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
1758 const VkSubpassEndInfo *pSubpassEndInfo) {
1759 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001760 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001761}
1762
1763void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
1764 const VkSubpassEndInfo *pSubpassEndInfo) {
1765 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001766 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001767}
1768
John Zulauf355e49b2020-04-24 15:11:15 -06001769bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
1770 const char *func_name) const {
1771 bool skip = false;
1772
1773 auto cb_context = GetAccessContext(commandBuffer);
1774 assert(cb_context);
1775 auto cb_state = cb_context->GetCommandBufferState();
1776 if (!cb_state) return skip;
1777
1778 auto rp_state = cb_state->activeRenderPass;
1779 if (!rp_state) return skip;
1780
1781 skip |= cb_context->ValidateEndRenderpass(func_name);
1782 return skip;
1783}
1784
1785bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
1786 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
1787 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
1788 return skip;
1789}
1790
1791bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
1792 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
1793 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
1794 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
1795 return skip;
1796}
1797
1798bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
1799 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
1800 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
1801 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
1802 return skip;
1803}
1804
1805void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
1806 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06001807 // Resolve the all subpass contexts to the command buffer contexts
1808 auto cb_context = GetAccessContext(commandBuffer);
1809 assert(cb_context);
1810 auto cb_state = cb_context->GetCommandBufferState();
1811 if (!cb_state) return;
1812
1813 const auto *rp_state = cb_state->activeRenderPass;
1814 if (!rp_state) return;
1815
John Zulauf355e49b2020-04-24 15:11:15 -06001816 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06001817}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001818
1819void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
1820 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06001821 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001822}
1823
1824void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
1825 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001826 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001827}
1828
1829void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
1830 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001831 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001832}
locke-lunarga19c71d2020-03-02 18:17:04 -07001833
1834bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
1835 VkImageLayout dstImageLayout, uint32_t regionCount,
1836 const VkBufferImageCopy *pRegions) const {
1837 bool skip = false;
1838 const auto *cb_access_context = GetAccessContext(commandBuffer);
1839 assert(cb_access_context);
1840 if (!cb_access_context) return skip;
1841
1842 const auto *context = cb_access_context->GetCurrentAccessContext();
1843 assert(context);
1844 if (!context) return skip;
1845
1846 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07001847 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
1848
1849 for (uint32_t region = 0; region < regionCount; region++) {
1850 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06001851 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06001852 ResourceAccessRange src_range =
1853 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06001854 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07001855 if (hazard.hazard) {
1856 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06001857 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
1858 "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07001859 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region);
1860 }
1861 }
1862 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001863 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07001864 copy_region.imageOffset, copy_region.imageExtent);
1865 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001866 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
1867 "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07001868 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region);
1869 }
1870 if (skip) break;
1871 }
1872 if (skip) break;
1873 }
1874 return skip;
1875}
1876
1877void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
1878 VkImageLayout dstImageLayout, uint32_t regionCount,
1879 const VkBufferImageCopy *pRegions) {
1880 auto *cb_access_context = GetAccessContext(commandBuffer);
1881 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001882 const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07001883 auto *context = cb_access_context->GetCurrentAccessContext();
1884 assert(context);
1885
1886 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06001887 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07001888
1889 for (uint32_t region = 0; region < regionCount; region++) {
1890 const auto &copy_region = pRegions[region];
1891 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06001892 ResourceAccessRange src_range =
1893 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06001894 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001895 }
1896 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001897 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06001898 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001899 }
1900 }
1901}
1902
1903bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
1904 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
1905 const VkBufferImageCopy *pRegions) const {
1906 bool skip = false;
1907 const auto *cb_access_context = GetAccessContext(commandBuffer);
1908 assert(cb_access_context);
1909 if (!cb_access_context) return skip;
1910
1911 const auto *context = cb_access_context->GetCurrentAccessContext();
1912 assert(context);
1913 if (!context) return skip;
1914
1915 const auto *src_image = Get<IMAGE_STATE>(srcImage);
1916 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
1917 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
1918 for (uint32_t region = 0; region < regionCount; region++) {
1919 const auto &copy_region = pRegions[region];
1920 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001921 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07001922 copy_region.imageOffset, copy_region.imageExtent);
1923 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001924 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
1925 "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07001926 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region);
1927 }
1928 }
1929 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06001930 ResourceAccessRange dst_range =
1931 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06001932 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07001933 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001934 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
1935 "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07001936 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region);
1937 }
1938 }
1939 if (skip) break;
1940 }
1941 return skip;
1942}
1943
1944void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1945 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
1946 auto *cb_access_context = GetAccessContext(commandBuffer);
1947 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001948 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER);
locke-lunarga19c71d2020-03-02 18:17:04 -07001949 auto *context = cb_access_context->GetCurrentAccessContext();
1950 assert(context);
1951
1952 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07001953 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
1954 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 -06001955 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07001956
1957 for (uint32_t region = 0; region < regionCount; region++) {
1958 const auto &copy_region = pRegions[region];
1959 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001960 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06001961 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001962 }
1963 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06001964 ResourceAccessRange dst_range =
1965 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06001966 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001967 }
1968 }
1969}
1970
1971bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1972 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
1973 const VkImageBlit *pRegions, VkFilter filter) const {
1974 bool skip = false;
1975 const auto *cb_access_context = GetAccessContext(commandBuffer);
1976 assert(cb_access_context);
1977 if (!cb_access_context) return skip;
1978
1979 const auto *context = cb_access_context->GetCurrentAccessContext();
1980 assert(context);
1981 if (!context) return skip;
1982
1983 const auto *src_image = Get<IMAGE_STATE>(srcImage);
1984 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
1985
1986 for (uint32_t region = 0; region < regionCount; region++) {
1987 const auto &blit_region = pRegions[region];
1988 if (src_image) {
1989 VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x),
1990 static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y),
1991 static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06001992 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07001993 blit_region.srcOffsets[0], extent);
1994 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001995 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
1996 "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1997 report_data->FormatHandle(srcImage).c_str(), region);
locke-lunarga19c71d2020-03-02 18:17:04 -07001998 }
1999 }
2000
2001 if (dst_image) {
2002 VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x),
2003 static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y),
2004 static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002005 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002006 blit_region.dstOffsets[0], extent);
2007 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002008 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
2009 "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
2010 report_data->FormatHandle(dstImage).c_str(), region);
locke-lunarga19c71d2020-03-02 18:17:04 -07002011 }
2012 if (skip) break;
2013 }
2014 }
2015
2016 return skip;
2017}
2018
2019void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2020 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2021 const VkImageBlit *pRegions, VkFilter filter) {
2022 auto *cb_access_context = GetAccessContext(commandBuffer);
2023 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002024 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07002025 auto *context = cb_access_context->GetCurrentAccessContext();
2026 assert(context);
2027
2028 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002029 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002030
2031 for (uint32_t region = 0; region < regionCount; region++) {
2032 const auto &blit_region = pRegions[region];
2033 if (src_image) {
2034 VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x),
2035 static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y),
2036 static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002037 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002038 blit_region.srcOffsets[0], extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002039 }
2040 if (dst_image) {
2041 VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x),
2042 static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y),
2043 static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002044 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002045 blit_region.dstOffsets[0], extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002046 }
2047 }
2048}