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 | |
18 | #include "VkDebug.hpp" |
19 | #include "VkFormat.h" |
20 | #include "VkObject.hpp" |
21 | #include "VkImage.hpp" |
22 | |
23 | #include <atomic> |
24 | |
25 | namespace vk |
26 | { |
27 | class SamplerYcbcrConversion; |
28 | |
29 | class ImageView : public Object<ImageView, VkImageView> |
30 | { |
31 | public: |
32 | // Image usage: |
33 | // RAW: Use the base image as is |
34 | // SAMPLING: Image used for texture sampling |
35 | enum Usage { RAW, SAMPLING }; |
36 | |
37 | ImageView(const VkImageViewCreateInfo* pCreateInfo, void* mem, const vk::SamplerYcbcrConversion *ycbcrConversion); |
38 | void destroy(const VkAllocationCallbacks* pAllocator); |
39 | |
40 | static size_t ComputeRequiredAllocationSize(const VkImageViewCreateInfo* pCreateInfo); |
41 | |
42 | void clear(const VkClearValue& clearValues, VkImageAspectFlags aspectMask, const VkRect2D& renderArea); |
43 | void clear(const VkClearValue& clearValue, VkImageAspectFlags aspectMask, const VkClearRect& renderArea); |
44 | void clearWithLayerMask(const VkClearValue &clearValue, VkImageAspectFlags aspectMask, const VkRect2D &renderArea, uint32_t layerMask); |
45 | void resolve(ImageView* resolveAttachment); |
46 | void resolve(ImageView* resolveAttachment, int layer); |
47 | void resolveWithLayerMask(ImageView *resolveAttachment, uint32_t layerMask); |
48 | |
49 | VkImageViewType getType() const { return viewType; } |
50 | Format getFormat(Usage usage = RAW) const; |
51 | Format getFormat(VkImageAspectFlagBits aspect) const { return image->getFormat(aspect); } |
52 | int rowPitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage = RAW) const; |
53 | int slicePitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage = RAW) const; |
54 | int layerPitchBytes(VkImageAspectFlagBits aspect, Usage usage = RAW) const; |
55 | VkExtent3D getMipLevelExtent(uint32_t mipLevel) const; |
56 | |
57 | int getSampleCount() const |
58 | { |
59 | switch (image->getSampleCountFlagBits()) |
60 | { |
61 | case VK_SAMPLE_COUNT_1_BIT: return 1; |
62 | case VK_SAMPLE_COUNT_4_BIT: return 4; |
63 | default: |
64 | UNIMPLEMENTED("Sample count flags %d" , image->getSampleCountFlagBits()); |
65 | return 1; |
66 | } |
67 | } |
68 | |
69 | void *getOffsetPointer(const VkOffset3D& offset, VkImageAspectFlagBits aspect, uint32_t mipLevel, uint32_t layer, Usage usage = RAW) const; |
70 | bool hasDepthAspect() const { return (subresourceRange.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != 0; } |
71 | bool hasStencilAspect() const { return (subresourceRange.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) != 0; } |
72 | |
73 | void prepareForSampling() const { image->prepareForSampling(subresourceRange); } |
74 | |
75 | const VkComponentMapping &getComponentMapping() const { return components; } |
76 | const VkImageSubresourceRange &getSubresourceRange() const { return subresourceRange; } |
77 | size_t getImageSizeInBytes() const { return image->getMemoryRequirements().size; } |
78 | |
79 | const uint32_t id = nextID++; |
80 | |
81 | private: |
82 | static std::atomic<uint32_t> nextID; |
83 | friend class BufferView; // ImageView/BufferView share the ID space above. |
84 | |
85 | bool imageTypesMatch(VkImageType imageType) const; |
86 | const Image* getImage(Usage usage) const; |
87 | |
88 | Image *const image = nullptr; |
89 | const VkImageViewType viewType = VK_IMAGE_VIEW_TYPE_2D; |
90 | const Format format; |
91 | const VkComponentMapping components = {}; |
92 | const VkImageSubresourceRange subresourceRange = {}; |
93 | |
94 | const vk::SamplerYcbcrConversion *ycbcrConversion = nullptr; |
95 | }; |
96 | |
97 | // TODO(b/132437008): Also used by SamplerYcbcrConversion. Move somewhere centrally? |
98 | inline VkComponentMapping ResolveIdentityMapping(VkComponentMapping m) |
99 | { |
100 | return { |
101 | (m.r == VK_COMPONENT_SWIZZLE_IDENTITY) ? VK_COMPONENT_SWIZZLE_R : m.r, |
102 | (m.g == VK_COMPONENT_SWIZZLE_IDENTITY) ? VK_COMPONENT_SWIZZLE_G : m.g, |
103 | (m.b == VK_COMPONENT_SWIZZLE_IDENTITY) ? VK_COMPONENT_SWIZZLE_B : m.b, |
104 | (m.a == VK_COMPONENT_SWIZZLE_IDENTITY) ? VK_COMPONENT_SWIZZLE_A : m.a, |
105 | }; |
106 | } |
107 | |
108 | static inline ImageView* Cast(VkImageView object) |
109 | { |
110 | return ImageView::Cast(object); |
111 | } |
112 | |
113 | } // namespace vk |
114 | |
115 | #endif // VK_IMAGE_VIEW_HPP_ |
116 | |