blob: 420010820fce0fa035a8af1c5db12bf2f65bb1da [file] [log] [blame]
Jeremy Gebbenb6c18132021-03-17 17:00:20 -06001/* Copyright (c) 2019-2021 The Khronos Group Inc.
2 * Copyright (c) 2019-2021 Valve Corporation
3 * Copyright (c) 2019-2021 LunarG, Inc.
4 * Copyright (C) 2019-2021 Google Inc.
John Zulauf11211402019-11-15 14:02:36 -07005 *
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 Zulauf5823c622019-11-25 13:33:44 -070021#include "image_layout_map.h"
Jeremy Gebben1dfbd172021-05-19 14:00:58 -060022#ifndef SPARSE_CONTAINER_UNIT_TEST
23#include "image_state.h"
Jeremy Gebben159b3cc2021-06-03 09:09:03 -060024#include "cmd_buffer_state.h"
John Zulauf5823c622019-11-25 13:33:44 -070025#endif
John Zulauf11211402019-11-15 14:02:36 -070026
27namespace image_layout_map {
28// Storage for the static state
29const ImageSubresourceLayoutMap::ConstIterator ImageSubresourceLayoutMap::end_iterator = ImageSubresourceLayoutMap::ConstIterator();
30
John Zulauf81408f12019-11-27 16:40:27 -070031using InitialLayoutStates = ImageSubresourceLayoutMap::InitialLayoutStates;
Jeremy Gebben53631302021-04-13 16:46:37 -060032using LayoutEntry = ImageSubresourceLayoutMap::LayoutEntry;
John Zulauf81408f12019-11-27 16:40:27 -070033
Jeremy Gebben53631302021-04-13 16:46:37 -060034template <typename LayoutsMap>
35static bool UpdateLayoutStateImpl(LayoutsMap& layouts, InitialLayoutStates& initial_layout_states, const IndexRange& range,
36 LayoutEntry& new_entry, const CMD_BUFFER_STATE& cb_state, const IMAGE_VIEW_STATE* view_state) {
37 using CachedLowerBound = typename sparse_container::cached_lower_bound_impl<LayoutsMap>;
38 CachedLowerBound pos(layouts, range.begin);
39 if (!range.includes(pos->index)) {
40 return false;
John Zulauf81408f12019-11-27 16:40:27 -070041 }
Jeremy Gebben53631302021-04-13 16:46:37 -060042 bool updated_current = false;
43 while (range.includes(pos->index)) {
44 if (!pos->valid) {
45 // Fill in the leading space (or in the case of pos at end the trailing space
46 const auto start = pos->index;
47 auto it = pos->lower_bound;
48 const auto limit = (it != layouts.end()) ? std::min(it->first.begin, range.end) : range.end;
49 if (new_entry.state == nullptr) {
50 // Allocate on demand... initial_layout_states_ holds ownership, while
51 // each subresource has a non-owning copy of the plain pointer.
52 initial_layout_states.emplace_back(cb_state, view_state);
53 new_entry.state = &initial_layout_states.back();
54 }
55 layouts.insert(it, std::make_pair(IndexRange(start, limit), new_entry));
56 // We inserted before pos->lower_bound, so pos->lower_bound isn't invalid, but the associated index *is* and seek
57 // will fix this (and move the state to valid)
58 pos.seek(limit);
59 updated_current = true;
60 }
61 // Note that after the "fill" operation pos may have become valid so we check again
62 if (pos->valid) {
Jeremy Gebben884579e2021-04-29 14:12:37 -060063 if (pos->lower_bound->second.CurrentWillChange(new_entry.current_layout)) {
64 LayoutEntry orig_entry = pos->lower_bound->second; //intentional copy
65 assert(orig_entry.state != nullptr);
66 updated_current |= orig_entry.Update(new_entry);
67
68 layouts.overwrite_range(std::make_pair(range, orig_entry));
69 break;
70 } else {
71 // Point just past the end of this section, if it's within the given range, it will get filled next iteration
72 // ++pos could move us past the end of range (which would exit the loop) so we don't use it.
73 pos.seek(pos->lower_bound->first.end);
74 }
Jeremy Gebben53631302021-04-13 16:46:37 -060075 }
76 }
77
78 return updated_current;
John Zulauf81408f12019-11-27 16:40:27 -070079}
80
John Zulauf11211402019-11-15 14:02:36 -070081InitialLayoutState::InitialLayoutState(const CMD_BUFFER_STATE& cb_state_, const IMAGE_VIEW_STATE* view_state_)
82 : image_view(VK_NULL_HANDLE), aspect_mask(0), label(cb_state_.debug_label) {
83 if (view_state_) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -060084 image_view = view_state_->image_view();
Jeremy Gebbenb4d17012021-07-08 13:18:15 -060085 aspect_mask = view_state_->normalized_subresource_range.aspectMask;
John Zulauf11211402019-11-15 14:02:36 -070086 }
87}
88bool ImageSubresourceLayoutMap::SubresourceLayout::operator==(const ImageSubresourceLayoutMap::SubresourceLayout& rhs) const {
89 bool is_equal =
90 (current_layout == rhs.current_layout) && (initial_layout == rhs.initial_layout) && (subresource == rhs.subresource);
91 return is_equal;
92}
93ImageSubresourceLayoutMap::ImageSubresourceLayoutMap(const IMAGE_STATE& image_state)
John Zulaufb58415b2019-12-09 15:02:32 -070094 : image_state_(image_state),
locke-lunarg296a3c92020-03-25 01:04:29 -060095 encoder_(image_state.subresource_encoder),
John Zulauf81408f12019-11-27 16:40:27 -070096 layouts_(encoder_.SubresourceCount()),
Jeremy Gebben53631302021-04-13 16:46:37 -060097 initial_layout_states_() {}
John Zulauf11211402019-11-15 14:02:36 -070098
99ImageSubresourceLayoutMap::ConstIterator ImageSubresourceLayoutMap::Begin(bool always_get_initial) const {
100 return Find(image_state_.full_range, /* skip_invalid */ true, always_get_initial);
101}
John Zulauf81408f12019-11-27 16:40:27 -0700102
103// Use the unwrapped maps from the BothMap in the actual implementation
Jeremy Gebben53631302021-04-13 16:46:37 -0600104template <typename LayoutMap>
105static bool SetSubresourceRangeLayoutImpl(LayoutMap& layouts, InitialLayoutStates& initial_layout_states, RangeGenerator& range_gen,
106 const CMD_BUFFER_STATE& cb_state, VkImageLayout layout, VkImageLayout expected_layout) {
John Zulauf81408f12019-11-27 16:40:27 -0700107 bool updated = false;
Jeremy Gebben53631302021-04-13 16:46:37 -0600108 LayoutEntry entry(expected_layout, layout);
John Zulauf81408f12019-11-27 16:40:27 -0700109 for (; range_gen->non_empty(); ++range_gen) {
Jeremy Gebben53631302021-04-13 16:46:37 -0600110 updated |= UpdateLayoutStateImpl(layouts, initial_layout_states, *range_gen, entry, cb_state, nullptr);
John Zulauf81408f12019-11-27 16:40:27 -0700111 }
112 return updated;
113}
114
John Zulauf11211402019-11-15 14:02:36 -0700115bool ImageSubresourceLayoutMap::SetSubresourceRangeLayout(const CMD_BUFFER_STATE& cb_state, const VkImageSubresourceRange& range,
116 VkImageLayout layout, VkImageLayout expected_layout) {
John Zulauf11211402019-11-15 14:02:36 -0700117 if (expected_layout == kInvalidLayout) {
118 // Set the initial layout to the set layout as we had no other layout to reference
119 expected_layout = layout;
120 }
121 if (!InRange(range)) return false; // Don't even try to track bogus subreources
122
John Zulauf11211402019-11-15 14:02:36 -0700123 RangeGenerator range_gen(encoder_, range);
Jeremy Gebben53631302021-04-13 16:46:37 -0600124 if (layouts_.SmallMode()) {
125 return SetSubresourceRangeLayoutImpl(layouts_.GetSmallMap(), initial_layout_states_, range_gen, cb_state, layout,
126 expected_layout);
John Zulauf81408f12019-11-27 16:40:27 -0700127 } else {
Jeremy Gebben53631302021-04-13 16:46:37 -0600128 assert(!layouts_.Tristate());
129 return SetSubresourceRangeLayoutImpl(layouts_.GetBigMap(), initial_layout_states_, range_gen, cb_state, layout,
130 expected_layout);
John Zulauf81408f12019-11-27 16:40:27 -0700131 }
132}
133
134// Use the unwrapped maps from the BothMap in the actual implementation
Jeremy Gebben53631302021-04-13 16:46:37 -0600135template <typename LayoutMap>
136static void SetSubresourceRangeInitialLayoutImpl(LayoutMap& layouts, InitialLayoutStates& initial_layout_states,
137 RangeGenerator& range_gen, const CMD_BUFFER_STATE& cb_state, VkImageLayout layout,
John Zulauf81408f12019-11-27 16:40:27 -0700138 const IMAGE_VIEW_STATE* view_state) {
Jeremy Gebben53631302021-04-13 16:46:37 -0600139 LayoutEntry entry(layout);
John Zulauf11211402019-11-15 14:02:36 -0700140 for (; range_gen->non_empty(); ++range_gen) {
Jeremy Gebben53631302021-04-13 16:46:37 -0600141 UpdateLayoutStateImpl(layouts, initial_layout_states, *range_gen, entry, cb_state, view_state);
John Zulauf11211402019-11-15 14:02:36 -0700142 }
John Zulauf11211402019-11-15 14:02:36 -0700143}
John Zulauf81408f12019-11-27 16:40:27 -0700144
145// Unwrap the BothMaps entry here as this is a performance hotspot.
Jeremy Gebben53631302021-04-13 16:46:37 -0600146void ImageSubresourceLayoutMap::SetSubresourceRangeInitialLayout(const CMD_BUFFER_STATE& cb_state,
147 const VkImageSubresourceRange& range, VkImageLayout layout) {
148 if (!InRange(range)) return; // Don't even try to track bogus subreources
John Zulauf11211402019-11-15 14:02:36 -0700149
John Zulauf11211402019-11-15 14:02:36 -0700150 RangeGenerator range_gen(encoder_, range);
Jeremy Gebben53631302021-04-13 16:46:37 -0600151 if (layouts_.SmallMode()) {
152 SetSubresourceRangeInitialLayoutImpl(layouts_.GetSmallMap(), initial_layout_states_, range_gen, cb_state, layout, nullptr);
John Zulauf81408f12019-11-27 16:40:27 -0700153 } else {
Jeremy Gebben53631302021-04-13 16:46:37 -0600154 assert(!layouts_.Tristate());
155 SetSubresourceRangeInitialLayoutImpl(layouts_.GetBigMap(), initial_layout_states_, range_gen, cb_state, layout, nullptr);
John Zulauf11211402019-11-15 14:02:36 -0700156 }
John Zulauf11211402019-11-15 14:02:36 -0700157}
158
John Zulaufb58415b2019-12-09 15:02:32 -0700159// Unwrap the BothMaps entry here as this is a performance hotspot.
Jeremy Gebben53631302021-04-13 16:46:37 -0600160void ImageSubresourceLayoutMap::SetSubresourceRangeInitialLayout(const CMD_BUFFER_STATE& cb_state, VkImageLayout layout,
John Zulaufb58415b2019-12-09 15:02:32 -0700161 const IMAGE_VIEW_STATE& view_state) {
162 RangeGenerator range_gen(view_state.range_generator);
Jeremy Gebben53631302021-04-13 16:46:37 -0600163 if (layouts_.SmallMode()) {
164 SetSubresourceRangeInitialLayoutImpl(layouts_.GetSmallMap(), initial_layout_states_, range_gen, cb_state, layout,
165 &view_state);
John Zulaufb58415b2019-12-09 15:02:32 -0700166 } else {
Jeremy Gebben53631302021-04-13 16:46:37 -0600167 assert(!layouts_.Tristate());
168 SetSubresourceRangeInitialLayoutImpl(layouts_.GetBigMap(), initial_layout_states_, range_gen, cb_state, layout,
169 &view_state);
John Zulaufb58415b2019-12-09 15:02:32 -0700170 }
171}
172
John Zulauf11211402019-11-15 14:02:36 -0700173// Saves an encode to fetch both in the same call
Jeremy Gebben53631302021-04-13 16:46:37 -0600174const ImageSubresourceLayoutMap::LayoutEntry* ImageSubresourceLayoutMap::GetSubresourceLayouts(
175 const VkImageSubresource& subresource) const {
John Zulauf11211402019-11-15 14:02:36 -0700176 IndexType index = encoder_.Encode(subresource);
Jeremy Gebben53631302021-04-13 16:46:37 -0600177 auto found = layouts_.find(index);
178 if (found != layouts_.end()) {
179 return &found->second;
John Zulauf11211402019-11-15 14:02:36 -0700180 }
Jeremy Gebben53631302021-04-13 16:46:37 -0600181 return nullptr;
John Zulauf11211402019-11-15 14:02:36 -0700182}
183
John Zulauf2076e812020-01-08 14:55:54 -0700184const InitialLayoutState* ImageSubresourceLayoutMap::GetSubresourceInitialLayoutState(const IndexType index) const {
Jeremy Gebben53631302021-04-13 16:46:37 -0600185 const auto found = layouts_.find(index);
186 if (found != layouts_.end()) {
187 return found->second.state;
John Zulauf11211402019-11-15 14:02:36 -0700188 }
189 return nullptr;
190}
191
John Zulauf2076e812020-01-08 14:55:54 -0700192const InitialLayoutState* ImageSubresourceLayoutMap::GetSubresourceInitialLayoutState(const VkImageSubresource& subresource) const {
193 if (!InRange(subresource)) return nullptr;
194 const auto index = encoder_.Encode(subresource);
195 return GetSubresourceInitialLayoutState(index);
196}
197
John Zulauf11211402019-11-15 14:02:36 -0700198// TODO: make sure this paranoia check is sufficient and not too much.
199uintptr_t ImageSubresourceLayoutMap::CompatibilityKey() const {
Mike Schuchardte5c15cf2020-04-06 22:57:13 -0700200 return (reinterpret_cast<uintptr_t>(&image_state_) ^ encoder_.AspectMask());
John Zulauf11211402019-11-15 14:02:36 -0700201}
202
203bool ImageSubresourceLayoutMap::UpdateFrom(const ImageSubresourceLayoutMap& other) {
John Zulauf11211402019-11-15 14:02:36 -0700204 // Must be from matching images for the reinterpret cast to be valid
205 assert(CompatibilityKey() == other.CompatibilityKey());
206 if (CompatibilityKey() != other.CompatibilityKey()) return false;
207
Jeremy Gebben53631302021-04-13 16:46:37 -0600208 // NOTE -- we are copying plain state pointers from 'other' which owns them in a vector. This works because
John Zulauf11211402019-11-15 14:02:36 -0700209 // currently this function is only used to import from secondary command buffers, destruction of which
210 // invalidate the referencing primary command buffer, meaning that the dangling pointer will either be
211 // cleaned up in invalidation, on not referenced by validation code.
Jeremy Gebben53631302021-04-13 16:46:37 -0600212 return sparse_container::splice(layouts_, other.layouts_, LayoutEntry::Updater());
John Zulauf11211402019-11-15 14:02:36 -0700213}
John Zulauf11211402019-11-15 14:02:36 -0700214
John Zulauf11211402019-11-15 14:02:36 -0700215// This is the same constant value range, subreource position advance logic as ForRange above, but suitable for use with
216// an Increment operator.
217void ImageSubresourceLayoutMap::ConstIterator::UpdateRangeAndValue() {
218 bool not_found = true;
Jeremy Gebben53631302021-04-13 16:46:37 -0600219 if (layouts_ == nullptr || layouts_->empty()) {
220 return;
221 }
222 while (iter_ != layouts_->end() && range_gen_->non_empty() && not_found) {
223 if (!iter_->first.includes(current_index_)) { // NOTE: empty ranges can't include anything
224 iter_ = layouts_->find(current_index_);
John Zulauf11211402019-11-15 14:02:36 -0700225 }
Jeremy Gebben53631302021-04-13 16:46:37 -0600226 if (iter_ == layouts_->end() || (iter_->first.empty() && skip_invalid_)) {
John Zulauf11211402019-11-15 14:02:36 -0700227 // We're past the end of mapped data, and we aren't interested, so we're done
228 // Set end condtion....
229 ForceEndCondition();
230 }
231 // Search within the current range_ for a constant valid constant value interval
Jeremy Gebben53631302021-04-13 16:46:37 -0600232 // The while condition allows the iterator to advance constant value ranges as needed.
233 while (iter_ != layouts_->end() && range_gen_->includes(current_index_) && not_found) {
John Zulauf11211402019-11-15 14:02:36 -0700234 pos_.current_layout = kInvalidLayout;
235 pos_.initial_layout = kInvalidLayout;
236 constant_value_bound_ = range_gen_->end;
237 // The generated range can validly traverse past the end of stored data
Jeremy Gebben53631302021-04-13 16:46:37 -0600238 if (!iter_->first.empty()) {
239 const LayoutEntry& entry = iter_->second;
240 pos_.current_layout = entry.current_layout;
John Zulauf11211402019-11-15 14:02:36 -0700241 if (pos_.current_layout == kInvalidLayout || always_get_initial_) {
Jeremy Gebben53631302021-04-13 16:46:37 -0600242 pos_.initial_layout = entry.initial_layout;
John Zulauf11211402019-11-15 14:02:36 -0700243 }
Jeremy Gebben53631302021-04-13 16:46:37 -0600244
John Zulauf11211402019-11-15 14:02:36 -0700245 // The constant value bound marks the end of contiguous (w.r.t. range_gen_) indices with the same value, allowing
246 // Increment (for example) to forgo this logic until finding a new range is needed.
Jeremy Gebben53631302021-04-13 16:46:37 -0600247 constant_value_bound_ = std::min(iter_->first.end, constant_value_bound_);
John Zulauf11211402019-11-15 14:02:36 -0700248 }
249 if (!skip_invalid_ || (pos_.current_layout != kInvalidLayout) || (pos_.initial_layout != kInvalidLayout)) {
250 // we found it ... set the position and exit condition.
John Zulauf2ea823e2019-11-19 08:54:59 -0700251 pos_.subresource = range_gen_.GetSubresource();
John Zulauf11211402019-11-15 14:02:36 -0700252 not_found = false;
253 } else {
254 // We're skipping this constant value range, set the index to the exclusive end and look again
John Zulaufdd18b3a2019-11-20 08:30:23 -0700255 // Note that we ONLY need to Seek the Subresource generator on a skip condition.
256 range_gen_.GetSubresourceGenerator().Seek(
John Zulauf11211402019-11-15 14:02:36 -0700257 constant_value_bound_); // Move the subresource to the end of the skipped range
258 current_index_ = constant_value_bound_;
259
Jeremy Gebben53631302021-04-13 16:46:37 -0600260 // Advance the iterator it if needed and possible
John Zulauf11211402019-11-15 14:02:36 -0700261 // NOTE: We don't need to seek, as current_index_ can only be in the current or next constant value range
Jeremy Gebben53631302021-04-13 16:46:37 -0600262 if (!iter_->first.empty() && !iter_->first.includes(current_index_)) {
263 ++iter_;
John Zulauf11211402019-11-15 14:02:36 -0700264 }
265 }
266 }
267
268 if (not_found) {
269 // ++range_gen will update subres_gen.
270 ++range_gen_;
271 current_index_ = range_gen_->begin;
272 }
273 }
274
275 if (range_gen_->empty()) {
276 ForceEndCondition();
277 }
278}
279
280void ImageSubresourceLayoutMap::ConstIterator::Increment() {
281 ++current_index_;
John Zulauf2ea823e2019-11-19 08:54:59 -0700282 ++(range_gen_.GetSubresourceGenerator());
John Zulauf11211402019-11-15 14:02:36 -0700283 if (constant_value_bound_ <= current_index_) {
284 UpdateRangeAndValue();
285 } else {
John Zulauf2ea823e2019-11-19 08:54:59 -0700286 pos_.subresource = range_gen_.GetSubresource();
John Zulauf11211402019-11-15 14:02:36 -0700287 }
288}
Tony Barbour55688172020-09-23 15:19:50 -0700289
290void ImageSubresourceLayoutMap::ConstIterator::IncrementInterval() {
291 // constant_value_bound_ is the exclusive upper bound of the constant value range.
292 // When current index is set to point to that, UpdateRangeAndValue skips to the next constant value range,
293 // setting that state as the current position / state for the iterator.
294 current_index_ = constant_value_bound_;
295 UpdateRangeAndValue();
296}
297
Jeremy Gebben53631302021-04-13 16:46:37 -0600298ImageSubresourceLayoutMap::ConstIterator::ConstIterator(const RangeMap& layouts, const Encoder& encoder,
John Zulauf11211402019-11-15 14:02:36 -0700299 const VkImageSubresourceRange& subres, bool skip_invalid,
300 bool always_get_initial)
301 : range_gen_(encoder, subres),
Jeremy Gebben53631302021-04-13 16:46:37 -0600302 layouts_(&layouts),
303 iter_(layouts.begin()),
John Zulauf11211402019-11-15 14:02:36 -0700304 skip_invalid_(skip_invalid),
305 always_get_initial_(always_get_initial),
306 pos_(),
307 current_index_(range_gen_->begin),
308 constant_value_bound_() {
309 UpdateRangeAndValue();
310}
311
312} // namespace image_layout_map