• 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 Shader derivate function tests.
22  *
23  * \todo [2013-06-25 pyry] Missing features:
24  *  - lines and points
25  *  - projected coordinates
26  *  - continous non-trivial functions (sin, exp)
27  *  - non-continous functions (step)
28  *//*--------------------------------------------------------------------*/
29 
30 #include "es3fShaderDerivateTests.hpp"
31 #include "gluShaderProgram.hpp"
32 #include "gluRenderContext.hpp"
33 #include "gluDrawUtil.hpp"
34 #include "gluPixelTransfer.hpp"
35 #include "gluShaderUtil.hpp"
36 #include "gluStrUtil.hpp"
37 #include "gluTextureUtil.hpp"
38 #include "gluTexture.hpp"
39 #include "tcuStringTemplate.hpp"
40 #include "tcuRenderTarget.hpp"
41 #include "tcuSurface.hpp"
42 #include "tcuTestLog.hpp"
43 #include "tcuVectorUtil.hpp"
44 #include "tcuTextureUtil.hpp"
45 #include "tcuRGBA.hpp"
46 #include "tcuFloat.hpp"
47 #include "tcuInterval.hpp"
48 #include "deRandom.hpp"
49 #include "deUniquePtr.hpp"
50 #include "deString.h"
51 #include "glwEnums.hpp"
52 #include "glwFunctions.hpp"
53 #include "glsShaderRenderCase.hpp" // gls::setupDefaultUniforms()
54 
55 #include <sstream>
56 
57 namespace deqp
58 {
59 namespace gles3
60 {
61 namespace Functional
62 {
63 
64 using std::vector;
65 using std::string;
66 using std::map;
67 using tcu::TestLog;
68 using std::ostringstream;
69 
70 enum
71 {
72 	VIEWPORT_WIDTH		= 167,
73 	VIEWPORT_HEIGHT		= 103,
74 	FBO_WIDTH			= 99,
75 	FBO_HEIGHT			= 133,
76 	MAX_FAILED_MESSAGES	= 10
77 };
78 
79 enum DerivateFunc
80 {
81 	DERIVATE_DFDX	= 0,
82 	DERIVATE_DFDY,
83 	DERIVATE_FWIDTH,
84 
85 	DERIVATE_LAST
86 };
87 
88 enum SurfaceType
89 {
90 	SURFACETYPE_DEFAULT_FRAMEBUFFER = 0,
91 	SURFACETYPE_UNORM_FBO,
92 	SURFACETYPE_FLOAT_FBO,	// \note Uses RGBA32UI fbo actually, since FP rendertargets are not in core spec.
93 
94 	SURFACETYPE_LAST
95 };
96 
97 // Utilities
98 
99 namespace
100 {
101 
102 class AutoFbo
103 {
104 public:
AutoFbo(const glw::Functions & gl)105 	AutoFbo (const glw::Functions& gl)
106 		: m_gl	(gl)
107 		, m_fbo	(0)
108 	{
109 	}
110 
~AutoFbo(void)111 	~AutoFbo (void)
112 	{
113 		if (m_fbo)
114 			m_gl.deleteFramebuffers(1, &m_fbo);
115 	}
116 
gen(void)117 	void gen (void)
118 	{
119 		DE_ASSERT(!m_fbo);
120 		m_gl.genFramebuffers(1, &m_fbo);
121 	}
122 
operator *(void) const123 	deUint32 operator* (void) const { return m_fbo; }
124 
125 private:
126 	const glw::Functions&	m_gl;
127 	deUint32				m_fbo;
128 };
129 
130 class AutoRbo
131 {
132 public:
AutoRbo(const glw::Functions & gl)133 	AutoRbo (const glw::Functions& gl)
134 		: m_gl	(gl)
135 		, m_rbo	(0)
136 	{
137 	}
138 
~AutoRbo(void)139 	~AutoRbo (void)
140 	{
141 		if (m_rbo)
142 			m_gl.deleteRenderbuffers(1, &m_rbo);
143 	}
144 
gen(void)145 	void gen (void)
146 	{
147 		DE_ASSERT(!m_rbo);
148 		m_gl.genRenderbuffers(1, &m_rbo);
149 	}
150 
operator *(void) const151 	deUint32 operator* (void) const { return m_rbo; }
152 
153 private:
154 	const glw::Functions&	m_gl;
155 	deUint32				m_rbo;
156 };
157 
158 } // anonymous
159 
getDerivateFuncName(DerivateFunc func)160 static const char* getDerivateFuncName (DerivateFunc func)
161 {
162 	switch (func)
163 	{
164 		case DERIVATE_DFDX:		return "dFdx";
165 		case DERIVATE_DFDY:		return "dFdy";
166 		case DERIVATE_FWIDTH:	return "fwidth";
167 		default:
168 			DE_ASSERT(false);
169 			return DE_NULL;
170 	}
171 }
172 
getDerivateFuncCaseName(DerivateFunc func)173 static const char* getDerivateFuncCaseName (DerivateFunc func)
174 {
175 	switch (func)
176 	{
177 		case DERIVATE_DFDX:		return "dfdx";
178 		case DERIVATE_DFDY:		return "dfdy";
179 		case DERIVATE_FWIDTH:	return "fwidth";
180 		default:
181 			DE_ASSERT(false);
182 			return DE_NULL;
183 	}
184 }
185 
getDerivateMask(glu::DataType type)186 static inline tcu::BVec4 getDerivateMask (glu::DataType type)
187 {
188 	switch (type)
189 	{
190 		case glu::TYPE_FLOAT:		return tcu::BVec4(true, false, false, false);
191 		case glu::TYPE_FLOAT_VEC2:	return tcu::BVec4(true, true, false, false);
192 		case glu::TYPE_FLOAT_VEC3:	return tcu::BVec4(true, true, true, false);
193 		case glu::TYPE_FLOAT_VEC4:	return tcu::BVec4(true, true, true, true);
194 		default:
195 			DE_ASSERT(false);
196 			return tcu::BVec4(true);
197 	}
198 }
199 
readDerivate(const tcu::ConstPixelBufferAccess & surface,const tcu::Vec4 & derivScale,const tcu::Vec4 & derivBias,int x,int y)200 static inline tcu::Vec4 readDerivate (const tcu::ConstPixelBufferAccess& surface, const tcu::Vec4& derivScale, const tcu::Vec4& derivBias, int x, int y)
201 {
202 	return (surface.getPixel(x, y) - derivBias) / derivScale;
203 }
204 
getCompExpBits(const tcu::Vec4 & v)205 static inline tcu::UVec4 getCompExpBits (const tcu::Vec4& v)
206 {
207 	return tcu::UVec4(tcu::Float32(v[0]).exponentBits(),
208 					  tcu::Float32(v[1]).exponentBits(),
209 					  tcu::Float32(v[2]).exponentBits(),
210 					  tcu::Float32(v[3]).exponentBits());
211 }
212 
computeFloatingPointError(const float value,const int numAccurateBits)213 float computeFloatingPointError (const float value, const int numAccurateBits)
214 {
215 	const int		numGarbageBits	= 23-numAccurateBits;
216 	const deUint32	mask			= (1u<<numGarbageBits)-1u;
217 	const int		exp				= (tcu::Float32(value).exponent() < -3) ? -3 : tcu::Float32(value).exponent();
218 
219 	return tcu::Float32::construct(+1, exp, (1u<<23) | mask).asFloat() - tcu::Float32::construct(+1, exp, 1u<<23).asFloat();
220 }
221 
getNumMantissaBits(const glu::Precision precision)222 static int getNumMantissaBits (const glu::Precision precision)
223 {
224 	switch (precision)
225 	{
226 		case glu::PRECISION_HIGHP:		return 23;
227 		case glu::PRECISION_MEDIUMP:	return 10;
228 		case glu::PRECISION_LOWP:		return 6;
229 		default:
230 			DE_ASSERT(false);
231 			return 0;
232 	}
233 }
234 
getMinExponent(const glu::Precision precision)235 static int getMinExponent (const glu::Precision precision)
236 {
237 	switch (precision)
238 	{
239 		case glu::PRECISION_HIGHP:		return -126;
240 		case glu::PRECISION_MEDIUMP:	return -14;
241 		case glu::PRECISION_LOWP:		return -8;
242 		default:
243 			DE_ASSERT(false);
244 			return 0;
245 	}
246 }
247 
getSingleULPForExponent(int exp,int numMantissaBits)248 static float getSingleULPForExponent (int exp, int numMantissaBits)
249 {
250 	if (numMantissaBits > 0)
251 	{
252 		DE_ASSERT(numMantissaBits <= 23);
253 
254 		const int ulpBitNdx = 23-numMantissaBits;
255 		return tcu::Float32::construct(+1, exp, (1<<23) | (1 << ulpBitNdx)).asFloat() - tcu::Float32::construct(+1, exp, (1<<23)).asFloat();
256 	}
257 	else
258 	{
259 		DE_ASSERT(numMantissaBits == 0);
260 		return tcu::Float32::construct(+1, exp, (1<<23)).asFloat();
261 	}
262 }
263 
getSingleULPForValue(float value,int numMantissaBits)264 static float getSingleULPForValue (float value, int numMantissaBits)
265 {
266 	const int exp = tcu::Float32(value).exponent();
267 	return getSingleULPForExponent(exp, numMantissaBits);
268 }
269 
convertFloatFlushToZeroRtn(float value,int minExponent,int numAccurateBits)270 static float convertFloatFlushToZeroRtn (float value, int minExponent, int numAccurateBits)
271 {
272 	if (value == 0.0f)
273 	{
274 		return 0.0f;
275 	}
276 	else
277 	{
278 		const tcu::Float32	inputFloat			= tcu::Float32(value);
279 		const int			numTruncatedBits	= 23-numAccurateBits;
280 		const deUint32		truncMask			= (1u<<numTruncatedBits)-1u;
281 
282 		if (value > 0.0f)
283 		{
284 			if (value > 0.0f && tcu::Float32(value).exponent() < minExponent)
285 			{
286 				// flush to zero if possible
287 				return 0.0f;
288 			}
289 			else
290 			{
291 				// just mask away non-representable bits
292 				return tcu::Float32::construct(+1, inputFloat.exponent(), inputFloat.mantissa() & ~truncMask).asFloat();
293 			}
294 		}
295 		else
296 		{
297 			if (inputFloat.mantissa() & truncMask)
298 			{
299 				// decrement one ulp if truncated bits are non-zero (i.e. if value is not representable)
300 				return tcu::Float32::construct(-1, inputFloat.exponent(), inputFloat.mantissa() & ~truncMask).asFloat() - getSingleULPForExponent(inputFloat.exponent(), numAccurateBits);
301 			}
302 			else
303 			{
304 				// value is representable, no need to do anything
305 				return value;
306 			}
307 		}
308 	}
309 }
310 
convertFloatFlushToZeroRtp(float value,int minExponent,int numAccurateBits)311 static float convertFloatFlushToZeroRtp (float value, int minExponent, int numAccurateBits)
312 {
313 	return -convertFloatFlushToZeroRtn(-value, minExponent, numAccurateBits);
314 }
315 
addErrorUlp(float value,float numUlps,int numMantissaBits)316 static float addErrorUlp (float value, float numUlps, int numMantissaBits)
317 {
318 	return value + numUlps * getSingleULPForValue(value, numMantissaBits);
319 }
320 
321 enum
322 {
323 	INTERPOLATION_LOST_BITS = 3, // number mantissa of bits allowed to be lost in varying interpolation
324 };
325 
getInterpolationLostBitsWarning(const glu::Precision precision)326 static int getInterpolationLostBitsWarning (const glu::Precision precision)
327 {
328 	// number mantissa of bits allowed to be lost in varying interpolation
329 	switch (precision)
330 	{
331 		case glu::PRECISION_HIGHP:		return 9;
332 		case glu::PRECISION_MEDIUMP:	return 3;
333 		case glu::PRECISION_LOWP:		return 3;
334 		default:
335 			DE_ASSERT(false);
336 			return 0;
337 	}
338 }
339 
getDerivateThreshold(const glu::Precision precision,const tcu::Vec4 & valueMin,const tcu::Vec4 & valueMax,const tcu::Vec4 & expectedDerivate)340 static inline tcu::Vec4 getDerivateThreshold (const glu::Precision precision, const tcu::Vec4& valueMin, const tcu::Vec4& valueMax, const tcu::Vec4& expectedDerivate)
341 {
342 	const int			baseBits		= getNumMantissaBits(precision);
343 	const tcu::UVec4	derivExp		= getCompExpBits(expectedDerivate);
344 	const tcu::UVec4	maxValueExp		= max(getCompExpBits(valueMin), getCompExpBits(valueMax));
345 	const tcu::UVec4	numBitsLost		= maxValueExp - min(maxValueExp, derivExp);
346 	const tcu::IVec4	numAccurateBits	= max(baseBits - numBitsLost.asInt() - (int)INTERPOLATION_LOST_BITS, tcu::IVec4(0));
347 
348 	return tcu::Vec4(computeFloatingPointError(expectedDerivate[0], numAccurateBits[0]),
349 					 computeFloatingPointError(expectedDerivate[1], numAccurateBits[1]),
350 					 computeFloatingPointError(expectedDerivate[2], numAccurateBits[2]),
351 					 computeFloatingPointError(expectedDerivate[3], numAccurateBits[3]));
352 }
353 
getDerivateThresholdWarning(const glu::Precision precision,const tcu::Vec4 & valueMin,const tcu::Vec4 & valueMax,const tcu::Vec4 & expectedDerivate)354 static inline tcu::Vec4 getDerivateThresholdWarning (const glu::Precision precision, const tcu::Vec4& valueMin, const tcu::Vec4& valueMax, const tcu::Vec4& expectedDerivate)
355 {
356 	const int			baseBits		= getNumMantissaBits(precision);
357 	const tcu::UVec4	derivExp		= getCompExpBits(expectedDerivate);
358 	const tcu::UVec4	maxValueExp		= max(getCompExpBits(valueMin), getCompExpBits(valueMax));
359 	const tcu::UVec4	numBitsLost		= maxValueExp - min(maxValueExp, derivExp);
360 	const tcu::IVec4	numAccurateBits	= max(baseBits - numBitsLost.asInt() - getInterpolationLostBitsWarning(precision), tcu::IVec4(0));
361 
362 	return tcu::Vec4(computeFloatingPointError(expectedDerivate[0], numAccurateBits[0]),
363 					 computeFloatingPointError(expectedDerivate[1], numAccurateBits[1]),
364 					 computeFloatingPointError(expectedDerivate[2], numAccurateBits[2]),
365 					 computeFloatingPointError(expectedDerivate[3], numAccurateBits[3]));
366 }
367 
368 
369 namespace
370 {
371 
372 struct LogVecComps
373 {
374 	const tcu::Vec4&	v;
375 	int					numComps;
376 
LogVecCompsdeqp::gles3::Functional::__anonc9c4db800411::LogVecComps377 	LogVecComps (const tcu::Vec4& v_, int numComps_)
378 		: v			(v_)
379 		, numComps	(numComps_)
380 	{
381 	}
382 };
383 
operator <<(std::ostream & str,const LogVecComps & v)384 std::ostream& operator<< (std::ostream& str, const LogVecComps& v)
385 {
386 	DE_ASSERT(de::inRange(v.numComps, 1, 4));
387 	if (v.numComps == 1)		return str << v.v[0];
388 	else if (v.numComps == 2)	return str << v.v.toWidth<2>();
389 	else if (v.numComps == 3)	return str << v.v.toWidth<3>();
390 	else						return str << v.v;
391 }
392 
393 } // anonymous
394 
395 enum VerificationLogging
396 {
397 	LOG_ALL = 0,
398 	LOG_NOTHING
399 };
400 
verifyConstantDerivate(tcu::TestLog & log,const tcu::ConstPixelBufferAccess & result,const tcu::PixelBufferAccess & errorMask,glu::DataType dataType,const tcu::Vec4 & reference,const tcu::Vec4 & threshold,const tcu::Vec4 & scale,const tcu::Vec4 & bias,VerificationLogging logPolicy=LOG_ALL)401 static qpTestResult verifyConstantDerivate (tcu::TestLog&				log,
402 									const tcu::ConstPixelBufferAccess&	result,
403 									const tcu::PixelBufferAccess&		errorMask,
404 									glu::DataType						dataType,
405 									const tcu::Vec4&					reference,
406 									const tcu::Vec4&					threshold,
407 									const tcu::Vec4&					scale,
408 									const tcu::Vec4&					bias,
409 									VerificationLogging					logPolicy = LOG_ALL)
410 {
411 	const int			numComps		= glu::getDataTypeFloatScalars(dataType);
412 	const tcu::BVec4	mask			= tcu::logicalNot(getDerivateMask(dataType));
413 	int					numFailedPixels	= 0;
414 
415 	if (logPolicy == LOG_ALL)
416 		log << TestLog::Message << "Expecting " << LogVecComps(reference, numComps) << " with threshold " << LogVecComps(threshold, numComps) << TestLog::EndMessage;
417 
418 	for (int y = 0; y < result.getHeight(); y++)
419 	{
420 		for (int x = 0; x < result.getWidth(); x++)
421 		{
422 			const tcu::Vec4		resDerivate		= readDerivate(result, scale, bias, x, y);
423 			const bool			isOk			= tcu::allEqual(tcu::logicalOr(tcu::lessThanEqual(tcu::abs(reference - resDerivate), threshold), mask), tcu::BVec4(true));
424 
425 			if (!isOk)
426 			{
427 				if (numFailedPixels < MAX_FAILED_MESSAGES && logPolicy == LOG_ALL)
428 					log << TestLog::Message << "FAIL: got " << LogVecComps(resDerivate, numComps)
429 											<< ", diff = " << LogVecComps(tcu::abs(reference - resDerivate), numComps)
430 											<< ", at x = " << x << ", y = " << y
431 						<< TestLog::EndMessage;
432 				numFailedPixels += 1;
433 				errorMask.setPixel(tcu::RGBA::red().toVec(), x, y);
434 			}
435 		}
436 	}
437 
438 	if (numFailedPixels >= MAX_FAILED_MESSAGES && logPolicy == LOG_ALL)
439 		log << TestLog::Message << "..." << TestLog::EndMessage;
440 
441 	if (numFailedPixels > 0 && logPolicy == LOG_ALL)
442 		log << TestLog::Message << "FAIL: found " << numFailedPixels << " failed pixels" << TestLog::EndMessage;
443 
444 	return (numFailedPixels == 0) ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL;
445 }
446 
447 struct Linear2DFunctionEvaluator
448 {
449 	tcu::Matrix<float, 4, 3> matrix;
450 
451 	//      .-----.
452 	//      | s_x |
453 	//  M x | s_y |
454 	//      | 1.0 |
455 	//      '-----'
456 	tcu::Vec4 evaluateAt (float screenX, float screenY) const;
457 };
458 
evaluateAt(float screenX,float screenY) const459 tcu::Vec4 Linear2DFunctionEvaluator::evaluateAt (float screenX, float screenY) const
460 {
461 	const tcu::Vec3 position(screenX, screenY, 1.0f);
462 	return matrix * position;
463 }
464 
reverifyConstantDerivateWithFlushRelaxations(tcu::TestLog & log,const tcu::ConstPixelBufferAccess & result,const tcu::PixelBufferAccess & errorMask,glu::DataType dataType,glu::Precision precision,const tcu::Vec4 & derivScale,const tcu::Vec4 & derivBias,const tcu::Vec4 & surfaceThreshold,DerivateFunc derivateFunc,const Linear2DFunctionEvaluator & function)465 static qpTestResult reverifyConstantDerivateWithFlushRelaxations (tcu::TestLog&							log,
466 														  const tcu::ConstPixelBufferAccess&	result,
467 														  const tcu::PixelBufferAccess&			errorMask,
468 														  glu::DataType							dataType,
469 														  glu::Precision						precision,
470 														  const tcu::Vec4&						derivScale,
471 														  const tcu::Vec4&						derivBias,
472 														  const tcu::Vec4&						surfaceThreshold,
473 														  DerivateFunc							derivateFunc,
474 														  const Linear2DFunctionEvaluator&		function)
475 {
476 	DE_ASSERT(result.getWidth() == errorMask.getWidth());
477 	DE_ASSERT(result.getHeight() == errorMask.getHeight());
478 	DE_ASSERT(derivateFunc == DERIVATE_DFDX || derivateFunc == DERIVATE_DFDY);
479 
480 	const tcu::IVec4	red						(255, 0, 0, 255);
481 	const tcu::IVec4	green					(0, 255, 0, 255);
482 	const float			divisionErrorUlps		= 2.5f;
483 
484 	const int			numComponents			= glu::getDataTypeFloatScalars(dataType);
485 	const int			numBits					= getNumMantissaBits(precision);
486 	const int			minExponent				= getMinExponent(precision);
487 
488 	const int			numVaryingSampleBits	= numBits - INTERPOLATION_LOST_BITS;
489 	int					numFailedPixels			= 0;
490 
491 	tcu::clear(errorMask, green);
492 
493 	// search for failed pixels
494 	for (int y = 0; y < result.getHeight(); ++y)
495 	for (int x = 0; x < result.getWidth(); ++x)
496 	{
497 		//                 flushToZero?(f2z?(functionValueCurrent) - f2z?(functionValueBefore))
498 		// flushToZero? ( ------------------------------------------------------------------------ +- 2.5 ULP )
499 		//                                                  dx
500 
501 		const tcu::Vec4	resultDerivative		= readDerivate(result, derivScale, derivBias, x, y);
502 
503 		// sample at the front of the back pixel and the back of the front pixel to cover the whole area of
504 		// legal sample positions. In general case this is NOT OK, but we know that the target function is
505 		// (mostly*) linear which allows us to take the sample points at arbitrary points. This gets us the
506 		// maximum difference possible in exponents which are used in error bound calculations.
507 		// * non-linearity may happen around zero or with very high function values due to subnorms not
508 		//   behaving well.
509 		const tcu::Vec4	functionValueForward	= (derivateFunc == DERIVATE_DFDX)
510 													? (function.evaluateAt((float)x + 2.0f, (float)y + 0.5f))
511 													: (function.evaluateAt((float)x + 0.5f, (float)y + 2.0f));
512 		const tcu::Vec4	functionValueBackward	= (derivateFunc == DERIVATE_DFDX)
513 													? (function.evaluateAt((float)x - 1.0f, (float)y + 0.5f))
514 													: (function.evaluateAt((float)x + 0.5f, (float)y - 1.0f));
515 
516 		bool	anyComponentFailed				= false;
517 
518 		// check components separately
519 		for (int c = 0; c < numComponents; ++c)
520 		{
521 			// Simulate interpolation. Add allowed interpolation error and round to target precision. Allow one half ULP (i.e. correct rounding)
522 			const tcu::Interval	forwardComponent		(convertFloatFlushToZeroRtn(addErrorUlp((float)functionValueForward[c],  -0.5f, numVaryingSampleBits), minExponent, numBits),
523 														 convertFloatFlushToZeroRtp(addErrorUlp((float)functionValueForward[c],  +0.5f, numVaryingSampleBits), minExponent, numBits));
524 			const tcu::Interval	backwardComponent		(convertFloatFlushToZeroRtn(addErrorUlp((float)functionValueBackward[c], -0.5f, numVaryingSampleBits), minExponent, numBits),
525 														 convertFloatFlushToZeroRtp(addErrorUlp((float)functionValueBackward[c], +0.5f, numVaryingSampleBits), minExponent, numBits));
526 			const int			maxValueExp				= de::max(de::max(tcu::Float32(forwardComponent.lo()).exponent(),   tcu::Float32(forwardComponent.hi()).exponent()),
527 																  de::max(tcu::Float32(backwardComponent.lo()).exponent(),  tcu::Float32(backwardComponent.hi()).exponent()));
528 
529 			// subtraction in numerator will likely cause a cancellation of the most
530 			// significant bits. Apply error bounds.
531 
532 			const tcu::Interval	numerator				(forwardComponent - backwardComponent);
533 			const int			numeratorLoExp			= tcu::Float32(numerator.lo()).exponent();
534 			const int			numeratorHiExp			= tcu::Float32(numerator.hi()).exponent();
535 			const int			numeratorLoBitsLost		= de::max(0, maxValueExp - numeratorLoExp); //!< must clamp to zero since if forward and backward components have different
536 			const int			numeratorHiBitsLost		= de::max(0, maxValueExp - numeratorHiExp); //!< sign, numerator might have larger exponent than its operands.
537 			const int			numeratorLoBits			= de::max(0, numBits - numeratorLoBitsLost);
538 			const int			numeratorHiBits			= de::max(0, numBits - numeratorHiBitsLost);
539 
540 			const tcu::Interval	numeratorRange			(convertFloatFlushToZeroRtn((float)numerator.lo(), minExponent, numeratorLoBits),
541 														 convertFloatFlushToZeroRtp((float)numerator.hi(), minExponent, numeratorHiBits));
542 
543 			const tcu::Interval	divisionRange			= numeratorRange / 3.0f; // legal sample area is anywhere within this and neighboring pixels (i.e. size = 3)
544 			const tcu::Interval	divisionResultRange		(convertFloatFlushToZeroRtn(addErrorUlp((float)divisionRange.lo(), -divisionErrorUlps, numBits), minExponent, numBits),
545 														 convertFloatFlushToZeroRtp(addErrorUlp((float)divisionRange.hi(), +divisionErrorUlps, numBits), minExponent, numBits));
546 			const tcu::Interval	finalResultRange		(divisionResultRange.lo() - surfaceThreshold[c], divisionResultRange.hi() + surfaceThreshold[c]);
547 
548 			if (resultDerivative[c] >= finalResultRange.lo() && resultDerivative[c] <= finalResultRange.hi())
549 			{
550 				// value ok
551 			}
552 			else
553 			{
554 				if (numFailedPixels < MAX_FAILED_MESSAGES)
555 					log << tcu::TestLog::Message
556 						<< "Error in pixel at " << x << ", " << y << " with component " << c << " (channel " << ("rgba"[c]) << ")\n"
557 						<< "\tGot pixel value " << result.getPixelInt(x, y) << "\n"
558 						<< "\t\tdFd" << ((derivateFunc == DERIVATE_DFDX) ? ('x') : ('y')) << " ~= " << resultDerivative[c] << "\n"
559 						<< "\t\tdifference to a valid range: "
560 							<< ((resultDerivative[c] < finalResultRange.lo()) ? ("-") : ("+"))
561 							<< ((resultDerivative[c] < finalResultRange.lo()) ? (finalResultRange.lo() - resultDerivative[c]) : (resultDerivative[c] - finalResultRange.hi()))
562 							<< "\n"
563 						<< "\tDerivative value range:\n"
564 						<< "\t\tMin: " << finalResultRange.lo() << "\n"
565 						<< "\t\tMax: " << finalResultRange.hi() << "\n"
566 						<< tcu::TestLog::EndMessage;
567 
568 				++numFailedPixels;
569 				anyComponentFailed = true;
570 			}
571 		}
572 
573 		if (anyComponentFailed)
574 			errorMask.setPixel(red, x, y);
575 	}
576 
577 	if (numFailedPixels >= MAX_FAILED_MESSAGES)
578 		log << TestLog::Message << "..." << TestLog::EndMessage;
579 
580 	if (numFailedPixels > 0)
581 		log << TestLog::Message << "FAIL: found " << numFailedPixels << " failed pixels" << TestLog::EndMessage;
582 
583 	return (numFailedPixels == 0) ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL;
584 }
585 
586 // TriangleDerivateCase
587 
588 class TriangleDerivateCase : public TestCase
589 {
590 public:
591 							TriangleDerivateCase	(Context& context, const char* name, const char* description);
592 							~TriangleDerivateCase	(void);
593 
594 	IterateResult			iterate					(void);
595 
596 protected:
setupRenderState(deUint32 program)597 	virtual void			setupRenderState		(deUint32 program) { DE_UNREF(program); }
598 	virtual qpTestResult	verify					(const tcu::ConstPixelBufferAccess& result, const tcu::PixelBufferAccess& errorMask) = DE_NULL;
599 
600 	tcu::IVec2				getViewportSize			(void) const;
601 	tcu::Vec4				getSurfaceThreshold		(void) const;
602 
603 	glu::DataType			m_dataType;
604 	glu::Precision			m_precision;
605 
606 	glu::DataType			m_coordDataType;
607 	glu::Precision			m_coordPrecision;
608 
609 	std::string				m_fragmentSrc;
610 
611 	tcu::Vec4				m_coordMin;
612 	tcu::Vec4				m_coordMax;
613 	tcu::Vec4				m_derivScale;
614 	tcu::Vec4				m_derivBias;
615 
616 	SurfaceType				m_surfaceType;
617 	int						m_numSamples;
618 	deUint32				m_hint;
619 
620 	bool					m_useAsymmetricCoords;
621 };
622 
TriangleDerivateCase(Context & context,const char * name,const char * description)623 TriangleDerivateCase::TriangleDerivateCase (Context& context, const char* name, const char* description)
624 	: TestCase				(context, name, description)
625 	, m_dataType			(glu::TYPE_LAST)
626 	, m_precision			(glu::PRECISION_LAST)
627 	, m_coordDataType		(glu::TYPE_LAST)
628 	, m_coordPrecision		(glu::PRECISION_LAST)
629 	, m_surfaceType			(SURFACETYPE_DEFAULT_FRAMEBUFFER)
630 	, m_numSamples			(0)
631 	, m_hint				(GL_DONT_CARE)
632 	, m_useAsymmetricCoords	(false)
633 {
634 	DE_ASSERT(m_surfaceType != SURFACETYPE_DEFAULT_FRAMEBUFFER || m_numSamples == 0);
635 }
636 
~TriangleDerivateCase(void)637 TriangleDerivateCase::~TriangleDerivateCase (void)
638 {
639 	TriangleDerivateCase::deinit();
640 }
641 
genVertexSource(glu::DataType coordType,glu::Precision precision)642 static std::string genVertexSource (glu::DataType coordType, glu::Precision precision)
643 {
644 	DE_ASSERT(glu::isDataTypeFloatOrVec(coordType));
645 
646 	const char* vertexTmpl =
647 		"#version 300 es\n"
648 		"in highp vec4 a_position;\n"
649 		"in ${PRECISION} ${DATATYPE} a_coord;\n"
650 		"out ${PRECISION} ${DATATYPE} v_coord;\n"
651 		"void main (void)\n"
652 		"{\n"
653 		"	gl_Position = a_position;\n"
654 		"	v_coord = a_coord;\n"
655 		"}\n";
656 
657 	map<string, string> vertexParams;
658 
659 	vertexParams["PRECISION"]	= glu::getPrecisionName(precision);
660 	vertexParams["DATATYPE"]	= glu::getDataTypeName(coordType);
661 
662 	return tcu::StringTemplate(vertexTmpl).specialize(vertexParams);
663 }
664 
getViewportSize(void) const665 inline tcu::IVec2 TriangleDerivateCase::getViewportSize (void) const
666 {
667 	if (m_surfaceType == SURFACETYPE_DEFAULT_FRAMEBUFFER)
668 	{
669 		const int	width	= de::min<int>(m_context.getRenderTarget().getWidth(),	VIEWPORT_WIDTH);
670 		const int	height	= de::min<int>(m_context.getRenderTarget().getHeight(),	VIEWPORT_HEIGHT);
671 		return tcu::IVec2(width, height);
672 	}
673 	else
674 		return tcu::IVec2(FBO_WIDTH, FBO_HEIGHT);
675 }
676 
iterate(void)677 TriangleDerivateCase::IterateResult TriangleDerivateCase::iterate (void)
678 {
679 	const glw::Functions&		gl				= m_context.getRenderContext().getFunctions();
680 	const glu::ShaderProgram	program			(m_context.getRenderContext(), glu::makeVtxFragSources(genVertexSource(m_coordDataType, m_coordPrecision), m_fragmentSrc));
681 	de::Random					rnd				(deStringHash(getName()) ^ 0xbbc24);
682 	const bool					useFbo			= m_surfaceType != SURFACETYPE_DEFAULT_FRAMEBUFFER;
683 	const deUint32				fboFormat		= m_surfaceType == SURFACETYPE_FLOAT_FBO ? GL_RGBA32UI : GL_RGBA8;
684 	const tcu::IVec2			viewportSize	= getViewportSize();
685 	const int					viewportX		= useFbo ? 0 : rnd.getInt(0, m_context.getRenderTarget().getWidth()		- viewportSize.x());
686 	const int					viewportY		= useFbo ? 0 : rnd.getInt(0, m_context.getRenderTarget().getHeight()	- viewportSize.y());
687 	AutoFbo						fbo				(gl);
688 	AutoRbo						rbo				(gl);
689 	tcu::TextureLevel			result;
690 
691 	m_testCtx.getLog() << program;
692 
693 	if (!program.isOk())
694 		TCU_FAIL("Compile failed");
695 
696 	if (useFbo)
697 	{
698 		m_testCtx.getLog() << TestLog::Message
699 						   << "Rendering to FBO, format = " << glu::getTextureFormatStr(fboFormat)
700 						   << ", samples = " << m_numSamples
701 						   << TestLog::EndMessage;
702 
703 		fbo.gen();
704 		rbo.gen();
705 
706 		gl.bindRenderbuffer(GL_RENDERBUFFER, *rbo);
707 		gl.renderbufferStorageMultisample(GL_RENDERBUFFER, m_numSamples, fboFormat, viewportSize.x(), viewportSize.y());
708 		gl.bindFramebuffer(GL_FRAMEBUFFER, *fbo);
709 		gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, *rbo);
710 		TCU_CHECK(gl.checkFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
711 	}
712 	else
713 	{
714 		const tcu::PixelFormat pixelFormat = m_context.getRenderTarget().getPixelFormat();
715 
716 		m_testCtx.getLog()
717 			<< TestLog::Message
718 			<< "Rendering to default framebuffer\n"
719 			<< "\tColor depth: R=" << pixelFormat.redBits << ", G=" << pixelFormat.greenBits << ", B=" << pixelFormat.blueBits << ", A=" << pixelFormat.alphaBits
720 			<< TestLog::EndMessage;
721 	}
722 
723 	m_testCtx.getLog() << TestLog::Message << "in: " << m_coordMin << " -> " << m_coordMax << "\n"
724 										   << (m_useAsymmetricCoords ? "v_coord.x = in.x * (x+y)/2\n" : "v_coord.x = in.x * x\n")
725 										   << (m_useAsymmetricCoords ? "v_coord.y = in.y * (x+y)/2\n" : "v_coord.y = in.y * y\n")
726 										   << "v_coord.z = in.z * (x+y)/2\n"
727 										   << "v_coord.w = in.w * (1 - (x+y)/2)\n"
728 					   << TestLog::EndMessage
729 					   << TestLog::Message << "u_scale: " << m_derivScale << ", u_bias: " << m_derivBias << " (displayed values have scale/bias removed)" << TestLog::EndMessage
730 					   << TestLog::Message << "Viewport: " << viewportSize.x() << "x" << viewportSize.y() << TestLog::EndMessage
731 					   << TestLog::Message << "GL_FRAGMENT_SHADER_DERIVATE_HINT: " << glu::getHintModeStr(m_hint) << TestLog::EndMessage;
732 
733 	// Draw
734 	{
735 		const float positions[] =
736 		{
737 			-1.0f, -1.0f, 0.0f, 1.0f,
738 			-1.0f,  1.0f, 0.0f, 1.0f,
739 			 1.0f, -1.0f, 0.0f, 1.0f,
740 			 1.0f,  1.0f, 0.0f, 1.0f
741 		};
742 		float coords[] =
743 		{
744 			m_coordMin.x(), m_coordMin.y(), m_coordMin.z(),							m_coordMax.w(),
745 			m_coordMin.x(), m_coordMax.y(), (m_coordMin.z()+m_coordMax.z())*0.5f,	(m_coordMin.w()+m_coordMax.w())*0.5f,
746 			m_coordMax.x(), m_coordMin.y(), (m_coordMin.z()+m_coordMax.z())*0.5f,	(m_coordMin.w()+m_coordMax.w())*0.5f,
747 			m_coordMax.x(), m_coordMax.y(), m_coordMax.z(),							m_coordMin.w()
748 		};
749 
750 		// For linear tests we want varying data x and y to vary along both axes
751 		// to get nonzero x for dfdy and nonzero y for dfdx. To make the gradient
752 		// the same for both triangles we set vertices 2 and 3 to middle values.
753 		// This way the values go from min -> (max+min) / 2 or (max+min) / 2 -> max
754 		// depending on the triangle, but the derivative is the same for both.
755 		if (m_useAsymmetricCoords)
756 		{
757 			coords[4] = coords[8] = (m_coordMin.x() + m_coordMax.x())*0.5f;
758 			coords[5] = coords[9] = (m_coordMin.y() + m_coordMax.y())*0.5f;
759 		}
760 
761 		const glu::VertexArrayBinding vertexArrays[] =
762 		{
763 			glu::va::Float("a_position",	4, 4, 0, &positions[0]),
764 			glu::va::Float("a_coord",		4, 4, 0, &coords[0])
765 		};
766 		const deUint16 indices[] = { 0, 2, 1, 2, 3, 1 };
767 
768 		gl.clearColor(0.125f, 0.25f, 0.5f, 1.0f);
769 		gl.clear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
770 		gl.disable(GL_DITHER);
771 
772 		gl.useProgram(program.getProgram());
773 
774 		{
775 			const int	scaleLoc	= gl.getUniformLocation(program.getProgram(), "u_scale");
776 			const int	biasLoc		= gl.getUniformLocation(program.getProgram(), "u_bias");
777 
778 			switch (m_dataType)
779 			{
780 				case glu::TYPE_FLOAT:
781 					gl.uniform1f(scaleLoc, m_derivScale.x());
782 					gl.uniform1f(biasLoc, m_derivBias.x());
783 					break;
784 
785 				case glu::TYPE_FLOAT_VEC2:
786 					gl.uniform2fv(scaleLoc, 1, m_derivScale.getPtr());
787 					gl.uniform2fv(biasLoc, 1, m_derivBias.getPtr());
788 					break;
789 
790 				case glu::TYPE_FLOAT_VEC3:
791 					gl.uniform3fv(scaleLoc, 1, m_derivScale.getPtr());
792 					gl.uniform3fv(biasLoc, 1, m_derivBias.getPtr());
793 					break;
794 
795 				case glu::TYPE_FLOAT_VEC4:
796 					gl.uniform4fv(scaleLoc, 1, m_derivScale.getPtr());
797 					gl.uniform4fv(biasLoc, 1, m_derivBias.getPtr());
798 					break;
799 
800 				default:
801 					DE_ASSERT(false);
802 			}
803 		}
804 
805 		gls::setupDefaultUniforms(m_context.getRenderContext(), program.getProgram());
806 		setupRenderState(program.getProgram());
807 
808 		gl.hint(GL_FRAGMENT_SHADER_DERIVATIVE_HINT, m_hint);
809 		GLU_EXPECT_NO_ERROR(gl.getError(), "Setup program state");
810 
811 		gl.viewport(viewportX, viewportY, viewportSize.x(), viewportSize.y());
812 		glu::draw(m_context.getRenderContext(), program.getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays), &vertexArrays[0],
813 				  glu::pr::Triangles(DE_LENGTH_OF_ARRAY(indices), &indices[0]));
814 		GLU_EXPECT_NO_ERROR(gl.getError(), "Draw");
815 	}
816 
817 	// Read back results
818 	{
819 		const bool		isMSAA		= useFbo && m_numSamples > 0;
820 		AutoFbo			resFbo		(gl);
821 		AutoRbo			resRbo		(gl);
822 
823 		// Resolve if necessary
824 		if (isMSAA)
825 		{
826 			resFbo.gen();
827 			resRbo.gen();
828 
829 			gl.bindRenderbuffer(GL_RENDERBUFFER, *resRbo);
830 			gl.renderbufferStorageMultisample(GL_RENDERBUFFER, 0, fboFormat, viewportSize.x(), viewportSize.y());
831 			gl.bindFramebuffer(GL_DRAW_FRAMEBUFFER, *resFbo);
832 			gl.framebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, *resRbo);
833 			TCU_CHECK(gl.checkFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
834 
835 			gl.blitFramebuffer(0, 0, viewportSize.x(), viewportSize.y(), 0, 0, viewportSize.x(), viewportSize.y(), GL_COLOR_BUFFER_BIT, GL_NEAREST);
836 			GLU_EXPECT_NO_ERROR(gl.getError(), "Resolve blit");
837 
838 			gl.bindFramebuffer(GL_READ_FRAMEBUFFER, *resFbo);
839 		}
840 
841 		switch (m_surfaceType)
842 		{
843 			case SURFACETYPE_DEFAULT_FRAMEBUFFER:
844 			case SURFACETYPE_UNORM_FBO:
845 				result.setStorage(tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), viewportSize.x(), viewportSize.y());
846 				glu::readPixels(m_context.getRenderContext(), viewportX, viewportY, result);
847 				break;
848 
849 			case SURFACETYPE_FLOAT_FBO:
850 			{
851 				const tcu::TextureFormat	dataFormat		(tcu::TextureFormat::RGBA, tcu::TextureFormat::FLOAT);
852 				const tcu::TextureFormat	transferFormat	(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNSIGNED_INT32);
853 
854 				result.setStorage(dataFormat, viewportSize.x(), viewportSize.y());
855 				glu::readPixels(m_context.getRenderContext(), viewportX, viewportY,
856 								tcu::PixelBufferAccess(transferFormat, result.getWidth(), result.getHeight(), result.getDepth(), result.getAccess().getDataPtr()));
857 				break;
858 			}
859 
860 			default:
861 				DE_ASSERT(false);
862 		}
863 
864 		GLU_EXPECT_NO_ERROR(gl.getError(), "Read pixels");
865 	}
866 
867 	// Verify
868 	{
869 		tcu::Surface errorMask(result.getWidth(), result.getHeight());
870 		tcu::clear(errorMask.getAccess(), tcu::RGBA::green().toVec());
871 
872 		const qpTestResult testResult = verify(result.getAccess(), errorMask.getAccess());
873 		const char* failStr = "Fail";
874 
875 		m_testCtx.getLog() << TestLog::ImageSet("Result", "Result images")
876 						   << TestLog::Image("Rendered", "Rendered image", result);
877 
878 		if (testResult != QP_TEST_RESULT_PASS)
879 			m_testCtx.getLog() << TestLog::Image("ErrorMask", "Error mask", errorMask);
880 
881 		m_testCtx.getLog() << TestLog::EndImageSet;
882 
883 		if (testResult == QP_TEST_RESULT_PASS)
884 			failStr = "Pass";
885 		else if (testResult == QP_TEST_RESULT_QUALITY_WARNING)
886 			failStr = "QualityWarning";
887 
888 		m_testCtx.setTestResult(testResult, failStr);
889 
890 	}
891 
892 	return STOP;
893 }
894 
getSurfaceThreshold(void) const895 tcu::Vec4 TriangleDerivateCase::getSurfaceThreshold (void) const
896 {
897 	switch (m_surfaceType)
898 	{
899 		case SURFACETYPE_DEFAULT_FRAMEBUFFER:
900 		{
901 			const tcu::PixelFormat	pixelFormat		= m_context.getRenderTarget().getPixelFormat();
902 			const tcu::IVec4		channelBits		(pixelFormat.redBits, pixelFormat.greenBits, pixelFormat.blueBits, pixelFormat.alphaBits);
903 			const tcu::IVec4		intThreshold	= tcu::IVec4(1) << (8 - channelBits);
904 			const tcu::Vec4			normThreshold	= intThreshold.asFloat() / 255.0f;
905 
906 			return normThreshold;
907 		}
908 
909 		case SURFACETYPE_UNORM_FBO:				return tcu::IVec4(1).asFloat() / 255.0f;
910 		case SURFACETYPE_FLOAT_FBO:				return tcu::Vec4(0.0f);
911 		default:
912 			DE_ASSERT(false);
913 			return tcu::Vec4(0.0f);
914 	}
915 }
916 
917 // ConstantDerivateCase
918 
919 class ConstantDerivateCase : public TriangleDerivateCase
920 {
921 public:
922 						ConstantDerivateCase		(Context& context, const char* name, const char* description, DerivateFunc func, glu::DataType type);
~ConstantDerivateCase(void)923 						~ConstantDerivateCase		(void) {}
924 
925 	void				init						(void);
926 
927 protected:
928 	qpTestResult		verify						(const tcu::ConstPixelBufferAccess& result, const tcu::PixelBufferAccess& errorMask);
929 
930 private:
931 	DerivateFunc		m_func;
932 };
933 
ConstantDerivateCase(Context & context,const char * name,const char * description,DerivateFunc func,glu::DataType type)934 ConstantDerivateCase::ConstantDerivateCase (Context& context, const char* name, const char* description, DerivateFunc func, glu::DataType type)
935 	: TriangleDerivateCase	(context, name, description)
936 	, m_func				(func)
937 {
938 	m_dataType			= type;
939 	m_precision			= glu::PRECISION_HIGHP;
940 	m_coordDataType		= m_dataType;
941 	m_coordPrecision	= m_precision;
942 }
943 
init(void)944 void ConstantDerivateCase::init (void)
945 {
946 	const char* fragmentTmpl =
947 		"#version 300 es\n"
948 		"layout(location = 0) out mediump vec4 o_color;\n"
949 		"uniform ${PRECISION} ${DATATYPE} u_scale;\n"
950 		"uniform ${PRECISION} ${DATATYPE} u_bias;\n"
951 		"void main (void)\n"
952 		"{\n"
953 		"	${PRECISION} ${DATATYPE} res = ${FUNC}(${VALUE}) * u_scale + u_bias;\n"
954 		"	o_color = ${CAST_TO_OUTPUT};\n"
955 		"}\n";
956 	map<string, string> fragmentParams;
957 	fragmentParams["PRECISION"]			= glu::getPrecisionName(m_precision);
958 	fragmentParams["DATATYPE"]			= glu::getDataTypeName(m_dataType);
959 	fragmentParams["FUNC"]				= getDerivateFuncName(m_func);
960 	fragmentParams["VALUE"]				= m_dataType == glu::TYPE_FLOAT_VEC4 ? "vec4(1.0, 7.2, -1e5, 0.0)" :
961 										  m_dataType == glu::TYPE_FLOAT_VEC3 ? "vec3(1e2, 8.0, 0.01)" :
962 										  m_dataType == glu::TYPE_FLOAT_VEC2 ? "vec2(-0.0, 2.7)" :
963 										  /* TYPE_FLOAT */					   "7.7";
964 	fragmentParams["CAST_TO_OUTPUT"]	= m_dataType == glu::TYPE_FLOAT_VEC4 ? "res" :
965 										  m_dataType == glu::TYPE_FLOAT_VEC3 ? "vec4(res, 1.0)" :
966 										  m_dataType == glu::TYPE_FLOAT_VEC2 ? "vec4(res, 0.0, 1.0)" :
967 										  /* TYPE_FLOAT */					   "vec4(res, 0.0, 0.0, 1.0)";
968 
969 	m_fragmentSrc = tcu::StringTemplate(fragmentTmpl).specialize(fragmentParams);
970 
971 	m_derivScale	= tcu::Vec4(1e3f, 1e3f, 1e3f, 1e3f);
972 	m_derivBias		= tcu::Vec4(0.5f, 0.5f, 0.5f, 0.5f);
973 }
974 
verify(const tcu::ConstPixelBufferAccess & result,const tcu::PixelBufferAccess & errorMask)975 qpTestResult ConstantDerivateCase::verify (const tcu::ConstPixelBufferAccess& result, const tcu::PixelBufferAccess& errorMask)
976 {
977 	const tcu::Vec4 reference	(0.0f); // Derivate of constant argument should always be 0
978 	const tcu::Vec4	threshold	= getSurfaceThreshold() / abs(m_derivScale);
979 
980 	return verifyConstantDerivate(m_testCtx.getLog(), result, errorMask, m_dataType,
981 								  reference, threshold, m_derivScale, m_derivBias);
982 }
983 
984 // LinearDerivateCase
985 
986 class LinearDerivateCase : public TriangleDerivateCase
987 {
988 public:
989 						LinearDerivateCase		(Context& context, const char* name, const char* description, DerivateFunc func, glu::DataType type, glu::Precision precision, deUint32 hint, SurfaceType surfaceType, int numSamples, const char* fragmentSrcTmpl);
~LinearDerivateCase(void)990 						~LinearDerivateCase		(void) {}
991 
992 	void				init					(void);
993 
994 protected:
995 	qpTestResult		verify					(const tcu::ConstPixelBufferAccess& result, const tcu::PixelBufferAccess& errorMask);
996 
997 private:
998 	DerivateFunc		m_func;
999 	std::string			m_fragmentTmpl;
1000 };
1001 
LinearDerivateCase(Context & context,const char * name,const char * description,DerivateFunc func,glu::DataType type,glu::Precision precision,deUint32 hint,SurfaceType surfaceType,int numSamples,const char * fragmentSrcTmpl)1002 LinearDerivateCase::LinearDerivateCase (Context& context, const char* name, const char* description, DerivateFunc func, glu::DataType type, glu::Precision precision, deUint32 hint, SurfaceType surfaceType, int numSamples, const char* fragmentSrcTmpl)
1003 	: TriangleDerivateCase	(context, name, description)
1004 	, m_func				(func)
1005 	, m_fragmentTmpl		(fragmentSrcTmpl)
1006 {
1007 	m_dataType				= type;
1008 	m_precision				= precision;
1009 	m_coordDataType			= m_dataType;
1010 	m_coordPrecision		= m_precision;
1011 	m_hint					= hint;
1012 	m_surfaceType			= surfaceType;
1013 	m_numSamples			= numSamples;
1014 	m_useAsymmetricCoords	= true;
1015 }
1016 
init(void)1017 void LinearDerivateCase::init (void)
1018 {
1019 	const tcu::IVec2	viewportSize	= getViewportSize();
1020 	const float			w				= float(viewportSize.x());
1021 	const float			h				= float(viewportSize.y());
1022 	const bool			packToInt		= m_surfaceType == SURFACETYPE_FLOAT_FBO;
1023 	map<string, string>	fragmentParams;
1024 
1025 	fragmentParams["OUTPUT_TYPE"]		= glu::getDataTypeName(packToInt ? glu::TYPE_UINT_VEC4 : glu::TYPE_FLOAT_VEC4);
1026 	fragmentParams["OUTPUT_PREC"]		= glu::getPrecisionName(packToInt ? glu::PRECISION_HIGHP : m_precision);
1027 	fragmentParams["PRECISION"]			= glu::getPrecisionName(m_precision);
1028 	fragmentParams["DATATYPE"]			= glu::getDataTypeName(m_dataType);
1029 	fragmentParams["FUNC"]				= getDerivateFuncName(m_func);
1030 
1031 	if (packToInt)
1032 	{
1033 		fragmentParams["CAST_TO_OUTPUT"]	= m_dataType == glu::TYPE_FLOAT_VEC4 ? "floatBitsToUint(res)" :
1034 											  m_dataType == glu::TYPE_FLOAT_VEC3 ? "floatBitsToUint(vec4(res, 1.0))" :
1035 											  m_dataType == glu::TYPE_FLOAT_VEC2 ? "floatBitsToUint(vec4(res, 0.0, 1.0))" :
1036 											  /* TYPE_FLOAT */					   "floatBitsToUint(vec4(res, 0.0, 0.0, 1.0))";
1037 	}
1038 	else
1039 	{
1040 		fragmentParams["CAST_TO_OUTPUT"]	= m_dataType == glu::TYPE_FLOAT_VEC4 ? "res" :
1041 											  m_dataType == glu::TYPE_FLOAT_VEC3 ? "vec4(res, 1.0)" :
1042 											  m_dataType == glu::TYPE_FLOAT_VEC2 ? "vec4(res, 0.0, 1.0)" :
1043 											  /* TYPE_FLOAT */					   "vec4(res, 0.0, 0.0, 1.0)";
1044 	}
1045 
1046 	m_fragmentSrc = tcu::StringTemplate(m_fragmentTmpl.c_str()).specialize(fragmentParams);
1047 
1048 	switch (m_precision)
1049 	{
1050 		case glu::PRECISION_HIGHP:
1051 			m_coordMin = tcu::Vec4(-97.f, 0.2f, 71.f, 74.f);
1052 			m_coordMax = tcu::Vec4(-13.2f, -77.f, 44.f, 76.f);
1053 			break;
1054 
1055 		case glu::PRECISION_MEDIUMP:
1056 			m_coordMin = tcu::Vec4(-37.0f, 47.f, -7.f, 0.0f);
1057 			m_coordMax = tcu::Vec4(-1.0f, 12.f, 7.f, 19.f);
1058 			break;
1059 
1060 		case glu::PRECISION_LOWP:
1061 			m_coordMin = tcu::Vec4(0.0f, -1.0f, 0.0f, 1.0f);
1062 			m_coordMax = tcu::Vec4(1.0f, 1.0f, -1.0f, -1.0f);
1063 			break;
1064 
1065 		default:
1066 			DE_ASSERT(false);
1067 	}
1068 
1069 	if (m_surfaceType == SURFACETYPE_FLOAT_FBO)
1070 	{
1071 		// No scale or bias used for accuracy.
1072 		m_derivScale	= tcu::Vec4(1.0f);
1073 		m_derivBias		= tcu::Vec4(0.0f);
1074 	}
1075 	else
1076 	{
1077 		// Compute scale - bias that normalizes to 0..1 range.
1078 		const tcu::Vec4 dx = (m_coordMax - m_coordMin) / tcu::Vec4(w, w, w*0.5f, -w*0.5f);
1079 		const tcu::Vec4 dy = (m_coordMax - m_coordMin) / tcu::Vec4(h, h, h*0.5f, -h*0.5f);
1080 
1081 		switch (m_func)
1082 		{
1083 			case DERIVATE_DFDX:
1084 				m_derivScale = 0.5f / dx;
1085 				break;
1086 
1087 			case DERIVATE_DFDY:
1088 				m_derivScale = 0.5f / dy;
1089 				break;
1090 
1091 			case DERIVATE_FWIDTH:
1092 				m_derivScale = 0.5f / (tcu::abs(dx) + tcu::abs(dy));
1093 				break;
1094 
1095 			default:
1096 				DE_ASSERT(false);
1097 		}
1098 
1099 		m_derivBias = tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f);
1100 	}
1101 }
1102 
verify(const tcu::ConstPixelBufferAccess & result,const tcu::PixelBufferAccess & errorMask)1103 qpTestResult LinearDerivateCase::verify (const tcu::ConstPixelBufferAccess& result, const tcu::PixelBufferAccess& errorMask)
1104 {
1105 	const tcu::Vec4		xScale				= tcu::Vec4(0.5f, 0.5f, 0.5f, -0.5f);
1106 	const tcu::Vec4		yScale				= tcu::Vec4(0.5f, 0.5f, 0.5f, -0.5f);
1107 
1108 	const tcu::Vec4		surfaceThreshold	= getSurfaceThreshold() / abs(m_derivScale);
1109 
1110 	if (m_func == DERIVATE_DFDX || m_func == DERIVATE_DFDY)
1111 	{
1112 		const bool			isX				= m_func == DERIVATE_DFDX;
1113 		const float			div				= isX ? float(result.getWidth()) : float(result.getHeight());
1114 		const tcu::Vec4		scale			= isX ? xScale : yScale;
1115 		tcu::Vec4			reference		= ((m_coordMax - m_coordMin) / div);
1116 		const tcu::Vec4		opThreshold		= getDerivateThreshold(m_precision, m_coordMin, m_coordMax, reference);
1117 		const tcu::Vec4		opThresholdW	= getDerivateThresholdWarning(m_precision, m_coordMin, m_coordMax, reference);
1118 		const tcu::Vec4		threshold		= max(surfaceThreshold, opThreshold);
1119 		const tcu::Vec4		thresholdW		= max(surfaceThreshold, opThresholdW);
1120 		const int			numComps		= glu::getDataTypeFloatScalars(m_dataType);
1121 
1122 		/* adjust the reference value for the correct dfdx or dfdy sample adjacency */
1123 		reference	= reference * scale;
1124 
1125 		m_testCtx.getLog()
1126 			<< tcu::TestLog::Message
1127 			<< "Verifying result image.\n"
1128 			<< "\tValid derivative is " << LogVecComps(reference, numComps) << " with threshold " << LogVecComps(threshold, numComps)
1129 			<< tcu::TestLog::EndMessage;
1130 
1131 		// short circuit if result is strictly within the normal value error bounds.
1132 		// This improves performance significantly.
1133 		if (verifyConstantDerivate(m_testCtx.getLog(), result, errorMask, m_dataType,
1134 								   reference, threshold, m_derivScale, m_derivBias,
1135 								   LOG_NOTHING) == QP_TEST_RESULT_PASS)
1136 		{
1137 			m_testCtx.getLog()
1138 				<< tcu::TestLog::Message
1139 				<< "No incorrect derivatives found, result valid."
1140 				<< tcu::TestLog::EndMessage;
1141 
1142 			return QP_TEST_RESULT_PASS;
1143 		}
1144 
1145 		// Check with relaxed threshold value
1146 		if (verifyConstantDerivate(m_testCtx.getLog(), result, errorMask, m_dataType,
1147 								   reference, thresholdW, m_derivScale, m_derivBias,
1148 								   LOG_NOTHING) == QP_TEST_RESULT_PASS)
1149 		{
1150 			m_testCtx.getLog()
1151 				<< tcu::TestLog::Message
1152 				<< "No incorrect derivatives found, result valid with quality warning."
1153 				<< tcu::TestLog::EndMessage;
1154 
1155 			return QP_TEST_RESULT_QUALITY_WARNING;
1156 		}
1157 
1158 		// some pixels exceed error bounds calculated for normal values. Verify that these
1159 		// potentially invalid pixels are in fact valid due to (for example) subnorm flushing.
1160 
1161 		m_testCtx.getLog()
1162 			<< tcu::TestLog::Message
1163 			<< "Initial verification failed, verifying image by calculating accurate error bounds for each result pixel.\n"
1164 			<< "\tVerifying each result derivative is within its range of legal result values."
1165 			<< tcu::TestLog::EndMessage;
1166 
1167 		{
1168 			const tcu::IVec2			viewportSize	= getViewportSize();
1169 			const float					w				= float(viewportSize.x());
1170 			const float					h				= float(viewportSize.y());
1171 			const tcu::Vec4				valueRamp		= (m_coordMax - m_coordMin);
1172 			Linear2DFunctionEvaluator	function;
1173 
1174 			function.matrix.setRow(0, tcu::Vec3((valueRamp.x() / w) / 2.0f, (valueRamp.x() / h) / 2.0f, m_coordMin.x()));
1175 			function.matrix.setRow(1, tcu::Vec3((valueRamp.y() / w) / 2.0f, (valueRamp.y() / h) / 2.0f, m_coordMin.y()));
1176 			function.matrix.setRow(2, tcu::Vec3(valueRamp.z() / w, valueRamp.z() / h, m_coordMin.z() + m_coordMin.z()) / 2.0f);
1177 			function.matrix.setRow(3, tcu::Vec3(-valueRamp.w() / w, -valueRamp.w() / h, m_coordMax.w() + m_coordMax.w()) / 2.0f);
1178 
1179 			return reverifyConstantDerivateWithFlushRelaxations(m_testCtx.getLog(), result, errorMask,
1180 																m_dataType, m_precision, m_derivScale,
1181 																m_derivBias, surfaceThreshold, m_func,
1182 																function);
1183 		}
1184 	}
1185 	else
1186 	{
1187 		DE_ASSERT(m_func == DERIVATE_FWIDTH);
1188 		const float			w				= float(result.getWidth());
1189 		const float			h				= float(result.getHeight());
1190 
1191 		const tcu::Vec4		dx				= ((m_coordMax - m_coordMin) / w) * xScale;
1192 		const tcu::Vec4		dy				= ((m_coordMax - m_coordMin) / h) * yScale;
1193 		const tcu::Vec4		reference		= tcu::abs(dx) + tcu::abs(dy);
1194 		const tcu::Vec4		dxThreshold		= getDerivateThreshold(m_precision, m_coordMin*xScale, m_coordMax*xScale, dx);
1195 		const tcu::Vec4		dyThreshold		= getDerivateThreshold(m_precision, m_coordMin*yScale, m_coordMax*yScale, dy);
1196 		const tcu::Vec4		dxThresholdW	= getDerivateThresholdWarning(m_precision, m_coordMin*xScale, m_coordMax*xScale, dx);
1197 		const tcu::Vec4		dyThresholdW	= getDerivateThresholdWarning(m_precision, m_coordMin*yScale, m_coordMax*yScale, dy);
1198 		const tcu::Vec4		threshold		= max(surfaceThreshold, max(dxThreshold, dyThreshold));
1199 		const tcu::Vec4		thresholdW		= max(surfaceThreshold, max(dxThresholdW, dyThresholdW));
1200 		qpTestResult        testResult		= QP_TEST_RESULT_FAIL;
1201 
1202 		testResult = verifyConstantDerivate(m_testCtx.getLog(), result, errorMask, m_dataType,
1203 									  reference, threshold, m_derivScale, m_derivBias);
1204 
1205 		// return if result is pass
1206 		if (testResult == QP_TEST_RESULT_PASS)
1207 			return testResult;
1208 
1209 		// re-check with relaxed threshold
1210 		testResult = verifyConstantDerivate(m_testCtx.getLog(), result, errorMask, m_dataType,
1211 									  reference, thresholdW, m_derivScale, m_derivBias);
1212 
1213 		// if with relaxed threshold test is passing then mark the result with quality warning.
1214 		if (testResult == QP_TEST_RESULT_PASS)
1215 			testResult = QP_TEST_RESULT_QUALITY_WARNING;
1216 
1217 		return testResult;
1218 	}
1219 }
1220 
1221 // TextureDerivateCase
1222 
1223 class TextureDerivateCase : public TriangleDerivateCase
1224 {
1225 public:
1226 						TextureDerivateCase		(Context& context, const char* name, const char* description, DerivateFunc func, glu::DataType type, glu::Precision precision, deUint32 hint, SurfaceType surfaceType, int numSamples);
1227 						~TextureDerivateCase	(void);
1228 
1229 	void				init					(void);
1230 	void				deinit					(void);
1231 
1232 protected:
1233 	void				setupRenderState		(deUint32 program);
1234 	qpTestResult		verify					(const tcu::ConstPixelBufferAccess& result, const tcu::PixelBufferAccess& errorMask);
1235 
1236 private:
1237 	DerivateFunc		m_func;
1238 
1239 	tcu::Vec4			m_texValueMin;
1240 	tcu::Vec4			m_texValueMax;
1241 	glu::Texture2D*		m_texture;
1242 };
1243 
TextureDerivateCase(Context & context,const char * name,const char * description,DerivateFunc func,glu::DataType type,glu::Precision precision,deUint32 hint,SurfaceType surfaceType,int numSamples)1244 TextureDerivateCase::TextureDerivateCase (Context& context, const char* name, const char* description, DerivateFunc func, glu::DataType type, glu::Precision precision, deUint32 hint, SurfaceType surfaceType, int numSamples)
1245 	: TriangleDerivateCase	(context, name, description)
1246 	, m_func				(func)
1247 	, m_texture				(DE_NULL)
1248 {
1249 	m_dataType			= type;
1250 	m_precision			= precision;
1251 	m_coordDataType		= glu::TYPE_FLOAT_VEC2;
1252 	m_coordPrecision	= glu::PRECISION_HIGHP;
1253 	m_hint				= hint;
1254 	m_surfaceType		= surfaceType;
1255 	m_numSamples		= numSamples;
1256 }
1257 
~TextureDerivateCase(void)1258 TextureDerivateCase::~TextureDerivateCase (void)
1259 {
1260 	delete m_texture;
1261 }
1262 
init(void)1263 void TextureDerivateCase::init (void)
1264 {
1265 	// Generate shader
1266 	{
1267 		const char* fragmentTmpl =
1268 			"#version 300 es\n"
1269 			"in highp vec2 v_coord;\n"
1270 			"layout(location = 0) out ${OUTPUT_PREC} ${OUTPUT_TYPE} o_color;\n"
1271 			"uniform ${PRECISION} sampler2D u_sampler;\n"
1272 			"uniform ${PRECISION} ${DATATYPE} u_scale;\n"
1273 			"uniform ${PRECISION} ${DATATYPE} u_bias;\n"
1274 			"void main (void)\n"
1275 			"{\n"
1276 			"	${PRECISION} vec4 tex = texture(u_sampler, v_coord);\n"
1277 			"	${PRECISION} ${DATATYPE} res = ${FUNC}(tex${SWIZZLE}) * u_scale + u_bias;\n"
1278 			"	o_color = ${CAST_TO_OUTPUT};\n"
1279 			"}\n";
1280 
1281 		const bool			packToInt		= m_surfaceType == SURFACETYPE_FLOAT_FBO;
1282 		map<string, string> fragmentParams;
1283 
1284 		fragmentParams["OUTPUT_TYPE"]		= glu::getDataTypeName(packToInt ? glu::TYPE_UINT_VEC4 : glu::TYPE_FLOAT_VEC4);
1285 		fragmentParams["OUTPUT_PREC"]		= glu::getPrecisionName(packToInt ? glu::PRECISION_HIGHP : m_precision);
1286 		fragmentParams["PRECISION"]			= glu::getPrecisionName(m_precision);
1287 		fragmentParams["DATATYPE"]			= glu::getDataTypeName(m_dataType);
1288 		fragmentParams["FUNC"]				= getDerivateFuncName(m_func);
1289 		fragmentParams["SWIZZLE"]			= m_dataType == glu::TYPE_FLOAT_VEC4 ? "" :
1290 											  m_dataType == glu::TYPE_FLOAT_VEC3 ? ".xyz" :
1291 											  m_dataType == glu::TYPE_FLOAT_VEC2 ? ".xy" :
1292 											  /* TYPE_FLOAT */					   ".x";
1293 
1294 		if (packToInt)
1295 		{
1296 			fragmentParams["CAST_TO_OUTPUT"]	= m_dataType == glu::TYPE_FLOAT_VEC4 ? "floatBitsToUint(res)" :
1297 												  m_dataType == glu::TYPE_FLOAT_VEC3 ? "floatBitsToUint(vec4(res, 1.0))" :
1298 												  m_dataType == glu::TYPE_FLOAT_VEC2 ? "floatBitsToUint(vec4(res, 0.0, 1.0))" :
1299 												  /* TYPE_FLOAT */					   "floatBitsToUint(vec4(res, 0.0, 0.0, 1.0))";
1300 		}
1301 		else
1302 		{
1303 			fragmentParams["CAST_TO_OUTPUT"]	= m_dataType == glu::TYPE_FLOAT_VEC4 ? "res" :
1304 												  m_dataType == glu::TYPE_FLOAT_VEC3 ? "vec4(res, 1.0)" :
1305 												  m_dataType == glu::TYPE_FLOAT_VEC2 ? "vec4(res, 0.0, 1.0)" :
1306 												  /* TYPE_FLOAT */					   "vec4(res, 0.0, 0.0, 1.0)";
1307 		}
1308 
1309 		m_fragmentSrc = tcu::StringTemplate(fragmentTmpl).specialize(fragmentParams);
1310 	}
1311 
1312 	// Texture size matches viewport and nearest sampling is used. Thus texture sampling
1313 	// is equal to just interpolating the texture value range.
1314 
1315 	// Determine value range for texture.
1316 
1317 	switch (m_precision)
1318 	{
1319 		case glu::PRECISION_HIGHP:
1320 			m_texValueMin = tcu::Vec4(-97.f, 0.2f, 71.f, 74.f);
1321 			m_texValueMax = tcu::Vec4(-13.2f, -77.f, 44.f, 76.f);
1322 			break;
1323 
1324 		case glu::PRECISION_MEDIUMP:
1325 			m_texValueMin = tcu::Vec4(-37.0f, 47.f, -7.f, 0.0f);
1326 			m_texValueMax = tcu::Vec4(-1.0f, 12.f, 7.f, 19.f);
1327 			break;
1328 
1329 		case glu::PRECISION_LOWP:
1330 			m_texValueMin = tcu::Vec4(0.0f, -1.0f, 0.0f, 1.0f);
1331 			m_texValueMax = tcu::Vec4(1.0f, 1.0f, -1.0f, -1.0f);
1332 			break;
1333 
1334 		default:
1335 			DE_ASSERT(false);
1336 	}
1337 
1338 	// Lowp and mediump cases use RGBA16F format, while highp uses RGBA32F.
1339 	{
1340 		const tcu::IVec2 viewportSize = getViewportSize();
1341 		DE_ASSERT(!m_texture);
1342 		m_texture = new glu::Texture2D(m_context.getRenderContext(), m_precision == glu::PRECISION_HIGHP ? GL_RGBA32F : GL_RGBA16F, viewportSize.x(), viewportSize.y());
1343 		m_texture->getRefTexture().allocLevel(0);
1344 	}
1345 
1346 	// Texture coordinates
1347 	m_coordMin = tcu::Vec4(0.0f);
1348 	m_coordMax = tcu::Vec4(1.0f);
1349 
1350 	// Fill with gradients.
1351 	{
1352 		const tcu::PixelBufferAccess level0 = m_texture->getRefTexture().getLevel(0);
1353 		for (int y = 0; y < level0.getHeight(); y++)
1354 		{
1355 			for (int x = 0; x < level0.getWidth(); x++)
1356 			{
1357 				const float		xf	= (float(x)+0.5f) / float(level0.getWidth());
1358 				const float		yf	= (float(y)+0.5f) / float(level0.getHeight());
1359 				// Make x and y data to have dependency to both axes so that dfdx(tex).y and dfdy(tex).x are nonzero.
1360 				const tcu::Vec4	s	= tcu::Vec4(xf + yf/2.0f, yf + xf/2.0f, (xf+yf)/2.0f, 1.0f - (xf+yf)/2.0f);
1361 
1362 				level0.setPixel(m_texValueMin + (m_texValueMax - m_texValueMin)*s, x, y);
1363 			}
1364 		}
1365 	}
1366 
1367 	m_texture->upload();
1368 
1369 	if (m_surfaceType == SURFACETYPE_FLOAT_FBO)
1370 	{
1371 		// No scale or bias used for accuracy.
1372 		m_derivScale	= tcu::Vec4(1.0f);
1373 		m_derivBias		= tcu::Vec4(0.0f);
1374 	}
1375 	else
1376 	{
1377 		// Compute scale - bias that normalizes to 0..1 range.
1378 		const tcu::IVec2	viewportSize	= getViewportSize();
1379 		const float			w				= float(viewportSize.x());
1380 		const float			h				= float(viewportSize.y());
1381 		const tcu::Vec4		dx				= (m_texValueMax - m_texValueMin) / tcu::Vec4(w, w, w*0.5f, -w*0.5f);
1382 		const tcu::Vec4		dy				= (m_texValueMax - m_texValueMin) / tcu::Vec4(h, h, h*0.5f, -h*0.5f);
1383 
1384 		switch (m_func)
1385 		{
1386 			case DERIVATE_DFDX:
1387 				m_derivScale = 0.5f / dx;
1388 				break;
1389 
1390 			case DERIVATE_DFDY:
1391 				m_derivScale = 0.5f / dy;
1392 				break;
1393 
1394 			case DERIVATE_FWIDTH:
1395 				m_derivScale = 0.5f / (tcu::abs(dx) + tcu::abs(dy));
1396 				break;
1397 
1398 			default:
1399 				DE_ASSERT(false);
1400 		}
1401 
1402 		m_derivBias = tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f);
1403 	}
1404 }
1405 
deinit(void)1406 void TextureDerivateCase::deinit (void)
1407 {
1408 	delete m_texture;
1409 	m_texture = DE_NULL;
1410 }
1411 
setupRenderState(deUint32 program)1412 void TextureDerivateCase::setupRenderState (deUint32 program)
1413 {
1414 	const glw::Functions&	gl			= m_context.getRenderContext().getFunctions();
1415 	const int				texUnit		= 1;
1416 
1417 	gl.activeTexture	(GL_TEXTURE0+texUnit);
1418 	gl.bindTexture		(GL_TEXTURE_2D, m_texture->getGLTexture());
1419 	gl.texParameteri	(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,	GL_NEAREST);
1420 	gl.texParameteri	(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,	GL_NEAREST);
1421 	gl.texParameteri	(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,		GL_CLAMP_TO_EDGE);
1422 	gl.texParameteri	(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,		GL_CLAMP_TO_EDGE);
1423 
1424 	gl.uniform1i		(gl.getUniformLocation(program, "u_sampler"), texUnit);
1425 }
1426 
verify(const tcu::ConstPixelBufferAccess & result,const tcu::PixelBufferAccess & errorMask)1427 qpTestResult TextureDerivateCase::verify (const tcu::ConstPixelBufferAccess& result, const tcu::PixelBufferAccess& errorMask)
1428 {
1429 	// \note Edges are ignored in comparison
1430 	if (result.getWidth() < 2 || result.getHeight() < 2)
1431 		throw tcu::NotSupportedError("Too small viewport");
1432 
1433 	tcu::ConstPixelBufferAccess	compareArea			= tcu::getSubregion(result, 1, 1, result.getWidth()-2, result.getHeight()-2);
1434 	tcu::PixelBufferAccess		maskArea			= tcu::getSubregion(errorMask, 1, 1, errorMask.getWidth()-2, errorMask.getHeight()-2);
1435 	const tcu::Vec4				xScale				= tcu::Vec4(1.0f, 0.5f, 0.5f, -0.5f);
1436 	const tcu::Vec4				yScale				= tcu::Vec4(0.5f, 1.0f, 0.5f, -0.5f);
1437 	const float					w					= float(result.getWidth());
1438 	const float					h					= float(result.getHeight());
1439 
1440 	const tcu::Vec4				surfaceThreshold	= getSurfaceThreshold() / abs(m_derivScale);
1441 
1442 	if (m_func == DERIVATE_DFDX || m_func == DERIVATE_DFDY)
1443 	{
1444 		const bool			isX				= m_func == DERIVATE_DFDX;
1445 		const float			div				= isX ? w : h;
1446 		const tcu::Vec4		scale			= isX ? xScale : yScale;
1447 		tcu::Vec4			reference		= ((m_texValueMax - m_texValueMin) / div);
1448 		const tcu::Vec4		opThreshold		= getDerivateThreshold(m_precision, m_texValueMin, m_texValueMax, reference);
1449 		const tcu::Vec4		opThresholdW	= getDerivateThresholdWarning(m_precision, m_texValueMin, m_texValueMax, reference);
1450 		const tcu::Vec4		threshold		= max(surfaceThreshold, opThreshold);
1451 		const tcu::Vec4		thresholdW		= max(surfaceThreshold, opThresholdW);
1452 		const int			numComps		= glu::getDataTypeFloatScalars(m_dataType);
1453 
1454 		/* adjust the reference value for the correct dfdx or dfdy sample adjacency */
1455 		reference	= reference * scale;
1456 
1457 		m_testCtx.getLog()
1458 			<< tcu::TestLog::Message
1459 			<< "Verifying result image.\n"
1460 			<< "\tValid derivative is " << LogVecComps(reference, numComps) << " with threshold " << LogVecComps(threshold, numComps)
1461 			<< tcu::TestLog::EndMessage;
1462 
1463 		// short circuit if result is strictly within the normal value error bounds.
1464 		// This improves performance significantly.
1465 		if (verifyConstantDerivate(m_testCtx.getLog(), compareArea, maskArea, m_dataType,
1466 								   reference, threshold, m_derivScale, m_derivBias,
1467 								   LOG_NOTHING) == QP_TEST_RESULT_PASS)
1468 		{
1469 			m_testCtx.getLog()
1470 				<< tcu::TestLog::Message
1471 				<< "No incorrect derivatives found, result valid."
1472 				<< tcu::TestLog::EndMessage;
1473 
1474 			return QP_TEST_RESULT_PASS;
1475 		}
1476 
1477 		m_testCtx.getLog()
1478 			<< tcu::TestLog::Message
1479 			<< "Verifying result image.\n"
1480 			<< "\tValid derivative is " << LogVecComps(reference, numComps) << " with Warning threshold " << LogVecComps(thresholdW, numComps)
1481 			<< tcu::TestLog::EndMessage;
1482 
1483 		// Re-check with relaxed threshold
1484 		if (verifyConstantDerivate(m_testCtx.getLog(), compareArea, maskArea, m_dataType,
1485 								   reference, thresholdW, m_derivScale, m_derivBias,
1486 								   LOG_NOTHING) == QP_TEST_RESULT_PASS)
1487 		{
1488 			m_testCtx.getLog()
1489 				<< tcu::TestLog::Message
1490 				<< "No incorrect derivatives found, result valid with quality warning."
1491 				<< tcu::TestLog::EndMessage;
1492 
1493 			return QP_TEST_RESULT_QUALITY_WARNING;
1494 		}
1495 
1496 
1497 		// some pixels exceed error bounds calculated for normal values. Verify that these
1498 		// potentially invalid pixels are in fact valid due to (for example) subnorm flushing.
1499 
1500 		m_testCtx.getLog()
1501 			<< tcu::TestLog::Message
1502 			<< "Initial verification failed, verifying image by calculating accurate error bounds for each result pixel.\n"
1503 			<< "\tVerifying each result derivative is within its range of legal result values."
1504 			<< tcu::TestLog::EndMessage;
1505 
1506 		{
1507 			const tcu::Vec4				valueRamp		= (m_texValueMax - m_texValueMin);
1508 			Linear2DFunctionEvaluator	function;
1509 
1510 			function.matrix.setRow(0, tcu::Vec3(valueRamp.x() / w, (valueRamp.x() / h) / 2.0f, m_texValueMin.x()));
1511 			function.matrix.setRow(1, tcu::Vec3((valueRamp.y() / w) / 2.0f, valueRamp.y() / h, m_texValueMin.y()));
1512 			function.matrix.setRow(2, tcu::Vec3(valueRamp.z() / w, valueRamp.z() / h, m_texValueMin.z() + m_texValueMin.z()) / 2.0f);
1513 			function.matrix.setRow(3, tcu::Vec3(-valueRamp.w() / w, -valueRamp.w() / h, m_texValueMax.w() + m_texValueMax.w()) / 2.0f);
1514 
1515 			return reverifyConstantDerivateWithFlushRelaxations(m_testCtx.getLog(), compareArea, maskArea,
1516 																m_dataType, m_precision, m_derivScale,
1517 																m_derivBias, surfaceThreshold, m_func,
1518 																function);
1519 		}
1520 	}
1521 	else
1522 	{
1523 		DE_ASSERT(m_func == DERIVATE_FWIDTH);
1524 		const tcu::Vec4	dx				= ((m_texValueMax - m_texValueMin) / w) * xScale;
1525 		const tcu::Vec4	dy				= ((m_texValueMax - m_texValueMin) / h) * yScale;
1526 		const tcu::Vec4	reference		= tcu::abs(dx) + tcu::abs(dy);
1527 		const tcu::Vec4	dxThreshold		= getDerivateThreshold(m_precision, m_texValueMin*xScale, m_texValueMax*xScale, dx);
1528 		const tcu::Vec4	dyThreshold		= getDerivateThreshold(m_precision, m_texValueMin*yScale, m_texValueMax*yScale, dy);
1529 		const tcu::Vec4	dxThresholdW	= getDerivateThresholdWarning(m_precision, m_texValueMin*xScale, m_texValueMax*xScale, dx);
1530 		const tcu::Vec4	dyThresholdW	= getDerivateThresholdWarning(m_precision, m_texValueMin*yScale, m_texValueMax*yScale, dy);
1531 		const tcu::Vec4	threshold		= max(surfaceThreshold, max(dxThreshold, dyThreshold));
1532 		const tcu::Vec4	thresholdW		= max(surfaceThreshold, max(dxThresholdW, dyThresholdW));
1533 		qpTestResult	testResult		= QP_TEST_RESULT_FAIL;
1534 
1535 		testResult = verifyConstantDerivate(m_testCtx.getLog(), compareArea, maskArea, m_dataType,
1536 									  reference, threshold, m_derivScale, m_derivBias);
1537 
1538 		if (testResult == QP_TEST_RESULT_PASS)
1539 			return testResult;
1540 
1541 		// Re-Check with relaxed threshold
1542 		testResult = verifyConstantDerivate(m_testCtx.getLog(), compareArea, maskArea, m_dataType,
1543 									  reference, thresholdW, m_derivScale, m_derivBias);
1544 
1545 		// If test is passing with relaxed threshold then mark quality warning
1546 		if (testResult == QP_TEST_RESULT_PASS)
1547 			testResult = QP_TEST_RESULT_QUALITY_WARNING;
1548 
1549 		return testResult;
1550 	}
1551 }
1552 
ShaderDerivateTests(Context & context)1553 ShaderDerivateTests::ShaderDerivateTests (Context& context)
1554 	: TestCaseGroup(context, "derivate", "Derivate Function Tests")
1555 {
1556 }
1557 
~ShaderDerivateTests(void)1558 ShaderDerivateTests::~ShaderDerivateTests (void)
1559 {
1560 }
1561 
1562 struct FunctionSpec
1563 {
1564 	std::string		name;
1565 	DerivateFunc	function;
1566 	glu::DataType	dataType;
1567 	glu::Precision	precision;
1568 
FunctionSpecdeqp::gles3::Functional::FunctionSpec1569 	FunctionSpec (const std::string& name_, DerivateFunc function_, glu::DataType dataType_, glu::Precision precision_)
1570 		: name		(name_)
1571 		, function	(function_)
1572 		, dataType	(dataType_)
1573 		, precision	(precision_)
1574 	{
1575 	}
1576 };
1577 
init(void)1578 void ShaderDerivateTests::init (void)
1579 {
1580 	static const struct
1581 	{
1582 		const char*		name;
1583 		const char*		description;
1584 		const char*		source;
1585 	} s_linearDerivateCases[] =
1586 	{
1587 		{
1588 			"linear",
1589 			"Basic derivate of linearly interpolated argument",
1590 
1591 			"#version 300 es\n"
1592 			"in ${PRECISION} ${DATATYPE} v_coord;\n"
1593 			"layout(location = 0) out ${OUTPUT_PREC} ${OUTPUT_TYPE} o_color;\n"
1594 			"uniform ${PRECISION} ${DATATYPE} u_scale;\n"
1595 			"uniform ${PRECISION} ${DATATYPE} u_bias;\n"
1596 			"void main (void)\n"
1597 			"{\n"
1598 			"	${PRECISION} ${DATATYPE} res = ${FUNC}(v_coord) * u_scale + u_bias;\n"
1599 			"	o_color = ${CAST_TO_OUTPUT};\n"
1600 			"}\n"
1601 		},
1602 		{
1603 			"in_function",
1604 			"Derivate of linear function argument",
1605 
1606 			"#version 300 es\n"
1607 			"in ${PRECISION} ${DATATYPE} v_coord;\n"
1608 			"layout(location = 0) out ${OUTPUT_PREC} ${OUTPUT_TYPE} o_color;\n"
1609 			"uniform ${PRECISION} ${DATATYPE} u_scale;\n"
1610 			"uniform ${PRECISION} ${DATATYPE} u_bias;\n"
1611 			"\n"
1612 			"${PRECISION} ${DATATYPE} computeRes (${PRECISION} ${DATATYPE} value)\n"
1613 			"{\n"
1614 			"	return ${FUNC}(v_coord) * u_scale + u_bias;\n"
1615 			"}\n"
1616 			"\n"
1617 			"void main (void)\n"
1618 			"{\n"
1619 			"	${PRECISION} ${DATATYPE} res = computeRes(v_coord);\n"
1620 			"	o_color = ${CAST_TO_OUTPUT};\n"
1621 			"}\n"
1622 		},
1623 		{
1624 			"static_if",
1625 			"Derivate of linearly interpolated value in static if",
1626 
1627 			"#version 300 es\n"
1628 			"in ${PRECISION} ${DATATYPE} v_coord;\n"
1629 			"layout(location = 0) out ${OUTPUT_PREC} ${OUTPUT_TYPE} o_color;\n"
1630 			"uniform ${PRECISION} ${DATATYPE} u_scale;\n"
1631 			"uniform ${PRECISION} ${DATATYPE} u_bias;\n"
1632 			"void main (void)\n"
1633 			"{\n"
1634 			"	${PRECISION} ${DATATYPE} res;\n"
1635 			"	if (false)\n"
1636 			"		res = ${FUNC}(-v_coord) * u_scale + u_bias;\n"
1637 			"	else\n"
1638 			"		res = ${FUNC}(v_coord) * u_scale + u_bias;\n"
1639 			"	o_color = ${CAST_TO_OUTPUT};\n"
1640 			"}\n"
1641 		},
1642 		{
1643 			"static_loop",
1644 			"Derivate of linearly interpolated value in static loop",
1645 
1646 			"#version 300 es\n"
1647 			"in ${PRECISION} ${DATATYPE} v_coord;\n"
1648 			"layout(location = 0) out ${OUTPUT_PREC} ${OUTPUT_TYPE} o_color;\n"
1649 			"uniform ${PRECISION} ${DATATYPE} u_scale;\n"
1650 			"uniform ${PRECISION} ${DATATYPE} u_bias;\n"
1651 			"void main (void)\n"
1652 			"{\n"
1653 			"	${PRECISION} ${DATATYPE} res = ${DATATYPE}(0.0);\n"
1654 			"	for (int i = 0; i < 2; i++)\n"
1655 			"		res += ${FUNC}(v_coord * float(i));\n"
1656 			"	res = res * u_scale + u_bias;\n"
1657 			"	o_color = ${CAST_TO_OUTPUT};\n"
1658 			"}\n"
1659 		},
1660 		{
1661 			"static_switch",
1662 			"Derivate of linearly interpolated value in static switch",
1663 
1664 			"#version 300 es\n"
1665 			"in ${PRECISION} ${DATATYPE} v_coord;\n"
1666 			"layout(location = 0) out ${OUTPUT_PREC} ${OUTPUT_TYPE} o_color;\n"
1667 			"uniform ${PRECISION} ${DATATYPE} u_scale;\n"
1668 			"uniform ${PRECISION} ${DATATYPE} u_bias;\n"
1669 			"void main (void)\n"
1670 			"{\n"
1671 			"	${PRECISION} ${DATATYPE} res;\n"
1672 			"	switch (1)\n"
1673 			"	{\n"
1674 			"		case 0:	res = ${FUNC}(-v_coord) * u_scale + u_bias;	break;\n"
1675 			"		case 1:	res = ${FUNC}(v_coord) * u_scale + u_bias;	break;\n"
1676 			"	}\n"
1677 			"	o_color = ${CAST_TO_OUTPUT};\n"
1678 			"}\n"
1679 		},
1680 		{
1681 			"uniform_if",
1682 			"Derivate of linearly interpolated value in uniform if",
1683 
1684 			"#version 300 es\n"
1685 			"in ${PRECISION} ${DATATYPE} v_coord;\n"
1686 			"layout(location = 0) out ${OUTPUT_PREC} ${OUTPUT_TYPE} o_color;\n"
1687 			"uniform ${PRECISION} ${DATATYPE} u_scale;\n"
1688 			"uniform ${PRECISION} ${DATATYPE} u_bias;\n"
1689 			"uniform bool ub_true;\n"
1690 			"void main (void)\n"
1691 			"{\n"
1692 			"	${PRECISION} ${DATATYPE} res;\n"
1693 			"	if (ub_true)"
1694 			"		res = ${FUNC}(v_coord) * u_scale + u_bias;\n"
1695 			"	else\n"
1696 			"		res = ${FUNC}(-v_coord) * u_scale + u_bias;\n"
1697 			"	o_color = ${CAST_TO_OUTPUT};\n"
1698 			"}\n"
1699 		},
1700 		{
1701 			"uniform_loop",
1702 			"Derivate of linearly interpolated value in uniform loop",
1703 
1704 			"#version 300 es\n"
1705 			"in ${PRECISION} ${DATATYPE} v_coord;\n"
1706 			"layout(location = 0) out ${OUTPUT_PREC} ${OUTPUT_TYPE} o_color;\n"
1707 			"uniform ${PRECISION} ${DATATYPE} u_scale;\n"
1708 			"uniform ${PRECISION} ${DATATYPE} u_bias;\n"
1709 			"uniform int ui_two;\n"
1710 			"void main (void)\n"
1711 			"{\n"
1712 			"	${PRECISION} ${DATATYPE} res = ${DATATYPE}(0.0);\n"
1713 			"	for (int i = 0; i < ui_two; i++)\n"
1714 			"		res += ${FUNC}(v_coord * float(i));\n"
1715 			"	res = res * u_scale + u_bias;\n"
1716 			"	o_color = ${CAST_TO_OUTPUT};\n"
1717 			"}\n"
1718 		},
1719 		{
1720 			"uniform_switch",
1721 			"Derivate of linearly interpolated value in uniform switch",
1722 
1723 			"#version 300 es\n"
1724 			"in ${PRECISION} ${DATATYPE} v_coord;\n"
1725 			"layout(location = 0) out ${OUTPUT_PREC} ${OUTPUT_TYPE} o_color;\n"
1726 			"uniform ${PRECISION} ${DATATYPE} u_scale;\n"
1727 			"uniform ${PRECISION} ${DATATYPE} u_bias;\n"
1728 			"uniform int ui_one;\n"
1729 			"void main (void)\n"
1730 			"{\n"
1731 			"	${PRECISION} ${DATATYPE} res;\n"
1732 			"	switch (ui_one)\n"
1733 			"	{\n"
1734 			"		case 0:	res = ${FUNC}(-v_coord) * u_scale + u_bias;	break;\n"
1735 			"		case 1:	res = ${FUNC}(v_coord) * u_scale + u_bias;	break;\n"
1736 			"	}\n"
1737 			"	o_color = ${CAST_TO_OUTPUT};\n"
1738 			"}\n"
1739 		},
1740 	};
1741 
1742 	static const struct
1743 	{
1744 		const char*		name;
1745 		SurfaceType		surfaceType;
1746 		int				numSamples;
1747 	} s_fboConfigs[] =
1748 	{
1749 		{ "fbo",		SURFACETYPE_DEFAULT_FRAMEBUFFER,	0 },
1750 		{ "fbo_msaa2",	SURFACETYPE_UNORM_FBO,				2 },
1751 		{ "fbo_msaa4",	SURFACETYPE_UNORM_FBO,				4 },
1752 		{ "fbo_float",	SURFACETYPE_FLOAT_FBO,				0 },
1753 	};
1754 
1755 	static const struct
1756 	{
1757 		const char*		name;
1758 		deUint32		hint;
1759 	} s_hints[] =
1760 	{
1761 		{ "fastest",	GL_FASTEST	},
1762 		{ "nicest",		GL_NICEST	},
1763 	};
1764 
1765 	static const struct
1766 	{
1767 		const char*		name;
1768 		SurfaceType		surfaceType;
1769 		int				numSamples;
1770 	} s_hintFboConfigs[] =
1771 	{
1772 		{ "default",		SURFACETYPE_DEFAULT_FRAMEBUFFER,	0 },
1773 		{ "fbo_msaa4",		SURFACETYPE_UNORM_FBO,				4 },
1774 		{ "fbo_float",		SURFACETYPE_FLOAT_FBO,				0 }
1775 	};
1776 
1777 	static const struct
1778 	{
1779 		const char*		name;
1780 		SurfaceType		surfaceType;
1781 		int				numSamples;
1782 		deUint32		hint;
1783 	} s_textureConfigs[] =
1784 	{
1785 		{ "basic",			SURFACETYPE_DEFAULT_FRAMEBUFFER,	0,	GL_DONT_CARE	},
1786 		{ "msaa4",			SURFACETYPE_UNORM_FBO,				4,	GL_DONT_CARE	},
1787 		{ "float_fastest",	SURFACETYPE_FLOAT_FBO,				0,	GL_FASTEST		},
1788 		{ "float_nicest",	SURFACETYPE_FLOAT_FBO,				0,	GL_NICEST		},
1789 	};
1790 
1791 	// .dfdx, .dfdy, .fwidth
1792 	for (int funcNdx = 0; funcNdx < DERIVATE_LAST; funcNdx++)
1793 	{
1794 		const DerivateFunc			function		= DerivateFunc(funcNdx);
1795 		tcu::TestCaseGroup* const	functionGroup	= new tcu::TestCaseGroup(m_testCtx, getDerivateFuncCaseName(function), getDerivateFuncName(function));
1796 		addChild(functionGroup);
1797 
1798 		// .constant - no precision variants, checks that derivate of constant arguments is 0
1799 		{
1800 			tcu::TestCaseGroup* const constantGroup = new tcu::TestCaseGroup(m_testCtx, "constant", "Derivate of constant argument");
1801 			functionGroup->addChild(constantGroup);
1802 
1803 			for (int vecSize = 1; vecSize <= 4; vecSize++)
1804 			{
1805 				const glu::DataType dataType = vecSize > 1 ? glu::getDataTypeFloatVec(vecSize) : glu::TYPE_FLOAT;
1806 				constantGroup->addChild(new ConstantDerivateCase(m_context, glu::getDataTypeName(dataType), "", function, dataType));
1807 			}
1808 		}
1809 
1810 		// Cases based on LinearDerivateCase
1811 		for (int caseNdx = 0; caseNdx < DE_LENGTH_OF_ARRAY(s_linearDerivateCases); caseNdx++)
1812 		{
1813 			tcu::TestCaseGroup* const linearCaseGroup	= new tcu::TestCaseGroup(m_testCtx, s_linearDerivateCases[caseNdx].name, s_linearDerivateCases[caseNdx].description);
1814 			const char*			source					= s_linearDerivateCases[caseNdx].source;
1815 			functionGroup->addChild(linearCaseGroup);
1816 
1817 			for (int vecSize = 1; vecSize <= 4; vecSize++)
1818 			{
1819 				for (int precNdx = 0; precNdx < glu::PRECISION_LAST; precNdx++)
1820 				{
1821 					const glu::DataType		dataType		= vecSize > 1 ? glu::getDataTypeFloatVec(vecSize) : glu::TYPE_FLOAT;
1822 					const glu::Precision	precision		= glu::Precision(precNdx);
1823 					const SurfaceType		surfaceType		= SURFACETYPE_DEFAULT_FRAMEBUFFER;
1824 					const int				numSamples		= 0;
1825 					const deUint32			hint			= GL_DONT_CARE;
1826 					ostringstream			caseName;
1827 
1828 					if (caseNdx != 0 && precision == glu::PRECISION_LOWP)
1829 						continue; // Skip as lowp doesn't actually produce any bits when rendered to default FB.
1830 
1831 					caseName << glu::getDataTypeName(dataType) << "_" << glu::getPrecisionName(precision);
1832 
1833 					linearCaseGroup->addChild(new LinearDerivateCase(m_context, caseName.str().c_str(), "", function, dataType, precision, hint, surfaceType, numSamples, source));
1834 				}
1835 			}
1836 		}
1837 
1838 		// Fbo cases
1839 		for (int caseNdx = 0; caseNdx < DE_LENGTH_OF_ARRAY(s_fboConfigs); caseNdx++)
1840 		{
1841 			tcu::TestCaseGroup*	const	fboGroup		= new tcu::TestCaseGroup(m_testCtx, s_fboConfigs[caseNdx].name, "Derivate usage when rendering into FBO");
1842 			const char*					source			= s_linearDerivateCases[0].source; // use source from .linear group
1843 			const SurfaceType			surfaceType		= s_fboConfigs[caseNdx].surfaceType;
1844 			const int					numSamples		= s_fboConfigs[caseNdx].numSamples;
1845 			functionGroup->addChild(fboGroup);
1846 
1847 			for (int vecSize = 1; vecSize <= 4; vecSize++)
1848 			{
1849 				for (int precNdx = 0; precNdx < glu::PRECISION_LAST; precNdx++)
1850 				{
1851 					const glu::DataType		dataType		= vecSize > 1 ? glu::getDataTypeFloatVec(vecSize) : glu::TYPE_FLOAT;
1852 					const glu::Precision	precision		= glu::Precision(precNdx);
1853 					const deUint32			hint			= GL_DONT_CARE;
1854 					ostringstream			caseName;
1855 
1856 					if (surfaceType != SURFACETYPE_FLOAT_FBO && precision == glu::PRECISION_LOWP)
1857 						continue; // Skip as lowp doesn't actually produce any bits when rendered to U8 RT.
1858 
1859 					caseName << glu::getDataTypeName(dataType) << "_" << glu::getPrecisionName(precision);
1860 
1861 					fboGroup->addChild(new LinearDerivateCase(m_context, caseName.str().c_str(), "", function, dataType, precision, hint, surfaceType, numSamples, source));
1862 				}
1863 			}
1864 		}
1865 
1866 		// .fastest, .nicest
1867 		for (int hintCaseNdx = 0; hintCaseNdx < DE_LENGTH_OF_ARRAY(s_hints); hintCaseNdx++)
1868 		{
1869 			tcu::TestCaseGroup* const	hintGroup		= new tcu::TestCaseGroup(m_testCtx, s_hints[hintCaseNdx].name, "Shader derivate hints");
1870 			const char*					source			= s_linearDerivateCases[0].source; // use source from .linear group
1871 			const deUint32				hint			= s_hints[hintCaseNdx].hint;
1872 			functionGroup->addChild(hintGroup);
1873 
1874 			for (int fboCaseNdx = 0; fboCaseNdx < DE_LENGTH_OF_ARRAY(s_hintFboConfigs); fboCaseNdx++)
1875 			{
1876 				tcu::TestCaseGroup*	const	fboGroup		= new tcu::TestCaseGroup(m_testCtx, s_hintFboConfigs[fboCaseNdx].name, "");
1877 				const SurfaceType			surfaceType		= s_hintFboConfigs[fboCaseNdx].surfaceType;
1878 				const int					numSamples		= s_hintFboConfigs[fboCaseNdx].numSamples;
1879 				hintGroup->addChild(fboGroup);
1880 
1881 				for (int vecSize = 1; vecSize <= 4; vecSize++)
1882 				{
1883 					for (int precNdx = 0; precNdx < glu::PRECISION_LAST; precNdx++)
1884 					{
1885 						const glu::DataType		dataType		= vecSize > 1 ? glu::getDataTypeFloatVec(vecSize) : glu::TYPE_FLOAT;
1886 						const glu::Precision	precision		= glu::Precision(precNdx);
1887 						ostringstream			caseName;
1888 
1889 						if (surfaceType != SURFACETYPE_FLOAT_FBO && precision == glu::PRECISION_LOWP)
1890 							continue; // Skip as lowp doesn't actually produce any bits when rendered to U8 RT.
1891 
1892 						caseName << glu::getDataTypeName(dataType) << "_" << glu::getPrecisionName(precision);
1893 
1894 						fboGroup->addChild(new LinearDerivateCase(m_context, caseName.str().c_str(), "", function, dataType, precision, hint, surfaceType, numSamples, source));
1895 					}
1896 				}
1897 			}
1898 		}
1899 
1900 		// .texture
1901 		{
1902 			tcu::TestCaseGroup* const textureGroup = new tcu::TestCaseGroup(m_testCtx, "texture", "Derivate of texture lookup result");
1903 			functionGroup->addChild(textureGroup);
1904 
1905 			for (int texCaseNdx = 0; texCaseNdx < DE_LENGTH_OF_ARRAY(s_textureConfigs); texCaseNdx++)
1906 			{
1907 				tcu::TestCaseGroup*	const	caseGroup		= new tcu::TestCaseGroup(m_testCtx, s_textureConfigs[texCaseNdx].name, "");
1908 				const SurfaceType			surfaceType		= s_textureConfigs[texCaseNdx].surfaceType;
1909 				const int					numSamples		= s_textureConfigs[texCaseNdx].numSamples;
1910 				const deUint32				hint			= s_textureConfigs[texCaseNdx].hint;
1911 				textureGroup->addChild(caseGroup);
1912 
1913 				for (int vecSize = 1; vecSize <= 4; vecSize++)
1914 				{
1915 					for (int precNdx = 0; precNdx < glu::PRECISION_LAST; precNdx++)
1916 					{
1917 						const glu::DataType		dataType		= vecSize > 1 ? glu::getDataTypeFloatVec(vecSize) : glu::TYPE_FLOAT;
1918 						const glu::Precision	precision		= glu::Precision(precNdx);
1919 						ostringstream			caseName;
1920 
1921 						if (surfaceType != SURFACETYPE_FLOAT_FBO && precision == glu::PRECISION_LOWP)
1922 							continue; // Skip as lowp doesn't actually produce any bits when rendered to U8 RT.
1923 
1924 						caseName << glu::getDataTypeName(dataType) << "_" << glu::getPrecisionName(precision);
1925 
1926 						caseGroup->addChild(new TextureDerivateCase(m_context, caseName.str().c_str(), "", function, dataType, precision, hint, surfaceType, numSamples));
1927 					}
1928 				}
1929 			}
1930 		}
1931 	}
1932 }
1933 
1934 } // Functional
1935 } // gles3
1936 } // deqp
1937