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