Home | History | Annotate | Download | only in glshared
      1 /*-------------------------------------------------------------------------
      2  * drawElements Quality Program OpenGL (ES) 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 execute test.
     22  *
     23  * \todo [petri] Multiple grid with differing constants/uniforms.
     24  * \todo [petri]
     25  *//*--------------------------------------------------------------------*/
     26 
     27 #include "glsShaderRenderCase.hpp"
     28 
     29 #include "tcuSurface.hpp"
     30 #include "tcuVector.hpp"
     31 #include "tcuImageCompare.hpp"
     32 #include "tcuTestLog.hpp"
     33 #include "tcuRenderTarget.hpp"
     34 
     35 #include "gluPixelTransfer.hpp"
     36 #include "gluTexture.hpp"
     37 #include "gluTextureUtil.hpp"
     38 #include "gluDrawUtil.hpp"
     39 
     40 #include "glwFunctions.hpp"
     41 #include "glwEnums.hpp"
     42 
     43 #include "deRandom.hpp"
     44 #include "deMemory.h"
     45 #include "deString.h"
     46 #include "deMath.h"
     47 #include "deStringUtil.hpp"
     48 
     49 #include <stdio.h>
     50 #include <vector>
     51 #include <string>
     52 
     53 namespace deqp
     54 {
     55 namespace gls
     56 {
     57 
     58 using namespace std;
     59 using namespace tcu;
     60 using namespace glu;
     61 
     62 static const int			GRID_SIZE				= 64;
     63 static const int			MAX_RENDER_WIDTH		= 128;
     64 static const int			MAX_RENDER_HEIGHT		= 112;
     65 static const tcu::Vec4		DEFAULT_CLEAR_COLOR		= tcu::Vec4(0.125f, 0.25f, 0.5f, 1.0f);
     66 
     67 inline RGBA toRGBA (const Vec4& a)
     68 {
     69 	return RGBA(deClamp32(deRoundFloatToInt32(a.x() * 255.0f), 0, 255),
     70 				deClamp32(deRoundFloatToInt32(a.y() * 255.0f), 0, 255),
     71 				deClamp32(deRoundFloatToInt32(a.z() * 255.0f), 0, 255),
     72 				deClamp32(deRoundFloatToInt32(a.w() * 255.0f), 0, 255));
     73 }
     74 
     75 inline tcu::Vec4 toVec (const RGBA& c)
     76 {
     77 	return tcu::Vec4(c.getRed()		/ 255.0f,
     78 					 c.getGreen()	/ 255.0f,
     79 					 c.getBlue()	/ 255.0f,
     80 					 c.getAlpha()	/ 255.0f);
     81 }
     82 
     83 // TextureBinding
     84 
     85 TextureBinding::TextureBinding (const glu::Texture2D* tex2D, const tcu::Sampler& sampler)
     86 	: m_type	(TYPE_2D)
     87 	, m_sampler	(sampler)
     88 {
     89 	m_binding.tex2D = tex2D;
     90 }
     91 
     92 TextureBinding::TextureBinding (const glu::TextureCube* texCube, const tcu::Sampler& sampler)
     93 	: m_type	(TYPE_CUBE_MAP)
     94 	, m_sampler	(sampler)
     95 {
     96 	m_binding.texCube = texCube;
     97 }
     98 
     99 TextureBinding::TextureBinding (const glu::Texture2DArray* tex2DArray, const tcu::Sampler& sampler)
    100 	: m_type	(TYPE_2D_ARRAY)
    101 	, m_sampler	(sampler)
    102 {
    103 	m_binding.tex2DArray = tex2DArray;
    104 }
    105 
    106 TextureBinding::TextureBinding (const glu::Texture3D* tex3D, const tcu::Sampler& sampler)
    107 	: m_type	(TYPE_3D)
    108 	, m_sampler	(sampler)
    109 {
    110 	m_binding.tex3D = tex3D;
    111 }
    112 
    113 TextureBinding::TextureBinding (void)
    114 	: m_type	(TYPE_NONE)
    115 {
    116 	m_binding.tex2D = DE_NULL;
    117 }
    118 
    119 void TextureBinding::setSampler (const tcu::Sampler& sampler)
    120 {
    121 	m_sampler = sampler;
    122 }
    123 
    124 void TextureBinding::setTexture (const glu::Texture2D* tex2D)
    125 {
    126 	m_type			= TYPE_2D;
    127 	m_binding.tex2D	= tex2D;
    128 }
    129 
    130 void TextureBinding::setTexture (const glu::TextureCube* texCube)
    131 {
    132 	m_type				= TYPE_CUBE_MAP;
    133 	m_binding.texCube	= texCube;
    134 }
    135 
    136 void TextureBinding::setTexture (const glu::Texture2DArray* tex2DArray)
    137 {
    138 	m_type					= TYPE_2D_ARRAY;
    139 	m_binding.tex2DArray	= tex2DArray;
    140 }
    141 
    142 void TextureBinding::setTexture (const glu::Texture3D* tex3D)
    143 {
    144 	m_type			= TYPE_3D;
    145 	m_binding.tex3D	= tex3D;
    146 }
    147 
    148 // QuadGrid.
    149 
    150 class QuadGrid
    151 {
    152 public:
    153 							QuadGrid				(int gridSize, int screenWidth, int screenHeight, const Vec4& constCoords, const vector<Mat4>& userAttribTransforms, const vector<TextureBinding>& textures);
    154 							~QuadGrid				(void);
    155 
    156 	int						getGridSize				(void) const { return m_gridSize; }
    157 	int						getNumVertices			(void) const { return m_numVertices; }
    158 	int						getNumTriangles			(void) const { return m_numTriangles; }
    159 	const Vec4&				getConstCoords			(void) const { return m_constCoords; }
    160 	const vector<Mat4>		getUserAttribTransforms	(void) const { return m_userAttribTransforms; }
    161 	const vector<TextureBinding>&	getTextures		(void) const { return m_textures; }
    162 
    163 	const Vec4*				getPositions			(void) const { return &m_positions[0]; }
    164 	const float*			getAttribOne			(void) const { return &m_attribOne[0]; }
    165 	const Vec4*				getCoords				(void) const { return &m_coords[0]; }
    166 	const Vec4*				getUnitCoords			(void) const { return &m_unitCoords[0]; }
    167 	const Vec4*				getUserAttrib			(int attribNdx) const { return &m_userAttribs[attribNdx][0]; }
    168 	const deUint16*			getIndices				(void) const { return &m_indices[0]; }
    169 
    170 	Vec4					getCoords				(float sx, float sy) const;
    171 	Vec4					getUnitCoords			(float sx, float sy) const;
    172 
    173 	int						getNumUserAttribs		(void) const { return (int)m_userAttribTransforms.size(); }
    174 	Vec4					getUserAttrib			(int attribNdx, float sx, float sy) const;
    175 
    176 private:
    177 	int						m_gridSize;
    178 	int						m_numVertices;
    179 	int						m_numTriangles;
    180 	Vec4					m_constCoords;
    181 	vector<Mat4>			m_userAttribTransforms;
    182 	vector<TextureBinding>	m_textures;
    183 
    184 	vector<Vec4>			m_screenPos;
    185 	vector<Vec4>			m_positions;
    186 	vector<Vec4>			m_coords;			//!< Near-unit coordinates, roughly [-2.0 .. 2.0].
    187 	vector<Vec4>			m_unitCoords;		//!< Positive-only coordinates [0.0 .. 1.5].
    188 	vector<float>			m_attribOne;
    189 	vector<Vec4>			m_userAttribs[ShaderEvalContext::MAX_TEXTURES];
    190 	vector<deUint16>		m_indices;
    191 };
    192 
    193 QuadGrid::QuadGrid (int gridSize, int width, int height, const Vec4& constCoords, const vector<Mat4>& userAttribTransforms, const vector<TextureBinding>& textures)
    194 	: m_gridSize				(gridSize)
    195 	, m_numVertices				((gridSize + 1) * (gridSize + 1))
    196 	, m_numTriangles			(gridSize * gridSize * 2)
    197 	, m_constCoords				(constCoords)
    198 	, m_userAttribTransforms	(userAttribTransforms)
    199 	, m_textures				(textures)
    200 {
    201 	Vec4 viewportScale = Vec4((float)width, (float)height, 0.0f, 0.0f);
    202 
    203 	// Compute vertices.
    204 	m_positions.resize(m_numVertices);
    205 	m_coords.resize(m_numVertices);
    206 	m_unitCoords.resize(m_numVertices);
    207 	m_attribOne.resize(m_numVertices);
    208 	m_screenPos.resize(m_numVertices);
    209 
    210 	// User attributes.
    211 	for (int i = 0; i < DE_LENGTH_OF_ARRAY(m_userAttribs); i++)
    212 		m_userAttribs[i].resize(m_numVertices);
    213 
    214 	for (int y = 0; y < gridSize+1; y++)
    215 	for (int x = 0; x < gridSize+1; x++)
    216 	{
    217 		float				sx			= x / (float)gridSize;
    218 		float				sy			= y / (float)gridSize;
    219 		float				fx			= 2.0f * sx - 1.0f;
    220 		float				fy			= 2.0f * sy - 1.0f;
    221 		int					vtxNdx		= ((y * (gridSize+1)) + x);
    222 
    223 		m_positions[vtxNdx]		= Vec4(fx, fy, 0.0f, 1.0f);
    224 		m_attribOne[vtxNdx]		= 1.0f;
    225 		m_screenPos[vtxNdx]		= Vec4(sx, sy, 0.0f, 1.0f) * viewportScale;
    226 		m_coords[vtxNdx]		= getCoords(sx, sy);
    227 		m_unitCoords[vtxNdx]	= getUnitCoords(sx, sy);
    228 
    229 		for (int attribNdx = 0; attribNdx < getNumUserAttribs(); attribNdx++)
    230 			m_userAttribs[attribNdx][vtxNdx] = getUserAttrib(attribNdx, sx, sy);
    231 	}
    232 
    233 	// Compute indices.
    234 	m_indices.resize(3 * m_numTriangles);
    235 	for (int y = 0; y < gridSize; y++)
    236 	for (int x = 0; x < gridSize; x++)
    237 	{
    238 		int stride = gridSize + 1;
    239 		int v00 = (y * stride) + x;
    240 		int v01 = (y * stride) + x + 1;
    241 		int v10 = ((y+1) * stride) + x;
    242 		int v11 = ((y+1) * stride) + x + 1;
    243 
    244 		int baseNdx = ((y * gridSize) + x) * 6;
    245 		m_indices[baseNdx + 0] = v10;
    246 		m_indices[baseNdx + 1] = v00;
    247 		m_indices[baseNdx + 2] = v01;
    248 
    249 		m_indices[baseNdx + 3] = v10;
    250 		m_indices[baseNdx + 4] = v01;
    251 		m_indices[baseNdx + 5] = v11;
    252 	}
    253 }
    254 
    255 QuadGrid::~QuadGrid (void)
    256 {
    257 }
    258 
    259 inline Vec4 QuadGrid::getCoords (float sx, float sy) const
    260 {
    261 	float fx = 2.0f * sx - 1.0f;
    262 	float fy = 2.0f * sy - 1.0f;
    263 	return Vec4(fx, fy, -fx + 0.33f*fy, -0.275f*fx - fy);
    264 }
    265 
    266 inline Vec4 QuadGrid::getUnitCoords (float sx, float sy) const
    267 {
    268 	return Vec4(sx, sy, 0.33f*sx + 0.5f*sy, 0.5f*sx + 0.25f*sy);
    269 }
    270 
    271 inline Vec4 QuadGrid::getUserAttrib (int attribNdx, float sx, float sy) const
    272 {
    273 	// homogeneous normalized screen-space coordinates
    274 	return m_userAttribTransforms[attribNdx] * Vec4(sx, sy, 0.0f, 1.0f);
    275 }
    276 
    277 // ShaderEvalContext.
    278 
    279 ShaderEvalContext::ShaderEvalContext (const QuadGrid& quadGrid_)
    280 	: constCoords	(quadGrid_.getConstCoords())
    281 	, isDiscarded	(false)
    282 	, quadGrid		(quadGrid_)
    283 {
    284 	const vector<TextureBinding>& bindings = quadGrid.getTextures();
    285 	DE_ASSERT((int)bindings.size() <= MAX_TEXTURES);
    286 
    287 	// Fill in texture array.
    288 	for (int ndx = 0; ndx < (int)bindings.size(); ndx++)
    289 	{
    290 		const TextureBinding& binding = bindings[ndx];
    291 
    292 		if (binding.getType() == TextureBinding::TYPE_NONE)
    293 			continue;
    294 
    295 		textures[ndx].sampler = binding.getSampler();
    296 
    297 		switch (binding.getType())
    298 		{
    299 			case TextureBinding::TYPE_2D:		textures[ndx].tex2D			= &binding.get2D()->getRefTexture();		break;
    300 			case TextureBinding::TYPE_CUBE_MAP:	textures[ndx].texCube		= &binding.getCube()->getRefTexture();		break;
    301 			case TextureBinding::TYPE_2D_ARRAY:	textures[ndx].tex2DArray	= &binding.get2DArray()->getRefTexture();	break;
    302 			case TextureBinding::TYPE_3D:		textures[ndx].tex3D			= &binding.get3D()->getRefTexture();		break;
    303 			default:
    304 				DE_ASSERT(DE_FALSE);
    305 		}
    306 	}
    307 }
    308 
    309 ShaderEvalContext::~ShaderEvalContext (void)
    310 {
    311 }
    312 
    313 void ShaderEvalContext::reset (float sx, float sy)
    314 {
    315 	// Clear old values
    316 	color		= Vec4(0.0f, 0.0f, 0.0f, 1.0f);
    317 	isDiscarded	= false;
    318 
    319 	// Compute coords
    320 	coords		= quadGrid.getCoords(sx, sy);
    321 	unitCoords	= quadGrid.getUnitCoords(sx, sy);
    322 
    323 	// Compute user attributes.
    324 	int numAttribs = quadGrid.getNumUserAttribs();
    325 	DE_ASSERT(numAttribs <= MAX_USER_ATTRIBS);
    326 	for (int attribNdx = 0; attribNdx < numAttribs; attribNdx++)
    327 		in[attribNdx] = quadGrid.getUserAttrib(attribNdx, sx, sy);
    328 }
    329 
    330 tcu::Vec4 ShaderEvalContext::texture2D (int unitNdx, const tcu::Vec2& texCoords)
    331 {
    332 	if (textures[unitNdx].tex2D)
    333 		return textures[unitNdx].tex2D->sample(textures[unitNdx].sampler, texCoords.x(), texCoords.y(), 0.0f);
    334 	else
    335 		return tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f);
    336 }
    337 
    338 // ShaderEvaluator
    339 
    340 ShaderEvaluator::ShaderEvaluator (void)
    341 	: m_evalFunc(DE_NULL)
    342 {
    343 }
    344 
    345 ShaderEvaluator::ShaderEvaluator (ShaderEvalFunc evalFunc)
    346 	: m_evalFunc(evalFunc)
    347 {
    348 }
    349 
    350 ShaderEvaluator::~ShaderEvaluator (void)
    351 {
    352 }
    353 
    354 void ShaderEvaluator::evaluate (ShaderEvalContext& ctx)
    355 {
    356 	DE_ASSERT(m_evalFunc);
    357 	m_evalFunc(ctx);
    358 }
    359 
    360 // ShaderRenderCase.
    361 
    362 ShaderRenderCase::ShaderRenderCase (TestContext& testCtx, RenderContext& renderCtx, const ContextInfo& ctxInfo, const char* name, const char* description, bool isVertexCase, ShaderEvalFunc evalFunc)
    363 	: TestCase				(testCtx, name, description)
    364 	, m_renderCtx			(renderCtx)
    365 	, m_ctxInfo				(ctxInfo)
    366 	, m_isVertexCase		(isVertexCase)
    367 	, m_defaultEvaluator	(evalFunc)
    368 	, m_evaluator			(m_defaultEvaluator)
    369 	, m_clearColor			(DEFAULT_CLEAR_COLOR)
    370 	, m_program				(DE_NULL)
    371 {
    372 }
    373 
    374 ShaderRenderCase::ShaderRenderCase (TestContext& testCtx, RenderContext& renderCtx, const ContextInfo& ctxInfo, const char* name, const char* description, bool isVertexCase, ShaderEvaluator& evaluator)
    375 	: TestCase				(testCtx, name, description)
    376 	, m_renderCtx			(renderCtx)
    377 	, m_ctxInfo				(ctxInfo)
    378 	, m_isVertexCase		(isVertexCase)
    379 	, m_defaultEvaluator	(DE_NULL)
    380 	, m_evaluator			(evaluator)
    381 	, m_clearColor			(DEFAULT_CLEAR_COLOR)
    382 	, m_program				(DE_NULL)
    383 {
    384 }
    385 
    386 ShaderRenderCase::~ShaderRenderCase (void)
    387 {
    388 	ShaderRenderCase::deinit();
    389 }
    390 
    391 void ShaderRenderCase::init (void)
    392 {
    393 	TestLog&				log		= m_testCtx.getLog();
    394 	const glw::Functions&	gl		= m_renderCtx.getFunctions();
    395 
    396 	GLU_EXPECT_NO_ERROR(gl.getError(), "ShaderRenderCase::init() begin");
    397 
    398 	if (m_vertShaderSource.empty() || m_fragShaderSource.empty())
    399 	{
    400 		DE_ASSERT(m_vertShaderSource.empty() && m_fragShaderSource.empty());
    401 		setupShaderData();
    402 	}
    403 
    404 	DE_ASSERT(!m_program);
    405 	m_program = new ShaderProgram(m_renderCtx, makeVtxFragSources(m_vertShaderSource, m_fragShaderSource));
    406 
    407 	try
    408 	{
    409 		log << *m_program; // Always log shader program.
    410 
    411 		if (!m_program->isOk())
    412 			throw CompileFailed(__FILE__, __LINE__);
    413 
    414 		GLU_EXPECT_NO_ERROR(gl.getError(), "ShaderRenderCase::init() end");
    415 	}
    416 	catch (const std::exception&)
    417 	{
    418 		// Clean up.
    419 		ShaderRenderCase::deinit();
    420 		throw;
    421 	}
    422 }
    423 
    424 void ShaderRenderCase::deinit (void)
    425 {
    426 	delete m_program;
    427 	m_program = DE_NULL;
    428 }
    429 
    430 tcu::IVec2 ShaderRenderCase::getViewportSize (void) const
    431 {
    432 	return tcu::IVec2(de::min(m_renderCtx.getRenderTarget().getWidth(), MAX_RENDER_WIDTH),
    433 					  de::min(m_renderCtx.getRenderTarget().getHeight(), MAX_RENDER_HEIGHT));
    434 }
    435 
    436 TestNode::IterateResult ShaderRenderCase::iterate (void)
    437 {
    438 	const glw::Functions& gl = m_renderCtx.getFunctions();
    439 
    440 	GLU_EXPECT_NO_ERROR(gl.getError(), "ShaderRenderCase::iterate() begin");
    441 
    442 	DE_ASSERT(m_program);
    443 	deUint32 programID = m_program->getProgram();
    444 	gl.useProgram(programID);
    445 
    446 	// Create quad grid.
    447 	IVec2	viewportSize	= getViewportSize();
    448 	int		width			= viewportSize.x();
    449 	int		height			= viewportSize.y();
    450 
    451 	// \todo [petri] Better handling of constCoords (render in multiple chunks, vary coords).
    452 	QuadGrid quadGrid(m_isVertexCase ? GRID_SIZE : 4, width, height, Vec4(0.125f, 0.25f, 0.5f, 1.0f), m_userAttribTransforms, m_textures);
    453 
    454 	// Render result.
    455 	Surface resImage(width, height);
    456 	render(resImage, programID, quadGrid);
    457 
    458 	// Compute reference.
    459 	Surface refImage (width, height);
    460 	if (m_isVertexCase)
    461 		computeVertexReference(refImage, quadGrid);
    462 	else
    463 		computeFragmentReference(refImage, quadGrid);
    464 
    465 	// Compare.
    466 	bool testOk = compareImages(resImage, refImage, 0.05f);
    467 
    468 	// De-initialize.
    469 	gl.useProgram(0);
    470 
    471 	m_testCtx.setTestResult(testOk ? QP_TEST_RESULT_PASS	: QP_TEST_RESULT_FAIL,
    472 							testOk ? "Pass"					: "Fail");
    473 	return TestNode::STOP;
    474 }
    475 
    476 void ShaderRenderCase::setupShaderData (void)
    477 {
    478 }
    479 
    480 void ShaderRenderCase::setup (int programID)
    481 {
    482 	DE_UNREF(programID);
    483 }
    484 
    485 void ShaderRenderCase::setupUniforms (int programID, const Vec4& constCoords)
    486 {
    487 	DE_UNREF(programID);
    488 	DE_UNREF(constCoords);
    489 }
    490 
    491 void ShaderRenderCase::setupDefaultInputs (int programID)
    492 {
    493 	const glw::Functions& gl = m_renderCtx.getFunctions();
    494 
    495 	// SETUP UNIFORMS.
    496 
    497 	setupDefaultUniforms(m_renderCtx, programID);
    498 
    499 	GLU_EXPECT_NO_ERROR(gl.getError(), "post uniform setup");
    500 
    501 	// SETUP TEXTURES.
    502 
    503 	for (int ndx = 0; ndx < (int)m_textures.size(); ndx++)
    504 	{
    505 		const TextureBinding&	tex			= m_textures[ndx];
    506 		const tcu::Sampler&		sampler		= tex.getSampler();
    507 		deUint32				texTarget	= GL_NONE;
    508 		deUint32				texObj		= 0;
    509 
    510 		if (tex.getType() == TextureBinding::TYPE_NONE)
    511 			continue;
    512 
    513 		// Feature check.
    514 		if (m_renderCtx.getType().getAPI() == glu::ApiType::es(2,0))
    515 		{
    516 			if (tex.getType() == TextureBinding::TYPE_2D_ARRAY)
    517 				throw tcu::NotSupportedError("2D array texture binding is not supported");
    518 
    519 			if (tex.getType() == TextureBinding::TYPE_3D)
    520 				throw tcu::NotSupportedError("3D texture binding is not supported");
    521 
    522 			if (sampler.compare != tcu::Sampler::COMPAREMODE_NONE)
    523 				throw tcu::NotSupportedError("Shadow lookups are not supported");
    524 		}
    525 
    526 		switch (tex.getType())
    527 		{
    528 			case TextureBinding::TYPE_2D:		texTarget = GL_TEXTURE_2D;			texObj = tex.get2D()->getGLTexture();		break;
    529 			case TextureBinding::TYPE_CUBE_MAP:	texTarget = GL_TEXTURE_CUBE_MAP;	texObj = tex.getCube()->getGLTexture();		break;
    530 			case TextureBinding::TYPE_2D_ARRAY:	texTarget = GL_TEXTURE_2D_ARRAY;	texObj = tex.get2DArray()->getGLTexture();	break;
    531 			case TextureBinding::TYPE_3D:		texTarget = GL_TEXTURE_3D;			texObj = tex.get3D()->getGLTexture();		break;
    532 			default:
    533 				DE_ASSERT(DE_FALSE);
    534 		}
    535 
    536 		gl.activeTexture(GL_TEXTURE0+ndx);
    537 		gl.bindTexture(texTarget, texObj);
    538 		gl.texParameteri(texTarget, GL_TEXTURE_WRAP_S,		glu::getGLWrapMode(sampler.wrapS));
    539 		gl.texParameteri(texTarget, GL_TEXTURE_WRAP_T,		glu::getGLWrapMode(sampler.wrapT));
    540 		gl.texParameteri(texTarget, GL_TEXTURE_MIN_FILTER,	glu::getGLFilterMode(sampler.minFilter));
    541 		gl.texParameteri(texTarget, GL_TEXTURE_MAG_FILTER,	glu::getGLFilterMode(sampler.magFilter));
    542 
    543 		if (texTarget == GL_TEXTURE_3D)
    544 			gl.texParameteri(texTarget, GL_TEXTURE_WRAP_R, glu::getGLWrapMode(sampler.wrapR));
    545 
    546 		if (sampler.compare != tcu::Sampler::COMPAREMODE_NONE)
    547 		{
    548 			gl.texParameteri(texTarget, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
    549 			gl.texParameteri(texTarget, GL_TEXTURE_COMPARE_FUNC, glu::getGLCompareFunc(sampler.compare));
    550 		}
    551 	}
    552 
    553 	GLU_EXPECT_NO_ERROR(gl.getError(), "texture sampler setup");
    554 }
    555 
    556 static void getDefaultVertexArrays (const glw::Functions& gl, const QuadGrid& quadGrid, deUint32 program, vector<VertexArrayBinding>& vertexArrays)
    557 {
    558 	const int numElements = quadGrid.getNumVertices();
    559 
    560 	vertexArrays.push_back(va::Float("a_position",		4, numElements, 0, (const float*)quadGrid.getPositions()));
    561 	vertexArrays.push_back(va::Float("a_coords",		4, numElements, 0, (const float*)quadGrid.getCoords()));
    562 	vertexArrays.push_back(va::Float("a_unitCoords",	4, numElements, 0, (const float*)quadGrid.getUnitCoords()));
    563 	vertexArrays.push_back(va::Float("a_one",			1, numElements, 0, quadGrid.getAttribOne()));
    564 
    565 	// a_inN.
    566 	for (int userNdx = 0; userNdx < quadGrid.getNumUserAttribs(); userNdx++)
    567 	{
    568 		string name = string("a_in") + de::toString(userNdx);
    569 		vertexArrays.push_back(va::Float(name, 4, numElements, 0, (const float*)quadGrid.getUserAttrib(userNdx)));
    570 	}
    571 
    572 	// Matrix attributes - these are set by location
    573 	static const struct
    574 	{
    575 		const char*	name;
    576 		int			numCols;
    577 		int			numRows;
    578 	} matrices[] =
    579 	{
    580 		{ "a_mat2",		2, 2 },
    581 		{ "a_mat2x3",	2, 3 },
    582 		{ "a_mat2x4",	2, 4 },
    583 		{ "a_mat3x2",	3, 2 },
    584 		{ "a_mat3",		3, 3 },
    585 		{ "a_mat3x4",	3, 4 },
    586 		{ "a_mat4x2",	4, 2 },
    587 		{ "a_mat4x3",	4, 3 },
    588 		{ "a_mat4",		4, 4 }
    589 	};
    590 
    591 	for (int matNdx = 0; matNdx < DE_LENGTH_OF_ARRAY(matrices); matNdx++)
    592 	{
    593 		int loc = gl.getAttribLocation(program, matrices[matNdx].name);
    594 
    595 		if (loc < 0)
    596 			continue; // Not used in shader.
    597 
    598 		int numRows	= matrices[matNdx].numRows;
    599 		int numCols	= matrices[matNdx].numCols;
    600 
    601 		for (int colNdx = 0; colNdx < numCols; colNdx++)
    602 			vertexArrays.push_back(va::Float(loc+colNdx, numRows, numElements, 4*(int)sizeof(float), (const float*)quadGrid.getUserAttrib(colNdx)));
    603 	}
    604 }
    605 
    606 void ShaderRenderCase::render (Surface& result, int programID, const QuadGrid& quadGrid)
    607 {
    608 	const glw::Functions& gl = m_renderCtx.getFunctions();
    609 
    610 	GLU_EXPECT_NO_ERROR(gl.getError(), "pre render");
    611 
    612 	// Buffer info.
    613 	int				width		= result.getWidth();
    614 	int				height		= result.getHeight();
    615 
    616 	int				xOffsetMax	= m_renderCtx.getRenderTarget().getWidth() - width;
    617 	int				yOffsetMax	= m_renderCtx.getRenderTarget().getHeight() - height;
    618 
    619 	deUint32		hash		= deStringHash(m_vertShaderSource.c_str()) + deStringHash(m_fragShaderSource.c_str());
    620 	de::Random		rnd			(hash);
    621 
    622 	int				xOffset		= rnd.getInt(0, xOffsetMax);
    623 	int				yOffset		= rnd.getInt(0, yOffsetMax);
    624 
    625 	gl.viewport(xOffset, yOffset, width, height);
    626 
    627 	// Setup program.
    628 	setupUniforms(programID, quadGrid.getConstCoords());
    629 	setupDefaultInputs(programID);
    630 
    631 	// Clear.
    632 	gl.clearColor(m_clearColor.x(), m_clearColor.y(), m_clearColor.z(), m_clearColor.w());
    633 	gl.clear(GL_COLOR_BUFFER_BIT);
    634 
    635 	// Draw.
    636 	{
    637 		std::vector<VertexArrayBinding>	vertexArrays;
    638 		const int						numElements		= quadGrid.getNumTriangles()*3;
    639 
    640 		getDefaultVertexArrays(gl, quadGrid, programID, vertexArrays);
    641 		draw(m_renderCtx, programID, (int)vertexArrays.size(), &vertexArrays[0], pr::Triangles(numElements, quadGrid.getIndices()));
    642 	}
    643 	GLU_EXPECT_NO_ERROR(gl.getError(), "draw");
    644 
    645 	// Read back results.
    646 	glu::readPixels(m_renderCtx, xOffset, yOffset, result.getAccess());
    647 
    648 	GLU_EXPECT_NO_ERROR(gl.getError(), "post render");
    649 }
    650 
    651 void ShaderRenderCase::computeVertexReference (Surface& result, const QuadGrid& quadGrid)
    652 {
    653 	// Buffer info.
    654 	int					width		= result.getWidth();
    655 	int					height		= result.getHeight();
    656 	int					gridSize	= quadGrid.getGridSize();
    657 	int					stride		= gridSize + 1;
    658 	bool				hasAlpha	= m_renderCtx.getRenderTarget().getPixelFormat().alphaBits > 0;
    659 	ShaderEvalContext	evalCtx		(quadGrid);
    660 
    661 	// Evaluate color for each vertex.
    662 	vector<Vec4> colors((gridSize+1)*(gridSize+1));
    663 	for (int y = 0; y < gridSize+1; y++)
    664 	for (int x = 0; x < gridSize+1; x++)
    665 	{
    666 		float				sx			= x / (float)gridSize;
    667 		float				sy			= y / (float)gridSize;
    668 		int					vtxNdx		= ((y * (gridSize+1)) + x);
    669 
    670 		evalCtx.reset(sx, sy);
    671 		m_evaluator.evaluate(evalCtx);
    672 		DE_ASSERT(!evalCtx.isDiscarded); // Discard is not available in vertex shader.
    673 		Vec4 color = evalCtx.color;
    674 
    675 		if (!hasAlpha)
    676 			color.w() = 1.0f;
    677 
    678 		colors[vtxNdx] = color;
    679 	}
    680 
    681 	// Render quads.
    682 	for (int y = 0; y < gridSize; y++)
    683 	for (int x = 0; x < gridSize; x++)
    684 	{
    685 		float x0 = x / (float)gridSize;
    686 		float x1 = (x + 1) / (float)gridSize;
    687 		float y0 = y / (float)gridSize;
    688 		float y1 = (y + 1) / (float)gridSize;
    689 
    690 		float sx0 = x0 * (float)width;
    691 		float sx1 = x1 * (float)width;
    692 		float sy0 = y0 * (float)height;
    693 		float sy1 = y1 * (float)height;
    694 		float oosx = 1.0f / (sx1 - sx0);
    695 		float oosy = 1.0f / (sy1 - sy0);
    696 
    697 		int ix0 = deCeilFloatToInt32(sx0 - 0.5f);
    698 		int ix1 = deCeilFloatToInt32(sx1 - 0.5f);
    699 		int iy0 = deCeilFloatToInt32(sy0 - 0.5f);
    700 		int iy1 = deCeilFloatToInt32(sy1 - 0.5f);
    701 
    702 		int		v00 = (y * stride) + x;
    703 		int		v01 = (y * stride) + x + 1;
    704 		int		v10 = ((y + 1) * stride) + x;
    705 		int		v11 = ((y + 1) * stride) + x + 1;
    706 		Vec4	c00 = colors[v00];
    707 		Vec4	c01 = colors[v01];
    708 		Vec4	c10 = colors[v10];
    709 		Vec4	c11 = colors[v11];
    710 
    711 		//printf("(%d,%d) -> (%f..%f, %f..%f) (%d..%d, %d..%d)\n", x, y, sx0, sx1, sy0, sy1, ix0, ix1, iy0, iy1);
    712 
    713 		for (int iy = iy0; iy < iy1; iy++)
    714 		for (int ix = ix0; ix < ix1; ix++)
    715 		{
    716 			DE_ASSERT(deInBounds32(ix, 0, width));
    717 			DE_ASSERT(deInBounds32(iy, 0, height));
    718 
    719 			float		sfx		= (float)ix + 0.5f;
    720 			float		sfy		= (float)iy + 0.5f;
    721 			float		fx1		= deFloatClamp((sfx - sx0) * oosx, 0.0f, 1.0f);
    722 			float		fy1		= deFloatClamp((sfy - sy0) * oosy, 0.0f, 1.0f);
    723 
    724 			// Triangle quad interpolation.
    725 			bool		tri		= fx1 + fy1 <= 1.0f;
    726 			float		tx		= tri ? fx1 : (1.0f-fx1);
    727 			float		ty		= tri ? fy1 : (1.0f-fy1);
    728 			const Vec4&	t0		= tri ? c00 : c11;
    729 			const Vec4&	t1		= tri ? c01 : c10;
    730 			const Vec4&	t2		= tri ? c10 : c01;
    731 			Vec4		color	= t0 + (t1-t0)*tx + (t2-t0)*ty;
    732 
    733 			result.setPixel(ix, iy, toRGBA(color));
    734 		}
    735 	}
    736 }
    737 
    738 void ShaderRenderCase::computeFragmentReference (Surface& result, const QuadGrid& quadGrid)
    739 {
    740 	// Buffer info.
    741 	int					width		= result.getWidth();
    742 	int					height		= result.getHeight();
    743 	bool				hasAlpha	= m_renderCtx.getRenderTarget().getPixelFormat().alphaBits > 0;
    744 	ShaderEvalContext	evalCtx		(quadGrid);
    745 
    746 	// Render.
    747 	for (int y = 0; y < height; y++)
    748 	for (int x = 0; x < width; x++)
    749 	{
    750 		float sx = ((float)x + 0.5f) / (float)width;
    751 		float sy = ((float)y + 0.5f) / (float)height;
    752 
    753 		evalCtx.reset(sx, sy);
    754 		m_evaluator.evaluate(evalCtx);
    755 		// Select either clear color or computed color based on discarded bit.
    756 		Vec4 color = evalCtx.isDiscarded ? m_clearColor : evalCtx.color;
    757 
    758 		if (!hasAlpha)
    759 			color.w() = 1.0f;
    760 
    761 		result.setPixel(x, y, toRGBA(color));
    762 	}
    763 }
    764 
    765 bool ShaderRenderCase::compareImages (const Surface& resImage, const Surface& refImage, float errorThreshold)
    766 {
    767 	return tcu::fuzzyCompare(m_testCtx.getLog(), "ComparisonResult", "Image comparison result", refImage, resImage, errorThreshold, tcu::COMPARE_LOG_RESULT);
    768 }
    769 
    770 // Uniform name helpers.
    771 
    772 const char* getIntUniformName (int number)
    773 {
    774 	switch (number)
    775 	{
    776 		case 0:		return "ui_zero";
    777 		case 1:		return "ui_one";
    778 		case 2:		return "ui_two";
    779 		case 3:		return "ui_three";
    780 		case 4:		return "ui_four";
    781 		case 5:		return "ui_five";
    782 		case 6:		return "ui_six";
    783 		case 7:		return "ui_seven";
    784 		case 8:		return "ui_eight";
    785 		case 101:	return "ui_oneHundredOne";
    786 		default:
    787 			DE_ASSERT(false);
    788 			return "";
    789 	}
    790 }
    791 
    792 const char* getFloatUniformName (int number)
    793 {
    794 	switch (number)
    795 	{
    796 		case 0:	return "uf_zero";
    797 		case 1: return "uf_one";
    798 		case 2: return "uf_two";
    799 		case 3: return "uf_three";
    800 		case 4: return "uf_four";
    801 		case 5: return "uf_five";
    802 		case 6: return "uf_six";
    803 		case 7: return "uf_seven";
    804 		case 8: return "uf_eight";
    805 		default:
    806 			DE_ASSERT(false);
    807 			return "";
    808 	}
    809 }
    810 
    811 const char* getFloatFractionUniformName (int number)
    812 {
    813 	switch (number)
    814 	{
    815 		case 1: return "uf_one";
    816 		case 2: return "uf_half";
    817 		case 3: return "uf_third";
    818 		case 4: return "uf_fourth";
    819 		case 5: return "uf_fifth";
    820 		case 6: return "uf_sixth";
    821 		case 7: return "uf_seventh";
    822 		case 8: return "uf_eighth";
    823 		default:
    824 			DE_ASSERT(false);
    825 			return "";
    826 	}
    827 }
    828 
    829 void setupDefaultUniforms (const glu::RenderContext& context, deUint32 programID)
    830 {
    831 	const glw::Functions& gl = context.getFunctions();
    832 
    833 	// Bool.
    834 	struct BoolUniform { const char* name; bool value; };
    835 	static const BoolUniform s_boolUniforms[] =
    836 	{
    837 		{ "ub_true",	true },
    838 		{ "ub_false",	false },
    839 	};
    840 
    841 	for (int i = 0; i < DE_LENGTH_OF_ARRAY(s_boolUniforms); i++)
    842 	{
    843 		int uniLoc = gl.getUniformLocation(programID, s_boolUniforms[i].name);
    844 		if (uniLoc != -1)
    845 			gl.uniform1i(uniLoc, s_boolUniforms[i].value);
    846 	}
    847 
    848 	// BVec4.
    849 	struct BVec4Uniform { const char* name; BVec4 value; };
    850 	static const BVec4Uniform s_bvec4Uniforms[] =
    851 	{
    852 		{ "ub4_true",	BVec4(true) },
    853 		{ "ub4_false",	BVec4(false) },
    854 	};
    855 
    856 	for (int i = 0; i < DE_LENGTH_OF_ARRAY(s_bvec4Uniforms); i++)
    857 	{
    858 		const BVec4Uniform& uni = s_bvec4Uniforms[i];
    859 		int arr[4];
    860 		arr[0] = (int)uni.value.x();
    861 		arr[1] = (int)uni.value.y();
    862 		arr[2] = (int)uni.value.z();
    863 		arr[3] = (int)uni.value.w();
    864 		int uniLoc = gl.getUniformLocation(programID, uni.name);
    865 		if (uniLoc != -1)
    866 			gl.uniform4iv(uniLoc, 1, &arr[0]);
    867 	}
    868 
    869 	// Int.
    870 	struct IntUniform { const char* name; int value; };
    871 	static const IntUniform s_intUniforms[] =
    872 	{
    873 		{ "ui_minusOne",		-1 },
    874 		{ "ui_zero",			0 },
    875 		{ "ui_one",				1 },
    876 		{ "ui_two",				2 },
    877 		{ "ui_three",			3 },
    878 		{ "ui_four",			4 },
    879 		{ "ui_five",			5 },
    880 		{ "ui_six",				6 },
    881 		{ "ui_seven",			7 },
    882 		{ "ui_eight",			8 },
    883 		{ "ui_oneHundredOne",	101 }
    884 	};
    885 
    886 	for (int i = 0; i < DE_LENGTH_OF_ARRAY(s_intUniforms); i++)
    887 	{
    888 		int uniLoc = gl.getUniformLocation(programID, s_intUniforms[i].name);
    889 		if (uniLoc != -1)
    890 			gl.uniform1i(uniLoc, s_intUniforms[i].value);
    891 	}
    892 
    893 	// IVec2.
    894 	struct IVec2Uniform { const char* name; IVec2 value; };
    895 	static const IVec2Uniform s_ivec2Uniforms[] =
    896 	{
    897 		{ "ui2_minusOne",	IVec2(-1) },
    898 		{ "ui2_zero",		IVec2(0) },
    899 		{ "ui2_one",		IVec2(1) },
    900 		{ "ui2_two",		IVec2(2) },
    901 		{ "ui2_four",		IVec2(4) },
    902 		{ "ui2_five",		IVec2(5) }
    903 	};
    904 
    905 	for (int i = 0; i < DE_LENGTH_OF_ARRAY(s_ivec2Uniforms); i++)
    906 	{
    907 		int uniLoc = gl.getUniformLocation(programID, s_ivec2Uniforms[i].name);
    908 		if (uniLoc != -1)
    909 			gl.uniform2iv(uniLoc, 1, s_ivec2Uniforms[i].value.getPtr());
    910 	}
    911 
    912 	// IVec3.
    913 	struct IVec3Uniform { const char* name; IVec3 value; };
    914 	static const IVec3Uniform s_ivec3Uniforms[] =
    915 	{
    916 		{ "ui3_minusOne",	IVec3(-1) },
    917 		{ "ui3_zero",		IVec3(0) },
    918 		{ "ui3_one",		IVec3(1) },
    919 		{ "ui3_two",		IVec3(2) },
    920 		{ "ui3_four",		IVec3(4) },
    921 		{ "ui3_five",		IVec3(5) }
    922 	};
    923 
    924 	for (int i = 0; i < DE_LENGTH_OF_ARRAY(s_ivec3Uniforms); i++)
    925 	{
    926 		int uniLoc = gl.getUniformLocation(programID, s_ivec3Uniforms[i].name);
    927 		if (uniLoc != -1)
    928 			gl.uniform3iv(uniLoc, 1, s_ivec3Uniforms[i].value.getPtr());
    929 	}
    930 
    931 	// IVec4.
    932 	struct IVec4Uniform { const char* name; IVec4 value; };
    933 	static const IVec4Uniform s_ivec4Uniforms[] =
    934 	{
    935 		{ "ui4_minusOne",	IVec4(-1) },
    936 		{ "ui4_zero",		IVec4(0) },
    937 		{ "ui4_one",		IVec4(1) },
    938 		{ "ui4_two",		IVec4(2) },
    939 		{ "ui4_four",		IVec4(4) },
    940 		{ "ui4_five",		IVec4(5) }
    941 	};
    942 
    943 	for (int i = 0; i < DE_LENGTH_OF_ARRAY(s_ivec4Uniforms); i++)
    944 	{
    945 		int uniLoc = gl.getUniformLocation(programID, s_ivec4Uniforms[i].name);
    946 		if (uniLoc != -1)
    947 			gl.uniform4iv(uniLoc, 1, s_ivec4Uniforms[i].value.getPtr());
    948 	}
    949 
    950 	// Float.
    951 	struct FloatUniform { const char* name; float value; };
    952 	static const FloatUniform s_floatUniforms[] =
    953 	{
    954 		{ "uf_zero",	0.0f },
    955 		{ "uf_one",		1.0f },
    956 		{ "uf_two",		2.0f },
    957 		{ "uf_three",	3.0f },
    958 		{ "uf_four",	4.0f },
    959 		{ "uf_five",	5.0f },
    960 		{ "uf_six",		6.0f },
    961 		{ "uf_seven",	7.0f },
    962 		{ "uf_eight",	8.0f },
    963 		{ "uf_half",	1.0f / 2.0f },
    964 		{ "uf_third",	1.0f / 3.0f },
    965 		{ "uf_fourth",	1.0f / 4.0f },
    966 		{ "uf_fifth",	1.0f / 5.0f },
    967 		{ "uf_sixth",	1.0f / 6.0f },
    968 		{ "uf_seventh",	1.0f / 7.0f },
    969 		{ "uf_eighth",	1.0f / 8.0f }
    970 	};
    971 
    972 	for (int i = 0; i < DE_LENGTH_OF_ARRAY(s_floatUniforms); i++)
    973 	{
    974 		int uniLoc = gl.getUniformLocation(programID, s_floatUniforms[i].name);
    975 		if (uniLoc != -1)
    976 			gl.uniform1f(uniLoc, s_floatUniforms[i].value);
    977 	}
    978 
    979 	// Vec2.
    980 	struct Vec2Uniform { const char* name; Vec2 value; };
    981 	static const Vec2Uniform s_vec2Uniforms[] =
    982 	{
    983 		{ "uv2_minusOne",	Vec2(-1.0f) },
    984 		{ "uv2_zero",		Vec2(0.0f) },
    985 		{ "uv2_half",		Vec2(0.5f) },
    986 		{ "uv2_one",		Vec2(1.0f) },
    987 		{ "uv2_two",		Vec2(2.0f) },
    988 	};
    989 
    990 	for (int i = 0; i < DE_LENGTH_OF_ARRAY(s_vec2Uniforms); i++)
    991 	{
    992 		int uniLoc = gl.getUniformLocation(programID, s_vec2Uniforms[i].name);
    993 		if (uniLoc != -1)
    994 			gl.uniform2fv(uniLoc, 1, s_vec2Uniforms[i].value.getPtr());
    995 	}
    996 
    997 	// Vec3.
    998 	struct Vec3Uniform { const char* name; Vec3 value; };
    999 	static const Vec3Uniform s_vec3Uniforms[] =
   1000 	{
   1001 		{ "uv3_minusOne",	Vec3(-1.0f) },
   1002 		{ "uv3_zero",		Vec3(0.0f) },
   1003 		{ "uv3_half",		Vec3(0.5f) },
   1004 		{ "uv3_one",		Vec3(1.0f) },
   1005 		{ "uv3_two",		Vec3(2.0f) },
   1006 	};
   1007 
   1008 	for (int i = 0; i < DE_LENGTH_OF_ARRAY(s_vec3Uniforms); i++)
   1009 	{
   1010 		int uniLoc = gl.getUniformLocation(programID, s_vec3Uniforms[i].name);
   1011 		if (uniLoc != -1)
   1012 			gl.uniform3fv(uniLoc, 1, s_vec3Uniforms[i].value.getPtr());
   1013 	}
   1014 
   1015 	// Vec4.
   1016 	struct Vec4Uniform { const char* name; Vec4 value; };
   1017 	static const Vec4Uniform s_vec4Uniforms[] =
   1018 	{
   1019 		{ "uv4_minusOne",	Vec4(-1.0f) },
   1020 		{ "uv4_zero",		Vec4(0.0f) },
   1021 		{ "uv4_half",		Vec4(0.5f) },
   1022 		{ "uv4_one",		Vec4(1.0f) },
   1023 		{ "uv4_two",		Vec4(2.0f) },
   1024 		{ "uv4_black",		Vec4(0.0f, 0.0f, 0.0f, 1.0f) },
   1025 		{ "uv4_gray",		Vec4(0.5f, 0.5f, 0.5f, 1.0f) },
   1026 		{ "uv4_white",		Vec4(1.0f, 1.0f, 1.0f, 1.0f) },
   1027 	};
   1028 
   1029 	for (int i = 0; i < DE_LENGTH_OF_ARRAY(s_vec4Uniforms); i++)
   1030 	{
   1031 		int uniLoc = gl.getUniformLocation(programID, s_vec4Uniforms[i].name);
   1032 		if (uniLoc != -1)
   1033 			gl.uniform4fv(uniLoc, 1, s_vec4Uniforms[i].value.getPtr());
   1034 	}
   1035 }
   1036 
   1037 } // gls
   1038 } // deqp
   1039