1 /*-------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2017 Google Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief Tests sparse render target.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "vktRenderPassSparseRenderTargetTests.hpp"
25 #include "vktRenderPassTestsUtil.hpp"
26 
27 #include "vktTestCaseUtil.hpp"
28 #include "vktTestGroupUtil.hpp"
29 
30 #include "vkDefs.hpp"
31 #include "vkImageUtil.hpp"
32 #include "vkMemUtil.hpp"
33 #include "vkPrograms.hpp"
34 #include "vkQueryUtil.hpp"
35 #include "vkRef.hpp"
36 #include "vkRefUtil.hpp"
37 #include "vkTypeUtil.hpp"
38 #include "vkCmdUtil.hpp"
39 #include "vkObjUtil.hpp"
40 
41 #include "tcuImageCompare.hpp"
42 #include "tcuResultCollector.hpp"
43 #include "tcuTextureUtil.hpp"
44 
45 #include "deUniquePtr.hpp"
46 #include "deSharedPtr.hpp"
47 
48 using namespace vk;
49 
50 using tcu::UVec4;
51 using tcu::Vec4;
52 
53 using tcu::ConstPixelBufferAccess;
54 using tcu::PixelBufferAccess;
55 
56 using tcu::TestLog;
57 
58 using std::string;
59 using std::vector;
60 
61 namespace vkt
62 {
63 namespace
64 {
65 using namespace renderpass;
66 
createBufferMemory(const DeviceInterface & vk,VkDevice device,Allocator & allocator,VkBuffer buffer)67 de::MovePtr<Allocation> createBufferMemory (const DeviceInterface&	vk,
68 											VkDevice				device,
69 											Allocator&				allocator,
70 											VkBuffer				buffer)
71 {
72 	de::MovePtr<Allocation> allocation (allocator.allocate(getBufferMemoryRequirements(vk, device, buffer), MemoryRequirement::HostVisible));
73 	VK_CHECK(vk.bindBufferMemory(device, buffer, allocation->getMemory(), allocation->getOffset()));
74 	return allocation;
75 }
76 
createSparseImageAndMemory(const DeviceInterface & vk,VkDevice device,const VkPhysicalDevice physicalDevice,const InstanceInterface & instance,Allocator & allocator,vector<de::SharedPtr<Allocation>> & allocations,deUint32 universalQueueFamilyIndex,VkQueue sparseQueue,deUint32 sparseQueueFamilyIndex,const VkSemaphore & bindSemaphore,VkFormat format,deUint32 width,deUint32 height)77 Move<VkImage> createSparseImageAndMemory (const DeviceInterface&				vk,
78 										  VkDevice								device,
79 										  const VkPhysicalDevice				physicalDevice,
80 										  const InstanceInterface&				instance,
81 										  Allocator&							allocator,
82 										  vector<de::SharedPtr<Allocation> >&	allocations,
83 										  deUint32								universalQueueFamilyIndex,
84 										  VkQueue								sparseQueue,
85 										  deUint32								sparseQueueFamilyIndex,
86 										  const VkSemaphore&					bindSemaphore,
87 										  VkFormat								format,
88 										  deUint32								width,
89 										  deUint32								height)
90 {
91 	deUint32				queueFamilyIndices[]	= {universalQueueFamilyIndex, sparseQueueFamilyIndex};
92 	const VkSharingMode		sharingMode             = universalQueueFamilyIndex != sparseQueueFamilyIndex ? VK_SHARING_MODE_CONCURRENT : VK_SHARING_MODE_EXCLUSIVE;
93 
94 	const VkExtent3D		imageExtent				=
95 	{
96 		width,
97 		height,
98 		1u
99 	};
100 
101 	const VkImageCreateInfo	imageCreateInfo			=
102 	{
103 		VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
104 		DE_NULL,
105 		VK_IMAGE_CREATE_SPARSE_BINDING_BIT | VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT,
106 		VK_IMAGE_TYPE_2D,
107 		format,
108 		imageExtent,
109 		1u,
110 		1u,
111 		VK_SAMPLE_COUNT_1_BIT,
112 		VK_IMAGE_TILING_OPTIMAL,
113 		VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
114 		sharingMode,
115 		sharingMode == VK_SHARING_MODE_CONCURRENT ? 2u : 1u,
116 		queueFamilyIndices,
117 		VK_IMAGE_LAYOUT_UNDEFINED
118 	};
119 
120 	if (!checkSparseImageFormatSupport(physicalDevice, instance, imageCreateInfo))
121 		TCU_THROW(NotSupportedError, "The image format does not support sparse operations");
122 
123 	Move<VkImage> destImage = createImage(vk, device, &imageCreateInfo);
124 	allocateAndBindSparseImage(vk, device, physicalDevice, instance, imageCreateInfo, bindSemaphore, sparseQueue, allocator, allocations, mapVkFormat(format), *destImage);
125 
126 	return destImage;
127 }
128 
createImageView(const DeviceInterface & vk,VkDevice device,VkImageViewCreateFlags flags,VkImage image,VkImageViewType viewType,VkFormat format,VkComponentMapping components,VkImageSubresourceRange subresourceRange)129 Move<VkImageView> createImageView (const DeviceInterface&	vk,
130 								   VkDevice					device,
131 								   VkImageViewCreateFlags	flags,
132 								   VkImage					image,
133 								   VkImageViewType			viewType,
134 								   VkFormat					format,
135 								   VkComponentMapping		components,
136 								   VkImageSubresourceRange	subresourceRange)
137 {
138 	const VkImageViewCreateInfo pCreateInfo =
139 	{
140 		VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
141 		DE_NULL,
142 		flags,
143 		image,
144 		viewType,
145 		format,
146 		components,
147 		subresourceRange,
148 	};
149 
150 	return createImageView(vk, device, &pCreateInfo);
151 }
152 
createImageView(const DeviceInterface & vkd,VkDevice device,VkImage image,VkFormat format,VkImageAspectFlags aspect)153 Move<VkImageView> createImageView (const DeviceInterface&	vkd,
154 								   VkDevice					device,
155 								   VkImage					image,
156 								   VkFormat					format,
157 								   VkImageAspectFlags		aspect)
158 {
159 	const VkImageSubresourceRange range =
160 	{
161 		aspect,
162 		0u,
163 		1u,
164 		0u,
165 		1u
166 	};
167 
168 	return createImageView(vkd, device, 0u, image, VK_IMAGE_VIEW_TYPE_2D, format, makeComponentMappingRGBA(), range);
169 }
170 
createBuffer(const DeviceInterface & vkd,VkDevice device,VkFormat format,deUint32 width,deUint32 height)171 Move<VkBuffer> createBuffer (const DeviceInterface&		vkd,
172 							 VkDevice					device,
173 							 VkFormat					format,
174 							 deUint32					width,
175 							 deUint32					height)
176 {
177 	const VkBufferUsageFlags	bufferUsage			(VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT);
178 	const VkDeviceSize			pixelSize			= mapVkFormat(format).getPixelSize();
179 	const VkBufferCreateInfo	createInfo			=
180 	{
181 		VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
182 		DE_NULL,
183 		0u,
184 
185 		width * height * pixelSize,
186 		bufferUsage,
187 
188 		VK_SHARING_MODE_EXCLUSIVE,
189 		0u,
190 		DE_NULL
191 	};
192 
193 	return createBuffer(vkd, device, &createInfo);
194 }
195 
196 template<typename AttachmentDesc, typename AttachmentRef, typename SubpassDesc, typename SubpassDep, typename RenderPassCreateInfo>
createRenderPass(const DeviceInterface & vkd,VkDevice device,VkFormat dstFormat)197 Move<VkRenderPass> createRenderPass (const DeviceInterface&	vkd,
198 									 VkDevice				device,
199 									 VkFormat				dstFormat)
200 {
201 	const AttachmentRef		dstAttachmentRef		//  VkAttachmentReference										||  VkAttachmentReference2KHR
202 	(
203 													//																||  VkStructureType						sType;
204 		DE_NULL,									//																||  const void*							pNext;
205 		0u,											//  deUint32						attachment;					||  deUint32							attachment;
206 		VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,	//  VkImageLayout					layout;						||  VkImageLayout						layout;
207 		0u											//																||  VkImageAspectFlags					aspectMask;
208 	);
209 	const AttachmentDesc	dstAttachment			//  VkAttachmentDescription										||  VkAttachmentDescription2KHR
210 	(
211 													//																||  VkStructureType						sType;
212 		DE_NULL,									//																||  const void*							pNext;
213 		0u,											//  VkAttachmentDescriptionFlags	flags;						||  VkAttachmentDescriptionFlags		flags;
214 		dstFormat,									//  VkFormat						format;						||  VkFormat							format;
215 		VK_SAMPLE_COUNT_1_BIT,						//  VkSampleCountFlagBits			samples;					||  VkSampleCountFlagBits				samples;
216 		VK_ATTACHMENT_LOAD_OP_DONT_CARE,			//  VkAttachmentLoadOp				loadOp;						||  VkAttachmentLoadOp					loadOp;
217 		VK_ATTACHMENT_STORE_OP_STORE,				//  VkAttachmentStoreOp				storeOp;					||  VkAttachmentStoreOp					storeOp;
218 		VK_ATTACHMENT_LOAD_OP_DONT_CARE,			//  VkAttachmentLoadOp				stencilLoadOp;				||  VkAttachmentLoadOp					stencilLoadOp;
219 		VK_ATTACHMENT_STORE_OP_DONT_CARE,			//  VkAttachmentStoreOp				stencilStoreOp;				||  VkAttachmentStoreOp					stencilStoreOp;
220 		VK_IMAGE_LAYOUT_UNDEFINED,					//  VkImageLayout					initialLayout;				||  VkImageLayout						initialLayout;
221 		VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL	//  VkImageLayout					finalLayout;				||  VkImageLayout						finalLayout;
222 	);
223 	const SubpassDesc			subpass				//  VkSubpassDescription										||  VkSubpassDescription2KHR
224 	(
225 													//																||  VkStructureType						sType;
226 		DE_NULL,									//																||  const void*							pNext;
227 		(VkSubpassDescriptionFlags)0,				//  VkSubpassDescriptionFlags		flags;						||  VkSubpassDescriptionFlags			flags;
228 		VK_PIPELINE_BIND_POINT_GRAPHICS,			//  VkPipelineBindPoint				pipelineBindPoint;			||  VkPipelineBindPoint					pipelineBindPoint;
229 		0u,											//																||  deUint32							viewMask;
230 		0u,											//  deUint32						inputAttachmentCount;		||  deUint32							inputAttachmentCount;
231 		DE_NULL,									//  const VkAttachmentReference*	pInputAttachments;			||  const VkAttachmentReference2KHR*	pInputAttachments;
232 		1u,											//  deUint32						colorAttachmentCount;		||  deUint32							colorAttachmentCount;
233 		&dstAttachmentRef,							//  const VkAttachmentReference*	pColorAttachments;			||  const VkAttachmentReference2KHR*	pColorAttachments;
234 		DE_NULL,									//  const VkAttachmentReference*	pResolveAttachments;		||  const VkAttachmentReference2KHR*	pResolveAttachments;
235 		DE_NULL,									//  const VkAttachmentReference*	pDepthStencilAttachment;	||  const VkAttachmentReference2KHR*	pDepthStencilAttachment;
236 		0u,											//  deUint32						preserveAttachmentCount;	||  deUint32							preserveAttachmentCount;
237 		DE_NULL										//  const deUint32*					pPreserveAttachments;		||  const deUint32*						pPreserveAttachments;
238 	);
239 	const RenderPassCreateInfo	renderPassCreator	//  VkRenderPassCreateInfo										||  VkRenderPassCreateInfo2KHR
240 	(
241 													//  VkStructureType					sType;						||  VkStructureType						sType;
242 		DE_NULL,									//  const void*						pNext;						||  const void*							pNext;
243 		(VkRenderPassCreateFlags)0u,				//  VkRenderPassCreateFlags			flags;						||  VkRenderPassCreateFlags				flags;
244 		1u,											//  deUint32						attachmentCount;			||  deUint32							attachmentCount;
245 		&dstAttachment,								//  const VkAttachmentDescription*	pAttachments;				||  const VkAttachmentDescription2KHR*	pAttachments;
246 		1u,											//  deUint32						subpassCount;				||  deUint32							subpassCount;
247 		&subpass,									//  const VkSubpassDescription*		pSubpasses;					||  const VkSubpassDescription2KHR*		pSubpasses;
248 		0u,											//  deUint32						dependencyCount;			||  deUint32							dependencyCount;
249 		DE_NULL,									//  const VkSubpassDependency*		pDependencies;				||  const VkSubpassDependency2KHR*		pDependencies;
250 		0u,											//																||  deUint32							correlatedViewMaskCount;
251 		DE_NULL										//																||  const deUint32*						pCorrelatedViewMasks;
252 	);
253 
254 	return renderPassCreator.createRenderPass(vkd, device);
255 }
256 
createRenderPass(const DeviceInterface & vkd,VkDevice device,VkFormat dstFormat,const RenderPassType renderPassType)257 Move<VkRenderPass> createRenderPass (const DeviceInterface&	vkd,
258 									 VkDevice				device,
259 									 VkFormat				dstFormat,
260 									 const RenderPassType	renderPassType)
261 {
262 	switch (renderPassType)
263 	{
264 		case RENDERPASS_TYPE_LEGACY:
265 			return createRenderPass<AttachmentDescription1, AttachmentReference1, SubpassDescription1, SubpassDependency1, RenderPassCreateInfo1>(vkd, device, dstFormat);
266 		case RENDERPASS_TYPE_RENDERPASS2:
267 			return createRenderPass<AttachmentDescription2, AttachmentReference2, SubpassDescription2, SubpassDependency2, RenderPassCreateInfo2>(vkd, device, dstFormat);
268 		default:
269 			TCU_THROW(InternalError, "Impossible");
270 	}
271 }
272 
createFramebuffer(const DeviceInterface & vkd,VkDevice device,VkRenderPass renderPass,VkImageView dstImageView,deUint32 width,deUint32 height)273 Move<VkFramebuffer> createFramebuffer (const DeviceInterface&	vkd,
274 									   VkDevice					device,
275 									   VkRenderPass				renderPass,
276 									   VkImageView				dstImageView,
277 									   deUint32					width,
278 									   deUint32					height)
279 {
280 	const VkFramebufferCreateInfo createInfo =
281 	{
282 		VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
283 		DE_NULL,
284 		0u,
285 
286 		renderPass,
287 		1u,
288 		&dstImageView,
289 
290 		width,
291 		height,
292 		1u
293 	};
294 
295 	return createFramebuffer(vkd, device, &createInfo);
296 }
297 
createRenderPipelineLayout(const DeviceInterface & vkd,VkDevice device)298 Move<VkPipelineLayout> createRenderPipelineLayout (const DeviceInterface&	vkd,
299 												   VkDevice					device)
300 {
301 	const VkPipelineLayoutCreateInfo createInfo =
302 	{
303 		VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
304 		DE_NULL,
305 		(vk::VkPipelineLayoutCreateFlags)0,
306 
307 		0u,
308 		DE_NULL,
309 
310 		0u,
311 		DE_NULL
312 	};
313 
314 	return createPipelineLayout(vkd, device, &createInfo);
315 }
316 
createRenderPipeline(const DeviceInterface & vkd,VkDevice device,VkRenderPass renderPass,VkPipelineLayout pipelineLayout,const BinaryCollection & binaryCollection,deUint32 width,deUint32 height)317 Move<VkPipeline> createRenderPipeline (const DeviceInterface&							vkd,
318 									   VkDevice											device,
319 									   VkRenderPass										renderPass,
320 									   VkPipelineLayout									pipelineLayout,
321 									   const BinaryCollection&							binaryCollection,
322 									   deUint32											width,
323 									   deUint32											height)
324 {
325 	const Unique<VkShaderModule>					vertexShaderModule				(createShaderModule(vkd, device, binaryCollection.get("quad-vert"), 0u));
326 	const Unique<VkShaderModule>					fragmentShaderModule			(createShaderModule(vkd, device, binaryCollection.get("quad-frag"), 0u));
327 
328 	const VkPipelineVertexInputStateCreateInfo		vertexInputState				=
329 	{
330 		VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
331 		DE_NULL,
332 		(VkPipelineVertexInputStateCreateFlags)0u,
333 
334 		0u,
335 		DE_NULL,
336 
337 		0u,
338 		DE_NULL
339 	};
340 
341 	const std::vector<VkViewport>					viewports						(1, makeViewport(tcu::UVec2(width, height)));
342 	const std::vector<VkRect2D>						scissors						(1, makeRect2D(tcu::UVec2(width, height)));
343 
344 	return makeGraphicsPipeline(vkd,									// const DeviceInterface&                        vk
345 								device,									// const VkDevice                                device
346 								pipelineLayout,							// const VkPipelineLayout                        pipelineLayout
347 								*vertexShaderModule,					// const VkShaderModule                          vertexShaderModule
348 								DE_NULL,								// const VkShaderModule                          tessellationControlShaderModule
349 								DE_NULL,								// const VkShaderModule                          tessellationEvalShaderModule
350 								DE_NULL,								// const VkShaderModule                          geometryShaderModule
351 								*fragmentShaderModule,					// const VkShaderModule                          fragmentShaderModule
352 								renderPass,								// const VkRenderPass                            renderPass
353 								viewports,								// const std::vector<VkViewport>&                viewports
354 								scissors,								// const std::vector<VkRect2D>&                  scissors
355 								VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,	// const VkPrimitiveTopology                     topology
356 								0u,										// const deUint32                                subpass
357 								0u,										// const deUint32                                patchControlPoints
358 								&vertexInputState);						// const VkPipelineVertexInputStateCreateInfo*   vertexInputStateCreateInfo
359 }
360 
361 struct TestConfig
362 {
TestConfigvkt::__anon143489790111::TestConfig363 				TestConfig		(VkFormat		format_,
364 								 RenderPassType	renderPassType_)
365 		: format			(format_)
366 		, renderPassType	(renderPassType_)
367 	{
368 	}
369 
370 	VkFormat		format;
371 	RenderPassType	renderPassType;
372 };
373 
374 class SparseRenderTargetTestInstance : public TestInstance
375 {
376 public:
377 											SparseRenderTargetTestInstance	(Context& context, TestConfig testConfig);
378 											~SparseRenderTargetTestInstance	(void);
379 
380 	tcu::TestStatus							iterate					(void);
381 
382 	template<typename RenderpassSubpass>
383 	tcu::TestStatus							iterateInternal			(void);
384 
385 private:
386 	const bool								m_extensionSupported;
387 	const RenderPassType					m_renderPassType;
388 
389 	const deUint32							m_width;
390 	const deUint32							m_height;
391 	const VkFormat							m_format;
392 
393 	vector<de::SharedPtr<Allocation> >		m_allocations;
394 
395 	const Unique<VkSemaphore>				m_bindSemaphore;
396 
397 	const Unique<VkImage>					m_dstImage;
398 	const Unique<VkImageView>				m_dstImageView;
399 
400 	const Unique<VkBuffer>					m_dstBuffer;
401 	const de::UniquePtr<Allocation>			m_dstBufferMemory;
402 
403 	const Unique<VkRenderPass>				m_renderPass;
404 	const Unique<VkFramebuffer>				m_framebuffer;
405 
406 	const Unique<VkPipelineLayout>			m_renderPipelineLayout;
407 	const Unique<VkPipeline>				m_renderPipeline;
408 
409 	const Unique<VkCommandPool>				m_commandPool;
410 	tcu::ResultCollector					m_resultCollector;
411 };
412 
SparseRenderTargetTestInstance(Context & context,TestConfig testConfig)413 SparseRenderTargetTestInstance::SparseRenderTargetTestInstance (Context& context, TestConfig testConfig)
414 	: TestInstance				(context)
415 	, m_extensionSupported		((testConfig.renderPassType == RENDERPASS_TYPE_RENDERPASS2) && context.requireDeviceFunctionality("VK_KHR_create_renderpass2"))
416 	, m_renderPassType			(testConfig.renderPassType)
417 	, m_width					(32u)
418 	, m_height					(32u)
419 	, m_format					(testConfig.format)
420 	, m_bindSemaphore			(createSemaphore(context.getDeviceInterface(), context.getDevice()))
421 	, m_dstImage				(createSparseImageAndMemory(context.getDeviceInterface(), context.getDevice(), context.getPhysicalDevice(), context.getInstanceInterface(), context.getDefaultAllocator(), m_allocations, context.getUniversalQueueFamilyIndex(), context.getSparseQueue(), context.getSparseQueueFamilyIndex(), *m_bindSemaphore, m_format, m_width, m_height))
422 	, m_dstImageView			(createImageView(context.getDeviceInterface(), context.getDevice(), *m_dstImage, m_format, VK_IMAGE_ASPECT_COLOR_BIT))
423 	, m_dstBuffer				(createBuffer(context.getDeviceInterface(), context.getDevice(), m_format, m_width, m_height))
424 	, m_dstBufferMemory			(createBufferMemory(context.getDeviceInterface(), context.getDevice(), context.getDefaultAllocator(), *m_dstBuffer))
425 	, m_renderPass				(createRenderPass(context.getDeviceInterface(), context.getDevice(), m_format, testConfig.renderPassType))
426 	, m_framebuffer				(createFramebuffer(context.getDeviceInterface(), context.getDevice(), *m_renderPass, *m_dstImageView, m_width, m_height))
427 	, m_renderPipelineLayout	(createRenderPipelineLayout(context.getDeviceInterface(), context.getDevice()))
428 	, m_renderPipeline			(createRenderPipeline(context.getDeviceInterface(), context.getDevice(), *m_renderPass, *m_renderPipelineLayout, context.getBinaryCollection(), m_width, m_height))
429 	, m_commandPool				(createCommandPool(context.getDeviceInterface(), context.getDevice(), VK_COMMAND_POOL_CREATE_TRANSIENT_BIT, context.getUniversalQueueFamilyIndex()))
430 {
431 }
432 
~SparseRenderTargetTestInstance(void)433 SparseRenderTargetTestInstance::~SparseRenderTargetTestInstance (void)
434 {
435 }
436 
iterate(void)437 tcu::TestStatus SparseRenderTargetTestInstance::iterate (void)
438 {
439 	switch (m_renderPassType)
440 	{
441 		case RENDERPASS_TYPE_LEGACY:
442 			return iterateInternal<RenderpassSubpass1>();
443 		case RENDERPASS_TYPE_RENDERPASS2:
444 			return iterateInternal<RenderpassSubpass2>();
445 		default:
446 			TCU_THROW(InternalError, "Impossible");
447 	}
448 }
449 
450 template<typename RenderpassSubpass>
iterateInternal(void)451 tcu::TestStatus SparseRenderTargetTestInstance::iterateInternal (void)
452 
453 {
454 	const DeviceInterface&								vkd					(m_context.getDeviceInterface());
455 	const Unique<VkCommandBuffer>						commandBuffer		(allocateCommandBuffer(vkd, m_context.getDevice(), *m_commandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
456 	const typename RenderpassSubpass::SubpassBeginInfo	subpassBeginInfo	(DE_NULL, VK_SUBPASS_CONTENTS_INLINE);
457 	const typename RenderpassSubpass::SubpassEndInfo	subpassEndInfo		(DE_NULL);
458 
459 	beginCommandBuffer(vkd, *commandBuffer);
460 
461 	{
462 		const VkRenderPassBeginInfo beginInfo =
463 		{
464 			VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
465 			DE_NULL,
466 
467 			*m_renderPass,
468 			*m_framebuffer,
469 
470 			{
471 				{ 0u, 0u },
472 				{ m_width, m_height }
473 			},
474 
475 			0u,
476 			DE_NULL
477 		};
478 		RenderpassSubpass::cmdBeginRenderPass(vkd, *commandBuffer, &beginInfo, &subpassBeginInfo);
479 	}
480 
481 	vkd.cmdBindPipeline(*commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, *m_renderPipeline);
482 	vkd.cmdDraw(*commandBuffer, 6u, 1u, 0u, 0u);
483 	RenderpassSubpass::cmdEndRenderPass(vkd, *commandBuffer, &subpassEndInfo);
484 
485 	copyImageToBuffer(vkd, *commandBuffer, *m_dstImage, *m_dstBuffer, tcu::IVec2(m_width, m_height));
486 
487 	endCommandBuffer(vkd, *commandBuffer);
488 
489 	submitCommandsAndWait(vkd, m_context.getDevice(), m_context.getUniversalQueue(), *commandBuffer);
490 
491 	{
492 		const tcu::TextureFormat			format			(mapVkFormat(m_format));
493 		const void* const					ptr				(m_dstBufferMemory->getHostPtr());
494 		const tcu::ConstPixelBufferAccess	access			(format, m_width, m_height, 1, ptr);
495 		tcu::TextureLevel					reference		(format, m_width, m_height);
496 		const tcu::TextureChannelClass		channelClass	(tcu::getTextureChannelClass(format.type));
497 
498 		switch (channelClass)
499 		{
500 			case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
501 			{
502 				const UVec4	bits	(tcu::getTextureFormatBitDepth(format).cast<deUint32>());
503 				const UVec4	color	(1u << (bits.x()-1), 1u << (bits.y()-2), 1u << (bits.z()-3), 0xffffffff);
504 
505 				for (deUint32 y = 0; y < m_height; y++)
506 				for (deUint32 x = 0; x < m_width; x++)
507 				{
508 					reference.getAccess().setPixel(color, x, y);
509 				}
510 
511 				if (!tcu::intThresholdCompare(m_context.getTestContext().getLog(), "", "", reference.getAccess(), access, UVec4(0u), tcu::COMPARE_LOG_ON_ERROR))
512 					m_resultCollector.fail("Compare failed.");
513 			}
514 			break;
515 
516 			case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
517 			{
518 				const UVec4	bits	(tcu::getTextureFormatBitDepth(format).cast<deUint32>());
519 				const UVec4	color	(1u << (bits.x()-2), 1u << (bits.y()-3), 1u << (bits.z()-4), 0xffffffff);
520 
521 				for (deUint32 y = 0; y < m_height; y++)
522 				for (deUint32 x = 0; x < m_width; x++)
523 				{
524 					reference.getAccess().setPixel(color, x, y);
525 				}
526 
527 				if (!tcu::intThresholdCompare(m_context.getTestContext().getLog(), "", "", reference.getAccess(), access, UVec4(0u), tcu::COMPARE_LOG_ON_ERROR))
528 					m_resultCollector.fail("Compare failed.");
529 			}
530 			break;
531 
532 			case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
533 			case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
534 			{
535 				const tcu::TextureFormatInfo	info		(tcu::getTextureFormatInfo(format));
536 				const Vec4						maxValue	(info.valueMax);
537 				const Vec4						color		(maxValue.x() / 2.0f, maxValue.y() / 4.0f, maxValue.z() / 8.0f, maxValue.w());
538 
539 				for (deUint32 y = 0; y < m_height; y++)
540 				for (deUint32 x = 0; x < m_width; x++)
541 				{
542 					if (tcu::isSRGB(format))
543 						reference.getAccess().setPixel(tcu::linearToSRGB(color), x, y);
544 					else
545 						reference.getAccess().setPixel(color, x, y);
546 				}
547 
548 				{
549 					// Allow error of 4 times the minimum presentable difference
550 					const Vec4 threshold (4.0f * 1.0f / ((UVec4(1u) << tcu::getTextureFormatMantissaBitDepth(format).cast<deUint32>()) - 1u).cast<float>());
551 
552 					if (!tcu::floatThresholdCompare(m_context.getTestContext().getLog(), "", "", reference.getAccess(), access, threshold, tcu::COMPARE_LOG_ON_ERROR))
553 						m_resultCollector.fail("Compare failed.");
554 				}
555 			}
556 			break;
557 
558 			case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
559 			{
560 				const Vec4 color(0.5f, 0.25f, 0.125f, 1.0f);
561 
562 				for (deUint32 y = 0; y < m_height; y++)
563 				for (deUint32 x = 0; x < m_width; x++)
564 				{
565 					if (tcu::isSRGB(format))
566 						reference.getAccess().setPixel(tcu::linearToSRGB(color), x, y);
567 					else
568 						reference.getAccess().setPixel(color, x, y);
569 				}
570 
571 				{
572 					// Convert target format ulps to float ulps and allow 64ulp differences
573 					const UVec4 threshold (64u * (UVec4(1u) << (UVec4(23) - tcu::getTextureFormatMantissaBitDepth(format).cast<deUint32>())));
574 
575 					if (!tcu::floatUlpThresholdCompare(m_context.getTestContext().getLog(), "", "", reference.getAccess(), access, threshold, tcu::COMPARE_LOG_ON_ERROR))
576 						m_resultCollector.fail("Compare failed.");
577 				}
578 			}
579 			break;
580 
581 			default:
582 				DE_FATAL("Unknown channel class");
583 		}
584 	}
585 
586 	return tcu::TestStatus(m_resultCollector.getResult(), m_resultCollector.getMessage());
587 }
588 
589 struct Programs
590 {
initvkt::__anon143489790111::Programs591 	void init (vk::SourceCollections& dst, TestConfig testConfig) const
592 	{
593 		std::ostringstream				fragmentShader;
594 		const VkFormat					format			(testConfig.format);
595 		const tcu::TextureFormat		texFormat		(mapVkFormat(format));
596 		const UVec4						bits			(tcu::getTextureFormatBitDepth(texFormat).cast<deUint32>());
597 		const tcu::TextureChannelClass	channelClass	(tcu::getTextureChannelClass(texFormat.type));
598 
599 		dst.glslSources.add("quad-vert") << glu::VertexSource(
600 			"#version 450\n"
601 			"out gl_PerVertex {\n"
602 			"\tvec4 gl_Position;\n"
603 			"};\n"
604 			"highp float;\n"
605 			"void main (void)\n"
606 			"{\n"
607 			"    gl_Position = vec4(((gl_VertexIndex + 2) / 3) % 2 == 0 ? -1.0 : 1.0,\n"
608 			"                       ((gl_VertexIndex + 1) / 3) % 2 == 0 ? -1.0 : 1.0, 0.0, 1.0);\n"
609 			"}\n");
610 
611 		switch (channelClass)
612 		{
613 			case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
614 			{
615 				fragmentShader <<
616 					"#version 450\n"
617 					"layout(location = 0) out highp uvec4 o_color;\n"
618 					"void main (void)\n"
619 					"{\n"
620 					"    o_color = uvec4(" << de::toString(1u << (bits.x()-1)) << ", " << de::toString(1u << (bits.y()-2)) << ", " << de::toString(1u << (bits.z()-3)) << ", 0xffffffff);"
621 					"}\n";
622 			}
623 			break;
624 
625 			case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
626 			{
627 				fragmentShader <<
628 					"#version 450\n"
629 					"layout(location = 0) out highp ivec4 o_color;\n"
630 					"void main (void)\n"
631 					"{\n"
632 					"    o_color = ivec4(" << de::toString(1u << (bits.x()-2)) << ", " << de::toString(1u << (bits.y()-3)) << ", " << de::toString(1u << (bits.z()-4)) << ", 0xffffffff);"
633 					"}\n";
634 			}
635 			break;
636 
637 			default:
638 			{
639 				fragmentShader <<
640 					"#version 450\n"
641 					"layout(location = 0) out highp vec4 o_color;\n"
642 					"void main (void)\n"
643 					"{\n"
644 					"    o_color = vec4(0.5, 0.25, 0.125, 1.0);\n"
645 					"}\n";
646 			}
647 			break;
648 		};
649 
650 		dst.glslSources.add("quad-frag") << glu::FragmentSource(fragmentShader.str());
651 	}
652 };
653 
formatToName(VkFormat format)654 std::string formatToName (VkFormat format)
655 {
656 	const std::string	formatStr	= de::toString(format);
657 	const std::string	prefix		= "VK_FORMAT_";
658 
659 	DE_ASSERT(formatStr.substr(0, prefix.length()) == prefix);
660 
661 	return de::toLower(formatStr.substr(prefix.length()));
662 }
663 
initTests(tcu::TestCaseGroup * group,const RenderPassType renderPassType)664 void initTests (tcu::TestCaseGroup* group, const RenderPassType renderPassType)
665 {
666 	static const VkFormat	formats[]	=
667 	{
668 		VK_FORMAT_R5G6B5_UNORM_PACK16,
669 		VK_FORMAT_R8_UNORM,
670 		VK_FORMAT_R8_SNORM,
671 		VK_FORMAT_R8_UINT,
672 		VK_FORMAT_R8_SINT,
673 		VK_FORMAT_R8G8_UNORM,
674 		VK_FORMAT_R8G8_SNORM,
675 		VK_FORMAT_R8G8_UINT,
676 		VK_FORMAT_R8G8_SINT,
677 		VK_FORMAT_R8G8B8A8_UNORM,
678 		VK_FORMAT_R8G8B8A8_SNORM,
679 		VK_FORMAT_R8G8B8A8_UINT,
680 		VK_FORMAT_R8G8B8A8_SINT,
681 		VK_FORMAT_R8G8B8A8_SRGB,
682 		VK_FORMAT_A8B8G8R8_UNORM_PACK32,
683 		VK_FORMAT_A8B8G8R8_SNORM_PACK32,
684 		VK_FORMAT_A8B8G8R8_UINT_PACK32,
685 		VK_FORMAT_A8B8G8R8_SINT_PACK32,
686 		VK_FORMAT_A8B8G8R8_SRGB_PACK32,
687 		VK_FORMAT_B8G8R8A8_UNORM,
688 		VK_FORMAT_B8G8R8A8_SRGB,
689 		VK_FORMAT_A2R10G10B10_UNORM_PACK32,
690 		VK_FORMAT_A2B10G10R10_UNORM_PACK32,
691 		VK_FORMAT_A2B10G10R10_UINT_PACK32,
692 		VK_FORMAT_R16_UNORM,
693 		VK_FORMAT_R16_SNORM,
694 		VK_FORMAT_R16_UINT,
695 		VK_FORMAT_R16_SINT,
696 		VK_FORMAT_R16_SFLOAT,
697 		VK_FORMAT_R16G16_UNORM,
698 		VK_FORMAT_R16G16_SNORM,
699 		VK_FORMAT_R16G16_UINT,
700 		VK_FORMAT_R16G16_SINT,
701 		VK_FORMAT_R16G16_SFLOAT,
702 		VK_FORMAT_R16G16B16A16_UNORM,
703 		VK_FORMAT_R16G16B16A16_SNORM,
704 		VK_FORMAT_R16G16B16A16_UINT,
705 		VK_FORMAT_R16G16B16A16_SINT,
706 		VK_FORMAT_R16G16B16A16_SFLOAT,
707 		VK_FORMAT_R32_UINT,
708 		VK_FORMAT_R32_SINT,
709 		VK_FORMAT_R32_SFLOAT,
710 		VK_FORMAT_R32G32_UINT,
711 		VK_FORMAT_R32G32_SINT,
712 		VK_FORMAT_R32G32_SFLOAT,
713 		VK_FORMAT_R32G32B32A32_UINT,
714 		VK_FORMAT_R32G32B32A32_SINT,
715 		VK_FORMAT_R32G32B32A32_SFLOAT
716 	};
717 
718 	tcu::TestContext&		testCtx		(group->getTestContext());
719 
720 	for (size_t formatNdx = 0; formatNdx < DE_LENGTH_OF_ARRAY(formats); formatNdx++)
721 	{
722 		const VkFormat		format		(formats[formatNdx]);
723 		const TestConfig	testConfig	(format, renderPassType);
724 		string				testName	(formatToName(format));
725 
726 		group->addChild(new InstanceFactory1<SparseRenderTargetTestInstance, TestConfig, Programs>(testCtx, tcu::NODETYPE_SELF_VALIDATE, testName.c_str(), testName.c_str(), testConfig));
727 	}
728 }
729 
730 } // anonymous
731 
createRenderPassSparseRenderTargetTests(tcu::TestContext & testCtx)732 tcu::TestCaseGroup* createRenderPassSparseRenderTargetTests (tcu::TestContext& testCtx)
733 {
734 	return createTestGroup(testCtx, "sparserendertarget", "Sparse render target tests", initTests, RENDERPASS_TYPE_LEGACY);
735 }
736 
createRenderPass2SparseRenderTargetTests(tcu::TestContext & testCtx)737 tcu::TestCaseGroup* createRenderPass2SparseRenderTargetTests (tcu::TestContext& testCtx)
738 {
739 	return createTestGroup(testCtx, "sparserendertarget", "Sparse render target tests", initTests, RENDERPASS_TYPE_RENDERPASS2);
740 }
741 } // vkt
742