John Zulauf | 86ce1cf | 2020-01-23 12:27:01 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2019-2020 The Khronos Group Inc. |
| 2 | * Copyright (c) 2019-2020 Valve Corporation |
| 3 | * Copyright (c) 2019-2020 LunarG, Inc. |
| 4 | * Copyright (C) 2019-2020 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 | */ |
| 21 | #include <cassert> |
| 22 | #include "subresource_adapter.h" |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 23 | #include "vk_format_utils.h" |
| 24 | #include "state_tracker.h" |
| 25 | #include "core_validation_types.h" |
locke-lunarg | f929301 | 2020-04-16 17:01:23 -0600 | [diff] [blame] | 26 | #include <cmath> |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 27 | |
| 28 | namespace subresource_adapter { |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 29 | Subresource::Subresource(const RangeEncoder& encoder, const VkImageSubresource& subres) |
| 30 | : VkImageSubresource({0, subres.mipLevel, subres.arrayLayer}), aspect_index() { |
| 31 | aspect_index = encoder.LowerBoundFromMask(subres.aspectMask); |
| 32 | aspectMask = encoder.AspectBit(aspect_index); |
| 33 | } |
| 34 | |
| 35 | IndexType RangeEncoder::Encode1AspectArrayOnly(const Subresource& pos) const { return pos.arrayLayer; } |
| 36 | IndexType RangeEncoder::Encode1AspectMipArray(const Subresource& pos) const { return pos.arrayLayer + pos.mipLevel * mip_size_; } |
| 37 | IndexType RangeEncoder::Encode1AspectMipOnly(const Subresource& pos) const { return pos.mipLevel; } |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 38 | |
| 39 | IndexType RangeEncoder::EncodeAspectArrayOnly(const Subresource& pos) const { |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 40 | return pos.arrayLayer + aspect_base_[pos.aspect_index]; |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 41 | } |
| 42 | IndexType RangeEncoder::EncodeAspectMipArray(const Subresource& pos) const { |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 43 | return pos.arrayLayer + pos.mipLevel * mip_size_ + aspect_base_[pos.aspect_index]; |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 44 | } |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 45 | IndexType RangeEncoder::EncodeAspectMipOnly(const Subresource& pos) const { return pos.mipLevel + aspect_base_[pos.aspect_index]; } |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 46 | |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 47 | uint32_t RangeEncoder::LowerBoundImpl1(VkImageAspectFlags aspect_mask) const { |
| 48 | assert(aspect_mask & aspect_bits_[0]); |
| 49 | return 0; |
| 50 | } |
| 51 | uint32_t RangeEncoder::LowerBoundWithStartImpl1(VkImageAspectFlags aspect_mask, uint32_t start) const { |
| 52 | assert(start == 0); |
| 53 | if (aspect_mask & aspect_bits_[0]) { |
| 54 | return 0; |
| 55 | } |
| 56 | return limits_.aspect_index; |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 59 | uint32_t RangeEncoder::LowerBoundImpl2(VkImageAspectFlags aspect_mask) const { |
| 60 | if (aspect_mask & aspect_bits_[0]) { |
| 61 | return 0; |
| 62 | } |
| 63 | assert(aspect_mask & aspect_bits_[1]); |
| 64 | return 1; |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 65 | } |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 66 | uint32_t RangeEncoder::LowerBoundWithStartImpl2(VkImageAspectFlags aspect_mask, uint32_t start) const { |
John Zulauf | 5823c62 | 2019-11-25 13:33:44 -0700 | [diff] [blame] | 67 | switch (start) { |
| 68 | case 0: |
| 69 | if (aspect_mask & aspect_bits_[0]) { |
| 70 | return 0; |
| 71 | } |
| 72 | // no break |
| 73 | case 1: |
| 74 | if (aspect_mask & aspect_bits_[1]) { |
| 75 | return 1; |
| 76 | } |
| 77 | break; |
| 78 | default: |
| 79 | break; |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 80 | } |
| 81 | return limits_.aspect_index; |
| 82 | } |
| 83 | |
| 84 | uint32_t RangeEncoder::LowerBoundImpl3(VkImageAspectFlags aspect_mask) const { |
| 85 | if (aspect_mask & aspect_bits_[0]) { |
| 86 | return 0; |
| 87 | } else if (aspect_mask & aspect_bits_[1]) { |
| 88 | return 1; |
| 89 | } else { |
| 90 | assert(aspect_mask & aspect_bits_[2]); |
| 91 | return 2; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | uint32_t RangeEncoder::LowerBoundWithStartImpl3(VkImageAspectFlags aspect_mask, uint32_t start) const { |
John Zulauf | 5823c62 | 2019-11-25 13:33:44 -0700 | [diff] [blame] | 96 | switch (start) { |
| 97 | case 0: |
| 98 | if (aspect_mask & aspect_bits_[0]) { |
| 99 | return 0; |
| 100 | } |
| 101 | // no break |
| 102 | case 1: |
| 103 | if ((aspect_mask & aspect_bits_[1])) { |
| 104 | return 1; |
| 105 | } |
| 106 | // no break |
| 107 | case 2: |
| 108 | if ((aspect_mask & aspect_bits_[2])) { |
| 109 | return 2; |
| 110 | } |
| 111 | break; |
| 112 | default: |
| 113 | break; |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 114 | } |
| 115 | return limits_.aspect_index; |
| 116 | } |
| 117 | |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 118 | void RangeEncoder::PopulateFunctionPointers() { |
| 119 | // Select the encode/decode specialists |
| 120 | if (limits_.aspect_index == 1) { |
| 121 | // One aspect use simplified encode/decode math |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 122 | if (limits_.arrayLayer == 1) { // Same as mip_size_ == 1 |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 123 | encode_function_ = &RangeEncoder::Encode1AspectMipOnly; |
| 124 | decode_function_ = &RangeEncoder::DecodeAspectMipOnly<1>; |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 125 | } else if (limits_.mipLevel == 1) { |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 126 | encode_function_ = &RangeEncoder::Encode1AspectArrayOnly; |
| 127 | decode_function_ = &RangeEncoder::DecodeAspectArrayOnly<1>; |
| 128 | } else { |
| 129 | encode_function_ = &RangeEncoder::Encode1AspectMipArray; |
| 130 | decode_function_ = &RangeEncoder::DecodeAspectMipArray<1>; |
| 131 | } |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 132 | lower_bound_function_ = &RangeEncoder::LowerBoundImpl1; |
| 133 | lower_bound_with_start_function_ = &RangeEncoder::LowerBoundWithStartImpl1; |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 134 | } else if (limits_.aspect_index == 2) { |
| 135 | // Two aspect use simplified encode/decode math |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 136 | if (limits_.arrayLayer == 1) { // Same as mip_size_ == 1 |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 137 | encode_function_ = &RangeEncoder::EncodeAspectMipOnly; |
| 138 | decode_function_ = &RangeEncoder::DecodeAspectMipOnly<2>; |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 139 | } else if (limits_.mipLevel == 1) { |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 140 | encode_function_ = &RangeEncoder::EncodeAspectArrayOnly; |
| 141 | decode_function_ = &RangeEncoder::DecodeAspectArrayOnly<2>; |
| 142 | } else { |
| 143 | encode_function_ = &RangeEncoder::EncodeAspectMipArray; |
| 144 | decode_function_ = &RangeEncoder::DecodeAspectMipArray<2>; |
| 145 | } |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 146 | lower_bound_function_ = &RangeEncoder::LowerBoundImpl2; |
| 147 | lower_bound_with_start_function_ = &RangeEncoder::LowerBoundWithStartImpl2; |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 148 | } else { |
| 149 | encode_function_ = &RangeEncoder::EncodeAspectMipArray; |
| 150 | decode_function_ = &RangeEncoder::DecodeAspectMipArray<3>; |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 151 | lower_bound_function_ = &RangeEncoder::LowerBoundImpl3; |
| 152 | lower_bound_with_start_function_ = &RangeEncoder::LowerBoundWithStartImpl3; |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // Initialize the offset array |
| 156 | aspect_base_[0] = 0; |
| 157 | for (uint32_t i = 1; i < limits_.aspect_index; ++i) { |
| 158 | aspect_base_[i] = aspect_base_[i - 1] + aspect_size_; |
| 159 | } |
| 160 | } |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 161 | |
| 162 | RangeEncoder::RangeEncoder(const VkImageSubresourceRange& full_range, const AspectParameters* param) |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 163 | : limits_(param->AspectMask(), full_range.levelCount, full_range.layerCount, param->AspectCount()), |
| 164 | full_range_(full_range), |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 165 | mip_size_(full_range.layerCount), |
| 166 | aspect_size_(mip_size_ * full_range.levelCount), |
| 167 | aspect_bits_(param->AspectBits()), |
| 168 | mask_index_function_(param->MaskToIndexFunction()), |
| 169 | encode_function_(nullptr), |
| 170 | decode_function_(nullptr) { |
| 171 | // Only valid to create an encoder for a *whole* image (i.e. base must be zero, and the specified limits_.selected_aspects |
| 172 | // *must* be equal to the traits aspect mask. (Encoder range assumes zero bases) |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 173 | assert(full_range.aspectMask == limits_.aspectMask); |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 174 | assert(full_range.baseArrayLayer == 0); |
| 175 | assert(full_range.baseMipLevel == 0); |
| 176 | // TODO: should be some static assert |
| 177 | assert(param->AspectCount() <= kMaxSupportedAspect); |
| 178 | PopulateFunctionPointers(); |
| 179 | } |
| 180 | |
locke-lunarg | 5faaff5 | 2020-02-27 14:31:11 -0700 | [diff] [blame] | 181 | static bool IsValid(const RangeEncoder& encoder, const VkImageSubresourceRange& bounds) { |
| 182 | const auto& limits = encoder.Limits(); |
| 183 | return (((bounds.aspectMask & limits.aspectMask) == bounds.aspectMask) && |
| 184 | (bounds.baseMipLevel + bounds.levelCount <= limits.mipLevel) && |
| 185 | (bounds.baseArrayLayer + bounds.layerCount <= limits.arrayLayer)); |
| 186 | } |
| 187 | |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 188 | // Create an iterator like "generator" that for each increment produces the next index range matching the |
| 189 | // next contiguous (in index space) section of the VkImageSubresourceRange |
| 190 | // Ranges will always span the layerCount layers, and if the layerCount is the full range of the image (as known by |
| 191 | // the encoder) will span the levelCount mip levels as weill. |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 192 | RangeGenerator::RangeGenerator(const RangeEncoder& encoder, const VkImageSubresourceRange& subres_range) |
| 193 | : encoder_(&encoder), isr_pos_(encoder, subres_range), pos_(), aspect_base_() { |
Mark Lobodzinski | bb279b9 | 2020-05-08 13:03:52 -0600 | [diff] [blame] | 194 | assert((((isr_pos_.Limits()).aspectMask & (encoder.Limits()).aspectMask) == (isr_pos_.Limits()).aspectMask) && |
| 195 | ((isr_pos_.Limits()).baseMipLevel + (isr_pos_.Limits()).levelCount <= (encoder.Limits()).mipLevel) && |
| 196 | ((isr_pos_.Limits()).baseArrayLayer + (isr_pos_.Limits()).layerCount <= (encoder.Limits()).arrayLayer)); |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 197 | |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 198 | // To see if we have a full range special case, need to compare the subres_range against the *encoders* limits |
| 199 | const auto& limits = encoder.Limits(); |
| 200 | if ((subres_range.baseArrayLayer == 0 && subres_range.layerCount == limits.arrayLayer)) { |
| 201 | if ((subres_range.baseMipLevel == 0) && (subres_range.levelCount == limits.mipLevel)) { |
| 202 | if (subres_range.aspectMask == limits.aspectMask) { |
| 203 | // Full range |
| 204 | pos_.begin = 0; |
| 205 | pos_.end = encoder.AspectSize() * limits.aspect_index; |
| 206 | aspect_count_ = 1; // Flag this to never advance aspects. |
| 207 | } else { |
| 208 | // All mips all layers but not all aspect |
| 209 | pos_.begin = encoder.AspectBase(isr_pos_.aspect_index); |
| 210 | pos_.end = pos_.begin + encoder.AspectSize(); |
| 211 | aspect_count_ = limits.aspect_index; |
| 212 | } |
| 213 | } else { |
| 214 | // All array layers, but not all levels |
| 215 | pos_.begin = encoder.AspectBase(isr_pos_.aspect_index) + subres_range.baseMipLevel * encoder.MipSize(); |
| 216 | pos_.end = pos_.begin + subres_range.levelCount * encoder.MipSize(); |
| 217 | aspect_count_ = limits.aspect_index; |
| 218 | } |
| 219 | |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 220 | // Full set of array layers at a time, thus we can span across all selected mip levels |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 221 | mip_count_ = 1; // we don't ever advance across mips, as we do all of then in one range |
| 222 | } else { |
| 223 | // Each range covers all included array_layers for each selected mip_level for each given selected aspect |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 224 | // so we'll use the general purpose encode and smallest range size |
| 225 | pos_.begin = encoder.Encode(isr_pos_); |
| 226 | pos_.end = pos_.begin + subres_range.layerCount; |
| 227 | |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 228 | // we do have to traverse across mips, though (other than Encode abover), we don't have to know which one we are on. |
| 229 | mip_count_ = subres_range.levelCount; |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 230 | aspect_count_ = limits.aspect_index; |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 231 | } |
| 232 | |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 233 | // To get to the next aspect range we offset from the last base |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 234 | aspect_base_ = pos_; |
| 235 | mip_index_ = 0; |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 236 | aspect_index_ = isr_pos_.aspect_index; |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | RangeGenerator& RangeGenerator::operator++() { |
| 240 | mip_index_++; |
| 241 | // NOTE: If all selected mip levels are done at once, mip_count_ is set to one, not the number of selected mip_levels |
| 242 | if (mip_index_ >= mip_count_) { |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 243 | const auto last_aspect_index = aspect_index_; |
| 244 | // Seek the next value aspect (if any) |
| 245 | aspect_index_ = encoder_->LowerBoundFromMask(isr_pos_.Limits().aspectMask, aspect_index_ + 1); |
| 246 | if (aspect_index_ < aspect_count_) { |
| 247 | // Force isr_pos to the beginning of this found aspect |
John Zulauf | dd18b3a | 2019-11-20 08:30:23 -0700 | [diff] [blame] | 248 | isr_pos_.SeekAspect(aspect_index_); |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 249 | // SubresourceGenerator should never be at tombstones we we aren't |
| 250 | assert(isr_pos_.aspectMask != 0); |
| 251 | |
| 252 | // Offset by the distance between the last start of aspect and *this* start of aspect |
| 253 | aspect_base_ += (encoder_->AspectBase(isr_pos_.aspect_index) - encoder_->AspectBase(last_aspect_index)); |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 254 | pos_ = aspect_base_; |
| 255 | mip_index_ = 0; |
| 256 | } else { |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 257 | // Tombstone both index range and subresource positions to "At end" convention |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 258 | pos_ = {0, 0}; |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 259 | isr_pos_.aspectMask = 0; |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 260 | } |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 261 | } else { |
| 262 | // Note: for the layerCount < full_range.layerCount case, because the generated ranges per mip_level are discontinuous |
John Zulauf | 2ea823e | 2019-11-19 08:54:59 -0700 | [diff] [blame] | 263 | // we have to do each individual array of ranges |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 264 | pos_ += encoder_->MipSize(); |
John Zulauf | dd18b3a | 2019-11-20 08:30:23 -0700 | [diff] [blame] | 265 | isr_pos_.SeekMip(isr_pos_.Limits().baseMipLevel + mip_index_); |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 266 | } |
| 267 | return *this; |
| 268 | } |
| 269 | |
locke-lunarg | ae26eac | 2020-04-16 15:29:05 -0600 | [diff] [blame] | 270 | ImageRangeEncoder::ImageRangeEncoder(const IMAGE_STATE& image) |
| 271 | : ImageRangeEncoder(image, AspectParameters::Get(image.full_range.aspectMask)) {} |
locke-lunarg | 5faaff5 | 2020-02-27 14:31:11 -0700 | [diff] [blame] | 272 | |
locke-lunarg | ae26eac | 2020-04-16 15:29:05 -0600 | [diff] [blame] | 273 | ImageRangeEncoder::ImageRangeEncoder(const IMAGE_STATE& image, const AspectParameters* param) |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 274 | : RangeEncoder(image.full_range, param), image_(&image) { |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 275 | VkSubresourceLayout layout = {}; |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 276 | VkImageSubresource subres = {}; |
| 277 | VkExtent2D divisors = {}; |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 278 | VkImageSubresourceLayers subres_layers = {limits_.aspectMask, 0, 0, limits_.arrayLayer}; |
locke-lunarg | 3f6978b | 2020-04-16 16:51:35 -0600 | [diff] [blame] | 279 | linear_image = false; |
| 280 | |
| 281 | // WORKAROUND for dev_sim and mock_icd not containing valid VkSubresourceLayout yet. Treat it as optimal image. |
| 282 | if (image_->createInfo.tiling != VK_IMAGE_TILING_OPTIMAL) { |
| 283 | subres = {static_cast<VkImageAspectFlags>(AspectBit(0)), 0, 0}; |
| 284 | DispatchGetImageSubresourceLayout(image_->store_device_as_workaround, image_->image, &subres, &layout); |
| 285 | if (layout.size > 0) { |
| 286 | linear_image = true; |
| 287 | } |
| 288 | } |
locke-lunarg | 5faaff5 | 2020-02-27 14:31:11 -0700 | [diff] [blame] | 289 | |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 290 | for (uint32_t mip_index = 0; mip_index < limits_.mipLevel; ++mip_index) { |
| 291 | subres_layers.mipLevel = mip_index; |
| 292 | auto subres_extent = GetImageSubresourceExtent(image_, &subres_layers); |
| 293 | subres_extents_.push_back(subres_extent); |
locke-lunarg | 3f6978b | 2020-04-16 16:51:35 -0600 | [diff] [blame] | 294 | subres.mipLevel = mip_index; |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 295 | for (uint32_t aspect_index = 0; aspect_index < limits_.aspect_index; ++aspect_index) { |
locke-lunarg | 3f6978b | 2020-04-16 16:51:35 -0600 | [diff] [blame] | 296 | subres.aspectMask = static_cast<VkImageAspectFlags>(AspectBit(aspect_index)); |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 297 | if (mip_index == 0) { |
locke-lunarg | f929301 | 2020-04-16 17:01:23 -0600 | [diff] [blame] | 298 | texel_sizes_.push_back(FormatTexelSize(image.createInfo.format, subres.aspectMask)); |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 299 | } |
locke-lunarg | 3f6978b | 2020-04-16 16:51:35 -0600 | [diff] [blame] | 300 | if (linear_image) { |
| 301 | DispatchGetImageSubresourceLayout(image_->store_device_as_workaround, image_->image, &subres, &layout); |
| 302 | subres_layouts_.push_back(layout); |
| 303 | } else { |
| 304 | divisors = FindMultiplaneExtentDivisors(image.createInfo.format, subres.aspectMask); |
| 305 | layout.offset += layout.size; |
locke-lunarg | f929301 | 2020-04-16 17:01:23 -0600 | [diff] [blame] | 306 | layout.rowPitch = |
| 307 | static_cast<VkDeviceSize>(ceil(subres_extent.width * texel_sizes_[aspect_index] / divisors.width)); |
locke-lunarg | 3f6978b | 2020-04-16 16:51:35 -0600 | [diff] [blame] | 308 | layout.arrayPitch = layout.rowPitch * subres_extent.height / divisors.height; |
| 309 | layout.depthPitch = layout.arrayPitch; |
| 310 | layout.size = layout.arrayPitch * limits_.arrayLayer; |
| 311 | subres_layouts_.push_back(layout); |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 312 | } |
locke-lunarg | 5faaff5 | 2020-02-27 14:31:11 -0700 | [diff] [blame] | 313 | } |
locke-lunarg | 5faaff5 | 2020-02-27 14:31:11 -0700 | [diff] [blame] | 314 | } |
locke-lunarg | 5faaff5 | 2020-02-27 14:31:11 -0700 | [diff] [blame] | 315 | } |
| 316 | |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 317 | IndexType ImageRangeEncoder::Encode(const VkImageSubresource& subres, uint32_t layer, VkOffset3D offset) const { |
| 318 | const auto& subres_layout = SubresourceLayout(subres); |
locke-lunarg | f929301 | 2020-04-16 17:01:23 -0600 | [diff] [blame] | 319 | return static_cast<IndexType>(ceil(layer * subres_layout.arrayPitch + offset.z * subres_layout.depthPitch + |
| 320 | offset.y * subres_layout.rowPitch + |
| 321 | offset.x * texel_sizes_[LowerBoundFromMask(subres.aspectMask)] + subres_layout.offset)); |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 322 | } |
locke-lunarg | 5faaff5 | 2020-02-27 14:31:11 -0700 | [diff] [blame] | 323 | |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 324 | void ImageRangeEncoder::Decode(const VkImageSubresource& subres, const IndexType& encode, uint32_t& out_layer, |
| 325 | VkOffset3D& out_offset) const { |
| 326 | const auto& subres_layout = SubresourceLayout(subres); |
| 327 | IndexType decode = encode - subres_layout.offset; |
| 328 | out_layer = static_cast<uint32_t>(decode / subres_layout.arrayPitch); |
| 329 | decode -= (out_layer * subres_layout.arrayPitch); |
| 330 | out_offset.z = static_cast<int32_t>(decode / subres_layout.depthPitch); |
| 331 | decode -= (out_offset.z * subres_layout.depthPitch); |
| 332 | out_offset.y = static_cast<int32_t>(decode / subres_layout.rowPitch); |
| 333 | decode -= (out_offset.y * subres_layout.rowPitch); |
locke-lunarg | f929301 | 2020-04-16 17:01:23 -0600 | [diff] [blame] | 334 | out_offset.x = static_cast<int32_t>(decode / texel_sizes_[LowerBoundFromMask(subres.aspectMask)]); |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 335 | } |
| 336 | |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 337 | const VkSubresourceLayout& ImageRangeEncoder::SubresourceLayout(const VkImageSubresource& subres) const { |
| 338 | uint32_t subres_layouts_index = subres.mipLevel * limits_.aspect_index + LowerBoundFromMask(subres.aspectMask); |
| 339 | return subres_layouts_[subres_layouts_index]; |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 340 | } |
| 341 | |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 342 | ImageRangeGenerator::ImageRangeGenerator(const ImageRangeEncoder& encoder, const VkImageSubresourceRange& subres_range, |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 343 | const VkOffset3D& offset, const VkExtent3D& extent) |
| 344 | : encoder_(&encoder), subres_range_(subres_range), offset_(offset), extent_(extent) { |
| 345 | assert(IsValid(*encoder_, subres_range)); |
| 346 | mip_level_index_ = 0; |
| 347 | aspect_index_ = encoder_->LowerBoundFromMask(subres_range.aspectMask); |
locke-lunarg | c93c4f0 | 2020-04-17 01:46:35 -0600 | [diff] [blame^] | 348 | if ((offset_.z + extent_.depth) == 1) { |
| 349 | range_arraylayer_base_ = subres_range.baseArrayLayer; |
| 350 | range_layer_count_ = subres_range_.layerCount; |
| 351 | } else { |
| 352 | range_arraylayer_base_ = offset_.z; |
| 353 | range_layer_count_ = extent_.depth; |
| 354 | } |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 355 | SetPos(); |
| 356 | } |
| 357 | |
| 358 | void ImageRangeGenerator::SetPos() { |
locke-lunarg | c93c4f0 | 2020-04-17 01:46:35 -0600 | [diff] [blame^] | 359 | VkImageSubresource subres = {static_cast<VkImageAspectFlags>(encoder_->AspectBit(aspect_index_)), |
| 360 | subres_range_.baseMipLevel + mip_level_index_, |
| 361 | subres_range_.baseArrayLayer}; |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 362 | subres_layout_ = &(encoder_->SubresourceLayout(subres)); |
locke-lunarg | c93c4f0 | 2020-04-17 01:46:35 -0600 | [diff] [blame^] | 363 | const VkExtent3D& subres_extent = encoder_->SubresourceExtent(subres.mipLevel); |
| 364 | Subresource limits = encoder_->Limits(); |
| 365 | |
| 366 | offset_y_count_ = static_cast<int32_t>(extent_.height); |
| 367 | layer_count_ = range_layer_count_; |
| 368 | mip_count_ = subres_range_.levelCount; |
| 369 | aspect_count_ = limits.aspect_index; |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 370 | pos_.begin = encoder_->Encode(subres, subres_range_.baseArrayLayer, offset_); |
locke-lunarg | c93c4f0 | 2020-04-17 01:46:35 -0600 | [diff] [blame^] | 371 | pos_.end = pos_.begin; |
| 372 | |
| 373 | if (offset_.x == 0 && extent_.width == subres_extent.width) { |
| 374 | if (offset_.y == 0 && extent_.height == subres_extent.height) { |
| 375 | offset_y_count_ = 1; |
| 376 | if (range_arraylayer_base_ == 0 && range_layer_count_ == limits.arrayLayer) { |
| 377 | layer_count_ = 1; |
| 378 | if (subres_range_.baseMipLevel == 0 && subres_range_.levelCount == limits.mipLevel) { |
| 379 | mip_count_ = 1; |
| 380 | for (uint32_t aspect_index = aspect_index_; aspect_index < aspect_count_;) { |
| 381 | subres.aspectMask = static_cast<VkImageAspectFlags>(encoder_->AspectBit(aspect_index)); |
| 382 | for (uint32_t mip_index = 0; mip_index < limits.mipLevel; ++mip_index) { |
| 383 | subres.mipLevel = mip_index; |
| 384 | const VkSubresourceLayout& subres_layout = encoder_->SubresourceLayout(subres); |
| 385 | pos_.end += subres_layout.size; |
| 386 | } |
| 387 | aspect_index = encoder_->LowerBoundFromMask(subres_range_.aspectMask, aspect_index + 1); |
| 388 | } |
| 389 | aspect_count_ = 1; |
| 390 | } else { |
| 391 | for (uint32_t mip_index = mip_level_index_; mip_index < subres_range_.levelCount; ++mip_index) { |
| 392 | const VkSubresourceLayout& subres_layout = encoder_->SubresourceLayout(subres); |
| 393 | pos_.end += subres_layout.size; |
| 394 | subres.mipLevel++; |
| 395 | } |
| 396 | } |
| 397 | } else { |
| 398 | pos_.end += subres_layout_->arrayPitch * range_layer_count_; |
| 399 | } |
| 400 | } else { |
| 401 | pos_.end += (subres_layout_->rowPitch * offset_y_count_); |
| 402 | } |
| 403 | } else { |
| 404 | pos_.end += static_cast<IndexType>(ceil(encoder_->TexelSize(aspect_index_) * extent_.width)); |
| 405 | } |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 406 | offset_layer_base_ = pos_; |
| 407 | offset_offset_y_base_ = pos_; |
| 408 | arrayLayer_index_ = 0; |
| 409 | offset_y_index_ = 0; |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | ImageRangeGenerator* ImageRangeGenerator::operator++() { |
| 413 | offset_y_index_++; |
| 414 | |
| 415 | if (offset_y_index_ < offset_y_count_) { |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 416 | offset_offset_y_base_ += subres_layout_->rowPitch; |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 417 | pos_ = offset_offset_y_base_; |
| 418 | } else { |
| 419 | offset_y_index_ = 0; |
| 420 | arrayLayer_index_++; |
| 421 | if (arrayLayer_index_ < layer_count_) { |
locke-lunarg | 5f7d3c6 | 2020-04-07 00:10:39 -0600 | [diff] [blame] | 422 | offset_layer_base_ += subres_layout_->arrayPitch; |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 423 | offset_offset_y_base_ = offset_layer_base_; |
| 424 | pos_ = offset_layer_base_; |
locke-lunarg | 5faaff5 | 2020-02-27 14:31:11 -0700 | [diff] [blame] | 425 | } else { |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 426 | arrayLayer_index_ = 0; |
| 427 | mip_level_index_++; |
| 428 | if (mip_level_index_ < subres_range_.levelCount) { |
| 429 | SetPos(); |
locke-lunarg | 5faaff5 | 2020-02-27 14:31:11 -0700 | [diff] [blame] | 430 | } else { |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 431 | mip_level_index_ = 0; |
| 432 | aspect_index_ = encoder_->LowerBoundFromMask(subres_range_.aspectMask, aspect_index_ + 1); |
| 433 | if (aspect_index_ < aspect_count_) { |
| 434 | SetPos(); |
locke-lunarg | 5faaff5 | 2020-02-27 14:31:11 -0700 | [diff] [blame] | 435 | } else { |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 436 | // End |
locke-lunarg | 5faaff5 | 2020-02-27 14:31:11 -0700 | [diff] [blame] | 437 | pos_ = {0, 0}; |
locke-lunarg | 5faaff5 | 2020-02-27 14:31:11 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | } |
locke-lunarg | 5faaff5 | 2020-02-27 14:31:11 -0700 | [diff] [blame] | 441 | } |
locke-lunarg | 296a3c9 | 2020-03-25 01:04:29 -0600 | [diff] [blame] | 442 | return this; |
locke-lunarg | 5faaff5 | 2020-02-27 14:31:11 -0700 | [diff] [blame] | 443 | } |
| 444 | |
John Zulauf | 1121140 | 2019-11-15 14:02:36 -0700 | [diff] [blame] | 445 | template <typename AspectTraits> |
| 446 | class AspectParametersImpl : public AspectParameters { |
| 447 | public: |
| 448 | VkImageAspectFlags AspectMask() const override { return AspectTraits::kAspectMask; } |
| 449 | MaskIndexFunc MaskToIndexFunction() const override { return &AspectTraits::MaskIndex; } |
| 450 | uint32_t AspectCount() const override { return AspectTraits::kAspectCount; }; |
| 451 | const VkImageAspectFlagBits* AspectBits() const override { return AspectTraits::AspectBits().data(); } |
| 452 | }; |
| 453 | |
| 454 | struct NullAspectTraits { |
| 455 | static constexpr uint32_t kAspectCount = 0; |
| 456 | static constexpr VkImageAspectFlags kAspectMask = 0; |
| 457 | static uint32_t MaskIndex(VkImageAspectFlags mask) { return 0; }; |
| 458 | static const std::array<VkImageAspectFlagBits, kAspectCount>& AspectBits() { |
| 459 | static std::array<VkImageAspectFlagBits, kAspectCount> kAspectBits{}; |
| 460 | return kAspectBits; |
| 461 | } |
| 462 | }; |
| 463 | |
| 464 | struct ColorAspectTraits { |
| 465 | static constexpr uint32_t kAspectCount = 1; |
| 466 | static constexpr VkImageAspectFlags kAspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 467 | static uint32_t MaskIndex(VkImageAspectFlags mask) { return 0; }; |
| 468 | static const std::array<VkImageAspectFlagBits, kAspectCount>& AspectBits() { |
| 469 | static std::array<VkImageAspectFlagBits, kAspectCount> kAspectBits{{VK_IMAGE_ASPECT_COLOR_BIT}}; |
| 470 | return kAspectBits; |
| 471 | } |
| 472 | }; |
| 473 | |
| 474 | struct DepthAspectTraits { |
| 475 | static constexpr uint32_t kAspectCount = 1; |
| 476 | static constexpr VkImageAspectFlags kAspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; |
| 477 | static uint32_t MaskIndex(VkImageAspectFlags mask) { return 0; }; |
| 478 | static const std::array<VkImageAspectFlagBits, kAspectCount>& AspectBits() { |
| 479 | static std::array<VkImageAspectFlagBits, kAspectCount> kAspectBits{{VK_IMAGE_ASPECT_DEPTH_BIT}}; |
| 480 | return kAspectBits; |
| 481 | } |
| 482 | }; |
| 483 | |
| 484 | struct StencilAspectTraits { |
| 485 | static constexpr uint32_t kAspectCount = 1; |
| 486 | static constexpr VkImageAspectFlags kAspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 487 | static uint32_t MaskIndex(VkImageAspectFlags mask) { return 0; }; |
| 488 | static const std::array<VkImageAspectFlagBits, kAspectCount>& AspectBits() { |
| 489 | static std::array<VkImageAspectFlagBits, kAspectCount> kAspectBits{{VK_IMAGE_ASPECT_STENCIL_BIT}}; |
| 490 | return kAspectBits; |
| 491 | } |
| 492 | }; |
| 493 | |
| 494 | struct DepthStencilAspectTraits { |
| 495 | // VK_IMAGE_ASPECT_DEPTH_BIT = 0x00000002, >> 1 -> 1 -1 -> 0 |
| 496 | // VK_IMAGE_ASPECT_STENCIL_BIT = 0x00000004, >> 1 -> 2 -1 = 1 |
| 497 | static constexpr uint32_t kAspectCount = 2; |
| 498 | static constexpr VkImageAspectFlags kAspectMask = (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT); |
| 499 | static uint32_t MaskIndex(VkImageAspectFlags mask) { |
| 500 | uint32_t index = (mask >> 1) - 1; |
| 501 | assert((index == 0) || (index == 1)); |
| 502 | return index; |
| 503 | }; |
| 504 | static const std::array<VkImageAspectFlagBits, kAspectCount>& AspectBits() { |
| 505 | static std::array<VkImageAspectFlagBits, kAspectCount> kAspectBits{ |
| 506 | {VK_IMAGE_ASPECT_DEPTH_BIT, VK_IMAGE_ASPECT_STENCIL_BIT}}; |
| 507 | return kAspectBits; |
| 508 | } |
| 509 | }; |
| 510 | |
| 511 | struct Multiplane2AspectTraits { |
| 512 | // VK_IMAGE_ASPECT_PLANE_0_BIT = 0x00000010, >> 4 - 1 -> 0 |
| 513 | // VK_IMAGE_ASPECT_PLANE_1_BIT = 0x00000020, >> 4 - 1 -> 1 |
| 514 | static constexpr uint32_t kAspectCount = 2; |
| 515 | static constexpr VkImageAspectFlags kAspectMask = (VK_IMAGE_ASPECT_PLANE_0_BIT | VK_IMAGE_ASPECT_PLANE_1_BIT); |
| 516 | static uint32_t MaskIndex(VkImageAspectFlags mask) { |
| 517 | uint32_t index = (mask >> 4) - 1; |
| 518 | assert((index == 0) || (index == 1)); |
| 519 | return index; |
| 520 | }; |
| 521 | static const std::array<VkImageAspectFlagBits, kAspectCount>& AspectBits() { |
| 522 | static std::array<VkImageAspectFlagBits, kAspectCount> kAspectBits{ |
| 523 | {VK_IMAGE_ASPECT_PLANE_0_BIT, VK_IMAGE_ASPECT_PLANE_1_BIT}}; |
| 524 | return kAspectBits; |
| 525 | } |
| 526 | }; |
| 527 | |
| 528 | struct Multiplane3AspectTraits { |
| 529 | // VK_IMAGE_ASPECT_PLANE_0_BIT = 0x00000010, >> 4 - 1 -> 0 |
| 530 | // VK_IMAGE_ASPECT_PLANE_1_BIT = 0x00000020, >> 4 - 1 -> 1 |
| 531 | // VK_IMAGE_ASPECT_PLANE_2_BIT = 0x00000040, >> 4 - 1 -> 3 |
| 532 | static constexpr uint32_t kAspectCount = 3; |
| 533 | static constexpr VkImageAspectFlags kAspectMask = |
| 534 | (VK_IMAGE_ASPECT_PLANE_0_BIT | VK_IMAGE_ASPECT_PLANE_1_BIT | VK_IMAGE_ASPECT_PLANE_2_BIT); |
| 535 | static uint32_t MaskIndex(VkImageAspectFlags mask) { |
| 536 | uint32_t index = (mask >> 4) - 1; |
| 537 | index = index > 2 ? 2 : index; |
| 538 | assert((index == 0) || (index == 1) || (index == 2)); |
| 539 | return index; |
| 540 | }; |
| 541 | static const std::array<VkImageAspectFlagBits, kAspectCount>& AspectBits() { |
| 542 | static std::array<VkImageAspectFlagBits, kAspectCount> kAspectBits{ |
| 543 | {VK_IMAGE_ASPECT_PLANE_0_BIT, VK_IMAGE_ASPECT_PLANE_1_BIT, VK_IMAGE_ASPECT_PLANE_2_BIT}}; |
| 544 | return kAspectBits; |
| 545 | } |
| 546 | }; |
| 547 | |
| 548 | // Create the encoder parameter suitable to the full range aspect mask (*must* be canonical) |
| 549 | const AspectParameters* AspectParameters::Get(VkImageAspectFlags aspect_mask) { |
| 550 | // We need a persitent instance of each specialist containing only a VTABLE each |
| 551 | static const AspectParametersImpl<ColorAspectTraits> kColorParam; |
| 552 | static const AspectParametersImpl<DepthAspectTraits> kDepthParam; |
| 553 | static const AspectParametersImpl<StencilAspectTraits> kStencilParam; |
| 554 | static const AspectParametersImpl<DepthStencilAspectTraits> kDepthStencilParam; |
| 555 | static const AspectParametersImpl<Multiplane2AspectTraits> kMutliplane2Param; |
| 556 | static const AspectParametersImpl<Multiplane3AspectTraits> kMutliplane3Param; |
| 557 | static const AspectParametersImpl<NullAspectTraits> kNullAspect; |
| 558 | |
| 559 | const AspectParameters* param; |
| 560 | switch (aspect_mask) { |
| 561 | case ColorAspectTraits::kAspectMask: |
| 562 | param = &kColorParam; |
| 563 | break; |
| 564 | case DepthAspectTraits::kAspectMask: |
| 565 | param = &kDepthParam; |
| 566 | break; |
| 567 | case StencilAspectTraits::kAspectMask: |
| 568 | param = &kStencilParam; |
| 569 | break; |
| 570 | case DepthStencilAspectTraits::kAspectMask: |
| 571 | param = &kDepthStencilParam; |
| 572 | break; |
| 573 | case Multiplane2AspectTraits::kAspectMask: |
| 574 | param = &kMutliplane2Param; |
| 575 | break; |
| 576 | case Multiplane3AspectTraits::kAspectMask: |
| 577 | param = &kMutliplane3Param; |
| 578 | break; |
| 579 | default: |
| 580 | assert(false); |
| 581 | param = &kNullAspect; |
| 582 | } |
| 583 | return param; |
| 584 | } |
| 585 | |
| 586 | }; // namespace subresource_adapter |