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 Dithering tests.
22 *//*--------------------------------------------------------------------*/
24 #include "es3fDitheringTests.hpp"
25 #include "gluRenderContext.hpp"
26 #include "gluDefs.hpp"
27 #include "glsFragmentOpUtil.hpp"
28 #include "gluPixelTransfer.hpp"
29 #include "tcuRenderTarget.hpp"
30 #include "tcuRGBA.hpp"
31 #include "tcuVector.hpp"
32 #include "tcuPixelFormat.hpp"
33 #include "tcuTestLog.hpp"
34 #include "tcuSurface.hpp"
35 #include "tcuCommandLine.hpp"
36 #include "deRandom.hpp"
37 #include "deStringUtil.hpp"
52 using gls::FragmentOpUtil::QuadRenderer;
53 using gls::FragmentOpUtil::Quad;
54 using tcu::PixelFormat;
65 static const char* const s_channelNames[4] = { "red", "green", "blue", "alpha" };
67 static inline IVec4 pixelFormatToIVec4 (const PixelFormat& format)
69 return IVec4(format.redBits, format.greenBits, format.blueBits, format.alphaBits);
73 static inline string choiceListStr (const vector<T>& choices)
76 for (int i = 0; i < (int)choices.size(); i++)
78 if (i == (int)choices.size()-1)
82 result += de::toString(choices[i]);
87 class DitheringCase : public tcu::TestCase
92 PATTERNTYPE_GRADIENT = 0,
93 PATTERNTYPE_UNICOLORED_QUAD,
98 DitheringCase (tcu::TestContext& testCtx, glu::RenderContext& renderCtx, const char* name, const char* description, bool isEnabled, PatternType patternType, const tcu::Vec4& color);
99 ~DitheringCase (void);
101 IterateResult iterate (void);
105 static const char* getPatternTypeName (PatternType type);
108 bool checkColor (const tcu::Vec4& inputClr, const tcu::RGBA& renderedClr, bool logErrors) const;
110 bool drawAndCheckGradient (bool isVerticallyIncreasing, const tcu::Vec4& highColor) const;
111 bool drawAndCheckUnicoloredQuad (const tcu::Vec4& color) const;
113 const glu::RenderContext& m_renderCtx;
115 const bool m_ditheringEnabled;
116 const PatternType m_patternType;
117 const tcu::Vec4 m_color;
119 const tcu::PixelFormat m_renderFormat;
121 const QuadRenderer* m_renderer;
125 const char* DitheringCase::getPatternTypeName (const PatternType type)
129 case PATTERNTYPE_GRADIENT: return "gradient";
130 case PATTERNTYPE_UNICOLORED_QUAD: return "unicolored_quad";
138 DitheringCase::DitheringCase (tcu::TestContext& testCtx, glu::RenderContext& renderCtx, const char* const name, const char* const description, const bool ditheringEnabled, const PatternType patternType, const Vec4& color)
139 : TestCase (testCtx, name, description)
140 , m_renderCtx (renderCtx)
141 , m_ditheringEnabled (ditheringEnabled)
142 , m_patternType (patternType)
144 , m_renderFormat (renderCtx.getRenderTarget().getPixelFormat())
145 , m_renderer (DE_NULL)
150 DitheringCase::~DitheringCase (void)
152 DitheringCase::deinit();
155 void DitheringCase::init (void)
157 DE_ASSERT(!m_renderer);
158 m_renderer = new QuadRenderer(m_renderCtx, glu::GLSL_VERSION_300_ES);
162 void DitheringCase::deinit (void)
165 m_renderer = DE_NULL;
168 bool DitheringCase::checkColor (const Vec4& inputClr, const tcu::RGBA& renderedClr, const bool logErrors) const
170 const IVec4 channelBits = pixelFormatToIVec4(m_renderFormat);
171 bool allChannelsOk = true;
173 for (int chanNdx = 0; chanNdx < 4; chanNdx++)
175 if (channelBits[chanNdx] == 0)
178 const int channelMax = (1 << channelBits[chanNdx]) - 1;
179 const float scaledInput = inputClr[chanNdx] * (float)channelMax;
180 const bool useRoundingMargin = deFloatAbs(scaledInput - deFloatRound(scaledInput)) < 0.0001f;
181 vector<int> channelChoices;
183 channelChoices.push_back(de::min(channelMax, (int)deFloatCeil(scaledInput)));
184 channelChoices.push_back(de::max(0, (int)deFloatCeil(scaledInput) - 1));
186 // If the input color results in a scaled value that is very close to an integer, account for a little bit of possible inaccuracy.
187 if (useRoundingMargin)
189 if (scaledInput > deFloatRound(scaledInput))
190 channelChoices.push_back((int)deFloatCeil(scaledInput) - 2);
192 channelChoices.push_back((int)deFloatCeil(scaledInput) + 1);
195 std::sort(channelChoices.begin(), channelChoices.end());
198 const int renderedClrInFormat = (int)deFloatRound((float)(renderedClr.toIVec()[chanNdx] * channelMax) / 255.0f);
199 bool goodChannel = false;
201 for (int i = 0; i < (int)channelChoices.size(); i++)
203 if (renderedClrInFormat == channelChoices[i])
214 m_testCtx.getLog() << TestLog::Message
215 << "Failure: " << channelBits[chanNdx] << "-bit " << s_channelNames[chanNdx] << " channel is " << renderedClrInFormat
216 << ", should be " << choiceListStr(channelChoices)
217 << " (corresponding fragment color channel is " << inputClr[chanNdx] << ")"
218 << TestLog::EndMessage
220 << "Note: " << inputClr[chanNdx] << " * (" << channelMax + 1 << "-1) = " << scaledInput
221 << TestLog::EndMessage;
223 if (useRoundingMargin)
225 m_testCtx.getLog() << TestLog::Message
226 << "Note: one extra color candidate was allowed because fragmentColorChannel * (2^bits-1) is close to an integer"
227 << TestLog::EndMessage;
231 allChannelsOk = false;
236 return allChannelsOk;
239 bool DitheringCase::drawAndCheckGradient (const bool isVerticallyIncreasing, const Vec4& highColor) const
241 TestLog& log = m_testCtx.getLog();
242 Random rnd (deStringHash(getName()));
243 const int maxViewportWid = 256;
244 const int maxViewportHei = 256;
245 const int viewportWid = de::min(m_renderCtx.getRenderTarget().getWidth(), maxViewportWid);
246 const int viewportHei = de::min(m_renderCtx.getRenderTarget().getHeight(), maxViewportHei);
247 const int viewportX = rnd.getInt(0, m_renderCtx.getRenderTarget().getWidth() - viewportWid);
248 const int viewportY = rnd.getInt(0, m_renderCtx.getRenderTarget().getHeight() - viewportHei);
249 const Vec4 quadClr0 (0.0f, 0.0f, 0.0f, 0.0f);
250 const Vec4& quadClr1 = highColor;
252 Surface renderedImg (viewportWid, viewportHei);
254 GLU_CHECK_CALL(glViewport(viewportX, viewportY, viewportWid, viewportHei));
256 log << TestLog::Message << "Dithering is " << (m_ditheringEnabled ? "enabled" : "disabled") << TestLog::EndMessage;
258 if (m_ditheringEnabled)
259 GLU_CHECK_CALL(glEnable(GL_DITHER));
261 GLU_CHECK_CALL(glDisable(GL_DITHER));
263 log << TestLog::Message << "Drawing a " << (isVerticallyIncreasing ? "vertically" : "horizontally") << " increasing gradient" << TestLog::EndMessage;
265 quad.color[0] = quadClr0;
266 quad.color[1] = isVerticallyIncreasing ? quadClr1 : quadClr0;
267 quad.color[2] = isVerticallyIncreasing ? quadClr0 : quadClr1;
268 quad.color[3] = quadClr1;
270 m_renderer->render(quad);
272 glu::readPixels(m_renderCtx, viewportX, viewportY, renderedImg.getAccess());
273 GLU_CHECK_MSG("glReadPixels()");
275 log << TestLog::Image(isVerticallyIncreasing ? "VerGradient" : "HorGradient",
276 isVerticallyIncreasing ? "Vertical gradient" : "Horizontal gradient",
279 // Validate, at each pixel, that each color channel is one of its two allowed values.
282 Surface errorMask (viewportWid, viewportHei);
283 bool colorChoicesOk = true;
285 for (int y = 0; y < renderedImg.getHeight(); y++)
287 for (int x = 0; x < renderedImg.getWidth(); x++)
289 const float inputF = ((float)(isVerticallyIncreasing ? y : x) + 0.5f) / (float)(isVerticallyIncreasing ? renderedImg.getHeight() : renderedImg.getWidth());
290 const Vec4 inputClr = (1.0f-inputF)*quadClr0 + inputF*quadClr1;
292 if (!checkColor(inputClr, renderedImg.getPixel(x, y), colorChoicesOk))
294 errorMask.setPixel(x, y, tcu::RGBA::red());
298 log << TestLog::Message << "First failure at pixel (" << x << ", " << y << ") (not printing further errors)" << TestLog::EndMessage;
299 colorChoicesOk = false;
303 errorMask.setPixel(x, y, tcu::RGBA::green());
309 log << TestLog::Image("ColorChoiceErrorMask", "Error mask for color choices", errorMask);
314 // When dithering is disabled, the color selection must be coordinate-independent - i.e. the colors must be constant in the gradient's constant direction.
316 if (!m_ditheringEnabled)
318 const int increasingDirectionSize = isVerticallyIncreasing ? renderedImg.getHeight() : renderedImg.getWidth();
319 const int constantDirectionSize = isVerticallyIncreasing ? renderedImg.getWidth() : renderedImg.getHeight();
321 for (int incrPos = 0; incrPos < increasingDirectionSize; incrPos++)
323 bool colorHasChanged = false;
324 tcu::RGBA prevConstantDirectionPix;
326 for (int constPos = 0; constPos < constantDirectionSize; constPos++)
328 const int x = isVerticallyIncreasing ? constPos : incrPos;
329 const int y = isVerticallyIncreasing ? incrPos : constPos;
330 const tcu::RGBA clr = renderedImg.getPixel(x, y);
332 if (constPos > 0 && clr != prevConstantDirectionPix)
336 log << TestLog::Message
337 << "Failure: colors should be constant per " << (isVerticallyIncreasing ? "row" : "column")
338 << " (since dithering is disabled), but the color at position (" << x << ", " << y << ") is " << clr
339 << " and does not equal the color at (" << (isVerticallyIncreasing ? x-1 : x) << ", " << (isVerticallyIncreasing ? y : y-1) << "), which is " << prevConstantDirectionPix
340 << TestLog::EndMessage;
345 colorHasChanged = true;
348 prevConstantDirectionPix = clr;
356 bool DitheringCase::drawAndCheckUnicoloredQuad (const Vec4& quadColor) const
358 TestLog& log = m_testCtx.getLog();
359 Random rnd (deStringHash(getName()));
360 const int maxViewportWid = 32;
361 const int maxViewportHei = 32;
362 const int viewportWid = de::min(m_renderCtx.getRenderTarget().getWidth(), maxViewportWid);
363 const int viewportHei = de::min(m_renderCtx.getRenderTarget().getHeight(), maxViewportHei);
364 const int viewportX = rnd.getInt(0, m_renderCtx.getRenderTarget().getWidth() - viewportWid);
365 const int viewportY = rnd.getInt(0, m_renderCtx.getRenderTarget().getHeight() - viewportHei);
367 Surface renderedImg (viewportWid, viewportHei);
369 GLU_CHECK_CALL(glViewport(viewportX, viewportY, viewportWid, viewportHei));
371 log << TestLog::Message << "Dithering is " << (m_ditheringEnabled ? "enabled" : "disabled") << TestLog::EndMessage;
373 if (m_ditheringEnabled)
374 GLU_CHECK_CALL(glEnable(GL_DITHER));
376 GLU_CHECK_CALL(glDisable(GL_DITHER));
378 log << TestLog::Message << "Drawing an unicolored quad with color " << quadColor << TestLog::EndMessage;
380 quad.color[0] = quadColor;
381 quad.color[1] = quadColor;
382 quad.color[2] = quadColor;
383 quad.color[3] = quadColor;
385 m_renderer->render(quad);
387 glu::readPixels(m_renderCtx, viewportX, viewportY, renderedImg.getAccess());
388 GLU_CHECK_MSG("glReadPixels()");
390 log << TestLog::Image(("Quad" + de::toString(m_iteration)).c_str(), ("Quad " + de::toString(m_iteration)).c_str(), renderedImg);
392 // Validate, at each pixel, that each color channel is one of its two allowed values.
395 Surface errorMask (viewportWid, viewportHei);
396 bool colorChoicesOk = true;
398 for (int y = 0; y < renderedImg.getHeight(); y++)
400 for (int x = 0; x < renderedImg.getWidth(); x++)
402 if (!checkColor(quadColor, renderedImg.getPixel(x, y), colorChoicesOk))
404 errorMask.setPixel(x, y, tcu::RGBA::red());
408 log << TestLog::Message << "First failure at pixel (" << x << ", " << y << ") (not printing further errors)" << TestLog::EndMessage;
409 colorChoicesOk = false;
413 errorMask.setPixel(x, y, tcu::RGBA::green());
419 log << TestLog::Image("ColorChoiceErrorMask", "Error mask for color choices", errorMask);
424 // When dithering is disabled, the color selection must be coordinate-independent - i.e. the entire rendered image must be unicolored.
426 if (!m_ditheringEnabled)
428 const tcu::RGBA renderedClr00 = renderedImg.getPixel(0, 0);
430 for (int y = 0; y < renderedImg.getHeight(); y++)
432 for (int x = 0; x < renderedImg.getWidth(); x++)
434 const tcu::RGBA curClr = renderedImg.getPixel(x, y);
436 if (curClr != renderedClr00)
438 log << TestLog::Message
439 << "Failure: color at (" << x << ", " << y << ") is " << curClr
440 << " and does not equal the color at (0, 0), which is " << renderedClr00
441 << TestLog::EndMessage;
452 DitheringCase::IterateResult DitheringCase::iterate (void)
454 if (m_patternType == PATTERNTYPE_GRADIENT)
456 // Draw horizontal and vertical gradients.
458 DE_ASSERT(m_iteration < 2);
460 const bool success = drawAndCheckGradient(m_iteration == 1, m_color);
464 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
468 if (m_iteration == 1)
470 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
474 else if (m_patternType == PATTERNTYPE_UNICOLORED_QUAD)
476 const int numQuads = m_testCtx.getCommandLine().getTestIterationCount() > 0 ? m_testCtx.getCommandLine().getTestIterationCount() : 30;
478 DE_ASSERT(m_iteration < numQuads);
480 const Vec4 quadColor = (float)m_iteration / (float)(numQuads-1) * m_color;
481 const bool success = drawAndCheckUnicoloredQuad(quadColor);
485 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
489 if (m_iteration == numQuads - 1)
491 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
503 DitheringTests::DitheringTests (Context& context)
504 : TestCaseGroup(context, "dither", "Dithering tests")
508 DitheringTests::~DitheringTests (void)
512 void DitheringTests::init (void)
520 { "white", Vec4(1.0f, 1.0f, 1.0f, 1.0f) },
521 { "red", Vec4(1.0f, 0.0f, 0.0f, 1.0f) },
522 { "green", Vec4(0.0f, 1.0f, 0.0f, 1.0f) },
523 { "blue", Vec4(0.0f, 0.0f, 1.0f, 1.0f) },
524 { "alpha", Vec4(0.0f, 0.0f, 0.0f, 1.0f) }
527 for (int ditheringEnabledI = 0; ditheringEnabledI <= 1; ditheringEnabledI++)
529 const bool ditheringEnabled = ditheringEnabledI != 0;
530 TestCaseGroup* const group = new TestCaseGroup(m_context, ditheringEnabled ? "enabled" : "disabled", "");
533 for (int patternTypeI = 0; patternTypeI < DitheringCase::PATTERNTYPE_LAST; patternTypeI++)
535 for (int caseColorNdx = 0; caseColorNdx < DE_LENGTH_OF_ARRAY(caseColors); caseColorNdx++)
537 const DitheringCase::PatternType patternType = (DitheringCase::PatternType)patternTypeI;
538 const string caseName = string("") + DitheringCase::getPatternTypeName(patternType) + "_" + caseColors[caseColorNdx].name;
540 group->addChild(new DitheringCase(m_context.getTestContext(), m_context.getRenderContext(), caseName.c_str(), "", ditheringEnabled, patternType, caseColors[caseColorNdx].color));