John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1 | /* 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-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 22 | #include <memory> |
| 23 | #include <bitset> |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 24 | #include "synchronization_validation.h" |
| 25 | |
| 26 | static const char *string_SyncHazardVUID(SyncHazard hazard) { |
| 27 | switch (hazard) { |
| 28 | case SyncHazard::NONE: |
John Zulauf | 2f952d2 | 2020-02-10 11:34:51 -0700 | [diff] [blame] | 29 | return "SYNC-HAZARD-NONE"; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 30 | 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 Zulauf | 2f952d2 | 2020-02-10 11:34:51 -0700 | [diff] [blame] | 40 | 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 Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 49 | default: |
| 50 | assert(0); |
| 51 | } |
| 52 | return "SYNC-HAZARD-INVALID"; |
| 53 | } |
| 54 | |
| 55 | static 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 Zulauf | 2f952d2 | 2020-02-10 11:34:51 -0700 | [diff] [blame] | 69 | 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 Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 78 | default: |
| 79 | assert(0); |
| 80 | } |
| 81 | return "INVALID HAZARD"; |
| 82 | } |
| 83 | |
locke-lunarg | 3c03800 | 2020-04-30 23:08:08 -0600 | [diff] [blame] | 84 | inline 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 Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 91 | template <typename T> |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 92 | static ResourceAccessRange MakeRange(const T &has_offset_and_size) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 93 | return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size)); |
| 94 | } |
| 95 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 96 | static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); } |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 97 | |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 98 | // Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension |
| 99 | VkPipelineStageFlags 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 Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 116 | VkPipelineStageFlags 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 | |
| 131 | VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) { |
| 132 | return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages); |
| 133 | } |
| 134 | |
| 135 | VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) { |
| 136 | return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages); |
| 137 | } |
| 138 | |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 139 | static const ResourceAccessRange full_range(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max()); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 140 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 141 | // Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue |
| 142 | const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = { |
| 143 | AccessContext::AddressType::kLinearAddress, AccessContext::AddressType::kIdealizedAddress}; |
| 144 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 145 | AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags, |
| 146 | const std::vector<SubpassDependencyGraphNode> &dependencies, |
| 147 | const std::vector<AccessContext> &contexts, AccessContext *external_context) { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 148 | Reset(); |
| 149 | const auto &subpass_dep = dependencies[subpass]; |
| 150 | prev_.reserve(subpass_dep.prev.size()); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 151 | prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 152 | for (const auto &prev_dep : subpass_dep.prev) { |
| 153 | assert(prev_dep.dependency); |
| 154 | const auto dep = *prev_dep.dependency; |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 155 | prev_.emplace_back(const_cast<AccessContext *>(&contexts[dep.srcSubpass]), queue_flags, dep); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 156 | prev_by_subpass_[dep.srcSubpass] = &prev_.back(); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 157 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 158 | |
| 159 | async_.reserve(subpass_dep.async.size()); |
| 160 | for (const auto async_subpass : subpass_dep.async) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 161 | async_.emplace_back(const_cast<AccessContext *>(&contexts[async_subpass])); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 162 | } |
John Zulauf | e5da6e5 | 2020-03-18 15:32:18 -0600 | [diff] [blame] | 163 | 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 Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 172 | } |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 173 | } |
| 174 | |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 175 | template <typename Detector> |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 176 | HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector, |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 177 | const ResourceAccessRange &range) const { |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 178 | ResourceAccessRangeMap descent_map; |
| 179 | ResourceAccessState default_state; // When present, PreviousAccess will "infill" |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 180 | ResolvePreviousAccess(type, range, &descent_map, &default_state); |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 181 | |
| 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 Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 189 | // 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) |
| 191 | template <typename Detector> |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 192 | HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range, |
| 193 | DetectOptions options) const { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 194 | HazardResult hazard; |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 195 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 196 | 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 Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 203 | } |
| 204 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 205 | 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 Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 214 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 215 | // 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 Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 218 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 219 | // 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 Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 229 | hazard = DetectPreviousHazard(type, detector, gap); |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 230 | if (hazard.hazard) return hazard; |
| 231 | } |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 232 | } else { |
| 233 | hazard = DetectPreviousHazard(type, detector, range); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 234 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | return hazard; |
| 238 | } |
| 239 | |
| 240 | // A non recursive range walker for the asynchronous contexts (those we have no barriers with) |
| 241 | template <typename Detector> |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 242 | HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 243 | auto &accesses = GetAccessStateMap(type); |
| 244 | const auto from = accesses.lower_bound(range); |
| 245 | const auto to = accesses.upper_bound(range); |
| 246 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 247 | HazardResult hazard; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 248 | for (auto pos = from; pos != to && !hazard.hazard; ++pos) { |
| 249 | hazard = detector.DetectAsync(pos); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 250 | } |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 251 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 252 | return hazard; |
| 253 | } |
| 254 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 255 | // Returns the last resolved entry |
| 256 | static 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 | |
| 275 | void 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 Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 280 | if (current->pos_B->valid) { |
| 281 | const auto &src_pos = current->pos_B->lower_bound; |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 282 | auto access = src_pos->second; |
| 283 | if (barrier) { |
| 284 | access.ApplyBarrier(*barrier); |
| 285 | } |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 286 | if (current->pos_A->valid) { |
| 287 | current.trim_A(); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 288 | current->pos_A->lower_bound->second.Resolve(access); |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 289 | } else { |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 290 | 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 Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 292 | } |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 293 | } else { |
| 294 | // we have to descend to fill this gap |
| 295 | if (recur_to_infill) { |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 296 | 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 Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 324 | } |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 325 | } |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 326 | ++current; |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 327 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 328 | } |
| 329 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 330 | void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map, |
| 331 | const ResourceAccessState *infill_state) const { |
John Zulauf | e5da6e5 | 2020-03-18 15:32:18 -0600 | [diff] [blame] | 332 | if ((prev_.size() == 0) && (src_external_.context == nullptr)) { |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 333 | 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 Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 339 | prev_dep.context->ResolveAccessRange(type, range, &prev_dep.barrier, descent_map, infill_state); |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 340 | } |
| 341 | |
John Zulauf | e5da6e5 | 2020-03-18 15:32:18 -0600 | [diff] [blame] | 342 | if (src_external_.context) { |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 343 | src_external_.context->ResolveAccessRange(type, range, &src_external_.barrier, descent_map, infill_state); |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 344 | } |
| 345 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 346 | } |
| 347 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 348 | AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) { |
locke-lunarg | 3f6978b | 2020-04-16 16:51:35 -0600 | [diff] [blame] | 349 | return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) { |
| 353 | return bindable.binding.offset + bindable.binding.mem_state->fake_base_address; |
| 354 | } |
| 355 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 356 | static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; } |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 357 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 358 | void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg, |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 359 | AddressType address_type, ResourceAccessRangeMap *descent_map, |
| 360 | const ResourceAccessState *infill_state) const { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 361 | if (!SimpleBinding(image_state)) return; |
| 362 | |
John Zulauf | 62f1059 | 2020-04-03 12:20:02 -0600 | [diff] [blame] | 363 | auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg); |
locke-lunarg | ae26eac | 2020-04-16 15:29:05 -0600 | [diff] [blame] | 364 | subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0}, |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 365 | image_state.createInfo.extent); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 366 | const auto base_address = ResourceBaseAddress(image_state); |
John Zulauf | 62f1059 | 2020-04-03 12:20:02 -0600 | [diff] [blame] | 367 | for (; range_gen->non_empty(); ++range_gen) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 368 | ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state); |
John Zulauf | 62f1059 | 2020-04-03 12:20:02 -0600 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 372 | static bool ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state, |
| 373 | const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name, |
| 374 | uint32_t subpass, const AccessContext &context) { |
| 375 | bool skip = false; |
| 376 | const auto &transitions = rp_state.subpass_transitions[subpass]; |
| 377 | for (const auto &transition : transitions) { |
| 378 | auto hazard = context.DetectSubpassTransitionHazard(transition, attachment_views); |
| 379 | if (hazard.hazard) { |
| 380 | skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard), |
| 381 | "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " image layout transition.", |
| 382 | func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment); |
| 383 | } |
| 384 | } |
| 385 | return skip; |
| 386 | } |
| 387 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 388 | class HazardDetector { |
| 389 | SyncStageAccessIndex usage_index_; |
| 390 | |
| 391 | public: |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 392 | HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 393 | HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const { |
| 394 | return pos->second.DetectAsyncHazard(usage_index_); |
| 395 | } |
| 396 | HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {} |
| 397 | }; |
| 398 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 399 | HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index, |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 400 | const ResourceAccessRange &range) const { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 401 | HazardDetector detector(usage_index); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 402 | return DetectHazard(type, detector, range, DetectOptions::kDetectAll); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 403 | } |
| 404 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 405 | HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index, |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 406 | const ResourceAccessRange &range) const { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 407 | if (!SimpleBinding(buffer)) return HazardResult(); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 408 | return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer)); |
John Zulauf | e5da6e5 | 2020-03-18 15:32:18 -0600 | [diff] [blame] | 409 | } |
| 410 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 411 | HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, |
| 412 | const VkImageSubresourceLayers &subresource, const VkOffset3D &offset, |
| 413 | const VkExtent3D &extent) const { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 414 | if (!SimpleBinding(image)) return HazardResult(); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 415 | // TODO: replace the encoder/generator with offset3D/extent3D aware versions |
| 416 | VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer, |
| 417 | subresource.layerCount}; |
locke-lunarg | ae26eac | 2020-04-16 15:29:05 -0600 | [diff] [blame] | 418 | subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 419 | const auto address_type = ImageAddressType(image); |
| 420 | const auto base_address = ResourceBaseAddress(image); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 421 | for (; range_gen->non_empty(); ++range_gen) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 422 | HazardResult hazard = DetectHazard(address_type, current_usage, (*range_gen + base_address)); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 423 | if (hazard.hazard) return hazard; |
| 424 | } |
| 425 | return HazardResult(); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 426 | } |
| 427 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 428 | class BarrierHazardDetector { |
| 429 | public: |
| 430 | BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope, |
| 431 | SyncStageAccessFlags src_access_scope) |
| 432 | : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {} |
| 433 | |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 434 | HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { |
| 435 | return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_); |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 436 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 437 | HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const { |
| 438 | // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite |
| 439 | return pos->second.DetectAsyncHazard(usage_index_); |
| 440 | } |
| 441 | |
| 442 | private: |
| 443 | SyncStageAccessIndex usage_index_; |
| 444 | VkPipelineStageFlags src_exec_scope_; |
| 445 | SyncStageAccessFlags src_access_scope_; |
| 446 | }; |
| 447 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 448 | HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage, |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 449 | VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope, |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 450 | const ResourceAccessRange &range, DetectOptions options) const { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 451 | BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 452 | return DetectHazard(type, detector, range, DetectOptions::kDetectAll); |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 453 | } |
| 454 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 455 | HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope, |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 456 | SyncStageAccessFlags src_access_scope, |
| 457 | const VkImageSubresourceRange &subresource_range, |
| 458 | DetectOptions options) const { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 459 | if (!SimpleBinding(image)) return HazardResult(); |
locke-lunarg | ae26eac | 2020-04-16 15:29:05 -0600 | [diff] [blame] | 460 | subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0}, |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 461 | image.createInfo.extent); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 462 | const auto address_type = ImageAddressType(image); |
| 463 | const auto base_address = ResourceBaseAddress(image); |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 464 | for (; range_gen->non_empty(); ++range_gen) { |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 465 | HazardResult hazard = DetectBarrierHazard(address_type, SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, |
| 466 | src_access_scope, (*range_gen + base_address), options); |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 467 | if (hazard.hazard) return hazard; |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 468 | } |
| 469 | return HazardResult(); |
| 470 | } |
| 471 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 472 | HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope, |
| 473 | SyncStageAccessFlags src_stage_accesses, |
| 474 | const VkImageMemoryBarrier &barrier) const { |
| 475 | auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange); |
| 476 | const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask); |
| 477 | return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll); |
| 478 | } |
| 479 | |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 480 | template <typename Flags, typename Map> |
| 481 | SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) { |
| 482 | SyncStageAccessFlags scope = 0; |
| 483 | for (const auto &bit_scope : map) { |
| 484 | if (flag_mask < bit_scope.first) break; |
| 485 | |
| 486 | if (flag_mask & bit_scope.first) { |
| 487 | scope |= bit_scope.second; |
| 488 | } |
| 489 | } |
| 490 | return scope; |
| 491 | } |
| 492 | |
| 493 | SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) { |
| 494 | return AccessScopeImpl(stages, syncStageAccessMaskByStageBit); |
| 495 | } |
| 496 | |
| 497 | SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) { |
| 498 | return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit); |
| 499 | } |
| 500 | |
| 501 | // Getting from stage mask and access mask to stage/acess masks is something we need to be good at... |
| 502 | SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) { |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 503 | // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables |
| 504 | // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections |
| 505 | // of the union of all stage/access types for all the stages and the same unions for the access mask... |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 506 | return AccessScopeByStage(stages) & AccessScopeByAccess(accesses); |
| 507 | } |
| 508 | |
| 509 | template <typename Action> |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 510 | void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) { |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 511 | // TODO -- region/mem-range accuracte update |
| 512 | auto pos = accesses->lower_bound(range); |
| 513 | if (pos == accesses->end() || !pos->first.intersects(range)) { |
| 514 | // The range is empty, fill it with a default value. |
| 515 | pos = action.Infill(accesses, pos, range); |
| 516 | } else if (range.begin < pos->first.begin) { |
| 517 | // Leading empty space, infill |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 518 | pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin)); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 519 | } else if (pos->first.begin < range.begin) { |
| 520 | // Trim the beginning if needed |
| 521 | pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both()); |
| 522 | ++pos; |
| 523 | } |
| 524 | |
| 525 | const auto the_end = accesses->end(); |
| 526 | while ((pos != the_end) && pos->first.intersects(range)) { |
| 527 | if (pos->first.end > range.end) { |
| 528 | pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both()); |
| 529 | } |
| 530 | |
| 531 | pos = action(accesses, pos); |
| 532 | if (pos == the_end) break; |
| 533 | |
| 534 | auto next = pos; |
| 535 | ++next; |
| 536 | if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) { |
| 537 | // Need to infill if next is disjoint |
| 538 | VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 539 | ResourceAccessRange new_range(pos->first.end, limit); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 540 | next = action.Infill(accesses, next, new_range); |
| 541 | } |
| 542 | pos = next; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | struct UpdateMemoryAccessStateFunctor { |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 547 | using Iterator = ResourceAccessRangeMap::iterator; |
| 548 | Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 549 | // this is only called on gaps, and never returns a gap. |
| 550 | ResourceAccessState default_state; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 551 | context.ResolvePreviousAccess(type, range, accesses, &default_state); |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 552 | return accesses->lower_bound(range); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 553 | } |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 554 | |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 555 | Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const { |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 556 | auto &access_state = pos->second; |
| 557 | access_state.Update(usage, tag); |
| 558 | return pos; |
| 559 | } |
| 560 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 561 | UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_, |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 562 | const ResourceUsageTag &tag_) |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 563 | : type(type_), context(context_), usage(usage_), tag(tag_) {} |
| 564 | const AccessContext::AddressType type; |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 565 | const AccessContext &context; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 566 | const SyncStageAccessIndex usage; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 567 | const ResourceUsageTag &tag; |
| 568 | }; |
| 569 | |
| 570 | struct ApplyMemoryAccessBarrierFunctor { |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 571 | using Iterator = ResourceAccessRangeMap::iterator; |
| 572 | inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; } |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 573 | |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 574 | Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const { |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 575 | auto &access_state = pos->second; |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 576 | access_state.ApplyMemoryAccessBarrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 577 | return pos; |
| 578 | } |
| 579 | |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 580 | ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_, |
| 581 | VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_) |
| 582 | : src_exec_scope(src_exec_scope_), |
| 583 | src_access_scope(src_access_scope_), |
| 584 | dst_exec_scope(dst_exec_scope_), |
| 585 | dst_access_scope(dst_access_scope_) {} |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 586 | |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 587 | VkPipelineStageFlags src_exec_scope; |
| 588 | SyncStageAccessFlags src_access_scope; |
| 589 | VkPipelineStageFlags dst_exec_scope; |
| 590 | SyncStageAccessFlags dst_access_scope; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 591 | }; |
| 592 | |
| 593 | struct ApplyGlobalBarrierFunctor { |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 594 | using Iterator = ResourceAccessRangeMap::iterator; |
| 595 | inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; } |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 596 | |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 597 | Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const { |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 598 | auto &access_state = pos->second; |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 599 | access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 600 | |
| 601 | for (const auto &functor : barrier_functor) { |
| 602 | functor(accesses, pos); |
| 603 | } |
| 604 | return pos; |
| 605 | } |
| 606 | |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 607 | ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope, |
| 608 | SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses, |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 609 | uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers) |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 610 | : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) { |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 611 | // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier... |
| 612 | barrier_functor.reserve(memoryBarrierCount); |
| 613 | for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) { |
| 614 | const auto &barrier = pMemoryBarriers[barrier_index]; |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 615 | barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask), |
| 616 | dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask)); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 620 | const VkPipelineStageFlags src_exec_scope; |
| 621 | const VkPipelineStageFlags dst_exec_scope; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 622 | std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor; |
| 623 | }; |
| 624 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 625 | void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range, |
| 626 | const ResourceUsageTag &tag) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 627 | UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag); |
| 628 | UpdateMemoryAccessState(&GetAccessStateMap(type), range, action); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 629 | } |
| 630 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 631 | void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 632 | const ResourceAccessRange &range, const ResourceUsageTag &tag) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 633 | if (!SimpleBinding(buffer)) return; |
| 634 | const auto base_address = ResourceBaseAddress(buffer); |
| 635 | UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag); |
| 636 | } |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 637 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 638 | void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 639 | const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset, |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 640 | const VkExtent3D &extent, const ResourceUsageTag &tag) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 641 | if (!SimpleBinding(image)) return; |
locke-lunarg | ae26eac | 2020-04-16 15:29:05 -0600 | [diff] [blame] | 642 | subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 643 | const auto address_type = ImageAddressType(image); |
| 644 | const auto base_address = ResourceBaseAddress(image); |
| 645 | UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag); |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 646 | for (; range_gen->non_empty(); ++range_gen) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 647 | UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action); |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 648 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 649 | } |
| 650 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 651 | void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, |
| 652 | const VkImageSubresourceLayers &subresource, const VkOffset3D &offset, |
| 653 | const VkExtent3D &extent, const ResourceUsageTag &tag) { |
| 654 | // TODO: replace the encoder/generator with offset3D aware versions |
| 655 | VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer, |
| 656 | subresource.layerCount}; |
| 657 | UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag); |
| 658 | } |
| 659 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 660 | template <typename Action> |
| 661 | void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 662 | if (!SimpleBinding(buffer)) return; |
| 663 | const auto base_address = ResourceBaseAddress(buffer); |
| 664 | UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | template <typename Action> |
| 668 | void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range, |
| 669 | const Action action) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 670 | if (!SimpleBinding(image)) return; |
| 671 | const auto address_type = ImageAddressType(image); |
| 672 | auto *accesses = &GetAccessStateMap(address_type); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 673 | |
locke-lunarg | ae26eac | 2020-04-16 15:29:05 -0600 | [diff] [blame] | 674 | subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0}, |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 675 | image.createInfo.extent); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 676 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 677 | const auto base_address = ResourceBaseAddress(image); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 678 | for (; range_gen->non_empty(); ++range_gen) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 679 | UpdateMemoryAccessState(accesses, (*range_gen + base_address), action); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
| 683 | template <typename Action> |
| 684 | void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) { |
| 685 | // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses) |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 686 | for (const auto address_type : kAddressTypes) { |
| 687 | UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
| 691 | void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 692 | for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) { |
| 693 | auto &context = contexts[subpass_index]; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 694 | for (const auto address_type : kAddressTypes) { |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 695 | context.ResolveAccessRange(address_type, full_range, &context.GetDstExternalTrackBack().barrier, |
| 696 | &GetAccessStateMap(address_type), nullptr, false); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 701 | void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope, |
| 702 | SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope, |
| 703 | SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) { |
| 704 | const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope); |
| 705 | UpdateMemoryAccess(image, subresource_range, barrier_action); |
| 706 | } |
| 707 | |
| 708 | // TODO: Plumb offset/extent throughout the image call stacks, with default injector overloads to preserved backwards compatiblity |
| 709 | // as needed |
| 710 | void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope, |
| 711 | SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope, |
| 712 | SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range, |
| 713 | bool layout_transition, const ResourceUsageTag &tag) { |
| 714 | if (layout_transition) { |
| 715 | UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent, |
| 716 | tag); |
| 717 | ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope, |
| 718 | subresource_range); |
John Zulauf | c920122 | 2020-05-13 15:13:03 -0600 | [diff] [blame^] | 719 | } else { |
| 720 | ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 721 | } |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier, |
| 725 | const VkImageSubresourceRange &subresource_range, bool layout_transition, |
| 726 | const ResourceUsageTag &tag) { |
| 727 | ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope, |
| 728 | subresource_range, layout_transition, tag); |
| 729 | } |
| 730 | |
| 731 | // Suitable only for *subpass* access contexts |
| 732 | HazardResult AccessContext::DetectSubpassTransitionHazard(const RENDER_PASS_STATE::AttachmentTransition &transition, |
| 733 | const std::vector<const IMAGE_VIEW_STATE *> &attachments) const { |
| 734 | const auto *attach_view = attachments[transition.attachment]; |
| 735 | if (!attach_view) return HazardResult(); |
| 736 | const auto image_state = attach_view->image_state.get(); |
| 737 | if (!image_state) return HazardResult(); |
| 738 | |
| 739 | const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass); |
| 740 | // We should never ask for a transition from a context we don't have |
| 741 | assert(track_back); |
| 742 | assert(track_back->context); |
| 743 | |
| 744 | // Do the detection against the specific prior context independent of other contexts. (Synchronous only) |
| 745 | auto hazard = track_back->context->DetectImageBarrierHazard(*image_state, track_back->barrier.src_exec_scope, |
| 746 | track_back->barrier.src_access_scope, |
| 747 | attach_view->normalized_subresource_range, kDetectPrevious); |
| 748 | if (!hazard.hazard) { |
| 749 | // The Async hazard check is against the current context's async set. |
| 750 | hazard = DetectImageBarrierHazard(*image_state, track_back->barrier.src_exec_scope, track_back->barrier.src_access_scope, |
| 751 | attach_view->normalized_subresource_range, kDetectAsync); |
| 752 | } |
| 753 | return hazard; |
| 754 | } |
| 755 | |
| 756 | // Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer |
| 757 | bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state, |
| 758 | |
| 759 | const VkRenderPassBeginInfo *pRenderPassBegin, |
| 760 | const VkSubpassBeginInfoKHR *pSubpassBeginInfo, |
| 761 | const char *func_name) const { |
| 762 | // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we |
| 763 | bool skip = false; |
| 764 | uint32_t subpass = 0; |
| 765 | const auto &transitions = rp_state.subpass_transitions[subpass]; |
| 766 | if (transitions.size()) { |
| 767 | const std::vector<AccessContext> empty_context_vector; |
| 768 | // Create context we can use to validate against... |
| 769 | AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector, |
| 770 | const_cast<AccessContext *>(&cb_access_context_)); |
| 771 | |
| 772 | assert(pRenderPassBegin); |
| 773 | if (nullptr == pRenderPassBegin) return skip; |
| 774 | |
| 775 | const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer); |
| 776 | assert(fb_state); |
| 777 | if (nullptr == fb_state) return skip; |
| 778 | |
| 779 | // Create a limited array of views (which we'll need to toss |
| 780 | std::vector<const IMAGE_VIEW_STATE *> views; |
| 781 | const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state); |
| 782 | const auto attachment_count = count_attachment.first; |
| 783 | const auto *attachments = count_attachment.second; |
| 784 | views.resize(attachment_count, nullptr); |
| 785 | for (const auto &transition : transitions) { |
| 786 | assert(transition.attachment < attachment_count); |
| 787 | views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]); |
| 788 | } |
| 789 | |
| 790 | skip |= ValidateLayoutTransitions(*sync_state_, rp_state, views, func_name, 0, temp_context); |
| 791 | } |
| 792 | return skip; |
| 793 | } |
| 794 | |
| 795 | bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const { |
| 796 | // TODO: Things to add here. |
| 797 | // Validate Preserve/Resolve attachments |
| 798 | bool skip = false; |
| 799 | skip |= current_renderpass_context_->ValidateNextSubpassLayoutTransitions(*sync_state_, func_name); |
| 800 | |
| 801 | return skip; |
| 802 | } |
| 803 | |
| 804 | bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const { |
| 805 | // TODO: Things to add here. |
| 806 | // Validate Preserve/Resolve attachments |
| 807 | bool skip = false; |
| 808 | skip |= current_renderpass_context_->ValidateFinalSubpassLayoutTransitions(*sync_state_, func_name); |
| 809 | |
| 810 | return skip; |
| 811 | } |
| 812 | |
| 813 | void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) { |
| 814 | assert(sync_state_); |
| 815 | if (!cb_state_) return; |
| 816 | |
| 817 | // Create an access context the current renderpass. |
| 818 | render_pass_contexts_.emplace_back(&cb_access_context_); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 819 | current_renderpass_context_ = &render_pass_contexts_.back(); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 820 | current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, queue_flags_, tag); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 821 | current_context_ = ¤t_renderpass_context_->CurrentContext(); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 822 | } |
| 823 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 824 | void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 825 | assert(current_renderpass_context_); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 826 | current_renderpass_context_->RecordNextSubpass(tag); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 827 | current_context_ = ¤t_renderpass_context_->CurrentContext(); |
| 828 | } |
| 829 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 830 | void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) { |
| 831 | // TODO: Add layout load/store/resolve access (here or in RenderPassContext) |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 832 | assert(current_renderpass_context_); |
| 833 | if (!current_renderpass_context_) return; |
| 834 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 835 | current_renderpass_context_->RecordEndRenderPass(tag); |
| 836 | current_context_ = &cb_access_context_; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 837 | current_renderpass_context_ = nullptr; |
| 838 | } |
| 839 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 840 | bool RenderPassAccessContext::ValidateNextSubpassLayoutTransitions(const SyncValidator &sync_state, const char *func_name) const { |
| 841 | bool skip = false; |
| 842 | const auto next_subpass = current_subpass_ + 1; |
| 843 | skip |= ValidateLayoutTransitions(sync_state, *rp_state_, attachment_views_, func_name, next_subpass, |
| 844 | subpass_contexts_[next_subpass]); |
| 845 | return skip; |
| 846 | } |
| 847 | |
| 848 | bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const char *func_name) const { |
| 849 | bool skip = false; |
| 850 | |
| 851 | // Validate the "finalLayout" transitions to external |
| 852 | // Get them from where there we're hidding in the extra entry. |
| 853 | const auto &final_transitions = rp_state_->subpass_transitions.back(); |
| 854 | for (const auto &transition : final_transitions) { |
| 855 | const auto &attach_view = attachment_views_[transition.attachment]; |
| 856 | const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack(); |
| 857 | assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly |
| 858 | auto hazard = trackback.context->DetectImageBarrierHazard( |
| 859 | *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope, |
| 860 | attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious); |
| 861 | if (hazard.hazard) { |
| 862 | skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard), |
| 863 | "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32 |
| 864 | " final image layout transition.", |
| 865 | func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment); |
| 866 | } |
| 867 | } |
| 868 | return skip; |
| 869 | } |
| 870 | |
| 871 | void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) { |
| 872 | // Add layout transitions... |
| 873 | const auto &transitions = rp_state_->subpass_transitions[current_subpass_]; |
| 874 | auto &subpass_context = subpass_contexts_[current_subpass_]; |
John Zulauf | c920122 | 2020-05-13 15:13:03 -0600 | [diff] [blame^] | 875 | std::set<const IMAGE_VIEW_STATE *> view_seen; |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 876 | for (const auto &transition : transitions) { |
| 877 | const auto attachment_view = attachment_views_[transition.attachment]; |
| 878 | if (!attachment_view) continue; |
| 879 | const auto image = attachment_view->image_state.get(); |
| 880 | if (!image) continue; |
| 881 | |
| 882 | const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass); |
John Zulauf | c920122 | 2020-05-13 15:13:03 -0600 | [diff] [blame^] | 883 | auto insert_pair = view_seen.insert(attachment_view); |
| 884 | if (insert_pair.second) { |
| 885 | // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion. |
| 886 | subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag); |
| 887 | |
| 888 | } else { |
| 889 | // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition |
| 890 | // would clear out the prior barrier flags, so apply this as a *non* transition barrier |
| 891 | auto barrier_to_transition = barrier->barrier; |
| 892 | barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT; |
| 893 | subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag); |
| 894 | } |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
| 898 | void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state, |
| 899 | VkQueueFlags queue_flags, const ResourceUsageTag &tag) { |
| 900 | current_subpass_ = 0; |
| 901 | rp_state_ = cb_state.activeRenderPass; |
| 902 | subpass_contexts_.reserve(rp_state_->createInfo.subpassCount); |
| 903 | // Add this for all subpasses here so that they exsist during next subpass validation |
| 904 | for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) { |
| 905 | subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context_); |
| 906 | } |
| 907 | attachment_views_ = state.GetCurrentAttachmentViews(cb_state); |
| 908 | |
| 909 | RecordLayoutTransitions(tag); |
| 910 | } |
| 911 | void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag &tag) { |
| 912 | current_subpass_++; |
| 913 | assert(current_subpass_ < subpass_contexts_.size()); |
| 914 | RecordLayoutTransitions(tag); |
| 915 | } |
| 916 | |
| 917 | void RenderPassAccessContext::RecordEndRenderPass(const ResourceUsageTag &tag) { |
| 918 | // Export the accesses from the renderpass... |
| 919 | external_context_->ResolveChildContexts(subpass_contexts_); |
| 920 | |
| 921 | // Add the "finalLayout" transitions to external |
| 922 | // Get them from where there we're hidding in the extra entry. |
| 923 | const auto &final_transitions = rp_state_->subpass_transitions.back(); |
| 924 | for (const auto &transition : final_transitions) { |
| 925 | const auto &attachment = attachment_views_[transition.attachment]; |
| 926 | const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack(); |
| 927 | assert(external_context_ == last_trackback.context); |
| 928 | external_context_->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier, |
| 929 | attachment->normalized_subresource_range, true, tag); |
| 930 | } |
| 931 | } |
| 932 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 933 | SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) { |
| 934 | const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask); |
| 935 | src_exec_scope = WithEarlierPipelineStages(src_stage_mask); |
| 936 | src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask); |
| 937 | const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask); |
| 938 | dst_exec_scope = WithLaterPipelineStages(dst_stage_mask); |
| 939 | dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask); |
| 940 | } |
| 941 | |
| 942 | void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) { |
| 943 | ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope); |
| 944 | ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope); |
| 945 | } |
| 946 | |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 947 | HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const { |
| 948 | HazardResult hazard; |
| 949 | auto usage = FlagBit(usage_index); |
| 950 | if (IsRead(usage)) { |
John Zulauf | c920122 | 2020-05-13 15:13:03 -0600 | [diff] [blame^] | 951 | if (last_write && IsWriteHazard(usage)) { |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 952 | hazard.Set(READ_AFTER_WRITE, write_tag); |
| 953 | } |
| 954 | } else { |
| 955 | // Assume write |
| 956 | // TODO determine what to do with READ-WRITE usage states if any |
| 957 | // Write-After-Write check -- if we have a previous write to test against |
| 958 | if (last_write && IsWriteHazard(usage)) { |
| 959 | hazard.Set(WRITE_AFTER_WRITE, write_tag); |
| 960 | } else { |
| 961 | // Only look for casus belli for WAR |
| 962 | const auto usage_stage = PipelineStageBit(usage_index); |
| 963 | for (uint32_t read_index = 0; read_index < last_read_count; read_index++) { |
| 964 | if (IsReadHazard(usage_stage, last_reads[read_index])) { |
| 965 | hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag); |
| 966 | break; |
| 967 | } |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | return hazard; |
| 972 | } |
| 973 | |
John Zulauf | 2f952d2 | 2020-02-10 11:34:51 -0700 | [diff] [blame] | 974 | // Asynchronous Hazards occur between subpasses with no connection through the DAG |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 975 | HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const { |
John Zulauf | 2f952d2 | 2020-02-10 11:34:51 -0700 | [diff] [blame] | 976 | HazardResult hazard; |
| 977 | auto usage = FlagBit(usage_index); |
| 978 | if (IsRead(usage)) { |
| 979 | if (last_write != 0) { |
| 980 | hazard.Set(READ_RACING_WRITE, write_tag); |
| 981 | } |
| 982 | } else { |
| 983 | if (last_write != 0) { |
| 984 | hazard.Set(WRITE_RACING_WRITE, write_tag); |
| 985 | } else if (last_read_count > 0) { |
| 986 | hazard.Set(WRITE_RACING_READ, last_reads[0].tag); |
| 987 | } |
| 988 | } |
| 989 | return hazard; |
| 990 | } |
| 991 | |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 992 | HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope, |
| 993 | SyncStageAccessFlags src_access_scope) const { |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 994 | // Only supporting image layout transitions for now |
| 995 | assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION); |
| 996 | HazardResult hazard; |
| 997 | if (last_write) { |
| 998 | // If the previous write is *not* in the 1st access scope |
| 999 | // *AND* the current barrier is not in the dependency chain |
| 1000 | // *AND* the there is no prior memory barrier for the previous write in the dependency chain |
| 1001 | // then the barrier access is unsafe (R/W after W) |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1002 | if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) { |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 1003 | // TODO: Do we need a difference hazard name for this? |
| 1004 | hazard.Set(WRITE_AFTER_WRITE, write_tag); |
| 1005 | } |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1006 | } |
| 1007 | if (!hazard.hazard) { |
| 1008 | // Look at the reads if any |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 1009 | for (uint32_t read_index = 0; read_index < last_read_count; read_index++) { |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1010 | const auto &read_access = last_reads[read_index]; |
| 1011 | // If the read stage is not in the src sync sync |
| 1012 | // *AND* not execution chained with an existing sync barrier (that's the or) |
| 1013 | // then the barrier access is unsafe (R/W after R) |
| 1014 | if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) { |
| 1015 | hazard.Set(WRITE_AFTER_READ, read_access.tag); |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 1016 | break; |
| 1017 | } |
| 1018 | } |
| 1019 | } |
| 1020 | return hazard; |
| 1021 | } |
| 1022 | |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 1023 | // The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no |
| 1024 | // tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another |
| 1025 | // exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones. |
| 1026 | void ResourceAccessState::Resolve(const ResourceAccessState &other) { |
| 1027 | if (write_tag.IsBefore(other.write_tag)) { |
| 1028 | // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation |
| 1029 | *this = other; |
| 1030 | } else if (!other.write_tag.IsBefore(write_tag)) { |
| 1031 | // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the |
| 1032 | // dependency chaining logic or any stage expansion) |
| 1033 | write_barriers |= other.write_barriers; |
| 1034 | |
| 1035 | // Merge that read states |
| 1036 | for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) { |
| 1037 | auto &other_read = other.last_reads[other_read_index]; |
| 1038 | if (last_read_stages & other_read.stage) { |
| 1039 | // Merge in the barriers for read stages that exist in *both* this and other |
| 1040 | // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index. |
| 1041 | for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) { |
| 1042 | auto &my_read = last_reads[my_read_index]; |
| 1043 | if (other_read.stage == my_read.stage) { |
| 1044 | if (my_read.tag.IsBefore(other_read.tag)) { |
| 1045 | my_read.tag = other_read.tag; |
| 1046 | } |
| 1047 | my_read.barriers |= other_read.barriers; |
| 1048 | break; |
| 1049 | } |
| 1050 | } |
| 1051 | } else { |
| 1052 | // The other read stage doesn't exist in this, so add it. |
| 1053 | last_reads[last_read_count] = other_read; |
| 1054 | last_read_count++; |
| 1055 | last_read_stages |= other_read.stage; |
| 1056 | } |
| 1057 | } |
| 1058 | } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore |
| 1059 | // it. |
| 1060 | } |
| 1061 | |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1062 | void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) { |
| 1063 | // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource... |
| 1064 | const auto usage_bit = FlagBit(usage_index); |
| 1065 | if (IsRead(usage_index)) { |
| 1066 | // Mulitple outstanding reads may be of interest and do dependency chains independently |
| 1067 | // However, for purposes of barrier tracking, only one read per pipeline stage matters |
| 1068 | const auto usage_stage = PipelineStageBit(usage_index); |
| 1069 | if (usage_stage & last_read_stages) { |
| 1070 | for (uint32_t read_index = 0; read_index < last_read_count; read_index++) { |
| 1071 | ReadState &access = last_reads[read_index]; |
| 1072 | if (access.stage == usage_stage) { |
| 1073 | access.barriers = 0; |
| 1074 | access.tag = tag; |
| 1075 | break; |
| 1076 | } |
| 1077 | } |
| 1078 | } else { |
| 1079 | // We don't have this stage in the list yet... |
| 1080 | assert(last_read_count < last_reads.size()); |
| 1081 | ReadState &access = last_reads[last_read_count++]; |
| 1082 | access.stage = usage_stage; |
| 1083 | access.barriers = 0; |
| 1084 | access.tag = tag; |
| 1085 | last_read_stages |= usage_stage; |
| 1086 | } |
| 1087 | } else { |
| 1088 | // Assume write |
| 1089 | // TODO determine what to do with READ-WRITE operations if any |
| 1090 | // Clobber last read and both sets of barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!! |
| 1091 | // if the last_reads/last_write were unsafe, we've reported them, |
| 1092 | // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them |
| 1093 | last_read_count = 0; |
| 1094 | last_read_stages = 0; |
| 1095 | |
| 1096 | write_barriers = 0; |
| 1097 | write_dependency_chain = 0; |
| 1098 | write_tag = tag; |
| 1099 | last_write = usage_bit; |
| 1100 | } |
| 1101 | } |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 1102 | |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1103 | void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) { |
| 1104 | // Execution Barriers only protect read operations |
| 1105 | for (uint32_t read_index = 0; read_index < last_read_count; read_index++) { |
| 1106 | ReadState &access = last_reads[read_index]; |
| 1107 | // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope |
| 1108 | if (srcStageMask & (access.stage | access.barriers)) { |
| 1109 | access.barriers |= dstStageMask; |
| 1110 | } |
| 1111 | } |
| 1112 | if (write_dependency_chain & srcStageMask) write_dependency_chain |= dstStageMask; |
| 1113 | } |
| 1114 | |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1115 | void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope, |
| 1116 | VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) { |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1117 | // Assuming we've applied the execution side of this barrier, we update just the write |
| 1118 | // The || implements the "dependency chain" logic for this barrier |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1119 | if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) { |
| 1120 | write_barriers |= dst_access_scope; |
| 1121 | write_dependency_chain |= dst_exec_scope; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1122 | } |
| 1123 | } |
| 1124 | |
John Zulauf | d1f85d4 | 2020-04-15 12:23:15 -0600 | [diff] [blame] | 1125 | void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1126 | auto *access_context = GetAccessContextNoInsert(command_buffer); |
| 1127 | if (access_context) { |
| 1128 | access_context->Reset(); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1129 | } |
| 1130 | } |
| 1131 | |
John Zulauf | d1f85d4 | 2020-04-15 12:23:15 -0600 | [diff] [blame] | 1132 | void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) { |
| 1133 | auto access_found = cb_access_state.find(command_buffer); |
| 1134 | if (access_found != cb_access_state.end()) { |
| 1135 | access_found->second->Reset(); |
| 1136 | cb_access_state.erase(access_found); |
| 1137 | } |
| 1138 | } |
| 1139 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1140 | void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask, |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1141 | VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope, |
| 1142 | SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount, |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1143 | const VkMemoryBarrier *pMemoryBarriers) { |
| 1144 | // TODO: Implement this better (maybe some delayed/on-demand integration). |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1145 | ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount, |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1146 | pMemoryBarriers); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1147 | context->ApplyGlobalBarriers(barriers_functor); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1148 | } |
| 1149 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1150 | void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope, |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1151 | SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope, |
| 1152 | SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count, |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1153 | const VkBufferMemoryBarrier *barriers) { |
| 1154 | // TODO Implement this at subresource/memory_range accuracy |
| 1155 | for (uint32_t index = 0; index < barrier_count; index++) { |
locke-lunarg | 3c03800 | 2020-04-30 23:08:08 -0600 | [diff] [blame] | 1156 | auto barrier = barriers[index]; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1157 | const auto *buffer = Get<BUFFER_STATE>(barrier.buffer); |
| 1158 | if (!buffer) continue; |
locke-lunarg | 3c03800 | 2020-04-30 23:08:08 -0600 | [diff] [blame] | 1159 | barrier.size = GetRealWholeSize(barrier.offset, barrier.size, buffer->createInfo.size); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1160 | ResourceAccessRange range = MakeRange(barrier); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1161 | const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask); |
| 1162 | const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask); |
| 1163 | const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope); |
| 1164 | context->UpdateMemoryAccess(*buffer, range, update_action); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1168 | void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope, |
| 1169 | SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope, |
| 1170 | SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count, |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1171 | const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) { |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1172 | for (uint32_t index = 0; index < barrier_count; index++) { |
| 1173 | const auto &barrier = barriers[index]; |
| 1174 | const auto *image = Get<IMAGE_STATE>(barrier.image); |
| 1175 | if (!image) continue; |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1176 | auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1177 | bool layout_transition = barrier.oldLayout != barrier.newLayout; |
| 1178 | const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask); |
| 1179 | const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask); |
| 1180 | context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range, |
| 1181 | layout_transition, tag); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1182 | } |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer, |
| 1186 | uint32_t regionCount, const VkBufferCopy *pRegions) const { |
| 1187 | bool skip = false; |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1188 | const auto *cb_context = GetAccessContext(commandBuffer); |
| 1189 | assert(cb_context); |
| 1190 | if (!cb_context) return skip; |
| 1191 | const auto *context = cb_context->GetCurrentAccessContext(); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1192 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1193 | // If we have no previous accesses, we have no hazards |
| 1194 | // TODO: make this sub-resource capable |
| 1195 | // TODO: make this general, and stuff it into templates/utility functions |
| 1196 | const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1197 | const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1198 | |
| 1199 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1200 | const auto ©_region = pRegions[region]; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1201 | if (src_buffer) { |
locke-lunarg | 3c03800 | 2020-04-30 23:08:08 -0600 | [diff] [blame] | 1202 | ResourceAccessRange src_range = |
| 1203 | MakeRange(copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size)); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1204 | auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1205 | if (hazard.hazard) { |
| 1206 | // TODO -- add tag information to log msg when useful. |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1207 | skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard), |
| 1208 | "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard), |
| 1209 | report_data->FormatHandle(srcBuffer).c_str(), region); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1210 | } |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1211 | } |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1212 | if (dst_buffer && !skip) { |
locke-lunarg | 3c03800 | 2020-04-30 23:08:08 -0600 | [diff] [blame] | 1213 | ResourceAccessRange dst_range = |
| 1214 | MakeRange(copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size)); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1215 | auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1216 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1217 | skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), |
| 1218 | "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard), |
| 1219 | report_data->FormatHandle(dstBuffer).c_str(), region); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1220 | } |
| 1221 | } |
| 1222 | if (skip) break; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1223 | } |
| 1224 | return skip; |
| 1225 | } |
| 1226 | |
| 1227 | void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer, |
| 1228 | uint32_t regionCount, const VkBufferCopy *pRegions) { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1229 | auto *cb_context = GetAccessContext(commandBuffer); |
| 1230 | assert(cb_context); |
John Zulauf | 2b151bf | 2020-04-24 15:37:44 -0600 | [diff] [blame] | 1231 | const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1232 | auto *context = cb_context->GetCurrentAccessContext(); |
| 1233 | |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1234 | const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1235 | const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1236 | |
| 1237 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1238 | const auto ©_region = pRegions[region]; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1239 | if (src_buffer) { |
locke-lunarg | 3c03800 | 2020-04-30 23:08:08 -0600 | [diff] [blame] | 1240 | ResourceAccessRange src_range = |
| 1241 | MakeRange(copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size)); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1242 | context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1243 | } |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1244 | if (dst_buffer) { |
locke-lunarg | 3c03800 | 2020-04-30 23:08:08 -0600 | [diff] [blame] | 1245 | ResourceAccessRange dst_range = |
| 1246 | MakeRange(copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size)); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1247 | context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1248 | } |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 1253 | VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, |
| 1254 | const VkImageCopy *pRegions) const { |
| 1255 | bool skip = false; |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1256 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1257 | assert(cb_access_context); |
| 1258 | if (!cb_access_context) return skip; |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1259 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1260 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 1261 | assert(context); |
| 1262 | if (!context) return skip; |
| 1263 | |
| 1264 | const auto *src_image = Get<IMAGE_STATE>(srcImage); |
| 1265 | const auto *dst_image = Get<IMAGE_STATE>(dstImage); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1266 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1267 | const auto ©_region = pRegions[region]; |
| 1268 | if (src_image) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1269 | auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1270 | copy_region.srcOffset, copy_region.extent); |
| 1271 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1272 | skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard), |
| 1273 | "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard), |
| 1274 | report_data->FormatHandle(srcImage).c_str(), region); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1275 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | if (dst_image) { |
locke-lunarg | 1df1f88 | 2020-03-02 16:42:08 -0700 | [diff] [blame] | 1279 | VkExtent3D dst_copy_extent = |
| 1280 | GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1281 | auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, |
locke-lunarg | 1df1f88 | 2020-03-02 16:42:08 -0700 | [diff] [blame] | 1282 | copy_region.dstOffset, dst_copy_extent); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1283 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1284 | skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard), |
| 1285 | "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard), |
| 1286 | report_data->FormatHandle(dstImage).c_str(), region); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1287 | } |
locke-lunarg | 1dbbb9e | 2020-02-28 22:43:53 -0700 | [diff] [blame] | 1288 | if (skip) break; |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1289 | } |
| 1290 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1291 | |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1292 | return skip; |
| 1293 | } |
| 1294 | |
| 1295 | void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 1296 | VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, |
| 1297 | const VkImageCopy *pRegions) { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1298 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1299 | assert(cb_access_context); |
John Zulauf | 2b151bf | 2020-04-24 15:37:44 -0600 | [diff] [blame] | 1300 | const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1301 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 1302 | assert(context); |
| 1303 | |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1304 | auto *src_image = Get<IMAGE_STATE>(srcImage); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1305 | auto *dst_image = Get<IMAGE_STATE>(dstImage); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1306 | |
| 1307 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1308 | const auto ©_region = pRegions[region]; |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1309 | if (src_image) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1310 | context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset, |
| 1311 | copy_region.extent, tag); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1312 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1313 | if (dst_image) { |
locke-lunarg | 1df1f88 | 2020-03-02 16:42:08 -0700 | [diff] [blame] | 1314 | VkExtent3D dst_copy_extent = |
| 1315 | GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1316 | context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset, |
| 1317 | dst_copy_extent, tag); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1318 | } |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask, |
| 1323 | VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags, |
| 1324 | uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers, |
| 1325 | uint32_t bufferMemoryBarrierCount, |
| 1326 | const VkBufferMemoryBarrier *pBufferMemoryBarriers, |
| 1327 | uint32_t imageMemoryBarrierCount, |
| 1328 | const VkImageMemoryBarrier *pImageMemoryBarriers) const { |
| 1329 | bool skip = false; |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1330 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1331 | assert(cb_access_context); |
| 1332 | if (!cb_access_context) return skip; |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 1333 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1334 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 1335 | assert(context); |
| 1336 | if (!context) return skip; |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 1337 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1338 | const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask); |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1339 | const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask); |
| 1340 | auto src_stage_accesses = AccessScopeByStage(src_stage_mask); |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 1341 | // Validate Image Layout transitions |
| 1342 | for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) { |
| 1343 | const auto &barrier = pImageMemoryBarriers[index]; |
| 1344 | if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point. |
| 1345 | const auto *image_state = Get<IMAGE_STATE>(barrier.image); |
| 1346 | if (!image_state) continue; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1347 | const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier); |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 1348 | if (hazard.hazard) { |
| 1349 | // TODO -- add tag information to log msg when useful. |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1350 | skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard), |
| 1351 | "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s", string_SyncHazard(hazard.hazard), |
| 1352 | index, report_data->FormatHandle(barrier.image).c_str()); |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 1353 | } |
| 1354 | } |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1355 | |
| 1356 | return skip; |
| 1357 | } |
| 1358 | |
| 1359 | void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask, |
| 1360 | VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags, |
| 1361 | uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers, |
| 1362 | uint32_t bufferMemoryBarrierCount, |
| 1363 | const VkBufferMemoryBarrier *pBufferMemoryBarriers, |
| 1364 | uint32_t imageMemoryBarrierCount, |
| 1365 | const VkImageMemoryBarrier *pImageMemoryBarriers) { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1366 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1367 | assert(cb_access_context); |
| 1368 | if (!cb_access_context) return; |
John Zulauf | 2b151bf | 2020-04-24 15:37:44 -0600 | [diff] [blame] | 1369 | const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1370 | auto access_context = cb_access_context->GetCurrentAccessContext(); |
| 1371 | assert(access_context); |
| 1372 | if (!access_context) return; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1373 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1374 | const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask); |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1375 | auto src_stage_accesses = AccessScopeByStage(src_stage_mask); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1376 | const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask); |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1377 | auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask); |
| 1378 | const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask); |
| 1379 | const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1380 | ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses, |
| 1381 | bufferMemoryBarrierCount, pBufferMemoryBarriers); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1382 | ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses, |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1383 | imageMemoryBarrierCount, pImageMemoryBarriers, tag); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1384 | |
| 1385 | // Apply these last in-case there operation is a superset of the other two and would clean them up... |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1386 | ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount, |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 1387 | pMemoryBarriers); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo, |
| 1391 | const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) { |
| 1392 | // The state tracker sets up the device state |
| 1393 | StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result); |
| 1394 | |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 1395 | // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker |
| 1396 | // refactor would be messier without. |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1397 | // TODO: Find a good way to do this hooklessly. |
| 1398 | ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map); |
| 1399 | ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation); |
| 1400 | SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data); |
| 1401 | |
John Zulauf | d1f85d4 | 2020-04-15 12:23:15 -0600 | [diff] [blame] | 1402 | sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void { |
| 1403 | sync_device_state->ResetCommandBufferCallback(command_buffer); |
| 1404 | }); |
| 1405 | sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void { |
| 1406 | sync_device_state->FreeCommandBufferCallback(command_buffer); |
| 1407 | }); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1408 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1409 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1410 | bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin, |
| 1411 | const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const { |
| 1412 | bool skip = false; |
| 1413 | const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass); |
| 1414 | auto cb_context = GetAccessContext(commandBuffer); |
| 1415 | |
| 1416 | if (rp_state && cb_context) { |
| 1417 | skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name); |
| 1418 | } |
| 1419 | |
| 1420 | return skip; |
| 1421 | } |
| 1422 | |
| 1423 | bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin, |
| 1424 | VkSubpassContents contents) const { |
| 1425 | bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents); |
| 1426 | auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>(); |
| 1427 | subpass_begin_info.contents = contents; |
| 1428 | skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass"); |
| 1429 | return skip; |
| 1430 | } |
| 1431 | |
| 1432 | bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin, |
| 1433 | const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const { |
| 1434 | bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo); |
| 1435 | skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2"); |
| 1436 | return skip; |
| 1437 | } |
| 1438 | |
| 1439 | bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer, |
| 1440 | const VkRenderPassBeginInfo *pRenderPassBegin, |
| 1441 | const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const { |
| 1442 | bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo); |
| 1443 | skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR"); |
| 1444 | return skip; |
| 1445 | } |
| 1446 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1447 | void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo, |
| 1448 | VkResult result) { |
| 1449 | // The state tracker sets up the command buffer state |
| 1450 | StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result); |
| 1451 | |
| 1452 | // Create/initialize the structure that trackers accesses at the command buffer scope. |
| 1453 | auto cb_access_context = GetAccessContext(commandBuffer); |
| 1454 | assert(cb_access_context); |
| 1455 | cb_access_context->Reset(); |
| 1456 | } |
| 1457 | |
| 1458 | void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin, |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1459 | const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1460 | auto cb_context = GetAccessContext(commandBuffer); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1461 | if (cb_context) { |
| 1462 | cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command)); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin, |
| 1467 | VkSubpassContents contents) { |
| 1468 | StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents); |
| 1469 | auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>(); |
| 1470 | subpass_begin_info.contents = contents; |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1471 | RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin, |
| 1475 | const VkSubpassBeginInfo *pSubpassBeginInfo) { |
| 1476 | StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1477 | RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer, |
| 1481 | const VkRenderPassBeginInfo *pRenderPassBegin, |
| 1482 | const VkSubpassBeginInfo *pSubpassBeginInfo) { |
| 1483 | StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1484 | RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2); |
| 1485 | } |
| 1486 | |
| 1487 | bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo, |
| 1488 | const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const { |
| 1489 | bool skip = false; |
| 1490 | |
| 1491 | auto cb_context = GetAccessContext(commandBuffer); |
| 1492 | assert(cb_context); |
| 1493 | auto cb_state = cb_context->GetCommandBufferState(); |
| 1494 | if (!cb_state) return skip; |
| 1495 | |
| 1496 | auto rp_state = cb_state->activeRenderPass; |
| 1497 | if (!rp_state) return skip; |
| 1498 | |
| 1499 | skip |= cb_context->ValidateNextSubpass(func_name); |
| 1500 | |
| 1501 | return skip; |
| 1502 | } |
| 1503 | |
| 1504 | bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const { |
| 1505 | bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents); |
| 1506 | auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>(); |
| 1507 | subpass_begin_info.contents = contents; |
| 1508 | skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass"); |
| 1509 | return skip; |
| 1510 | } |
| 1511 | |
| 1512 | bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo, |
| 1513 | const VkSubpassEndInfoKHR *pSubpassEndInfo) const { |
| 1514 | bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo); |
| 1515 | skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR"); |
| 1516 | return skip; |
| 1517 | } |
| 1518 | |
| 1519 | bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo, |
| 1520 | const VkSubpassEndInfo *pSubpassEndInfo) const { |
| 1521 | bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo); |
| 1522 | skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2"); |
| 1523 | return skip; |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo, |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1527 | const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1528 | auto cb_context = GetAccessContext(commandBuffer); |
| 1529 | assert(cb_context); |
| 1530 | auto cb_state = cb_context->GetCommandBufferState(); |
| 1531 | if (!cb_state) return; |
| 1532 | |
| 1533 | auto rp_state = cb_state->activeRenderPass; |
| 1534 | if (!rp_state) return; |
| 1535 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1536 | cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command)); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) { |
| 1540 | StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents); |
| 1541 | auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>(); |
| 1542 | subpass_begin_info.contents = contents; |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1543 | RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo, |
| 1547 | const VkSubpassEndInfo *pSubpassEndInfo) { |
| 1548 | StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1549 | RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo, |
| 1553 | const VkSubpassEndInfo *pSubpassEndInfo) { |
| 1554 | StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1555 | RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1556 | } |
| 1557 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1558 | bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo, |
| 1559 | const char *func_name) const { |
| 1560 | bool skip = false; |
| 1561 | |
| 1562 | auto cb_context = GetAccessContext(commandBuffer); |
| 1563 | assert(cb_context); |
| 1564 | auto cb_state = cb_context->GetCommandBufferState(); |
| 1565 | if (!cb_state) return skip; |
| 1566 | |
| 1567 | auto rp_state = cb_state->activeRenderPass; |
| 1568 | if (!rp_state) return skip; |
| 1569 | |
| 1570 | skip |= cb_context->ValidateEndRenderpass(func_name); |
| 1571 | return skip; |
| 1572 | } |
| 1573 | |
| 1574 | bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const { |
| 1575 | bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer); |
| 1576 | skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass"); |
| 1577 | return skip; |
| 1578 | } |
| 1579 | |
| 1580 | bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, |
| 1581 | const VkSubpassEndInfoKHR *pSubpassEndInfo) const { |
| 1582 | bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo); |
| 1583 | skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2"); |
| 1584 | return skip; |
| 1585 | } |
| 1586 | |
| 1587 | bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, |
| 1588 | const VkSubpassEndInfoKHR *pSubpassEndInfo) const { |
| 1589 | bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo); |
| 1590 | skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR"); |
| 1591 | return skip; |
| 1592 | } |
| 1593 | |
| 1594 | void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, |
| 1595 | CMD_TYPE command) { |
John Zulauf | e5da6e5 | 2020-03-18 15:32:18 -0600 | [diff] [blame] | 1596 | // Resolve the all subpass contexts to the command buffer contexts |
| 1597 | auto cb_context = GetAccessContext(commandBuffer); |
| 1598 | assert(cb_context); |
| 1599 | auto cb_state = cb_context->GetCommandBufferState(); |
| 1600 | if (!cb_state) return; |
| 1601 | |
| 1602 | const auto *rp_state = cb_state->activeRenderPass; |
| 1603 | if (!rp_state) return; |
| 1604 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1605 | cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command)); |
John Zulauf | e5da6e5 | 2020-03-18 15:32:18 -0600 | [diff] [blame] | 1606 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1607 | |
| 1608 | void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) { |
| 1609 | StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1610 | RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) { |
| 1614 | StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1615 | RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) { |
| 1619 | StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1620 | RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1621 | } |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1622 | |
| 1623 | bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage, |
| 1624 | VkImageLayout dstImageLayout, uint32_t regionCount, |
| 1625 | const VkBufferImageCopy *pRegions) const { |
| 1626 | bool skip = false; |
| 1627 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1628 | assert(cb_access_context); |
| 1629 | if (!cb_access_context) return skip; |
| 1630 | |
| 1631 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 1632 | assert(context); |
| 1633 | if (!context) return skip; |
| 1634 | |
| 1635 | const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1636 | const auto *dst_image = Get<IMAGE_STATE>(dstImage); |
| 1637 | |
| 1638 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1639 | const auto ©_region = pRegions[region]; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1640 | if (src_buffer) { |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1641 | ResourceAccessRange src_range = |
| 1642 | MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format)); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1643 | auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1644 | if (hazard.hazard) { |
| 1645 | // TODO -- add tag information to log msg when useful. |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1646 | skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard), |
| 1647 | "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32, |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1648 | string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region); |
| 1649 | } |
| 1650 | } |
| 1651 | if (dst_image) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1652 | auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource, |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1653 | copy_region.imageOffset, copy_region.imageExtent); |
| 1654 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1655 | skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard), |
| 1656 | "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32, |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1657 | string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region); |
| 1658 | } |
| 1659 | if (skip) break; |
| 1660 | } |
| 1661 | if (skip) break; |
| 1662 | } |
| 1663 | return skip; |
| 1664 | } |
| 1665 | |
| 1666 | void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage, |
| 1667 | VkImageLayout dstImageLayout, uint32_t regionCount, |
| 1668 | const VkBufferImageCopy *pRegions) { |
| 1669 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1670 | assert(cb_access_context); |
John Zulauf | 2b151bf | 2020-04-24 15:37:44 -0600 | [diff] [blame] | 1671 | const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1672 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 1673 | assert(context); |
| 1674 | |
| 1675 | const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1676 | const auto *dst_image = Get<IMAGE_STATE>(dstImage); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1677 | |
| 1678 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1679 | const auto ©_region = pRegions[region]; |
| 1680 | if (src_buffer) { |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1681 | ResourceAccessRange src_range = |
| 1682 | MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format)); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1683 | context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1684 | } |
| 1685 | if (dst_image) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1686 | context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource, |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 1687 | copy_region.imageOffset, copy_region.imageExtent, tag); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1688 | } |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, |
| 1693 | VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount, |
| 1694 | const VkBufferImageCopy *pRegions) const { |
| 1695 | bool skip = false; |
| 1696 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1697 | assert(cb_access_context); |
| 1698 | if (!cb_access_context) return skip; |
| 1699 | |
| 1700 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 1701 | assert(context); |
| 1702 | if (!context) return skip; |
| 1703 | |
| 1704 | const auto *src_image = Get<IMAGE_STATE>(srcImage); |
| 1705 | const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer); |
| 1706 | const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE; |
| 1707 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1708 | const auto ©_region = pRegions[region]; |
| 1709 | if (src_image) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1710 | auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource, |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1711 | copy_region.imageOffset, copy_region.imageExtent); |
| 1712 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1713 | skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard), |
| 1714 | "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32, |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1715 | string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region); |
| 1716 | } |
| 1717 | } |
| 1718 | if (dst_mem) { |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1719 | ResourceAccessRange dst_range = |
| 1720 | MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format)); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1721 | auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1722 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1723 | skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), |
| 1724 | "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32, |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1725 | string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region); |
| 1726 | } |
| 1727 | } |
| 1728 | if (skip) break; |
| 1729 | } |
| 1730 | return skip; |
| 1731 | } |
| 1732 | |
| 1733 | void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 1734 | VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) { |
| 1735 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1736 | assert(cb_access_context); |
John Zulauf | 2b151bf | 2020-04-24 15:37:44 -0600 | [diff] [blame] | 1737 | const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1738 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 1739 | assert(context); |
| 1740 | |
| 1741 | const auto *src_image = Get<IMAGE_STATE>(srcImage); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1742 | auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer); |
| 1743 | const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE; |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 1744 | const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1745 | |
| 1746 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1747 | const auto ©_region = pRegions[region]; |
| 1748 | if (src_image) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1749 | context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource, |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 1750 | copy_region.imageOffset, copy_region.imageExtent, tag); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1751 | } |
| 1752 | if (dst_buffer) { |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1753 | ResourceAccessRange dst_range = |
| 1754 | MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format)); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1755 | context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1756 | } |
| 1757 | } |
| 1758 | } |
| 1759 | |
| 1760 | bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 1761 | VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, |
| 1762 | const VkImageBlit *pRegions, VkFilter filter) const { |
| 1763 | bool skip = false; |
| 1764 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1765 | assert(cb_access_context); |
| 1766 | if (!cb_access_context) return skip; |
| 1767 | |
| 1768 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 1769 | assert(context); |
| 1770 | if (!context) return skip; |
| 1771 | |
| 1772 | const auto *src_image = Get<IMAGE_STATE>(srcImage); |
| 1773 | const auto *dst_image = Get<IMAGE_STATE>(dstImage); |
| 1774 | |
| 1775 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1776 | const auto &blit_region = pRegions[region]; |
| 1777 | if (src_image) { |
| 1778 | VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x), |
| 1779 | static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y), |
| 1780 | static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)}; |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1781 | auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1782 | blit_region.srcOffsets[0], extent); |
| 1783 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1784 | skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard), |
| 1785 | "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard), |
| 1786 | report_data->FormatHandle(srcImage).c_str(), region); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | if (dst_image) { |
| 1791 | VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x), |
| 1792 | static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y), |
| 1793 | static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)}; |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1794 | auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1795 | blit_region.dstOffsets[0], extent); |
| 1796 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1797 | skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard), |
| 1798 | "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard), |
| 1799 | report_data->FormatHandle(dstImage).c_str(), region); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1800 | } |
| 1801 | if (skip) break; |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | return skip; |
| 1806 | } |
| 1807 | |
| 1808 | void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 1809 | VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, |
| 1810 | const VkImageBlit *pRegions, VkFilter filter) { |
| 1811 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1812 | assert(cb_access_context); |
John Zulauf | 2b151bf | 2020-04-24 15:37:44 -0600 | [diff] [blame] | 1813 | const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1814 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 1815 | assert(context); |
| 1816 | |
| 1817 | auto *src_image = Get<IMAGE_STATE>(srcImage); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1818 | auto *dst_image = Get<IMAGE_STATE>(dstImage); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1819 | |
| 1820 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1821 | const auto &blit_region = pRegions[region]; |
| 1822 | if (src_image) { |
| 1823 | VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x), |
| 1824 | static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y), |
| 1825 | static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)}; |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1826 | context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 1827 | blit_region.srcOffsets[0], extent, tag); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1828 | } |
| 1829 | if (dst_image) { |
| 1830 | VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x), |
| 1831 | static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y), |
| 1832 | static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)}; |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1833 | context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 1834 | blit_region.dstOffsets[0], extent, tag); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1835 | } |
| 1836 | } |
| 1837 | } |