aitor-lunarg | b435960 | 2022-02-16 19:54:50 +0100 | [diff] [blame] | 1 | /* Copyright (c) 2019-2022 The Khronos Group Inc. |
| 2 | * Copyright (c) 2019-2022 Valve Corporation |
| 3 | * Copyright (c) 2019-2022 LunarG, Inc. |
| 4 | * Copyright (C) 2019-2022 Google Inc. |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | * John Zulauf <jzulauf@lunarg.com> |
| 19 | * |
| 20 | */ |
John Zulauf | 5823c62 | 2019-11-25 13:33:44 -0700 | [diff] [blame] | 21 | #include "image_layout_map.h" |
Jeremy Gebben | 1dfbd17 | 2021-05-19 14:00:58 -0600 | [diff] [blame] | 22 | #ifndef SPARSE_CONTAINER_UNIT_TEST |
| 23 | #include "image_state.h" |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 24 | #include "cmd_buffer_state.h" |
John Zulauf | 5823c62 | 2019-11-25 13:33:44 -0700 | [diff] [blame] | 25 | #endif |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 26 | |
| 27 | namespace image_layout_map { |
John Zulauf | 81408f1 | 2019-11-27 16:40:27 -0700 | [diff] [blame] | 28 | using InitialLayoutStates = ImageSubresourceLayoutMap::InitialLayoutStates; |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 29 | using LayoutEntry = ImageSubresourceLayoutMap::LayoutEntry; |
John Zulauf | 81408f1 | 2019-11-27 16:40:27 -0700 | [diff] [blame] | 30 | |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 31 | template <typename LayoutsMap> |
| 32 | static bool UpdateLayoutStateImpl(LayoutsMap& layouts, InitialLayoutStates& initial_layout_states, const IndexRange& range, |
| 33 | LayoutEntry& new_entry, const CMD_BUFFER_STATE& cb_state, const IMAGE_VIEW_STATE* view_state) { |
| 34 | using CachedLowerBound = typename sparse_container::cached_lower_bound_impl<LayoutsMap>; |
| 35 | CachedLowerBound pos(layouts, range.begin); |
| 36 | if (!range.includes(pos->index)) { |
| 37 | return false; |
John Zulauf | 81408f1 | 2019-11-27 16:40:27 -0700 | [diff] [blame] | 38 | } |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 39 | bool updated_current = false; |
| 40 | while (range.includes(pos->index)) { |
| 41 | if (!pos->valid) { |
| 42 | // Fill in the leading space (or in the case of pos at end the trailing space |
| 43 | const auto start = pos->index; |
| 44 | auto it = pos->lower_bound; |
| 45 | const auto limit = (it != layouts.end()) ? std::min(it->first.begin, range.end) : range.end; |
| 46 | if (new_entry.state == nullptr) { |
| 47 | // Allocate on demand... initial_layout_states_ holds ownership, while |
| 48 | // each subresource has a non-owning copy of the plain pointer. |
| 49 | initial_layout_states.emplace_back(cb_state, view_state); |
| 50 | new_entry.state = &initial_layout_states.back(); |
| 51 | } |
Jeremy Gebben | 53585a6 | 2021-06-30 14:50:43 -0600 | [diff] [blame] | 52 | auto insert_result = layouts.insert(it, std::make_pair(IndexRange(start, limit), new_entry)); |
| 53 | pos.invalidate(insert_result, start); |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 54 | pos.seek(limit); |
| 55 | updated_current = true; |
| 56 | } |
| 57 | // Note that after the "fill" operation pos may have become valid so we check again |
| 58 | if (pos->valid) { |
Jeremy Gebben | 53585a6 | 2021-06-30 14:50:43 -0600 | [diff] [blame] | 59 | auto intersected_range = pos->lower_bound->first & range; |
| 60 | if (!intersected_range.empty() && pos->lower_bound->second.CurrentWillChange(new_entry.current_layout)) { |
Jeremy Gebben | f33bcd1 | 2022-04-19 08:12:44 -0600 | [diff] [blame^] | 61 | LayoutEntry orig_entry = pos->lower_bound->second; // intentional copy |
Jeremy Gebben | 884579e | 2021-04-29 14:12:37 -0600 | [diff] [blame] | 62 | assert(orig_entry.state != nullptr); |
| 63 | updated_current |= orig_entry.Update(new_entry); |
Jeremy Gebben | 53585a6 | 2021-06-30 14:50:43 -0600 | [diff] [blame] | 64 | auto overwrite_result = layouts.overwrite_range(pos->lower_bound, std::make_pair(intersected_range, orig_entry)); |
| 65 | // If we didn't cover the whole range, we'll need to go around again |
| 66 | pos.invalidate(overwrite_result, intersected_range.begin); |
| 67 | pos.seek(intersected_range.end); |
Jeremy Gebben | 884579e | 2021-04-29 14:12:37 -0600 | [diff] [blame] | 68 | } else { |
| 69 | // Point just past the end of this section, if it's within the given range, it will get filled next iteration |
| 70 | // ++pos could move us past the end of range (which would exit the loop) so we don't use it. |
| 71 | pos.seek(pos->lower_bound->first.end); |
| 72 | } |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | return updated_current; |
John Zulauf | 81408f1 | 2019-11-27 16:40:27 -0700 | [diff] [blame] | 77 | } |
| 78 | |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 79 | InitialLayoutState::InitialLayoutState(const CMD_BUFFER_STATE& cb_state_, const IMAGE_VIEW_STATE* view_state_) |
| 80 | : image_view(VK_NULL_HANDLE), aspect_mask(0), label(cb_state_.debug_label) { |
| 81 | if (view_state_) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 82 | image_view = view_state_->image_view(); |
Jeremy Gebben | b4d1701 | 2021-07-08 13:18:15 -0600 | [diff] [blame] | 83 | aspect_mask = view_state_->normalized_subresource_range.aspectMask; |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | bool ImageSubresourceLayoutMap::SubresourceLayout::operator==(const ImageSubresourceLayoutMap::SubresourceLayout& rhs) const { |
| 87 | bool is_equal = |
| 88 | (current_layout == rhs.current_layout) && (initial_layout == rhs.initial_layout) && (subresource == rhs.subresource); |
| 89 | return is_equal; |
| 90 | } |
| 91 | ImageSubresourceLayoutMap::ImageSubresourceLayoutMap(const IMAGE_STATE& image_state) |
John Zulauf | b58415b | 2019-12-09 15:02:32 -0700 | [diff] [blame] | 92 | : image_state_(image_state), |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 93 | encoder_(image_state.subresource_encoder), |
John Zulauf | 81408f1 | 2019-11-27 16:40:27 -0700 | [diff] [blame] | 94 | layouts_(encoder_.SubresourceCount()), |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 95 | initial_layout_states_() {} |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 96 | |
John Zulauf | 81408f1 | 2019-11-27 16:40:27 -0700 | [diff] [blame] | 97 | // Use the unwrapped maps from the BothMap in the actual implementation |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 98 | template <typename LayoutMap> |
| 99 | static bool SetSubresourceRangeLayoutImpl(LayoutMap& layouts, InitialLayoutStates& initial_layout_states, RangeGenerator& range_gen, |
| 100 | const CMD_BUFFER_STATE& cb_state, VkImageLayout layout, VkImageLayout expected_layout) { |
John Zulauf | 81408f1 | 2019-11-27 16:40:27 -0700 | [diff] [blame] | 101 | bool updated = false; |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 102 | LayoutEntry entry(expected_layout, layout); |
John Zulauf | 81408f1 | 2019-11-27 16:40:27 -0700 | [diff] [blame] | 103 | for (; range_gen->non_empty(); ++range_gen) { |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 104 | updated |= UpdateLayoutStateImpl(layouts, initial_layout_states, *range_gen, entry, cb_state, nullptr); |
John Zulauf | 81408f1 | 2019-11-27 16:40:27 -0700 | [diff] [blame] | 105 | } |
| 106 | return updated; |
| 107 | } |
| 108 | |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 109 | bool ImageSubresourceLayoutMap::SetSubresourceRangeLayout(const CMD_BUFFER_STATE& cb_state, const VkImageSubresourceRange& range, |
| 110 | VkImageLayout layout, VkImageLayout expected_layout) { |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 111 | if (expected_layout == kInvalidLayout) { |
| 112 | // Set the initial layout to the set layout as we had no other layout to reference |
| 113 | expected_layout = layout; |
| 114 | } |
| 115 | if (!InRange(range)) return false; // Don't even try to track bogus subreources |
| 116 | |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 117 | RangeGenerator range_gen(encoder_, range); |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 118 | if (layouts_.SmallMode()) { |
| 119 | return SetSubresourceRangeLayoutImpl(layouts_.GetSmallMap(), initial_layout_states_, range_gen, cb_state, layout, |
| 120 | expected_layout); |
John Zulauf | 81408f1 | 2019-11-27 16:40:27 -0700 | [diff] [blame] | 121 | } else { |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 122 | assert(!layouts_.Tristate()); |
| 123 | return SetSubresourceRangeLayoutImpl(layouts_.GetBigMap(), initial_layout_states_, range_gen, cb_state, layout, |
| 124 | expected_layout); |
John Zulauf | 81408f1 | 2019-11-27 16:40:27 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | // Use the unwrapped maps from the BothMap in the actual implementation |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 129 | template <typename LayoutMap> |
| 130 | static void SetSubresourceRangeInitialLayoutImpl(LayoutMap& layouts, InitialLayoutStates& initial_layout_states, |
| 131 | RangeGenerator& range_gen, const CMD_BUFFER_STATE& cb_state, VkImageLayout layout, |
John Zulauf | 81408f1 | 2019-11-27 16:40:27 -0700 | [diff] [blame] | 132 | const IMAGE_VIEW_STATE* view_state) { |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 133 | LayoutEntry entry(layout); |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 134 | for (; range_gen->non_empty(); ++range_gen) { |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 135 | UpdateLayoutStateImpl(layouts, initial_layout_states, *range_gen, entry, cb_state, view_state); |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 136 | } |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 137 | } |
John Zulauf | 81408f1 | 2019-11-27 16:40:27 -0700 | [diff] [blame] | 138 | |
| 139 | // Unwrap the BothMaps entry here as this is a performance hotspot. |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 140 | void ImageSubresourceLayoutMap::SetSubresourceRangeInitialLayout(const CMD_BUFFER_STATE& cb_state, |
| 141 | const VkImageSubresourceRange& range, VkImageLayout layout) { |
| 142 | if (!InRange(range)) return; // Don't even try to track bogus subreources |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 143 | |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 144 | RangeGenerator range_gen(encoder_, range); |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 145 | if (layouts_.SmallMode()) { |
| 146 | SetSubresourceRangeInitialLayoutImpl(layouts_.GetSmallMap(), initial_layout_states_, range_gen, cb_state, layout, nullptr); |
John Zulauf | 81408f1 | 2019-11-27 16:40:27 -0700 | [diff] [blame] | 147 | } else { |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 148 | assert(!layouts_.Tristate()); |
| 149 | SetSubresourceRangeInitialLayoutImpl(layouts_.GetBigMap(), initial_layout_states_, range_gen, cb_state, layout, nullptr); |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 150 | } |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
John Zulauf | b58415b | 2019-12-09 15:02:32 -0700 | [diff] [blame] | 153 | // Unwrap the BothMaps entry here as this is a performance hotspot. |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 154 | void ImageSubresourceLayoutMap::SetSubresourceRangeInitialLayout(const CMD_BUFFER_STATE& cb_state, VkImageLayout layout, |
John Zulauf | b58415b | 2019-12-09 15:02:32 -0700 | [diff] [blame] | 155 | const IMAGE_VIEW_STATE& view_state) { |
| 156 | RangeGenerator range_gen(view_state.range_generator); |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 157 | if (layouts_.SmallMode()) { |
| 158 | SetSubresourceRangeInitialLayoutImpl(layouts_.GetSmallMap(), initial_layout_states_, range_gen, cb_state, layout, |
| 159 | &view_state); |
John Zulauf | b58415b | 2019-12-09 15:02:32 -0700 | [diff] [blame] | 160 | } else { |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 161 | assert(!layouts_.Tristate()); |
| 162 | SetSubresourceRangeInitialLayoutImpl(layouts_.GetBigMap(), initial_layout_states_, range_gen, cb_state, layout, |
| 163 | &view_state); |
John Zulauf | b58415b | 2019-12-09 15:02:32 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 167 | // Saves an encode to fetch both in the same call |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 168 | const ImageSubresourceLayoutMap::LayoutEntry* ImageSubresourceLayoutMap::GetSubresourceLayouts( |
| 169 | const VkImageSubresource& subresource) const { |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 170 | IndexType index = encoder_.Encode(subresource); |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 171 | auto found = layouts_.find(index); |
| 172 | if (found != layouts_.end()) { |
| 173 | return &found->second; |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 174 | } |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 175 | return nullptr; |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 176 | } |
| 177 | |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 178 | // TODO: make sure this paranoia check is sufficient and not too much. |
| 179 | uintptr_t ImageSubresourceLayoutMap::CompatibilityKey() const { |
Mike Schuchardt | e5c15cf | 2020-04-06 22:57:13 -0700 | [diff] [blame] | 180 | return (reinterpret_cast<uintptr_t>(&image_state_) ^ encoder_.AspectMask()); |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | bool ImageSubresourceLayoutMap::UpdateFrom(const ImageSubresourceLayoutMap& other) { |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 184 | // Must be from matching images for the reinterpret cast to be valid |
| 185 | assert(CompatibilityKey() == other.CompatibilityKey()); |
| 186 | if (CompatibilityKey() != other.CompatibilityKey()) return false; |
| 187 | |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 188 | // NOTE -- we are copying plain state pointers from 'other' which owns them in a vector. This works because |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 189 | // currently this function is only used to import from secondary command buffers, destruction of which |
| 190 | // invalidate the referencing primary command buffer, meaning that the dangling pointer will either be |
| 191 | // cleaned up in invalidation, on not referenced by validation code. |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 192 | return sparse_container::splice(layouts_, other.layouts_, LayoutEntry::Updater()); |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 193 | } |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 194 | |
Jeremy Gebben | f33bcd1 | 2022-04-19 08:12:44 -0600 | [diff] [blame^] | 195 | bool ImageSubresourceLayoutMap::AnyInRange(const VkImageSubresourceRange& normalized_range, |
| 196 | std::function<bool(const VkImageSubresource&, const LayoutEntry&)> func) const { |
| 197 | if (!encoder_.InRange(normalized_range)) { |
| 198 | return false; |
Jeremy Gebben | 5363130 | 2021-04-13 16:46:37 -0600 | [diff] [blame] | 199 | } |
Jeremy Gebben | f33bcd1 | 2022-04-19 08:12:44 -0600 | [diff] [blame^] | 200 | for (auto range_gen = RangeGenerator(encoder_, normalized_range); range_gen->non_empty(); ++range_gen) { |
| 201 | for (auto pos = layouts_.lower_bound(*range_gen); (pos != layouts_.end()) && (range_gen->intersects(pos->first)); ++pos) { |
| 202 | auto subres = encoder_.Decode(pos->first.begin); |
| 203 | if (func(subres, pos->second)) { |
| 204 | return true; |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
Jeremy Gebben | f33bcd1 | 2022-04-19 08:12:44 -0600 | [diff] [blame^] | 207 | } |
| 208 | return false; |
| 209 | } |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 210 | |
Jeremy Gebben | f33bcd1 | 2022-04-19 08:12:44 -0600 | [diff] [blame^] | 211 | bool ImageSubresourceLayoutMap::AnyInRange(const RangeGenerator& gen, |
| 212 | std::function<bool(const VkImageSubresource&, const LayoutEntry&)> func) const { |
| 213 | for (auto range_gen = gen; range_gen->non_empty(); ++range_gen) { |
| 214 | for (auto pos = layouts_.lower_bound(*range_gen); (pos != layouts_.end()) && (range_gen->intersects(pos->first)); ++pos) { |
| 215 | auto subres = encoder_.Decode(pos->first.begin); |
| 216 | if (func(subres, pos->second)) { |
| 217 | return true; |
| 218 | } |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
Jeremy Gebben | f33bcd1 | 2022-04-19 08:12:44 -0600 | [diff] [blame^] | 221 | return false; |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | } // namespace image_layout_map |