blob: 65dca48c9b80a2075eafa97aca2ef377445d02f4 [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
Alexis Hetu25ec7b02019-03-12 14:19:22 -040018#include "VkFormat.h"
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
30class ImageView : public Object<ImageView, VkImageView>
31{
32public:
Alexis Hetuac873342019-04-17 15:59:03 -040033 // Image usage:
34 // RAW: Use the base image as is
35 // SAMPLING: Image used for texture sampling
Ben Clayton2ed93ab2019-12-17 20:38:03 +000036 enum Usage
37 {
38 RAW,
39 SAMPLING
40 };
Alexis Hetuac873342019-04-17 15:59:03 -040041
Ben Clayton2ed93ab2019-12-17 20:38:03 +000042 ImageView(const VkImageViewCreateInfo *pCreateInfo, void *mem, const vk::SamplerYcbcrConversion *ycbcrConversion);
43 void destroy(const VkAllocationCallbacks *pAllocator);
Alexis Hetu9fbaf692018-11-19 11:30:43 -050044
Ben Clayton2ed93ab2019-12-17 20:38:03 +000045 static size_t ComputeRequiredAllocationSize(const VkImageViewCreateInfo *pCreateInfo);
Alexis Hetu9fbaf692018-11-19 11:30:43 -050046
Ben Clayton2ed93ab2019-12-17 20:38:03 +000047 void clear(const VkClearValue &clearValues, VkImageAspectFlags aspectMask, const VkRect2D &renderArea);
48 void clear(const VkClearValue &clearValue, VkImageAspectFlags aspectMask, const VkClearRect &renderArea);
Chris Forbes2e5042a2019-08-23 09:03:48 -070049 void clearWithLayerMask(const VkClearValue &clearValue, VkImageAspectFlags aspectMask, const VkRect2D &renderArea, uint32_t layerMask);
Ben Clayton2ed93ab2019-12-17 20:38:03 +000050 void resolve(ImageView *resolveAttachment);
51 void resolve(ImageView *resolveAttachment, int layer);
Chris Forbes2e5042a2019-08-23 09:03:48 -070052 void resolveWithLayerMask(ImageView *resolveAttachment, uint32_t layerMask);
Alexis Hetu9fbaf692018-11-19 11:30:43 -050053
Nicolas Capens7d867272019-04-08 22:51:08 -040054 VkImageViewType getType() const { return viewType; }
Alexis Hetuac873342019-04-17 15:59:03 -040055 Format getFormat(Usage usage = RAW) const;
Nicolas Capensba873302019-05-16 11:25:27 -040056 Format getFormat(VkImageAspectFlagBits aspect) const { return image->getFormat(aspect); }
Alexis Hetuac873342019-04-17 15:59:03 -040057 int rowPitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage = RAW) const;
58 int slicePitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage = RAW) const;
Alexis Hetu8a6dcf72019-11-26 17:24:42 -050059 int getMipLevelSize(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage = RAW) const;
Alexis Hetuac873342019-04-17 15:59:03 -040060 int layerPitchBytes(VkImageAspectFlagBits aspect, Usage usage = RAW) const;
61 VkExtent3D getMipLevelExtent(uint32_t mipLevel) const;
Alexis Hetu6159a852019-02-26 14:42:36 -050062
Chris Forbes52a3bba2019-05-03 15:11:41 -070063 int getSampleCount() const
64 {
Nicolas Capens81bc9d92019-12-16 15:05:57 -050065 switch(image->getSampleCountFlagBits())
Chris Forbes52a3bba2019-05-03 15:11:41 -070066 {
Ben Clayton2ed93ab2019-12-17 20:38:03 +000067 case VK_SAMPLE_COUNT_1_BIT: return 1;
68 case VK_SAMPLE_COUNT_4_BIT: return 4;
69 default:
Nicolas Capens865f8892020-01-21 14:27:10 -050070 UNSUPPORTED("Sample count flags %d", image->getSampleCountFlagBits());
Ben Clayton2ed93ab2019-12-17 20:38:03 +000071 return 1;
Chris Forbes52a3bba2019-05-03 15:11:41 -070072 }
73 }
74
Ben Clayton2ed93ab2019-12-17 20:38:03 +000075 void *getOffsetPointer(const VkOffset3D &offset, VkImageAspectFlagBits aspect, uint32_t mipLevel, uint32_t layer, Usage usage = RAW) const;
Chris Forbes2995dc22019-03-02 14:57:20 -080076 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 Forbes7c33e882019-02-21 14:58:28 -080078
Alexis Hetuac873342019-04-17 15:59:03 -040079 void prepareForSampling() const { image->prepareForSampling(subresourceRange); }
80
Nicolas Capens7d867272019-04-08 22:51:08 -040081 const VkComponentMapping &getComponentMapping() const { return components; }
82 const VkImageSubresourceRange &getSubresourceRange() const { return subresourceRange; }
Chris Forbes821feba2019-04-24 12:22:02 -070083 size_t getImageSizeInBytes() const { return image->getMemoryRequirements().size; }
Alexis Hetued303732019-01-16 15:47:19 -050084
Ben Claytoneac32c42019-04-26 11:25:57 +010085 const uint32_t id = nextID++;
Nicolas Capens6b63c802019-05-16 11:10:34 -040086
Nicolas Capens7d867272019-04-08 22:51:08 -040087private:
Ben Claytoneac32c42019-04-26 11:25:57 +010088 static std::atomic<uint32_t> nextID;
Ben Clayton2ed93ab2019-12-17 20:38:03 +000089 friend class BufferView; // ImageView/BufferView share the ID space above.
Ben Claytoneac32c42019-04-26 11:25:57 +010090
Ben Clayton2ed93ab2019-12-17 20:38:03 +000091 bool imageTypesMatch(VkImageType imageType) const;
92 const Image *getImage(Usage usage) const;
Nicolas Capens7d867272019-04-08 22:51:08 -040093
Ben Clayton2ed93ab2019-12-17 20:38:03 +000094 Image *const image = nullptr;
95 const VkImageViewType viewType = VK_IMAGE_VIEW_TYPE_2D;
96 const Format format;
97 const VkComponentMapping components = {};
Nicolas Capens7d867272019-04-08 22:51:08 -040098 const VkImageSubresourceRange subresourceRange = {};
Nicolas Capens6b63c802019-05-16 11:10:34 -040099
100 const vk::SamplerYcbcrConversion *ycbcrConversion = nullptr;
Alexis Hetu9fbaf692018-11-19 11:30:43 -0500101};
102
Nicolas Capens6b63c802019-05-16 11:10:34 -0400103// TODO(b/132437008): Also used by SamplerYcbcrConversion. Move somewhere centrally?
104inline VkComponentMapping ResolveIdentityMapping(VkComponentMapping m)
105{
106 return {
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000107 (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 Capens6b63c802019-05-16 11:10:34 -0400112}
113
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000114static inline ImageView *Cast(VkImageView object)
Alexis Hetu9fbaf692018-11-19 11:30:43 -0500115{
Alexis Hetubd4cf812019-06-14 15:14:07 -0400116 return ImageView::Cast(object);
Alexis Hetu9fbaf692018-11-19 11:30:43 -0500117}
118
Nicolas Capens157ba262019-12-10 17:49:14 -0500119} // namespace vk
Alexis Hetu9fbaf692018-11-19 11:30:43 -0500120
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000121#endif // VK_IMAGE_VIEW_HPP_