1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES 3.0 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 precision tests.
23 * \note Floating-point case uses R32UI render target and uses
24 * floatBitsToUint() in shader to write out floating-point value bits.
25 * This is done since ES3 core doesn't support FP render targets.
26 *//*--------------------------------------------------------------------*/
28 #include "es3fShaderPrecisionTests.hpp"
29 #include "tcuVector.hpp"
30 #include "tcuTestLog.hpp"
31 #include "tcuVectorUtil.hpp"
32 #include "tcuFloat.hpp"
33 #include "tcuFormatUtil.hpp"
34 #include "gluRenderContext.hpp"
35 #include "gluShaderProgram.hpp"
36 #include "gluShaderUtil.hpp"
37 #include "gluDrawUtil.hpp"
38 #include "deRandom.hpp"
41 #include "glwEnums.hpp"
42 #include "glwFunctions.hpp"
55 using std::ostringstream;
60 FRAMEBUFFER_WIDTH = 32,
61 FRAMEBUFFER_HEIGHT = 32
64 static glu::ShaderProgram* createFloatPrecisionEvalProgram (const glu::RenderContext& context, glu::Precision precision, const char* evalOp, bool isVertexCase)
66 glu::DataType type = glu::TYPE_FLOAT;
67 glu::DataType outType = glu::TYPE_UINT;
68 const char* typeName = glu::getDataTypeName(type);
69 const char* outTypeName = glu::getDataTypeName(outType);
70 const char* precName = glu::getPrecisionName(precision);
73 ostringstream& op = isVertexCase ? vtx : frag;
75 vtx << "#version 300 es\n"
76 << "in highp vec4 a_position;\n"
77 << "in " << precName << " " << typeName << " a_in0;\n"
78 << "in " << precName << " " << typeName << " a_in1;\n";
79 frag << "#version 300 es\n"
80 << "layout(location = 0) out highp " << outTypeName << " o_out;\n";
84 vtx << "flat out " << precName << " " << typeName << " v_out;\n";
85 frag << "flat in " << precName << " " << typeName << " v_out;\n";
89 vtx << "flat out " << precName << " " << typeName << " v_in0;\n"
90 << "flat out " << precName << " " << typeName << " v_in1;\n";
91 frag << "flat in " << precName << " " << typeName << " v_in0;\n"
92 << "flat in " << precName << " " << typeName << " v_in1;\n";
95 vtx << "\nvoid main (void)\n{\n"
96 << " gl_Position = a_position;\n";
97 frag << "\nvoid main (void)\n{\n";
99 op << "\t" << precName << " " << typeName << " in0 = " << (isVertexCase ? "a_" : "v_") << "in0;\n"
100 << "\t" << precName << " " << typeName << " in1 = " << (isVertexCase ? "a_" : "v_") << "in1;\n";
103 op << "\t" << precName << " " << typeName << " res;\n";
105 op << "\t" << (isVertexCase ? "v_out" : "res") << " = " << evalOp << ";\n";
109 frag << " o_out = floatBitsToUint(v_out);\n";
113 vtx << " v_in0 = a_in0;\n"
114 << " v_in1 = a_in1;\n";
115 frag << " o_out = floatBitsToUint(res);\n";
121 return new glu::ShaderProgram(context, glu::makeVtxFragSources(vtx.str(), frag.str()));
124 static glu::ShaderProgram* createIntUintPrecisionEvalProgram (const glu::RenderContext& context, glu::DataType type, glu::Precision precision, const char* evalOp, bool isVertexCase)
126 const char* typeName = glu::getDataTypeName(type);
127 const char* precName = glu::getPrecisionName(precision);
130 ostringstream& op = isVertexCase ? vtx : frag;
132 vtx << "#version 300 es\n"
133 << "in highp vec4 a_position;\n"
134 << "in " << precName << " " << typeName << " a_in0;\n"
135 << "in " << precName << " " << typeName << " a_in1;\n";
136 frag << "#version 300 es\n"
137 << "layout(location = 0) out " << precName << " " << typeName << " o_out;\n";
141 vtx << "flat out " << precName << " " << typeName << " v_out;\n";
142 frag << "flat in " << precName << " " << typeName << " v_out;\n";
146 vtx << "flat out " << precName << " " << typeName << " v_in0;\n"
147 << "flat out " << precName << " " << typeName << " v_in1;\n";
148 frag << "flat in " << precName << " " << typeName << " v_in0;\n"
149 << "flat in " << precName << " " << typeName << " v_in1;\n";
152 vtx << "\nvoid main (void)\n{\n"
153 << " gl_Position = a_position;\n";
154 frag << "\nvoid main (void)\n{\n";
156 op << "\t" << precName << " " << typeName << " in0 = " << (isVertexCase ? "a_" : "v_") << "in0;\n"
157 << "\t" << precName << " " << typeName << " in1 = " << (isVertexCase ? "a_" : "v_") << "in1;\n";
159 op << "\t" << (isVertexCase ? "v_" : "o_") << "out = " << evalOp << ";\n";
163 frag << " o_out = v_out;\n";
167 vtx << " v_in0 = a_in0;\n"
168 << " v_in1 = a_in1;\n";
174 return new glu::ShaderProgram(context, glu::makeVtxFragSources(vtx.str(), frag.str()));
177 class ShaderFloatPrecisionCase : public TestCase
180 typedef double (*EvalFunc) (double in0, double in1);
182 ShaderFloatPrecisionCase (Context& context, const char* name, const char* desc, const char* op, EvalFunc evalFunc, glu::Precision precision, const tcu::Vec2& rangeA, const tcu::Vec2& rangeB, bool isVertexCase);
183 ~ShaderFloatPrecisionCase (void);
187 IterateResult iterate (void);
190 bool compare (float in0, float in1, double reference, float result);
193 ShaderFloatPrecisionCase (const ShaderFloatPrecisionCase& other);
194 ShaderFloatPrecisionCase& operator= (const ShaderFloatPrecisionCase& other);
199 glu::Precision m_precision;
204 int m_numTestsPerIter;
209 glu::ShaderProgram* m_program;
210 deUint32 m_framebuffer;
211 deUint32 m_renderbuffer;
215 ShaderFloatPrecisionCase::ShaderFloatPrecisionCase (Context& context, const char* name, const char* desc, const char* op, EvalFunc evalFunc, glu::Precision precision, const tcu::Vec2& rangeA, const tcu::Vec2& rangeB, bool isVertexCase)
216 : TestCase (context, name, desc)
218 , m_evalFunc (evalFunc)
219 , m_precision (precision)
222 , m_isVertexCase (isVertexCase)
223 , m_numTestsPerIter (32)
225 , m_rnd (deStringHash(name))
226 , m_program (DE_NULL)
233 ShaderFloatPrecisionCase::~ShaderFloatPrecisionCase (void)
235 ShaderFloatPrecisionCase::deinit();
238 void ShaderFloatPrecisionCase::init (void)
240 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
241 TestLog& log = m_testCtx.getLog();
243 DE_ASSERT(!m_program && !m_framebuffer && !m_renderbuffer);
246 m_program = createFloatPrecisionEvalProgram(m_context.getRenderContext(), m_precision, m_op.c_str(), m_isVertexCase);
249 TCU_CHECK(m_program->isOk());
251 // Create framebuffer.
252 gl.genFramebuffers(1, &m_framebuffer);
253 gl.genRenderbuffers(1, &m_renderbuffer);
255 gl.bindRenderbuffer(GL_RENDERBUFFER, m_renderbuffer);
256 gl.renderbufferStorage(GL_RENDERBUFFER, GL_R32UI, FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT);
258 gl.bindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
259 gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_renderbuffer);
261 GLU_EXPECT_NO_ERROR(gl.getError(), "Post framebuffer setup");
262 TCU_CHECK(gl.checkFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
264 gl.bindFramebuffer(GL_FRAMEBUFFER, m_context.getRenderContext().getDefaultFramebuffer());
266 // Initialize test result to pass.
267 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
271 void ShaderFloatPrecisionCase::deinit (void)
276 m_context.getRenderContext().getFunctions().deleteFramebuffers(1, &m_framebuffer);
279 m_context.getRenderContext().getFunctions().deleteRenderbuffers(1, &m_renderbuffer);
286 bool ShaderFloatPrecisionCase::compare (float in0, float in1, double reference, float result)
288 // Comparison is done using 64-bit reference value to accurately evaluate rounding mode error.
289 // If 32-bit reference value is used, 2 bits of rounding error must be allowed.
291 // For mediump and lowp types the comparison currently allows 3 bits of rounding error:
292 // two bits from conversions and one from actual operation.
294 // \todo [2013-09-30 pyry] Make this more strict: determine if rounding can actually happen.
296 const int mantissaBits = m_precision == glu::PRECISION_HIGHP ? 23 : 10;
297 const int numPrecBits = 52 - mantissaBits;
299 const int in0Exp = tcu::Float32(in0).exponent();
300 const int in1Exp = tcu::Float32(in1).exponent();
301 const int resExp = tcu::Float32(result).exponent();
302 const int numLostBits = de::max(de::max(in0Exp-resExp, in1Exp-resExp), 0); // Lost due to mantissa shift.
304 const int roundingUlpError = m_precision == glu::PRECISION_HIGHP ? 1 : 3;
305 const int maskBits = numLostBits + numPrecBits;
307 m_testCtx.getLog() << TestLog::Message << "Assuming " << mantissaBits << " mantissa bits, " << numLostBits << " bits lost in operation, and " << roundingUlpError << " ULP rounding error."
308 << TestLog::EndMessage;
311 const deUint64 refBits = tcu::Float64(reference).bits();
312 const deUint64 resBits = tcu::Float64(result).bits();
313 const deUint64 accurateRefBits = maskBits < 64 ? refBits >> (deUint64)maskBits : 0u;
314 const deUint64 accurateResBits = maskBits < 64 ? resBits >> (deUint64)maskBits : 0u;
315 const deUint64 ulpDiff = (deUint64)de::abs((deInt64)accurateRefBits - (deInt64)accurateResBits);
317 if (ulpDiff > (deUint64)roundingUlpError)
319 m_testCtx.getLog() << TestLog::Message << "ERROR: comparison failed! ULP diff (ignoring lost/undefined bits) = " << ulpDiff << TestLog::EndMessage;
327 ShaderFloatPrecisionCase::IterateResult ShaderFloatPrecisionCase::iterate (void)
330 const float position[] =
332 -1.0f, -1.0f, 0.0f, 1.0f,
333 -1.0f, 1.0f, 0.0f, 1.0f,
334 1.0f, -1.0f, 0.0f, 1.0f,
335 1.0f, 1.0f, 0.0f, 1.0f
337 const deUint16 indices[] = { 0, 1, 2, 2, 1, 3 };
339 const int numVertices = 4;
340 float in0Arr[4] = { 0.0f };
341 float in1Arr[4] = { 0.0f };
343 TestLog& log = m_testCtx.getLog();
344 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
345 vector<glu::VertexArrayBinding> vertexArrays;
347 // Image read from GL.
348 std::vector<float> pixels (FRAMEBUFFER_WIDTH*FRAMEBUFFER_HEIGHT*4);
350 // \todo [2012-05-03 pyry] Could be cached.
351 deUint32 prog = m_program->getProgram();
354 gl.bindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
356 vertexArrays.push_back(glu::va::Float("a_position", 4, numVertices, 0, &position[0]));
357 vertexArrays.push_back(glu::va::Float("a_in0", 1, numVertices, 0, &in0Arr[0]));
358 vertexArrays.push_back(glu::va::Float("a_in1", 1, numVertices, 0, &in1Arr[0]));
360 GLU_EXPECT_NO_ERROR(gl.getError(), "After program setup");
362 // Compute values and reference.
363 for (int testNdx = 0; testNdx < m_numTestsPerIter; testNdx++)
365 const float in0 = m_rnd.getFloat(m_rangeA.x(), m_rangeA.y());
366 const float in1 = m_rnd.getFloat(m_rangeB.x(), m_rangeB.y());
367 const double refD = m_evalFunc((double)in0, (double)in1);
368 const float refF = tcu::Float64(refD).asFloat(); // Uses RTE rounding mode.
370 log << TestLog::Message << "iter " << m_iterNdx << ", test " << testNdx << ": "
371 << "in0 = " << in0 << " / " << tcu::toHex(tcu::Float32(in0).bits())
372 << ", in1 = " << in1 << " / " << tcu::toHex(tcu::Float32(in1).bits())
373 << TestLog::EndMessage
374 << TestLog::Message << " reference = " << refF << " / " << tcu::toHex(tcu::Float32(refF).bits()) << TestLog::EndMessage;
376 std::fill(&in0Arr[0], &in0Arr[0] + DE_LENGTH_OF_ARRAY(in0Arr), in0);
377 std::fill(&in1Arr[0], &in1Arr[0] + DE_LENGTH_OF_ARRAY(in1Arr), in1);
379 glu::draw(m_context.getRenderContext(), prog, (int)vertexArrays.size(), &vertexArrays[0],
380 glu::pr::Triangles(DE_LENGTH_OF_ARRAY(indices), &indices[0]));
381 gl.readPixels(0, 0, FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT, GL_RGBA_INTEGER, GL_UNSIGNED_INT, &pixels[0]);
382 GLU_EXPECT_NO_ERROR(gl.getError(), "After render");
384 log << TestLog::Message << " result = " << pixels[0] << " / " << tcu::toHex(tcu::Float32(pixels[0]).bits()) << TestLog::EndMessage;
388 const bool firstPixelOk = compare(in0, in1, refD, pixels[0]);
392 // Check that rest of pixels match to first one.
393 const deUint32 firstPixelBits = tcu::Float32(pixels[0]).bits();
394 bool allPixelsOk = true;
396 for (int y = 0; y < FRAMEBUFFER_HEIGHT; y++)
398 for (int x = 0; x < FRAMEBUFFER_WIDTH; x++)
400 const deUint32 pixelBits = tcu::Float32(pixels[(y*FRAMEBUFFER_WIDTH + x)*4]).bits();
402 if (pixelBits != firstPixelBits)
404 log << TestLog::Message << "ERROR: Inconsistent results, got " << tcu::toHex(pixelBits) << " at (" << x << ", " << y << ")" << TestLog::EndMessage;
414 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Inconsistent values in framebuffer");
417 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Result comparison failed");
420 if (m_testCtx.getTestResult() != QP_TEST_RESULT_PASS)
424 gl.bindFramebuffer(GL_FRAMEBUFFER, m_context.getRenderContext().getDefaultFramebuffer());
425 GLU_EXPECT_NO_ERROR(gl.getError(), "After iteration");
428 return (m_iterNdx < m_numIters && m_testCtx.getTestResult() == QP_TEST_RESULT_PASS) ? CONTINUE : STOP;
431 class ShaderIntPrecisionCase : public TestCase
434 typedef int (*EvalFunc) (int a, int b);
436 ShaderIntPrecisionCase (Context& context, const char* name, const char* desc, const char* op, EvalFunc evalFunc, glu::Precision precision, int bits, const tcu::IVec2& rangeA, const tcu::IVec2& rangeB, bool isVertexCase);
437 ~ShaderIntPrecisionCase (void);
441 IterateResult iterate (void);
444 ShaderIntPrecisionCase (const ShaderIntPrecisionCase& other);
445 ShaderIntPrecisionCase& operator= (const ShaderIntPrecisionCase& other);
450 glu::Precision m_precision;
456 int m_numTestsPerIter;
461 glu::ShaderProgram* m_program;
462 deUint32 m_framebuffer;
463 deUint32 m_renderbuffer;
467 ShaderIntPrecisionCase::ShaderIntPrecisionCase (Context& context, const char* name, const char* desc, const char* op, EvalFunc evalFunc, glu::Precision precision, int bits, const tcu::IVec2& rangeA, const tcu::IVec2& rangeB, bool isVertexCase)
468 : TestCase (context, name, desc)
470 , m_evalFunc (evalFunc)
471 , m_precision (precision)
475 , m_isVertexCase (isVertexCase)
476 , m_numTestsPerIter (32)
478 , m_rnd (deStringHash(name))
479 , m_program (DE_NULL)
486 ShaderIntPrecisionCase::~ShaderIntPrecisionCase (void)
488 ShaderIntPrecisionCase::deinit();
491 void ShaderIntPrecisionCase::init (void)
493 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
494 TestLog& log = m_testCtx.getLog();
496 DE_ASSERT(!m_program && !m_framebuffer && !m_renderbuffer);
499 m_program = createIntUintPrecisionEvalProgram(m_context.getRenderContext(), glu::TYPE_INT, m_precision, m_op.c_str(), m_isVertexCase);
502 TCU_CHECK(m_program->isOk());
504 // Create framebuffer.
505 gl.genFramebuffers(1, &m_framebuffer);
506 gl.genRenderbuffers(1, &m_renderbuffer);
508 gl.bindRenderbuffer(GL_RENDERBUFFER, m_renderbuffer);
509 gl.renderbufferStorage(GL_RENDERBUFFER, GL_R32I, FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT);
511 gl.bindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
512 gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_renderbuffer);
514 GLU_EXPECT_NO_ERROR(gl.getError(), "Post framebuffer setup");
515 TCU_CHECK(gl.checkFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
517 gl.bindFramebuffer(GL_FRAMEBUFFER, m_context.getRenderContext().getDefaultFramebuffer());
519 // Initialize test result to pass.
520 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
523 log << TestLog::Message << "Number of accurate bits assumed = " << m_bits << TestLog::EndMessage;
526 void ShaderIntPrecisionCase::deinit (void)
531 m_context.getRenderContext().getFunctions().deleteFramebuffers(1, &m_framebuffer);
534 m_context.getRenderContext().getFunctions().deleteRenderbuffers(1, &m_renderbuffer);
541 ShaderIntPrecisionCase::IterateResult ShaderIntPrecisionCase::iterate (void)
544 const float position[] =
546 -1.0f, -1.0f, 0.0f, 1.0f,
547 -1.0f, 1.0f, 0.0f, 1.0f,
548 1.0f, -1.0f, 0.0f, 1.0f,
549 1.0f, 1.0f, 0.0f, 1.0f
551 const deUint16 indices[] = { 0, 1, 2, 2, 1, 3 };
553 const int numVertices = 4;
554 int in0Arr[4] = { 0 };
555 int in1Arr[4] = { 0 };
557 TestLog& log = m_testCtx.getLog();
558 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
559 deUint32 mask = m_bits == 32 ? 0xffffffffu : ((1u<<m_bits)-1);
560 vector<int> pixels (FRAMEBUFFER_WIDTH*FRAMEBUFFER_HEIGHT*4);
561 vector<glu::VertexArrayBinding> vertexArrays;
563 deUint32 prog = m_program->getProgram();
565 // \todo [2012-05-03 pyry] A bit hacky. getInt() should work fine with ranges like this.
566 bool isMaxRangeA = m_rangeA.x() == (int)0x80000000 && m_rangeA.y() == (int)0x7fffffff;
567 bool isMaxRangeB = m_rangeB.x() == (int)0x80000000 && m_rangeB.y() == (int)0x7fffffff;
570 gl.bindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
572 vertexArrays.push_back(glu::va::Float("a_position", 4, numVertices, 0, &position[0]));
573 vertexArrays.push_back(glu::va::Int32("a_in0", 1, numVertices, 0, &in0Arr[0]));
574 vertexArrays.push_back(glu::va::Int32("a_in1", 1, numVertices, 0, &in1Arr[0]));
576 GLU_EXPECT_NO_ERROR(gl.getError(), "After program setup");
578 // Compute values and reference.
579 for (int testNdx = 0; testNdx < m_numTestsPerIter; testNdx++)
581 int in0 = deSignExtendTo32(((isMaxRangeA ? (int)m_rnd.getUint32() : m_rnd.getInt(m_rangeA.x(), m_rangeA.y())) & mask), m_bits);
582 int in1 = deSignExtendTo32(((isMaxRangeB ? (int)m_rnd.getUint32() : m_rnd.getInt(m_rangeB.x(), m_rangeB.y())) & mask), m_bits);
583 int refMasked = m_evalFunc(in0, in1) & mask;
584 int refOut = deSignExtendTo32(refMasked, m_bits);
586 log << TestLog::Message << "iter " << m_iterNdx << ", test " << testNdx << ": "
587 << "in0 = " << in0 << ", in1 = " << in1 << ", ref out = " << refOut << " / " << tcu::toHex(refMasked)
588 << TestLog::EndMessage;
590 std::fill(&in0Arr[0], &in0Arr[0] + DE_LENGTH_OF_ARRAY(in0Arr), in0);
591 std::fill(&in1Arr[0], &in1Arr[0] + DE_LENGTH_OF_ARRAY(in1Arr), in1);
593 glu::draw(m_context.getRenderContext(), prog, (int)vertexArrays.size(), &vertexArrays[0],
594 glu::pr::Triangles(DE_LENGTH_OF_ARRAY(indices), &indices[0]));
595 gl.readPixels(0, 0, FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT, GL_RGBA_INTEGER, GL_INT, &pixels[0]);
596 GLU_EXPECT_NO_ERROR(gl.getError(), "After render");
599 for (int y = 0; y < FRAMEBUFFER_HEIGHT; y++)
601 for (int x = 0; x < FRAMEBUFFER_WIDTH; x++)
603 int cmpOut = pixels[(y*FRAMEBUFFER_WIDTH + x)*4];
604 int cmpMasked = cmpOut & mask;
606 if (cmpMasked != refMasked)
608 log << TestLog::Message << "Comparison failed (at " << x << ", " << y << "): "
609 << "got " << cmpOut << " / " << tcu::toHex(cmpOut)
610 << TestLog::EndMessage;
611 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
618 gl.bindFramebuffer(GL_FRAMEBUFFER, m_context.getRenderContext().getDefaultFramebuffer());
619 GLU_EXPECT_NO_ERROR(gl.getError(), "After iteration");
622 return (m_iterNdx < m_numIters) ? CONTINUE : STOP;
625 class ShaderUintPrecisionCase : public TestCase
628 typedef deUint32 (*EvalFunc) (deUint32 a, deUint32 b);
630 ShaderUintPrecisionCase (Context& context, const char* name, const char* desc, const char* op, EvalFunc evalFunc, glu::Precision precision, int bits, const tcu::UVec2& rangeA, const tcu::UVec2& rangeB, bool isVertexCase);
631 ~ShaderUintPrecisionCase (void);
635 IterateResult iterate (void);
638 ShaderUintPrecisionCase (const ShaderUintPrecisionCase& other);
639 ShaderUintPrecisionCase& operator= (const ShaderUintPrecisionCase& other);
644 glu::Precision m_precision;
650 int m_numTestsPerIter;
655 glu::ShaderProgram* m_program;
656 deUint32 m_framebuffer;
657 deUint32 m_renderbuffer;
661 ShaderUintPrecisionCase::ShaderUintPrecisionCase (Context& context, const char* name, const char* desc, const char* op, EvalFunc evalFunc, glu::Precision precision, int bits, const tcu::UVec2& rangeA, const tcu::UVec2& rangeB, bool isVertexCase)
662 : TestCase (context, name, desc)
664 , m_evalFunc (evalFunc)
665 , m_precision (precision)
669 , m_isVertexCase (isVertexCase)
670 , m_numTestsPerIter (32)
672 , m_rnd (deStringHash(name))
673 , m_program (DE_NULL)
680 ShaderUintPrecisionCase::~ShaderUintPrecisionCase (void)
682 ShaderUintPrecisionCase::deinit();
685 void ShaderUintPrecisionCase::init (void)
687 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
688 TestLog& log = m_testCtx.getLog();
690 DE_ASSERT(!m_program && !m_framebuffer && !m_renderbuffer);
693 m_program = createIntUintPrecisionEvalProgram(m_context.getRenderContext(), glu::TYPE_UINT, m_precision, m_op.c_str(), m_isVertexCase);
696 TCU_CHECK(m_program->isOk());
698 // Create framebuffer.
699 gl.genFramebuffers(1, &m_framebuffer);
700 gl.genRenderbuffers(1, &m_renderbuffer);
702 gl.bindRenderbuffer(GL_RENDERBUFFER, m_renderbuffer);
703 gl.renderbufferStorage(GL_RENDERBUFFER, GL_R32UI, FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT);
705 gl.bindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
706 gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_renderbuffer);
708 GLU_EXPECT_NO_ERROR(gl.getError(), "Post framebuffer setup");
709 TCU_CHECK(gl.checkFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
711 gl.bindFramebuffer(GL_FRAMEBUFFER, m_context.getRenderContext().getDefaultFramebuffer());
713 // Initialize test result to pass.
714 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
717 log << TestLog::Message << "Number of accurate bits assumed = " << m_bits << TestLog::EndMessage;
720 void ShaderUintPrecisionCase::deinit (void)
725 m_context.getRenderContext().getFunctions().deleteFramebuffers(1, &m_framebuffer);
728 m_context.getRenderContext().getFunctions().deleteRenderbuffers(1, &m_renderbuffer);
735 ShaderUintPrecisionCase::IterateResult ShaderUintPrecisionCase::iterate (void)
738 const float position[] =
740 -1.0f, -1.0f, 0.0f, 1.0f,
741 -1.0f, 1.0f, 0.0f, 1.0f,
742 1.0f, -1.0f, 0.0f, 1.0f,
743 1.0f, 1.0f, 0.0f, 1.0f
745 const deUint16 indices[] = { 0, 1, 2, 2, 1, 3 };
747 const int numVertices = 4;
748 deUint32 in0Arr[4] = { 0 };
749 deUint32 in1Arr[4] = { 0 };
751 TestLog& log = m_testCtx.getLog();
752 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
753 deUint32 mask = m_bits == 32 ? 0xffffffffu : ((1u<<m_bits)-1);
754 vector<deUint32> pixels (FRAMEBUFFER_WIDTH*FRAMEBUFFER_HEIGHT*4);
755 vector<glu::VertexArrayBinding> vertexArrays;
757 deUint32 prog = m_program->getProgram();
759 // \todo [2012-05-03 pyry] A bit hacky.
760 bool isMaxRangeA = m_rangeA.x() == 0 && m_rangeA.y() == 0xffffffff;
761 bool isMaxRangeB = m_rangeB.x() == 0 && m_rangeB.y() == 0xffffffff;
764 gl.bindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
766 vertexArrays.push_back(glu::va::Float("a_position", 4, numVertices, 0, &position[0]));
767 vertexArrays.push_back(glu::va::Uint32("a_in0", 1, numVertices, 0, &in0Arr[0]));
768 vertexArrays.push_back(glu::va::Uint32("a_in1", 1, numVertices, 0, &in1Arr[0]));
770 GLU_EXPECT_NO_ERROR(gl.getError(), "After program setup");
772 // Compute values and reference.
773 for (int testNdx = 0; testNdx < m_numTestsPerIter; testNdx++)
775 deUint32 in0 = (isMaxRangeA ? m_rnd.getUint32() : (m_rangeA.x() + m_rnd.getUint32()%(m_rangeA.y()-m_rangeA.x()+1))) & mask;
776 deUint32 in1 = (isMaxRangeB ? m_rnd.getUint32() : (m_rangeB.x() + m_rnd.getUint32()%(m_rangeB.y()-m_rangeB.x()+1))) & mask;
777 deUint32 refOut = m_evalFunc(in0, in1) & mask;
779 log << TestLog::Message << "iter " << m_iterNdx << ", test " << testNdx << ": "
780 << "in0 = " << tcu::toHex(in0) << ", in1 = " << tcu::toHex(in1) << ", ref out = " << tcu::toHex(refOut)
781 << TestLog::EndMessage;
783 std::fill(&in0Arr[0], &in0Arr[0] + DE_LENGTH_OF_ARRAY(in0Arr), in0);
784 std::fill(&in1Arr[0], &in1Arr[0] + DE_LENGTH_OF_ARRAY(in1Arr), in1);
786 glu::draw(m_context.getRenderContext(), prog, (int)vertexArrays.size(), &vertexArrays[0],
787 glu::pr::Triangles(DE_LENGTH_OF_ARRAY(indices), &indices[0]));
788 gl.readPixels(0, 0, FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT, GL_RGBA_INTEGER, GL_UNSIGNED_INT, &pixels[0]);
789 GLU_EXPECT_NO_ERROR(gl.getError(), "After render");
792 for (int y = 0; y < FRAMEBUFFER_HEIGHT; y++)
794 for (int x = 0; x < FRAMEBUFFER_WIDTH; x++)
796 deUint32 cmpOut = pixels[(y*FRAMEBUFFER_WIDTH + x)*4];
797 deUint32 cmpMasked = cmpOut & mask;
799 if (cmpMasked != refOut)
801 log << TestLog::Message << "Comparison failed (at " << x << ", " << y << "): "
802 << "got " << tcu::toHex(cmpOut)
803 << TestLog::EndMessage;
804 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
811 gl.bindFramebuffer(GL_FRAMEBUFFER, 0);
812 GLU_EXPECT_NO_ERROR(gl.getError(), "After iteration");
815 return (m_iterNdx < m_numIters) ? CONTINUE : STOP;
818 ShaderPrecisionTests::ShaderPrecisionTests (Context& context)
819 : TestCaseGroup(context, "precision", "Shader precision requirements validation tests")
823 ShaderPrecisionTests::~ShaderPrecisionTests (void)
827 void ShaderPrecisionTests::init (void)
837 // Exp = Emax-2, Mantissa = 0
838 float minF32 = tcu::Float32((1u<<31) | (0xfdu<<23) | 0x0u).asFloat();
839 float maxF32 = tcu::Float32((0u<<31) | (0xfdu<<23) | 0x0u).asFloat();
840 float minF16 = tcu::Float16((deUint16)((1u<<15) | (0x1du<<10) | 0x0u)).asFloat();
841 float maxF16 = tcu::Float16((deUint16)((0u<<15) | (0x1du<<10) | 0x0u)).asFloat();
842 tcu::Vec2 fullRange32F (minF32, maxF32);
843 tcu::Vec2 fullRange16F (minF16, maxF16);
844 tcu::IVec2 fullRange32I (0x80000000, 0x7fffffff);
845 tcu::IVec2 fullRange16I (-(1<<15), (1<<15)-1);
846 tcu::IVec2 fullRange8I (-(1<<7), (1<<7)-1);
847 tcu::UVec2 fullRange32U (0u, 0xffffffffu);
848 tcu::UVec2 fullRange16U (0u, 0xffffu);
849 tcu::UVec2 fullRange8U (0u, 0xffu);
851 // \note Right now it is not programmatically verified that the results shouldn't end up being inf/nan but
852 // actual values used are ok.
858 ShaderFloatPrecisionCase::EvalFunc evalFunc;
859 glu::Precision precision;
864 // Name Op Eval Precision RangeA RangeB
865 { "highp_add", "in0 + in1", add<double>, glu::PRECISION_HIGHP, fullRange32F, fullRange32F },
866 { "highp_sub", "in0 - in1", sub<double>, glu::PRECISION_HIGHP, fullRange32F, fullRange32F },
867 { "highp_mul", "in0 * in1", mul<double>, glu::PRECISION_HIGHP, Vec2(-1e5f, 1e5f), Vec2(-1e5f, 1e5f) },
868 { "highp_div", "in0 / in1", div<double>, glu::PRECISION_HIGHP, Vec2(-1e5f, 1e5f), Vec2(-1e5f, 1e5f) },
869 { "mediump_add", "in0 + in1", add<double>, glu::PRECISION_MEDIUMP, fullRange16F, fullRange16F },
870 { "mediump_sub", "in0 - in1", sub<double>, glu::PRECISION_MEDIUMP, fullRange16F, fullRange16F },
871 { "mediump_mul", "in0 * in1", mul<double>, glu::PRECISION_MEDIUMP, Vec2(-1e2f, 1e2f), Vec2(-1e2f, 1e2f) },
872 { "mediump_div", "in0 / in1", div<double>, glu::PRECISION_MEDIUMP, Vec2(-1e2f, 1e2f), Vec2(-1e2f, 1e2f) }
879 ShaderIntPrecisionCase::EvalFunc evalFunc;
880 glu::Precision precision;
886 // Name Op Eval Precision Bits RangeA RangeB
887 { "highp_add", "in0 + in1", add<int>, glu::PRECISION_HIGHP, 32, fullRange32I, fullRange32I },
888 { "highp_sub", "in0 - in1", sub<int>, glu::PRECISION_HIGHP, 32, fullRange32I, fullRange32I },
889 { "highp_mul", "in0 * in1", mul<int>, glu::PRECISION_HIGHP, 32, fullRange32I, fullRange32I },
890 { "highp_div", "in0 / in1", div<int>, glu::PRECISION_HIGHP, 32, fullRange32I, IVec2(-10000, -1) },
891 { "mediump_add", "in0 + in1", add<int>, glu::PRECISION_MEDIUMP, 16, fullRange16I, fullRange16I },
892 { "mediump_sub", "in0 - in1", sub<int>, glu::PRECISION_MEDIUMP, 16, fullRange16I, fullRange16I },
893 { "mediump_mul", "in0 * in1", mul<int>, glu::PRECISION_MEDIUMP, 16, fullRange16I, fullRange16I },
894 { "mediump_div", "in0 / in1", div<int>, glu::PRECISION_MEDIUMP, 16, fullRange16I, IVec2(1, 1000) },
895 { "lowp_add", "in0 + in1", add<int>, glu::PRECISION_LOWP, 8, fullRange8I, fullRange8I },
896 { "lowp_sub", "in0 - in1", sub<int>, glu::PRECISION_LOWP, 8, fullRange8I, fullRange8I },
897 { "lowp_mul", "in0 * in1", mul<int>, glu::PRECISION_LOWP, 8, fullRange8I, fullRange8I },
898 { "lowp_div", "in0 / in1", div<int>, glu::PRECISION_LOWP, 8, fullRange8I, IVec2(-50, -1) }
905 ShaderUintPrecisionCase::EvalFunc evalFunc;
906 glu::Precision precision;
912 // Name Op Eval Precision Bits RangeA RangeB
913 { "highp_add", "in0 + in1", add<deUint32>, glu::PRECISION_HIGHP, 32, fullRange32U, fullRange32U },
914 { "highp_sub", "in0 - in1", sub<deUint32>, glu::PRECISION_HIGHP, 32, fullRange32U, fullRange32U },
915 { "highp_mul", "in0 * in1", mul<deUint32>, glu::PRECISION_HIGHP, 32, fullRange32U, fullRange32U },
916 { "highp_div", "in0 / in1", div<deUint32>, glu::PRECISION_HIGHP, 32, fullRange32U, UVec2(1u, 10000u) },
917 { "mediump_add", "in0 + in1", add<deUint32>, glu::PRECISION_MEDIUMP, 16, fullRange16U, fullRange16U },
918 { "mediump_sub", "in0 - in1", sub<deUint32>, glu::PRECISION_MEDIUMP, 16, fullRange16U, fullRange16U },
919 { "mediump_mul", "in0 * in1", mul<deUint32>, glu::PRECISION_MEDIUMP, 16, fullRange16U, fullRange16U },
920 { "mediump_div", "in0 / in1", div<deUint32>, glu::PRECISION_MEDIUMP, 16, fullRange16U, UVec2(1, 1000u) },
921 { "lowp_add", "in0 + in1", add<deUint32>, glu::PRECISION_LOWP, 8, fullRange8U, fullRange8U },
922 { "lowp_sub", "in0 - in1", sub<deUint32>, glu::PRECISION_LOWP, 8, fullRange8U, fullRange8U },
923 { "lowp_mul", "in0 * in1", mul<deUint32>, glu::PRECISION_LOWP, 8, fullRange8U, fullRange8U },
924 { "lowp_div", "in0 / in1", div<deUint32>, glu::PRECISION_LOWP, 8, fullRange8U, UVec2(1, 50u) }
927 tcu::TestCaseGroup* floatGroup = new tcu::TestCaseGroup(m_testCtx, "float", "Floating-point precision tests");
928 addChild(floatGroup);
929 for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(floatCases); ndx++)
931 floatGroup->addChild(new ShaderFloatPrecisionCase(m_context,
932 (string(floatCases[ndx].name) + "_vertex").c_str(), "",
934 floatCases[ndx].evalFunc,
935 floatCases[ndx].precision,
936 floatCases[ndx].rangeA,
937 floatCases[ndx].rangeB,
939 floatGroup->addChild(new ShaderFloatPrecisionCase(m_context,
940 (string(floatCases[ndx].name) + "_fragment").c_str(), "",
942 floatCases[ndx].evalFunc,
943 floatCases[ndx].precision,
944 floatCases[ndx].rangeA,
945 floatCases[ndx].rangeB,
949 tcu::TestCaseGroup* intGroup = new tcu::TestCaseGroup(m_testCtx, "int", "Integer precision tests");
951 for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(intCases); ndx++)
953 intGroup->addChild(new ShaderIntPrecisionCase(m_context,
954 (string(intCases[ndx].name) + "_vertex").c_str(), "",
956 intCases[ndx].evalFunc,
957 intCases[ndx].precision,
959 intCases[ndx].rangeA,
960 intCases[ndx].rangeB,
962 intGroup->addChild(new ShaderIntPrecisionCase(m_context,
963 (string(intCases[ndx].name) + "_fragment").c_str(), "",
965 intCases[ndx].evalFunc,
966 intCases[ndx].precision,
968 intCases[ndx].rangeA,
969 intCases[ndx].rangeB,
973 tcu::TestCaseGroup* uintGroup = new tcu::TestCaseGroup(m_testCtx, "uint", "Unsigned integer precision tests");
975 for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(uintCases); ndx++)
977 uintGroup->addChild(new ShaderUintPrecisionCase(m_context,
978 (string(uintCases[ndx].name) + "_vertex").c_str(), "",
980 uintCases[ndx].evalFunc,
981 uintCases[ndx].precision,
983 uintCases[ndx].rangeA,
984 uintCases[ndx].rangeB,
986 uintGroup->addChild(new ShaderUintPrecisionCase(m_context,
987 (string(uintCases[ndx].name) + "_fragment").c_str(), "",
989 uintCases[ndx].evalFunc,
990 uintCases[ndx].precision,
992 uintCases[ndx].rangeA,
993 uintCases[ndx].rangeB,