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