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_HPP_
16 #define VK_IMAGE_HPP_
17
18 #include "VkFormat.h"
19 #include "VkObject.hpp"
20
21 #ifdef __ANDROID__
22 # include <vulkan/vk_android_native_buffer.h> // For VkSwapchainImageUsageFlagsANDROID and buffer_handle_t
23 #endif
24
25 namespace vk {
26
27 class Buffer;
28 class Device;
29 class DeviceMemory;
30
31 #ifdef __ANDROID__
32 struct BackingMemory
33 {
34 int stride = 0;
35 bool externalMemory = false;
36 buffer_handle_t nativeHandle = nullptr;
37 VkSwapchainImageUsageFlagsANDROID androidUsage = 0;
38 };
39 #endif
40
41 class Image : public Object<Image, VkImage>
42 {
43 public:
44 Image(const VkImageCreateInfo *pCreateInfo, void *mem, Device *device);
45 void destroy(const VkAllocationCallbacks *pAllocator);
46
47 #ifdef __ANDROID__
48 VkResult prepareForExternalUseANDROID() const;
49 #endif
50
51 static size_t ComputeRequiredAllocationSize(const VkImageCreateInfo *pCreateInfo);
52
53 const VkMemoryRequirements getMemoryRequirements() const;
54 void getSubresourceLayout(const VkImageSubresource *pSubresource, VkSubresourceLayout *pLayout) const;
55 void bind(DeviceMemory *pDeviceMemory, VkDeviceSize pMemoryOffset);
56 void copyTo(Image *dstImage, const VkImageCopy &pRegion) const;
57 void copyTo(Buffer *dstBuffer, const VkBufferImageCopy ®ion);
58 void copyFrom(Buffer *srcBuffer, const VkBufferImageCopy ®ion);
59
60 void blit(Image *dstImage, const VkImageBlit ®ion, VkFilter filter) const;
61 void blitToBuffer(VkImageSubresourceLayers subresource, VkOffset3D offset, VkExtent3D extent, uint8_t *dst, int bufferRowPitch, int bufferSlicePitch) const;
62 void resolve(Image *dstImage, const VkImageResolve ®ion) const;
63 void clear(const VkClearValue &clearValue, const vk::Format &viewFormat, const VkRect2D &renderArea, const VkImageSubresourceRange &subresourceRange);
64 void clear(const VkClearColorValue &color, const VkImageSubresourceRange &subresourceRange);
65 void clear(const VkClearDepthStencilValue &color, const VkImageSubresourceRange &subresourceRange);
66
getImageType() const67 VkImageType getImageType() const { return imageType; }
getFormat() const68 const Format &getFormat() const { return format; }
69 Format getFormat(VkImageAspectFlagBits aspect) const;
getArrayLayers() const70 uint32_t getArrayLayers() const { return arrayLayers; }
getMipLevels() const71 uint32_t getMipLevels() const { return mipLevels; }
getUsage() const72 VkImageUsageFlags getUsage() const { return usage; }
73 uint32_t getLastLayerIndex(const VkImageSubresourceRange &subresourceRange) const;
74 uint32_t getLastMipLevel(const VkImageSubresourceRange &subresourceRange) const;
getSampleCountFlagBits() const75 VkSampleCountFlagBits getSampleCountFlagBits() const { return samples; }
76 VkExtent3D getMipLevelExtent(VkImageAspectFlagBits aspect, uint32_t mipLevel) const;
77 int rowPitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel) const;
78 int slicePitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel) const;
79 void *getTexelPointer(const VkOffset3D &offset, const VkImageSubresourceLayers &subresource) const;
80 bool isCube() const;
81 bool is3DSlice() const;
82 uint8_t *end() const;
83 VkDeviceSize getLayerSize(VkImageAspectFlagBits aspect) const;
84 VkDeviceSize getMipLevelSize(VkImageAspectFlagBits aspect, uint32_t mipLevel) const;
85 bool canBindToMemory(DeviceMemory *pDeviceMemory) const;
86
87 void prepareForSampling(const VkImageSubresourceRange &subresourceRange);
88 const Image *getSampledImage(const vk::Format &imageViewFormat) const;
89
90 #ifdef __ANDROID__
setBackingMemory(BackingMemory & bm)91 void setBackingMemory(BackingMemory &bm)
92 {
93 backingMemory = bm;
94 }
hasExternalMemory() const95 bool hasExternalMemory() const { return backingMemory.externalMemory; }
96 VkDeviceMemory getExternalMemory() const;
97 #endif
98
99 private:
100 void copy(Buffer *buffer, const VkBufferImageCopy ®ion, bool bufferIsSource);
101 VkDeviceSize getStorageSize(VkImageAspectFlags flags) const;
102 VkDeviceSize getMultiSampledLevelSize(VkImageAspectFlagBits aspect, uint32_t mipLevel) const;
103 VkDeviceSize getLayerOffset(VkImageAspectFlagBits aspect, uint32_t mipLevel) const;
104 VkDeviceSize getMemoryOffset(VkImageAspectFlagBits aspect, uint32_t mipLevel) const;
105 VkDeviceSize getMemoryOffset(VkImageAspectFlagBits aspect, uint32_t mipLevel, uint32_t layer) const;
106 VkDeviceSize texelOffsetBytesInStorage(const VkOffset3D &offset, const VkImageSubresourceLayers &subresource) const;
107 VkDeviceSize getMemoryOffset(VkImageAspectFlagBits aspect) const;
108 VkExtent3D imageExtentInBlocks(const VkExtent3D &extent, VkImageAspectFlagBits aspect) const;
109 VkOffset3D imageOffsetInBlocks(const VkOffset3D &offset, VkImageAspectFlagBits aspect) const;
110 VkExtent2D bufferExtentInBlocks(const VkExtent2D &extent, const VkBufferImageCopy ®ion) const;
111 VkFormat getClearFormat() const;
112 void clear(void *pixelData, VkFormat pixelFormat, const vk::Format &viewFormat, const VkImageSubresourceRange &subresourceRange, const VkRect2D &renderArea);
113 int borderSize() const;
114 void decodeETC2(const VkImageSubresourceRange &subresourceRange) const;
115 void decodeBC(const VkImageSubresourceRange &subresourceRange) const;
116 void decodeASTC(const VkImageSubresourceRange &subresourceRange) const;
117
118 const Device *const device = nullptr;
119 DeviceMemory *deviceMemory = nullptr;
120 VkDeviceSize memoryOffset = 0;
121 VkImageCreateFlags flags = 0;
122 VkImageType imageType = VK_IMAGE_TYPE_2D;
123 Format format;
124 VkExtent3D extent = { 0, 0, 0 };
125 uint32_t mipLevels = 0;
126 uint32_t arrayLayers = 0;
127 VkSampleCountFlagBits samples = VK_SAMPLE_COUNT_1_BIT;
128 VkImageTiling tiling = VK_IMAGE_TILING_OPTIMAL;
129 VkImageUsageFlags usage = (VkImageUsageFlags)0;
130 Image *decompressedImage = nullptr;
131 #ifdef __ANDROID__
132 BackingMemory backingMemory = {};
133 #endif
134
135 VkExternalMemoryHandleTypeFlags supportedExternalMemoryHandleTypes = (VkExternalMemoryHandleTypeFlags)0;
136 };
137
Cast(VkImage object)138 static inline Image *Cast(VkImage object)
139 {
140 return Image::Cast(object);
141 }
142
143 } // namespace vk
144
145 #endif // VK_IMAGE_HPP_
146