Home | History | Annotate | Download | only in sparse_resources
      1 #ifndef _VKTSPARSERESOURCESTESTSUTIL_HPP
      2 #define _VKTSPARSERESOURCESTESTSUTIL_HPP
      3 /*------------------------------------------------------------------------
      4  * Vulkan Conformance Tests
      5  * ------------------------
      6  *
      7  * Copyright (c) 2016 The Khronos Group Inc.
      8  *
      9  * Licensed under the Apache License, Version 2.0 (the "License");
     10  * you may not use this file except in compliance with the License.
     11  * You may obtain a copy of the License at
     12  *
     13  *      http://www.apache.org/licenses/LICENSE-2.0
     14  *
     15  * Unless required by applicable law or agreed to in writing, software
     16  * distributed under the License is distributed on an "AS IS" BASIS,
     17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     18  * See the License for the specific language governing permissions and
     19  * limitations under the License.
     20  *
     21  *//*!
     22  * \file  vktSparseResourcesTestsUtil.hpp
     23  * \brief Sparse Resources Tests Utility Classes
     24  *//*--------------------------------------------------------------------*/
     25 
     26 #include "vkDefs.hpp"
     27 #include "vkMemUtil.hpp"
     28 #include "vkRef.hpp"
     29 #include "vkRefUtil.hpp"
     30 #include "vkMemUtil.hpp"
     31 #include "vkImageUtil.hpp"
     32 #include "deSharedPtr.hpp"
     33 #include "deUniquePtr.hpp"
     34 
     35 namespace vkt
     36 {
     37 namespace sparse
     38 {
     39 
     40 typedef de::SharedPtr<vk::Unique<vk::VkDeviceMemory> > DeviceMemorySp;
     41 
     42 enum ImageType
     43 {
     44 	IMAGE_TYPE_1D = 0,
     45 	IMAGE_TYPE_1D_ARRAY,
     46 	IMAGE_TYPE_2D,
     47 	IMAGE_TYPE_2D_ARRAY,
     48 	IMAGE_TYPE_3D,
     49 	IMAGE_TYPE_CUBE,
     50 	IMAGE_TYPE_CUBE_ARRAY,
     51 	IMAGE_TYPE_BUFFER,
     52 
     53 	IMAGE_TYPE_LAST
     54 };
     55 
     56 enum FeatureFlagBits
     57 {
     58 	FEATURE_TESSELLATION_SHADER							= 1u << 0,
     59 	FEATURE_GEOMETRY_SHADER								= 1u << 1,
     60 	FEATURE_SHADER_FLOAT_64								= 1u << 2,
     61 	FEATURE_VERTEX_PIPELINE_STORES_AND_ATOMICS			= 1u << 3,
     62 	FEATURE_FRAGMENT_STORES_AND_ATOMICS					= 1u << 4,
     63 	FEATURE_SHADER_TESSELLATION_AND_GEOMETRY_POINT_SIZE	= 1u << 5,
     64 };
     65 typedef deUint32 FeatureFlags;
     66 
     67 enum
     68 {
     69 	BUFFER_IMAGE_COPY_OFFSET_GRANULARITY	= 4u,
     70 	NO_MATCH_FOUND							= ~((deUint32)0),	//!< no matching index
     71 };
     72 
     73 vk::VkImageType					mapImageType						(const ImageType					imageType);
     74 
     75 vk::VkImageViewType				mapImageViewType					(const ImageType					imageType);
     76 
     77 std::string						getImageTypeName					(const ImageType					imageType);
     78 
     79 std::string						getShaderImageType					(const tcu::TextureFormat&			format,
     80 																	 const ImageType					imageType);
     81 
     82 std::string						getShaderImageDataType				(const tcu::TextureFormat&			format);
     83 
     84 std::string						getShaderImageFormatQualifier		(const tcu::TextureFormat&			format);
     85 
     86 std::string						getShaderImageCoordinates			(const ImageType					imageType,
     87 																	 const std::string&					x,
     88 																	 const std::string&					xy,
     89 																	 const std::string&					xyz);
     90 
     91 //!< Size used for addresing image in a compute shader
     92 tcu::UVec3						getShaderGridSize					(const ImageType					imageType,
     93 																	 const tcu::UVec3&					imageSize,
     94 																	 const deUint32						mipLevel	= 0);
     95 
     96 //!< Size of a single image layer
     97 tcu::UVec3						getLayerSize						(const ImageType					imageType,
     98 																	 const tcu::UVec3&					imageSize);
     99 
    100 //!< Number of array layers (for array and cube types)
    101 deUint32						getNumLayers						(const ImageType					imageType,
    102 																	 const tcu::UVec3&					imageSize);
    103 
    104 //!< Number of texels in an image
    105 deUint32						getNumPixels						(const ImageType					imageType,
    106 																	 const tcu::UVec3&					imageSize);
    107 
    108 //!< Coordinate dimension used for addressing (e.g. 3 (x,y,z) for 2d array)
    109 deUint32						getDimensions						(const ImageType					imageType);
    110 
    111 //!< Coordinate dimension used for addressing a single layer (e.g. 2 (x,y) for 2d array)
    112 deUint32						getLayerDimensions					(const ImageType					imageType);
    113 
    114 //!< Helper function for checking if requested image size does not exceed device limits
    115 bool							isImageSizeSupported				(const vk::InstanceInterface&		instance,
    116 																	 const vk::VkPhysicalDevice			physicalDevice,
    117 																	 const ImageType					imageType,
    118 																	 const tcu::UVec3&					imageSize);
    119 
    120 vk::VkExtent3D					mipLevelExtents						(const vk::VkExtent3D&				baseExtents,
    121 																	 const deUint32						mipLevel);
    122 
    123 tcu::UVec3						mipLevelExtents						(const tcu::UVec3&					baseExtents,
    124 																	 const deUint32						mipLevel);
    125 
    126 deUint32						getImageMaxMipLevels				(const vk::VkImageFormatProperties& imageFormatProperties,
    127 																	 const vk::VkExtent3D&				extent);
    128 
    129 deUint32						getImageMipLevelSizeInBytes			(const vk::VkExtent3D&				baseExtents,
    130 																	 const deUint32						layersCount,
    131 																	 const tcu::TextureFormat&			format,
    132 																	 const deUint32						mipmapLevel,
    133 																	 const deUint32						mipmapMemoryAlignment	= 1u);
    134 
    135 deUint32						getImageSizeInBytes					(const vk::VkExtent3D&				baseExtents,
    136 																	 const deUint32						layersCount,
    137 																	 const tcu::TextureFormat&			format,
    138 																	 const deUint32						mipmapLevelsCount		= 1u,
    139 																	 const deUint32						mipmapMemoryAlignment	= 1u);
    140 
    141 vk::Move<vk::VkCommandPool>		makeCommandPool						(const vk::DeviceInterface&			vk,
    142 																	 const vk::VkDevice					device,
    143 																	 const deUint32						queueFamilyIndex);
    144 
    145 vk::Move<vk::VkPipelineLayout>	makePipelineLayout					(const vk::DeviceInterface&			vk,
    146 																	 const vk::VkDevice					device,
    147 																	 const vk::VkDescriptorSetLayout	descriptorSetLayout = DE_NULL);
    148 
    149 vk::Move<vk::VkPipeline>		makeComputePipeline					(const vk::DeviceInterface&			vk,
    150 																	 const vk::VkDevice					device,
    151 																	 const vk::VkPipelineLayout			pipelineLayout,
    152 																	 const vk::VkShaderModule			shaderModule,
    153 																	 const vk::VkSpecializationInfo*	specializationInfo	= 0);
    154 
    155 vk::Move<vk::VkBufferView>		makeBufferView						(const vk::DeviceInterface&			vk,
    156 																	 const vk::VkDevice					device,
    157 																	 const vk::VkBuffer					buffer,
    158 																	 const vk::VkFormat					format,
    159 																	 const vk::VkDeviceSize				offset,
    160 																	 const vk::VkDeviceSize				size);
    161 
    162 vk::Move<vk::VkImageView>		makeImageView						(const vk::DeviceInterface&			vk,
    163 																	 const vk::VkDevice					device,
    164 																	 const vk::VkImage					image,
    165 																	 const vk::VkImageViewType			imageViewType,
    166 																	 const vk::VkFormat					format,
    167 																	 const vk::VkImageSubresourceRange	subresourceRange);
    168 
    169 vk::Move<vk::VkDescriptorSet>	makeDescriptorSet					(const vk::DeviceInterface&			vk,
    170 																	 const vk::VkDevice					device,
    171 																	 const vk::VkDescriptorPool			descriptorPool,
    172 																	 const vk::VkDescriptorSetLayout	setLayout);
    173 
    174 vk::Move<vk::VkFramebuffer>		makeFramebuffer						(const vk::DeviceInterface&			vk,
    175 																	 const vk::VkDevice					device,
    176 																	 const vk::VkRenderPass				renderPass,
    177 																	 const deUint32						attachmentCount,
    178 																	 const vk::VkImageView*				pAttachments,
    179 																	 const deUint32						width,
    180 																	 const deUint32						height,
    181 																	 const deUint32						layers = 1u);
    182 
    183 de::MovePtr<vk::Allocation>		bindImage							(const vk::DeviceInterface&			vk,
    184 																	 const vk::VkDevice					device,
    185 																	 vk::Allocator&						allocator,
    186 																	 const vk::VkImage					image,
    187 																	 const vk::MemoryRequirement		requirement);
    188 
    189 de::MovePtr<vk::Allocation>		bindBuffer							(const vk::DeviceInterface&			vk,
    190 																	 const vk::VkDevice					device,
    191 																	 vk::Allocator&						allocator,
    192 																	 const vk::VkBuffer					buffer,
    193 																	 const vk::MemoryRequirement		requirement);
    194 
    195 vk::VkBufferCreateInfo			makeBufferCreateInfo				(const vk::VkDeviceSize				bufferSize,
    196 																	 const vk::VkBufferUsageFlags		usage);
    197 
    198 vk::VkBufferImageCopy			makeBufferImageCopy					(const vk::VkExtent3D				extent,
    199 																	 const deUint32						layersCount,
    200 																	 const deUint32						mipmapLevel		= 0u,
    201 																	 const vk::VkDeviceSize				bufferOffset	= 0ull);
    202 
    203 vk::VkBufferMemoryBarrier		makeBufferMemoryBarrier				(const vk::VkAccessFlags			srcAccessMask,
    204 																	 const vk::VkAccessFlags			dstAccessMask,
    205 																	 const vk::VkBuffer					buffer,
    206 																	 const vk::VkDeviceSize				offset,
    207 																	 const vk::VkDeviceSize				bufferSizeBytes);
    208 
    209 vk::VkImageMemoryBarrier		makeImageMemoryBarrier				(const vk::VkAccessFlags			srcAccessMask,
    210 																	 const vk::VkAccessFlags			dstAccessMask,
    211 																	 const vk::VkImageLayout			oldLayout,
    212 																	 const vk::VkImageLayout			newLayout,
    213 																	 const vk::VkImage					image,
    214 																	 const vk::VkImageSubresourceRange	subresourceRange);
    215 
    216 vk::VkImageMemoryBarrier		makeImageMemoryBarrier				(const vk::VkAccessFlags			srcAccessMask,
    217 																	 const vk::VkAccessFlags			dstAccessMask,
    218 																	 const vk::VkImageLayout			oldLayout,
    219 																	 const vk::VkImageLayout			newLayout,
    220 																	 const deUint32						srcQueueFamilyIndex,
    221 																	 const deUint32						destQueueFamilyIndex,
    222 																	 const vk::VkImage					image,
    223 																	 const vk::VkImageSubresourceRange	subresourceRange);
    224 
    225 vk::VkMemoryBarrier				makeMemoryBarrier					(const vk::VkAccessFlags			srcAccessMask,
    226 																	 const vk::VkAccessFlags			dstAccessMask);
    227 
    228 vk::VkSparseImageMemoryBind		makeSparseImageMemoryBind			(const vk::DeviceInterface&			vk,
    229 																	 const vk::VkDevice					device,
    230 																	 const vk::VkDeviceSize				allocationSize,
    231 																	 const deUint32						memoryType,
    232 																	 const vk::VkImageSubresource&		subresource,
    233 																	 const vk::VkOffset3D&				offset,
    234 																	 const vk::VkExtent3D&				extent);
    235 
    236 vk::VkSparseMemoryBind			makeSparseMemoryBind				(const vk::DeviceInterface&			vk,
    237 																	 const vk::VkDevice					device,
    238 																	 const vk::VkDeviceSize				allocationSize,
    239 																	 const deUint32						memoryType,
    240 																	 const vk::VkDeviceSize				resourceOffset,
    241 																	 const vk::VkSparseMemoryBindFlags	flags			= 0u);
    242 
    243 void							beginCommandBuffer					(const vk::DeviceInterface&			vk,
    244 																	 const vk::VkCommandBuffer			cmdBuffer);
    245 
    246 void							endCommandBuffer					(const vk::DeviceInterface&			vk,
    247 																	 const vk::VkCommandBuffer			cmdBuffer);
    248 
    249 void							submitCommands						(const vk::DeviceInterface&			vk,
    250 																	 const vk::VkQueue					queue,
    251 																	 const vk::VkCommandBuffer			cmdBuffer,
    252 																	 const deUint32						waitSemaphoreCount		= 0,
    253 																	 const vk::VkSemaphore*				pWaitSemaphores			= DE_NULL,
    254 																	 const vk::VkPipelineStageFlags*	pWaitDstStageMask		= DE_NULL,
    255 																	 const deUint32						signalSemaphoreCount	= 0,
    256 																	 const vk::VkSemaphore*				pSignalSemaphores		= DE_NULL);
    257 
    258 void							submitCommandsAndWait				(const vk::DeviceInterface&			vk,
    259 																	 const vk::VkDevice					device,
    260 																	 const vk::VkQueue					queue,
    261 																	 const vk::VkCommandBuffer			cmdBuffer,
    262 																	 const deUint32						waitSemaphoreCount		= 0,
    263 																	 const vk::VkSemaphore*				pWaitSemaphores			= DE_NULL,
    264 																	 const vk::VkPipelineStageFlags*	pWaitDstStageMask		= DE_NULL,
    265 																	 const deUint32						signalSemaphoreCount	= 0,
    266 																	 const vk::VkSemaphore*				pSignalSemaphores		= DE_NULL);
    267 
    268 void							requireFeatures						(const vk::InstanceInterface&		vki,
    269 																	 const vk::VkPhysicalDevice			physicalDevice,
    270 																	 const FeatureFlags					flags);
    271 
    272 deUint32						findMatchingMemoryType				(const vk::InstanceInterface&		instance,
    273 																	 const vk::VkPhysicalDevice			physicalDevice,
    274 																	 const vk::VkMemoryRequirements&	objectMemoryRequirements,
    275 																	 const vk::MemoryRequirement&		memoryRequirement);
    276 
    277 bool							checkSparseSupportForImageType		(const vk::InstanceInterface&		instance,
    278 																	 const vk::VkPhysicalDevice			physicalDevice,
    279 																	 const ImageType					imageType);
    280 
    281 bool							checkSparseSupportForImageFormat	(const vk::InstanceInterface&		instance,
    282 																	 const vk::VkPhysicalDevice			physicalDevice,
    283 																	 const vk::VkImageCreateInfo&		imageInfo);
    284 
    285 bool							checkImageFormatFeatureSupport		(const vk::InstanceInterface&		instance,
    286 																	 const vk::VkPhysicalDevice			physicalDevice,
    287 																	 const vk::VkFormat					format,
    288 																	 const vk::VkFormatFeatureFlags		featureFlags);
    289 
    290 deUint32						getSparseAspectRequirementsIndex	(const std::vector<vk::VkSparseImageMemoryRequirements>&	requirements,
    291 																	 const vk::VkImageAspectFlags								aspectFlags);
    292 
    293 inline vk::Move<vk::VkBuffer> makeBuffer (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkBufferCreateInfo& createInfo)
    294 {
    295 	return createBuffer(vk, device, &createInfo);
    296 }
    297 
    298 inline vk::Move<vk::VkImage> makeImage (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkImageCreateInfo& createInfo)
    299 {
    300 	return createImage(vk, device, &createInfo);
    301 }
    302 
    303 template<typename T>
    304 inline de::SharedPtr<vk::Unique<T> > makeVkSharedPtr (vk::Move<T> vkMove)
    305 {
    306 	return de::SharedPtr<vk::Unique<T> >(new vk::Unique<T>(vkMove));
    307 }
    308 
    309 template<typename T>
    310 inline de::SharedPtr<de::UniquePtr<T> > makeDeSharedPtr (de::MovePtr<T> deMove)
    311 {
    312 	return de::SharedPtr<de::UniquePtr<T> >(new de::UniquePtr<T>(deMove));
    313 }
    314 
    315 template<typename T>
    316 inline std::size_t sizeInBytes (const std::vector<T>& vec)
    317 {
    318 	return vec.size() * sizeof(vec[0]);
    319 }
    320 
    321 template<typename T>
    322 inline const T* getDataOrNullptr (const std::vector<T>& vec, const std::size_t index = 0u)
    323 {
    324 	return (index < vec.size() ? &vec[index] : DE_NULL);
    325 }
    326 
    327 } // sparse
    328 } // vkt
    329 
    330 #endif // _VKTSPARSERESOURCESTESTSUTIL_HPP
    331