vktSparseResourcesTestsUtil.hpp revision 0abb8ae884208e3355eb68bde6cedab1dd7b773c
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 "vkPrograms.hpp"
31#include "vkTypeUtil.hpp"
32#include "vkImageUtil.hpp"
33#include "deSharedPtr.hpp"
34
35namespace vkt
36{
37namespace sparse
38{
39
40enum ImageType
41{
42	IMAGE_TYPE_1D = 0,
43	IMAGE_TYPE_1D_ARRAY,
44	IMAGE_TYPE_2D,
45	IMAGE_TYPE_2D_ARRAY,
46	IMAGE_TYPE_3D,
47	IMAGE_TYPE_CUBE,
48	IMAGE_TYPE_CUBE_ARRAY,
49	IMAGE_TYPE_BUFFER,
50
51	IMAGE_TYPE_LAST
52};
53
54class Buffer
55{
56public:
57									Buffer			(const vk::DeviceInterface&		deviceInterface,
58													 const vk::VkDevice				device,
59													 vk::Allocator&					allocator,
60													 const vk::VkBufferCreateInfo&	bufferCreateInfo,
61													 const vk::MemoryRequirement	memoryRequirement);
62
63	const vk::VkBuffer&				get				(void) const { return *m_buffer; }
64	const vk::VkBuffer&				operator*		(void) const { return get(); }
65	vk::Allocation&					getAllocation	(void) const { return *m_allocation; }
66
67private:
68	vk::Unique<vk::VkBuffer>		m_buffer;
69	de::UniquePtr<vk::Allocation>	m_allocation;
70
71									Buffer			(const Buffer&);
72	Buffer&							operator=		(const Buffer&);
73};
74
75class Image
76{
77public:
78									Image			(const vk::DeviceInterface&		deviceInterface,
79													 const vk::VkDevice				device,
80													 vk::Allocator&					allocator,
81													 const vk::VkImageCreateInfo&	imageCreateInfo,
82													 const vk::MemoryRequirement	memoryRequirement);
83
84	const vk::VkImage&				get				(void) const { return *m_image; }
85	const vk::VkImage&				operator*		(void) const { return get(); }
86	vk::Allocation&					getAllocation	(void) const { return *m_allocation; }
87
88private:
89	vk::Unique<vk::VkImage>			m_image;
90	de::UniquePtr<vk::Allocation>	m_allocation;
91
92									Image			(const Image&);
93	Image&							operator=		(const Image&);
94};
95
96class GraphicsPipelineBuilder
97{
98public:
99								GraphicsPipelineBuilder	(void) : m_renderSize			(0, 0)
100															   , m_shaderStageFlags		(0u)
101															   , m_cullModeFlags		(vk::VK_CULL_MODE_NONE)
102															   , m_frontFace			(vk::VK_FRONT_FACE_COUNTER_CLOCKWISE)
103															   , m_patchControlPoints	(1u)
104															   , m_attachmentsCount		(1u)
105															   , m_blendEnable			(false)
106															   , m_primitiveTopology	(vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST) {}
107
108	GraphicsPipelineBuilder&	setRenderSize					(const tcu::IVec2& size) { m_renderSize = size; return *this; }
109	GraphicsPipelineBuilder&	setShader						(const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkShaderStageFlagBits stage, const vk::ProgramBinary& binary, const vk::VkSpecializationInfo* specInfo);
110	GraphicsPipelineBuilder&	setPatchControlPoints			(const deUint32 controlPoints) { m_patchControlPoints = controlPoints; return *this; }
111	GraphicsPipelineBuilder&	setAttachmentsCount				(const deUint32 attachmentsCount) { m_attachmentsCount = attachmentsCount; return *this; }
112	GraphicsPipelineBuilder&	setCullModeFlags				(const vk::VkCullModeFlags cullModeFlags) { m_cullModeFlags = cullModeFlags; return *this; }
113	GraphicsPipelineBuilder&	setFrontFace					(const vk::VkFrontFace frontFace) { m_frontFace = frontFace; return *this; }
114	GraphicsPipelineBuilder&	setBlend						(const bool enable) { m_blendEnable = enable; return *this; }
115
116	//! Applies only to pipelines without tessellation shaders.
117	GraphicsPipelineBuilder&	setPrimitiveTopology			(const vk::VkPrimitiveTopology topology) { m_primitiveTopology = topology; return *this; }
118
119	GraphicsPipelineBuilder&	addVertexBinding				(const vk::VkVertexInputBindingDescription vertexBinding) { m_vertexInputBindings.push_back(vertexBinding); return *this; }
120	GraphicsPipelineBuilder&	addVertexAttribute				(const vk::VkVertexInputAttributeDescription vertexAttribute) { m_vertexInputAttributes.push_back(vertexAttribute); return *this; }
121	GraphicsPipelineBuilder&	addDynamicState					(const vk::VkDynamicState dynamicState) { m_dynamicStates.push_back(dynamicState); return *this; }
122
123	vk::Move<vk::VkPipeline>	build							(const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkPipelineLayout pipelineLayout, const vk::VkRenderPass renderPass);
124
125private:
126	tcu::IVec2											m_renderSize;
127	vk::Move<vk::VkShaderModule>						m_vertexShaderModule;
128	vk::Move<vk::VkShaderModule>						m_fragmentShaderModule;
129	vk::Move<vk::VkShaderModule>						m_geometryShaderModule;
130	vk::Move<vk::VkShaderModule>						m_tessControlShaderModule;
131	vk::Move<vk::VkShaderModule>						m_tessEvaluationShaderModule;
132	std::vector<vk::VkPipelineShaderStageCreateInfo>	m_shaderStages;
133	std::vector<vk::VkVertexInputBindingDescription>	m_vertexInputBindings;
134	std::vector<vk::VkVertexInputAttributeDescription>	m_vertexInputAttributes;
135	std::vector<vk::VkDynamicState>						m_dynamicStates;
136	vk::VkShaderStageFlags								m_shaderStageFlags;
137	vk::VkCullModeFlags									m_cullModeFlags;
138	vk::VkFrontFace										m_frontFace;
139	deUint32											m_patchControlPoints;
140	deUint32											m_attachmentsCount;
141	bool												m_blendEnable;
142	vk::VkPrimitiveTopology								m_primitiveTopology;
143
144	GraphicsPipelineBuilder (const GraphicsPipelineBuilder&);
145	GraphicsPipelineBuilder& operator= (const GraphicsPipelineBuilder&);
146};
147
148enum FeatureFlagBits
149{
150	FEATURE_TESSELLATION_SHADER = 1u << 0,
151	FEATURE_GEOMETRY_SHADER = 1u << 1,
152	FEATURE_SHADER_FLOAT_64 = 1u << 2,
153	FEATURE_VERTEX_PIPELINE_STORES_AND_ATOMICS = 1u << 3,
154	FEATURE_FRAGMENT_STORES_AND_ATOMICS = 1u << 4,
155	FEATURE_SHADER_TESSELLATION_AND_GEOMETRY_POINT_SIZE = 1u << 5,
156};
157typedef deUint32 FeatureFlags;
158
159// Image helper functions
160vk::VkImageType		mapImageType					(const ImageType			imageType);
161vk::VkImageViewType	mapImageViewType				(const ImageType			imageType);
162std::string			getImageTypeName				(const ImageType			imageType);
163std::string			getShaderImageType				(const tcu::TextureFormat&	format,
164													 const ImageType			imageType);
165std::string			getShaderImageDataType			(const tcu::TextureFormat&	format);
166std::string			getShaderImageFormatQualifier	(const tcu::TextureFormat&	format);
167std::string			getShaderImageCoordinates		(const ImageType			imageType,
168													 const std::string&			x,
169													 const std::string&			xy,
170													 const std::string&			xyz);
171//!< Size used for addresing image in a compute shader
172tcu::UVec3			getShaderGridSize				(const ImageType			imageType,
173													 const tcu::UVec3&			imageSize,
174													 const deUint32				mipLevel = 0);
175//!< Size of a single image layer
176tcu::UVec3			getLayerSize					(const ImageType			imageType,
177													 const tcu::UVec3&			imageSize);
178//!< Number of array layers (for array and cube types)
179deUint32			getNumLayers					(const ImageType			imageType,
180													 const tcu::UVec3&			imageSize);
181//!< Number of texels in an image
182deUint32			getNumPixels					(const ImageType			imageType,
183													 const tcu::UVec3&			imageSize);
184//!< Coordinate dimension used for addressing (e.g. 3 (x,y,z) for 2d array)
185deUint32			getDimensions					(const ImageType			imageType);
186//!< Coordinate dimension used for addressing a single layer (e.g. 2 (x,y) for 2d array)
187deUint32			getLayerDimensions				(const ImageType			imageType);
188//!< Helper function for checking if requested image size does not exceed device limits
189bool				isImageSizeSupported			(const vk::InstanceInterface&		instance,
190													 const vk::VkPhysicalDevice			physicalDevice,
191													 const ImageType					imageType,
192													 const tcu::UVec3&					imageSize);
193
194vk::VkExtent3D		mipLevelExtents					(const vk::VkExtent3D&				baseExtents,
195													 const deUint32						mipLevel);
196
197tcu::UVec3			mipLevelExtents					(const tcu::UVec3&					baseExtents,
198													 const deUint32						mipLevel);
199
200deUint32			getImageMaxMipLevels			(const vk::VkImageFormatProperties& imageFormatProperties,
201													 const vk::VkExtent3D&				extent);
202
203deUint32			getImageMipLevelSizeInBytes		(const vk::VkExtent3D&				baseExtents,
204													 const deUint32						layersCount,
205													 const tcu::TextureFormat&			format,
206													 const deUint32						mipmapLevel);
207
208deUint32			getImageSizeInBytes				(const vk::VkExtent3D&				baseExtents,
209													 const deUint32						layersCount,
210													 const tcu::TextureFormat&			format,
211													 const deUint32						mipmapLevelsCount = 1u);
212
213vk::Move<vk::VkCommandPool>		makeCommandPool					(const vk::DeviceInterface&			vk,
214																 const vk::VkDevice					device,
215																 const deUint32						queueFamilyIndex);
216
217vk::Move<vk::VkCommandBuffer>	makeCommandBuffer				(const vk::DeviceInterface&			vk,
218																 const vk::VkDevice					device,
219																 const vk::VkCommandPool			commandPool);
220
221vk::Move<vk::VkPipelineLayout>	makePipelineLayout				(const vk::DeviceInterface&			vk,
222																 const vk::VkDevice					device,
223																 const vk::VkDescriptorSetLayout	descriptorSetLayout);
224
225vk::Move<vk::VkPipeline>		makeComputePipeline				(const vk::DeviceInterface&			vk,
226																 const vk::VkDevice					device,
227																 const vk::VkPipelineLayout			pipelineLayout,
228																 const vk::VkShaderModule			shaderModule,
229																 const vk::VkSpecializationInfo*	specializationInfo = 0);
230
231vk::Move<vk::VkBufferView>		makeBufferView					(const vk::DeviceInterface&			vk,
232																 const vk::VkDevice					device,
233																 const vk::VkBuffer					buffer,
234																 const vk::VkFormat					format,
235																 const vk::VkDeviceSize				offset,
236																 const vk::VkDeviceSize				size);
237
238vk::Move<vk::VkImageView>		makeImageView					(const vk::DeviceInterface&			vk,
239																 const vk::VkDevice					device,
240																 const vk::VkImage					image,
241																 const vk::VkImageViewType			imageViewType,
242																 const vk::VkFormat					format,
243																 const vk::VkImageSubresourceRange	subresourceRange);
244
245vk::Move<vk::VkDescriptorSet>	makeDescriptorSet				(const vk::DeviceInterface&			vk,
246																 const vk::VkDevice					device,
247																 const vk::VkDescriptorPool			descriptorPool,
248																 const vk::VkDescriptorSetLayout	setLayout);
249
250vk::Move<vk::VkSemaphore>		makeSemaphore					(const vk::DeviceInterface&			vk,
251																 const vk::VkDevice					device);
252
253vk::VkBufferCreateInfo			makeBufferCreateInfo			(const vk::VkDeviceSize				bufferSize,
254																 const vk::VkBufferUsageFlags		usage);
255
256vk::VkBufferImageCopy			makeBufferImageCopy				(const vk::VkExtent3D				extent,
257																 const deUint32						layersCount,
258																 const deUint32						mipmapLevel = 0u,
259																 const vk::VkDeviceSize				bufferOffset = 0ull);
260
261vk::VkBufferMemoryBarrier		makeBufferMemoryBarrier			(const vk::VkAccessFlags			srcAccessMask,
262																 const vk::VkAccessFlags			dstAccessMask,
263																 const vk::VkBuffer					buffer,
264																 const vk::VkDeviceSize				offset,
265																 const vk::VkDeviceSize				bufferSizeBytes);
266
267vk::VkImageMemoryBarrier		makeImageMemoryBarrier			(const vk::VkAccessFlags			srcAccessMask,
268																 const vk::VkAccessFlags			dstAccessMask,
269																 const vk::VkImageLayout			oldLayout,
270																 const vk::VkImageLayout			newLayout,
271																 const vk::VkImage					image,
272																 const vk::VkImageSubresourceRange	subresourceRange);
273
274vk::VkImageMemoryBarrier		makeImageMemoryBarrier			(const vk::VkAccessFlags			srcAccessMask,
275																 const vk::VkAccessFlags			dstAccessMask,
276																 const vk::VkImageLayout			oldLayout,
277																 const vk::VkImageLayout			newLayout,
278																 const deUint32						srcQueueFamilyIndex,
279																 const deUint32						destQueueFamilyIndex,
280																 const vk::VkImage					image,
281																 const vk::VkImageSubresourceRange	subresourceRange);
282
283vk::VkMemoryBarrier				makeMemoryBarrier				(const vk::VkAccessFlags			srcAccessMask,
284																 const vk::VkAccessFlags			dstAccessMask);
285
286void							beginCommandBuffer				(const vk::DeviceInterface&			vk,
287																 const vk::VkCommandBuffer			cmdBuffer);
288
289void							endCommandBuffer				(const vk::DeviceInterface&			vk,
290																 const vk::VkCommandBuffer			cmdBuffer);
291
292void							submitCommands					(const vk::DeviceInterface&			vk,
293																 const vk::VkQueue					queue,
294																 const vk::VkCommandBuffer			cmdBuffer,
295																 const deUint32						waitSemaphoreCount		= 0,
296																 const vk::VkSemaphore*				pWaitSemaphores			= DE_NULL,
297																 const vk::VkPipelineStageFlags*	pWaitDstStageMask		= DE_NULL,
298																 const deUint32						signalSemaphoreCount	= 0,
299																 const vk::VkSemaphore*				pSignalSemaphores		= DE_NULL);
300
301void							submitCommandsAndWait			(const vk::DeviceInterface&			vk,
302																 const vk::VkDevice					device,
303																 const vk::VkQueue					queue,
304																 const vk::VkCommandBuffer			cmdBuffer,
305																 const deUint32						waitSemaphoreCount		= 0,
306																 const vk::VkSemaphore*				pWaitSemaphores			= DE_NULL,
307																 const vk::VkPipelineStageFlags*	pWaitDstStageMask		= DE_NULL,
308																 const deUint32						signalSemaphoreCount	= 0,
309																 const vk::VkSemaphore*				pSignalSemaphores		= DE_NULL);
310
311vk::VkSparseImageMemoryBind		makeSparseImageMemoryBind		(const vk::DeviceInterface&			vk,
312																 const vk::VkDevice					device,
313																 const vk::VkDeviceSize				allocationSize,
314																 const deUint32						memoryType,
315																 const vk::VkImageSubresource&		subresource,
316																 const vk::VkOffset3D&				offset,
317																 const vk::VkExtent3D&				extent);
318
319vk::VkSparseMemoryBind			makeSparseMemoryBind			(const vk::DeviceInterface&			vk,
320																 const vk::VkDevice					device,
321																 const vk::VkDeviceSize				allocationSize,
322																 const deUint32						memoryType,
323																 const vk::VkDeviceSize				resourceOffset);
324
325vk::Move<vk::VkRenderPass>		makeRenderPass					(const vk::DeviceInterface&			vk,
326																 const vk::VkDevice					device,
327																 const vk::VkFormat					colorFormat);
328
329vk::Move<vk::VkRenderPass>		makeRenderPassWithoutAttachments(const vk::DeviceInterface&			vk,
330																 const vk::VkDevice					device);
331
332vk::Move<vk::VkFramebuffer>		makeFramebuffer					(const vk::DeviceInterface&			vk,
333																 const vk::VkDevice					device,
334																 const vk::VkRenderPass				renderPass,
335																 const vk::VkImageView				colorAttachment,
336																 const deUint32						width,
337																 const deUint32						height,
338																 const deUint32						layers);
339
340vk::Move<vk::VkFramebuffer>	makeFramebufferWithoutAttachments	(const vk::DeviceInterface&			vk,
341																 const vk::VkDevice					device,
342																 const vk::VkRenderPass				renderPass);
343
344void							beginRenderPass					(const vk::DeviceInterface&				vk,
345																 const vk::VkCommandBuffer				commandBuffer,
346																 const vk::VkRenderPass					renderPass,
347																 const vk::VkFramebuffer				framebuffer,
348																 const vk::VkRect2D&					renderArea,
349																 const std::vector<vk::VkClearValue>&	clearValues);
350
351void					beginRenderPassWithRasterizationDisabled(const vk::DeviceInterface&			vk,
352																 const vk::VkCommandBuffer			commandBuffer,
353																 const vk::VkRenderPass				renderPass,
354																 const vk::VkFramebuffer			framebuffer);
355
356void							endRenderPass					(const vk::DeviceInterface&			vk,
357																 const vk::VkCommandBuffer			commandBuffer);
358
359void							requireFeatures					(const vk::InstanceInterface&		vki,
360																 const vk::VkPhysicalDevice			physicalDevice,
361																 const FeatureFlags					flags);
362
363template<typename T>
364inline de::SharedPtr<vk::Unique<T> > makeVkSharedPtr			(vk::Move<T> vkMove)
365{
366	return de::SharedPtr<vk::Unique<T> >(new vk::Unique<T>(vkMove));
367}
368
369template<typename T>
370inline std::size_t sizeInBytes(const std::vector<T>& vec)
371{
372	return vec.size() * sizeof(vec[0]);
373}
374
375} // sparse
376} // vkt
377
378#endif // _VKTSPARSERESOURCESTESTSUTIL_HPP
379