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