blob: 6634d4bf73224d97905f4a495aadc58774d7dc31 [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;
179 ResourceAccessState default_state; // When present, PreviousAccess will "infill"
John Zulauf16adfc92020-04-08 10:28:33 -0600180 ResolvePreviousAccess(type, range, &descent_map, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600181
182 HazardResult hazard;
183 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
184 hazard = detector.Detect(prev);
185 }
186 return hazard;
187}
188
John Zulauf3d84f1b2020-03-09 13:33:25 -0600189// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
190// the DAG of the contexts (for example subpasses)
191template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600192HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
193 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600194 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600195
John Zulauf355e49b2020-04-24 15:11:15 -0600196 if (static_cast<uint32_t>(options) | DetectOptions::kDetectAsync) {
197 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
198 // so we'll check these first
199 for (const auto &async_context : async_) {
200 hazard = async_context->DetectAsyncHazard(type, detector, range);
201 if (hazard.hazard) return hazard;
202 }
John Zulauf5f13a792020-03-10 07:31:21 -0600203 }
204
John Zulauf355e49b2020-04-24 15:11:15 -0600205 if (static_cast<uint32_t>(options) | DetectOptions::kDetectAsync) {
206 const auto &accesses = GetAccessStateMap(type);
207 const auto from = accesses.lower_bound(range);
208 if (from != accesses.end() && from->first.intersects(range)) {
209 const auto to = accesses.upper_bound(range);
210 ResourceAccessRange gap = {range.begin, range.begin};
211 for (auto pos = from; pos != to; ++pos) {
212 hazard = detector.Detect(pos);
213 if (hazard.hazard) return hazard;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600214
John Zulauf355e49b2020-04-24 15:11:15 -0600215 // make sure we don't go past range
216 auto upper_bound = std::min(range.end, pos->first.end);
217 gap.end = upper_bound;
John Zulauf5f13a792020-03-10 07:31:21 -0600218
John Zulauf355e49b2020-04-24 15:11:15 -0600219 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
220 if (!gap.empty()) {
221 // Must recur on all gaps
222 hazard = DetectPreviousHazard(type, detector, gap);
223 if (hazard.hazard) return hazard;
224 }
225 gap.begin = upper_bound;
226 }
227 gap.end = range.end;
228 if (gap.non_empty()) {
John Zulauf16adfc92020-04-08 10:28:33 -0600229 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600230 if (hazard.hazard) return hazard;
231 }
John Zulauf355e49b2020-04-24 15:11:15 -0600232 } else {
233 hazard = DetectPreviousHazard(type, detector, range);
John Zulauf16adfc92020-04-08 10:28:33 -0600234 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600235 }
236
237 return hazard;
238}
239
240// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
241template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600242HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600243 auto &accesses = GetAccessStateMap(type);
244 const auto from = accesses.lower_bound(range);
245 const auto to = accesses.upper_bound(range);
246
John Zulauf3d84f1b2020-03-09 13:33:25 -0600247 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600248 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
249 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600250 }
John Zulauf16adfc92020-04-08 10:28:33 -0600251
John Zulauf3d84f1b2020-03-09 13:33:25 -0600252 return hazard;
253}
254
John Zulauf355e49b2020-04-24 15:11:15 -0600255// Returns the last resolved entry
256static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
257 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
258 const SyncBarrier *barrier) {
259 auto at = entry;
260 for (auto pos = first; pos != last; ++pos) {
261 // Every member of the input iterator range must fit within the remaining portion of entry
262 assert(at->first.includes(pos->first));
263 assert(at != dest->end());
264 // Trim up at to the same size as the entry to resolve
265 at = sparse_container::split(at, *dest, pos->first);
266 auto access = pos->second;
267 if (barrier) {
268 access.ApplyBarrier(*barrier);
269 }
270 at->second.Resolve(access);
271 ++at; // Go to the remaining unused section of entry
272 }
273}
274
275void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, const SyncBarrier *barrier,
276 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
277 bool recur_to_infill) const {
278 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
279 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf16adfc92020-04-08 10:28:33 -0600280 if (current->pos_B->valid) {
281 const auto &src_pos = current->pos_B->lower_bound;
John Zulauf355e49b2020-04-24 15:11:15 -0600282 auto access = src_pos->second;
283 if (barrier) {
284 access.ApplyBarrier(*barrier);
285 }
John Zulauf16adfc92020-04-08 10:28:33 -0600286 if (current->pos_A->valid) {
287 current.trim_A();
John Zulauf355e49b2020-04-24 15:11:15 -0600288 current->pos_A->lower_bound->second.Resolve(access);
John Zulauf5f13a792020-03-10 07:31:21 -0600289 } else {
John Zulauf355e49b2020-04-24 15:11:15 -0600290 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, access));
291 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600292 }
John Zulauf16adfc92020-04-08 10:28:33 -0600293 } else {
294 // we have to descend to fill this gap
295 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600296 if (current->pos_A->valid) {
297 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
298 ResourceAccessRangeMap gap_map;
299 ResolvePreviousAccess(type, current->range, &gap_map, infill_state);
300 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier);
301 } else {
302 // There isn't anything in dest in current->range, so we can accumulate directly into it.
303 ResolvePreviousAccess(type, current->range, resolve_map, infill_state);
304 if (barrier) {
305 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
306 for (auto pos = resolve_map->lower_bound(current->range); pos != current->pos_A->lower_bound; ++pos) {
307 pos->second.ApplyBarrier(*barrier);
308 }
309 }
310 }
311 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
312 // iterator of the outer while.
313
314 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
315 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
316 // we stepped on the dest map
317 const auto seek_to = current->range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
318 current.invalidate_A(); // Changes current->range
319 current.seek(seek_to);
320 } else if (!current->pos_A->valid && infill_state) {
321 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
322 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
323 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600324 }
John Zulauf5f13a792020-03-10 07:31:21 -0600325 }
John Zulauf16adfc92020-04-08 10:28:33 -0600326 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600327 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600328}
329
John Zulauf355e49b2020-04-24 15:11:15 -0600330void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
331 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600332 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600333 if (range.non_empty() && infill_state) {
334 descent_map->insert(std::make_pair(range, *infill_state));
335 }
336 } else {
337 // Look for something to fill the gap further along.
338 for (const auto &prev_dep : prev_) {
John Zulauf355e49b2020-04-24 15:11:15 -0600339 prev_dep.context->ResolveAccessRange(type, range, &prev_dep.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600340 }
341
John Zulaufe5da6e52020-03-18 15:32:18 -0600342 if (src_external_.context) {
John Zulauf355e49b2020-04-24 15:11:15 -0600343 src_external_.context->ResolveAccessRange(type, range, &src_external_.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600344 }
345 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600346}
347
John Zulauf16adfc92020-04-08 10:28:33 -0600348AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600349 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600350}
351
352VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) {
353 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
354}
355
John Zulauf355e49b2020-04-24 15:11:15 -0600356static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
John Zulauf16adfc92020-04-08 10:28:33 -0600357
John Zulauf540266b2020-04-06 18:54:53 -0600358void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
John Zulauf355e49b2020-04-24 15:11:15 -0600359 AddressType address_type, ResourceAccessRangeMap *descent_map,
360 const ResourceAccessState *infill_state) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600361 if (!SimpleBinding(image_state)) return;
362
John Zulauf62f10592020-04-03 12:20:02 -0600363 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
locke-lunargae26eac2020-04-16 15:29:05 -0600364 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600365 image_state.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600366 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf62f10592020-04-03 12:20:02 -0600367 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600368 ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state);
John Zulauf62f10592020-04-03 12:20:02 -0600369 }
370}
371
John Zulauf355e49b2020-04-24 15:11:15 -0600372static bool ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
373 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
374 uint32_t subpass, const AccessContext &context) {
375 bool skip = false;
376 const auto &transitions = rp_state.subpass_transitions[subpass];
377 for (const auto &transition : transitions) {
378 auto hazard = context.DetectSubpassTransitionHazard(transition, attachment_views);
379 if (hazard.hazard) {
380 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
381 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " image layout transition.",
382 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment);
383 }
384 }
385 return skip;
386}
387
John Zulauf3d84f1b2020-03-09 13:33:25 -0600388class HazardDetector {
389 SyncStageAccessIndex usage_index_;
390
391 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600392 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600393 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
394 return pos->second.DetectAsyncHazard(usage_index_);
395 }
396 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
397};
398
John Zulauf16adfc92020-04-08 10:28:33 -0600399HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600400 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600401 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600402 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600403}
404
John Zulauf16adfc92020-04-08 10:28:33 -0600405HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600406 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600407 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -0600408 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -0600409}
410
John Zulauf540266b2020-04-06 18:54:53 -0600411HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
412 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
413 const VkExtent3D &extent) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600414 if (!SimpleBinding(image)) return HazardResult();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700415 // TODO: replace the encoder/generator with offset3D/extent3D aware versions
416 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
417 subresource.layerCount};
locke-lunargae26eac2020-04-16 15:29:05 -0600418 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600419 const auto address_type = ImageAddressType(image);
420 const auto base_address = ResourceBaseAddress(image);
John Zulauf5c5e88d2019-12-26 11:22:02 -0700421 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600422 HazardResult hazard = DetectHazard(address_type, current_usage, (*range_gen + base_address));
John Zulauf5c5e88d2019-12-26 11:22:02 -0700423 if (hazard.hazard) return hazard;
424 }
425 return HazardResult();
John Zulauf9cb530d2019-09-30 14:14:10 -0600426}
427
John Zulauf3d84f1b2020-03-09 13:33:25 -0600428class BarrierHazardDetector {
429 public:
430 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
431 SyncStageAccessFlags src_access_scope)
432 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
433
John Zulauf5f13a792020-03-10 07:31:21 -0600434 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
435 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -0700436 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600437 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
438 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
439 return pos->second.DetectAsyncHazard(usage_index_);
440 }
441
442 private:
443 SyncStageAccessIndex usage_index_;
444 VkPipelineStageFlags src_exec_scope_;
445 SyncStageAccessFlags src_access_scope_;
446};
447
John Zulauf16adfc92020-04-08 10:28:33 -0600448HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -0600449 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600450 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600451 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf355e49b2020-04-24 15:11:15 -0600452 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf0cb5be22020-01-23 12:18:22 -0700453}
454
John Zulauf16adfc92020-04-08 10:28:33 -0600455HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600456 SyncStageAccessFlags src_access_scope,
457 const VkImageSubresourceRange &subresource_range,
458 DetectOptions options) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600459 if (!SimpleBinding(image)) return HazardResult();
locke-lunargae26eac2020-04-16 15:29:05 -0600460 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600461 image.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600462 const auto address_type = ImageAddressType(image);
463 const auto base_address = ResourceBaseAddress(image);
locke-lunarg296a3c92020-03-25 01:04:29 -0600464 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf355e49b2020-04-24 15:11:15 -0600465 HazardResult hazard = DetectBarrierHazard(address_type, SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope,
466 src_access_scope, (*range_gen + base_address), options);
locke-lunarg296a3c92020-03-25 01:04:29 -0600467 if (hazard.hazard) return hazard;
John Zulauf0cb5be22020-01-23 12:18:22 -0700468 }
469 return HazardResult();
470}
471
John Zulauf355e49b2020-04-24 15:11:15 -0600472HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
473 SyncStageAccessFlags src_stage_accesses,
474 const VkImageMemoryBarrier &barrier) const {
475 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
476 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
477 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
478}
479
John Zulauf9cb530d2019-09-30 14:14:10 -0600480template <typename Flags, typename Map>
481SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
482 SyncStageAccessFlags scope = 0;
483 for (const auto &bit_scope : map) {
484 if (flag_mask < bit_scope.first) break;
485
486 if (flag_mask & bit_scope.first) {
487 scope |= bit_scope.second;
488 }
489 }
490 return scope;
491}
492
493SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
494 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
495}
496
497SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
498 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
499}
500
501// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
502SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -0600503 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
504 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
505 // 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 -0600506 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
507}
508
509template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -0700510void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf9cb530d2019-09-30 14:14:10 -0600511 // TODO -- region/mem-range accuracte update
512 auto pos = accesses->lower_bound(range);
513 if (pos == accesses->end() || !pos->first.intersects(range)) {
514 // The range is empty, fill it with a default value.
515 pos = action.Infill(accesses, pos, range);
516 } else if (range.begin < pos->first.begin) {
517 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -0700518 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -0600519 } else if (pos->first.begin < range.begin) {
520 // Trim the beginning if needed
521 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
522 ++pos;
523 }
524
525 const auto the_end = accesses->end();
526 while ((pos != the_end) && pos->first.intersects(range)) {
527 if (pos->first.end > range.end) {
528 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
529 }
530
531 pos = action(accesses, pos);
532 if (pos == the_end) break;
533
534 auto next = pos;
535 ++next;
536 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
537 // Need to infill if next is disjoint
538 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -0700539 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -0600540 next = action.Infill(accesses, next, new_range);
541 }
542 pos = next;
543 }
544}
545
546struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700547 using Iterator = ResourceAccessRangeMap::iterator;
548 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600549 // this is only called on gaps, and never returns a gap.
550 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -0600551 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600552 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -0600553 }
John Zulauf5f13a792020-03-10 07:31:21 -0600554
John Zulauf5c5e88d2019-12-26 11:22:02 -0700555 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -0600556 auto &access_state = pos->second;
557 access_state.Update(usage, tag);
558 return pos;
559 }
560
John Zulauf16adfc92020-04-08 10:28:33 -0600561 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -0600562 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -0600563 : type(type_), context(context_), usage(usage_), tag(tag_) {}
564 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -0600565 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -0600566 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -0600567 const ResourceUsageTag &tag;
568};
569
570struct ApplyMemoryAccessBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700571 using Iterator = ResourceAccessRangeMap::iterator;
572 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -0600573
John Zulauf5c5e88d2019-12-26 11:22:02 -0700574 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -0600575 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700576 access_state.ApplyMemoryAccessBarrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -0600577 return pos;
578 }
579
John Zulauf36bcf6a2020-02-03 15:12:52 -0700580 ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_,
581 VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_)
582 : src_exec_scope(src_exec_scope_),
583 src_access_scope(src_access_scope_),
584 dst_exec_scope(dst_exec_scope_),
585 dst_access_scope(dst_access_scope_) {}
John Zulauf9cb530d2019-09-30 14:14:10 -0600586
John Zulauf36bcf6a2020-02-03 15:12:52 -0700587 VkPipelineStageFlags src_exec_scope;
588 SyncStageAccessFlags src_access_scope;
589 VkPipelineStageFlags dst_exec_scope;
590 SyncStageAccessFlags dst_access_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -0600591};
592
593struct ApplyGlobalBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700594 using Iterator = ResourceAccessRangeMap::iterator;
595 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -0600596
John Zulauf5c5e88d2019-12-26 11:22:02 -0700597 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -0600598 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700599 access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -0600600
601 for (const auto &functor : barrier_functor) {
602 functor(accesses, pos);
603 }
604 return pos;
605 }
606
John Zulauf36bcf6a2020-02-03 15:12:52 -0700607 ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope,
608 SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses,
John Zulauf9cb530d2019-09-30 14:14:10 -0600609 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers)
John Zulauf36bcf6a2020-02-03 15:12:52 -0700610 : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -0600611 // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier...
612 barrier_functor.reserve(memoryBarrierCount);
613 for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) {
614 const auto &barrier = pMemoryBarriers[barrier_index];
John Zulauf36bcf6a2020-02-03 15:12:52 -0700615 barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask),
616 dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
John Zulauf9cb530d2019-09-30 14:14:10 -0600617 }
618 }
619
John Zulauf36bcf6a2020-02-03 15:12:52 -0700620 const VkPipelineStageFlags src_exec_scope;
621 const VkPipelineStageFlags dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -0600622 std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor;
623};
624
John Zulauf355e49b2020-04-24 15:11:15 -0600625void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
626 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -0600627 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
628 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600629}
630
John Zulauf16adfc92020-04-08 10:28:33 -0600631void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -0600632 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -0600633 if (!SimpleBinding(buffer)) return;
634 const auto base_address = ResourceBaseAddress(buffer);
635 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
636}
John Zulauf355e49b2020-04-24 15:11:15 -0600637
John Zulauf540266b2020-04-06 18:54:53 -0600638void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -0600639 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -0600640 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -0600641 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -0600642 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600643 const auto address_type = ImageAddressType(image);
644 const auto base_address = ResourceBaseAddress(image);
645 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -0600646 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600647 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -0600648 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600649}
650
John Zulauf355e49b2020-04-24 15:11:15 -0600651void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
652 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
653 const VkExtent3D &extent, const ResourceUsageTag &tag) {
654 // TODO: replace the encoder/generator with offset3D aware versions
655 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
656 subresource.layerCount};
657 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
658}
659
John Zulauf540266b2020-04-06 18:54:53 -0600660template <typename Action>
661void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -0600662 if (!SimpleBinding(buffer)) return;
663 const auto base_address = ResourceBaseAddress(buffer);
664 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -0600665}
666
667template <typename Action>
668void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
669 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -0600670 if (!SimpleBinding(image)) return;
671 const auto address_type = ImageAddressType(image);
672 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -0600673
locke-lunargae26eac2020-04-16 15:29:05 -0600674 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600675 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -0600676
John Zulauf16adfc92020-04-08 10:28:33 -0600677 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -0600678 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600679 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -0600680 }
681}
682
683template <typename Action>
684void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
685 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -0600686 for (const auto address_type : kAddressTypes) {
687 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -0600688 }
689}
690
691void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -0600692 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
693 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -0600694 for (const auto address_type : kAddressTypes) {
John Zulauf355e49b2020-04-24 15:11:15 -0600695 context.ResolveAccessRange(address_type, full_range, &context.GetDstExternalTrackBack().barrier,
696 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -0600697 }
698 }
699}
700
John Zulauf355e49b2020-04-24 15:11:15 -0600701void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
702 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
703 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) {
704 const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
705 UpdateMemoryAccess(image, subresource_range, barrier_action);
706}
707
708// TODO: Plumb offset/extent throughout the image call stacks, with default injector overloads to preserved backwards compatiblity
709// as needed
710void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
711 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
712 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range,
713 bool layout_transition, const ResourceUsageTag &tag) {
714 if (layout_transition) {
715 UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent,
716 tag);
717 ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope,
718 subresource_range);
John Zulaufc9201222020-05-13 15:13:03 -0600719 } else {
720 ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range);
John Zulauf355e49b2020-04-24 15:11:15 -0600721 }
John Zulauf355e49b2020-04-24 15:11:15 -0600722}
723
724void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier,
725 const VkImageSubresourceRange &subresource_range, bool layout_transition,
726 const ResourceUsageTag &tag) {
727 ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope,
728 subresource_range, layout_transition, tag);
729}
730
731// Suitable only for *subpass* access contexts
732HazardResult AccessContext::DetectSubpassTransitionHazard(const RENDER_PASS_STATE::AttachmentTransition &transition,
733 const std::vector<const IMAGE_VIEW_STATE *> &attachments) const {
734 const auto *attach_view = attachments[transition.attachment];
735 if (!attach_view) return HazardResult();
736 const auto image_state = attach_view->image_state.get();
737 if (!image_state) return HazardResult();
738
739 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
740 // We should never ask for a transition from a context we don't have
741 assert(track_back);
742 assert(track_back->context);
743
744 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
745 auto hazard = track_back->context->DetectImageBarrierHazard(*image_state, track_back->barrier.src_exec_scope,
746 track_back->barrier.src_access_scope,
747 attach_view->normalized_subresource_range, kDetectPrevious);
748 if (!hazard.hazard) {
749 // The Async hazard check is against the current context's async set.
750 hazard = DetectImageBarrierHazard(*image_state, track_back->barrier.src_exec_scope, track_back->barrier.src_access_scope,
751 attach_view->normalized_subresource_range, kDetectAsync);
752 }
753 return hazard;
754}
755
756// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
757bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
758
759 const VkRenderPassBeginInfo *pRenderPassBegin,
760 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
761 const char *func_name) const {
762 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
763 bool skip = false;
764 uint32_t subpass = 0;
765 const auto &transitions = rp_state.subpass_transitions[subpass];
766 if (transitions.size()) {
767 const std::vector<AccessContext> empty_context_vector;
768 // Create context we can use to validate against...
769 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
770 const_cast<AccessContext *>(&cb_access_context_));
771
772 assert(pRenderPassBegin);
773 if (nullptr == pRenderPassBegin) return skip;
774
775 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
776 assert(fb_state);
777 if (nullptr == fb_state) return skip;
778
779 // Create a limited array of views (which we'll need to toss
780 std::vector<const IMAGE_VIEW_STATE *> views;
781 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
782 const auto attachment_count = count_attachment.first;
783 const auto *attachments = count_attachment.second;
784 views.resize(attachment_count, nullptr);
785 for (const auto &transition : transitions) {
786 assert(transition.attachment < attachment_count);
787 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
788 }
789
790 skip |= ValidateLayoutTransitions(*sync_state_, rp_state, views, func_name, 0, temp_context);
791 }
792 return skip;
793}
794
795bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
796 // TODO: Things to add here.
797 // Validate Preserve/Resolve attachments
798 bool skip = false;
799 skip |= current_renderpass_context_->ValidateNextSubpassLayoutTransitions(*sync_state_, func_name);
800
801 return skip;
802}
803
804bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
805 // TODO: Things to add here.
806 // Validate Preserve/Resolve attachments
807 bool skip = false;
808 skip |= current_renderpass_context_->ValidateFinalSubpassLayoutTransitions(*sync_state_, func_name);
809
810 return skip;
811}
812
813void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
814 assert(sync_state_);
815 if (!cb_state_) return;
816
817 // Create an access context the current renderpass.
818 render_pass_contexts_.emplace_back(&cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -0600819 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf355e49b2020-04-24 15:11:15 -0600820 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -0600821 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -0600822}
823
John Zulauf355e49b2020-04-24 15:11:15 -0600824void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -0600825 assert(current_renderpass_context_);
John Zulauf355e49b2020-04-24 15:11:15 -0600826 current_renderpass_context_->RecordNextSubpass(tag);
John Zulauf16adfc92020-04-08 10:28:33 -0600827 current_context_ = &current_renderpass_context_->CurrentContext();
828}
829
John Zulauf355e49b2020-04-24 15:11:15 -0600830void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
831 // TODO: Add layout load/store/resolve access (here or in RenderPassContext)
John Zulauf16adfc92020-04-08 10:28:33 -0600832 assert(current_renderpass_context_);
833 if (!current_renderpass_context_) return;
834
John Zulauf355e49b2020-04-24 15:11:15 -0600835 current_renderpass_context_->RecordEndRenderPass(tag);
836 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -0600837 current_renderpass_context_ = nullptr;
838}
839
John Zulauf355e49b2020-04-24 15:11:15 -0600840bool RenderPassAccessContext::ValidateNextSubpassLayoutTransitions(const SyncValidator &sync_state, const char *func_name) const {
841 bool skip = false;
842 const auto next_subpass = current_subpass_ + 1;
843 skip |= ValidateLayoutTransitions(sync_state, *rp_state_, attachment_views_, func_name, next_subpass,
844 subpass_contexts_[next_subpass]);
845 return skip;
846}
847
848bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const char *func_name) const {
849 bool skip = false;
850
851 // Validate the "finalLayout" transitions to external
852 // Get them from where there we're hidding in the extra entry.
853 const auto &final_transitions = rp_state_->subpass_transitions.back();
854 for (const auto &transition : final_transitions) {
855 const auto &attach_view = attachment_views_[transition.attachment];
856 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
857 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
858 auto hazard = trackback.context->DetectImageBarrierHazard(
859 *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope,
860 attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious);
861 if (hazard.hazard) {
862 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
863 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
864 " final image layout transition.",
865 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment);
866 }
867 }
868 return skip;
869}
870
871void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
872 // Add layout transitions...
873 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
874 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -0600875 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -0600876 for (const auto &transition : transitions) {
877 const auto attachment_view = attachment_views_[transition.attachment];
878 if (!attachment_view) continue;
879 const auto image = attachment_view->image_state.get();
880 if (!image) continue;
881
882 const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
John Zulaufc9201222020-05-13 15:13:03 -0600883 auto insert_pair = view_seen.insert(attachment_view);
884 if (insert_pair.second) {
885 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
886 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag);
887
888 } else {
889 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
890 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
891 auto barrier_to_transition = barrier->barrier;
892 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
893 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag);
894 }
John Zulauf355e49b2020-04-24 15:11:15 -0600895 }
896}
897
898void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
899 VkQueueFlags queue_flags, const ResourceUsageTag &tag) {
900 current_subpass_ = 0;
901 rp_state_ = cb_state.activeRenderPass;
902 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
903 // Add this for all subpasses here so that they exsist during next subpass validation
904 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
905 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context_);
906 }
907 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
908
909 RecordLayoutTransitions(tag);
910}
911void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag &tag) {
912 current_subpass_++;
913 assert(current_subpass_ < subpass_contexts_.size());
914 RecordLayoutTransitions(tag);
915}
916
917void RenderPassAccessContext::RecordEndRenderPass(const ResourceUsageTag &tag) {
918 // Export the accesses from the renderpass...
919 external_context_->ResolveChildContexts(subpass_contexts_);
920
921 // Add the "finalLayout" transitions to external
922 // Get them from where there we're hidding in the extra entry.
923 const auto &final_transitions = rp_state_->subpass_transitions.back();
924 for (const auto &transition : final_transitions) {
925 const auto &attachment = attachment_views_[transition.attachment];
926 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
927 assert(external_context_ == last_trackback.context);
928 external_context_->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier,
929 attachment->normalized_subresource_range, true, tag);
930 }
931}
932
John Zulauf3d84f1b2020-03-09 13:33:25 -0600933SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
934 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
935 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
936 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
937 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
938 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
939 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
940}
941
942void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) {
943 ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
944 ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope);
945}
946
John Zulauf9cb530d2019-09-30 14:14:10 -0600947HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
948 HazardResult hazard;
949 auto usage = FlagBit(usage_index);
950 if (IsRead(usage)) {
John Zulaufc9201222020-05-13 15:13:03 -0600951 if (last_write && IsWriteHazard(usage)) {
John Zulauf9cb530d2019-09-30 14:14:10 -0600952 hazard.Set(READ_AFTER_WRITE, write_tag);
953 }
954 } else {
955 // Assume write
956 // TODO determine what to do with READ-WRITE usage states if any
957 // Write-After-Write check -- if we have a previous write to test against
958 if (last_write && IsWriteHazard(usage)) {
959 hazard.Set(WRITE_AFTER_WRITE, write_tag);
960 } else {
961 // Only look for casus belli for WAR
962 const auto usage_stage = PipelineStageBit(usage_index);
963 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
964 if (IsReadHazard(usage_stage, last_reads[read_index])) {
965 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
966 break;
967 }
968 }
969 }
970 }
971 return hazard;
972}
973
John Zulauf2f952d22020-02-10 11:34:51 -0700974// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -0600975HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -0700976 HazardResult hazard;
977 auto usage = FlagBit(usage_index);
978 if (IsRead(usage)) {
979 if (last_write != 0) {
980 hazard.Set(READ_RACING_WRITE, write_tag);
981 }
982 } else {
983 if (last_write != 0) {
984 hazard.Set(WRITE_RACING_WRITE, write_tag);
985 } else if (last_read_count > 0) {
986 hazard.Set(WRITE_RACING_READ, last_reads[0].tag);
987 }
988 }
989 return hazard;
990}
991
John Zulauf36bcf6a2020-02-03 15:12:52 -0700992HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
993 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -0700994 // Only supporting image layout transitions for now
995 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
996 HazardResult hazard;
997 if (last_write) {
998 // If the previous write is *not* in the 1st access scope
999 // *AND* the current barrier is not in the dependency chain
1000 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
1001 // then the barrier access is unsafe (R/W after W)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001002 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
John Zulauf0cb5be22020-01-23 12:18:22 -07001003 // TODO: Do we need a difference hazard name for this?
1004 hazard.Set(WRITE_AFTER_WRITE, write_tag);
1005 }
John Zulauf355e49b2020-04-24 15:11:15 -06001006 }
1007 if (!hazard.hazard) {
1008 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07001009 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07001010 const auto &read_access = last_reads[read_index];
1011 // If the read stage is not in the src sync sync
1012 // *AND* not execution chained with an existing sync barrier (that's the or)
1013 // then the barrier access is unsafe (R/W after R)
1014 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
1015 hazard.Set(WRITE_AFTER_READ, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07001016 break;
1017 }
1018 }
1019 }
1020 return hazard;
1021}
1022
John Zulauf5f13a792020-03-10 07:31:21 -06001023// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
1024// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
1025// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
1026void ResourceAccessState::Resolve(const ResourceAccessState &other) {
1027 if (write_tag.IsBefore(other.write_tag)) {
1028 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
1029 *this = other;
1030 } else if (!other.write_tag.IsBefore(write_tag)) {
1031 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
1032 // dependency chaining logic or any stage expansion)
1033 write_barriers |= other.write_barriers;
1034
1035 // Merge that read states
1036 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
1037 auto &other_read = other.last_reads[other_read_index];
1038 if (last_read_stages & other_read.stage) {
1039 // Merge in the barriers for read stages that exist in *both* this and other
1040 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
1041 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
1042 auto &my_read = last_reads[my_read_index];
1043 if (other_read.stage == my_read.stage) {
1044 if (my_read.tag.IsBefore(other_read.tag)) {
1045 my_read.tag = other_read.tag;
1046 }
1047 my_read.barriers |= other_read.barriers;
1048 break;
1049 }
1050 }
1051 } else {
1052 // The other read stage doesn't exist in this, so add it.
1053 last_reads[last_read_count] = other_read;
1054 last_read_count++;
1055 last_read_stages |= other_read.stage;
1056 }
1057 }
1058 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
1059 // it.
1060}
1061
John Zulauf9cb530d2019-09-30 14:14:10 -06001062void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
1063 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
1064 const auto usage_bit = FlagBit(usage_index);
1065 if (IsRead(usage_index)) {
1066 // Mulitple outstanding reads may be of interest and do dependency chains independently
1067 // However, for purposes of barrier tracking, only one read per pipeline stage matters
1068 const auto usage_stage = PipelineStageBit(usage_index);
1069 if (usage_stage & last_read_stages) {
1070 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
1071 ReadState &access = last_reads[read_index];
1072 if (access.stage == usage_stage) {
1073 access.barriers = 0;
1074 access.tag = tag;
1075 break;
1076 }
1077 }
1078 } else {
1079 // We don't have this stage in the list yet...
1080 assert(last_read_count < last_reads.size());
1081 ReadState &access = last_reads[last_read_count++];
1082 access.stage = usage_stage;
1083 access.barriers = 0;
1084 access.tag = tag;
1085 last_read_stages |= usage_stage;
1086 }
1087 } else {
1088 // Assume write
1089 // TODO determine what to do with READ-WRITE operations if any
1090 // Clobber last read and both sets of barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
1091 // if the last_reads/last_write were unsafe, we've reported them,
1092 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
1093 last_read_count = 0;
1094 last_read_stages = 0;
1095
1096 write_barriers = 0;
1097 write_dependency_chain = 0;
1098 write_tag = tag;
1099 last_write = usage_bit;
1100 }
1101}
John Zulauf5f13a792020-03-10 07:31:21 -06001102
John Zulauf9cb530d2019-09-30 14:14:10 -06001103void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
1104 // Execution Barriers only protect read operations
1105 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
1106 ReadState &access = last_reads[read_index];
1107 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
1108 if (srcStageMask & (access.stage | access.barriers)) {
1109 access.barriers |= dstStageMask;
1110 }
1111 }
1112 if (write_dependency_chain & srcStageMask) write_dependency_chain |= dstStageMask;
1113}
1114
John Zulauf36bcf6a2020-02-03 15:12:52 -07001115void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
1116 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001117 // Assuming we've applied the execution side of this barrier, we update just the write
1118 // The || implements the "dependency chain" logic for this barrier
John Zulauf36bcf6a2020-02-03 15:12:52 -07001119 if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) {
1120 write_barriers |= dst_access_scope;
1121 write_dependency_chain |= dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001122 }
1123}
1124
John Zulaufd1f85d42020-04-15 12:23:15 -06001125void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001126 auto *access_context = GetAccessContextNoInsert(command_buffer);
1127 if (access_context) {
1128 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06001129 }
1130}
1131
John Zulaufd1f85d42020-04-15 12:23:15 -06001132void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
1133 auto access_found = cb_access_state.find(command_buffer);
1134 if (access_found != cb_access_state.end()) {
1135 access_found->second->Reset();
1136 cb_access_state.erase(access_found);
1137 }
1138}
1139
John Zulauf540266b2020-04-06 18:54:53 -06001140void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07001141 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
1142 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06001143 const VkMemoryBarrier *pMemoryBarriers) {
1144 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07001145 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06001146 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06001147 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06001148}
1149
John Zulauf540266b2020-04-06 18:54:53 -06001150void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07001151 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
1152 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06001153 const VkBufferMemoryBarrier *barriers) {
1154 // TODO Implement this at subresource/memory_range accuracy
1155 for (uint32_t index = 0; index < barrier_count; index++) {
locke-lunarg3c038002020-04-30 23:08:08 -06001156 auto barrier = barriers[index];
John Zulauf9cb530d2019-09-30 14:14:10 -06001157 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
1158 if (!buffer) continue;
locke-lunarg3c038002020-04-30 23:08:08 -06001159 barrier.size = GetRealWholeSize(barrier.offset, barrier.size, buffer->createInfo.size);
John Zulauf16adfc92020-04-08 10:28:33 -06001160 ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06001161 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
1162 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
1163 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1164 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06001165 }
1166}
1167
John Zulauf540266b2020-04-06 18:54:53 -06001168void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
1169 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
1170 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06001171 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001172 for (uint32_t index = 0; index < barrier_count; index++) {
1173 const auto &barrier = barriers[index];
1174 const auto *image = Get<IMAGE_STATE>(barrier.image);
1175 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06001176 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06001177 bool layout_transition = barrier.oldLayout != barrier.newLayout;
1178 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
1179 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
1180 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
1181 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001182 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001183}
1184
1185bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
1186 uint32_t regionCount, const VkBufferCopy *pRegions) const {
1187 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001188 const auto *cb_context = GetAccessContext(commandBuffer);
1189 assert(cb_context);
1190 if (!cb_context) return skip;
1191 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06001192
John Zulauf3d84f1b2020-03-09 13:33:25 -06001193 // If we have no previous accesses, we have no hazards
1194 // TODO: make this sub-resource capable
1195 // TODO: make this general, and stuff it into templates/utility functions
1196 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001197 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001198
1199 for (uint32_t region = 0; region < regionCount; region++) {
1200 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06001201 if (src_buffer) {
locke-lunarg3c038002020-04-30 23:08:08 -06001202 ResourceAccessRange src_range =
1203 MakeRange(copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06001204 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001205 if (hazard.hazard) {
1206 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06001207 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
1208 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1209 report_data->FormatHandle(srcBuffer).c_str(), region);
John Zulauf9cb530d2019-09-30 14:14:10 -06001210 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001211 }
John Zulauf16adfc92020-04-08 10:28:33 -06001212 if (dst_buffer && !skip) {
locke-lunarg3c038002020-04-30 23:08:08 -06001213 ResourceAccessRange dst_range =
1214 MakeRange(copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf355e49b2020-04-24 15:11:15 -06001215 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001216 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001217 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
1218 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1219 report_data->FormatHandle(dstBuffer).c_str(), region);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001220 }
1221 }
1222 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06001223 }
1224 return skip;
1225}
1226
1227void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
1228 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001229 auto *cb_context = GetAccessContext(commandBuffer);
1230 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001231 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001232 auto *context = cb_context->GetCurrentAccessContext();
1233
John Zulauf9cb530d2019-09-30 14:14:10 -06001234 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06001235 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06001236
1237 for (uint32_t region = 0; region < regionCount; region++) {
1238 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06001239 if (src_buffer) {
locke-lunarg3c038002020-04-30 23:08:08 -06001240 ResourceAccessRange src_range =
1241 MakeRange(copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06001242 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001243 }
John Zulauf16adfc92020-04-08 10:28:33 -06001244 if (dst_buffer) {
locke-lunarg3c038002020-04-30 23:08:08 -06001245 ResourceAccessRange dst_range =
1246 MakeRange(copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06001247 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001248 }
1249 }
1250}
1251
1252bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1253 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
1254 const VkImageCopy *pRegions) const {
1255 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001256 const auto *cb_access_context = GetAccessContext(commandBuffer);
1257 assert(cb_access_context);
1258 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07001259
John Zulauf3d84f1b2020-03-09 13:33:25 -06001260 const auto *context = cb_access_context->GetCurrentAccessContext();
1261 assert(context);
1262 if (!context) return skip;
1263
1264 const auto *src_image = Get<IMAGE_STATE>(srcImage);
1265 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001266 for (uint32_t region = 0; region < regionCount; region++) {
1267 const auto &copy_region = pRegions[region];
1268 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001269 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001270 copy_region.srcOffset, copy_region.extent);
1271 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001272 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
1273 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1274 report_data->FormatHandle(srcImage).c_str(), region);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001275 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001276 }
1277
1278 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07001279 VkExtent3D dst_copy_extent =
1280 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001281 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07001282 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001283 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001284 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
1285 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1286 report_data->FormatHandle(dstImage).c_str(), region);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001287 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07001288 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07001289 }
1290 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001291
John Zulauf5c5e88d2019-12-26 11:22:02 -07001292 return skip;
1293}
1294
1295void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1296 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
1297 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001298 auto *cb_access_context = GetAccessContext(commandBuffer);
1299 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001300 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001301 auto *context = cb_access_context->GetCurrentAccessContext();
1302 assert(context);
1303
John Zulauf5c5e88d2019-12-26 11:22:02 -07001304 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001305 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001306
1307 for (uint32_t region = 0; region < regionCount; region++) {
1308 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06001309 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001310 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
1311 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001312 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001313 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07001314 VkExtent3D dst_copy_extent =
1315 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001316 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
1317 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001318 }
1319 }
1320}
1321
1322bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1323 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1324 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1325 uint32_t bufferMemoryBarrierCount,
1326 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1327 uint32_t imageMemoryBarrierCount,
1328 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
1329 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001330 const auto *cb_access_context = GetAccessContext(commandBuffer);
1331 assert(cb_access_context);
1332 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07001333
John Zulauf3d84f1b2020-03-09 13:33:25 -06001334 const auto *context = cb_access_context->GetCurrentAccessContext();
1335 assert(context);
1336 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07001337
John Zulauf3d84f1b2020-03-09 13:33:25 -06001338 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07001339 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
1340 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07001341 // Validate Image Layout transitions
1342 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
1343 const auto &barrier = pImageMemoryBarriers[index];
1344 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
1345 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
1346 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06001347 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07001348 if (hazard.hazard) {
1349 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06001350 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
1351 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s", string_SyncHazard(hazard.hazard),
1352 index, report_data->FormatHandle(barrier.image).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07001353 }
1354 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001355
1356 return skip;
1357}
1358
1359void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1360 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1361 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1362 uint32_t bufferMemoryBarrierCount,
1363 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1364 uint32_t imageMemoryBarrierCount,
1365 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001366 auto *cb_access_context = GetAccessContext(commandBuffer);
1367 assert(cb_access_context);
1368 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06001369 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001370 auto access_context = cb_access_context->GetCurrentAccessContext();
1371 assert(access_context);
1372 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06001373
John Zulauf3d84f1b2020-03-09 13:33:25 -06001374 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07001375 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001376 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07001377 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
1378 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
1379 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001380 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
1381 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06001382 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001383 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001384
1385 // 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 -06001386 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07001387 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06001388}
1389
1390void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
1391 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
1392 // The state tracker sets up the device state
1393 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
1394
John Zulauf5f13a792020-03-10 07:31:21 -06001395 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
1396 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06001397 // TODO: Find a good way to do this hooklessly.
1398 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
1399 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
1400 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
1401
John Zulaufd1f85d42020-04-15 12:23:15 -06001402 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
1403 sync_device_state->ResetCommandBufferCallback(command_buffer);
1404 });
1405 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
1406 sync_device_state->FreeCommandBufferCallback(command_buffer);
1407 });
John Zulauf9cb530d2019-09-30 14:14:10 -06001408}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001409
John Zulauf355e49b2020-04-24 15:11:15 -06001410bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1411 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
1412 bool skip = false;
1413 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
1414 auto cb_context = GetAccessContext(commandBuffer);
1415
1416 if (rp_state && cb_context) {
1417 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
1418 }
1419
1420 return skip;
1421}
1422
1423bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1424 VkSubpassContents contents) const {
1425 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
1426 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
1427 subpass_begin_info.contents = contents;
1428 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
1429 return skip;
1430}
1431
1432bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1433 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
1434 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1435 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
1436 return skip;
1437}
1438
1439bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1440 const VkRenderPassBeginInfo *pRenderPassBegin,
1441 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
1442 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1443 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
1444 return skip;
1445}
1446
John Zulauf3d84f1b2020-03-09 13:33:25 -06001447void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
1448 VkResult result) {
1449 // The state tracker sets up the command buffer state
1450 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
1451
1452 // Create/initialize the structure that trackers accesses at the command buffer scope.
1453 auto cb_access_context = GetAccessContext(commandBuffer);
1454 assert(cb_access_context);
1455 cb_access_context->Reset();
1456}
1457
1458void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06001459 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001460 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06001461 if (cb_context) {
1462 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06001463 }
1464}
1465
1466void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1467 VkSubpassContents contents) {
1468 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
1469 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
1470 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06001471 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001472}
1473
1474void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1475 const VkSubpassBeginInfo *pSubpassBeginInfo) {
1476 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001477 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001478}
1479
1480void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1481 const VkRenderPassBeginInfo *pRenderPassBegin,
1482 const VkSubpassBeginInfo *pSubpassBeginInfo) {
1483 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001484 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
1485}
1486
1487bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1488 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
1489 bool skip = false;
1490
1491 auto cb_context = GetAccessContext(commandBuffer);
1492 assert(cb_context);
1493 auto cb_state = cb_context->GetCommandBufferState();
1494 if (!cb_state) return skip;
1495
1496 auto rp_state = cb_state->activeRenderPass;
1497 if (!rp_state) return skip;
1498
1499 skip |= cb_context->ValidateNextSubpass(func_name);
1500
1501 return skip;
1502}
1503
1504bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
1505 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
1506 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
1507 subpass_begin_info.contents = contents;
1508 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
1509 return skip;
1510}
1511
1512bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1513 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
1514 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
1515 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
1516 return skip;
1517}
1518
1519bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
1520 const VkSubpassEndInfo *pSubpassEndInfo) const {
1521 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
1522 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
1523 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001524}
1525
1526void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06001527 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001528 auto cb_context = GetAccessContext(commandBuffer);
1529 assert(cb_context);
1530 auto cb_state = cb_context->GetCommandBufferState();
1531 if (!cb_state) return;
1532
1533 auto rp_state = cb_state->activeRenderPass;
1534 if (!rp_state) return;
1535
John Zulauf355e49b2020-04-24 15:11:15 -06001536 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06001537}
1538
1539void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
1540 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
1541 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
1542 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06001543 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001544}
1545
1546void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
1547 const VkSubpassEndInfo *pSubpassEndInfo) {
1548 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001549 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001550}
1551
1552void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
1553 const VkSubpassEndInfo *pSubpassEndInfo) {
1554 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001555 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001556}
1557
John Zulauf355e49b2020-04-24 15:11:15 -06001558bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
1559 const char *func_name) const {
1560 bool skip = false;
1561
1562 auto cb_context = GetAccessContext(commandBuffer);
1563 assert(cb_context);
1564 auto cb_state = cb_context->GetCommandBufferState();
1565 if (!cb_state) return skip;
1566
1567 auto rp_state = cb_state->activeRenderPass;
1568 if (!rp_state) return skip;
1569
1570 skip |= cb_context->ValidateEndRenderpass(func_name);
1571 return skip;
1572}
1573
1574bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
1575 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
1576 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
1577 return skip;
1578}
1579
1580bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
1581 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
1582 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
1583 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
1584 return skip;
1585}
1586
1587bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
1588 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
1589 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
1590 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
1591 return skip;
1592}
1593
1594void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
1595 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06001596 // Resolve the all subpass contexts to the command buffer contexts
1597 auto cb_context = GetAccessContext(commandBuffer);
1598 assert(cb_context);
1599 auto cb_state = cb_context->GetCommandBufferState();
1600 if (!cb_state) return;
1601
1602 const auto *rp_state = cb_state->activeRenderPass;
1603 if (!rp_state) return;
1604
John Zulauf355e49b2020-04-24 15:11:15 -06001605 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06001606}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001607
1608void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
1609 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06001610 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001611}
1612
1613void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
1614 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001615 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001616}
1617
1618void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
1619 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001620 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001621}
locke-lunarga19c71d2020-03-02 18:17:04 -07001622
1623bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
1624 VkImageLayout dstImageLayout, uint32_t regionCount,
1625 const VkBufferImageCopy *pRegions) const {
1626 bool skip = false;
1627 const auto *cb_access_context = GetAccessContext(commandBuffer);
1628 assert(cb_access_context);
1629 if (!cb_access_context) return skip;
1630
1631 const auto *context = cb_access_context->GetCurrentAccessContext();
1632 assert(context);
1633 if (!context) return skip;
1634
1635 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07001636 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
1637
1638 for (uint32_t region = 0; region < regionCount; region++) {
1639 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06001640 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06001641 ResourceAccessRange src_range =
1642 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06001643 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07001644 if (hazard.hazard) {
1645 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06001646 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
1647 "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07001648 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region);
1649 }
1650 }
1651 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001652 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07001653 copy_region.imageOffset, copy_region.imageExtent);
1654 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001655 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
1656 "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07001657 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region);
1658 }
1659 if (skip) break;
1660 }
1661 if (skip) break;
1662 }
1663 return skip;
1664}
1665
1666void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
1667 VkImageLayout dstImageLayout, uint32_t regionCount,
1668 const VkBufferImageCopy *pRegions) {
1669 auto *cb_access_context = GetAccessContext(commandBuffer);
1670 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001671 const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07001672 auto *context = cb_access_context->GetCurrentAccessContext();
1673 assert(context);
1674
1675 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06001676 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07001677
1678 for (uint32_t region = 0; region < regionCount; region++) {
1679 const auto &copy_region = pRegions[region];
1680 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06001681 ResourceAccessRange src_range =
1682 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06001683 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001684 }
1685 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001686 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06001687 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001688 }
1689 }
1690}
1691
1692bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
1693 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
1694 const VkBufferImageCopy *pRegions) const {
1695 bool skip = false;
1696 const auto *cb_access_context = GetAccessContext(commandBuffer);
1697 assert(cb_access_context);
1698 if (!cb_access_context) return skip;
1699
1700 const auto *context = cb_access_context->GetCurrentAccessContext();
1701 assert(context);
1702 if (!context) return skip;
1703
1704 const auto *src_image = Get<IMAGE_STATE>(srcImage);
1705 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
1706 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
1707 for (uint32_t region = 0; region < regionCount; region++) {
1708 const auto &copy_region = pRegions[region];
1709 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001710 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07001711 copy_region.imageOffset, copy_region.imageExtent);
1712 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001713 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
1714 "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07001715 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region);
1716 }
1717 }
1718 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06001719 ResourceAccessRange dst_range =
1720 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06001721 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07001722 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001723 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
1724 "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07001725 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region);
1726 }
1727 }
1728 if (skip) break;
1729 }
1730 return skip;
1731}
1732
1733void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1734 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
1735 auto *cb_access_context = GetAccessContext(commandBuffer);
1736 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001737 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER);
locke-lunarga19c71d2020-03-02 18:17:04 -07001738 auto *context = cb_access_context->GetCurrentAccessContext();
1739 assert(context);
1740
1741 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07001742 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
1743 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 -06001744 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07001745
1746 for (uint32_t region = 0; region < regionCount; region++) {
1747 const auto &copy_region = pRegions[region];
1748 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001749 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06001750 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001751 }
1752 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06001753 ResourceAccessRange dst_range =
1754 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06001755 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001756 }
1757 }
1758}
1759
1760bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1761 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
1762 const VkImageBlit *pRegions, VkFilter filter) const {
1763 bool skip = false;
1764 const auto *cb_access_context = GetAccessContext(commandBuffer);
1765 assert(cb_access_context);
1766 if (!cb_access_context) return skip;
1767
1768 const auto *context = cb_access_context->GetCurrentAccessContext();
1769 assert(context);
1770 if (!context) return skip;
1771
1772 const auto *src_image = Get<IMAGE_STATE>(srcImage);
1773 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
1774
1775 for (uint32_t region = 0; region < regionCount; region++) {
1776 const auto &blit_region = pRegions[region];
1777 if (src_image) {
1778 VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x),
1779 static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y),
1780 static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06001781 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07001782 blit_region.srcOffsets[0], extent);
1783 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001784 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
1785 "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1786 report_data->FormatHandle(srcImage).c_str(), region);
locke-lunarga19c71d2020-03-02 18:17:04 -07001787 }
1788 }
1789
1790 if (dst_image) {
1791 VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x),
1792 static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y),
1793 static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06001794 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07001795 blit_region.dstOffsets[0], extent);
1796 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001797 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
1798 "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1799 report_data->FormatHandle(dstImage).c_str(), region);
locke-lunarga19c71d2020-03-02 18:17:04 -07001800 }
1801 if (skip) break;
1802 }
1803 }
1804
1805 return skip;
1806}
1807
1808void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1809 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
1810 const VkImageBlit *pRegions, VkFilter filter) {
1811 auto *cb_access_context = GetAccessContext(commandBuffer);
1812 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001813 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07001814 auto *context = cb_access_context->GetCurrentAccessContext();
1815 assert(context);
1816
1817 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07001818 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07001819
1820 for (uint32_t region = 0; region < regionCount; region++) {
1821 const auto &blit_region = pRegions[region];
1822 if (src_image) {
1823 VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x),
1824 static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y),
1825 static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06001826 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06001827 blit_region.srcOffsets[0], extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001828 }
1829 if (dst_image) {
1830 VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x),
1831 static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y),
1832 static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06001833 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06001834 blit_region.dstOffsets[0], extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001835 }
1836 }
1837}