Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 1 | // Copyright 2018 The SwiftShader Authors. All Rights Reserved. |
| 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 | #ifndef VK_IMAGE_VIEW_HPP_ |
| 16 | #define VK_IMAGE_VIEW_HPP_ |
| 17 | |
Antonio Maiorano | 42fd159 | 2020-04-27 11:30:40 -0400 | [diff] [blame] | 18 | #include "VkFormat.hpp" |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 19 | #include "VkImage.hpp" |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 20 | #include "VkObject.hpp" |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 21 | |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 22 | #include "System/Debug.hpp" |
| 23 | |
Ben Clayton | eac32c4 | 2019-04-26 11:25:57 +0100 | [diff] [blame] | 24 | #include <atomic> |
| 25 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 26 | namespace vk { |
| 27 | |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 28 | class SamplerYcbcrConversion; |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 29 | |
Nicolas Capens | 7709026 | 2020-03-19 00:26:21 -0400 | [diff] [blame] | 30 | // Uniquely identifies state used by sampling routine generation. |
| 31 | // ID space shared by image views and buffer views. |
| 32 | union Identifier |
| 33 | { |
| 34 | // Image view identifier |
| 35 | Identifier(const Image *image, VkImageViewType type, VkFormat format, VkComponentMapping mapping); |
| 36 | |
| 37 | // Buffer view identifier |
| 38 | Identifier(VkFormat format); |
| 39 | |
| 40 | operator uint32_t() const |
| 41 | { |
| 42 | static_assert(sizeof(Identifier) == sizeof(uint32_t), "Identifier must be 32-bit"); |
| 43 | return id; |
| 44 | } |
| 45 | |
| 46 | uint32_t id = 0; |
| 47 | |
| 48 | struct |
| 49 | { |
| 50 | uint32_t imageViewType : 3; |
| 51 | uint32_t format : 8; |
| 52 | uint32_t r : 3; |
| 53 | uint32_t g : 3; |
| 54 | uint32_t b : 3; |
| 55 | uint32_t a : 3; |
Nicolas Capens | 7709026 | 2020-03-19 00:26:21 -0400 | [diff] [blame] | 56 | }; |
| 57 | }; |
| 58 | |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 59 | class ImageView : public Object<ImageView, VkImageView> |
| 60 | { |
| 61 | public: |
Alexis Hetu | ac87334 | 2019-04-17 15:59:03 -0400 | [diff] [blame] | 62 | // Image usage: |
| 63 | // RAW: Use the base image as is |
| 64 | // SAMPLING: Image used for texture sampling |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 65 | enum Usage |
| 66 | { |
| 67 | RAW, |
| 68 | SAMPLING |
| 69 | }; |
Alexis Hetu | ac87334 | 2019-04-17 15:59:03 -0400 | [diff] [blame] | 70 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 71 | ImageView(const VkImageViewCreateInfo *pCreateInfo, void *mem, const vk::SamplerYcbcrConversion *ycbcrConversion); |
| 72 | void destroy(const VkAllocationCallbacks *pAllocator); |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 73 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 74 | static size_t ComputeRequiredAllocationSize(const VkImageViewCreateInfo *pCreateInfo); |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 75 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 76 | void clear(const VkClearValue &clearValues, VkImageAspectFlags aspectMask, const VkRect2D &renderArea); |
| 77 | void clear(const VkClearValue &clearValue, VkImageAspectFlags aspectMask, const VkClearRect &renderArea); |
Chris Forbes | 2e5042a | 2019-08-23 09:03:48 -0700 | [diff] [blame] | 78 | void clearWithLayerMask(const VkClearValue &clearValue, VkImageAspectFlags aspectMask, const VkRect2D &renderArea, uint32_t layerMask); |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 79 | void resolve(ImageView *resolveAttachment); |
| 80 | void resolve(ImageView *resolveAttachment, int layer); |
Chris Forbes | 2e5042a | 2019-08-23 09:03:48 -0700 | [diff] [blame] | 81 | void resolveWithLayerMask(ImageView *resolveAttachment, uint32_t layerMask); |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 82 | |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 83 | VkImageViewType getType() const { return viewType; } |
Alexis Hetu | ac87334 | 2019-04-17 15:59:03 -0400 | [diff] [blame] | 84 | Format getFormat(Usage usage = RAW) const; |
Nicolas Capens | ba87330 | 2019-05-16 11:25:27 -0400 | [diff] [blame] | 85 | Format getFormat(VkImageAspectFlagBits aspect) const { return image->getFormat(aspect); } |
Alexis Hetu | ac87334 | 2019-04-17 15:59:03 -0400 | [diff] [blame] | 86 | int rowPitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage = RAW) const; |
| 87 | int slicePitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage = RAW) const; |
Alexis Hetu | 8a6dcf7 | 2019-11-26 17:24:42 -0500 | [diff] [blame] | 88 | int getMipLevelSize(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage = RAW) const; |
Alexis Hetu | ac87334 | 2019-04-17 15:59:03 -0400 | [diff] [blame] | 89 | int layerPitchBytes(VkImageAspectFlagBits aspect, Usage usage = RAW) const; |
| 90 | VkExtent3D getMipLevelExtent(uint32_t mipLevel) const; |
Alexis Hetu | 6159a85 | 2019-02-26 14:42:36 -0500 | [diff] [blame] | 91 | |
Chris Forbes | 52a3bba | 2019-05-03 15:11:41 -0700 | [diff] [blame] | 92 | int getSampleCount() const |
| 93 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 94 | switch(image->getSampleCountFlagBits()) |
Chris Forbes | 52a3bba | 2019-05-03 15:11:41 -0700 | [diff] [blame] | 95 | { |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 96 | case VK_SAMPLE_COUNT_1_BIT: return 1; |
| 97 | case VK_SAMPLE_COUNT_4_BIT: return 4; |
| 98 | default: |
Nicolas Capens | 865f889 | 2020-01-21 14:27:10 -0500 | [diff] [blame] | 99 | UNSUPPORTED("Sample count flags %d", image->getSampleCountFlagBits()); |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 100 | return 1; |
Chris Forbes | 52a3bba | 2019-05-03 15:11:41 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 104 | void *getOffsetPointer(const VkOffset3D &offset, VkImageAspectFlagBits aspect, uint32_t mipLevel, uint32_t layer, Usage usage = RAW) const; |
Chris Forbes | 2995dc2 | 2019-03-02 14:57:20 -0800 | [diff] [blame] | 105 | bool hasDepthAspect() const { return (subresourceRange.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != 0; } |
| 106 | bool hasStencilAspect() const { return (subresourceRange.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) != 0; } |
Chris Forbes | 7c33e88 | 2019-02-21 14:58:28 -0800 | [diff] [blame] | 107 | |
Alexis Hetu | 4f438a5 | 2020-06-15 16:13:51 -0400 | [diff] [blame] | 108 | // This function is only called from the renderer, so use the USING_STORAGE flag, |
| 109 | // as it is required in order to write to an image from a shader |
| 110 | void contentsChanged() { image->contentsChanged(subresourceRange, Image::USING_STORAGE); } |
| 111 | |
| 112 | void prepareForSampling() { image->prepareForSampling(subresourceRange); } |
Alexis Hetu | ac87334 | 2019-04-17 15:59:03 -0400 | [diff] [blame] | 113 | |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 114 | const VkComponentMapping &getComponentMapping() const { return components; } |
| 115 | const VkImageSubresourceRange &getSubresourceRange() const { return subresourceRange; } |
Alexis Hetu | 0774188 | 2020-04-01 16:49:03 -0400 | [diff] [blame] | 116 | size_t getSizeInBytes() const { return image->getSizeInBytes(subresourceRange); } |
Alexis Hetu | ed30373 | 2019-01-16 15:47:19 -0500 | [diff] [blame] | 117 | |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 118 | private: |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 119 | bool imageTypesMatch(VkImageType imageType) const; |
| 120 | const Image *getImage(Usage usage) const; |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 121 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 122 | Image *const image = nullptr; |
| 123 | const VkImageViewType viewType = VK_IMAGE_VIEW_TYPE_2D; |
Nicolas Capens | 7709026 | 2020-03-19 00:26:21 -0400 | [diff] [blame] | 124 | const Format format = VK_FORMAT_UNDEFINED; |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 125 | const VkComponentMapping components = {}; |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 126 | const VkImageSubresourceRange subresourceRange = {}; |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 127 | |
| 128 | const vk::SamplerYcbcrConversion *ycbcrConversion = nullptr; |
Nicolas Capens | 7709026 | 2020-03-19 00:26:21 -0400 | [diff] [blame] | 129 | |
| 130 | public: |
| 131 | const Identifier id; |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 132 | }; |
| 133 | |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 134 | // TODO(b/132437008): Also used by SamplerYcbcrConversion. Move somewhere centrally? |
| 135 | inline VkComponentMapping ResolveIdentityMapping(VkComponentMapping m) |
| 136 | { |
| 137 | return { |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 138 | (m.r == VK_COMPONENT_SWIZZLE_IDENTITY) ? VK_COMPONENT_SWIZZLE_R : m.r, |
| 139 | (m.g == VK_COMPONENT_SWIZZLE_IDENTITY) ? VK_COMPONENT_SWIZZLE_G : m.g, |
| 140 | (m.b == VK_COMPONENT_SWIZZLE_IDENTITY) ? VK_COMPONENT_SWIZZLE_B : m.b, |
| 141 | (m.a == VK_COMPONENT_SWIZZLE_IDENTITY) ? VK_COMPONENT_SWIZZLE_A : m.a, |
| 142 | }; |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 145 | static inline ImageView *Cast(VkImageView object) |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 146 | { |
Alexis Hetu | bd4cf81 | 2019-06-14 15:14:07 -0400 | [diff] [blame] | 147 | return ImageView::Cast(object); |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 148 | } |
| 149 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 150 | } // namespace vk |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 151 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 152 | #endif // VK_IMAGE_VIEW_HPP_ |