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.
22 *//*--------------------------------------------------------------------*/
24 #include "es3fBlendTests.hpp"
25 #include "gluStrUtil.hpp"
26 #include "glsFragmentOpUtil.hpp"
27 #include "gluPixelTransfer.hpp"
28 #include "tcuPixelFormat.hpp"
29 #include "tcuTexture.hpp"
30 #include "tcuTextureUtil.hpp"
31 #include "tcuImageCompare.hpp"
32 #include "tcuRenderTarget.hpp"
33 #include "tcuTestLog.hpp"
34 #include "deRandom.hpp"
35 #include "rrFragmentOperations.hpp"
36 #include "sglrReferenceUtils.hpp"
46 using gls::FragmentOpUtil::Quad;
47 using gls::FragmentOpUtil::IntegerQuad;
48 using gls::FragmentOpUtil::QuadRenderer;
49 using gls::FragmentOpUtil::ReferenceQuadRenderer;
50 using glu::getBlendEquationName;
51 using glu::getBlendFactorName;
55 using tcu::TextureLevel;
56 using tcu::TextureFormat;
65 static const int MAX_VIEWPORT_WIDTH = 64;
66 static const int MAX_VIEWPORT_HEIGHT = 64;
68 // \note src and dst can point to same memory as long as there is 1-to-1 correspondence between
70 static void sRGBAToLinear (const tcu::PixelBufferAccess& dst, const tcu::ConstPixelBufferAccess& src)
72 const int width = src.getWidth();
73 const int height = src.getHeight();
75 for (int y = 0; y < height; y++)
76 for (int x = 0; x < width; x++)
77 dst.setPixel(tcu::sRGBToLinear(src.getPixel(x, y)), x, y);
90 BlendParams (GLenum equationRGB_,
93 GLenum equationAlpha_,
97 : equationRGB (equationRGB_)
98 , srcFuncRGB (srcFuncRGB_)
99 , dstFuncRGB (dstFuncRGB_)
100 , equationAlpha (equationAlpha_)
101 , srcFuncAlpha (srcFuncAlpha_)
102 , dstFuncAlpha (dstFuncAlpha_)
103 , blendColor (blendColor_)
108 class BlendCase : public TestCase
111 BlendCase (Context& context,
114 const vector<BlendParams>& paramSets,
122 IterateResult iterate (void);
125 BlendCase (const BlendCase& other);
126 BlendCase& operator= (const BlendCase& other);
128 vector<BlendParams> m_paramSets;
129 int m_curParamSetNdx;
135 QuadRenderer* m_renderer;
136 ReferenceQuadRenderer* m_referenceRenderer;
137 TextureLevel* m_refColorBuffer;
140 IntegerQuad m_firstQuadInt;
141 IntegerQuad m_secondQuadInt;
146 int m_viewportHeight;
149 BlendCase::BlendCase (Context& context,
152 const vector<BlendParams>& paramSets,
154 : TestCase (context, name, desc)
155 , m_paramSets (paramSets)
156 , m_curParamSetNdx (0)
157 , m_useSrgbFbo (useSrgbFbo)
160 , m_renderer (DE_NULL)
161 , m_referenceRenderer (DE_NULL)
162 , m_refColorBuffer (DE_NULL)
163 , m_renderWidth (m_useSrgbFbo ? 2*MAX_VIEWPORT_WIDTH : m_context.getRenderTarget().getWidth())
164 , m_renderHeight (m_useSrgbFbo ? 2*MAX_VIEWPORT_HEIGHT : m_context.getRenderTarget().getHeight())
165 , m_viewportWidth (0)
166 , m_viewportHeight (0)
168 DE_ASSERT(!m_paramSets.empty());
171 void BlendCase::init (void)
173 bool useRGB = !m_useSrgbFbo && m_context.getRenderTarget().getPixelFormat().alphaBits == 0;
175 static const Vec4 baseGradientColors[4] =
177 Vec4(0.0f, 0.5f, 1.0f, 0.5f),
178 Vec4(0.5f, 0.0f, 0.5f, 1.0f),
179 Vec4(0.5f, 1.0f, 0.5f, 0.0f),
180 Vec4(1.0f, 0.5f, 0.0f, 0.5f)
183 DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(m_firstQuad.color) == DE_LENGTH_OF_ARRAY(m_firstQuadInt.color));
184 for (int i = 0; i < DE_LENGTH_OF_ARRAY(m_firstQuad.color); i++)
186 m_firstQuad.color[i] = (baseGradientColors[i] - 0.5f) * 0.2f + 0.5f;
187 m_firstQuadInt.color[i] = m_firstQuad.color[i];
189 m_secondQuad.color[i] = (Vec4(1.0f) - baseGradientColors[i] - 0.5f) * 1.0f + 0.5f;
190 m_secondQuadInt.color[i] = m_secondQuad.color[i];
193 m_viewportWidth = de::min<int>(m_renderWidth, MAX_VIEWPORT_WIDTH);
194 m_viewportHeight = de::min<int>(m_renderHeight, MAX_VIEWPORT_HEIGHT);
196 m_firstQuadInt.posA = tcu::IVec2(0, 0);
197 m_secondQuadInt.posA = tcu::IVec2(0, 0);
198 m_firstQuadInt.posB = tcu::IVec2(m_viewportWidth-1, m_viewportHeight-1);
199 m_secondQuadInt.posB = tcu::IVec2(m_viewportWidth-1, m_viewportHeight-1);
201 DE_ASSERT(!m_renderer);
202 DE_ASSERT(!m_referenceRenderer);
203 DE_ASSERT(!m_refColorBuffer);
205 m_renderer = new QuadRenderer(m_context.getRenderContext(), glu::GLSL_VERSION_300_ES);
206 m_referenceRenderer = new ReferenceQuadRenderer;
207 m_refColorBuffer = new TextureLevel(TextureFormat(m_useSrgbFbo ? TextureFormat::sRGBA : useRGB ? TextureFormat::RGB : TextureFormat::RGBA, TextureFormat::UNORM_INT8),
208 m_viewportWidth, m_viewportHeight);
210 m_curParamSetNdx = 0;
214 m_testCtx.getLog() << TestLog::Message << "Using FBO of size (" << m_renderWidth << ", " << m_renderHeight << ") with format GL_SRGB8_ALPHA8" << TestLog::EndMessage;
216 GLU_CHECK_CALL(glGenRenderbuffers(1, &m_colorRbo));
217 GLU_CHECK_CALL(glBindRenderbuffer(GL_RENDERBUFFER, m_colorRbo));
218 GLU_CHECK_CALL(glRenderbufferStorage(GL_RENDERBUFFER, GL_SRGB8_ALPHA8, m_renderWidth, m_renderHeight));
220 GLU_CHECK_CALL(glGenFramebuffers(1, &m_fbo));
221 GLU_CHECK_CALL(glBindFramebuffer(GL_FRAMEBUFFER, m_fbo));
222 GLU_CHECK_CALL(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_colorRbo));
226 BlendCase::~BlendCase (void)
231 void BlendCase::deinit (void)
234 delete m_referenceRenderer;
235 delete m_refColorBuffer;
237 m_renderer = DE_NULL;
238 m_referenceRenderer = DE_NULL;
239 m_refColorBuffer = DE_NULL;
241 GLU_CHECK_CALL(glBindRenderbuffer(GL_RENDERBUFFER, 0));
242 GLU_CHECK_CALL(glBindFramebuffer(GL_FRAMEBUFFER, 0));
246 GLU_CHECK_CALL(glDeleteRenderbuffers(1, &m_colorRbo));
251 GLU_CHECK_CALL(glDeleteFramebuffers(1, &m_fbo));
256 BlendCase::IterateResult BlendCase::iterate (void)
258 de::Random rnd (deStringHash(getName()) ^ deInt32Hash(m_curParamSetNdx));
259 int viewportX = rnd.getInt(0, m_renderWidth - m_viewportWidth);
260 int viewportY = rnd.getInt(0, m_renderHeight - m_viewportHeight);
261 TextureLevel renderedImg (TextureFormat(m_useSrgbFbo ? TextureFormat::sRGBA : TextureFormat::RGBA, TextureFormat::UNORM_INT8), m_viewportWidth, m_viewportHeight);
262 TextureLevel referenceImg (renderedImg.getFormat(), m_viewportWidth, m_viewportHeight);
263 TestLog& log (m_testCtx.getLog());
264 const BlendParams& paramSet = m_paramSets[m_curParamSetNdx];
265 rr::FragmentOperationState referenceState;
267 // Log the blend parameters.
269 log << TestLog::Message << "RGB equation = " << getBlendEquationName(paramSet.equationRGB) << TestLog::EndMessage;
270 log << TestLog::Message << "RGB src func = " << getBlendFactorName(paramSet.srcFuncRGB) << TestLog::EndMessage;
271 log << TestLog::Message << "RGB dst func = " << getBlendFactorName(paramSet.dstFuncRGB) << TestLog::EndMessage;
272 log << TestLog::Message << "Alpha equation = " << getBlendEquationName(paramSet.equationAlpha) << TestLog::EndMessage;
273 log << TestLog::Message << "Alpha src func = " << getBlendFactorName(paramSet.srcFuncAlpha) << TestLog::EndMessage;
274 log << TestLog::Message << "Alpha dst func = " << getBlendFactorName(paramSet.dstFuncAlpha) << TestLog::EndMessage;
275 log << TestLog::Message << "Blend color = (" << paramSet.blendColor.x() << ", " << paramSet.blendColor.y() << ", " << paramSet.blendColor.z() << ", " << paramSet.blendColor.w() << ")" << TestLog::EndMessage;
279 GLU_CHECK_CALL(glBlendEquationSeparate(paramSet.equationRGB, paramSet.equationAlpha));
280 GLU_CHECK_CALL(glBlendFuncSeparate(paramSet.srcFuncRGB, paramSet.dstFuncRGB, paramSet.srcFuncAlpha, paramSet.dstFuncAlpha));
281 GLU_CHECK_CALL(glBlendColor(paramSet.blendColor.x(), paramSet.blendColor.y(), paramSet.blendColor.z(), paramSet.blendColor.w()));
283 // Set reference state.
285 referenceState.blendRGBState.equation = sglr::rr_util::mapGLBlendEquation(paramSet.equationRGB);
286 referenceState.blendRGBState.srcFunc = sglr::rr_util::mapGLBlendFunc(paramSet.srcFuncRGB);
287 referenceState.blendRGBState.dstFunc = sglr::rr_util::mapGLBlendFunc(paramSet.dstFuncRGB);
288 referenceState.blendAState.equation = sglr::rr_util::mapGLBlendEquation(paramSet.equationAlpha);
289 referenceState.blendAState.srcFunc = sglr::rr_util::mapGLBlendFunc(paramSet.srcFuncAlpha);
290 referenceState.blendAState.dstFunc = sglr::rr_util::mapGLBlendFunc(paramSet.dstFuncAlpha);
291 referenceState.blendColor = paramSet.blendColor;
296 glViewport(viewportX, viewportY, m_viewportWidth, m_viewportHeight);
297 m_renderer->render(m_firstQuad);
299 m_renderer->render(m_secondQuad);
304 const tcu::PixelBufferAccess nullAccess = tcu::PixelBufferAccess();
306 referenceState.blendMode = rr::BLENDMODE_NONE;
307 m_referenceRenderer->render(gls::FragmentOpUtil::getMultisampleAccess(m_refColorBuffer->getAccess()), nullAccess /* no depth */, nullAccess /* no stencil */, m_firstQuadInt, referenceState);
308 referenceState.blendMode = rr::BLENDMODE_STANDARD;
309 m_referenceRenderer->render(gls::FragmentOpUtil::getMultisampleAccess(m_refColorBuffer->getAccess()), nullAccess /* no depth */, nullAccess /* no stencil */, m_secondQuadInt, referenceState);
311 // Copy to reference (expansion to RGBA happens here if necessary)
312 copy(referenceImg, m_refColorBuffer->getAccess());
316 glu::readPixels(m_context.getRenderContext(), viewportX, viewportY, renderedImg.getAccess());
319 // \note In sRGB cases, convert to linear space for comparison.
323 sRGBAToLinear(renderedImg, renderedImg);
324 sRGBAToLinear(referenceImg, referenceImg);
327 UVec4 compareThreshold = (m_useSrgbFbo ? tcu::PixelFormat(8, 8, 8, 8) : m_context.getRenderTarget().getPixelFormat()).getColorThreshold().toIVec().asUint()
328 * UVec4(5) / UVec4(2) + UVec4(m_useSrgbFbo ? 5 : 2); // \note Non-scientific ad hoc formula. Need big threshold when few color bits; blending brings extra inaccuracy.
330 bool comparePass = tcu::intThresholdCompare(m_testCtx.getLog(), "CompareResult", "Image Comparison Result",
331 referenceImg.getAccess(), renderedImg.getAccess(),
332 compareThreshold, tcu::COMPARE_LOG_RESULT);
334 // Fail now if images don't match.
338 m_context.getTestContext().setTestResult(QP_TEST_RESULT_FAIL, "Image compare failed");
342 // Continue if param sets still remain in m_paramSets; otherwise stop.
346 if (m_curParamSetNdx < (int)m_paramSets.size())
350 m_context.getTestContext().setTestResult(QP_TEST_RESULT_PASS, "Passed");
355 BlendTests::BlendTests (Context& context)
356 : TestCaseGroup(context, "blend", "Blend tests")
360 BlendTests::~BlendTests (void)
364 void BlendTests::init (void)
372 static const EnumGL blendEquations[] =
374 { GL_FUNC_ADD, "add" },
375 { GL_FUNC_SUBTRACT, "subtract" },
376 { GL_FUNC_REVERSE_SUBTRACT, "reverse_subtract" },
381 static const EnumGL blendFunctions[] =
385 { GL_SRC_COLOR, "src_color" },
386 { GL_ONE_MINUS_SRC_COLOR, "one_minus_src_color" },
387 { GL_DST_COLOR, "dst_color" },
388 { GL_ONE_MINUS_DST_COLOR, "one_minus_dst_color" },
389 { GL_SRC_ALPHA, "src_alpha" },
390 { GL_ONE_MINUS_SRC_ALPHA, "one_minus_src_alpha" },
391 { GL_DST_ALPHA, "dst_alpha" },
392 { GL_ONE_MINUS_DST_ALPHA, "one_minus_dst_alpha" },
393 { GL_CONSTANT_COLOR, "constant_color" },
394 { GL_ONE_MINUS_CONSTANT_COLOR, "one_minus_constant_color" },
395 { GL_CONSTANT_ALPHA, "constant_alpha" },
396 { GL_ONE_MINUS_CONSTANT_ALPHA, "one_minus_constant_alpha" },
397 { GL_SRC_ALPHA_SATURATE, "src_alpha_saturate" }
400 const Vec4 defaultBlendColor(0.2f, 0.4f, 0.6f, 0.8f);
402 for (int useSrgbFboI = 0; useSrgbFboI <= 1; useSrgbFboI++)
404 bool useSrgbFbo = useSrgbFboI != 0;
405 TestCaseGroup* fbGroup = new TestCaseGroup(m_context, useSrgbFbo ? "fbo_srgb" : "default_framebuffer", useSrgbFbo ? "Use a FBO with GL_SRGB8_ALPHA8" : "Use the default framebuffer");
408 // Test all blend equation, src blend function, dst blend function combinations. RGB and alpha modes are the same.
411 TestCaseGroup* group = new TestCaseGroup(m_context, "equation_src_func_dst_func", "Combinations of Blend Equations and Functions");
412 fbGroup->addChild(group);
414 for (int equationNdx = 0; equationNdx < DE_LENGTH_OF_ARRAY(blendEquations); equationNdx++)
415 for (int srcFuncNdx = 0; srcFuncNdx < DE_LENGTH_OF_ARRAY(blendFunctions); srcFuncNdx++)
416 for (int dstFuncNdx = 0; dstFuncNdx < DE_LENGTH_OF_ARRAY(blendFunctions); dstFuncNdx++)
418 const EnumGL& eq = blendEquations[equationNdx];
419 const EnumGL& src = blendFunctions[srcFuncNdx];
420 const EnumGL& dst = blendFunctions[dstFuncNdx];
422 if ((eq.glValue == GL_MIN || eq.glValue == GL_MAX) && (srcFuncNdx > 0 || dstFuncNdx > 0)) // MIN and MAX don't depend on factors.
425 string name = eq.nameStr;
426 string description = string("") +
427 "Equations " + getBlendEquationName(eq.glValue) +
428 ", src funcs " + getBlendFactorName(src.glValue) +
429 ", dst funcs " + getBlendFactorName(dst.glValue);
431 if (eq.glValue != GL_MIN && eq.glValue != GL_MAX)
432 name += string("") + "_" + src.nameStr + "_" + dst.nameStr;
434 vector<BlendParams> paramSets;
435 paramSets.push_back(BlendParams(eq.glValue, src.glValue, dst.glValue, eq.glValue, src.glValue, dst.glValue, defaultBlendColor));
437 group->addChild(new BlendCase(m_context, name.c_str(), description.c_str(), paramSets, useSrgbFbo));
441 // Test all RGB src, alpha src and RGB dst, alpha dst combinations. Equations are ADD.
442 // \note For all RGB src, alpha src combinations, also test a couple of different RGBA dst functions, and vice versa.
445 TestCaseGroup* mainGroup = new TestCaseGroup(m_context, "rgb_func_alpha_func", "Combinations of RGB and Alpha Functions");
446 fbGroup->addChild(mainGroup);
447 TestCaseGroup* srcGroup = new TestCaseGroup(m_context, "src", "Source functions");
448 TestCaseGroup* dstGroup = new TestCaseGroup(m_context, "dst", "Destination functions");
449 mainGroup->addChild(srcGroup);
450 mainGroup->addChild(dstGroup);
452 for (int isDstI = 0; isDstI <= 1; isDstI++)
453 for (int rgbFuncNdx = 0; rgbFuncNdx < DE_LENGTH_OF_ARRAY(blendFunctions); rgbFuncNdx++)
454 for (int alphaFuncNdx = 0; alphaFuncNdx < DE_LENGTH_OF_ARRAY(blendFunctions); alphaFuncNdx++)
456 bool isSrc = isDstI == 0;
457 TestCaseGroup* curGroup = isSrc ? srcGroup : dstGroup;
458 const EnumGL& funcRGB = blendFunctions[rgbFuncNdx];
459 const EnumGL& funcAlpha = blendFunctions[alphaFuncNdx];
460 const char* dstOrSrcStr = isSrc ? "src" : "dst";
462 string name = string("") + funcRGB.nameStr + "_" + funcAlpha.nameStr;
463 string description = string("") +
464 "RGB " + dstOrSrcStr + " func " + getBlendFactorName(funcRGB.glValue) +
465 ", alpha " + dstOrSrcStr + " func " + getBlendFactorName(funcAlpha.glValue);
467 // First, make param sets as if this was a src case.
469 vector<BlendParams> paramSets;
470 paramSets.push_back(BlendParams(GL_FUNC_ADD, funcRGB.glValue, GL_ONE, GL_FUNC_ADD, funcAlpha.glValue, GL_ONE, defaultBlendColor));
471 paramSets.push_back(BlendParams(GL_FUNC_ADD, funcRGB.glValue, GL_ZERO, GL_FUNC_ADD, funcAlpha.glValue, GL_ZERO, defaultBlendColor));
472 paramSets.push_back(BlendParams(GL_FUNC_ADD, funcRGB.glValue, GL_SRC_COLOR, GL_FUNC_ADD, funcAlpha.glValue, GL_SRC_COLOR, defaultBlendColor));
473 paramSets.push_back(BlendParams(GL_FUNC_ADD, funcRGB.glValue, GL_DST_COLOR, GL_FUNC_ADD, funcAlpha.glValue, GL_DST_COLOR, defaultBlendColor));
475 // Swap src and dst params if this is a dst case.
479 for (int i = 0; i < (int)paramSets.size(); i++)
481 std::swap(paramSets[i].srcFuncRGB, paramSets[i].dstFuncRGB);
482 std::swap(paramSets[i].srcFuncAlpha, paramSets[i].dstFuncAlpha);
486 curGroup->addChild(new BlendCase(m_context, name.c_str(), description.c_str(), paramSets, useSrgbFbo));
490 // Test all RGB and alpha equation combinations. Src and dst funcs are ONE for both.
493 TestCaseGroup* group = new TestCaseGroup(m_context, "rgb_equation_alpha_equation", "Combinations of RGB and Alpha Equation Combinations");
494 fbGroup->addChild(group);
496 for (int equationRGBNdx = 0; equationRGBNdx < DE_LENGTH_OF_ARRAY(blendEquations); equationRGBNdx++)
497 for (int equationAlphaNdx = 0; equationAlphaNdx < DE_LENGTH_OF_ARRAY(blendEquations); equationAlphaNdx++)
499 const EnumGL& eqRGB = blendEquations[equationRGBNdx];
500 const EnumGL& eqAlpha = blendEquations[equationAlphaNdx];
502 string name = string("") + eqRGB.nameStr + "_" + eqAlpha.nameStr;
503 string description = string("") +
504 "RGB equation " + getBlendEquationName(eqRGB.glValue) +
505 ", alpha equation " + getBlendEquationName(eqAlpha.glValue);
507 vector<BlendParams> paramSets;
508 paramSets.push_back(BlendParams(eqRGB.glValue, GL_ONE, GL_ONE, eqAlpha.glValue, GL_ONE, GL_ONE, defaultBlendColor));
510 group->addChild(new BlendCase(m_context, name.c_str(), description.c_str(), paramSets, useSrgbFbo));