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