John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1 | /* Copyright (c) 2019 The Khronos Group Inc. |
| 2 | * Copyright (c) 2019 Valve Corporation |
| 3 | * Copyright (c) 2019 LunarG, Inc. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | * Author: John Zulauf <jzulauf@lunarg.com> |
| 18 | */ |
| 19 | |
| 20 | #include <limits> |
| 21 | #include <vector> |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 22 | #include <memory> |
| 23 | #include <bitset> |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 24 | #include "synchronization_validation.h" |
| 25 | |
| 26 | static const char *string_SyncHazardVUID(SyncHazard hazard) { |
| 27 | switch (hazard) { |
| 28 | case SyncHazard::NONE: |
John Zulauf | 2f952d2 | 2020-02-10 11:34:51 -0700 | [diff] [blame] | 29 | return "SYNC-HAZARD-NONE"; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 30 | break; |
| 31 | case SyncHazard::READ_AFTER_WRITE: |
| 32 | return "SYNC-HAZARD-READ_AFTER_WRITE"; |
| 33 | break; |
| 34 | case SyncHazard::WRITE_AFTER_READ: |
| 35 | return "SYNC-HAZARD-WRITE_AFTER_READ"; |
| 36 | break; |
| 37 | case SyncHazard::WRITE_AFTER_WRITE: |
| 38 | return "SYNC-HAZARD-WRITE_AFTER_WRITE"; |
| 39 | break; |
John Zulauf | 2f952d2 | 2020-02-10 11:34:51 -0700 | [diff] [blame] | 40 | case SyncHazard::READ_RACING_WRITE: |
| 41 | return "SYNC-HAZARD-READ-RACING-WRITE"; |
| 42 | break; |
| 43 | case SyncHazard::WRITE_RACING_WRITE: |
| 44 | return "SYNC-HAZARD-WRITE-RACING-WRITE"; |
| 45 | break; |
| 46 | case SyncHazard::WRITE_RACING_READ: |
| 47 | return "SYNC-HAZARD-WRITE-RACING-READ"; |
| 48 | break; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 49 | default: |
| 50 | assert(0); |
| 51 | } |
| 52 | return "SYNC-HAZARD-INVALID"; |
| 53 | } |
| 54 | |
| 55 | static const char *string_SyncHazard(SyncHazard hazard) { |
| 56 | switch (hazard) { |
| 57 | case SyncHazard::NONE: |
| 58 | return "NONR"; |
| 59 | break; |
| 60 | case SyncHazard::READ_AFTER_WRITE: |
| 61 | return "READ_AFTER_WRITE"; |
| 62 | break; |
| 63 | case SyncHazard::WRITE_AFTER_READ: |
| 64 | return "WRITE_AFTER_READ"; |
| 65 | break; |
| 66 | case SyncHazard::WRITE_AFTER_WRITE: |
| 67 | return "WRITE_AFTER_WRITE"; |
| 68 | break; |
John Zulauf | 2f952d2 | 2020-02-10 11:34:51 -0700 | [diff] [blame] | 69 | case SyncHazard::READ_RACING_WRITE: |
| 70 | return "READ_RACING_WRITE"; |
| 71 | break; |
| 72 | case SyncHazard::WRITE_RACING_WRITE: |
| 73 | return "WRITE_RACING_WRITE"; |
| 74 | break; |
| 75 | case SyncHazard::WRITE_RACING_READ: |
| 76 | return "WRITE_RACING_READ"; |
| 77 | break; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 78 | default: |
| 79 | assert(0); |
| 80 | } |
| 81 | return "INVALID HAZARD"; |
| 82 | } |
| 83 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 84 | template <typename T> |
| 85 | static ResourceAccessRange MakeRange(const T&has_offset_and_size) { |
| 86 | return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size)); |
| 87 | } |
| 88 | |
| 89 | static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { |
| 90 | return ResourceAccessRange(start, (start + size)); |
| 91 | } |
| 92 | |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 93 | // Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension |
| 94 | VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) { |
| 95 | VkPipelineStageFlags expanded = stage_mask; |
| 96 | if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) { |
| 97 | expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; |
| 98 | for (const auto &all_commands : syncAllCommandStagesByQueueFlags) { |
| 99 | if (all_commands.first & queue_flags) { |
| 100 | expanded |= all_commands.second; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) { |
| 105 | expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT; |
| 106 | expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT; |
| 107 | } |
| 108 | return expanded; |
| 109 | } |
| 110 | |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 111 | VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask, |
| 112 | std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) { |
| 113 | VkPipelineStageFlags unscanned = stage_mask; |
| 114 | VkPipelineStageFlags related = 0; |
| 115 | for (const auto entry : map) { |
| 116 | const auto stage = entry.first; |
| 117 | if (stage & unscanned) { |
| 118 | related = related | entry.second; |
| 119 | unscanned = unscanned & ~stage; |
| 120 | if (!unscanned) break; |
| 121 | } |
| 122 | } |
| 123 | return related; |
| 124 | } |
| 125 | |
| 126 | VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) { |
| 127 | return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages); |
| 128 | } |
| 129 | |
| 130 | VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) { |
| 131 | return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages); |
| 132 | } |
| 133 | |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 134 | 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] | 135 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 136 | AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags, |
| 137 | const std::vector<SubpassDependencyGraphNode> &dependencies, |
| 138 | const std::vector<AccessContext> &contexts, AccessContext *external_context) { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 139 | Reset(); |
| 140 | const auto &subpass_dep = dependencies[subpass]; |
| 141 | prev_.reserve(subpass_dep.prev.size()); |
| 142 | for (const auto &prev_dep : subpass_dep.prev) { |
| 143 | assert(prev_dep.dependency); |
| 144 | const auto dep = *prev_dep.dependency; |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 145 | prev_.emplace_back(const_cast<AccessContext *>(&contexts[dep.srcSubpass]), queue_flags, dep); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 146 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 147 | |
| 148 | async_.reserve(subpass_dep.async.size()); |
| 149 | for (const auto async_subpass : subpass_dep.async) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 150 | async_.emplace_back(const_cast<AccessContext *>(&contexts[async_subpass])); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 151 | } |
John Zulauf | e5da6e5 | 2020-03-18 15:32:18 -0600 | [diff] [blame] | 152 | if (subpass_dep.barrier_from_external) { |
| 153 | src_external_ = TrackBack(external_context, queue_flags, *subpass_dep.barrier_from_external); |
| 154 | } else { |
| 155 | src_external_ = TrackBack(); |
| 156 | } |
| 157 | if (subpass_dep.barrier_to_external) { |
| 158 | dst_external_ = TrackBack(this, queue_flags, *subpass_dep.barrier_to_external); |
| 159 | } else { |
| 160 | dst_external_ = TrackBack(); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 161 | } |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 162 | } |
| 163 | |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 164 | template <typename Detector> |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 165 | HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector, |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 166 | const ResourceAccessRange &range) const { |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 167 | ResourceAccessRangeMap descent_map; |
| 168 | ResourceAccessState default_state; // When present, PreviousAccess will "infill" |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 169 | ResolvePreviousAccess(type, range, &descent_map, &default_state); |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 170 | |
| 171 | HazardResult hazard; |
| 172 | for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) { |
| 173 | hazard = detector.Detect(prev); |
| 174 | } |
| 175 | return hazard; |
| 176 | } |
| 177 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 178 | // A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk |
| 179 | // the DAG of the contexts (for example subpasses) |
| 180 | template <typename Detector> |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 181 | HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 182 | const ResourceAccessRange &range) const { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 183 | HazardResult hazard; |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 184 | |
| 185 | // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context |
| 186 | // so we'll check these first |
| 187 | for (const auto &async_context : async_) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 188 | hazard = async_context->DetectAsyncHazard(type, detector, range); |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 189 | if (hazard.hazard) return hazard; |
| 190 | } |
| 191 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 192 | const auto &accesses = GetAccessStateMap(type); |
| 193 | const auto from = accesses.lower_bound(range); |
| 194 | if (from != accesses.end() && from->first.intersects(range)) { |
| 195 | const auto to = accesses.upper_bound(range); |
| 196 | ResourceAccessRange gap = {range.begin, range.begin}; |
| 197 | for (auto pos = from; pos != to; ++pos) { |
| 198 | hazard = detector.Detect(pos); |
| 199 | if (hazard.hazard) return hazard; |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 200 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 201 | // make sure we don't go past range |
| 202 | auto upper_bound = std::min(range.end, pos->first.end); |
| 203 | gap.end = upper_bound; |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 204 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 205 | // TODO: After profiling we may want to change the descent logic such that we don't recur per gap... |
| 206 | if (!gap.empty()) { |
| 207 | // Must recur on all gaps |
| 208 | hazard = DetectPreviousHazard(type, detector, gap); |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 209 | if (hazard.hazard) return hazard; |
| 210 | } |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 211 | gap.begin = upper_bound; |
| 212 | } |
| 213 | gap.end = range.end; |
| 214 | if (gap.non_empty()) { |
| 215 | hazard = DetectPreviousHazard(type, detector, gap); |
| 216 | if (hazard.hazard) return hazard; |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 217 | } |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 218 | } else { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 219 | hazard = DetectPreviousHazard(type, detector, range); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | return hazard; |
| 223 | } |
| 224 | |
| 225 | // A non recursive range walker for the asynchronous contexts (those we have no barriers with) |
| 226 | template <typename Detector> |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 227 | HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 228 | const ResourceAccessRange &range) const { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 229 | auto &accesses = GetAccessStateMap(type); |
| 230 | const auto from = accesses.lower_bound(range); |
| 231 | const auto to = accesses.upper_bound(range); |
| 232 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 233 | HazardResult hazard; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 234 | for (auto pos = from; pos != to && !hazard.hazard; ++pos) { |
| 235 | hazard = detector.DetectAsync(pos); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 236 | } |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 237 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 238 | return hazard; |
| 239 | } |
| 240 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 241 | void AccessContext::ResolveTrackBack(AddressType type, const ResourceAccessRange &range, |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 242 | const AccessContext::TrackBack &track_back, ResourceAccessRangeMap *descent_map, |
| 243 | const ResourceAccessState *infill_state, bool recur_to_infill) const { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 244 | ParallelMapIterator current(*descent_map, GetAccessStateMap(type), range.begin); |
| 245 | while (current->range.non_empty()) { |
| 246 | if (current->pos_B->valid) { |
| 247 | const auto &src_pos = current->pos_B->lower_bound; |
| 248 | auto access_with_barrier = src_pos->second; |
| 249 | access_with_barrier.ApplyBarrier(track_back.barrier); |
| 250 | if (current->pos_A->valid) { |
| 251 | current.trim_A(); |
| 252 | current->pos_A->lower_bound->second.Resolve(access_with_barrier); |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 253 | } else { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 254 | descent_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, access_with_barrier)); |
| 255 | current.invalidate_A(); // Update the parallel iterator to point at the correct segment after split(s) |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 256 | } |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 257 | } else { |
| 258 | // we have to descend to fill this gap |
| 259 | if (recur_to_infill) { |
| 260 | track_back.context->ResolvePreviousAccess(type, current->range, descent_map, infill_state); |
| 261 | current.invalidate_A(); // Update the parallel iterator to point at the correct segment after recursion. |
| 262 | } |
| 263 | if (!current->pos_A->valid && infill_state) { |
| 264 | // If we didn't find anything in the previous range, we infill with default to prevent repeating |
| 265 | // a fruitless search |
| 266 | descent_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state)); |
| 267 | current.invalidate_A(); // Update the parallel iterator to point at the correct segment after insert |
| 268 | } |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 269 | } |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 270 | ++current; |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 271 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 272 | } |
| 273 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 274 | void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 275 | ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const { |
John Zulauf | e5da6e5 | 2020-03-18 15:32:18 -0600 | [diff] [blame] | 276 | if ((prev_.size() == 0) && (src_external_.context == nullptr)) { |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 277 | if (range.non_empty() && infill_state) { |
| 278 | descent_map->insert(std::make_pair(range, *infill_state)); |
| 279 | } |
| 280 | } else { |
| 281 | // Look for something to fill the gap further along. |
| 282 | for (const auto &prev_dep : prev_) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 283 | ResolveTrackBack(type, range, prev_dep, descent_map, infill_state); |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 284 | } |
| 285 | |
John Zulauf | e5da6e5 | 2020-03-18 15:32:18 -0600 | [diff] [blame] | 286 | if (src_external_.context) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 287 | ResolveTrackBack(type, range, src_external_, descent_map, infill_state); |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 288 | } |
| 289 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 290 | } |
| 291 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 292 | AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) { |
| 293 | return (image.createInfo.tiling == VK_IMAGE_TILING_LINEAR) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress; |
| 294 | } |
| 295 | |
| 296 | VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) { |
| 297 | return bindable.binding.offset + bindable.binding.mem_state->fake_base_address; |
| 298 | } |
| 299 | |
| 300 | static bool SimpleBinding(const BINDABLE &bindable) { |
| 301 | return !bindable.sparse && bindable.binding.mem_state; |
| 302 | } |
| 303 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 304 | void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg, |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 305 | AddressType address_type, ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const { |
| 306 | if (!SimpleBinding(image_state)) return; |
| 307 | |
John Zulauf | 62f1059 | 2020-04-03 12:20:02 -0600 | [diff] [blame] | 308 | auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg); |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 309 | subresource_adapter::ImageRangeGenerator range_gen(image_state.fragment_encoder, subresource_range, {0, 0, 0}, |
| 310 | image_state.createInfo.extent); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 311 | const auto base_address = ResourceBaseAddress(image_state); |
John Zulauf | 62f1059 | 2020-04-03 12:20:02 -0600 | [diff] [blame] | 312 | for (; range_gen->non_empty(); ++range_gen) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 313 | ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state); |
John Zulauf | 62f1059 | 2020-04-03 12:20:02 -0600 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 317 | class HazardDetector { |
| 318 | SyncStageAccessIndex usage_index_; |
| 319 | |
| 320 | public: |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 321 | 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] | 322 | HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const { |
| 323 | return pos->second.DetectAsyncHazard(usage_index_); |
| 324 | } |
| 325 | HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {} |
| 326 | }; |
| 327 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 328 | HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index, |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 329 | const ResourceAccessRange &range) const { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 330 | HazardDetector detector(usage_index); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 331 | return DetectHazard(type, detector, range); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 332 | } |
| 333 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 334 | HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index, |
| 335 | const ResourceAccessRange &range) const { |
| 336 | if (!SimpleBinding(buffer)) return HazardResult(); |
| 337 | return DetectHazard (AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer)); |
John Zulauf | e5da6e5 | 2020-03-18 15:32:18 -0600 | [diff] [blame] | 338 | } |
| 339 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 340 | HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, |
| 341 | const VkImageSubresourceLayers &subresource, const VkOffset3D &offset, |
| 342 | const VkExtent3D &extent) const { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 343 | if (!SimpleBinding(image)) return HazardResult(); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 344 | // TODO: replace the encoder/generator with offset3D/extent3D aware versions |
| 345 | VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer, |
| 346 | subresource.layerCount}; |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 347 | subresource_adapter::ImageRangeGenerator range_gen(image.fragment_encoder, subresource_range, offset, extent); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 348 | const auto address_type = ImageAddressType(image); |
| 349 | const auto base_address = ResourceBaseAddress(image); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 350 | for (; range_gen->non_empty(); ++range_gen) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 351 | HazardResult hazard = DetectHazard(address_type, current_usage, (*range_gen + base_address)); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 352 | if (hazard.hazard) return hazard; |
| 353 | } |
| 354 | return HazardResult(); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 355 | } |
| 356 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 357 | class BarrierHazardDetector { |
| 358 | public: |
| 359 | BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope, |
| 360 | SyncStageAccessFlags src_access_scope) |
| 361 | : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {} |
| 362 | |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 363 | HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { |
| 364 | return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_); |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 365 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 366 | HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const { |
| 367 | // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite |
| 368 | return pos->second.DetectAsyncHazard(usage_index_); |
| 369 | } |
| 370 | |
| 371 | private: |
| 372 | SyncStageAccessIndex usage_index_; |
| 373 | VkPipelineStageFlags src_exec_scope_; |
| 374 | SyncStageAccessFlags src_access_scope_; |
| 375 | }; |
| 376 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 377 | HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage, |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 378 | VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope, |
| 379 | const ResourceAccessRange &range) const { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 380 | BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 381 | return DetectHazard(type, detector, range); |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 382 | } |
| 383 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 384 | HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope, |
| 385 | SyncStageAccessFlags src_stage_accesses, const VkImageMemoryBarrier &barrier) const { |
| 386 | if (!SimpleBinding(image)) return HazardResult(); |
| 387 | |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 388 | auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 389 | const VulkanTypedHandle image_handle(image.image, kVulkanObjectTypeImage); |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 390 | const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask); |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 391 | subresource_adapter::ImageRangeGenerator range_gen(image.fragment_encoder, subresource_range, {0, 0, 0}, |
| 392 | image.createInfo.extent); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 393 | const auto address_type = ImageAddressType(image); |
| 394 | const auto base_address = ResourceBaseAddress(image); |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 395 | for (; range_gen->non_empty(); ++range_gen) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 396 | HazardResult hazard = DetectBarrierHazard(address_type, SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, |
| 397 | src_exec_scope, src_access_scope, (*range_gen + base_address)); |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 398 | if (hazard.hazard) return hazard; |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 399 | } |
| 400 | return HazardResult(); |
| 401 | } |
| 402 | |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 403 | template <typename Flags, typename Map> |
| 404 | SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) { |
| 405 | SyncStageAccessFlags scope = 0; |
| 406 | for (const auto &bit_scope : map) { |
| 407 | if (flag_mask < bit_scope.first) break; |
| 408 | |
| 409 | if (flag_mask & bit_scope.first) { |
| 410 | scope |= bit_scope.second; |
| 411 | } |
| 412 | } |
| 413 | return scope; |
| 414 | } |
| 415 | |
| 416 | SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) { |
| 417 | return AccessScopeImpl(stages, syncStageAccessMaskByStageBit); |
| 418 | } |
| 419 | |
| 420 | SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) { |
| 421 | return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit); |
| 422 | } |
| 423 | |
| 424 | // Getting from stage mask and access mask to stage/acess masks is something we need to be good at... |
| 425 | SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) { |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 426 | // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables |
| 427 | // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections |
| 428 | // 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] | 429 | return AccessScopeByStage(stages) & AccessScopeByAccess(accesses); |
| 430 | } |
| 431 | |
| 432 | template <typename Action> |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 433 | void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) { |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 434 | // TODO -- region/mem-range accuracte update |
| 435 | auto pos = accesses->lower_bound(range); |
| 436 | if (pos == accesses->end() || !pos->first.intersects(range)) { |
| 437 | // The range is empty, fill it with a default value. |
| 438 | pos = action.Infill(accesses, pos, range); |
| 439 | } else if (range.begin < pos->first.begin) { |
| 440 | // Leading empty space, infill |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 441 | pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin)); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 442 | } else if (pos->first.begin < range.begin) { |
| 443 | // Trim the beginning if needed |
| 444 | pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both()); |
| 445 | ++pos; |
| 446 | } |
| 447 | |
| 448 | const auto the_end = accesses->end(); |
| 449 | while ((pos != the_end) && pos->first.intersects(range)) { |
| 450 | if (pos->first.end > range.end) { |
| 451 | pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both()); |
| 452 | } |
| 453 | |
| 454 | pos = action(accesses, pos); |
| 455 | if (pos == the_end) break; |
| 456 | |
| 457 | auto next = pos; |
| 458 | ++next; |
| 459 | if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) { |
| 460 | // Need to infill if next is disjoint |
| 461 | 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] | 462 | ResourceAccessRange new_range(pos->first.end, limit); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 463 | next = action.Infill(accesses, next, new_range); |
| 464 | } |
| 465 | pos = next; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | struct UpdateMemoryAccessStateFunctor { |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 470 | using Iterator = ResourceAccessRangeMap::iterator; |
| 471 | Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 472 | // this is only called on gaps, and never returns a gap. |
| 473 | ResourceAccessState default_state; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 474 | context.ResolvePreviousAccess(type, range, accesses, &default_state); |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 475 | return accesses->lower_bound(range); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 476 | } |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 477 | |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 478 | Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const { |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 479 | auto &access_state = pos->second; |
| 480 | access_state.Update(usage, tag); |
| 481 | return pos; |
| 482 | } |
| 483 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 484 | UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_, |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 485 | const ResourceUsageTag &tag_) |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 486 | : type(type_), context(context_), usage(usage_), tag(tag_) {} |
| 487 | const AccessContext::AddressType type; |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 488 | const AccessContext &context; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 489 | const SyncStageAccessIndex usage; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 490 | const ResourceUsageTag &tag; |
| 491 | }; |
| 492 | |
| 493 | struct ApplyMemoryAccessBarrierFunctor { |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 494 | using Iterator = ResourceAccessRangeMap::iterator; |
| 495 | inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; } |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 496 | |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 497 | Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const { |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 498 | auto &access_state = pos->second; |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 499 | 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] | 500 | return pos; |
| 501 | } |
| 502 | |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 503 | ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_, |
| 504 | VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_) |
| 505 | : src_exec_scope(src_exec_scope_), |
| 506 | src_access_scope(src_access_scope_), |
| 507 | dst_exec_scope(dst_exec_scope_), |
| 508 | dst_access_scope(dst_access_scope_) {} |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 509 | |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 510 | VkPipelineStageFlags src_exec_scope; |
| 511 | SyncStageAccessFlags src_access_scope; |
| 512 | VkPipelineStageFlags dst_exec_scope; |
| 513 | SyncStageAccessFlags dst_access_scope; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 514 | }; |
| 515 | |
| 516 | struct ApplyGlobalBarrierFunctor { |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 517 | using Iterator = ResourceAccessRangeMap::iterator; |
| 518 | inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; } |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 519 | |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 520 | Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const { |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 521 | auto &access_state = pos->second; |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 522 | access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 523 | |
| 524 | for (const auto &functor : barrier_functor) { |
| 525 | functor(accesses, pos); |
| 526 | } |
| 527 | return pos; |
| 528 | } |
| 529 | |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 530 | ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope, |
| 531 | SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses, |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 532 | uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers) |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 533 | : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) { |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 534 | // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier... |
| 535 | barrier_functor.reserve(memoryBarrierCount); |
| 536 | for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) { |
| 537 | const auto &barrier = pMemoryBarriers[barrier_index]; |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 538 | barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask), |
| 539 | dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask)); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 543 | const VkPipelineStageFlags src_exec_scope; |
| 544 | const VkPipelineStageFlags dst_exec_scope; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 545 | std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor; |
| 546 | }; |
| 547 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 548 | void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 549 | const ResourceAccessRange &range, const ResourceUsageTag &tag) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 550 | UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag); |
| 551 | UpdateMemoryAccessState(&GetAccessStateMap(type), range, action); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 552 | } |
| 553 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 554 | void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, |
| 555 | const ResourceAccessRange &range, const ResourceUsageTag &tag) { |
| 556 | if (!SimpleBinding(buffer)) return; |
| 557 | const auto base_address = ResourceBaseAddress(buffer); |
| 558 | UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag); |
| 559 | } |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 560 | void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, |
| 561 | const VkImageSubresourceLayers &subresource, const VkOffset3D &offset, |
| 562 | const VkExtent3D &extent, const ResourceUsageTag &tag) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 563 | if (!SimpleBinding(image)) return; |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 564 | // TODO: replace the encoder/generator with offset3D aware versions |
| 565 | VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer, |
| 566 | subresource.layerCount}; |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 567 | subresource_adapter::ImageRangeGenerator range_gen(image.fragment_encoder, subresource_range, offset, extent); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 568 | const VulkanTypedHandle handle(image.image, kVulkanObjectTypeImage); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 569 | const auto address_type = ImageAddressType(image); |
| 570 | const auto base_address = ResourceBaseAddress(image); |
| 571 | UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag); |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 572 | for (; range_gen->non_empty(); ++range_gen) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 573 | UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action); |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 574 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 575 | } |
| 576 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 577 | template <typename Action> |
| 578 | 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] | 579 | if (!SimpleBinding(buffer)) return; |
| 580 | const auto base_address = ResourceBaseAddress(buffer); |
| 581 | UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | template <typename Action> |
| 585 | void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range, |
| 586 | const Action action) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 587 | if (!SimpleBinding(image)) return; |
| 588 | const auto address_type = ImageAddressType(image); |
| 589 | auto *accesses = &GetAccessStateMap(address_type); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 590 | |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 591 | subresource_adapter::ImageRangeGenerator range_gen(image.fragment_encoder, subresource_range, {0, 0, 0}, |
| 592 | image.createInfo.extent); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 593 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 594 | const auto base_address = ResourceBaseAddress(image); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 595 | for (; range_gen->non_empty(); ++range_gen) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 596 | UpdateMemoryAccessState(accesses, (*range_gen + base_address), action); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
| 600 | template <typename Action> |
| 601 | void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) { |
| 602 | // 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] | 603 | for (const auto address_type : kAddressTypes) { |
| 604 | UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 608 | const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = { |
| 609 | AccessContext::AddressType::kLinearAddress, |
| 610 | AccessContext::AddressType::kIdealizedAddress |
| 611 | }; |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 612 | void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 613 | for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) { |
| 614 | auto &context = contexts[subpass_index]; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 615 | for (const auto address_type : kAddressTypes) { |
| 616 | context.ResolveTrackBack(address_type, full_range, context.GetDstExternalTrackBack(), |
| 617 | &GetAccessStateMap(address_type), nullptr, false); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 622 | void CommandBufferAccessContext::BeginRenderPass(const RENDER_PASS_STATE &rp_state) { |
| 623 | // Create an access context for the first subpass and add it to the command buffers collection |
| 624 | render_pass_contexts_.emplace_back(queue_flags_, &rp_state.subpass_dependencies, &cb_tracker_context_); |
| 625 | current_renderpass_context_ = &render_pass_contexts_.back(); |
| 626 | current_context_ = ¤t_renderpass_context_->CurrentContext(); |
| 627 | |
| 628 | // TODO: Add layout load/store/transition/resolve access (here or in RenderPassContext) |
| 629 | } |
| 630 | |
| 631 | void CommandBufferAccessContext::NextRenderPass(const RENDER_PASS_STATE &rp_state) { |
| 632 | assert(current_renderpass_context_); |
| 633 | current_renderpass_context_->NextSubpass(queue_flags_, &cb_tracker_context_); |
| 634 | // TODO: Add layout load/store/transition/resolve access (here or in RenderPassContext) |
| 635 | current_context_ = ¤t_renderpass_context_->CurrentContext(); |
| 636 | } |
| 637 | |
| 638 | void CommandBufferAccessContext::EndRenderPass(const RENDER_PASS_STATE &render_pass) { |
| 639 | // TODO: Add layout load/store/transition/resolve access (here or in RenderPassContext) |
| 640 | assert(current_renderpass_context_); |
| 641 | if (!current_renderpass_context_) return; |
| 642 | |
| 643 | const auto &contexts = current_renderpass_context_->subpass_contexts_; |
| 644 | cb_tracker_context_.ResolveChildContexts(contexts); |
| 645 | |
| 646 | current_context_ = &cb_tracker_context_; |
| 647 | current_renderpass_context_ = nullptr; |
| 648 | } |
| 649 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 650 | SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) { |
| 651 | const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask); |
| 652 | src_exec_scope = WithEarlierPipelineStages(src_stage_mask); |
| 653 | src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask); |
| 654 | const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask); |
| 655 | dst_exec_scope = WithLaterPipelineStages(dst_stage_mask); |
| 656 | dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask); |
| 657 | } |
| 658 | |
| 659 | void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) { |
| 660 | ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope); |
| 661 | ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope); |
| 662 | } |
| 663 | |
| 664 | ResourceAccessState ResourceAccessState::ApplyBarrierStack(const ResourceAccessState &that, const SyncBarrierStack &barrier_stack) { |
| 665 | ResourceAccessState copy = that; |
| 666 | for (auto barrier = barrier_stack.begin(); barrier != barrier_stack.end(); ++barrier) { |
| 667 | assert(*barrier); |
| 668 | copy.ApplyBarrier(*(*barrier)); |
| 669 | } |
| 670 | return copy; |
| 671 | } |
| 672 | |
| 673 | HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, SyncBarrierStack *barrier_stack) const { |
| 674 | if (barrier_stack) { |
| 675 | return ApplyBarrierStack(*this, *barrier_stack).DetectHazard(usage_index); |
| 676 | } |
| 677 | return DetectHazard(usage_index); |
| 678 | } |
| 679 | |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 680 | HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const { |
| 681 | HazardResult hazard; |
| 682 | auto usage = FlagBit(usage_index); |
| 683 | if (IsRead(usage)) { |
| 684 | if (IsWriteHazard(usage)) { |
| 685 | hazard.Set(READ_AFTER_WRITE, write_tag); |
| 686 | } |
| 687 | } else { |
| 688 | // Assume write |
| 689 | // TODO determine what to do with READ-WRITE usage states if any |
| 690 | // Write-After-Write check -- if we have a previous write to test against |
| 691 | if (last_write && IsWriteHazard(usage)) { |
| 692 | hazard.Set(WRITE_AFTER_WRITE, write_tag); |
| 693 | } else { |
| 694 | // Only look for casus belli for WAR |
| 695 | const auto usage_stage = PipelineStageBit(usage_index); |
| 696 | for (uint32_t read_index = 0; read_index < last_read_count; read_index++) { |
| 697 | if (IsReadHazard(usage_stage, last_reads[read_index])) { |
| 698 | hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag); |
| 699 | break; |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | return hazard; |
| 705 | } |
| 706 | |
John Zulauf | 2f952d2 | 2020-02-10 11:34:51 -0700 | [diff] [blame] | 707 | // Asynchronous Hazards occur between subpasses with no connection through the DAG |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 708 | HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const { |
John Zulauf | 2f952d2 | 2020-02-10 11:34:51 -0700 | [diff] [blame] | 709 | HazardResult hazard; |
| 710 | auto usage = FlagBit(usage_index); |
| 711 | if (IsRead(usage)) { |
| 712 | if (last_write != 0) { |
| 713 | hazard.Set(READ_RACING_WRITE, write_tag); |
| 714 | } |
| 715 | } else { |
| 716 | if (last_write != 0) { |
| 717 | hazard.Set(WRITE_RACING_WRITE, write_tag); |
| 718 | } else if (last_read_count > 0) { |
| 719 | hazard.Set(WRITE_RACING_READ, last_reads[0].tag); |
| 720 | } |
| 721 | } |
| 722 | return hazard; |
| 723 | } |
| 724 | |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 725 | HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope, |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 726 | SyncStageAccessFlags src_access_scope, |
| 727 | SyncBarrierStack *barrier_stack) const { |
| 728 | if (barrier_stack) { |
| 729 | return ApplyBarrierStack(*this, *barrier_stack).DetectBarrierHazard(usage_index, src_exec_scope, src_access_scope); |
| 730 | } |
| 731 | return DetectBarrierHazard(usage_index, src_exec_scope, src_access_scope); |
| 732 | } |
| 733 | |
| 734 | HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope, |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 735 | SyncStageAccessFlags src_access_scope) const { |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 736 | // Only supporting image layout transitions for now |
| 737 | assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION); |
| 738 | HazardResult hazard; |
| 739 | if (last_write) { |
| 740 | // If the previous write is *not* in the 1st access scope |
| 741 | // *AND* the current barrier is not in the dependency chain |
| 742 | // *AND* the there is no prior memory barrier for the previous write in the dependency chain |
| 743 | // then the barrier access is unsafe (R/W after W) |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 744 | 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] | 745 | // TODO: Do we need a difference hazard name for this? |
| 746 | hazard.Set(WRITE_AFTER_WRITE, write_tag); |
| 747 | } |
| 748 | } else { |
| 749 | // Look at the reads |
| 750 | 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] | 751 | const auto &read_access = last_reads[read_index]; |
| 752 | // If the read stage is not in the src sync sync |
| 753 | // *AND* not execution chained with an existing sync barrier (that's the or) |
| 754 | // then the barrier access is unsafe (R/W after R) |
| 755 | if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) { |
| 756 | hazard.Set(WRITE_AFTER_READ, read_access.tag); |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 757 | break; |
| 758 | } |
| 759 | } |
| 760 | } |
| 761 | return hazard; |
| 762 | } |
| 763 | |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 764 | // The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no |
| 765 | // tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another |
| 766 | // exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones. |
| 767 | void ResourceAccessState::Resolve(const ResourceAccessState &other) { |
| 768 | if (write_tag.IsBefore(other.write_tag)) { |
| 769 | // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation |
| 770 | *this = other; |
| 771 | } else if (!other.write_tag.IsBefore(write_tag)) { |
| 772 | // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the |
| 773 | // dependency chaining logic or any stage expansion) |
| 774 | write_barriers |= other.write_barriers; |
| 775 | |
| 776 | // Merge that read states |
| 777 | for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) { |
| 778 | auto &other_read = other.last_reads[other_read_index]; |
| 779 | if (last_read_stages & other_read.stage) { |
| 780 | // Merge in the barriers for read stages that exist in *both* this and other |
| 781 | // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index. |
| 782 | for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) { |
| 783 | auto &my_read = last_reads[my_read_index]; |
| 784 | if (other_read.stage == my_read.stage) { |
| 785 | if (my_read.tag.IsBefore(other_read.tag)) { |
| 786 | my_read.tag = other_read.tag; |
| 787 | } |
| 788 | my_read.barriers |= other_read.barriers; |
| 789 | break; |
| 790 | } |
| 791 | } |
| 792 | } else { |
| 793 | // The other read stage doesn't exist in this, so add it. |
| 794 | last_reads[last_read_count] = other_read; |
| 795 | last_read_count++; |
| 796 | last_read_stages |= other_read.stage; |
| 797 | } |
| 798 | } |
| 799 | } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore |
| 800 | // it. |
| 801 | } |
| 802 | |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 803 | void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) { |
| 804 | // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource... |
| 805 | const auto usage_bit = FlagBit(usage_index); |
| 806 | if (IsRead(usage_index)) { |
| 807 | // Mulitple outstanding reads may be of interest and do dependency chains independently |
| 808 | // However, for purposes of barrier tracking, only one read per pipeline stage matters |
| 809 | const auto usage_stage = PipelineStageBit(usage_index); |
| 810 | if (usage_stage & last_read_stages) { |
| 811 | for (uint32_t read_index = 0; read_index < last_read_count; read_index++) { |
| 812 | ReadState &access = last_reads[read_index]; |
| 813 | if (access.stage == usage_stage) { |
| 814 | access.barriers = 0; |
| 815 | access.tag = tag; |
| 816 | break; |
| 817 | } |
| 818 | } |
| 819 | } else { |
| 820 | // We don't have this stage in the list yet... |
| 821 | assert(last_read_count < last_reads.size()); |
| 822 | ReadState &access = last_reads[last_read_count++]; |
| 823 | access.stage = usage_stage; |
| 824 | access.barriers = 0; |
| 825 | access.tag = tag; |
| 826 | last_read_stages |= usage_stage; |
| 827 | } |
| 828 | } else { |
| 829 | // Assume write |
| 830 | // TODO determine what to do with READ-WRITE operations if any |
| 831 | // Clobber last read and both sets of barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!! |
| 832 | // if the last_reads/last_write were unsafe, we've reported them, |
| 833 | // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them |
| 834 | last_read_count = 0; |
| 835 | last_read_stages = 0; |
| 836 | |
| 837 | write_barriers = 0; |
| 838 | write_dependency_chain = 0; |
| 839 | write_tag = tag; |
| 840 | last_write = usage_bit; |
| 841 | } |
| 842 | } |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 843 | |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 844 | void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) { |
| 845 | // Execution Barriers only protect read operations |
| 846 | for (uint32_t read_index = 0; read_index < last_read_count; read_index++) { |
| 847 | ReadState &access = last_reads[read_index]; |
| 848 | // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope |
| 849 | if (srcStageMask & (access.stage | access.barriers)) { |
| 850 | access.barriers |= dstStageMask; |
| 851 | } |
| 852 | } |
| 853 | if (write_dependency_chain & srcStageMask) write_dependency_chain |= dstStageMask; |
| 854 | } |
| 855 | |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 856 | void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope, |
| 857 | VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) { |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 858 | // Assuming we've applied the execution side of this barrier, we update just the write |
| 859 | // The || implements the "dependency chain" logic for this barrier |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 860 | if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) { |
| 861 | write_barriers |= dst_access_scope; |
| 862 | write_dependency_chain |= dst_exec_scope; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | |
| 866 | void SyncValidator::ResetCommandBuffer(VkCommandBuffer command_buffer) { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 867 | auto *access_context = GetAccessContextNoInsert(command_buffer); |
| 868 | if (access_context) { |
| 869 | access_context->Reset(); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 870 | } |
| 871 | } |
| 872 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 873 | void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask, |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 874 | VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope, |
| 875 | SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount, |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 876 | const VkMemoryBarrier *pMemoryBarriers) { |
| 877 | // TODO: Implement this better (maybe some delayed/on-demand integration). |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 878 | ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount, |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 879 | pMemoryBarriers); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 880 | context->ApplyGlobalBarriers(barriers_functor); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 881 | } |
| 882 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 883 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 884 | void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope, |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 885 | SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope, |
| 886 | SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count, |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 887 | const VkBufferMemoryBarrier *barriers) { |
| 888 | // TODO Implement this at subresource/memory_range accuracy |
| 889 | for (uint32_t index = 0; index < barrier_count; index++) { |
| 890 | const auto &barrier = barriers[index]; |
| 891 | const auto *buffer = Get<BUFFER_STATE>(barrier.buffer); |
| 892 | if (!buffer) continue; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 893 | ResourceAccessRange range = MakeRange(barrier); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 894 | const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask); |
| 895 | const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask); |
| 896 | const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope); |
| 897 | context->UpdateMemoryAccess(*buffer, range, update_action); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 901 | void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope, |
| 902 | SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope, |
| 903 | SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count, |
| 904 | const VkImageMemoryBarrier *barriers) { |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 905 | for (uint32_t index = 0; index < barrier_count; index++) { |
| 906 | const auto &barrier = barriers[index]; |
| 907 | const auto *image = Get<IMAGE_STATE>(barrier.image); |
| 908 | if (!image) continue; |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 909 | const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, AccessScope(src_stage_accesses, barrier.srcAccessMask), |
| 910 | dst_exec_scope, |
| 911 | AccessScope(dst_stage_accesses, barrier.dstAccessMask)); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 912 | |
| 913 | auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange); |
| 914 | context->UpdateMemoryAccess(*image, subresource_range, barrier_action); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 915 | } |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer, |
| 919 | uint32_t regionCount, const VkBufferCopy *pRegions) const { |
| 920 | bool skip = false; |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 921 | const auto *cb_context = GetAccessContext(commandBuffer); |
| 922 | assert(cb_context); |
| 923 | if (!cb_context) return skip; |
| 924 | const auto *context = cb_context->GetCurrentAccessContext(); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 925 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 926 | // If we have no previous accesses, we have no hazards |
| 927 | // TODO: make this sub-resource capable |
| 928 | // TODO: make this general, and stuff it into templates/utility functions |
| 929 | const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 930 | const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 931 | |
| 932 | for (uint32_t region = 0; region < regionCount; region++) { |
| 933 | const auto ©_region = pRegions[region]; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 934 | if (src_buffer) { |
| 935 | ResourceAccessRange src_range = MakeRange(copy_region.srcOffset, copy_region.size); |
| 936 | auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 937 | if (hazard.hazard) { |
| 938 | // TODO -- add tag information to log msg when useful. |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 939 | skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard), |
| 940 | "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard), |
| 941 | report_data->FormatHandle(srcBuffer).c_str(), region); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 942 | } |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 943 | } |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 944 | if (dst_buffer && !skip) { |
| 945 | ResourceAccessRange dst_range = MakeRange(copy_region.dstOffset, copy_region.size); |
| 946 | auto hazard = context->DetectHazard(*dst_buffer, |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 947 | SYNC_TRANSFER_TRANSFER_WRITE, dst_range); |
| 948 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 949 | skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), |
| 950 | "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard), |
| 951 | report_data->FormatHandle(dstBuffer).c_str(), region); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 952 | } |
| 953 | } |
| 954 | if (skip) break; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 955 | } |
| 956 | return skip; |
| 957 | } |
| 958 | |
| 959 | void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer, |
| 960 | uint32_t regionCount, const VkBufferCopy *pRegions) { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 961 | auto *cb_context = GetAccessContext(commandBuffer); |
| 962 | assert(cb_context); |
| 963 | auto *context = cb_context->GetCurrentAccessContext(); |
| 964 | |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 965 | const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 966 | const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 967 | |
| 968 | for (uint32_t region = 0; region < regionCount; region++) { |
| 969 | const auto ©_region = pRegions[region]; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 970 | if (src_buffer) { |
| 971 | ResourceAccessRange src_range = MakeRange(copy_region.srcOffset, copy_region.size); |
| 972 | context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 973 | } |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 974 | if (dst_buffer) { |
| 975 | ResourceAccessRange dst_range = MakeRange(copy_region.dstOffset, copy_region.size); |
| 976 | context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 982 | VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, |
| 983 | const VkImageCopy *pRegions) const { |
| 984 | bool skip = false; |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 985 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 986 | assert(cb_access_context); |
| 987 | if (!cb_access_context) return skip; |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 988 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 989 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 990 | assert(context); |
| 991 | if (!context) return skip; |
| 992 | |
| 993 | const auto *src_image = Get<IMAGE_STATE>(srcImage); |
| 994 | const auto *dst_image = Get<IMAGE_STATE>(dstImage); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 995 | for (uint32_t region = 0; region < regionCount; region++) { |
| 996 | const auto ©_region = pRegions[region]; |
| 997 | if (src_image) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 998 | 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] | 999 | copy_region.srcOffset, copy_region.extent); |
| 1000 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1001 | skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard), |
| 1002 | "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard), |
| 1003 | report_data->FormatHandle(srcImage).c_str(), region); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1004 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | if (dst_image) { |
locke-lunarg | 1df1f88 | 2020-03-02 16:42:08 -0700 | [diff] [blame] | 1008 | VkExtent3D dst_copy_extent = |
| 1009 | GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1010 | 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] | 1011 | copy_region.dstOffset, dst_copy_extent); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1012 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1013 | skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard), |
| 1014 | "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard), |
| 1015 | report_data->FormatHandle(dstImage).c_str(), region); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1016 | } |
locke-lunarg | 1dbbb9e | 2020-02-28 22:43:53 -0700 | [diff] [blame] | 1017 | if (skip) break; |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1018 | } |
| 1019 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1020 | |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1021 | return skip; |
| 1022 | } |
| 1023 | |
| 1024 | void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 1025 | VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, |
| 1026 | const VkImageCopy *pRegions) { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1027 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1028 | assert(cb_access_context); |
| 1029 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 1030 | assert(context); |
| 1031 | |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1032 | auto *src_image = Get<IMAGE_STATE>(srcImage); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1033 | auto *dst_image = Get<IMAGE_STATE>(dstImage); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1034 | |
| 1035 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1036 | const auto ©_region = pRegions[region]; |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1037 | if (src_image) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1038 | context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset, |
| 1039 | copy_region.extent, tag); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1040 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1041 | if (dst_image) { |
locke-lunarg | 1df1f88 | 2020-03-02 16:42:08 -0700 | [diff] [blame] | 1042 | VkExtent3D dst_copy_extent = |
| 1043 | GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1044 | context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset, |
| 1045 | dst_copy_extent, tag); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask, |
| 1051 | VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags, |
| 1052 | uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers, |
| 1053 | uint32_t bufferMemoryBarrierCount, |
| 1054 | const VkBufferMemoryBarrier *pBufferMemoryBarriers, |
| 1055 | uint32_t imageMemoryBarrierCount, |
| 1056 | const VkImageMemoryBarrier *pImageMemoryBarriers) const { |
| 1057 | bool skip = false; |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1058 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1059 | assert(cb_access_context); |
| 1060 | if (!cb_access_context) return skip; |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 1061 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1062 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 1063 | assert(context); |
| 1064 | if (!context) return skip; |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 1065 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1066 | const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask); |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1067 | const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask); |
| 1068 | auto src_stage_accesses = AccessScopeByStage(src_stage_mask); |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 1069 | // Validate Image Layout transitions |
| 1070 | for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) { |
| 1071 | const auto &barrier = pImageMemoryBarriers[index]; |
| 1072 | if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point. |
| 1073 | const auto *image_state = Get<IMAGE_STATE>(barrier.image); |
| 1074 | if (!image_state) continue; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1075 | 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] | 1076 | if (hazard.hazard) { |
| 1077 | // TODO -- add tag information to log msg when useful. |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1078 | skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard), |
| 1079 | "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s", string_SyncHazard(hazard.hazard), |
| 1080 | index, report_data->FormatHandle(barrier.image).c_str()); |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 1081 | } |
| 1082 | } |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1083 | |
| 1084 | return skip; |
| 1085 | } |
| 1086 | |
| 1087 | void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask, |
| 1088 | VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags, |
| 1089 | uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers, |
| 1090 | uint32_t bufferMemoryBarrierCount, |
| 1091 | const VkBufferMemoryBarrier *pBufferMemoryBarriers, |
| 1092 | uint32_t imageMemoryBarrierCount, |
| 1093 | const VkImageMemoryBarrier *pImageMemoryBarriers) { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1094 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1095 | assert(cb_access_context); |
| 1096 | if (!cb_access_context) return; |
| 1097 | auto access_context = cb_access_context->GetCurrentAccessContext(); |
| 1098 | assert(access_context); |
| 1099 | if (!access_context) return; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1100 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1101 | const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask); |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1102 | auto src_stage_accesses = AccessScopeByStage(src_stage_mask); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1103 | const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask); |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1104 | auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask); |
| 1105 | const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask); |
| 1106 | const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1107 | ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses, |
| 1108 | bufferMemoryBarrierCount, pBufferMemoryBarriers); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1109 | ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses, |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1110 | imageMemoryBarrierCount, pImageMemoryBarriers); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1111 | |
| 1112 | // 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] | 1113 | 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] | 1114 | pMemoryBarriers); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo, |
| 1118 | const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) { |
| 1119 | // The state tracker sets up the device state |
| 1120 | StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result); |
| 1121 | |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 1122 | // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker |
| 1123 | // refactor would be messier without. |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1124 | // TODO: Find a good way to do this hooklessly. |
| 1125 | ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map); |
| 1126 | ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation); |
| 1127 | SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data); |
| 1128 | |
| 1129 | sync_device_state->SetCommandBufferResetCallback( |
| 1130 | [sync_device_state](VkCommandBuffer command_buffer) -> void { sync_device_state->ResetCommandBuffer(command_buffer); }); |
| 1131 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1132 | |
| 1133 | void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo, |
| 1134 | VkResult result) { |
| 1135 | // The state tracker sets up the command buffer state |
| 1136 | StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result); |
| 1137 | |
| 1138 | // Create/initialize the structure that trackers accesses at the command buffer scope. |
| 1139 | auto cb_access_context = GetAccessContext(commandBuffer); |
| 1140 | assert(cb_access_context); |
| 1141 | cb_access_context->Reset(); |
| 1142 | } |
| 1143 | |
| 1144 | void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin, |
| 1145 | const VkSubpassBeginInfo *pSubpassBeginInfo) { |
| 1146 | const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass); |
| 1147 | auto cb_context = GetAccessContext(commandBuffer); |
| 1148 | if (rp_state && cb_context) { |
| 1149 | cb_context->BeginRenderPass(*rp_state); |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin, |
| 1154 | VkSubpassContents contents) { |
| 1155 | StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents); |
| 1156 | auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>(); |
| 1157 | subpass_begin_info.contents = contents; |
| 1158 | RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info); |
| 1159 | } |
| 1160 | |
| 1161 | void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin, |
| 1162 | const VkSubpassBeginInfo *pSubpassBeginInfo) { |
| 1163 | StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo); |
| 1164 | RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo); |
| 1165 | } |
| 1166 | |
| 1167 | void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer, |
| 1168 | const VkRenderPassBeginInfo *pRenderPassBegin, |
| 1169 | const VkSubpassBeginInfo *pSubpassBeginInfo) { |
| 1170 | StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo); |
| 1171 | RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo); |
| 1172 | } |
| 1173 | |
| 1174 | void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo, |
| 1175 | const VkSubpassEndInfo *pSubpassEndInfo) { |
| 1176 | auto cb_context = GetAccessContext(commandBuffer); |
| 1177 | assert(cb_context); |
| 1178 | auto cb_state = cb_context->GetCommandBufferState(); |
| 1179 | if (!cb_state) return; |
| 1180 | |
| 1181 | auto rp_state = cb_state->activeRenderPass; |
| 1182 | if (!rp_state) return; |
| 1183 | |
| 1184 | cb_context->NextRenderPass(*rp_state); |
| 1185 | } |
| 1186 | |
| 1187 | void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) { |
| 1188 | StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents); |
| 1189 | auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>(); |
| 1190 | subpass_begin_info.contents = contents; |
| 1191 | RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr); |
| 1192 | } |
| 1193 | |
| 1194 | void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo, |
| 1195 | const VkSubpassEndInfo *pSubpassEndInfo) { |
| 1196 | StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo); |
| 1197 | RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo); |
| 1198 | } |
| 1199 | |
| 1200 | void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo, |
| 1201 | const VkSubpassEndInfo *pSubpassEndInfo) { |
| 1202 | StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo); |
| 1203 | RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo); |
| 1204 | } |
| 1205 | |
John Zulauf | e5da6e5 | 2020-03-18 15:32:18 -0600 | [diff] [blame] | 1206 | void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) { |
| 1207 | // Resolve the all subpass contexts to the command buffer contexts |
| 1208 | auto cb_context = GetAccessContext(commandBuffer); |
| 1209 | assert(cb_context); |
| 1210 | auto cb_state = cb_context->GetCommandBufferState(); |
| 1211 | if (!cb_state) return; |
| 1212 | |
| 1213 | const auto *rp_state = cb_state->activeRenderPass; |
| 1214 | if (!rp_state) return; |
| 1215 | |
| 1216 | cb_context->EndRenderPass(*rp_state); |
| 1217 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1218 | |
| 1219 | void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) { |
| 1220 | StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer); |
| 1221 | RecordCmdEndRenderPass(commandBuffer, nullptr); |
| 1222 | } |
| 1223 | |
| 1224 | void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) { |
| 1225 | StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo); |
| 1226 | RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo); |
| 1227 | } |
| 1228 | |
| 1229 | void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) { |
| 1230 | StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo); |
| 1231 | RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo); |
| 1232 | } |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1233 | |
| 1234 | bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage, |
| 1235 | VkImageLayout dstImageLayout, uint32_t regionCount, |
| 1236 | const VkBufferImageCopy *pRegions) const { |
| 1237 | bool skip = false; |
| 1238 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1239 | assert(cb_access_context); |
| 1240 | if (!cb_access_context) return skip; |
| 1241 | |
| 1242 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 1243 | assert(context); |
| 1244 | if (!context) return skip; |
| 1245 | |
| 1246 | const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1247 | const auto *dst_image = Get<IMAGE_STATE>(dstImage); |
| 1248 | |
| 1249 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1250 | const auto ©_region = pRegions[region]; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1251 | if (src_buffer) { |
| 1252 | ResourceAccessRange src_range = MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format)); |
| 1253 | auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1254 | if (hazard.hazard) { |
| 1255 | // TODO -- add tag information to log msg when useful. |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1256 | skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard), |
| 1257 | "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32, |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1258 | string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region); |
| 1259 | } |
| 1260 | } |
| 1261 | if (dst_image) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1262 | 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] | 1263 | copy_region.imageOffset, copy_region.imageExtent); |
| 1264 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1265 | skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard), |
| 1266 | "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32, |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1267 | string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region); |
| 1268 | } |
| 1269 | if (skip) break; |
| 1270 | } |
| 1271 | if (skip) break; |
| 1272 | } |
| 1273 | return skip; |
| 1274 | } |
| 1275 | |
| 1276 | void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage, |
| 1277 | VkImageLayout dstImageLayout, uint32_t regionCount, |
| 1278 | const VkBufferImageCopy *pRegions) { |
| 1279 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1280 | assert(cb_access_context); |
| 1281 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 1282 | assert(context); |
| 1283 | |
| 1284 | const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1285 | const auto *dst_image = Get<IMAGE_STATE>(dstImage); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1286 | |
| 1287 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1288 | const auto ©_region = pRegions[region]; |
| 1289 | if (src_buffer) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1290 | ResourceAccessRange src_range = MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format)); |
| 1291 | context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1292 | } |
| 1293 | if (dst_image) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1294 | context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource, |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 1295 | copy_region.imageOffset, copy_region.imageExtent, tag); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, |
| 1301 | VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount, |
| 1302 | const VkBufferImageCopy *pRegions) const { |
| 1303 | bool skip = false; |
| 1304 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1305 | assert(cb_access_context); |
| 1306 | if (!cb_access_context) return skip; |
| 1307 | |
| 1308 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 1309 | assert(context); |
| 1310 | if (!context) return skip; |
| 1311 | |
| 1312 | const auto *src_image = Get<IMAGE_STATE>(srcImage); |
| 1313 | const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer); |
| 1314 | const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE; |
| 1315 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1316 | const auto ©_region = pRegions[region]; |
| 1317 | if (src_image) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1318 | 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] | 1319 | copy_region.imageOffset, copy_region.imageExtent); |
| 1320 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1321 | skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard), |
| 1322 | "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32, |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1323 | string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region); |
| 1324 | } |
| 1325 | } |
| 1326 | if (dst_mem) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1327 | ResourceAccessRange dst_range = MakeRange( copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format)); |
| 1328 | auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1329 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1330 | skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), |
| 1331 | "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32, |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1332 | string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region); |
| 1333 | } |
| 1334 | } |
| 1335 | if (skip) break; |
| 1336 | } |
| 1337 | return skip; |
| 1338 | } |
| 1339 | |
| 1340 | void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 1341 | VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) { |
| 1342 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1343 | assert(cb_access_context); |
| 1344 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 1345 | assert(context); |
| 1346 | |
| 1347 | const auto *src_image = Get<IMAGE_STATE>(srcImage); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1348 | auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer); |
| 1349 | 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] | 1350 | const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1351 | |
| 1352 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1353 | const auto ©_region = pRegions[region]; |
| 1354 | if (src_image) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1355 | context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource, |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 1356 | copy_region.imageOffset, copy_region.imageExtent, tag); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1357 | } |
| 1358 | if (dst_buffer) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1359 | ResourceAccessRange dst_range = MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format)); |
| 1360 | context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 1366 | VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, |
| 1367 | const VkImageBlit *pRegions, VkFilter filter) const { |
| 1368 | bool skip = false; |
| 1369 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1370 | assert(cb_access_context); |
| 1371 | if (!cb_access_context) return skip; |
| 1372 | |
| 1373 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 1374 | assert(context); |
| 1375 | if (!context) return skip; |
| 1376 | |
| 1377 | const auto *src_image = Get<IMAGE_STATE>(srcImage); |
| 1378 | const auto *dst_image = Get<IMAGE_STATE>(dstImage); |
| 1379 | |
| 1380 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1381 | const auto &blit_region = pRegions[region]; |
| 1382 | if (src_image) { |
| 1383 | VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x), |
| 1384 | static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y), |
| 1385 | 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] | 1386 | 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] | 1387 | blit_region.srcOffsets[0], extent); |
| 1388 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1389 | skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard), |
| 1390 | "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard), |
| 1391 | report_data->FormatHandle(srcImage).c_str(), region); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | if (dst_image) { |
| 1396 | VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x), |
| 1397 | static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y), |
| 1398 | 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] | 1399 | 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] | 1400 | blit_region.dstOffsets[0], extent); |
| 1401 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1402 | skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard), |
| 1403 | "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard), |
| 1404 | report_data->FormatHandle(dstImage).c_str(), region); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1405 | } |
| 1406 | if (skip) break; |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | return skip; |
| 1411 | } |
| 1412 | |
| 1413 | void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 1414 | VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, |
| 1415 | const VkImageBlit *pRegions, VkFilter filter) { |
| 1416 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1417 | assert(cb_access_context); |
| 1418 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 1419 | assert(context); |
| 1420 | |
| 1421 | auto *src_image = Get<IMAGE_STATE>(srcImage); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1422 | auto *dst_image = Get<IMAGE_STATE>(dstImage); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1423 | |
| 1424 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1425 | const auto &blit_region = pRegions[region]; |
| 1426 | if (src_image) { |
| 1427 | VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x), |
| 1428 | static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y), |
| 1429 | 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] | 1430 | context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 1431 | blit_region.srcOffsets[0], extent, tag); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1432 | } |
| 1433 | if (dst_image) { |
| 1434 | VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x), |
| 1435 | static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y), |
| 1436 | 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] | 1437 | context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 1438 | blit_region.dstOffsets[0], extent, tag); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 1439 | } |
| 1440 | } |
| 1441 | } |