/*------------------------------------------------------------------------- * drawElements Quality Program OpenGL ES 3.0 Module * ------------------------------------------------- * * Copyright 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * *//*! * \file * \brief Stencil tests. *//*--------------------------------------------------------------------*/ #include "es3fStencilTests.hpp" #include "tcuSurface.hpp" #include "tcuVector.hpp" #include "tcuTestLog.hpp" #include "tcuImageCompare.hpp" #include "tcuRenderTarget.hpp" #include "sglrContextUtil.hpp" #include "sglrGLContext.hpp" #include "sglrReferenceContext.hpp" #include "deRandom.hpp" #include "deMath.h" #include "deString.h" #include #include "glwEnums.hpp" #include "glwDefs.hpp" using tcu::Vec3; using tcu::IVec2; using tcu::IVec4; using std::vector; using namespace glw; namespace deqp { namespace gles3 { namespace Functional { class StencilShader : public sglr::ShaderProgram { public: StencilShader (void) : sglr::ShaderProgram(sglr::pdec::ShaderProgramDeclaration() << sglr::pdec::VertexAttribute("a_position", rr::GENERICVECTYPE_FLOAT) << sglr::pdec::VertexToFragmentVarying(rr::GENERICVECTYPE_FLOAT) << sglr::pdec::FragmentOutput(rr::GENERICVECTYPE_FLOAT) << sglr::pdec::Uniform("u_color", glu::TYPE_FLOAT_VEC4) << sglr::pdec::VertexSource("#version 300 es\n" "in highp vec4 a_position;\n" "void main (void)\n" "{\n" " gl_Position = a_position;\n" "}\n") << sglr::pdec::FragmentSource("#version 300 es\n" "uniform highp vec4 u_color;\n" "layout(location = 0) out mediump vec4 o_color;\n" "void main (void)\n" "{\n" " o_color = u_color;\n" "}\n")) , u_color (getUniformByName("u_color")) { } void setColor (sglr::Context& ctx, deUint32 program, const tcu::Vec4& color) { ctx.useProgram(program); ctx.uniform4fv(ctx.getUniformLocation(program, "u_color"), 1, color.getPtr()); } private: void shadeVertices (const rr::VertexAttrib* inputs, rr::VertexPacket* const* packets, const int numPackets) const { for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx) { rr::VertexPacket& packet = *packets[packetNdx]; packet.position = rr::readVertexAttribFloat(inputs[0], packet.instanceNdx, packet.vertexNdx); } } void shadeFragments (rr::FragmentPacket* packets, const int numPackets, const rr::FragmentShadingContext& context) const { const tcu::Vec4 color(u_color.value.f4); DE_UNREF(packets); for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx) for (int fragNdx = 0; fragNdx < 4; ++fragNdx) rr::writeFragmentOutput(context, packetNdx, fragNdx, 0, color); } const sglr::UniformSlot& u_color; }; class StencilOp { public: enum Type { TYPE_CLEAR_STENCIL = 0, TYPE_CLEAR_DEPTH, TYPE_QUAD, TYPE_LAST }; Type type; GLenum stencilTest; int stencil; //!< Ref for quad op, clear value for clears deUint32 stencilMask; GLenum depthTest; float depth; //!< Quad depth or clear value GLenum sFail; GLenum dFail; GLenum dPass; 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) : type (type_) , stencilTest (stencilTest_) , stencil (stencil_) , stencilMask (0xffffffffu) , depthTest (depthTest_) , depth (depth_) , sFail (sFail_) , dFail (dFail_) , dPass (dPass_) { } static StencilOp clearStencil (int stencil) { StencilOp op(TYPE_CLEAR_STENCIL); op.stencil = stencil; return op; } static StencilOp clearDepth (float depth) { StencilOp op(TYPE_CLEAR_DEPTH); op.depth = depth; return op; } static StencilOp quad (GLenum stencilTest, int stencil, GLenum depthTest, float depth, GLenum sFail, GLenum dFail, GLenum dPass) { return StencilOp(TYPE_QUAD, stencilTest, stencil, depthTest, depth, sFail, dFail, dPass); } }; class StencilCase : public TestCase { public: StencilCase (Context& context, const char* name, const char* description); virtual ~StencilCase (void); void init (void); void deinit (void); IterateResult iterate (void); virtual void genOps (vector& dst, int stencilBits, int depthBits, int targetStencil) = DE_NULL; private: void executeOps (sglr::Context& context, const IVec4& cell, const vector& ops); void visualizeStencil (sglr::Context& context, int stencilBits, int stencilStep); StencilShader m_shader; deUint32 m_shaderID; }; StencilCase::StencilCase (Context& context, const char* name, const char* description) : TestCase (context, name, description) , m_shaderID (0) { } StencilCase::~StencilCase (void) { } void StencilCase::init (void) { } void StencilCase::deinit (void) { } void StencilCase::executeOps (sglr::Context& context, const IVec4& cell, const vector& ops) { // For quadOps float x0 = 2.0f*((float)cell.x() / (float)context.getWidth())-1.0f; float y0 = 2.0f*((float)cell.y() / (float)context.getHeight())-1.0f; float x1 = x0 + 2.0f*((float)cell.z() / (float)context.getWidth()); float y1 = y0 + 2.0f*((float)cell.w() / (float)context.getHeight()); m_shader.setColor(context, m_shaderID, tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f)); for (vector::const_iterator i = ops.begin(); i != ops.end(); i++) { const StencilOp& op = *i; switch (op.type) { case StencilOp::TYPE_CLEAR_DEPTH: context.enable(GL_SCISSOR_TEST); context.scissor(cell.x(), cell.y(), cell.z(), cell.w()); context.clearDepthf(op.depth); context.clear(GL_DEPTH_BUFFER_BIT); context.disable(GL_SCISSOR_TEST); break; case StencilOp::TYPE_CLEAR_STENCIL: context.enable(GL_SCISSOR_TEST); context.scissor(cell.x(), cell.y(), cell.z(), cell.w()); context.clearStencil(op.stencil); context.clear(GL_STENCIL_BUFFER_BIT); context.disable(GL_SCISSOR_TEST); break; case StencilOp::TYPE_QUAD: context.enable(GL_DEPTH_TEST); context.enable(GL_STENCIL_TEST); context.depthFunc(op.depthTest); context.stencilFunc(op.stencilTest, op.stencil, op.stencilMask); context.stencilOp(op.sFail, op.dFail, op.dPass); sglr::drawQuad(context, m_shaderID, Vec3(x0, y0, op.depth), Vec3(x1, y1, op.depth)); context.disable(GL_STENCIL_TEST); context.disable(GL_DEPTH_TEST); break; default: DE_ASSERT(DE_FALSE); } } } void StencilCase::visualizeStencil (sglr::Context& context, int stencilBits, int stencilStep) { int endVal = 1< > ops(numStencilValues+2); { // Values from 0 to max for (int ndx = 0; ndx < numStencilValues; ndx++) genOps(ops[ndx], stencilBits, depthBits, deMin32(ndx*stencilStep, (1< cells; int cellWidth = width/gridSize; int cellHeight = height/gridSize; for (int y = 0; y < gridSize; y++) for (int x = 0; x < gridSize; x++) cells.push_back(IVec4(x*cellWidth, y*cellHeight, cellWidth, cellHeight)); DE_ASSERT(ops.size() <= cells.size()); // Execute for gles3 context { sglr::GLContext context(m_context.getRenderContext(), log, 0 /* don't log calls or program */, viewport); m_shaderID = context.createProgram(&m_shader); context.clearColor(1.0f, 0.0f, 0.0f, 1.0f); context.clear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); for (int ndx = 0; ndx < (int)ops.size(); ndx++) executeOps(context, cells[ndx], ops[ndx]); visualizeStencil(context, stencilBits, stencilStep); gles2Error = context.getError(); context.readPixels(gles2Frame, 0, 0, width, height); } // Execute for reference context { sglr::ReferenceContextBuffers buffers (tcu::PixelFormat(8,8,8,renderTarget.getPixelFormat().alphaBits?8:0), renderTarget.getDepthBits(), renderTarget.getStencilBits(), width, height); sglr::ReferenceContext context (sglr::ReferenceContextLimits(m_context.getRenderContext()), buffers.getColorbuffer(), buffers.getDepthbuffer(), buffers.getStencilbuffer()); m_shaderID = context.createProgram(&m_shader); context.clearColor(1.0f, 0.0f, 0.0f, 1.0f); context.clear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); for (int ndx = 0; ndx < (int)ops.size(); ndx++) executeOps(context, cells[ndx], ops[ndx]); visualizeStencil(context, stencilBits, stencilStep); context.readPixels(refFrame, 0, 0, width, height); } // Check error bool errorCodeOk = (gles2Error == GL_NO_ERROR); if (!errorCodeOk && !failReason) failReason = "Got unexpected error"; // Compare images const float threshold = 0.02f; bool imagesOk = tcu::fuzzyCompare(log, "ComparisonResult", "Image comparison result", refFrame, gles2Frame, threshold, tcu::COMPARE_LOG_RESULT); if (!imagesOk && !failReason) failReason = "Image comparison failed"; // Store test result bool isOk = errorCodeOk && imagesOk; m_testCtx.setTestResult(isOk ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL, isOk ? "Pass" : failReason); return STOP; } StencilTests::StencilTests (Context& context) : TestCaseGroup(context, "stencil", "Stencil Tests") { } StencilTests::~StencilTests (void) { } typedef void (*GenStencilOpsFunc) (vector& dst, int stencilBits, int depthBits, int targetStencil); class SimpleStencilCase : public StencilCase { public: SimpleStencilCase (Context& context, const char* name, const char* description, GenStencilOpsFunc genOpsFunc) : StencilCase (context, name, description) , m_genOps (genOpsFunc) { } void genOps (vector& dst, int stencilBits, int depthBits, int targetStencil) { m_genOps(dst, stencilBits, depthBits, targetStencil); } private: GenStencilOpsFunc m_genOps; }; void StencilTests::init (void) { #define STENCIL_CASE(NAME, DESCRIPTION, GEN_OPS_BODY) \ do { \ struct Gen_##NAME { \ static void genOps (vector& dst, int stencilBits, int depthBits, int targetStencil) \ { \ DE_UNREF(stencilBits && depthBits); \ GEN_OPS_BODY \ } \ }; \ addChild(new SimpleStencilCase(m_context, #NAME, DESCRIPTION, Gen_##NAME::genOps)); \ } while (deGetFalse()); STENCIL_CASE(clear, "Stencil clear", { // \note Unused bits are set to 1, clear should mask them out int mask = (1< 0) { dst.push_back(StencilOp::clearStencil(targetStencil-1)); dst.push_back(StencilOp::quad(GL_EQUAL, targetStencil, GL_ALWAYS, 0.0f, GL_INCR, GL_KEEP, GL_KEEP)); } else dst.push_back(StencilOp::clearStencil(targetStencil)); }); STENCIL_CASE(decr_stencil_fail, "Decrement on stencil fail", { int maxStencil = (1< 0) { dst.push_back(StencilOp::clearStencil(targetStencil-1)); dst.push_back(StencilOp::quad(GL_GREATER, targetStencil, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INCR)); dst.push_back(StencilOp::quad(GL_GREATER, targetStencil, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INCR)); } else dst.push_back(StencilOp::clearStencil(targetStencil)); }); STENCIL_CASE(cmp_greater_or_equal, "Greater or equal comparison", { if (targetStencil > 0) { dst.push_back(StencilOp::clearStencil(targetStencil-1)); dst.push_back(StencilOp::quad(GL_GEQUAL, targetStencil-1, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INCR)); dst.push_back(StencilOp::quad(GL_GEQUAL, targetStencil-1, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INCR)); } else dst.push_back(StencilOp::clearStencil(targetStencil)); }); STENCIL_CASE(cmp_mask_equal, "Equality comparison with mask", { int valMask = (1<