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
35namespace vkt
36{
37namespace sparse
38{
39
40typedef de::SharedPtr<vk::Unique<vk::VkDeviceMemory> > DeviceMemorySp;
41
42enum 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
56enum 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};
65typedef deUint32 FeatureFlags;
66
67enum
68{
69	BUFFER_IMAGE_COPY_OFFSET_GRANULARITY	= 4u,
70	NO_MATCH_FOUND							= ~((deUint32)0),	//!< no matching index
71};
72
73vk::VkImageType					mapImageType						(const ImageType					imageType);
74
75vk::VkImageViewType				mapImageViewType					(const ImageType					imageType);
76
77std::string						getImageTypeName					(const ImageType					imageType);
78
79std::string						getShaderImageType					(const tcu::TextureFormat&			format,
80																	 const ImageType					imageType);
81
82std::string						getShaderImageDataType				(const tcu::TextureFormat&			format);
83
84std::string						getShaderImageFormatQualifier		(const tcu::TextureFormat&			format);
85
86std::string						getShaderImageCoordinates			(const ImageType					imageType,
87																	 const std::string&					x,
88																	 const std::string&					xy,
89																	 const std::string&					xyz);
90
91//!< Create instance with specific extensions
92vk::Move<vk::VkInstance>		createInstanceWithExtensions		(const vk::PlatformInterface&		vkp,
93																	 const deUint32						version,
94																	 const std::vector<std::string>		enableExtensions);
95
96//!< Size used for addresing image in a compute shader
97tcu::UVec3						getShaderGridSize					(const ImageType					imageType,
98																	 const tcu::UVec3&					imageSize,
99																	 const deUint32						mipLevel	= 0);
100
101//!< Size of a single image layer
102tcu::UVec3						getLayerSize						(const ImageType					imageType,
103																	 const tcu::UVec3&					imageSize);
104
105//!< Number of array layers (for array and cube types)
106deUint32						getNumLayers						(const ImageType					imageType,
107																	 const tcu::UVec3&					imageSize);
108
109//!< Number of texels in an image
110deUint32						getNumPixels						(const ImageType					imageType,
111																	 const tcu::UVec3&					imageSize);
112
113//!< Coordinate dimension used for addressing (e.g. 3 (x,y,z) for 2d array)
114deUint32						getDimensions						(const ImageType					imageType);
115
116//!< Coordinate dimension used for addressing a single layer (e.g. 2 (x,y) for 2d array)
117deUint32						getLayerDimensions					(const ImageType					imageType);
118
119//!< Helper function for checking if requested image size does not exceed device limits
120bool							isImageSizeSupported				(const vk::InstanceInterface&		instance,
121																	 const vk::VkPhysicalDevice			physicalDevice,
122																	 const ImageType					imageType,
123																	 const tcu::UVec3&					imageSize);
124
125vk::VkExtent3D					mipLevelExtents						(const vk::VkExtent3D&				baseExtents,
126																	 const deUint32						mipLevel);
127
128tcu::UVec3						mipLevelExtents						(const tcu::UVec3&					baseExtents,
129																	 const deUint32						mipLevel);
130
131deUint32						getImageMaxMipLevels				(const vk::VkImageFormatProperties& imageFormatProperties,
132																	 const vk::VkExtent3D&				extent);
133
134deUint32						getImageMipLevelSizeInBytes			(const vk::VkExtent3D&				baseExtents,
135																	 const deUint32						layersCount,
136																	 const tcu::TextureFormat&			format,
137																	 const deUint32						mipmapLevel,
138																	 const deUint32						mipmapMemoryAlignment	= 1u);
139
140deUint32						getImageSizeInBytes					(const vk::VkExtent3D&				baseExtents,
141																	 const deUint32						layersCount,
142																	 const tcu::TextureFormat&			format,
143																	 const deUint32						mipmapLevelsCount		= 1u,
144																	 const deUint32						mipmapMemoryAlignment	= 1u);
145
146vk::Move<vk::VkCommandPool>		makeCommandPool						(const vk::DeviceInterface&			vk,
147																	 const vk::VkDevice					device,
148																	 const deUint32						queueFamilyIndex);
149
150vk::Move<vk::VkPipelineLayout>	makePipelineLayout					(const vk::DeviceInterface&			vk,
151																	 const vk::VkDevice					device,
152																	 const vk::VkDescriptorSetLayout	descriptorSetLayout = DE_NULL);
153
154vk::Move<vk::VkPipeline>		makeComputePipeline					(const vk::DeviceInterface&			vk,
155																	 const vk::VkDevice					device,
156																	 const vk::VkPipelineLayout			pipelineLayout,
157																	 const vk::VkShaderModule			shaderModule,
158																	 const vk::VkSpecializationInfo*	specializationInfo	= 0);
159
160vk::Move<vk::VkBufferView>		makeBufferView						(const vk::DeviceInterface&			vk,
161																	 const vk::VkDevice					device,
162																	 const vk::VkBuffer					buffer,
163																	 const vk::VkFormat					format,
164																	 const vk::VkDeviceSize				offset,
165																	 const vk::VkDeviceSize				size);
166
167vk::Move<vk::VkImageView>		makeImageView						(const vk::DeviceInterface&			vk,
168																	 const vk::VkDevice					device,
169																	 const vk::VkImage					image,
170																	 const vk::VkImageViewType			imageViewType,
171																	 const vk::VkFormat					format,
172																	 const vk::VkImageSubresourceRange	subresourceRange);
173
174vk::Move<vk::VkDescriptorSet>	makeDescriptorSet					(const vk::DeviceInterface&			vk,
175																	 const vk::VkDevice					device,
176																	 const vk::VkDescriptorPool			descriptorPool,
177																	 const vk::VkDescriptorSetLayout	setLayout);
178
179vk::Move<vk::VkFramebuffer>		makeFramebuffer						(const vk::DeviceInterface&			vk,
180																	 const vk::VkDevice					device,
181																	 const vk::VkRenderPass				renderPass,
182																	 const deUint32						attachmentCount,
183																	 const vk::VkImageView*				pAttachments,
184																	 const deUint32						width,
185																	 const deUint32						height,
186																	 const deUint32						layers = 1u);
187
188de::MovePtr<vk::Allocation>		bindImage							(const vk::DeviceInterface&			vk,
189																	 const vk::VkDevice					device,
190																	 vk::Allocator&						allocator,
191																	 const vk::VkImage					image,
192																	 const vk::MemoryRequirement		requirement);
193
194de::MovePtr<vk::Allocation>		bindBuffer							(const vk::DeviceInterface&			vk,
195																	 const vk::VkDevice					device,
196																	 vk::Allocator&						allocator,
197																	 const vk::VkBuffer					buffer,
198																	 const vk::MemoryRequirement		requirement);
199
200vk::VkBufferCreateInfo			makeBufferCreateInfo				(const vk::VkDeviceSize				bufferSize,
201																	 const vk::VkBufferUsageFlags		usage);
202
203vk::VkBufferImageCopy			makeBufferImageCopy					(const vk::VkExtent3D				extent,
204																	 const deUint32						layersCount,
205																	 const deUint32						mipmapLevel		= 0u,
206																	 const vk::VkDeviceSize				bufferOffset	= 0ull);
207
208vk::VkBufferMemoryBarrier		makeBufferMemoryBarrier				(const vk::VkAccessFlags			srcAccessMask,
209																	 const vk::VkAccessFlags			dstAccessMask,
210																	 const vk::VkBuffer					buffer,
211																	 const vk::VkDeviceSize				offset,
212																	 const vk::VkDeviceSize				bufferSizeBytes);
213
214vk::VkImageMemoryBarrier		makeImageMemoryBarrier				(const vk::VkAccessFlags			srcAccessMask,
215																	 const vk::VkAccessFlags			dstAccessMask,
216																	 const vk::VkImageLayout			oldLayout,
217																	 const vk::VkImageLayout			newLayout,
218																	 const vk::VkImage					image,
219																	 const vk::VkImageSubresourceRange	subresourceRange);
220
221vk::VkImageMemoryBarrier		makeImageMemoryBarrier				(const vk::VkAccessFlags			srcAccessMask,
222																	 const vk::VkAccessFlags			dstAccessMask,
223																	 const vk::VkImageLayout			oldLayout,
224																	 const vk::VkImageLayout			newLayout,
225																	 const deUint32						srcQueueFamilyIndex,
226																	 const deUint32						destQueueFamilyIndex,
227																	 const vk::VkImage					image,
228																	 const vk::VkImageSubresourceRange	subresourceRange);
229
230vk::VkMemoryBarrier				makeMemoryBarrier					(const vk::VkAccessFlags			srcAccessMask,
231																	 const vk::VkAccessFlags			dstAccessMask);
232
233vk::VkSparseImageMemoryBind		makeSparseImageMemoryBind			(const vk::DeviceInterface&			vk,
234																	 const vk::VkDevice					device,
235																	 const vk::VkDeviceSize				allocationSize,
236																	 const deUint32						memoryType,
237																	 const vk::VkImageSubresource&		subresource,
238																	 const vk::VkOffset3D&				offset,
239																	 const vk::VkExtent3D&				extent);
240
241vk::VkSparseMemoryBind			makeSparseMemoryBind				(const vk::DeviceInterface&			vk,
242																	 const vk::VkDevice					device,
243																	 const vk::VkDeviceSize				allocationSize,
244																	 const deUint32						memoryType,
245																	 const vk::VkDeviceSize				resourceOffset,
246																	 const vk::VkSparseMemoryBindFlags	flags			= 0u);
247
248void							beginCommandBuffer					(const vk::DeviceInterface&			vk,
249																	 const vk::VkCommandBuffer			cmdBuffer);
250
251void							endCommandBuffer					(const vk::DeviceInterface&			vk,
252																	 const vk::VkCommandBuffer			cmdBuffer);
253
254void							submitCommands						(const vk::DeviceInterface&			vk,
255																	 const vk::VkQueue					queue,
256																	 const vk::VkCommandBuffer			cmdBuffer,
257																	 const deUint32						waitSemaphoreCount		= 0,
258																	 const vk::VkSemaphore*				pWaitSemaphores			= DE_NULL,
259																	 const vk::VkPipelineStageFlags*	pWaitDstStageMask		= DE_NULL,
260																	 const deUint32						signalSemaphoreCount	= 0,
261																	 const vk::VkSemaphore*				pSignalSemaphores		= DE_NULL);
262
263void							submitCommandsAndWait				(const vk::DeviceInterface&			vk,
264																	 const vk::VkDevice					device,
265																	 const vk::VkQueue					queue,
266																	 const vk::VkCommandBuffer			cmdBuffer,
267																	 const deUint32						waitSemaphoreCount		= 0,
268																	 const vk::VkSemaphore*				pWaitSemaphores			= DE_NULL,
269																	 const vk::VkPipelineStageFlags*	pWaitDstStageMask		= DE_NULL,
270																	 const deUint32						signalSemaphoreCount	= 0,
271																	 const vk::VkSemaphore*				pSignalSemaphores		= DE_NULL,
272																	 const bool							useDeviceGroups			= false,
273																	 const deUint32						physicalDeviceID		= 0);
274
275void							requireFeatures						(const vk::InstanceInterface&		vki,
276																	 const vk::VkPhysicalDevice			physicalDevice,
277																	 const FeatureFlags					flags);
278
279deUint32						findMatchingMemoryType				(const vk::InstanceInterface&		instance,
280																	 const vk::VkPhysicalDevice			physicalDevice,
281																	 const vk::VkMemoryRequirements&	objectMemoryRequirements,
282																	 const vk::MemoryRequirement&		memoryRequirement);
283
284bool							checkSparseSupportForImageType		(const vk::InstanceInterface&		instance,
285																	 const vk::VkPhysicalDevice			physicalDevice,
286																	 const ImageType					imageType);
287
288bool							checkSparseSupportForImageFormat	(const vk::InstanceInterface&		instance,
289																	 const vk::VkPhysicalDevice			physicalDevice,
290																	 const vk::VkImageCreateInfo&		imageInfo);
291
292bool							checkImageFormatFeatureSupport		(const vk::InstanceInterface&		instance,
293																	 const vk::VkPhysicalDevice			physicalDevice,
294																	 const vk::VkFormat					format,
295																	 const vk::VkFormatFeatureFlags		featureFlags);
296
297deUint32						getSparseAspectRequirementsIndex	(const std::vector<vk::VkSparseImageMemoryRequirements>&	requirements,
298																	 const vk::VkImageAspectFlags								aspectFlags);
299
300inline vk::Move<vk::VkBuffer> makeBuffer (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkBufferCreateInfo& createInfo)
301{
302	return createBuffer(vk, device, &createInfo);
303}
304
305inline vk::Move<vk::VkImage> makeImage (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkImageCreateInfo& createInfo)
306{
307	return createImage(vk, device, &createInfo);
308}
309
310template<typename T>
311inline de::SharedPtr<vk::Unique<T> > makeVkSharedPtr (vk::Move<T> vkMove)
312{
313	return de::SharedPtr<vk::Unique<T> >(new vk::Unique<T>(vkMove));
314}
315
316template<typename T>
317inline de::SharedPtr<de::UniquePtr<T> > makeDeSharedPtr (de::MovePtr<T> deMove)
318{
319	return de::SharedPtr<de::UniquePtr<T> >(new de::UniquePtr<T>(deMove));
320}
321
322template<typename T>
323inline std::size_t sizeInBytes (const std::vector<T>& vec)
324{
325	return vec.size() * sizeof(vec[0]);
326}
327
328template<typename T>
329inline const T* getDataOrNullptr (const std::vector<T>& vec, const std::size_t index = 0u)
330{
331	return (index < vec.size() ? &vec[index] : DE_NULL);
332}
333
334} // sparse
335} // vkt
336
337#endif // _VKTSPARSERESOURCESTESTSUTIL_HPP
338