blob: 69ce024bb71d9b5a32e8b1be87be640895f89b0c [file] [log] [blame]
John Zulauf9cb530d2019-09-30 14:14:10 -06001/* Copyright (c) 2019 The Khronos Group Inc.
2 * Copyright (c) 2019 Valve Corporation
3 * Copyright (c) 2019 LunarG, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Author: John Zulauf <jzulauf@lunarg.com>
18 */
19
20#include <limits>
21#include <vector>
locke-lunarg296a3c92020-03-25 01:04:29 -060022#include <memory>
23#include <bitset>
John Zulauf9cb530d2019-09-30 14:14:10 -060024#include "synchronization_validation.h"
25
26static const char *string_SyncHazardVUID(SyncHazard hazard) {
27 switch (hazard) {
28 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070029 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060030 break;
31 case SyncHazard::READ_AFTER_WRITE:
32 return "SYNC-HAZARD-READ_AFTER_WRITE";
33 break;
34 case SyncHazard::WRITE_AFTER_READ:
35 return "SYNC-HAZARD-WRITE_AFTER_READ";
36 break;
37 case SyncHazard::WRITE_AFTER_WRITE:
38 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
39 break;
John Zulauf2f952d22020-02-10 11:34:51 -070040 case SyncHazard::READ_RACING_WRITE:
41 return "SYNC-HAZARD-READ-RACING-WRITE";
42 break;
43 case SyncHazard::WRITE_RACING_WRITE:
44 return "SYNC-HAZARD-WRITE-RACING-WRITE";
45 break;
46 case SyncHazard::WRITE_RACING_READ:
47 return "SYNC-HAZARD-WRITE-RACING-READ";
48 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060049 default:
50 assert(0);
51 }
52 return "SYNC-HAZARD-INVALID";
53}
54
55static const char *string_SyncHazard(SyncHazard hazard) {
56 switch (hazard) {
57 case SyncHazard::NONE:
58 return "NONR";
59 break;
60 case SyncHazard::READ_AFTER_WRITE:
61 return "READ_AFTER_WRITE";
62 break;
63 case SyncHazard::WRITE_AFTER_READ:
64 return "WRITE_AFTER_READ";
65 break;
66 case SyncHazard::WRITE_AFTER_WRITE:
67 return "WRITE_AFTER_WRITE";
68 break;
John Zulauf2f952d22020-02-10 11:34:51 -070069 case SyncHazard::READ_RACING_WRITE:
70 return "READ_RACING_WRITE";
71 break;
72 case SyncHazard::WRITE_RACING_WRITE:
73 return "WRITE_RACING_WRITE";
74 break;
75 case SyncHazard::WRITE_RACING_READ:
76 return "WRITE_RACING_READ";
77 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060078 default:
79 assert(0);
80 }
81 return "INVALID HAZARD";
82}
83
locke-lunarg3c038002020-04-30 23:08:08 -060084inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
85 if (size == VK_WHOLE_SIZE) {
86 return (whole_size - offset);
87 }
88 return size;
89}
90
John Zulauf16adfc92020-04-08 10:28:33 -060091template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -060092static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -060093 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
94}
95
John Zulauf355e49b2020-04-24 15:11:15 -060096static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -060097
John Zulauf0cb5be22020-01-23 12:18:22 -070098// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
99VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
100 VkPipelineStageFlags expanded = stage_mask;
101 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
102 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
103 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
104 if (all_commands.first & queue_flags) {
105 expanded |= all_commands.second;
106 }
107 }
108 }
109 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
110 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
111 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
112 }
113 return expanded;
114}
115
John Zulauf36bcf6a2020-02-03 15:12:52 -0700116VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
117 std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
118 VkPipelineStageFlags unscanned = stage_mask;
119 VkPipelineStageFlags related = 0;
120 for (const auto entry : map) {
121 const auto stage = entry.first;
122 if (stage & unscanned) {
123 related = related | entry.second;
124 unscanned = unscanned & ~stage;
125 if (!unscanned) break;
126 }
127 }
128 return related;
129}
130
131VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
132 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
133}
134
135VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
136 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
137}
138
John Zulauf5c5e88d2019-12-26 11:22:02 -0700139static const ResourceAccessRange full_range(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700140
John Zulauf355e49b2020-04-24 15:11:15 -0600141// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
142const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = {
143 AccessContext::AddressType::kLinearAddress, AccessContext::AddressType::kIdealizedAddress};
144
John Zulauf540266b2020-04-06 18:54:53 -0600145AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
146 const std::vector<SubpassDependencyGraphNode> &dependencies,
147 const std::vector<AccessContext> &contexts, AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600148 Reset();
149 const auto &subpass_dep = dependencies[subpass];
150 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600151 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600152 for (const auto &prev_dep : subpass_dep.prev) {
153 assert(prev_dep.dependency);
154 const auto dep = *prev_dep.dependency;
John Zulauf540266b2020-04-06 18:54:53 -0600155 prev_.emplace_back(const_cast<AccessContext *>(&contexts[dep.srcSubpass]), queue_flags, dep);
John Zulauf355e49b2020-04-24 15:11:15 -0600156 prev_by_subpass_[dep.srcSubpass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700157 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600158
159 async_.reserve(subpass_dep.async.size());
160 for (const auto async_subpass : subpass_dep.async) {
John Zulauf540266b2020-04-06 18:54:53 -0600161 async_.emplace_back(const_cast<AccessContext *>(&contexts[async_subpass]));
John Zulauf3d84f1b2020-03-09 13:33:25 -0600162 }
John Zulaufe5da6e52020-03-18 15:32:18 -0600163 if (subpass_dep.barrier_from_external) {
164 src_external_ = TrackBack(external_context, queue_flags, *subpass_dep.barrier_from_external);
165 } else {
166 src_external_ = TrackBack();
167 }
168 if (subpass_dep.barrier_to_external) {
169 dst_external_ = TrackBack(this, queue_flags, *subpass_dep.barrier_to_external);
170 } else {
171 dst_external_ = TrackBack();
John Zulauf3d84f1b2020-03-09 13:33:25 -0600172 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700173}
174
John Zulauf5f13a792020-03-10 07:31:21 -0600175template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600176HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600177 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600178 ResourceAccessRangeMap descent_map;
179 ResourceAccessState default_state; // When present, PreviousAccess will "infill"
John Zulauf16adfc92020-04-08 10:28:33 -0600180 ResolvePreviousAccess(type, range, &descent_map, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600181
182 HazardResult hazard;
183 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
184 hazard = detector.Detect(prev);
185 }
186 return hazard;
187}
188
John Zulauf3d84f1b2020-03-09 13:33:25 -0600189// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
190// the DAG of the contexts (for example subpasses)
191template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600192HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
193 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600194 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600195
John Zulauf355e49b2020-04-24 15:11:15 -0600196 if (static_cast<uint32_t>(options) | DetectOptions::kDetectAsync) {
197 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
198 // so we'll check these first
199 for (const auto &async_context : async_) {
200 hazard = async_context->DetectAsyncHazard(type, detector, range);
201 if (hazard.hazard) return hazard;
202 }
John Zulauf5f13a792020-03-10 07:31:21 -0600203 }
204
John Zulauf355e49b2020-04-24 15:11:15 -0600205 if (static_cast<uint32_t>(options) | DetectOptions::kDetectAsync) {
206 const auto &accesses = GetAccessStateMap(type);
207 const auto from = accesses.lower_bound(range);
208 if (from != accesses.end() && from->first.intersects(range)) {
209 const auto to = accesses.upper_bound(range);
210 ResourceAccessRange gap = {range.begin, range.begin};
211 for (auto pos = from; pos != to; ++pos) {
212 hazard = detector.Detect(pos);
213 if (hazard.hazard) return hazard;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600214
John Zulauf355e49b2020-04-24 15:11:15 -0600215 // make sure we don't go past range
216 auto upper_bound = std::min(range.end, pos->first.end);
217 gap.end = upper_bound;
John Zulauf5f13a792020-03-10 07:31:21 -0600218
John Zulauf355e49b2020-04-24 15:11:15 -0600219 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
220 if (!gap.empty()) {
221 // Must recur on all gaps
222 hazard = DetectPreviousHazard(type, detector, gap);
223 if (hazard.hazard) return hazard;
224 }
225 gap.begin = upper_bound;
226 }
227 gap.end = range.end;
228 if (gap.non_empty()) {
John Zulauf16adfc92020-04-08 10:28:33 -0600229 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600230 if (hazard.hazard) return hazard;
231 }
John Zulauf355e49b2020-04-24 15:11:15 -0600232 } else {
233 hazard = DetectPreviousHazard(type, detector, range);
John Zulauf16adfc92020-04-08 10:28:33 -0600234 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600235 }
236
237 return hazard;
238}
239
240// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
241template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600242HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600243 auto &accesses = GetAccessStateMap(type);
244 const auto from = accesses.lower_bound(range);
245 const auto to = accesses.upper_bound(range);
246
John Zulauf3d84f1b2020-03-09 13:33:25 -0600247 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600248 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
249 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600250 }
John Zulauf16adfc92020-04-08 10:28:33 -0600251
John Zulauf3d84f1b2020-03-09 13:33:25 -0600252 return hazard;
253}
254
John Zulauf355e49b2020-04-24 15:11:15 -0600255// Returns the last resolved entry
256static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
257 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
258 const SyncBarrier *barrier) {
259 auto at = entry;
260 for (auto pos = first; pos != last; ++pos) {
261 // Every member of the input iterator range must fit within the remaining portion of entry
262 assert(at->first.includes(pos->first));
263 assert(at != dest->end());
264 // Trim up at to the same size as the entry to resolve
265 at = sparse_container::split(at, *dest, pos->first);
266 auto access = pos->second;
267 if (barrier) {
268 access.ApplyBarrier(*barrier);
269 }
270 at->second.Resolve(access);
271 ++at; // Go to the remaining unused section of entry
272 }
273}
274
275void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, const SyncBarrier *barrier,
276 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
277 bool recur_to_infill) const {
278 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
279 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf16adfc92020-04-08 10:28:33 -0600280 if (current->pos_B->valid) {
281 const auto &src_pos = current->pos_B->lower_bound;
John Zulauf355e49b2020-04-24 15:11:15 -0600282 auto access = src_pos->second;
283 if (barrier) {
284 access.ApplyBarrier(*barrier);
285 }
John Zulauf16adfc92020-04-08 10:28:33 -0600286 if (current->pos_A->valid) {
287 current.trim_A();
John Zulauf355e49b2020-04-24 15:11:15 -0600288 current->pos_A->lower_bound->second.Resolve(access);
John Zulauf5f13a792020-03-10 07:31:21 -0600289 } else {
John Zulauf355e49b2020-04-24 15:11:15 -0600290 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, access));
291 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600292 }
John Zulauf16adfc92020-04-08 10:28:33 -0600293 } else {
294 // we have to descend to fill this gap
295 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600296 if (current->pos_A->valid) {
297 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
298 ResourceAccessRangeMap gap_map;
299 ResolvePreviousAccess(type, current->range, &gap_map, infill_state);
300 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier);
301 } else {
302 // There isn't anything in dest in current->range, so we can accumulate directly into it.
303 ResolvePreviousAccess(type, current->range, resolve_map, infill_state);
304 if (barrier) {
305 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
306 for (auto pos = resolve_map->lower_bound(current->range); pos != current->pos_A->lower_bound; ++pos) {
307 pos->second.ApplyBarrier(*barrier);
308 }
309 }
310 }
311 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
312 // iterator of the outer while.
313
314 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
315 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
316 // we stepped on the dest map
317 const auto seek_to = current->range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
318 current.invalidate_A(); // Changes current->range
319 current.seek(seek_to);
320 } else if (!current->pos_A->valid && infill_state) {
321 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
322 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
323 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600324 }
John Zulauf5f13a792020-03-10 07:31:21 -0600325 }
John Zulauf16adfc92020-04-08 10:28:33 -0600326 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600327 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600328}
329
John Zulauf355e49b2020-04-24 15:11:15 -0600330void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
331 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600332 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600333 if (range.non_empty() && infill_state) {
334 descent_map->insert(std::make_pair(range, *infill_state));
335 }
336 } else {
337 // Look for something to fill the gap further along.
338 for (const auto &prev_dep : prev_) {
John Zulauf355e49b2020-04-24 15:11:15 -0600339 prev_dep.context->ResolveAccessRange(type, range, &prev_dep.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600340 }
341
John Zulaufe5da6e52020-03-18 15:32:18 -0600342 if (src_external_.context) {
John Zulauf355e49b2020-04-24 15:11:15 -0600343 src_external_.context->ResolveAccessRange(type, range, &src_external_.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600344 }
345 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600346}
347
John Zulauf16adfc92020-04-08 10:28:33 -0600348AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600349 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600350}
351
352VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) {
353 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
354}
355
John Zulauf355e49b2020-04-24 15:11:15 -0600356static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
John Zulauf16adfc92020-04-08 10:28:33 -0600357
John Zulauf1507ee42020-05-18 11:33:09 -0600358static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
359 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
360 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
361 return stage_access;
362}
363static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
364 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
365 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
366 return stage_access;
367}
368
John Zulauf540266b2020-04-06 18:54:53 -0600369void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
John Zulauf355e49b2020-04-24 15:11:15 -0600370 AddressType address_type, ResourceAccessRangeMap *descent_map,
371 const ResourceAccessState *infill_state) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600372 if (!SimpleBinding(image_state)) return;
373
John Zulauf62f10592020-04-03 12:20:02 -0600374 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
locke-lunargae26eac2020-04-16 15:29:05 -0600375 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600376 image_state.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600377 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf62f10592020-04-03 12:20:02 -0600378 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600379 ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state);
John Zulauf62f10592020-04-03 12:20:02 -0600380 }
381}
382
John Zulauf1507ee42020-05-18 11:33:09 -0600383bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
384 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
385 uint32_t subpass) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600386 bool skip = false;
387 const auto &transitions = rp_state.subpass_transitions[subpass];
388 for (const auto &transition : transitions) {
John Zulauf1507ee42020-05-18 11:33:09 -0600389 auto hazard = DetectSubpassTransitionHazard(transition, attachment_views);
John Zulauf355e49b2020-04-24 15:11:15 -0600390 if (hazard.hazard) {
391 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
392 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " image layout transition.",
393 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment);
394 }
395 }
396 return skip;
397}
398
John Zulauf1507ee42020-05-18 11:33:09 -0600399bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
400 const VkRect2D &render_area,
401 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
402 uint32_t subpass) const {
403 bool skip = false;
404 const auto *attachment_ci = rp_state.createInfo.pAttachments;
405 VkExtent3D extent = CastTo3D(render_area.extent);
406 VkOffset3D offset = CastTo3D(render_area.offset);
407 const auto external_access_scope = src_external_.barrier.dst_access_scope;
408 HazardResult hazard;
409
410 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
411 if (subpass == rp_state.attachment_first_subpass[i]) {
412 if (attachment_views[i] == nullptr) continue;
413 const IMAGE_VIEW_STATE &view = *attachment_views[i];
414 const IMAGE_STATE *image = view.image_state.get();
415 if (image == nullptr) continue;
416 const auto &ci = attachment_ci[i];
417 const bool is_transition = rp_state.attachment_first_is_transition[i];
418
419 // Need check in the following way
420 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
421 // vs. transition
422 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
423 // for each aspect loaded.
424
425 const bool has_depth = FormatHasDepth(ci.format);
426 const bool has_stencil = FormatHasDepth(ci.format);
427 const bool is_color = !(has_depth || has_stencil);
428
429 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
430 const SyncStageAccessFlags load_mask = (has_depth || is_color) ? SyncStageAccess::Flags(load_index) : 0U;
431 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
432 const SyncStageAccessFlags stencil_mask = has_stencil ? SyncStageAccess::Flags(stencil_load_index) : 0U;
433
434 const char *aspect = nullptr;
435 if (is_transition) {
436 // For transition w
437 SyncHazard transition_hazard = SyncHazard::NONE;
438 bool checked_stencil = false;
439 if (load_mask) {
440 if ((load_mask & external_access_scope) != load_mask) {
441 transition_hazard =
442 SyncStageAccess::HasWrite(load_mask) ? SyncHazard::WRITE_AFTER_WRITE : SyncHazard::READ_AFTER_WRITE;
443 aspect = is_color ? "color" : "depth";
444 }
445 if (!transition_hazard && stencil_mask) {
446 if ((stencil_mask & external_access_scope) != stencil_mask) {
447 transition_hazard = SyncStageAccess::HasWrite(stencil_mask) ? SyncHazard::WRITE_AFTER_WRITE
448 : SyncHazard::READ_AFTER_WRITE;
449 aspect = "stencil";
450 checked_stencil = true;
451 }
452 }
453 }
454 if (transition_hazard) {
455 // Hazard vs. ILT
456 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
457 skip |=
458 sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
459 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
460 " aspect %s during load with loadOp %s.",
461 func_name, string_SyncHazard(transition_hazard), subpass, i, aspect, load_op_string);
462 }
463 } else {
464 auto hazard_range = view.normalized_subresource_range;
465 bool checked_stencil = false;
466 if (is_color) {
467 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, offset, extent);
468 aspect = "color";
469 } else {
470 if (has_depth) {
471 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
472 hazard = DetectHazard(*image, load_index, hazard_range, offset, extent);
473 aspect = "depth";
474 }
475 if (!hazard.hazard && has_stencil) {
476 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
477 hazard = DetectHazard(*image, stencil_load_index, hazard_range, offset, extent);
478 aspect = "stencil";
479 checked_stencil = true;
480 }
481 }
482
483 if (hazard.hazard) {
484 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
485 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
486 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
487 " aspect %s during load with loadOp %s.",
488 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
489 }
490 }
491 }
492 }
493 return skip;
494}
495
John Zulauf3d84f1b2020-03-09 13:33:25 -0600496class HazardDetector {
497 SyncStageAccessIndex usage_index_;
498
499 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600500 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600501 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
502 return pos->second.DetectAsyncHazard(usage_index_);
503 }
504 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
505};
506
John Zulauf16adfc92020-04-08 10:28:33 -0600507HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600508 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600509 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600510 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600511}
512
John Zulauf16adfc92020-04-08 10:28:33 -0600513HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600514 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600515 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -0600516 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -0600517}
518
John Zulauf540266b2020-04-06 18:54:53 -0600519HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
520 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
521 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700522 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
523 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -0600524 return DetectHazard(image, current_usage, subresource_range, offset, extent);
525}
526
527HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
528 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
529 const VkExtent3D &extent) const {
530 if (!SimpleBinding(image)) return HazardResult();
locke-lunargae26eac2020-04-16 15:29:05 -0600531 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600532 const auto address_type = ImageAddressType(image);
533 const auto base_address = ResourceBaseAddress(image);
John Zulauf5c5e88d2019-12-26 11:22:02 -0700534 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600535 HazardResult hazard = DetectHazard(address_type, current_usage, (*range_gen + base_address));
John Zulauf5c5e88d2019-12-26 11:22:02 -0700536 if (hazard.hazard) return hazard;
537 }
538 return HazardResult();
John Zulauf9cb530d2019-09-30 14:14:10 -0600539}
540
John Zulauf3d84f1b2020-03-09 13:33:25 -0600541class BarrierHazardDetector {
542 public:
543 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
544 SyncStageAccessFlags src_access_scope)
545 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
546
John Zulauf5f13a792020-03-10 07:31:21 -0600547 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
548 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -0700549 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600550 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
551 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
552 return pos->second.DetectAsyncHazard(usage_index_);
553 }
554
555 private:
556 SyncStageAccessIndex usage_index_;
557 VkPipelineStageFlags src_exec_scope_;
558 SyncStageAccessFlags src_access_scope_;
559};
560
John Zulauf16adfc92020-04-08 10:28:33 -0600561HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -0600562 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600563 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600564 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf355e49b2020-04-24 15:11:15 -0600565 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf0cb5be22020-01-23 12:18:22 -0700566}
567
John Zulauf16adfc92020-04-08 10:28:33 -0600568HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600569 SyncStageAccessFlags src_access_scope,
570 const VkImageSubresourceRange &subresource_range,
571 DetectOptions options) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600572 if (!SimpleBinding(image)) return HazardResult();
locke-lunargae26eac2020-04-16 15:29:05 -0600573 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600574 image.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600575 const auto address_type = ImageAddressType(image);
576 const auto base_address = ResourceBaseAddress(image);
locke-lunarg296a3c92020-03-25 01:04:29 -0600577 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf355e49b2020-04-24 15:11:15 -0600578 HazardResult hazard = DetectBarrierHazard(address_type, SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope,
579 src_access_scope, (*range_gen + base_address), options);
locke-lunarg296a3c92020-03-25 01:04:29 -0600580 if (hazard.hazard) return hazard;
John Zulauf0cb5be22020-01-23 12:18:22 -0700581 }
582 return HazardResult();
583}
584
John Zulauf355e49b2020-04-24 15:11:15 -0600585HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
586 SyncStageAccessFlags src_stage_accesses,
587 const VkImageMemoryBarrier &barrier) const {
588 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
589 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
590 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
591}
592
John Zulauf9cb530d2019-09-30 14:14:10 -0600593template <typename Flags, typename Map>
594SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
595 SyncStageAccessFlags scope = 0;
596 for (const auto &bit_scope : map) {
597 if (flag_mask < bit_scope.first) break;
598
599 if (flag_mask & bit_scope.first) {
600 scope |= bit_scope.second;
601 }
602 }
603 return scope;
604}
605
606SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
607 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
608}
609
610SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
611 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
612}
613
614// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
615SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -0600616 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
617 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
618 // 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 -0600619 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
620}
621
622template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -0700623void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf9cb530d2019-09-30 14:14:10 -0600624 // TODO -- region/mem-range accuracte update
625 auto pos = accesses->lower_bound(range);
626 if (pos == accesses->end() || !pos->first.intersects(range)) {
627 // The range is empty, fill it with a default value.
628 pos = action.Infill(accesses, pos, range);
629 } else if (range.begin < pos->first.begin) {
630 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -0700631 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -0600632 } else if (pos->first.begin < range.begin) {
633 // Trim the beginning if needed
634 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
635 ++pos;
636 }
637
638 const auto the_end = accesses->end();
639 while ((pos != the_end) && pos->first.intersects(range)) {
640 if (pos->first.end > range.end) {
641 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
642 }
643
644 pos = action(accesses, pos);
645 if (pos == the_end) break;
646
647 auto next = pos;
648 ++next;
649 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
650 // Need to infill if next is disjoint
651 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -0700652 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -0600653 next = action.Infill(accesses, next, new_range);
654 }
655 pos = next;
656 }
657}
658
659struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700660 using Iterator = ResourceAccessRangeMap::iterator;
661 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600662 // this is only called on gaps, and never returns a gap.
663 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -0600664 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600665 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -0600666 }
John Zulauf5f13a792020-03-10 07:31:21 -0600667
John Zulauf5c5e88d2019-12-26 11:22:02 -0700668 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -0600669 auto &access_state = pos->second;
670 access_state.Update(usage, tag);
671 return pos;
672 }
673
John Zulauf16adfc92020-04-08 10:28:33 -0600674 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -0600675 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -0600676 : type(type_), context(context_), usage(usage_), tag(tag_) {}
677 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -0600678 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -0600679 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -0600680 const ResourceUsageTag &tag;
681};
682
683struct ApplyMemoryAccessBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700684 using Iterator = ResourceAccessRangeMap::iterator;
685 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -0600686
John Zulauf5c5e88d2019-12-26 11:22:02 -0700687 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -0600688 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700689 access_state.ApplyMemoryAccessBarrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -0600690 return pos;
691 }
692
John Zulauf36bcf6a2020-02-03 15:12:52 -0700693 ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_,
694 VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_)
695 : src_exec_scope(src_exec_scope_),
696 src_access_scope(src_access_scope_),
697 dst_exec_scope(dst_exec_scope_),
698 dst_access_scope(dst_access_scope_) {}
John Zulauf9cb530d2019-09-30 14:14:10 -0600699
John Zulauf36bcf6a2020-02-03 15:12:52 -0700700 VkPipelineStageFlags src_exec_scope;
701 SyncStageAccessFlags src_access_scope;
702 VkPipelineStageFlags dst_exec_scope;
703 SyncStageAccessFlags dst_access_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -0600704};
705
706struct ApplyGlobalBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700707 using Iterator = ResourceAccessRangeMap::iterator;
708 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -0600709
John Zulauf5c5e88d2019-12-26 11:22:02 -0700710 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -0600711 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700712 access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -0600713
714 for (const auto &functor : barrier_functor) {
715 functor(accesses, pos);
716 }
717 return pos;
718 }
719
John Zulauf36bcf6a2020-02-03 15:12:52 -0700720 ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope,
721 SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses,
John Zulauf9cb530d2019-09-30 14:14:10 -0600722 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers)
John Zulauf36bcf6a2020-02-03 15:12:52 -0700723 : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -0600724 // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier...
725 barrier_functor.reserve(memoryBarrierCount);
726 for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) {
727 const auto &barrier = pMemoryBarriers[barrier_index];
John Zulauf36bcf6a2020-02-03 15:12:52 -0700728 barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask),
729 dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
John Zulauf9cb530d2019-09-30 14:14:10 -0600730 }
731 }
732
John Zulauf36bcf6a2020-02-03 15:12:52 -0700733 const VkPipelineStageFlags src_exec_scope;
734 const VkPipelineStageFlags dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -0600735 std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor;
736};
737
John Zulauf355e49b2020-04-24 15:11:15 -0600738void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
739 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -0600740 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
741 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600742}
743
John Zulauf16adfc92020-04-08 10:28:33 -0600744void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -0600745 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -0600746 if (!SimpleBinding(buffer)) return;
747 const auto base_address = ResourceBaseAddress(buffer);
748 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
749}
John Zulauf355e49b2020-04-24 15:11:15 -0600750
John Zulauf540266b2020-04-06 18:54:53 -0600751void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -0600752 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -0600753 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -0600754 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -0600755 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600756 const auto address_type = ImageAddressType(image);
757 const auto base_address = ResourceBaseAddress(image);
758 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -0600759 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600760 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -0600761 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600762}
763
John Zulauf355e49b2020-04-24 15:11:15 -0600764void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
765 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
766 const VkExtent3D &extent, const ResourceUsageTag &tag) {
767 // TODO: replace the encoder/generator with offset3D aware versions
768 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
769 subresource.layerCount};
770 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
771}
772
John Zulauf540266b2020-04-06 18:54:53 -0600773template <typename Action>
774void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -0600775 if (!SimpleBinding(buffer)) return;
776 const auto base_address = ResourceBaseAddress(buffer);
777 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -0600778}
779
780template <typename Action>
781void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
782 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -0600783 if (!SimpleBinding(image)) return;
784 const auto address_type = ImageAddressType(image);
785 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -0600786
locke-lunargae26eac2020-04-16 15:29:05 -0600787 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600788 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -0600789
John Zulauf16adfc92020-04-08 10:28:33 -0600790 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -0600791 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600792 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -0600793 }
794}
795
796template <typename Action>
797void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
798 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -0600799 for (const auto address_type : kAddressTypes) {
800 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -0600801 }
802}
803
804void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -0600805 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
806 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -0600807 for (const auto address_type : kAddressTypes) {
John Zulauf355e49b2020-04-24 15:11:15 -0600808 context.ResolveAccessRange(address_type, full_range, &context.GetDstExternalTrackBack().barrier,
809 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -0600810 }
811 }
812}
813
John Zulauf355e49b2020-04-24 15:11:15 -0600814void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
815 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
816 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) {
817 const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
818 UpdateMemoryAccess(image, subresource_range, barrier_action);
819}
820
821// TODO: Plumb offset/extent throughout the image call stacks, with default injector overloads to preserved backwards compatiblity
822// as needed
823void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
824 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
825 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range,
826 bool layout_transition, const ResourceUsageTag &tag) {
827 if (layout_transition) {
828 UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent,
829 tag);
830 ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope,
831 subresource_range);
John Zulaufc9201222020-05-13 15:13:03 -0600832 } else {
833 ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range);
John Zulauf355e49b2020-04-24 15:11:15 -0600834 }
John Zulauf355e49b2020-04-24 15:11:15 -0600835}
836
837void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier,
838 const VkImageSubresourceRange &subresource_range, bool layout_transition,
839 const ResourceUsageTag &tag) {
840 ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope,
841 subresource_range, layout_transition, tag);
842}
843
844// Suitable only for *subpass* access contexts
845HazardResult AccessContext::DetectSubpassTransitionHazard(const RENDER_PASS_STATE::AttachmentTransition &transition,
846 const std::vector<const IMAGE_VIEW_STATE *> &attachments) const {
847 const auto *attach_view = attachments[transition.attachment];
848 if (!attach_view) return HazardResult();
849 const auto image_state = attach_view->image_state.get();
850 if (!image_state) return HazardResult();
851
852 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
853 // We should never ask for a transition from a context we don't have
854 assert(track_back);
855 assert(track_back->context);
856
857 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
858 auto hazard = track_back->context->DetectImageBarrierHazard(*image_state, track_back->barrier.src_exec_scope,
859 track_back->barrier.src_access_scope,
860 attach_view->normalized_subresource_range, kDetectPrevious);
861 if (!hazard.hazard) {
862 // The Async hazard check is against the current context's async set.
863 hazard = DetectImageBarrierHazard(*image_state, track_back->barrier.src_exec_scope, track_back->barrier.src_access_scope,
864 attach_view->normalized_subresource_range, kDetectAsync);
865 }
866 return hazard;
867}
868
869// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
870bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
871
872 const VkRenderPassBeginInfo *pRenderPassBegin,
873 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
874 const char *func_name) const {
875 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
876 bool skip = false;
877 uint32_t subpass = 0;
878 const auto &transitions = rp_state.subpass_transitions[subpass];
879 if (transitions.size()) {
880 const std::vector<AccessContext> empty_context_vector;
881 // Create context we can use to validate against...
882 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
883 const_cast<AccessContext *>(&cb_access_context_));
884
885 assert(pRenderPassBegin);
886 if (nullptr == pRenderPassBegin) return skip;
887
888 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
889 assert(fb_state);
890 if (nullptr == fb_state) return skip;
891
892 // Create a limited array of views (which we'll need to toss
893 std::vector<const IMAGE_VIEW_STATE *> views;
894 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
895 const auto attachment_count = count_attachment.first;
896 const auto *attachments = count_attachment.second;
897 views.resize(attachment_count, nullptr);
898 for (const auto &transition : transitions) {
899 assert(transition.attachment < attachment_count);
900 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
901 }
902
John Zulauf1507ee42020-05-18 11:33:09 -0600903 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, views, func_name, 0);
904 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, views, func_name, 0);
John Zulauf355e49b2020-04-24 15:11:15 -0600905 }
906 return skip;
907}
908
909bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
910 // TODO: Things to add here.
911 // Validate Preserve/Resolve attachments
912 bool skip = false;
John Zulauf1507ee42020-05-18 11:33:09 -0600913 skip |=
914 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -0600915
916 return skip;
917}
918
919bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
920 // TODO: Things to add here.
921 // Validate Preserve/Resolve attachments
922 bool skip = false;
923 skip |= current_renderpass_context_->ValidateFinalSubpassLayoutTransitions(*sync_state_, func_name);
924
925 return skip;
926}
927
928void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
929 assert(sync_state_);
930 if (!cb_state_) return;
931
932 // Create an access context the current renderpass.
933 render_pass_contexts_.emplace_back(&cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -0600934 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf355e49b2020-04-24 15:11:15 -0600935 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -0600936 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -0600937}
938
John Zulauf355e49b2020-04-24 15:11:15 -0600939void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -0600940 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -0600941 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -0600942 current_context_ = &current_renderpass_context_->CurrentContext();
943}
944
John Zulauf355e49b2020-04-24 15:11:15 -0600945void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
946 // TODO: Add layout load/store/resolve access (here or in RenderPassContext)
John Zulauf16adfc92020-04-08 10:28:33 -0600947 assert(current_renderpass_context_);
948 if (!current_renderpass_context_) return;
949
John Zulauf355e49b2020-04-24 15:11:15 -0600950 current_renderpass_context_->RecordEndRenderPass(tag);
951 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -0600952 current_renderpass_context_ = nullptr;
953}
954
John Zulauf1507ee42020-05-18 11:33:09 -0600955bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
956 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600957 bool skip = false;
958 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -0600959 const auto &next_context = subpass_contexts_[next_subpass];
960 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, attachment_views_, func_name, next_subpass);
961 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, attachment_views_, func_name, next_subpass);
John Zulauf355e49b2020-04-24 15:11:15 -0600962 return skip;
963}
964
965bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const char *func_name) const {
966 bool skip = false;
967
968 // Validate the "finalLayout" transitions to external
969 // Get them from where there we're hidding in the extra entry.
970 const auto &final_transitions = rp_state_->subpass_transitions.back();
971 for (const auto &transition : final_transitions) {
972 const auto &attach_view = attachment_views_[transition.attachment];
973 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
974 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
975 auto hazard = trackback.context->DetectImageBarrierHazard(
976 *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope,
977 attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious);
978 if (hazard.hazard) {
979 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
980 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
981 " final image layout transition.",
982 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment);
983 }
984 }
985 return skip;
986}
987
988void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
989 // Add layout transitions...
990 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
991 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -0600992 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -0600993 for (const auto &transition : transitions) {
994 const auto attachment_view = attachment_views_[transition.attachment];
995 if (!attachment_view) continue;
996 const auto image = attachment_view->image_state.get();
997 if (!image) continue;
998
999 const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
John Zulaufc9201222020-05-13 15:13:03 -06001000 auto insert_pair = view_seen.insert(attachment_view);
1001 if (insert_pair.second) {
1002 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
1003 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag);
1004
1005 } else {
1006 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
1007 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
1008 auto barrier_to_transition = barrier->barrier;
1009 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
1010 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag);
1011 }
John Zulauf355e49b2020-04-24 15:11:15 -06001012 }
1013}
1014
John Zulauf1507ee42020-05-18 11:33:09 -06001015void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
1016 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
1017 auto &subpass_context = subpass_contexts_[current_subpass_];
1018 VkExtent3D extent = CastTo3D(render_area.extent);
1019 VkOffset3D offset = CastTo3D(render_area.offset);
1020
1021 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
1022 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
1023 if (attachment_views_[i] == nullptr) continue; // UNUSED
1024 const auto &view = *attachment_views_[i];
1025 const IMAGE_STATE *image = view.image_state.get();
1026 if (image == nullptr) continue;
1027
1028 const auto &ci = attachment_ci[i];
1029 const bool has_depth = FormatHasDepth(ci.format);
1030 const bool has_stencil = FormatHasDepth(ci.format);
1031 const bool is_color = !(has_depth || has_stencil);
1032
1033 if (is_color) {
1034 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
1035 extent, tag);
1036 } else {
1037 auto update_range = view.normalized_subresource_range;
1038 if (has_depth) {
1039 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1040 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
1041 }
1042 if (has_stencil) {
1043 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1044 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
1045 tag);
1046 }
1047 }
1048 }
1049 }
1050}
1051
John Zulauf355e49b2020-04-24 15:11:15 -06001052void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
1053 VkQueueFlags queue_flags, const ResourceUsageTag &tag) {
1054 current_subpass_ = 0;
1055 rp_state_ = cb_state.activeRenderPass;
1056 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
1057 // Add this for all subpasses here so that they exsist during next subpass validation
1058 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
1059 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context_);
1060 }
1061 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
1062
1063 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06001064 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001065}
John Zulauf1507ee42020-05-18 11:33:09 -06001066
1067void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001068 current_subpass_++;
1069 assert(current_subpass_ < subpass_contexts_.size());
1070 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06001071 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001072}
1073
1074void RenderPassAccessContext::RecordEndRenderPass(const ResourceUsageTag &tag) {
1075 // Export the accesses from the renderpass...
1076 external_context_->ResolveChildContexts(subpass_contexts_);
1077
1078 // Add the "finalLayout" transitions to external
1079 // Get them from where there we're hidding in the extra entry.
1080 const auto &final_transitions = rp_state_->subpass_transitions.back();
1081 for (const auto &transition : final_transitions) {
1082 const auto &attachment = attachment_views_[transition.attachment];
1083 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
1084 assert(external_context_ == last_trackback.context);
1085 external_context_->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier,
1086 attachment->normalized_subresource_range, true, tag);
1087 }
1088}
1089
John Zulauf3d84f1b2020-03-09 13:33:25 -06001090SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
1091 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
1092 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
1093 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
1094 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
1095 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
1096 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
1097}
1098
1099void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) {
1100 ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
1101 ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope);
1102}
1103
John Zulauf9cb530d2019-09-30 14:14:10 -06001104HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
1105 HazardResult hazard;
1106 auto usage = FlagBit(usage_index);
1107 if (IsRead(usage)) {
John Zulaufc9201222020-05-13 15:13:03 -06001108 if (last_write && IsWriteHazard(usage)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001109 hazard.Set(READ_AFTER_WRITE, write_tag);
1110 }
1111 } else {
1112 // Assume write
1113 // TODO determine what to do with READ-WRITE usage states if any
1114 // Write-After-Write check -- if we have a previous write to test against
1115 if (last_write && IsWriteHazard(usage)) {
1116 hazard.Set(WRITE_AFTER_WRITE, write_tag);
1117 } else {
1118 // Only look for casus belli for WAR
1119 const auto usage_stage = PipelineStageBit(usage_index);
1120 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
1121 if (IsReadHazard(usage_stage, last_reads[read_index])) {
1122 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
1123 break;
1124 }
1125 }
1126 }
1127 }
1128 return hazard;
1129}
1130
John Zulauf2f952d22020-02-10 11:34:51 -07001131// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06001132HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07001133 HazardResult hazard;
1134 auto usage = FlagBit(usage_index);
1135 if (IsRead(usage)) {
1136 if (last_write != 0) {
1137 hazard.Set(READ_RACING_WRITE, write_tag);
1138 }
1139 } else {
1140 if (last_write != 0) {
1141 hazard.Set(WRITE_RACING_WRITE, write_tag);
1142 } else if (last_read_count > 0) {
1143 hazard.Set(WRITE_RACING_READ, last_reads[0].tag);
1144 }
1145 }
1146 return hazard;
1147}
1148
John Zulauf36bcf6a2020-02-03 15:12:52 -07001149HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1150 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07001151 // Only supporting image layout transitions for now
1152 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
1153 HazardResult hazard;
1154 if (last_write) {
1155 // If the previous write is *not* in the 1st access scope
1156 // *AND* the current barrier is not in the dependency chain
1157 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
1158 // then the barrier access is unsafe (R/W after W)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001159 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
John Zulauf0cb5be22020-01-23 12:18:22 -07001160 // TODO: Do we need a difference hazard name for this?
1161 hazard.Set(WRITE_AFTER_WRITE, write_tag);
1162 }
John Zulauf355e49b2020-04-24 15:11:15 -06001163 }
1164 if (!hazard.hazard) {
1165 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07001166 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07001167 const auto &read_access = last_reads[read_index];
1168 // If the read stage is not in the src sync sync
1169 // *AND* not execution chained with an existing sync barrier (that's the or)
1170 // then the barrier access is unsafe (R/W after R)
1171 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
1172 hazard.Set(WRITE_AFTER_READ, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07001173 break;
1174 }
1175 }
1176 }
1177 return hazard;
1178}
1179
John Zulauf5f13a792020-03-10 07:31:21 -06001180// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
1181// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
1182// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
1183void ResourceAccessState::Resolve(const ResourceAccessState &other) {
1184 if (write_tag.IsBefore(other.write_tag)) {
1185 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
1186 *this = other;
1187 } else if (!other.write_tag.IsBefore(write_tag)) {
1188 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
1189 // dependency chaining logic or any stage expansion)
1190 write_barriers |= other.write_barriers;
1191
1192 // Merge that read states
1193 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
1194 auto &other_read = other.last_reads[other_read_index];
1195 if (last_read_stages & other_read.stage) {
1196 // Merge in the barriers for read stages that exist in *both* this and other
1197 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
1198 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
1199 auto &my_read = last_reads[my_read_index];
1200 if (other_read.stage == my_read.stage) {
1201 if (my_read.tag.IsBefore(other_read.tag)) {
1202 my_read.tag = other_read.tag;
1203 }
1204 my_read.barriers |= other_read.barriers;
1205 break;
1206 }
1207 }
1208 } else {
1209 // The other read stage doesn't exist in this, so add it.
1210 last_reads[last_read_count] = other_read;
1211 last_read_count++;
1212 last_read_stages |= other_read.stage;
1213 }
1214 }
1215 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
1216 // it.
1217}
1218
John Zulauf9cb530d2019-09-30 14:14:10 -06001219void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
1220 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
1221 const auto usage_bit = FlagBit(usage_index);
1222 if (IsRead(usage_index)) {
1223 // Mulitple outstanding reads may be of interest and do dependency chains independently
1224 // However, for purposes of barrier tracking, only one read per pipeline stage matters
1225 const auto usage_stage = PipelineStageBit(usage_index);
1226 if (usage_stage & last_read_stages) {
1227 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
1228 ReadState &access = last_reads[read_index];
1229 if (access.stage == usage_stage) {
1230 access.barriers = 0;
1231 access.tag = tag;
1232 break;
1233 }
1234 }
1235 } else {
1236 // We don't have this stage in the list yet...
1237 assert(last_read_count < last_reads.size());
1238 ReadState &access = last_reads[last_read_count++];
1239 access.stage = usage_stage;
1240 access.barriers = 0;
1241 access.tag = tag;
1242 last_read_stages |= usage_stage;
1243 }
1244 } else {
1245 // Assume write
1246 // TODO determine what to do with READ-WRITE operations if any
1247 // Clobber last read and both sets of barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
1248 // if the last_reads/last_write were unsafe, we've reported them,
1249 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
1250 last_read_count = 0;
1251 last_read_stages = 0;
1252
1253 write_barriers = 0;
1254 write_dependency_chain = 0;
1255 write_tag = tag;
1256 last_write = usage_bit;
1257 }
1258}
John Zulauf5f13a792020-03-10 07:31:21 -06001259
John Zulauf9cb530d2019-09-30 14:14:10 -06001260void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
1261 // Execution Barriers only protect read operations
1262 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
1263 ReadState &access = last_reads[read_index];
1264 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
1265 if (srcStageMask & (access.stage | access.barriers)) {
1266 access.barriers |= dstStageMask;
1267 }
1268 }
1269 if (write_dependency_chain & srcStageMask) write_dependency_chain |= dstStageMask;
1270}
1271
John Zulauf36bcf6a2020-02-03 15:12:52 -07001272void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
1273 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001274 // Assuming we've applied the execution side of this barrier, we update just the write
1275 // The || implements the "dependency chain" logic for this barrier
John Zulauf36bcf6a2020-02-03 15:12:52 -07001276 if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) {
1277 write_barriers |= dst_access_scope;
1278 write_dependency_chain |= dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001279 }
1280}
1281
John Zulaufd1f85d42020-04-15 12:23:15 -06001282void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001283 auto *access_context = GetAccessContextNoInsert(command_buffer);
1284 if (access_context) {
1285 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06001286 }
1287}
1288
John Zulaufd1f85d42020-04-15 12:23:15 -06001289void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
1290 auto access_found = cb_access_state.find(command_buffer);
1291 if (access_found != cb_access_state.end()) {
1292 access_found->second->Reset();
1293 cb_access_state.erase(access_found);
1294 }
1295}
1296
John Zulauf540266b2020-04-06 18:54:53 -06001297void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07001298 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
1299 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06001300 const VkMemoryBarrier *pMemoryBarriers) {
1301 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07001302 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06001303 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06001304 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06001305}
1306
John Zulauf540266b2020-04-06 18:54:53 -06001307void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07001308 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
1309 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06001310 const VkBufferMemoryBarrier *barriers) {
1311 // TODO Implement this at subresource/memory_range accuracy
1312 for (uint32_t index = 0; index < barrier_count; index++) {
locke-lunarg3c038002020-04-30 23:08:08 -06001313 auto barrier = barriers[index];
John Zulauf9cb530d2019-09-30 14:14:10 -06001314 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
1315 if (!buffer) continue;
locke-lunarg3c038002020-04-30 23:08:08 -06001316 barrier.size = GetRealWholeSize(barrier.offset, barrier.size, buffer->createInfo.size);
John Zulauf16adfc92020-04-08 10:28:33 -06001317 ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06001318 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
1319 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
1320 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1321 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06001322 }
1323}
1324
John Zulauf540266b2020-04-06 18:54:53 -06001325void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
1326 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
1327 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06001328 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001329 for (uint32_t index = 0; index < barrier_count; index++) {
1330 const auto &barrier = barriers[index];
1331 const auto *image = Get<IMAGE_STATE>(barrier.image);
1332 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06001333 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06001334 bool layout_transition = barrier.oldLayout != barrier.newLayout;
1335 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
1336 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
1337 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
1338 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001339 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001340}
1341
1342bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
1343 uint32_t regionCount, const VkBufferCopy *pRegions) const {
1344 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001345 const auto *cb_context = GetAccessContext(commandBuffer);
1346 assert(cb_context);
1347 if (!cb_context) return skip;
1348 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06001349
John Zulauf3d84f1b2020-03-09 13:33:25 -06001350 // If we have no previous accesses, we have no hazards
1351 // TODO: make this sub-resource capable
1352 // TODO: make this general, and stuff it into templates/utility functions
1353 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001354 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001355
1356 for (uint32_t region = 0; region < regionCount; region++) {
1357 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06001358 if (src_buffer) {
locke-lunarg3c038002020-04-30 23:08:08 -06001359 ResourceAccessRange src_range =
1360 MakeRange(copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06001361 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001362 if (hazard.hazard) {
1363 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06001364 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
1365 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1366 report_data->FormatHandle(srcBuffer).c_str(), region);
John Zulauf9cb530d2019-09-30 14:14:10 -06001367 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001368 }
John Zulauf16adfc92020-04-08 10:28:33 -06001369 if (dst_buffer && !skip) {
locke-lunarg3c038002020-04-30 23:08:08 -06001370 ResourceAccessRange dst_range =
1371 MakeRange(copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf355e49b2020-04-24 15:11:15 -06001372 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001373 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001374 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
1375 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1376 report_data->FormatHandle(dstBuffer).c_str(), region);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001377 }
1378 }
1379 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06001380 }
1381 return skip;
1382}
1383
1384void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
1385 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001386 auto *cb_context = GetAccessContext(commandBuffer);
1387 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001388 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001389 auto *context = cb_context->GetCurrentAccessContext();
1390
John Zulauf9cb530d2019-09-30 14:14:10 -06001391 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06001392 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06001393
1394 for (uint32_t region = 0; region < regionCount; region++) {
1395 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06001396 if (src_buffer) {
locke-lunarg3c038002020-04-30 23:08:08 -06001397 ResourceAccessRange src_range =
1398 MakeRange(copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06001399 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001400 }
John Zulauf16adfc92020-04-08 10:28:33 -06001401 if (dst_buffer) {
locke-lunarg3c038002020-04-30 23:08:08 -06001402 ResourceAccessRange dst_range =
1403 MakeRange(copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06001404 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001405 }
1406 }
1407}
1408
1409bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1410 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
1411 const VkImageCopy *pRegions) const {
1412 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001413 const auto *cb_access_context = GetAccessContext(commandBuffer);
1414 assert(cb_access_context);
1415 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07001416
John Zulauf3d84f1b2020-03-09 13:33:25 -06001417 const auto *context = cb_access_context->GetCurrentAccessContext();
1418 assert(context);
1419 if (!context) return skip;
1420
1421 const auto *src_image = Get<IMAGE_STATE>(srcImage);
1422 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001423 for (uint32_t region = 0; region < regionCount; region++) {
1424 const auto &copy_region = pRegions[region];
1425 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001426 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001427 copy_region.srcOffset, copy_region.extent);
1428 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001429 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
1430 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1431 report_data->FormatHandle(srcImage).c_str(), region);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001432 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001433 }
1434
1435 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07001436 VkExtent3D dst_copy_extent =
1437 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001438 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07001439 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001440 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001441 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
1442 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1443 report_data->FormatHandle(dstImage).c_str(), region);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001444 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07001445 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07001446 }
1447 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001448
John Zulauf5c5e88d2019-12-26 11:22:02 -07001449 return skip;
1450}
1451
1452void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1453 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
1454 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001455 auto *cb_access_context = GetAccessContext(commandBuffer);
1456 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001457 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001458 auto *context = cb_access_context->GetCurrentAccessContext();
1459 assert(context);
1460
John Zulauf5c5e88d2019-12-26 11:22:02 -07001461 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001462 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001463
1464 for (uint32_t region = 0; region < regionCount; region++) {
1465 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06001466 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001467 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
1468 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001469 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001470 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07001471 VkExtent3D dst_copy_extent =
1472 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001473 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
1474 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001475 }
1476 }
1477}
1478
1479bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1480 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1481 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1482 uint32_t bufferMemoryBarrierCount,
1483 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1484 uint32_t imageMemoryBarrierCount,
1485 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
1486 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001487 const auto *cb_access_context = GetAccessContext(commandBuffer);
1488 assert(cb_access_context);
1489 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07001490
John Zulauf3d84f1b2020-03-09 13:33:25 -06001491 const auto *context = cb_access_context->GetCurrentAccessContext();
1492 assert(context);
1493 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07001494
John Zulauf3d84f1b2020-03-09 13:33:25 -06001495 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07001496 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
1497 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07001498 // Validate Image Layout transitions
1499 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
1500 const auto &barrier = pImageMemoryBarriers[index];
1501 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
1502 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
1503 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06001504 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07001505 if (hazard.hazard) {
1506 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06001507 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
1508 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s", string_SyncHazard(hazard.hazard),
1509 index, report_data->FormatHandle(barrier.image).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07001510 }
1511 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001512
1513 return skip;
1514}
1515
1516void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1517 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1518 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1519 uint32_t bufferMemoryBarrierCount,
1520 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1521 uint32_t imageMemoryBarrierCount,
1522 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001523 auto *cb_access_context = GetAccessContext(commandBuffer);
1524 assert(cb_access_context);
1525 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06001526 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001527 auto access_context = cb_access_context->GetCurrentAccessContext();
1528 assert(access_context);
1529 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06001530
John Zulauf3d84f1b2020-03-09 13:33:25 -06001531 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07001532 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001533 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07001534 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
1535 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
1536 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001537 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
1538 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06001539 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001540 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001541
1542 // 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 -06001543 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07001544 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06001545}
1546
1547void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
1548 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
1549 // The state tracker sets up the device state
1550 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
1551
John Zulauf5f13a792020-03-10 07:31:21 -06001552 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
1553 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06001554 // TODO: Find a good way to do this hooklessly.
1555 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
1556 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
1557 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
1558
John Zulaufd1f85d42020-04-15 12:23:15 -06001559 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
1560 sync_device_state->ResetCommandBufferCallback(command_buffer);
1561 });
1562 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
1563 sync_device_state->FreeCommandBufferCallback(command_buffer);
1564 });
John Zulauf9cb530d2019-09-30 14:14:10 -06001565}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001566
John Zulauf355e49b2020-04-24 15:11:15 -06001567bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1568 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
1569 bool skip = false;
1570 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
1571 auto cb_context = GetAccessContext(commandBuffer);
1572
1573 if (rp_state && cb_context) {
1574 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
1575 }
1576
1577 return skip;
1578}
1579
1580bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1581 VkSubpassContents contents) const {
1582 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
1583 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
1584 subpass_begin_info.contents = contents;
1585 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
1586 return skip;
1587}
1588
1589bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1590 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
1591 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1592 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
1593 return skip;
1594}
1595
1596bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1597 const VkRenderPassBeginInfo *pRenderPassBegin,
1598 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
1599 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1600 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
1601 return skip;
1602}
1603
John Zulauf3d84f1b2020-03-09 13:33:25 -06001604void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
1605 VkResult result) {
1606 // The state tracker sets up the command buffer state
1607 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
1608
1609 // Create/initialize the structure that trackers accesses at the command buffer scope.
1610 auto cb_access_context = GetAccessContext(commandBuffer);
1611 assert(cb_access_context);
1612 cb_access_context->Reset();
1613}
1614
1615void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06001616 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001617 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06001618 if (cb_context) {
1619 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06001620 }
1621}
1622
1623void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1624 VkSubpassContents contents) {
1625 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
1626 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
1627 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06001628 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001629}
1630
1631void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1632 const VkSubpassBeginInfo *pSubpassBeginInfo) {
1633 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001634 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001635}
1636
1637void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1638 const VkRenderPassBeginInfo *pRenderPassBegin,
1639 const VkSubpassBeginInfo *pSubpassBeginInfo) {
1640 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001641 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
1642}
1643
1644bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1645 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
1646 bool skip = false;
1647
1648 auto cb_context = GetAccessContext(commandBuffer);
1649 assert(cb_context);
1650 auto cb_state = cb_context->GetCommandBufferState();
1651 if (!cb_state) return skip;
1652
1653 auto rp_state = cb_state->activeRenderPass;
1654 if (!rp_state) return skip;
1655
1656 skip |= cb_context->ValidateNextSubpass(func_name);
1657
1658 return skip;
1659}
1660
1661bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
1662 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
1663 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
1664 subpass_begin_info.contents = contents;
1665 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
1666 return skip;
1667}
1668
1669bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1670 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
1671 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
1672 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
1673 return skip;
1674}
1675
1676bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
1677 const VkSubpassEndInfo *pSubpassEndInfo) const {
1678 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
1679 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
1680 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001681}
1682
1683void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06001684 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001685 auto cb_context = GetAccessContext(commandBuffer);
1686 assert(cb_context);
1687 auto cb_state = cb_context->GetCommandBufferState();
1688 if (!cb_state) return;
1689
1690 auto rp_state = cb_state->activeRenderPass;
1691 if (!rp_state) return;
1692
John Zulauf355e49b2020-04-24 15:11:15 -06001693 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06001694}
1695
1696void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
1697 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
1698 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
1699 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06001700 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001701}
1702
1703void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
1704 const VkSubpassEndInfo *pSubpassEndInfo) {
1705 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001706 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001707}
1708
1709void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
1710 const VkSubpassEndInfo *pSubpassEndInfo) {
1711 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001712 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001713}
1714
John Zulauf355e49b2020-04-24 15:11:15 -06001715bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
1716 const char *func_name) const {
1717 bool skip = false;
1718
1719 auto cb_context = GetAccessContext(commandBuffer);
1720 assert(cb_context);
1721 auto cb_state = cb_context->GetCommandBufferState();
1722 if (!cb_state) return skip;
1723
1724 auto rp_state = cb_state->activeRenderPass;
1725 if (!rp_state) return skip;
1726
1727 skip |= cb_context->ValidateEndRenderpass(func_name);
1728 return skip;
1729}
1730
1731bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
1732 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
1733 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
1734 return skip;
1735}
1736
1737bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
1738 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
1739 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
1740 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
1741 return skip;
1742}
1743
1744bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
1745 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
1746 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
1747 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
1748 return skip;
1749}
1750
1751void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
1752 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06001753 // Resolve the all subpass contexts to the command buffer contexts
1754 auto cb_context = GetAccessContext(commandBuffer);
1755 assert(cb_context);
1756 auto cb_state = cb_context->GetCommandBufferState();
1757 if (!cb_state) return;
1758
1759 const auto *rp_state = cb_state->activeRenderPass;
1760 if (!rp_state) return;
1761
John Zulauf355e49b2020-04-24 15:11:15 -06001762 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06001763}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001764
1765void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
1766 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06001767 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001768}
1769
1770void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
1771 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001772 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001773}
1774
1775void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
1776 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06001777 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001778}
locke-lunarga19c71d2020-03-02 18:17:04 -07001779
1780bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
1781 VkImageLayout dstImageLayout, uint32_t regionCount,
1782 const VkBufferImageCopy *pRegions) const {
1783 bool skip = false;
1784 const auto *cb_access_context = GetAccessContext(commandBuffer);
1785 assert(cb_access_context);
1786 if (!cb_access_context) return skip;
1787
1788 const auto *context = cb_access_context->GetCurrentAccessContext();
1789 assert(context);
1790 if (!context) return skip;
1791
1792 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07001793 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
1794
1795 for (uint32_t region = 0; region < regionCount; region++) {
1796 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06001797 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06001798 ResourceAccessRange src_range =
1799 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06001800 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07001801 if (hazard.hazard) {
1802 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06001803 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
1804 "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07001805 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region);
1806 }
1807 }
1808 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001809 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07001810 copy_region.imageOffset, copy_region.imageExtent);
1811 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001812 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
1813 "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07001814 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region);
1815 }
1816 if (skip) break;
1817 }
1818 if (skip) break;
1819 }
1820 return skip;
1821}
1822
1823void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
1824 VkImageLayout dstImageLayout, uint32_t regionCount,
1825 const VkBufferImageCopy *pRegions) {
1826 auto *cb_access_context = GetAccessContext(commandBuffer);
1827 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001828 const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07001829 auto *context = cb_access_context->GetCurrentAccessContext();
1830 assert(context);
1831
1832 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06001833 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07001834
1835 for (uint32_t region = 0; region < regionCount; region++) {
1836 const auto &copy_region = pRegions[region];
1837 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06001838 ResourceAccessRange src_range =
1839 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06001840 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001841 }
1842 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001843 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06001844 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001845 }
1846 }
1847}
1848
1849bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
1850 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
1851 const VkBufferImageCopy *pRegions) const {
1852 bool skip = false;
1853 const auto *cb_access_context = GetAccessContext(commandBuffer);
1854 assert(cb_access_context);
1855 if (!cb_access_context) return skip;
1856
1857 const auto *context = cb_access_context->GetCurrentAccessContext();
1858 assert(context);
1859 if (!context) return skip;
1860
1861 const auto *src_image = Get<IMAGE_STATE>(srcImage);
1862 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
1863 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
1864 for (uint32_t region = 0; region < regionCount; region++) {
1865 const auto &copy_region = pRegions[region];
1866 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001867 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07001868 copy_region.imageOffset, copy_region.imageExtent);
1869 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001870 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
1871 "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07001872 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region);
1873 }
1874 }
1875 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06001876 ResourceAccessRange dst_range =
1877 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06001878 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07001879 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001880 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
1881 "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07001882 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region);
1883 }
1884 }
1885 if (skip) break;
1886 }
1887 return skip;
1888}
1889
1890void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1891 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
1892 auto *cb_access_context = GetAccessContext(commandBuffer);
1893 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001894 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER);
locke-lunarga19c71d2020-03-02 18:17:04 -07001895 auto *context = cb_access_context->GetCurrentAccessContext();
1896 assert(context);
1897
1898 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07001899 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
1900 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 -06001901 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07001902
1903 for (uint32_t region = 0; region < regionCount; region++) {
1904 const auto &copy_region = pRegions[region];
1905 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001906 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06001907 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001908 }
1909 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06001910 ResourceAccessRange dst_range =
1911 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06001912 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001913 }
1914 }
1915}
1916
1917bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1918 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
1919 const VkImageBlit *pRegions, VkFilter filter) const {
1920 bool skip = false;
1921 const auto *cb_access_context = GetAccessContext(commandBuffer);
1922 assert(cb_access_context);
1923 if (!cb_access_context) return skip;
1924
1925 const auto *context = cb_access_context->GetCurrentAccessContext();
1926 assert(context);
1927 if (!context) return skip;
1928
1929 const auto *src_image = Get<IMAGE_STATE>(srcImage);
1930 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
1931
1932 for (uint32_t region = 0; region < regionCount; region++) {
1933 const auto &blit_region = pRegions[region];
1934 if (src_image) {
1935 VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x),
1936 static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y),
1937 static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06001938 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07001939 blit_region.srcOffsets[0], extent);
1940 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001941 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
1942 "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1943 report_data->FormatHandle(srcImage).c_str(), region);
locke-lunarga19c71d2020-03-02 18:17:04 -07001944 }
1945 }
1946
1947 if (dst_image) {
1948 VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x),
1949 static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y),
1950 static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06001951 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07001952 blit_region.dstOffsets[0], extent);
1953 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001954 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
1955 "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1956 report_data->FormatHandle(dstImage).c_str(), region);
locke-lunarga19c71d2020-03-02 18:17:04 -07001957 }
1958 if (skip) break;
1959 }
1960 }
1961
1962 return skip;
1963}
1964
1965void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1966 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
1967 const VkImageBlit *pRegions, VkFilter filter) {
1968 auto *cb_access_context = GetAccessContext(commandBuffer);
1969 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001970 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07001971 auto *context = cb_access_context->GetCurrentAccessContext();
1972 assert(context);
1973
1974 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07001975 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07001976
1977 for (uint32_t region = 0; region < regionCount; region++) {
1978 const auto &blit_region = pRegions[region];
1979 if (src_image) {
1980 VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x),
1981 static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y),
1982 static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06001983 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06001984 blit_region.srcOffsets[0], extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001985 }
1986 if (dst_image) {
1987 VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x),
1988 static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y),
1989 static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06001990 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06001991 blit_region.dstOffsets[0], extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001992 }
1993 }
1994}