• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-------------------------------------------------------------------------
2  * drawElements Quality Program OpenGL ES 3.0 Module
3  * -------------------------------------------------
4  *
5  * Copyright 2014 The Android Open Source Project
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 Invariance tests.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "es3fShaderInvarianceTests.hpp"
25 #include "deStringUtil.hpp"
26 #include "deRandom.hpp"
27 #include "gluContextInfo.hpp"
28 #include "gluRenderContext.hpp"
29 #include "gluShaderProgram.hpp"
30 #include "gluPixelTransfer.hpp"
31 #include "glwFunctions.hpp"
32 #include "glwEnums.hpp"
33 #include "tcuRenderTarget.hpp"
34 #include "tcuTestLog.hpp"
35 #include "tcuSurface.hpp"
36 #include "tcuTextureUtil.hpp"
37 #include "tcuStringTemplate.hpp"
38 
39 
40 namespace deqp
41 {
42 namespace gles3
43 {
44 namespace Functional
45 {
46 namespace
47 {
48 
49 class FormatArgumentList;
50 
genRandomVector(de::Random & rnd)51 static tcu::Vec4 genRandomVector (de::Random& rnd)
52 {
53 	tcu::Vec4 retVal;
54 
55 	retVal.x() = rnd.getFloat(-1.0f, 1.0f);
56 	retVal.y() = rnd.getFloat(-1.0f, 1.0f);
57 	retVal.z() = rnd.getFloat(-1.0f, 1.0f);
58 	retVal.w() = rnd.getFloat( 0.2f, 1.0f);
59 
60 	return retVal;
61 }
62 
63 class FormatArgument
64 {
65 public:
66 						FormatArgument (const char* name, const std::string& value);
67 
68 private:
69 	friend class FormatArgumentList;
70 
71 	const char* const	m_name;
72 	const std::string	m_value;
73 };
74 
FormatArgument(const char * name,const std::string & value)75 FormatArgument::FormatArgument (const char* name, const std::string& value)
76 	: m_name	(name)
77 	, m_value	(value)
78 {
79 }
80 
81 class FormatArgumentList
82 {
83 public:
84 												FormatArgumentList	(void);
85 
86 	FormatArgumentList&							operator<<			(const FormatArgument&);
87 	const std::map<std::string, std::string>&	getArguments		(void) const;
88 
89 private:
90 	std::map<std::string, std::string>			m_formatArguments;
91 };
92 
FormatArgumentList(void)93 FormatArgumentList::FormatArgumentList (void)
94 {
95 }
96 
operator <<(const FormatArgument & arg)97 FormatArgumentList&	FormatArgumentList::operator<< (const FormatArgument& arg)
98 {
99 	m_formatArguments[arg.m_name] = arg.m_value;
100 	return *this;
101 }
102 
getArguments(void) const103 const std::map<std::string, std::string>& FormatArgumentList::getArguments (void) const
104 {
105 	return m_formatArguments;
106 }
107 
formatGLSL(const char * templateString,const FormatArgumentList & args)108 static std::string formatGLSL (const char* templateString, const FormatArgumentList& args)
109 {
110 	const std::map<std::string, std::string>& params = args.getArguments();
111 
112 	return tcu::StringTemplate(std::string(templateString)).specialize(params);
113 }
114 
115 /*--------------------------------------------------------------------*//*!
116  * \brief Vertex shader invariance test
117  *
118  * Test vertex shader invariance by drawing a test pattern two times, each
119  * time with a different shader. Shaders have set identical values to
120  * invariant gl_Position using identical expressions. No fragments from the
121  * first pass using should remain visible.
122  *//*--------------------------------------------------------------------*/
123 class InvarianceTest : public TestCase
124 {
125 public:
126 	struct ShaderPair
127 	{
128 		std::string vertexShaderSource0;
129 		std::string fragmentShaderSource0;
130 		std::string vertexShaderSource1;
131 		std::string fragmentShaderSource1;
132 	};
133 
134 							InvarianceTest		(Context& ctx, const char* name, const char* desc);
135 							~InvarianceTest		(void);
136 
137 	void					init				(void);
138 	void					deinit				(void);
139 	IterateResult			iterate				(void);
140 
141 private:
142 	virtual ShaderPair		genShaders			(void) const = DE_NULL;
143 	bool					checkImage			(const tcu::Surface&) const;
144 
145 	glu::ShaderProgram*		m_shader0;
146 	glu::ShaderProgram*		m_shader1;
147 	glw::GLuint				m_arrayBuf;
148 	int						m_verticesInPattern;
149 
150 	const int				m_renderSize;
151 };
152 
InvarianceTest(Context & ctx,const char * name,const char * desc)153 InvarianceTest::InvarianceTest (Context& ctx, const char* name, const char* desc)
154 	: TestCase				(ctx, name, desc)
155 	, m_shader0				(DE_NULL)
156 	, m_shader1				(DE_NULL)
157 	, m_arrayBuf			(0)
158 	, m_verticesInPattern	(0)
159 	, m_renderSize			(256)
160 {
161 }
162 
~InvarianceTest(void)163 InvarianceTest::~InvarianceTest (void)
164 {
165 	deinit();
166 }
167 
init(void)168 void InvarianceTest::init (void)
169 {
170 	// Invariance tests require drawing to the screen and reading back results.
171 	// Tests results are not reliable if the resolution is too small
172 	{
173 		if (m_context.getRenderTarget().getWidth()  < m_renderSize ||
174 			m_context.getRenderTarget().getHeight() < m_renderSize)
175 			throw tcu::NotSupportedError(std::string("Render target size must be at least ") + de::toString(m_renderSize) + "x" + de::toString(m_renderSize));
176 	}
177 
178 	// Gen shaders
179 	{
180 		ShaderPair vertexShaders = genShaders();
181 
182 		m_shader0 = new glu::ShaderProgram(m_context.getRenderContext(), glu::ProgramSources() << glu::VertexSource(vertexShaders.vertexShaderSource0) << glu::FragmentSource(vertexShaders.fragmentShaderSource0));
183 		if (!m_shader0->isOk())
184 		{
185 			m_testCtx.getLog() << *m_shader0;
186 			throw tcu::TestError("Test shader compile failed.");
187 		}
188 
189 		m_shader1 = new glu::ShaderProgram(m_context.getRenderContext(), glu::ProgramSources() << glu::VertexSource(vertexShaders.vertexShaderSource1) << glu::FragmentSource(vertexShaders.fragmentShaderSource1));
190 		if (!m_shader1->isOk())
191 		{
192 			m_testCtx.getLog() << *m_shader1;
193 			throw tcu::TestError("Test shader compile failed.");
194 		}
195 
196 		// log
197 		m_testCtx.getLog()
198 			<< tcu::TestLog::Message << "Shader 1:" << tcu::TestLog::EndMessage
199 			<< *m_shader0
200 			<< tcu::TestLog::Message << "Shader 2:" << tcu::TestLog::EndMessage
201 			<< *m_shader1;
202 	}
203 
204 	// Gen test pattern
205 	{
206 		const int				numTriangles	= 72;
207 		de::Random				rnd				(123);
208 		std::vector<tcu::Vec4>	triangles		(numTriangles * 3 * 2);
209 		const glw::Functions&	gl				= m_context.getRenderContext().getFunctions();
210 
211 		// Narrow triangle pattern
212 		for (int triNdx = 0; triNdx < numTriangles; ++triNdx)
213 		{
214 			const tcu::Vec4 vertex1 = genRandomVector(rnd);
215 			const tcu::Vec4 vertex2 = genRandomVector(rnd);
216 			const tcu::Vec4 vertex3 = vertex2 + genRandomVector(rnd) * 0.01f; // generate narrow triangles
217 
218 			triangles[triNdx*3 + 0] = vertex1;
219 			triangles[triNdx*3 + 1] = vertex2;
220 			triangles[triNdx*3 + 2] = vertex3;
221 		}
222 
223 		// Normal triangle pattern
224 		for (int triNdx = 0; triNdx < numTriangles; ++triNdx)
225 		{
226 			triangles[(numTriangles + triNdx)*3 + 0] = genRandomVector(rnd);
227 			triangles[(numTriangles + triNdx)*3 + 1] = genRandomVector(rnd);
228 			triangles[(numTriangles + triNdx)*3 + 2] = genRandomVector(rnd);
229 		}
230 
231 		// upload
232 		gl.genBuffers(1, &m_arrayBuf);
233 		gl.bindBuffer(GL_ARRAY_BUFFER, m_arrayBuf);
234 		gl.bufferData(GL_ARRAY_BUFFER, (int)(triangles.size() * sizeof(tcu::Vec4)), &triangles[0], GL_STATIC_DRAW);
235 		GLU_EXPECT_NO_ERROR(gl.getError(), "buffer gen");
236 
237 		m_verticesInPattern = numTriangles * 3;
238 	}
239 }
240 
deinit(void)241 void InvarianceTest::deinit (void)
242 {
243 	delete m_shader0;
244 	delete m_shader1;
245 
246 	m_shader0 = DE_NULL;
247 	m_shader1 = DE_NULL;
248 
249 	if (m_arrayBuf)
250 	{
251 		m_context.getRenderContext().getFunctions().deleteBuffers(1, &m_arrayBuf);
252 		m_arrayBuf = 0;
253 	}
254 }
255 
iterate(void)256 InvarianceTest::IterateResult InvarianceTest::iterate (void)
257 {
258 	const glw::Functions&	gl					= m_context.getRenderContext().getFunctions();
259 	const bool				depthBufferExists	= m_context.getRenderTarget().getDepthBits() != 0;
260 	tcu::Surface			resultSurface		(m_renderSize, m_renderSize);
261 	bool					error				= false;
262 
263 	// Prepare draw
264 	gl.clearColor		(0.0f, 0.0f, 0.0f, 1.0f);
265 	gl.clear			(GL_COLOR_BUFFER_BIT);
266 	gl.viewport			(0, 0, m_renderSize, m_renderSize);
267 	gl.bindBuffer		(GL_ARRAY_BUFFER, m_arrayBuf);
268 	GLU_EXPECT_NO_ERROR	(gl.getError(), "setup draw");
269 
270 	m_testCtx.getLog() << tcu::TestLog::Message << "Testing position invariance." << tcu::TestLog::EndMessage;
271 
272 	// Draw position check passes
273 	for (int passNdx = 0; passNdx < 2; ++passNdx)
274 	{
275 		const glu::ShaderProgram&	shader		= (passNdx == 0) ? (*m_shader0) : (*m_shader1);
276 		const glw::GLint			positionLoc = gl.getAttribLocation(shader.getProgram(), "a_input");
277 		const glw::GLint			colorLoc	= gl.getUniformLocation(shader.getProgram(), "u_color");
278 		const tcu::Vec4				red			= tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f);
279 		const tcu::Vec4				green		= tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f);
280 		const tcu::Vec4				color		= (passNdx == 0) ? (red) : (green);
281 		const char* const			colorStr	= (passNdx == 0) ? ("red - purple") : ("green");
282 
283 		m_testCtx.getLog() << tcu::TestLog::Message << "Drawing position test pattern using shader " << (passNdx+1) << ". Primitive color: " << colorStr << "." << tcu::TestLog::EndMessage;
284 
285 		gl.useProgram				(shader.getProgram());
286 		gl.uniform4fv				(colorLoc, 1, color.getPtr());
287 		gl.enableVertexAttribArray	(positionLoc);
288 		gl.vertexAttribPointer		(positionLoc, 4, GL_FLOAT, GL_FALSE, sizeof(tcu::Vec4), DE_NULL);
289 		gl.drawArrays				(GL_TRIANGLES, 0, m_verticesInPattern);
290 		gl.disableVertexAttribArray	(positionLoc);
291 		GLU_EXPECT_NO_ERROR			(gl.getError(), "draw pass");
292 	}
293 
294 	// Read result
295 	glu::readPixels(m_context.getRenderContext(), 0, 0, resultSurface.getAccess());
296 
297 	// Check there are no red pixels
298 	m_testCtx.getLog() << tcu::TestLog::Message << "Verifying output. Expecting only green or background colored pixels." << tcu::TestLog::EndMessage;
299 	error |= !checkImage(resultSurface);
300 
301 	if (!depthBufferExists)
302 	{
303 		m_testCtx.getLog() << tcu::TestLog::Message << "Depth buffer not available, skipping z-test." << tcu::TestLog::EndMessage;
304 	}
305 	else
306 	{
307 		// Test with Z-test
308 		gl.clearDepthf		(1.0f);
309 		gl.clear			(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
310 		gl.enable			(GL_DEPTH_TEST);
311 
312 		m_testCtx.getLog() << tcu::TestLog::Message << "Testing position invariance with z-test. Enabling GL_DEPTH_TEST." << tcu::TestLog::EndMessage;
313 
314 		// Draw position check passes
315 		for (int passNdx = 0; passNdx < 2; ++passNdx)
316 		{
317 			const glu::ShaderProgram&	shader			= (passNdx == 0) ? (*m_shader0) : (*m_shader1);
318 			const glw::GLint			positionLoc		= gl.getAttribLocation(shader.getProgram(), "a_input");
319 			const glw::GLint			colorLoc		= gl.getUniformLocation(shader.getProgram(), "u_color");
320 			const tcu::Vec4				red				= tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f);
321 			const tcu::Vec4				green			= tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f);
322 			const tcu::Vec4				color			= (passNdx == 0) ? (red) : (green);
323 			const glw::GLenum			depthFunc		= (passNdx == 0) ? (GL_ALWAYS) : (GL_EQUAL);
324 			const char* const			depthFuncStr	= (passNdx == 0) ? ("GL_ALWAYS") : ("GL_EQUAL");
325 			const char* const			colorStr		= (passNdx == 0) ? ("red - purple") : ("green");
326 
327 			m_testCtx.getLog() << tcu::TestLog::Message << "Drawing Z-test pattern using shader " << (passNdx+1) << ". Primitive color: " << colorStr << ". DepthFunc: " << depthFuncStr << tcu::TestLog::EndMessage;
328 
329 			gl.useProgram				(shader.getProgram());
330 			gl.uniform4fv				(colorLoc, 1, color.getPtr());
331 			gl.depthFunc				(depthFunc);
332 			gl.enableVertexAttribArray	(positionLoc);
333 			gl.vertexAttribPointer		(positionLoc, 4, GL_FLOAT, GL_FALSE, sizeof(tcu::Vec4), DE_NULL);
334 			gl.drawArrays				(GL_TRIANGLES, m_verticesInPattern, m_verticesInPattern); // !< buffer contains 2 m_verticesInPattern-sized patterns
335 			gl.disableVertexAttribArray	(positionLoc);
336 			GLU_EXPECT_NO_ERROR			(gl.getError(), "draw pass");
337 		}
338 
339 		// Read result
340 		glu::readPixels(m_context.getRenderContext(), 0, 0, resultSurface.getAccess());
341 
342 		// Check there are no red pixels
343 		m_testCtx.getLog() << tcu::TestLog::Message << "Verifying output. Expecting only green or background colored pixels." << tcu::TestLog::EndMessage;
344 		error |= !checkImage(resultSurface);
345 	}
346 
347 	// Report result
348 	if (error)
349 		m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Detected variance between two invariant values");
350 	else
351 		m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
352 
353 	return STOP;
354 }
355 
checkImage(const tcu::Surface & surface) const356 bool InvarianceTest::checkImage (const tcu::Surface& surface) const
357 {
358 	const tcu::IVec4	okColor		= tcu::IVec4(0, 255, 0, 255);
359 	const tcu::RGBA		errColor	= tcu::RGBA(255, 0, 0, 255);
360 	bool				error		= false;
361 	tcu::Surface		errorMask	(m_renderSize, m_renderSize);
362 
363 	tcu::clear(errorMask.getAccess(), okColor);
364 
365 	for (int y = 0; y < m_renderSize; ++y)
366 	for (int x = 0; x < m_renderSize; ++x)
367 	{
368 		const tcu::RGBA col = surface.getPixel(x, y);
369 
370 		if (col.getRed() != 0)
371 		{
372 			errorMask.setPixel(x, y, errColor);
373 			error = true;
374 		}
375 	}
376 
377 	// report error
378 	if (error)
379 	{
380 		m_testCtx.getLog() << tcu::TestLog::Message << "Invalid pixels found (fragments from first render pass found). Variance detected." << tcu::TestLog::EndMessage;
381 		m_testCtx.getLog()
382 			<< tcu::TestLog::ImageSet("Results", "Result verification")
383 			<< tcu::TestLog::Image("Result",		"Result",		surface)
384 			<< tcu::TestLog::Image("Error mask",	"Error mask",	errorMask)
385 			<< tcu::TestLog::EndImageSet;
386 
387 		return false;
388 	}
389 	else
390 	{
391 		m_testCtx.getLog() << tcu::TestLog::Message << "No variance found." << tcu::TestLog::EndMessage;
392 		m_testCtx.getLog()
393 			<< tcu::TestLog::ImageSet("Results", "Result verification")
394 			<< tcu::TestLog::Image("Result", "Result", surface)
395 			<< tcu::TestLog::EndImageSet;
396 
397 		return true;
398 	}
399 }
400 
401 class BasicInvarianceTest : public InvarianceTest
402 {
403 public:
404 								BasicInvarianceTest		(Context& ctx, const char* name, const char* desc, const std::string& vertexShader1, const std::string& vertexShader2);
405 	ShaderPair					genShaders				(void) const;
406 
407 private:
408 	const std::string			m_vertexShader1;
409 	const std::string			m_vertexShader2;
410 	const std::string			m_fragmentShader;
411 	static const char* const	s_basicFragmentShader;
412 };
413 
414 const char* const BasicInvarianceTest::s_basicFragmentShader =	"#version 300 es\n"
415 																"layout(location = 0) out mediump vec4 fragColor;\n"
416 																"uniform mediump vec4 u_color;\n"
417 																"in mediump vec4 v_unrelated;\n"
418 																"void main ()\n"
419 																"{\n"
420 																"	mediump float blue = dot(v_unrelated, vec4(1.0, 1.0, 1.0, 1.0));\n"
421 																"	fragColor = vec4(u_color.r, u_color.g, blue, u_color.a);\n"
422 																"}\n";
423 
BasicInvarianceTest(Context & ctx,const char * name,const char * desc,const std::string & vertexShader1,const std::string & vertexShader2)424 BasicInvarianceTest::BasicInvarianceTest (Context& ctx, const char* name, const char* desc, const std::string& vertexShader1, const std::string& vertexShader2)
425 	: InvarianceTest	(ctx, name, desc)
426 	, m_vertexShader1	(vertexShader1)
427 	, m_vertexShader2	(vertexShader2)
428 	, m_fragmentShader	(s_basicFragmentShader)
429 {
430 }
431 
genShaders(void) const432 BasicInvarianceTest::ShaderPair BasicInvarianceTest::genShaders (void) const
433 {
434 	ShaderPair retVal;
435 
436 	retVal.vertexShaderSource0 = m_vertexShader1;
437 	retVal.vertexShaderSource1 = m_vertexShader2;
438 	retVal.fragmentShaderSource0 = m_fragmentShader;
439 	retVal.fragmentShaderSource1 = m_fragmentShader;
440 
441 	return retVal;
442 }
443 
444 } // anonymous
445 
ShaderInvarianceTests(Context & context)446 ShaderInvarianceTests::ShaderInvarianceTests (Context& context)
447 	: TestCaseGroup(context, "invariance", "Invariance tests")
448 {
449 }
450 
~ShaderInvarianceTests(void)451 ShaderInvarianceTests::~ShaderInvarianceTests (void)
452 {
453 }
454 
init(void)455 void ShaderInvarianceTests::init (void)
456 {
457 	static const struct PrecisionCase
458 	{
459 		glu::Precision	prec;
460 		const char*		name;
461 
462 		// set literals in the glsl to be in the representable range
463 		const char*		highValue;		// !< highValue < maxValue
464 		const char*		invHighValue;
465 		const char*		mediumValue;	// !< mediumValue^2 < maxValue
466 		const char*		lowValue;		// !< lowValue^4 < maxValue
467 		const char*		invlowValue;
468 		int				loopIterations;
469 		int				loopPartialIterations;
470 		int				loopNormalizationExponent;
471 		const char*		loopNormalizationConstantLiteral;
472 		const char*		loopMultiplier;
473 		const char*		sumLoopNormalizationConstantLiteral;
474 	} precisions[] =
475 	{
476 		{ glu::PRECISION_HIGHP,		"highp",	"1.0e20",	"1.0e-20",	"1.0e14",	"1.0e9",	"1.0e-9",	14,	11,	2,	"1.0e4",	"1.9",	"1.0e3"	},
477 		{ glu::PRECISION_MEDIUMP,	"mediump",	"1.0e4",	"1.0e-4",	"1.0e2",	"1.0e1",	"1.0e-1",	13,	11,	2,	"1.0e4",	"1.9",	"1.0e3"	},
478 		{ glu::PRECISION_LOWP,		"lowp",		"0.9",		"1.1",		"1.1",		"1.15",		"0.87",		6,	2,	0,	"2.0",		"1.1",	"1.0"	},
479 	};
480 
481 	for (int precNdx = 0; precNdx < DE_LENGTH_OF_ARRAY(precisions); ++precNdx)
482 	{
483 		const char* const			precisionName	= precisions[precNdx].name;
484 		const glu::Precision		precision		= precisions[precNdx].prec;
485 		tcu::TestCaseGroup* const	group			= new tcu::TestCaseGroup(m_testCtx, precisionName, "Invariance tests using the given precision.");
486 
487 		const FormatArgumentList	args			= FormatArgumentList()
488 														<< FormatArgument("VERSION",				"#version 300 es\n")
489 														<< FormatArgument("IN",						"in")
490 														<< FormatArgument("OUT",					"out")
491 														<< FormatArgument("IN_PREC",				precisionName)
492 														<< FormatArgument("HIGH_VALUE",				de::toString(precisions[precNdx].highValue))
493 														<< FormatArgument("HIGH_VALUE_INV",			de::toString(precisions[precNdx].invHighValue))
494 														<< FormatArgument("MEDIUM_VALUE",			de::toString(precisions[precNdx].mediumValue))
495 														<< FormatArgument("LOW_VALUE",				de::toString(precisions[precNdx].lowValue))
496 														<< FormatArgument("LOW_VALUE_INV",			de::toString(precisions[precNdx].invlowValue))
497 														<< FormatArgument("LOOP_ITERS",				de::toString(precisions[precNdx].loopIterations))
498 														<< FormatArgument("LOOP_ITERS_PARTIAL",		de::toString(precisions[precNdx].loopPartialIterations))
499 														<< FormatArgument("LOOP_NORM_FRACT_EXP",	de::toString(precisions[precNdx].loopNormalizationExponent))
500 														<< FormatArgument("LOOP_NORM_LITERAL",		precisions[precNdx].loopNormalizationConstantLiteral)
501 														<< FormatArgument("LOOP_MULTIPLIER",		precisions[precNdx].loopMultiplier)
502 														<< FormatArgument("SUM_LOOP_NORM_LITERAL",	precisions[precNdx].sumLoopNormalizationConstantLiteral);
503 
504 		addChild(group);
505 
506 		// subexpression cases
507 		{
508 			// First shader shares "${HIGH_VALUE}*a_input.x*a_input.xxxx + ${HIGH_VALUE}*a_input.y*a_input.yyyy" with unrelated output variable. Reordering might result in accuracy loss
509 			// due to the high exponent. In the second shader, the high exponent may be removed during compilation.
510 
511 			group->addChild(new BasicInvarianceTest(m_context, "common_subexpression_0", "Shader shares a subexpression with an unrelated variable.",
512 				formatGLSL(	"${VERSION}"
513 							"${IN} ${IN_PREC} vec4 a_input;\n"
514 							"${OUT} mediump vec4 v_unrelated;\n"
515 							"invariant gl_Position;\n"
516 							"void main ()\n"
517 							"{\n"
518 							"	v_unrelated = a_input.xzxz + (${HIGH_VALUE}*a_input.x*a_input.xxxx + ${HIGH_VALUE}*a_input.y*a_input.yyyy) * (1.08 * a_input.zyzy * a_input.xzxz) * ${HIGH_VALUE_INV} * (a_input.z * a_input.zzxz - a_input.z * a_input.zzxz) + (${HIGH_VALUE}*a_input.x*a_input.xxxx + ${HIGH_VALUE}*a_input.y*a_input.yyyy) / ${HIGH_VALUE};\n"
519 							"	gl_Position = a_input + (${HIGH_VALUE}*a_input.x*a_input.xxxx + ${HIGH_VALUE}*a_input.y*a_input.yyyy) * ${HIGH_VALUE_INV};\n"
520 							"}\n", args),
521 				formatGLSL(	"${VERSION}"
522 							"${IN} ${IN_PREC} vec4 a_input;\n"
523 							"${OUT} mediump vec4 v_unrelated;\n"
524 							"invariant gl_Position;\n"
525 							"void main ()\n"
526 							"{\n"
527 							"	v_unrelated = vec4(0.0, 0.0, 0.0, 0.0);\n"
528 							"	gl_Position = a_input + (${HIGH_VALUE}*a_input.x*a_input.xxxx + ${HIGH_VALUE}*a_input.y*a_input.yyyy) * ${HIGH_VALUE_INV};\n"
529 							"}\n", args)));
530 
531 			// In the first shader, the unrelated variable "d" has mathematically the same expression as "e", but the different
532 			// order of calculation might cause different results.
533 
534 			group->addChild(new BasicInvarianceTest(m_context, "common_subexpression_1", "Shader shares a subexpression with an unrelated variable.",
535 				formatGLSL(	"${VERSION}"
536 							"${IN} ${IN_PREC} vec4 a_input;\n"
537 							"${OUT} mediump vec4 v_unrelated;\n"
538 							"invariant gl_Position;\n"
539 							"void main ()\n"
540 							"{\n"
541 							"	${IN_PREC} vec4 a = ${HIGH_VALUE} * a_input.zzxx + a_input.xzxy - ${HIGH_VALUE} * a_input.zzxx;\n"
542 							"	${IN_PREC} vec4 b = ${HIGH_VALUE} * a_input.zzxx;\n"
543 							"	${IN_PREC} vec4 c = b - ${HIGH_VALUE} * a_input.zzxx + a_input.xzxy;\n"
544 							"	${IN_PREC} vec4 d = (${LOW_VALUE} * a_input.yzxx) * (${LOW_VALUE} * a_input.yzzw) * (1.1*${LOW_VALUE_INV} * a_input.yzxx) * (${LOW_VALUE_INV} * a_input.xzzy);\n"
545 							"	${IN_PREC} vec4 e = ((${LOW_VALUE} * a_input.yzxx) * (1.1*${LOW_VALUE_INV} * a_input.yzxx)) * ((${LOW_VALUE_INV} * a_input.xzzy) * (${LOW_VALUE} * a_input.yzzw));\n"
546 							"	v_unrelated = a + b + c + d + e;\n"
547 							"	gl_Position = a_input + fract(c) + e;\n"
548 							"}\n", args),
549 				formatGLSL(	"${VERSION}"
550 							"${IN} ${IN_PREC} vec4 a_input;\n"
551 							"${OUT} mediump vec4 v_unrelated;\n"
552 							"invariant gl_Position;\n"
553 							"void main ()\n"
554 							"{\n"
555 							"	${IN_PREC} vec4 b = ${HIGH_VALUE} * a_input.zzxx;\n"
556 							"	${IN_PREC} vec4 c = b - ${HIGH_VALUE} * a_input.zzxx + a_input.xzxy;\n"
557 							"	${IN_PREC} vec4 e = ((${LOW_VALUE} * a_input.yzxx) * (1.1*${LOW_VALUE_INV} * a_input.yzxx)) * ((${LOW_VALUE_INV} * a_input.xzzy) * (${LOW_VALUE} * a_input.yzzw));\n"
558 							"	v_unrelated = vec4(0.0, 0.0, 0.0, 0.0);\n"
559 							"	gl_Position = a_input + fract(c) + e;\n"
560 							"}\n", args)));
561 
562 			// Intermediate values used by an unrelated output variable
563 
564 			group->addChild(new BasicInvarianceTest(m_context, "common_subexpression_2", "Shader shares a subexpression with an unrelated variable.",
565 				formatGLSL(	"${VERSION}"
566 							"${IN} ${IN_PREC} vec4 a_input;\n"
567 							"${OUT} mediump vec4 v_unrelated;\n"
568 							"invariant gl_Position;\n"
569 							"void main ()\n"
570 							"{\n"
571 							"	${IN_PREC} vec4 a = ${MEDIUM_VALUE} * (a_input.xxxx + a_input.yyyy);\n"
572 							"	${IN_PREC} vec4 b = (${MEDIUM_VALUE} * (a_input.xxxx + a_input.yyyy)) * (${MEDIUM_VALUE} * (a_input.xxxx + a_input.yyyy)) / ${MEDIUM_VALUE} / ${MEDIUM_VALUE};\n"
573 							"	${IN_PREC} vec4 c = a * a;\n"
574 							"	${IN_PREC} vec4 d = c / ${MEDIUM_VALUE} / ${MEDIUM_VALUE};\n"
575 							"	v_unrelated = a + b + c + d;\n"
576 							"	gl_Position = a_input + d;\n"
577 							"}\n", args),
578 				formatGLSL(	"${VERSION}"
579 							"${IN} ${IN_PREC} vec4 a_input;\n"
580 							"${OUT} mediump vec4 v_unrelated;\n"
581 							"invariant gl_Position;\n"
582 							"void main ()\n"
583 							"{\n"
584 							"	${IN_PREC} vec4 a = ${MEDIUM_VALUE} * (a_input.xxxx + a_input.yyyy);\n"
585 							"	${IN_PREC} vec4 c = a * a;\n"
586 							"	${IN_PREC} vec4 d = c / ${MEDIUM_VALUE} / ${MEDIUM_VALUE};\n"
587 							"	v_unrelated = vec4(0.0, 0.0, 0.0, 0.0);\n"
588 							"	gl_Position = a_input + d;\n"
589 							"}\n", args)));
590 
591 			// Invariant value can be calculated using unrelated value
592 
593 			group->addChild(new BasicInvarianceTest(m_context, "common_subexpression_3", "Shader shares a subexpression with an unrelated variable.",
594 				formatGLSL(	"${VERSION}"
595 							"${IN} ${IN_PREC} vec4 a_input;\n"
596 							"${OUT} mediump vec4 v_unrelated;\n"
597 							"invariant gl_Position;\n"
598 							"void main ()\n"
599 							"{\n"
600 							"	${IN_PREC} float x = a_input.x * 0.2;\n"
601 							"	${IN_PREC} vec4 a = a_input.xxyx * 0.7;\n"
602 							"	${IN_PREC} vec4 b = a_input.yxyz * 0.7;\n"
603 							"	${IN_PREC} vec4 c = a_input.zxyx * 0.5;\n"
604 							"	${IN_PREC} vec4 f = x*a + x*b + x*c;\n"
605 							"	v_unrelated = f;\n"
606 							"	${IN_PREC} vec4 g = x * (a + b + c);\n"
607 							"	gl_Position = a_input + g;\n"
608 							"}\n", args),
609 				formatGLSL(	"${VERSION}"
610 							"${IN} ${IN_PREC} vec4 a_input;\n"
611 							"${OUT} mediump vec4 v_unrelated;\n"
612 							"invariant gl_Position;\n"
613 							"void main ()\n"
614 							"{\n"
615 							"	${IN_PREC} float x = a_input.x * 0.2;\n"
616 							"	${IN_PREC} vec4 a = a_input.xxyx * 0.7;\n"
617 							"	${IN_PREC} vec4 b = a_input.yxyz * 0.7;\n"
618 							"	${IN_PREC} vec4 c = a_input.zxyx * 0.5;\n"
619 							"	v_unrelated = vec4(0.0, 0.0, 0.0, 0.0);\n"
620 							"	${IN_PREC} vec4 g = x * (a + b + c);\n"
621 							"	gl_Position = a_input + g;\n"
622 							"}\n", args)));
623 		}
624 
625 		// shared subexpression of different precision
626 		{
627 			for (int precisionOther = glu::PRECISION_LOWP; precisionOther != glu::PRECISION_LAST; ++precisionOther)
628 			{
629 				const char* const		unrelatedPrec				= glu::getPrecisionName((glu::Precision)precisionOther);
630 				const glu::Precision	minPrecision				= (precisionOther < (int)precision) ? ((glu::Precision)precisionOther) : (precision);
631 				const char* const		multiplierStr				= (minPrecision == glu::PRECISION_LOWP) ? ("0.8, 0.4, -0.2, 0.3") : ("1.0e1, 5.0e2, 2.0e2, 1.0");
632 				const char* const		normalizationStrUsed		= (minPrecision == glu::PRECISION_LOWP) ? ("vec4(fract(used2).xyz, 0.0)") : ("vec4(fract(used2 / 1.0e2).xyz - fract(used2 / 1.0e3).xyz, 0.0)");
633 				const char* const		normalizationStrUnrelated	= (minPrecision == glu::PRECISION_LOWP) ? ("vec4(fract(unrelated2).xyz, 0.0)") : ("vec4(fract(unrelated2 / 1.0e2).xyz - fract(unrelated2 / 1.0e3).xyz, 0.0)");
634 
635 				group->addChild(new BasicInvarianceTest(m_context, ("subexpression_precision_" + std::string(unrelatedPrec)).c_str(), "Shader shares subexpression of different precision with an unrelated variable.",
636 					formatGLSL(	"${VERSION}"
637 								"${IN} ${IN_PREC} vec4 a_input;\n"
638 								"${OUT} ${UNRELATED_PREC} vec4 v_unrelated;\n"
639 								"invariant gl_Position;\n"
640 								"void main ()\n"
641 								"{\n"
642 								"	${UNRELATED_PREC} vec4 unrelated0 = a_input + vec4(0.1, 0.2, 0.3, 0.4);\n"
643 								"	${UNRELATED_PREC} vec4 unrelated1 = vec4(${MULTIPLIER}) * unrelated0.xywz + unrelated0;\n"
644 								"	${UNRELATED_PREC} vec4 unrelated2 = refract(unrelated1, unrelated0, distance(unrelated0, unrelated1));\n"
645 								"	v_unrelated = a_input + 0.02 * ${NORMALIZE_UNRELATED};\n"
646 								"	${IN_PREC} vec4 used0 = a_input + vec4(0.1, 0.2, 0.3, 0.4);\n"
647 								"	${IN_PREC} vec4 used1 = vec4(${MULTIPLIER}) * used0.xywz + used0;\n"
648 								"	${IN_PREC} vec4 used2 = refract(used1, used0, distance(used0, used1));\n"
649 								"	gl_Position = a_input + 0.02 * ${NORMALIZE_USED};\n"
650 								"}\n", FormatArgumentList(args)
651 											<< FormatArgument("UNRELATED_PREC",			unrelatedPrec)
652 											<< FormatArgument("MULTIPLIER",				multiplierStr)
653 											<< FormatArgument("NORMALIZE_USED",			normalizationStrUsed)
654 											<< FormatArgument("NORMALIZE_UNRELATED",	normalizationStrUnrelated)),
655 					formatGLSL(	"${VERSION}"
656 								"${IN} ${IN_PREC} vec4 a_input;\n"
657 								"${OUT} ${UNRELATED_PREC} vec4 v_unrelated;\n"
658 								"invariant gl_Position;\n"
659 								"void main ()\n"
660 								"{\n"
661 								"	v_unrelated = vec4(0.0, 0.0, 0.0, 0.0);\n"
662 								"	${IN_PREC} vec4 used0 = a_input + vec4(0.1, 0.2, 0.3, 0.4);\n"
663 								"	${IN_PREC} vec4 used1 = vec4(${MULTIPLIER}) * used0.xywz + used0;\n"
664 								"	${IN_PREC} vec4 used2 = refract(used1, used0, distance(used0, used1));\n"
665 								"	gl_Position = a_input + 0.02 * ${NORMALIZE_USED};\n"
666 								"}\n", FormatArgumentList(args)
667 											<< FormatArgument("UNRELATED_PREC",			unrelatedPrec)
668 											<< FormatArgument("MULTIPLIER",				multiplierStr)
669 											<< FormatArgument("NORMALIZE_USED",			normalizationStrUsed)
670 											<< FormatArgument("NORMALIZE_UNRELATED",	normalizationStrUnrelated))));
671 			}
672 		}
673 
674 		// loops
675 		{
676 			group->addChild(new BasicInvarianceTest(m_context, "loop_0", "Invariant value set using a loop",
677 				formatGLSL(	"${VERSION}"
678 							"${IN} ${IN_PREC} vec4 a_input;\n"
679 							"${OUT} highp vec4 v_unrelated;\n"
680 							"invariant gl_Position;\n"
681 							"void main ()\n"
682 							"{\n"
683 							"	${IN_PREC} vec4 value = a_input;\n"
684 							"	v_unrelated = vec4(0.0, 0.0, 0.0, 0.0);\n"
685 							"	for (mediump int i = 0; i < ${LOOP_ITERS}; ++i)\n"
686 							"	{\n"
687 							"		value *= ${LOOP_MULTIPLIER};\n"
688 							"		v_unrelated += value;\n"
689 							"	}\n"
690 							"	gl_Position = vec4(value.xyz / ${LOOP_NORM_LITERAL} + a_input.xyz * 0.1, 1.0);\n"
691 							"}\n", args),
692 				formatGLSL(	"${VERSION}"
693 							"${IN} ${IN_PREC} vec4 a_input;\n"
694 							"${OUT} highp vec4 v_unrelated;\n"
695 							"invariant gl_Position;\n"
696 							"void main ()\n"
697 							"{\n"
698 							"	${IN_PREC} vec4 value = a_input;\n"
699 							"	v_unrelated = vec4(0.0, 0.0, 0.0, 0.0);\n"
700 							"	for (mediump int i = 0; i < ${LOOP_ITERS}; ++i)\n"
701 							"	{\n"
702 							"		value *= ${LOOP_MULTIPLIER};\n"
703 							"	}\n"
704 							"	gl_Position = vec4(value.xyz / ${LOOP_NORM_LITERAL} + a_input.xyz * 0.1, 1.0);\n"
705 							"}\n", args)));
706 
707 			group->addChild(new BasicInvarianceTest(m_context, "loop_1", "Invariant value set using a loop",
708 				formatGLSL(	"${VERSION}"
709 							"${IN} ${IN_PREC} vec4 a_input;\n"
710 							"${OUT} mediump vec4 v_unrelated;\n"
711 							"invariant gl_Position;\n"
712 							"void main ()\n"
713 							"{\n"
714 							"	${IN_PREC} vec4 value = a_input;\n"
715 							"	for (mediump int i = 0; i < ${LOOP_ITERS}; ++i)\n"
716 							"	{\n"
717 							"		value *= ${LOOP_MULTIPLIER};\n"
718 							"		if (i == ${LOOP_ITERS_PARTIAL})\n"
719 							"			v_unrelated = value;\n"
720 							"	}\n"
721 							"	gl_Position = vec4(value.xyz / ${LOOP_NORM_LITERAL} + a_input.xyz * 0.1, 1.0);\n"
722 							"}\n", args),
723 				formatGLSL(	"${VERSION}"
724 							"${IN} ${IN_PREC} vec4 a_input;\n"
725 							"${OUT} mediump vec4 v_unrelated;\n"
726 							"invariant gl_Position;\n"
727 							"void main ()\n"
728 							"{\n"
729 							"	${IN_PREC} vec4 value = a_input;\n"
730 							"	v_unrelated = vec4(0.0, 0.0, 0.0, 0.0);\n"
731 							"	for (mediump int i = 0; i < ${LOOP_ITERS}; ++i)\n"
732 							"	{\n"
733 							"		value *= ${LOOP_MULTIPLIER};\n"
734 							"	}\n"
735 							"	gl_Position = vec4(value.xyz / ${LOOP_NORM_LITERAL} + a_input.xyz * 0.1, 1.0);\n"
736 							"}\n", args)));
737 
738 			group->addChild(new BasicInvarianceTest(m_context, "loop_2", "Invariant value set using a loop",
739 				formatGLSL(	"${VERSION}"
740 							"${IN} ${IN_PREC} vec4 a_input;\n"
741 							"${OUT} mediump vec4 v_unrelated;\n"
742 							"invariant gl_Position;\n"
743 							"void main ()\n"
744 							"{\n"
745 							"	${IN_PREC} vec4 value = a_input;\n"
746 							"	v_unrelated = vec4(0.0, 0.0, -1.0, 1.0);\n"
747 							"	for (mediump int i = 0; i < ${LOOP_ITERS}; ++i)\n"
748 							"	{\n"
749 							"		value *= ${LOOP_MULTIPLIER};\n"
750 							"		if (i == ${LOOP_ITERS_PARTIAL})\n"
751 							"			gl_Position = a_input + 0.05 * vec4(fract(value.xyz / 1.0e${LOOP_NORM_FRACT_EXP}), 1.0);\n"
752 							"		else\n"
753 							"			v_unrelated = value + a_input;\n"
754 							"	}\n"
755 							"}\n", args),
756 				formatGLSL(	"${VERSION}"
757 							"${IN} ${IN_PREC} vec4 a_input;\n"
758 							"${OUT} mediump vec4 v_unrelated;\n"
759 							"invariant gl_Position;\n"
760 							"void main ()\n"
761 							"{\n"
762 							"	${IN_PREC} vec4 value = a_input;\n"
763 							"	v_unrelated = vec4(0.0, 0.0, -1.0, 1.0);\n"
764 							"	for (mediump int i = 0; i < ${LOOP_ITERS}; ++i)\n"
765 							"	{\n"
766 							"		value *= ${LOOP_MULTIPLIER};\n"
767 							"		if (i == ${LOOP_ITERS_PARTIAL})\n"
768 							"			gl_Position = a_input + 0.05 * vec4(fract(value.xyz / 1.0e${LOOP_NORM_FRACT_EXP}), 1.0);\n"
769 							"		else\n"
770 							"			v_unrelated = vec4(0.0, 0.0, 0.0, 0.0);\n"
771 							"	}\n"
772 							"}\n", args)));
773 
774 			group->addChild(new BasicInvarianceTest(m_context, "loop_3", "Invariant value set using a loop",
775 				formatGLSL(	"${VERSION}"
776 							"${IN} ${IN_PREC} vec4 a_input;\n"
777 							"${OUT} mediump vec4 v_unrelated;\n"
778 							"invariant gl_Position;\n"
779 							"void main ()\n"
780 							"{\n"
781 							"	${IN_PREC} vec4 value = a_input;\n"
782 							"	gl_Position = vec4(0.0, 0.0, 0.0, 0.0);\n"
783 							"	v_unrelated = vec4(0.0, 0.0, 0.0, 0.0);\n"
784 							"	for (mediump int i = 0; i < ${LOOP_ITERS}; ++i)\n"
785 							"	{\n"
786 							"		value *= ${LOOP_MULTIPLIER};\n"
787 							"		gl_Position += vec4(value.xyz / ${SUM_LOOP_NORM_LITERAL} + a_input.xyz * 0.1, 1.0);\n"
788 							"		v_unrelated = gl_Position.xyzx * a_input;\n"
789 							"	}\n"
790 							"}\n", args),
791 				formatGLSL(	"${VERSION}"
792 							"${IN} ${IN_PREC} vec4 a_input;\n"
793 							"${OUT} mediump vec4 v_unrelated;\n"
794 							"invariant gl_Position;\n"
795 							"void main ()\n"
796 							"{\n"
797 							"	${IN_PREC} vec4 value = a_input;\n"
798 							"	gl_Position = vec4(0.0, 0.0, 0.0, 0.0);\n"
799 							"	v_unrelated = vec4(0.0, 0.0, 0.0, 0.0);\n"
800 							"	for (mediump int i = 0; i < ${LOOP_ITERS}; ++i)\n"
801 							"	{\n"
802 							"		value *= ${LOOP_MULTIPLIER};\n"
803 							"		gl_Position += vec4(value.xyz / ${SUM_LOOP_NORM_LITERAL} + a_input.xyz * 0.1, 1.0);\n"
804 							"	}\n"
805 							"}\n", args)));
806 
807 			group->addChild(new BasicInvarianceTest(m_context, "loop_4", "Invariant value set using a loop",
808 				formatGLSL(	"${VERSION}"
809 							"${IN} ${IN_PREC} vec4 a_input;\n"
810 							"${OUT} mediump vec4 v_unrelated;\n"
811 							"invariant gl_Position;\n"
812 							"void main ()\n"
813 							"{\n"
814 							"	${IN_PREC} vec4 position = vec4(0.0, 0.0, 0.0, 0.0);\n"
815 							"	${IN_PREC} vec4 value1 = a_input;\n"
816 							"	${IN_PREC} vec4 value2 = a_input;\n"
817 							"	v_unrelated = vec4(0.0, 0.0, 0.0, 0.0);\n"
818 							"	for (mediump int i = 0; i < ${LOOP_ITERS}; ++i)\n"
819 							"	{\n"
820 							"		value1 *= ${LOOP_MULTIPLIER};\n"
821 							"		v_unrelated = v_unrelated*1.3 + a_input.xyzx * value1.xyxw;\n"
822 							"	}\n"
823 							"	for (mediump int i = 0; i < ${LOOP_ITERS}; ++i)\n"
824 							"	{\n"
825 							"		value2 *= ${LOOP_MULTIPLIER};\n"
826 							"		position = position*1.3 + a_input.xyzx * value2.xyxw;\n"
827 							"	}\n"
828 							"	gl_Position = a_input + 0.05 * vec4(fract(position.xyz / 1.0e${LOOP_NORM_FRACT_EXP}), 1.0);\n"
829 							"}\n", args),
830 				formatGLSL(	"${VERSION}"
831 							"${IN} ${IN_PREC} vec4 a_input;\n"
832 							"${OUT} mediump vec4 v_unrelated;\n"
833 							"invariant gl_Position;\n"
834 							"void main ()\n"
835 							"{\n"
836 							"	${IN_PREC} vec4 position = vec4(0.0, 0.0, 0.0, 0.0);\n"
837 							"	${IN_PREC} vec4 value2 = a_input;\n"
838 							"	v_unrelated = vec4(0.0, 0.0, 0.0, 0.0);\n"
839 							"	for (mediump int i = 0; i < ${LOOP_ITERS}; ++i)\n"
840 							"	{\n"
841 							"		value2 *= ${LOOP_MULTIPLIER};\n"
842 							"		position = position*1.3 + a_input.xyzx * value2.xyxw;\n"
843 							"	}\n"
844 							"	gl_Position = a_input + 0.05 * vec4(fract(position.xyz / 1.0e${LOOP_NORM_FRACT_EXP}), 1.0);\n"
845 							"}\n", args)));
846 		}
847 	}
848 }
849 
850 } // Functional
851 } // gles3
852 } // deqp
853