1 /*-------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2015 The Khronos Group Inc.
6  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7  * Copyright (c) 2015 Google 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 
23 #include "vkDefs.hpp"
24 #include "vktTestCaseUtil.hpp"
25 #include "vkBuilderUtil.hpp"
26 #include "vkPlatform.hpp"
27 #include "vkRefUtil.hpp"
28 #include "vkQueryUtil.hpp"
29 #include "vkMemUtil.hpp"
30 #include "vkDeviceUtil.hpp"
31 #include "tcuTextureUtil.hpp"
32 #include "vkImageUtil.hpp"
33 #include "vkPrograms.hpp"
34 #include "vkTypeUtil.hpp"
35 #include "vkAllocationCallbackUtil.hpp"
36 #include "vktApiCommandBuffersTests.hpp"
37 #include "vktApiBufferComputeInstance.hpp"
38 #include "vktApiComputeInstanceResultBuffer.hpp"
39 #include "deSharedPtr.hpp"
40 #include <sstream>
41 
42 namespace vkt
43 {
44 namespace api
45 {
46 namespace
47 {
48 
49 using namespace vk;
50 
51 typedef de::SharedPtr<vk::Unique<vk::VkEvent> >	VkEventSp;
52 
53 // Global variables
54 const deUint64								INFINITE_TIMEOUT		= ~(deUint64)0u;
55 
56 
57 template <deUint32 NumBuffers>
58 class CommandBufferBareTestEnvironment
59 {
60 public:
61 											CommandBufferBareTestEnvironment	(Context&						context,
62 																				 VkCommandPoolCreateFlags		commandPoolCreateFlags);
63 
getCommandPool(void) const64 	VkCommandPool							getCommandPool						(void) const					{ return *m_commandPool; }
65 	VkCommandBuffer							getCommandBuffer					(deUint32 bufferIndex) const;
66 
67 protected:
68 	Context&								m_context;
69 	const VkDevice							m_device;
70 	const DeviceInterface&					m_vkd;
71 	const VkQueue							m_queue;
72 	const deUint32							m_queueFamilyIndex;
73 	Allocator&								m_allocator;
74 
75 	Move<VkCommandPool>						m_commandPool;
76 	VkCommandBuffer							m_primaryCommandBuffers[NumBuffers];
77 };
78 
79 template <deUint32 NumBuffers>
CommandBufferBareTestEnvironment(Context & context,VkCommandPoolCreateFlags commandPoolCreateFlags)80 CommandBufferBareTestEnvironment<NumBuffers>::CommandBufferBareTestEnvironment(Context& context, VkCommandPoolCreateFlags commandPoolCreateFlags)
81 	: m_context								(context)
82 	, m_device								(context.getDevice())
83 	, m_vkd									(context.getDeviceInterface())
84 	, m_queue								(context.getUniversalQueue())
85 	, m_queueFamilyIndex					(context.getUniversalQueueFamilyIndex())
86 	, m_allocator							(context.getDefaultAllocator())
87 {
88 	const VkCommandPoolCreateInfo			cmdPoolCreateInfo		=
89 	{
90 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// VkStructureType             sType;
91 		DE_NULL,													// const void*                 pNext;
92 		commandPoolCreateFlags,										// VkCommandPoolCreateFlags    flags;
93 		m_queueFamilyIndex											// deUint32                    queueFamilyIndex;
94 	};
95 
96 	m_commandPool = createCommandPool(m_vkd, m_device, &cmdPoolCreateInfo, DE_NULL);
97 
98 	const VkCommandBufferAllocateInfo		cmdBufferAllocateInfo	=
99 	{
100 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// VkStructureType             sType;
101 		DE_NULL,													// const void*                 pNext;
102 		*m_commandPool,												// VkCommandPool               commandPool;
103 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							// VkCommandBufferLevel        level;
104 		NumBuffers												// deUint32                    commandBufferCount;
105 	};
106 
107 	VK_CHECK(m_vkd.allocateCommandBuffers(m_device, &cmdBufferAllocateInfo, m_primaryCommandBuffers));
108 }
109 
110 template <deUint32 NumBuffers>
getCommandBuffer(deUint32 bufferIndex) const111 VkCommandBuffer CommandBufferBareTestEnvironment<NumBuffers>::getCommandBuffer(deUint32 bufferIndex) const
112 {
113 	DE_ASSERT(bufferIndex < NumBuffers);
114 	return m_primaryCommandBuffers[bufferIndex];
115 }
116 
117 class CommandBufferRenderPassTestEnvironment : public CommandBufferBareTestEnvironment<1>
118 {
119 public:
120 											CommandBufferRenderPassTestEnvironment	(Context&						context,
121 																					 VkCommandPoolCreateFlags		commandPoolCreateFlags);
122 
getRenderPass(void) const123 	VkRenderPass							getRenderPass							(void) const { return *m_renderPass; }
getFrameBuffer(void) const124 	VkFramebuffer							getFrameBuffer							(void) const { return *m_frameBuffer; }
getPrimaryCommandBuffer(void) const125 	VkCommandBuffer							getPrimaryCommandBuffer					(void) const { return getCommandBuffer(0); }
getSecondaryCommandBuffer(void) const126 	VkCommandBuffer							getSecondaryCommandBuffer				(void) const { return *m_secondaryCommandBuffer; }
127 
128 	void									beginPrimaryCommandBuffer				(VkCommandBufferUsageFlags usageFlags);
129 	void									beginSecondaryCommandBuffer				(VkCommandBufferUsageFlags usageFlags);
130 	void									beginRenderPass							(VkSubpassContents content);
131 	void									submitPrimaryCommandBuffer				(void);
132 	de::MovePtr<tcu::TextureLevel>			readColorAttachment						(void);
133 
134 	static const VkImageType				DEFAULT_IMAGE_TYPE;
135 	static const VkFormat					DEFAULT_IMAGE_FORMAT;
136 	static const VkExtent3D					DEFAULT_IMAGE_SIZE;
137 	static const VkRect2D					DEFAULT_IMAGE_AREA;
138 
139 protected:
140 
141 	Move<VkImage>							m_colorImage;
142 	Move<VkImageView>						m_colorImageView;
143 	Move<VkRenderPass>						m_renderPass;
144 	Move<VkFramebuffer>						m_frameBuffer;
145 	de::MovePtr<Allocation>					m_colorImageMemory;
146 	Move<VkCommandBuffer>					m_secondaryCommandBuffer;
147 
148 };
149 
150 const VkImageType		CommandBufferRenderPassTestEnvironment::DEFAULT_IMAGE_TYPE		= VK_IMAGE_TYPE_2D;
151 const VkFormat			CommandBufferRenderPassTestEnvironment::DEFAULT_IMAGE_FORMAT	= VK_FORMAT_R8G8B8A8_UINT;
152 const VkExtent3D		CommandBufferRenderPassTestEnvironment::DEFAULT_IMAGE_SIZE		= {255, 255, 1};
153 const VkRect2D			CommandBufferRenderPassTestEnvironment::DEFAULT_IMAGE_AREA		=
154 {
155 	{ 0u, 0u, },												//	VkOffset2D	offset;
156 	{ DEFAULT_IMAGE_SIZE.width,	DEFAULT_IMAGE_SIZE.height },	//	VkExtent2D	extent;
157 };
158 
CommandBufferRenderPassTestEnvironment(Context & context,VkCommandPoolCreateFlags commandPoolCreateFlags)159 CommandBufferRenderPassTestEnvironment::CommandBufferRenderPassTestEnvironment(Context& context, VkCommandPoolCreateFlags commandPoolCreateFlags)
160 	: CommandBufferBareTestEnvironment<1>		(context, commandPoolCreateFlags)
161 {
162 	{
163 		const VkAttachmentDescription			colorAttDesc			=
164 		{
165 			0u,													// VkAttachmentDescriptionFlags		flags;
166 			DEFAULT_IMAGE_FORMAT,								// VkFormat							format;
167 			VK_SAMPLE_COUNT_1_BIT,								// VkSampleCountFlagBits			samples;
168 			VK_ATTACHMENT_LOAD_OP_CLEAR,						// VkAttachmentLoadOp				loadOp;
169 			VK_ATTACHMENT_STORE_OP_STORE,						// VkAttachmentStoreOp				storeOp;
170 			VK_ATTACHMENT_LOAD_OP_DONT_CARE,					// VkAttachmentLoadOp				stencilLoadOp;
171 			VK_ATTACHMENT_STORE_OP_DONT_CARE,					// VkAttachmentStoreOp				stencilStoreOp;
172 			VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,			// VkImageLayout					initialLayout;
173 			VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,			// VkImageLayout					finalLayout;
174 		};
175 
176 		const VkAttachmentDescription			attachments[1]			=
177 		{
178 			colorAttDesc
179 		};
180 
181 		const VkAttachmentReference				colorAttRef				=
182 		{
183 			0u,													// deUint32							attachment;
184 			VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,			// VkImageLayout					layout;
185 		};
186 
187 		const VkSubpassDescription				subpassDesc[1]			=
188 		{
189 			{
190 				0u,												// VkSubpassDescriptionFlags		flags;
191 				VK_PIPELINE_BIND_POINT_GRAPHICS,				// VkPipelineBindPoint				pipelineBindPoint;
192 				0u,												// deUint32							inputAttachmentCount;
193 				DE_NULL,										// const VkAttachmentReference*		pInputAttachments;
194 				1u,												// deUint32							colorAttachmentCount;
195 				&colorAttRef,									// const VkAttachmentReference*		pColorAttachments;
196 				DE_NULL,										// const VkAttachmentReference*		pResolveAttachments;
197 				DE_NULL,										// const VkAttachmentReference*		depthStencilAttachment;
198 				0u,												// deUint32							preserveAttachmentCount;
199 				DE_NULL,										// const VkAttachmentReference*		pPreserveAttachments;
200 			}
201 		};
202 
203 		const VkRenderPassCreateInfo			renderPassCreateInfo	=
204 		{
205 			VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,			// VkStructureType					sType;
206 			DE_NULL,											// const void*						pNext;
207 			0u,													// VkRenderPassCreateFlags			flags;
208 			1u,													// deUint32							attachmentCount;
209 			attachments,										// const VkAttachmentDescription*	pAttachments;
210 			1u,													// deUint32							subpassCount;
211 			subpassDesc,										// const VkSubpassDescription*		pSubpasses;
212 			0u,													// deUint32							dependencyCount;
213 			DE_NULL,											// const VkSubpassDependency*		pDependencies;
214 		};
215 
216 		m_renderPass = createRenderPass(m_vkd, m_device, &renderPassCreateInfo, DE_NULL);
217 	}
218 
219 	{
220 		const VkImageCreateInfo					imageCreateInfo			=
221 		{
222 			VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,		// VkStructureType			sType;
223 			DE_NULL,									// const void*				pNext;
224 			0u,											// VkImageCreateFlags		flags;
225 			DEFAULT_IMAGE_TYPE,							// VkImageType				imageType;
226 			DEFAULT_IMAGE_FORMAT,						// VkFormat					format;
227 			DEFAULT_IMAGE_SIZE,							// VkExtent3D				extent;
228 			1,											// deUint32					mipLevels;
229 			1,											// deUint32					arrayLayers;
230 			VK_SAMPLE_COUNT_1_BIT,						// VkSampleCountFlagBits	samples;
231 			VK_IMAGE_TILING_OPTIMAL,					// VkImageTiling			tiling;
232 			VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
233 			VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
234 			VK_IMAGE_USAGE_TRANSFER_DST_BIT,			// VkImageUsageFlags		usage;
235 			VK_SHARING_MODE_EXCLUSIVE,					// VkSharingMode			sharingMode;
236 			1,											// deUint32					queueFamilyIndexCount;
237 			&m_queueFamilyIndex,						// const deUint32*			pQueueFamilyIndices;
238 			VK_IMAGE_LAYOUT_UNDEFINED					// VkImageLayout			initialLayout;
239 		};
240 
241 		m_colorImage = createImage(m_vkd, m_device, &imageCreateInfo, DE_NULL);
242 	}
243 
244 	m_colorImageMemory = m_allocator.allocate(getImageMemoryRequirements(m_vkd, m_device, *m_colorImage), MemoryRequirement::Any);
245 	VK_CHECK(m_vkd.bindImageMemory(m_device, *m_colorImage, m_colorImageMemory->getMemory(), m_colorImageMemory->getOffset()));
246 
247 	{
248 		const VkImageViewCreateInfo				imageViewCreateInfo		=
249 		{
250 			VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,	// VkStructureType				sType;
251 			DE_NULL,									// const void*					pNext;
252 			0u,											// VkImageViewCreateFlags		flags;
253 			*m_colorImage,								// VkImage						image;
254 			VK_IMAGE_VIEW_TYPE_2D,						// VkImageViewType				viewType;
255 			DEFAULT_IMAGE_FORMAT,						// VkFormat						format;
256 			{
257 				VK_COMPONENT_SWIZZLE_R,
258 				VK_COMPONENT_SWIZZLE_G,
259 				VK_COMPONENT_SWIZZLE_B,
260 				VK_COMPONENT_SWIZZLE_A
261 			},											// VkComponentMapping			components;
262 			{
263 				VK_IMAGE_ASPECT_COLOR_BIT,					// VkImageAspectFlags			aspectMask;
264 				0u,											// deUint32						baseMipLevel;
265 				1u,											// deUint32						mipLevels;
266 				0u,											// deUint32						baseArrayLayer;
267 				1u,											// deUint32						arraySize;
268 			},											// VkImageSubresourceRange		subresourceRange;
269 		};
270 
271 		m_colorImageView = createImageView(m_vkd, m_device, &imageViewCreateInfo, DE_NULL);
272 	}
273 
274 	{
275 		const VkImageView						attachmentViews[1]		=
276 		{
277 			*m_colorImageView
278 		};
279 
280 		const VkFramebufferCreateInfo			framebufferCreateInfo	=
281 		{
282 			VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,	// VkStructureType			sType;
283 			DE_NULL,									// const void*				pNext;
284 			0u,											// VkFramebufferCreateFlags	flags;
285 			*m_renderPass,								// VkRenderPass				renderPass;
286 			1,											// deUint32					attachmentCount;
287 			attachmentViews,							// const VkImageView*		pAttachments;
288 			DEFAULT_IMAGE_SIZE.width,					// deUint32					width;
289 			DEFAULT_IMAGE_SIZE.height,					// deUint32					height;
290 			1u,											// deUint32					layers;
291 		};
292 
293 		m_frameBuffer = createFramebuffer(m_vkd, m_device, &framebufferCreateInfo, DE_NULL);
294 	}
295 
296 	{
297 		const VkCommandBufferAllocateInfo		cmdBufferAllocateInfo	=
298 		{
299 			VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// VkStructureType             sType;
300 			DE_NULL,													// const void*                 pNext;
301 			*m_commandPool,												// VkCommandPool               commandPool;
302 			VK_COMMAND_BUFFER_LEVEL_SECONDARY,							// VkCommandBufferLevel        level;
303 			1u															// deUint32                    commandBufferCount;
304 		};
305 
306 		m_secondaryCommandBuffer = allocateCommandBuffer(m_vkd, m_device, &cmdBufferAllocateInfo);
307 
308 	}
309 }
310 
beginRenderPass(VkSubpassContents content)311 void CommandBufferRenderPassTestEnvironment::beginRenderPass(VkSubpassContents content)
312 {
313 	const VkClearValue						clearValues[1]			=
314 	{
315 		makeClearValueColorU32(17, 59, 163, 251),
316 	};
317 
318 	const VkRenderPassBeginInfo				renderPassBeginInfo		=
319 	{
320 		VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,				// VkStructureType		sType;
321 		DE_NULL,												// const void*			pNext;
322 		*m_renderPass,											// VkRenderPass			renderPass;
323 		*m_frameBuffer,											// VkFramebuffer		framebuffer;
324 		DEFAULT_IMAGE_AREA,										// VkRect2D				renderArea;
325 		1u,														// deUint32				clearValueCount;
326 		clearValues												// const VkClearValue*	pClearValues;
327 	};
328 
329 	m_vkd.cmdBeginRenderPass(m_primaryCommandBuffers[0], &renderPassBeginInfo, content);
330 }
331 
beginPrimaryCommandBuffer(VkCommandBufferUsageFlags usageFlags)332 void CommandBufferRenderPassTestEnvironment::beginPrimaryCommandBuffer(VkCommandBufferUsageFlags usageFlags)
333 {
334 	const VkCommandBufferBeginInfo			commandBufferBeginInfo	=
335 	{
336 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,			// VkStructureType                          sType;
337 		DE_NULL,												// const void*                              pNext;
338 		usageFlags,												// VkCommandBufferUsageFlags                flags;
339 		DE_NULL													// const VkCommandBufferInheritanceInfo*    pInheritanceInfo;
340 	};
341 
342 	VK_CHECK(m_vkd.beginCommandBuffer(m_primaryCommandBuffers[0], &commandBufferBeginInfo));
343 
344 }
345 
346 
beginSecondaryCommandBuffer(VkCommandBufferUsageFlags usageFlags)347 void CommandBufferRenderPassTestEnvironment::beginSecondaryCommandBuffer(VkCommandBufferUsageFlags usageFlags)
348 {
349 	const VkCommandBufferInheritanceInfo	commandBufferInheritanceInfo =
350 	{
351 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,		// VkStructureType                  sType;
352 		DE_NULL,												// const void*                      pNext;
353 		*m_renderPass,											// VkRenderPass                     renderPass;
354 		0u,														// deUint32                         subpass;
355 		*m_frameBuffer,											// VkFramebuffer                    framebuffer;
356 		VK_FALSE,												// VkBool32                         occlusionQueryEnable;
357 		0u,														// VkQueryControlFlags              queryFlags;
358 		0u														// VkQueryPipelineStatisticFlags    pipelineStatistics;
359 	};
360 
361 	const VkCommandBufferBeginInfo			commandBufferBeginInfo	=
362 	{
363 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,			// VkStructureType                          sType;
364 		DE_NULL,												// const void*                              pNext;
365 		usageFlags,												// VkCommandBufferUsageFlags                flags;
366 		&commandBufferInheritanceInfo							// const VkCommandBufferInheritanceInfo*    pInheritanceInfo;
367 	};
368 
369 	VK_CHECK(m_vkd.beginCommandBuffer(*m_secondaryCommandBuffer, &commandBufferBeginInfo));
370 
371 }
372 
submitPrimaryCommandBuffer(void)373 void CommandBufferRenderPassTestEnvironment::submitPrimaryCommandBuffer(void)
374 {
375 
376 	const VkFenceCreateInfo fenceCreateInfo							=
377 	{
378 		VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,					// VkStructureType       sType;
379 		DE_NULL,												// const void*           pNext;
380 		0u														// VkFenceCreateFlags    flags;
381 	};
382 
383 	const Unique<VkFence>					fence					(createFence(m_vkd, m_device, &fenceCreateInfo));
384 
385 
386 	const VkSubmitInfo						submitInfo				=
387 	{
388 		VK_STRUCTURE_TYPE_SUBMIT_INFO,							// VkStructureType                sType;
389 		DE_NULL,												// const void*                    pNext;
390 		0u,														// deUint32                       waitSemaphoreCount;
391 		DE_NULL,												// const VkSemaphore*             pWaitSemaphores;
392 		DE_NULL,												// const VkPipelineStageFlags*    pWaitDstStageMask;
393 		1u,														// deUint32                       commandBufferCount;
394 		m_primaryCommandBuffers,								// const VkCommandBuffer*         pCommandBuffers;
395 		0u,														// deUint32                       signalSemaphoreCount;
396 		DE_NULL													// const VkSemaphore*             pSignalSemaphores;
397 	};
398 
399 	VK_CHECK(m_vkd.queueSubmit(m_queue, 1, &submitInfo, *fence));
400 
401 	VK_CHECK(m_vkd.waitForFences(m_device, 1, &fence.get(), VK_TRUE, ~0ull));
402 
403 }
404 
readColorAttachment()405 de::MovePtr<tcu::TextureLevel> CommandBufferRenderPassTestEnvironment::readColorAttachment ()
406 {
407 	Move<VkBuffer>					buffer;
408 	de::MovePtr<Allocation>			bufferAlloc;
409 	const tcu::TextureFormat		tcuFormat		= mapVkFormat(DEFAULT_IMAGE_FORMAT);
410 	const VkDeviceSize				pixelDataSize	= DEFAULT_IMAGE_SIZE.height * DEFAULT_IMAGE_SIZE.height * tcuFormat.getPixelSize();
411 	de::MovePtr<tcu::TextureLevel>	resultLevel		(new tcu::TextureLevel(tcuFormat, DEFAULT_IMAGE_SIZE.width, DEFAULT_IMAGE_SIZE.height));
412 
413 	// Create destination buffer
414 	{
415 		const VkBufferCreateInfo bufferParams =
416 		{
417 			VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,		// VkStructureType		sType;
418 			DE_NULL,									// const void*			pNext;
419 			0u,											// VkBufferCreateFlags	flags;
420 			pixelDataSize,								// VkDeviceSize			size;
421 			VK_BUFFER_USAGE_TRANSFER_DST_BIT,			// VkBufferUsageFlags	usage;
422 			VK_SHARING_MODE_EXCLUSIVE,					// VkSharingMode		sharingMode;
423 			0u,											// deUint32				queueFamilyIndexCount;
424 			DE_NULL										// const deUint32*		pQueueFamilyIndices;
425 		};
426 
427 		buffer		= createBuffer(m_vkd, m_device, &bufferParams);
428 		bufferAlloc = m_allocator.allocate(getBufferMemoryRequirements(m_vkd, m_device, *buffer), MemoryRequirement::HostVisible);
429 		VK_CHECK(m_vkd.bindBufferMemory(m_device, *buffer, bufferAlloc->getMemory(), bufferAlloc->getOffset()));
430 	}
431 
432 	// Barriers for copying image to buffer
433 
434 	const VkImageMemoryBarrier imageBarrier =
435 	{
436 		VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,		// VkStructureType			sType;
437 		DE_NULL,									// const void*				pNext;
438 		VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,		// VkAccessFlags			srcAccessMask;
439 		VK_ACCESS_TRANSFER_READ_BIT,				// VkAccessFlags			dstAccessMask;
440 		VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,	// VkImageLayout			oldLayout;
441 		VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,		// VkImageLayout			newLayout;
442 		VK_QUEUE_FAMILY_IGNORED,					// deUint32					srcQueueFamilyIndex;
443 		VK_QUEUE_FAMILY_IGNORED,					// deUint32					dstQueueFamilyIndex;
444 		*m_colorImage,								// VkImage					image;
445 		{											// VkImageSubresourceRange	subresourceRange;
446 			VK_IMAGE_ASPECT_COLOR_BIT,	// VkImageAspectFlags	aspectMask;
447 			0u,							// deUint32				baseMipLevel;
448 			1u,							// deUint32				mipLevels;
449 			0u,							// deUint32				baseArraySlice;
450 			1u							// deUint32				arraySize;
451 		}
452 	};
453 
454 	const VkBufferMemoryBarrier bufferBarrier =
455 	{
456 		VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,	// VkStructureType	sType;
457 		DE_NULL,									// const void*		pNext;
458 		VK_ACCESS_TRANSFER_WRITE_BIT,				// VkAccessFlags	srcAccessMask;
459 		VK_ACCESS_HOST_READ_BIT,					// VkAccessFlags	dstAccessMask;
460 		VK_QUEUE_FAMILY_IGNORED,					// deUint32			srcQueueFamilyIndex;
461 		VK_QUEUE_FAMILY_IGNORED,					// deUint32			dstQueueFamilyIndex;
462 		*buffer,									// VkBuffer			buffer;
463 		0u,											// VkDeviceSize		offset;
464 		pixelDataSize								// VkDeviceSize		size;
465 	};
466 
467 	// Copy image to buffer
468 
469 	const VkBufferImageCopy copyRegion =
470 	{
471 		0u,												// VkDeviceSize				bufferOffset;
472 		DEFAULT_IMAGE_SIZE.width,						// deUint32					bufferRowLength;
473 		DEFAULT_IMAGE_SIZE.height,						// deUint32					bufferImageHeight;
474 		{ VK_IMAGE_ASPECT_COLOR_BIT, 0u, 0u, 1u },		// VkImageSubresourceLayers	imageSubresource;
475 		{ 0, 0, 0 },									// VkOffset3D				imageOffset;
476 		DEFAULT_IMAGE_SIZE								// VkExtent3D				imageExtent;
477 	};
478 
479 	beginPrimaryCommandBuffer(0);
480 	m_vkd.cmdPipelineBarrier(m_primaryCommandBuffers[0], VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, (VkDependencyFlags)0, 0, (const VkMemoryBarrier*)DE_NULL, 0, (const VkBufferMemoryBarrier*)DE_NULL, 1, &imageBarrier);
481 	m_vkd.cmdCopyImageToBuffer(m_primaryCommandBuffers[0], *m_colorImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, *buffer, 1, &copyRegion);
482 	m_vkd.cmdPipelineBarrier(m_primaryCommandBuffers[0], VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, (VkDependencyFlags)0, 0, (const VkMemoryBarrier*)DE_NULL, 1, &bufferBarrier, 0, (const VkImageMemoryBarrier*)DE_NULL);
483 	VK_CHECK(m_vkd.endCommandBuffer(m_primaryCommandBuffers[0]));
484 
485 	submitPrimaryCommandBuffer();
486 
487 	// Read buffer data
488 	invalidateMappedMemoryRange(m_vkd, m_device, bufferAlloc->getMemory(), bufferAlloc->getOffset(), pixelDataSize);
489 	tcu::copy(*resultLevel, tcu::ConstPixelBufferAccess(resultLevel->getFormat(), resultLevel->getSize(), bufferAlloc->getHostPtr()));
490 
491 	return resultLevel;
492 }
493 
494 
495 // Testcases
496 /********* 19.1. Command Pools (5.1 in VK 1.0 Spec) ***************************/
createPoolNullParamsTest(Context & context)497 tcu::TestStatus createPoolNullParamsTest(Context& context)
498 {
499 	const VkDevice							vkDevice				= context.getDevice();
500 	const DeviceInterface&					vk						= context.getDeviceInterface();
501 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
502 
503 	const VkCommandPoolCreateInfo			cmdPoolParams			=
504 	{
505 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// sType;
506 		DE_NULL,													// pNext;
507 		0u,															// flags;
508 		queueFamilyIndex,											// queueFamilyIndex;
509 	};
510 
511 	createCommandPool(vk, vkDevice, &cmdPoolParams, DE_NULL);
512 
513 	return tcu::TestStatus::pass("Command Pool allocated correctly.");
514 }
515 
createPoolNonNullAllocatorTest(Context & context)516 tcu::TestStatus createPoolNonNullAllocatorTest(Context& context)
517 {
518 	const VkDevice							vkDevice				= context.getDevice();
519 	const DeviceInterface&					vk						= context.getDeviceInterface();
520 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
521 	const VkAllocationCallbacks*			allocationCallbacks		= getSystemAllocator();
522 
523 	const VkCommandPoolCreateInfo			cmdPoolParams			=
524 	{
525 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// sType;
526 		DE_NULL,													// pNext;
527 		0u,															// flags;
528 		queueFamilyIndex,											// queueFamilyIndex;
529 	};
530 
531 	createCommandPool(vk, vkDevice, &cmdPoolParams, allocationCallbacks);
532 
533 	return tcu::TestStatus::pass("Command Pool allocated correctly.");
534 }
535 
createPoolTransientBitTest(Context & context)536 tcu::TestStatus createPoolTransientBitTest(Context& context)
537 {
538 	const VkDevice							vkDevice				= context.getDevice();
539 	const DeviceInterface&					vk						= context.getDeviceInterface();
540 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
541 
542 	const VkCommandPoolCreateInfo			cmdPoolParams			=
543 	{
544 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// sType;
545 		DE_NULL,													// pNext;
546 		VK_COMMAND_POOL_CREATE_TRANSIENT_BIT,						// flags;
547 		queueFamilyIndex,											// queueFamilyIndex;
548 	};
549 
550 	createCommandPool(vk, vkDevice, &cmdPoolParams, DE_NULL);
551 
552 	return tcu::TestStatus::pass("Command Pool allocated correctly.");
553 }
554 
createPoolResetBitTest(Context & context)555 tcu::TestStatus createPoolResetBitTest(Context& context)
556 {
557 	const VkDevice							vkDevice				= context.getDevice();
558 	const DeviceInterface&					vk						= context.getDeviceInterface();
559 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
560 
561 	const VkCommandPoolCreateInfo			cmdPoolParams			=
562 	{
563 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// sType;
564 		DE_NULL,													// pNext;
565 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			// flags;
566 		queueFamilyIndex,											// queueFamilyIndex;
567 	};
568 
569 	createCommandPool(vk, vkDevice, &cmdPoolParams, DE_NULL);
570 
571 	return tcu::TestStatus::pass("Command Pool allocated correctly.");
572 }
573 
resetPoolReleaseResourcesBitTest(Context & context)574 tcu::TestStatus resetPoolReleaseResourcesBitTest(Context& context)
575 {
576 	const VkDevice							vkDevice				= context.getDevice();
577 	const DeviceInterface&					vk						= context.getDeviceInterface();
578 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
579 
580 	const VkCommandPoolCreateInfo			cmdPoolParams			=
581 	{
582 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// sType;
583 		DE_NULL,													// pNext;
584 		0u,															// flags;
585 		queueFamilyIndex,											// queueFamilyIndex;
586 	};
587 
588 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams, DE_NULL));
589 
590 	VK_CHECK(vk.resetCommandPool(vkDevice, *cmdPool, VK_COMMAND_POOL_RESET_RELEASE_RESOURCES_BIT));
591 
592 	return tcu::TestStatus::pass("Command Pool allocated correctly.");
593 }
594 
resetPoolNoFlagsTest(Context & context)595 tcu::TestStatus resetPoolNoFlagsTest(Context& context)
596 {
597 	const VkDevice							vkDevice				= context.getDevice();
598 	const DeviceInterface&					vk						= context.getDeviceInterface();
599 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
600 
601 	const VkCommandPoolCreateInfo			cmdPoolParams			=
602 	{
603 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// sType;
604 		DE_NULL,													// pNext;
605 		0u,															// flags;
606 		queueFamilyIndex,											// queueFamilyIndex;
607 	};
608 
609 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams, DE_NULL));
610 
611 	VK_CHECK(vk.resetCommandPool(vkDevice, *cmdPool, 0u));
612 
613 	return tcu::TestStatus::pass("Command Pool allocated correctly.");
614 }
615 
616 /******** 19.2. Command Buffer Lifetime (5.2 in VK 1.0 Spec) ******************/
allocatePrimaryBufferTest(Context & context)617 tcu::TestStatus allocatePrimaryBufferTest(Context& context)
618 {
619 	const VkDevice							vkDevice				= context.getDevice();
620 	const DeviceInterface&					vk						= context.getDeviceInterface();
621 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
622 
623 	const VkCommandPoolCreateInfo			cmdPoolParams			=
624 	{
625 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// sType;
626 		DE_NULL,													// pNext;
627 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			// flags;
628 		queueFamilyIndex,											// queueFamilyIndex;
629 	};
630 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
631 
632 	// Command buffer
633 	const VkCommandBufferAllocateInfo		cmdBufParams			=
634 	{
635 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// sType;
636 		DE_NULL,													// pNext;
637 		*cmdPool,													// commandPool;
638 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							// level;
639 		1u,															// bufferCount;
640 	};
641 	const Unique<VkCommandBuffer>			cmdBuf					(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
642 
643 	return tcu::TestStatus::pass("Buffer was created correctly.");
644 }
645 
allocateManyPrimaryBuffersTest(Context & context)646 tcu::TestStatus allocateManyPrimaryBuffersTest(Context& context)
647 {
648 
649 	const VkDevice							vkDevice				= context.getDevice();
650 	const DeviceInterface&					vk						= context.getDeviceInterface();
651 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
652 
653 	const VkCommandPoolCreateInfo			cmdPoolParams			=
654 	{
655 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					//	VkStructureType				sType;
656 		DE_NULL,													//	const void*					pNext;
657 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			//	VkCommandPoolCreateFlags	flags;
658 		queueFamilyIndex,											//	deUint32					queueFamilyIndex;
659 	};
660 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
661 
662 	// \todo Determining the minimum number of command buffers should be a function of available system memory and driver capabilities.
663 #if (DE_PTR_SIZE == 4)
664 	const unsigned minCommandBuffer = 1024;
665 #else
666 	const unsigned minCommandBuffer = 10000;
667 #endif
668 
669 	// Command buffer
670 	const VkCommandBufferAllocateInfo		cmdBufParams			=
671 	{
672 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				//	VkStructureType				sType;
673 		DE_NULL,													//	const void*					pNext;
674 		*cmdPool,													//	VkCommandPool				pool;
675 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							//	VkCommandBufferLevel		level;
676 		minCommandBuffer,											//	uint32_t					bufferCount;
677 	};
678 
679 	// do not keep the handles to buffers, as they will be freed with command pool
680 
681 	// allocate the minimum required amount of buffers
682 	VkCommandBuffer cmdBuffers[minCommandBuffer];
683 	VK_CHECK(vk.allocateCommandBuffers(vkDevice, &cmdBufParams, cmdBuffers));
684 
685 	std::ostringstream out;
686 	out << "allocateManyPrimaryBuffersTest succeded: created " << minCommandBuffer << " command buffers";
687 
688 	return tcu::TestStatus::pass(out.str());
689 }
690 
allocateZeroPrimaryBuffersTest(Context & context)691 tcu::TestStatus allocateZeroPrimaryBuffersTest(Context& context)
692 {
693 	const VkDevice							vkDevice				= context.getDevice();
694 	const DeviceInterface&					vk						= context.getDeviceInterface();
695 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
696 
697 	const VkCommandPoolCreateInfo			cmdPoolParams			=
698 	{
699 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// sType;
700 		DE_NULL,													// pNext;
701 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			// flags;
702 		queueFamilyIndex,											// queueFamilyIndex;
703 	};
704 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
705 
706 	// Command buffer
707 	const VkCommandBufferAllocateInfo		cmdBufParams			=
708 	{
709 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// sType;
710 		DE_NULL,													// pNext;
711 		*cmdPool,													// commandPool;
712 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							// level;
713 		0u,															// bufferCount;
714 	};
715 
716 	VkCommandBuffer cmdBuffer;
717 	VK_CHECK(vk.allocateCommandBuffers(vkDevice, &cmdBufParams, &cmdBuffer));
718 
719 	return tcu::TestStatus::pass("allocateZeroPrimaryBuffersTest passed.");
720 }
721 
allocateSecondaryBufferTest(Context & context)722 tcu::TestStatus allocateSecondaryBufferTest(Context& context)
723 {
724 	const VkDevice							vkDevice				= context.getDevice();
725 	const DeviceInterface&					vk						= context.getDeviceInterface();
726 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
727 
728 	const VkCommandPoolCreateInfo			cmdPoolParams			=
729 	{
730 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// sType;
731 		DE_NULL,													// pNext;
732 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			// flags;
733 		queueFamilyIndex,											// queueFamilyIndex;
734 	};
735 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
736 
737 	// Command buffer
738 	const VkCommandBufferAllocateInfo		cmdBufParams			=
739 	{
740 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// sType;
741 		DE_NULL,													// pNext;
742 		*cmdPool,													// commandPool;
743 		VK_COMMAND_BUFFER_LEVEL_SECONDARY,							// level;
744 		1u,															// bufferCount;
745 	};
746 	const Unique<VkCommandBuffer>			cmdBuf					(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
747 
748 	return tcu::TestStatus::pass("Buffer was created correctly.");
749 }
750 
allocateManySecondaryBuffersTest(Context & context)751 tcu::TestStatus allocateManySecondaryBuffersTest(Context& context)
752 {
753 
754 	const VkDevice							vkDevice				= context.getDevice();
755 	const DeviceInterface&					vk						= context.getDeviceInterface();
756 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
757 
758 	const VkCommandPoolCreateInfo			cmdPoolParams			=
759 	{
760 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					//	VkStructureType				sType;
761 		DE_NULL,													//	const void*					pNext;
762 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			//	VkCommandPoolCreateFlags	flags;
763 		queueFamilyIndex,											//	deUint32					queueFamilyIndex;
764 	};
765 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
766 
767 	// \todo Determining the minimum number of command buffers should be a function of available system memory and driver capabilities.
768 #if (DE_PTR_SIZE == 4)
769 	const unsigned minCommandBuffer = 1024;
770 #else
771 	const unsigned minCommandBuffer = 10000;
772 #endif
773 
774 	// Command buffer
775 	const VkCommandBufferAllocateInfo		cmdBufParams			=
776 	{
777 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				//	VkStructureType				sType;
778 		DE_NULL,													//	const void*					pNext;
779 		*cmdPool,													//	VkCommandPool				pool;
780 		VK_COMMAND_BUFFER_LEVEL_SECONDARY,							//	VkCommandBufferLevel		level;
781 		minCommandBuffer,											//	uint32_t					bufferCount;
782 	};
783 
784 	// do not keep the handles to buffers, as they will be freed with command pool
785 
786 	// allocate the minimum required amount of buffers
787 	VkCommandBuffer cmdBuffers[minCommandBuffer];
788 	VK_CHECK(vk.allocateCommandBuffers(vkDevice, &cmdBufParams, cmdBuffers));
789 
790 	std::ostringstream out;
791 	out << "allocateManySecondaryBuffersTest succeded: created " << minCommandBuffer << " command buffers";
792 
793 	return tcu::TestStatus::pass(out.str());
794 }
795 
allocateZeroSecondaryBuffersTest(Context & context)796 tcu::TestStatus allocateZeroSecondaryBuffersTest(Context& context)
797 {
798 	const VkDevice							vkDevice				= context.getDevice();
799 	const DeviceInterface&					vk						= context.getDeviceInterface();
800 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
801 
802 	const VkCommandPoolCreateInfo			cmdPoolParams			=
803 	{
804 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// sType;
805 		DE_NULL,													// pNext;
806 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			// flags;
807 		queueFamilyIndex,											// queueFamilyIndex;
808 	};
809 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
810 
811 	// Command buffer
812 	const VkCommandBufferAllocateInfo		cmdBufParams			=
813 	{
814 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// sType;
815 		DE_NULL,													// pNext;
816 		*cmdPool,													// commandPool;
817 		VK_COMMAND_BUFFER_LEVEL_SECONDARY,							// level;
818 		0u,															// bufferCount;
819 	};
820 
821 	VkCommandBuffer cmdBuffer;
822 	VK_CHECK(vk.allocateCommandBuffers(vkDevice, &cmdBufParams, &cmdBuffer));
823 
824 	return tcu::TestStatus::pass("allocateZeroSecondaryBuffersTest passed.");
825 }
826 
executePrimaryBufferTest(Context & context)827 tcu::TestStatus executePrimaryBufferTest(Context& context)
828 {
829 	const VkDevice							vkDevice				= context.getDevice();
830 	const DeviceInterface&					vk						= context.getDeviceInterface();
831 	const VkQueue							queue					= context.getUniversalQueue();
832 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
833 
834 	const VkCommandPoolCreateInfo			cmdPoolParams			=
835 	{
836 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					//	VkStructureType				sType;
837 		DE_NULL,													//	const void*					pNext;
838 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			//	VkCommandPoolCreateFlags	flags;
839 		queueFamilyIndex,											//	deUint32					queueFamilyIndex;
840 	};
841 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
842 
843 	// Command buffer
844 	const VkCommandBufferAllocateInfo		cmdBufParams			=
845 	{
846 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				//	VkStructureType				sType;
847 		DE_NULL,													//	const void*					pNext;
848 		*cmdPool,													//	VkCommandPool				pool;
849 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							//	VkCommandBufferLevel		level;
850 		1u,															//	uint32_t					bufferCount;
851 	};
852 	const Unique<VkCommandBuffer>			primCmdBuf				(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
853 	const VkCommandBufferBeginInfo			primCmdBufBeginInfo		=
854 	{
855 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
856 		DE_NULL,
857 		0,															// flags
858 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
859 	};
860 
861 	// Fill create info struct for event
862 	const VkEventCreateInfo					eventCreateInfo			=
863 	{
864 		VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,
865 		DE_NULL,
866 		0u,
867 	};
868 
869 	// create event that will be used to check if secondary command buffer has been executed
870 	const Unique<VkEvent>					event					(createEvent(vk, vkDevice, &eventCreateInfo));
871 
872 	// reset event
873 	VK_CHECK(vk.resetEvent(vkDevice, *event));
874 
875 	// record primary command buffer
876 	VK_CHECK(vk.beginCommandBuffer(*primCmdBuf, &primCmdBufBeginInfo));
877 	{
878 		// allow execution of event during every stage of pipeline
879 		VkPipelineStageFlags stageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
880 
881 		// record setting event
882 		vk.cmdSetEvent(*primCmdBuf, *event,stageMask);
883 	}
884 	VK_CHECK(vk.endCommandBuffer(*primCmdBuf));
885 
886 	const VkFenceCreateInfo					fenceCreateInfo			=
887 	{
888 		VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
889 		DE_NULL,
890 		0u,															// flags
891 	};
892 
893 	// create fence to wait for execution of queue
894 	const Unique<VkFence>					fence					(createFence(vk, vkDevice, &fenceCreateInfo));
895 
896 	const VkSubmitInfo						submitInfo				=
897 	{
898 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
899 		DE_NULL,													// pNext
900 		0u,															// waitSemaphoreCount
901 		DE_NULL,													// pWaitSemaphores
902 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
903 		1,															// commandBufferCount
904 		&primCmdBuf.get(),											// pCommandBuffers
905 		0u,															// signalSemaphoreCount
906 		DE_NULL,													// pSignalSemaphores
907 	};
908 
909 	// Submit the command buffer to the queue
910 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, *fence));
911 
912 	// wait for end of execution of queue
913 	VK_CHECK(vk.waitForFences(vkDevice, 1, &fence.get(), 0u, INFINITE_TIMEOUT));
914 
915 	// check if buffer has been executed
916 	VkResult result = vk.getEventStatus(vkDevice,*event);
917 	if (result == VK_EVENT_SET)
918 		return tcu::TestStatus::pass("Execute Primary Command Buffer succeeded");
919 
920 	return tcu::TestStatus::fail("Execute Primary Command Buffer FAILED");
921 }
922 
executeLargePrimaryBufferTest(Context & context)923 tcu::TestStatus executeLargePrimaryBufferTest(Context& context)
924 {
925 	const VkDevice							vkDevice				= context.getDevice();
926 	const DeviceInterface&					vk						= context.getDeviceInterface();
927 	const VkQueue							queue					= context.getUniversalQueue();
928 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
929 	const deUint32							LARGE_BUFFER_SIZE		= 10000;
930 
931 	const VkCommandPoolCreateInfo			cmdPoolParams			=
932 	{
933 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					//	VkStructureType				sType;
934 		DE_NULL,													//	const void*					pNext;
935 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			//	VkCommandPoolCreateFlags	flags;
936 		queueFamilyIndex,											//	deUint32					queueFamilyIndex;
937 	};
938 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
939 
940 	// Command buffer
941 	const VkCommandBufferAllocateInfo		cmdBufParams			=
942 	{
943 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				//	VkStructureType				sType;
944 		DE_NULL,													//	const void*					pNext;
945 		*cmdPool,													//	VkCommandPool				pool;
946 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							//	VkCommandBufferLevel		level;
947 		1u,															//	uint32_t					bufferCount;
948 	};
949 	const Unique<VkCommandBuffer>			primCmdBuf				(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
950 	const VkCommandBufferBeginInfo			primCmdBufBeginInfo		=
951 	{
952 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
953 		DE_NULL,
954 		0,															// flags
955 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
956 	};
957 
958 	// Fill create info struct for event
959 	const VkEventCreateInfo					eventCreateInfo			=
960 	{
961 		VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,
962 		DE_NULL,
963 		0u,
964 	};
965 
966 	std::vector<VkEventSp>					events;
967 	for (deUint32 ndx = 0; ndx < LARGE_BUFFER_SIZE; ++ndx)
968 		events.push_back(VkEventSp(new vk::Unique<VkEvent>(createEvent(vk, vkDevice, &eventCreateInfo, DE_NULL))));
969 
970 	// record primary command buffer
971 	VK_CHECK(vk.beginCommandBuffer(*primCmdBuf, &primCmdBufBeginInfo));
972 	{
973 		// set all the events
974 		for (deUint32 ndx = 0; ndx < LARGE_BUFFER_SIZE; ++ndx)
975 		{
976 			vk.cmdSetEvent(*primCmdBuf, events[ndx]->get(), VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
977 		}
978 	}
979 	VK_CHECK(vk.endCommandBuffer(*primCmdBuf));
980 
981 	const VkFenceCreateInfo					fenceCreateInfo			=
982 	{
983 		VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
984 		DE_NULL,
985 		0u,															// flags
986 	};
987 
988 	// create fence to wait for execution of queue
989 	const Unique<VkFence>					fence					(createFence(vk, vkDevice, &fenceCreateInfo));
990 
991 	const VkSubmitInfo						submitInfo				=
992 	{
993 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
994 		DE_NULL,													// pNext
995 		0u,															// waitSemaphoreCount
996 		DE_NULL,													// pWaitSemaphores
997 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
998 		1,															// commandBufferCount
999 		&primCmdBuf.get(),											// pCommandBuffers
1000 		0u,															// signalSemaphoreCount
1001 		DE_NULL,													// pSignalSemaphores
1002 	};
1003 
1004 	// Submit the command buffer to the queue
1005 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, *fence));
1006 
1007 	// wait for end of execution of queue
1008 	VK_CHECK(vk.waitForFences(vkDevice, 1, &fence.get(), 0u, INFINITE_TIMEOUT));
1009 
1010 	// check if the buffer was executed correctly - all events had their status
1011 	// changed
1012 	tcu::TestStatus testResult = tcu::TestStatus::incomplete();
1013 
1014 	for (deUint32 ndx = 0; ndx < LARGE_BUFFER_SIZE; ++ndx)
1015 	{
1016 		if (vk.getEventStatus(vkDevice, events[ndx]->get()) != VK_EVENT_SET)
1017 		{
1018 			testResult = tcu::TestStatus::fail("An event was not set.");
1019 			break;
1020 		}
1021 	}
1022 
1023 	if (!testResult.isComplete())
1024 		testResult = tcu::TestStatus::pass("All events set correctly.");
1025 
1026 	return testResult;
1027 }
1028 
resetBufferImplicitlyTest(Context & context)1029 tcu::TestStatus resetBufferImplicitlyTest(Context& context)
1030 {
1031 	const VkDevice							vkDevice				= context.getDevice();
1032 	const DeviceInterface&					vk						= context.getDeviceInterface();
1033 	const VkQueue							queue					= context.getUniversalQueue();
1034 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
1035 
1036 	const VkCommandPoolCreateInfo			cmdPoolParams			=
1037 	{
1038 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// sType;
1039 		DE_NULL,													// pNext;
1040 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			// flags;
1041 		queueFamilyIndex,											// queueFamilyIndex;
1042 	};
1043 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
1044 
1045 	// Command buffer
1046 	const VkCommandBufferAllocateInfo		cmdBufParams			=
1047 	{
1048 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// sType;
1049 		DE_NULL,													// pNext;
1050 		*cmdPool,													// pool;
1051 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							// level;
1052 		1u,															// bufferCount;
1053 	};
1054 	const Unique<VkCommandBuffer>			cmdBuf						(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
1055 
1056 	const VkCommandBufferBeginInfo			cmdBufBeginInfo			=
1057 	{
1058 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,				// sType
1059 		DE_NULL,													// pNext
1060 		0u,															// flags
1061 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
1062 	};
1063 
1064 	const VkEventCreateInfo					eventCreateInfo			=
1065 	{
1066 		VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,						// sType;
1067 		DE_NULL,													// pNext;
1068 		0u,															// flags;
1069 	};
1070 	const Unique<VkEvent>					event					(createEvent(vk, vkDevice, &eventCreateInfo));
1071 
1072 	// Put the command buffer in recording state.
1073 	VK_CHECK(vk.beginCommandBuffer(*cmdBuf, &cmdBufBeginInfo));
1074 	{
1075 		// Set the event
1076 		vk.cmdSetEvent(*cmdBuf, *event, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
1077 	}
1078 	VK_CHECK(vk.endCommandBuffer(*cmdBuf));
1079 
1080 	// We'll use a fence to wait for the execution of the queue
1081 	const VkFenceCreateInfo					fenceCreateInfo			=
1082 	{
1083 		VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,						// sType;
1084 		DE_NULL,													// pNext;
1085 		0u,															// flags
1086 	};
1087 	const Unique<VkFence>					fence					(createFence(vk, vkDevice, &fenceCreateInfo));
1088 
1089 	const VkSubmitInfo						submitInfo				=
1090 	{
1091 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
1092 		DE_NULL,													// pNext
1093 		0u,															// waitSemaphoreCount
1094 		DE_NULL,													// pWaitSemaphores
1095 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
1096 		1u,															// commandBufferCount
1097 		&cmdBuf.get(),												// pCommandBuffers
1098 		0u,															// signalSemaphoreCount
1099 		DE_NULL,													// pSignalSemaphores
1100 	};
1101 
1102 	// Submitting the command buffer that sets the event to the queue
1103 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, fence.get()));
1104 
1105 	// Waiting for the queue to finish executing
1106 	VK_CHECK(vk.waitForFences(vkDevice, 1u, &fence.get(), 0u, INFINITE_TIMEOUT));
1107 	// Reset the fence so that we can reuse it
1108 	VK_CHECK(vk.resetFences(vkDevice, 1u, &fence.get()));
1109 
1110 	// Check if the buffer was executed
1111 	if (vk.getEventStatus(vkDevice, *event) != VK_EVENT_SET)
1112 		return tcu::TestStatus::fail("Failed to set the event.");
1113 
1114 	// Reset the event
1115 	vk.resetEvent(vkDevice, *event);
1116 	if(vk.getEventStatus(vkDevice, *event) != VK_EVENT_RESET)
1117 		return tcu::TestStatus::fail("Failed to reset the event.");
1118 
1119 	// Reset the command buffer by putting it in recording state again. This
1120 	// should empty the command buffer.
1121 	VK_CHECK(vk.beginCommandBuffer(*cmdBuf, &cmdBufBeginInfo));
1122 	VK_CHECK(vk.endCommandBuffer(*cmdBuf));
1123 
1124 	// Submit the command buffer after resetting. It should have no commands
1125 	// recorded, so the event should remain unsignaled.
1126 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, fence.get()));
1127 	// Waiting for the queue to finish executing
1128 	VK_CHECK(vk.waitForFences(vkDevice, 1u, &fence.get(), 0u, INFINITE_TIMEOUT));
1129 
1130 	// Check if the event remained unset.
1131 	if(vk.getEventStatus(vkDevice, *event) == VK_EVENT_RESET)
1132 		return tcu::TestStatus::pass("Buffer was reset correctly.");
1133 	else
1134 		return tcu::TestStatus::fail("Buffer was not reset correctly.");
1135 }
1136 
1137 /******** 19.3. Command Buffer Recording (5.3 in VK 1.0 Spec) *****************/
recordSinglePrimaryBufferTest(Context & context)1138 tcu::TestStatus recordSinglePrimaryBufferTest(Context& context)
1139 {
1140 	const VkDevice							vkDevice				= context.getDevice();
1141 	const DeviceInterface&					vk						= context.getDeviceInterface();
1142 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
1143 
1144 	const VkCommandPoolCreateInfo			cmdPoolParams			=
1145 	{
1146 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					//	VkStructureType				sType;
1147 		DE_NULL,													//	const void*					pNext;
1148 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			//	VkCommandPoolCreateFlags	flags;
1149 		queueFamilyIndex,											//	deUint32					queueFamilyIndex;
1150 	};
1151 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
1152 
1153 	// Command buffer
1154 	const VkCommandBufferAllocateInfo		cmdBufParams			=
1155 	{
1156 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				//	VkStructureType				sType;
1157 		DE_NULL,													//	const void*					pNext;
1158 		*cmdPool,													//	VkCommandPool				pool;
1159 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							//	VkCommandBufferLevel		level;
1160 		1u,															//	uint32_t					bufferCount;
1161 	};
1162 	const Unique<VkCommandBuffer>			primCmdBuf				(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
1163 
1164 	const VkCommandBufferBeginInfo			primCmdBufBeginInfo		=
1165 	{
1166 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1167 		DE_NULL,
1168 		0,															// flags
1169 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
1170 	};
1171 
1172 	// Fill create info struct for event
1173 	const VkEventCreateInfo					eventCreateInfo			=
1174 	{
1175 		VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,
1176 		DE_NULL,
1177 		0u,
1178 	};
1179 
1180 	// create event that will be used to check if secondary command buffer has been executed
1181 	const Unique<VkEvent>					event					(createEvent(vk, vkDevice, &eventCreateInfo));
1182 
1183 	// record primary command buffer
1184 	VK_CHECK(vk.beginCommandBuffer(*primCmdBuf, &primCmdBufBeginInfo));
1185 	{
1186 		// record setting event
1187 		vk.cmdSetEvent(*primCmdBuf, *event, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
1188 	}
1189 	VK_CHECK(vk.endCommandBuffer(*primCmdBuf));
1190 
1191 	return tcu::TestStatus::pass("Primary buffer recorded successfully.");
1192 }
1193 
recordLargePrimaryBufferTest(Context & context)1194 tcu::TestStatus recordLargePrimaryBufferTest(Context &context)
1195 {
1196 
1197 	const VkDevice							vkDevice				= context.getDevice();
1198 	const DeviceInterface&					vk						= context.getDeviceInterface();
1199 	const VkQueue							queue					= context.getUniversalQueue();
1200 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
1201 
1202 	const VkCommandPoolCreateInfo			cmdPoolParams			=
1203 	{
1204 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					//	VkStructureType				sType;
1205 		DE_NULL,													//	const void*					pNext;
1206 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			//	VkCommandPoolCreateFlags	flags;
1207 		queueFamilyIndex,											//	deUint32					queueFamilyIndex;
1208 	};
1209 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
1210 
1211 	// Command buffer
1212 	const VkCommandBufferAllocateInfo		cmdBufParams			=
1213 	{
1214 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				//	VkStructureType				sType;
1215 		DE_NULL,													//	const void*					pNext;
1216 		*cmdPool,													//	VkCommandPool				pool;
1217 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							//	VkCommandBufferLevel		level;
1218 		1u,															//	uint32_t					bufferCount;
1219 	};
1220 	const Unique<VkCommandBuffer>			primCmdBuf				(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
1221 	const VkCommandBufferBeginInfo			primCmdBufBeginInfo		=
1222 	{
1223 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1224 		DE_NULL,
1225 		0,															// flags
1226 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
1227 	};
1228 
1229 	// Fill create info struct for event
1230 	const VkEventCreateInfo					eventCreateInfo			=
1231 	{
1232 		VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,
1233 		DE_NULL,
1234 		0u,
1235 	};
1236 
1237 	// create event that will be used to check if secondary command buffer has been executed
1238 	const Unique<VkEvent>					event					(createEvent(vk, vkDevice, &eventCreateInfo));
1239 
1240 	// reset event
1241 	VK_CHECK(vk.resetEvent(vkDevice, *event));
1242 
1243 	// record primary command buffer
1244 	VK_CHECK(vk.beginCommandBuffer(*primCmdBuf, &primCmdBufBeginInfo));
1245 	{
1246 		// allow execution of event during every stage of pipeline
1247 		VkPipelineStageFlags stageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1248 
1249 		// define minimal amount of commands to accept
1250 		const long long unsigned minNumCommands = 10000llu;
1251 
1252 		for ( long long unsigned currentCommands = 0; currentCommands < minNumCommands / 2; ++currentCommands )
1253 		{
1254 			// record setting event
1255 			vk.cmdSetEvent(*primCmdBuf, *event,stageMask);
1256 
1257 			// record resetting event
1258 			vk.cmdResetEvent(*primCmdBuf, *event,stageMask);
1259 		};
1260 
1261 	}
1262 	VK_CHECK(vk.endCommandBuffer(*primCmdBuf));
1263 
1264 	const VkFenceCreateInfo					fenceCreateInfo			=
1265 	{
1266 		VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
1267 		DE_NULL,
1268 		0u,															// flags
1269 	};
1270 
1271 	// create fence to wait for execution of queue
1272 	const Unique<VkFence>					fence					(createFence(vk, vkDevice, &fenceCreateInfo));
1273 
1274 	const VkSubmitInfo						submitInfo				=
1275 	{
1276 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
1277 		DE_NULL,													// pNext
1278 		0u,															// waitSemaphoreCount
1279 		DE_NULL,													// pWaitSemaphores
1280 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
1281 		1,															// commandBufferCount
1282 		&primCmdBuf.get(),											// pCommandBuffers
1283 		0u,															// signalSemaphoreCount
1284 		DE_NULL,													// pSignalSemaphores
1285 	};
1286 
1287 	// Submit the command buffer to the queue
1288 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, *fence));
1289 
1290 	// wait for end of execution of queue
1291 	VK_CHECK(vk.waitForFences(vkDevice, 1, &fence.get(), 0u, INFINITE_TIMEOUT));
1292 
1293 	return tcu::TestStatus::pass("hugeTest succeeded");
1294 }
1295 
recordSingleSecondaryBufferTest(Context & context)1296 tcu::TestStatus recordSingleSecondaryBufferTest(Context& context)
1297 {
1298 	const VkDevice							vkDevice				= context.getDevice();
1299 	const DeviceInterface&					vk						= context.getDeviceInterface();
1300 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
1301 
1302 	const VkCommandPoolCreateInfo			cmdPoolParams			=
1303 	{
1304 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					//	VkStructureType				sType;
1305 		DE_NULL,													//	const void*					pNext;
1306 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			//	VkCommandPoolCreateFlags	flags;
1307 		queueFamilyIndex,											//	deUint32					queueFamilyIndex;
1308 	};
1309 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
1310 
1311 	// Command buffer
1312 	const VkCommandBufferAllocateInfo		cmdBufParams			=
1313 	{
1314 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				//	VkStructureType				sType;
1315 		DE_NULL,													//	const void*					pNext;
1316 		*cmdPool,													//	VkCommandPool				pool;
1317 		VK_COMMAND_BUFFER_LEVEL_SECONDARY,							//	VkCommandBufferLevel		level;
1318 		1u,															//	uint32_t					bufferCount;
1319 	};
1320 	const Unique<VkCommandBuffer>			secCmdBuf				(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
1321 
1322 	const VkCommandBufferInheritanceInfo	secCmdBufInheritInfo	=
1323 	{
1324 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
1325 		DE_NULL,
1326 		(VkRenderPass)0u,											// renderPass
1327 		0u,															// subpass
1328 		(VkFramebuffer)0u,											// framebuffer
1329 		VK_FALSE,													// occlusionQueryEnable
1330 		(VkQueryControlFlags)0u,									// queryFlags
1331 		(VkQueryPipelineStatisticFlags)0u,							// pipelineStatistics
1332 	};
1333 	const VkCommandBufferBeginInfo			secCmdBufBeginInfo		=
1334 	{
1335 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1336 		DE_NULL,
1337 		0,															// flags
1338 		&secCmdBufInheritInfo,
1339 	};
1340 
1341 	// Fill create info struct for event
1342 	const VkEventCreateInfo					eventCreateInfo			=
1343 	{
1344 		VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,
1345 		DE_NULL,
1346 		0u,
1347 	};
1348 
1349 	// create event that will be used to check if secondary command buffer has been executed
1350 	const Unique<VkEvent>					event					(createEvent(vk, vkDevice, &eventCreateInfo));
1351 
1352 	// record primary command buffer
1353 	VK_CHECK(vk.beginCommandBuffer(*secCmdBuf, &secCmdBufBeginInfo));
1354 	{
1355 		// record setting event
1356 		vk.cmdSetEvent(*secCmdBuf, *event, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
1357 	}
1358 	VK_CHECK(vk.endCommandBuffer(*secCmdBuf));
1359 
1360 	return tcu::TestStatus::pass("Secondary buffer recorded successfully.");
1361 }
1362 
recordLargeSecondaryBufferTest(Context & context)1363 tcu::TestStatus recordLargeSecondaryBufferTest(Context &context)
1364 {
1365 
1366 	const VkDevice							vkDevice				= context.getDevice();
1367 	const DeviceInterface&					vk						= context.getDeviceInterface();
1368 	const VkQueue							queue					= context.getUniversalQueue();
1369 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
1370 
1371 	const VkCommandPoolCreateInfo			cmdPoolParams			=
1372 	{
1373 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					//	VkStructureType				sType;
1374 		DE_NULL,													//	const void*					pNext;
1375 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			//	VkCommandPoolCreateFlags	flags;
1376 		queueFamilyIndex,											//	deUint32					queueFamilyIndex;
1377 	};
1378 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
1379 
1380 	// Command buffer
1381 	const VkCommandBufferAllocateInfo		cmdBufParams			=
1382 	{
1383 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				//	VkStructureType				sType;
1384 		DE_NULL,													//	const void*					pNext;
1385 		*cmdPool,													//	VkCommandPool				pool;
1386 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							//	VkCommandBufferLevel		level;
1387 		1u,															//	uint32_t					bufferCount;
1388 	};
1389 	const Unique<VkCommandBuffer>			primCmdBuf				(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
1390 	const VkCommandBufferBeginInfo			primCmdBufBeginInfo		=
1391 	{
1392 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1393 		DE_NULL,
1394 		0,															// flags
1395 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
1396 	};
1397 
1398 	// Fill create info struct for event
1399 	const VkEventCreateInfo					eventCreateInfo			=
1400 	{
1401 		VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,
1402 		DE_NULL,
1403 		0u,
1404 	};
1405 
1406 	// create event that will be used to check if secondary command buffer has been executed
1407 	const Unique<VkEvent>					event					(createEvent(vk, vkDevice, &eventCreateInfo));
1408 
1409 	// reset event
1410 	VK_CHECK(vk.resetEvent(vkDevice, *event));
1411 
1412 	// record primary command buffer
1413 	VK_CHECK(vk.beginCommandBuffer(*primCmdBuf, &primCmdBufBeginInfo));
1414 	{
1415 		// allow execution of event during every stage of pipeline
1416 		VkPipelineStageFlags stageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1417 
1418 		// define minimal amount of commands to accept
1419 		const long long unsigned minNumCommands = 10000llu;
1420 
1421 		for ( long long unsigned currentCommands = 0; currentCommands < minNumCommands / 2; ++currentCommands )
1422 		{
1423 			// record setting event
1424 			vk.cmdSetEvent(*primCmdBuf, *event,stageMask);
1425 
1426 			// record resetting event
1427 			vk.cmdResetEvent(*primCmdBuf, *event,stageMask);
1428 		};
1429 
1430 
1431 	}
1432 	VK_CHECK(vk.endCommandBuffer(*primCmdBuf));
1433 
1434 	const VkFenceCreateInfo					fenceCreateInfo			=
1435 	{
1436 		VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
1437 		DE_NULL,
1438 		0u,															// flags
1439 	};
1440 
1441 	// create fence to wait for execution of queue
1442 	const Unique<VkFence>					fence					(createFence(vk, vkDevice, &fenceCreateInfo));
1443 
1444 	const VkSubmitInfo						submitInfo				=
1445 	{
1446 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
1447 		DE_NULL,													// pNext
1448 		0u,															// waitSemaphoreCount
1449 		DE_NULL,													// pWaitSemaphores
1450 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
1451 		1,															// commandBufferCount
1452 		&primCmdBuf.get(),											// pCommandBuffers
1453 		0u,															// signalSemaphoreCount
1454 		DE_NULL,													// pSignalSemaphores
1455 	};
1456 
1457 	// Submit the command buffer to the queue
1458 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, *fence));
1459 
1460 	// wait for end of execution of queue
1461 	VK_CHECK(vk.waitForFences(vkDevice, 1, &fence.get(), 0u, INFINITE_TIMEOUT));
1462 
1463 	return tcu::TestStatus::pass("hugeTest succeeded");
1464 }
1465 
submitPrimaryBufferTwiceTest(Context & context)1466 tcu::TestStatus submitPrimaryBufferTwiceTest(Context& context)
1467 {
1468 
1469 	const VkDevice							vkDevice				= context.getDevice();
1470 	const DeviceInterface&					vk						= context.getDeviceInterface();
1471 	const VkQueue							queue					= context.getUniversalQueue();
1472 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
1473 
1474 	const VkCommandPoolCreateInfo			cmdPoolParams			=
1475 	{
1476 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					//	VkStructureType				sType;
1477 		DE_NULL,													//	const void*					pNext;
1478 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			//	VkCommandPoolCreateFlags	flags;
1479 		queueFamilyIndex,											//	deUint32					queueFamilyIndex;
1480 	};
1481 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
1482 
1483 	// Command buffer
1484 	const VkCommandBufferAllocateInfo		cmdBufParams			=
1485 	{
1486 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				//	VkStructureType			sType;
1487 		DE_NULL,													//	const void*				pNext;
1488 		*cmdPool,													//	VkCommandPool				pool;
1489 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							//	VkCommandBufferLevel		level;
1490 		1u,															//	uint32_t					bufferCount;
1491 	};
1492 	const Unique<VkCommandBuffer>			primCmdBuf				(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
1493 	const VkCommandBufferBeginInfo			primCmdBufBeginInfo		=
1494 	{
1495 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1496 		DE_NULL,
1497 		0,															// flags
1498 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
1499 	};
1500 
1501 	// Fill create info struct for event
1502 	const VkEventCreateInfo					eventCreateInfo			=
1503 	{
1504 		VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,
1505 		DE_NULL,
1506 		0u,
1507 	};
1508 
1509 	// create event that will be used to check if secondary command buffer has been executed
1510 	const Unique<VkEvent>					event					(createEvent(vk, vkDevice, &eventCreateInfo));
1511 
1512 	// reset event
1513 	VK_CHECK(vk.resetEvent(vkDevice, *event));
1514 
1515 	// record primary command buffer
1516 	VK_CHECK(vk.beginCommandBuffer(*primCmdBuf, &primCmdBufBeginInfo));
1517 	{
1518 		// allow execution of event during every stage of pipeline
1519 		VkPipelineStageFlags stageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1520 
1521 		// record setting event
1522 		vk.cmdSetEvent(*primCmdBuf, *event,stageMask);
1523 	}
1524 	VK_CHECK(vk.endCommandBuffer(*primCmdBuf));
1525 
1526 	const VkFenceCreateInfo					fenceCreateInfo			=
1527 	{
1528 		VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
1529 		DE_NULL,
1530 		0u,															// flags
1531 	};
1532 
1533 	// create fence to wait for execution of queue
1534 	const Unique<VkFence>					fence					(createFence(vk, vkDevice, &fenceCreateInfo));
1535 
1536 	const VkSubmitInfo						submitInfo				=
1537 	{
1538 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
1539 		DE_NULL,													// pNext
1540 		0u,															// waitSemaphoreCount
1541 		DE_NULL,													// pWaitSemaphores
1542 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
1543 		1,															// commandBufferCount
1544 		&primCmdBuf.get(),											// pCommandBuffers
1545 		0u,															// signalSemaphoreCount
1546 		DE_NULL,													// pSignalSemaphores
1547 	};
1548 
1549 	// submit primary buffer
1550 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, *fence));
1551 
1552 	// wait for end of execution of queue
1553 	VK_CHECK(vk.waitForFences(vkDevice, 1, &fence.get(), 0u, INFINITE_TIMEOUT));
1554 	VK_CHECK(vk.resetFences(vkDevice, 1u, &fence.get()));
1555 	// check if buffer has been executed
1556 	VkResult result = vk.getEventStatus(vkDevice,*event);
1557 	if (result != VK_EVENT_SET)
1558 		return tcu::TestStatus::fail("Submit Twice Test FAILED");
1559 
1560 	// reset event
1561 	VK_CHECK(vk.resetEvent(vkDevice, *event));
1562 
1563 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, *fence));
1564 
1565 	// wait for end of execution of queue
1566 	VK_CHECK(vk.waitForFences(vkDevice, 1, &fence.get(), 0u, INFINITE_TIMEOUT));
1567 
1568 	// check if buffer has been executed
1569 	result = vk.getEventStatus(vkDevice,*event);
1570 	if (result != VK_EVENT_SET)
1571 		return tcu::TestStatus::fail("Submit Twice Test FAILED");
1572 	else
1573 		return tcu::TestStatus::pass("Submit Twice Test succeeded");
1574 }
1575 
submitSecondaryBufferTwiceTest(Context & context)1576 tcu::TestStatus submitSecondaryBufferTwiceTest(Context& context)
1577 {
1578 
1579 	const VkDevice							vkDevice				= context.getDevice();
1580 	const DeviceInterface&					vk						= context.getDeviceInterface();
1581 	const VkQueue							queue					= context.getUniversalQueue();
1582 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
1583 
1584 	const VkCommandPoolCreateInfo			cmdPoolParams			=
1585 	{
1586 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					//	VkStructureType				sType;
1587 		DE_NULL,													//	const void*					pNext;
1588 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			//	VkCommandPoolCreateFlags	flags;
1589 		queueFamilyIndex,											//	deUint32					queueFamilyIndex;
1590 	};
1591 
1592 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
1593 
1594 	// Command buffer
1595 	const VkCommandBufferAllocateInfo		cmdBufParams			=
1596 	{
1597 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				//	VkStructureType			sType;
1598 		DE_NULL,													//	const void*				pNext;
1599 		*cmdPool,													//	VkCommandPool				pool;
1600 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							//	VkCommandBufferLevel		level;
1601 		1u,															//	uint32_t					bufferCount;
1602 	};
1603 
1604 	const Unique<VkCommandBuffer>			primCmdBuf1				(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
1605 	const Unique<VkCommandBuffer>			primCmdBuf2				(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
1606 
1607 	// Secondary Command buffer
1608 	const VkCommandBufferAllocateInfo		secCmdBufParams			=
1609 	{
1610 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				//	VkStructureType			sType;
1611 		DE_NULL,													//	const void*				pNext;
1612 		*cmdPool,													//	VkCommandPool				pool;
1613 		VK_COMMAND_BUFFER_LEVEL_SECONDARY,							//	VkCommandBufferLevel		level;
1614 		1u,															//	uint32_t					bufferCount;
1615 	};
1616 	const Unique<VkCommandBuffer>			secCmdBuf				(allocateCommandBuffer(vk, vkDevice, &secCmdBufParams));
1617 
1618 	const VkCommandBufferBeginInfo			primCmdBufBeginInfo		=
1619 	{
1620 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1621 		DE_NULL,
1622 		0,															// flags
1623 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
1624 	};
1625 
1626 	const VkCommandBufferInheritanceInfo	secCmdBufInheritInfo	=
1627 	{
1628 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
1629 		DE_NULL,
1630 		(VkRenderPass)0u,											// renderPass
1631 		0u,															// subpass
1632 		(VkFramebuffer)0u,											// framebuffer
1633 		VK_FALSE,													// occlusionQueryEnable
1634 		(VkQueryControlFlags)0u,									// queryFlags
1635 		(VkQueryPipelineStatisticFlags)0u,							// pipelineStatistics
1636 	};
1637 	const VkCommandBufferBeginInfo			secCmdBufBeginInfo		=
1638 	{
1639 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1640 		DE_NULL,
1641 		0u,															// flags
1642 		&secCmdBufInheritInfo,
1643 	};
1644 
1645 	// Fill create info struct for event
1646 	const VkEventCreateInfo					eventCreateInfo			=
1647 	{
1648 		VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,
1649 		DE_NULL,
1650 		0u,
1651 	};
1652 
1653 	// create event that will be used to check if secondary command buffer has been executed
1654 	const Unique<VkEvent>					event					(createEvent(vk, vkDevice, &eventCreateInfo));
1655 
1656 	// reset event
1657 	VK_CHECK(vk.resetEvent(vkDevice, *event));
1658 
1659 	// record first primary command buffer
1660 	VK_CHECK(vk.beginCommandBuffer(*primCmdBuf1, &primCmdBufBeginInfo));
1661 	{
1662 		// record secondary command buffer
1663 		VK_CHECK(vk.beginCommandBuffer(*secCmdBuf, &secCmdBufBeginInfo));
1664 		{
1665 			// allow execution of event during every stage of pipeline
1666 			VkPipelineStageFlags stageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1667 
1668 			// record setting event
1669 			vk.cmdSetEvent(*secCmdBuf, *event,stageMask);
1670 		}
1671 
1672 		// end recording of secondary buffers
1673 		VK_CHECK(vk.endCommandBuffer(*secCmdBuf));
1674 
1675 		// execute secondary buffer
1676 		vk.cmdExecuteCommands(*primCmdBuf1, 1, &secCmdBuf.get());
1677 	}
1678 	VK_CHECK(vk.endCommandBuffer(*primCmdBuf1));
1679 
1680 	const VkFenceCreateInfo					fenceCreateInfo			=
1681 	{
1682 		VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
1683 		DE_NULL,
1684 		0u,															// flags
1685 	};
1686 
1687 	// create fence to wait for execution of queue
1688 	const Unique<VkFence>					fence					(createFence(vk, vkDevice, &fenceCreateInfo));
1689 
1690 	const VkSubmitInfo						submitInfo1				=
1691 	{
1692 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
1693 		DE_NULL,													// pNext
1694 		0u,															// waitSemaphoreCount
1695 		DE_NULL,													// pWaitSemaphores
1696 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
1697 		1,															// commandBufferCount
1698 		&primCmdBuf1.get(),											// pCommandBuffers
1699 		0u,															// signalSemaphoreCount
1700 		DE_NULL,													// pSignalSemaphores
1701 	};
1702 
1703 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo1, *fence));
1704 
1705 	// wait for end of execution of queue
1706 	VK_CHECK(vk.waitForFences(vkDevice, 1, &fence.get(), 0u, INFINITE_TIMEOUT));
1707 	VK_CHECK(vk.resetFences(vkDevice, 1u, &fence.get()));
1708 
1709 	// check if secondary buffer has been executed
1710 	VkResult result = vk.getEventStatus(vkDevice,*event);
1711 	if (result != VK_EVENT_SET)
1712 		return tcu::TestStatus::fail("Submit Twice Secondary Command Buffer FAILED");
1713 
1714 	// reset first primary buffer
1715 	vk.resetCommandBuffer( *primCmdBuf1, 0u);
1716 
1717 	// reset event to allow receiving it again
1718 	VK_CHECK(vk.resetEvent(vkDevice, *event));
1719 
1720 	// record second primary command buffer
1721 	VK_CHECK(vk.beginCommandBuffer(*primCmdBuf2, &primCmdBufBeginInfo));
1722 	{
1723 		// execute secondary buffer
1724 		vk.cmdExecuteCommands(*primCmdBuf2, 1, &secCmdBuf.get());
1725 	}
1726 	// end recording
1727 	VK_CHECK(vk.endCommandBuffer(*primCmdBuf2));
1728 
1729 	// submit second primary buffer, the secondary should be executed too
1730 	const VkSubmitInfo						submitInfo2				=
1731 	{
1732 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
1733 		DE_NULL,													// pNext
1734 		0u,															// waitSemaphoreCount
1735 		DE_NULL,													// pWaitSemaphores
1736 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
1737 		1,															// commandBufferCount
1738 		&primCmdBuf2.get(),											// pCommandBuffers
1739 		0u,															// signalSemaphoreCount
1740 		DE_NULL,													// pSignalSemaphores
1741 	};
1742 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo2, *fence));
1743 
1744 	// wait for end of execution of queue
1745 	VK_CHECK(vk.waitForFences(vkDevice, 1, &fence.get(), 0u, INFINITE_TIMEOUT));
1746 
1747 	// check if secondary buffer has been executed
1748 	result = vk.getEventStatus(vkDevice,*event);
1749 	if (result != VK_EVENT_SET)
1750 		return tcu::TestStatus::fail("Submit Twice Secondary Command Buffer FAILED");
1751 	else
1752 		return tcu::TestStatus::pass("Submit Twice Secondary Command Buffer succeeded");
1753 }
1754 
oneTimeSubmitFlagPrimaryBufferTest(Context & context)1755 tcu::TestStatus oneTimeSubmitFlagPrimaryBufferTest(Context& context)
1756 {
1757 
1758 	const VkDevice							vkDevice				= context.getDevice();
1759 	const DeviceInterface&					vk						= context.getDeviceInterface();
1760 	const VkQueue							queue					= context.getUniversalQueue();
1761 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
1762 
1763 	const VkCommandPoolCreateInfo			cmdPoolParams			=
1764 	{
1765 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					//	VkStructureType				sType;
1766 		DE_NULL,													//	const void*					pNext;
1767 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			//	VkCommandPoolCreateFlags	flags;
1768 		queueFamilyIndex,											//	deUint32					queueFamilyIndex;
1769 	};
1770 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
1771 
1772 	// Command buffer
1773 	const VkCommandBufferAllocateInfo		cmdBufParams			=
1774 	{
1775 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				//	VkStructureType				sType;
1776 		DE_NULL,													//	const void*					pNext;
1777 		*cmdPool,													//	VkCommandPool				pool;
1778 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							//	VkCommandBufferLevel		level;
1779 		1u,															//	uint32_t					bufferCount;
1780 	};
1781 	const Unique<VkCommandBuffer>			primCmdBuf				(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
1782 	const VkCommandBufferBeginInfo			primCmdBufBeginInfo		=
1783 	{
1784 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1785 		DE_NULL,
1786 		VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,				// flags
1787 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
1788 	};
1789 
1790 	// Fill create info struct for event
1791 	const VkEventCreateInfo					eventCreateInfo			=
1792 	{
1793 		VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,
1794 		DE_NULL,
1795 		0u,
1796 	};
1797 
1798 	// create event that will be used to check if secondary command buffer has been executed
1799 	const Unique<VkEvent>					event					(createEvent(vk, vkDevice, &eventCreateInfo));
1800 
1801 	// reset event
1802 	VK_CHECK(vk.resetEvent(vkDevice, *event));
1803 
1804 	// record primary command buffer
1805 	VK_CHECK(vk.beginCommandBuffer(*primCmdBuf, &primCmdBufBeginInfo));
1806 	{
1807 		// allow execution of event during every stage of pipeline
1808 		VkPipelineStageFlags stageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1809 
1810 		// record setting event
1811 		vk.cmdSetEvent(*primCmdBuf, *event,stageMask);
1812 	}
1813 	VK_CHECK(vk.endCommandBuffer(*primCmdBuf));
1814 
1815 	const VkFenceCreateInfo					fenceCreateInfo			=
1816 	{
1817 		VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
1818 		DE_NULL,
1819 		0u,															// flags
1820 	};
1821 
1822 	// create fence to wait for execution of queue
1823 	const Unique<VkFence>					fence					(createFence(vk, vkDevice, &fenceCreateInfo));
1824 
1825 	const VkSubmitInfo						submitInfo				=
1826 	{
1827 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
1828 		DE_NULL,													// pNext
1829 		0u,															// waitSemaphoreCount
1830 		DE_NULL,													// pWaitSemaphores
1831 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
1832 		1,															// commandBufferCount
1833 		&primCmdBuf.get(),											// pCommandBuffers
1834 		0u,															// signalSemaphoreCount
1835 		DE_NULL,													// pSignalSemaphores
1836 	};
1837 
1838 	// submit primary buffer
1839 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, *fence));
1840 
1841 	// wait for end of execution of queue
1842 	VK_CHECK(vk.waitForFences(vkDevice, 1, &fence.get(), 0u, INFINITE_TIMEOUT));
1843 	VK_CHECK(vk.resetFences(vkDevice, 1u, &fence.get()));
1844 
1845 	// check if buffer has been executed
1846 	VkResult result = vk.getEventStatus(vkDevice,*event);
1847 	if (result != VK_EVENT_SET)
1848 		return tcu::TestStatus::fail("oneTimeSubmitFlagPrimaryBufferTest FAILED");
1849 
1850 	// record primary command buffer again - implicit reset because of VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT
1851 	VK_CHECK(vk.beginCommandBuffer(*primCmdBuf, &primCmdBufBeginInfo));
1852 	{
1853 		// allow execution of event during every stage of pipeline
1854 		VkPipelineStageFlags stageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1855 
1856 		// record setting event
1857 		vk.cmdSetEvent(*primCmdBuf, *event,stageMask);
1858 	}
1859 	VK_CHECK(vk.endCommandBuffer(*primCmdBuf));
1860 
1861 	// reset event
1862 	VK_CHECK(vk.resetEvent(vkDevice, *event));
1863 
1864 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, *fence));
1865 
1866 	// wait for end of execution of queue
1867 	VK_CHECK(vk.waitForFences(vkDevice, 1, &fence.get(), 0u, INFINITE_TIMEOUT));
1868 
1869 	// check if buffer has been executed
1870 	result = vk.getEventStatus(vkDevice,*event);
1871 	if (result != VK_EVENT_SET)
1872 		return tcu::TestStatus::fail("oneTimeSubmitFlagPrimaryBufferTest FAILED");
1873 	else
1874 		return tcu::TestStatus::pass("oneTimeSubmitFlagPrimaryBufferTest succeeded");
1875 }
1876 
oneTimeSubmitFlagSecondaryBufferTest(Context & context)1877 tcu::TestStatus oneTimeSubmitFlagSecondaryBufferTest(Context& context)
1878 {
1879 
1880 	const VkDevice							vkDevice				= context.getDevice();
1881 	const DeviceInterface&					vk						= context.getDeviceInterface();
1882 	const VkQueue							queue					= context.getUniversalQueue();
1883 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
1884 
1885 	const VkCommandPoolCreateInfo			cmdPoolParams			=
1886 	{
1887 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					//	VkStructureType				sType;
1888 		DE_NULL,													//	const void*					pNext;
1889 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			//	VkCommandPoolCreateFlags	flags;
1890 		queueFamilyIndex,											//	deUint32					queueFamilyIndex;
1891 	};
1892 
1893 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
1894 
1895 	// Command buffer
1896 	const VkCommandBufferAllocateInfo		cmdBufParams			=
1897 	{
1898 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				//	VkStructureType			sType;
1899 		DE_NULL,													//	const void*				pNext;
1900 		*cmdPool,													//	VkCommandPool				pool;
1901 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							//	VkCommandBufferLevel		level;
1902 		1u,															//	uint32_t					bufferCount;
1903 	};
1904 
1905 	const Unique<VkCommandBuffer>			primCmdBuf1				(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
1906 	const Unique<VkCommandBuffer>			primCmdBuf2				(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
1907 
1908 	// Secondary Command buffer
1909 	const VkCommandBufferAllocateInfo		secCmdBufParams			=
1910 	{
1911 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				//	VkStructureType			sType;
1912 		DE_NULL,													//	const void*				pNext;
1913 		*cmdPool,													//	VkCommandPool				pool;
1914 		VK_COMMAND_BUFFER_LEVEL_SECONDARY,							//	VkCommandBufferLevel		level;
1915 		1u,															//	uint32_t					bufferCount;
1916 	};
1917 	const Unique<VkCommandBuffer>			secCmdBuf				(allocateCommandBuffer(vk, vkDevice, &secCmdBufParams));
1918 
1919 	const VkCommandBufferBeginInfo			primCmdBufBeginInfo		=
1920 	{
1921 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1922 		DE_NULL,
1923 		0,															// flags
1924 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
1925 	};
1926 
1927 	const VkCommandBufferInheritanceInfo	secCmdBufInheritInfo	=
1928 	{
1929 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
1930 		DE_NULL,
1931 		(VkRenderPass)0u,											// renderPass
1932 		0u,															// subpass
1933 		(VkFramebuffer)0u,											// framebuffer
1934 		VK_FALSE,													// occlusionQueryEnable
1935 		(VkQueryControlFlags)0u,									// queryFlags
1936 		(VkQueryPipelineStatisticFlags)0u,							// pipelineStatistics
1937 	};
1938 	const VkCommandBufferBeginInfo			secCmdBufBeginInfo		=
1939 	{
1940 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1941 		DE_NULL,
1942 		VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,				// flags
1943 		&secCmdBufInheritInfo,
1944 	};
1945 
1946 	// Fill create info struct for event
1947 	const VkEventCreateInfo					eventCreateInfo			=
1948 	{
1949 		VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,
1950 		DE_NULL,
1951 		0u,
1952 	};
1953 
1954 	// create event that will be used to check if secondary command buffer has been executed
1955 	const Unique<VkEvent>					event					(createEvent(vk, vkDevice, &eventCreateInfo));
1956 
1957 	// reset event
1958 	VK_CHECK(vk.resetEvent(vkDevice, *event));
1959 
1960 	// record first primary command buffer
1961 	VK_CHECK(vk.beginCommandBuffer(*primCmdBuf1, &primCmdBufBeginInfo));
1962 	{
1963 		// record secondary command buffer
1964 		VK_CHECK(vk.beginCommandBuffer(*secCmdBuf, &secCmdBufBeginInfo));
1965 		{
1966 			// allow execution of event during every stage of pipeline
1967 			VkPipelineStageFlags stageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1968 
1969 			// record setting event
1970 			vk.cmdSetEvent(*secCmdBuf, *event,stageMask);
1971 		}
1972 
1973 		// end recording of secondary buffers
1974 		VK_CHECK(vk.endCommandBuffer(*secCmdBuf));
1975 
1976 		// execute secondary buffer
1977 		vk.cmdExecuteCommands(*primCmdBuf1, 1, &secCmdBuf.get());
1978 	}
1979 	VK_CHECK(vk.endCommandBuffer(*primCmdBuf1));
1980 
1981 	const VkFenceCreateInfo					fenceCreateInfo			=
1982 	{
1983 		VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
1984 		DE_NULL,
1985 		0u,															// flags
1986 	};
1987 
1988 	// create fence to wait for execution of queue
1989 	const Unique<VkFence>					fence					(createFence(vk, vkDevice, &fenceCreateInfo));
1990 
1991 	const VkSubmitInfo						submitInfo1				=
1992 	{
1993 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
1994 		DE_NULL,													// pNext
1995 		0u,															// waitSemaphoreCount
1996 		DE_NULL,													// pWaitSemaphores
1997 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
1998 		1,															// commandBufferCount
1999 		&primCmdBuf1.get(),											// pCommandBuffers
2000 		0u,															// signalSemaphoreCount
2001 		DE_NULL,													// pSignalSemaphores
2002 	};
2003 
2004 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo1, *fence));
2005 
2006 	// wait for end of execution of queue
2007 	VK_CHECK(vk.waitForFences(vkDevice, 1, &fence.get(), 0u, INFINITE_TIMEOUT));
2008 	VK_CHECK(vk.resetFences(vkDevice, 1u, &fence.get()));
2009 
2010 	// check if secondary buffer has been executed
2011 	VkResult result = vk.getEventStatus(vkDevice,*event);
2012 	if (result != VK_EVENT_SET)
2013 		return tcu::TestStatus::fail("Submit Twice Secondary Command Buffer FAILED");
2014 
2015 	// reset first primary buffer
2016 	vk.resetCommandBuffer( *primCmdBuf1, 0u);
2017 
2018 	// reset event to allow receiving it again
2019 	VK_CHECK(vk.resetEvent(vkDevice, *event));
2020 
2021 	// record secondary command buffer again
2022 	VK_CHECK(vk.beginCommandBuffer(*secCmdBuf, &secCmdBufBeginInfo));
2023 	{
2024 		// allow execution of event during every stage of pipeline
2025 		VkPipelineStageFlags stageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
2026 
2027 		// record setting event
2028 		vk.cmdSetEvent(*secCmdBuf, *event,stageMask);
2029 	}
2030 	// end recording of secondary buffers
2031 	VK_CHECK(vk.endCommandBuffer(*secCmdBuf));
2032 
2033 	// record second primary command buffer
2034 	VK_CHECK(vk.beginCommandBuffer(*primCmdBuf2, &primCmdBufBeginInfo));
2035 	{
2036 		// execute secondary buffer
2037 		vk.cmdExecuteCommands(*primCmdBuf2, 1, &secCmdBuf.get());
2038 	}
2039 	// end recording
2040 	VK_CHECK(vk.endCommandBuffer(*primCmdBuf2));
2041 
2042 	// submit second primary buffer, the secondary should be executed too
2043 	const VkSubmitInfo						submitInfo2				=
2044 	{
2045 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
2046 		DE_NULL,													// pNext
2047 		0u,															// waitSemaphoreCount
2048 		DE_NULL,													// pWaitSemaphores
2049 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
2050 		1,															// commandBufferCount
2051 		&primCmdBuf2.get(),											// pCommandBuffers
2052 		0u,															// signalSemaphoreCount
2053 		DE_NULL,													// pSignalSemaphores
2054 	};
2055 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo2, *fence));
2056 
2057 	// wait for end of execution of queue
2058 	VK_CHECK(vk.waitForFences(vkDevice, 1, &fence.get(), 0u, INFINITE_TIMEOUT));
2059 
2060 	// check if secondary buffer has been executed
2061 	result = vk.getEventStatus(vkDevice,*event);
2062 	if (result != VK_EVENT_SET)
2063 		return tcu::TestStatus::fail("oneTimeSubmitFlagSecondaryBufferTest FAILED");
2064 	else
2065 		return tcu::TestStatus::pass("oneTimeSubmitFlagSecondaryBufferTest succeeded");
2066 }
2067 
renderPassContinueTest(Context & context)2068 tcu::TestStatus renderPassContinueTest(Context& context)
2069 {
2070 	const DeviceInterface&					vkd						= context.getDeviceInterface();
2071 	CommandBufferRenderPassTestEnvironment	env						(context, VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT);
2072 
2073 	VkCommandBuffer							primaryCommandBuffer	= env.getPrimaryCommandBuffer();
2074 	VkCommandBuffer							secondaryCommandBuffer	= env.getSecondaryCommandBuffer();
2075 	const deUint32							clearColor[4]			= { 2, 47, 131, 211 };
2076 
2077 	const VkClearAttachment					clearAttachment			=
2078 	{
2079 		VK_IMAGE_ASPECT_COLOR_BIT,									// VkImageAspectFlags	aspectMask;
2080 		0,															// deUint32				colorAttachment;
2081 		makeClearValueColorU32(clearColor[0],
2082 							   clearColor[1],
2083 							   clearColor[2],
2084 							   clearColor[3])						// VkClearValue			clearValue;
2085 	};
2086 
2087 	const VkClearRect						clearRect				=
2088 	{
2089 		CommandBufferRenderPassTestEnvironment::DEFAULT_IMAGE_AREA,	// VkRect2D	rect;
2090 		0u,															// deUint32	baseArrayLayer;
2091 		1u															// deUint32	layerCount;
2092 	};
2093 
2094 	env.beginSecondaryCommandBuffer(VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT);
2095 	vkd.cmdClearAttachments(secondaryCommandBuffer, 1, &clearAttachment, 1, &clearRect);
2096 	VK_CHECK(vkd.endCommandBuffer(secondaryCommandBuffer));
2097 
2098 
2099 	env.beginPrimaryCommandBuffer(0);
2100 	env.beginRenderPass(VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS);
2101 	vkd.cmdExecuteCommands(primaryCommandBuffer, 1, &secondaryCommandBuffer);
2102 	vkd.cmdEndRenderPass(primaryCommandBuffer);
2103 
2104 	VK_CHECK(vkd.endCommandBuffer(primaryCommandBuffer));
2105 
2106 	env.submitPrimaryCommandBuffer();
2107 
2108 	de::MovePtr<tcu::TextureLevel>			result					= env.readColorAttachment();
2109 	tcu::PixelBufferAccess					pixelBufferAccess		= result->getAccess();
2110 
2111 	for (deUint32 i = 0; i < (CommandBufferRenderPassTestEnvironment::DEFAULT_IMAGE_SIZE.width * CommandBufferRenderPassTestEnvironment::DEFAULT_IMAGE_SIZE.height); ++i)
2112 	{
2113 		deUint8* colorData = reinterpret_cast<deUint8*>(pixelBufferAccess.getDataPtr());
2114 		for (int colorComponent = 0; colorComponent < 4; ++colorComponent)
2115 			if (colorData[i * 4 + colorComponent] != clearColor[colorComponent])
2116 				return tcu::TestStatus::fail("clear value mismatch");
2117 	}
2118 
2119 	return tcu::TestStatus::pass("render pass continue test passed");
2120 }
2121 
simultaneousUsePrimaryBufferTest(Context & context)2122 tcu::TestStatus simultaneousUsePrimaryBufferTest(Context& context)
2123 {
2124 
2125 	const VkDevice							vkDevice				= context.getDevice();
2126 	const DeviceInterface&					vk						= context.getDeviceInterface();
2127 	const VkQueue							queue					= context.getUniversalQueue();
2128 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
2129 
2130 	const VkCommandPoolCreateInfo			cmdPoolParams			=
2131 	{
2132 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					//	VkStructureType				sType;
2133 		DE_NULL,													//	const void*					pNext;
2134 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			//	VkCommandPoolCreateFlags	flags;
2135 		queueFamilyIndex,											//	deUint32					queueFamilyIndex;
2136 	};
2137 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
2138 
2139 	// Command buffer
2140 	const VkCommandBufferAllocateInfo		cmdBufParams			=
2141 	{
2142 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				//	VkStructureType				sType;
2143 		DE_NULL,													//	const void*					pNext;
2144 		*cmdPool,													//	VkCommandPool				pool;
2145 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							//	VkCommandBufferLevel		level;
2146 		1u,															//	uint32_t					bufferCount;
2147 	};
2148 	const Unique<VkCommandBuffer>			primCmdBuf				(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
2149 	const VkCommandBufferBeginInfo			primCmdBufBeginInfo		=
2150 	{
2151 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2152 		DE_NULL,
2153 		VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,				// flags
2154 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
2155 	};
2156 
2157 	// Fill create info struct for event
2158 	const VkEventCreateInfo					eventCreateInfo			=
2159 	{
2160 		VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,
2161 		DE_NULL,
2162 		0u,
2163 	};
2164 
2165 	// create event that will be used to check if secondary command buffer has been executed
2166 	const Unique<VkEvent>					eventOne				(createEvent(vk, vkDevice, &eventCreateInfo));
2167 	const Unique<VkEvent>					eventTwo				(createEvent(vk, vkDevice, &eventCreateInfo));
2168 
2169 	// reset event
2170 	VK_CHECK(vk.resetEvent(vkDevice, *eventOne));
2171 
2172 	// record primary command buffer
2173 	VK_CHECK(vk.beginCommandBuffer(*primCmdBuf, &primCmdBufBeginInfo));
2174 	{
2175 		// allow execution of event during every stage of pipeline
2176 		VkPipelineStageFlags stageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
2177 
2178 		// wait for event
2179 		vk.cmdWaitEvents(*primCmdBuf, 1u, &eventOne.get(), stageMask, stageMask, 0u, DE_NULL, 0u, DE_NULL, 0u, DE_NULL);
2180 
2181 		// Set the second event
2182 		vk.cmdSetEvent(*primCmdBuf, eventTwo.get(), stageMask);
2183 	}
2184 	VK_CHECK(vk.endCommandBuffer(*primCmdBuf));
2185 
2186 	const VkFenceCreateInfo					fenceCreateInfo			=
2187 	{
2188 		VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
2189 		DE_NULL,
2190 		0u,															// flags
2191 	};
2192 
2193 	// create fence to wait for execution of queue
2194 	const Unique<VkFence>					fence1					(createFence(vk, vkDevice, &fenceCreateInfo));
2195 	const Unique<VkFence>					fence2					(createFence(vk, vkDevice, &fenceCreateInfo));
2196 
2197 
2198 	const VkSubmitInfo						submitInfo				=
2199 	{
2200 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
2201 		DE_NULL,													// pNext
2202 		0u,															// waitSemaphoreCount
2203 		DE_NULL,													// pWaitSemaphores
2204 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
2205 		1,															// commandBufferCount
2206 		&primCmdBuf.get(),											// pCommandBuffers
2207 		0u,															// signalSemaphoreCount
2208 		DE_NULL,													// pSignalSemaphores
2209 	};
2210 
2211 	// submit first buffer
2212 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, *fence1));
2213 
2214 	// submit second buffer
2215 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, *fence2));
2216 
2217 	// wait for both buffer to stop at event for 100 microseconds
2218 	vk.waitForFences(vkDevice, 1, &fence1.get(), 0u, 100000);
2219 	vk.waitForFences(vkDevice, 1, &fence2.get(), 0u, 100000);
2220 
2221 	// set event
2222 	VK_CHECK(vk.setEvent(vkDevice, *eventOne));
2223 
2224 	// wait for end of execution of the first buffer
2225 	VK_CHECK(vk.waitForFences(vkDevice, 1, &fence1.get(), 0u, INFINITE_TIMEOUT));
2226 	// wait for end of execution of the second buffer
2227 	VK_CHECK(vk.waitForFences(vkDevice, 1, &fence2.get(), 0u, INFINITE_TIMEOUT));
2228 
2229 	// TODO: this will be true if the command buffer was executed only once
2230 	// TODO: add some test that will say if it was executed twice
2231 
2232 	// check if buffer has been executed
2233 	VkResult result = vk.getEventStatus(vkDevice, *eventTwo);
2234 	if (result == VK_EVENT_SET)
2235 		return tcu::TestStatus::pass("simultaneous use - primary buffers test succeeded");
2236 	else
2237 		return tcu::TestStatus::fail("simultaneous use - primary buffers test FAILED");
2238 }
2239 
simultaneousUseSecondaryBufferTest(Context & context)2240 tcu::TestStatus simultaneousUseSecondaryBufferTest(Context& context)
2241 {
2242 	const VkDevice							vkDevice				= context.getDevice();
2243 	const DeviceInterface&					vk						= context.getDeviceInterface();
2244 	const VkQueue							queue					= context.getUniversalQueue();
2245 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
2246 
2247 	const VkCommandPoolCreateInfo			cmdPoolParams			=
2248 	{
2249 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					//	VkStructureType				sType;
2250 		DE_NULL,													//	const void*					pNext;
2251 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			//	VkCommandPoolCreateFlags	flags;
2252 		queueFamilyIndex,											//	deUint32					queueFamilyIndex;
2253 	};
2254 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
2255 
2256 	// Command buffer
2257 	const VkCommandBufferAllocateInfo		cmdBufParams			=
2258 	{
2259 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				//	VkStructureType			sType;
2260 		DE_NULL,													//	const void*				pNext;
2261 		*cmdPool,													//	VkCommandPool				pool;
2262 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							//	VkCommandBufferLevel		level;
2263 		1u,															//	uint32_t					bufferCount;
2264 	};
2265 	const Unique<VkCommandBuffer>			primCmdBuf				(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
2266 
2267 	// Secondary Command buffer params
2268 	const VkCommandBufferAllocateInfo		secCmdBufParams			=
2269 	{
2270 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				//	VkStructureType			sType;
2271 		DE_NULL,													//	const void*				pNext;
2272 		*cmdPool,													//	VkCommandPool				pool;
2273 		VK_COMMAND_BUFFER_LEVEL_SECONDARY,							//	VkCommandBufferLevel		level;
2274 		1u,															//	uint32_t					bufferCount;
2275 	};
2276 	const Unique<VkCommandBuffer>			secCmdBuf				(allocateCommandBuffer(vk, vkDevice, &secCmdBufParams));
2277 
2278 	const VkCommandBufferBeginInfo			primCmdBufBeginInfo		=
2279 	{
2280 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2281 		DE_NULL,
2282 		0,															// flags
2283 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
2284 	};
2285 
2286 	const VkCommandBufferInheritanceInfo	secCmdBufInheritInfo	=
2287 	{
2288 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
2289 		DE_NULL,
2290 		(VkRenderPass)0u,											// renderPass
2291 		0u,															// subpass
2292 		(VkFramebuffer)0u,											// framebuffer
2293 		VK_FALSE,													// occlusionQueryEnable
2294 		(VkQueryControlFlags)0u,									// queryFlags
2295 		(VkQueryPipelineStatisticFlags)0u,							// pipelineStatistics
2296 	};
2297 	const VkCommandBufferBeginInfo			secCmdBufBeginInfo		=
2298 	{
2299 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2300 		DE_NULL,
2301 		VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,				// flags
2302 		&secCmdBufInheritInfo,
2303 	};
2304 
2305 	// Fill create info struct for event
2306 	const VkEventCreateInfo					eventCreateInfo			=
2307 	{
2308 		VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,
2309 		DE_NULL,
2310 		0u,
2311 	};
2312 
2313 	// create event that will be used to check if secondary command buffer has been executed
2314 	const Unique<VkEvent>					eventOne				(createEvent(vk, vkDevice, &eventCreateInfo));
2315 	const Unique<VkEvent>					eventTwo				(createEvent(vk, vkDevice, &eventCreateInfo));
2316 
2317 	// reset event
2318 	VK_CHECK(vk.resetEvent(vkDevice, *eventOne));
2319 	VK_CHECK(vk.resetEvent(vkDevice, *eventTwo));
2320 
2321 	// record secondary command buffer
2322 	VK_CHECK(vk.beginCommandBuffer(*secCmdBuf, &secCmdBufBeginInfo));
2323 	{
2324 		// allow execution of event during every stage of pipeline
2325 		VkPipelineStageFlags stageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
2326 
2327 		// wait for event
2328 		vk.cmdWaitEvents(*secCmdBuf, 1, &eventOne.get(), stageMask, stageMask, 0, DE_NULL, 0u, DE_NULL, 0u, DE_NULL);
2329 
2330 		// reset event
2331 		vk.cmdSetEvent(*secCmdBuf, *eventTwo, stageMask);
2332 	}
2333 	// end recording of secondary buffers
2334 	VK_CHECK(vk.endCommandBuffer(*secCmdBuf));
2335 
2336 	// record primary command buffer
2337 	VK_CHECK(vk.beginCommandBuffer(*primCmdBuf, &primCmdBufBeginInfo));
2338 	{
2339 		// execute secondary buffer
2340 		vk.cmdExecuteCommands(*primCmdBuf, 1, &secCmdBuf.get());
2341 	}
2342 	VK_CHECK(vk.endCommandBuffer(*primCmdBuf));
2343 
2344 	const VkFenceCreateInfo					fenceCreateInfo			=
2345 	{
2346 		VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
2347 		DE_NULL,
2348 		0u,															// flags
2349 	};
2350 
2351 	// create fence to wait for execution of queue
2352 	const Unique<VkFence>					fence					(createFence(vk, vkDevice, &fenceCreateInfo));
2353 
2354 	const VkSubmitInfo						submitInfo				=
2355 	{
2356 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
2357 		DE_NULL,													// pNext
2358 		0u,															// waitSemaphoreCount
2359 		DE_NULL,													// pWaitSemaphores
2360 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
2361 		1,															// commandBufferCount
2362 		&primCmdBuf.get(),											// pCommandBuffers
2363 		0u,															// signalSemaphoreCount
2364 		DE_NULL,													// pSignalSemaphores
2365 	};
2366 
2367 	// submit primary buffer, the secondary should be executed too
2368 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, *fence));
2369 
2370 	// wait for both buffers to stop at event for 100 microseconds
2371 	vk.waitForFences(vkDevice, 1, &fence.get(), 0u, 100000);
2372 
2373 	// set event
2374 	VK_CHECK(vk.setEvent(vkDevice, *eventOne));
2375 
2376 	// wait for end of execution of queue
2377 	VK_CHECK(vk.waitForFences(vkDevice, 1, &fence.get(), 0u, INFINITE_TIMEOUT));
2378 
2379 	// TODO: this will be true if the command buffer was executed only once
2380 	// TODO: add some test that will say if it was executed twice
2381 
2382 	// check if secondary buffer has been executed
2383 	VkResult result = vk.getEventStatus(vkDevice,*eventTwo);
2384 	if (result == VK_EVENT_SET)
2385 		return tcu::TestStatus::pass("Simulatous Secondary Command Buffer Execution succeeded");
2386 	else
2387 		return tcu::TestStatus::fail("Simulatous Secondary Command Buffer Execution FAILED");
2388 }
2389 
recordBufferQueryPreciseWithFlagTest(Context & context)2390 tcu::TestStatus recordBufferQueryPreciseWithFlagTest(Context& context)
2391 {
2392 	const VkDevice							vkDevice				= context.getDevice();
2393 	const DeviceInterface&					vk						= context.getDeviceInterface();
2394 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
2395 
2396 	if (!context.getDeviceFeatures().inheritedQueries)
2397 		TCU_THROW(NotSupportedError, "Inherited queries feature is not supported");
2398 
2399 	const VkCommandPoolCreateInfo			cmdPoolParams			=
2400 	{
2401 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// sType;
2402 		DE_NULL,													// pNext;
2403 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			// flags;
2404 		queueFamilyIndex,											// queueFamilyIndex;
2405 	};
2406 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
2407 
2408 	// Command buffer
2409 	const VkCommandBufferAllocateInfo		primCmdBufParams		=
2410 	{
2411 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// sType;
2412 		DE_NULL,													// pNext;
2413 		*cmdPool,													// pool;
2414 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							// level;
2415 		1u,															// flags;
2416 	};
2417 	const Unique<VkCommandBuffer>			primCmdBuf				(allocateCommandBuffer(vk, vkDevice, &primCmdBufParams));
2418 
2419 	// Secondary Command buffer params
2420 	const VkCommandBufferAllocateInfo		secCmdBufParams			=
2421 	{
2422 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// sType;
2423 		DE_NULL,													// pNext;
2424 		*cmdPool,													// pool;
2425 		VK_COMMAND_BUFFER_LEVEL_SECONDARY,							// level;
2426 		1u,															// flags;
2427 	};
2428 	const Unique<VkCommandBuffer>			secCmdBuf				(allocateCommandBuffer(vk, vkDevice, &secCmdBufParams));
2429 
2430 	const VkCommandBufferBeginInfo			primBufferBeginInfo		=
2431 	{
2432 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,				// sType
2433 		DE_NULL,													// pNext
2434 		0u,															// flags
2435 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
2436 	};
2437 
2438 	const VkCommandBufferInheritanceInfo	secBufferInheritInfo	=
2439 	{
2440 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
2441 		DE_NULL,
2442 		0u,															// renderPass
2443 		0u,															// subpass
2444 		0u,															// framebuffer
2445 		VK_TRUE,													// occlusionQueryEnable
2446 		VK_QUERY_CONTROL_PRECISE_BIT,								// queryFlags
2447 		(VkQueryPipelineStatisticFlags)0u,							// pipelineStatistics
2448 	};
2449 	const VkCommandBufferBeginInfo			secBufferBeginInfo		=
2450 	{
2451 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,				// sType
2452 		DE_NULL,													// pNext
2453 		0u,															// flags
2454 		&secBufferInheritInfo,
2455 	};
2456 
2457 	// Create an occlusion query with VK_QUERY_CONTROL_PRECISE_BIT set
2458 	const VkQueryPoolCreateInfo				queryPoolCreateInfo		=
2459 	{
2460 		VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO,					// sType
2461 		DE_NULL,													// pNext
2462 		VK_QUERY_CONTROL_PRECISE_BIT,								// flags
2463 		VK_QUERY_TYPE_OCCLUSION,									// queryType
2464 		1u,															// entryCount
2465 		0u,															// pipelineStatistics
2466 	};
2467 	Unique<VkQueryPool>						queryPool				(createQueryPool(vk, vkDevice, &queryPoolCreateInfo));
2468 
2469 	VK_CHECK(vk.beginCommandBuffer(secCmdBuf.get(), &secBufferBeginInfo));
2470 	VK_CHECK(vk.endCommandBuffer(secCmdBuf.get()));
2471 
2472 	VK_CHECK(vk.beginCommandBuffer(primCmdBuf.get(), &primBufferBeginInfo));
2473 	{
2474 		vk.cmdResetQueryPool(primCmdBuf.get(), queryPool.get(), 0u, 1u);
2475 		vk.cmdBeginQuery(primCmdBuf.get(), queryPool.get(), 0u, VK_QUERY_CONTROL_PRECISE_BIT);
2476 		{
2477 			vk.cmdExecuteCommands(primCmdBuf.get(), 1u, &secCmdBuf.get());
2478 		}
2479 		vk.cmdEndQuery(primCmdBuf.get(), queryPool.get(), 0u);
2480 	}
2481 	VK_CHECK(vk.endCommandBuffer(primCmdBuf.get()));
2482 
2483 	return tcu::TestStatus::pass("Successfully recorded a secondary command buffer allowing a precise occlusion query.");
2484 }
2485 
recordBufferQueryImpreciseWithFlagTest(Context & context)2486 tcu::TestStatus recordBufferQueryImpreciseWithFlagTest(Context& context)
2487 {
2488 	const VkDevice							vkDevice				= context.getDevice();
2489 	const DeviceInterface&					vk						= context.getDeviceInterface();
2490 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
2491 
2492 	if (!context.getDeviceFeatures().inheritedQueries)
2493 		TCU_THROW(NotSupportedError, "Inherited queries feature is not supported");
2494 
2495 	const VkCommandPoolCreateInfo			cmdPoolParams			=
2496 	{
2497 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// sType;
2498 		DE_NULL,													// pNext;
2499 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			// flags;
2500 		queueFamilyIndex,											// queueFamilyIndex;
2501 	};
2502 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
2503 
2504 	// Command buffer
2505 	const VkCommandBufferAllocateInfo		primCmdBufParams		=
2506 	{
2507 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// sType;
2508 		DE_NULL,													// pNext;
2509 		*cmdPool,													// pool;
2510 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							// level;
2511 		1u,															// flags;
2512 	};
2513 	const Unique<VkCommandBuffer>			primCmdBuf				(allocateCommandBuffer(vk, vkDevice, &primCmdBufParams));
2514 
2515 	// Secondary Command buffer params
2516 	const VkCommandBufferAllocateInfo		secCmdBufParams			=
2517 	{
2518 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// sType;
2519 		DE_NULL,													// pNext;
2520 		*cmdPool,													// pool;
2521 		VK_COMMAND_BUFFER_LEVEL_SECONDARY,							// level;
2522 		1u,															// flags;
2523 	};
2524 	const Unique<VkCommandBuffer>			secCmdBuf				(allocateCommandBuffer(vk, vkDevice, &secCmdBufParams));
2525 
2526 	const VkCommandBufferBeginInfo			primBufferBeginInfo		=
2527 	{
2528 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,				// sType
2529 		DE_NULL,													// pNext
2530 		0u,															// flags
2531 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
2532 	};
2533 
2534 	const VkCommandBufferInheritanceInfo	secBufferInheritInfo	=
2535 	{
2536 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
2537 		DE_NULL,
2538 		0u,															// renderPass
2539 		0u,															// subpass
2540 		0u,															// framebuffer
2541 		VK_TRUE,													// occlusionQueryEnable
2542 		VK_QUERY_CONTROL_PRECISE_BIT,								// queryFlags
2543 		(VkQueryPipelineStatisticFlags)0u,							// pipelineStatistics
2544 	};
2545 	const VkCommandBufferBeginInfo			secBufferBeginInfo		=
2546 	{
2547 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,				// sType
2548 		DE_NULL,													// pNext
2549 		0u,															// flags
2550 		&secBufferInheritInfo,
2551 	};
2552 
2553 	// Create an occlusion query with VK_QUERY_CONTROL_PRECISE_BIT set
2554 	const VkQueryPoolCreateInfo				queryPoolCreateInfo		=
2555 	{
2556 		VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO,					// sType
2557 		DE_NULL,													// pNext
2558 		0u,															// flags
2559 		VK_QUERY_TYPE_OCCLUSION,									// queryType
2560 		1u,															// entryCount
2561 		0u,															// pipelineStatistics
2562 	};
2563 	Unique<VkQueryPool>						queryPool				(createQueryPool(vk, vkDevice, &queryPoolCreateInfo));
2564 
2565 	VK_CHECK(vk.beginCommandBuffer(secCmdBuf.get(), &secBufferBeginInfo));
2566 	VK_CHECK(vk.endCommandBuffer(secCmdBuf.get()));
2567 
2568 	VK_CHECK(vk.beginCommandBuffer(primCmdBuf.get(), &primBufferBeginInfo));
2569 	{
2570 		vk.cmdResetQueryPool(primCmdBuf.get(), queryPool.get(), 0u, 1u);
2571 		vk.cmdBeginQuery(primCmdBuf.get(), queryPool.get(), 0u, VK_QUERY_CONTROL_PRECISE_BIT);
2572 		{
2573 			vk.cmdExecuteCommands(primCmdBuf.get(), 1u, &secCmdBuf.get());
2574 		}
2575 		vk.cmdEndQuery(primCmdBuf.get(), queryPool.get(), 0u);
2576 	}
2577 	VK_CHECK(vk.endCommandBuffer(primCmdBuf.get()));
2578 
2579 	return tcu::TestStatus::pass("Successfully recorded a secondary command buffer allowing a precise occlusion query.");
2580 }
2581 
recordBufferQueryImpreciseWithoutFlagTest(Context & context)2582 tcu::TestStatus recordBufferQueryImpreciseWithoutFlagTest(Context& context)
2583 {
2584 	const VkDevice							vkDevice				= context.getDevice();
2585 	const DeviceInterface&					vk						= context.getDeviceInterface();
2586 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
2587 
2588 	if (!context.getDeviceFeatures().inheritedQueries)
2589 		TCU_THROW(NotSupportedError, "Inherited queries feature is not supported");
2590 
2591 	const VkCommandPoolCreateInfo			cmdPoolParams			=
2592 	{
2593 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// sType;
2594 		DE_NULL,													// pNext;
2595 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			// flags;
2596 		queueFamilyIndex,											// queueFamilyIndex;
2597 	};
2598 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
2599 
2600 	// Command buffer
2601 	const VkCommandBufferAllocateInfo		primCmdBufParams		=
2602 	{
2603 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// sType;
2604 		DE_NULL,													// pNext;
2605 		*cmdPool,													// pool;
2606 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							// level;
2607 		1u,															// flags;
2608 	};
2609 	const Unique<VkCommandBuffer>			primCmdBuf				(allocateCommandBuffer(vk, vkDevice, &primCmdBufParams));
2610 
2611 	// Secondary Command buffer params
2612 	const VkCommandBufferAllocateInfo		secCmdBufParams			=
2613 	{
2614 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// sType;
2615 		DE_NULL,													// pNext;
2616 		*cmdPool,													// pool;
2617 		VK_COMMAND_BUFFER_LEVEL_SECONDARY,							// level;
2618 		1u,															// flags;
2619 	};
2620 	const Unique<VkCommandBuffer>			secCmdBuf				(allocateCommandBuffer(vk, vkDevice, &secCmdBufParams));
2621 
2622 	const VkCommandBufferBeginInfo			primBufferBeginInfo		=
2623 	{
2624 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,				// sType
2625 		DE_NULL,													// pNext
2626 		0u,															// flags
2627 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
2628 	};
2629 
2630 	const VkCommandBufferInheritanceInfo	secBufferInheritInfo	=
2631 	{
2632 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
2633 		DE_NULL,
2634 		0u,															// renderPass
2635 		0u,															// subpass
2636 		0u,															// framebuffer
2637 		VK_TRUE,													// occlusionQueryEnable
2638 		0u,															// queryFlags
2639 		(VkQueryPipelineStatisticFlags)0u,							// pipelineStatistics
2640 	};
2641 	const VkCommandBufferBeginInfo			secBufferBeginInfo		=
2642 	{
2643 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,				// sType
2644 		DE_NULL,													// pNext
2645 		0u,															// flags
2646 		&secBufferInheritInfo,
2647 	};
2648 
2649 	// Create an occlusion query with VK_QUERY_CONTROL_PRECISE_BIT set
2650 	const VkQueryPoolCreateInfo				queryPoolCreateInfo		=
2651 	{
2652 		VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO,					// sType
2653 		DE_NULL,													// pNext
2654 		(VkQueryPoolCreateFlags)0,
2655 		VK_QUERY_TYPE_OCCLUSION,
2656 		1u,
2657 		0u,
2658 	};
2659 	Unique<VkQueryPool>						queryPool				(createQueryPool(vk, vkDevice, &queryPoolCreateInfo));
2660 
2661 	VK_CHECK(vk.beginCommandBuffer(secCmdBuf.get(), &secBufferBeginInfo));
2662 	VK_CHECK(vk.endCommandBuffer(secCmdBuf.get()));
2663 
2664 	VK_CHECK(vk.beginCommandBuffer(primCmdBuf.get(), &primBufferBeginInfo));
2665 	{
2666 		vk.cmdResetQueryPool(primCmdBuf.get(), queryPool.get(), 0u, 1u);
2667 		vk.cmdBeginQuery(primCmdBuf.get(), queryPool.get(), 0u, VK_QUERY_CONTROL_PRECISE_BIT);
2668 		{
2669 			vk.cmdExecuteCommands(primCmdBuf.get(), 1u, &secCmdBuf.get());
2670 		}
2671 		vk.cmdEndQuery(primCmdBuf.get(), queryPool.get(), 0u);
2672 	}
2673 	VK_CHECK(vk.endCommandBuffer(primCmdBuf.get()));
2674 
2675 	return tcu::TestStatus::pass("Successfully recorded a secondary command buffer allowing a precise occlusion query.");
2676 }
2677 
2678 /******** 19.4. Command Buffer Submission (5.4 in VK 1.0 Spec) ****************/
submitBufferCountNonZero(Context & context)2679 tcu::TestStatus submitBufferCountNonZero(Context& context)
2680 {
2681 	const VkDevice							vkDevice				= context.getDevice();
2682 	const DeviceInterface&					vk						= context.getDeviceInterface();
2683 	const VkQueue							queue					= context.getUniversalQueue();
2684 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
2685 
2686 	const deUint32							BUFFER_COUNT			= 5u;
2687 
2688 	const VkCommandPoolCreateInfo			cmdPoolParams			=
2689 	{
2690 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// sType;
2691 		DE_NULL,													// pNext;
2692 		0u,															// flags;
2693 		queueFamilyIndex,											// queueFamilyIndex;
2694 	};
2695 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
2696 
2697 	// Command buffer
2698 	const VkCommandBufferAllocateInfo		cmdBufParams			=
2699 	{
2700 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// sType;
2701 		DE_NULL,													// pNext;
2702 		*cmdPool,													// pool;
2703 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							// level;
2704 		BUFFER_COUNT,												// bufferCount;
2705 	};
2706 	VkCommandBuffer cmdBuffers[BUFFER_COUNT];
2707 	VK_CHECK(vk.allocateCommandBuffers(vkDevice, &cmdBufParams, cmdBuffers));
2708 
2709 	const VkCommandBufferBeginInfo			cmdBufBeginInfo			=
2710 	{
2711 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,				// sType
2712 		DE_NULL,													// pNext
2713 		0u,															// flags
2714 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
2715 	};
2716 
2717 	const VkEventCreateInfo					eventCreateInfo			=
2718 	{
2719 		VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,						// sType;
2720 		DE_NULL,													// pNext;
2721 		0u,															// flags;
2722 	};
2723 
2724 	std::vector<VkEventSp>					events;
2725 	for (deUint32 ndx = 0; ndx < BUFFER_COUNT; ++ndx)
2726 	{
2727 		events.push_back(VkEventSp(new vk::Unique<VkEvent>(createEvent(vk, vkDevice, &eventCreateInfo, DE_NULL))));
2728 	}
2729 
2730 	// Record the command buffers
2731 	for (deUint32 ndx = 0; ndx < BUFFER_COUNT; ++ndx)
2732 	{
2733 		VK_CHECK(vk.beginCommandBuffer(cmdBuffers[ndx], &cmdBufBeginInfo));
2734 		{
2735 			vk.cmdSetEvent(cmdBuffers[ndx], events[ndx]->get(), VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
2736 		}
2737 		VK_CHECK(vk.endCommandBuffer(cmdBuffers[ndx]));
2738 	}
2739 
2740 	// We'll use a fence to wait for the execution of the queue
2741 	const VkFenceCreateInfo					fenceCreateInfo			=
2742 	{
2743 		VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,						// sType;
2744 		DE_NULL,													// pNext;
2745 		0u,															// flags
2746 	};
2747 	const Unique<VkFence>					fence					(createFence(vk, vkDevice, &fenceCreateInfo));
2748 
2749 	const VkSubmitInfo						submitInfo				=
2750 	{
2751 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
2752 		DE_NULL,													// pNext
2753 		0u,															// waitSemaphoreCount
2754 		DE_NULL,													// pWaitSemaphores
2755 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
2756 		BUFFER_COUNT,												// commandBufferCount
2757 		cmdBuffers,													// pCommandBuffers
2758 		0u,															// signalSemaphoreCount
2759 		DE_NULL,													// pSignalSemaphores
2760 	};
2761 
2762 	// Submit the alpha command buffer to the queue
2763 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, fence.get()));
2764 	// Wait for the queue
2765 	VK_CHECK(vk.waitForFences(vkDevice, 1u, &fence.get(), VK_TRUE, INFINITE_TIMEOUT));
2766 
2767 	// Check if the buffers were executed
2768 	tcu::TestStatus testResult = tcu::TestStatus::incomplete();
2769 
2770 	for (deUint32 ndx = 0; ndx < BUFFER_COUNT; ++ndx)
2771 	{
2772 		if (vk.getEventStatus(vkDevice, events[ndx]->get()) != VK_EVENT_SET)
2773 		{
2774 			testResult = tcu::TestStatus::fail("Failed to set the event.");
2775 			break;
2776 		}
2777 	}
2778 
2779 	if (!testResult.isComplete())
2780 		testResult = tcu::TestStatus::pass("All buffers were submitted and executed correctly.");
2781 
2782 	return testResult;
2783 }
2784 
submitBufferCountEqualZero(Context & context)2785 tcu::TestStatus submitBufferCountEqualZero(Context& context)
2786 {
2787 	const VkDevice							vkDevice				= context.getDevice();
2788 	const DeviceInterface&					vk						= context.getDeviceInterface();
2789 	const VkQueue							queue					= context.getUniversalQueue();
2790 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
2791 
2792 	const deUint32							BUFFER_COUNT			= 2u;
2793 
2794 	const VkCommandPoolCreateInfo			cmdPoolParams			=
2795 	{
2796 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// sType;
2797 		DE_NULL,													// pNext;
2798 		0u,															// flags;
2799 		queueFamilyIndex,											// queueFamilyIndex;
2800 	};
2801 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
2802 
2803 	// Command buffer
2804 	const VkCommandBufferAllocateInfo		cmdBufParams			=
2805 	{
2806 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// sType;
2807 		DE_NULL,													// pNext;
2808 		*cmdPool,													// pool;
2809 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							// level;
2810 		BUFFER_COUNT,												// bufferCount;
2811 	};
2812 	VkCommandBuffer cmdBuffers[BUFFER_COUNT];
2813 	VK_CHECK(vk.allocateCommandBuffers(vkDevice, &cmdBufParams, cmdBuffers));
2814 
2815 	const VkCommandBufferBeginInfo			cmdBufBeginInfo			=
2816 	{
2817 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,				// sType
2818 		DE_NULL,													// pNext
2819 		0u,															// flags
2820 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
2821 	};
2822 
2823 	const VkEventCreateInfo					eventCreateInfo			=
2824 	{
2825 		VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,						// sType;
2826 		DE_NULL,													// pNext;
2827 		0u,															// flags;
2828 	};
2829 
2830 	std::vector<VkEventSp>					events;
2831 	for (deUint32 ndx = 0; ndx < BUFFER_COUNT; ++ndx)
2832 		events.push_back(VkEventSp(new vk::Unique<VkEvent>(createEvent(vk, vkDevice, &eventCreateInfo, DE_NULL))));
2833 
2834 	// Record the command buffers
2835 	for (deUint32 ndx = 0; ndx < BUFFER_COUNT; ++ndx)
2836 	{
2837 		VK_CHECK(vk.beginCommandBuffer(cmdBuffers[ndx], &cmdBufBeginInfo));
2838 		{
2839 			vk.cmdSetEvent(cmdBuffers[ndx], events[ndx]->get(), VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
2840 		}
2841 		VK_CHECK(vk.endCommandBuffer(cmdBuffers[ndx]));
2842 	}
2843 
2844 	// We'll use a fence to wait for the execution of the queue
2845 	const VkFenceCreateInfo					fenceCreateInfo			=
2846 	{
2847 		VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,						// sType;
2848 		DE_NULL,													// pNext;
2849 		0u,															// flags
2850 	};
2851 	const Unique<VkFence>					fenceZero				(createFence(vk, vkDevice, &fenceCreateInfo));
2852 	const Unique<VkFence>					fenceOne				(createFence(vk, vkDevice, &fenceCreateInfo));
2853 
2854 	const VkSubmitInfo						submitInfoCountZero		=
2855 	{
2856 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
2857 		DE_NULL,													// pNext
2858 		0u,															// waitSemaphoreCount
2859 		DE_NULL,													// pWaitSemaphores
2860 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
2861 		1u,															// commandBufferCount
2862 		&cmdBuffers[0],												// pCommandBuffers
2863 		0u,															// signalSemaphoreCount
2864 		DE_NULL,													// pSignalSemaphores
2865 	};
2866 
2867 	const VkSubmitInfo						submitInfoCountOne		=
2868 	{
2869 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
2870 		DE_NULL,													// pNext
2871 		0u,															// waitSemaphoreCount
2872 		DE_NULL,													// pWaitSemaphores
2873 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
2874 		1u,															// commandBufferCount
2875 		&cmdBuffers[1],												// pCommandBuffers
2876 		0u,															// signalSemaphoreCount
2877 		DE_NULL,													// pSignalSemaphores
2878 	};
2879 
2880 	// Submit the command buffers to the queue
2881 	// We're performing two submits to make sure that the first one has
2882 	// a chance to be processed before we check the event's status
2883 	VK_CHECK(vk.queueSubmit(queue, 0, &submitInfoCountZero, fenceZero.get()));
2884 	VK_CHECK(vk.queueSubmit(queue, 1, &submitInfoCountOne, fenceOne.get()));
2885 
2886 	const VkFence							fences[]				=
2887 	{
2888 		fenceZero.get(),
2889 		fenceOne.get(),
2890 	};
2891 
2892 	// Wait for the queue
2893 	VK_CHECK(vk.waitForFences(vkDevice, (deUint32)DE_LENGTH_OF_ARRAY(fences), fences, VK_TRUE, INFINITE_TIMEOUT));
2894 
2895 	// Check if the first buffer was executed
2896 	tcu::TestStatus testResult = tcu::TestStatus::incomplete();
2897 
2898 	if (vk.getEventStatus(vkDevice, events[0]->get()) == VK_EVENT_SET)
2899 		testResult = tcu::TestStatus::fail("The first event was signaled.");
2900 	else
2901 		testResult = tcu::TestStatus::pass("The first submission was ignored.");
2902 
2903 	return testResult;
2904 }
2905 
submitBufferWaitSingleSemaphore(Context & context)2906 tcu::TestStatus submitBufferWaitSingleSemaphore(Context& context)
2907 {
2908 	const VkDevice							vkDevice				= context.getDevice();
2909 	const DeviceInterface&					vk						= context.getDeviceInterface();
2910 	const VkQueue							queue					= context.getUniversalQueue();
2911 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
2912 
2913 	const VkCommandPoolCreateInfo			cmdPoolParams			=
2914 	{
2915 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// VkStructureType				sType;
2916 		DE_NULL,													// const void*					pNext;
2917 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			// VkCommandPoolCreateFlags		flags;
2918 		queueFamilyIndex,											// deUint32						queueFamilyIndex;
2919 	};
2920 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
2921 
2922 	// Command buffer
2923 	const VkCommandBufferAllocateInfo		cmdBufParams			=
2924 	{
2925 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// VkStructureType				sType;
2926 		DE_NULL,													// const void*					pNext;
2927 		*cmdPool,													// VkCommandPool				pool;
2928 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							// VkCommandBufferLevel			level;
2929 		1u,															// uint32_t						bufferCount;
2930 	};
2931 
2932 	// Create two command buffers
2933 	const Unique<VkCommandBuffer>			primCmdBuf1				(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
2934 	const Unique<VkCommandBuffer>			primCmdBuf2				(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
2935 
2936 	const VkCommandBufferBeginInfo			primCmdBufBeginInfo		=
2937 	{
2938 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,				// sType
2939 		DE_NULL,													// pNext
2940 		0,															// flags
2941 		DE_NULL														// const VkCommandBufferInheritanceInfo*	pInheritanceInfo;
2942 	};
2943 
2944 	// Fill create info struct for event
2945 	const VkEventCreateInfo					eventCreateInfo			=
2946 	{
2947 		VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,
2948 		DE_NULL,
2949 		0u,
2950 	};
2951 
2952 	// create two events that will be used to check if command buffers has been executed
2953 	const Unique<VkEvent>					event1					(createEvent(vk, vkDevice, &eventCreateInfo));
2954 	const Unique<VkEvent>					event2					(createEvent(vk, vkDevice, &eventCreateInfo));
2955 
2956 	// reset events
2957 	VK_CHECK(vk.resetEvent(vkDevice, *event1));
2958 	VK_CHECK(vk.resetEvent(vkDevice, *event2));
2959 
2960 	// record first command buffer
2961 	VK_CHECK(vk.beginCommandBuffer(*primCmdBuf1, &primCmdBufBeginInfo));
2962 	{
2963 		// allow execution of event during every stage of pipeline
2964 		VkPipelineStageFlags stageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
2965 
2966 		// record setting event
2967 		vk.cmdSetEvent(*primCmdBuf1, *event1,stageMask);
2968 	}
2969 	VK_CHECK(vk.endCommandBuffer(*primCmdBuf1));
2970 
2971 	// record second command buffer
2972 	VK_CHECK(vk.beginCommandBuffer(*primCmdBuf2, &primCmdBufBeginInfo));
2973 	{
2974 		// allow execution of event during every stage of pipeline
2975 		VkPipelineStageFlags stageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
2976 
2977 		// record setting event
2978 		vk.cmdSetEvent(*primCmdBuf2, *event2,stageMask);
2979 	}
2980 	VK_CHECK(vk.endCommandBuffer(*primCmdBuf2));
2981 
2982 	const VkFenceCreateInfo					fenceCreateInfo			=
2983 	{
2984 		VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
2985 		DE_NULL,
2986 		0u,															// flags
2987 	};
2988 
2989 	// create fence to wait for execution of queue
2990 	const Unique<VkFence>					fence					(createFence(vk, vkDevice, &fenceCreateInfo));
2991 
2992 	// create semaphore for use in this test
2993 	const VkSemaphoreCreateInfo				semaphoreCreateInfo		=
2994 	{
2995 		VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,					// sType;
2996 		DE_NULL,													// pNext;
2997 		0,															// flags;
2998 	};
2999 
3000 	const Unique <VkSemaphore>				semaphore				(createSemaphore(vk, vkDevice, &semaphoreCreateInfo));
3001 
3002 	// create submit info for first buffer - signalling semaphore
3003 	const VkSubmitInfo						submitInfo1				=
3004 	{
3005 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
3006 		DE_NULL,													// pNext
3007 		0u,															// waitSemaphoreCount
3008 		DE_NULL,													// pWaitSemaphores
3009 		DE_NULL,													// pWaitDstStageMask
3010 		1,															// commandBufferCount
3011 		&primCmdBuf1.get(),											// pCommandBuffers
3012 		1u,															// signalSemaphoreCount
3013 		&semaphore.get(),											// pSignalSemaphores
3014 	};
3015 
3016 	// Submit the command buffer to the queue
3017 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo1, *fence));
3018 
3019 	// wait for end of execution of queue
3020 	VK_CHECK(vk.waitForFences(vkDevice, 1, &fence.get(), 0u, INFINITE_TIMEOUT));
3021 
3022 	// check if buffer has been executed
3023 	VkResult result = vk.getEventStatus(vkDevice,*event1);
3024 	if (result != VK_EVENT_SET)
3025 		return tcu::TestStatus::fail("Submit Buffer and Wait for Single Semaphore Test FAILED");
3026 
3027 	const VkPipelineStageFlags				waitDstStageFlags		= VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
3028 
3029 	// create submit info for second buffer - waiting for semaphore
3030 	const VkSubmitInfo						submitInfo2				=
3031 	{
3032 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
3033 		DE_NULL,													// pNext
3034 		1u,															// waitSemaphoreCount
3035 		&semaphore.get(),											// pWaitSemaphores
3036 		&waitDstStageFlags,											// pWaitDstStageMask
3037 		1,															// commandBufferCount
3038 		&primCmdBuf2.get(),											// pCommandBuffers
3039 		0u,															// signalSemaphoreCount
3040 		DE_NULL,													// pSignalSemaphores
3041 	};
3042 
3043 	// reset fence, so it can be used again
3044 	VK_CHECK(vk.resetFences(vkDevice, 1u, &fence.get()));
3045 
3046 	// Submit the second command buffer to the queue
3047 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo2, *fence));
3048 
3049 	// wait for end of execution of queue
3050 	VK_CHECK(vk.waitForFences(vkDevice, 1, &fence.get(), 0u, INFINITE_TIMEOUT));
3051 
3052 	// check if second buffer has been executed
3053 	// if it has been executed, it means that the semaphore was signalled - so test if passed
3054 	result = vk.getEventStatus(vkDevice,*event1);
3055 	if (result != VK_EVENT_SET)
3056 		return tcu::TestStatus::fail("Submit Buffer and Wait for Single Semaphore Test FAILED");
3057 
3058 	return tcu::TestStatus::pass("Submit Buffer and Wait for Single Semaphore Test succeeded");
3059 }
3060 
submitBufferWaitManySemaphores(Context & context)3061 tcu::TestStatus submitBufferWaitManySemaphores(Context& context)
3062 {
3063 	// This test will create numSemaphores semaphores, and signal them in NUM_SEMAPHORES submits to queue
3064 	// After that the numSubmissions queue submissions will wait for each semaphore
3065 
3066 	const deUint32							numSemaphores			= 10u;  // it must be multiply of numSubmission
3067 	const deUint32							numSubmissions			= 2u;
3068 	const VkDevice							vkDevice				= context.getDevice();
3069 	const DeviceInterface&					vk						= context.getDeviceInterface();
3070 	const VkQueue							queue					= context.getUniversalQueue();
3071 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
3072 
3073 	const VkCommandPoolCreateInfo			cmdPoolParams			=
3074 	{
3075 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// VkStructureType				sType;
3076 		DE_NULL,													// const void*					pNext;
3077 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			// VkCommandPoolCreateFlags		flags;
3078 		queueFamilyIndex,											// deUint32						queueFamilyIndex;
3079 	};
3080 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
3081 
3082 	// Command buffer
3083 	const VkCommandBufferAllocateInfo		cmdBufParams			=
3084 	{
3085 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// VkStructureType				sType;
3086 		DE_NULL,													// const void*					pNext;
3087 		*cmdPool,													// VkCommandPool				pool;
3088 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							// VkCommandBufferLevel			level;
3089 		1u,															// uint32_t						bufferCount;
3090 	};
3091 
3092 	// Create command buffer
3093 	const Unique<VkCommandBuffer>			primCmdBuf				(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
3094 
3095 	const VkCommandBufferBeginInfo			primCmdBufBeginInfo		=
3096 	{
3097 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,				// sType
3098 		DE_NULL,													// pNext
3099 		0,															// flags
3100 		DE_NULL														// const VkCommandBufferInheritanceInfo*	pInheritanceInfo;
3101 	};
3102 
3103 	// Fill create info struct for event
3104 	const VkEventCreateInfo					eventCreateInfo			=
3105 	{
3106 		VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,
3107 		DE_NULL,
3108 		0u,
3109 	};
3110 
3111 	// create event that will be used to check if command buffers has been executed
3112 	const Unique<VkEvent>					event					(createEvent(vk, vkDevice, &eventCreateInfo));
3113 
3114 	// reset event - at creation state is undefined
3115 	VK_CHECK(vk.resetEvent(vkDevice, *event));
3116 
3117 	// record command buffer
3118 	VK_CHECK(vk.beginCommandBuffer(*primCmdBuf, &primCmdBufBeginInfo));
3119 	{
3120 		// allow execution of event during every stage of pipeline
3121 		VkPipelineStageFlags stageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
3122 
3123 		// record setting event
3124 		vk.cmdSetEvent(*primCmdBuf, *event,stageMask);
3125 	}
3126 	VK_CHECK(vk.endCommandBuffer(*primCmdBuf));
3127 
3128 	const VkFenceCreateInfo					fenceCreateInfo			=
3129 	{
3130 		VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3131 		DE_NULL,
3132 		0u,															// flags
3133 	};
3134 
3135 	// create fence to wait for execution of queue
3136 	const Unique<VkFence>					fence					(createFence(vk, vkDevice, &fenceCreateInfo));
3137 
3138 	// numSemaphores is declared const, so this array can be static
3139 	// the semaphores will be destroyed automatically at end of scope
3140 	Move <VkSemaphore>						semaphoreArray[numSemaphores];
3141 	VkSemaphore								semaphores[numSemaphores];
3142 
3143 	// prepare create info for semaphores - same for all
3144 	const VkSemaphoreCreateInfo				semaphoreCreateInfo		=
3145 	{
3146 		VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,					// sType;
3147 		DE_NULL,													// pNext;
3148 		0,															// flags;
3149 	};
3150 
3151 	for (deUint32 idx = 0; idx < numSemaphores; ++idx) {
3152 		// create semaphores for use in this test
3153 		semaphoreArray[idx] = createSemaphore(vk, vkDevice, &semaphoreCreateInfo);
3154 		semaphores[idx] = semaphoreArray[idx].get();
3155 	};
3156 
3157 	{
3158 		// create submit info for buffer - signal semaphores
3159 		const VkSubmitInfo submitInfo1 =
3160 		{
3161 			VK_STRUCTURE_TYPE_SUBMIT_INFO,							// sType
3162 			DE_NULL,												// pNext
3163 			0u,														// waitSemaphoreCount
3164 			DE_NULL,												// pWaitSemaphores
3165 			DE_NULL,												// pWaitDstStageMask
3166 			1,														// commandBufferCount
3167 			&primCmdBuf.get(),										// pCommandBuffers
3168 			numSemaphores,											// signalSemaphoreCount
3169 			semaphores												// pSignalSemaphores
3170 		};
3171 		// Submit the command buffer to the queue
3172 		VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo1, *fence));
3173 
3174 		// wait for end of execution of queue
3175 		VK_CHECK(vk.waitForFences(vkDevice, 1, &fence.get(), 0u, INFINITE_TIMEOUT));
3176 
3177 		// check if buffer has been executed
3178 		VkResult result = vk.getEventStatus(vkDevice,*event);
3179 		if (result != VK_EVENT_SET)
3180 			return tcu::TestStatus::fail("Submit Buffer and Wait for Many Semaphores Test FAILED");
3181 
3182 		// reset event, so next buffers can set it again
3183 		VK_CHECK(vk.resetEvent(vkDevice, *event));
3184 
3185 		// reset fence, so it can be used again
3186 		VK_CHECK(vk.resetFences(vkDevice, 1u, &fence.get()));
3187 	}
3188 
3189 	const deUint32							numberOfSemaphoresToBeWaitedByOneSubmission	= numSemaphores / numSubmissions;
3190 	const std::vector<VkPipelineStageFlags>	waitDstStageFlags							(numberOfSemaphoresToBeWaitedByOneSubmission, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
3191 
3192 	// the following code waits for the semaphores set above - numSubmissions queues will wait for each semaphore from above
3193 	for (deUint32 idxSubmission = 0; idxSubmission < numSubmissions; ++idxSubmission) {
3194 
3195 		// create submit info for buffer - waiting for semaphore
3196 		const VkSubmitInfo				submitInfo2				=
3197 		{
3198 			VK_STRUCTURE_TYPE_SUBMIT_INFO,												// sType
3199 			DE_NULL,																	// pNext
3200 			numberOfSemaphoresToBeWaitedByOneSubmission,								// waitSemaphoreCount
3201 			semaphores + (numberOfSemaphoresToBeWaitedByOneSubmission * idxSubmission),	// pWaitSemaphores
3202 			waitDstStageFlags.data(),													// pWaitDstStageMask
3203 			1,																			// commandBufferCount
3204 			&primCmdBuf.get(),															// pCommandBuffers
3205 			0u,																			// signalSemaphoreCount
3206 			DE_NULL,																	// pSignalSemaphores
3207 		};
3208 
3209 		// Submit the second command buffer to the queue
3210 		VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo2, *fence));
3211 
3212 		// wait for 1 second.
3213 		VK_CHECK(vk.waitForFences(vkDevice, 1, &fence.get(), 0u, 1000 * 1000 * 1000));
3214 
3215 		// check if second buffer has been executed
3216 		// if it has been executed, it means that the semaphore was signalled - so test if passed
3217 		VkResult result = vk.getEventStatus(vkDevice,*event);
3218 		if (result != VK_EVENT_SET)
3219 			return tcu::TestStatus::fail("Submit Buffer and Wait for Many Semaphores Test FAILED");
3220 
3221 		// reset fence, so it can be used again
3222 		VK_CHECK(vk.resetFences(vkDevice, 1u, &fence.get()));
3223 
3224 		// reset event, so next buffers can set it again
3225 		VK_CHECK(vk.resetEvent(vkDevice, *event));
3226 	}
3227 
3228 	return tcu::TestStatus::pass("Submit Buffer and Wait for Many Semaphores Test succeeded");
3229 }
3230 
submitBufferNullFence(Context & context)3231 tcu::TestStatus submitBufferNullFence(Context& context)
3232 {
3233 	const VkDevice							vkDevice				= context.getDevice();
3234 	const DeviceInterface&					vk						= context.getDeviceInterface();
3235 	const VkQueue							queue					= context.getUniversalQueue();
3236 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
3237 
3238 	const short								BUFFER_COUNT			= 2;
3239 
3240 	const VkCommandPoolCreateInfo			cmdPoolParams			=
3241 	{
3242 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// sType;
3243 		DE_NULL,													// pNext;
3244 		0u,															// flags;
3245 		queueFamilyIndex,											// queueFamilyIndex;
3246 	};
3247 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
3248 
3249 	// Command buffer
3250 	const VkCommandBufferAllocateInfo		cmdBufParams			=
3251 	{
3252 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// sType;
3253 		DE_NULL,													// pNext;
3254 		*cmdPool,													// pool;
3255 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							// level;
3256 		1u,															// bufferCount;
3257 	};
3258 	VkCommandBuffer cmdBuffers[BUFFER_COUNT];
3259 	for (deUint32 ndx = 0; ndx < BUFFER_COUNT; ++ndx)
3260 		VK_CHECK(vk.allocateCommandBuffers(vkDevice, &cmdBufParams, &cmdBuffers[ndx]));
3261 
3262 	const VkCommandBufferBeginInfo			cmdBufBeginInfo			=
3263 	{
3264 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,				// sType
3265 		DE_NULL,													// pNext
3266 		0u,															// flags
3267 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
3268 	};
3269 
3270 	const VkEventCreateInfo					eventCreateInfo			=
3271 	{
3272 		VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,						// sType;
3273 		DE_NULL,													// pNext;
3274 		0u,															// flags;
3275 	};
3276 
3277 	std::vector<VkEventSp>					events;
3278 	for (deUint32 ndx = 0; ndx < BUFFER_COUNT; ++ndx)
3279 		events.push_back(VkEventSp(new vk::Unique<VkEvent>(createEvent(vk, vkDevice, &eventCreateInfo, DE_NULL))));
3280 
3281 	// Record the command buffers
3282 	for (deUint32 ndx = 0; ndx < BUFFER_COUNT; ++ndx)
3283 	{
3284 		VK_CHECK(vk.beginCommandBuffer(cmdBuffers[ndx], &cmdBufBeginInfo));
3285 		{
3286 			vk.cmdSetEvent(cmdBuffers[ndx], events[ndx]->get(), VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
3287 		}
3288 		VK_CHECK(vk.endCommandBuffer(cmdBuffers[ndx]));
3289 	}
3290 
3291 	// We'll use a fence to wait for the execution of the queue
3292 	const VkFenceCreateInfo					fenceCreateInfo			=
3293 	{
3294 		VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,						// sType;
3295 		DE_NULL,													// pNext;
3296 		0u,															// flags
3297 	};
3298 	const Unique<VkFence>					fence					(createFence(vk, vkDevice, &fenceCreateInfo));
3299 
3300 	const VkSubmitInfo						submitInfoNullFence		=
3301 	{
3302 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
3303 		DE_NULL,													// pNext
3304 		0u,															// waitSemaphoreCount
3305 		DE_NULL,													// pWaitSemaphores
3306 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
3307 		1u,															// commandBufferCount
3308 		&cmdBuffers[0],												// pCommandBuffers
3309 		0u,															// signalSemaphoreCount
3310 		DE_NULL,													// pSignalSemaphores
3311 	};
3312 
3313 	const VkSubmitInfo						submitInfoNonNullFence	=
3314 	{
3315 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
3316 		DE_NULL,													// pNext
3317 		0u,															// waitSemaphoreCount
3318 		DE_NULL,													// pWaitSemaphores
3319 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
3320 		1u,															// commandBufferCount
3321 		&cmdBuffers[1],												// pCommandBuffers
3322 		0u,															// signalSemaphoreCount
3323 		DE_NULL,													// pSignalSemaphores
3324 	};
3325 
3326 	// Perform two submissions - one with no fence, the other one with a valid
3327 	// fence Hoping submitting the other buffer will give the first one time to
3328 	// execute
3329 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfoNullFence, DE_NULL));
3330 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfoNonNullFence, fence.get()));
3331 
3332 	// Wait for the queue
3333 	VK_CHECK(vk.waitForFences(vkDevice, 1u, &fence.get(), VK_TRUE, INFINITE_TIMEOUT));
3334 
3335 	tcu::TestStatus testResult = tcu::TestStatus::incomplete();
3336 
3337 	if (vk.getEventStatus(vkDevice, events[0]->get()) != VK_EVENT_SET)
3338 		testResult = tcu::TestStatus::fail("The first event was not signaled -> the buffer was not executed.");
3339 	else
3340 		testResult = tcu::TestStatus::pass("The first event was signaled -> the buffer with null fence submitted and executed correctly.");
3341 
3342 	return testResult;
3343 }
3344 
3345 /******** 19.5. Secondary Command Buffer Execution (5.6 in VK 1.0 Spec) *******/
executeSecondaryBufferTest(Context & context)3346 tcu::TestStatus executeSecondaryBufferTest(Context& context)
3347 {
3348 	const VkDevice							vkDevice				= context.getDevice();
3349 	const DeviceInterface&					vk						= context.getDeviceInterface();
3350 	const VkQueue							queue					= context.getUniversalQueue();
3351 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
3352 
3353 	const VkCommandPoolCreateInfo			cmdPoolParams			=
3354 	{
3355 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// sType;
3356 		DE_NULL,													// pNext;
3357 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			// flags;
3358 		queueFamilyIndex,											// queueFamilyIndex;
3359 	};
3360 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
3361 
3362 	// Command buffer
3363 	const VkCommandBufferAllocateInfo		cmdBufParams			=
3364 	{
3365 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// sType;
3366 		DE_NULL,													// pNext;
3367 		*cmdPool,													// commandPool;
3368 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							// level;
3369 		1u,															// bufferCount;
3370 	};
3371 	const Unique<VkCommandBuffer>			primCmdBuf				(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
3372 
3373 	// Secondary Command buffer
3374 	const VkCommandBufferAllocateInfo		secCmdBufParams			=
3375 	{
3376 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// sType;
3377 		DE_NULL,													// pNext;
3378 		*cmdPool,													// commandPool;
3379 		VK_COMMAND_BUFFER_LEVEL_SECONDARY,							// level;
3380 		1u,															// bufferCount;
3381 	};
3382 	const Unique<VkCommandBuffer>			secCmdBuf				(allocateCommandBuffer(vk, vkDevice, &secCmdBufParams));
3383 
3384 	const VkCommandBufferBeginInfo			primCmdBufBeginInfo		=
3385 	{
3386 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,				// sType
3387 		DE_NULL,													// pNext
3388 		0u,															// flags
3389 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
3390 	};
3391 
3392 	const VkCommandBufferInheritanceInfo	secCmdBufInheritInfo	=
3393 	{
3394 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
3395 		DE_NULL,
3396 		DE_NULL,													// renderPass
3397 		0u,															// subpass
3398 		DE_NULL,													// framebuffer
3399 		VK_FALSE,													// occlusionQueryEnable
3400 		(VkQueryControlFlags)0u,									// queryFlags
3401 		(VkQueryPipelineStatisticFlags)0u,							// pipelineStatistics
3402 	};
3403 	const VkCommandBufferBeginInfo			secCmdBufBeginInfo		=
3404 	{
3405 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,				// sType
3406 		DE_NULL,													// pNext
3407 		0u,															// flags
3408 		&secCmdBufInheritInfo,
3409 	};
3410 
3411 	// Fill create info struct for event
3412 	const VkEventCreateInfo					eventCreateInfo			=
3413 	{
3414 		VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,
3415 		DE_NULL,
3416 		0u,
3417 	};
3418 
3419 	// create event that will be used to check if secondary command buffer has been executed
3420 	const Unique<VkEvent>					event					(createEvent(vk, vkDevice, &eventCreateInfo));
3421 
3422 	// reset event
3423 	VK_CHECK(vk.resetEvent(vkDevice, *event));
3424 
3425 	// record secondary command buffer
3426 	VK_CHECK(vk.beginCommandBuffer(*secCmdBuf, &secCmdBufBeginInfo));
3427 	{
3428 		// allow execution of event during every stage of pipeline
3429 		VkPipelineStageFlags stageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
3430 		// record setting event
3431 		vk.cmdSetEvent(*secCmdBuf, *event, stageMask);
3432 	}
3433 	// end recording of the secondary buffer
3434 	VK_CHECK(vk.endCommandBuffer(*secCmdBuf));
3435 
3436 	// record primary command buffer
3437 	VK_CHECK(vk.beginCommandBuffer(*primCmdBuf, &primCmdBufBeginInfo));
3438 	{
3439 		// execute secondary buffer
3440 		vk.cmdExecuteCommands(*primCmdBuf, 1u, &secCmdBuf.get());
3441 	}
3442 	VK_CHECK(vk.endCommandBuffer(*primCmdBuf));
3443 
3444 	const VkFenceCreateInfo					fenceCreateInfo			=
3445 	{
3446 		VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3447 		DE_NULL,
3448 		0u,															// flags
3449 	};
3450 
3451 	// create fence to wait for execution of queue
3452 	const Unique<VkFence>					fence					(createFence(vk, vkDevice, &fenceCreateInfo));
3453 	const VkSubmitInfo						submitInfo				=
3454 	{
3455 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
3456 		DE_NULL,													// pNext
3457 		0u,															// waitSemaphoreCount
3458 		DE_NULL,													// pWaitSemaphores
3459 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
3460 		1u,															// commandBufferCount
3461 		&primCmdBuf.get(),											// pCommandBuffers
3462 		0u,															// signalSemaphoreCount
3463 		DE_NULL,													// pSignalSemaphores
3464 	};
3465 
3466 	// submit primary buffer, the secondary should be executed too
3467 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, fence.get()));
3468 
3469 	// wait for end of execution of queue
3470 	VK_CHECK(vk.waitForFences(vkDevice, 1, &fence.get(), 0u, INFINITE_TIMEOUT));
3471 
3472 	// check if secondary buffer has been executed
3473 	VkResult result = vk.getEventStatus(vkDevice, *event);
3474 	if (result == VK_EVENT_SET)
3475 		return tcu::TestStatus::pass("executeSecondaryBufferTest succeeded");
3476 
3477 	return tcu::TestStatus::fail("executeSecondaryBufferTest FAILED");
3478 }
3479 
executeSecondaryBufferTwiceTest(Context & context)3480 tcu::TestStatus executeSecondaryBufferTwiceTest(Context& context)
3481 {
3482 	const deUint32							BUFFER_COUNT			= 10u;
3483 	const VkDevice							vkDevice				= context.getDevice();
3484 	const DeviceInterface&					vk						= context.getDeviceInterface();
3485 	const VkQueue							queue					= context.getUniversalQueue();
3486 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
3487 
3488 	const VkCommandPoolCreateInfo			cmdPoolParams			=
3489 	{
3490 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					//	VkStructureType				sType;
3491 		DE_NULL,													//	const void*					pNext;
3492 		VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,			//	VkCommandPoolCreateFlags	flags;
3493 		queueFamilyIndex,											//	deUint32					queueFamilyIndex;
3494 	};
3495 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, vkDevice, &cmdPoolParams));
3496 
3497 	// Command buffer
3498 	const VkCommandBufferAllocateInfo		cmdBufParams			=
3499 	{
3500 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				//	VkStructureType			sType;
3501 		DE_NULL,													//	const void*				pNext;
3502 		*cmdPool,													//	VkCommandPool				pool;
3503 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							//	VkCommandBufferLevel		level;
3504 		1u,															//	uint32_t					bufferCount;
3505 	};
3506 	const Unique<VkCommandBuffer>			primCmdBufOne			(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
3507 	const Unique<VkCommandBuffer>			primCmdBufTwo			(allocateCommandBuffer(vk, vkDevice, &cmdBufParams));
3508 
3509 	// Secondary Command buffers params
3510 	const VkCommandBufferAllocateInfo		secCmdBufParams			=
3511 	{
3512 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				//	VkStructureType			sType;
3513 		DE_NULL,													//	const void*				pNext;
3514 		*cmdPool,													//	VkCommandPool				pool;
3515 		VK_COMMAND_BUFFER_LEVEL_SECONDARY,							//	VkCommandBufferLevel		level;
3516 		BUFFER_COUNT,												//	uint32_t					bufferCount;
3517 	};
3518 	VkCommandBuffer cmdBuffers[BUFFER_COUNT];
3519 	VK_CHECK(vk.allocateCommandBuffers(vkDevice, &secCmdBufParams, cmdBuffers));
3520 
3521 	const VkCommandBufferBeginInfo			primCmdBufBeginInfo		=
3522 	{
3523 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
3524 		DE_NULL,
3525 		0,															// flags
3526 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
3527 	};
3528 
3529 	const VkCommandBufferInheritanceInfo	secCmdBufInheritInfo	=
3530 	{
3531 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
3532 		DE_NULL,
3533 		(VkRenderPass)0u,											// renderPass
3534 		0u,															// subpass
3535 		(VkFramebuffer)0u,											// framebuffer
3536 		VK_FALSE,													// occlusionQueryEnable
3537 		(VkQueryControlFlags)0u,									// queryFlags
3538 		(VkQueryPipelineStatisticFlags)0u,							// pipelineStatistics
3539 	};
3540 	const VkCommandBufferBeginInfo			secCmdBufBeginInfo		=
3541 	{
3542 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
3543 		DE_NULL,
3544 		VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,				// flags
3545 		&secCmdBufInheritInfo,
3546 	};
3547 
3548 	// Fill create info struct for event
3549 	const VkEventCreateInfo					eventCreateInfo			=
3550 	{
3551 		VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,
3552 		DE_NULL,
3553 		0u,
3554 	};
3555 
3556 	// create event that will be used to check if secondary command buffer has been executed
3557 	const Unique<VkEvent>					eventOne				(createEvent(vk, vkDevice, &eventCreateInfo));
3558 
3559 	// reset event
3560 	VK_CHECK(vk.resetEvent(vkDevice, *eventOne));
3561 
3562 	for (deUint32 ndx = 0; ndx < BUFFER_COUNT; ++ndx)
3563 	{
3564 		// record secondary command buffer
3565 		VK_CHECK(vk.beginCommandBuffer(cmdBuffers[ndx], &secCmdBufBeginInfo));
3566 		{
3567 			// allow execution of event during every stage of pipeline
3568 			VkPipelineStageFlags stageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
3569 
3570 			// wait for event
3571 			vk.cmdWaitEvents(cmdBuffers[ndx], 1, &eventOne.get(), stageMask, stageMask, 0, DE_NULL, 0u, DE_NULL, 0u, DE_NULL);
3572 		}
3573 		// end recording of secondary buffers
3574 		VK_CHECK(vk.endCommandBuffer(cmdBuffers[ndx]));
3575 	};
3576 
3577 	// record primary command buffer one
3578 	VK_CHECK(vk.beginCommandBuffer(*primCmdBufOne, &primCmdBufBeginInfo));
3579 	{
3580 		// execute one secondary buffer
3581 		vk.cmdExecuteCommands(*primCmdBufOne, 1, cmdBuffers );
3582 	}
3583 	VK_CHECK(vk.endCommandBuffer(*primCmdBufOne));
3584 
3585 	// record primary command buffer two
3586 	VK_CHECK(vk.beginCommandBuffer(*primCmdBufTwo, &primCmdBufBeginInfo));
3587 	{
3588 		// execute one secondary buffer with all buffers
3589 		vk.cmdExecuteCommands(*primCmdBufTwo, BUFFER_COUNT, cmdBuffers );
3590 	}
3591 	VK_CHECK(vk.endCommandBuffer(*primCmdBufTwo));
3592 
3593 	const VkFenceCreateInfo					fenceCreateInfo			=
3594 	{
3595 		VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3596 		DE_NULL,
3597 		0u,															// flags
3598 	};
3599 
3600 	// create fence to wait for execution of queue
3601 	const Unique<VkFence>					fenceOne				(createFence(vk, vkDevice, &fenceCreateInfo));
3602 	const Unique<VkFence>					fenceTwo				(createFence(vk, vkDevice, &fenceCreateInfo));
3603 
3604 	const VkSubmitInfo						submitInfoOne			=
3605 	{
3606 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
3607 		DE_NULL,													// pNext
3608 		0u,															// waitSemaphoreCount
3609 		DE_NULL,													// pWaitSemaphores
3610 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
3611 		1,															// commandBufferCount
3612 		&primCmdBufOne.get(),										// pCommandBuffers
3613 		0u,															// signalSemaphoreCount
3614 		DE_NULL,													// pSignalSemaphores
3615 	};
3616 
3617 	// submit primary buffer, the secondary should be executed too
3618 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfoOne, *fenceOne));
3619 
3620 	// wait for buffer to stop at event for 100 microseconds
3621 	vk.waitForFences(vkDevice, 1, &fenceOne.get(), 0u, 100000);
3622 
3623 	const VkSubmitInfo						submitInfoTwo			=
3624 	{
3625 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
3626 		DE_NULL,													// pNext
3627 		0u,															// waitSemaphoreCount
3628 		DE_NULL,													// pWaitSemaphores
3629 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
3630 		1,															// commandBufferCount
3631 		&primCmdBufTwo.get(),										// pCommandBuffers
3632 		0u,															// signalSemaphoreCount
3633 		DE_NULL,													// pSignalSemaphores
3634 	};
3635 
3636 	// submit second primary buffer, the secondary should be executed too
3637 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfoTwo, *fenceTwo));
3638 
3639 	// wait for all buffers to stop at event for 100 microseconds
3640 	vk.waitForFences(vkDevice, 1, &fenceOne.get(), 0u, 100000);
3641 
3642 	// now all buffers are waiting at eventOne
3643 	// set event eventOne
3644 	VK_CHECK(vk.setEvent(vkDevice, *eventOne));
3645 
3646 	// wait for end of execution of fenceOne
3647 	VK_CHECK(vk.waitForFences(vkDevice, 1, &fenceOne.get(), 0u, INFINITE_TIMEOUT));
3648 
3649 	// wait for end of execution of second queue
3650 	VK_CHECK(vk.waitForFences(vkDevice, 1, &fenceTwo.get(), 0u, INFINITE_TIMEOUT));
3651 
3652 	return tcu::TestStatus::pass("executeSecondaryBufferTwiceTest succeeded");
3653 }
3654 
3655 /******** 19.6. Commands Allowed Inside Command Buffers (? in VK 1.0 Spec) **/
orderBindPipelineTest(Context & context)3656 tcu::TestStatus orderBindPipelineTest(Context& context)
3657 {
3658 	const DeviceInterface&					vk						= context.getDeviceInterface();
3659 	const VkDevice							device					= context.getDevice();
3660 	const VkQueue							queue					= context.getUniversalQueue();
3661 	const deUint32							queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
3662 	Allocator&								allocator				= context.getDefaultAllocator();
3663 	const ComputeInstanceResultBuffer		result					(vk, device, allocator);
3664 
3665 	enum
3666 	{
3667 		ADDRESSABLE_SIZE = 256, // allocate a lot more than required
3668 	};
3669 
3670 	const tcu::Vec4							colorA1					= tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f);
3671 	const tcu::Vec4							colorA2					= tcu::Vec4(1.0f, 1.0f, 0.0f, 1.0f);
3672 	const tcu::Vec4							colorB1					= tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f);
3673 	const tcu::Vec4							colorB2					= tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f);
3674 
3675 	const deUint32							dataOffsetA				= (0u);
3676 	const deUint32							dataOffsetB				= (0u);
3677 	const deUint32							viewOffsetA				= (0u);
3678 	const deUint32							viewOffsetB				= (0u);
3679 	const deUint32							bufferSizeA				= dataOffsetA + ADDRESSABLE_SIZE;
3680 	const deUint32							bufferSizeB				= dataOffsetB + ADDRESSABLE_SIZE;
3681 
3682 	de::MovePtr<Allocation>					bufferMemA;
3683 	const Unique<VkBuffer>					bufferA					(createColorDataBuffer(dataOffsetA, bufferSizeA, colorA1, colorA2, &bufferMemA, context));
3684 
3685 	de::MovePtr<Allocation>					bufferMemB;
3686 	const Unique<VkBuffer>					bufferB					(createColorDataBuffer(dataOffsetB, bufferSizeB, colorB1, colorB2, &bufferMemB, context));
3687 
3688 	const Unique<VkDescriptorSetLayout>		descriptorSetLayout		(createDescriptorSetLayout(context));
3689 	const Unique<VkDescriptorPool>			descriptorPool			(createDescriptorPool(context));
3690 	const Unique<VkDescriptorSet>			descriptorSet			(createDescriptorSet(*descriptorPool, *descriptorSetLayout, *bufferA, viewOffsetA, *bufferB, viewOffsetB, result.getBuffer(), context));
3691 	const VkDescriptorSet					descriptorSets[]		= { *descriptorSet };
3692 	const int								numDescriptorSets		= DE_LENGTH_OF_ARRAY(descriptorSets);
3693 
3694 	const VkPipelineLayoutCreateInfo layoutCreateInfo =
3695 	{
3696 		VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,				// sType
3697 		DE_NULL,													// pNext
3698 		(VkPipelineLayoutCreateFlags)0,
3699 		numDescriptorSets,											// setLayoutCount
3700 		&descriptorSetLayout.get(),									// pSetLayouts
3701 		0u,															// pushConstantRangeCount
3702 		DE_NULL,													// pPushConstantRanges
3703 	};
3704 	Unique<VkPipelineLayout>				pipelineLayout			(createPipelineLayout(vk, device, &layoutCreateInfo));
3705 
3706 	const Unique<VkShaderModule>			computeModuleGood		(createShaderModule(vk, device, context.getBinaryCollection().get("compute_good"), (VkShaderModuleCreateFlags)0u));
3707 	const Unique<VkShaderModule>			computeModuleBad		(createShaderModule(vk, device, context.getBinaryCollection().get("compute_bad"),  (VkShaderModuleCreateFlags)0u));
3708 
3709 	const VkPipelineShaderStageCreateInfo	shaderCreateInfoGood	=
3710 	{
3711 		VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
3712 		DE_NULL,
3713 		(VkPipelineShaderStageCreateFlags)0,
3714 		VK_SHADER_STAGE_COMPUTE_BIT,								// stage
3715 		*computeModuleGood,											// shader
3716 		"main",
3717 		DE_NULL,													// pSpecializationInfo
3718 	};
3719 
3720 	const VkPipelineShaderStageCreateInfo	shaderCreateInfoBad	=
3721 	{
3722 		VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
3723 		DE_NULL,
3724 		(vk::VkPipelineShaderStageCreateFlags)0,
3725 		vk::VK_SHADER_STAGE_COMPUTE_BIT,							// stage
3726 		*computeModuleBad,											// shader
3727 		"main",
3728 		DE_NULL,													// pSpecializationInfo
3729 	};
3730 
3731 	const VkComputePipelineCreateInfo		createInfoGood			=
3732 	{
3733 		VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
3734 		DE_NULL,
3735 		0u,															// flags
3736 		shaderCreateInfoGood,										// cs
3737 		*pipelineLayout,											// layout
3738 		(vk::VkPipeline)0,											// basePipelineHandle
3739 		0u,															// basePipelineIndex
3740 	};
3741 
3742 	const VkComputePipelineCreateInfo		createInfoBad			=
3743 	{
3744 		VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
3745 		DE_NULL,
3746 		0u,															// flags
3747 		shaderCreateInfoBad,										// cs
3748 		*pipelineLayout,											// descriptorSetLayout.get()
3749 		(VkPipeline)0,												// basePipelineHandle
3750 		0u,															// basePipelineIndex
3751 	};
3752 
3753 	const Unique<VkPipeline>				pipelineGood			(createComputePipeline(vk, device, (VkPipelineCache)0u, &createInfoGood));
3754 	const Unique<VkPipeline>				pipelineBad				(createComputePipeline(vk, device, (VkPipelineCache)0u, &createInfoBad));
3755 
3756 	const VkAccessFlags						inputBit				= (VK_ACCESS_UNIFORM_READ_BIT);
3757 	const VkBufferMemoryBarrier				bufferBarriers[]		=
3758 	{
3759 		{
3760 			VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
3761 			DE_NULL,
3762 			VK_ACCESS_HOST_WRITE_BIT,									// outputMask
3763 			inputBit,													// inputMask
3764 			VK_QUEUE_FAMILY_IGNORED,									// srcQueueFamilyIndex
3765 			VK_QUEUE_FAMILY_IGNORED,									// destQueueFamilyIndex
3766 			*bufferA,													// buffer
3767 			(VkDeviceSize)0u,											// offset
3768 			(VkDeviceSize)bufferSizeA,									// size
3769 		},
3770 		{
3771 			VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
3772 			DE_NULL,
3773 			VK_ACCESS_HOST_WRITE_BIT,									// outputMask
3774 			inputBit,													// inputMask
3775 			VK_QUEUE_FAMILY_IGNORED,									// srcQueueFamilyIndex
3776 			VK_QUEUE_FAMILY_IGNORED,									// destQueueFamilyIndex
3777 			*bufferB,													// buffer
3778 			(VkDeviceSize)0u,											// offset
3779 			(VkDeviceSize)bufferSizeB,									// size
3780 		}
3781 	};
3782 
3783 	const deUint32							numSrcBuffers			= 1u;
3784 
3785 	const deUint32* const					dynamicOffsets			= (DE_NULL);
3786 	const deUint32							numDynamicOffsets		= (0);
3787 	const int								numPreBarriers			= numSrcBuffers;
3788 	const vk::VkBufferMemoryBarrier* const	postBarriers			= result.getResultReadBarrier();
3789 	const int								numPostBarriers			= 1;
3790 	const tcu::Vec4							refQuadrantValue14		= (colorA2);
3791 	const tcu::Vec4							refQuadrantValue23		= (colorA1);
3792 	const tcu::Vec4							references[4]			=
3793 	{
3794 		refQuadrantValue14,
3795 		refQuadrantValue23,
3796 		refQuadrantValue23,
3797 		refQuadrantValue14,
3798 	};
3799 	tcu::Vec4								results[4];
3800 
3801 	// submit and wait begin
3802 
3803 	const tcu::UVec3 numWorkGroups = tcu::UVec3(4, 1u, 1);
3804 
3805 	const VkCommandPoolCreateInfo			cmdPoolCreateInfo		=
3806 	{
3807 		VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,					// sType;
3808 		DE_NULL,													// pNext
3809 		VK_COMMAND_POOL_CREATE_TRANSIENT_BIT,						// flags
3810 		queueFamilyIndex,											// queueFamilyIndex
3811 	};
3812 	const Unique<VkCommandPool>				cmdPool					(createCommandPool(vk, device, &cmdPoolCreateInfo));
3813 
3814 	const VkFenceCreateInfo					fenceCreateInfo			=
3815 	{
3816 		VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3817 		DE_NULL,
3818 		0u,			// flags
3819 	};
3820 
3821 	const VkCommandBufferAllocateInfo		cmdBufCreateInfo		=
3822 	{
3823 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,				// sType
3824 		DE_NULL,													// pNext
3825 		*cmdPool,													// commandPool
3826 		VK_COMMAND_BUFFER_LEVEL_PRIMARY,							// level
3827 		1u,															// bufferCount;
3828 	};
3829 
3830 	const VkCommandBufferBeginInfo			cmdBufBeginInfo			=
3831 	{
3832 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,				// sType
3833 		DE_NULL,													// pNext
3834 		0u,															// flags
3835 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
3836 	};
3837 
3838 	const Unique<VkFence>					cmdCompleteFence		(createFence(vk, device, &fenceCreateInfo));
3839 	const Unique<VkCommandBuffer>			cmd						(allocateCommandBuffer(vk, device, &cmdBufCreateInfo));
3840 
3841 	VK_CHECK(vk.beginCommandBuffer(*cmd, &cmdBufBeginInfo));
3842 
3843 	vk.cmdBindPipeline(*cmd, VK_PIPELINE_BIND_POINT_COMPUTE, *pipelineBad);
3844 	vk.cmdBindPipeline(*cmd, VK_PIPELINE_BIND_POINT_COMPUTE, *pipelineGood);
3845 	vk.cmdBindDescriptorSets(*cmd, VK_PIPELINE_BIND_POINT_COMPUTE, *pipelineLayout, 0, numDescriptorSets, descriptorSets, numDynamicOffsets, dynamicOffsets);
3846 
3847 	if (numPreBarriers)
3848 		vk.cmdPipelineBarrier(*cmd, 0u, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, (VkDependencyFlags)0,
3849 							  0, (const VkMemoryBarrier*)DE_NULL,
3850 							  numPreBarriers, bufferBarriers,
3851 							  0, (const VkImageMemoryBarrier*)DE_NULL);
3852 
3853 	vk.cmdDispatch(*cmd, numWorkGroups.x(), numWorkGroups.y(), numWorkGroups.z());
3854 	vk.cmdPipelineBarrier(*cmd, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, (VkDependencyFlags)0,
3855 						  0, (const VkMemoryBarrier*)DE_NULL,
3856 						  numPostBarriers, postBarriers,
3857 						  0, (const VkImageMemoryBarrier*)DE_NULL);
3858 	VK_CHECK(vk.endCommandBuffer(*cmd));
3859 
3860 	// run
3861 	// submit second primary buffer, the secondary should be executed too
3862 	const VkSubmitInfo						submitInfo				=
3863 	{
3864 		VK_STRUCTURE_TYPE_SUBMIT_INFO,								// sType
3865 		DE_NULL,													// pNext
3866 		0u,															// waitSemaphoreCount
3867 		DE_NULL,													// pWaitSemaphores
3868 		(const VkPipelineStageFlags*)DE_NULL,						// pWaitDstStageMask
3869 		1,															// commandBufferCount
3870 		&cmd.get(),													// pCommandBuffers
3871 		0u,															// signalSemaphoreCount
3872 		DE_NULL,													// pSignalSemaphores
3873 	};
3874 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, *cmdCompleteFence));
3875 
3876 	VK_CHECK(vk.waitForFences(device, 1u, &cmdCompleteFence.get(), 0u, INFINITE_TIMEOUT)); // \note: timeout is failure
3877 	VK_CHECK(vk.resetFences(device, 1u, &cmdCompleteFence.get()));
3878 
3879 	// submit and wait end
3880 	result.readResultContentsTo(&results);
3881 
3882 	// verify
3883 	if (results[0] == references[0] &&
3884 		results[1] == references[1] &&
3885 		results[2] == references[2] &&
3886 		results[3] == references[3])
3887 	{
3888 		return tcu::TestStatus::pass("Pass");
3889 	}
3890 	else if (results[0] == tcu::Vec4(-1.0f) &&
3891 			 results[1] == tcu::Vec4(-1.0f) &&
3892 			 results[2] == tcu::Vec4(-1.0f) &&
3893 			 results[3] == tcu::Vec4(-1.0f))
3894 	{
3895 		context.getTestContext().getLog()
3896 		<< tcu::TestLog::Message
3897 		<< "Result buffer was not written to."
3898 		<< tcu::TestLog::EndMessage;
3899 		return tcu::TestStatus::fail("Result buffer was not written to");
3900 	}
3901 	else
3902 	{
3903 		context.getTestContext().getLog()
3904 		<< tcu::TestLog::Message
3905 		<< "Error expected ["
3906 		<< references[0] << ", "
3907 		<< references[1] << ", "
3908 		<< references[2] << ", "
3909 		<< references[3] << "], got ["
3910 		<< results[0] << ", "
3911 		<< results[1] << ", "
3912 		<< results[2] << ", "
3913 		<< results[3] << "]"
3914 		<< tcu::TestLog::EndMessage;
3915 		return tcu::TestStatus::fail("Invalid result values");
3916 	}
3917 }
3918 
3919 // Shaders
genComputeSource(SourceCollections & programCollection)3920 void genComputeSource (SourceCollections& programCollection)
3921 {
3922 	const char* const						versionDecl				= glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_310_ES);
3923 	std::ostringstream						buf_good;
3924 
3925 	buf_good << versionDecl << "\n"
3926 	<< ""
3927 	<< "layout(local_size_x = 1u, local_size_y = 1u, local_size_z = 1) in;\n"
3928 	<< "layout(set = 0, binding = 1u, std140) uniform BufferName\n"
3929 	<< "{\n"
3930 	<< "	highp vec4 colorA;\n"
3931 	<< "	highp vec4 colorB;\n"
3932 	<< "} b_instance;\n"
3933 	<< "layout(set = 0, binding = 0, std140) writeonly buffer OutBuf\n"
3934 	<< "{\n"
3935 	<< "	highp vec4 read_colors[4];\n"
3936 	<< "} b_out;\n"
3937 	<< "void main(void)\n"
3938 	<< "{\n"
3939 	<< "	highp int quadrant_id = int(gl_WorkGroupID.x);\n"
3940 	<< "	highp vec4 result_color;\n"
3941 	<< "	if (quadrant_id == 1 || quadrant_id == 2)\n"
3942 	<< "		result_color = b_instance.colorA;\n"
3943 	<< "	else\n"
3944 	<< "		result_color = b_instance.colorB;\n"
3945 	<< "	b_out.read_colors[gl_WorkGroupID.x] = result_color;\n"
3946 	<< "}\n";
3947 
3948 	programCollection.glslSources.add("compute_good") << glu::ComputeSource(buf_good.str());
3949 
3950 	std::ostringstream	buf_bad;
3951 
3952 	buf_bad	<< versionDecl << "\n"
3953 	<< ""
3954 	<< "layout(local_size_x = 1u, local_size_y = 1u, local_size_z = 1) in;\n"
3955 	<< "layout(set = 0, binding = 1u, std140) uniform BufferName\n"
3956 	<< "{\n"
3957 	<< "	highp vec4 colorA;\n"
3958 	<< "	highp vec4 colorB;\n"
3959 	<< "} b_instance;\n"
3960 	<< "layout(set = 0, binding = 0, std140) writeonly buffer OutBuf\n"
3961 	<< "{\n"
3962 	<< "	highp vec4 read_colors[4];\n"
3963 	<< "} b_out;\n"
3964 	<< "void main(void)\n"
3965 	<< "{\n"
3966 	<< "	highp int quadrant_id = int(gl_WorkGroupID.x);\n"
3967 	<< "	highp vec4 result_color;\n"
3968 	<< "	if (quadrant_id == 1 || quadrant_id == 2)\n"
3969 	<< "		result_color = b_instance.colorA;\n"
3970 	<< "	else\n"
3971 	<< "		result_color = b_instance.colorB;\n"
3972 	<< "	b_out.read_colors[gl_WorkGroupID.x] = vec4(0.0, 0.0, 0.0, 0.0);\n"
3973 	<< "}\n";
3974 
3975 	programCollection.glslSources.add("compute_bad") << glu::ComputeSource(buf_bad.str());
3976 }
3977 
3978 } // anonymous
3979 
createCommandBuffersTests(tcu::TestContext & testCtx)3980 tcu::TestCaseGroup* createCommandBuffersTests (tcu::TestContext& testCtx)
3981 {
3982 	de::MovePtr<tcu::TestCaseGroup>	commandBuffersTests	(new tcu::TestCaseGroup(testCtx, "command_buffers", "Command Buffers Tests"));
3983 
3984 	/* 19.1. Command Pools (5.1 in VK 1.0 Spec) */
3985 	addFunctionCase				(commandBuffersTests.get(), "pool_create_null_params",			"",	createPoolNullParamsTest);
3986 	addFunctionCase				(commandBuffersTests.get(), "pool_create_non_null_allocator",	"",	createPoolNonNullAllocatorTest);
3987 	addFunctionCase				(commandBuffersTests.get(), "pool_create_transient_bit",		"",	createPoolTransientBitTest);
3988 	addFunctionCase				(commandBuffersTests.get(), "pool_create_reset_bit",			"",	createPoolResetBitTest);
3989 	addFunctionCase				(commandBuffersTests.get(), "pool_reset_release_res",			"",	resetPoolReleaseResourcesBitTest);
3990 	addFunctionCase				(commandBuffersTests.get(), "pool_reset_no_flags_res",			"",	resetPoolNoFlagsTest);
3991 	/* 19.2. Command Buffer Lifetime (5.2 in VK 1.0 Spec) */
3992 	addFunctionCase				(commandBuffersTests.get(), "allocate_single_primary",			"", allocatePrimaryBufferTest);
3993 	addFunctionCase				(commandBuffersTests.get(), "allocate_many_primary",			"",	allocateManyPrimaryBuffersTest);
3994 	addFunctionCase				(commandBuffersTests.get(), "allocate_zero_primary",			"", allocateZeroPrimaryBuffersTest);
3995 	addFunctionCase				(commandBuffersTests.get(), "allocate_single_secondary",		"", allocateSecondaryBufferTest);
3996 	addFunctionCase				(commandBuffersTests.get(), "allocate_many_secondary",			"", allocateManySecondaryBuffersTest);
3997 	addFunctionCase				(commandBuffersTests.get(), "allocate_zero_secondary",			"", allocateZeroSecondaryBuffersTest);
3998 	addFunctionCase				(commandBuffersTests.get(), "execute_small_primary",			"",	executePrimaryBufferTest);
3999 	addFunctionCase				(commandBuffersTests.get(), "execute_large_primary",			"",	executeLargePrimaryBufferTest);
4000 	addFunctionCase				(commandBuffersTests.get(), "reset_implicit",					"", resetBufferImplicitlyTest);
4001 	/* 19.3. Command Buffer Recording (5.3 in VK 1.0 Spec) */
4002 	addFunctionCase				(commandBuffersTests.get(), "record_single_primary",			"",	recordSinglePrimaryBufferTest);
4003 	addFunctionCase				(commandBuffersTests.get(), "record_many_primary",				"", recordLargePrimaryBufferTest);
4004 	addFunctionCase				(commandBuffersTests.get(), "record_single_secondary",			"",	recordSingleSecondaryBufferTest);
4005 	addFunctionCase				(commandBuffersTests.get(), "record_many_secondary",			"", recordLargeSecondaryBufferTest);
4006 	addFunctionCase				(commandBuffersTests.get(), "submit_twice_primary",				"",	submitPrimaryBufferTwiceTest);
4007 	addFunctionCase				(commandBuffersTests.get(), "submit_twice_secondary",			"",	submitSecondaryBufferTwiceTest);
4008 	addFunctionCase				(commandBuffersTests.get(), "record_one_time_submit_primary",	"",	oneTimeSubmitFlagPrimaryBufferTest);
4009 	addFunctionCase				(commandBuffersTests.get(), "record_one_time_submit_secondary",	"",	oneTimeSubmitFlagSecondaryBufferTest);
4010 	addFunctionCase				(commandBuffersTests.get(), "render_pass_continue",				"",	renderPassContinueTest);
4011 	addFunctionCase				(commandBuffersTests.get(), "record_simul_use_primary",			"",	simultaneousUsePrimaryBufferTest);
4012 	addFunctionCase				(commandBuffersTests.get(), "record_simul_use_secondary",		"",	simultaneousUseSecondaryBufferTest);
4013 	addFunctionCase				(commandBuffersTests.get(), "record_query_precise_w_flag",		"",	recordBufferQueryPreciseWithFlagTest);
4014 	addFunctionCase				(commandBuffersTests.get(), "record_query_imprecise_w_flag",	"",	recordBufferQueryImpreciseWithFlagTest);
4015 	addFunctionCase				(commandBuffersTests.get(), "record_query_imprecise_wo_flag",	"",	recordBufferQueryImpreciseWithoutFlagTest);
4016 	/* 19.4. Command Buffer Submission (5.4 in VK 1.0 Spec) */
4017 	addFunctionCase				(commandBuffersTests.get(), "submit_count_non_zero",			"", submitBufferCountNonZero);
4018 	addFunctionCase				(commandBuffersTests.get(), "submit_count_equal_zero",			"", submitBufferCountEqualZero);
4019 	addFunctionCase				(commandBuffersTests.get(), "submit_wait_single_semaphore",		"", submitBufferWaitSingleSemaphore);
4020 	addFunctionCase				(commandBuffersTests.get(), "submit_wait_many_semaphores",		"", submitBufferWaitManySemaphores);
4021 	addFunctionCase				(commandBuffersTests.get(), "submit_null_fence",				"", submitBufferNullFence);
4022 	/* 19.5. Secondary Command Buffer Execution (5.6 in VK 1.0 Spec) */
4023 	addFunctionCase				(commandBuffersTests.get(), "secondary_execute",				"",	executeSecondaryBufferTest);
4024 	addFunctionCase				(commandBuffersTests.get(), "secondary_execute_twice",			"",	executeSecondaryBufferTwiceTest);
4025 	/* 19.6. Commands Allowed Inside Command Buffers (? in VK 1.0 Spec) */
4026 	addFunctionCaseWithPrograms (commandBuffersTests.get(), "order_bind_pipeline",				"", genComputeSource, orderBindPipelineTest);
4027 
4028 	return commandBuffersTests.release();
4029 }
4030 
4031 } // api
4032 } // vkt
4033 
4034