13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements Quality Program OpenGL ES 2.0 Module
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * -------------------------------------------------
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License");
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License.
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *      http://www.apache.org/licenses/LICENSE-2.0
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS,
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License.
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*!
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Stencil tests.
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "es2fStencilTests.hpp"
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuSurface.hpp"
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuVector.hpp"
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuTestLog.hpp"
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuImageCompare.hpp"
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuRenderTarget.hpp"
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "sglrContextUtil.hpp"
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "sglrGLContext.hpp"
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "sglrReferenceContext.hpp"
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deRandom.hpp"
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deMath.h"
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deString.h"
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <vector>
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "glwEnums.hpp"
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "glwDefs.hpp"
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing tcu::Vec3;
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing tcu::IVec2;
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing tcu::IVec4;
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing std::vector;
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing namespace glw;
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace deqp
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace gles2
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace Functional
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass StencilShader : public sglr::ShaderProgram
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	StencilShader (void)
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		: sglr::ShaderProgram(sglr::pdec::ShaderProgramDeclaration()
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry									<< sglr::pdec::VertexAttribute("a_position", rr::GENERICVECTYPE_FLOAT)
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry									<< sglr::pdec::VertexToFragmentVarying(rr::GENERICVECTYPE_FLOAT)
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry									<< sglr::pdec::FragmentOutput(rr::GENERICVECTYPE_FLOAT)
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry									<< sglr::pdec::Uniform("u_color", glu::TYPE_FLOAT_VEC4)
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry									<< sglr::pdec::VertexSource("attribute highp vec4 a_position;\n"
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry																"void main (void)\n"
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry																"{\n"
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry																"	gl_Position = a_position;\n"
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry																"}\n")
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry									<< sglr::pdec::FragmentSource("uniform mediump vec4 u_color;\n"
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry																  "void main (void)\n"
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry																  "{\n"
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry																  "	gl_FragColor = u_color;\n"
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry																  "}\n"))
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		, u_color	(getUniformByName("u_color"))
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void setColor (sglr::Context& ctx, deUint32 program, const tcu::Vec4& color)
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		ctx.useProgram(program);
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		ctx.uniform4fv(ctx.getUniformLocation(program, "u_color"), 1, color.getPtr());
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void shadeVertices (const rr::VertexAttrib* inputs, rr::VertexPacket* const* packets, const int numPackets) const
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx)
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			rr::VertexPacket& packet = *packets[packetNdx];
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			packet.position = rr::readVertexAttribFloat(inputs[0], packet.instanceNdx, packet.vertexNdx);
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void shadeFragments (rr::FragmentPacket* packets, const int numPackets, const rr::FragmentShadingContext& context) const
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const tcu::Vec4 color(u_color.value.f4);
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_UNREF(packets);
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx)
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int fragNdx = 0; fragNdx < 4; ++fragNdx)
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			rr::writeFragmentOutput(context, packetNdx, fragNdx, 0, color);
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const sglr::UniformSlot& u_color;
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass StencilOp
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	enum Type
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		TYPE_CLEAR_STENCIL = 0,
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		TYPE_CLEAR_DEPTH,
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		TYPE_QUAD,
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		TYPE_LAST
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	};
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Type		type;
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	GLenum		stencilTest;
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int			stencil;	//!< Ref for quad op, clear value for clears
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deUint32	stencilMask;
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	GLenum		depthTest;
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	float		depth;		//!< Quad depth or clear value
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	GLenum		sFail;
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	GLenum		dFail;
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	GLenum		dPass;
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	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)
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		: type			(type_)
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		, stencilTest	(stencilTest_)
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		, stencil		(stencil_)
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		, stencilMask	(0xffffffffu)
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		, depthTest		(depthTest_)
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		, depth			(depth_)
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		, sFail			(sFail_)
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		, dFail			(dFail_)
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		, dPass			(dPass_)
1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	static StencilOp clearStencil (int stencil)
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		StencilOp op(TYPE_CLEAR_STENCIL);
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		op.stencil = stencil;
1513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return op;
1523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	static StencilOp clearDepth (float depth)
1553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		StencilOp op(TYPE_CLEAR_DEPTH);
1573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		op.depth = depth;
1583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return op;
1593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	static StencilOp quad (GLenum stencilTest, int stencil, GLenum depthTest, float depth, GLenum sFail, GLenum dFail, GLenum dPass)
1623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return StencilOp(TYPE_QUAD, stencilTest, stencil, depthTest, depth, sFail, dFail, dPass);
1643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
1663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1673c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass StencilCase : public TestCase
1683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1693c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
1703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry						StencilCase			(Context& context, const char* name, const char* description);
1713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	virtual				~StencilCase		(void);
1723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void				init				(void);
1743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void				deinit				(void);
1753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	IterateResult		iterate				(void);
1763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	virtual void		genOps				(vector<StencilOp>& dst, int stencilBits, int depthBits, int targetStencil) = DE_NULL;
1783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1793c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
1803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void				executeOps			(sglr::Context& context, const IVec4& cell, const vector<StencilOp>& ops);
1813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void				visualizeStencil	(sglr::Context& context, int stencilBits, int stencilStep);
1823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	StencilShader		m_shader;
1843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deUint32			m_shaderID;
1853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
1863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1873c827367444ee418f129b2c238299f49d3264554Jarkko PoyryStencilCase::StencilCase (Context& context, const char* name, const char* description)
1883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	: TestCase		(context, name, description)
1893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_shaderID	(0)
1903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1933c827367444ee418f129b2c238299f49d3264554Jarkko PoyryStencilCase::~StencilCase (void)
1943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1973c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid StencilCase::init (void)
1983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2013c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid StencilCase::deinit (void)
2023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2053c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid StencilCase::executeOps (sglr::Context& context, const IVec4& cell, const vector<StencilOp>& ops)
2063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// For quadOps
2083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	float x0 = 2.0f*((float)cell.x() / (float)context.getWidth())-1.0f;
2093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	float y0 = 2.0f*((float)cell.y() / (float)context.getHeight())-1.0f;
2103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	float x1 = x0 + 2.0f*((float)cell.z() / (float)context.getWidth());
2113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	float y1 = y0 + 2.0f*((float)cell.w() / (float)context.getHeight());
2123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_shader.setColor(context, m_shaderID, tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
2143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (vector<StencilOp>::const_iterator i = ops.begin(); i != ops.end(); i++)
2163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
2173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const StencilOp& op = *i;
2183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		switch (op.type)
2203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
2213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			case StencilOp::TYPE_CLEAR_DEPTH:
2223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				context.enable(GL_SCISSOR_TEST);
2233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				context.scissor(cell.x(), cell.y(), cell.z(), cell.w());
2243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				context.clearDepthf(op.depth);
2253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				context.clear(GL_DEPTH_BUFFER_BIT);
2263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				context.disable(GL_SCISSOR_TEST);
2273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				break;
2283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			case StencilOp::TYPE_CLEAR_STENCIL:
2303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				context.enable(GL_SCISSOR_TEST);
2313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				context.scissor(cell.x(), cell.y(), cell.z(), cell.w());
2323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				context.clearStencil(op.stencil);
2333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				context.clear(GL_STENCIL_BUFFER_BIT);
2343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				context.disable(GL_SCISSOR_TEST);
2353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				break;
2363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			case StencilOp::TYPE_QUAD:
2383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				context.enable(GL_DEPTH_TEST);
2393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				context.enable(GL_STENCIL_TEST);
2403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				context.depthFunc(op.depthTest);
2413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				context.stencilFunc(op.stencilTest, op.stencil, op.stencilMask);
2423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				context.stencilOp(op.sFail, op.dFail, op.dPass);
2433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				sglr::drawQuad(context, m_shaderID, Vec3(x0, y0, op.depth), Vec3(x1, y1, op.depth));
2443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				context.disable(GL_STENCIL_TEST);
2453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				context.disable(GL_DEPTH_TEST);
2463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				break;
2473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			default:
2493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				DE_ASSERT(DE_FALSE);
2503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
2513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2543c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid StencilCase::visualizeStencil (sglr::Context& context, int stencilBits, int stencilStep)
2553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int endVal				= 1<<stencilBits;
2573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int numStencilValues	= endVal/stencilStep + 1;
2583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	context.enable(GL_STENCIL_TEST);
2603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	context.stencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
2613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int ndx = 0; ndx < numStencilValues; ndx++)
2633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
2643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		int			value		= deMin32(ndx*stencilStep, endVal-1);
2653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		float		colorMix	= (float)value/(float)de::max(1, endVal-1);
2663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		tcu::Vec4	color		(0.0f, 1.0f-colorMix, colorMix, 1.0f);
2673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_shader.setColor(context, m_shaderID, color);
2693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		context.stencilFunc(GL_EQUAL, value, 0xffffffffu);
2703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		sglr::drawQuad(context, m_shaderID, Vec3(-1.0f, -1.0f, 0.0f), Vec3(+1.0f, +1.0f, 0.0f));
2713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2743c827367444ee418f129b2c238299f49d3264554Jarkko PoyryTestCase::IterateResult StencilCase::iterate (void)
2753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const tcu::RenderTarget&	renderTarget		= m_context.getRenderContext().getRenderTarget();
2773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int							depthBits			= renderTarget.getDepthBits();
2783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int							stencilBits			= renderTarget.getStencilBits();
2793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int							stencilStep			= stencilBits == 8 ? 8 : 1;
2813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int							numStencilValues	= (1<<stencilBits)/stencilStep + 1;
2823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int							gridSize			= (int)deFloatCeil(deFloatSqrt((float)(numStencilValues+2)));
2843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int							width				= deMin32(128, renderTarget.getWidth());
2863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int							height				= deMin32(128, renderTarget.getHeight());
2873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	tcu::TestLog&				log					= m_testCtx.getLog();
2893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	de::Random					rnd					(deStringHash(m_name.c_str()));
2903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int							viewportX			= rnd.getInt(0, renderTarget.getWidth()-width);
2913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int							viewportY			= rnd.getInt(0, renderTarget.getHeight()-height);
2923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	IVec4						viewport			= IVec4(viewportX, viewportY, width, height);
2933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	tcu::Surface				gles2Frame			(width, height);
2953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	tcu::Surface				refFrame			(width, height);
2963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	GLenum						gles2Error;
2973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const char*					failReason			= DE_NULL;
2993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Get ops for stencil values
3013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	vector<vector<StencilOp> >	ops(numStencilValues+2);
3023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
3033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Values from 0 to max
3043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int ndx = 0; ndx < numStencilValues; ndx++)
3053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			genOps(ops[ndx], stencilBits, depthBits, deMin32(ndx*stencilStep, (1<<stencilBits)-1));
3063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// -1 and max+1
3083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		genOps(ops[numStencilValues+0], stencilBits, depthBits, 1<<stencilBits);
3093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		genOps(ops[numStencilValues+1], stencilBits, depthBits, -1);
3103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
3113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Compute cells: (x, y, w, h)
3133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	vector<IVec4>				cells;
3143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int							cellWidth			= width/gridSize;
3153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int							cellHeight			= height/gridSize;
3163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int y = 0; y < gridSize; y++)
3173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int x = 0; x < gridSize; x++)
3183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		cells.push_back(IVec4(x*cellWidth, y*cellHeight, cellWidth, cellHeight));
3193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_ASSERT(ops.size() <= cells.size());
3213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Execute for gles2 context
3233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
3243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		sglr::GLContext context(m_context.getRenderContext(), log, 0 /* don't log calls or program */, viewport);
3253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_shaderID = context.createProgram(&m_shader);
3273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		context.clearColor(1.0f, 0.0f, 0.0f, 1.0f);
3293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		context.clear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
3303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int ndx = 0; ndx < (int)ops.size(); ndx++)
3323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			executeOps(context, cells[ndx], ops[ndx]);
3333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		visualizeStencil(context, stencilBits, stencilStep);
3353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gles2Error = context.getError();
3373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		context.readPixels(gles2Frame, 0, 0, width, height);
3383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
3393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Execute for reference context
3413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
3423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		sglr::ReferenceContextBuffers	buffers	(tcu::PixelFormat(8,8,8,renderTarget.getPixelFormat().alphaBits?8:0), renderTarget.getDepthBits(), renderTarget.getStencilBits(), width, height);
3433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		sglr::ReferenceContext			context	(sglr::ReferenceContextLimits(m_context.getRenderContext()), buffers.getColorbuffer(), buffers.getDepthbuffer(), buffers.getStencilbuffer());
3443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_shaderID = context.createProgram(&m_shader);
3463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		context.clearColor(1.0f, 0.0f, 0.0f, 1.0f);
3483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		context.clear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
3493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int ndx = 0; ndx < (int)ops.size(); ndx++)
3513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			executeOps(context, cells[ndx], ops[ndx]);
3523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		visualizeStencil(context, stencilBits, stencilStep);
3543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		context.readPixels(refFrame, 0, 0, width, height);
3563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
3573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Check error
3593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool errorCodeOk = (gles2Error == GL_NO_ERROR);
3603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (!errorCodeOk && !failReason)
3613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		failReason = "Got unexpected error";
3623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Compare images
3643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const float		threshold	= 0.02f;
3653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool			imagesOk	= tcu::fuzzyCompare(log, "ComparisonResult", "Image comparison result", refFrame, gles2Frame, threshold, tcu::COMPARE_LOG_RESULT);
3663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (!imagesOk && !failReason)
3683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		failReason = "Image comparison failed";
3693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Store test result
3713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool isOk = errorCodeOk && imagesOk;
3723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_testCtx.setTestResult(isOk ? QP_TEST_RESULT_PASS	: QP_TEST_RESULT_FAIL,
3733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry							isOk ? "Pass"				: failReason);
3743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return STOP;
3763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3783c827367444ee418f129b2c238299f49d3264554Jarkko PoyryStencilTests::StencilTests (Context& context)
3793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	: TestCaseGroup(context, "stencil", "Stencil Tests")
3803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3833c827367444ee418f129b2c238299f49d3264554Jarkko PoyryStencilTests::~StencilTests (void)
3843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3873c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytypedef void (*GenStencilOpsFunc) (vector<StencilOp>& dst, int stencilBits, int depthBits, int targetStencil);
3883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3893c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass SimpleStencilCase : public StencilCase
3903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3913c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
3923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	SimpleStencilCase (Context& context, const char* name, const char* description, GenStencilOpsFunc genOpsFunc)
3933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		: StencilCase	(context, name, description)
3943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		, m_genOps		(genOpsFunc)
3953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
3963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
3973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void genOps (vector<StencilOp>& dst, int stencilBits, int depthBits, int targetStencil)
3993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
4003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_genOps(dst, stencilBits, depthBits, targetStencil);
4013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
4023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4033c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
4043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	GenStencilOpsFunc	m_genOps;
4053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
4063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4073c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid StencilTests::init (void)
4083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
4093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define STENCIL_CASE(NAME, DESCRIPTION, GEN_OPS_BODY)														\
4103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	do {																									\
4113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		struct Gen_##NAME {																					\
4123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			static void genOps (vector<StencilOp>& dst, int stencilBits, int depthBits, int targetStencil)	\
4133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{																								\
4143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				DE_UNREF(stencilBits && depthBits);															\
4153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				GEN_OPS_BODY																				\
4163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}																								\
4173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		};																									\
4183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		addChild(new SimpleStencilCase(m_context, #NAME, DESCRIPTION, Gen_##NAME::genOps));					\
4193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	} while (deGetFalse());
4203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	STENCIL_CASE(clear, "Stencil clear",
4223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
4233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			// \note Unused bits are set to 1, clear should mask them out
4243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			int mask = (1<<stencilBits)-1;
4253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::clearStencil(targetStencil | ~mask));
4263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		});
4273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Replace in different points
4293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	STENCIL_CASE(stencil_fail_replace, "Set stencil on stencil fail",
4303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
4313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::quad(GL_NEVER, targetStencil, GL_ALWAYS, 0.0f, GL_REPLACE, GL_KEEP, GL_KEEP));
4323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		});
4333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	STENCIL_CASE(depth_fail_replace, "Set stencil on depth fail",
4343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
4353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::clearDepth(0.0f));
4363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::quad(GL_ALWAYS, targetStencil, GL_LESS, 0.5f, GL_KEEP, GL_REPLACE, GL_KEEP));
4373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		});
4383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	STENCIL_CASE(depth_pass_replace, "Set stencil on depth pass",
4393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
4403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::quad(GL_ALWAYS, targetStencil, GL_LESS, 0.0f, GL_KEEP, GL_KEEP, GL_REPLACE));
4413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		});
4423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Increment, decrement
4443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	STENCIL_CASE(incr_stencil_fail, "Increment on stencil fail",
4453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
4463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (targetStencil > 0)
4473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
4483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::clearStencil(targetStencil-1));
4493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::quad(GL_EQUAL, targetStencil, GL_ALWAYS, 0.0f, GL_INCR, GL_KEEP, GL_KEEP));
4503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
4513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			else
4523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::clearStencil(targetStencil));
4533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		});
4543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	STENCIL_CASE(decr_stencil_fail, "Decrement on stencil fail",
4553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
4563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			int maxStencil = (1<<stencilBits)-1;
4573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (targetStencil < maxStencil)
4583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
4593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::clearStencil(targetStencil+1));
4603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::quad(GL_EQUAL, targetStencil, GL_ALWAYS, 0.0f, GL_DECR, GL_KEEP, GL_KEEP));
4613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
4623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			else
4633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::clearStencil(targetStencil));
4643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		});
4653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	STENCIL_CASE(incr_wrap_stencil_fail, "Increment (wrap) on stencil fail",
4663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
4673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			int maxStencil = (1<<stencilBits)-1;
4683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::clearStencil((targetStencil-1)&maxStencil));
4693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::quad(GL_EQUAL, targetStencil, GL_ALWAYS, 0.0f, GL_INCR_WRAP, GL_KEEP, GL_KEEP));
4703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		});
4713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	STENCIL_CASE(decr_wrap_stencil_fail, "Decrement (wrap) on stencil fail",
4723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
4733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			int maxStencil = (1<<stencilBits)-1;
4743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::clearStencil((targetStencil+1)&maxStencil));
4753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::quad(GL_EQUAL, targetStencil, GL_ALWAYS, 0.0f, GL_DECR_WRAP, GL_KEEP, GL_KEEP));
4763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		});
4773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Zero, Invert
4793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	STENCIL_CASE(zero_stencil_fail, "Zero on stencil fail",
4803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
4813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::clearStencil(targetStencil));
4823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::quad(GL_NOTEQUAL, targetStencil, GL_ALWAYS, 0.0f, GL_ZERO, GL_KEEP, GL_KEEP));
4833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::quad(GL_EQUAL, targetStencil, GL_ALWAYS, 0.0f, GL_REPLACE, GL_KEEP, GL_KEEP));
4843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		});
4853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	STENCIL_CASE(invert_stencil_fail, "Invert on stencil fail",
4863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
4873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			int mask = (1<<stencilBits)-1;
4883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::clearStencil((~targetStencil)&mask));
4893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::quad(GL_EQUAL, targetStencil, GL_ALWAYS, 0.0f, GL_INVERT, GL_KEEP, GL_KEEP));
4903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		});
4913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Comparison modes
4933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	STENCIL_CASE(cmp_equal, "Equality comparison",
4943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
4953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			int mask = (1<<stencilBits)-1;
4963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			int inv  = (~targetStencil)&mask;
4973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::clearStencil(inv));
4983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::quad(GL_EQUAL, inv, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INVERT));
4993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::quad(GL_EQUAL, inv, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INVERT));
5003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		});
5013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	STENCIL_CASE(cmp_not_equal, "Equality comparison",
5023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
5033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			int mask = (1<<stencilBits)-1;
5043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			int inv  = (~targetStencil)&mask;
5053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::clearStencil(inv));
5063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::quad(GL_NOTEQUAL, targetStencil, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INVERT));
5073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::quad(GL_NOTEQUAL, targetStencil, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INVERT));
5083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		});
5093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	STENCIL_CASE(cmp_less_than, "Less than comparison",
5103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
5113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			int maxStencil = (1<<stencilBits)-1;
5123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (targetStencil < maxStencil)
5133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
5143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::clearStencil(targetStencil+1));
5153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::quad(GL_LESS, targetStencil, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_DECR));
5163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::quad(GL_LESS, targetStencil, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_DECR));
5173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
5183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			else
5193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::clearStencil(targetStencil));
5203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		});
5213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	STENCIL_CASE(cmp_less_or_equal, "Less or equal comparison",
5223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
5233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			int maxStencil = (1<<stencilBits)-1;
5243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (targetStencil < maxStencil)
5253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
5263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::clearStencil(targetStencil+1));
5273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::quad(GL_LEQUAL, targetStencil+1, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_DECR));
5283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::quad(GL_LEQUAL, targetStencil+1, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_DECR));
5293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
5303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			else
5313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::clearStencil(targetStencil));
5323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		});
5333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	STENCIL_CASE(cmp_greater_than, "Greater than comparison",
5343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
5353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (targetStencil > 0)
5363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
5373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::clearStencil(targetStencil-1));
5383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::quad(GL_GREATER, targetStencil, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INCR));
5393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::quad(GL_GREATER, targetStencil, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INCR));
5403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
5413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			else
5423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::clearStencil(targetStencil));
5433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		});
5443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	STENCIL_CASE(cmp_greater_or_equal, "Greater or equal comparison",
5453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
5463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (targetStencil > 0)
5473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
5483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::clearStencil(targetStencil-1));
5493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::quad(GL_GEQUAL, targetStencil-1, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INCR));
5503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::quad(GL_GEQUAL, targetStencil-1, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INCR));
5513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
5523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			else
5533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				dst.push_back(StencilOp::clearStencil(targetStencil));
5543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		});
5553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	STENCIL_CASE(cmp_mask_equal, "Equality comparison with mask",
5563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
5573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			int valMask = (1<<stencilBits)-1;
5583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			int mask	= (1<<7)|(1<<5)|(1<<3)|(1<<1);
5593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(StencilOp::clearStencil(~targetStencil));
5603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			StencilOp op = StencilOp::quad(GL_EQUAL, (~targetStencil | ~mask) & valMask, GL_ALWAYS, 0.0f, GL_KEEP, GL_KEEP, GL_INVERT);
5613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			op.stencilMask = mask;
5623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			dst.push_back(op);
5633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		});
5643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
5653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // Functional
5673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // gles2
5683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // deqp
569