blob: 297ffd1ec67d90b6fc3f86fe49eb27d0f8bb91c9 [file] [log] [blame]
Alexis Hetu9fbaf692018-11-19 11:30:43 -05001// 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 Maiorano42fd1592020-04-27 11:30:40 -040018#include "VkFormat.hpp"
Alexis Hetu9fbaf692018-11-19 11:30:43 -050019#include "VkImage.hpp"
Ben Clayton2ed93ab2019-12-17 20:38:03 +000020#include "VkObject.hpp"
Alexis Hetu9fbaf692018-11-19 11:30:43 -050021
Ben Clayton25e06e02020-02-07 11:19:08 +000022#include "System/Debug.hpp"
23
Ben Claytoneac32c42019-04-26 11:25:57 +010024#include <atomic>
25
Nicolas Capens157ba262019-12-10 17:49:14 -050026namespace vk {
27
Nicolas Capens6b63c802019-05-16 11:10:34 -040028class SamplerYcbcrConversion;
Alexis Hetu9fbaf692018-11-19 11:30:43 -050029
Nicolas Capens77090262020-03-19 00:26:21 -040030// Uniquely identifies state used by sampling routine generation.
31// ID space shared by image views and buffer views.
32union 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 Capens77090262020-03-19 00:26:21 -040056 };
57};
58
Alexis Hetu9fbaf692018-11-19 11:30:43 -050059class ImageView : public Object<ImageView, VkImageView>
60{
61public:
Alexis Hetuac873342019-04-17 15:59:03 -040062 // Image usage:
63 // RAW: Use the base image as is
64 // SAMPLING: Image used for texture sampling
Ben Clayton2ed93ab2019-12-17 20:38:03 +000065 enum Usage
66 {
67 RAW,
68 SAMPLING
69 };
Alexis Hetuac873342019-04-17 15:59:03 -040070
Ben Clayton2ed93ab2019-12-17 20:38:03 +000071 ImageView(const VkImageViewCreateInfo *pCreateInfo, void *mem, const vk::SamplerYcbcrConversion *ycbcrConversion);
72 void destroy(const VkAllocationCallbacks *pAllocator);
Alexis Hetu9fbaf692018-11-19 11:30:43 -050073
Ben Clayton2ed93ab2019-12-17 20:38:03 +000074 static size_t ComputeRequiredAllocationSize(const VkImageViewCreateInfo *pCreateInfo);
Alexis Hetu9fbaf692018-11-19 11:30:43 -050075
Ben Clayton2ed93ab2019-12-17 20:38:03 +000076 void clear(const VkClearValue &clearValues, VkImageAspectFlags aspectMask, const VkRect2D &renderArea);
77 void clear(const VkClearValue &clearValue, VkImageAspectFlags aspectMask, const VkClearRect &renderArea);
Chris Forbes2e5042a2019-08-23 09:03:48 -070078 void clearWithLayerMask(const VkClearValue &clearValue, VkImageAspectFlags aspectMask, const VkRect2D &renderArea, uint32_t layerMask);
Ben Clayton2ed93ab2019-12-17 20:38:03 +000079 void resolve(ImageView *resolveAttachment);
80 void resolve(ImageView *resolveAttachment, int layer);
Chris Forbes2e5042a2019-08-23 09:03:48 -070081 void resolveWithLayerMask(ImageView *resolveAttachment, uint32_t layerMask);
Alexis Hetu9fbaf692018-11-19 11:30:43 -050082
Nicolas Capens7d867272019-04-08 22:51:08 -040083 VkImageViewType getType() const { return viewType; }
Alexis Hetuac873342019-04-17 15:59:03 -040084 Format getFormat(Usage usage = RAW) const;
Nicolas Capensba873302019-05-16 11:25:27 -040085 Format getFormat(VkImageAspectFlagBits aspect) const { return image->getFormat(aspect); }
Alexis Hetuac873342019-04-17 15:59:03 -040086 int rowPitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage = RAW) const;
87 int slicePitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage = RAW) const;
Alexis Hetu8a6dcf72019-11-26 17:24:42 -050088 int getMipLevelSize(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage = RAW) const;
Alexis Hetuac873342019-04-17 15:59:03 -040089 int layerPitchBytes(VkImageAspectFlagBits aspect, Usage usage = RAW) const;
90 VkExtent3D getMipLevelExtent(uint32_t mipLevel) const;
Alexis Hetu6159a852019-02-26 14:42:36 -050091
Chris Forbes52a3bba2019-05-03 15:11:41 -070092 int getSampleCount() const
93 {
Nicolas Capens81bc9d92019-12-16 15:05:57 -050094 switch(image->getSampleCountFlagBits())
Chris Forbes52a3bba2019-05-03 15:11:41 -070095 {
Ben Clayton2ed93ab2019-12-17 20:38:03 +000096 case VK_SAMPLE_COUNT_1_BIT: return 1;
97 case VK_SAMPLE_COUNT_4_BIT: return 4;
98 default:
Nicolas Capens865f8892020-01-21 14:27:10 -050099 UNSUPPORTED("Sample count flags %d", image->getSampleCountFlagBits());
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000100 return 1;
Chris Forbes52a3bba2019-05-03 15:11:41 -0700101 }
102 }
103
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000104 void *getOffsetPointer(const VkOffset3D &offset, VkImageAspectFlagBits aspect, uint32_t mipLevel, uint32_t layer, Usage usage = RAW) const;
Chris Forbes2995dc22019-03-02 14:57:20 -0800105 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 Forbes7c33e882019-02-21 14:58:28 -0800107
Alexis Hetu4f438a52020-06-15 16:13:51 -0400108 // 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 Hetuac873342019-04-17 15:59:03 -0400113
Nicolas Capens7d867272019-04-08 22:51:08 -0400114 const VkComponentMapping &getComponentMapping() const { return components; }
115 const VkImageSubresourceRange &getSubresourceRange() const { return subresourceRange; }
Alexis Hetu07741882020-04-01 16:49:03 -0400116 size_t getSizeInBytes() const { return image->getSizeInBytes(subresourceRange); }
Alexis Hetued303732019-01-16 15:47:19 -0500117
Nicolas Capens7d867272019-04-08 22:51:08 -0400118private:
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000119 bool imageTypesMatch(VkImageType imageType) const;
120 const Image *getImage(Usage usage) const;
Nicolas Capens7d867272019-04-08 22:51:08 -0400121
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000122 Image *const image = nullptr;
123 const VkImageViewType viewType = VK_IMAGE_VIEW_TYPE_2D;
Nicolas Capens77090262020-03-19 00:26:21 -0400124 const Format format = VK_FORMAT_UNDEFINED;
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000125 const VkComponentMapping components = {};
Nicolas Capens7d867272019-04-08 22:51:08 -0400126 const VkImageSubresourceRange subresourceRange = {};
Nicolas Capens6b63c802019-05-16 11:10:34 -0400127
128 const vk::SamplerYcbcrConversion *ycbcrConversion = nullptr;
Nicolas Capens77090262020-03-19 00:26:21 -0400129
130public:
131 const Identifier id;
Alexis Hetu9fbaf692018-11-19 11:30:43 -0500132};
133
Nicolas Capens6b63c802019-05-16 11:10:34 -0400134// TODO(b/132437008): Also used by SamplerYcbcrConversion. Move somewhere centrally?
135inline VkComponentMapping ResolveIdentityMapping(VkComponentMapping m)
136{
137 return {
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000138 (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 Capens6b63c802019-05-16 11:10:34 -0400143}
144
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000145static inline ImageView *Cast(VkImageView object)
Alexis Hetu9fbaf692018-11-19 11:30:43 -0500146{
Alexis Hetubd4cf812019-06-14 15:14:07 -0400147 return ImageView::Cast(object);
Alexis Hetu9fbaf692018-11-19 11:30:43 -0500148}
149
Nicolas Capens157ba262019-12-10 17:49:14 -0500150} // namespace vk
Alexis Hetu9fbaf692018-11-19 11:30:43 -0500151
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000152#endif // VK_IMAGE_VIEW_HPP_