Home | History | Annotate | Download | only in vulkan
      1 #ifndef _VKIMAGEUTIL_HPP
      2 #define _VKIMAGEUTIL_HPP
      3 /*-------------------------------------------------------------------------
      4  * Vulkan CTS Framework
      5  * --------------------
      6  *
      7  * Copyright (c) 2015 The Khronos Group Inc.
      8  * Copyright (c) 2015 Imagination Technologies Ltd.
      9  * Copyright (c) 2015 Google Inc.
     10  *
     11  * Licensed under the Apache License, Version 2.0 (the "License");
     12  * you may not use this file except in compliance with the License.
     13  * You may obtain a copy of the License at
     14  *
     15  *      http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing, software
     18  * distributed under the License is distributed on an "AS IS" BASIS,
     19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     20  * See the License for the specific language governing permissions and
     21  * limitations under the License.
     22  *
     23  *//*!
     24  * \file
     25  * \brief Utilities for images.
     26  *//*--------------------------------------------------------------------*/
     27 
     28 #include "vkDefs.hpp"
     29 #include "vkMemUtil.hpp"
     30 #include "tcuTexture.hpp"
     31 #include "tcuCompressedTexture.hpp"
     32 #include "deSharedPtr.hpp"
     33 
     34 namespace vk
     35 {
     36 
     37 bool						isFloatFormat				(VkFormat format);
     38 bool						isUnormFormat				(VkFormat format);
     39 bool						isSnormFormat				(VkFormat format);
     40 bool						isIntFormat					(VkFormat format);
     41 bool						isUintFormat				(VkFormat format);
     42 bool						isDepthStencilFormat		(VkFormat format);
     43 bool						isCompressedFormat			(VkFormat format);
     44 bool						isSrgbFormat				(VkFormat format);
     45 
     46 bool						isSupportedByFramework		(VkFormat format);
     47 
     48 tcu::TextureFormat			mapVkFormat					(VkFormat format);
     49 tcu::CompressedTexFormat	mapVkCompressedFormat		(VkFormat format);
     50 tcu::TextureFormat			getDepthCopyFormat			(VkFormat combinedFormat);
     51 tcu::TextureFormat			getStencilCopyFormat		(VkFormat combinedFormat);
     52 
     53 tcu::Sampler				mapVkSampler				(const VkSamplerCreateInfo& samplerCreateInfo);
     54 tcu::Sampler::CompareMode	mapVkSamplerCompareOp		(VkCompareOp compareOp);
     55 tcu::Sampler::WrapMode		mapVkSamplerAddressMode		(VkSamplerAddressMode addressMode);
     56 tcu::Sampler::ReductionMode mapVkSamplerReductionMode	(VkSamplerReductionModeEXT reductionMode);
     57 tcu::Sampler::FilterMode	mapVkMinTexFilter			(VkFilter filter, VkSamplerMipmapMode mipMode);
     58 tcu::Sampler::FilterMode	mapVkMagTexFilter			(VkFilter filter);
     59 
     60 VkFilter					mapFilterMode				(tcu::Sampler::FilterMode filterMode);
     61 VkSamplerMipmapMode			mapMipmapMode				(tcu::Sampler::FilterMode filterMode);
     62 VkSamplerAddressMode		mapWrapMode					(tcu::Sampler::WrapMode wrapMode);
     63 VkCompareOp					mapCompareMode				(tcu::Sampler::CompareMode mode);
     64 VkFormat					mapTextureFormat			(const tcu::TextureFormat& format);
     65 VkFormat					mapCompressedTextureFormat	(const tcu::CompressedTexFormat format);
     66 VkSamplerCreateInfo			mapSampler					(const tcu::Sampler& sampler, const tcu::TextureFormat& format, float minLod = 0.0f, float maxLod = 1000.0f, bool unnormal = false);
     67 
     68 void						imageUtilSelfTest			(void);
     69 
     70 float						getRepresentableDiffUnorm	(const VkFormat format, const deUint32 componentNdx);
     71 float						getRepresentableDiffSnorm	(const VkFormat format, const deUint32 componentNdx);
     72 deUint32					getFormatComponentWidth		(const VkFormat format, const deUint32 componentNdx);
     73 deUint32					getBlockSizeInBytes			(const VkFormat compressedFormat);
     74 deUint32					getBlockWidth				(const VkFormat compressedFormat);
     75 deUint32					getBlockHeight				(const VkFormat compressedFormat);
     76 
     77 // \todo [2017-05-18 pyry] Consider moving this to tcu
     78 struct PlanarFormatDescription
     79 {
     80 	enum
     81 	{
     82 		MAX_CHANNELS	= 4,
     83 		MAX_PLANES		= 3
     84 	};
     85 
     86 	enum ChannelFlags
     87 	{
     88 		CHANNEL_R	= (1u<<0),	// Has "R" (0) channel
     89 		CHANNEL_G	= (1u<<1),	// Has "G" (1) channel
     90 		CHANNEL_B	= (1u<<2),	// Has "B" (2) channel
     91 		CHANNEL_A	= (1u<<3),	// Has "A" (3) channel
     92 	};
     93 
     94 	struct Plane
     95 	{
     96 		deUint8		elementSizeBytes;
     97 		deUint8		widthDivisor;
     98 		deUint8		heightDivisor;
     99 	};
    100 
    101 	struct Channel
    102 	{
    103 		deUint8		planeNdx;
    104 		deUint8		type;				// tcu::TextureChannelClass value
    105 		deUint8		offsetBits;			// Offset in element in bits
    106 		deUint8		sizeBits;			// Value size in bits
    107 		deUint8		strideBytes;		// Pixel stride (in bytes), usually plane elementSize
    108 	};
    109 
    110 	deUint8		numPlanes;
    111 	deUint8		presentChannels;
    112 	Plane		planes[MAX_PLANES];
    113 	Channel		channels[MAX_CHANNELS];
    114 
    115 	inline bool hasChannelNdx (deUint32 ndx) const
    116 	{
    117 		DE_ASSERT(de::inBounds(ndx, 0u, 4u));
    118 		return (presentChannels & (1u<<ndx)) != 0;
    119 	}
    120 };
    121 
    122 bool							isYCbCrFormat					(VkFormat format);
    123 PlanarFormatDescription			getPlanarFormatDescription		(VkFormat format);
    124 const PlanarFormatDescription&	getYCbCrPlanarFormatDescription	(VkFormat format);
    125 int								getPlaneCount					(VkFormat format);
    126 VkImageAspectFlagBits			getPlaneAspect					(deUint32 planeNdx);
    127 deUint32						getAspectPlaneNdx				(VkImageAspectFlagBits planeAspect);
    128 bool							isChromaSubsampled				(VkFormat format);
    129 bool							isYCbCr422Format				(VkFormat format);
    130 bool							isYCbCr420Format				(VkFormat format);
    131 
    132 tcu::PixelBufferAccess			getChannelAccess				(const PlanarFormatDescription&	formatInfo,
    133 																 const tcu::UVec2&				size,
    134 																 const deUint32*				planeRowPitches,
    135 																 void* const*					planePtrs,
    136 																 deUint32						channelNdx);
    137 tcu::ConstPixelBufferAccess		getChannelAccess				(const PlanarFormatDescription&	formatInfo,
    138 																 const tcu::UVec2&				size,
    139 																 const deUint32*				planeRowPitches,
    140 																 const void* const*				planePtrs,
    141 																 deUint32						channelNdx);
    142 VkImageAspectFlags				getImageAspectFlags				(const tcu::TextureFormat		textureFormat);
    143 VkExtent3D						mipLevelExtents					(const VkExtent3D&				baseExtents,
    144 																 const deUint32					mipLevel);
    145 tcu::UVec3						alignedDivide					(const VkExtent3D&				extent,
    146 																 const VkExtent3D&				divisor);
    147 
    148 /*--------------------------------------------------------------------*//*!
    149  * Copies buffer data into an image. The buffer is expected to be
    150  * in a state after host write.
    151 *//*--------------------------------------------------------------------*/
    152 void							copyBufferToImage				(const DeviceInterface&						vk,
    153 																 vk::VkDevice								device,
    154 																 vk::VkQueue								queue,
    155 																 deUint32									queueFamilyIndex,
    156 																 const vk::VkBuffer&						buffer,
    157 																 vk::VkDeviceSize							bufferSize,
    158 																 const std::vector<vk::VkBufferImageCopy>&	copyRegions,
    159 																 const vk::VkSemaphore*						waitSemaphore,
    160 																 vk::VkImageAspectFlags						imageAspectFlags,
    161 																 deUint32									mipLevels,
    162 																 deUint32									arrayLayers,
    163 																 vk::VkImage								destImage,
    164 																 VkImageLayout								destImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
    165 																 VkPipelineStageFlags						destImageDstStageFlags = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
    166 
    167 void							copyBufferToImage				(const DeviceInterface&					vk,
    168 																 const VkCommandBuffer&					cmdBuffer,
    169 																 const VkBuffer&						buffer,
    170 																 vk::VkDeviceSize						bufferSize,
    171 																 const std::vector<VkBufferImageCopy>&	copyRegions,
    172 																 VkImageAspectFlags						imageAspectFlags,
    173 																 deUint32								mipLevels,
    174 																 deUint32								arrayLayers,
    175 																 VkImage								destImage,
    176 																 VkImageLayout							destImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
    177 																 VkPipelineStageFlags					destImageDstStageFlags = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
    178 
    179 /*--------------------------------------------------------------------*//*!
    180  * Copies image data into a buffer. The buffer is expected to be
    181  * read by the host.
    182 *//*--------------------------------------------------------------------*/
    183 void							copyImageToBuffer				(const DeviceInterface&		vk,
    184 																 vk::VkCommandBuffer		cmdBuffer,
    185 																 vk::VkImage				image,
    186 																 vk::VkBuffer				buffer,
    187 																 tcu::IVec2					size,
    188 																 vk::VkAccessFlags			srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
    189 																 vk::VkImageLayout			oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
    190 																 deUint32					numLayers = 1u);
    191 
    192 /*--------------------------------------------------------------------*//*!
    193  * Checks if the physical device supports creation of the specified
    194  * image format.
    195  *//*--------------------------------------------------------------------*/
    196 bool							checkSparseImageFormatSupport	(const vk::VkPhysicalDevice		physicalDevice,
    197 																 const vk::InstanceInterface&	instance,
    198 																 const vk::VkImageCreateInfo&	imageCreateInfo);
    199 
    200 /*--------------------------------------------------------------------*//*!
    201  * Allocates memory for a sparse image and handles the memory binding.
    202  *//*--------------------------------------------------------------------*/
    203 void							allocateAndBindSparseImage		(const vk::DeviceInterface&						vk,
    204 																 vk::VkDevice									device,
    205 																 const vk::VkPhysicalDevice						physicalDevice,
    206 																 const vk::InstanceInterface&					instance,
    207 																 const vk::VkImageCreateInfo&					imageCreateInfo,
    208 																 const vk::VkSemaphore&							signalSemaphore,
    209 																 vk::VkQueue									queue,
    210 																 vk::Allocator&									allocator,
    211 																 std::vector<de::SharedPtr<vk::Allocation> >&	allocations,
    212 																 tcu::TextureFormat								format,
    213 																 vk::VkImage									destImage);
    214 
    215 } // vk
    216 
    217 #endif // _VKIMAGEUTIL_HPP
    218