Alexis Hetu | 911b85f | 2019-04-09 17:43:05 -0400 | [diff] [blame] | 1 | // Copyright 2018 The SwiftShader Authors. All Rights Reserved. |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "VkImageView.hpp" |
| 16 | #include "VkImage.hpp" |
Chris Forbes | 2e5042a | 2019-08-23 09:03:48 -0700 | [diff] [blame] | 17 | #include <System/Math.hpp> |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 18 | |
Chris Forbes | 2527b4d | 2019-04-24 14:32:07 -0700 | [diff] [blame] | 19 | namespace |
| 20 | { |
Chris Forbes | 10186cb | 2019-05-14 18:44:52 -0700 | [diff] [blame] | 21 | VkComponentMapping ResolveComponentMapping(VkComponentMapping m, vk::Format format) |
Chris Forbes | 2527b4d | 2019-04-24 14:32:07 -0700 | [diff] [blame] | 22 | { |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 23 | m = vk::ResolveIdentityMapping(m); |
Chris Forbes | 10186cb | 2019-05-14 18:44:52 -0700 | [diff] [blame] | 24 | |
| 25 | // Replace non-present components with zero/one swizzles so that the sampler |
| 26 | // will give us correct interactions between channel replacement and texel replacement, |
| 27 | // where we've had to invent new channels behind the app's back (eg transparent decompression |
| 28 | // of ETC2 RGB -> BGRA8) |
| 29 | VkComponentSwizzle table[] = { |
| 30 | VK_COMPONENT_SWIZZLE_IDENTITY, |
| 31 | VK_COMPONENT_SWIZZLE_ZERO, |
| 32 | VK_COMPONENT_SWIZZLE_ONE, |
| 33 | VK_COMPONENT_SWIZZLE_R, |
| 34 | format.componentCount() < 2 ? VK_COMPONENT_SWIZZLE_ZERO : VK_COMPONENT_SWIZZLE_G, |
| 35 | format.componentCount() < 3 ? VK_COMPONENT_SWIZZLE_ZERO : VK_COMPONENT_SWIZZLE_B, |
| 36 | format.componentCount() < 4 ? VK_COMPONENT_SWIZZLE_ONE : VK_COMPONENT_SWIZZLE_A, |
Chris Forbes | 2527b4d | 2019-04-24 14:32:07 -0700 | [diff] [blame] | 37 | }; |
Chris Forbes | 10186cb | 2019-05-14 18:44:52 -0700 | [diff] [blame] | 38 | |
| 39 | return {table[m.r], table[m.g], table[m.b], table[m.a]}; |
Chris Forbes | 2527b4d | 2019-04-24 14:32:07 -0700 | [diff] [blame] | 40 | } |
Nicolas Capens | 22fbf8f | 2019-04-25 15:58:13 -0400 | [diff] [blame] | 41 | |
| 42 | VkImageSubresourceRange ResolveRemainingLevelsLayers(VkImageSubresourceRange range, const vk::Image *image) |
| 43 | { |
| 44 | return { |
| 45 | range.aspectMask, |
| 46 | range.baseMipLevel, |
| 47 | (range.levelCount == VK_REMAINING_MIP_LEVELS) ? (image->getMipLevels() - range.baseMipLevel) : range.levelCount, |
| 48 | range.baseArrayLayer, |
| 49 | (range.layerCount == VK_REMAINING_ARRAY_LAYERS) ? (image->getArrayLayers() - range.baseArrayLayer) : range.layerCount, |
| 50 | }; |
| 51 | } |
Chris Forbes | 2527b4d | 2019-04-24 14:32:07 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 54 | namespace vk |
| 55 | { |
| 56 | |
Ben Clayton | eac32c4 | 2019-04-26 11:25:57 +0100 | [diff] [blame] | 57 | std::atomic<uint32_t> ImageView::nextID(1); |
| 58 | |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 59 | ImageView::ImageView(const VkImageViewCreateInfo* pCreateInfo, void* mem, const vk::SamplerYcbcrConversion *ycbcrConversion) : |
Alexis Hetu | bd4cf81 | 2019-06-14 15:14:07 -0400 | [diff] [blame] | 60 | image(vk::Cast(pCreateInfo->image)), viewType(pCreateInfo->viewType), format(pCreateInfo->format), |
Chris Forbes | 10186cb | 2019-05-14 18:44:52 -0700 | [diff] [blame] | 61 | components(ResolveComponentMapping(pCreateInfo->components, format)), |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 62 | subresourceRange(ResolveRemainingLevelsLayers(pCreateInfo->subresourceRange, image)), |
| 63 | ycbcrConversion(ycbcrConversion) |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 64 | { |
| 65 | } |
| 66 | |
| 67 | size_t ImageView::ComputeRequiredAllocationSize(const VkImageViewCreateInfo* pCreateInfo) |
| 68 | { |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | void ImageView::destroy(const VkAllocationCallbacks* pAllocator) |
| 73 | { |
| 74 | } |
| 75 | |
Alexis Hetu | ed30373 | 2019-01-16 15:47:19 -0500 | [diff] [blame] | 76 | bool ImageView::imageTypesMatch(VkImageType imageType) const |
| 77 | { |
Alexis Hetu | 911b85f | 2019-04-09 17:43:05 -0400 | [diff] [blame] | 78 | uint32_t imageArrayLayers = image->getArrayLayers(); |
Alexis Hetu | ed30373 | 2019-01-16 15:47:19 -0500 | [diff] [blame] | 79 | |
Alexis Hetu | 911b85f | 2019-04-09 17:43:05 -0400 | [diff] [blame] | 80 | switch(viewType) |
Alexis Hetu | ed30373 | 2019-01-16 15:47:19 -0500 | [diff] [blame] | 81 | { |
Alexis Hetu | 911b85f | 2019-04-09 17:43:05 -0400 | [diff] [blame] | 82 | case VK_IMAGE_VIEW_TYPE_1D: |
| 83 | return (imageType == VK_IMAGE_TYPE_1D) && |
| 84 | (subresourceRange.layerCount == 1); |
| 85 | case VK_IMAGE_VIEW_TYPE_1D_ARRAY: |
| 86 | return imageType == VK_IMAGE_TYPE_1D; |
| 87 | case VK_IMAGE_VIEW_TYPE_2D: |
| 88 | return ((imageType == VK_IMAGE_TYPE_2D) || |
| 89 | ((imageType == VK_IMAGE_TYPE_3D) && |
| 90 | (imageArrayLayers == 1))) && |
| 91 | (subresourceRange.layerCount == 1); |
| 92 | case VK_IMAGE_VIEW_TYPE_2D_ARRAY: |
| 93 | return (imageType == VK_IMAGE_TYPE_2D) || |
| 94 | ((imageType == VK_IMAGE_TYPE_3D) && |
| 95 | (imageArrayLayers == 1)); |
| 96 | case VK_IMAGE_VIEW_TYPE_CUBE: |
| 97 | return image->isCube() && |
| 98 | (imageArrayLayers >= subresourceRange.layerCount) && |
| 99 | (subresourceRange.layerCount == 6); |
| 100 | case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY: |
| 101 | return image->isCube() && |
| 102 | (imageArrayLayers >= subresourceRange.layerCount) && |
| 103 | (subresourceRange.layerCount >= 6); |
| 104 | case VK_IMAGE_VIEW_TYPE_3D: |
| 105 | return (imageType == VK_IMAGE_TYPE_3D) && |
| 106 | (imageArrayLayers == 1) && |
| 107 | (subresourceRange.layerCount == 1); |
Alexis Hetu | ed30373 | 2019-01-16 15:47:19 -0500 | [diff] [blame] | 108 | default: |
Alexis Hetu | 911b85f | 2019-04-09 17:43:05 -0400 | [diff] [blame] | 109 | UNREACHABLE("Unexpected viewType %d", (int)viewType); |
Alexis Hetu | ed30373 | 2019-01-16 15:47:19 -0500 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | return false; |
| 113 | } |
| 114 | |
Alexis Hetu | bfdb95a | 2019-01-24 16:42:22 -0500 | [diff] [blame] | 115 | void ImageView::clear(const VkClearValue& clearValue, const VkImageAspectFlags aspectMask, const VkRect2D& renderArea) |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 116 | { |
| 117 | // Note: clearing ignores swizzling, so components is ignored. |
| 118 | |
Alexis Hetu | ed30373 | 2019-01-16 15:47:19 -0500 | [diff] [blame] | 119 | if(!imageTypesMatch(image->getImageType())) |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 120 | { |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 121 | UNIMPLEMENTED("imageTypesMatch"); |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 122 | } |
| 123 | |
Alexis Hetu | 04dae5e | 2019-04-08 13:41:50 -0400 | [diff] [blame] | 124 | if(!format.isCompatible(image->getFormat())) |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 125 | { |
Alexis Hetu | 04dae5e | 2019-04-08 13:41:50 -0400 | [diff] [blame] | 126 | UNIMPLEMENTED("incompatible formats"); |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 127 | } |
| 128 | |
Chris Forbes | 64cf139 | 2019-03-15 18:37:58 -0700 | [diff] [blame] | 129 | VkImageSubresourceRange sr = subresourceRange; |
| 130 | sr.aspectMask = aspectMask; |
Alexis Hetu | 04dae5e | 2019-04-08 13:41:50 -0400 | [diff] [blame] | 131 | image->clear(clearValue, format, renderArea, sr); |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 132 | } |
| 133 | |
Alexis Hetu | 1cd31ea | 2019-01-17 17:14:57 -0500 | [diff] [blame] | 134 | void ImageView::clear(const VkClearValue& clearValue, const VkImageAspectFlags aspectMask, const VkClearRect& renderArea) |
| 135 | { |
| 136 | // Note: clearing ignores swizzling, so components is ignored. |
| 137 | |
| 138 | if(!imageTypesMatch(image->getImageType())) |
| 139 | { |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 140 | UNIMPLEMENTED("imageTypesMatch"); |
Alexis Hetu | 1cd31ea | 2019-01-17 17:14:57 -0500 | [diff] [blame] | 141 | } |
| 142 | |
Alexis Hetu | 04dae5e | 2019-04-08 13:41:50 -0400 | [diff] [blame] | 143 | if(!format.isCompatible(image->getFormat())) |
Alexis Hetu | 1cd31ea | 2019-01-17 17:14:57 -0500 | [diff] [blame] | 144 | { |
Alexis Hetu | 04dae5e | 2019-04-08 13:41:50 -0400 | [diff] [blame] | 145 | UNIMPLEMENTED("incompatible formats"); |
Chris Forbes | 5822882 | 2019-04-17 12:51:29 -0700 | [diff] [blame] | 146 | } |
Alexis Hetu | 1cd31ea | 2019-01-17 17:14:57 -0500 | [diff] [blame] | 147 | |
| 148 | VkImageSubresourceRange sr; |
| 149 | sr.aspectMask = aspectMask; |
| 150 | sr.baseMipLevel = subresourceRange.baseMipLevel; |
| 151 | sr.levelCount = subresourceRange.levelCount; |
| 152 | sr.baseArrayLayer = renderArea.baseArrayLayer + subresourceRange.baseArrayLayer; |
| 153 | sr.layerCount = renderArea.layerCount; |
| 154 | |
Alexis Hetu | 04dae5e | 2019-04-08 13:41:50 -0400 | [diff] [blame] | 155 | image->clear(clearValue, format, renderArea.rect, sr); |
Alexis Hetu | 1cd31ea | 2019-01-17 17:14:57 -0500 | [diff] [blame] | 156 | } |
| 157 | |
Chris Forbes | 2e5042a | 2019-08-23 09:03:48 -0700 | [diff] [blame] | 158 | void ImageView::clearWithLayerMask(const VkClearValue &clearValue, VkImageAspectFlags aspectMask, const VkRect2D &renderArea, uint32_t layerMask) |
| 159 | { |
| 160 | while (layerMask) |
| 161 | { |
| 162 | uint32_t layer = sw::log2i(layerMask); |
| 163 | layerMask &= ~(1 << layer); |
| 164 | VkClearRect r = {renderArea, layer, 1}; |
| 165 | r.baseArrayLayer = layer; |
| 166 | clear(clearValue, aspectMask, r); |
| 167 | } |
| 168 | } |
| 169 | |
Chris Forbes | 02d4c0d | 2019-08-21 12:04:34 -0700 | [diff] [blame] | 170 | void ImageView::resolve(ImageView* resolveAttachment, int layer) |
| 171 | { |
| 172 | if((subresourceRange.levelCount != 1) || (resolveAttachment->subresourceRange.levelCount != 1)) |
| 173 | { |
| 174 | UNIMPLEMENTED("levelCount"); |
| 175 | } |
| 176 | |
| 177 | VkImageCopy region; |
| 178 | region.srcSubresource = |
| 179 | { |
| 180 | subresourceRange.aspectMask, |
| 181 | subresourceRange.baseMipLevel, |
| 182 | subresourceRange.baseArrayLayer + layer, |
| 183 | 1 |
| 184 | }; |
| 185 | region.srcOffset = { 0, 0, 0 }; |
| 186 | region.dstSubresource = |
| 187 | { |
| 188 | resolveAttachment->subresourceRange.aspectMask, |
| 189 | resolveAttachment->subresourceRange.baseMipLevel, |
| 190 | resolveAttachment->subresourceRange.baseArrayLayer + layer, |
| 191 | 1 |
| 192 | }; |
| 193 | region.dstOffset = { 0, 0, 0 }; |
| 194 | region.extent = image->getMipLevelExtent(static_cast<VkImageAspectFlagBits>(subresourceRange.aspectMask), |
| 195 | subresourceRange.baseMipLevel); |
| 196 | |
| 197 | image->copyTo(resolveAttachment->image, region); |
| 198 | } |
| 199 | |
Alexis Hetu | 54ec759 | 2019-03-20 14:37:16 -0400 | [diff] [blame] | 200 | void ImageView::resolve(ImageView* resolveAttachment) |
| 201 | { |
| 202 | if((subresourceRange.levelCount != 1) || (resolveAttachment->subresourceRange.levelCount != 1)) |
| 203 | { |
| 204 | UNIMPLEMENTED("levelCount"); |
| 205 | } |
| 206 | |
| 207 | VkImageCopy region; |
| 208 | region.srcSubresource = |
| 209 | { |
| 210 | subresourceRange.aspectMask, |
| 211 | subresourceRange.baseMipLevel, |
| 212 | subresourceRange.baseArrayLayer, |
| 213 | subresourceRange.layerCount |
| 214 | }; |
| 215 | region.srcOffset = { 0, 0, 0 }; |
| 216 | region.dstSubresource = |
| 217 | { |
| 218 | resolveAttachment->subresourceRange.aspectMask, |
| 219 | resolveAttachment->subresourceRange.baseMipLevel, |
| 220 | resolveAttachment->subresourceRange.baseArrayLayer, |
| 221 | resolveAttachment->subresourceRange.layerCount |
| 222 | }; |
| 223 | region.dstOffset = { 0, 0, 0 }; |
Nicolas Capens | ba87330 | 2019-05-16 11:25:27 -0400 | [diff] [blame] | 224 | region.extent = image->getMipLevelExtent(static_cast<VkImageAspectFlagBits>(subresourceRange.aspectMask), |
| 225 | subresourceRange.baseMipLevel); |
Alexis Hetu | 54ec759 | 2019-03-20 14:37:16 -0400 | [diff] [blame] | 226 | |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 227 | image->copyTo(resolveAttachment->image, region); |
Alexis Hetu | 54ec759 | 2019-03-20 14:37:16 -0400 | [diff] [blame] | 228 | } |
| 229 | |
Chris Forbes | 2e5042a | 2019-08-23 09:03:48 -0700 | [diff] [blame] | 230 | void ImageView::resolveWithLayerMask(ImageView *resolveAttachment, uint32_t layerMask) |
| 231 | { |
| 232 | while (layerMask) |
| 233 | { |
| 234 | int layer = sw::log2i(layerMask); |
| 235 | layerMask &= ~(1 << layer); |
| 236 | resolve(resolveAttachment, layer); |
| 237 | } |
| 238 | } |
| 239 | |
Alexis Hetu | ac87334 | 2019-04-17 15:59:03 -0400 | [diff] [blame] | 240 | const Image* ImageView::getImage(Usage usage) const |
| 241 | { |
| 242 | switch(usage) |
| 243 | { |
| 244 | case RAW: |
| 245 | return image; |
| 246 | case SAMPLING: |
Alexis Hetu | 62ad1ee | 2019-05-16 14:20:16 -0400 | [diff] [blame] | 247 | return image->getSampledImage(format); |
Alexis Hetu | ac87334 | 2019-04-17 15:59:03 -0400 | [diff] [blame] | 248 | default: |
| 249 | UNIMPLEMENTED("usage %d", int(usage)); |
| 250 | return nullptr; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | Format ImageView::getFormat(Usage usage) const |
| 255 | { |
| 256 | return ((usage == RAW) || (getImage(usage) == image)) ? format : getImage(usage)->getFormat(); |
| 257 | } |
| 258 | |
| 259 | int ImageView::rowPitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage) const |
| 260 | { |
| 261 | return getImage(usage)->rowPitchBytes(aspect, subresourceRange.baseMipLevel + mipLevel); |
| 262 | } |
| 263 | |
| 264 | int ImageView::slicePitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage) const |
| 265 | { |
| 266 | return getImage(usage)->slicePitchBytes(aspect, subresourceRange.baseMipLevel + mipLevel); |
| 267 | } |
| 268 | |
| 269 | int ImageView::layerPitchBytes(VkImageAspectFlagBits aspect, Usage usage) const |
| 270 | { |
| 271 | return static_cast<int>(getImage(usage)->getLayerSize(aspect)); |
| 272 | } |
| 273 | |
| 274 | VkExtent3D ImageView::getMipLevelExtent(uint32_t mipLevel) const |
| 275 | { |
Nicolas Capens | ba87330 | 2019-05-16 11:25:27 -0400 | [diff] [blame] | 276 | return image->getMipLevelExtent(static_cast<VkImageAspectFlagBits>(subresourceRange.aspectMask), |
| 277 | subresourceRange.baseMipLevel + mipLevel); |
Alexis Hetu | ac87334 | 2019-04-17 15:59:03 -0400 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void *ImageView::getOffsetPointer(const VkOffset3D& offset, VkImageAspectFlagBits aspect, uint32_t mipLevel, uint32_t layer, Usage usage) const |
Alexis Hetu | 6159a85 | 2019-02-26 14:42:36 -0500 | [diff] [blame] | 281 | { |
Nicolas Capens | 9e73510 | 2019-04-18 15:03:06 -0400 | [diff] [blame] | 282 | ASSERT(mipLevel < subresourceRange.levelCount); |
| 283 | |
Alexis Hetu | 6159a85 | 2019-02-26 14:42:36 -0500 | [diff] [blame] | 284 | VkImageSubresourceLayers imageSubresourceLayers = |
| 285 | { |
Alexis Hetu | f955e63 | 2019-03-08 15:15:20 -0500 | [diff] [blame] | 286 | static_cast<VkImageAspectFlags>(aspect), |
Nicolas Capens | 9e73510 | 2019-04-18 15:03:06 -0400 | [diff] [blame] | 287 | subresourceRange.baseMipLevel + mipLevel, |
Nicolas Capens | a195abb | 2019-04-25 17:15:56 -0400 | [diff] [blame] | 288 | subresourceRange.baseArrayLayer + layer, |
Alexis Hetu | 6159a85 | 2019-02-26 14:42:36 -0500 | [diff] [blame] | 289 | subresourceRange.layerCount |
| 290 | }; |
Nicolas Capens | ba87330 | 2019-05-16 11:25:27 -0400 | [diff] [blame] | 291 | |
Alexis Hetu | ac87334 | 2019-04-17 15:59:03 -0400 | [diff] [blame] | 292 | return getImage(usage)->getTexelPointer(offset, imageSubresourceLayers); |
Chris Forbes | 7c33e88 | 2019-02-21 14:58:28 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Chris Forbes | 2995dc2 | 2019-03-02 14:57:20 -0800 | [diff] [blame] | 295 | } |