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 | |
Alexis Hetu | 25ec7b0 | 2019-03-12 14:19:22 -0400 | [diff] [blame] | 18 | #include "VkFormat.h" |
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 | |
| 30 | class ImageView : public Object<ImageView, VkImageView> |
| 31 | { |
| 32 | public: |
Alexis Hetu | ac87334 | 2019-04-17 15:59:03 -0400 | [diff] [blame] | 33 | // Image usage: |
| 34 | // RAW: Use the base image as is |
| 35 | // SAMPLING: Image used for texture sampling |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 36 | enum Usage |
| 37 | { |
| 38 | RAW, |
| 39 | SAMPLING |
| 40 | }; |
Alexis Hetu | ac87334 | 2019-04-17 15:59:03 -0400 | [diff] [blame] | 41 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 42 | ImageView(const VkImageViewCreateInfo *pCreateInfo, void *mem, const vk::SamplerYcbcrConversion *ycbcrConversion); |
| 43 | void destroy(const VkAllocationCallbacks *pAllocator); |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 44 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 45 | static size_t ComputeRequiredAllocationSize(const VkImageViewCreateInfo *pCreateInfo); |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 46 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 47 | void clear(const VkClearValue &clearValues, VkImageAspectFlags aspectMask, const VkRect2D &renderArea); |
| 48 | void clear(const VkClearValue &clearValue, VkImageAspectFlags aspectMask, const VkClearRect &renderArea); |
Chris Forbes | 2e5042a | 2019-08-23 09:03:48 -0700 | [diff] [blame] | 49 | 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] | 50 | void resolve(ImageView *resolveAttachment); |
| 51 | void resolve(ImageView *resolveAttachment, int layer); |
Chris Forbes | 2e5042a | 2019-08-23 09:03:48 -0700 | [diff] [blame] | 52 | void resolveWithLayerMask(ImageView *resolveAttachment, uint32_t layerMask); |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 53 | |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 54 | VkImageViewType getType() const { return viewType; } |
Alexis Hetu | ac87334 | 2019-04-17 15:59:03 -0400 | [diff] [blame] | 55 | Format getFormat(Usage usage = RAW) const; |
Nicolas Capens | ba87330 | 2019-05-16 11:25:27 -0400 | [diff] [blame] | 56 | Format getFormat(VkImageAspectFlagBits aspect) const { return image->getFormat(aspect); } |
Alexis Hetu | ac87334 | 2019-04-17 15:59:03 -0400 | [diff] [blame] | 57 | int rowPitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage = RAW) const; |
| 58 | int slicePitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage = RAW) const; |
Alexis Hetu | 8a6dcf7 | 2019-11-26 17:24:42 -0500 | [diff] [blame] | 59 | int getMipLevelSize(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage = RAW) const; |
Alexis Hetu | ac87334 | 2019-04-17 15:59:03 -0400 | [diff] [blame] | 60 | int layerPitchBytes(VkImageAspectFlagBits aspect, Usage usage = RAW) const; |
| 61 | VkExtent3D getMipLevelExtent(uint32_t mipLevel) const; |
Alexis Hetu | 6159a85 | 2019-02-26 14:42:36 -0500 | [diff] [blame] | 62 | |
Chris Forbes | 52a3bba | 2019-05-03 15:11:41 -0700 | [diff] [blame] | 63 | int getSampleCount() const |
| 64 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 65 | switch(image->getSampleCountFlagBits()) |
Chris Forbes | 52a3bba | 2019-05-03 15:11:41 -0700 | [diff] [blame] | 66 | { |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 67 | case VK_SAMPLE_COUNT_1_BIT: return 1; |
| 68 | case VK_SAMPLE_COUNT_4_BIT: return 4; |
| 69 | default: |
Nicolas Capens | 865f889 | 2020-01-21 14:27:10 -0500 | [diff] [blame] | 70 | UNSUPPORTED("Sample count flags %d", image->getSampleCountFlagBits()); |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 71 | return 1; |
Chris Forbes | 52a3bba | 2019-05-03 15:11:41 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 75 | 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] | 76 | bool hasDepthAspect() const { return (subresourceRange.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != 0; } |
| 77 | bool hasStencilAspect() const { return (subresourceRange.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) != 0; } |
Chris Forbes | 7c33e88 | 2019-02-21 14:58:28 -0800 | [diff] [blame] | 78 | |
Alexis Hetu | ac87334 | 2019-04-17 15:59:03 -0400 | [diff] [blame] | 79 | void prepareForSampling() const { image->prepareForSampling(subresourceRange); } |
| 80 | |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 81 | const VkComponentMapping &getComponentMapping() const { return components; } |
| 82 | const VkImageSubresourceRange &getSubresourceRange() const { return subresourceRange; } |
Chris Forbes | 821feba | 2019-04-24 12:22:02 -0700 | [diff] [blame] | 83 | size_t getImageSizeInBytes() const { return image->getMemoryRequirements().size; } |
Alexis Hetu | ed30373 | 2019-01-16 15:47:19 -0500 | [diff] [blame] | 84 | |
Ben Clayton | eac32c4 | 2019-04-26 11:25:57 +0100 | [diff] [blame] | 85 | const uint32_t id = nextID++; |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 86 | |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 87 | private: |
Ben Clayton | eac32c4 | 2019-04-26 11:25:57 +0100 | [diff] [blame] | 88 | static std::atomic<uint32_t> nextID; |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 89 | friend class BufferView; // ImageView/BufferView share the ID space above. |
Ben Clayton | eac32c4 | 2019-04-26 11:25:57 +0100 | [diff] [blame] | 90 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 91 | bool imageTypesMatch(VkImageType imageType) const; |
| 92 | const Image *getImage(Usage usage) const; |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 93 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 94 | Image *const image = nullptr; |
| 95 | const VkImageViewType viewType = VK_IMAGE_VIEW_TYPE_2D; |
| 96 | const Format format; |
| 97 | const VkComponentMapping components = {}; |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 98 | const VkImageSubresourceRange subresourceRange = {}; |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 99 | |
| 100 | const vk::SamplerYcbcrConversion *ycbcrConversion = nullptr; |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 101 | }; |
| 102 | |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 103 | // TODO(b/132437008): Also used by SamplerYcbcrConversion. Move somewhere centrally? |
| 104 | inline VkComponentMapping ResolveIdentityMapping(VkComponentMapping m) |
| 105 | { |
| 106 | return { |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 107 | (m.r == VK_COMPONENT_SWIZZLE_IDENTITY) ? VK_COMPONENT_SWIZZLE_R : m.r, |
| 108 | (m.g == VK_COMPONENT_SWIZZLE_IDENTITY) ? VK_COMPONENT_SWIZZLE_G : m.g, |
| 109 | (m.b == VK_COMPONENT_SWIZZLE_IDENTITY) ? VK_COMPONENT_SWIZZLE_B : m.b, |
| 110 | (m.a == VK_COMPONENT_SWIZZLE_IDENTITY) ? VK_COMPONENT_SWIZZLE_A : m.a, |
| 111 | }; |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 112 | } |
| 113 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 114 | static inline ImageView *Cast(VkImageView object) |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 115 | { |
Alexis Hetu | bd4cf81 | 2019-06-14 15:14:07 -0400 | [diff] [blame] | 116 | return ImageView::Cast(object); |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 117 | } |
| 118 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 119 | } // namespace vk |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 120 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 121 | #endif // VK_IMAGE_VIEW_HPP_ |