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 "VkObject.hpp" |
19 | #include "VkFormat.h" |
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 | |
28 | class Buffer; |
29 | class Device; |
30 | class DeviceMemory; |
31 | |
32 | #ifdef __ANDROID__ |
33 | struct BackingMemory { |
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& region); |
58 | void copyFrom(Buffer* srcBuffer, const VkBufferImageCopy& region); |
59 | |
60 | void blit(Image* dstImage, const VkImageBlit& region, 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& region) 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 | |
67 | VkImageType getImageType() const { return imageType; } |
68 | const Format& getFormat() const { return format; } |
69 | Format getFormat(VkImageAspectFlagBits aspect) const; |
70 | uint32_t getArrayLayers() const { return arrayLayers; } |
71 | uint32_t getMipLevels() const { return mipLevels; } |
72 | VkImageUsageFlags getUsage() const { return usage; } |
73 | uint32_t getLastLayerIndex(const VkImageSubresourceRange& subresourceRange) const; |
74 | uint32_t getLastMipLevel(const VkImageSubresourceRange& subresourceRange) const; |
75 | 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 | bool canBindToMemory(DeviceMemory* pDeviceMemory) const; |
85 | |
86 | void prepareForSampling(const VkImageSubresourceRange& subresourceRange); |
87 | const Image* getSampledImage(const vk::Format& imageViewFormat) const; |
88 | |
89 | #ifdef __ANDROID__ |
90 | void setBackingMemory(BackingMemory& bm) { backingMemory = bm; } |
91 | bool hasExternalMemory() const { return backingMemory.externalMemory; } |
92 | VkDeviceMemory getExternalMemory() const; |
93 | #endif |
94 | |
95 | private: |
96 | void copy(Buffer* buffer, const VkBufferImageCopy& region, bool bufferIsSource); |
97 | VkDeviceSize getStorageSize(VkImageAspectFlags flags) const; |
98 | VkDeviceSize getMipLevelSize(VkImageAspectFlagBits aspect, uint32_t mipLevel) const; |
99 | VkDeviceSize getMultiSampledLevelSize(VkImageAspectFlagBits aspect, uint32_t mipLevel) const; |
100 | VkDeviceSize getLayerOffset(VkImageAspectFlagBits aspect, uint32_t mipLevel) const; |
101 | VkDeviceSize getMemoryOffset(VkImageAspectFlagBits aspect, uint32_t mipLevel) const; |
102 | VkDeviceSize getMemoryOffset(VkImageAspectFlagBits aspect, uint32_t mipLevel, uint32_t layer) const; |
103 | VkDeviceSize texelOffsetBytesInStorage(const VkOffset3D& offset, const VkImageSubresourceLayers& subresource) const; |
104 | VkDeviceSize getMemoryOffset(VkImageAspectFlagBits aspect) const; |
105 | VkExtent3D imageExtentInBlocks(const VkExtent3D& extent, VkImageAspectFlagBits aspect) const; |
106 | VkOffset3D imageOffsetInBlocks(const VkOffset3D& offset, VkImageAspectFlagBits aspect) const; |
107 | VkExtent2D bufferExtentInBlocks(const VkExtent2D& extent, const VkBufferImageCopy& region) const; |
108 | VkFormat getClearFormat() const; |
109 | void clear(void* pixelData, VkFormat pixelFormat, const vk::Format& viewFormat, const VkImageSubresourceRange& subresourceRange, const VkRect2D& renderArea); |
110 | int borderSize() const; |
111 | void decodeETC2(const VkImageSubresourceRange& subresourceRange) const; |
112 | |
113 | const Device *const device = nullptr; |
114 | DeviceMemory* deviceMemory = nullptr; |
115 | VkDeviceSize memoryOffset = 0; |
116 | VkImageCreateFlags flags = 0; |
117 | VkImageType imageType = VK_IMAGE_TYPE_2D; |
118 | Format format; |
119 | VkExtent3D extent = {0, 0, 0}; |
120 | uint32_t mipLevels = 0; |
121 | uint32_t arrayLayers = 0; |
122 | VkSampleCountFlagBits samples = VK_SAMPLE_COUNT_1_BIT; |
123 | VkImageTiling tiling = VK_IMAGE_TILING_OPTIMAL; |
124 | VkImageUsageFlags usage = (VkImageUsageFlags)0; |
125 | Image* decompressedImage = nullptr; |
126 | #ifdef __ANDROID__ |
127 | BackingMemory backingMemory = {}; |
128 | #endif |
129 | |
130 | VkExternalMemoryHandleTypeFlags supportedExternalMemoryHandleTypes = (VkExternalMemoryHandleTypeFlags)0; |
131 | }; |
132 | |
133 | static inline Image* Cast(VkImage object) |
134 | { |
135 | return Image::Cast(object); |
136 | } |
137 | |
138 | } // namespace vk |
139 | |
140 | #endif // VK_IMAGE_HPP_ |
141 | |