1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2016 The Khronos Group Inc.
6  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  *//*!
21  * \file
22  * \brief Instanced Draw Tests
23  *//*--------------------------------------------------------------------*/
24 
25 #include "vktDrawInstancedTests.hpp"
26 
27 #include <climits>
28 
29 #include "deSharedPtr.hpp"
30 #include "rrRenderer.hpp"
31 #include "tcuImageCompare.hpp"
32 #include "tcuRGBA.hpp"
33 #include "tcuTextureUtil.hpp"
34 #include "vkImageUtil.hpp"
35 #include "vkPrograms.hpp"
36 #include "vkCmdUtil.hpp"
37 #include "vkTypeUtil.hpp"
38 #include "vkQueryUtil.hpp"
39 #include "vktDrawBufferObjectUtil.hpp"
40 #include "vktDrawCreateInfoUtil.hpp"
41 #include "vktDrawImageObjectUtil.hpp"
42 #include "vktDrawTestCaseUtil.hpp"
43 
44 namespace vkt
45 {
46 namespace Draw
47 {
48 namespace
49 {
50 
51 static const int	QUAD_GRID_SIZE	= 8;
52 static const int	WIDTH			= 128;
53 static const int	HEIGHT			= 128;
54 
55 struct TestParams
56 {
57 	enum DrawFunction
58 	{
59 		FUNCTION_DRAW = 0,
60 		FUNCTION_DRAW_INDEXED,
61 		FUNCTION_DRAW_INDIRECT,
62 		FUNCTION_DRAW_INDEXED_INDIRECT,
63 
64 		FUNTION_LAST
65 	};
66 
67 	DrawFunction			function;
68 	vk::VkPrimitiveTopology	topology;
69 
70 	deBool					testAttribDivisor;
71 	deUint32				attribDivisor;
72 
73 	deBool					testMultiview;
74 };
75 
76 struct VertexPositionAndColor
77 {
VertexPositionAndColorvkt::Draw::__anonbc71c0ab0111::VertexPositionAndColor78 				VertexPositionAndColor (tcu::Vec4 position_, tcu::Vec4 color_)
79 					: position	(position_)
80 					, color		(color_)
81 				{
82 				}
83 
84 	tcu::Vec4	position;
85 	tcu::Vec4	color;
86 };
87 
operator <<(std::ostream & str,TestParams const & v)88 std::ostream & operator<<(std::ostream & str, TestParams const & v)
89 {
90 	std::ostringstream string;
91 	switch (v.function)
92 	{
93 		case TestParams::FUNCTION_DRAW:
94 			string << "draw";
95 			break;
96 		case TestParams::FUNCTION_DRAW_INDEXED:
97 			string << "draw_indexed";
98 			break;
99 		case TestParams::FUNCTION_DRAW_INDIRECT:
100 			string << "draw_indirect";
101 			break;
102 		case TestParams::FUNCTION_DRAW_INDEXED_INDIRECT:
103 			string << "draw_indexed_indirect";
104 			break;
105 		default:
106 			DE_ASSERT(false);
107 	}
108 
109 	string << "_" << de::toString(v.topology);
110 
111 	if (v.testAttribDivisor)
112 		string << "_attrib_divisor_" << v.attribDivisor;
113 
114 	if (v.testMultiview)
115 		string << "_multiview";
116 
117 	return str << string.str();
118 }
119 
mapVkPrimitiveTopology(vk::VkPrimitiveTopology primitiveTopology)120 rr::PrimitiveType mapVkPrimitiveTopology (vk::VkPrimitiveTopology primitiveTopology)
121 {
122 	switch (primitiveTopology)
123 	{
124 		case vk::VK_PRIMITIVE_TOPOLOGY_POINT_LIST:						return rr::PRIMITIVETYPE_POINTS;
125 		case vk::VK_PRIMITIVE_TOPOLOGY_LINE_LIST:						return rr::PRIMITIVETYPE_LINES;
126 		case vk::VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:						return rr::PRIMITIVETYPE_LINE_STRIP;
127 		case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:					return rr::PRIMITIVETYPE_TRIANGLES;
128 		case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:					return rr::PRIMITIVETYPE_TRIANGLE_FAN;
129 		case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:					return rr::PRIMITIVETYPE_TRIANGLE_STRIP;
130 		case vk::VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:		return rr::PRIMITIVETYPE_LINES_ADJACENCY;
131 		case vk::VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:		return rr::PRIMITIVETYPE_LINE_STRIP_ADJACENCY;
132 		case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:	return rr::PRIMITIVETYPE_TRIANGLES_ADJACENCY;
133 		case vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:	return rr::PRIMITIVETYPE_TRIANGLE_STRIP_ADJACENCY;
134 		default:
135 			DE_ASSERT(false);
136 	}
137 	return rr::PRIMITIVETYPE_LAST;
138 }
139 
140 template<typename T>
createAndUploadBuffer(const std::vector<T> data,const vk::DeviceInterface & vk,const Context & context,vk::VkBufferUsageFlags usage)141 de::SharedPtr<Buffer> createAndUploadBuffer(const std::vector<T> data, const vk::DeviceInterface& vk, const Context& context, vk::VkBufferUsageFlags usage)
142 {
143 	const vk::VkDeviceSize dataSize = data.size() * sizeof(T);
144 	de::SharedPtr<Buffer> buffer = Buffer::createAndAlloc(vk, context.getDevice(),
145 														  BufferCreateInfo(dataSize, usage),
146 														  context.getDefaultAllocator(),
147 														  vk::MemoryRequirement::HostVisible);
148 
149 	deUint8* ptr = reinterpret_cast<deUint8*>(buffer->getBoundMemory().getHostPtr());
150 
151 	deMemcpy(ptr, &data[0], static_cast<size_t>(dataSize));
152 
153 	vk::flushAlloc(vk, context.getDevice(), buffer->getBoundMemory());
154 	return buffer;
155 }
156 
157 class TestVertShader : public rr::VertexShader
158 {
159 public:
TestVertShader(int numInstances,int firstInstance)160 	TestVertShader (int numInstances, int firstInstance)
161 		: rr::VertexShader	(3, 1)
162 		, m_numInstances	(numInstances)
163 		, m_firstInstance	(firstInstance)
164 	{
165 		m_inputs[0].type	= rr::GENERICVECTYPE_FLOAT;
166 		m_inputs[1].type	= rr::GENERICVECTYPE_FLOAT;
167 		m_inputs[2].type	= rr::GENERICVECTYPE_FLOAT;
168 		m_outputs[0].type	= rr::GENERICVECTYPE_FLOAT;
169 	}
170 
shadeVertices(const rr::VertexAttrib * inputs,rr::VertexPacket * const * packets,const int numPackets) const171 	void shadeVertices (const rr::VertexAttrib* inputs,
172 						rr::VertexPacket* const* packets,
173 						const int numPackets) const
174 	{
175 		for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx)
176 		{
177 			const int		instanceNdx		= packets[packetNdx]->instanceNdx + m_firstInstance;
178 			const tcu::Vec4	position		= rr::readVertexAttribFloat(inputs[0], packets[packetNdx]->instanceNdx,	packets[packetNdx]->vertexNdx, m_firstInstance);
179 			const tcu::Vec4	color			= rr::readVertexAttribFloat(inputs[1], packets[packetNdx]->instanceNdx,	packets[packetNdx]->vertexNdx, m_firstInstance);
180 			const tcu::Vec4	color2			= rr::readVertexAttribFloat(inputs[2], packets[packetNdx]->instanceNdx, packets[packetNdx]->vertexNdx, m_firstInstance);
181 			packets[packetNdx]->position	= position + tcu::Vec4((float)(packets[packetNdx]->instanceNdx * 2.0 / m_numInstances), 0.0, 0.0, 0.0);
182 			packets[packetNdx]->outputs[0]	= color + tcu::Vec4((float)instanceNdx / (float)m_numInstances, 0.0, 0.0, 1.0) + color2;
183 		}
184 	}
185 
186 private:
187 	const int m_numInstances;
188 	const int m_firstInstance;
189 };
190 
191 class TestFragShader : public rr::FragmentShader
192 {
193 public:
TestFragShader(void)194 	TestFragShader (void)
195 		: rr::FragmentShader(1, 1)
196 	{
197 		m_inputs[0].type	= rr::GENERICVECTYPE_FLOAT;
198 		m_outputs[0].type	= rr::GENERICVECTYPE_FLOAT;
199 	}
200 
shadeFragments(rr::FragmentPacket * packets,const int numPackets,const rr::FragmentShadingContext & context) const201 	void shadeFragments (rr::FragmentPacket* packets,
202 						 const int numPackets,
203 						 const rr::FragmentShadingContext& context) const
204 	{
205 		for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx)
206 		{
207 			rr::FragmentPacket& packet = packets[packetNdx];
208 			for (int fragNdx = 0; fragNdx < rr::NUM_FRAGMENTS_PER_PACKET; ++fragNdx)
209 			{
210 				const tcu::Vec4 color = rr::readVarying<float>(packet, context, 0, fragNdx);
211 				rr::writeFragmentOutput(context, packetNdx, fragNdx, 0, color);
212 			}
213 		}
214 	}
215 };
216 
217 class InstancedDrawInstance : public TestInstance
218 {
219 public:
220 												InstancedDrawInstance	(Context& context, TestParams params);
221 	virtual	tcu::TestStatus						iterate					(void);
222 
223 private:
224 	void										prepareVertexData		(int instanceCount, int firstInstance, int instanceDivisor);
225 
226 	const TestParams							m_params;
227 	const vk::DeviceInterface&					m_vk;
228 
229 	vk::VkFormat								m_colorAttachmentFormat;
230 
231 	vk::Move<vk::VkPipeline>					m_pipeline;
232 	vk::Move<vk::VkPipelineLayout>				m_pipelineLayout;
233 
234 	de::SharedPtr<Image>						m_colorTargetImage;
235 	vk::Move<vk::VkImageView>					m_colorTargetView;
236 
237 	PipelineCreateInfo::VertexInputState		m_vertexInputState;
238 
239 	vk::Move<vk::VkCommandPool>					m_cmdPool;
240 	vk::Move<vk::VkCommandBuffer>				m_cmdBuffer;
241 
242 	vk::Move<vk::VkFramebuffer>					m_framebuffer;
243 	vk::Move<vk::VkRenderPass>					m_renderPass;
244 
245 	// Vertex data
246 	std::vector<VertexPositionAndColor>			m_data;
247 	std::vector<deUint32>						m_indexes;
248 	std::vector<tcu::Vec4>						m_instancedColor;
249 };
250 
251 class InstancedDrawCase : public TestCase
252 {
253 public:
InstancedDrawCase(tcu::TestContext & testCtx,const std::string & name,const std::string & desc,TestParams params)254 	InstancedDrawCase (tcu::TestContext&	testCtx,
255 					   const std::string&	name,
256 					   const std::string&	desc,
257 					   TestParams			params)
258 		: TestCase	(testCtx, name, desc)
259 		, m_params	(params)
260 	{
261 		m_vertexShader = "#version 430\n"
262 				"layout(location = 0) in vec4 in_position;\n"
263 				"layout(location = 1) in vec4 in_color;\n"
264 				"layout(location = 2) in vec4 in_color_2;\n"
265 				"layout(push_constant) uniform TestParams {\n"
266 				"	float firstInstance;\n"
267 				"	float instanceCount;\n"
268 				"} params;\n"
269 				"layout(location = 0) out vec4 out_color;\n"
270 				"out gl_PerVertex {\n"
271 				"    vec4  gl_Position;\n"
272 				"    float gl_PointSize;\n"
273 				"};\n"
274 				"void main() {\n"
275 				"    gl_PointSize = 1.0;\n"
276 				"    gl_Position  = in_position + vec4(float(gl_InstanceIndex - params.firstInstance) * 2.0 / params.instanceCount, 0.0, 0.0, 0.0);\n"
277 				"    out_color    = in_color + vec4(float(gl_InstanceIndex) / params.instanceCount, 0.0, 0.0, 1.0) + in_color_2;\n"
278 				"}\n";
279 
280 		m_fragmentShader = "#version 430\n"
281 				"layout(location = 0) in vec4 in_color;\n"
282 				"layout(location = 0) out vec4 out_color;\n"
283 				"void main()\n"
284 				"{\n"
285 				"    out_color = in_color;\n"
286 				"}\n";
287 	}
288 
checkSupport(Context & context) const289 	virtual void	checkSupport	(Context& context) const
290 	{
291 		if (m_params.testAttribDivisor)
292 		{
293 			context.requireDeviceFunctionality("VK_EXT_vertex_attribute_divisor");
294 
295 			const vk::VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT& vertexAttributeDivisorFeatures = context.getVertexAttributeDivisorFeaturesEXT();
296 
297 			if (m_params.attribDivisor != 1 && !vertexAttributeDivisorFeatures.vertexAttributeInstanceRateDivisor)
298 				TCU_THROW(NotSupportedError, "Implementation does not support vertexAttributeInstanceRateDivisor");
299 
300 			if (m_params.attribDivisor == 0 && !vertexAttributeDivisorFeatures.vertexAttributeInstanceRateZeroDivisor)
301 				TCU_THROW(NotSupportedError, "Implementation does not support vertexAttributeInstanceRateDivisorZero");
302 
303 			if (m_params.testMultiview)
304 			{
305 				context.requireDeviceFunctionality("VK_KHR_multiview");
306 
307 				const vk::VkPhysicalDeviceMultiviewFeatures& multiviewFeatures = context.getMultiviewFeatures();
308 
309 				if (!multiviewFeatures.multiview)
310 					TCU_THROW(NotSupportedError, "Implementation does not support multiview feature");
311 			}
312 		}
313 
314 		if (m_params.topology == vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN &&
315 			context.isDeviceFunctionalitySupported("VK_KHR_portability_subset") &&
316 			!context.getPortabilitySubsetFeatures().triangleFans)
317 		{
318 			TCU_THROW(NotSupportedError, "VK_KHR_portability_subset: Triangle fans are not supported by this implementation");
319 		}
320 	}
321 
createInstance(Context & context) const322 	TestInstance*	createInstance	(Context& context) const
323 	{
324 		return new InstancedDrawInstance(context, m_params);
325 	}
326 
initPrograms(vk::SourceCollections & programCollection) const327 	virtual void	initPrograms	(vk::SourceCollections& programCollection) const
328 	{
329 		programCollection.glslSources.add("InstancedDrawVert") << glu::VertexSource(m_vertexShader);
330 		programCollection.glslSources.add("InstancedDrawFrag") << glu::FragmentSource(m_fragmentShader);
331 	}
332 
333 private:
334 	const TestParams	m_params;
335 	std::string			m_vertexShader;
336 	std::string			m_fragmentShader;
337 };
338 
InstancedDrawInstance(Context & context,TestParams params)339 InstancedDrawInstance::InstancedDrawInstance(Context &context, TestParams params)
340 	: TestInstance				(context)
341 	, m_params					(params)
342 	, m_vk						(context.getDeviceInterface())
343 	, m_colorAttachmentFormat	(vk::VK_FORMAT_R8G8B8A8_UNORM)
344 {
345 	const vk::VkDevice device				= m_context.getDevice();
346 	const deUint32 queueFamilyIndex			= m_context.getUniversalQueueFamilyIndex();
347 
348 	const vk::VkPushConstantRange pushConstantRange = {
349 		vk::VK_SHADER_STAGE_VERTEX_BIT,				// VkShaderStageFlags    stageFlags;
350 		0u,											// uint32_t              offset;
351 		(deUint32)sizeof(float) * 2,				// uint32_t              size;
352 	};
353 
354 	const PipelineLayoutCreateInfo pipelineLayoutCreateInfo(0, DE_NULL, 1, &pushConstantRange);
355 	m_pipelineLayout						= vk::createPipelineLayout(m_vk, device, &pipelineLayoutCreateInfo);
356 
357 	deUint32 arrayLayers = m_params.testMultiview ? 2 : 1;
358 	const vk::VkExtent3D targetImageExtent	= { WIDTH, HEIGHT, 1 };
359 	const ImageCreateInfo targetImageCreateInfo(vk::VK_IMAGE_TYPE_2D, m_colorAttachmentFormat, targetImageExtent, 1, arrayLayers, vk::VK_SAMPLE_COUNT_1_BIT,
360 		vk::VK_IMAGE_TILING_OPTIMAL, vk::VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | vk::VK_IMAGE_USAGE_TRANSFER_SRC_BIT | vk::VK_IMAGE_USAGE_TRANSFER_DST_BIT);
361 
362 	m_colorTargetImage						= Image::createAndAlloc(m_vk, device, targetImageCreateInfo, m_context.getDefaultAllocator(), m_context.getUniversalQueueFamilyIndex());
363 
364 	const enum vk::VkImageViewType imageViewType = m_params.testMultiview ? vk::VK_IMAGE_VIEW_TYPE_2D_ARRAY : vk::VK_IMAGE_VIEW_TYPE_2D;
365 	ImageSubresourceRange subresourceRange = ImageSubresourceRange(vk::VK_IMAGE_ASPECT_COLOR_BIT);
366 
367 	if (m_params.testMultiview)
368 		subresourceRange.layerCount = 2;
369 
370 	const ImageViewCreateInfo colorTargetViewInfo(m_colorTargetImage->object(), imageViewType, m_colorAttachmentFormat, subresourceRange);
371 	m_colorTargetView						= vk::createImageView(m_vk, device, &colorTargetViewInfo);
372 
373 	RenderPassCreateInfo renderPassCreateInfo;
374 	renderPassCreateInfo.addAttachment(AttachmentDescription(m_colorAttachmentFormat,
375 															 vk::VK_SAMPLE_COUNT_1_BIT,
376 															 vk::VK_ATTACHMENT_LOAD_OP_LOAD,
377 															 vk::VK_ATTACHMENT_STORE_OP_STORE,
378 															 vk::VK_ATTACHMENT_LOAD_OP_DONT_CARE,
379 															 vk::VK_ATTACHMENT_STORE_OP_STORE,
380 															 vk::VK_IMAGE_LAYOUT_GENERAL,
381 															 vk::VK_IMAGE_LAYOUT_GENERAL));
382 
383 	const vk::VkAttachmentReference colorAttachmentReference =
384 	{
385 		0,
386 		vk::VK_IMAGE_LAYOUT_GENERAL
387 	};
388 
389 	renderPassCreateInfo.addSubpass(SubpassDescription(vk::VK_PIPELINE_BIND_POINT_GRAPHICS,
390 													   0,
391 													   0,
392 													   DE_NULL,
393 													   1,
394 													   &colorAttachmentReference,
395 													   DE_NULL,
396 													   AttachmentReference(),
397 													   0,
398 													   DE_NULL));
399 
400 	vk::VkRenderPassMultiviewCreateInfo renderPassMultiviewCreateInfo;
401 	// Bit mask that specifies which view rendering is broadcast to
402 	// 0011 = Broadcast to first and second view (layer)
403 	const deUint32 viewMask = 0x3;
404 	// Bit mask that specifices correlation between views
405 	// An implementation may use this for optimizations (concurrent render)
406 	const deUint32 correlationMask = 0x3;
407 
408 	if (m_params.testMultiview)
409 	{
410 		DE_ASSERT(renderPassCreateInfo.subpassCount == 1);
411 
412 
413 
414 		renderPassMultiviewCreateInfo.sType = vk::VK_STRUCTURE_TYPE_RENDER_PASS_MULTIVIEW_CREATE_INFO;
415 		renderPassMultiviewCreateInfo.pNext = DE_NULL;
416 		renderPassMultiviewCreateInfo.subpassCount = renderPassCreateInfo.subpassCount;
417 		renderPassMultiviewCreateInfo.pViewMasks = &viewMask;
418 		renderPassMultiviewCreateInfo.correlationMaskCount = 1u;
419 		renderPassMultiviewCreateInfo.pCorrelationMasks = &correlationMask;
420 		renderPassMultiviewCreateInfo.pViewOffsets = DE_NULL;
421 		renderPassMultiviewCreateInfo.dependencyCount = 0u;
422 
423 		renderPassCreateInfo.pNext = &renderPassMultiviewCreateInfo;
424 	}
425 
426 	m_renderPass		= vk::createRenderPass(m_vk, device, &renderPassCreateInfo);
427 
428 	std::vector<vk::VkImageView> colorAttachments(1);
429 	colorAttachments[0] = *m_colorTargetView;
430 
431 	const FramebufferCreateInfo framebufferCreateInfo(*m_renderPass, colorAttachments, WIDTH, HEIGHT, 1);
432 
433 	m_framebuffer		= vk::createFramebuffer(m_vk, device, &framebufferCreateInfo);
434 
435 	const vk::VkVertexInputBindingDescription vertexInputBindingDescription[2] =
436 	{
437 		{
438 			0u,
439 			(deUint32)sizeof(VertexPositionAndColor),
440 			vk::VK_VERTEX_INPUT_RATE_VERTEX,
441 		},
442 		{
443 			1u,
444 			(deUint32)sizeof(tcu::Vec4),
445 			vk::VK_VERTEX_INPUT_RATE_INSTANCE,
446 		},
447 	};
448 
449 	const vk::VkVertexInputAttributeDescription vertexInputAttributeDescriptions[] =
450 	{
451 		{
452 			0u,
453 			0u,
454 			vk::VK_FORMAT_R32G32B32A32_SFLOAT,
455 			0u
456 		},
457 		{
458 			1u,
459 			0u,
460 			vk::VK_FORMAT_R32G32B32A32_SFLOAT,
461 			(deUint32)sizeof(tcu::Vec4),
462 		},
463 		{
464 			2u,
465 			1u,
466 			vk::VK_FORMAT_R32G32B32A32_SFLOAT,
467 			0,
468 		}
469 	};
470 
471 	m_vertexInputState = PipelineCreateInfo::VertexInputState(2,
472 															  vertexInputBindingDescription,
473 															  DE_LENGTH_OF_ARRAY(vertexInputAttributeDescriptions),
474 															  vertexInputAttributeDescriptions);
475 
476 	const vk::VkVertexInputBindingDivisorDescriptionEXT vertexInputBindingDivisorDescription =
477 	{
478 		1u,
479 		m_params.attribDivisor,
480 	};
481 	if (m_params.testAttribDivisor)
482 		m_vertexInputState.addDivisors(1, &vertexInputBindingDivisorDescription);
483 
484 	const CmdPoolCreateInfo cmdPoolCreateInfo(queueFamilyIndex);
485 	m_cmdPool = vk::createCommandPool(m_vk, device, &cmdPoolCreateInfo);
486 
487 	m_cmdBuffer = vk::allocateCommandBuffer(m_vk, device, *m_cmdPool, vk::VK_COMMAND_BUFFER_LEVEL_PRIMARY);
488 
489 	const vk::Unique<vk::VkShaderModule> vs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get("InstancedDrawVert"), 0));
490 	const vk::Unique<vk::VkShaderModule> fs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get("InstancedDrawFrag"), 0));
491 
492 	const PipelineCreateInfo::ColorBlendState::Attachment vkCbAttachmentState;
493 
494 	vk::VkViewport	viewport	= vk::makeViewport(WIDTH, HEIGHT);
495 	vk::VkRect2D	scissor		= vk::makeRect2D(WIDTH, HEIGHT);
496 
497 	PipelineCreateInfo pipelineCreateInfo(*m_pipelineLayout, *m_renderPass, 0, 0);
498 	pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*vs, "main", vk::VK_SHADER_STAGE_VERTEX_BIT));
499 	pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*fs, "main", vk::VK_SHADER_STAGE_FRAGMENT_BIT));
500 	pipelineCreateInfo.addState(PipelineCreateInfo::VertexInputState(m_vertexInputState));
501 	pipelineCreateInfo.addState(PipelineCreateInfo::InputAssemblerState(m_params.topology));
502 	pipelineCreateInfo.addState(PipelineCreateInfo::ColorBlendState(1, &vkCbAttachmentState));
503 	pipelineCreateInfo.addState(PipelineCreateInfo::ViewportState(1, std::vector<vk::VkViewport>(1, viewport), std::vector<vk::VkRect2D>(1, scissor)));
504 	pipelineCreateInfo.addState(PipelineCreateInfo::DepthStencilState());
505 	pipelineCreateInfo.addState(PipelineCreateInfo::RasterizerState());
506 	pipelineCreateInfo.addState(PipelineCreateInfo::MultiSampleState());
507 
508 	m_pipeline = vk::createGraphicsPipeline(m_vk, device, DE_NULL, &pipelineCreateInfo);
509 }
510 
iterate()511 tcu::TestStatus InstancedDrawInstance::iterate()
512 {
513 	const vk::VkQueue		queue					= m_context.getUniversalQueue();
514 	const vk::VkDevice		device					= m_context.getDevice();
515 	static const deUint32	instanceCounts[]		= { 0, 1, 2, 4, 20 };
516 	static const deUint32	firstInstanceIndices[]	= { 0, 1, 3, 4, 20 };
517 	const deUint32			numLayers				= m_params.testMultiview ? 2 : 1;
518 
519 	qpTestResult			res						= QP_TEST_RESULT_PASS;
520 
521 	const vk::VkClearColorValue clearColor = { { 0.0f, 0.0f, 0.0f, 1.0f } };
522 	int firstInstanceIndicesCount = 1;
523 
524 	// Require 'drawIndirectFirstInstance' feature to run non-zero firstInstance indirect draw tests.
525 	if (m_context.getDeviceFeatures().drawIndirectFirstInstance)
526 		firstInstanceIndicesCount = DE_LENGTH_OF_ARRAY(firstInstanceIndices);
527 
528 	for (int instanceCountNdx = 0; instanceCountNdx < DE_LENGTH_OF_ARRAY(instanceCounts); instanceCountNdx++)
529 	{
530 		const deUint32 instanceCount = instanceCounts[instanceCountNdx];
531 		for (int firstInstanceIndexNdx = 0; firstInstanceIndexNdx < firstInstanceIndicesCount; firstInstanceIndexNdx++)
532 		{
533 			// Prepare vertex data for at least one instance
534 			const deUint32				prepareCount			= de::max(instanceCount, 1u);
535 			const deUint32				firstInstance			= firstInstanceIndices[firstInstanceIndexNdx];
536 
537 			prepareVertexData(prepareCount, firstInstance, m_params.testAttribDivisor ? m_params.attribDivisor : 1);
538 			const de::SharedPtr<Buffer>	vertexBuffer			= createAndUploadBuffer(m_data, m_vk, m_context, vk::VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
539 			const de::SharedPtr<Buffer>	instancedVertexBuffer	= createAndUploadBuffer(m_instancedColor, m_vk, m_context, vk::VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
540 			de::SharedPtr<Buffer>		indexBuffer;
541 			de::SharedPtr<Buffer>		indirectBuffer;
542 			beginCommandBuffer(m_vk, *m_cmdBuffer, 0u);
543 
544 			if (m_params.testMultiview)
545 			{
546 				vk::VkImageMemoryBarrier barrier;
547 				barrier.sType							= vk::VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
548 				barrier.pNext							= DE_NULL;
549 				barrier.srcAccessMask					= 0u;
550 				barrier.dstAccessMask					= vk::VK_ACCESS_TRANSFER_WRITE_BIT;
551 				barrier.oldLayout						= vk::VK_IMAGE_LAYOUT_UNDEFINED;
552 				barrier.newLayout						= vk::VK_IMAGE_LAYOUT_GENERAL;
553 				barrier.srcQueueFamilyIndex				= VK_QUEUE_FAMILY_IGNORED;
554 				barrier.dstQueueFamilyIndex				= VK_QUEUE_FAMILY_IGNORED;
555 				barrier.image							= m_colorTargetImage->object();
556 				barrier.subresourceRange.aspectMask		= vk::VK_IMAGE_ASPECT_COLOR_BIT;
557 				barrier.subresourceRange.baseMipLevel	= 0;
558 				barrier.subresourceRange.levelCount		= 1;
559 				barrier.subresourceRange.baseArrayLayer = 0;
560 				barrier.subresourceRange.layerCount		= numLayers;
561 
562 				m_vk.cmdPipelineBarrier(*m_cmdBuffer, vk::VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, vk::VK_PIPELINE_STAGE_TRANSFER_BIT, (vk::VkDependencyFlags)0, 0, (const vk::VkMemoryBarrier*)DE_NULL,
563 										0, (const vk::VkBufferMemoryBarrier*)DE_NULL, 1, &barrier);
564 
565 			}
566 			else
567 			{
568 				initialTransitionColor2DImage(m_vk, *m_cmdBuffer, m_colorTargetImage->object(), vk::VK_IMAGE_LAYOUT_GENERAL,
569 											  vk::VK_ACCESS_TRANSFER_WRITE_BIT, vk::VK_PIPELINE_STAGE_TRANSFER_BIT);
570 			}
571 
572 			const ImageSubresourceRange subresourceRange(vk::VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, numLayers);
573 			m_vk.cmdClearColorImage(*m_cmdBuffer, m_colorTargetImage->object(),
574 				vk::VK_IMAGE_LAYOUT_GENERAL, &clearColor, 1, &subresourceRange);
575 
576 			const vk::VkMemoryBarrier memBarrier =
577 			{
578 				vk::VK_STRUCTURE_TYPE_MEMORY_BARRIER,
579 				DE_NULL,
580 				vk::VK_ACCESS_TRANSFER_WRITE_BIT,
581 				vk::VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | vk::VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT
582 			};
583 
584 			m_vk.cmdPipelineBarrier(*m_cmdBuffer, vk::VK_PIPELINE_STAGE_TRANSFER_BIT,
585 				vk::VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
586 				0, 1, &memBarrier, 0, DE_NULL, 0, DE_NULL);
587 
588 			const vk::VkRect2D renderArea = vk::makeRect2D(WIDTH, HEIGHT);
589 			beginRenderPass(m_vk, *m_cmdBuffer, *m_renderPass, *m_framebuffer, renderArea);
590 
591 			if (m_params.function == TestParams::FUNCTION_DRAW_INDEXED || m_params.function == TestParams::FUNCTION_DRAW_INDEXED_INDIRECT)
592 			{
593 				indexBuffer = createAndUploadBuffer(m_indexes, m_vk, m_context, vk::VK_BUFFER_USAGE_INDEX_BUFFER_BIT);
594 				m_vk.cmdBindIndexBuffer(*m_cmdBuffer, indexBuffer->object(), 0, vk::VK_INDEX_TYPE_UINT32);
595 			}
596 
597 			const vk::VkBuffer vertexBuffers[] =
598 			{
599 				vertexBuffer->object(),
600 				instancedVertexBuffer->object(),
601 			};
602 
603 			const vk::VkDeviceSize vertexBufferOffsets[] =
604 			{
605 				0,	// vertexBufferOffset
606 				0,	// instancedVertexBufferOffset
607 			};
608 
609 			m_vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, DE_LENGTH_OF_ARRAY(vertexBuffers), vertexBuffers, vertexBufferOffsets);
610 
611 			const float pushConstants[] = { (float)firstInstance, (float)instanceCount };
612 			m_vk.cmdPushConstants(*m_cmdBuffer, *m_pipelineLayout, vk::VK_SHADER_STAGE_VERTEX_BIT, 0u, (deUint32)sizeof(pushConstants), pushConstants);
613 
614 			m_vk.cmdBindPipeline(*m_cmdBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
615 
616 			switch (m_params.function)
617 			{
618 				case TestParams::FUNCTION_DRAW:
619 					m_vk.cmdDraw(*m_cmdBuffer, (deUint32)m_data.size(), instanceCount, 0u, firstInstance);
620 					break;
621 
622 				case TestParams::FUNCTION_DRAW_INDEXED:
623 					m_vk.cmdDrawIndexed(*m_cmdBuffer, (deUint32)m_indexes.size(), instanceCount, 0u, 0u, firstInstance);
624 					break;
625 
626 				case TestParams::FUNCTION_DRAW_INDIRECT:
627 				{
628 					vk::VkDrawIndirectCommand drawCommand =
629 					{
630 						(deUint32)m_data.size(),	// uint32_t	vertexCount;
631 						instanceCount,				// uint32_t	instanceCount;
632 						0u,							// uint32_t	firstVertex;
633 						firstInstance,				// uint32_t	firstInstance;
634 					};
635 					std::vector<vk::VkDrawIndirectCommand> drawCommands;
636 					drawCommands.push_back(drawCommand);
637 					indirectBuffer = createAndUploadBuffer(drawCommands, m_vk, m_context, vk::VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT);
638 
639 					m_vk.cmdDrawIndirect(*m_cmdBuffer, indirectBuffer->object(), 0, 1u, 0u);
640 					break;
641 				}
642 				case TestParams::FUNCTION_DRAW_INDEXED_INDIRECT:
643 				{
644 					vk::VkDrawIndexedIndirectCommand drawCommand =
645 					{
646 						(deUint32)m_indexes.size(),	// uint32_t	indexCount;
647 						instanceCount,				// uint32_t	instanceCount;
648 						0u,							// uint32_t	firstIndex;
649 						0,							// int32_t	vertexOffset;
650 						firstInstance,				// uint32_t	firstInstance;
651 					};
652 					std::vector<vk::VkDrawIndexedIndirectCommand> drawCommands;
653 					drawCommands.push_back(drawCommand);
654 					indirectBuffer = createAndUploadBuffer(drawCommands, m_vk, m_context, vk::VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT);
655 
656 					m_vk.cmdDrawIndexedIndirect(*m_cmdBuffer, indirectBuffer->object(), 0, 1u, 0u);
657 					break;
658 				}
659 				default:
660 					DE_ASSERT(false);
661 			}
662 
663 			endRenderPass(m_vk, *m_cmdBuffer);
664 			endCommandBuffer(m_vk, *m_cmdBuffer);
665 
666 			submitCommandsAndWait(m_vk, device, queue, m_cmdBuffer.get());
667 
668 			// Reference rendering
669 			std::vector<tcu::Vec4>	vetrices;
670 			std::vector<tcu::Vec4>	colors;
671 
672 			for (std::vector<VertexPositionAndColor>::const_iterator it = m_data.begin(); it != m_data.end(); ++it)
673 			{
674 				vetrices.push_back(it->position);
675 				colors.push_back(it->color);
676 			}
677 
678 			tcu::TextureLevel refImage (vk::mapVkFormat(m_colorAttachmentFormat), (int)(0.5 + WIDTH), (int)(0.5 + HEIGHT));
679 
680 			tcu::clear(refImage.getAccess(), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
681 
682 			const TestVertShader					vertShader(instanceCount, firstInstance);
683 			const TestFragShader					fragShader;
684 			const rr::Program						program			(&vertShader, &fragShader);
685 			const rr::MultisamplePixelBufferAccess	colorBuffer		= rr::MultisamplePixelBufferAccess::fromSinglesampleAccess(refImage.getAccess());
686 			const rr::RenderTarget					renderTarget	(colorBuffer);
687 			const rr::RenderState					renderState		((rr::ViewportState(colorBuffer)), m_context.getDeviceProperties().limits.subPixelPrecisionBits);
688 			const rr::Renderer						renderer;
689 
690 			const rr::VertexAttrib	vertexAttribs[] =
691 			{
692 				rr::VertexAttrib(rr::VERTEXATTRIBTYPE_FLOAT, 4, sizeof(tcu::Vec4), 0, &vetrices[0]),
693 				rr::VertexAttrib(rr::VERTEXATTRIBTYPE_FLOAT, 4, sizeof(tcu::Vec4), 0, &colors[0]),
694 				// The reference renderer treats a divisor of 0 as meaning per-vertex.  Use INT_MAX instead; it should work just as well.
695 				rr::VertexAttrib(rr::VERTEXATTRIBTYPE_FLOAT, 4, sizeof(tcu::Vec4), m_params.testAttribDivisor ? (m_params.attribDivisor == 0 ? INT_MAX : m_params.attribDivisor) : 1, &m_instancedColor[0])
696 			};
697 
698 			if (m_params.function == TestParams::FUNCTION_DRAW || m_params.function == TestParams::FUNCTION_DRAW_INDIRECT)
699 			{
700 				const rr::PrimitiveList	primitives = rr::PrimitiveList(mapVkPrimitiveTopology(m_params.topology), (int)vetrices.size(), 0);
701 				const rr::DrawCommand	command(renderState, renderTarget, program, DE_LENGTH_OF_ARRAY(vertexAttribs), &vertexAttribs[0],
702 												primitives);
703 				renderer.drawInstanced(command, instanceCount);
704 			}
705 			else
706 			{
707 				const rr::DrawIndices indicies(m_indexes.data());
708 
709 				const rr::PrimitiveList	primitives = rr::PrimitiveList(mapVkPrimitiveTopology(m_params.topology), (int)m_indexes.size(), indicies);
710 				const rr::DrawCommand	command(renderState, renderTarget, program, DE_LENGTH_OF_ARRAY(vertexAttribs), &vertexAttribs[0],
711 												primitives);
712 				renderer.drawInstanced(command, instanceCount);
713 			}
714 
715 			const vk::VkOffset3D zeroOffset = { 0, 0, 0 };
716 			for (deUint32 i = 0; i < numLayers; i++)
717 			{
718 				const tcu::ConstPixelBufferAccess renderedFrame = m_colorTargetImage->readSurface(queue, m_context.getDefaultAllocator(),
719 					vk::VK_IMAGE_LAYOUT_GENERAL, zeroOffset, WIDTH, HEIGHT, vk::VK_IMAGE_ASPECT_COLOR_BIT, 0, i);
720 
721 				tcu::TestLog &log		= m_context.getTestContext().getLog();
722 
723 				std::ostringstream resultDesc;
724 				resultDesc << "Image layer " << i << " comparison result. Instance count: " << instanceCount << " first instance index: " << firstInstance;
725 
726 				if (m_params.topology == vk::VK_PRIMITIVE_TOPOLOGY_POINT_LIST)
727 				{
728 					const bool ok = tcu::intThresholdPositionDeviationCompare(
729 						log, "Result", resultDesc.str().c_str(), refImage.getAccess(), renderedFrame,
730 						tcu::UVec4(4u),					// color threshold
731 						tcu::IVec3(1, 1, 0),			// position deviation tolerance
732 						true,							// don't check the pixels at the boundary
733 						tcu::COMPARE_LOG_RESULT);
734 
735 					if (!ok)
736 						res = QP_TEST_RESULT_FAIL;
737 				}
738 				else
739 				{
740 					if (!tcu::fuzzyCompare(log, "Result", resultDesc.str().c_str(), refImage.getAccess(), renderedFrame, 0.05f, tcu::COMPARE_LOG_RESULT))
741 						res = QP_TEST_RESULT_FAIL;
742 				}
743 			}
744 		}
745 	}
746 	return tcu::TestStatus(res, qpGetTestResultName(res));
747 }
748 
prepareVertexData(int instanceCount,int firstInstance,int instanceDivisor)749 void InstancedDrawInstance::prepareVertexData(int instanceCount, int firstInstance, int instanceDivisor)
750 {
751 	m_data.clear();
752 	m_indexes.clear();
753 	m_instancedColor.clear();
754 
755 	if (m_params.function == TestParams::FUNCTION_DRAW || m_params.function == TestParams::FUNCTION_DRAW_INDIRECT)
756 	{
757 		for (int y = 0; y < QUAD_GRID_SIZE; y++)
758 		{
759 			for (int x = 0; x < QUAD_GRID_SIZE; x++)
760 			{
761 				const float fx0 = -1.0f + (float)(x+0) / (float)QUAD_GRID_SIZE * 2.0f / (float)instanceCount;
762 				const float fx1 = -1.0f + (float)(x+1) / (float)QUAD_GRID_SIZE * 2.0f / (float)instanceCount;
763 				const float fy0 = -1.0f + (float)(y+0) / (float)QUAD_GRID_SIZE * 2.0f;
764 				const float fy1 = -1.0f + (float)(y+1) / (float)QUAD_GRID_SIZE * 2.0f;
765 
766 				// Vertices of a quad's lower-left triangle: (fx0, fy0), (fx1, fy0) and (fx0, fy1)
767 				m_data.push_back(VertexPositionAndColor(tcu::Vec4(fx0, fy0, 1.0f, 1.0f), tcu::RGBA::blue().toVec()));
768 				m_data.push_back(VertexPositionAndColor(tcu::Vec4(fx1, fy0, 1.0f, 1.0f), tcu::RGBA::blue().toVec()));
769 				m_data.push_back(VertexPositionAndColor(tcu::Vec4(fx0, fy1, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
770 
771 				// Vertices of a quad's upper-right triangle: (fx1, fy1), (fx0, fy1) and (fx1, fy0)
772 				m_data.push_back(VertexPositionAndColor(tcu::Vec4(fx1, fy1, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
773 				m_data.push_back(VertexPositionAndColor(tcu::Vec4(fx0, fy1, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
774 				m_data.push_back(VertexPositionAndColor(tcu::Vec4(fx1, fy0, 1.0f, 1.0f), tcu::RGBA::blue().toVec()));
775 			}
776 		}
777 	}
778 	else
779 	{
780 		for (int y = 0; y < QUAD_GRID_SIZE + 1; y++)
781 		{
782 			for (int x = 0; x < QUAD_GRID_SIZE + 1; x++)
783 			{
784 				const float fx = -1.0f + (float)x / (float)QUAD_GRID_SIZE * 2.0f / (float)instanceCount;
785 				const float fy = -1.0f + (float)y / (float)QUAD_GRID_SIZE * 2.0f;
786 
787 				m_data.push_back(VertexPositionAndColor(tcu::Vec4(fx, fy, 1.0f, 1.0f),
788 														(y % 2 ? tcu::RGBA::blue().toVec() : tcu::RGBA::green().toVec())));
789 			}
790 		}
791 
792 		for (int y = 0; y < QUAD_GRID_SIZE; y++)
793 		{
794 			for (int x = 0; x < QUAD_GRID_SIZE; x++)
795 			{
796 				const int ndx00 = y*(QUAD_GRID_SIZE + 1) + x;
797 				const int ndx10 = y*(QUAD_GRID_SIZE + 1) + x + 1;
798 				const int ndx01 = (y + 1)*(QUAD_GRID_SIZE + 1) + x;
799 				const int ndx11 = (y + 1)*(QUAD_GRID_SIZE + 1) + x + 1;
800 
801 				// Lower-left triangle of a quad.
802 				m_indexes.push_back((deUint16)ndx00);
803 				m_indexes.push_back((deUint16)ndx10);
804 				m_indexes.push_back((deUint16)ndx01);
805 
806 				// Upper-right triangle of a quad.
807 				m_indexes.push_back((deUint16)ndx11);
808 				m_indexes.push_back((deUint16)ndx01);
809 				m_indexes.push_back((deUint16)ndx10);
810 			}
811 		}
812 	}
813 
814 	const int colorCount = instanceDivisor == 0 ? 1 : (instanceCount + firstInstance + instanceDivisor - 1) / instanceDivisor;
815 	for (int i = 0; i < instanceCount + firstInstance; i++)
816 	{
817 		m_instancedColor.push_back(tcu::Vec4(0.0, (float)(1.0 - i * 1.0 / colorCount) / 2, 0.0, 1.0));
818 	}
819 }
820 
821 } // anonymus
822 
InstancedTests(tcu::TestContext & testCtx)823 InstancedTests::InstancedTests(tcu::TestContext& testCtx)
824 	: TestCaseGroup	(testCtx, "instanced", "Instanced drawing tests")
825 {
826 	static const vk::VkPrimitiveTopology	topologies[]			=
827 	{
828 		vk::VK_PRIMITIVE_TOPOLOGY_POINT_LIST,
829 		vk::VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
830 		vk::VK_PRIMITIVE_TOPOLOGY_LINE_STRIP,
831 		vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
832 		vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
833 		vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN,
834 	};
835 	static const TestParams::DrawFunction	functions[]				=
836 	{
837 		TestParams::FUNCTION_DRAW,
838 		TestParams::FUNCTION_DRAW_INDEXED,
839 		TestParams::FUNCTION_DRAW_INDIRECT,
840 		TestParams::FUNCTION_DRAW_INDEXED_INDIRECT,
841 	};
842 
843 	static const deBool multiviews[] = { DE_FALSE, DE_TRUE };
844 
845 	static const deUint32 divisors[] = { 0, 1, 2, 4, 20 };
846 
847 	for (int topologyNdx = 0; topologyNdx < DE_LENGTH_OF_ARRAY(topologies); topologyNdx++)
848 	{
849 		for (int functionNdx = 0; functionNdx < DE_LENGTH_OF_ARRAY(functions); functionNdx++)
850 		{
851 			for (int testAttribDivisor = 0; testAttribDivisor < 2; testAttribDivisor++)
852 			{
853 				for (int divisorNdx = 0; divisorNdx < DE_LENGTH_OF_ARRAY(divisors); divisorNdx++)
854 				{
855 					for (int multiviewNdx = 0; multiviewNdx < DE_LENGTH_OF_ARRAY(multiviews); multiviewNdx++)
856 					{
857 						// If we don't have VK_EXT_vertex_attribute_divisor, we only get a divisor or 1.
858 						if (!testAttribDivisor && divisors[divisorNdx] != 1)
859 							continue;
860 
861 						TestParams param;
862 						param.function = functions[functionNdx];
863 						param.topology = topologies[topologyNdx];
864 						param.testAttribDivisor = testAttribDivisor ? DE_TRUE : DE_FALSE;
865 						param.attribDivisor = divisors[divisorNdx];
866 						param.testMultiview = multiviews[multiviewNdx];
867 
868 						// Add multiview tests only when vertex attribute divisor is enabled.
869 						if (param.testMultiview && !testAttribDivisor)
870 							continue;
871 
872 						std::string testName = de::toString(param);
873 
874 						addChild(new InstancedDrawCase(m_testCtx, de::toLower(testName), "Instanced drawing test", param));
875 					}
876 				}
877 			}
878 		}
879 	}
880 }
881 
~InstancedTests()882 InstancedTests::~InstancedTests() {}
883 
884 } // DrawTests
885 } // vkt
886