• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2016 The Khronos Group Inc.
6  * Copyright (c) 2016 The Android Open Source Project
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 Basic Geometry Shader Tests
23  *//*--------------------------------------------------------------------*/
24 
25 #include "vktGeometryBasicGeometryShaderTests.hpp"
26 #include "vktGeometryBasicClass.hpp"
27 #include "vktGeometryTestsUtil.hpp"
28 
29 #include "gluTextureUtil.hpp"
30 #include "glwEnums.hpp"
31 #include "vkDefs.hpp"
32 #include "vktTestCase.hpp"
33 #include "vktTestCaseUtil.hpp"
34 #include "vkImageUtil.hpp"
35 #include "vkTypeUtil.hpp"
36 #include "vkPrograms.hpp"
37 #include "vkBuilderUtil.hpp"
38 #include "vkRefUtil.hpp"
39 #include "vkQueryUtil.hpp"
40 #include "vkCmdUtil.hpp"
41 #include "vkMemUtil.hpp"
42 #include "vkCmdUtil.hpp"
43 #include "vkObjUtil.hpp"
44 #include "tcuTextureUtil.hpp"
45 
46 #include <string>
47 
48 using namespace vk;
49 
50 namespace vkt
51 {
52 namespace geometry
53 {
54 namespace
55 {
56 using tcu::TestStatus;
57 using tcu::TestContext;
58 using tcu::TestCaseGroup;
59 using de::MovePtr;
60 using std::string;
61 using std::vector;
62 
63 enum VaryingSource
64 {
65 	READ_ATTRIBUTE = 0,
66 	READ_UNIFORM,
67 	READ_TEXTURE,
68 
69 	READ_LAST
70 };
71 enum ShaderInstancingMode
72 {
73 	MODE_WITHOUT_INSTANCING = 0,
74 	MODE_WITH_INSTANCING,
75 
76 	MODE_LAST
77 };
78 enum
79 {
80 	EMIT_COUNT_VERTEX_0 = 6,
81 	EMIT_COUNT_VERTEX_1 = 0,
82 	EMIT_COUNT_VERTEX_2 = -1,
83 	EMIT_COUNT_VERTEX_3 = 10,
84 };
85 enum VariableTest
86 {
87 	TEST_POINT_SIZE = 0,
88 	TEST_PRIMITIVE_ID_IN,
89 	TEST_PRIMITIVE_ID,
90 	TEST_LAST
91 };
92 
uploadImage(Context & context,const tcu::ConstPixelBufferAccess & access,VkImage destImage)93 void uploadImage (Context&								context,
94 				  const tcu::ConstPixelBufferAccess&	access,
95 				  VkImage								destImage)
96 {
97 	const DeviceInterface&			vk					= context.getDeviceInterface();
98 	const VkDevice					device				= context.getDevice();
99 	const deUint32					queueFamilyIndex	= context.getUniversalQueueFamilyIndex();
100 	const VkQueue					queue				= context.getUniversalQueue();
101 	Allocator&						memAlloc			= context.getDefaultAllocator();
102 	const VkImageAspectFlags		aspectMask			= VK_IMAGE_ASPECT_COLOR_BIT;
103 	const deUint32					bufferSize			= access.getWidth() * access.getHeight() * access.getDepth() * access.getFormat().getPixelSize();
104 	Move<VkBuffer>					buffer;
105 	de::MovePtr<Allocation>			bufferAlloc;
106 	Move<VkCommandPool>				cmdPool;
107 	Move<VkCommandBuffer>			cmdBuffer;
108 	Move<VkFence>					fence;
109 
110 	// Create source buffer
111 	{
112 		const VkBufferCreateInfo bufferParams =
113 		{
114 			VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,		// VkStructureType		sType;
115 			DE_NULL,									// const void*			pNext;
116 			0u,											// VkBufferCreateFlags	flags;
117 			bufferSize,									// VkDeviceSize			size;
118 			VK_BUFFER_USAGE_TRANSFER_SRC_BIT,			// VkBufferUsageFlags	usage;
119 			VK_SHARING_MODE_EXCLUSIVE,					// VkSharingMode		sharingMode;
120 			0u,											// deUint32				queueFamilyIndexCount;
121 			DE_NULL,									// const deUint32*		pQueueFamilyIndices;
122 		};
123 		buffer		= createBuffer(vk, device, &bufferParams);
124 		bufferAlloc	= memAlloc.allocate(getBufferMemoryRequirements(vk, device, *buffer), MemoryRequirement::HostVisible);
125 		VK_CHECK(vk.bindBufferMemory(device, *buffer, bufferAlloc->getMemory(), bufferAlloc->getOffset()));
126 	}
127 
128 	// Get copy regions and write buffer data
129 	const VkBufferImageCopy			copyRegion	=
130 	{
131 		0u,								// VkDeviceSize				bufferOffset;
132 		(deUint32)access.getWidth(),	// deUint32					bufferRowLength;
133 		(deUint32)access.getHeight(),	// deUint32					bufferImageHeight;
134 		{								// VkImageSubresourceLayers	imageSubresource;
135 				aspectMask,				// VkImageAspectFlags		aspectMask;
136 				(deUint32)0u,			// uint32_t					mipLevel;
137 				(deUint32)0u,			// uint32_t					baseArrayLayer;
138 				1u						// uint32_t					layerCount;
139 		},
140 		{ 0u, 0u, 0u },					// VkOffset3D			imageOffset;
141 		{								// VkExtent3D			imageExtent;
142 			(deUint32)access.getWidth(),
143 			(deUint32)access.getHeight(),
144 			(deUint32)access.getDepth()
145 		}
146 	};
147 
148 	vector<VkBufferImageCopy>		copyRegions	(1, copyRegion);
149 
150 	{
151 		const tcu::PixelBufferAccess	destAccess	(access.getFormat(), access.getSize(), bufferAlloc->getHostPtr());
152 		tcu::copy(destAccess, access);
153 		flushAlloc(vk, device, *bufferAlloc);
154 	}
155 
156 	// Copy buffer to image
157 	copyBufferToImage(vk, device, queue, queueFamilyIndex, *buffer, bufferSize, copyRegions, DE_NULL, aspectMask, 1, 1, destImage, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
158 }
159 
160 class GeometryOutputCountTestInstance : public GeometryExpanderRenderTestInstance
161 {
162 public:
163 								GeometryOutputCountTestInstance	(Context&					context,
164 																 const VkPrimitiveTopology	primitiveType,
165 																 const int					primitiveCount,
166 																 const char*				name);
167 	void						genVertexAttribData				(void);
168 private:
169 	const int					m_primitiveCount;
170 };
171 
GeometryOutputCountTestInstance(Context & context,const VkPrimitiveTopology primitiveType,const int primitiveCount,const char * name)172 GeometryOutputCountTestInstance::GeometryOutputCountTestInstance (Context&					context,
173 																  const VkPrimitiveTopology	primitiveType,
174 																  const int					primitiveCount,
175 																  const char*				name)
176 	: GeometryExpanderRenderTestInstance	(context, primitiveType, name)
177 	, m_primitiveCount						(primitiveCount)
178 
179 {
180 	genVertexAttribData();
181 }
182 
genVertexAttribData(void)183 void GeometryOutputCountTestInstance::genVertexAttribData (void)
184 {
185 	m_vertexPosData.resize(m_primitiveCount);
186 	m_vertexAttrData.resize(m_primitiveCount);
187 
188 	for (int ndx = 0; ndx < m_primitiveCount; ++ndx)
189 	{
190 		m_vertexPosData[ndx] = tcu::Vec4(-1.0f, ((float)ndx) / (float)m_primitiveCount * 2.0f - 1.0f, 0.0f, 1.0f);
191 		m_vertexAttrData[ndx] = (ndx % 2 == 0) ? tcu::Vec4(1, 1, 1, 1) : tcu::Vec4(1, 0, 0, 1);
192 	}
193 	m_numDrawVertices = m_primitiveCount;
194 }
195 
196 class VaryingOutputCountTestInstance : public GeometryExpanderRenderTestInstance
197 {
198 public:
199 								VaryingOutputCountTestInstance	(Context&					context,
200 																 const char*				name,
201 																 const VkPrimitiveTopology	primitiveType,
202 																 const VaryingSource		test,
203 																 const ShaderInstancingMode	mode);
204 	void						genVertexAttribData				(void);
205 protected:
206 	Move<VkPipelineLayout>		createPipelineLayout			(const DeviceInterface& vk, const VkDevice device);
207 	void						bindDescriptorSets				(const DeviceInterface&		vk,
208 																 const VkDevice				device,
209 																 Allocator&					memAlloc,
210 																 const VkCommandBuffer&		cmdBuffer,
211 																 const VkPipelineLayout&	pipelineLayout);
212 private:
213 	void						genVertexDataWithoutInstancing	(void);
214 	void						genVertexDataWithInstancing		(void);
215 
216 	const VaryingSource			m_test;
217 	const ShaderInstancingMode	m_mode;
218 	const deInt32				m_maxEmitCount;
219 	Move<VkDescriptorPool>		m_descriptorPool;
220 	Move<VkDescriptorSetLayout>	m_descriptorSetLayout;
221 	Move<VkDescriptorSet>		m_descriptorSet;
222 	Move<VkBuffer>				m_buffer;
223 	Move<VkImage>				m_texture;
224 	Move<VkImageView>			m_imageView;
225 	Move<VkSampler>				m_sampler;
226 	de::MovePtr<Allocation>		m_allocation;
227 };
228 
VaryingOutputCountTestInstance(Context & context,const char * name,const VkPrimitiveTopology primitiveType,const VaryingSource test,const ShaderInstancingMode mode)229 VaryingOutputCountTestInstance::VaryingOutputCountTestInstance (Context&					context,
230 																const char*					name,
231 																const VkPrimitiveTopology	primitiveType,
232 																const VaryingSource			test,
233 																const ShaderInstancingMode	mode)
234 	: GeometryExpanderRenderTestInstance	(context, primitiveType, name)
235 	, m_test								(test)
236 	, m_mode								(mode)
237 	, m_maxEmitCount						(128)
238 {
239 	genVertexAttribData ();
240 }
241 
genVertexAttribData(void)242 void VaryingOutputCountTestInstance::genVertexAttribData (void)
243 {
244 	if (m_mode == MODE_WITHOUT_INSTANCING)
245 		genVertexDataWithoutInstancing();
246 	else if (m_mode == MODE_WITH_INSTANCING)
247 		genVertexDataWithInstancing();
248 	else
249 		DE_ASSERT(false);
250 }
251 
createPipelineLayout(const DeviceInterface & vk,const VkDevice device)252 Move<VkPipelineLayout> VaryingOutputCountTestInstance::createPipelineLayout (const DeviceInterface& vk, const VkDevice device)
253 {
254 	if (m_test == READ_UNIFORM)
255 	{
256 		m_descriptorSetLayout	=	DescriptorSetLayoutBuilder()
257 									.addSingleBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_GEOMETRY_BIT)
258 									.build(vk, device);
259 		m_descriptorPool		=	DescriptorPoolBuilder()
260 									.addType(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER)
261 									.build(vk, device, VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, 1u);
262 		m_descriptorSet			=	makeDescriptorSet(vk, device, *m_descriptorPool, *m_descriptorSetLayout);
263 
264 		return makePipelineLayout(vk, device, *m_descriptorSetLayout);
265 	}
266 	else if (m_test == READ_TEXTURE)
267 	{
268 		const tcu::Vec4				data[4]				=
269 														{
270 															tcu::Vec4(255, 0, 0, 0),
271 															tcu::Vec4(0, 255, 0, 0),
272 															tcu::Vec4(0, 0, 255, 0),
273 															tcu::Vec4(0, 0, 0, 255)
274 														};
275 		const tcu::UVec2			viewportSize		(4, 1);
276 		const tcu::TextureFormat	texFormat			= glu::mapGLInternalFormat(GL_RGBA8);
277 		const VkFormat				format				= mapTextureFormat(texFormat);
278 		const VkImageUsageFlags		imageUsageFlags		= VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
279 		Allocator&					memAlloc			= m_context.getDefaultAllocator();
280 		tcu::TextureLevel			texture				(texFormat, static_cast<int>(viewportSize.x()), static_cast<int>(viewportSize.y()));
281 
282 		// Fill with data
283 		{
284 			tcu::PixelBufferAccess access = texture.getAccess();
285 			for (int x = 0; x < texture.getWidth(); ++x)
286 				access.setPixel(data[x], x, 0);
287 		}
288 		// Create image
289 		const VkImageCreateInfo			imageParams =
290 		{
291 			VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,	// VkStructureType			sType;
292 			DE_NULL,								// const void*				pNext;
293 			0,										// VkImageCreateFlags		flags;
294 			VK_IMAGE_TYPE_2D,						// VkImageType				imageType;
295 			format,									// VkFormat					format;
296 			{										// VkExtent3D				extent;
297 					viewportSize.x(),
298 					viewportSize.y(),
299 					1u,
300 			},
301 			1u,							// deUint32					mipLevels;
302 			1u,							// deUint32					arrayLayers;
303 			VK_SAMPLE_COUNT_1_BIT,		// VkSampleCountFlagBits	samples;
304 			VK_IMAGE_TILING_OPTIMAL,	// VkImageTiling			tiling;
305 			imageUsageFlags,			// VkImageUsageFlags		usage;
306 			VK_SHARING_MODE_EXCLUSIVE,	// VkSharingMode			sharingMode;
307 			0u,							// deUint32					queueFamilyIndexCount;
308 			DE_NULL,					// const deUint32*			pQueueFamilyIndices;
309 			VK_IMAGE_LAYOUT_UNDEFINED	// VkImageLayout			initialLayout;
310 		};
311 
312 		m_texture		= createImage(vk, device, &imageParams);
313 		m_allocation	= memAlloc.allocate(getImageMemoryRequirements(vk, device, *m_texture), MemoryRequirement::Any);
314 		VK_CHECK(vk.bindImageMemory(device, *m_texture, m_allocation->getMemory(), m_allocation->getOffset()));
315 		uploadImage(m_context, texture.getAccess(), *m_texture);
316 
317 		m_descriptorSetLayout	=	DescriptorSetLayoutBuilder()
318 									.addSingleBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_GEOMETRY_BIT)
319 									.build(vk, device);
320 		m_descriptorPool		=	DescriptorPoolBuilder()
321 									.addType(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
322 									.build(vk, device, VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, 1u);
323 		m_descriptorSet			=	makeDescriptorSet(vk, device, *m_descriptorPool, *m_descriptorSetLayout);
324 
325 		return makePipelineLayout(vk, device, *m_descriptorSetLayout);
326 	}
327 	else
328 		return makePipelineLayout(vk, device);
329 }
330 
bindDescriptorSets(const DeviceInterface & vk,const VkDevice device,Allocator & memAlloc,const VkCommandBuffer & cmdBuffer,const VkPipelineLayout & pipelineLayout)331 void VaryingOutputCountTestInstance::bindDescriptorSets (const DeviceInterface& vk, const VkDevice device, Allocator& memAlloc,
332 														 const VkCommandBuffer& cmdBuffer, const VkPipelineLayout& pipelineLayout)
333 {
334 	if (m_test == READ_UNIFORM)
335 	{
336 		const deInt32				emitCount[4]		= { 6, 0, m_maxEmitCount, 10 };
337 		const VkBufferCreateInfo	bufferCreateInfo	= makeBufferCreateInfo(sizeof(emitCount), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT);
338 		m_buffer										= createBuffer(vk, device, &bufferCreateInfo);
339 		m_allocation									= memAlloc.allocate(getBufferMemoryRequirements(vk, device, *m_buffer), MemoryRequirement::HostVisible);
340 
341 		VK_CHECK(vk.bindBufferMemory(device, *m_buffer, m_allocation->getMemory(), m_allocation->getOffset()));
342 		{
343 			deMemcpy(m_allocation->getHostPtr(), &emitCount[0], sizeof(emitCount));
344 			flushAlloc(vk, device, *m_allocation);
345 
346 			const VkDescriptorBufferInfo bufferDescriptorInfo = makeDescriptorBufferInfo(*m_buffer, 0ull, sizeof(emitCount));
347 
348 			DescriptorSetUpdateBuilder()
349 				.writeSingle(*m_descriptorSet, DescriptorSetUpdateBuilder::Location::binding(0u), VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &bufferDescriptorInfo)
350 				.update(vk, device);
351 			vk.cmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0u, 1u, &*m_descriptorSet, 0u, DE_NULL);
352 		}
353 	}
354 	else if (m_test == READ_TEXTURE)
355 	{
356 		const tcu::TextureFormat	texFormat			= glu::mapGLInternalFormat(GL_RGBA8);
357 		const VkFormat				format				= mapTextureFormat(texFormat);
358 		const VkSamplerCreateInfo	samplerParams		=
359 														{
360 															VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,		// VkStructureType			sType;
361 															DE_NULL,									// const void*				pNext;
362 															0u,											// VkSamplerCreateFlags		flags;
363 															VK_FILTER_NEAREST,							// VkFilter					magFilter;
364 															VK_FILTER_NEAREST,							// VkFilter					minFilter;
365 															VK_SAMPLER_MIPMAP_MODE_NEAREST,				// VkSamplerMipmapMode		mipmapMode;
366 															VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,		// VkSamplerAddressMode		addressModeU;
367 															VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,		// VkSamplerAddressMode		addressModeV;
368 															VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,		// VkSamplerAddressMode		addressModeW;
369 															0.0f,										// float					mipLodBias;
370 															VK_FALSE,									// VkBool32					anisotropyEnable;
371 															1.0f,										// float					maxAnisotropy;
372 															false,										// VkBool32					compareEnable;
373 															VK_COMPARE_OP_NEVER,						// VkCompareOp				compareOp;
374 															0.0f,										// float					minLod;
375 															0.0f,										// float					maxLod;
376 															VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK,	// VkBorderColor			borderColor;
377 															false										// VkBool32					unnormalizedCoordinates;
378 														};
379 		m_sampler										= createSampler(vk, device, &samplerParams);
380 		const VkImageViewCreateInfo	viewParams			=
381 														{
382 															VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,	// VkStructureType			sType;
383 															NULL,										// const voide*				pNext;
384 															0u,											// VkImageViewCreateFlags	flags;
385 															*m_texture,									// VkImage					image;
386 															VK_IMAGE_VIEW_TYPE_2D,						// VkImageViewType			viewType;
387 															format,										// VkFormat					format;
388 															makeComponentMappingRGBA(),					// VkChannelMapping			channels;
389 															{
390 																VK_IMAGE_ASPECT_COLOR_BIT,				// VkImageAspectFlags	aspectMask;
391 																0u,										// deUint32				baseMipLevel;
392 																1u,										// deUint32				mipLevels;
393 																0,										// deUint32				baseArraySlice;
394 																1u										// deUint32				arraySize;
395 															},											// VkImageSubresourceRange	subresourceRange;
396 														};
397 		m_imageView										= createImageView(vk, device, &viewParams);
398 		const VkDescriptorImageInfo descriptorImageInfo = makeDescriptorImageInfo (*m_sampler, *m_imageView, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
399 		DescriptorSetUpdateBuilder()
400 			.writeSingle(*m_descriptorSet, DescriptorSetUpdateBuilder::Location::binding(0u), VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, &descriptorImageInfo)
401 			.update(vk, device);
402 		vk.cmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0u, 1u, &*m_descriptorSet, 0u, DE_NULL);
403 	}
404 }
405 
genVertexDataWithoutInstancing(void)406 void VaryingOutputCountTestInstance::genVertexDataWithoutInstancing (void)
407 {
408 	m_numDrawVertices = 4;
409 	m_vertexPosData.resize(m_numDrawVertices);
410 	m_vertexAttrData.resize(m_numDrawVertices);
411 
412 	m_vertexPosData[0] = tcu::Vec4( 0.5f,  0.0f, 0.0f, 1.0f);
413 	m_vertexPosData[1] = tcu::Vec4( 0.0f,  0.5f, 0.0f, 1.0f);
414 	m_vertexPosData[2] = tcu::Vec4(-0.7f, -0.1f, 0.0f, 1.0f);
415 	m_vertexPosData[3] = tcu::Vec4(-0.1f, -0.7f, 0.0f, 1.0f);
416 
417 	if (m_test == READ_ATTRIBUTE)
418 	{
419 		m_vertexAttrData[0] = tcu::Vec4(((EMIT_COUNT_VERTEX_0 == -1) ? ((float)m_maxEmitCount) : ((float)EMIT_COUNT_VERTEX_0)), 0.0f, 0.0f, 0.0f);
420 		m_vertexAttrData[1] = tcu::Vec4(((EMIT_COUNT_VERTEX_1 == -1) ? ((float)m_maxEmitCount) : ((float)EMIT_COUNT_VERTEX_1)), 0.0f, 0.0f, 0.0f);
421 		m_vertexAttrData[2] = tcu::Vec4(((EMIT_COUNT_VERTEX_2 == -1) ? ((float)m_maxEmitCount) : ((float)EMIT_COUNT_VERTEX_2)), 0.0f, 0.0f, 0.0f);
422 		m_vertexAttrData[3] = tcu::Vec4(((EMIT_COUNT_VERTEX_3 == -1) ? ((float)m_maxEmitCount) : ((float)EMIT_COUNT_VERTEX_3)), 0.0f, 0.0f, 0.0f);
423 	}
424 	else
425 	{
426 		m_vertexAttrData[0] = tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f);
427 		m_vertexAttrData[1] = tcu::Vec4(1.0f, 0.0f, 0.0f, 0.0f);
428 		m_vertexAttrData[2] = tcu::Vec4(2.0f, 0.0f, 0.0f, 0.0f);
429 		m_vertexAttrData[3] = tcu::Vec4(3.0f, 0.0f, 0.0f, 0.0f);
430 	}
431 }
432 
genVertexDataWithInstancing(void)433 void VaryingOutputCountTestInstance::genVertexDataWithInstancing (void)
434 {
435 	m_numDrawVertices = 1;
436 	m_vertexPosData.resize(m_numDrawVertices);
437 	m_vertexAttrData.resize(m_numDrawVertices);
438 
439 	m_vertexPosData[0] = tcu::Vec4(0.0f,  0.0f, 0.0f, 1.0f);
440 
441 	if (m_test == READ_ATTRIBUTE)
442 	{
443 		const int emitCounts[] =
444 		{
445 			(EMIT_COUNT_VERTEX_0 == -1) ? (m_maxEmitCount) : (EMIT_COUNT_VERTEX_0),
446 			(EMIT_COUNT_VERTEX_1 == -1) ? (m_maxEmitCount) : (EMIT_COUNT_VERTEX_1),
447 			(EMIT_COUNT_VERTEX_2 == -1) ? (m_maxEmitCount) : (EMIT_COUNT_VERTEX_2),
448 			(EMIT_COUNT_VERTEX_3 == -1) ? (m_maxEmitCount) : (EMIT_COUNT_VERTEX_3),
449 		};
450 
451 		m_vertexAttrData[0] = tcu::Vec4((float)emitCounts[0], (float)emitCounts[1], (float)emitCounts[2], (float)emitCounts[3]);
452 	}
453 	else
454 	{
455 		// not used
456 		m_vertexAttrData[0] = tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f);
457 	}
458 }
459 
460 class BuiltinVariableRenderTestInstance: public GeometryExpanderRenderTestInstance
461 {
462 public:
463 			BuiltinVariableRenderTestInstance	(Context&				context,
464 												 const char*			name,
465 												 const VariableTest		test,
466 												 const bool				indicesTest);
467 	void	genVertexAttribData					(void);
468 	void	createIndicesBuffer					(void);
469 
470 protected:
471 	void	drawCommand							(const VkCommandBuffer&	cmdBuffer);
472 
473 private:
474 	const bool				m_indicesTest;
475 	std::vector<deUint16>	m_indices;
476 	Move<vk::VkBuffer>		m_indicesBuffer;
477 	MovePtr<Allocation>		m_allocation;
478 };
479 
BuiltinVariableRenderTestInstance(Context & context,const char * name,const VariableTest test,const bool indicesTest)480 BuiltinVariableRenderTestInstance::BuiltinVariableRenderTestInstance (Context& context, const char* name, const VariableTest test, const bool indicesTest)
481 	: GeometryExpanderRenderTestInstance	(context, (test == TEST_PRIMITIVE_ID_IN) ? VK_PRIMITIVE_TOPOLOGY_LINE_STRIP : VK_PRIMITIVE_TOPOLOGY_POINT_LIST, name)
482 	, m_indicesTest							(indicesTest)
483 {
484 	genVertexAttribData();
485 }
486 
genVertexAttribData(void)487 void BuiltinVariableRenderTestInstance::genVertexAttribData (void)
488 {
489 	m_numDrawVertices = 5;
490 
491 	m_vertexPosData.resize(m_numDrawVertices);
492 	m_vertexPosData[0] = tcu::Vec4( 0.5f,  0.0f, 0.0f, 1.0f);
493 	m_vertexPosData[1] = tcu::Vec4( 0.0f,  0.5f, 0.0f, 1.0f);
494 	m_vertexPosData[2] = tcu::Vec4(-0.7f, -0.1f, 0.0f, 1.0f);
495 	m_vertexPosData[3] = tcu::Vec4(-0.1f, -0.7f, 0.0f, 1.0f);
496 	m_vertexPosData[4] = tcu::Vec4( 0.5f,  0.0f, 0.0f, 1.0f);
497 
498 	m_vertexAttrData.resize(m_numDrawVertices);
499 	m_vertexAttrData[0] = tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f);
500 	m_vertexAttrData[1] = tcu::Vec4(1.0f, 0.0f, 0.0f, 0.0f);
501 	m_vertexAttrData[2] = tcu::Vec4(2.0f, 0.0f, 0.0f, 0.0f);
502 	m_vertexAttrData[3] = tcu::Vec4(3.0f, 0.0f, 0.0f, 0.0f);
503 	m_vertexAttrData[4] = tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f);
504 
505 	if (m_indicesTest)
506 	{
507 		// Only used by primitive ID restart test
508 		m_indices.resize(m_numDrawVertices);
509 		m_indices[0] = 1;
510 		m_indices[1] = 4;
511 		m_indices[2] = 0xFFFF; // restart
512 		m_indices[3] = 2;
513 		m_indices[4] = 1;
514 		createIndicesBuffer();
515 	}
516 }
517 
createIndicesBuffer(void)518 void BuiltinVariableRenderTestInstance::createIndicesBuffer (void)
519 {
520 	// Create vertex indices buffer
521 	const DeviceInterface&			vk					= m_context.getDeviceInterface();
522 	const VkDevice					device				= m_context.getDevice();
523 	Allocator&						memAlloc			= m_context.getDefaultAllocator();
524 	const VkDeviceSize				indexBufferSize		= m_indices.size() * sizeof(deUint16);
525 	const VkBufferCreateInfo		indexBufferParams	=
526 														{
527 															VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,	// VkStructureType		sType;
528 															DE_NULL,								// const void*			pNext;
529 															0u,										// VkBufferCreateFlags	flags;
530 															indexBufferSize,						// VkDeviceSize			size;
531 															VK_BUFFER_USAGE_INDEX_BUFFER_BIT,		// VkBufferUsageFlags	usage;
532 															VK_SHARING_MODE_EXCLUSIVE,				// VkSharingMode		sharingMode;
533 															0u,										// deUint32				queueFamilyCount;
534 															DE_NULL									// const deUint32*		pQueueFamilyIndices;
535 														};
536 
537 	m_indicesBuffer = createBuffer(vk, device, &indexBufferParams);
538 	m_allocation = memAlloc.allocate(getBufferMemoryRequirements(vk, device, *m_indicesBuffer), MemoryRequirement::HostVisible);
539 	VK_CHECK(vk.bindBufferMemory(device, *m_indicesBuffer, m_allocation->getMemory(), m_allocation->getOffset()));
540 	// Load indices into buffer
541 	deMemcpy(m_allocation->getHostPtr(), &m_indices[0], (size_t)indexBufferSize);
542 	flushAlloc(vk, device, *m_allocation);
543 }
544 
drawCommand(const VkCommandBuffer & cmdBuffer)545 void BuiltinVariableRenderTestInstance::drawCommand (const VkCommandBuffer&	cmdBuffer)
546 {
547 	const DeviceInterface&	vk = m_context.getDeviceInterface();
548 	if (m_indicesTest)
549 	{
550 		vk.cmdBindIndexBuffer(cmdBuffer, *m_indicesBuffer, 0, VK_INDEX_TYPE_UINT16);
551 		vk.cmdDrawIndexed(cmdBuffer, static_cast<deUint32>(m_indices.size()), 1, 0, 0, 0);
552 	}
553 	else
554 		vk.cmdDraw(cmdBuffer, static_cast<deUint32>(m_numDrawVertices), 1u, 0u, 0u);
555 }
556 
557 class GeometryOutputCountTest : public TestCase
558 {
559 public:
560 							GeometryOutputCountTest	(TestContext&		testCtx,
561 													 const char*		name,
562 													 const vector<int>	pattern);
563 
564 	void					initPrograms			(SourceCollections&	sourceCollections) const;
565 	virtual TestInstance*	createInstance			(Context&			context) const;
566 	virtual void			checkSupport			(Context&			context) const;
567 
568 protected:
569 	const vector<int> m_pattern;
570 };
571 
GeometryOutputCountTest(TestContext & testCtx,const char * name,const vector<int> pattern)572 GeometryOutputCountTest::GeometryOutputCountTest (TestContext& testCtx, const char* name, const vector<int> pattern)
573 	: TestCase	(testCtx, name)
574 	, m_pattern	(pattern)
575 {
576 
577 }
578 
checkSupport(Context & context) const579 void GeometryOutputCountTest::checkSupport (Context& context) const
580 {
581 	context.requireDeviceCoreFeature(DEVICE_CORE_FEATURE_GEOMETRY_SHADER);
582 }
583 
initPrograms(SourceCollections & sourceCollections) const584 void GeometryOutputCountTest::initPrograms (SourceCollections& sourceCollections) const
585 {
586 	{
587 		std::ostringstream src;
588 		src	<< "#version 310 es\n"
589 			<<"layout(location = 0) in highp vec4 a_position;\n"
590 			<<"layout(location = 1) in highp vec4 a_color;\n"
591 			<<"layout(location = 0) out highp vec4 v_geom_FragColor;\n"
592 			<<"void main (void)\n"
593 			<<"{\n"
594 			<<"	gl_Position = a_position;\n"
595 			<<"	v_geom_FragColor = a_color;\n"
596 			<<"}\n";
597 		sourceCollections.glslSources.add("vertex") << glu::VertexSource(src.str());
598 	}
599 
600 	{
601 		const int max_vertices = m_pattern.size() == 2 ? std::max(m_pattern[0], m_pattern[1]) : m_pattern[0];
602 
603 		std::ostringstream src;
604 		src	<< "#version 310 es\n"
605 			<< "#extension GL_EXT_geometry_shader : require\n"
606 			<< "#extension GL_OES_texture_storage_multisample_2d_array : require\n"
607 			<< "layout(points) in;\n"
608 			<< "layout(triangle_strip, max_vertices = " << max_vertices << ") out;\n"
609 			<< "layout(location = 0) in highp vec4 v_geom_FragColor[];\n"
610 			<< "layout(location = 0) out highp vec4 v_frag_FragColor;\n"
611 			<< "out gl_PerVertex\n"
612 			<< "{\n"
613 			<< "	vec4 gl_Position;\n"
614 			<< "};\n"
615 			<< "void main (void)\n"
616 			<< "{\n"
617 			<< "	const highp float rowHeight = 2.0 / float(" << m_pattern.size() << ");\n"
618 			<< "	const highp float colWidth = 2.0 / float(" << max_vertices << ");\n";
619 
620 		if (m_pattern.size() == 2)
621 			src	<< "	highp int emitCount = (gl_PrimitiveIDIn == 0) ? (" << m_pattern[0] << ") : (" << m_pattern[1] << ");\n";
622 		else
623 			src	<< "	highp int emitCount = " << m_pattern[0] << ";\n";
624 		src	<< "	for (highp int ndx = 0; ndx < emitCount / 2; ndx++)\n"
625 			<< "	{\n"
626 			<< "		gl_Position = gl_in[0].gl_Position + vec4(float(ndx) * 2.0 * colWidth, 0.0, 0.0, 0.0);\n"
627 			<< "		v_frag_FragColor = v_geom_FragColor[0];\n"
628 			<< "		EmitVertex();\n"
629 
630 			<< "		gl_Position = gl_in[0].gl_Position + vec4(float(ndx) * 2.0 * colWidth, rowHeight, 0.0, 0.0);\n"
631 			<< "		v_frag_FragColor = v_geom_FragColor[0];\n"
632 			<< "		EmitVertex();\n"
633 
634 			<< "	}\n"
635 			<< "}\n";
636 		sourceCollections.glslSources.add("geometry") << glu::GeometrySource(src.str());
637 	}
638 
639 	{
640 		std::ostringstream src;
641 		src	<< "#version 310 es\n"
642 			<<"layout(location = 0) out mediump vec4 fragColor;\n"
643 			<<"layout(location = 0) in highp vec4 v_frag_FragColor;\n"
644 			<<"void main (void)\n"
645 			<<"{\n"
646 			<<"	fragColor = v_frag_FragColor;\n"
647 			<<"}\n";
648 		sourceCollections.glslSources.add("fragment") << glu::FragmentSource(src.str());
649 	}
650 }
651 
createInstance(Context & context) const652 TestInstance* GeometryOutputCountTest::createInstance (Context& context) const
653 {
654 	return new GeometryOutputCountTestInstance (context, VK_PRIMITIVE_TOPOLOGY_POINT_LIST, static_cast<int>(m_pattern.size()), getName());
655 }
656 
657 class VaryingOutputCountCase : public TestCase
658 {
659 public:
660 							VaryingOutputCountCase	(TestContext&				testCtx,
661 													 const char*				name,
662 													 const VaryingSource		test,
663 													 const ShaderInstancingMode	mode);
664 	void					initPrograms			(SourceCollections&			sourceCollections) const;
665 	virtual TestInstance*	createInstance			(Context&					context) const;
666 	virtual void			checkSupport			(Context&					context) const;
667 protected:
668 	const VaryingSource			m_test;
669 	const ShaderInstancingMode	m_mode;
670 };
671 
VaryingOutputCountCase(TestContext & testCtx,const char * name,const VaryingSource test,const ShaderInstancingMode mode)672 VaryingOutputCountCase::VaryingOutputCountCase (TestContext& testCtx, const char* name, const VaryingSource test, const ShaderInstancingMode mode)
673 	: TestCase	(testCtx, name)
674 	, m_test	(test)
675 	, m_mode	(mode)
676 {
677 }
678 
checkSupport(Context & context) const679 void VaryingOutputCountCase::checkSupport (Context& context) const
680 {
681 	context.requireDeviceCoreFeature(DEVICE_CORE_FEATURE_GEOMETRY_SHADER);
682 }
683 
initPrograms(SourceCollections & sourceCollections) const684 void VaryingOutputCountCase::initPrograms (SourceCollections& sourceCollections) const
685 {
686 	{
687 		std::ostringstream src;
688 		switch(m_test)
689 		{
690 			case READ_ATTRIBUTE:
691 			case READ_TEXTURE:
692 				src	<< "#version 310 es\n"
693 					<< "layout(location = 0) in highp vec4 a_position;\n"
694 					<< "layout(location = 1) in highp vec4 a_emitCount;\n"
695 					<< "layout(location = 0) out highp vec4 v_geom_emitCount;\n"
696 					<< "void main (void)\n"
697 					<< "{\n"
698 					<< "	gl_Position = a_position;\n"
699 					<< "	v_geom_emitCount = a_emitCount;\n"
700 					<< "}\n";
701 				break;
702 			case READ_UNIFORM:
703 				src	<< "#version 310 es\n"
704 					<< "layout(location = 0) in highp vec4 a_position;\n"
705 					<< "layout(location = 1) in highp vec4 a_vertexNdx;\n"
706 					<< "layout(location = 0) out highp vec4 v_geom_vertexNdx;\n"
707 					<< "void main (void)\n"
708 					<< "{\n"
709 					<< "	gl_Position = a_position;\n"
710 					<< "	v_geom_vertexNdx = a_vertexNdx;\n"
711 					<< "}\n";
712 				break;
713 			default:
714 				DE_ASSERT(0);
715 				break;
716 		}
717 		sourceCollections.glslSources.add("vertex") << glu::VertexSource(src.str());
718 	}
719 
720 	{
721 		const bool instanced = MODE_WITH_INSTANCING == m_mode;
722 		std::ostringstream src;
723 		src	<< "#version 310 es\n"
724 			<< "#extension GL_EXT_geometry_shader : require\n"
725 			<< "#extension GL_OES_texture_storage_multisample_2d_array : require\n";
726 		if (instanced)
727 			src	<< "layout(points, invocations=4) in;\n";
728 		else
729 			src	<< "layout(points) in;\n";
730 
731 		switch(m_test)
732 		{
733 			case READ_ATTRIBUTE:
734 				src	<< "layout(triangle_strip, max_vertices = 128) out;\n"
735 					<< "layout(location = 0) in highp vec4 v_geom_emitCount[];\n"
736 					<< "layout(location = 0) out highp vec4 v_frag_FragColor;\n"
737 					<< "out gl_PerVertex\n"
738 					<< "{\n"
739 					<< "	vec4 gl_Position;\n"
740 					<< "};\n"
741 					<< "void main (void)\n"
742 					<< "{\n"
743 					<< "	highp vec4 attrEmitCounts = v_geom_emitCount[0];\n"
744 					<< "	mediump int emitCount = int(attrEmitCounts[" << ((instanced) ? ("gl_InvocationID") : ("0")) << "]);\n"
745 					<< "	highp vec4 color = vec4((emitCount < 10) ? (0.0) : (1.0), (emitCount > 10) ? (0.0) : (1.0), 1.0, 1.0);\n"
746 					<< "	highp vec4 basePos = " << ((instanced) ? ("gl_in[0].gl_Position + 0.5 * vec4(cos(float(gl_InvocationID)), sin(float(gl_InvocationID)), 0.0, 0.0)") : ("gl_in[0].gl_Position")) << ";\n"
747 					<< "	for (mediump int i = 0; i < emitCount / 2; i++)\n"
748 					<< "	{\n"
749 					<< "		highp float angle = (float(i) + 0.5) / float(emitCount / 2) * 3.142;\n"
750 					<< "		gl_Position = basePos + vec4(cos(angle),  sin(angle), 0.0, 0.0) * 0.15;\n"
751 					<< "		v_frag_FragColor = color;\n"
752 					<< "		EmitVertex();\n"
753 					<< "		gl_Position = basePos + vec4(cos(angle), -sin(angle), 0.0, 0.0) * 0.15;\n"
754 					<< "		v_frag_FragColor = color;\n"
755 					<< "		EmitVertex();\n"
756 					<< "	}\n"
757 					<<"}\n";
758 				break;
759 			case READ_UNIFORM:
760 				src	<< "layout(triangle_strip, max_vertices = 128) out;\n"
761 					<< "layout(location = 0) in highp vec4 v_geom_vertexNdx[];\n"
762 					<< "layout(binding = 0) readonly uniform Input {\n"
763 					<< "	ivec4 u_emitCount;\n"
764 					<< "} emit;\n"
765 					<< "layout(location = 0) out highp vec4 v_frag_FragColor;\n"
766 					<< "out gl_PerVertex\n"
767 					<< "{\n"
768 					<< "	vec4 gl_Position;\n"
769 					<< "};\n"
770 					<< "void main (void)\n"
771 					<< "{\n"
772 					<< "	mediump int primitiveNdx = " << ((instanced) ? ("gl_InvocationID") : ("int(v_geom_vertexNdx[0].x)")) << ";\n"
773 					<< "	mediump int emitCount = emit.u_emitCount[primitiveNdx];\n"
774 					<< "\n"
775 					<< "	const highp vec4 red = vec4(1.0, 0.0, 0.0, 1.0);\n"
776 					<< "	const highp vec4 green = vec4(0.0, 1.0, 0.0, 1.0);\n"
777 					<< "	const highp vec4 blue = vec4(0.0, 0.0, 1.0, 1.0);\n"
778 					<< "	const highp vec4 yellow = vec4(1.0, 1.0, 0.0, 1.0);\n"
779 					<< "	const highp vec4 colors[4] = vec4[4](red, green, blue, yellow);\n"
780 					<< "	highp vec4 color = colors[int(primitiveNdx)];\n"
781 					<< "\n"
782 					<< "	highp vec4 basePos = " << ((instanced) ? ("gl_in[0].gl_Position + 0.5 * vec4(cos(float(gl_InvocationID)), sin(float(gl_InvocationID)), 0.0, 0.0)") : ("gl_in[0].gl_Position")) << ";\n"
783 					<< "	for (mediump int i = 0; i < emitCount / 2; i++)\n"
784 					<< "	{\n"
785 					<< "		highp float angle = (float(i) + 0.5) / float(emitCount / 2) * 3.142;\n"
786 					<< "		gl_Position = basePos + vec4(cos(angle),  sin(angle), 0.0, 0.0) * 0.15;\n"
787 					<< "		v_frag_FragColor = color;\n"
788 					<< "		EmitVertex();\n"
789 					<< "		gl_Position = basePos + vec4(cos(angle), -sin(angle), 0.0, 0.0) * 0.15;\n"
790 					<< "		v_frag_FragColor = color;\n"
791 					<< "		EmitVertex();\n"
792 					<< "	}\n"
793 					<<"}\n";
794 				break;
795 			case READ_TEXTURE:
796 				src	<< "layout(triangle_strip, max_vertices = 128) out;\n"
797 					<< "layout(location = 0) in highp vec4 v_geom_vertexNdx[];\n"
798 					<< "layout(binding = 0) uniform highp sampler2D u_sampler;\n"
799 					<< "layout(location = 0) out highp vec4 v_frag_FragColor;\n"
800 					<< "out gl_PerVertex\n"
801 					<< "{\n"
802 					<< "	vec4 gl_Position;\n"
803 					<< "};\n"
804 					<< "void main (void)\n"
805 					<< "{\n"
806 					<< "	highp float primitiveNdx = " << ((instanced) ? ("float(gl_InvocationID)") : ("v_geom_vertexNdx[0].x")) << ";\n"
807 					<< "	highp vec2 texCoord = vec2(1.0 / 8.0 + primitiveNdx / 4.0, 0.5);\n"
808 					<< "	highp vec4 texColor = texture(u_sampler, texCoord);\n"
809 					<< "	mediump int emitCount = 0;\n"
810 					<< "	if (texColor.x > 0.0)\n"
811 					<< "		emitCount += 6;\n"
812 					<< "	if (texColor.y > 0.0)\n"
813 					<< "		emitCount += 0;\n"
814 					<< "	if (texColor.z > 0.0)\n"
815 					<< "		emitCount += 128;\n"
816 					<< "	if (texColor.w > 0.0)\n"
817 					<< "		emitCount += 10;\n"
818 					<< "	const highp vec4 red = vec4(1.0, 0.0, 0.0, 1.0);\n"
819 					<< "	const highp vec4 green = vec4(0.0, 1.0, 0.0, 1.0);\n"
820 					<< "	const highp vec4 blue = vec4(0.0, 0.0, 1.0, 1.0);\n"
821 					<< "	const highp vec4 yellow = vec4(1.0, 1.0, 0.0, 1.0);\n"
822 					<< "	const highp vec4 colors[4] = vec4[4](red, green, blue, yellow);\n"
823 					<< "	highp vec4 color = colors[int(primitiveNdx)];\n"
824 					<< "	highp vec4 basePos = "<< ((instanced) ? ("gl_in[0].gl_Position + 0.5 * vec4(cos(float(gl_InvocationID)), sin(float(gl_InvocationID)), 0.0, 0.0)") : ("gl_in[0].gl_Position")) << ";\n"
825 					<< "	for (mediump int i = 0; i < emitCount / 2; i++)\n"
826 					<< "	{\n"
827 					<< "		highp float angle = (float(i) + 0.5) / float(emitCount / 2) * 3.142;\n"
828 					<< "		gl_Position = basePos + vec4(cos(angle),  sin(angle), 0.0, 0.0) * 0.15;\n"
829 					<< "		v_frag_FragColor = color;\n"
830 					<< "		EmitVertex();\n"
831 					<< "		gl_Position = basePos + vec4(cos(angle), -sin(angle), 0.0, 0.0) * 0.15;\n"
832 					<< "		v_frag_FragColor = color;\n"
833 					<< "		EmitVertex();\n"
834 					<< "	}\n"
835 					<<"}\n";
836 				break;
837 			default:
838 				DE_ASSERT(0);
839 				break;
840 		}
841 		sourceCollections.glslSources.add("geometry") << glu::GeometrySource(src.str());
842 	}
843 
844 	{
845 		std::ostringstream src;
846 		src	<< "#version 310 es\n"
847 			<< "layout(location = 0) out mediump vec4 fragColor;\n"
848 			<< "layout(location = 0) in highp vec4 v_frag_FragColor;\n"
849 			<< "void main (void)\n"
850 			<< "{\n"
851 			<< "	fragColor = v_frag_FragColor;\n"
852 			<< "}\n";
853 		sourceCollections.glslSources.add("fragment") << glu::FragmentSource(src.str());
854 	}
855 }
856 
createInstance(Context & context) const857 TestInstance* VaryingOutputCountCase::createInstance (Context& context) const
858 {
859 	return new VaryingOutputCountTestInstance (context, getName(), VK_PRIMITIVE_TOPOLOGY_POINT_LIST, m_test, m_mode);
860 }
861 
862 class BuiltinVariableRenderTest : public TestCase
863 {
864 public:
865 							BuiltinVariableRenderTest	(TestContext&		testCtx,
866 														const char*			name,
867 														const VariableTest	test,
868 														const bool			flag = false);
869 	void					initPrograms				(SourceCollections&	sourceCollections) const;
870 	virtual TestInstance*	createInstance				(Context&			context) const;
871 	virtual void			checkSupport				(Context&			context) const;
872 protected:
873 	const VariableTest	m_test;
874 	const bool			m_flag;
875 };
876 
BuiltinVariableRenderTest(TestContext & testCtx,const char * name,const VariableTest test,const bool flag)877 BuiltinVariableRenderTest::BuiltinVariableRenderTest (TestContext& testCtx, const char* name, const VariableTest test, const bool flag)
878 	: TestCase	(testCtx, name)
879 	, m_test	(test)
880 	, m_flag	(flag)
881 {
882 }
883 
checkSupport(Context & context) const884 void BuiltinVariableRenderTest::checkSupport (Context& context) const
885 {
886 	context.requireDeviceCoreFeature(DEVICE_CORE_FEATURE_GEOMETRY_SHADER);
887 
888 	if (m_test == TEST_POINT_SIZE)
889 		context.requireDeviceCoreFeature(DEVICE_CORE_FEATURE_SHADER_TESSELLATION_AND_GEOMETRY_POINT_SIZE);
890 }
891 
892 
initPrograms(SourceCollections & sourceCollections) const893 void BuiltinVariableRenderTest::initPrograms (SourceCollections& sourceCollections) const
894 {
895 	{
896 		std::ostringstream src;
897 		src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_450) << "\n"
898 			<< "out gl_PerVertex\n"
899 			<<" {\n"
900 			<< "	vec4 gl_Position;\n"
901 			<< "	float gl_PointSize;\n"
902 			<< "};\n"
903 			<< "layout(location = 0) in vec4 a_position;\n";
904 		switch(m_test)
905 		{
906 			case TEST_POINT_SIZE:
907 				src	<< "layout(location = 1) in vec4 a_pointSize;\n"
908 					<< "layout(location = 0) out vec4 v_geom_pointSize;\n"
909 					<< "void main (void)\n"
910 					<< "{\n"
911 					<< "	gl_Position = a_position;\n"
912 					<< "	gl_PointSize = 1.0;\n"
913 					<< "	v_geom_pointSize = a_pointSize;\n"
914 					<< "}\n";
915 				break;
916 			case TEST_PRIMITIVE_ID_IN:
917 				src	<< "void main (void)\n"
918 					<< "{\n"
919 					<< "	gl_Position = a_position;\n"
920 					<< "}\n";
921 				break;
922 			case TEST_PRIMITIVE_ID:
923 				src	<< "layout(location = 1) in vec4 a_primitiveID;\n"
924 					<< "layout(location = 0) out vec4 v_geom_primitiveID;\n"
925 					<< "void main (void)\n"
926 					<< "{\n"
927 					<< "	gl_Position = a_position;\n"
928 					<< "	v_geom_primitiveID = a_primitiveID;\n"
929 					<< "}\n";
930 				break;
931 			default:
932 				DE_ASSERT(0);
933 				break;
934 		}
935 		sourceCollections.glslSources.add("vertex") << glu::VertexSource(src.str());
936 	}
937 
938 	{
939 		std::ostringstream src;
940 		src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_450) << "\n"
941 			<< "in gl_PerVertex\n"
942 			<<"{\n"
943 			<< "	vec4 gl_Position;\n"
944 			<< "	float gl_PointSize;\n"
945 			<< "} gl_in[];\n"
946 			<< "out gl_PerVertex\n"
947 			<<"{\n"
948 			<< "	vec4 gl_Position;\n"
949 			<< "	float gl_PointSize;\n"
950 			<< "};\n";
951 		switch(m_test)
952 		{
953 			case TEST_POINT_SIZE:
954 				src	<< "#extension GL_EXT_geometry_point_size : require\n"
955 					<< "layout(points) in;\n"
956 					<< "layout(points, max_vertices = 1) out;\n"
957 					<< "layout(location = 0) in vec4 v_geom_pointSize[];\n"
958 					<< "layout(location = 0) out vec4 v_frag_FragColor;\n"
959 					<< "void main (void)\n"
960 					<< "{\n"
961 					<< "	gl_Position = gl_in[0].gl_Position;\n"
962 					<< "	gl_PointSize = v_geom_pointSize[0].x + 1.0;\n"
963 					<< "	v_frag_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
964 					<< "	EmitVertex();\n"
965 					<< "}\n";
966 				break;
967 			case TEST_PRIMITIVE_ID_IN:
968 				src	<< "layout(lines) in;\n"
969 					<< "layout(triangle_strip, max_vertices = 10) out;\n"
970 					<< "layout(location = 0) out vec4 v_frag_FragColor;\n"
971 					<< "void main (void)\n"
972 					<< "{\n"
973 					<< "	const vec4 red = vec4(1.0, 0.0, 0.0, 1.0);\n"
974 					<< "	const vec4 green = vec4(0.0, 1.0, 0.0, 1.0);\n"
975 					<< "	const vec4 blue = vec4(0.0, 0.0, 1.0, 1.0);\n"
976 					<< "	const vec4 yellow = vec4(1.0, 1.0, 0.0, 1.0);\n"
977 					<< "	const vec4 colors[4] = vec4[4](red, green, blue, yellow);\n"
978 					<< "	for (int counter = 0; counter < 3; ++counter)\n"
979 					<< "	{\n"
980 					<< "		float percent = 0.1 * counter;\n"
981 					<< "		gl_Position = gl_in[0].gl_Position * vec4(1.0 + percent, 1.0 + percent, 1.0, 1.0);\n"
982 					<< "		v_frag_FragColor = colors[gl_PrimitiveIDIn % 4];\n"
983 					<< "		EmitVertex();\n"
984 					<< "		gl_Position = gl_in[1].gl_Position * vec4(1.0 + percent, 1.0 + percent, 1.0, 1.0);\n"
985 					<< "		v_frag_FragColor = colors[gl_PrimitiveIDIn % 4];\n"
986 					<< "		EmitVertex();\n"
987 					<< "	}\n"
988 					<< "}\n";
989 				break;
990 			case TEST_PRIMITIVE_ID:
991 				src	<< "layout(points, invocations=1) in;\n"
992 					<< "layout(triangle_strip, max_vertices = 3) out;\n"
993 					<< "layout(location = 0) in vec4 v_geom_primitiveID[];\n"
994 					<< "void main (void)\n"
995 					<< "{\n"
996 					<< "	gl_Position = gl_in[0].gl_Position + vec4(0.05, 0.0, 0.0, 0.0);\n"
997 					<< "	gl_PrimitiveID = int(floor(v_geom_primitiveID[0].x)) + 3;\n"
998 					<< "	EmitVertex();\n"
999 					<< "	gl_Position = gl_in[0].gl_Position - vec4(0.05, 0.0, 0.0, 0.0);\n"
1000 					<< "	gl_PrimitiveID = int(floor(v_geom_primitiveID[0].x)) + 3;\n"
1001 					<< "	EmitVertex();\n"
1002 					<< "	gl_Position = gl_in[0].gl_Position + vec4(0.0, 0.05, 0.0, 0.0);\n"
1003 					<< "	gl_PrimitiveID = int(floor(v_geom_primitiveID[0].x)) + 3;\n"
1004 					<< "	EmitVertex();\n"
1005 					<< "}\n";
1006 				break;
1007 			default:
1008 				DE_ASSERT(0);
1009 				break;
1010 		}
1011 		sourceCollections.glslSources.add("geometry") << glu::GeometrySource(src.str());
1012 	}
1013 
1014 	{
1015 		std::ostringstream src;
1016 		src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_450) << "\n";
1017 		switch(m_test)
1018 		{
1019 			case TEST_POINT_SIZE:
1020 				src	<< "layout(location = 0) out vec4 fragColor;\n"
1021 					<< "layout(location = 0) in vec4 v_frag_FragColor;\n"
1022 					<< "void main (void)\n"
1023 					<< "{\n"
1024 					<< "	fragColor = v_frag_FragColor;\n"
1025 					<< "}\n";
1026 				break;
1027 			case TEST_PRIMITIVE_ID_IN:
1028 				src	<< "layout(location = 0) out vec4 fragColor;\n"
1029 					<< "layout(location = 0) in vec4 v_frag_FragColor;\n"
1030 					<< "void main (void)\n"
1031 					<< "{\n"
1032 					<< "	fragColor = v_frag_FragColor;\n"
1033 					<< "}\n";
1034 				break;
1035 			case TEST_PRIMITIVE_ID:
1036 				src	<< "layout(location = 0) out vec4 fragColor;\n"
1037 					<< "void main (void)\n"
1038 					<< "{\n"
1039 					<< "	const vec4 red			= vec4(1.0, 0.0, 0.0, 1.0);\n"
1040 					<< "	const vec4 green		= vec4(0.0, 1.0, 0.0, 1.0);\n"
1041 					<< "	const vec4 blue			= vec4(0.0, 0.0, 1.0, 1.0);\n"
1042 					<< "	const vec4 yellow		= vec4(1.0, 1.0, 0.0, 1.0);\n"
1043 					<< "	const vec4 colors[4]	= vec4[4](yellow, red, green, blue);\n"
1044 					<< "	fragColor = colors[gl_PrimitiveID % 4];\n"
1045 					<< "}\n";
1046 				break;
1047 			default:
1048 				DE_ASSERT(0);
1049 				break;
1050 		}
1051 		sourceCollections.glslSources.add("fragment") << glu::FragmentSource(src.str());
1052 	}
1053 }
1054 
createInstance(Context & context) const1055 TestInstance* BuiltinVariableRenderTest::createInstance (Context& context) const
1056 {
1057 	return new BuiltinVariableRenderTestInstance(context, getName(), m_test, m_flag);
1058 }
1059 
createPattern(int count)1060 inline vector<int> createPattern (int count)
1061 {
1062 	vector<int>	pattern;
1063 	pattern.push_back(count);
1064 	return pattern;
1065 }
1066 
createPattern(int count0,int count1)1067 inline vector<int> createPattern (int count0, int count1)
1068 {
1069 	vector<int>	pattern;
1070 	pattern.push_back(count0);
1071 	pattern.push_back(count1);
1072 	return pattern;
1073 }
1074 
1075 } // anonymous
1076 
createBasicGeometryShaderTests(TestContext & testCtx)1077 TestCaseGroup* createBasicGeometryShaderTests (TestContext& testCtx)
1078 {
1079 	MovePtr<TestCaseGroup> basicGroup	(new tcu::TestCaseGroup(testCtx, "basic"));
1080 
1081 	// Output N vertices
1082 	basicGroup->addChild(new GeometryOutputCountTest	(testCtx,	"output_10",								createPattern(10)));
1083 	basicGroup->addChild(new GeometryOutputCountTest	(testCtx,	"output_128",								createPattern(128)));
1084 	// Output N, M vertices in two invocations
1085 	basicGroup->addChild(new GeometryOutputCountTest	(testCtx,	"output_10_and_100",	createPattern(10, 100)));
1086 	basicGroup->addChild(new GeometryOutputCountTest	(testCtx,	"output_100_and_10",	createPattern(100, 10)));
1087 	basicGroup->addChild(new GeometryOutputCountTest	(testCtx,	"output_0_and_128",		createPattern(0, 128)));
1088 	basicGroup->addChild(new GeometryOutputCountTest	(testCtx,	"output_128_and_0",		createPattern(128, 0)));
1089 
1090 	// Output varying number of vertices
1091 	basicGroup->addChild(new VaryingOutputCountCase		(testCtx,	"output_vary_by_attribute",	READ_ATTRIBUTE,	MODE_WITHOUT_INSTANCING));
1092 	basicGroup->addChild(new VaryingOutputCountCase		(testCtx,	"output_vary_by_uniform",	READ_UNIFORM,	MODE_WITHOUT_INSTANCING));
1093 	basicGroup->addChild(new VaryingOutputCountCase		(testCtx,	"output_vary_by_texture",	READ_TEXTURE,	MODE_WITHOUT_INSTANCING));
1094 	basicGroup->addChild(new VaryingOutputCountCase		(testCtx,	"output_vary_by_attribute_instancing",	READ_ATTRIBUTE,	MODE_WITH_INSTANCING));
1095 	basicGroup->addChild(new VaryingOutputCountCase		(testCtx,	"output_vary_by_uniform_instancing",	READ_UNIFORM,	MODE_WITH_INSTANCING));
1096 	basicGroup->addChild(new VaryingOutputCountCase		(testCtx,	"output_vary_by_texture_instancing",	READ_TEXTURE,	MODE_WITH_INSTANCING));
1097 
1098 	// test gl_PointSize
1099 	basicGroup->addChild(new BuiltinVariableRenderTest	(testCtx,	"point_size", TEST_POINT_SIZE));
1100 	// test gl_PrimitiveIDIn
1101 	basicGroup->addChild(new BuiltinVariableRenderTest	(testCtx,	"primitive_id_in", TEST_PRIMITIVE_ID_IN));
1102 	// test gl_PrimitiveIDIn with primitive restart
1103 	basicGroup->addChild(new BuiltinVariableRenderTest	(testCtx,	"primitive_id_in_restarted", TEST_PRIMITIVE_ID_IN, true));
1104 	// test gl_PrimitiveID
1105 	basicGroup->addChild(new BuiltinVariableRenderTest	(testCtx,	"primitive_id", TEST_PRIMITIVE_ID));
1106 
1107 	return basicGroup.release();
1108 }
1109 
1110 } // geometry
1111 } // vkt
1112