blob: 26cde874164c74608cfe4b4afe7f064e4a3b79d3 [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
John Zulauf16adfc92020-04-08 10:28:33 -060084template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -060085static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -060086 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
87}
88
John Zulauf355e49b2020-04-24 15:11:15 -060089static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -060090
John Zulauf0cb5be22020-01-23 12:18:22 -070091// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
92VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
93 VkPipelineStageFlags expanded = stage_mask;
94 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
95 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
96 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
97 if (all_commands.first & queue_flags) {
98 expanded |= all_commands.second;
99 }
100 }
101 }
102 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
103 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
104 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
105 }
106 return expanded;
107}
108
John Zulauf36bcf6a2020-02-03 15:12:52 -0700109VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
110 std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
111 VkPipelineStageFlags unscanned = stage_mask;
112 VkPipelineStageFlags related = 0;
113 for (const auto entry : map) {
114 const auto stage = entry.first;
115 if (stage & unscanned) {
116 related = related | entry.second;
117 unscanned = unscanned & ~stage;
118 if (!unscanned) break;
119 }
120 }
121 return related;
122}
123
124VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
125 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
126}
127
128VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
129 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
130}
131
John Zulauf5c5e88d2019-12-26 11:22:02 -0700132static const ResourceAccessRange full_range(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700133
John Zulauf355e49b2020-04-24 15:11:15 -0600134// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
135const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = {
136 AccessContext::AddressType::kLinearAddress, AccessContext::AddressType::kIdealizedAddress};
137
John Zulauf540266b2020-04-06 18:54:53 -0600138AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
139 const std::vector<SubpassDependencyGraphNode> &dependencies,
140 const std::vector<AccessContext> &contexts, AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600141 Reset();
142 const auto &subpass_dep = dependencies[subpass];
143 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600144 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600145 for (const auto &prev_dep : subpass_dep.prev) {
146 assert(prev_dep.dependency);
147 const auto dep = *prev_dep.dependency;
John Zulauf540266b2020-04-06 18:54:53 -0600148 prev_.emplace_back(const_cast<AccessContext *>(&contexts[dep.srcSubpass]), queue_flags, dep);
John Zulauf355e49b2020-04-24 15:11:15 -0600149 prev_by_subpass_[dep.srcSubpass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700150 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600151
152 async_.reserve(subpass_dep.async.size());
153 for (const auto async_subpass : subpass_dep.async) {
John Zulauf540266b2020-04-06 18:54:53 -0600154 async_.emplace_back(const_cast<AccessContext *>(&contexts[async_subpass]));
John Zulauf3d84f1b2020-03-09 13:33:25 -0600155 }
John Zulaufe5da6e52020-03-18 15:32:18 -0600156 if (subpass_dep.barrier_from_external) {
157 src_external_ = TrackBack(external_context, queue_flags, *subpass_dep.barrier_from_external);
158 } else {
159 src_external_ = TrackBack();
160 }
161 if (subpass_dep.barrier_to_external) {
162 dst_external_ = TrackBack(this, queue_flags, *subpass_dep.barrier_to_external);
163 } else {
164 dst_external_ = TrackBack();
John Zulauf3d84f1b2020-03-09 13:33:25 -0600165 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700166}
167
John Zulauf5f13a792020-03-10 07:31:21 -0600168template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600169HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600170 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600171 ResourceAccessRangeMap descent_map;
172 ResourceAccessState default_state; // When present, PreviousAccess will "infill"
John Zulauf16adfc92020-04-08 10:28:33 -0600173 ResolvePreviousAccess(type, range, &descent_map, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600174
175 HazardResult hazard;
176 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
177 hazard = detector.Detect(prev);
178 }
179 return hazard;
180}
181
John Zulauf3d84f1b2020-03-09 13:33:25 -0600182// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
183// the DAG of the contexts (for example subpasses)
184template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600185HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
186 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600187 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600188
John Zulauf355e49b2020-04-24 15:11:15 -0600189 if (static_cast<uint32_t>(options) | DetectOptions::kDetectAsync) {
190 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
191 // so we'll check these first
192 for (const auto &async_context : async_) {
193 hazard = async_context->DetectAsyncHazard(type, detector, range);
194 if (hazard.hazard) return hazard;
195 }
John Zulauf5f13a792020-03-10 07:31:21 -0600196 }
197
John Zulauf355e49b2020-04-24 15:11:15 -0600198 if (static_cast<uint32_t>(options) | DetectOptions::kDetectAsync) {
199 const auto &accesses = GetAccessStateMap(type);
200 const auto from = accesses.lower_bound(range);
201 if (from != accesses.end() && from->first.intersects(range)) {
202 const auto to = accesses.upper_bound(range);
203 ResourceAccessRange gap = {range.begin, range.begin};
204 for (auto pos = from; pos != to; ++pos) {
205 hazard = detector.Detect(pos);
206 if (hazard.hazard) return hazard;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600207
John Zulauf355e49b2020-04-24 15:11:15 -0600208 // make sure we don't go past range
209 auto upper_bound = std::min(range.end, pos->first.end);
210 gap.end = upper_bound;
John Zulauf5f13a792020-03-10 07:31:21 -0600211
John Zulauf355e49b2020-04-24 15:11:15 -0600212 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
213 if (!gap.empty()) {
214 // Must recur on all gaps
215 hazard = DetectPreviousHazard(type, detector, gap);
216 if (hazard.hazard) return hazard;
217 }
218 gap.begin = upper_bound;
219 }
220 gap.end = range.end;
221 if (gap.non_empty()) {
John Zulauf16adfc92020-04-08 10:28:33 -0600222 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600223 if (hazard.hazard) return hazard;
224 }
John Zulauf355e49b2020-04-24 15:11:15 -0600225 } else {
226 hazard = DetectPreviousHazard(type, detector, range);
John Zulauf16adfc92020-04-08 10:28:33 -0600227 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600228 }
229
230 return hazard;
231}
232
233// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
234template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600235HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600236 auto &accesses = GetAccessStateMap(type);
237 const auto from = accesses.lower_bound(range);
238 const auto to = accesses.upper_bound(range);
239
John Zulauf3d84f1b2020-03-09 13:33:25 -0600240 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600241 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
242 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600243 }
John Zulauf16adfc92020-04-08 10:28:33 -0600244
John Zulauf3d84f1b2020-03-09 13:33:25 -0600245 return hazard;
246}
247
John Zulauf355e49b2020-04-24 15:11:15 -0600248// Returns the last resolved entry
249static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
250 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
251 const SyncBarrier *barrier) {
252 auto at = entry;
253 for (auto pos = first; pos != last; ++pos) {
254 // Every member of the input iterator range must fit within the remaining portion of entry
255 assert(at->first.includes(pos->first));
256 assert(at != dest->end());
257 // Trim up at to the same size as the entry to resolve
258 at = sparse_container::split(at, *dest, pos->first);
259 auto access = pos->second;
260 if (barrier) {
261 access.ApplyBarrier(*barrier);
262 }
263 at->second.Resolve(access);
264 ++at; // Go to the remaining unused section of entry
265 }
266}
267
268void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, const SyncBarrier *barrier,
269 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
270 bool recur_to_infill) const {
271 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
272 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf16adfc92020-04-08 10:28:33 -0600273 if (current->pos_B->valid) {
274 const auto &src_pos = current->pos_B->lower_bound;
John Zulauf355e49b2020-04-24 15:11:15 -0600275 auto access = src_pos->second;
276 if (barrier) {
277 access.ApplyBarrier(*barrier);
278 }
John Zulauf16adfc92020-04-08 10:28:33 -0600279 if (current->pos_A->valid) {
280 current.trim_A();
John Zulauf355e49b2020-04-24 15:11:15 -0600281 current->pos_A->lower_bound->second.Resolve(access);
John Zulauf5f13a792020-03-10 07:31:21 -0600282 } else {
John Zulauf355e49b2020-04-24 15:11:15 -0600283 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, access));
284 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600285 }
John Zulauf16adfc92020-04-08 10:28:33 -0600286 } else {
287 // we have to descend to fill this gap
288 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600289 if (current->pos_A->valid) {
290 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
291 ResourceAccessRangeMap gap_map;
292 ResolvePreviousAccess(type, current->range, &gap_map, infill_state);
293 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier);
294 } else {
295 // There isn't anything in dest in current->range, so we can accumulate directly into it.
296 ResolvePreviousAccess(type, current->range, resolve_map, infill_state);
297 if (barrier) {
298 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
299 for (auto pos = resolve_map->lower_bound(current->range); pos != current->pos_A->lower_bound; ++pos) {
300 pos->second.ApplyBarrier(*barrier);
301 }
302 }
303 }
304 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
305 // iterator of the outer while.
306
307 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
308 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
309 // we stepped on the dest map
310 const auto seek_to = current->range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
311 current.invalidate_A(); // Changes current->range
312 current.seek(seek_to);
313 } else if (!current->pos_A->valid && infill_state) {
314 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
315 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
316 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600317 }
John Zulauf5f13a792020-03-10 07:31:21 -0600318 }
John Zulauf16adfc92020-04-08 10:28:33 -0600319 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600320 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600321}
322
John Zulauf355e49b2020-04-24 15:11:15 -0600323void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
324 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600325 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600326 if (range.non_empty() && infill_state) {
327 descent_map->insert(std::make_pair(range, *infill_state));
328 }
329 } else {
330 // Look for something to fill the gap further along.
331 for (const auto &prev_dep : prev_) {
John Zulauf355e49b2020-04-24 15:11:15 -0600332 prev_dep.context->ResolveAccessRange(type, range, &prev_dep.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600333 }
334
John Zulaufe5da6e52020-03-18 15:32:18 -0600335 if (src_external_.context) {
John Zulauf355e49b2020-04-24 15:11:15 -0600336 src_external_.context->ResolveAccessRange(type, range, &src_external_.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600337 }
338 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600339}
340
John Zulauf16adfc92020-04-08 10:28:33 -0600341AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600342 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600343}
344
345VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) {
346 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
347}
348
John Zulauf355e49b2020-04-24 15:11:15 -0600349static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
John Zulauf16adfc92020-04-08 10:28:33 -0600350
John Zulauf540266b2020-04-06 18:54:53 -0600351void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
John Zulauf355e49b2020-04-24 15:11:15 -0600352 AddressType address_type, ResourceAccessRangeMap *descent_map,
353 const ResourceAccessState *infill_state) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600354 if (!SimpleBinding(image_state)) return;
355
John Zulauf62f10592020-04-03 12:20:02 -0600356 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
locke-lunargae26eac2020-04-16 15:29:05 -0600357 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600358 image_state.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600359 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf62f10592020-04-03 12:20:02 -0600360 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600361 ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state);
John Zulauf62f10592020-04-03 12:20:02 -0600362 }
363}
364
John Zulauf355e49b2020-04-24 15:11:15 -0600365static bool ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
366 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
367 uint32_t subpass, const AccessContext &context) {
368 bool skip = false;
369 const auto &transitions = rp_state.subpass_transitions[subpass];
370 for (const auto &transition : transitions) {
371 auto hazard = context.DetectSubpassTransitionHazard(transition, attachment_views);
372 if (hazard.hazard) {
373 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
374 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " image layout transition.",
375 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment);
376 }
377 }
378 return skip;
379}
380
John Zulauf3d84f1b2020-03-09 13:33:25 -0600381class HazardDetector {
382 SyncStageAccessIndex usage_index_;
383
384 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600385 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600386 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
387 return pos->second.DetectAsyncHazard(usage_index_);
388 }
389 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
390};
391
John Zulauf16adfc92020-04-08 10:28:33 -0600392HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600393 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600394 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600395 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600396}
397
John Zulauf16adfc92020-04-08 10:28:33 -0600398HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600399 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600400 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -0600401 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -0600402}
403
John Zulauf540266b2020-04-06 18:54:53 -0600404HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
405 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
406 const VkExtent3D &extent) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600407 if (!SimpleBinding(image)) return HazardResult();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700408 // TODO: replace the encoder/generator with offset3D/extent3D aware versions
409 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
410 subresource.layerCount};
locke-lunargae26eac2020-04-16 15:29:05 -0600411 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600412 const auto address_type = ImageAddressType(image);
413 const auto base_address = ResourceBaseAddress(image);
John Zulauf5c5e88d2019-12-26 11:22:02 -0700414 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600415 HazardResult hazard = DetectHazard(address_type, current_usage, (*range_gen + base_address));
John Zulauf5c5e88d2019-12-26 11:22:02 -0700416 if (hazard.hazard) return hazard;
417 }
418 return HazardResult();
John Zulauf9cb530d2019-09-30 14:14:10 -0600419}
420
John Zulauf3d84f1b2020-03-09 13:33:25 -0600421class BarrierHazardDetector {
422 public:
423 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
424 SyncStageAccessFlags src_access_scope)
425 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
426
John Zulauf5f13a792020-03-10 07:31:21 -0600427 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
428 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -0700429 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600430 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
431 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
432 return pos->second.DetectAsyncHazard(usage_index_);
433 }
434
435 private:
436 SyncStageAccessIndex usage_index_;
437 VkPipelineStageFlags src_exec_scope_;
438 SyncStageAccessFlags src_access_scope_;
439};
440
John Zulauf16adfc92020-04-08 10:28:33 -0600441HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -0600442 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600443 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600444 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf355e49b2020-04-24 15:11:15 -0600445 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf0cb5be22020-01-23 12:18:22 -0700446}
447
John Zulauf16adfc92020-04-08 10:28:33 -0600448HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600449 SyncStageAccessFlags src_access_scope,
450 const VkImageSubresourceRange &subresource_range,
451 DetectOptions options) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600452 if (!SimpleBinding(image)) return HazardResult();
locke-lunargae26eac2020-04-16 15:29:05 -0600453 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600454 image.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600455 const auto address_type = ImageAddressType(image);
456 const auto base_address = ResourceBaseAddress(image);
locke-lunarg296a3c92020-03-25 01:04:29 -0600457 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf355e49b2020-04-24 15:11:15 -0600458 HazardResult hazard = DetectBarrierHazard(address_type, SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope,
459 src_access_scope, (*range_gen + base_address), options);
locke-lunarg296a3c92020-03-25 01:04:29 -0600460 if (hazard.hazard) return hazard;
John Zulauf0cb5be22020-01-23 12:18:22 -0700461 }
462 return HazardResult();
463}
464
John Zulauf355e49b2020-04-24 15:11:15 -0600465HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
466 SyncStageAccessFlags src_stage_accesses,
467 const VkImageMemoryBarrier &barrier) const {
468 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
469 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
470 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
471}
472
John Zulauf9cb530d2019-09-30 14:14:10 -0600473template <typename Flags, typename Map>
474SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
475 SyncStageAccessFlags scope = 0;
476 for (const auto &bit_scope : map) {
477 if (flag_mask < bit_scope.first) break;
478
479 if (flag_mask & bit_scope.first) {
480 scope |= bit_scope.second;
481 }
482 }
483 return scope;
484}
485
486SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
487 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
488}
489
490SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
491 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
492}
493
494// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
495SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -0600496 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
497 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
498 // 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 -0600499 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
500}
501
502template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -0700503void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf9cb530d2019-09-30 14:14:10 -0600504 // TODO -- region/mem-range accuracte update
505 auto pos = accesses->lower_bound(range);
506 if (pos == accesses->end() || !pos->first.intersects(range)) {
507 // The range is empty, fill it with a default value.
508 pos = action.Infill(accesses, pos, range);
509 } else if (range.begin < pos->first.begin) {
510 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -0700511 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -0600512 } else if (pos->first.begin < range.begin) {
513 // Trim the beginning if needed
514 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
515 ++pos;
516 }
517
518 const auto the_end = accesses->end();
519 while ((pos != the_end) && pos->first.intersects(range)) {
520 if (pos->first.end > range.end) {
521 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
522 }
523
524 pos = action(accesses, pos);
525 if (pos == the_end) break;
526
527 auto next = pos;
528 ++next;
529 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
530 // Need to infill if next is disjoint
531 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -0700532 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -0600533 next = action.Infill(accesses, next, new_range);
534 }
535 pos = next;
536 }
537}
538
539struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700540 using Iterator = ResourceAccessRangeMap::iterator;
541 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600542 // this is only called on gaps, and never returns a gap.
543 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -0600544 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600545 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -0600546 }
John Zulauf5f13a792020-03-10 07:31:21 -0600547
John Zulauf5c5e88d2019-12-26 11:22:02 -0700548 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -0600549 auto &access_state = pos->second;
550 access_state.Update(usage, tag);
551 return pos;
552 }
553
John Zulauf16adfc92020-04-08 10:28:33 -0600554 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -0600555 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -0600556 : type(type_), context(context_), usage(usage_), tag(tag_) {}
557 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -0600558 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -0600559 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -0600560 const ResourceUsageTag &tag;
561};
562
563struct ApplyMemoryAccessBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700564 using Iterator = ResourceAccessRangeMap::iterator;
565 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -0600566
John Zulauf5c5e88d2019-12-26 11:22:02 -0700567 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -0600568 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700569 access_state.ApplyMemoryAccessBarrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -0600570 return pos;
571 }
572
John Zulauf36bcf6a2020-02-03 15:12:52 -0700573 ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_,
574 VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_)
575 : src_exec_scope(src_exec_scope_),
576 src_access_scope(src_access_scope_),
577 dst_exec_scope(dst_exec_scope_),
578 dst_access_scope(dst_access_scope_) {}
John Zulauf9cb530d2019-09-30 14:14:10 -0600579
John Zulauf36bcf6a2020-02-03 15:12:52 -0700580 VkPipelineStageFlags src_exec_scope;
581 SyncStageAccessFlags src_access_scope;
582 VkPipelineStageFlags dst_exec_scope;
583 SyncStageAccessFlags dst_access_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -0600584};
585
586struct ApplyGlobalBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700587 using Iterator = ResourceAccessRangeMap::iterator;
588 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -0600589
John Zulauf5c5e88d2019-12-26 11:22:02 -0700590 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -0600591 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700592 access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -0600593
594 for (const auto &functor : barrier_functor) {
595 functor(accesses, pos);
596 }
597 return pos;
598 }
599
John Zulauf36bcf6a2020-02-03 15:12:52 -0700600 ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope,
601 SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses,
John Zulauf9cb530d2019-09-30 14:14:10 -0600602 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers)
John Zulauf36bcf6a2020-02-03 15:12:52 -0700603 : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -0600604 // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier...
605 barrier_functor.reserve(memoryBarrierCount);
606 for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) {
607 const auto &barrier = pMemoryBarriers[barrier_index];
John Zulauf36bcf6a2020-02-03 15:12:52 -0700608 barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask),
609 dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
John Zulauf9cb530d2019-09-30 14:14:10 -0600610 }
611 }
612
John Zulauf36bcf6a2020-02-03 15:12:52 -0700613 const VkPipelineStageFlags src_exec_scope;
614 const VkPipelineStageFlags dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -0600615 std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor;
616};
617
John Zulauf355e49b2020-04-24 15:11:15 -0600618void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
619 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -0600620 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
621 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600622}
623
John Zulauf16adfc92020-04-08 10:28:33 -0600624void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -0600625 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -0600626 if (!SimpleBinding(buffer)) return;
627 const auto base_address = ResourceBaseAddress(buffer);
628 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
629}
John Zulauf355e49b2020-04-24 15:11:15 -0600630
John Zulauf540266b2020-04-06 18:54:53 -0600631void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -0600632 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -0600633 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -0600634 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -0600635 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600636 const auto address_type = ImageAddressType(image);
637 const auto base_address = ResourceBaseAddress(image);
638 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -0600639 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600640 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -0600641 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600642}
643
John Zulauf355e49b2020-04-24 15:11:15 -0600644void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
645 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
646 const VkExtent3D &extent, const ResourceUsageTag &tag) {
647 // TODO: replace the encoder/generator with offset3D aware versions
648 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
649 subresource.layerCount};
650 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
651}
652
John Zulauf540266b2020-04-06 18:54:53 -0600653template <typename Action>
654void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -0600655 if (!SimpleBinding(buffer)) return;
656 const auto base_address = ResourceBaseAddress(buffer);
657 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -0600658}
659
660template <typename Action>
661void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
662 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -0600663 if (!SimpleBinding(image)) return;
664 const auto address_type = ImageAddressType(image);
665 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -0600666
locke-lunargae26eac2020-04-16 15:29:05 -0600667 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600668 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -0600669
John Zulauf16adfc92020-04-08 10:28:33 -0600670 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -0600671 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600672 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -0600673 }
674}
675
676template <typename Action>
677void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
678 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -0600679 for (const auto address_type : kAddressTypes) {
680 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -0600681 }
682}
683
684void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -0600685 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
686 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -0600687 for (const auto address_type : kAddressTypes) {
John Zulauf355e49b2020-04-24 15:11:15 -0600688 context.ResolveAccessRange(address_type, full_range, &context.GetDstExternalTrackBack().barrier,
689 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -0600690 }
691 }
692}
693
John Zulauf355e49b2020-04-24 15:11:15 -0600694void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
695 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
696 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) {
697 const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
698 UpdateMemoryAccess(image, subresource_range, barrier_action);
699}
700
701// TODO: Plumb offset/extent throughout the image call stacks, with default injector overloads to preserved backwards compatiblity
702// as needed
703void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
704 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
705 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range,
706 bool layout_transition, const ResourceUsageTag &tag) {
707 if (layout_transition) {
708 UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent,
709 tag);
710 ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope,
711 subresource_range);
712 }
713 ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range);
714}
715
716void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier,
717 const VkImageSubresourceRange &subresource_range, bool layout_transition,
718 const ResourceUsageTag &tag) {
719 ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope,
720 subresource_range, layout_transition, tag);
721}
722
723// Suitable only for *subpass* access contexts
724HazardResult AccessContext::DetectSubpassTransitionHazard(const RENDER_PASS_STATE::AttachmentTransition &transition,
725 const std::vector<const IMAGE_VIEW_STATE *> &attachments) const {
726 const auto *attach_view = attachments[transition.attachment];
727 if (!attach_view) return HazardResult();
728 const auto image_state = attach_view->image_state.get();
729 if (!image_state) return HazardResult();
730
731 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
732 // We should never ask for a transition from a context we don't have
733 assert(track_back);
734 assert(track_back->context);
735
736 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
737 auto hazard = track_back->context->DetectImageBarrierHazard(*image_state, track_back->barrier.src_exec_scope,
738 track_back->barrier.src_access_scope,
739 attach_view->normalized_subresource_range, kDetectPrevious);
740 if (!hazard.hazard) {
741 // The Async hazard check is against the current context's async set.
742 hazard = DetectImageBarrierHazard(*image_state, track_back->barrier.src_exec_scope, track_back->barrier.src_access_scope,
743 attach_view->normalized_subresource_range, kDetectAsync);
744 }
745 return hazard;
746}
747
748// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
749bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
750
751 const VkRenderPassBeginInfo *pRenderPassBegin,
752 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
753 const char *func_name) const {
754 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
755 bool skip = false;
756 uint32_t subpass = 0;
757 const auto &transitions = rp_state.subpass_transitions[subpass];
758 if (transitions.size()) {
759 const std::vector<AccessContext> empty_context_vector;
760 // Create context we can use to validate against...
761 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
762 const_cast<AccessContext *>(&cb_access_context_));
763
764 assert(pRenderPassBegin);
765 if (nullptr == pRenderPassBegin) return skip;
766
767 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
768 assert(fb_state);
769 if (nullptr == fb_state) return skip;
770
771 // Create a limited array of views (which we'll need to toss
772 std::vector<const IMAGE_VIEW_STATE *> views;
773 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
774 const auto attachment_count = count_attachment.first;
775 const auto *attachments = count_attachment.second;
776 views.resize(attachment_count, nullptr);
777 for (const auto &transition : transitions) {
778 assert(transition.attachment < attachment_count);
779 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
780 }
781
782 skip |= ValidateLayoutTransitions(*sync_state_, rp_state, views, func_name, 0, temp_context);
783 }
784 return skip;
785}
786
787bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
788 // TODO: Things to add here.
789 // Validate Preserve/Resolve attachments
790 bool skip = false;
791 skip |= current_renderpass_context_->ValidateNextSubpassLayoutTransitions(*sync_state_, func_name);
792
793 return skip;
794}
795
796bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
797 // TODO: Things to add here.
798 // Validate Preserve/Resolve attachments
799 bool skip = false;
800 skip |= current_renderpass_context_->ValidateFinalSubpassLayoutTransitions(*sync_state_, func_name);
801
802 return skip;
803}
804
805void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
806 assert(sync_state_);
807 if (!cb_state_) return;
808
809 // Create an access context the current renderpass.
810 render_pass_contexts_.emplace_back(&cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -0600811 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf355e49b2020-04-24 15:11:15 -0600812 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -0600813 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -0600814}
815
John Zulauf355e49b2020-04-24 15:11:15 -0600816void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -0600817 assert(current_renderpass_context_);
John Zulauf355e49b2020-04-24 15:11:15 -0600818 current_renderpass_context_->RecordNextSubpass(tag);
John Zulauf16adfc92020-04-08 10:28:33 -0600819 current_context_ = &current_renderpass_context_->CurrentContext();
820}
821
John Zulauf355e49b2020-04-24 15:11:15 -0600822void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
823 // TODO: Add layout load/store/resolve access (here or in RenderPassContext)
John Zulauf16adfc92020-04-08 10:28:33 -0600824 assert(current_renderpass_context_);
825 if (!current_renderpass_context_) return;
826
John Zulauf355e49b2020-04-24 15:11:15 -0600827 current_renderpass_context_->RecordEndRenderPass(tag);
828 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -0600829 current_renderpass_context_ = nullptr;
830}
831
John Zulauf355e49b2020-04-24 15:11:15 -0600832bool RenderPassAccessContext::ValidateNextSubpassLayoutTransitions(const SyncValidator &sync_state, const char *func_name) const {
833 bool skip = false;
834 const auto next_subpass = current_subpass_ + 1;
835 skip |= ValidateLayoutTransitions(sync_state, *rp_state_, attachment_views_, func_name, next_subpass,
836 subpass_contexts_[next_subpass]);
837 return skip;
838}
839
840bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const char *func_name) const {
841 bool skip = false;
842
843 // Validate the "finalLayout" transitions to external
844 // Get them from where there we're hidding in the extra entry.
845 const auto &final_transitions = rp_state_->subpass_transitions.back();
846 for (const auto &transition : final_transitions) {
847 const auto &attach_view = attachment_views_[transition.attachment];
848 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
849 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
850 auto hazard = trackback.context->DetectImageBarrierHazard(
851 *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope,
852 attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious);
853 if (hazard.hazard) {
854 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
855 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
856 " final image layout transition.",
857 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment);
858 }
859 }
860 return skip;
861}
862
863void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
864 // Add layout transitions...
865 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
866 auto &subpass_context = subpass_contexts_[current_subpass_];
867 for (const auto &transition : transitions) {
868 const auto attachment_view = attachment_views_[transition.attachment];
869 if (!attachment_view) continue;
870 const auto image = attachment_view->image_state.get();
871 if (!image) continue;
872
873 const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
874 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag);
875 }
876}
877
878void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
879 VkQueueFlags queue_flags, const ResourceUsageTag &tag) {
880 current_subpass_ = 0;
881 rp_state_ = cb_state.activeRenderPass;
882 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
883 // Add this for all subpasses here so that they exsist during next subpass validation
884 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
885 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context_);
886 }
887 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
888
889 RecordLayoutTransitions(tag);
890}
891void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag &tag) {
892 current_subpass_++;
893 assert(current_subpass_ < subpass_contexts_.size());
894 RecordLayoutTransitions(tag);
895}
896
897void RenderPassAccessContext::RecordEndRenderPass(const ResourceUsageTag &tag) {
898 // Export the accesses from the renderpass...
899 external_context_->ResolveChildContexts(subpass_contexts_);
900
901 // Add the "finalLayout" transitions to external
902 // Get them from where there we're hidding in the extra entry.
903 const auto &final_transitions = rp_state_->subpass_transitions.back();
904 for (const auto &transition : final_transitions) {
905 const auto &attachment = attachment_views_[transition.attachment];
906 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
907 assert(external_context_ == last_trackback.context);
908 external_context_->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier,
909 attachment->normalized_subresource_range, true, tag);
910 }
911}
912
John Zulauf3d84f1b2020-03-09 13:33:25 -0600913SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
914 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
915 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
916 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
917 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
918 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
919 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
920}
921
922void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) {
923 ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
924 ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope);
925}
926
John Zulauf9cb530d2019-09-30 14:14:10 -0600927HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
928 HazardResult hazard;
929 auto usage = FlagBit(usage_index);
930 if (IsRead(usage)) {
931 if (IsWriteHazard(usage)) {
932 hazard.Set(READ_AFTER_WRITE, write_tag);
933 }
934 } else {
935 // Assume write
936 // TODO determine what to do with READ-WRITE usage states if any
937 // Write-After-Write check -- if we have a previous write to test against
938 if (last_write && IsWriteHazard(usage)) {
939 hazard.Set(WRITE_AFTER_WRITE, write_tag);
940 } else {
941 // Only look for casus belli for WAR
942 const auto usage_stage = PipelineStageBit(usage_index);
943 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
944 if (IsReadHazard(usage_stage, last_reads[read_index])) {
945 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
946 break;
947 }
948 }
949 }
950 }
951 return hazard;
952}
953
John Zulauf2f952d22020-02-10 11:34:51 -0700954// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -0600955HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -0700956 HazardResult hazard;
957 auto usage = FlagBit(usage_index);
958 if (IsRead(usage)) {
959 if (last_write != 0) {
960 hazard.Set(READ_RACING_WRITE, write_tag);
961 }
962 } else {
963 if (last_write != 0) {
964 hazard.Set(WRITE_RACING_WRITE, write_tag);
965 } else if (last_read_count > 0) {
966 hazard.Set(WRITE_RACING_READ, last_reads[0].tag);
967 }
968 }
969 return hazard;
970}
971
John Zulauf36bcf6a2020-02-03 15:12:52 -0700972HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
973 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -0700974 // Only supporting image layout transitions for now
975 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
976 HazardResult hazard;
977 if (last_write) {
978 // If the previous write is *not* in the 1st access scope
979 // *AND* the current barrier is not in the dependency chain
980 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
981 // then the barrier access is unsafe (R/W after W)
John Zulauf36bcf6a2020-02-03 15:12:52 -0700982 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
John Zulauf0cb5be22020-01-23 12:18:22 -0700983 // TODO: Do we need a difference hazard name for this?
984 hazard.Set(WRITE_AFTER_WRITE, write_tag);
985 }
John Zulauf355e49b2020-04-24 15:11:15 -0600986 }
987 if (!hazard.hazard) {
988 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -0700989 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -0700990 const auto &read_access = last_reads[read_index];
991 // If the read stage is not in the src sync sync
992 // *AND* not execution chained with an existing sync barrier (that's the or)
993 // then the barrier access is unsafe (R/W after R)
994 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
995 hazard.Set(WRITE_AFTER_READ, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -0700996 break;
997 }
998 }
999 }
1000 return hazard;
1001}
1002
John Zulauf5f13a792020-03-10 07:31:21 -06001003// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
1004// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
1005// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
1006void ResourceAccessState::Resolve(const ResourceAccessState &other) {
1007 if (write_tag.IsBefore(other.write_tag)) {
1008 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
1009 *this = other;
1010 } else if (!other.write_tag.IsBefore(write_tag)) {
1011 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
1012 // dependency chaining logic or any stage expansion)
1013 write_barriers |= other.write_barriers;
1014
1015 // Merge that read states
1016 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
1017 auto &other_read = other.last_reads[other_read_index];
1018 if (last_read_stages & other_read.stage) {
1019 // Merge in the barriers for read stages that exist in *both* this and other
1020 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
1021 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
1022 auto &my_read = last_reads[my_read_index];
1023 if (other_read.stage == my_read.stage) {
1024 if (my_read.tag.IsBefore(other_read.tag)) {
1025 my_read.tag = other_read.tag;
1026 }
1027 my_read.barriers |= other_read.barriers;
1028 break;
1029 }
1030 }
1031 } else {
1032 // The other read stage doesn't exist in this, so add it.
1033 last_reads[last_read_count] = other_read;
1034 last_read_count++;
1035 last_read_stages |= other_read.stage;
1036 }
1037 }
1038 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
1039 // it.
1040}
1041
John Zulauf9cb530d2019-09-30 14:14:10 -06001042void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
1043 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
1044 const auto usage_bit = FlagBit(usage_index);
1045 if (IsRead(usage_index)) {
1046 // Mulitple outstanding reads may be of interest and do dependency chains independently
1047 // However, for purposes of barrier tracking, only one read per pipeline stage matters
1048 const auto usage_stage = PipelineStageBit(usage_index);
1049 if (usage_stage & last_read_stages) {
1050 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
1051 ReadState &access = last_reads[read_index];
1052 if (access.stage == usage_stage) {
1053 access.barriers = 0;
1054 access.tag = tag;
1055 break;
1056 }
1057 }
1058 } else {
1059 // We don't have this stage in the list yet...
1060 assert(last_read_count < last_reads.size());
1061 ReadState &access = last_reads[last_read_count++];
1062 access.stage = usage_stage;
1063 access.barriers = 0;
1064 access.tag = tag;
1065 last_read_stages |= usage_stage;
1066 }
1067 } else {
1068 // Assume write
1069 // TODO determine what to do with READ-WRITE operations if any
1070 // Clobber last read and both sets of barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
1071 // if the last_reads/last_write were unsafe, we've reported them,
1072 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
1073 last_read_count = 0;
1074 last_read_stages = 0;
1075
1076 write_barriers = 0;
1077 write_dependency_chain = 0;
1078 write_tag = tag;
1079 last_write = usage_bit;
1080 }
1081}
John Zulauf5f13a792020-03-10 07:31:21 -06001082
John Zulauf9cb530d2019-09-30 14:14:10 -06001083void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
1084 // Execution Barriers only protect read operations
1085 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
1086 ReadState &access = last_reads[read_index];
1087 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
1088 if (srcStageMask & (access.stage | access.barriers)) {
1089 access.barriers |= dstStageMask;
1090 }
1091 }
1092 if (write_dependency_chain & srcStageMask) write_dependency_chain |= dstStageMask;
1093}
1094
John Zulauf36bcf6a2020-02-03 15:12:52 -07001095void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
1096 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001097 // Assuming we've applied the execution side of this barrier, we update just the write
1098 // The || implements the "dependency chain" logic for this barrier
John Zulauf36bcf6a2020-02-03 15:12:52 -07001099 if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) {
1100 write_barriers |= dst_access_scope;
1101 write_dependency_chain |= dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001102 }
1103}
1104
John Zulaufd1f85d42020-04-15 12:23:15 -06001105void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001106 auto *access_context = GetAccessContextNoInsert(command_buffer);
1107 if (access_context) {
1108 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06001109 }
1110}
1111
John Zulaufd1f85d42020-04-15 12:23:15 -06001112void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
1113 auto access_found = cb_access_state.find(command_buffer);
1114 if (access_found != cb_access_state.end()) {
1115 access_found->second->Reset();
1116 cb_access_state.erase(access_found);
1117 }
1118}
1119
John Zulauf540266b2020-04-06 18:54:53 -06001120void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07001121 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
1122 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06001123 const VkMemoryBarrier *pMemoryBarriers) {
1124 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07001125 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06001126 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06001127 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06001128}
1129
John Zulauf540266b2020-04-06 18:54:53 -06001130void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07001131 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
1132 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06001133 const VkBufferMemoryBarrier *barriers) {
1134 // TODO Implement this at subresource/memory_range accuracy
1135 for (uint32_t index = 0; index < barrier_count; index++) {
1136 const auto &barrier = barriers[index];
1137 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
1138 if (!buffer) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06001139 ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06001140 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
1141 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
1142 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1143 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06001144 }
1145}
1146
John Zulauf540266b2020-04-06 18:54:53 -06001147void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
1148 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
1149 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06001150 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001151 for (uint32_t index = 0; index < barrier_count; index++) {
1152 const auto &barrier = barriers[index];
1153 const auto *image = Get<IMAGE_STATE>(barrier.image);
1154 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06001155 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06001156 bool layout_transition = barrier.oldLayout != barrier.newLayout;
1157 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
1158 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
1159 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
1160 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001161 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001162}
1163
1164bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
1165 uint32_t regionCount, const VkBufferCopy *pRegions) const {
1166 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001167 const auto *cb_context = GetAccessContext(commandBuffer);
1168 assert(cb_context);
1169 if (!cb_context) return skip;
1170 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06001171
John Zulauf3d84f1b2020-03-09 13:33:25 -06001172 // If we have no previous accesses, we have no hazards
1173 // TODO: make this sub-resource capable
1174 // TODO: make this general, and stuff it into templates/utility functions
1175 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001176 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001177
1178 for (uint32_t region = 0; region < regionCount; region++) {
1179 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06001180 if (src_buffer) {
1181 ResourceAccessRange src_range = MakeRange(copy_region.srcOffset, copy_region.size);
1182 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001183 if (hazard.hazard) {
1184 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06001185 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
1186 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1187 report_data->FormatHandle(srcBuffer).c_str(), region);
John Zulauf9cb530d2019-09-30 14:14:10 -06001188 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001189 }
John Zulauf16adfc92020-04-08 10:28:33 -06001190 if (dst_buffer && !skip) {
1191 ResourceAccessRange dst_range = MakeRange(copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06001192 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001193 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001194 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
1195 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1196 report_data->FormatHandle(dstBuffer).c_str(), region);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001197 }
1198 }
1199 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06001200 }
1201 return skip;
1202}
1203
1204void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
1205 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001206 auto *cb_context = GetAccessContext(commandBuffer);
1207 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001208 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001209 auto *context = cb_context->GetCurrentAccessContext();
1210
John Zulauf9cb530d2019-09-30 14:14:10 -06001211 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06001212 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06001213
1214 for (uint32_t region = 0; region < regionCount; region++) {
1215 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06001216 if (src_buffer) {
1217 ResourceAccessRange src_range = MakeRange(copy_region.srcOffset, copy_region.size);
1218 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001219 }
John Zulauf16adfc92020-04-08 10:28:33 -06001220 if (dst_buffer) {
1221 ResourceAccessRange dst_range = MakeRange(copy_region.dstOffset, copy_region.size);
1222 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001223 }
1224 }
1225}
1226
1227bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1228 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
1229 const VkImageCopy *pRegions) const {
1230 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001231 const auto *cb_access_context = GetAccessContext(commandBuffer);
1232 assert(cb_access_context);
1233 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07001234
John Zulauf3d84f1b2020-03-09 13:33:25 -06001235 const auto *context = cb_access_context->GetCurrentAccessContext();
1236 assert(context);
1237 if (!context) return skip;
1238
1239 const auto *src_image = Get<IMAGE_STATE>(srcImage);
1240 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001241 for (uint32_t region = 0; region < regionCount; region++) {
1242 const auto &copy_region = pRegions[region];
1243 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001244 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001245 copy_region.srcOffset, copy_region.extent);
1246 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001247 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
1248 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1249 report_data->FormatHandle(srcImage).c_str(), region);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001250 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001251 }
1252
1253 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07001254 VkExtent3D dst_copy_extent =
1255 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001256 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07001257 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001258 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001259 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
1260 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1261 report_data->FormatHandle(dstImage).c_str(), region);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001262 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07001263 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07001264 }
1265 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001266
John Zulauf5c5e88d2019-12-26 11:22:02 -07001267 return skip;
1268}
1269
1270void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1271 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
1272 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001273 auto *cb_access_context = GetAccessContext(commandBuffer);
1274 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001275 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001276 auto *context = cb_access_context->GetCurrentAccessContext();
1277 assert(context);
1278
John Zulauf5c5e88d2019-12-26 11:22:02 -07001279 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001280 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001281
1282 for (uint32_t region = 0; region < regionCount; region++) {
1283 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06001284 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001285 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
1286 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001287 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001288 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07001289 VkExtent3D dst_copy_extent =
1290 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001291 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
1292 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001293 }
1294 }
1295}
1296
1297bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1298 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1299 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1300 uint32_t bufferMemoryBarrierCount,
1301 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1302 uint32_t imageMemoryBarrierCount,
1303 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
1304 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001305 const auto *cb_access_context = GetAccessContext(commandBuffer);
1306 assert(cb_access_context);
1307 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07001308
John Zulauf3d84f1b2020-03-09 13:33:25 -06001309 const auto *context = cb_access_context->GetCurrentAccessContext();
1310 assert(context);
1311 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07001312
John Zulauf3d84f1b2020-03-09 13:33:25 -06001313 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07001314 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
1315 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07001316 // Validate Image Layout transitions
1317 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
1318 const auto &barrier = pImageMemoryBarriers[index];
1319 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
1320 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
1321 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06001322 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07001323 if (hazard.hazard) {
1324 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06001325 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
1326 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s", string_SyncHazard(hazard.hazard),
1327 index, report_data->FormatHandle(barrier.image).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07001328 }
1329 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001330
1331 return skip;
1332}
1333
1334void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1335 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1336 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1337 uint32_t bufferMemoryBarrierCount,
1338 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1339 uint32_t imageMemoryBarrierCount,
1340 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001341 auto *cb_access_context = GetAccessContext(commandBuffer);
1342 assert(cb_access_context);
1343 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06001344 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001345 auto access_context = cb_access_context->GetCurrentAccessContext();
1346 assert(access_context);
1347 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06001348
John Zulauf3d84f1b2020-03-09 13:33:25 -06001349 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07001350 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001351 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07001352 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
1353 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
1354 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001355 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
1356 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06001357 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001358 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001359
1360 // 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 -06001361 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07001362 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06001363}
1364
1365void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
1366 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
1367 // The state tracker sets up the device state
1368 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
1369
John Zulauf5f13a792020-03-10 07:31:21 -06001370 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
1371 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06001372 // TODO: Find a good way to do this hooklessly.
1373 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
1374 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
1375 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
1376
John Zulaufd1f85d42020-04-15 12:23:15 -06001377 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
1378 sync_device_state->ResetCommandBufferCallback(command_buffer);
1379 });
1380 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
1381 sync_device_state->FreeCommandBufferCallback(command_buffer);
1382 });
John Zulauf9cb530d2019-09-30 14:14:10 -06001383}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001384
John Zulauf355e49b2020-04-24 15:11:15 -06001385bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1386 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
1387 bool skip = false;
1388 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
1389 auto cb_context = GetAccessContext(commandBuffer);
1390
1391 if (rp_state && cb_context) {
1392 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
1393 }
1394
1395 return skip;
1396}
1397
1398bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1399 VkSubpassContents contents) const {
1400 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
1401 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
1402 subpass_begin_info.contents = contents;
1403 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
1404 return skip;
1405}
1406
1407bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1408 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
1409 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1410 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
1411 return skip;
1412}
1413
1414bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1415 const VkRenderPassBeginInfo *pRenderPassBegin,
1416 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
1417 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1418 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
1419 return skip;
1420}
1421
John Zulauf3d84f1b2020-03-09 13:33:25 -06001422void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
1423 VkResult result) {
1424 // The state tracker sets up the command buffer state
1425 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
1426
1427 // Create/initialize the structure that trackers accesses at the command buffer scope.
1428 auto cb_access_context = GetAccessContext(commandBuffer);
1429 assert(cb_access_context);
1430 cb_access_context->Reset();
1431}
1432
1433void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06001434 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001435 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06001436 if (cb_context) {
1437 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06001438 }
1439}
1440
1441void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1442 VkSubpassContents contents) {
1443 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
1444 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
1445 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06001446 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001447}
1448
1449void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1450 const VkSubpassBeginInfo *pSubpassBeginInfo) {
1451 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001452 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001453}
1454
1455void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1456 const VkRenderPassBeginInfo *pRenderPassBegin,
1457 const VkSubpassBeginInfo *pSubpassBeginInfo) {
1458 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001459 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
1460}
1461
1462bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1463 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
1464 bool skip = false;
1465
1466 auto cb_context = GetAccessContext(commandBuffer);
1467 assert(cb_context);
1468 auto cb_state = cb_context->GetCommandBufferState();
1469 if (!cb_state) return skip;
1470
1471 auto rp_state = cb_state->activeRenderPass;
1472 if (!rp_state) return skip;
1473
1474 skip |= cb_context->ValidateNextSubpass(func_name);
1475
1476 return skip;
1477}
1478
1479bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
1480 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
1481 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
1482 subpass_begin_info.contents = contents;
1483 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
1484 return skip;
1485}
1486
1487bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1488 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
1489 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
1490 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
1491 return skip;
1492}
1493
1494bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
1495 const VkSubpassEndInfo *pSubpassEndInfo) const {
1496 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
1497 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
1498 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001499}
1500
1501void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06001502 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001503 auto cb_context = GetAccessContext(commandBuffer);
1504 assert(cb_context);
1505 auto cb_state = cb_context->GetCommandBufferState();
1506 if (!cb_state) return;
1507
1508 auto rp_state = cb_state->activeRenderPass;
1509 if (!rp_state) return;
1510
John Zulauf355e49b2020-04-24 15:11:15 -06001511 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06001512}
1513
1514void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
1515 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
1516 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
1517 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06001518 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001519}
1520
1521void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
1522 const VkSubpassEndInfo *pSubpassEndInfo) {
1523 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001524 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001525}
1526
1527void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
1528 const VkSubpassEndInfo *pSubpassEndInfo) {
1529 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001530 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001531}
1532
John Zulauf355e49b2020-04-24 15:11:15 -06001533bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
1534 const char *func_name) const {
1535 bool skip = false;
1536
1537 auto cb_context = GetAccessContext(commandBuffer);
1538 assert(cb_context);
1539 auto cb_state = cb_context->GetCommandBufferState();
1540 if (!cb_state) return skip;
1541
1542 auto rp_state = cb_state->activeRenderPass;
1543 if (!rp_state) return skip;
1544
1545 skip |= cb_context->ValidateEndRenderpass(func_name);
1546 return skip;
1547}
1548
1549bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
1550 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
1551 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
1552 return skip;
1553}
1554
1555bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
1556 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
1557 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
1558 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
1559 return skip;
1560}
1561
1562bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
1563 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
1564 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
1565 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
1566 return skip;
1567}
1568
1569void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
1570 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06001571 // Resolve the all subpass contexts to the command buffer contexts
1572 auto cb_context = GetAccessContext(commandBuffer);
1573 assert(cb_context);
1574 auto cb_state = cb_context->GetCommandBufferState();
1575 if (!cb_state) return;
1576
1577 const auto *rp_state = cb_state->activeRenderPass;
1578 if (!rp_state) return;
1579
John Zulauf355e49b2020-04-24 15:11:15 -06001580 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06001581}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001582
1583void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
1584 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06001585 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001586}
1587
1588void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
1589 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001590 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001591}
1592
1593void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
1594 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001595 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001596}
locke-lunarga19c71d2020-03-02 18:17:04 -07001597
1598bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
1599 VkImageLayout dstImageLayout, uint32_t regionCount,
1600 const VkBufferImageCopy *pRegions) const {
1601 bool skip = false;
1602 const auto *cb_access_context = GetAccessContext(commandBuffer);
1603 assert(cb_access_context);
1604 if (!cb_access_context) return skip;
1605
1606 const auto *context = cb_access_context->GetCurrentAccessContext();
1607 assert(context);
1608 if (!context) return skip;
1609
1610 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07001611 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
1612
1613 for (uint32_t region = 0; region < regionCount; region++) {
1614 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06001615 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06001616 ResourceAccessRange src_range =
1617 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06001618 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07001619 if (hazard.hazard) {
1620 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06001621 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
1622 "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07001623 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region);
1624 }
1625 }
1626 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001627 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07001628 copy_region.imageOffset, copy_region.imageExtent);
1629 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001630 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
1631 "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07001632 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region);
1633 }
1634 if (skip) break;
1635 }
1636 if (skip) break;
1637 }
1638 return skip;
1639}
1640
1641void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
1642 VkImageLayout dstImageLayout, uint32_t regionCount,
1643 const VkBufferImageCopy *pRegions) {
1644 auto *cb_access_context = GetAccessContext(commandBuffer);
1645 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001646 const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07001647 auto *context = cb_access_context->GetCurrentAccessContext();
1648 assert(context);
1649
1650 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06001651 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07001652
1653 for (uint32_t region = 0; region < regionCount; region++) {
1654 const auto &copy_region = pRegions[region];
1655 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06001656 ResourceAccessRange src_range =
1657 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06001658 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001659 }
1660 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001661 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06001662 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001663 }
1664 }
1665}
1666
1667bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
1668 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
1669 const VkBufferImageCopy *pRegions) const {
1670 bool skip = false;
1671 const auto *cb_access_context = GetAccessContext(commandBuffer);
1672 assert(cb_access_context);
1673 if (!cb_access_context) return skip;
1674
1675 const auto *context = cb_access_context->GetCurrentAccessContext();
1676 assert(context);
1677 if (!context) return skip;
1678
1679 const auto *src_image = Get<IMAGE_STATE>(srcImage);
1680 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
1681 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
1682 for (uint32_t region = 0; region < regionCount; region++) {
1683 const auto &copy_region = pRegions[region];
1684 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001685 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07001686 copy_region.imageOffset, copy_region.imageExtent);
1687 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001688 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
1689 "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07001690 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region);
1691 }
1692 }
1693 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06001694 ResourceAccessRange dst_range =
1695 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06001696 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07001697 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001698 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
1699 "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07001700 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region);
1701 }
1702 }
1703 if (skip) break;
1704 }
1705 return skip;
1706}
1707
1708void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1709 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
1710 auto *cb_access_context = GetAccessContext(commandBuffer);
1711 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001712 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER);
locke-lunarga19c71d2020-03-02 18:17:04 -07001713 auto *context = cb_access_context->GetCurrentAccessContext();
1714 assert(context);
1715
1716 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07001717 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
1718 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 -06001719 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07001720
1721 for (uint32_t region = 0; region < regionCount; region++) {
1722 const auto &copy_region = pRegions[region];
1723 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001724 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06001725 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001726 }
1727 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06001728 ResourceAccessRange dst_range =
1729 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06001730 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001731 }
1732 }
1733}
1734
1735bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1736 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
1737 const VkImageBlit *pRegions, VkFilter filter) const {
1738 bool skip = false;
1739 const auto *cb_access_context = GetAccessContext(commandBuffer);
1740 assert(cb_access_context);
1741 if (!cb_access_context) return skip;
1742
1743 const auto *context = cb_access_context->GetCurrentAccessContext();
1744 assert(context);
1745 if (!context) return skip;
1746
1747 const auto *src_image = Get<IMAGE_STATE>(srcImage);
1748 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
1749
1750 for (uint32_t region = 0; region < regionCount; region++) {
1751 const auto &blit_region = pRegions[region];
1752 if (src_image) {
1753 VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x),
1754 static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y),
1755 static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06001756 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07001757 blit_region.srcOffsets[0], extent);
1758 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001759 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
1760 "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1761 report_data->FormatHandle(srcImage).c_str(), region);
locke-lunarga19c71d2020-03-02 18:17:04 -07001762 }
1763 }
1764
1765 if (dst_image) {
1766 VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x),
1767 static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y),
1768 static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06001769 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07001770 blit_region.dstOffsets[0], extent);
1771 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001772 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
1773 "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1774 report_data->FormatHandle(dstImage).c_str(), region);
locke-lunarga19c71d2020-03-02 18:17:04 -07001775 }
1776 if (skip) break;
1777 }
1778 }
1779
1780 return skip;
1781}
1782
1783void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1784 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
1785 const VkImageBlit *pRegions, VkFilter filter) {
1786 auto *cb_access_context = GetAccessContext(commandBuffer);
1787 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001788 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07001789 auto *context = cb_access_context->GetCurrentAccessContext();
1790 assert(context);
1791
1792 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07001793 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07001794
1795 for (uint32_t region = 0; region < regionCount; region++) {
1796 const auto &blit_region = pRegions[region];
1797 if (src_image) {
1798 VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x),
1799 static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y),
1800 static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06001801 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06001802 blit_region.srcOffsets[0], extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001803 }
1804 if (dst_image) {
1805 VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x),
1806 static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y),
1807 static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06001808 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06001809 blit_region.dstOffsets[0], extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001810 }
1811 }
1812}