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 Randomized per-fragment operation tests.
22 *//*--------------------------------------------------------------------*/
24 #include "es2fRandomFragmentOpTests.hpp"
25 #include "glsFragmentOpUtil.hpp"
26 #include "glsInteractionTestUtil.hpp"
27 #include "tcuRenderTarget.hpp"
28 #include "tcuTestLog.hpp"
29 #include "tcuSurface.hpp"
30 #include "tcuCommandLine.hpp"
31 #include "tcuImageCompare.hpp"
32 #include "tcuVectorUtil.hpp"
33 #include "tcuTextureUtil.hpp"
34 #include "gluPixelTransfer.hpp"
35 #include "gluCallLogWrapper.hpp"
36 #include "gluRenderContext.hpp"
37 #include "deStringUtil.hpp"
38 #include "deRandom.hpp"
40 #include "glwFunctions.hpp"
41 #include "glwEnums.hpp"
42 #include "rrFragmentOperations.hpp"
43 #include "sglrReferenceUtils.hpp"
65 NUM_CALLS_PER_ITERATION = 3,
66 NUM_ITERATIONS_PER_CASE = 10
69 static const tcu::Vec4 CLEAR_COLOR (0.25f, 0.5f, 0.75f, 1.0f);
70 static const float CLEAR_DEPTH = 1.0f;
71 static const int CLEAR_STENCIL = 0;
72 static const bool ENABLE_CALL_LOG = true;
74 using namespace gls::FragmentOpUtil;
75 using namespace gls::InteractionTestUtil;
77 void translateStencilState (const StencilState& src, rr::StencilState& dst)
79 dst.func = sglr::rr_util::mapGLTestFunc(src.function);
80 dst.ref = src.reference;
81 dst.compMask = src.compareMask;
82 dst.sFail = sglr::rr_util::mapGLStencilOp(src.stencilFailOp);
83 dst.dpFail = sglr::rr_util::mapGLStencilOp(src.depthFailOp);
84 dst.dpPass = sglr::rr_util::mapGLStencilOp(src.depthPassOp);
85 dst.writeMask = src.writeMask;
88 void translateBlendState (const BlendState& src, rr::BlendState& dst)
90 dst.equation = sglr::rr_util::mapGLBlendEquation(src.equation);
91 dst.srcFunc = sglr::rr_util::mapGLBlendFunc(src.srcFunc);
92 dst.dstFunc = sglr::rr_util::mapGLBlendFunc(src.dstFunc);
95 void translateState (const RenderState& src, rr::FragmentOperationState& dst, const tcu::RenderTarget& renderTarget)
97 bool hasDepth = renderTarget.getDepthBits() > 0;
98 bool hasStencil = renderTarget.getStencilBits() > 0;
100 dst.scissorTestEnabled = src.scissorTestEnabled;
101 dst.scissorRectangle = src.scissorRectangle;
102 dst.stencilTestEnabled = hasStencil && src.stencilTestEnabled;
103 dst.depthTestEnabled = hasDepth && src.depthTestEnabled;
104 dst.blendMode = src.blendEnabled ? rr::BLENDMODE_STANDARD : rr::BLENDMODE_NONE;
105 dst.numStencilBits = renderTarget.getStencilBits();
107 dst.colorMask = src.colorMask;
109 if (dst.depthTestEnabled)
111 dst.depthFunc = sglr::rr_util::mapGLTestFunc(src.depthFunc);
112 dst.depthMask = src.depthWriteMask;
115 if (dst.stencilTestEnabled)
117 translateStencilState(src.stencil[rr::FACETYPE_BACK], dst.stencilStates[rr::FACETYPE_BACK]);
118 translateStencilState(src.stencil[rr::FACETYPE_FRONT], dst.stencilStates[rr::FACETYPE_FRONT]);
121 if (src.blendEnabled)
123 translateBlendState(src.blendRGBState, dst.blendRGBState);
124 translateBlendState(src.blendAState, dst.blendAState);
125 dst.blendColor = tcu::clamp(src.blendColor, Vec4(0.0f), Vec4(1.0f));
129 static void renderQuad (const glw::Functions& gl, gls::FragmentOpUtil::QuadRenderer& renderer, const gls::FragmentOpUtil::IntegerQuad& quad, int baseX, int baseY)
131 gls::FragmentOpUtil::Quad translated;
133 std::copy(DE_ARRAY_BEGIN(quad.color), DE_ARRAY_END(quad.color), DE_ARRAY_BEGIN(translated.color));
135 bool flipX = quad.posB.x() < quad.posA.x();
136 bool flipY = quad.posB.y() < quad.posA.y();
137 int viewportX = de::min(quad.posA.x(), quad.posB.x());
138 int viewportY = de::min(quad.posA.y(), quad.posB.y());
139 int viewportW = de::abs(quad.posA.x()-quad.posB.x())+1;
140 int viewportH = de::abs(quad.posA.y()-quad.posB.y())+1;
142 translated.posA = Vec2(flipX ? 1.0f : -1.0f, flipY ? 1.0f : -1.0f);
143 translated.posB = Vec2(flipX ? -1.0f : 1.0f, flipY ? -1.0f : 1.0f);
145 // \todo [2012-12-18 pyry] Pass in DepthRange parameters.
146 for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(quad.depth); ndx++)
147 translated.depth[ndx] = quad.depth[ndx]*2.0f - 1.0f;
149 gl.viewport(baseX+viewportX, baseY+viewportY, viewportW, viewportH);
150 renderer.render(translated);
153 static void setGLState (glu::CallLogWrapper& wrapper, const RenderState& state, int viewportX, int viewportY)
155 if (state.scissorTestEnabled)
157 wrapper.glEnable(GL_SCISSOR_TEST);
158 wrapper.glScissor(viewportX+state.scissorRectangle.left, viewportY+state.scissorRectangle.bottom,
159 state.scissorRectangle.width, state.scissorRectangle.height);
162 wrapper.glDisable(GL_SCISSOR_TEST);
164 if (state.stencilTestEnabled)
166 wrapper.glEnable(GL_STENCIL_TEST);
168 for (int face = 0; face < rr::FACETYPE_LAST; face++)
170 deUint32 glFace = face == rr::FACETYPE_BACK ? GL_BACK : GL_FRONT;
171 const StencilState& sParams = state.stencil[face];
173 wrapper.glStencilFuncSeparate(glFace, sParams.function, sParams.reference, sParams.compareMask);
174 wrapper.glStencilOpSeparate(glFace, sParams.stencilFailOp, sParams.depthFailOp, sParams.depthPassOp);
175 wrapper.glStencilMaskSeparate(glFace, sParams.writeMask);
179 wrapper.glDisable(GL_STENCIL_TEST);
181 if (state.depthTestEnabled)
183 wrapper.glEnable(GL_DEPTH_TEST);
184 wrapper.glDepthFunc(state.depthFunc);
185 wrapper.glDepthMask(state.depthWriteMask ? GL_TRUE : GL_FALSE);
188 wrapper.glDisable(GL_DEPTH_TEST);
190 if (state.blendEnabled)
192 wrapper.glEnable(GL_BLEND);
193 wrapper.glBlendEquationSeparate(state.blendRGBState.equation, state.blendAState.equation);
194 wrapper.glBlendFuncSeparate(state.blendRGBState.srcFunc, state.blendRGBState.dstFunc, state.blendAState.srcFunc, state.blendAState.dstFunc);
195 wrapper.glBlendColor(state.blendColor.x(), state.blendColor.y(), state.blendColor.z(), state.blendColor.w());
198 wrapper.glDisable(GL_BLEND);
200 if (state.ditherEnabled)
201 wrapper.glEnable(GL_DITHER);
203 wrapper.glDisable(GL_DITHER);
205 wrapper.glColorMask(state.colorMask[0] ? GL_TRUE : GL_FALSE,
206 state.colorMask[1] ? GL_TRUE : GL_FALSE,
207 state.colorMask[2] ? GL_TRUE : GL_FALSE,
208 state.colorMask[3] ? GL_TRUE : GL_FALSE);
211 class RandomFragmentOpCase : public TestCase
214 RandomFragmentOpCase (Context& context, const char* name, const char* desc, deUint32 seed);
215 ~RandomFragmentOpCase (void);
219 IterateResult iterate (void);
222 tcu::UVec4 getCompareThreshold (void) const;
226 glu::CallLogWrapper m_callLogWrapper;
228 gls::FragmentOpUtil::QuadRenderer* m_renderer;
229 tcu::TextureLevel* m_refColorBuffer;
230 tcu::TextureLevel* m_refDepthBuffer;
231 tcu::TextureLevel* m_refStencilBuffer;
232 gls::FragmentOpUtil::ReferenceQuadRenderer* m_refRenderer;
237 RandomFragmentOpCase::RandomFragmentOpCase (Context& context, const char* name, const char* desc, deUint32 seed)
238 : TestCase (context, name, desc)
240 , m_callLogWrapper (context.getRenderContext().getFunctions(), context.getTestContext().getLog())
241 , m_renderer (DE_NULL)
242 , m_refColorBuffer (DE_NULL)
243 , m_refDepthBuffer (DE_NULL)
244 , m_refStencilBuffer (DE_NULL)
245 , m_refRenderer (DE_NULL)
248 m_callLogWrapper.enableLogging(ENABLE_CALL_LOG);
251 RandomFragmentOpCase::~RandomFragmentOpCase (void)
254 delete m_refColorBuffer;
255 delete m_refDepthBuffer;
256 delete m_refStencilBuffer;
257 delete m_refRenderer;
260 void RandomFragmentOpCase::init (void)
262 DE_ASSERT(!m_renderer && !m_refColorBuffer && !m_refDepthBuffer && !m_refStencilBuffer && !m_refRenderer);
264 int width = de::min<int>(m_context.getRenderTarget().getWidth(), VIEWPORT_WIDTH);
265 int height = de::min<int>(m_context.getRenderTarget().getHeight(), VIEWPORT_HEIGHT);
266 bool useRGB = m_context.getRenderTarget().getPixelFormat().alphaBits == 0;
268 m_renderer = new gls::FragmentOpUtil::QuadRenderer(m_context.getRenderContext(), glu::GLSL_VERSION_100_ES);
269 m_refColorBuffer = new tcu::TextureLevel(tcu::TextureFormat(useRGB ? tcu::TextureFormat::RGB : tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), width, height);
270 m_refDepthBuffer = new tcu::TextureLevel(tcu::TextureFormat(tcu::TextureFormat::D, tcu::TextureFormat::FLOAT), width, height);
271 m_refStencilBuffer = new tcu::TextureLevel(tcu::TextureFormat(tcu::TextureFormat::S, tcu::TextureFormat::UNSIGNED_INT32), width, height);
272 m_refRenderer = new gls::FragmentOpUtil::ReferenceQuadRenderer();
275 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
278 void RandomFragmentOpCase::deinit (void)
281 delete m_refColorBuffer;
282 delete m_refDepthBuffer;
283 delete m_refStencilBuffer;
284 delete m_refRenderer;
286 m_renderer = DE_NULL;
287 m_refColorBuffer = DE_NULL;
288 m_refDepthBuffer = DE_NULL;
289 m_refStencilBuffer = DE_NULL;
290 m_refRenderer = DE_NULL;
293 RandomFragmentOpCase::IterateResult RandomFragmentOpCase::iterate (void)
295 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
296 const bool isMSAA = m_context.getRenderTarget().getNumSamples() > 1;
297 const deUint32 iterSeed = deUint32Hash(m_seed) ^ deInt32Hash(m_iterNdx) ^ deInt32Hash(m_testCtx.getCommandLine().getBaseSeed());
298 de::Random rnd (iterSeed);
300 const int width = m_refColorBuffer->getWidth();
301 const int height = m_refColorBuffer->getHeight();
302 const int viewportX = rnd.getInt(0, m_context.getRenderTarget().getWidth()-width);
303 const int viewportY = rnd.getInt(0, m_context.getRenderTarget().getHeight()-height);
305 tcu::Surface renderedImg (width, height);
306 tcu::Surface referenceImg (width, height);
308 const Vec4 clearColor = CLEAR_COLOR;
309 const float clearDepth = CLEAR_DEPTH;
310 const int clearStencil = CLEAR_STENCIL;
312 bool gotError = false;
314 const tcu::ScopedLogSection iterSection (m_testCtx.getLog(), std::string("Iteration") + de::toString(m_iterNdx), std::string("Iteration ") + de::toString(m_iterNdx));
316 // Compute randomized rendering commands.
317 vector<RenderCommand> commands;
318 computeRandomRenderCommands(rnd, glu::ApiType::es(2,0), NUM_CALLS_PER_ITERATION, width, height, commands);
320 // Reset default fragment state.
321 gl.disable(GL_SCISSOR_TEST);
322 gl.colorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
323 gl.depthMask(GL_TRUE);
327 m_callLogWrapper.glClearColor(clearColor.x(), clearColor.y(), clearColor.z(), clearColor.w());
328 m_callLogWrapper.glClearDepthf(clearDepth);
329 m_callLogWrapper.glClearStencil(clearStencil);
330 m_callLogWrapper.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
331 m_callLogWrapper.glViewport(viewportX, viewportY, width, height);
333 for (vector<RenderCommand>::const_iterator cmd = commands.begin(); cmd != commands.end(); cmd++)
335 setGLState(m_callLogWrapper, cmd->state, viewportX, viewportY);
338 m_testCtx.getLog() << TestLog::Message << "// Quad: " << cmd->quad.posA << " -> " << cmd->quad.posB
339 << ", color: [" << cmd->quad.color[0] << ", " << cmd->quad.color[1] << ", " << cmd->quad.color[2] << ", " << cmd->quad.color[3] << "]"
340 << ", depth: [" << cmd->quad.depth[0] << ", " << cmd->quad.depth[1] << ", " << cmd->quad.depth[2] << ", " << cmd->quad.depth[3] << "]"
341 << TestLog::EndMessage;
343 renderQuad(gl, *m_renderer, cmd->quad, viewportX, viewportY);
347 if (m_callLogWrapper.glGetError() != GL_NO_ERROR)
352 // Render reference while GPU is doing work.
353 tcu::clear (m_refColorBuffer->getAccess(), clearColor);
354 tcu::clearDepth (m_refDepthBuffer->getAccess(), clearDepth);
355 tcu::clearStencil (m_refStencilBuffer->getAccess(), clearStencil);
357 for (vector<RenderCommand>::const_iterator cmd = commands.begin(); cmd != commands.end(); cmd++)
359 rr::FragmentOperationState refState;
360 translateState(cmd->state, refState, m_context.getRenderTarget());
361 m_refRenderer->render(gls::FragmentOpUtil::getMultisampleAccess(m_refColorBuffer->getAccess()),
362 gls::FragmentOpUtil::getMultisampleAccess(m_refDepthBuffer->getAccess()),
363 gls::FragmentOpUtil::getMultisampleAccess(m_refStencilBuffer->getAccess()),
364 cmd->quad, refState);
367 // Expand reference color buffer to RGBA8
368 copy(referenceImg.getAccess(), m_refColorBuffer->getAccess());
370 // Read rendered image.
371 glu::readPixels(m_context.getRenderContext(), viewportX, viewportY, renderedImg.getAccess());
375 // Compare to reference.
376 bool isLastIter = m_iterNdx >= NUM_ITERATIONS_PER_CASE;
377 const tcu::UVec4 threshold = getCompareThreshold();
382 // in MSAA cases, the sampling points could be anywhere in the pixel and we could
383 // even have multiple samples that are combined in resolve. Allow arbitrary sample
384 // positions by using bilinearCompare.
385 compareOk = tcu::bilinearCompare(m_testCtx.getLog(),
387 "Image Comparison Result",
388 referenceImg.getAccess(),
389 renderedImg.getAccess(),
390 tcu::RGBA(threshold.x(), threshold.y(), threshold.z(), threshold.w()),
391 tcu::COMPARE_LOG_RESULT);
394 compareOk = tcu::intThresholdCompare(m_testCtx.getLog(),
396 "Image Comparison Result",
397 referenceImg.getAccess(),
398 renderedImg.getAccess(),
400 tcu::COMPARE_LOG_RESULT);
402 m_testCtx.getLog() << TestLog::Message << (compareOk ? " Passed." : " FAILED!") << TestLog::EndMessage;
405 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Image comparison failed");
407 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "GL error");
409 if (compareOk && !gotError && !isLastIter)
415 tcu::UVec4 RandomFragmentOpCase::getCompareThreshold (void) const
417 tcu::PixelFormat format = m_context.getRenderTarget().getPixelFormat();
419 if (format == tcu::PixelFormat(8, 8, 8, 8) || format == tcu::PixelFormat(8, 8, 8, 0))
420 return format.getColorThreshold().toIVec().asUint() + tcu::UVec4(6); // Default threshold.
422 return format.getColorThreshold().toIVec().asUint()
423 * tcu::UVec4(5) + tcu::UVec4(2); // \note Non-scientific ad hoc formula. Need big threshold when few color bits; especially multiple blendings bring extra inaccuracy.
426 RandomFragmentOpTests::RandomFragmentOpTests (Context& context)
427 : TestCaseGroup(context, "random", "Randomized Per-Fragment Operation Tests")
431 RandomFragmentOpTests::~RandomFragmentOpTests (void)
435 void RandomFragmentOpTests::init (void)
437 for (int ndx = 0; ndx < 100; ndx++)
438 addChild(new RandomFragmentOpCase(m_context, de::toString(ndx).c_str(), "", (deUint32)(ndx*NUM_ITERATIONS_PER_CASE)));