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