• 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::__anon53c5d24a0411::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 funtion 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 
TriangleDerivateCase(Context & context,const char * name,const char * description)621 TriangleDerivateCase::TriangleDerivateCase (Context& context, const char* name, const char* description)
622 	: TestCase			(context, name, description)
623 	, m_dataType		(glu::TYPE_LAST)
624 	, m_precision		(glu::PRECISION_LAST)
625 	, m_coordDataType	(glu::TYPE_LAST)
626 	, m_coordPrecision	(glu::PRECISION_LAST)
627 	, m_surfaceType		(SURFACETYPE_DEFAULT_FRAMEBUFFER)
628 	, m_numSamples		(0)
629 	, m_hint			(GL_DONT_CARE)
630 {
631 	DE_ASSERT(m_surfaceType != SURFACETYPE_DEFAULT_FRAMEBUFFER || m_numSamples == 0);
632 }
633 
~TriangleDerivateCase(void)634 TriangleDerivateCase::~TriangleDerivateCase (void)
635 {
636 	TriangleDerivateCase::deinit();
637 }
638 
genVertexSource(glu::DataType coordType,glu::Precision precision)639 static std::string genVertexSource (glu::DataType coordType, glu::Precision precision)
640 {
641 	DE_ASSERT(glu::isDataTypeFloatOrVec(coordType));
642 
643 	const char* vertexTmpl =
644 		"#version 300 es\n"
645 		"in highp vec4 a_position;\n"
646 		"in ${PRECISION} ${DATATYPE} a_coord;\n"
647 		"out ${PRECISION} ${DATATYPE} v_coord;\n"
648 		"void main (void)\n"
649 		"{\n"
650 		"	gl_Position = a_position;\n"
651 		"	v_coord = a_coord;\n"
652 		"}\n";
653 
654 	map<string, string> vertexParams;
655 
656 	vertexParams["PRECISION"]	= glu::getPrecisionName(precision);
657 	vertexParams["DATATYPE"]	= glu::getDataTypeName(coordType);
658 
659 	return tcu::StringTemplate(vertexTmpl).specialize(vertexParams);
660 }
661 
getViewportSize(void) const662 inline tcu::IVec2 TriangleDerivateCase::getViewportSize (void) const
663 {
664 	if (m_surfaceType == SURFACETYPE_DEFAULT_FRAMEBUFFER)
665 	{
666 		const int	width	= de::min<int>(m_context.getRenderTarget().getWidth(),	VIEWPORT_WIDTH);
667 		const int	height	= de::min<int>(m_context.getRenderTarget().getHeight(),	VIEWPORT_HEIGHT);
668 		return tcu::IVec2(width, height);
669 	}
670 	else
671 		return tcu::IVec2(FBO_WIDTH, FBO_HEIGHT);
672 }
673 
iterate(void)674 TriangleDerivateCase::IterateResult TriangleDerivateCase::iterate (void)
675 {
676 	const glw::Functions&		gl				= m_context.getRenderContext().getFunctions();
677 	const glu::ShaderProgram	program			(m_context.getRenderContext(), glu::makeVtxFragSources(genVertexSource(m_coordDataType, m_coordPrecision), m_fragmentSrc));
678 	de::Random					rnd				(deStringHash(getName()) ^ 0xbbc24);
679 	const bool					useFbo			= m_surfaceType != SURFACETYPE_DEFAULT_FRAMEBUFFER;
680 	const deUint32				fboFormat		= m_surfaceType == SURFACETYPE_FLOAT_FBO ? GL_RGBA32UI : GL_RGBA8;
681 	const tcu::IVec2			viewportSize	= getViewportSize();
682 	const int					viewportX		= useFbo ? 0 : rnd.getInt(0, m_context.getRenderTarget().getWidth()		- viewportSize.x());
683 	const int					viewportY		= useFbo ? 0 : rnd.getInt(0, m_context.getRenderTarget().getHeight()	- viewportSize.y());
684 	AutoFbo						fbo				(gl);
685 	AutoRbo						rbo				(gl);
686 	tcu::TextureLevel			result;
687 
688 	m_testCtx.getLog() << program;
689 
690 	if (!program.isOk())
691 		TCU_FAIL("Compile failed");
692 
693 	if (useFbo)
694 	{
695 		m_testCtx.getLog() << TestLog::Message
696 						   << "Rendering to FBO, format = " << glu::getTextureFormatStr(fboFormat)
697 						   << ", samples = " << m_numSamples
698 						   << TestLog::EndMessage;
699 
700 		fbo.gen();
701 		rbo.gen();
702 
703 		gl.bindRenderbuffer(GL_RENDERBUFFER, *rbo);
704 		gl.renderbufferStorageMultisample(GL_RENDERBUFFER, m_numSamples, fboFormat, viewportSize.x(), viewportSize.y());
705 		gl.bindFramebuffer(GL_FRAMEBUFFER, *fbo);
706 		gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, *rbo);
707 		TCU_CHECK(gl.checkFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
708 	}
709 	else
710 	{
711 		const tcu::PixelFormat pixelFormat = m_context.getRenderTarget().getPixelFormat();
712 
713 		m_testCtx.getLog()
714 			<< TestLog::Message
715 			<< "Rendering to default framebuffer\n"
716 			<< "\tColor depth: R=" << pixelFormat.redBits << ", G=" << pixelFormat.greenBits << ", B=" << pixelFormat.blueBits << ", A=" << pixelFormat.alphaBits
717 			<< TestLog::EndMessage;
718 	}
719 
720 	m_testCtx.getLog() << TestLog::Message << "in: " << m_coordMin << " -> " << m_coordMax << "\n"
721 										   << "v_coord.x = in.x * x\n"
722 										   << "v_coord.y = in.y * y\n"
723 										   << "v_coord.z = in.z * (x+y)/2\n"
724 										   << "v_coord.w = in.w * (1 - (x+y)/2)\n"
725 					   << TestLog::EndMessage
726 					   << TestLog::Message << "u_scale: " << m_derivScale << ", u_bias: " << m_derivBias << " (displayed values have scale/bias removed)" << TestLog::EndMessage
727 					   << TestLog::Message << "Viewport: " << viewportSize.x() << "x" << viewportSize.y() << TestLog::EndMessage
728 					   << TestLog::Message << "GL_FRAGMENT_SHADER_DERIVATE_HINT: " << glu::getHintModeStr(m_hint) << TestLog::EndMessage;
729 
730 	// Draw
731 	{
732 		const float positions[] =
733 		{
734 			-1.0f, -1.0f, 0.0f, 1.0f,
735 			-1.0f,  1.0f, 0.0f, 1.0f,
736 			 1.0f, -1.0f, 0.0f, 1.0f,
737 			 1.0f,  1.0f, 0.0f, 1.0f
738 		};
739 		const float coords[] =
740 		{
741 			m_coordMin.x(), m_coordMin.y(), m_coordMin.z(),							m_coordMax.w(),
742 			m_coordMin.x(), m_coordMax.y(), (m_coordMin.z()+m_coordMax.z())*0.5f,	(m_coordMin.w()+m_coordMax.w())*0.5f,
743 			m_coordMax.x(), m_coordMin.y(), (m_coordMin.z()+m_coordMax.z())*0.5f,	(m_coordMin.w()+m_coordMax.w())*0.5f,
744 			m_coordMax.x(), m_coordMax.y(), m_coordMax.z(),							m_coordMin.w()
745 		};
746 		const glu::VertexArrayBinding vertexArrays[] =
747 		{
748 			glu::va::Float("a_position",	4, 4, 0, &positions[0]),
749 			glu::va::Float("a_coord",		4, 4, 0, &coords[0])
750 		};
751 		const deUint16 indices[] = { 0, 2, 1, 2, 3, 1 };
752 
753 		gl.clearColor(0.125f, 0.25f, 0.5f, 1.0f);
754 		gl.clear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
755 		gl.disable(GL_DITHER);
756 
757 		gl.useProgram(program.getProgram());
758 
759 		{
760 			const int	scaleLoc	= gl.getUniformLocation(program.getProgram(), "u_scale");
761 			const int	biasLoc		= gl.getUniformLocation(program.getProgram(), "u_bias");
762 
763 			switch (m_dataType)
764 			{
765 				case glu::TYPE_FLOAT:
766 					gl.uniform1f(scaleLoc, m_derivScale.x());
767 					gl.uniform1f(biasLoc, m_derivBias.x());
768 					break;
769 
770 				case glu::TYPE_FLOAT_VEC2:
771 					gl.uniform2fv(scaleLoc, 1, m_derivScale.getPtr());
772 					gl.uniform2fv(biasLoc, 1, m_derivBias.getPtr());
773 					break;
774 
775 				case glu::TYPE_FLOAT_VEC3:
776 					gl.uniform3fv(scaleLoc, 1, m_derivScale.getPtr());
777 					gl.uniform3fv(biasLoc, 1, m_derivBias.getPtr());
778 					break;
779 
780 				case glu::TYPE_FLOAT_VEC4:
781 					gl.uniform4fv(scaleLoc, 1, m_derivScale.getPtr());
782 					gl.uniform4fv(biasLoc, 1, m_derivBias.getPtr());
783 					break;
784 
785 				default:
786 					DE_ASSERT(false);
787 			}
788 		}
789 
790 		gls::setupDefaultUniforms(m_context.getRenderContext(), program.getProgram());
791 		setupRenderState(program.getProgram());
792 
793 		gl.hint(GL_FRAGMENT_SHADER_DERIVATIVE_HINT, m_hint);
794 		GLU_EXPECT_NO_ERROR(gl.getError(), "Setup program state");
795 
796 		gl.viewport(viewportX, viewportY, viewportSize.x(), viewportSize.y());
797 		glu::draw(m_context.getRenderContext(), program.getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays), &vertexArrays[0],
798 				  glu::pr::Triangles(DE_LENGTH_OF_ARRAY(indices), &indices[0]));
799 		GLU_EXPECT_NO_ERROR(gl.getError(), "Draw");
800 	}
801 
802 	// Read back results
803 	{
804 		const bool		isMSAA		= useFbo && m_numSamples > 0;
805 		AutoFbo			resFbo		(gl);
806 		AutoRbo			resRbo		(gl);
807 
808 		// Resolve if necessary
809 		if (isMSAA)
810 		{
811 			resFbo.gen();
812 			resRbo.gen();
813 
814 			gl.bindRenderbuffer(GL_RENDERBUFFER, *resRbo);
815 			gl.renderbufferStorageMultisample(GL_RENDERBUFFER, 0, fboFormat, viewportSize.x(), viewportSize.y());
816 			gl.bindFramebuffer(GL_DRAW_FRAMEBUFFER, *resFbo);
817 			gl.framebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, *resRbo);
818 			TCU_CHECK(gl.checkFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
819 
820 			gl.blitFramebuffer(0, 0, viewportSize.x(), viewportSize.y(), 0, 0, viewportSize.x(), viewportSize.y(), GL_COLOR_BUFFER_BIT, GL_NEAREST);
821 			GLU_EXPECT_NO_ERROR(gl.getError(), "Resolve blit");
822 
823 			gl.bindFramebuffer(GL_READ_FRAMEBUFFER, *resFbo);
824 		}
825 
826 		switch (m_surfaceType)
827 		{
828 			case SURFACETYPE_DEFAULT_FRAMEBUFFER:
829 			case SURFACETYPE_UNORM_FBO:
830 				result.setStorage(tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), viewportSize.x(), viewportSize.y());
831 				glu::readPixels(m_context.getRenderContext(), viewportX, viewportY, result);
832 				break;
833 
834 			case SURFACETYPE_FLOAT_FBO:
835 			{
836 				const tcu::TextureFormat	dataFormat		(tcu::TextureFormat::RGBA, tcu::TextureFormat::FLOAT);
837 				const tcu::TextureFormat	transferFormat	(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNSIGNED_INT32);
838 
839 				result.setStorage(dataFormat, viewportSize.x(), viewportSize.y());
840 				glu::readPixels(m_context.getRenderContext(), viewportX, viewportY,
841 								tcu::PixelBufferAccess(transferFormat, result.getWidth(), result.getHeight(), result.getDepth(), result.getAccess().getDataPtr()));
842 				break;
843 			}
844 
845 			default:
846 				DE_ASSERT(false);
847 		}
848 
849 		GLU_EXPECT_NO_ERROR(gl.getError(), "Read pixels");
850 	}
851 
852 	// Verify
853 	{
854 		tcu::Surface errorMask(result.getWidth(), result.getHeight());
855 		tcu::clear(errorMask.getAccess(), tcu::RGBA::green().toVec());
856 
857 		const qpTestResult testResult = verify(result.getAccess(), errorMask.getAccess());
858 		const char* failStr = "Fail";
859 
860 		m_testCtx.getLog() << TestLog::ImageSet("Result", "Result images")
861 						   << TestLog::Image("Rendered", "Rendered image", result);
862 
863 		if (testResult != QP_TEST_RESULT_PASS)
864 			m_testCtx.getLog() << TestLog::Image("ErrorMask", "Error mask", errorMask);
865 
866 		m_testCtx.getLog() << TestLog::EndImageSet;
867 
868 		if (testResult == QP_TEST_RESULT_PASS)
869 			failStr = "Pass";
870 		else if (testResult == QP_TEST_RESULT_QUALITY_WARNING)
871 			failStr = "QualityWarning";
872 
873 		m_testCtx.setTestResult(testResult, failStr);
874 
875 	}
876 
877 	return STOP;
878 }
879 
getSurfaceThreshold(void) const880 tcu::Vec4 TriangleDerivateCase::getSurfaceThreshold (void) const
881 {
882 	switch (m_surfaceType)
883 	{
884 		case SURFACETYPE_DEFAULT_FRAMEBUFFER:
885 		{
886 			const tcu::PixelFormat	pixelFormat		= m_context.getRenderTarget().getPixelFormat();
887 			const tcu::IVec4		channelBits		(pixelFormat.redBits, pixelFormat.greenBits, pixelFormat.blueBits, pixelFormat.alphaBits);
888 			const tcu::IVec4		intThreshold	= tcu::IVec4(1) << (8 - channelBits);
889 			const tcu::Vec4			normThreshold	= intThreshold.asFloat() / 255.0f;
890 
891 			return normThreshold;
892 		}
893 
894 		case SURFACETYPE_UNORM_FBO:				return tcu::IVec4(1).asFloat() / 255.0f;
895 		case SURFACETYPE_FLOAT_FBO:				return tcu::Vec4(0.0f);
896 		default:
897 			DE_ASSERT(false);
898 			return tcu::Vec4(0.0f);
899 	}
900 }
901 
902 // ConstantDerivateCase
903 
904 class ConstantDerivateCase : public TriangleDerivateCase
905 {
906 public:
907 						ConstantDerivateCase		(Context& context, const char* name, const char* description, DerivateFunc func, glu::DataType type);
~ConstantDerivateCase(void)908 						~ConstantDerivateCase		(void) {}
909 
910 	void				init						(void);
911 
912 protected:
913 	qpTestResult		verify						(const tcu::ConstPixelBufferAccess& result, const tcu::PixelBufferAccess& errorMask);
914 
915 private:
916 	DerivateFunc		m_func;
917 };
918 
ConstantDerivateCase(Context & context,const char * name,const char * description,DerivateFunc func,glu::DataType type)919 ConstantDerivateCase::ConstantDerivateCase (Context& context, const char* name, const char* description, DerivateFunc func, glu::DataType type)
920 	: TriangleDerivateCase	(context, name, description)
921 	, m_func				(func)
922 {
923 	m_dataType			= type;
924 	m_precision			= glu::PRECISION_HIGHP;
925 	m_coordDataType		= m_dataType;
926 	m_coordPrecision	= m_precision;
927 }
928 
init(void)929 void ConstantDerivateCase::init (void)
930 {
931 	const char* fragmentTmpl =
932 		"#version 300 es\n"
933 		"layout(location = 0) out mediump vec4 o_color;\n"
934 		"uniform ${PRECISION} ${DATATYPE} u_scale;\n"
935 		"uniform ${PRECISION} ${DATATYPE} u_bias;\n"
936 		"void main (void)\n"
937 		"{\n"
938 		"	${PRECISION} ${DATATYPE} res = ${FUNC}(${VALUE}) * u_scale + u_bias;\n"
939 		"	o_color = ${CAST_TO_OUTPUT};\n"
940 		"}\n";
941 	map<string, string> fragmentParams;
942 	fragmentParams["PRECISION"]			= glu::getPrecisionName(m_precision);
943 	fragmentParams["DATATYPE"]			= glu::getDataTypeName(m_dataType);
944 	fragmentParams["FUNC"]				= getDerivateFuncName(m_func);
945 	fragmentParams["VALUE"]				= m_dataType == glu::TYPE_FLOAT_VEC4 ? "vec4(1.0, 7.2, -1e5, 0.0)" :
946 										  m_dataType == glu::TYPE_FLOAT_VEC3 ? "vec3(1e2, 8.0, 0.01)" :
947 										  m_dataType == glu::TYPE_FLOAT_VEC2 ? "vec2(-0.0, 2.7)" :
948 										  /* TYPE_FLOAT */					   "7.7";
949 	fragmentParams["CAST_TO_OUTPUT"]	= m_dataType == glu::TYPE_FLOAT_VEC4 ? "res" :
950 										  m_dataType == glu::TYPE_FLOAT_VEC3 ? "vec4(res, 1.0)" :
951 										  m_dataType == glu::TYPE_FLOAT_VEC2 ? "vec4(res, 0.0, 1.0)" :
952 										  /* TYPE_FLOAT */					   "vec4(res, 0.0, 0.0, 1.0)";
953 
954 	m_fragmentSrc = tcu::StringTemplate(fragmentTmpl).specialize(fragmentParams);
955 
956 	m_derivScale	= tcu::Vec4(1e3f, 1e3f, 1e3f, 1e3f);
957 	m_derivBias		= tcu::Vec4(0.5f, 0.5f, 0.5f, 0.5f);
958 }
959 
verify(const tcu::ConstPixelBufferAccess & result,const tcu::PixelBufferAccess & errorMask)960 qpTestResult ConstantDerivateCase::verify (const tcu::ConstPixelBufferAccess& result, const tcu::PixelBufferAccess& errorMask)
961 {
962 	const tcu::Vec4 reference	(0.0f); // Derivate of constant argument should always be 0
963 	const tcu::Vec4	threshold	= getSurfaceThreshold() / abs(m_derivScale);
964 
965 	return verifyConstantDerivate(m_testCtx.getLog(), result, errorMask, m_dataType,
966 								  reference, threshold, m_derivScale, m_derivBias);
967 }
968 
969 // LinearDerivateCase
970 
971 class LinearDerivateCase : public TriangleDerivateCase
972 {
973 public:
974 						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)975 						~LinearDerivateCase		(void) {}
976 
977 	void				init					(void);
978 
979 protected:
980 	qpTestResult		verify					(const tcu::ConstPixelBufferAccess& result, const tcu::PixelBufferAccess& errorMask);
981 
982 private:
983 	DerivateFunc		m_func;
984 	std::string			m_fragmentTmpl;
985 };
986 
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)987 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)
988 	: TriangleDerivateCase	(context, name, description)
989 	, m_func				(func)
990 	, m_fragmentTmpl		(fragmentSrcTmpl)
991 {
992 	m_dataType			= type;
993 	m_precision			= precision;
994 	m_coordDataType		= m_dataType;
995 	m_coordPrecision	= m_precision;
996 	m_hint				= hint;
997 	m_surfaceType		= surfaceType;
998 	m_numSamples		= numSamples;
999 }
1000 
init(void)1001 void LinearDerivateCase::init (void)
1002 {
1003 	const tcu::IVec2	viewportSize	= getViewportSize();
1004 	const float			w				= float(viewportSize.x());
1005 	const float			h				= float(viewportSize.y());
1006 	const bool			packToInt		= m_surfaceType == SURFACETYPE_FLOAT_FBO;
1007 	map<string, string>	fragmentParams;
1008 
1009 	fragmentParams["OUTPUT_TYPE"]		= glu::getDataTypeName(packToInt ? glu::TYPE_UINT_VEC4 : glu::TYPE_FLOAT_VEC4);
1010 	fragmentParams["OUTPUT_PREC"]		= glu::getPrecisionName(packToInt ? glu::PRECISION_HIGHP : m_precision);
1011 	fragmentParams["PRECISION"]			= glu::getPrecisionName(m_precision);
1012 	fragmentParams["DATATYPE"]			= glu::getDataTypeName(m_dataType);
1013 	fragmentParams["FUNC"]				= getDerivateFuncName(m_func);
1014 
1015 	if (packToInt)
1016 	{
1017 		fragmentParams["CAST_TO_OUTPUT"]	= m_dataType == glu::TYPE_FLOAT_VEC4 ? "floatBitsToUint(res)" :
1018 											  m_dataType == glu::TYPE_FLOAT_VEC3 ? "floatBitsToUint(vec4(res, 1.0))" :
1019 											  m_dataType == glu::TYPE_FLOAT_VEC2 ? "floatBitsToUint(vec4(res, 0.0, 1.0))" :
1020 											  /* TYPE_FLOAT */					   "floatBitsToUint(vec4(res, 0.0, 0.0, 1.0))";
1021 	}
1022 	else
1023 	{
1024 		fragmentParams["CAST_TO_OUTPUT"]	= m_dataType == glu::TYPE_FLOAT_VEC4 ? "res" :
1025 											  m_dataType == glu::TYPE_FLOAT_VEC3 ? "vec4(res, 1.0)" :
1026 											  m_dataType == glu::TYPE_FLOAT_VEC2 ? "vec4(res, 0.0, 1.0)" :
1027 											  /* TYPE_FLOAT */					   "vec4(res, 0.0, 0.0, 1.0)";
1028 	}
1029 
1030 	m_fragmentSrc = tcu::StringTemplate(m_fragmentTmpl.c_str()).specialize(fragmentParams);
1031 
1032 	switch (m_precision)
1033 	{
1034 		case glu::PRECISION_HIGHP:
1035 			m_coordMin = tcu::Vec4(-97.f, 0.2f, 71.f, 74.f);
1036 			m_coordMax = tcu::Vec4(-13.2f, -77.f, 44.f, 76.f);
1037 			break;
1038 
1039 		case glu::PRECISION_MEDIUMP:
1040 			m_coordMin = tcu::Vec4(-37.0f, 47.f, -7.f, 0.0f);
1041 			m_coordMax = tcu::Vec4(-1.0f, 12.f, 7.f, 19.f);
1042 			break;
1043 
1044 		case glu::PRECISION_LOWP:
1045 			m_coordMin = tcu::Vec4(0.0f, -1.0f, 0.0f, 1.0f);
1046 			m_coordMax = tcu::Vec4(1.0f, 1.0f, -1.0f, -1.0f);
1047 			break;
1048 
1049 		default:
1050 			DE_ASSERT(false);
1051 	}
1052 
1053 	if (m_surfaceType == SURFACETYPE_FLOAT_FBO)
1054 	{
1055 		// No scale or bias used for accuracy.
1056 		m_derivScale	= tcu::Vec4(1.0f);
1057 		m_derivBias		= tcu::Vec4(0.0f);
1058 	}
1059 	else
1060 	{
1061 		// Compute scale - bias that normalizes to 0..1 range.
1062 		const tcu::Vec4 dx = (m_coordMax - m_coordMin) / tcu::Vec4(w, w, w*0.5f, -w*0.5f);
1063 		const tcu::Vec4 dy = (m_coordMax - m_coordMin) / tcu::Vec4(h, h, h*0.5f, -h*0.5f);
1064 
1065 		switch (m_func)
1066 		{
1067 			case DERIVATE_DFDX:
1068 				m_derivScale = 0.5f / dx;
1069 				break;
1070 
1071 			case DERIVATE_DFDY:
1072 				m_derivScale = 0.5f / dy;
1073 				break;
1074 
1075 			case DERIVATE_FWIDTH:
1076 				m_derivScale = 0.5f / (tcu::abs(dx) + tcu::abs(dy));
1077 				break;
1078 
1079 			default:
1080 				DE_ASSERT(false);
1081 		}
1082 
1083 		m_derivBias = tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f);
1084 	}
1085 }
1086 
verify(const tcu::ConstPixelBufferAccess & result,const tcu::PixelBufferAccess & errorMask)1087 qpTestResult LinearDerivateCase::verify (const tcu::ConstPixelBufferAccess& result, const tcu::PixelBufferAccess& errorMask)
1088 {
1089 	const tcu::Vec4		xScale				= tcu::Vec4(1.0f, 0.0f, 0.5f, -0.5f);
1090 	const tcu::Vec4		yScale				= tcu::Vec4(0.0f, 1.0f, 0.5f, -0.5f);
1091 	const tcu::Vec4		surfaceThreshold	= getSurfaceThreshold() / abs(m_derivScale);
1092 
1093 	if (m_func == DERIVATE_DFDX || m_func == DERIVATE_DFDY)
1094 	{
1095 		const bool			isX				= m_func == DERIVATE_DFDX;
1096 		const float			div				= isX ? float(result.getWidth()) : float(result.getHeight());
1097 		const tcu::Vec4		scale			= isX ? xScale : yScale;
1098 		tcu::Vec4			reference		= ((m_coordMax - m_coordMin) / div);
1099 		const tcu::Vec4		opThreshold		= getDerivateThreshold(m_precision, m_coordMin, m_coordMax, reference);
1100 		const tcu::Vec4		opThresholdW	= getDerivateThresholdWarning(m_precision, m_coordMin, m_coordMax, reference);
1101 		const tcu::Vec4		threshold		= max(surfaceThreshold, opThreshold);
1102 		const tcu::Vec4		thresholdW		= max(surfaceThreshold, opThresholdW);
1103 		const int			numComps		= glu::getDataTypeFloatScalars(m_dataType);
1104 
1105 		/* adjust the reference value for the correct dfdx or dfdy sample adjacency */
1106 		reference	= reference * scale;
1107 
1108 		m_testCtx.getLog()
1109 			<< tcu::TestLog::Message
1110 			<< "Verifying result image.\n"
1111 			<< "\tValid derivative is " << LogVecComps(reference, numComps) << " with threshold " << LogVecComps(threshold, numComps)
1112 			<< tcu::TestLog::EndMessage;
1113 
1114 		// short circuit if result is strictly within the normal value error bounds.
1115 		// This improves performance significantly.
1116 		if (verifyConstantDerivate(m_testCtx.getLog(), result, errorMask, m_dataType,
1117 								   reference, threshold, m_derivScale, m_derivBias,
1118 								   LOG_NOTHING) == QP_TEST_RESULT_PASS)
1119 		{
1120 			m_testCtx.getLog()
1121 				<< tcu::TestLog::Message
1122 				<< "No incorrect derivatives found, result valid."
1123 				<< tcu::TestLog::EndMessage;
1124 
1125 			return QP_TEST_RESULT_PASS;
1126 		}
1127 
1128 		// Check with relaxed threshold value
1129 		if (verifyConstantDerivate(m_testCtx.getLog(), result, errorMask, m_dataType,
1130 								   reference, thresholdW, m_derivScale, m_derivBias,
1131 								   LOG_NOTHING) == QP_TEST_RESULT_PASS)
1132 		{
1133 			m_testCtx.getLog()
1134 				<< tcu::TestLog::Message
1135 				<< "No incorrect derivatives found, result valid with quality warning."
1136 				<< tcu::TestLog::EndMessage;
1137 
1138 			return QP_TEST_RESULT_QUALITY_WARNING;
1139 		}
1140 
1141 		// some pixels exceed error bounds calculated for normal values. Verify that these
1142 		// potentially invalid pixels are in fact valid due to (for example) subnorm flushing.
1143 
1144 		m_testCtx.getLog()
1145 			<< tcu::TestLog::Message
1146 			<< "Initial verification failed, verifying image by calculating accurate error bounds for each result pixel.\n"
1147 			<< "\tVerifying each result derivative is within its range of legal result values."
1148 			<< tcu::TestLog::EndMessage;
1149 
1150 		{
1151 			const tcu::IVec2			viewportSize	= getViewportSize();
1152 			const float					w				= float(viewportSize.x());
1153 			const float					h				= float(viewportSize.y());
1154 			const tcu::Vec4				valueRamp		= (m_coordMax - m_coordMin);
1155 			Linear2DFunctionEvaluator	function;
1156 
1157 			function.matrix.setRow(0, tcu::Vec3(valueRamp.x() / w, 0.0f, m_coordMin.x()));
1158 			function.matrix.setRow(1, tcu::Vec3(0.0f, valueRamp.y() / h, m_coordMin.y()));
1159 			function.matrix.setRow(2, tcu::Vec3(valueRamp.z() / w, valueRamp.z() / h, m_coordMin.z() + m_coordMin.z()) / 2.0f);
1160 			function.matrix.setRow(3, tcu::Vec3(-valueRamp.w() / w, -valueRamp.w() / h, m_coordMax.w() + m_coordMax.w()) / 2.0f);
1161 
1162 			return reverifyConstantDerivateWithFlushRelaxations(m_testCtx.getLog(), result, errorMask,
1163 																m_dataType, m_precision, m_derivScale,
1164 																m_derivBias, surfaceThreshold, m_func,
1165 																function);
1166 		}
1167 	}
1168 	else
1169 	{
1170 		DE_ASSERT(m_func == DERIVATE_FWIDTH);
1171 		const float			w				= float(result.getWidth());
1172 		const float			h				= float(result.getHeight());
1173 
1174 		const tcu::Vec4		dx				= ((m_coordMax - m_coordMin) / w) * xScale;
1175 		const tcu::Vec4		dy				= ((m_coordMax - m_coordMin) / h) * yScale;
1176 		const tcu::Vec4		reference		= tcu::abs(dx) + tcu::abs(dy);
1177 		const tcu::Vec4		dxThreshold		= getDerivateThreshold(m_precision, m_coordMin*xScale, m_coordMax*xScale, dx);
1178 		const tcu::Vec4		dyThreshold		= getDerivateThreshold(m_precision, m_coordMin*yScale, m_coordMax*yScale, dy);
1179 		const tcu::Vec4		dxThresholdW	= getDerivateThresholdWarning(m_precision, m_coordMin*xScale, m_coordMax*xScale, dx);
1180 		const tcu::Vec4		dyThresholdW	= getDerivateThresholdWarning(m_precision, m_coordMin*yScale, m_coordMax*yScale, dy);
1181 		const tcu::Vec4		threshold		= max(surfaceThreshold, max(dxThreshold, dyThreshold));
1182 		const tcu::Vec4		thresholdW		= max(surfaceThreshold, max(dxThresholdW, dyThresholdW));
1183 		qpTestResult        testResult		= QP_TEST_RESULT_FAIL;
1184 
1185 		testResult = verifyConstantDerivate(m_testCtx.getLog(), result, errorMask, m_dataType,
1186 									  reference, threshold, m_derivScale, m_derivBias);
1187 
1188 		// return if result is pass
1189 		if (testResult == QP_TEST_RESULT_PASS)
1190 			return testResult;
1191 
1192 		// re-check with relaxed threshold
1193 		testResult = verifyConstantDerivate(m_testCtx.getLog(), result, errorMask, m_dataType,
1194 									  reference, thresholdW, m_derivScale, m_derivBias);
1195 
1196 		// if with relaxed threshold test is passing then mark the result with quality warning.
1197 		if (testResult == QP_TEST_RESULT_PASS)
1198 			testResult = QP_TEST_RESULT_QUALITY_WARNING;
1199 
1200 		return testResult;
1201 	}
1202 }
1203 
1204 // TextureDerivateCase
1205 
1206 class TextureDerivateCase : public TriangleDerivateCase
1207 {
1208 public:
1209 						TextureDerivateCase		(Context& context, const char* name, const char* description, DerivateFunc func, glu::DataType type, glu::Precision precision, deUint32 hint, SurfaceType surfaceType, int numSamples);
1210 						~TextureDerivateCase	(void);
1211 
1212 	void				init					(void);
1213 	void				deinit					(void);
1214 
1215 protected:
1216 	void				setupRenderState		(deUint32 program);
1217 	qpTestResult		verify					(const tcu::ConstPixelBufferAccess& result, const tcu::PixelBufferAccess& errorMask);
1218 
1219 private:
1220 	DerivateFunc		m_func;
1221 
1222 	tcu::Vec4			m_texValueMin;
1223 	tcu::Vec4			m_texValueMax;
1224 	glu::Texture2D*		m_texture;
1225 };
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::TextureDerivateCase (Context& context, const char* name, const char* description, DerivateFunc func, glu::DataType type, glu::Precision precision, deUint32 hint, SurfaceType surfaceType, int numSamples)
1228 	: TriangleDerivateCase	(context, name, description)
1229 	, m_func				(func)
1230 	, m_texture				(DE_NULL)
1231 {
1232 	m_dataType			= type;
1233 	m_precision			= precision;
1234 	m_coordDataType		= glu::TYPE_FLOAT_VEC2;
1235 	m_coordPrecision	= glu::PRECISION_HIGHP;
1236 	m_hint				= hint;
1237 	m_surfaceType		= surfaceType;
1238 	m_numSamples		= numSamples;
1239 }
1240 
~TextureDerivateCase(void)1241 TextureDerivateCase::~TextureDerivateCase (void)
1242 {
1243 	delete m_texture;
1244 }
1245 
init(void)1246 void TextureDerivateCase::init (void)
1247 {
1248 	// Generate shader
1249 	{
1250 		const char* fragmentTmpl =
1251 			"#version 300 es\n"
1252 			"in highp vec2 v_coord;\n"
1253 			"layout(location = 0) out ${OUTPUT_PREC} ${OUTPUT_TYPE} o_color;\n"
1254 			"uniform ${PRECISION} sampler2D u_sampler;\n"
1255 			"uniform ${PRECISION} ${DATATYPE} u_scale;\n"
1256 			"uniform ${PRECISION} ${DATATYPE} u_bias;\n"
1257 			"void main (void)\n"
1258 			"{\n"
1259 			"	${PRECISION} vec4 tex = texture(u_sampler, v_coord);\n"
1260 			"	${PRECISION} ${DATATYPE} res = ${FUNC}(tex${SWIZZLE}) * u_scale + u_bias;\n"
1261 			"	o_color = ${CAST_TO_OUTPUT};\n"
1262 			"}\n";
1263 
1264 		const bool			packToInt		= m_surfaceType == SURFACETYPE_FLOAT_FBO;
1265 		map<string, string> fragmentParams;
1266 
1267 		fragmentParams["OUTPUT_TYPE"]		= glu::getDataTypeName(packToInt ? glu::TYPE_UINT_VEC4 : glu::TYPE_FLOAT_VEC4);
1268 		fragmentParams["OUTPUT_PREC"]		= glu::getPrecisionName(packToInt ? glu::PRECISION_HIGHP : m_precision);
1269 		fragmentParams["PRECISION"]			= glu::getPrecisionName(m_precision);
1270 		fragmentParams["DATATYPE"]			= glu::getDataTypeName(m_dataType);
1271 		fragmentParams["FUNC"]				= getDerivateFuncName(m_func);
1272 		fragmentParams["SWIZZLE"]			= m_dataType == glu::TYPE_FLOAT_VEC4 ? "" :
1273 											  m_dataType == glu::TYPE_FLOAT_VEC3 ? ".xyz" :
1274 											  m_dataType == glu::TYPE_FLOAT_VEC2 ? ".xy" :
1275 											  /* TYPE_FLOAT */					   ".x";
1276 
1277 		if (packToInt)
1278 		{
1279 			fragmentParams["CAST_TO_OUTPUT"]	= m_dataType == glu::TYPE_FLOAT_VEC4 ? "floatBitsToUint(res)" :
1280 												  m_dataType == glu::TYPE_FLOAT_VEC3 ? "floatBitsToUint(vec4(res, 1.0))" :
1281 												  m_dataType == glu::TYPE_FLOAT_VEC2 ? "floatBitsToUint(vec4(res, 0.0, 1.0))" :
1282 												  /* TYPE_FLOAT */					   "floatBitsToUint(vec4(res, 0.0, 0.0, 1.0))";
1283 		}
1284 		else
1285 		{
1286 			fragmentParams["CAST_TO_OUTPUT"]	= m_dataType == glu::TYPE_FLOAT_VEC4 ? "res" :
1287 												  m_dataType == glu::TYPE_FLOAT_VEC3 ? "vec4(res, 1.0)" :
1288 												  m_dataType == glu::TYPE_FLOAT_VEC2 ? "vec4(res, 0.0, 1.0)" :
1289 												  /* TYPE_FLOAT */					   "vec4(res, 0.0, 0.0, 1.0)";
1290 		}
1291 
1292 		m_fragmentSrc = tcu::StringTemplate(fragmentTmpl).specialize(fragmentParams);
1293 	}
1294 
1295 	// Texture size matches viewport and nearest sampling is used. Thus texture sampling
1296 	// is equal to just interpolating the texture value range.
1297 
1298 	// Determine value range for texture.
1299 
1300 	switch (m_precision)
1301 	{
1302 		case glu::PRECISION_HIGHP:
1303 			m_texValueMin = tcu::Vec4(-97.f, 0.2f, 71.f, 74.f);
1304 			m_texValueMax = tcu::Vec4(-13.2f, -77.f, 44.f, 76.f);
1305 			break;
1306 
1307 		case glu::PRECISION_MEDIUMP:
1308 			m_texValueMin = tcu::Vec4(-37.0f, 47.f, -7.f, 0.0f);
1309 			m_texValueMax = tcu::Vec4(-1.0f, 12.f, 7.f, 19.f);
1310 			break;
1311 
1312 		case glu::PRECISION_LOWP:
1313 			m_texValueMin = tcu::Vec4(0.0f, -1.0f, 0.0f, 1.0f);
1314 			m_texValueMax = tcu::Vec4(1.0f, 1.0f, -1.0f, -1.0f);
1315 			break;
1316 
1317 		default:
1318 			DE_ASSERT(false);
1319 	}
1320 
1321 	// Lowp and mediump cases use RGBA16F format, while highp uses RGBA32F.
1322 	{
1323 		const tcu::IVec2 viewportSize = getViewportSize();
1324 		DE_ASSERT(!m_texture);
1325 		m_texture = new glu::Texture2D(m_context.getRenderContext(), m_precision == glu::PRECISION_HIGHP ? GL_RGBA32F : GL_RGBA16F, viewportSize.x(), viewportSize.y());
1326 		m_texture->getRefTexture().allocLevel(0);
1327 	}
1328 
1329 	// Texture coordinates
1330 	m_coordMin = tcu::Vec4(0.0f);
1331 	m_coordMax = tcu::Vec4(1.0f);
1332 
1333 	// Fill with gradients.
1334 	{
1335 		const tcu::PixelBufferAccess level0 = m_texture->getRefTexture().getLevel(0);
1336 		for (int y = 0; y < level0.getHeight(); y++)
1337 		{
1338 			for (int x = 0; x < level0.getWidth(); x++)
1339 			{
1340 				const float		xf		= (float(x)+0.5f) / float(level0.getWidth());
1341 				const float		yf		= (float(y)+0.5f) / float(level0.getHeight());
1342 				const tcu::Vec4	s		= tcu::Vec4(xf, yf, (xf+yf)/2.0f, 1.0f - (xf+yf)/2.0f);
1343 
1344 				level0.setPixel(m_texValueMin + (m_texValueMax - m_texValueMin)*s, x, y);
1345 			}
1346 		}
1347 	}
1348 
1349 	m_texture->upload();
1350 
1351 	if (m_surfaceType == SURFACETYPE_FLOAT_FBO)
1352 	{
1353 		// No scale or bias used for accuracy.
1354 		m_derivScale	= tcu::Vec4(1.0f);
1355 		m_derivBias		= tcu::Vec4(0.0f);
1356 	}
1357 	else
1358 	{
1359 		// Compute scale - bias that normalizes to 0..1 range.
1360 		const tcu::IVec2	viewportSize	= getViewportSize();
1361 		const float			w				= float(viewportSize.x());
1362 		const float			h				= float(viewportSize.y());
1363 		const tcu::Vec4		dx				= (m_texValueMax - m_texValueMin) / tcu::Vec4(w, w, w*0.5f, -w*0.5f);
1364 		const tcu::Vec4		dy				= (m_texValueMax - m_texValueMin) / tcu::Vec4(h, h, h*0.5f, -h*0.5f);
1365 
1366 		switch (m_func)
1367 		{
1368 			case DERIVATE_DFDX:
1369 				m_derivScale = 0.5f / dx;
1370 				break;
1371 
1372 			case DERIVATE_DFDY:
1373 				m_derivScale = 0.5f / dy;
1374 				break;
1375 
1376 			case DERIVATE_FWIDTH:
1377 				m_derivScale = 0.5f / (tcu::abs(dx) + tcu::abs(dy));
1378 				break;
1379 
1380 			default:
1381 				DE_ASSERT(false);
1382 		}
1383 
1384 		m_derivBias = tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f);
1385 	}
1386 }
1387 
deinit(void)1388 void TextureDerivateCase::deinit (void)
1389 {
1390 	delete m_texture;
1391 	m_texture = DE_NULL;
1392 }
1393 
setupRenderState(deUint32 program)1394 void TextureDerivateCase::setupRenderState (deUint32 program)
1395 {
1396 	const glw::Functions&	gl			= m_context.getRenderContext().getFunctions();
1397 	const int				texUnit		= 1;
1398 
1399 	gl.activeTexture	(GL_TEXTURE0+texUnit);
1400 	gl.bindTexture		(GL_TEXTURE_2D, m_texture->getGLTexture());
1401 	gl.texParameteri	(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,	GL_NEAREST);
1402 	gl.texParameteri	(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,	GL_NEAREST);
1403 	gl.texParameteri	(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,		GL_CLAMP_TO_EDGE);
1404 	gl.texParameteri	(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,		GL_CLAMP_TO_EDGE);
1405 
1406 	gl.uniform1i		(gl.getUniformLocation(program, "u_sampler"), texUnit);
1407 }
1408 
verify(const tcu::ConstPixelBufferAccess & result,const tcu::PixelBufferAccess & errorMask)1409 qpTestResult TextureDerivateCase::verify (const tcu::ConstPixelBufferAccess& result, const tcu::PixelBufferAccess& errorMask)
1410 {
1411 	// \note Edges are ignored in comparison
1412 	if (result.getWidth() < 2 || result.getHeight() < 2)
1413 		throw tcu::NotSupportedError("Too small viewport");
1414 
1415 	tcu::ConstPixelBufferAccess	compareArea			= tcu::getSubregion(result, 1, 1, result.getWidth()-2, result.getHeight()-2);
1416 	tcu::PixelBufferAccess		maskArea			= tcu::getSubregion(errorMask, 1, 1, errorMask.getWidth()-2, errorMask.getHeight()-2);
1417 	const tcu::Vec4				xScale				= tcu::Vec4(1.0f, 0.0f, 0.5f, -0.5f);
1418 	const tcu::Vec4				yScale				= tcu::Vec4(0.0f, 1.0f, 0.5f, -0.5f);
1419 	const float					w					= float(result.getWidth());
1420 	const float					h					= float(result.getHeight());
1421 
1422 	const tcu::Vec4				surfaceThreshold	= getSurfaceThreshold() / abs(m_derivScale);
1423 
1424 	if (m_func == DERIVATE_DFDX || m_func == DERIVATE_DFDY)
1425 	{
1426 		const bool			isX				= m_func == DERIVATE_DFDX;
1427 		const float			div				= isX ? w : h;
1428 		const tcu::Vec4		scale			= isX ? xScale : yScale;
1429 		tcu::Vec4			reference		= ((m_texValueMax - m_texValueMin) / div);
1430 		const tcu::Vec4		opThreshold		= getDerivateThreshold(m_precision, m_texValueMin, m_texValueMax, reference);
1431 		const tcu::Vec4		opThresholdW	= getDerivateThresholdWarning(m_precision, m_texValueMin, m_texValueMax, reference);
1432 		const tcu::Vec4		threshold		= max(surfaceThreshold, opThreshold);
1433 		const tcu::Vec4		thresholdW		= max(surfaceThreshold, opThresholdW);
1434 		const int			numComps		= glu::getDataTypeFloatScalars(m_dataType);
1435 
1436 		/* adjust the reference value for the correct dfdx or dfdy sample adjacency */
1437 		reference	= reference * scale;
1438 
1439 		m_testCtx.getLog()
1440 			<< tcu::TestLog::Message
1441 			<< "Verifying result image.\n"
1442 			<< "\tValid derivative is " << LogVecComps(reference, numComps) << " with threshold " << LogVecComps(threshold, numComps)
1443 			<< tcu::TestLog::EndMessage;
1444 
1445 		// short circuit if result is strictly within the normal value error bounds.
1446 		// This improves performance significantly.
1447 		if (verifyConstantDerivate(m_testCtx.getLog(), compareArea, maskArea, m_dataType,
1448 								   reference, threshold, m_derivScale, m_derivBias,
1449 								   LOG_NOTHING) == QP_TEST_RESULT_PASS)
1450 		{
1451 			m_testCtx.getLog()
1452 				<< tcu::TestLog::Message
1453 				<< "No incorrect derivatives found, result valid."
1454 				<< tcu::TestLog::EndMessage;
1455 
1456 			return QP_TEST_RESULT_PASS;
1457 		}
1458 
1459 		m_testCtx.getLog()
1460 			<< tcu::TestLog::Message
1461 			<< "Verifying result image.\n"
1462 			<< "\tValid derivative is " << LogVecComps(reference, numComps) << " with Warning threshold " << LogVecComps(thresholdW, numComps)
1463 			<< tcu::TestLog::EndMessage;
1464 
1465 		// Re-check with relaxed threshold
1466 		if (verifyConstantDerivate(m_testCtx.getLog(), compareArea, maskArea, m_dataType,
1467 								   reference, thresholdW, m_derivScale, m_derivBias,
1468 								   LOG_NOTHING) == QP_TEST_RESULT_PASS)
1469 		{
1470 			m_testCtx.getLog()
1471 				<< tcu::TestLog::Message
1472 				<< "No incorrect derivatives found, result valid with quality warning."
1473 				<< tcu::TestLog::EndMessage;
1474 
1475 			return QP_TEST_RESULT_QUALITY_WARNING;
1476 		}
1477 
1478 
1479 		// some pixels exceed error bounds calculated for normal values. Verify that these
1480 		// potentially invalid pixels are in fact valid due to (for example) subnorm flushing.
1481 
1482 		m_testCtx.getLog()
1483 			<< tcu::TestLog::Message
1484 			<< "Initial verification failed, verifying image by calculating accurate error bounds for each result pixel.\n"
1485 			<< "\tVerifying each result derivative is within its range of legal result values."
1486 			<< tcu::TestLog::EndMessage;
1487 
1488 		{
1489 			const tcu::Vec4				valueRamp		= (m_texValueMax - m_texValueMin);
1490 			Linear2DFunctionEvaluator	function;
1491 
1492 			function.matrix.setRow(0, tcu::Vec3(valueRamp.x() / w, 0.0f, m_texValueMin.x()));
1493 			function.matrix.setRow(1, tcu::Vec3(0.0f, valueRamp.y() / h, m_texValueMin.y()));
1494 			function.matrix.setRow(2, tcu::Vec3(valueRamp.z() / w, valueRamp.z() / h, m_texValueMin.z() + m_texValueMin.z()) / 2.0f);
1495 			function.matrix.setRow(3, tcu::Vec3(-valueRamp.w() / w, -valueRamp.w() / h, m_texValueMax.w() + m_texValueMax.w()) / 2.0f);
1496 
1497 			return reverifyConstantDerivateWithFlushRelaxations(m_testCtx.getLog(), compareArea, maskArea,
1498 																m_dataType, m_precision, m_derivScale,
1499 																m_derivBias, surfaceThreshold, m_func,
1500 																function);
1501 		}
1502 	}
1503 	else
1504 	{
1505 		DE_ASSERT(m_func == DERIVATE_FWIDTH);
1506 		const tcu::Vec4	dx				= ((m_texValueMax - m_texValueMin) / w) * xScale;
1507 		const tcu::Vec4	dy				= ((m_texValueMax - m_texValueMin) / h) * yScale;
1508 		const tcu::Vec4	reference		= tcu::abs(dx) + tcu::abs(dy);
1509 		const tcu::Vec4	dxThreshold		= getDerivateThreshold(m_precision, m_texValueMin*xScale, m_texValueMax*xScale, dx);
1510 		const tcu::Vec4	dyThreshold		= getDerivateThreshold(m_precision, m_texValueMin*yScale, m_texValueMax*yScale, dy);
1511 		const tcu::Vec4	dxThresholdW	= getDerivateThresholdWarning(m_precision, m_texValueMin*xScale, m_texValueMax*xScale, dx);
1512 		const tcu::Vec4	dyThresholdW	= getDerivateThresholdWarning(m_precision, m_texValueMin*yScale, m_texValueMax*yScale, dy);
1513 		const tcu::Vec4	threshold		= max(surfaceThreshold, max(dxThreshold, dyThreshold));
1514 		const tcu::Vec4	thresholdW		= max(surfaceThreshold, max(dxThresholdW, dyThresholdW));
1515 		qpTestResult	testResult		= QP_TEST_RESULT_FAIL;
1516 
1517 		testResult = verifyConstantDerivate(m_testCtx.getLog(), compareArea, maskArea, m_dataType,
1518 									  reference, threshold, m_derivScale, m_derivBias);
1519 
1520 		if (testResult == QP_TEST_RESULT_PASS)
1521 			return testResult;
1522 
1523 		// Re-Check with relaxed threshold
1524 		testResult = verifyConstantDerivate(m_testCtx.getLog(), compareArea, maskArea, m_dataType,
1525 									  reference, thresholdW, m_derivScale, m_derivBias);
1526 
1527 		// If test is passing with relaxed threshold then mark quality warning
1528 		if (testResult == QP_TEST_RESULT_PASS)
1529 			testResult = QP_TEST_RESULT_QUALITY_WARNING;
1530 
1531 		return testResult;
1532 	}
1533 }
1534 
ShaderDerivateTests(Context & context)1535 ShaderDerivateTests::ShaderDerivateTests (Context& context)
1536 	: TestCaseGroup(context, "derivate", "Derivate Function Tests")
1537 {
1538 }
1539 
~ShaderDerivateTests(void)1540 ShaderDerivateTests::~ShaderDerivateTests (void)
1541 {
1542 }
1543 
1544 struct FunctionSpec
1545 {
1546 	std::string		name;
1547 	DerivateFunc	function;
1548 	glu::DataType	dataType;
1549 	glu::Precision	precision;
1550 
FunctionSpecdeqp::gles3::Functional::FunctionSpec1551 	FunctionSpec (const std::string& name_, DerivateFunc function_, glu::DataType dataType_, glu::Precision precision_)
1552 		: name		(name_)
1553 		, function	(function_)
1554 		, dataType	(dataType_)
1555 		, precision	(precision_)
1556 	{
1557 	}
1558 };
1559 
init(void)1560 void ShaderDerivateTests::init (void)
1561 {
1562 	static const struct
1563 	{
1564 		const char*		name;
1565 		const char*		description;
1566 		const char*		source;
1567 	} s_linearDerivateCases[] =
1568 	{
1569 		{
1570 			"linear",
1571 			"Basic derivate of linearly interpolated argument",
1572 
1573 			"#version 300 es\n"
1574 			"in ${PRECISION} ${DATATYPE} v_coord;\n"
1575 			"layout(location = 0) out ${OUTPUT_PREC} ${OUTPUT_TYPE} o_color;\n"
1576 			"uniform ${PRECISION} ${DATATYPE} u_scale;\n"
1577 			"uniform ${PRECISION} ${DATATYPE} u_bias;\n"
1578 			"void main (void)\n"
1579 			"{\n"
1580 			"	${PRECISION} ${DATATYPE} res = ${FUNC}(v_coord) * u_scale + u_bias;\n"
1581 			"	o_color = ${CAST_TO_OUTPUT};\n"
1582 			"}\n"
1583 		},
1584 		{
1585 			"in_function",
1586 			"Derivate of linear function argument",
1587 
1588 			"#version 300 es\n"
1589 			"in ${PRECISION} ${DATATYPE} v_coord;\n"
1590 			"layout(location = 0) out ${OUTPUT_PREC} ${OUTPUT_TYPE} o_color;\n"
1591 			"uniform ${PRECISION} ${DATATYPE} u_scale;\n"
1592 			"uniform ${PRECISION} ${DATATYPE} u_bias;\n"
1593 			"\n"
1594 			"${PRECISION} ${DATATYPE} computeRes (${PRECISION} ${DATATYPE} value)\n"
1595 			"{\n"
1596 			"	return ${FUNC}(v_coord) * u_scale + u_bias;\n"
1597 			"}\n"
1598 			"\n"
1599 			"void main (void)\n"
1600 			"{\n"
1601 			"	${PRECISION} ${DATATYPE} res = computeRes(v_coord);\n"
1602 			"	o_color = ${CAST_TO_OUTPUT};\n"
1603 			"}\n"
1604 		},
1605 		{
1606 			"static_if",
1607 			"Derivate of linearly interpolated value in static if",
1608 
1609 			"#version 300 es\n"
1610 			"in ${PRECISION} ${DATATYPE} v_coord;\n"
1611 			"layout(location = 0) out ${OUTPUT_PREC} ${OUTPUT_TYPE} o_color;\n"
1612 			"uniform ${PRECISION} ${DATATYPE} u_scale;\n"
1613 			"uniform ${PRECISION} ${DATATYPE} u_bias;\n"
1614 			"void main (void)\n"
1615 			"{\n"
1616 			"	${PRECISION} ${DATATYPE} res;\n"
1617 			"	if (false)\n"
1618 			"		res = ${FUNC}(-v_coord) * u_scale + u_bias;\n"
1619 			"	else\n"
1620 			"		res = ${FUNC}(v_coord) * u_scale + u_bias;\n"
1621 			"	o_color = ${CAST_TO_OUTPUT};\n"
1622 			"}\n"
1623 		},
1624 		{
1625 			"static_loop",
1626 			"Derivate of linearly interpolated value in static loop",
1627 
1628 			"#version 300 es\n"
1629 			"in ${PRECISION} ${DATATYPE} v_coord;\n"
1630 			"layout(location = 0) out ${OUTPUT_PREC} ${OUTPUT_TYPE} o_color;\n"
1631 			"uniform ${PRECISION} ${DATATYPE} u_scale;\n"
1632 			"uniform ${PRECISION} ${DATATYPE} u_bias;\n"
1633 			"void main (void)\n"
1634 			"{\n"
1635 			"	${PRECISION} ${DATATYPE} res = ${DATATYPE}(0.0);\n"
1636 			"	for (int i = 0; i < 2; i++)\n"
1637 			"		res += ${FUNC}(v_coord * float(i));\n"
1638 			"	res = res * u_scale + u_bias;\n"
1639 			"	o_color = ${CAST_TO_OUTPUT};\n"
1640 			"}\n"
1641 		},
1642 		{
1643 			"static_switch",
1644 			"Derivate of linearly interpolated value in static switch",
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;\n"
1654 			"	switch (1)\n"
1655 			"	{\n"
1656 			"		case 0:	res = ${FUNC}(-v_coord) * u_scale + u_bias;	break;\n"
1657 			"		case 1:	res = ${FUNC}(v_coord) * u_scale + u_bias;	break;\n"
1658 			"	}\n"
1659 			"	o_color = ${CAST_TO_OUTPUT};\n"
1660 			"}\n"
1661 		},
1662 		{
1663 			"uniform_if",
1664 			"Derivate of linearly interpolated value in uniform if",
1665 
1666 			"#version 300 es\n"
1667 			"in ${PRECISION} ${DATATYPE} v_coord;\n"
1668 			"layout(location = 0) out ${OUTPUT_PREC} ${OUTPUT_TYPE} o_color;\n"
1669 			"uniform ${PRECISION} ${DATATYPE} u_scale;\n"
1670 			"uniform ${PRECISION} ${DATATYPE} u_bias;\n"
1671 			"uniform bool ub_true;\n"
1672 			"void main (void)\n"
1673 			"{\n"
1674 			"	${PRECISION} ${DATATYPE} res;\n"
1675 			"	if (ub_true)"
1676 			"		res = ${FUNC}(v_coord) * u_scale + u_bias;\n"
1677 			"	else\n"
1678 			"		res = ${FUNC}(-v_coord) * u_scale + u_bias;\n"
1679 			"	o_color = ${CAST_TO_OUTPUT};\n"
1680 			"}\n"
1681 		},
1682 		{
1683 			"uniform_loop",
1684 			"Derivate of linearly interpolated value in uniform loop",
1685 
1686 			"#version 300 es\n"
1687 			"in ${PRECISION} ${DATATYPE} v_coord;\n"
1688 			"layout(location = 0) out ${OUTPUT_PREC} ${OUTPUT_TYPE} o_color;\n"
1689 			"uniform ${PRECISION} ${DATATYPE} u_scale;\n"
1690 			"uniform ${PRECISION} ${DATATYPE} u_bias;\n"
1691 			"uniform int ui_two;\n"
1692 			"void main (void)\n"
1693 			"{\n"
1694 			"	${PRECISION} ${DATATYPE} res = ${DATATYPE}(0.0);\n"
1695 			"	for (int i = 0; i < ui_two; i++)\n"
1696 			"		res += ${FUNC}(v_coord * float(i));\n"
1697 			"	res = res * u_scale + u_bias;\n"
1698 			"	o_color = ${CAST_TO_OUTPUT};\n"
1699 			"}\n"
1700 		},
1701 		{
1702 			"uniform_switch",
1703 			"Derivate of linearly interpolated value in uniform switch",
1704 
1705 			"#version 300 es\n"
1706 			"in ${PRECISION} ${DATATYPE} v_coord;\n"
1707 			"layout(location = 0) out ${OUTPUT_PREC} ${OUTPUT_TYPE} o_color;\n"
1708 			"uniform ${PRECISION} ${DATATYPE} u_scale;\n"
1709 			"uniform ${PRECISION} ${DATATYPE} u_bias;\n"
1710 			"uniform int ui_one;\n"
1711 			"void main (void)\n"
1712 			"{\n"
1713 			"	${PRECISION} ${DATATYPE} res;\n"
1714 			"	switch (ui_one)\n"
1715 			"	{\n"
1716 			"		case 0:	res = ${FUNC}(-v_coord) * u_scale + u_bias;	break;\n"
1717 			"		case 1:	res = ${FUNC}(v_coord) * u_scale + u_bias;	break;\n"
1718 			"	}\n"
1719 			"	o_color = ${CAST_TO_OUTPUT};\n"
1720 			"}\n"
1721 		},
1722 	};
1723 
1724 	static const struct
1725 	{
1726 		const char*		name;
1727 		SurfaceType		surfaceType;
1728 		int				numSamples;
1729 	} s_fboConfigs[] =
1730 	{
1731 		{ "fbo",		SURFACETYPE_DEFAULT_FRAMEBUFFER,	0 },
1732 		{ "fbo_msaa2",	SURFACETYPE_UNORM_FBO,				2 },
1733 		{ "fbo_msaa4",	SURFACETYPE_UNORM_FBO,				4 },
1734 		{ "fbo_float",	SURFACETYPE_FLOAT_FBO,				0 },
1735 	};
1736 
1737 	static const struct
1738 	{
1739 		const char*		name;
1740 		deUint32		hint;
1741 	} s_hints[] =
1742 	{
1743 		{ "fastest",	GL_FASTEST	},
1744 		{ "nicest",		GL_NICEST	},
1745 	};
1746 
1747 	static const struct
1748 	{
1749 		const char*		name;
1750 		SurfaceType		surfaceType;
1751 		int				numSamples;
1752 	} s_hintFboConfigs[] =
1753 	{
1754 		{ "default",		SURFACETYPE_DEFAULT_FRAMEBUFFER,	0 },
1755 		{ "fbo_msaa4",		SURFACETYPE_UNORM_FBO,				4 },
1756 		{ "fbo_float",		SURFACETYPE_FLOAT_FBO,				0 }
1757 	};
1758 
1759 	static const struct
1760 	{
1761 		const char*		name;
1762 		SurfaceType		surfaceType;
1763 		int				numSamples;
1764 		deUint32		hint;
1765 	} s_textureConfigs[] =
1766 	{
1767 		{ "basic",			SURFACETYPE_DEFAULT_FRAMEBUFFER,	0,	GL_DONT_CARE	},
1768 		{ "msaa4",			SURFACETYPE_UNORM_FBO,				4,	GL_DONT_CARE	},
1769 		{ "float_fastest",	SURFACETYPE_FLOAT_FBO,				0,	GL_FASTEST		},
1770 		{ "float_nicest",	SURFACETYPE_FLOAT_FBO,				0,	GL_NICEST		},
1771 	};
1772 
1773 	// .dfdx, .dfdy, .fwidth
1774 	for (int funcNdx = 0; funcNdx < DERIVATE_LAST; funcNdx++)
1775 	{
1776 		const DerivateFunc			function		= DerivateFunc(funcNdx);
1777 		tcu::TestCaseGroup* const	functionGroup	= new tcu::TestCaseGroup(m_testCtx, getDerivateFuncCaseName(function), getDerivateFuncName(function));
1778 		addChild(functionGroup);
1779 
1780 		// .constant - no precision variants, checks that derivate of constant arguments is 0
1781 		{
1782 			tcu::TestCaseGroup* const constantGroup = new tcu::TestCaseGroup(m_testCtx, "constant", "Derivate of constant argument");
1783 			functionGroup->addChild(constantGroup);
1784 
1785 			for (int vecSize = 1; vecSize <= 4; vecSize++)
1786 			{
1787 				const glu::DataType dataType = vecSize > 1 ? glu::getDataTypeFloatVec(vecSize) : glu::TYPE_FLOAT;
1788 				constantGroup->addChild(new ConstantDerivateCase(m_context, glu::getDataTypeName(dataType), "", function, dataType));
1789 			}
1790 		}
1791 
1792 		// Cases based on LinearDerivateCase
1793 		for (int caseNdx = 0; caseNdx < DE_LENGTH_OF_ARRAY(s_linearDerivateCases); caseNdx++)
1794 		{
1795 			tcu::TestCaseGroup* const linearCaseGroup	= new tcu::TestCaseGroup(m_testCtx, s_linearDerivateCases[caseNdx].name, s_linearDerivateCases[caseNdx].description);
1796 			const char*			source					= s_linearDerivateCases[caseNdx].source;
1797 			functionGroup->addChild(linearCaseGroup);
1798 
1799 			for (int vecSize = 1; vecSize <= 4; vecSize++)
1800 			{
1801 				for (int precNdx = 0; precNdx < glu::PRECISION_LAST; precNdx++)
1802 				{
1803 					const glu::DataType		dataType		= vecSize > 1 ? glu::getDataTypeFloatVec(vecSize) : glu::TYPE_FLOAT;
1804 					const glu::Precision	precision		= glu::Precision(precNdx);
1805 					const SurfaceType		surfaceType		= SURFACETYPE_DEFAULT_FRAMEBUFFER;
1806 					const int				numSamples		= 0;
1807 					const deUint32			hint			= GL_DONT_CARE;
1808 					ostringstream			caseName;
1809 
1810 					if (caseNdx != 0 && precision == glu::PRECISION_LOWP)
1811 						continue; // Skip as lowp doesn't actually produce any bits when rendered to default FB.
1812 
1813 					caseName << glu::getDataTypeName(dataType) << "_" << glu::getPrecisionName(precision);
1814 
1815 					linearCaseGroup->addChild(new LinearDerivateCase(m_context, caseName.str().c_str(), "", function, dataType, precision, hint, surfaceType, numSamples, source));
1816 				}
1817 			}
1818 		}
1819 
1820 		// Fbo cases
1821 		for (int caseNdx = 0; caseNdx < DE_LENGTH_OF_ARRAY(s_fboConfigs); caseNdx++)
1822 		{
1823 			tcu::TestCaseGroup*	const	fboGroup		= new tcu::TestCaseGroup(m_testCtx, s_fboConfigs[caseNdx].name, "Derivate usage when rendering into FBO");
1824 			const char*					source			= s_linearDerivateCases[0].source; // use source from .linear group
1825 			const SurfaceType			surfaceType		= s_fboConfigs[caseNdx].surfaceType;
1826 			const int					numSamples		= s_fboConfigs[caseNdx].numSamples;
1827 			functionGroup->addChild(fboGroup);
1828 
1829 			for (int vecSize = 1; vecSize <= 4; vecSize++)
1830 			{
1831 				for (int precNdx = 0; precNdx < glu::PRECISION_LAST; precNdx++)
1832 				{
1833 					const glu::DataType		dataType		= vecSize > 1 ? glu::getDataTypeFloatVec(vecSize) : glu::TYPE_FLOAT;
1834 					const glu::Precision	precision		= glu::Precision(precNdx);
1835 					const deUint32			hint			= GL_DONT_CARE;
1836 					ostringstream			caseName;
1837 
1838 					if (surfaceType != SURFACETYPE_FLOAT_FBO && precision == glu::PRECISION_LOWP)
1839 						continue; // Skip as lowp doesn't actually produce any bits when rendered to U8 RT.
1840 
1841 					caseName << glu::getDataTypeName(dataType) << "_" << glu::getPrecisionName(precision);
1842 
1843 					fboGroup->addChild(new LinearDerivateCase(m_context, caseName.str().c_str(), "", function, dataType, precision, hint, surfaceType, numSamples, source));
1844 				}
1845 			}
1846 		}
1847 
1848 		// .fastest, .nicest
1849 		for (int hintCaseNdx = 0; hintCaseNdx < DE_LENGTH_OF_ARRAY(s_hints); hintCaseNdx++)
1850 		{
1851 			tcu::TestCaseGroup* const	hintGroup		= new tcu::TestCaseGroup(m_testCtx, s_hints[hintCaseNdx].name, "Shader derivate hints");
1852 			const char*					source			= s_linearDerivateCases[0].source; // use source from .linear group
1853 			const deUint32				hint			= s_hints[hintCaseNdx].hint;
1854 			functionGroup->addChild(hintGroup);
1855 
1856 			for (int fboCaseNdx = 0; fboCaseNdx < DE_LENGTH_OF_ARRAY(s_hintFboConfigs); fboCaseNdx++)
1857 			{
1858 				tcu::TestCaseGroup*	const	fboGroup		= new tcu::TestCaseGroup(m_testCtx, s_hintFboConfigs[fboCaseNdx].name, "");
1859 				const SurfaceType			surfaceType		= s_hintFboConfigs[fboCaseNdx].surfaceType;
1860 				const int					numSamples		= s_hintFboConfigs[fboCaseNdx].numSamples;
1861 				hintGroup->addChild(fboGroup);
1862 
1863 				for (int vecSize = 1; vecSize <= 4; vecSize++)
1864 				{
1865 					for (int precNdx = 0; precNdx < glu::PRECISION_LAST; precNdx++)
1866 					{
1867 						const glu::DataType		dataType		= vecSize > 1 ? glu::getDataTypeFloatVec(vecSize) : glu::TYPE_FLOAT;
1868 						const glu::Precision	precision		= glu::Precision(precNdx);
1869 						ostringstream			caseName;
1870 
1871 						if (surfaceType != SURFACETYPE_FLOAT_FBO && precision == glu::PRECISION_LOWP)
1872 							continue; // Skip as lowp doesn't actually produce any bits when rendered to U8 RT.
1873 
1874 						caseName << glu::getDataTypeName(dataType) << "_" << glu::getPrecisionName(precision);
1875 
1876 						fboGroup->addChild(new LinearDerivateCase(m_context, caseName.str().c_str(), "", function, dataType, precision, hint, surfaceType, numSamples, source));
1877 					}
1878 				}
1879 			}
1880 		}
1881 
1882 		// .texture
1883 		{
1884 			tcu::TestCaseGroup* const textureGroup = new tcu::TestCaseGroup(m_testCtx, "texture", "Derivate of texture lookup result");
1885 			functionGroup->addChild(textureGroup);
1886 
1887 			for (int texCaseNdx = 0; texCaseNdx < DE_LENGTH_OF_ARRAY(s_textureConfigs); texCaseNdx++)
1888 			{
1889 				tcu::TestCaseGroup*	const	caseGroup		= new tcu::TestCaseGroup(m_testCtx, s_textureConfigs[texCaseNdx].name, "");
1890 				const SurfaceType			surfaceType		= s_textureConfigs[texCaseNdx].surfaceType;
1891 				const int					numSamples		= s_textureConfigs[texCaseNdx].numSamples;
1892 				const deUint32				hint			= s_textureConfigs[texCaseNdx].hint;
1893 				textureGroup->addChild(caseGroup);
1894 
1895 				for (int vecSize = 1; vecSize <= 4; vecSize++)
1896 				{
1897 					for (int precNdx = 0; precNdx < glu::PRECISION_LAST; precNdx++)
1898 					{
1899 						const glu::DataType		dataType		= vecSize > 1 ? glu::getDataTypeFloatVec(vecSize) : glu::TYPE_FLOAT;
1900 						const glu::Precision	precision		= glu::Precision(precNdx);
1901 						ostringstream			caseName;
1902 
1903 						if (surfaceType != SURFACETYPE_FLOAT_FBO && precision == glu::PRECISION_LOWP)
1904 							continue; // Skip as lowp doesn't actually produce any bits when rendered to U8 RT.
1905 
1906 						caseName << glu::getDataTypeName(dataType) << "_" << glu::getPrecisionName(precision);
1907 
1908 						caseGroup->addChild(new TextureDerivateCase(m_context, caseName.str().c_str(), "", function, dataType, precision, hint, surfaceType, numSamples));
1909 					}
1910 				}
1911 			}
1912 		}
1913 	}
1914 }
1915 
1916 } // Functional
1917 } // gles3
1918 } // deqp
1919