blob: d19b467b7279b023f3355ec6339b3db068248240 [file] [log] [blame]
Alexis Hetuf62f3752018-11-15 14:51:15 -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_HPP_
16#define VK_IMAGE_HPP_
17
Antonio Maiorano42fd1592020-04-27 11:30:40 -040018#include "VkFormat.hpp"
Ben Clayton2ed93ab2019-12-17 20:38:03 +000019#include "VkObject.hpp"
Alexis Hetuf62f3752018-11-15 14:51:15 -050020
Alexis Hetu4f438a52020-06-15 16:13:51 -040021#include "marl/mutex.h"
22
Alexis Hetu52445952019-10-31 17:51:07 -040023#ifdef __ANDROID__
Ben Clayton2ed93ab2019-12-17 20:38:03 +000024# include <vulkan/vk_android_native_buffer.h> // For VkSwapchainImageUsageFlagsANDROID and buffer_handle_t
Alexis Hetu52445952019-10-31 17:51:07 -040025#endif
26
Alexis Hetu4f438a52020-06-15 16:13:51 -040027#include <unordered_set>
28
Nicolas Capens157ba262019-12-10 17:49:14 -050029namespace vk {
Alexis Hetuf62f3752018-11-15 14:51:15 -050030
Alexis Hetu63ae9242019-06-06 13:52:15 -040031class Buffer;
Alexis Hetu0da99f52019-02-27 12:54:52 -050032class Device;
Alexis Hetu04595962018-12-14 11:07:39 -050033class DeviceMemory;
34
Alexis Hetu52445952019-10-31 17:51:07 -040035#ifdef __ANDROID__
Ben Clayton2ed93ab2019-12-17 20:38:03 +000036struct BackingMemory
37{
Alexis Hetu52445952019-10-31 17:51:07 -040038 int stride = 0;
39 bool externalMemory = false;
40 buffer_handle_t nativeHandle = nullptr;
41 VkSwapchainImageUsageFlagsANDROID androidUsage = 0;
42};
43#endif
44
Alexis Hetuf62f3752018-11-15 14:51:15 -050045class Image : public Object<Image, VkImage>
46{
47public:
Ben Clayton2ed93ab2019-12-17 20:38:03 +000048 Image(const VkImageCreateInfo *pCreateInfo, void *mem, Device *device);
49 void destroy(const VkAllocationCallbacks *pAllocator);
Alexis Hetuf62f3752018-11-15 14:51:15 -050050
Alexis Hetu52445952019-10-31 17:51:07 -040051#ifdef __ANDROID__
52 VkResult prepareForExternalUseANDROID() const;
53#endif
54
Ben Clayton2ed93ab2019-12-17 20:38:03 +000055 static size_t ComputeRequiredAllocationSize(const VkImageCreateInfo *pCreateInfo);
Alexis Hetuf62f3752018-11-15 14:51:15 -050056
Alexis Hetua233cea2018-12-07 11:53:19 -050057 const VkMemoryRequirements getMemoryRequirements() const;
Alexis Hetu07741882020-04-01 16:49:03 -040058 size_t getSizeInBytes(const VkImageSubresourceRange &subresourceRange) const;
Ben Clayton2ed93ab2019-12-17 20:38:03 +000059 void getSubresourceLayout(const VkImageSubresource *pSubresource, VkSubresourceLayout *pLayout) const;
60 void bind(DeviceMemory *pDeviceMemory, VkDeviceSize pMemoryOffset);
61 void copyTo(Image *dstImage, const VkImageCopy &pRegion) const;
62 void copyTo(Buffer *dstBuffer, const VkBufferImageCopy &region);
63 void copyFrom(Buffer *srcBuffer, const VkBufferImageCopy &region);
Alexis Hetua233cea2018-12-07 11:53:19 -050064
Nicolas Capens32f4be12020-06-10 13:14:07 -040065 void blitTo(Image *dstImage, const VkImageBlit &region, VkFilter filter) const;
66 void copyTo(uint8_t *dst, unsigned int dstPitch) const;
67 void resolveTo(Image *dstImage, const VkImageResolve &region) const;
Ben Clayton2ed93ab2019-12-17 20:38:03 +000068 void clear(const VkClearValue &clearValue, const vk::Format &viewFormat, const VkRect2D &renderArea, const VkImageSubresourceRange &subresourceRange);
69 void clear(const VkClearColorValue &color, const VkImageSubresourceRange &subresourceRange);
70 void clear(const VkClearDepthStencilValue &color, const VkImageSubresourceRange &subresourceRange);
Alexis Hetu9fbaf692018-11-19 11:30:43 -050071
Nicolas Capensad658262020-07-03 10:06:27 -040072 // Get the last layer and mipmap level, handling VK_REMAINING_ARRAY_LAYERS and
73 // VK_REMAINING_MIP_LEVELS, respectively. Note VkImageSubresourceLayers does not
74 // allow these symbolic values, so only VkImageSubresourceRange is accepted.
75 uint32_t getLastLayerIndex(const VkImageSubresourceRange &subresourceRange) const;
76 uint32_t getLastMipLevel(const VkImageSubresourceRange &subresourceRange) const;
77
Ben Clayton2ed93ab2019-12-17 20:38:03 +000078 VkImageType getImageType() const { return imageType; }
79 const Format &getFormat() const { return format; }
80 Format getFormat(VkImageAspectFlagBits aspect) const;
81 uint32_t getArrayLayers() const { return arrayLayers; }
82 uint32_t getMipLevels() const { return mipLevels; }
83 VkImageUsageFlags getUsage() const { return usage; }
Ben Clayton2ed93ab2019-12-17 20:38:03 +000084 VkSampleCountFlagBits getSampleCountFlagBits() const { return samples; }
85 VkExtent3D getMipLevelExtent(VkImageAspectFlagBits aspect, uint32_t mipLevel) const;
86 int rowPitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel) const;
87 int slicePitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel) const;
Alexis Hetu46159712020-06-15 16:13:51 -040088 void *getTexelPointer(const VkOffset3D &offset, const VkImageSubresource &subresource) const;
Ben Clayton2ed93ab2019-12-17 20:38:03 +000089 bool isCube() const;
90 bool is3DSlice() const;
91 uint8_t *end() const;
92 VkDeviceSize getLayerSize(VkImageAspectFlagBits aspect) const;
93 VkDeviceSize getMipLevelSize(VkImageAspectFlagBits aspect, uint32_t mipLevel) const;
94 bool canBindToMemory(DeviceMemory *pDeviceMemory) const;
Alexis Hetu9fbaf692018-11-19 11:30:43 -050095
Ben Clayton2ed93ab2019-12-17 20:38:03 +000096 void prepareForSampling(const VkImageSubresourceRange &subresourceRange);
Alexis Hetu4f438a52020-06-15 16:13:51 -040097 enum ContentsChangedContext
98 {
99 DIRECT_MEMORY_ACCESS = 0,
100 USING_STORAGE = 1
101 };
102 void contentsChanged(const VkImageSubresourceRange &subresourceRange, ContentsChangedContext contentsChangedContext = DIRECT_MEMORY_ACCESS);
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000103 const Image *getSampledImage(const vk::Format &imageViewFormat) const;
Alexis Hetuac873342019-04-17 15:59:03 -0400104
Alexis Hetu52445952019-10-31 17:51:07 -0400105#ifdef __ANDROID__
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000106 void setBackingMemory(BackingMemory &bm)
107 {
108 backingMemory = bm;
109 }
110 bool hasExternalMemory() const { return backingMemory.externalMemory; }
111 VkDeviceMemory getExternalMemory() const;
Alexis Hetu52445952019-10-31 17:51:07 -0400112#endif
113
Alexis Hetuf62f3752018-11-15 14:51:15 -0500114private:
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000115 void copy(Buffer *buffer, const VkBufferImageCopy &region, bool bufferIsSource);
Chris Forbes2995dc22019-03-02 14:57:20 -0800116 VkDeviceSize getStorageSize(VkImageAspectFlags flags) const;
Alexis Hetu54ec7592019-03-20 14:37:16 -0400117 VkDeviceSize getMultiSampledLevelSize(VkImageAspectFlagBits aspect, uint32_t mipLevel) const;
Alexis Hetu32ac8312019-04-15 17:20:29 -0400118 VkDeviceSize getLayerOffset(VkImageAspectFlagBits aspect, uint32_t mipLevel) const;
Chris Forbes2995dc22019-03-02 14:57:20 -0800119 VkDeviceSize getMemoryOffset(VkImageAspectFlagBits aspect, uint32_t mipLevel) const;
120 VkDeviceSize getMemoryOffset(VkImageAspectFlagBits aspect, uint32_t mipLevel, uint32_t layer) const;
Alexis Hetu46159712020-06-15 16:13:51 -0400121 VkDeviceSize texelOffsetBytesInStorage(const VkOffset3D &offset, const VkImageSubresource &subresource) const;
Chris Forbes2995dc22019-03-02 14:57:20 -0800122 VkDeviceSize getMemoryOffset(VkImageAspectFlagBits aspect) const;
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000123 VkExtent3D imageExtentInBlocks(const VkExtent3D &extent, VkImageAspectFlagBits aspect) const;
124 VkOffset3D imageOffsetInBlocks(const VkOffset3D &offset, VkImageAspectFlagBits aspect) const;
125 VkExtent2D bufferExtentInBlocks(const VkExtent2D &extent, const VkBufferImageCopy &region) const;
Alexis Hetueca9ca62019-01-16 14:21:33 -0500126 VkFormat getClearFormat() const;
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000127 void clear(void *pixelData, VkFormat pixelFormat, const vk::Format &viewFormat, const VkImageSubresourceRange &subresourceRange, const VkRect2D &renderArea);
Nicolas Capensba873302019-05-16 11:25:27 -0400128 int borderSize() const;
Alexis Hetu4f438a52020-06-15 16:13:51 -0400129 bool requiresPreprocessing() const;
Alexis Hetu46159712020-06-15 16:13:51 -0400130 void decompress(const VkImageSubresource &subresource);
Alexis Hetu4f438a52020-06-15 16:13:51 -0400131 bool updateCube(const VkImageSubresource &subresource);
Alexis Hetu46159712020-06-15 16:13:51 -0400132 void decodeETC2(const VkImageSubresource &subresource);
133 void decodeBC(const VkImageSubresource &subresource);
134 void decodeASTC(const VkImageSubresource &subresource);
Alexis Hetua233cea2018-12-07 11:53:19 -0500135
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000136 const Device *const device = nullptr;
137 DeviceMemory *deviceMemory = nullptr;
138 VkDeviceSize memoryOffset = 0;
139 VkImageCreateFlags flags = 0;
140 VkImageType imageType = VK_IMAGE_TYPE_2D;
141 Format format;
142 VkExtent3D extent = { 0, 0, 0 };
143 uint32_t mipLevels = 0;
144 uint32_t arrayLayers = 0;
145 VkSampleCountFlagBits samples = VK_SAMPLE_COUNT_1_BIT;
146 VkImageTiling tiling = VK_IMAGE_TILING_OPTIMAL;
147 VkImageUsageFlags usage = (VkImageUsageFlags)0;
148 Image *decompressedImage = nullptr;
Alexis Hetu52445952019-10-31 17:51:07 -0400149#ifdef __ANDROID__
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000150 BackingMemory backingMemory = {};
Alexis Hetu52445952019-10-31 17:51:07 -0400151#endif
David 'Digit' Turner359bc802019-08-14 17:46:07 +0200152
153 VkExternalMemoryHandleTypeFlags supportedExternalMemoryHandleTypes = (VkExternalMemoryHandleTypeFlags)0;
Alexis Hetu4f438a52020-06-15 16:13:51 -0400154
155 // VkImageSubresource wrapper for use in unordered_set
156 class Subresource
157 {
158 public:
159 Subresource()
160 : subresource{ (VkImageAspectFlags)0, 0, 0 }
161 {}
162 Subresource(const VkImageSubresource &subres)
163 : subresource(subres)
164 {}
165 inline operator VkImageSubresource() const { return subresource; }
166
167 bool operator==(const Subresource &other) const
168 {
169 return (subresource.aspectMask == other.subresource.aspectMask) &&
170 (subresource.mipLevel == other.subresource.mipLevel) &&
171 (subresource.arrayLayer == other.subresource.arrayLayer);
172 };
173
174 size_t operator()(const Subresource &other) const
175 {
176 return static_cast<size_t>(other.subresource.aspectMask) ^
177 static_cast<size_t>(other.subresource.mipLevel) ^
178 static_cast<size_t>(other.subresource.arrayLayer);
179 };
180
181 private:
182 VkImageSubresource subresource;
183 };
184
185 marl::mutex mutex;
186 std::unordered_set<Subresource, Subresource> dirtySubresources GUARDED_BY(mutex);
Alexis Hetuf62f3752018-11-15 14:51:15 -0500187};
188
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000189static inline Image *Cast(VkImage object)
Alexis Hetuf62f3752018-11-15 14:51:15 -0500190{
Alexis Hetubd4cf812019-06-14 15:14:07 -0400191 return Image::Cast(object);
Alexis Hetuf62f3752018-11-15 14:51:15 -0500192}
193
Nicolas Capens157ba262019-12-10 17:49:14 -0500194} // namespace vk
Alexis Hetuf62f3752018-11-15 14:51:15 -0500195
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000196#endif // VK_IMAGE_HPP_