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" |
| 17 | |
Chris Forbes | 2527b4d | 2019-04-24 14:32:07 -0700 | [diff] [blame] | 18 | namespace |
| 19 | { |
Chris Forbes | 10186cb | 2019-05-14 18:44:52 -0700 | [diff] [blame] | 20 | VkComponentMapping ResolveComponentMapping(VkComponentMapping m, vk::Format format) |
Chris Forbes | 2527b4d | 2019-04-24 14:32:07 -0700 | [diff] [blame] | 21 | { |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 22 | m = vk::ResolveIdentityMapping(m); |
Chris Forbes | 10186cb | 2019-05-14 18:44:52 -0700 | [diff] [blame] | 23 | |
| 24 | // Replace non-present components with zero/one swizzles so that the sampler |
| 25 | // will give us correct interactions between channel replacement and texel replacement, |
| 26 | // where we've had to invent new channels behind the app's back (eg transparent decompression |
| 27 | // of ETC2 RGB -> BGRA8) |
| 28 | VkComponentSwizzle table[] = { |
| 29 | VK_COMPONENT_SWIZZLE_IDENTITY, |
| 30 | VK_COMPONENT_SWIZZLE_ZERO, |
| 31 | VK_COMPONENT_SWIZZLE_ONE, |
| 32 | VK_COMPONENT_SWIZZLE_R, |
| 33 | format.componentCount() < 2 ? VK_COMPONENT_SWIZZLE_ZERO : VK_COMPONENT_SWIZZLE_G, |
| 34 | format.componentCount() < 3 ? VK_COMPONENT_SWIZZLE_ZERO : VK_COMPONENT_SWIZZLE_B, |
| 35 | format.componentCount() < 4 ? VK_COMPONENT_SWIZZLE_ONE : VK_COMPONENT_SWIZZLE_A, |
Chris Forbes | 2527b4d | 2019-04-24 14:32:07 -0700 | [diff] [blame] | 36 | }; |
Chris Forbes | 10186cb | 2019-05-14 18:44:52 -0700 | [diff] [blame] | 37 | |
| 38 | 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] | 39 | } |
Nicolas Capens | 22fbf8f | 2019-04-25 15:58:13 -0400 | [diff] [blame] | 40 | |
| 41 | VkImageSubresourceRange ResolveRemainingLevelsLayers(VkImageSubresourceRange range, const vk::Image *image) |
| 42 | { |
| 43 | return { |
| 44 | range.aspectMask, |
| 45 | range.baseMipLevel, |
| 46 | (range.levelCount == VK_REMAINING_MIP_LEVELS) ? (image->getMipLevels() - range.baseMipLevel) : range.levelCount, |
| 47 | range.baseArrayLayer, |
| 48 | (range.layerCount == VK_REMAINING_ARRAY_LAYERS) ? (image->getArrayLayers() - range.baseArrayLayer) : range.layerCount, |
| 49 | }; |
| 50 | } |
Chris Forbes | 2527b4d | 2019-04-24 14:32:07 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 53 | namespace vk |
| 54 | { |
| 55 | |
Ben Clayton | eac32c4 | 2019-04-26 11:25:57 +0100 | [diff] [blame] | 56 | std::atomic<uint32_t> ImageView::nextID(1); |
| 57 | |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 58 | ImageView::ImageView(const VkImageViewCreateInfo* pCreateInfo, void* mem, const vk::SamplerYcbcrConversion *ycbcrConversion) : |
Alexis Hetu | ed30373 | 2019-01-16 15:47:19 -0500 | [diff] [blame] | 59 | image(Cast(pCreateInfo->image)), viewType(pCreateInfo->viewType), format(pCreateInfo->format), |
Chris Forbes | 10186cb | 2019-05-14 18:44:52 -0700 | [diff] [blame] | 60 | components(ResolveComponentMapping(pCreateInfo->components, format)), |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 61 | subresourceRange(ResolveRemainingLevelsLayers(pCreateInfo->subresourceRange, image)), |
| 62 | ycbcrConversion(ycbcrConversion) |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 63 | { |
| 64 | } |
| 65 | |
| 66 | size_t ImageView::ComputeRequiredAllocationSize(const VkImageViewCreateInfo* pCreateInfo) |
| 67 | { |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | void ImageView::destroy(const VkAllocationCallbacks* pAllocator) |
| 72 | { |
| 73 | } |
| 74 | |
Alexis Hetu | ed30373 | 2019-01-16 15:47:19 -0500 | [diff] [blame] | 75 | bool ImageView::imageTypesMatch(VkImageType imageType) const |
| 76 | { |
Alexis Hetu | 911b85f | 2019-04-09 17:43:05 -0400 | [diff] [blame] | 77 | uint32_t imageArrayLayers = image->getArrayLayers(); |
Alexis Hetu | ed30373 | 2019-01-16 15:47:19 -0500 | [diff] [blame] | 78 | |
Alexis Hetu | 911b85f | 2019-04-09 17:43:05 -0400 | [diff] [blame] | 79 | switch(viewType) |
Alexis Hetu | ed30373 | 2019-01-16 15:47:19 -0500 | [diff] [blame] | 80 | { |
Alexis Hetu | 911b85f | 2019-04-09 17:43:05 -0400 | [diff] [blame] | 81 | case VK_IMAGE_VIEW_TYPE_1D: |
| 82 | return (imageType == VK_IMAGE_TYPE_1D) && |
| 83 | (subresourceRange.layerCount == 1); |
| 84 | case VK_IMAGE_VIEW_TYPE_1D_ARRAY: |
| 85 | return imageType == VK_IMAGE_TYPE_1D; |
| 86 | case VK_IMAGE_VIEW_TYPE_2D: |
| 87 | return ((imageType == VK_IMAGE_TYPE_2D) || |
| 88 | ((imageType == VK_IMAGE_TYPE_3D) && |
| 89 | (imageArrayLayers == 1))) && |
| 90 | (subresourceRange.layerCount == 1); |
| 91 | case VK_IMAGE_VIEW_TYPE_2D_ARRAY: |
| 92 | return (imageType == VK_IMAGE_TYPE_2D) || |
| 93 | ((imageType == VK_IMAGE_TYPE_3D) && |
| 94 | (imageArrayLayers == 1)); |
| 95 | case VK_IMAGE_VIEW_TYPE_CUBE: |
| 96 | return image->isCube() && |
| 97 | (imageArrayLayers >= subresourceRange.layerCount) && |
| 98 | (subresourceRange.layerCount == 6); |
| 99 | case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY: |
| 100 | return image->isCube() && |
| 101 | (imageArrayLayers >= subresourceRange.layerCount) && |
| 102 | (subresourceRange.layerCount >= 6); |
| 103 | case VK_IMAGE_VIEW_TYPE_3D: |
| 104 | return (imageType == VK_IMAGE_TYPE_3D) && |
| 105 | (imageArrayLayers == 1) && |
| 106 | (subresourceRange.layerCount == 1); |
Alexis Hetu | ed30373 | 2019-01-16 15:47:19 -0500 | [diff] [blame] | 107 | default: |
Alexis Hetu | 911b85f | 2019-04-09 17:43:05 -0400 | [diff] [blame] | 108 | UNREACHABLE("Unexpected viewType %d", (int)viewType); |
Alexis Hetu | ed30373 | 2019-01-16 15:47:19 -0500 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | return false; |
| 112 | } |
| 113 | |
Alexis Hetu | bfdb95a | 2019-01-24 16:42:22 -0500 | [diff] [blame] | 114 | void ImageView::clear(const VkClearValue& clearValue, const VkImageAspectFlags aspectMask, const VkRect2D& renderArea) |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 115 | { |
| 116 | // Note: clearing ignores swizzling, so components is ignored. |
| 117 | |
Alexis Hetu | ed30373 | 2019-01-16 15:47:19 -0500 | [diff] [blame] | 118 | if(!imageTypesMatch(image->getImageType())) |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 119 | { |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 120 | UNIMPLEMENTED("imageTypesMatch"); |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 121 | } |
| 122 | |
Alexis Hetu | 04dae5e | 2019-04-08 13:41:50 -0400 | [diff] [blame] | 123 | if(!format.isCompatible(image->getFormat())) |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 124 | { |
Alexis Hetu | 04dae5e | 2019-04-08 13:41:50 -0400 | [diff] [blame] | 125 | UNIMPLEMENTED("incompatible formats"); |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 126 | } |
| 127 | |
Chris Forbes | 64cf139 | 2019-03-15 18:37:58 -0700 | [diff] [blame] | 128 | VkImageSubresourceRange sr = subresourceRange; |
| 129 | sr.aspectMask = aspectMask; |
Alexis Hetu | 04dae5e | 2019-04-08 13:41:50 -0400 | [diff] [blame] | 130 | image->clear(clearValue, format, renderArea, sr); |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 131 | } |
| 132 | |
Alexis Hetu | 1cd31ea | 2019-01-17 17:14:57 -0500 | [diff] [blame] | 133 | void ImageView::clear(const VkClearValue& clearValue, const VkImageAspectFlags aspectMask, const VkClearRect& renderArea) |
| 134 | { |
| 135 | // Note: clearing ignores swizzling, so components is ignored. |
| 136 | |
| 137 | if(!imageTypesMatch(image->getImageType())) |
| 138 | { |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 139 | UNIMPLEMENTED("imageTypesMatch"); |
Alexis Hetu | 1cd31ea | 2019-01-17 17:14:57 -0500 | [diff] [blame] | 140 | } |
| 141 | |
Alexis Hetu | 04dae5e | 2019-04-08 13:41:50 -0400 | [diff] [blame] | 142 | if(!format.isCompatible(image->getFormat())) |
Alexis Hetu | 1cd31ea | 2019-01-17 17:14:57 -0500 | [diff] [blame] | 143 | { |
Alexis Hetu | 04dae5e | 2019-04-08 13:41:50 -0400 | [diff] [blame] | 144 | UNIMPLEMENTED("incompatible formats"); |
Chris Forbes | 5822882 | 2019-04-17 12:51:29 -0700 | [diff] [blame] | 145 | } |
Alexis Hetu | 1cd31ea | 2019-01-17 17:14:57 -0500 | [diff] [blame] | 146 | |
| 147 | VkImageSubresourceRange sr; |
| 148 | sr.aspectMask = aspectMask; |
| 149 | sr.baseMipLevel = subresourceRange.baseMipLevel; |
| 150 | sr.levelCount = subresourceRange.levelCount; |
| 151 | sr.baseArrayLayer = renderArea.baseArrayLayer + subresourceRange.baseArrayLayer; |
| 152 | sr.layerCount = renderArea.layerCount; |
| 153 | |
Alexis Hetu | 04dae5e | 2019-04-08 13:41:50 -0400 | [diff] [blame] | 154 | image->clear(clearValue, format, renderArea.rect, sr); |
Alexis Hetu | 1cd31ea | 2019-01-17 17:14:57 -0500 | [diff] [blame] | 155 | } |
| 156 | |
Alexis Hetu | 54ec759 | 2019-03-20 14:37:16 -0400 | [diff] [blame] | 157 | void ImageView::resolve(ImageView* resolveAttachment) |
| 158 | { |
| 159 | if((subresourceRange.levelCount != 1) || (resolveAttachment->subresourceRange.levelCount != 1)) |
| 160 | { |
| 161 | UNIMPLEMENTED("levelCount"); |
| 162 | } |
| 163 | |
| 164 | VkImageCopy region; |
| 165 | region.srcSubresource = |
| 166 | { |
| 167 | subresourceRange.aspectMask, |
| 168 | subresourceRange.baseMipLevel, |
| 169 | subresourceRange.baseArrayLayer, |
| 170 | subresourceRange.layerCount |
| 171 | }; |
| 172 | region.srcOffset = { 0, 0, 0 }; |
| 173 | region.dstSubresource = |
| 174 | { |
| 175 | resolveAttachment->subresourceRange.aspectMask, |
| 176 | resolveAttachment->subresourceRange.baseMipLevel, |
| 177 | resolveAttachment->subresourceRange.baseArrayLayer, |
| 178 | resolveAttachment->subresourceRange.layerCount |
| 179 | }; |
| 180 | region.dstOffset = { 0, 0, 0 }; |
Nicolas Capens | ba87330 | 2019-05-16 11:25:27 -0400 | [diff] [blame^] | 181 | region.extent = image->getMipLevelExtent(static_cast<VkImageAspectFlagBits>(subresourceRange.aspectMask), |
| 182 | subresourceRange.baseMipLevel); |
Alexis Hetu | 54ec759 | 2019-03-20 14:37:16 -0400 | [diff] [blame] | 183 | |
| 184 | image->copyTo(*(resolveAttachment->image), region); |
| 185 | } |
| 186 | |
Alexis Hetu | ac87334 | 2019-04-17 15:59:03 -0400 | [diff] [blame] | 187 | const Image* ImageView::getImage(Usage usage) const |
| 188 | { |
| 189 | switch(usage) |
| 190 | { |
| 191 | case RAW: |
| 192 | return image; |
| 193 | case SAMPLING: |
Alexis Hetu | 62ad1ee | 2019-05-16 14:20:16 -0400 | [diff] [blame] | 194 | return image->getSampledImage(format); |
Alexis Hetu | ac87334 | 2019-04-17 15:59:03 -0400 | [diff] [blame] | 195 | default: |
| 196 | UNIMPLEMENTED("usage %d", int(usage)); |
| 197 | return nullptr; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | Format ImageView::getFormat(Usage usage) const |
| 202 | { |
| 203 | return ((usage == RAW) || (getImage(usage) == image)) ? format : getImage(usage)->getFormat(); |
| 204 | } |
| 205 | |
| 206 | int ImageView::rowPitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage) const |
| 207 | { |
| 208 | return getImage(usage)->rowPitchBytes(aspect, subresourceRange.baseMipLevel + mipLevel); |
| 209 | } |
| 210 | |
| 211 | int ImageView::slicePitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage) const |
| 212 | { |
| 213 | return getImage(usage)->slicePitchBytes(aspect, subresourceRange.baseMipLevel + mipLevel); |
| 214 | } |
| 215 | |
| 216 | int ImageView::layerPitchBytes(VkImageAspectFlagBits aspect, Usage usage) const |
| 217 | { |
| 218 | return static_cast<int>(getImage(usage)->getLayerSize(aspect)); |
| 219 | } |
| 220 | |
| 221 | VkExtent3D ImageView::getMipLevelExtent(uint32_t mipLevel) const |
| 222 | { |
Nicolas Capens | ba87330 | 2019-05-16 11:25:27 -0400 | [diff] [blame^] | 223 | return image->getMipLevelExtent(static_cast<VkImageAspectFlagBits>(subresourceRange.aspectMask), |
| 224 | subresourceRange.baseMipLevel + mipLevel); |
Alexis Hetu | ac87334 | 2019-04-17 15:59:03 -0400 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | 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] | 228 | { |
Nicolas Capens | 9e73510 | 2019-04-18 15:03:06 -0400 | [diff] [blame] | 229 | ASSERT(mipLevel < subresourceRange.levelCount); |
| 230 | |
Alexis Hetu | 6159a85 | 2019-02-26 14:42:36 -0500 | [diff] [blame] | 231 | VkImageSubresourceLayers imageSubresourceLayers = |
| 232 | { |
Alexis Hetu | f955e63 | 2019-03-08 15:15:20 -0500 | [diff] [blame] | 233 | static_cast<VkImageAspectFlags>(aspect), |
Nicolas Capens | 9e73510 | 2019-04-18 15:03:06 -0400 | [diff] [blame] | 234 | subresourceRange.baseMipLevel + mipLevel, |
Nicolas Capens | a195abb | 2019-04-25 17:15:56 -0400 | [diff] [blame] | 235 | subresourceRange.baseArrayLayer + layer, |
Alexis Hetu | 6159a85 | 2019-02-26 14:42:36 -0500 | [diff] [blame] | 236 | subresourceRange.layerCount |
| 237 | }; |
Nicolas Capens | ba87330 | 2019-05-16 11:25:27 -0400 | [diff] [blame^] | 238 | |
Alexis Hetu | ac87334 | 2019-04-17 15:59:03 -0400 | [diff] [blame] | 239 | return getImage(usage)->getTexelPointer(offset, imageSubresourceLayers); |
Chris Forbes | 7c33e88 | 2019-02-21 14:58:28 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Chris Forbes | 2995dc2 | 2019-03-02 14:57:20 -0800 | [diff] [blame] | 242 | } |