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); |
John Zulauf | aff2066 | 2020-06-01 14:07:58 -0600 | [diff] [blame] | 516 | proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag); |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 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; |
John Zulauf | 1507ee4 | 2020-05-18 11:33:09 -0600 | [diff] [blame] | 582 | |
| 583 | for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) { |
| 584 | if (subpass == rp_state.attachment_first_subpass[i]) { |
| 585 | if (attachment_views[i] == nullptr) continue; |
| 586 | const IMAGE_VIEW_STATE &view = *attachment_views[i]; |
| 587 | const IMAGE_STATE *image = view.image_state.get(); |
| 588 | if (image == nullptr) continue; |
| 589 | const auto &ci = attachment_ci[i]; |
| 590 | const bool is_transition = rp_state.attachment_first_is_transition[i]; |
| 591 | |
| 592 | // Need check in the following way |
| 593 | // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard |
| 594 | // vs. transition |
| 595 | // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation |
| 596 | // for each aspect loaded. |
| 597 | |
| 598 | const bool has_depth = FormatHasDepth(ci.format); |
John Zulauf | b027cdb | 2020-05-21 14:25:22 -0600 | [diff] [blame] | 599 | const bool has_stencil = FormatHasStencil(ci.format); |
John Zulauf | 1507ee4 | 2020-05-18 11:33:09 -0600 | [diff] [blame] | 600 | const bool is_color = !(has_depth || has_stencil); |
| 601 | |
| 602 | const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp); |
| 603 | const SyncStageAccessFlags load_mask = (has_depth || is_color) ? SyncStageAccess::Flags(load_index) : 0U; |
| 604 | const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index; |
| 605 | const SyncStageAccessFlags stencil_mask = has_stencil ? SyncStageAccess::Flags(stencil_load_index) : 0U; |
| 606 | |
John Zulauf | aff2066 | 2020-06-01 14:07:58 -0600 | [diff] [blame] | 607 | HazardResult hazard; |
John Zulauf | 1507ee4 | 2020-05-18 11:33:09 -0600 | [diff] [blame] | 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 | aff2066 | 2020-06-01 14:07:58 -0600 | [diff] [blame] | 670 | // Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored |
| 671 | // because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because |
| 672 | // store is part of the same Next/End operation. |
| 673 | // The latter is handled in layout transistion validation directly |
| 674 | bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state, |
| 675 | const VkRect2D &render_area, uint32_t subpass, |
| 676 | const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, |
| 677 | const char *func_name) const { |
| 678 | bool skip = false; |
| 679 | const auto *attachment_ci = rp_state.createInfo.pAttachments; |
| 680 | VkExtent3D extent = CastTo3D(render_area.extent); |
| 681 | VkOffset3D offset = CastTo3D(render_area.offset); |
| 682 | |
| 683 | for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) { |
| 684 | if (subpass == rp_state.attachment_last_subpass[i]) { |
| 685 | if (attachment_views[i] == nullptr) continue; |
| 686 | const IMAGE_VIEW_STATE &view = *attachment_views[i]; |
| 687 | const IMAGE_STATE *image = view.image_state.get(); |
| 688 | if (image == nullptr) continue; |
| 689 | const auto &ci = attachment_ci[i]; |
| 690 | |
| 691 | // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, |
| 692 | // so we assume that an implementation is *free* to write in that case, meaning that for correctness |
| 693 | // sake, we treat DONT_CARE as writing. |
| 694 | const bool has_depth = FormatHasDepth(ci.format); |
| 695 | const bool has_stencil = FormatHasStencil(ci.format); |
| 696 | const bool is_color = !(has_depth || has_stencil); |
| 697 | const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM; |
| 698 | if (!has_stencil && !store_op_stores) continue; |
| 699 | |
| 700 | HazardResult hazard; |
| 701 | const char *aspect = nullptr; |
| 702 | bool checked_stencil = false; |
| 703 | if (is_color) { |
| 704 | hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, |
| 705 | view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent); |
| 706 | aspect = "color"; |
| 707 | } else { |
| 708 | const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM; |
| 709 | auto hazard_range = view.normalized_subresource_range; |
| 710 | if (has_depth && store_op_stores) { |
| 711 | hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; |
| 712 | hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range, |
| 713 | kAttachmentRasterOrder, offset, extent); |
| 714 | aspect = "depth"; |
| 715 | } |
| 716 | if (!hazard.hazard && has_stencil && stencil_op_stores) { |
| 717 | hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 718 | hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range, |
| 719 | kAttachmentRasterOrder, offset, extent); |
| 720 | aspect = "stencil"; |
| 721 | checked_stencil = true; |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | if (hazard.hazard) { |
| 726 | const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp"; |
| 727 | const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp); |
| 728 | skip |= sync_state.LogError( |
| 729 | rp_state.renderPass, string_SyncHazardVUID(hazard.hazard), |
| 730 | "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " %s aspect during store with %s %s.", func_name, |
| 731 | string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string, store_op_string); |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | return skip; |
| 736 | } |
| 737 | |
John Zulauf | b027cdb | 2020-05-21 14:25:22 -0600 | [diff] [blame] | 738 | bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state, |
| 739 | const VkRect2D &render_area, |
| 740 | const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name, |
| 741 | uint32_t subpass) const { |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 742 | ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name); |
| 743 | ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass); |
| 744 | return validate_action.GetSkip(); |
John Zulauf | b027cdb | 2020-05-21 14:25:22 -0600 | [diff] [blame] | 745 | } |
| 746 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 747 | class HazardDetector { |
| 748 | SyncStageAccessIndex usage_index_; |
| 749 | |
| 750 | public: |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 751 | 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] | 752 | HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const { |
| 753 | return pos->second.DetectAsyncHazard(usage_index_); |
| 754 | } |
| 755 | HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {} |
| 756 | }; |
| 757 | |
John Zulauf | 6913342 | 2020-05-20 14:55:53 -0600 | [diff] [blame] | 758 | class HazardDetectorWithOrdering { |
| 759 | const SyncStageAccessIndex usage_index_; |
| 760 | const SyncOrderingBarrier &ordering_; |
| 761 | |
| 762 | public: |
| 763 | HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { |
| 764 | return pos->second.DetectHazard(usage_index_, ordering_); |
| 765 | } |
| 766 | HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const { |
| 767 | return pos->second.DetectAsyncHazard(usage_index_); |
| 768 | } |
| 769 | HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering) |
| 770 | : usage_index_(usage), ordering_(ordering) {} |
| 771 | }; |
| 772 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 773 | HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index, |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 774 | const ResourceAccessRange &range) const { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 775 | HazardDetector detector(usage_index); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 776 | return DetectHazard(type, detector, range, DetectOptions::kDetectAll); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 777 | } |
| 778 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 779 | HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index, |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 780 | const ResourceAccessRange &range) const { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 781 | if (!SimpleBinding(buffer)) return HazardResult(); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 782 | return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer)); |
John Zulauf | e5da6e5 | 2020-03-18 15:32:18 -0600 | [diff] [blame] | 783 | } |
| 784 | |
John Zulauf | 6913342 | 2020-05-20 14:55:53 -0600 | [diff] [blame] | 785 | template <typename Detector> |
| 786 | HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image, |
| 787 | const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset, |
| 788 | const VkExtent3D &extent, DetectOptions options) const { |
| 789 | if (!SimpleBinding(image)) return HazardResult(); |
| 790 | subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent); |
| 791 | const auto address_type = ImageAddressType(image); |
| 792 | const auto base_address = ResourceBaseAddress(image); |
| 793 | for (; range_gen->non_empty(); ++range_gen) { |
| 794 | HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options); |
| 795 | if (hazard.hazard) return hazard; |
| 796 | } |
| 797 | return HazardResult(); |
| 798 | } |
| 799 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 800 | HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, |
| 801 | const VkImageSubresourceLayers &subresource, const VkOffset3D &offset, |
| 802 | const VkExtent3D &extent) const { |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 803 | VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer, |
| 804 | subresource.layerCount}; |
John Zulauf | 1507ee4 | 2020-05-18 11:33:09 -0600 | [diff] [blame] | 805 | return DetectHazard(image, current_usage, subresource_range, offset, extent); |
| 806 | } |
| 807 | |
| 808 | HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, |
| 809 | const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset, |
| 810 | const VkExtent3D &extent) const { |
John Zulauf | 6913342 | 2020-05-20 14:55:53 -0600 | [diff] [blame] | 811 | HazardDetector detector(current_usage); |
| 812 | return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll); |
| 813 | } |
| 814 | |
| 815 | HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, |
| 816 | const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering, |
| 817 | const VkOffset3D &offset, const VkExtent3D &extent) const { |
| 818 | HazardDetectorWithOrdering detector(current_usage, ordering); |
| 819 | return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 820 | } |
| 821 | |
John Zulauf | b027cdb | 2020-05-21 14:25:22 -0600 | [diff] [blame] | 822 | // Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation |
| 823 | // should have reported the issue regarding an invalid attachment entry |
| 824 | HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, |
| 825 | const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent, |
| 826 | VkImageAspectFlags aspect_mask) const { |
| 827 | if (view != nullptr) { |
| 828 | const IMAGE_STATE *image = view->image_state.get(); |
| 829 | if (image != nullptr) { |
| 830 | auto *detect_range = &view->normalized_subresource_range; |
| 831 | VkImageSubresourceRange masked_range; |
| 832 | if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask |
| 833 | masked_range = view->normalized_subresource_range; |
| 834 | masked_range.aspectMask = aspect_mask & masked_range.aspectMask; |
| 835 | detect_range = &masked_range; |
| 836 | } |
| 837 | |
| 838 | // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change |
| 839 | if (detect_range->aspectMask) { |
| 840 | return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent); |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | return HazardResult(); |
| 845 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 846 | class BarrierHazardDetector { |
| 847 | public: |
| 848 | BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope, |
| 849 | SyncStageAccessFlags src_access_scope) |
| 850 | : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {} |
| 851 | |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 852 | HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { |
| 853 | return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_); |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 854 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 855 | HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const { |
| 856 | // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite |
| 857 | return pos->second.DetectAsyncHazard(usage_index_); |
| 858 | } |
| 859 | |
| 860 | private: |
| 861 | SyncStageAccessIndex usage_index_; |
| 862 | VkPipelineStageFlags src_exec_scope_; |
| 863 | SyncStageAccessFlags src_access_scope_; |
| 864 | }; |
| 865 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 866 | HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage, |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 867 | VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope, |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 868 | const ResourceAccessRange &range, DetectOptions options) const { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 869 | BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope); |
John Zulauf | 6913342 | 2020-05-20 14:55:53 -0600 | [diff] [blame] | 870 | return DetectHazard(type, detector, range, options); |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 871 | } |
| 872 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 873 | HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope, |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 874 | SyncStageAccessFlags src_access_scope, |
| 875 | const VkImageSubresourceRange &subresource_range, |
| 876 | DetectOptions options) const { |
John Zulauf | 6913342 | 2020-05-20 14:55:53 -0600 | [diff] [blame] | 877 | BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope); |
| 878 | VkOffset3D zero_offset = {0, 0, 0}; |
| 879 | return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options); |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 880 | } |
| 881 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 882 | HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope, |
| 883 | SyncStageAccessFlags src_stage_accesses, |
| 884 | const VkImageMemoryBarrier &barrier) const { |
| 885 | auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange); |
| 886 | const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask); |
| 887 | return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll); |
| 888 | } |
| 889 | |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 890 | template <typename Flags, typename Map> |
| 891 | SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) { |
| 892 | SyncStageAccessFlags scope = 0; |
| 893 | for (const auto &bit_scope : map) { |
| 894 | if (flag_mask < bit_scope.first) break; |
| 895 | |
| 896 | if (flag_mask & bit_scope.first) { |
| 897 | scope |= bit_scope.second; |
| 898 | } |
| 899 | } |
| 900 | return scope; |
| 901 | } |
| 902 | |
| 903 | SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) { |
| 904 | return AccessScopeImpl(stages, syncStageAccessMaskByStageBit); |
| 905 | } |
| 906 | |
| 907 | SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) { |
| 908 | return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit); |
| 909 | } |
| 910 | |
| 911 | // Getting from stage mask and access mask to stage/acess masks is something we need to be good at... |
| 912 | SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) { |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 913 | // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables |
| 914 | // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections |
| 915 | // 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] | 916 | return AccessScopeByStage(stages) & AccessScopeByAccess(accesses); |
| 917 | } |
| 918 | |
| 919 | template <typename Action> |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 920 | void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) { |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 921 | // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages |
| 922 | // that do incrementalupdates |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 923 | auto pos = accesses->lower_bound(range); |
| 924 | if (pos == accesses->end() || !pos->first.intersects(range)) { |
| 925 | // The range is empty, fill it with a default value. |
| 926 | pos = action.Infill(accesses, pos, range); |
| 927 | } else if (range.begin < pos->first.begin) { |
| 928 | // Leading empty space, infill |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 929 | pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin)); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 930 | } else if (pos->first.begin < range.begin) { |
| 931 | // Trim the beginning if needed |
| 932 | pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both()); |
| 933 | ++pos; |
| 934 | } |
| 935 | |
| 936 | const auto the_end = accesses->end(); |
| 937 | while ((pos != the_end) && pos->first.intersects(range)) { |
| 938 | if (pos->first.end > range.end) { |
| 939 | pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both()); |
| 940 | } |
| 941 | |
| 942 | pos = action(accesses, pos); |
| 943 | if (pos == the_end) break; |
| 944 | |
| 945 | auto next = pos; |
| 946 | ++next; |
| 947 | if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) { |
| 948 | // Need to infill if next is disjoint |
| 949 | 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] | 950 | ResourceAccessRange new_range(pos->first.end, limit); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 951 | next = action.Infill(accesses, next, new_range); |
| 952 | } |
| 953 | pos = next; |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | struct UpdateMemoryAccessStateFunctor { |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 958 | using Iterator = ResourceAccessRangeMap::iterator; |
| 959 | Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 960 | // this is only called on gaps, and never returns a gap. |
| 961 | ResourceAccessState default_state; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 962 | context.ResolvePreviousAccess(type, range, accesses, &default_state); |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 963 | return accesses->lower_bound(range); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 964 | } |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 965 | |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 966 | Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const { |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 967 | auto &access_state = pos->second; |
| 968 | access_state.Update(usage, tag); |
| 969 | return pos; |
| 970 | } |
| 971 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 972 | UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_, |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 973 | const ResourceUsageTag &tag_) |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 974 | : type(type_), context(context_), usage(usage_), tag(tag_) {} |
| 975 | const AccessContext::AddressType type; |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 976 | const AccessContext &context; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 977 | const SyncStageAccessIndex usage; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 978 | const ResourceUsageTag &tag; |
| 979 | }; |
| 980 | |
| 981 | struct ApplyMemoryAccessBarrierFunctor { |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 982 | using Iterator = ResourceAccessRangeMap::iterator; |
| 983 | inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; } |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 984 | |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 985 | Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const { |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 986 | auto &access_state = pos->second; |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 987 | 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] | 988 | return pos; |
| 989 | } |
| 990 | |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 991 | ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_, |
| 992 | VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_) |
| 993 | : src_exec_scope(src_exec_scope_), |
| 994 | src_access_scope(src_access_scope_), |
| 995 | dst_exec_scope(dst_exec_scope_), |
| 996 | dst_access_scope(dst_access_scope_) {} |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 997 | |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 998 | VkPipelineStageFlags src_exec_scope; |
| 999 | SyncStageAccessFlags src_access_scope; |
| 1000 | VkPipelineStageFlags dst_exec_scope; |
| 1001 | SyncStageAccessFlags dst_access_scope; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1002 | }; |
| 1003 | |
| 1004 | struct ApplyGlobalBarrierFunctor { |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1005 | using Iterator = ResourceAccessRangeMap::iterator; |
| 1006 | inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; } |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1007 | |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1008 | Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const { |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1009 | auto &access_state = pos->second; |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1010 | access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1011 | |
| 1012 | for (const auto &functor : barrier_functor) { |
| 1013 | functor(accesses, pos); |
| 1014 | } |
| 1015 | return pos; |
| 1016 | } |
| 1017 | |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1018 | ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope, |
| 1019 | SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses, |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1020 | uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers) |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1021 | : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) { |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1022 | // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier... |
| 1023 | barrier_functor.reserve(memoryBarrierCount); |
| 1024 | for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) { |
| 1025 | const auto &barrier = pMemoryBarriers[barrier_index]; |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1026 | barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask), |
| 1027 | dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask)); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1028 | } |
| 1029 | } |
| 1030 | |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1031 | const VkPipelineStageFlags src_exec_scope; |
| 1032 | const VkPipelineStageFlags dst_exec_scope; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1033 | std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor; |
| 1034 | }; |
| 1035 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1036 | void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range, |
| 1037 | const ResourceUsageTag &tag) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1038 | UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag); |
| 1039 | UpdateMemoryAccessState(&GetAccessStateMap(type), range, action); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1040 | } |
| 1041 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1042 | void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1043 | const ResourceAccessRange &range, const ResourceUsageTag &tag) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1044 | if (!SimpleBinding(buffer)) return; |
| 1045 | const auto base_address = ResourceBaseAddress(buffer); |
| 1046 | UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag); |
| 1047 | } |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1048 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1049 | void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1050 | const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset, |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1051 | const VkExtent3D &extent, const ResourceUsageTag &tag) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1052 | if (!SimpleBinding(image)) return; |
locke-lunarg | ae26eac | 2020-04-16 15:29:05 -0600 | [diff] [blame] | 1053 | 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] | 1054 | const auto address_type = ImageAddressType(image); |
| 1055 | const auto base_address = ResourceBaseAddress(image); |
| 1056 | UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag); |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 1057 | for (; range_gen->non_empty(); ++range_gen) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1058 | UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action); |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 1059 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1060 | } |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1061 | void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset, |
| 1062 | const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) { |
| 1063 | if (view != nullptr) { |
| 1064 | const IMAGE_STATE *image = view->image_state.get(); |
| 1065 | if (image != nullptr) { |
| 1066 | auto *update_range = &view->normalized_subresource_range; |
| 1067 | VkImageSubresourceRange masked_range; |
| 1068 | if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask |
| 1069 | masked_range = view->normalized_subresource_range; |
| 1070 | masked_range.aspectMask = aspect_mask & masked_range.aspectMask; |
| 1071 | update_range = &masked_range; |
| 1072 | } |
| 1073 | UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag); |
| 1074 | } |
| 1075 | } |
| 1076 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1077 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1078 | void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, |
| 1079 | const VkImageSubresourceLayers &subresource, const VkOffset3D &offset, |
| 1080 | const VkExtent3D &extent, const ResourceUsageTag &tag) { |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1081 | VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer, |
| 1082 | subresource.layerCount}; |
| 1083 | UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag); |
| 1084 | } |
| 1085 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1086 | template <typename Action> |
| 1087 | 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] | 1088 | if (!SimpleBinding(buffer)) return; |
| 1089 | const auto base_address = ResourceBaseAddress(buffer); |
| 1090 | UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | template <typename Action> |
| 1094 | void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range, |
| 1095 | const Action action) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1096 | if (!SimpleBinding(image)) return; |
| 1097 | const auto address_type = ImageAddressType(image); |
| 1098 | auto *accesses = &GetAccessStateMap(address_type); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1099 | |
locke-lunarg | ae26eac | 2020-04-16 15:29:05 -0600 | [diff] [blame] | 1100 | 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] | 1101 | image.createInfo.extent); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1102 | |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1103 | const auto base_address = ResourceBaseAddress(image); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1104 | for (; range_gen->non_empty(); ++range_gen) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1105 | UpdateMemoryAccessState(accesses, (*range_gen + base_address), action); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1109 | void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area, |
| 1110 | const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass, |
| 1111 | const ResourceUsageTag &tag) { |
| 1112 | UpdateStateResolveAction update(*this, tag); |
| 1113 | ResolveOperation(update, rp_state, render_area, attachment_views, subpass); |
| 1114 | } |
| 1115 | |
John Zulauf | aff2066 | 2020-06-01 14:07:58 -0600 | [diff] [blame] | 1116 | void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area, |
| 1117 | const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass, |
| 1118 | const ResourceUsageTag &tag) { |
| 1119 | const auto *attachment_ci = rp_state.createInfo.pAttachments; |
| 1120 | VkExtent3D extent = CastTo3D(render_area.extent); |
| 1121 | VkOffset3D offset = CastTo3D(render_area.offset); |
| 1122 | |
| 1123 | for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) { |
| 1124 | if (rp_state.attachment_last_subpass[i] == subpass) { |
| 1125 | if (attachment_views[i] == nullptr) continue; // UNUSED |
| 1126 | const auto &view = *attachment_views[i]; |
| 1127 | const IMAGE_STATE *image = view.image_state.get(); |
| 1128 | if (image == nullptr) continue; |
| 1129 | |
| 1130 | const auto &ci = attachment_ci[i]; |
| 1131 | const bool has_depth = FormatHasDepth(ci.format); |
| 1132 | const bool has_stencil = FormatHasStencil(ci.format); |
| 1133 | const bool is_color = !(has_depth || has_stencil); |
| 1134 | const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM; |
| 1135 | |
| 1136 | if (is_color && store_op_stores) { |
| 1137 | UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range, |
| 1138 | offset, extent, tag); |
| 1139 | } else { |
| 1140 | auto update_range = view.normalized_subresource_range; |
| 1141 | if (has_depth && store_op_stores) { |
| 1142 | update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; |
| 1143 | UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent, |
| 1144 | tag); |
| 1145 | } |
| 1146 | const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM; |
| 1147 | if (has_stencil && stencil_op_stores) { |
| 1148 | update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 1149 | UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent, |
| 1150 | tag); |
| 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1157 | template <typename Action> |
| 1158 | void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) { |
| 1159 | // 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] | 1160 | for (const auto address_type : kAddressTypes) { |
| 1161 | UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1166 | for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) { |
| 1167 | auto &context = contexts[subpass_index]; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1168 | for (const auto address_type : kAddressTypes) { |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1169 | context.ResolveAccessRange(address_type, full_range, &context.GetDstExternalTrackBack().barrier, |
| 1170 | &GetAccessStateMap(address_type), nullptr, false); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1175 | void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope, |
| 1176 | SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope, |
| 1177 | SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) { |
| 1178 | const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope); |
| 1179 | UpdateMemoryAccess(image, subresource_range, barrier_action); |
| 1180 | } |
| 1181 | |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1182 | // 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] | 1183 | void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope, |
| 1184 | SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope, |
| 1185 | SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range, |
| 1186 | bool layout_transition, const ResourceUsageTag &tag) { |
| 1187 | if (layout_transition) { |
| 1188 | UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent, |
| 1189 | tag); |
| 1190 | ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope, |
| 1191 | subresource_range); |
John Zulauf | c920122 | 2020-05-13 15:13:03 -0600 | [diff] [blame] | 1192 | } else { |
| 1193 | 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] | 1194 | } |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1195 | } |
| 1196 | |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1197 | // 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] | 1198 | void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier, |
| 1199 | const VkImageSubresourceRange &subresource_range, bool layout_transition, |
| 1200 | const ResourceUsageTag &tag) { |
| 1201 | ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope, |
| 1202 | subresource_range, layout_transition, tag); |
| 1203 | } |
| 1204 | |
| 1205 | // Suitable only for *subpass* access contexts |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1206 | 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] | 1207 | if (!attach_view) return HazardResult(); |
| 1208 | const auto image_state = attach_view->image_state.get(); |
| 1209 | if (!image_state) return HazardResult(); |
| 1210 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1211 | // 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] | 1212 | assert(track_back.context); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1213 | |
| 1214 | // 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] | 1215 | auto hazard = track_back.context->DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope, |
| 1216 | track_back.barrier.src_access_scope, |
| 1217 | attach_view->normalized_subresource_range, kDetectPrevious); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1218 | if (!hazard.hazard) { |
| 1219 | // The Async hazard check is against the current context's async set. |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1220 | 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] | 1221 | attach_view->normalized_subresource_range, kDetectAsync); |
| 1222 | } |
| 1223 | return hazard; |
| 1224 | } |
| 1225 | |
| 1226 | // Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer |
| 1227 | bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state, |
| 1228 | |
| 1229 | const VkRenderPassBeginInfo *pRenderPassBegin, |
| 1230 | const VkSubpassBeginInfoKHR *pSubpassBeginInfo, |
| 1231 | const char *func_name) const { |
| 1232 | // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we |
| 1233 | bool skip = false; |
| 1234 | uint32_t subpass = 0; |
| 1235 | const auto &transitions = rp_state.subpass_transitions[subpass]; |
| 1236 | if (transitions.size()) { |
| 1237 | const std::vector<AccessContext> empty_context_vector; |
| 1238 | // Create context we can use to validate against... |
| 1239 | AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector, |
| 1240 | const_cast<AccessContext *>(&cb_access_context_)); |
| 1241 | |
| 1242 | assert(pRenderPassBegin); |
| 1243 | if (nullptr == pRenderPassBegin) return skip; |
| 1244 | |
| 1245 | const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer); |
| 1246 | assert(fb_state); |
| 1247 | if (nullptr == fb_state) return skip; |
| 1248 | |
| 1249 | // Create a limited array of views (which we'll need to toss |
| 1250 | std::vector<const IMAGE_VIEW_STATE *> views; |
| 1251 | const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state); |
| 1252 | const auto attachment_count = count_attachment.first; |
| 1253 | const auto *attachments = count_attachment.second; |
| 1254 | views.resize(attachment_count, nullptr); |
| 1255 | for (const auto &transition : transitions) { |
| 1256 | assert(transition.attachment < attachment_count); |
| 1257 | views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]); |
| 1258 | } |
| 1259 | |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1260 | skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name); |
| 1261 | 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] | 1262 | } |
| 1263 | return skip; |
| 1264 | } |
| 1265 | |
| 1266 | bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const { |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1267 | bool skip = false; |
John Zulauf | 1507ee4 | 2020-05-18 11:33:09 -0600 | [diff] [blame] | 1268 | skip |= |
| 1269 | current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1270 | |
| 1271 | return skip; |
| 1272 | } |
| 1273 | |
| 1274 | bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const { |
| 1275 | // TODO: Things to add here. |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1276 | // Validate Preserve attachments |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1277 | bool skip = false; |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1278 | skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, |
| 1279 | func_name); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1280 | |
| 1281 | return skip; |
| 1282 | } |
| 1283 | |
| 1284 | void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) { |
| 1285 | assert(sync_state_); |
| 1286 | if (!cb_state_) return; |
| 1287 | |
| 1288 | // Create an access context the current renderpass. |
| 1289 | render_pass_contexts_.emplace_back(&cb_access_context_); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1290 | current_renderpass_context_ = &render_pass_contexts_.back(); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1291 | current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, queue_flags_, tag); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1292 | current_context_ = ¤t_renderpass_context_->CurrentContext(); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1293 | } |
| 1294 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1295 | void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1296 | assert(current_renderpass_context_); |
John Zulauf | 1507ee4 | 2020-05-18 11:33:09 -0600 | [diff] [blame] | 1297 | current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1298 | current_context_ = ¤t_renderpass_context_->CurrentContext(); |
| 1299 | } |
| 1300 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1301 | void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) { |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1302 | assert(current_renderpass_context_); |
| 1303 | if (!current_renderpass_context_) return; |
| 1304 | |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1305 | current_renderpass_context_->RecordEndRenderPass(cb_state_->activeRenderPassBeginInfo.renderArea, tag); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1306 | current_context_ = &cb_access_context_; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1307 | current_renderpass_context_ = nullptr; |
| 1308 | } |
| 1309 | |
John Zulauf | 1507ee4 | 2020-05-18 11:33:09 -0600 | [diff] [blame] | 1310 | bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area, |
| 1311 | const char *func_name) const { |
John Zulauf | aff2066 | 2020-06-01 14:07:58 -0600 | [diff] [blame] | 1312 | // PHASE1 TODO: Add Validate Preserve attachments |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1313 | bool skip = false; |
John Zulauf | b027cdb | 2020-05-21 14:25:22 -0600 | [diff] [blame] | 1314 | skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name, |
| 1315 | current_subpass_); |
John Zulauf | aff2066 | 2020-06-01 14:07:58 -0600 | [diff] [blame] | 1316 | skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_, |
| 1317 | func_name); |
| 1318 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1319 | const auto next_subpass = current_subpass_ + 1; |
John Zulauf | 1507ee4 | 2020-05-18 11:33:09 -0600 | [diff] [blame] | 1320 | const auto &next_context = subpass_contexts_[next_subpass]; |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1321 | skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name); |
| 1322 | skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name); |
| 1323 | return skip; |
| 1324 | } |
| 1325 | bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area, |
| 1326 | const char *func_name) const { |
John Zulauf | aff2066 | 2020-06-01 14:07:58 -0600 | [diff] [blame] | 1327 | // PHASE1 TODO: Validate Preserve |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1328 | bool skip = false; |
| 1329 | skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name, |
| 1330 | current_subpass_); |
John Zulauf | aff2066 | 2020-06-01 14:07:58 -0600 | [diff] [blame] | 1331 | skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_, |
| 1332 | func_name); |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1333 | skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1334 | return skip; |
| 1335 | } |
| 1336 | |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1337 | AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const { |
| 1338 | return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_); |
| 1339 | } |
| 1340 | |
| 1341 | bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area, |
| 1342 | const char *func_name) const { |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1343 | bool skip = false; |
| 1344 | |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1345 | // As validation methods are const and precede the record/update phase, for any tranistions from the current (last) |
| 1346 | // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied. |
| 1347 | // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve) |
| 1348 | // to apply and only copy then, if this proves a hot spot. |
| 1349 | std::unique_ptr<AccessContext> proxy_for_current; |
| 1350 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1351 | // Validate the "finalLayout" transitions to external |
| 1352 | // Get them from where there we're hidding in the extra entry. |
| 1353 | const auto &final_transitions = rp_state_->subpass_transitions.back(); |
| 1354 | for (const auto &transition : final_transitions) { |
| 1355 | const auto &attach_view = attachment_views_[transition.attachment]; |
| 1356 | const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack(); |
| 1357 | 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] | 1358 | auto *context = trackback.context; |
| 1359 | |
| 1360 | if (transition.prev_pass == current_subpass_) { |
| 1361 | if (!proxy_for_current) { |
| 1362 | // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if* |
| 1363 | proxy_for_current.reset(CreateStoreResolveProxy(render_area)); |
| 1364 | } |
| 1365 | context = proxy_for_current.get(); |
| 1366 | } |
| 1367 | |
| 1368 | auto hazard = context->DetectImageBarrierHazard( |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1369 | *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope, |
| 1370 | attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious); |
| 1371 | if (hazard.hazard) { |
| 1372 | skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard), |
| 1373 | "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32 |
| 1374 | " final image layout transition.", |
| 1375 | func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment); |
| 1376 | } |
| 1377 | } |
| 1378 | return skip; |
| 1379 | } |
| 1380 | |
| 1381 | void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) { |
| 1382 | // Add layout transitions... |
| 1383 | const auto &transitions = rp_state_->subpass_transitions[current_subpass_]; |
| 1384 | auto &subpass_context = subpass_contexts_[current_subpass_]; |
John Zulauf | c920122 | 2020-05-13 15:13:03 -0600 | [diff] [blame] | 1385 | std::set<const IMAGE_VIEW_STATE *> view_seen; |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1386 | for (const auto &transition : transitions) { |
| 1387 | const auto attachment_view = attachment_views_[transition.attachment]; |
| 1388 | if (!attachment_view) continue; |
| 1389 | const auto image = attachment_view->image_state.get(); |
| 1390 | if (!image) continue; |
| 1391 | |
| 1392 | const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass); |
John Zulauf | c920122 | 2020-05-13 15:13:03 -0600 | [diff] [blame] | 1393 | auto insert_pair = view_seen.insert(attachment_view); |
| 1394 | if (insert_pair.second) { |
| 1395 | // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion. |
| 1396 | subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag); |
| 1397 | |
| 1398 | } else { |
| 1399 | // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition |
| 1400 | // would clear out the prior barrier flags, so apply this as a *non* transition barrier |
| 1401 | auto barrier_to_transition = barrier->barrier; |
| 1402 | barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT; |
| 1403 | subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag); |
| 1404 | } |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1405 | } |
| 1406 | } |
| 1407 | |
John Zulauf | 1507ee4 | 2020-05-18 11:33:09 -0600 | [diff] [blame] | 1408 | void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) { |
| 1409 | const auto *attachment_ci = rp_state_->createInfo.pAttachments; |
| 1410 | auto &subpass_context = subpass_contexts_[current_subpass_]; |
| 1411 | VkExtent3D extent = CastTo3D(render_area.extent); |
| 1412 | VkOffset3D offset = CastTo3D(render_area.offset); |
| 1413 | |
| 1414 | for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) { |
| 1415 | if (rp_state_->attachment_first_subpass[i] == current_subpass_) { |
| 1416 | if (attachment_views_[i] == nullptr) continue; // UNUSED |
| 1417 | const auto &view = *attachment_views_[i]; |
| 1418 | const IMAGE_STATE *image = view.image_state.get(); |
| 1419 | if (image == nullptr) continue; |
| 1420 | |
| 1421 | const auto &ci = attachment_ci[i]; |
| 1422 | const bool has_depth = FormatHasDepth(ci.format); |
John Zulauf | b027cdb | 2020-05-21 14:25:22 -0600 | [diff] [blame] | 1423 | const bool has_stencil = FormatHasStencil(ci.format); |
John Zulauf | 1507ee4 | 2020-05-18 11:33:09 -0600 | [diff] [blame] | 1424 | const bool is_color = !(has_depth || has_stencil); |
| 1425 | |
| 1426 | if (is_color) { |
| 1427 | subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset, |
| 1428 | extent, tag); |
| 1429 | } else { |
| 1430 | auto update_range = view.normalized_subresource_range; |
| 1431 | if (has_depth) { |
| 1432 | update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; |
| 1433 | subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag); |
| 1434 | } |
| 1435 | if (has_stencil) { |
| 1436 | update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 1437 | subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent, |
| 1438 | tag); |
| 1439 | } |
| 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1445 | void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state, |
| 1446 | VkQueueFlags queue_flags, const ResourceUsageTag &tag) { |
| 1447 | current_subpass_ = 0; |
locke-lunarg | aecf215 | 2020-05-12 17:15:41 -0600 | [diff] [blame] | 1448 | rp_state_ = cb_state.activeRenderPass.get(); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1449 | subpass_contexts_.reserve(rp_state_->createInfo.subpassCount); |
| 1450 | // Add this for all subpasses here so that they exsist during next subpass validation |
| 1451 | for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) { |
| 1452 | subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context_); |
| 1453 | } |
| 1454 | attachment_views_ = state.GetCurrentAttachmentViews(cb_state); |
| 1455 | |
| 1456 | RecordLayoutTransitions(tag); |
John Zulauf | 1507ee4 | 2020-05-18 11:33:09 -0600 | [diff] [blame] | 1457 | RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1458 | } |
John Zulauf | 1507ee4 | 2020-05-18 11:33:09 -0600 | [diff] [blame] | 1459 | |
| 1460 | void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) { |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1461 | // Resolves are against *prior* subpass context and thus *before* the subpass increment |
| 1462 | CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag); |
John Zulauf | aff2066 | 2020-06-01 14:07:58 -0600 | [diff] [blame] | 1463 | CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag); |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1464 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1465 | current_subpass_++; |
| 1466 | assert(current_subpass_ < subpass_contexts_.size()); |
| 1467 | RecordLayoutTransitions(tag); |
John Zulauf | 1507ee4 | 2020-05-18 11:33:09 -0600 | [diff] [blame] | 1468 | RecordLoadOperations(render_area, tag); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1469 | } |
| 1470 | |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1471 | void RenderPassAccessContext::RecordEndRenderPass(const VkRect2D &render_area, const ResourceUsageTag &tag) { |
John Zulauf | aff2066 | 2020-06-01 14:07:58 -0600 | [diff] [blame] | 1472 | // Add the resolve and store accesses |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1473 | CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag); |
John Zulauf | aff2066 | 2020-06-01 14:07:58 -0600 | [diff] [blame] | 1474 | CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag); |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1475 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1476 | // Export the accesses from the renderpass... |
| 1477 | external_context_->ResolveChildContexts(subpass_contexts_); |
| 1478 | |
| 1479 | // Add the "finalLayout" transitions to external |
| 1480 | // Get them from where there we're hidding in the extra entry. |
| 1481 | const auto &final_transitions = rp_state_->subpass_transitions.back(); |
| 1482 | for (const auto &transition : final_transitions) { |
| 1483 | const auto &attachment = attachment_views_[transition.attachment]; |
| 1484 | const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack(); |
| 1485 | assert(external_context_ == last_trackback.context); |
| 1486 | external_context_->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier, |
| 1487 | attachment->normalized_subresource_range, true, tag); |
| 1488 | } |
| 1489 | } |
| 1490 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1491 | SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) { |
| 1492 | const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask); |
| 1493 | src_exec_scope = WithEarlierPipelineStages(src_stage_mask); |
| 1494 | src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask); |
| 1495 | const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask); |
| 1496 | dst_exec_scope = WithLaterPipelineStages(dst_stage_mask); |
| 1497 | dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask); |
| 1498 | } |
| 1499 | |
| 1500 | void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) { |
| 1501 | ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope); |
| 1502 | ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope); |
| 1503 | } |
| 1504 | |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1505 | HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const { |
| 1506 | HazardResult hazard; |
| 1507 | auto usage = FlagBit(usage_index); |
| 1508 | if (IsRead(usage)) { |
John Zulauf | c920122 | 2020-05-13 15:13:03 -0600 | [diff] [blame] | 1509 | if (last_write && IsWriteHazard(usage)) { |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1510 | hazard.Set(READ_AFTER_WRITE, write_tag); |
| 1511 | } |
| 1512 | } else { |
| 1513 | // Assume write |
| 1514 | // TODO determine what to do with READ-WRITE usage states if any |
| 1515 | // Write-After-Write check -- if we have a previous write to test against |
| 1516 | if (last_write && IsWriteHazard(usage)) { |
| 1517 | hazard.Set(WRITE_AFTER_WRITE, write_tag); |
| 1518 | } else { |
John Zulauf | 6913342 | 2020-05-20 14:55:53 -0600 | [diff] [blame] | 1519 | // Look for casus belli for WAR |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1520 | const auto usage_stage = PipelineStageBit(usage_index); |
| 1521 | for (uint32_t read_index = 0; read_index < last_read_count; read_index++) { |
| 1522 | if (IsReadHazard(usage_stage, last_reads[read_index])) { |
| 1523 | hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag); |
| 1524 | break; |
| 1525 | } |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | return hazard; |
| 1530 | } |
| 1531 | |
John Zulauf | 6913342 | 2020-05-20 14:55:53 -0600 | [diff] [blame] | 1532 | HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const { |
| 1533 | // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations |
| 1534 | HazardResult hazard; |
| 1535 | const auto usage = FlagBit(usage_index); |
| 1536 | const bool write_is_ordered = (last_write & ordering.access_scope) == last_write; // Is true if no write, and that's good. |
| 1537 | if (IsRead(usage)) { |
| 1538 | if (!write_is_ordered && IsWriteHazard(usage)) { |
| 1539 | hazard.Set(READ_AFTER_WRITE, write_tag); |
| 1540 | } |
| 1541 | } else { |
| 1542 | if (!write_is_ordered && IsWriteHazard(usage)) { |
| 1543 | hazard.Set(WRITE_AFTER_WRITE, write_tag); |
| 1544 | } else { |
| 1545 | const auto usage_stage = PipelineStageBit(usage_index); |
| 1546 | const auto unordered_reads = last_read_stages & ~ordering.exec_scope; |
| 1547 | if (unordered_reads) { |
| 1548 | // Look for any WAR hazards outside the ordered set of stages |
| 1549 | for (uint32_t read_index = 0; read_index < last_read_count; read_index++) { |
| 1550 | if (last_reads[read_index].stage & unordered_reads) { |
| 1551 | if (IsReadHazard(usage_stage, last_reads[read_index])) { |
| 1552 | hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag); |
| 1553 | break; |
| 1554 | } |
| 1555 | } |
| 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | } |
| 1560 | return hazard; |
| 1561 | } |
| 1562 | |
John Zulauf | 2f952d2 | 2020-02-10 11:34:51 -0700 | [diff] [blame] | 1563 | // Asynchronous Hazards occur between subpasses with no connection through the DAG |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1564 | HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const { |
John Zulauf | 2f952d2 | 2020-02-10 11:34:51 -0700 | [diff] [blame] | 1565 | HazardResult hazard; |
| 1566 | auto usage = FlagBit(usage_index); |
| 1567 | if (IsRead(usage)) { |
| 1568 | if (last_write != 0) { |
| 1569 | hazard.Set(READ_RACING_WRITE, write_tag); |
| 1570 | } |
| 1571 | } else { |
| 1572 | if (last_write != 0) { |
| 1573 | hazard.Set(WRITE_RACING_WRITE, write_tag); |
| 1574 | } else if (last_read_count > 0) { |
| 1575 | hazard.Set(WRITE_RACING_READ, last_reads[0].tag); |
| 1576 | } |
| 1577 | } |
| 1578 | return hazard; |
| 1579 | } |
| 1580 | |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1581 | HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope, |
| 1582 | SyncStageAccessFlags src_access_scope) const { |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 1583 | // Only supporting image layout transitions for now |
| 1584 | assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION); |
| 1585 | HazardResult hazard; |
| 1586 | if (last_write) { |
| 1587 | // If the previous write is *not* in the 1st access scope |
| 1588 | // *AND* the current barrier is not in the dependency chain |
| 1589 | // *AND* the there is no prior memory barrier for the previous write in the dependency chain |
| 1590 | // then the barrier access is unsafe (R/W after W) |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1591 | 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] | 1592 | // TODO: Do we need a difference hazard name for this? |
| 1593 | hazard.Set(WRITE_AFTER_WRITE, write_tag); |
| 1594 | } |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1595 | } |
| 1596 | if (!hazard.hazard) { |
| 1597 | // Look at the reads if any |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 1598 | 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] | 1599 | const auto &read_access = last_reads[read_index]; |
| 1600 | // If the read stage is not in the src sync sync |
| 1601 | // *AND* not execution chained with an existing sync barrier (that's the or) |
| 1602 | // then the barrier access is unsafe (R/W after R) |
| 1603 | if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) { |
| 1604 | hazard.Set(WRITE_AFTER_READ, read_access.tag); |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 1605 | break; |
| 1606 | } |
| 1607 | } |
| 1608 | } |
| 1609 | return hazard; |
| 1610 | } |
| 1611 | |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 1612 | // The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no |
| 1613 | // tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another |
| 1614 | // exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones. |
| 1615 | void ResourceAccessState::Resolve(const ResourceAccessState &other) { |
| 1616 | if (write_tag.IsBefore(other.write_tag)) { |
| 1617 | // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation |
| 1618 | *this = other; |
| 1619 | } else if (!other.write_tag.IsBefore(write_tag)) { |
| 1620 | // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the |
| 1621 | // dependency chaining logic or any stage expansion) |
| 1622 | write_barriers |= other.write_barriers; |
| 1623 | |
| 1624 | // Merge that read states |
| 1625 | for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) { |
| 1626 | auto &other_read = other.last_reads[other_read_index]; |
| 1627 | if (last_read_stages & other_read.stage) { |
| 1628 | // Merge in the barriers for read stages that exist in *both* this and other |
| 1629 | // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index. |
| 1630 | for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) { |
| 1631 | auto &my_read = last_reads[my_read_index]; |
| 1632 | if (other_read.stage == my_read.stage) { |
| 1633 | if (my_read.tag.IsBefore(other_read.tag)) { |
| 1634 | my_read.tag = other_read.tag; |
| 1635 | } |
| 1636 | my_read.barriers |= other_read.barriers; |
| 1637 | break; |
| 1638 | } |
| 1639 | } |
| 1640 | } else { |
| 1641 | // The other read stage doesn't exist in this, so add it. |
| 1642 | last_reads[last_read_count] = other_read; |
| 1643 | last_read_count++; |
| 1644 | last_read_stages |= other_read.stage; |
| 1645 | } |
| 1646 | } |
| 1647 | } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore |
| 1648 | // it. |
| 1649 | } |
| 1650 | |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1651 | void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) { |
| 1652 | // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource... |
| 1653 | const auto usage_bit = FlagBit(usage_index); |
| 1654 | if (IsRead(usage_index)) { |
| 1655 | // Mulitple outstanding reads may be of interest and do dependency chains independently |
| 1656 | // However, for purposes of barrier tracking, only one read per pipeline stage matters |
| 1657 | const auto usage_stage = PipelineStageBit(usage_index); |
| 1658 | if (usage_stage & last_read_stages) { |
| 1659 | for (uint32_t read_index = 0; read_index < last_read_count; read_index++) { |
| 1660 | ReadState &access = last_reads[read_index]; |
| 1661 | if (access.stage == usage_stage) { |
| 1662 | access.barriers = 0; |
| 1663 | access.tag = tag; |
| 1664 | break; |
| 1665 | } |
| 1666 | } |
| 1667 | } else { |
| 1668 | // We don't have this stage in the list yet... |
| 1669 | assert(last_read_count < last_reads.size()); |
| 1670 | ReadState &access = last_reads[last_read_count++]; |
| 1671 | access.stage = usage_stage; |
| 1672 | access.barriers = 0; |
| 1673 | access.tag = tag; |
| 1674 | last_read_stages |= usage_stage; |
| 1675 | } |
| 1676 | } else { |
| 1677 | // Assume write |
| 1678 | // TODO determine what to do with READ-WRITE operations if any |
| 1679 | // Clobber last read and both sets of barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!! |
| 1680 | // if the last_reads/last_write were unsafe, we've reported them, |
| 1681 | // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them |
| 1682 | last_read_count = 0; |
| 1683 | last_read_stages = 0; |
| 1684 | |
| 1685 | write_barriers = 0; |
| 1686 | write_dependency_chain = 0; |
| 1687 | write_tag = tag; |
| 1688 | last_write = usage_bit; |
| 1689 | } |
| 1690 | } |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 1691 | |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1692 | void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) { |
| 1693 | // Execution Barriers only protect read operations |
| 1694 | for (uint32_t read_index = 0; read_index < last_read_count; read_index++) { |
| 1695 | ReadState &access = last_reads[read_index]; |
| 1696 | // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope |
| 1697 | if (srcStageMask & (access.stage | access.barriers)) { |
| 1698 | access.barriers |= dstStageMask; |
| 1699 | } |
| 1700 | } |
| 1701 | if (write_dependency_chain & srcStageMask) write_dependency_chain |= dstStageMask; |
| 1702 | } |
| 1703 | |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1704 | void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope, |
| 1705 | VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) { |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1706 | // Assuming we've applied the execution side of this barrier, we update just the write |
| 1707 | // The || implements the "dependency chain" logic for this barrier |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1708 | if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) { |
| 1709 | write_barriers |= dst_access_scope; |
| 1710 | write_dependency_chain |= dst_exec_scope; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1711 | } |
| 1712 | } |
| 1713 | |
John Zulauf | d1f85d4 | 2020-04-15 12:23:15 -0600 | [diff] [blame] | 1714 | void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1715 | auto *access_context = GetAccessContextNoInsert(command_buffer); |
| 1716 | if (access_context) { |
| 1717 | access_context->Reset(); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1718 | } |
| 1719 | } |
| 1720 | |
John Zulauf | d1f85d4 | 2020-04-15 12:23:15 -0600 | [diff] [blame] | 1721 | void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) { |
| 1722 | auto access_found = cb_access_state.find(command_buffer); |
| 1723 | if (access_found != cb_access_state.end()) { |
| 1724 | access_found->second->Reset(); |
| 1725 | cb_access_state.erase(access_found); |
| 1726 | } |
| 1727 | } |
| 1728 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1729 | void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask, |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1730 | VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope, |
| 1731 | SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount, |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1732 | const VkMemoryBarrier *pMemoryBarriers) { |
| 1733 | // TODO: Implement this better (maybe some delayed/on-demand integration). |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1734 | ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount, |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1735 | pMemoryBarriers); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1736 | context->ApplyGlobalBarriers(barriers_functor); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1737 | } |
| 1738 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1739 | void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope, |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1740 | SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope, |
| 1741 | SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count, |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1742 | const VkBufferMemoryBarrier *barriers) { |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1743 | for (uint32_t index = 0; index < barrier_count; index++) { |
locke-lunarg | 3c03800 | 2020-04-30 23:08:08 -0600 | [diff] [blame] | 1744 | auto barrier = barriers[index]; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1745 | const auto *buffer = Get<BUFFER_STATE>(barrier.buffer); |
| 1746 | if (!buffer) continue; |
locke-lunarg | 3c03800 | 2020-04-30 23:08:08 -0600 | [diff] [blame] | 1747 | barrier.size = GetRealWholeSize(barrier.offset, barrier.size, buffer->createInfo.size); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1748 | ResourceAccessRange range = MakeRange(barrier); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1749 | const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask); |
| 1750 | const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask); |
| 1751 | const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope); |
| 1752 | context->UpdateMemoryAccess(*buffer, range, update_action); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1753 | } |
| 1754 | } |
| 1755 | |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1756 | void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope, |
| 1757 | SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope, |
| 1758 | SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count, |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1759 | const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) { |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1760 | for (uint32_t index = 0; index < barrier_count; index++) { |
| 1761 | const auto &barrier = barriers[index]; |
| 1762 | const auto *image = Get<IMAGE_STATE>(barrier.image); |
| 1763 | if (!image) continue; |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1764 | auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1765 | bool layout_transition = barrier.oldLayout != barrier.newLayout; |
| 1766 | const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask); |
| 1767 | const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask); |
| 1768 | context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range, |
| 1769 | layout_transition, tag); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1770 | } |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer, |
| 1774 | uint32_t regionCount, const VkBufferCopy *pRegions) const { |
| 1775 | bool skip = false; |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1776 | const auto *cb_context = GetAccessContext(commandBuffer); |
| 1777 | assert(cb_context); |
| 1778 | if (!cb_context) return skip; |
| 1779 | const auto *context = cb_context->GetCurrentAccessContext(); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1780 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1781 | // If we have no previous accesses, we have no hazards |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1782 | const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1783 | const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1784 | |
| 1785 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1786 | const auto ©_region = pRegions[region]; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1787 | if (src_buffer) { |
locke-lunarg | 3c03800 | 2020-04-30 23:08:08 -0600 | [diff] [blame] | 1788 | ResourceAccessRange src_range = |
| 1789 | 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] | 1790 | auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1791 | if (hazard.hazard) { |
| 1792 | // TODO -- add tag information to log msg when useful. |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1793 | skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard), |
| 1794 | "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard), |
| 1795 | report_data->FormatHandle(srcBuffer).c_str(), region); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1796 | } |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1797 | } |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1798 | if (dst_buffer && !skip) { |
locke-lunarg | 3c03800 | 2020-04-30 23:08:08 -0600 | [diff] [blame] | 1799 | ResourceAccessRange dst_range = |
| 1800 | 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] | 1801 | auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1802 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1803 | skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), |
| 1804 | "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard), |
| 1805 | report_data->FormatHandle(dstBuffer).c_str(), region); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1806 | } |
| 1807 | } |
| 1808 | if (skip) break; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1809 | } |
| 1810 | return skip; |
| 1811 | } |
| 1812 | |
| 1813 | void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer, |
| 1814 | uint32_t regionCount, const VkBufferCopy *pRegions) { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1815 | auto *cb_context = GetAccessContext(commandBuffer); |
| 1816 | assert(cb_context); |
John Zulauf | 2b151bf | 2020-04-24 15:37:44 -0600 | [diff] [blame] | 1817 | const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1818 | auto *context = cb_context->GetCurrentAccessContext(); |
| 1819 | |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1820 | const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1821 | const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1822 | |
| 1823 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1824 | const auto ©_region = pRegions[region]; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1825 | if (src_buffer) { |
locke-lunarg | 3c03800 | 2020-04-30 23:08:08 -0600 | [diff] [blame] | 1826 | ResourceAccessRange src_range = |
| 1827 | 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] | 1828 | context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1829 | } |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1830 | if (dst_buffer) { |
locke-lunarg | 3c03800 | 2020-04-30 23:08:08 -0600 | [diff] [blame] | 1831 | ResourceAccessRange dst_range = |
| 1832 | 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] | 1833 | context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1834 | } |
| 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 1839 | VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, |
| 1840 | const VkImageCopy *pRegions) const { |
| 1841 | bool skip = false; |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1842 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1843 | assert(cb_access_context); |
| 1844 | if (!cb_access_context) return skip; |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1845 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1846 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 1847 | assert(context); |
| 1848 | if (!context) return skip; |
| 1849 | |
| 1850 | const auto *src_image = Get<IMAGE_STATE>(srcImage); |
| 1851 | const auto *dst_image = Get<IMAGE_STATE>(dstImage); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1852 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1853 | const auto ©_region = pRegions[region]; |
| 1854 | if (src_image) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1855 | 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] | 1856 | copy_region.srcOffset, copy_region.extent); |
| 1857 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1858 | skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard), |
| 1859 | "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard), |
| 1860 | report_data->FormatHandle(srcImage).c_str(), region); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1861 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | if (dst_image) { |
locke-lunarg | 1df1f88 | 2020-03-02 16:42:08 -0700 | [diff] [blame] | 1865 | VkExtent3D dst_copy_extent = |
| 1866 | GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1867 | 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] | 1868 | copy_region.dstOffset, dst_copy_extent); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1869 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1870 | skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard), |
| 1871 | "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard), |
| 1872 | report_data->FormatHandle(dstImage).c_str(), region); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1873 | } |
locke-lunarg | 1dbbb9e | 2020-02-28 22:43:53 -0700 | [diff] [blame] | 1874 | if (skip) break; |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1875 | } |
| 1876 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1877 | |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1878 | return skip; |
| 1879 | } |
| 1880 | |
| 1881 | void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 1882 | VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, |
| 1883 | const VkImageCopy *pRegions) { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1884 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1885 | assert(cb_access_context); |
John Zulauf | 2b151bf | 2020-04-24 15:37:44 -0600 | [diff] [blame] | 1886 | const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1887 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 1888 | assert(context); |
| 1889 | |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1890 | auto *src_image = Get<IMAGE_STATE>(srcImage); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1891 | auto *dst_image = Get<IMAGE_STATE>(dstImage); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1892 | |
| 1893 | for (uint32_t region = 0; region < regionCount; region++) { |
| 1894 | const auto ©_region = pRegions[region]; |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1895 | if (src_image) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1896 | context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset, |
| 1897 | copy_region.extent, tag); |
John Zulauf | 5c5e88d | 2019-12-26 11:22:02 -0700 | [diff] [blame] | 1898 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1899 | if (dst_image) { |
locke-lunarg | 1df1f88 | 2020-03-02 16:42:08 -0700 | [diff] [blame] | 1900 | VkExtent3D dst_copy_extent = |
| 1901 | GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1902 | context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset, |
| 1903 | dst_copy_extent, tag); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1904 | } |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask, |
| 1909 | VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags, |
| 1910 | uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers, |
| 1911 | uint32_t bufferMemoryBarrierCount, |
| 1912 | const VkBufferMemoryBarrier *pBufferMemoryBarriers, |
| 1913 | uint32_t imageMemoryBarrierCount, |
| 1914 | const VkImageMemoryBarrier *pImageMemoryBarriers) const { |
| 1915 | bool skip = false; |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1916 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1917 | assert(cb_access_context); |
| 1918 | if (!cb_access_context) return skip; |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 1919 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1920 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 1921 | assert(context); |
| 1922 | if (!context) return skip; |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 1923 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1924 | const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask); |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1925 | const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask); |
| 1926 | auto src_stage_accesses = AccessScopeByStage(src_stage_mask); |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 1927 | // Validate Image Layout transitions |
| 1928 | for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) { |
| 1929 | const auto &barrier = pImageMemoryBarriers[index]; |
| 1930 | if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point. |
| 1931 | const auto *image_state = Get<IMAGE_STATE>(barrier.image); |
| 1932 | if (!image_state) continue; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 1933 | 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] | 1934 | if (hazard.hazard) { |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 1935 | // PHASE1 TODO -- add tag information to log msg when useful. |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 1936 | skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard), |
| 1937 | "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s", string_SyncHazard(hazard.hazard), |
| 1938 | index, report_data->FormatHandle(barrier.image).c_str()); |
John Zulauf | 0cb5be2 | 2020-01-23 12:18:22 -0700 | [diff] [blame] | 1939 | } |
| 1940 | } |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1941 | |
| 1942 | return skip; |
| 1943 | } |
| 1944 | |
| 1945 | void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask, |
| 1946 | VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags, |
| 1947 | uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers, |
| 1948 | uint32_t bufferMemoryBarrierCount, |
| 1949 | const VkBufferMemoryBarrier *pBufferMemoryBarriers, |
| 1950 | uint32_t imageMemoryBarrierCount, |
| 1951 | const VkImageMemoryBarrier *pImageMemoryBarriers) { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1952 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 1953 | assert(cb_access_context); |
| 1954 | if (!cb_access_context) return; |
John Zulauf | 2b151bf | 2020-04-24 15:37:44 -0600 | [diff] [blame] | 1955 | const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1956 | auto access_context = cb_access_context->GetCurrentAccessContext(); |
| 1957 | assert(access_context); |
| 1958 | if (!access_context) return; |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1959 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1960 | const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask); |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1961 | auto src_stage_accesses = AccessScopeByStage(src_stage_mask); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1962 | const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask); |
John Zulauf | 36bcf6a | 2020-02-03 15:12:52 -0700 | [diff] [blame] | 1963 | auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask); |
| 1964 | const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask); |
| 1965 | const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1966 | ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses, |
| 1967 | bufferMemoryBarrierCount, pBufferMemoryBarriers); |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 1968 | 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] | 1969 | imageMemoryBarrierCount, pImageMemoryBarriers, tag); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1970 | |
| 1971 | // 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] | 1972 | 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] | 1973 | pMemoryBarriers); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo, |
| 1977 | const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) { |
| 1978 | // The state tracker sets up the device state |
| 1979 | StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result); |
| 1980 | |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 1981 | // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker |
| 1982 | // refactor would be messier without. |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1983 | // TODO: Find a good way to do this hooklessly. |
| 1984 | ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map); |
| 1985 | ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation); |
| 1986 | SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data); |
| 1987 | |
John Zulauf | d1f85d4 | 2020-04-15 12:23:15 -0600 | [diff] [blame] | 1988 | sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void { |
| 1989 | sync_device_state->ResetCommandBufferCallback(command_buffer); |
| 1990 | }); |
| 1991 | sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void { |
| 1992 | sync_device_state->FreeCommandBufferCallback(command_buffer); |
| 1993 | }); |
John Zulauf | 9cb530d | 2019-09-30 14:14:10 -0600 | [diff] [blame] | 1994 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 1995 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 1996 | bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin, |
| 1997 | const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const { |
| 1998 | bool skip = false; |
| 1999 | const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass); |
| 2000 | auto cb_context = GetAccessContext(commandBuffer); |
| 2001 | |
| 2002 | if (rp_state && cb_context) { |
| 2003 | skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name); |
| 2004 | } |
| 2005 | |
| 2006 | return skip; |
| 2007 | } |
| 2008 | |
| 2009 | bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin, |
| 2010 | VkSubpassContents contents) const { |
| 2011 | bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents); |
| 2012 | auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>(); |
| 2013 | subpass_begin_info.contents = contents; |
| 2014 | skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass"); |
| 2015 | return skip; |
| 2016 | } |
| 2017 | |
| 2018 | bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin, |
| 2019 | const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const { |
| 2020 | bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo); |
| 2021 | skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2"); |
| 2022 | return skip; |
| 2023 | } |
| 2024 | |
| 2025 | bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer, |
| 2026 | const VkRenderPassBeginInfo *pRenderPassBegin, |
| 2027 | const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const { |
| 2028 | bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo); |
| 2029 | skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR"); |
| 2030 | return skip; |
| 2031 | } |
| 2032 | |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 2033 | void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo, |
| 2034 | VkResult result) { |
| 2035 | // The state tracker sets up the command buffer state |
| 2036 | StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result); |
| 2037 | |
| 2038 | // Create/initialize the structure that trackers accesses at the command buffer scope. |
| 2039 | auto cb_access_context = GetAccessContext(commandBuffer); |
| 2040 | assert(cb_access_context); |
| 2041 | cb_access_context->Reset(); |
| 2042 | } |
| 2043 | |
| 2044 | void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin, |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 2045 | const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 2046 | auto cb_context = GetAccessContext(commandBuffer); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 2047 | if (cb_context) { |
| 2048 | cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command)); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 2049 | } |
| 2050 | } |
| 2051 | |
| 2052 | void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin, |
| 2053 | VkSubpassContents contents) { |
| 2054 | StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents); |
| 2055 | auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>(); |
| 2056 | subpass_begin_info.contents = contents; |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 2057 | RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin, |
| 2061 | const VkSubpassBeginInfo *pSubpassBeginInfo) { |
| 2062 | StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 2063 | RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer, |
| 2067 | const VkRenderPassBeginInfo *pRenderPassBegin, |
| 2068 | const VkSubpassBeginInfo *pSubpassBeginInfo) { |
| 2069 | StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 2070 | RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2); |
| 2071 | } |
| 2072 | |
| 2073 | bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo, |
| 2074 | const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const { |
| 2075 | bool skip = false; |
| 2076 | |
| 2077 | auto cb_context = GetAccessContext(commandBuffer); |
| 2078 | assert(cb_context); |
| 2079 | auto cb_state = cb_context->GetCommandBufferState(); |
| 2080 | if (!cb_state) return skip; |
| 2081 | |
| 2082 | auto rp_state = cb_state->activeRenderPass; |
| 2083 | if (!rp_state) return skip; |
| 2084 | |
| 2085 | skip |= cb_context->ValidateNextSubpass(func_name); |
| 2086 | |
| 2087 | return skip; |
| 2088 | } |
| 2089 | |
| 2090 | bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const { |
| 2091 | bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents); |
| 2092 | auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>(); |
| 2093 | subpass_begin_info.contents = contents; |
| 2094 | skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass"); |
| 2095 | return skip; |
| 2096 | } |
| 2097 | |
| 2098 | bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo, |
| 2099 | const VkSubpassEndInfoKHR *pSubpassEndInfo) const { |
| 2100 | bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo); |
| 2101 | skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR"); |
| 2102 | return skip; |
| 2103 | } |
| 2104 | |
| 2105 | bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo, |
| 2106 | const VkSubpassEndInfo *pSubpassEndInfo) const { |
| 2107 | bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo); |
| 2108 | skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2"); |
| 2109 | return skip; |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo, |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 2113 | const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) { |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 2114 | auto cb_context = GetAccessContext(commandBuffer); |
| 2115 | assert(cb_context); |
| 2116 | auto cb_state = cb_context->GetCommandBufferState(); |
| 2117 | if (!cb_state) return; |
| 2118 | |
| 2119 | auto rp_state = cb_state->activeRenderPass; |
| 2120 | if (!rp_state) return; |
| 2121 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 2122 | cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command)); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) { |
| 2126 | StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents); |
| 2127 | auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>(); |
| 2128 | subpass_begin_info.contents = contents; |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 2129 | RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo, |
| 2133 | const VkSubpassEndInfo *pSubpassEndInfo) { |
| 2134 | StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 2135 | RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo, |
| 2139 | const VkSubpassEndInfo *pSubpassEndInfo) { |
| 2140 | StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 2141 | RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 2142 | } |
| 2143 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 2144 | bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo, |
| 2145 | const char *func_name) const { |
| 2146 | bool skip = false; |
| 2147 | |
| 2148 | auto cb_context = GetAccessContext(commandBuffer); |
| 2149 | assert(cb_context); |
| 2150 | auto cb_state = cb_context->GetCommandBufferState(); |
| 2151 | if (!cb_state) return skip; |
| 2152 | |
| 2153 | auto rp_state = cb_state->activeRenderPass; |
| 2154 | if (!rp_state) return skip; |
| 2155 | |
| 2156 | skip |= cb_context->ValidateEndRenderpass(func_name); |
| 2157 | return skip; |
| 2158 | } |
| 2159 | |
| 2160 | bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const { |
| 2161 | bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer); |
| 2162 | skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass"); |
| 2163 | return skip; |
| 2164 | } |
| 2165 | |
| 2166 | bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, |
| 2167 | const VkSubpassEndInfoKHR *pSubpassEndInfo) const { |
| 2168 | bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo); |
| 2169 | skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2"); |
| 2170 | return skip; |
| 2171 | } |
| 2172 | |
| 2173 | bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, |
| 2174 | const VkSubpassEndInfoKHR *pSubpassEndInfo) const { |
| 2175 | bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo); |
| 2176 | skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR"); |
| 2177 | return skip; |
| 2178 | } |
| 2179 | |
| 2180 | void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, |
| 2181 | CMD_TYPE command) { |
John Zulauf | e5da6e5 | 2020-03-18 15:32:18 -0600 | [diff] [blame] | 2182 | // Resolve the all subpass contexts to the command buffer contexts |
| 2183 | auto cb_context = GetAccessContext(commandBuffer); |
| 2184 | assert(cb_context); |
| 2185 | auto cb_state = cb_context->GetCommandBufferState(); |
| 2186 | if (!cb_state) return; |
| 2187 | |
locke-lunarg | aecf215 | 2020-05-12 17:15:41 -0600 | [diff] [blame] | 2188 | const auto *rp_state = cb_state->activeRenderPass.get(); |
John Zulauf | e5da6e5 | 2020-03-18 15:32:18 -0600 | [diff] [blame] | 2189 | if (!rp_state) return; |
| 2190 | |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 2191 | cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command)); |
John Zulauf | e5da6e5 | 2020-03-18 15:32:18 -0600 | [diff] [blame] | 2192 | } |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 2193 | |
| 2194 | void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) { |
| 2195 | StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 2196 | RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 2197 | } |
| 2198 | |
| 2199 | void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) { |
| 2200 | StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 2201 | RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) { |
| 2205 | StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo); |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 2206 | RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2); |
John Zulauf | 3d84f1b | 2020-03-09 13:33:25 -0600 | [diff] [blame] | 2207 | } |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2208 | |
| 2209 | bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage, |
| 2210 | VkImageLayout dstImageLayout, uint32_t regionCount, |
| 2211 | const VkBufferImageCopy *pRegions) const { |
| 2212 | bool skip = false; |
| 2213 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 2214 | assert(cb_access_context); |
| 2215 | if (!cb_access_context) return skip; |
| 2216 | |
| 2217 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 2218 | assert(context); |
| 2219 | if (!context) return skip; |
| 2220 | |
| 2221 | const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2222 | const auto *dst_image = Get<IMAGE_STATE>(dstImage); |
| 2223 | |
| 2224 | for (uint32_t region = 0; region < regionCount; region++) { |
| 2225 | const auto ©_region = pRegions[region]; |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 2226 | if (src_buffer) { |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 2227 | ResourceAccessRange src_range = |
| 2228 | MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format)); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 2229 | auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2230 | if (hazard.hazard) { |
John Zulauf | 7635de3 | 2020-05-29 17:14:15 -0600 | [diff] [blame] | 2231 | // PHASE1 TODO -- add tag information to log msg when useful. |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 2232 | skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard), |
| 2233 | "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32, |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2234 | string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region); |
| 2235 | } |
| 2236 | } |
| 2237 | if (dst_image) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 2238 | 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] | 2239 | copy_region.imageOffset, copy_region.imageExtent); |
| 2240 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 2241 | skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard), |
| 2242 | "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32, |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2243 | string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region); |
| 2244 | } |
| 2245 | if (skip) break; |
| 2246 | } |
| 2247 | if (skip) break; |
| 2248 | } |
| 2249 | return skip; |
| 2250 | } |
| 2251 | |
| 2252 | void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage, |
| 2253 | VkImageLayout dstImageLayout, uint32_t regionCount, |
| 2254 | const VkBufferImageCopy *pRegions) { |
| 2255 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 2256 | assert(cb_access_context); |
John Zulauf | 2b151bf | 2020-04-24 15:37:44 -0600 | [diff] [blame] | 2257 | const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2258 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 2259 | assert(context); |
| 2260 | |
| 2261 | const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 2262 | const auto *dst_image = Get<IMAGE_STATE>(dstImage); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2263 | |
| 2264 | for (uint32_t region = 0; region < regionCount; region++) { |
| 2265 | const auto ©_region = pRegions[region]; |
| 2266 | if (src_buffer) { |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 2267 | ResourceAccessRange src_range = |
| 2268 | MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format)); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 2269 | context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2270 | } |
| 2271 | if (dst_image) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 2272 | context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource, |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 2273 | copy_region.imageOffset, copy_region.imageExtent, tag); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2274 | } |
| 2275 | } |
| 2276 | } |
| 2277 | |
| 2278 | bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, |
| 2279 | VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount, |
| 2280 | const VkBufferImageCopy *pRegions) const { |
| 2281 | bool skip = false; |
| 2282 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 2283 | assert(cb_access_context); |
| 2284 | if (!cb_access_context) return skip; |
| 2285 | |
| 2286 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 2287 | assert(context); |
| 2288 | if (!context) return skip; |
| 2289 | |
| 2290 | const auto *src_image = Get<IMAGE_STATE>(srcImage); |
| 2291 | const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer); |
| 2292 | const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE; |
| 2293 | for (uint32_t region = 0; region < regionCount; region++) { |
| 2294 | const auto ©_region = pRegions[region]; |
| 2295 | if (src_image) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 2296 | 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] | 2297 | copy_region.imageOffset, copy_region.imageExtent); |
| 2298 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 2299 | skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard), |
| 2300 | "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32, |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2301 | string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region); |
| 2302 | } |
| 2303 | } |
| 2304 | if (dst_mem) { |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 2305 | ResourceAccessRange dst_range = |
| 2306 | MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format)); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 2307 | auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2308 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 2309 | skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), |
| 2310 | "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32, |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2311 | string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region); |
| 2312 | } |
| 2313 | } |
| 2314 | if (skip) break; |
| 2315 | } |
| 2316 | return skip; |
| 2317 | } |
| 2318 | |
| 2319 | void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 2320 | VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) { |
| 2321 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 2322 | assert(cb_access_context); |
John Zulauf | 2b151bf | 2020-04-24 15:37:44 -0600 | [diff] [blame] | 2323 | const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2324 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 2325 | assert(context); |
| 2326 | |
| 2327 | const auto *src_image = Get<IMAGE_STATE>(srcImage); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2328 | auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer); |
| 2329 | 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] | 2330 | const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2331 | |
| 2332 | for (uint32_t region = 0; region < regionCount; region++) { |
| 2333 | const auto ©_region = pRegions[region]; |
| 2334 | if (src_image) { |
John Zulauf | 540266b | 2020-04-06 18:54:53 -0600 | [diff] [blame] | 2335 | context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource, |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 2336 | copy_region.imageOffset, copy_region.imageExtent, tag); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2337 | } |
| 2338 | if (dst_buffer) { |
John Zulauf | 355e49b | 2020-04-24 15:11:15 -0600 | [diff] [blame] | 2339 | ResourceAccessRange dst_range = |
| 2340 | MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format)); |
John Zulauf | 16adfc9 | 2020-04-08 10:28:33 -0600 | [diff] [blame] | 2341 | context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2342 | } |
| 2343 | } |
| 2344 | } |
| 2345 | |
| 2346 | bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 2347 | VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, |
| 2348 | const VkImageBlit *pRegions, VkFilter filter) const { |
| 2349 | bool skip = false; |
| 2350 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 2351 | assert(cb_access_context); |
| 2352 | if (!cb_access_context) return skip; |
| 2353 | |
| 2354 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 2355 | assert(context); |
| 2356 | if (!context) return skip; |
| 2357 | |
| 2358 | const auto *src_image = Get<IMAGE_STATE>(srcImage); |
| 2359 | const auto *dst_image = Get<IMAGE_STATE>(dstImage); |
| 2360 | |
| 2361 | for (uint32_t region = 0; region < regionCount; region++) { |
| 2362 | const auto &blit_region = pRegions[region]; |
| 2363 | if (src_image) { |
| 2364 | VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x), |
| 2365 | static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y), |
| 2366 | 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] | 2367 | 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] | 2368 | blit_region.srcOffsets[0], extent); |
| 2369 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 2370 | skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard), |
| 2371 | "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard), |
| 2372 | report_data->FormatHandle(srcImage).c_str(), region); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2373 | } |
| 2374 | } |
| 2375 | |
| 2376 | if (dst_image) { |
| 2377 | VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x), |
| 2378 | static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y), |
| 2379 | 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] | 2380 | 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] | 2381 | blit_region.dstOffsets[0], extent); |
| 2382 | if (hazard.hazard) { |
locke-lunarg | a000365 | 2020-03-10 11:38:51 -0600 | [diff] [blame] | 2383 | skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard), |
| 2384 | "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard), |
| 2385 | report_data->FormatHandle(dstImage).c_str(), region); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2386 | } |
| 2387 | if (skip) break; |
| 2388 | } |
| 2389 | } |
| 2390 | |
| 2391 | return skip; |
| 2392 | } |
| 2393 | |
| 2394 | void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 2395 | VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, |
| 2396 | const VkImageBlit *pRegions, VkFilter filter) { |
| 2397 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 2398 | assert(cb_access_context); |
John Zulauf | 2b151bf | 2020-04-24 15:37:44 -0600 | [diff] [blame] | 2399 | const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2400 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 2401 | assert(context); |
| 2402 | |
| 2403 | auto *src_image = Get<IMAGE_STATE>(srcImage); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2404 | auto *dst_image = Get<IMAGE_STATE>(dstImage); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2405 | |
| 2406 | for (uint32_t region = 0; region < regionCount; region++) { |
| 2407 | const auto &blit_region = pRegions[region]; |
| 2408 | if (src_image) { |
| 2409 | VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x), |
| 2410 | static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y), |
| 2411 | 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] | 2412 | context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 2413 | blit_region.srcOffsets[0], extent, tag); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2414 | } |
| 2415 | if (dst_image) { |
| 2416 | VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x), |
| 2417 | static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y), |
| 2418 | 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] | 2419 | context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, |
John Zulauf | 5f13a79 | 2020-03-10 07:31:21 -0600 | [diff] [blame] | 2420 | blit_region.dstOffsets[0], extent, tag); |
locke-lunarg | a19c71d | 2020-03-02 18:17:04 -0700 | [diff] [blame] | 2421 | } |
| 2422 | } |
| 2423 | } |
locke-lunarg | 36ba259 | 2020-04-03 09:42:04 -0600 | [diff] [blame] | 2424 | |
| 2425 | bool SyncValidator::DetectDescriptorSetHazard(const CMD_BUFFER_STATE &cmd, VkPipelineBindPoint pipelineBindPoint, |
| 2426 | const char *function) const { |
| 2427 | bool skip = false; |
| 2428 | |
| 2429 | const auto last_bound_it = cmd.lastBound.find(pipelineBindPoint); |
| 2430 | if (last_bound_it == cmd.lastBound.cend()) { |
| 2431 | return skip; |
| 2432 | } |
| 2433 | auto const &state = last_bound_it->second; |
| 2434 | const auto *pPipe = state.pipeline_state; |
| 2435 | if (!pPipe) { |
| 2436 | return skip; |
| 2437 | } |
| 2438 | |
| 2439 | const auto *cb_access_context = GetAccessContext(cmd.commandBuffer); |
| 2440 | assert(cb_access_context); |
| 2441 | if (!cb_access_context) return skip; |
| 2442 | |
| 2443 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 2444 | assert(context); |
| 2445 | if (!context) return skip; |
| 2446 | |
| 2447 | using DescriptorClass = cvdescriptorset::DescriptorClass; |
| 2448 | using BufferDescriptor = cvdescriptorset::BufferDescriptor; |
| 2449 | using ImageDescriptor = cvdescriptorset::ImageDescriptor; |
| 2450 | using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor; |
| 2451 | using TexelDescriptor = cvdescriptorset::TexelDescriptor; |
| 2452 | |
| 2453 | for (const auto &set_binding_pair : pPipe->active_slots) { |
| 2454 | uint32_t setIndex = set_binding_pair.first; |
| 2455 | cvdescriptorset::DescriptorSet *descriptor_set = state.per_set[setIndex].bound_descriptor_set; |
| 2456 | for (auto binding_pair : set_binding_pair.second) { |
| 2457 | auto binding = binding_pair.first; |
| 2458 | cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(), |
| 2459 | binding_pair.first); |
| 2460 | const auto descriptor_type = binding_it.GetType(); |
| 2461 | cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange(); |
| 2462 | auto array_idx = 0; |
| 2463 | |
| 2464 | if (binding_it.IsVariableDescriptorCount()) { |
| 2465 | index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount(); |
| 2466 | } |
| 2467 | for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) { |
| 2468 | uint32_t index = i - index_range.start; |
| 2469 | const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i); |
| 2470 | if (descriptor->GetClass() == DescriptorClass::ImageSampler || descriptor->GetClass() == DescriptorClass::Image) { |
| 2471 | const IMAGE_VIEW_STATE *img_view_state = nullptr; |
| 2472 | if (descriptor->GetClass() == DescriptorClass::ImageSampler) { |
| 2473 | img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState(); |
| 2474 | } else { |
| 2475 | img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState(); |
| 2476 | } |
| 2477 | if (!img_view_state) continue; |
| 2478 | SyncStageAccessIndex sync_index; |
| 2479 | if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) { |
| 2480 | sync_index = SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ; |
| 2481 | } else { |
| 2482 | sync_index = SYNC_VERTEX_SHADER_SHADER_READ; |
| 2483 | } |
| 2484 | const IMAGE_STATE *img_state = img_view_state->image_state.get(); |
locke-lunarg | 93d68af | 2020-05-12 17:18:03 -0600 | [diff] [blame^] | 2485 | auto hazard = context->DetectHazard(*img_state, sync_index, img_view_state->normalized_subresource_range, |
| 2486 | {0, 0, 0}, img_state->createInfo.extent); |
locke-lunarg | 36ba259 | 2020-04-03 09:42:04 -0600 | [diff] [blame] | 2487 | if (hazard.hazard) { |
| 2488 | skip |= LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard), |
| 2489 | "%s: Hazard %s for %s in %s and %s binding #%d index %d", function, |
| 2490 | string_SyncHazard(hazard.hazard), |
| 2491 | report_data->FormatHandle(img_view_state->image_view).c_str(), |
| 2492 | report_data->FormatHandle(cmd.commandBuffer).c_str(), |
| 2493 | report_data->FormatHandle(descriptor_set->GetSet()).c_str(), binding, index); |
| 2494 | } |
| 2495 | } else if (descriptor->GetClass() == DescriptorClass::TexelBuffer) { |
| 2496 | auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState(); |
| 2497 | if (!buf_view_state) continue; |
| 2498 | const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get(); |
| 2499 | ResourceAccessRange range = |
| 2500 | MakeRange(buf_view_state->create_info.offset, |
| 2501 | GetRealWholeSize(buf_view_state->create_info.offset, buf_view_state->create_info.range, |
| 2502 | buf_state->createInfo.size)); |
| 2503 | auto hazard = context->DetectHazard(*buf_state, SYNC_VERTEX_SHADER_SHADER_READ, range); |
| 2504 | if (hazard.hazard) { |
| 2505 | skip |= LogError(buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard), |
| 2506 | "%s: Hazard %s for %s in %s and %s binding #%d index %d", function, |
| 2507 | string_SyncHazard(hazard.hazard), |
| 2508 | report_data->FormatHandle(buf_view_state->buffer_view).c_str(), |
| 2509 | report_data->FormatHandle(cmd.commandBuffer).c_str(), |
| 2510 | report_data->FormatHandle(descriptor_set->GetSet()).c_str(), binding, index); |
| 2511 | } |
| 2512 | } else if (descriptor->GetClass() == DescriptorClass::GeneralBuffer) { |
| 2513 | auto buf_state = static_cast<const BufferDescriptor *>(descriptor)->GetBufferState(); |
| 2514 | if (!buf_state) continue; |
| 2515 | ResourceAccessRange range = MakeRange(0, buf_state->createInfo.size); |
| 2516 | SyncStageAccessIndex sync_index; |
| 2517 | if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || |
| 2518 | descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) { |
| 2519 | sync_index = SYNC_VERTEX_SHADER_UNIFORM_READ; |
| 2520 | } else { |
| 2521 | sync_index = SYNC_VERTEX_SHADER_SHADER_READ; |
| 2522 | } |
| 2523 | auto hazard = context->DetectHazard(*buf_state, sync_index, range); |
| 2524 | if (hazard.hazard) { |
| 2525 | skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard), |
| 2526 | "%s: Hazard %s for %s in %s and %s binding #%d index %d", function, |
| 2527 | string_SyncHazard(hazard.hazard), report_data->FormatHandle(buf_state->buffer).c_str(), |
| 2528 | report_data->FormatHandle(cmd.commandBuffer).c_str(), |
| 2529 | report_data->FormatHandle(descriptor_set->GetSet()).c_str(), binding, index); |
| 2530 | } |
| 2531 | } |
| 2532 | } |
| 2533 | } |
| 2534 | } |
| 2535 | return skip; |
| 2536 | } |
| 2537 | |
| 2538 | void SyncValidator::UpdateDescriptorSetAccessState(const CMD_BUFFER_STATE &cmd, CMD_TYPE command, |
| 2539 | VkPipelineBindPoint pipelineBindPoint) { |
| 2540 | const auto last_bound_it = cmd.lastBound.find(pipelineBindPoint); |
| 2541 | if (last_bound_it == cmd.lastBound.cend()) { |
| 2542 | return; |
| 2543 | } |
| 2544 | auto const &state = last_bound_it->second; |
| 2545 | const auto *pPipe = state.pipeline_state; |
| 2546 | if (!pPipe) { |
| 2547 | return; |
| 2548 | } |
| 2549 | |
| 2550 | auto *cb_access_context = GetAccessContext(cmd.commandBuffer); |
| 2551 | assert(cb_access_context); |
| 2552 | const auto tag = cb_access_context->NextCommandTag(command); |
| 2553 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 2554 | assert(context); |
| 2555 | |
| 2556 | using DescriptorClass = cvdescriptorset::DescriptorClass; |
| 2557 | using BufferDescriptor = cvdescriptorset::BufferDescriptor; |
| 2558 | using ImageDescriptor = cvdescriptorset::ImageDescriptor; |
| 2559 | using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor; |
| 2560 | using TexelDescriptor = cvdescriptorset::TexelDescriptor; |
| 2561 | |
| 2562 | for (const auto &set_binding_pair : pPipe->active_slots) { |
| 2563 | uint32_t setIndex = set_binding_pair.first; |
| 2564 | const cvdescriptorset::DescriptorSet *descriptor_set = state.per_set[setIndex].bound_descriptor_set; |
| 2565 | for (auto binding_pair : set_binding_pair.second) { |
| 2566 | cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(), |
| 2567 | binding_pair.first); |
| 2568 | const auto descriptor_type = binding_it.GetType(); |
| 2569 | cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange(); |
| 2570 | auto array_idx = 0; |
| 2571 | |
| 2572 | if (binding_it.IsVariableDescriptorCount()) { |
| 2573 | index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount(); |
| 2574 | } |
| 2575 | |
| 2576 | for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) { |
| 2577 | const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i); |
| 2578 | if (descriptor->GetClass() == DescriptorClass::ImageSampler || descriptor->GetClass() == DescriptorClass::Image) { |
| 2579 | const IMAGE_VIEW_STATE *img_view_state = nullptr; |
| 2580 | if (descriptor->GetClass() == DescriptorClass::ImageSampler) { |
| 2581 | img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState(); |
| 2582 | } else { |
| 2583 | img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState(); |
| 2584 | } |
| 2585 | if (!img_view_state) continue; |
| 2586 | SyncStageAccessIndex sync_index; |
| 2587 | if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) { |
| 2588 | sync_index = SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ; |
| 2589 | } else { |
| 2590 | sync_index = SYNC_VERTEX_SHADER_SHADER_READ; |
| 2591 | } |
| 2592 | const IMAGE_STATE *img_state = img_view_state->image_state.get(); |
locke-lunarg | 93d68af | 2020-05-12 17:18:03 -0600 | [diff] [blame^] | 2593 | context->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range, {0, 0, 0}, |
| 2594 | img_state->createInfo.extent, tag); |
locke-lunarg | 36ba259 | 2020-04-03 09:42:04 -0600 | [diff] [blame] | 2595 | |
| 2596 | } else if (descriptor->GetClass() == DescriptorClass::TexelBuffer) { |
| 2597 | auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState(); |
| 2598 | if (!buf_view_state) continue; |
| 2599 | const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get(); |
| 2600 | ResourceAccessRange range = MakeRange(buf_view_state->create_info.offset, buf_view_state->create_info.range); |
| 2601 | context->UpdateAccessState(*buf_state, SYNC_VERTEX_SHADER_SHADER_READ, range, tag); |
| 2602 | |
| 2603 | } else if (descriptor->GetClass() == DescriptorClass::GeneralBuffer) { |
| 2604 | auto buf_state = static_cast<const BufferDescriptor *>(descriptor)->GetBufferState(); |
| 2605 | if (!buf_state) continue; |
| 2606 | SyncStageAccessIndex sync_index; |
| 2607 | if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || |
| 2608 | descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) { |
| 2609 | sync_index = SYNC_VERTEX_SHADER_UNIFORM_READ; |
| 2610 | } else { |
| 2611 | sync_index = SYNC_VERTEX_SHADER_SHADER_READ; |
| 2612 | } |
| 2613 | ResourceAccessRange range = MakeRange(0, buf_state->createInfo.size); |
| 2614 | context->UpdateAccessState(*buf_state, sync_index, range, tag); |
| 2615 | } |
| 2616 | } |
| 2617 | } |
| 2618 | } |
| 2619 | } |
| 2620 | |
locke-lunarg | a4d39ea | 2020-05-22 14:17:29 -0600 | [diff] [blame] | 2621 | bool SyncValidator::DetectVertexHazard(const CMD_BUFFER_STATE &cmd, uint32_t vertexCount, uint32_t firstVertex, |
| 2622 | const char *function) const { |
| 2623 | bool skip = false; |
| 2624 | const auto last_bound_it = cmd.lastBound.find(VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 2625 | if (last_bound_it == cmd.lastBound.cend()) { |
| 2626 | return skip; |
| 2627 | } |
| 2628 | auto const &state = last_bound_it->second; |
| 2629 | const auto *pPipe = state.pipeline_state; |
| 2630 | if (!pPipe) { |
| 2631 | return skip; |
| 2632 | } |
| 2633 | |
| 2634 | const auto *cb_access_context = GetAccessContext(cmd.commandBuffer); |
| 2635 | assert(cb_access_context); |
| 2636 | if (!cb_access_context) return skip; |
| 2637 | |
| 2638 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 2639 | assert(context); |
| 2640 | if (!context) return skip; |
| 2641 | |
| 2642 | const auto &binding_buffers = cmd.current_vertex_buffer_binding_info.vertex_buffer_bindings; |
| 2643 | const auto &binding_buffers_size = binding_buffers.size(); |
| 2644 | const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size(); |
| 2645 | |
| 2646 | for (size_t i = 0; i < binding_descriptions_size; ++i) { |
| 2647 | const auto &binding_description = pPipe->vertex_binding_descriptions_[i]; |
| 2648 | if (binding_description.binding < binding_buffers_size) { |
| 2649 | const auto &binding_buffer = binding_buffers[binding_description.binding]; |
| 2650 | if (binding_buffer.buffer == VK_NULL_HANDLE) continue; |
| 2651 | |
| 2652 | auto *buf_state = Get<BUFFER_STATE>(binding_buffer.buffer); |
| 2653 | VkDeviceSize range_start = binding_buffer.offset + firstVertex * binding_description.stride; |
| 2654 | VkDeviceSize range_size = 0; |
| 2655 | if (vertexCount == UINT32_MAX) { |
| 2656 | range_size = buf_state->createInfo.size - range_start; |
| 2657 | } else { |
| 2658 | range_size = vertexCount * binding_description.stride; |
| 2659 | } |
| 2660 | ResourceAccessRange range = MakeRange(range_start, range_size); |
| 2661 | auto hazard = context->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range); |
| 2662 | if (hazard.hazard) { |
| 2663 | skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s", |
| 2664 | function, string_SyncHazard(hazard.hazard), report_data->FormatHandle(buf_state->buffer).c_str(), |
| 2665 | report_data->FormatHandle(cmd.commandBuffer).c_str()); |
| 2666 | } |
| 2667 | } |
| 2668 | } |
| 2669 | return skip; |
| 2670 | } |
| 2671 | |
| 2672 | void SyncValidator::UpdateVertexAccessState(const CMD_BUFFER_STATE &cmd, CMD_TYPE command, uint32_t vertexCount, |
| 2673 | uint32_t firstVertex) { |
| 2674 | const auto last_bound_it = cmd.lastBound.find(VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 2675 | if (last_bound_it == cmd.lastBound.cend()) { |
| 2676 | return; |
| 2677 | } |
| 2678 | auto const &state = last_bound_it->second; |
| 2679 | const auto *pPipe = state.pipeline_state; |
| 2680 | if (!pPipe) { |
| 2681 | return; |
| 2682 | } |
| 2683 | |
| 2684 | auto *cb_access_context = GetAccessContext(cmd.commandBuffer); |
| 2685 | assert(cb_access_context); |
| 2686 | const auto tag = cb_access_context->NextCommandTag(command); |
| 2687 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 2688 | assert(context); |
| 2689 | |
| 2690 | const auto &binding_buffers = cmd.current_vertex_buffer_binding_info.vertex_buffer_bindings; |
| 2691 | const auto &binding_buffers_size = binding_buffers.size(); |
| 2692 | const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size(); |
| 2693 | |
| 2694 | for (size_t i = 0; i < binding_descriptions_size; ++i) { |
| 2695 | const auto &binding_description = pPipe->vertex_binding_descriptions_[i]; |
| 2696 | if (binding_description.binding < binding_buffers_size) { |
| 2697 | const auto &binding_buffer = binding_buffers[binding_description.binding]; |
| 2698 | if (binding_buffer.buffer == VK_NULL_HANDLE) continue; |
| 2699 | |
| 2700 | auto *buf_state = Get<BUFFER_STATE>(binding_buffer.buffer); |
| 2701 | VkDeviceSize range_start = binding_buffer.offset + firstVertex * binding_description.stride; |
| 2702 | VkDeviceSize range_size = 0; |
| 2703 | if (vertexCount == UINT32_MAX) { |
| 2704 | range_size = buf_state->createInfo.size - range_start; |
| 2705 | } else { |
| 2706 | range_size = vertexCount * binding_description.stride; |
| 2707 | } |
| 2708 | ResourceAccessRange range = MakeRange(range_start, range_size); |
| 2709 | context->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag); |
| 2710 | } |
| 2711 | } |
| 2712 | } |
| 2713 | |
| 2714 | bool SyncValidator::DetectVertexIndexHazard(const CMD_BUFFER_STATE &cmd, uint32_t indexCount, uint32_t firstIndex, |
| 2715 | const char *function) const { |
| 2716 | bool skip = false; |
| 2717 | if (cmd.index_buffer_binding.buffer == VK_NULL_HANDLE) return skip; |
| 2718 | |
| 2719 | const auto *cb_access_context = GetAccessContext(cmd.commandBuffer); |
| 2720 | assert(cb_access_context); |
| 2721 | if (!cb_access_context) return skip; |
| 2722 | |
| 2723 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 2724 | assert(context); |
| 2725 | if (!context) return skip; |
| 2726 | |
| 2727 | auto *index_buf_state = Get<BUFFER_STATE>(cmd.index_buffer_binding.buffer); |
| 2728 | const auto index_size = GetIndexAlignment(cmd.index_buffer_binding.index_type); |
| 2729 | VkDeviceSize range_start = cmd.index_buffer_binding.offset + firstIndex * index_size; |
| 2730 | VkDeviceSize range_size = indexCount * index_size; |
| 2731 | ResourceAccessRange range = MakeRange(range_start, range_size); |
| 2732 | auto hazard = context->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range); |
| 2733 | if (hazard.hazard) { |
| 2734 | skip |= LogError(index_buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s", |
| 2735 | function, string_SyncHazard(hazard.hazard), report_data->FormatHandle(index_buf_state->buffer).c_str(), |
| 2736 | report_data->FormatHandle(cmd.commandBuffer).c_str()); |
| 2737 | } |
| 2738 | |
| 2739 | // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue. |
| 2740 | // We will detect more accurate range in the future. |
| 2741 | skip |= DetectVertexHazard(cmd, UINT32_MAX, 0, function); |
| 2742 | return skip; |
| 2743 | } |
| 2744 | |
| 2745 | void SyncValidator::UpdateVertexIndexAccessState(const CMD_BUFFER_STATE &cmd, CMD_TYPE command, uint32_t indexCount, |
| 2746 | uint32_t firstIndex) { |
| 2747 | if (cmd.index_buffer_binding.buffer == VK_NULL_HANDLE) return; |
| 2748 | |
| 2749 | auto *cb_access_context = GetAccessContext(cmd.commandBuffer); |
| 2750 | assert(cb_access_context); |
| 2751 | const auto tag = cb_access_context->NextCommandTag(command); |
| 2752 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 2753 | assert(context); |
| 2754 | |
| 2755 | auto *index_buf_state = Get<BUFFER_STATE>(cmd.index_buffer_binding.buffer); |
| 2756 | const auto index_size = GetIndexAlignment(cmd.index_buffer_binding.index_type); |
| 2757 | VkDeviceSize range_start = cmd.index_buffer_binding.offset + firstIndex * index_size; |
| 2758 | VkDeviceSize range_size = indexCount * index_size; |
| 2759 | ResourceAccessRange range = MakeRange(range_start, range_size); |
| 2760 | context->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag); |
| 2761 | |
| 2762 | // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue. |
| 2763 | // We will detect more accurate range in the future. |
| 2764 | UpdateVertexAccessState(cmd, command, UINT32_MAX, 0); |
| 2765 | } |
| 2766 | |
locke-lunarg | 93d68af | 2020-05-12 17:18:03 -0600 | [diff] [blame^] | 2767 | bool SyncValidator::DetectSubpassAttachmentHazard(const CMD_BUFFER_STATE &cmd, const char *function) const { |
| 2768 | bool skip = false; |
| 2769 | |
| 2770 | const auto *cb_access_context = GetAccessContext(cmd.commandBuffer); |
| 2771 | assert(cb_access_context); |
| 2772 | if (!cb_access_context) return skip; |
| 2773 | |
| 2774 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 2775 | assert(context); |
| 2776 | if (!context) return skip; |
| 2777 | |
| 2778 | const auto &subpass = cmd.activeRenderPass->createInfo.pSubpasses[cmd.activeSubpass]; |
| 2779 | const auto *framebuffer = cmd.activeFramebuffer.get(); |
| 2780 | VkExtent3D framebuffer_extent = {framebuffer->createInfo.width, framebuffer->createInfo.height, framebuffer->createInfo.layers}; |
| 2781 | |
| 2782 | auto dtct_fn = [&cmd, function, framebuffer, &framebuffer_extent, context]( |
| 2783 | const SyncValidator &this_, const safe_VkAttachmentReference2 &attachment_ref, |
| 2784 | const SyncStageAccessIndex sync_index, const std::string &attachment_desription) { |
| 2785 | if (attachment_ref.attachment == VK_ATTACHMENT_UNUSED) return false; |
| 2786 | auto attachment_index = attachment_ref.attachment; |
| 2787 | if (framebuffer->createInfo.attachmentCount > attachment_index) { |
| 2788 | const IMAGE_VIEW_STATE *img_view_state = |
| 2789 | this_.Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[attachment_index]); |
| 2790 | if (!img_view_state) return false; |
| 2791 | const IMAGE_STATE *img_state = img_view_state->image_state.get(); |
| 2792 | HazardResult hazard; |
| 2793 | if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) { |
| 2794 | hazard = context->DetectHazard(*img_state, sync_index, img_view_state->normalized_subresource_range, {0, 0, 0}, |
| 2795 | framebuffer_extent); |
| 2796 | } else if (sync_index == SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE) { |
| 2797 | if (attachment_desription.compare("pDepthStencilAttachment") == 0) { |
| 2798 | hazard = context->DetectHazard(*img_state, sync_index, img_view_state->normalized_subresource_range, |
| 2799 | kDepthStencilAttachmentRasterOrder, {0, 0, 0}, framebuffer_extent); |
| 2800 | } else { |
| 2801 | hazard = context->DetectHazard(*img_state, sync_index, img_view_state->normalized_subresource_range, |
| 2802 | kColorAttachmentRasterOrder, {0, 0, 0}, framebuffer_extent); |
| 2803 | } |
| 2804 | } |
| 2805 | |
| 2806 | if (hazard.hazard) { |
| 2807 | return this_.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard), |
| 2808 | "%s: Hazard %s for %s in %s, Subpass #%d, and %s", function, string_SyncHazard(hazard.hazard), |
| 2809 | this_.report_data->FormatHandle(img_view_state->image_view).c_str(), |
| 2810 | this_.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass, |
| 2811 | attachment_desription.c_str()); |
| 2812 | } |
| 2813 | } |
| 2814 | return false; |
| 2815 | }; |
| 2816 | |
| 2817 | if (subpass.inputAttachmentCount && subpass.pInputAttachments) { |
| 2818 | for (uint32_t i = 0; i < subpass.inputAttachmentCount; ++i) { |
| 2819 | std::string attachment_desription = "pInputAttachments #" + std::to_string(i); |
| 2820 | skip |= dtct_fn(*this, subpass.pInputAttachments[i], SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, attachment_desription); |
| 2821 | } |
| 2822 | } |
| 2823 | if (subpass.colorAttachmentCount) { |
| 2824 | if (subpass.pColorAttachments) { |
| 2825 | for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) { |
| 2826 | std::string attachment_desription = "pColorAttachments #" + std::to_string(i); |
| 2827 | skip |= dtct_fn(*this, subpass.pColorAttachments[i], SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, |
| 2828 | attachment_desription); |
| 2829 | } |
| 2830 | } |
| 2831 | if (subpass.pResolveAttachments) { |
| 2832 | for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) { |
| 2833 | std::string attachment_desription = "pResolveAttachments #" + std::to_string(i); |
| 2834 | skip |= dtct_fn(*this, subpass.pResolveAttachments[i], SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, |
| 2835 | attachment_desription); |
| 2836 | } |
| 2837 | } |
| 2838 | } |
| 2839 | if (subpass.pDepthStencilAttachment) { |
| 2840 | skip |= dtct_fn(*this, *subpass.pDepthStencilAttachment, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, |
| 2841 | "pDepthStencilAttachment"); |
| 2842 | } |
| 2843 | return skip; |
| 2844 | } |
| 2845 | |
| 2846 | void SyncValidator::UpdateSubpassAttachmentAccessState(const CMD_BUFFER_STATE &cmd, CMD_TYPE command) { |
| 2847 | auto *cb_access_context = GetAccessContext(cmd.commandBuffer); |
| 2848 | assert(cb_access_context); |
| 2849 | const auto tag = cb_access_context->NextCommandTag(command); |
| 2850 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 2851 | assert(context); |
| 2852 | |
| 2853 | const auto &subpass = cmd.activeRenderPass->createInfo.pSubpasses[cmd.activeSubpass]; |
| 2854 | const auto *framebuffer = cmd.activeFramebuffer.get(); |
| 2855 | VkExtent3D framebuffer_extent = {framebuffer->createInfo.width, framebuffer->createInfo.height, framebuffer->createInfo.layers}; |
| 2856 | |
| 2857 | auto updt_fn = [&cmd, framebuffer, &framebuffer_extent, context, &tag](const SyncValidator &this_, |
| 2858 | const safe_VkAttachmentReference2 &attachment_ref, |
| 2859 | const SyncStageAccessIndex sync_index) { |
| 2860 | if (attachment_ref.attachment == VK_ATTACHMENT_UNUSED) return; |
| 2861 | auto attachment_index = attachment_ref.attachment; |
| 2862 | if (framebuffer->createInfo.attachmentCount > attachment_index) { |
| 2863 | const IMAGE_VIEW_STATE *img_view_state = |
| 2864 | this_.Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[attachment_index]); |
| 2865 | if (!img_view_state) return; |
| 2866 | const IMAGE_STATE *img_state = img_view_state->image_state.get(); |
| 2867 | context->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range, {0, 0, 0}, |
| 2868 | framebuffer_extent, tag); |
| 2869 | } |
| 2870 | }; |
| 2871 | |
| 2872 | if (subpass.inputAttachmentCount && subpass.pInputAttachments) { |
| 2873 | for (uint32_t i = 0; i < subpass.inputAttachmentCount; ++i) { |
| 2874 | updt_fn(*this, subpass.pInputAttachments[i], SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ); |
| 2875 | } |
| 2876 | } |
| 2877 | if (subpass.colorAttachmentCount) { |
| 2878 | if (subpass.pColorAttachments) { |
| 2879 | for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) { |
| 2880 | updt_fn(*this, subpass.pColorAttachments[i], SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE); |
| 2881 | } |
| 2882 | } |
| 2883 | if (subpass.pResolveAttachments) { |
| 2884 | for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) { |
| 2885 | updt_fn(*this, subpass.pResolveAttachments[i], SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE); |
| 2886 | } |
| 2887 | } |
| 2888 | } |
| 2889 | if (subpass.pDepthStencilAttachment) { |
| 2890 | updt_fn(*this, *subpass.pDepthStencilAttachment, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE); |
| 2891 | } |
| 2892 | } |
| 2893 | |
locke-lunarg | 36ba259 | 2020-04-03 09:42:04 -0600 | [diff] [blame] | 2894 | bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const { |
| 2895 | const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer); |
| 2896 | return DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch"); |
| 2897 | } |
| 2898 | |
| 2899 | void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) { |
| 2900 | const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer); |
| 2901 | UpdateDescriptorSetAccessState(*cb_state, CMD_DISPATCH, VK_PIPELINE_BIND_POINT_COMPUTE); |
| 2902 | } |
locke-lunarg | e1a6702 | 2020-04-29 00:15:36 -0600 | [diff] [blame] | 2903 | |
| 2904 | bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const { |
| 2905 | const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer); |
| 2906 | return DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect"); |
| 2907 | } |
| 2908 | |
| 2909 | void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) { |
| 2910 | const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer); |
| 2911 | UpdateDescriptorSetAccessState(*cb_state, CMD_DISPATCHINDIRECT, VK_PIPELINE_BIND_POINT_COMPUTE); |
| 2912 | } |
| 2913 | |
| 2914 | bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount, |
| 2915 | uint32_t firstVertex, uint32_t firstInstance) const { |
locke-lunarg | a4d39ea | 2020-05-22 14:17:29 -0600 | [diff] [blame] | 2916 | bool skip = false; |
locke-lunarg | e1a6702 | 2020-04-29 00:15:36 -0600 | [diff] [blame] | 2917 | const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer); |
locke-lunarg | a4d39ea | 2020-05-22 14:17:29 -0600 | [diff] [blame] | 2918 | skip |= DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw"); |
| 2919 | skip |= DetectVertexHazard(*cb_state, vertexCount, firstVertex, "vkCmdDraw"); |
locke-lunarg | 93d68af | 2020-05-12 17:18:03 -0600 | [diff] [blame^] | 2920 | skip |= DetectSubpassAttachmentHazard(*cb_state, "vkCmdDraw"); |
locke-lunarg | a4d39ea | 2020-05-22 14:17:29 -0600 | [diff] [blame] | 2921 | return skip; |
locke-lunarg | e1a6702 | 2020-04-29 00:15:36 -0600 | [diff] [blame] | 2922 | } |
| 2923 | |
| 2924 | void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount, |
| 2925 | uint32_t firstVertex, uint32_t firstInstance) { |
| 2926 | const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer); |
| 2927 | UpdateDescriptorSetAccessState(*cb_state, CMD_DRAW, VK_PIPELINE_BIND_POINT_GRAPHICS); |
locke-lunarg | a4d39ea | 2020-05-22 14:17:29 -0600 | [diff] [blame] | 2928 | UpdateVertexAccessState(*cb_state, CMD_DRAW, vertexCount, firstVertex); |
locke-lunarg | 93d68af | 2020-05-12 17:18:03 -0600 | [diff] [blame^] | 2929 | UpdateSubpassAttachmentAccessState(*cb_state, CMD_DRAW); |
locke-lunarg | e1a6702 | 2020-04-29 00:15:36 -0600 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount, |
| 2933 | uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const { |
locke-lunarg | a4d39ea | 2020-05-22 14:17:29 -0600 | [diff] [blame] | 2934 | bool skip = false; |
locke-lunarg | e1a6702 | 2020-04-29 00:15:36 -0600 | [diff] [blame] | 2935 | const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer); |
locke-lunarg | a4d39ea | 2020-05-22 14:17:29 -0600 | [diff] [blame] | 2936 | skip |= DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed"); |
| 2937 | skip |= DetectVertexIndexHazard(*cb_state, indexCount, firstIndex, "vkCmdDrawIndexed"); |
locke-lunarg | 93d68af | 2020-05-12 17:18:03 -0600 | [diff] [blame^] | 2938 | skip |= DetectSubpassAttachmentHazard(*cb_state, "vkCmdDrawIndexed"); |
locke-lunarg | a4d39ea | 2020-05-22 14:17:29 -0600 | [diff] [blame] | 2939 | return skip; |
locke-lunarg | e1a6702 | 2020-04-29 00:15:36 -0600 | [diff] [blame] | 2940 | } |
| 2941 | |
| 2942 | void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount, |
| 2943 | uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) { |
| 2944 | const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer); |
| 2945 | UpdateDescriptorSetAccessState(*cb_state, CMD_DRAWINDEXED, VK_PIPELINE_BIND_POINT_GRAPHICS); |
locke-lunarg | a4d39ea | 2020-05-22 14:17:29 -0600 | [diff] [blame] | 2946 | UpdateVertexAccessState(*cb_state, CMD_DRAWINDEXED, indexCount, firstIndex); |
locke-lunarg | 93d68af | 2020-05-12 17:18:03 -0600 | [diff] [blame^] | 2947 | UpdateSubpassAttachmentAccessState(*cb_state, CMD_DRAWINDEXED); |
locke-lunarg | e1a6702 | 2020-04-29 00:15:36 -0600 | [diff] [blame] | 2948 | } |
| 2949 | |
| 2950 | bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 2951 | uint32_t drawCount, uint32_t stride) const { |
| 2952 | const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer); |
| 2953 | return DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect"); |
| 2954 | } |
| 2955 | |
| 2956 | void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 2957 | uint32_t drawCount, uint32_t stride) { |
| 2958 | const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer); |
| 2959 | UpdateDescriptorSetAccessState(*cb_state, CMD_DRAWINDIRECT, VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 2960 | } |
| 2961 | |
| 2962 | bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 2963 | uint32_t drawCount, uint32_t stride) const { |
| 2964 | const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer); |
| 2965 | return DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect"); |
| 2966 | } |
| 2967 | |
| 2968 | void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 2969 | uint32_t drawCount, uint32_t stride) { |
| 2970 | const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer); |
| 2971 | UpdateDescriptorSetAccessState(*cb_state, CMD_DRAWINDEXEDINDIRECT, VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 2972 | } |
| 2973 | |
| 2974 | bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 2975 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount, |
| 2976 | uint32_t stride) const { |
| 2977 | const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer); |
| 2978 | return DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirectCount"); |
| 2979 | } |
| 2980 | |
| 2981 | void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 2982 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount, |
| 2983 | uint32_t stride) { |
| 2984 | const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer); |
| 2985 | UpdateDescriptorSetAccessState(*cb_state, CMD_DRAWINDIRECTCOUNT, VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 2986 | } |
| 2987 | |
| 2988 | bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 2989 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, |
| 2990 | uint32_t maxDrawCount, uint32_t stride) const { |
| 2991 | const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer); |
| 2992 | return DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirectCountKHR"); |
| 2993 | } |
| 2994 | |
| 2995 | void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 2996 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, |
| 2997 | uint32_t maxDrawCount, uint32_t stride) { |
| 2998 | PostCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride); |
| 2999 | } |
| 3000 | |
| 3001 | bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 3002 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, |
| 3003 | uint32_t maxDrawCount, uint32_t stride) const { |
| 3004 | const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer); |
| 3005 | return DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirectCountAMD"); |
| 3006 | } |
| 3007 | |
| 3008 | void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 3009 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, |
| 3010 | uint32_t maxDrawCount, uint32_t stride) { |
| 3011 | PostCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride); |
| 3012 | } |
| 3013 | |
| 3014 | bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 3015 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, |
| 3016 | uint32_t maxDrawCount, uint32_t stride) const { |
| 3017 | const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer); |
| 3018 | return DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirectCount"); |
| 3019 | } |
| 3020 | |
| 3021 | void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 3022 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, |
| 3023 | uint32_t maxDrawCount, uint32_t stride) { |
| 3024 | const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer); |
| 3025 | UpdateDescriptorSetAccessState(*cb_state, CMD_DRAWINDEXEDINDIRECTCOUNT, VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 3026 | } |
| 3027 | |
| 3028 | bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, |
| 3029 | VkDeviceSize offset, VkBuffer countBuffer, |
| 3030 | VkDeviceSize countBufferOffset, uint32_t maxDrawCount, |
| 3031 | uint32_t stride) const { |
| 3032 | const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer); |
| 3033 | return DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirectCountKHR"); |
| 3034 | } |
| 3035 | |
| 3036 | void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 3037 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, |
| 3038 | uint32_t maxDrawCount, uint32_t stride) { |
| 3039 | PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride); |
| 3040 | } |
| 3041 | |
| 3042 | bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, |
| 3043 | VkDeviceSize offset, VkBuffer countBuffer, |
| 3044 | VkDeviceSize countBufferOffset, uint32_t maxDrawCount, |
| 3045 | uint32_t stride) const { |
| 3046 | const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer); |
| 3047 | return DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirectCountAMD"); |
| 3048 | } |
| 3049 | |
| 3050 | void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 3051 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, |
| 3052 | uint32_t maxDrawCount, uint32_t stride) { |
| 3053 | PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride); |
| 3054 | } |
| 3055 | |
| 3056 | bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout, |
| 3057 | const VkClearColorValue *pColor, uint32_t rangeCount, |
| 3058 | const VkImageSubresourceRange *pRanges) const { |
| 3059 | bool skip = false; |
| 3060 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 3061 | assert(cb_access_context); |
| 3062 | if (!cb_access_context) return skip; |
| 3063 | |
| 3064 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 3065 | assert(context); |
| 3066 | if (!context) return skip; |
| 3067 | |
| 3068 | const auto *image_state = Get<IMAGE_STATE>(image); |
| 3069 | |
| 3070 | for (uint32_t index = 0; index < rangeCount; index++) { |
| 3071 | const auto &range = pRanges[index]; |
| 3072 | if (image_state) { |
| 3073 | auto hazard = |
| 3074 | context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent); |
| 3075 | if (hazard.hazard) { |
| 3076 | skip |= LogError(image, string_SyncHazardVUID(hazard.hazard), |
| 3077 | "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32, string_SyncHazard(hazard.hazard), |
| 3078 | report_data->FormatHandle(image).c_str(), index); |
| 3079 | } |
| 3080 | } |
| 3081 | } |
| 3082 | return skip; |
| 3083 | } |
| 3084 | |
| 3085 | void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout, |
| 3086 | const VkClearColorValue *pColor, uint32_t rangeCount, |
| 3087 | const VkImageSubresourceRange *pRanges) { |
| 3088 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 3089 | assert(cb_access_context); |
| 3090 | const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE); |
| 3091 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 3092 | assert(context); |
| 3093 | |
| 3094 | const auto *image_state = Get<IMAGE_STATE>(image); |
| 3095 | |
| 3096 | for (uint32_t index = 0; index < rangeCount; index++) { |
| 3097 | const auto &range = pRanges[index]; |
| 3098 | if (image_state) { |
| 3099 | context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent, |
| 3100 | tag); |
| 3101 | } |
| 3102 | } |
| 3103 | } |
| 3104 | |
| 3105 | bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, |
| 3106 | VkImageLayout imageLayout, |
| 3107 | const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount, |
| 3108 | const VkImageSubresourceRange *pRanges) const { |
| 3109 | bool skip = false; |
| 3110 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 3111 | assert(cb_access_context); |
| 3112 | if (!cb_access_context) return skip; |
| 3113 | |
| 3114 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 3115 | assert(context); |
| 3116 | if (!context) return skip; |
| 3117 | |
| 3118 | const auto *image_state = Get<IMAGE_STATE>(image); |
| 3119 | |
| 3120 | for (uint32_t index = 0; index < rangeCount; index++) { |
| 3121 | const auto &range = pRanges[index]; |
| 3122 | if (image_state) { |
| 3123 | auto hazard = |
| 3124 | context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent); |
| 3125 | if (hazard.hazard) { |
| 3126 | skip |= LogError(image, string_SyncHazardVUID(hazard.hazard), |
| 3127 | "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32, |
| 3128 | string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index); |
| 3129 | } |
| 3130 | } |
| 3131 | } |
| 3132 | return skip; |
| 3133 | } |
| 3134 | |
| 3135 | void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout, |
| 3136 | const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount, |
| 3137 | const VkImageSubresourceRange *pRanges) { |
| 3138 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 3139 | assert(cb_access_context); |
| 3140 | const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE); |
| 3141 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 3142 | assert(context); |
| 3143 | |
| 3144 | const auto *image_state = Get<IMAGE_STATE>(image); |
| 3145 | |
| 3146 | for (uint32_t index = 0; index < rangeCount; index++) { |
| 3147 | const auto &range = pRanges[index]; |
| 3148 | if (image_state) { |
| 3149 | context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent, |
| 3150 | tag); |
| 3151 | } |
| 3152 | } |
| 3153 | } |
| 3154 | |
| 3155 | bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, |
| 3156 | uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer, |
| 3157 | VkDeviceSize dstOffset, VkDeviceSize stride, |
| 3158 | VkQueryResultFlags flags) const { |
| 3159 | bool skip = false; |
| 3160 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 3161 | assert(cb_access_context); |
| 3162 | if (!cb_access_context) return skip; |
| 3163 | |
| 3164 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 3165 | assert(context); |
| 3166 | if (!context) return skip; |
| 3167 | |
| 3168 | const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer); |
| 3169 | |
| 3170 | if (dst_buffer) { |
| 3171 | // TODO: size is ERROR |
| 3172 | ResourceAccessRange range = MakeRange(dstOffset, dst_buffer->createInfo.size); |
| 3173 | auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range); |
| 3174 | if (hazard.hazard) { |
| 3175 | skip |= |
| 3176 | LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s", |
| 3177 | string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str()); |
| 3178 | } |
| 3179 | } |
| 3180 | return skip; |
| 3181 | } |
| 3182 | |
| 3183 | void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery, |
| 3184 | uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset, |
| 3185 | VkDeviceSize stride, VkQueryResultFlags flags) { |
| 3186 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 3187 | assert(cb_access_context); |
| 3188 | const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE); |
| 3189 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 3190 | assert(context); |
| 3191 | |
| 3192 | const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer); |
| 3193 | |
| 3194 | if (dst_buffer) { |
| 3195 | // TODO: size is ERROR |
| 3196 | ResourceAccessRange range = MakeRange(dstOffset, dst_buffer->createInfo.size); |
| 3197 | context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag); |
| 3198 | } |
| 3199 | } |
| 3200 | |
| 3201 | bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset, |
| 3202 | VkDeviceSize size, uint32_t data) const { |
| 3203 | bool skip = false; |
| 3204 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 3205 | assert(cb_access_context); |
| 3206 | if (!cb_access_context) return skip; |
| 3207 | |
| 3208 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 3209 | assert(context); |
| 3210 | if (!context) return skip; |
| 3211 | |
| 3212 | const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer); |
| 3213 | |
| 3214 | if (dst_buffer) { |
| 3215 | ResourceAccessRange range = MakeRange(dstOffset, size); |
| 3216 | auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range); |
| 3217 | if (hazard.hazard) { |
| 3218 | skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdFillBuffer: Hazard %s for dstBuffer %s", |
| 3219 | string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str()); |
| 3220 | } |
| 3221 | } |
| 3222 | return skip; |
| 3223 | } |
| 3224 | |
| 3225 | void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset, |
| 3226 | VkDeviceSize size, uint32_t data) { |
| 3227 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 3228 | assert(cb_access_context); |
| 3229 | const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER); |
| 3230 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 3231 | assert(context); |
| 3232 | |
| 3233 | const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer); |
| 3234 | |
| 3235 | if (dst_buffer) { |
| 3236 | ResourceAccessRange range = MakeRange(dstOffset, size); |
| 3237 | context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag); |
| 3238 | } |
| 3239 | } |
| 3240 | |
| 3241 | bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 3242 | VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, |
| 3243 | const VkImageResolve *pRegions) const { |
| 3244 | bool skip = false; |
| 3245 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 3246 | assert(cb_access_context); |
| 3247 | if (!cb_access_context) return skip; |
| 3248 | |
| 3249 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 3250 | assert(context); |
| 3251 | if (!context) return skip; |
| 3252 | |
| 3253 | const auto *src_image = Get<IMAGE_STATE>(srcImage); |
| 3254 | const auto *dst_image = Get<IMAGE_STATE>(dstImage); |
| 3255 | |
| 3256 | for (uint32_t region = 0; region < regionCount; region++) { |
| 3257 | const auto &resolve_region = pRegions[region]; |
| 3258 | if (src_image) { |
| 3259 | auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource, |
| 3260 | resolve_region.srcOffset, resolve_region.extent); |
| 3261 | if (hazard.hazard) { |
| 3262 | skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard), |
| 3263 | "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard), |
| 3264 | report_data->FormatHandle(srcImage).c_str(), region); |
| 3265 | } |
| 3266 | } |
| 3267 | |
| 3268 | if (dst_image) { |
| 3269 | auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource, |
| 3270 | resolve_region.dstOffset, resolve_region.extent); |
| 3271 | if (hazard.hazard) { |
| 3272 | skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard), |
| 3273 | "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard), |
| 3274 | report_data->FormatHandle(dstImage).c_str(), region); |
| 3275 | } |
| 3276 | if (skip) break; |
| 3277 | } |
| 3278 | } |
| 3279 | |
| 3280 | return skip; |
| 3281 | } |
| 3282 | |
| 3283 | void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, |
| 3284 | VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, |
| 3285 | const VkImageResolve *pRegions) { |
| 3286 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 3287 | assert(cb_access_context); |
| 3288 | const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE); |
| 3289 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 3290 | assert(context); |
| 3291 | |
| 3292 | auto *src_image = Get<IMAGE_STATE>(srcImage); |
| 3293 | auto *dst_image = Get<IMAGE_STATE>(dstImage); |
| 3294 | |
| 3295 | for (uint32_t region = 0; region < regionCount; region++) { |
| 3296 | const auto &resolve_region = pRegions[region]; |
| 3297 | if (src_image) { |
| 3298 | context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource, |
| 3299 | resolve_region.srcOffset, resolve_region.extent, tag); |
| 3300 | } |
| 3301 | if (dst_image) { |
| 3302 | context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource, |
| 3303 | resolve_region.dstOffset, resolve_region.extent, tag); |
| 3304 | } |
| 3305 | } |
| 3306 | } |
| 3307 | |
| 3308 | bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset, |
| 3309 | VkDeviceSize dataSize, const void *pData) const { |
| 3310 | bool skip = false; |
| 3311 | const auto *cb_access_context = GetAccessContext(commandBuffer); |
| 3312 | assert(cb_access_context); |
| 3313 | if (!cb_access_context) return skip; |
| 3314 | |
| 3315 | const auto *context = cb_access_context->GetCurrentAccessContext(); |
| 3316 | assert(context); |
| 3317 | if (!context) return skip; |
| 3318 | |
| 3319 | const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer); |
| 3320 | |
| 3321 | if (dst_buffer) { |
| 3322 | ResourceAccessRange range = MakeRange(dstOffset, dataSize); |
| 3323 | auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range); |
| 3324 | if (hazard.hazard) { |
| 3325 | skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s", |
| 3326 | string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str()); |
| 3327 | } |
| 3328 | } |
| 3329 | return skip; |
| 3330 | } |
| 3331 | |
| 3332 | void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset, |
| 3333 | VkDeviceSize dataSize, const void *pData) { |
| 3334 | auto *cb_access_context = GetAccessContext(commandBuffer); |
| 3335 | assert(cb_access_context); |
| 3336 | const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER); |
| 3337 | auto *context = cb_access_context->GetCurrentAccessContext(); |
| 3338 | assert(context); |
| 3339 | |
| 3340 | const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer); |
| 3341 | |
| 3342 | if (dst_buffer) { |
| 3343 | ResourceAccessRange range = MakeRange(dstOffset, dataSize); |
| 3344 | context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag); |
| 3345 | } |
| 3346 | } |