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 "vkObjUtil.hpp"
28#include "vkMemUtil.hpp"
29#include "vkRef.hpp"
30#include "vkRefUtil.hpp"
31#include "vkMemUtil.hpp"
32#include "vkImageUtil.hpp"
33#include "deSharedPtr.hpp"
34#include "deUniquePtr.hpp"
35
36namespace vkt
37{
38namespace sparse
39{
40
41typedef de::SharedPtr<vk::Unique<vk::VkDeviceMemory> > DeviceMemorySp;
42
43enum ImageType
44{
45	IMAGE_TYPE_1D = 0,
46	IMAGE_TYPE_1D_ARRAY,
47	IMAGE_TYPE_2D,
48	IMAGE_TYPE_2D_ARRAY,
49	IMAGE_TYPE_3D,
50	IMAGE_TYPE_CUBE,
51	IMAGE_TYPE_CUBE_ARRAY,
52	IMAGE_TYPE_BUFFER,
53
54	IMAGE_TYPE_LAST
55};
56
57enum FeatureFlagBits
58{
59	FEATURE_TESSELLATION_SHADER							= 1u << 0,
60	FEATURE_GEOMETRY_SHADER								= 1u << 1,
61	FEATURE_SHADER_FLOAT_64								= 1u << 2,
62	FEATURE_VERTEX_PIPELINE_STORES_AND_ATOMICS			= 1u << 3,
63	FEATURE_FRAGMENT_STORES_AND_ATOMICS					= 1u << 4,
64	FEATURE_SHADER_TESSELLATION_AND_GEOMETRY_POINT_SIZE	= 1u << 5,
65};
66typedef deUint32 FeatureFlags;
67
68enum
69{
70	NO_MATCH_FOUND							= ~((deUint32)0),	//!< no matching index
71};
72
73struct TestFormat
74{
75	vk::VkFormat	format;
76};
77
78struct TestImageParameters
79{
80	ImageType				imageType;
81	std::vector<tcu::UVec3>	imageSizes;
82	std::vector<TestFormat>	formats;
83
84	TestImageParameters (ImageType imageType_, const std::vector<tcu::UVec3>& imageSizes_, const std::vector<TestFormat>& formats_)
85		: imageType		(imageType_)
86		, imageSizes	(imageSizes_)
87		, formats		(formats_)
88		{}
89};
90
91bool							formatIsR64							(const vk::VkFormat& format);
92
93std::vector<TestFormat>			getTestFormats						(const ImageType& imageType);
94
95vk::VkImageType					mapImageType						(const ImageType					imageType);
96
97vk::VkImageViewType				mapImageViewType					(const ImageType					imageType);
98
99std::string						getImageTypeName					(const ImageType					imageType);
100
101std::string						getShaderImageType					(const tcu::TextureFormat&			format,
102																	 const ImageType					imageType);
103
104std::string						getShaderImageType					(const vk::PlanarFormatDescription& description,
105																	 const ImageType imageType);
106
107std::string						getShaderImageDataType				(const tcu::TextureFormat&			format);
108
109std::string						getShaderImageDataType				(const vk::PlanarFormatDescription& description);
110
111std::string						getShaderImageFormatQualifier		(const tcu::TextureFormat&			format);
112
113std::string						getShaderImageFormatQualifier		(vk::VkFormat						format);
114
115std::string						getImageFormatID					(vk::VkFormat						format);
116
117std::string						getShaderImageCoordinates			(const ImageType					imageType,
118																	 const std::string&					x,
119																	 const std::string&					xy,
120																	 const std::string&					xyz);
121
122//!< Size used for addresing image in a compute shader
123tcu::UVec3						getShaderGridSize					(const ImageType					imageType,
124																	 const tcu::UVec3&					imageSize,
125																	 const deUint32						mipLevel	= 0);
126
127//!< Size of a single image layer
128tcu::UVec3						getLayerSize						(const ImageType					imageType,
129																	 const tcu::UVec3&					imageSize);
130
131//!< Number of array layers (for array and cube types)
132deUint32						getNumLayers						(const ImageType					imageType,
133																	 const tcu::UVec3&					imageSize);
134
135//!< Number of texels in an image
136deUint32						getNumPixels						(const ImageType					imageType,
137																	 const tcu::UVec3&					imageSize);
138
139//!< Coordinate dimension used for addressing (e.g. 3 (x,y,z) for 2d array)
140deUint32						getDimensions						(const ImageType					imageType);
141
142//!< Coordinate dimension used for addressing a single layer (e.g. 2 (x,y) for 2d array)
143deUint32						getLayerDimensions					(const ImageType					imageType);
144
145//!< Helper function for checking if requested image size does not exceed device limits
146bool							isImageSizeSupported				(const vk::InstanceInterface&		instance,
147																	 const vk::VkPhysicalDevice			physicalDevice,
148																	 const ImageType					imageType,
149																	 const tcu::UVec3&					imageSize);
150
151deUint32						getImageMipLevelSizeInBytes			(const vk::VkExtent3D&				baseExtents,
152																	 const deUint32						layersCount,
153																	 const tcu::TextureFormat&			format,
154																	 const deUint32						mipmapLevel,
155																	 const deUint32						mipmapMemoryAlignment	= 1u);
156
157deUint32						getImageSizeInBytes					(const vk::VkExtent3D&				baseExtents,
158																	 const deUint32						layersCount,
159																	 const tcu::TextureFormat&			format,
160																	 const deUint32						mipmapLevelsCount		= 1u,
161																	 const deUint32						mipmapMemoryAlignment	= 1u);
162
163deUint32						getImageMipLevelSizeInBytes			(const vk::VkExtent3D&				baseExtents,
164																	 const deUint32						layersCount,
165																	 const vk::PlanarFormatDescription&	formatDescription,
166																	 const deUint32						planeNdx,
167																	 const deUint32						mipmapLevel,
168																	 const deUint32						mipmapMemoryAlignment	= 1u);
169
170deUint32						getImageSizeInBytes					(const vk::VkExtent3D&				baseExtents,
171																	 const deUint32						layersCount,
172																	 const vk::PlanarFormatDescription&	formatDescription,
173																	 const deUint32						planeNdx,
174																	 const deUint32						mipmapLevelsCount		=1u,
175																	 const deUint32						mipmapMemoryAlignment	=1u);
176
177vk::VkBufferImageCopy			makeBufferImageCopy					(const vk::VkExtent3D				extent,
178																	 const deUint32						layersCount,
179																	 const deUint32						mipmapLevel		= 0u,
180																	 const vk::VkDeviceSize				bufferOffset	= 0ull);
181
182vk::VkSparseImageMemoryBind		makeSparseImageMemoryBind			(const vk::DeviceInterface&			vk,
183																	 const vk::VkDevice					device,
184																	 const vk::VkDeviceSize				allocationSize,
185																	 const deUint32						memoryType,
186																	 const vk::VkImageSubresource&		subresource,
187																	 const vk::VkOffset3D&				offset,
188																	 const vk::VkExtent3D&				extent);
189
190vk::VkSparseMemoryBind			makeSparseMemoryBind				(const vk::DeviceInterface&			vk,
191																	 const vk::VkDevice					device,
192																	 const vk::VkDeviceSize				allocationSize,
193																	 const deUint32						memoryType,
194																	 const vk::VkDeviceSize				resourceOffset,
195																	 const vk::VkSparseMemoryBindFlags	flags			= 0u);
196
197void							submitCommands						(const vk::DeviceInterface&			vk,
198																	 const vk::VkQueue					queue,
199																	 const vk::VkCommandBuffer			cmdBuffer,
200																	 const deUint32						waitSemaphoreCount		= 0,
201																	 const vk::VkSemaphore*				pWaitSemaphores			= DE_NULL,
202																	 const vk::VkPipelineStageFlags*	pWaitDstStageMask		= DE_NULL,
203																	 const deUint32						signalSemaphoreCount	= 0,
204																	 const vk::VkSemaphore*				pSignalSemaphores		= DE_NULL);
205
206void							submitCommandsAndWait				(const vk::DeviceInterface&			vk,
207																	 const vk::VkDevice					device,
208																	 const vk::VkQueue					queue,
209																	 const vk::VkCommandBuffer			cmdBuffer,
210																	 const deUint32						waitSemaphoreCount		= 0,
211																	 const vk::VkSemaphore*				pWaitSemaphores			= DE_NULL,
212																	 const vk::VkPipelineStageFlags*	pWaitDstStageMask		= DE_NULL,
213																	 const deUint32						signalSemaphoreCount	= 0,
214																	 const vk::VkSemaphore*				pSignalSemaphores		= DE_NULL,
215																	 const bool							useDeviceGroups			= false,
216																	 const deUint32						physicalDeviceID		= 0);
217
218void							requireFeatures						(const vk::InstanceInterface&		vki,
219																	 const vk::VkPhysicalDevice			physicalDevice,
220																	 const FeatureFlags					flags);
221
222deUint32						findMatchingMemoryType				(const vk::InstanceInterface&		instance,
223																	 const vk::VkPhysicalDevice			physicalDevice,
224																	 const vk::VkMemoryRequirements&	objectMemoryRequirements,
225																	 const vk::MemoryRequirement&		memoryRequirement);
226
227deUint32						getHeapIndexForMemoryType			(const vk::InstanceInterface&		instance,
228																	 const vk::VkPhysicalDevice			physicalDevice,
229																	 const deUint32						memoryType);
230
231bool							checkSparseSupportForImageType		(const vk::InstanceInterface&		instance,
232																	 const vk::VkPhysicalDevice			physicalDevice,
233																	 const ImageType					imageType);
234
235bool							checkSparseSupportForImageFormat	(const vk::InstanceInterface&		instance,
236																	 const vk::VkPhysicalDevice			physicalDevice,
237																	 const vk::VkImageCreateInfo&		imageInfo);
238
239bool							checkImageFormatFeatureSupport		(const vk::InstanceInterface&		instance,
240																	 const vk::VkPhysicalDevice			physicalDevice,
241																	 const vk::VkFormat					format,
242																	 const vk::VkFormatFeatureFlags		featureFlags);
243
244deUint32						getSparseAspectRequirementsIndex	(const std::vector<vk::VkSparseImageMemoryRequirements>&	requirements,
245																	 const vk::VkImageAspectFlags								aspectFlags);
246
247vk::VkFormat					getPlaneCompatibleFormatForWriting	(const vk::PlanarFormatDescription&	formatInfo,
248																	 deUint32							planeNdx);
249
250bool							areLsb6BitsDontCare(vk::VkFormat format);
251
252bool							areLsb4BitsDontCare(vk::VkFormat format);
253
254template<typename T>
255inline de::SharedPtr<vk::Unique<T> > makeVkSharedPtr (vk::Move<T> vkMove)
256{
257	return de::SharedPtr<vk::Unique<T> >(new vk::Unique<T>(vkMove));
258}
259
260template<typename T>
261inline de::SharedPtr<de::UniquePtr<T> > makeDeSharedPtr (de::MovePtr<T> deMove)
262{
263	return de::SharedPtr<de::UniquePtr<T> >(new de::UniquePtr<T>(deMove));
264}
265
266template<typename T>
267inline std::size_t sizeInBytes (const std::vector<T>& vec)
268{
269	return vec.size() * sizeof(vec[0]);
270}
271
272template<typename T>
273inline const T* getDataOrNullptr (const std::vector<T>& vec, const std::size_t index = 0u)
274{
275	return (index < vec.size() ? &vec[index] : DE_NULL);
276}
277
278} // sparse
279} // vkt
280
281#endif // _VKTSPARSERESOURCESTESTSUTIL_HPP
282