1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL (ES) Module
3 * -----------------------------------------------
5 * Copyright 2014 The Android Open Source Project
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * \brief Shader execute test.
23 * \todo [petri] Multiple grid with differing constants/uniforms.
25 *//*--------------------------------------------------------------------*/
27 #include "glsShaderRenderCase.hpp"
29 #include "tcuSurface.hpp"
30 #include "tcuVector.hpp"
31 #include "tcuImageCompare.hpp"
32 #include "tcuTestLog.hpp"
33 #include "tcuRenderTarget.hpp"
35 #include "gluPixelTransfer.hpp"
36 #include "gluTexture.hpp"
37 #include "gluTextureUtil.hpp"
38 #include "gluDrawUtil.hpp"
40 #include "glwFunctions.hpp"
41 #include "glwEnums.hpp"
43 #include "deRandom.hpp"
47 #include "deStringUtil.hpp"
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);
67 inline RGBA toRGBA (const Vec4& a)
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));
75 inline tcu::Vec4 toVec (const RGBA& c)
77 return tcu::Vec4(c.getRed() / 255.0f,
78 c.getGreen() / 255.0f,
80 c.getAlpha() / 255.0f);
85 TextureBinding::TextureBinding (const glu::Texture2D* tex2D, const tcu::Sampler& sampler)
89 m_binding.tex2D = tex2D;
92 TextureBinding::TextureBinding (const glu::TextureCube* texCube, const tcu::Sampler& sampler)
93 : m_type (TYPE_CUBE_MAP)
96 m_binding.texCube = texCube;
99 TextureBinding::TextureBinding (const glu::Texture2DArray* tex2DArray, const tcu::Sampler& sampler)
100 : m_type (TYPE_2D_ARRAY)
101 , m_sampler (sampler)
103 m_binding.tex2DArray = tex2DArray;
106 TextureBinding::TextureBinding (const glu::Texture3D* tex3D, const tcu::Sampler& sampler)
108 , m_sampler (sampler)
110 m_binding.tex3D = tex3D;
113 TextureBinding::TextureBinding (void)
116 m_binding.tex2D = DE_NULL;
119 void TextureBinding::setSampler (const tcu::Sampler& sampler)
124 void TextureBinding::setTexture (const glu::Texture2D* tex2D)
127 m_binding.tex2D = tex2D;
130 void TextureBinding::setTexture (const glu::TextureCube* texCube)
132 m_type = TYPE_CUBE_MAP;
133 m_binding.texCube = texCube;
136 void TextureBinding::setTexture (const glu::Texture2DArray* tex2DArray)
138 m_type = TYPE_2D_ARRAY;
139 m_binding.tex2DArray = tex2DArray;
142 void TextureBinding::setTexture (const glu::Texture3D* tex3D)
145 m_binding.tex3D = tex3D;
153 QuadGrid (int gridSize, int screenWidth, int screenHeight, const Vec4& constCoords, const vector<Mat4>& userAttribTransforms, const vector<TextureBinding>& textures);
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; }
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]; }
170 Vec4 getCoords (float sx, float sy) const;
171 Vec4 getUnitCoords (float sx, float sy) const;
173 int getNumUserAttribs (void) const { return (int)m_userAttribTransforms.size(); }
174 Vec4 getUserAttrib (int attribNdx, float sx, float sy) const;
181 vector<Mat4> m_userAttribTransforms;
182 vector<TextureBinding> m_textures;
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;
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)
201 Vec4 viewportScale = Vec4((float)width, (float)height, 0.0f, 0.0f);
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);
211 for (int i = 0; i < DE_LENGTH_OF_ARRAY(m_userAttribs); i++)
212 m_userAttribs[i].resize(m_numVertices);
214 for (int y = 0; y < gridSize+1; y++)
215 for (int x = 0; x < gridSize+1; x++)
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);
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);
229 for (int attribNdx = 0; attribNdx < getNumUserAttribs(); attribNdx++)
230 m_userAttribs[attribNdx][vtxNdx] = getUserAttrib(attribNdx, sx, sy);
234 m_indices.resize(3 * m_numTriangles);
235 for (int y = 0; y < gridSize; y++)
236 for (int x = 0; x < gridSize; x++)
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;
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;
249 m_indices[baseNdx + 3] = v10;
250 m_indices[baseNdx + 4] = v01;
251 m_indices[baseNdx + 5] = v11;
255 QuadGrid::~QuadGrid (void)
259 inline Vec4 QuadGrid::getCoords (float sx, float sy) const
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);
266 inline Vec4 QuadGrid::getUnitCoords (float sx, float sy) const
268 return Vec4(sx, sy, 0.33f*sx + 0.5f*sy, 0.5f*sx + 0.25f*sy);
271 inline Vec4 QuadGrid::getUserAttrib (int attribNdx, float sx, float sy) const
273 // homogeneous normalized screen-space coordinates
274 return m_userAttribTransforms[attribNdx] * Vec4(sx, sy, 0.0f, 1.0f);
277 // ShaderEvalContext.
279 ShaderEvalContext::ShaderEvalContext (const QuadGrid& quadGrid_)
280 : constCoords (quadGrid_.getConstCoords())
281 , isDiscarded (false)
282 , quadGrid (quadGrid_)
284 const vector<TextureBinding>& bindings = quadGrid.getTextures();
285 DE_ASSERT((int)bindings.size() <= MAX_TEXTURES);
287 // Fill in texture array.
288 for (int ndx = 0; ndx < (int)bindings.size(); ndx++)
290 const TextureBinding& binding = bindings[ndx];
292 if (binding.getType() == TextureBinding::TYPE_NONE)
295 textures[ndx].sampler = binding.getSampler();
297 switch (binding.getType())
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;
309 ShaderEvalContext::~ShaderEvalContext (void)
313 void ShaderEvalContext::reset (float sx, float sy)
316 color = Vec4(0.0f, 0.0f, 0.0f, 1.0f);
320 coords = quadGrid.getCoords(sx, sy);
321 unitCoords = quadGrid.getUnitCoords(sx, sy);
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);
330 tcu::Vec4 ShaderEvalContext::texture2D (int unitNdx, const tcu::Vec2& texCoords)
332 if (textures[unitNdx].tex2D)
333 return textures[unitNdx].tex2D->sample(textures[unitNdx].sampler, texCoords.x(), texCoords.y(), 0.0f);
335 return tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f);
340 ShaderEvaluator::ShaderEvaluator (void)
341 : m_evalFunc(DE_NULL)
345 ShaderEvaluator::ShaderEvaluator (ShaderEvalFunc evalFunc)
346 : m_evalFunc(evalFunc)
350 ShaderEvaluator::~ShaderEvaluator (void)
354 void ShaderEvaluator::evaluate (ShaderEvalContext& ctx)
356 DE_ASSERT(m_evalFunc);
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)
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)
386 ShaderRenderCase::~ShaderRenderCase (void)
388 ShaderRenderCase::deinit();
391 void ShaderRenderCase::init (void)
393 TestLog& log = m_testCtx.getLog();
394 const glw::Functions& gl = m_renderCtx.getFunctions();
396 GLU_EXPECT_NO_ERROR(gl.getError(), "ShaderRenderCase::init() begin");
398 if (m_vertShaderSource.empty() || m_fragShaderSource.empty())
400 DE_ASSERT(m_vertShaderSource.empty() && m_fragShaderSource.empty());
404 DE_ASSERT(!m_program);
405 m_program = new ShaderProgram(m_renderCtx, makeVtxFragSources(m_vertShaderSource, m_fragShaderSource));
409 log << *m_program; // Always log shader program.
411 if (!m_program->isOk())
412 throw CompileFailed(__FILE__, __LINE__);
414 GLU_EXPECT_NO_ERROR(gl.getError(), "ShaderRenderCase::init() end");
416 catch (const std::exception&)
419 ShaderRenderCase::deinit();
424 void ShaderRenderCase::deinit (void)
430 tcu::IVec2 ShaderRenderCase::getViewportSize (void) const
432 return tcu::IVec2(de::min(m_renderCtx.getRenderTarget().getWidth(), MAX_RENDER_WIDTH),
433 de::min(m_renderCtx.getRenderTarget().getHeight(), MAX_RENDER_HEIGHT));
436 TestNode::IterateResult ShaderRenderCase::iterate (void)
438 const glw::Functions& gl = m_renderCtx.getFunctions();
440 GLU_EXPECT_NO_ERROR(gl.getError(), "ShaderRenderCase::iterate() begin");
442 DE_ASSERT(m_program);
443 deUint32 programID = m_program->getProgram();
444 gl.useProgram(programID);
447 IVec2 viewportSize = getViewportSize();
448 int width = viewportSize.x();
449 int height = viewportSize.y();
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);
455 Surface resImage(width, height);
456 render(resImage, programID, quadGrid);
458 // Compute reference.
459 Surface refImage (width, height);
461 computeVertexReference(refImage, quadGrid);
463 computeFragmentReference(refImage, quadGrid);
466 bool testOk = compareImages(resImage, refImage, 0.05f);
471 m_testCtx.setTestResult(testOk ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL,
472 testOk ? "Pass" : "Fail");
473 return TestNode::STOP;
476 void ShaderRenderCase::setupShaderData (void)
480 void ShaderRenderCase::setup (int programID)
485 void ShaderRenderCase::setupUniforms (int programID, const Vec4& constCoords)
488 DE_UNREF(constCoords);
491 void ShaderRenderCase::setupDefaultInputs (int programID)
493 const glw::Functions& gl = m_renderCtx.getFunctions();
497 setupDefaultUniforms(m_renderCtx, programID);
499 GLU_EXPECT_NO_ERROR(gl.getError(), "post uniform setup");
503 for (int ndx = 0; ndx < (int)m_textures.size(); ndx++)
505 const TextureBinding& tex = m_textures[ndx];
506 const tcu::Sampler& sampler = tex.getSampler();
507 deUint32 texTarget = GL_NONE;
510 if (tex.getType() == TextureBinding::TYPE_NONE)
514 if (m_renderCtx.getType().getAPI() == glu::ApiType::es(2,0))
516 if (tex.getType() == TextureBinding::TYPE_2D_ARRAY)
517 throw tcu::NotSupportedError("2D array texture binding is not supported");
519 if (tex.getType() == TextureBinding::TYPE_3D)
520 throw tcu::NotSupportedError("3D texture binding is not supported");
522 if (sampler.compare != tcu::Sampler::COMPAREMODE_NONE)
523 throw tcu::NotSupportedError("Shadow lookups are not supported");
526 switch (tex.getType())
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;
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));
543 if (texTarget == GL_TEXTURE_3D)
544 gl.texParameteri(texTarget, GL_TEXTURE_WRAP_R, glu::getGLWrapMode(sampler.wrapR));
546 if (sampler.compare != tcu::Sampler::COMPAREMODE_NONE)
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));
553 GLU_EXPECT_NO_ERROR(gl.getError(), "texture sampler setup");
556 static void getDefaultVertexArrays (const glw::Functions& gl, const QuadGrid& quadGrid, deUint32 program, vector<VertexArrayBinding>& vertexArrays)
558 const int numElements = quadGrid.getNumVertices();
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()));
566 for (int userNdx = 0; userNdx < quadGrid.getNumUserAttribs(); userNdx++)
568 string name = string("a_in") + de::toString(userNdx);
569 vertexArrays.push_back(va::Float(name, 4, numElements, 0, (const float*)quadGrid.getUserAttrib(userNdx)));
572 // Matrix attributes - these are set by location
581 { "a_mat2x3", 2, 3 },
582 { "a_mat2x4", 2, 4 },
583 { "a_mat3x2", 3, 2 },
585 { "a_mat3x4", 3, 4 },
586 { "a_mat4x2", 4, 2 },
587 { "a_mat4x3", 4, 3 },
591 for (int matNdx = 0; matNdx < DE_LENGTH_OF_ARRAY(matrices); matNdx++)
593 int loc = gl.getAttribLocation(program, matrices[matNdx].name);
596 continue; // Not used in shader.
598 int numRows = matrices[matNdx].numRows;
599 int numCols = matrices[matNdx].numCols;
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)));
606 void ShaderRenderCase::render (Surface& result, int programID, const QuadGrid& quadGrid)
608 const glw::Functions& gl = m_renderCtx.getFunctions();
610 GLU_EXPECT_NO_ERROR(gl.getError(), "pre render");
613 int width = result.getWidth();
614 int height = result.getHeight();
616 int xOffsetMax = m_renderCtx.getRenderTarget().getWidth() - width;
617 int yOffsetMax = m_renderCtx.getRenderTarget().getHeight() - height;
619 deUint32 hash = deStringHash(m_vertShaderSource.c_str()) + deStringHash(m_fragShaderSource.c_str());
620 de::Random rnd (hash);
622 int xOffset = rnd.getInt(0, xOffsetMax);
623 int yOffset = rnd.getInt(0, yOffsetMax);
625 gl.viewport(xOffset, yOffset, width, height);
628 setupUniforms(programID, quadGrid.getConstCoords());
629 setupDefaultInputs(programID);
632 gl.clearColor(m_clearColor.x(), m_clearColor.y(), m_clearColor.z(), m_clearColor.w());
633 gl.clear(GL_COLOR_BUFFER_BIT);
637 std::vector<VertexArrayBinding> vertexArrays;
638 const int numElements = quadGrid.getNumTriangles()*3;
640 getDefaultVertexArrays(gl, quadGrid, programID, vertexArrays);
641 draw(m_renderCtx, programID, (int)vertexArrays.size(), &vertexArrays[0], pr::Triangles(numElements, quadGrid.getIndices()));
643 GLU_EXPECT_NO_ERROR(gl.getError(), "draw");
645 // Read back results.
646 glu::readPixels(m_renderCtx, xOffset, yOffset, result.getAccess());
648 GLU_EXPECT_NO_ERROR(gl.getError(), "post render");
651 void ShaderRenderCase::computeVertexReference (Surface& result, const QuadGrid& quadGrid)
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);
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++)
666 float sx = x / (float)gridSize;
667 float sy = y / (float)gridSize;
668 int vtxNdx = ((y * (gridSize+1)) + x);
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;
678 colors[vtxNdx] = color;
682 for (int y = 0; y < gridSize; y++)
683 for (int x = 0; x < gridSize; x++)
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;
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);
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);
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];
711 //printf("(%d,%d) -> (%f..%f, %f..%f) (%d..%d, %d..%d)\n", x, y, sx0, sx1, sy0, sy1, ix0, ix1, iy0, iy1);
713 for (int iy = iy0; iy < iy1; iy++)
714 for (int ix = ix0; ix < ix1; ix++)
716 DE_ASSERT(deInBounds32(ix, 0, width));
717 DE_ASSERT(deInBounds32(iy, 0, height));
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);
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;
733 result.setPixel(ix, iy, toRGBA(color));
738 void ShaderRenderCase::computeFragmentReference (Surface& result, const QuadGrid& quadGrid)
741 int width = result.getWidth();
742 int height = result.getHeight();
743 bool hasAlpha = m_renderCtx.getRenderTarget().getPixelFormat().alphaBits > 0;
744 ShaderEvalContext evalCtx (quadGrid);
747 for (int y = 0; y < height; y++)
748 for (int x = 0; x < width; x++)
750 float sx = ((float)x + 0.5f) / (float)width;
751 float sy = ((float)y + 0.5f) / (float)height;
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;
761 result.setPixel(x, y, toRGBA(color));
765 bool ShaderRenderCase::compareImages (const Surface& resImage, const Surface& refImage, float errorThreshold)
767 return tcu::fuzzyCompare(m_testCtx.getLog(), "ComparisonResult", "Image comparison result", refImage, resImage, errorThreshold, tcu::COMPARE_LOG_RESULT);
770 // Uniform name helpers.
772 const char* getIntUniformName (int number)
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";
792 const char* getFloatUniformName (int number)
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";
811 const char* getFloatFractionUniformName (int number)
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";
829 void setupDefaultUniforms (const glu::RenderContext& context, deUint32 programID)
831 const glw::Functions& gl = context.getFunctions();
834 struct BoolUniform { const char* name; bool value; };
835 static const BoolUniform s_boolUniforms[] =
838 { "ub_false", false },
841 for (int i = 0; i < DE_LENGTH_OF_ARRAY(s_boolUniforms); i++)
843 int uniLoc = gl.getUniformLocation(programID, s_boolUniforms[i].name);
845 gl.uniform1i(uniLoc, s_boolUniforms[i].value);
849 struct BVec4Uniform { const char* name; BVec4 value; };
850 static const BVec4Uniform s_bvec4Uniforms[] =
852 { "ub4_true", BVec4(true) },
853 { "ub4_false", BVec4(false) },
856 for (int i = 0; i < DE_LENGTH_OF_ARRAY(s_bvec4Uniforms); i++)
858 const BVec4Uniform& uni = s_bvec4Uniforms[i];
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);
866 gl.uniform4iv(uniLoc, 1, &arr[0]);
870 struct IntUniform { const char* name; int value; };
871 static const IntUniform s_intUniforms[] =
873 { "ui_minusOne", -1 },
883 { "ui_oneHundredOne", 101 }
886 for (int i = 0; i < DE_LENGTH_OF_ARRAY(s_intUniforms); i++)
888 int uniLoc = gl.getUniformLocation(programID, s_intUniforms[i].name);
890 gl.uniform1i(uniLoc, s_intUniforms[i].value);
894 struct IVec2Uniform { const char* name; IVec2 value; };
895 static const IVec2Uniform s_ivec2Uniforms[] =
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) }
905 for (int i = 0; i < DE_LENGTH_OF_ARRAY(s_ivec2Uniforms); i++)
907 int uniLoc = gl.getUniformLocation(programID, s_ivec2Uniforms[i].name);
909 gl.uniform2iv(uniLoc, 1, s_ivec2Uniforms[i].value.getPtr());
913 struct IVec3Uniform { const char* name; IVec3 value; };
914 static const IVec3Uniform s_ivec3Uniforms[] =
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) }
924 for (int i = 0; i < DE_LENGTH_OF_ARRAY(s_ivec3Uniforms); i++)
926 int uniLoc = gl.getUniformLocation(programID, s_ivec3Uniforms[i].name);
928 gl.uniform3iv(uniLoc, 1, s_ivec3Uniforms[i].value.getPtr());
932 struct IVec4Uniform { const char* name; IVec4 value; };
933 static const IVec4Uniform s_ivec4Uniforms[] =
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) }
943 for (int i = 0; i < DE_LENGTH_OF_ARRAY(s_ivec4Uniforms); i++)
945 int uniLoc = gl.getUniformLocation(programID, s_ivec4Uniforms[i].name);
947 gl.uniform4iv(uniLoc, 1, s_ivec4Uniforms[i].value.getPtr());
951 struct FloatUniform { const char* name; float value; };
952 static const FloatUniform s_floatUniforms[] =
957 { "uf_three", 3.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 }
972 for (int i = 0; i < DE_LENGTH_OF_ARRAY(s_floatUniforms); i++)
974 int uniLoc = gl.getUniformLocation(programID, s_floatUniforms[i].name);
976 gl.uniform1f(uniLoc, s_floatUniforms[i].value);
980 struct Vec2Uniform { const char* name; Vec2 value; };
981 static const Vec2Uniform s_vec2Uniforms[] =
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) },
990 for (int i = 0; i < DE_LENGTH_OF_ARRAY(s_vec2Uniforms); i++)
992 int uniLoc = gl.getUniformLocation(programID, s_vec2Uniforms[i].name);
994 gl.uniform2fv(uniLoc, 1, s_vec2Uniforms[i].value.getPtr());
998 struct Vec3Uniform { const char* name; Vec3 value; };
999 static const Vec3Uniform s_vec3Uniforms[] =
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) },
1008 for (int i = 0; i < DE_LENGTH_OF_ARRAY(s_vec3Uniforms); i++)
1010 int uniLoc = gl.getUniformLocation(programID, s_vec3Uniforms[i].name);
1012 gl.uniform3fv(uniLoc, 1, s_vec3Uniforms[i].value.getPtr());
1016 struct Vec4Uniform { const char* name; Vec4 value; };
1017 static const Vec4Uniform s_vec4Uniforms[] =
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) },
1029 for (int i = 0; i < DE_LENGTH_OF_ARRAY(s_vec4Uniforms); i++)
1031 int uniLoc = gl.getUniformLocation(programID, s_vec4Uniforms[i].name);
1033 gl.uniform4fv(uniLoc, 1, s_vec4Uniforms[i].value.getPtr());