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 
20 namespace sw
21 {
22 	class Blitter;
23 	class Surface;
24 };
25 
26 namespace vk
27 {
28 
29 class DeviceMemory;
30 
31 class Image : public Object<Image, VkImage>
32 {
33 public:
34 	Image(const VkImageCreateInfo* pCreateInfo, void* mem);
35 	~Image() = delete;
36 	void destroy(const VkAllocationCallbacks* pAllocator);
37 
38 	static size_t ComputeRequiredAllocationSize(const VkImageCreateInfo* pCreateInfo);
39 
40 	const VkMemoryRequirements getMemoryRequirements() const;
41 	void getSubresourceLayout(const VkImageSubresource* pSubresource, VkSubresourceLayout* pLayout) const;
42 	void bind(VkDeviceMemory pDeviceMemory, VkDeviceSize pMemoryOffset);
43 	void copyTo(VkImage dstImage, const VkImageCopy& pRegion);
44 	void copyTo(VkBuffer dstBuffer, const VkBufferImageCopy& region);
45 	void copyFrom(VkBuffer srcBuffer, const VkBufferImageCopy& region);
46 
47 	void blit(VkImage dstImage, const VkImageBlit& region, VkFilter filter);
48 	void clear(const VkClearValue& clearValue, const VkRect2D& renderArea, const VkImageSubresourceRange& subresourceRange);
49 	void clear(const VkClearColorValue& color, const VkImageSubresourceRange& subresourceRange);
50 	void clear(const VkClearDepthStencilValue& color, const VkImageSubresourceRange& subresourceRange);
51 
getImageType() const52 	VkImageType              getImageType() const { return imageType; }
getFormat() const53 	VkFormat                 getFormat() const { return format; }
getArrayLayers() const54 	uint32_t                 getArrayLayers() const { return arrayLayers; }
55 	bool                     isCube() const;
56 
57 private:
58 	void copy(VkBuffer buffer, const VkBufferImageCopy& region, bool bufferIsSource);
59 	VkDeviceSize getStorageSize(const VkImageAspectFlags& flags) const;
60 	VkDeviceSize getMipLevelSize(const VkImageAspectFlags& flags, uint32_t mipLevel) const;
61 	VkDeviceSize getLayerSize(const VkImageAspectFlags& flags) const;
62 	VkDeviceSize getMemoryOffset(const VkImageAspectFlags& flags, uint32_t mipLevel) const;
63 	VkDeviceSize getMemoryOffset(const VkImageAspectFlags& flags, uint32_t mipLevel, uint32_t layer) const;
64 	void* getTexelPointer(const VkOffset3D& offset, const VkImageSubresourceLayers& subresource) const;
65 	VkDeviceSize texelOffsetBytesInStorage(const VkOffset3D& offset, const VkImageSubresourceLayers& subresource) const;
66 	VkDeviceSize getMemoryOffset(const VkImageAspectFlags& flags) const;
67 	int rowPitchBytes(const VkImageAspectFlags& flags, uint32_t mipLevel) const;
68 	int slicePitchBytes(const VkImageAspectFlags& flags, uint32_t mipLevel) const;
69 	int bytesPerTexel(const VkImageAspectFlags& flags) const;
70 	VkExtent3D getMipLevelExtent(uint32_t mipLevel) const;
71 	VkFormat getFormat(const VkImageAspectFlags& flags) const;
72 	uint32_t getLastLayerIndex(const VkImageSubresourceRange& subresourceRange) const;
73 	uint32_t getLastMipLevel(const VkImageSubresourceRange& subresourceRange) const;
74 	VkFormat getClearFormat() const;
75 	void clear(void* pixelData, VkFormat format, const VkImageSubresourceRange& subresourceRange, VkImageAspectFlags aspectMask);
76 	void clear(void* pixelData, VkFormat format, const VkRect2D& renderArea, const VkImageSubresourceRange& subresourceRange, VkImageAspectFlags aspectMask);
77 	sw::Surface* asSurface(const VkImageAspectFlags& flags, uint32_t mipLevel, uint32_t layer) const;
78 
79 	DeviceMemory*            deviceMemory = nullptr;
80 	VkDeviceSize             memoryOffset = 0;
81 	VkImageCreateFlags       flags = 0;
82 	VkImageType              imageType = VK_IMAGE_TYPE_2D;
83 	VkFormat                 format = VK_FORMAT_UNDEFINED;
84 	VkExtent3D               extent = {0, 0, 0};
85 	uint32_t                 mipLevels = 0;
86 	uint32_t                 arrayLayers = 0;
87 	VkSampleCountFlagBits    samples = VK_SAMPLE_COUNT_1_BIT;
88 	VkImageTiling            tiling = VK_IMAGE_TILING_OPTIMAL;
89 	sw::Blitter*             blitter = nullptr;
90 };
91 
Cast(VkImage object)92 static inline Image* Cast(VkImage object)
93 {
94 	return reinterpret_cast<Image*>(object);
95 }
96 
97 } // namespace vk
98 
99 #endif // VK_IMAGE_HPP_