1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES 2.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 Stencil tests.
22 *//*--------------------------------------------------------------------*/
24 #include "es2fStencilTests.hpp"
26 #include "tcuSurface.hpp"
27 #include "tcuVector.hpp"
28 #include "tcuTestLog.hpp"
29 #include "tcuImageCompare.hpp"
30 #include "tcuRenderTarget.hpp"
32 #include "sglrContextUtil.hpp"
33 #include "sglrGLContext.hpp"
34 #include "sglrReferenceContext.hpp"
36 #include "deRandom.hpp"
42 #include "glwEnums.hpp"
43 #include "glwDefs.hpp"
58 class StencilShader : public sglr::ShaderProgram
62 : sglr::ShaderProgram(sglr::pdec::ShaderProgramDeclaration()
63 << sglr::pdec::VertexAttribute("a_position", rr::GENERICVECTYPE_FLOAT)
64 << sglr::pdec::VertexToFragmentVarying(rr::GENERICVECTYPE_FLOAT)
65 << sglr::pdec::FragmentOutput(rr::GENERICVECTYPE_FLOAT)
66 << sglr::pdec::Uniform("u_color", glu::TYPE_FLOAT_VEC4)
67 << sglr::pdec::VertexSource("attribute highp vec4 a_position;\n"
70 " gl_Position = a_position;\n"
72 << sglr::pdec::FragmentSource("uniform mediump vec4 u_color;\n"
75 " gl_FragColor = u_color;\n"
77 , u_color (getUniformByName("u_color"))
81 void setColor (sglr::Context& ctx, deUint32 program, const tcu::Vec4& color)
83 ctx.useProgram(program);
84 ctx.uniform4fv(ctx.getUniformLocation(program, "u_color"), 1, color.getPtr());
88 void shadeVertices (const rr::VertexAttrib* inputs, rr::VertexPacket* const* packets, const int numPackets) const
90 for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx)
92 rr::VertexPacket& packet = *packets[packetNdx];
94 packet.position = rr::readVertexAttribFloat(inputs[0], packet.instanceNdx, packet.vertexNdx);
98 void shadeFragments (rr::FragmentPacket* packets, const int numPackets, const rr::FragmentShadingContext& context) const
100 const tcu::Vec4 color(u_color.value.f4);
104 for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx)
105 for (int fragNdx = 0; fragNdx < 4; ++fragNdx)
106 rr::writeFragmentOutput(context, packetNdx, fragNdx, 0, color);
109 const sglr::UniformSlot& u_color;
117 TYPE_CLEAR_STENCIL = 0,
126 int stencil; //!< Ref for quad op, clear value for clears
127 deUint32 stencilMask;
129 float depth; //!< Quad depth or clear value
134 StencilOp (Type type_, GLenum stencilTest_ = GL_ALWAYS, int stencil_ = 0, GLenum depthTest_ = GL_ALWAYS, float depth_ = 1.0f, GLenum sFail_ = GL_KEEP, GLenum dFail_ = GL_KEEP, GLenum dPass_ = GL_KEEP)
136 , stencilTest (stencilTest_)
138 , stencilMask (0xffffffffu)
139 , depthTest (depthTest_)
147 static StencilOp clearStencil (int stencil)
149 StencilOp op(TYPE_CLEAR_STENCIL);
150 op.stencil = stencil;
154 static StencilOp clearDepth (float depth)
156 StencilOp op(TYPE_CLEAR_DEPTH);
161 static StencilOp quad (GLenum stencilTest, int stencil, GLenum depthTest, float depth, GLenum sFail, GLenum dFail, GLenum dPass)
163 return StencilOp(TYPE_QUAD, stencilTest, stencil, depthTest, depth, sFail, dFail, dPass);
167 class StencilCase : public TestCase
170 StencilCase (Context& context, const char* name, const char* description);
171 virtual ~StencilCase (void);
175 IterateResult iterate (void);
177 virtual void genOps (vector<StencilOp>& dst, int stencilBits, int depthBits, int targetStencil) = DE_NULL;
180 void executeOps (sglr::Context& context, const IVec4& cell, const vector<StencilOp>& ops);
181 void visualizeStencil (sglr::Context& context, int stencilBits, int stencilStep);
183 StencilShader m_shader;
187 StencilCase::StencilCase (Context& context, const char* name, const char* description)
188 : TestCase (context, name, description)
193 StencilCase::~StencilCase (void)
197 void StencilCase::init (void)
201 void StencilCase::deinit (void)
205 void StencilCase::executeOps (sglr::Context& context, const IVec4& cell, const vector<StencilOp>& ops)
208 float x0 = 2.0f*((float)cell.x() / (float)context.getWidth())-1.0f;
209 float y0 = 2.0f*((float)cell.y() / (float)context.getHeight())-1.0f;
210 float x1 = x0 + 2.0f*((float)cell.z() / (float)context.getWidth());
211 float y1 = y0 + 2.0f*((float)cell.w() / (float)context.getHeight());
213 m_shader.setColor(context, m_shaderID, tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
215 for (vector<StencilOp>::const_iterator i = ops.begin(); i != ops.end(); i++)
217 const StencilOp& op = *i;
221 case StencilOp::TYPE_CLEAR_DEPTH:
222 context.enable(GL_SCISSOR_TEST);
223 context.scissor(cell.x(), cell.y(), cell.z(), cell.w());
224 context.clearDepthf(op.depth);
225 context.clear(GL_DEPTH_BUFFER_BIT);
226 context.disable(GL_SCISSOR_TEST);
229 case StencilOp::TYPE_CLEAR_STENCIL:
230 context.enable(GL_SCISSOR_TEST);
231 context.scissor(cell.x(), cell.y(), cell.z(), cell.w());
232 context.clearStencil(op.stencil);
233 context.clear(GL_STENCIL_BUFFER_BIT);
234 context.disable(GL_SCISSOR_TEST);
237 case StencilOp::TYPE_QUAD:
238 context.enable(GL_DEPTH_TEST);
239 context.enable(GL_STENCIL_TEST);
240 context.depthFunc(op.depthTest);
241 context.stencilFunc(op.stencilTest, op.stencil, op.stencilMask);
242 context.stencilOp(op.sFail, op.dFail, op.dPass);
243 sglr::drawQuad(context, m_shaderID, Vec3(x0, y0, op.depth), Vec3(x1, y1, op.depth));
244 context.disable(GL_STENCIL_TEST);
245 context.disable(GL_DEPTH_TEST);
254 void StencilCase::visualizeStencil (sglr::Context& context, int stencilBits, int stencilStep)
256 int endVal = 1<<stencilBits;
257 int numStencilValues = endVal/stencilStep + 1;
259 context.enable(GL_STENCIL_TEST);
260 context.stencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
262 for (int ndx = 0; ndx < numStencilValues; ndx++)
264 int value = deMin32(ndx*stencilStep, endVal-1);
265 float colorMix = (float)value/(float)de::max(1, endVal-1);
266 tcu::Vec4 color (0.0f, 1.0f-colorMix, colorMix, 1.0f);
268 m_shader.setColor(context, m_shaderID, color);
269 context.stencilFunc(GL_EQUAL, value, 0xffffffffu);
270 sglr::drawQuad(context, m_shaderID, Vec3(-1.0f, -1.0f, 0.0f), Vec3(+1.0f, +1.0f, 0.0f));
274 TestCase::IterateResult StencilCase::iterate (void)
276 const tcu::RenderTarget& renderTarget = m_context.getRenderContext().getRenderTarget();
277 int depthBits = renderTarget.getDepthBits();
278 int stencilBits = renderTarget.getStencilBits();
280 int stencilStep = stencilBits == 8 ? 8 : 1;
281 int numStencilValues = (1<<stencilBits)/stencilStep + 1;
283 int gridSize = (int)deFloatCeil(deFloatSqrt((float)(numStencilValues+2)));
285 int width = deMin32(128, renderTarget.getWidth());
286 int height = deMin32(128, renderTarget.getHeight());
288 tcu::TestLog& log = m_testCtx.getLog();
289 de::Random rnd (deStringHash(m_name.c_str()));
290 int viewportX = rnd.getInt(0, renderTarget.getWidth()-width);
291 int viewportY = rnd.getInt(0, renderTarget.getHeight()-height);
292 IVec4 viewport = IVec4(viewportX, viewportY, width, height);
294 tcu::Surface gles2Frame (width, height);
295 tcu::Surface refFrame (width, height);
298 const char* failReason = DE_NULL;
300 // Get ops for stencil values
301 vector<vector<StencilOp> > ops(numStencilValues+2);
303 // Values from 0 to max
304 for (int ndx = 0; ndx < numStencilValues; ndx++)
305 genOps(ops[ndx], stencilBits, depthBits, deMin32(ndx*stencilStep, (1<<stencilBits)-1));
308 genOps(ops[numStencilValues+0], stencilBits, depthBits, 1<<stencilBits);
309 genOps(ops[numStencilValues+1], stencilBits, depthBits, -1);
312 // Compute cells: (x, y, w, h)
314 int cellWidth = width/gridSize;
315 int cellHeight = height/gridSize;
316 for (int y = 0; y < gridSize; y++)
317 for (int x = 0; x < gridSize; x++)
318 cells.push_back(IVec4(x*cellWidth, y*cellHeight, cellWidth, cellHeight));
320 DE_ASSERT(ops.size() <= cells.size());
322 // Execute for gles2 context
324 sglr::GLContext context(m_context.getRenderContext(), log, 0 /* don't log calls or program */, viewport);
326 m_shaderID = context.createProgram(&m_shader);
328 context.clearColor(1.0f, 0.0f, 0.0f, 1.0f);
329 context.clear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
331 for (int ndx = 0; ndx < (int)ops.size(); ndx++)
332 executeOps(context, cells[ndx], ops[ndx]);
334 visualizeStencil(context, stencilBits, stencilStep);
336 gles2Error = context.getError();
337 context.readPixels(gles2Frame, 0, 0, width, height);
340 // Execute for reference context
342 sglr::ReferenceContextBuffers buffers (tcu::PixelFormat(8,8,8,renderTarget.getPixelFormat().alphaBits?8:0), renderTarget.getDepthBits(), renderTarget.getStencilBits(), width, height);
343 sglr::ReferenceContext context (sglr::ReferenceContextLimits(m_context.getRenderContext()), buffers.getColorbuffer(), buffers.getDepthbuffer(), buffers.getStencilbuffer());
345 m_shaderID = context.createProgram(&m_shader);
347 context.clearColor(1.0f, 0.0f, 0.0f, 1.0f);
348 context.clear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
350 for (int ndx = 0; ndx < (int)ops.size(); ndx++)
351 executeOps(context, cells[ndx], ops[ndx]);
353 visualizeStencil(context, stencilBits, stencilStep);
355 context.readPixels(refFrame, 0, 0, width, height);
359 bool errorCodeOk = (gles2Error == GL_NO_ERROR);
360 if (!errorCodeOk && !failReason)
361 failReason = "Got unexpected error";
364 const float threshold = 0.02f;
365 bool imagesOk = tcu::fuzzyCompare(log, "ComparisonResult", "Image comparison result", refFrame, gles2Frame, threshold, tcu::COMPARE_LOG_RESULT);
367 if (!imagesOk && !failReason)
368 failReason = "Image comparison failed";
371 bool isOk = errorCodeOk && imagesOk;
372 m_testCtx.setTestResult(isOk ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL,
373 isOk ? "Pass" : failReason);
378 StencilTests::StencilTests (Context& context)
379 : TestCaseGroup(context, "stencil", "Stencil Tests")
383 StencilTests::~StencilTests (void)
387 typedef void (*GenStencilOpsFunc) (vector<StencilOp>& dst, int stencilBits, int depthBits, int targetStencil);
389 class SimpleStencilCase : public StencilCase
392 SimpleStencilCase (Context& context, const char* name, const char* description, GenStencilOpsFunc genOpsFunc)
393 : StencilCase (context, name, description)
394 , m_genOps (genOpsFunc)
398 void genOps (vector<StencilOp>& dst, int stencilBits, int depthBits, int targetStencil)
400 m_genOps(dst, stencilBits, depthBits, targetStencil);
404 GenStencilOpsFunc m_genOps;
407 void StencilTests::init (void)
409 #define STENCIL_CASE(NAME, DESCRIPTION, GEN_OPS_BODY) \
411 struct Gen_##NAME { \
412 static void genOps (vector<StencilOp>& dst, int stencilBits, int depthBits, int targetStencil) \
414 DE_UNREF(stencilBits && depthBits); \
418 addChild(new SimpleStencilCase(m_context, #NAME, DESCRIPTION, Gen_##NAME::genOps)); \
419 } while (deGetFalse());
421 STENCIL_CASE(clear, "Stencil clear",
423 // \note Unused bits are set to 1, clear should mask them out
424 int mask = (1<<stencilBits)-1;
425 dst.push_back(StencilOp::clearStencil(targetStencil | ~mask));
428 // Replace in different points
429 STENCIL_CASE(stencil_fail_replace, "Set stencil on stencil fail",
431 dst.push_back(StencilOp::quad(GL_NEVER, targetStencil, GL_ALWAYS, 0.0f, GL_REPLACE, GL_KEEP, GL_KEEP));
433 STENCIL_CASE(depth_fail_replace, "Set stencil on depth fail",
435 dst.push_back(StencilOp::clearDepth(0.0f));
436 dst.push_back(StencilOp::quad(GL_ALWAYS, targetStencil, GL_LESS, 0.5f, GL_KEEP, GL_REPLACE, GL_KEEP));
438 STENCIL_CASE(depth_pass_replace, "Set stencil on depth pass",
440 dst.push_back(StencilOp::quad(GL_ALWAYS, targetStencil, GL_LESS, 0.0f, GL_KEEP, GL_KEEP, GL_REPLACE));
443 // Increment, decrement
444 STENCIL_CASE(incr_stencil_fail, "Increment on stencil fail",
446 if (targetStencil > 0)
448 dst.push_back(StencilOp::clearStencil(targetStencil-1));
449 dst.push_back(StencilOp::quad(GL_EQUAL, targetStencil, GL_ALWAYS, 0.0f, GL_INCR, GL_KEEP, GL_KEEP));
452 dst.push_back(StencilOp::clearStencil(targetStencil));
454 STENCIL_CASE(decr_stencil_fail, "Decrement on stencil fail",
456 int maxStencil = (1<<stencilBits)-1;
457 if (targetStencil < maxStencil)
459 dst.push_back(StencilOp::clearStencil(targetStencil+1));
460 dst.push_back(StencilOp::quad(GL_EQUAL, targetStencil, GL_ALWAYS, 0.0f, GL_DECR, GL_KEEP, GL_KEEP));
463 dst.push_back(StencilOp::clearStencil(targetStencil));
465 STENCIL_CASE(incr_wrap_stencil_fail, "Increment (wrap) on stencil fail",
467 int maxStencil = (1<<stencilBits)-1;
468 dst.push_back(StencilOp::clearStencil((targetStencil-1)&maxStencil));
469 dst.push_back(StencilOp::quad(GL_EQUAL, targetStencil, GL_ALWAYS, 0.0f, GL_INCR_WRAP, GL_KEEP, GL_KEEP));
471 STENCIL_CASE(decr_wrap_stencil_fail, "Decrement (wrap) on stencil fail",
473 int maxStencil = (1<<stencilBits)-1;
474 dst.push_back(StencilOp::clearStencil((targetStencil+1)&maxStencil));
475 dst.push_back(StencilOp::quad(GL_EQUAL, targetStencil, GL_ALWAYS, 0.0f, GL_DECR_WRAP, GL_KEEP, GL_KEEP));
479 STENCIL_CASE(zero_stencil_fail, "Zero on stencil fail",
481 dst.push_back(StencilOp::clearStencil(targetStencil));
482 dst.push_back(StencilOp::quad(GL_NOTEQUAL, targetStencil, GL_ALWAYS, 0.0f, GL_ZERO, GL_KEEP, GL_KEEP));
483 dst.push_back(StencilOp::quad(GL_EQUAL, targetStencil, GL_ALWAYS, 0.0f, GL_REPLACE, GL_KEEP, GL_KEEP));
485 STENCIL_CASE(invert_stencil_fail, "Invert on stencil fail",
487 int mask = (1<<stencilBits)-1;
488 dst.push_back(StencilOp::clearStencil((~targetStencil)&mask));
489 dst.push_back(StencilOp::quad(GL_EQUAL, targetStencil, GL_ALWAYS, 0.0f, GL_INVERT, GL_KEEP, GL_KEEP));
493 STENCIL_CASE(cmp_equal, "Equality comparison",
495 int mask = (1<<stencilBits)-1;
496 int inv = (~targetStencil)&mask;
497 dst.push_back(StencilOp::clearStencil(inv));
498 dst.push_back(StencilOp::quad(GL_EQUAL, inv, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INVERT));
499 dst.push_back(StencilOp::quad(GL_EQUAL, inv, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INVERT));
501 STENCIL_CASE(cmp_not_equal, "Equality comparison",
503 int mask = (1<<stencilBits)-1;
504 int inv = (~targetStencil)&mask;
505 dst.push_back(StencilOp::clearStencil(inv));
506 dst.push_back(StencilOp::quad(GL_NOTEQUAL, targetStencil, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INVERT));
507 dst.push_back(StencilOp::quad(GL_NOTEQUAL, targetStencil, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INVERT));
509 STENCIL_CASE(cmp_less_than, "Less than comparison",
511 int maxStencil = (1<<stencilBits)-1;
512 if (targetStencil < maxStencil)
514 dst.push_back(StencilOp::clearStencil(targetStencil+1));
515 dst.push_back(StencilOp::quad(GL_LESS, targetStencil, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_DECR));
516 dst.push_back(StencilOp::quad(GL_LESS, targetStencil, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_DECR));
519 dst.push_back(StencilOp::clearStencil(targetStencil));
521 STENCIL_CASE(cmp_less_or_equal, "Less or equal comparison",
523 int maxStencil = (1<<stencilBits)-1;
524 if (targetStencil < maxStencil)
526 dst.push_back(StencilOp::clearStencil(targetStencil+1));
527 dst.push_back(StencilOp::quad(GL_LEQUAL, targetStencil+1, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_DECR));
528 dst.push_back(StencilOp::quad(GL_LEQUAL, targetStencil+1, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_DECR));
531 dst.push_back(StencilOp::clearStencil(targetStencil));
533 STENCIL_CASE(cmp_greater_than, "Greater than comparison",
535 if (targetStencil > 0)
537 dst.push_back(StencilOp::clearStencil(targetStencil-1));
538 dst.push_back(StencilOp::quad(GL_GREATER, targetStencil, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INCR));
539 dst.push_back(StencilOp::quad(GL_GREATER, targetStencil, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INCR));
542 dst.push_back(StencilOp::clearStencil(targetStencil));
544 STENCIL_CASE(cmp_greater_or_equal, "Greater or equal comparison",
546 if (targetStencil > 0)
548 dst.push_back(StencilOp::clearStencil(targetStencil-1));
549 dst.push_back(StencilOp::quad(GL_GEQUAL, targetStencil-1, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INCR));
550 dst.push_back(StencilOp::quad(GL_GEQUAL, targetStencil-1, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INCR));
553 dst.push_back(StencilOp::clearStencil(targetStencil));
555 STENCIL_CASE(cmp_mask_equal, "Equality comparison with mask",
557 int valMask = (1<<stencilBits)-1;
558 int mask = (1<<7)|(1<<5)|(1<<3)|(1<<1);
559 dst.push_back(StencilOp::clearStencil(~targetStencil));
560 StencilOp op = StencilOp::quad(GL_EQUAL, (~targetStencil | ~mask) & valMask, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INVERT);
561 op.stencilMask = mask;