Home | History | Annotate | Download | only in functional
      1 /*-------------------------------------------------------------------------
      2  * drawElements Quality Program OpenGL ES 2.0 Module
      3  * -------------------------------------------------
      4  *
      5  * Copyright 2014 The Android Open Source Project
      6  *
      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
     10  *
     11  *      http://www.apache.org/licenses/LICENSE-2.0
     12  *
     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.
     18  *
     19  *//*!
     20  * \file
     21  * \brief Depth tests.
     22  *//*--------------------------------------------------------------------*/
     23 
     24 #include "es2fDepthTests.hpp"
     25 
     26 #include "tcuTestLog.hpp"
     27 #include "gluPixelTransfer.hpp"
     28 #include "tcuImageCompare.hpp"
     29 #include "tcuRenderTarget.hpp"
     30 #include "gluStrUtil.hpp"
     31 
     32 #include "sglrContextUtil.hpp"
     33 #include "sglrReferenceContext.hpp"
     34 #include "sglrGLContext.hpp"
     35 
     36 #include "deRandom.hpp"
     37 
     38 #include "glwEnums.hpp"
     39 
     40 using tcu::RGBA;
     41 
     42 namespace deqp
     43 {
     44 namespace gles2
     45 {
     46 namespace Functional
     47 {
     48 
     49 class DepthShader : public sglr::ShaderProgram
     50 {
     51 public:
     52 								DepthShader		(void);
     53 
     54 	void						setColor		(sglr::Context& ctx, deUint32 programID, const tcu::Vec4& color);
     55 
     56 private:
     57 	void						shadeVertices	(const rr::VertexAttrib* inputs, rr::VertexPacket* const* packets, const int numPackets) const;
     58 	void						shadeFragments	(rr::FragmentPacket* packets, const int numPackets, const rr::FragmentShadingContext& context) const;
     59 
     60 	const sglr::UniformSlot&	u_color;
     61 };
     62 
     63 DepthShader::DepthShader (void)
     64 	: sglr::ShaderProgram(sglr::pdec::ShaderProgramDeclaration()
     65 							<< sglr::pdec::VertexAttribute("a_position", rr::GENERICVECTYPE_FLOAT)
     66 							<< sglr::pdec::FragmentOutput(rr::GENERICVECTYPE_FLOAT)
     67 							<< sglr::pdec::Uniform("u_color", glu::TYPE_FLOAT_VEC4)
     68 							<< sglr::pdec::VertexSource("attribute highp vec4 a_position;\n"
     69 														"void main (void)\n"
     70 														"{\n"
     71 														"	gl_Position = a_position;\n"
     72 														"}\n")
     73 							<< sglr::pdec::FragmentSource("uniform mediump vec4 u_color;\n"
     74 														  "void main (void)\n"
     75 														  "{\n"
     76 														  "	gl_FragColor = u_color;\n"
     77 														  "}\n"))
     78 	, u_color(getUniformByName("u_color"))
     79 {
     80 }
     81 
     82 void DepthShader::setColor (sglr::Context& ctx, deUint32 programID, const tcu::Vec4& color)
     83 {
     84 	ctx.useProgram(programID);
     85 	ctx.uniform4fv(ctx.getUniformLocation(programID, "u_color"), 1, color.getPtr());
     86 }
     87 
     88 void DepthShader::shadeVertices (const rr::VertexAttrib* inputs, rr::VertexPacket* const* packets, const int numPackets) const
     89 {
     90 	for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx)
     91 		packets[packetNdx]->position = rr::readVertexAttribFloat(inputs[0], packets[packetNdx]->instanceNdx, packets[packetNdx]->vertexNdx);
     92 }
     93 
     94 void DepthShader::shadeFragments (rr::FragmentPacket* packets, const int numPackets, const rr::FragmentShadingContext& context) const
     95 {
     96 	const tcu::Vec4 color(u_color.value.f4);
     97 
     98 	DE_UNREF(packets);
     99 
    100 	for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx)
    101 	for (int fragNdx = 0; fragNdx < 4; ++fragNdx)
    102 		rr::writeFragmentOutput(context, packetNdx, fragNdx, 0, color);
    103 }
    104 
    105 // \todo [2011-07-11 pyry] This code is duplicated in a quite many places. Move it to some utility?
    106 class DepthCase : public TestCase
    107 {
    108 public:
    109 								DepthCase				(Context& context, const char* name, const char* description);
    110 	virtual						~DepthCase				(void) {}
    111 
    112 	virtual IterateResult		iterate					(void);
    113 	virtual void				render					(sglr::Context& context) = DE_NULL;
    114 };
    115 
    116 DepthCase::DepthCase (Context& context, const char* name, const char* description)
    117 	: TestCase(context, name, description)
    118 {
    119 }
    120 
    121 TestCase::IterateResult DepthCase::iterate (void)
    122 {
    123 	tcu::Vec4					clearColor				= tcu::Vec4(0.125f, 0.25f, 0.5f, 1.0f);
    124 	glu::RenderContext&			renderCtx				= m_context.getRenderContext();
    125 	const tcu::RenderTarget&	renderTarget			= renderCtx.getRenderTarget();
    126 	tcu::TestLog&				log						= m_testCtx.getLog();
    127 	const char*					failReason				= DE_NULL;
    128 
    129 	// Position & size for context
    130 	de::Random rnd(deStringHash(getName()));
    131 
    132 	int		width	= deMin32(renderTarget.getWidth(),	128);
    133 	int		height	= deMin32(renderTarget.getHeight(),	128);
    134 	int		x		= rnd.getInt(0, renderTarget.getWidth()		- width);
    135 	int		y		= rnd.getInt(0, renderTarget.getHeight()	- height);
    136 
    137 	tcu::Surface	gles2Frame	(width, height);
    138 	tcu::Surface	refFrame	(width, height);
    139 	deUint32		gles2Error;
    140 	deUint32		refError;
    141 
    142 	// Render using GLES2
    143 	{
    144 		sglr::GLContext context(renderCtx, log, sglr::GLCONTEXT_LOG_CALLS, tcu::IVec4(x, y, width, height));
    145 
    146 		context.clearColor(clearColor.x(), clearColor.y(), clearColor.z(), clearColor.w());
    147 		context.clear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
    148 
    149 		render(context); // Call actual render func
    150 		context.readPixels(gles2Frame, 0, 0, width, height);
    151 		gles2Error = context.getError();
    152 	}
    153 
    154 	// Render reference image
    155 	{
    156 		sglr::ReferenceContextBuffers	buffers	(tcu::PixelFormat(8,8,8,renderTarget.getPixelFormat().alphaBits?8:0), renderTarget.getDepthBits(), renderTarget.getStencilBits(), width, height);
    157 		sglr::ReferenceContext			context	(sglr::ReferenceContextLimits(renderCtx), buffers.getColorbuffer(), buffers.getDepthbuffer(), buffers.getStencilbuffer());
    158 
    159 		context.clearColor(clearColor.x(), clearColor.y(), clearColor.z(), clearColor.w());
    160 		context.clear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
    161 
    162 		render(context);
    163 		context.readPixels(refFrame, 0, 0, width, height);
    164 		refError = context.getError();
    165 	}
    166 
    167 	// Compare error codes
    168 	bool errorCodesOk = (gles2Error == refError);
    169 
    170 	if (!errorCodesOk)
    171 	{
    172 		log << tcu::TestLog::Message << "Error code mismatch: got " << glu::getErrorStr(gles2Error) << ", expected " << glu::getErrorStr(refError) << tcu::TestLog::EndMessage;
    173 		failReason = "Got unexpected error";
    174 	}
    175 
    176 	// Compare images
    177 	const float		threshold	= 0.02f;
    178 	bool			imagesOk	= tcu::fuzzyCompare(log, "ComparisonResult", "Image comparison result", refFrame, gles2Frame, threshold, tcu::COMPARE_LOG_RESULT);
    179 
    180 	if (!imagesOk && !failReason)
    181 		failReason = "Image comparison failed";
    182 
    183 	// Store test result
    184 	bool isOk = errorCodesOk && imagesOk;
    185 	m_testCtx.setTestResult(isOk ? QP_TEST_RESULT_PASS	: QP_TEST_RESULT_FAIL,
    186 							isOk ? "Pass"				: failReason);
    187 
    188 	return STOP;
    189 }
    190 
    191 class DepthCompareCase : public DepthCase
    192 {
    193 public:
    194 	DepthCompareCase (Context& context, const char* name, const char* description, deUint32 compareOp)
    195 		: DepthCase		(context, name, description)
    196 		, m_compareOp	(compareOp)
    197 	{
    198 	}
    199 
    200 	void render (sglr::Context& context)
    201 	{
    202 		using tcu::Vec3;
    203 
    204 		DepthShader	shader;
    205 		deUint32	shaderID = context.createProgram(&shader);
    206 
    207 		tcu::Vec4	red		(1.0f, 0.0f, 0.0f, 1.0);
    208 		tcu::Vec4	green	(0.0f, 1.0f, 0.0f, 1.0f);
    209 
    210 		// Clear depth to 1
    211 		context.clearDepthf(1.0f);
    212 		context.clear(GL_DEPTH_BUFFER_BIT);
    213 
    214 		// Enable depth test.
    215 		context.enable(GL_DEPTH_TEST);
    216 
    217 		// Upper left: two quads with same depth
    218 		context.depthFunc(GL_ALWAYS);
    219 		shader.setColor(context, shaderID, red);
    220 		sglr::drawQuad(context, shaderID, Vec3(-1.0f, -1.0f, 0.2f),	Vec3(0.0f, 0.0f, 0.2f));
    221 		context.depthFunc(m_compareOp);
    222 		shader.setColor(context, shaderID, green);
    223 		sglr::drawQuad(context, shaderID, Vec3(-1.0f, -1.0f, 0.2f),	Vec3(0.0f, 0.0f, 0.2f));
    224 
    225 		// Lower left: two quads, d1 < d2
    226 		context.depthFunc(GL_ALWAYS);
    227 		shader.setColor(context, shaderID, red);
    228 		sglr::drawQuad(context, shaderID, Vec3(-1.0f, 0.0f, -0.4f),	Vec3(0.0f, 1.0f, -0.4f));
    229 		context.depthFunc(m_compareOp);
    230 		shader.setColor(context, shaderID, green);
    231 		sglr::drawQuad(context, shaderID, Vec3(-1.0f, 0.0f, -0.1f),	Vec3(0.0f, 1.0f, -0.1f));
    232 
    233 		// Upper right: two quads, d1 > d2
    234 		context.depthFunc(GL_ALWAYS);
    235 		shader.setColor(context, shaderID, red);
    236 		sglr::drawQuad(context, shaderID, Vec3(0.0f, -1.0f, 0.5f),	Vec3(1.0f, 0.0f, 0.5f));
    237 		context.depthFunc(m_compareOp);
    238 		shader.setColor(context, shaderID, green);
    239 		sglr::drawQuad(context, shaderID, Vec3(0.0f, -1.0f, 0.3f),	Vec3(1.0f, 0.0f, 0.3f));
    240 
    241 		// Lower right: two quads, d1 = 0, d2 = [-1..1]
    242 		context.depthFunc(GL_ALWAYS);
    243 		shader.setColor(context, shaderID, red);
    244 		sglr::drawQuad(context, shaderID, Vec3(0.0f, 0.0f, 0.0f),	Vec3(1.0f, 1.0f, 0.0f));
    245 		context.depthFunc(m_compareOp);
    246 		shader.setColor(context, shaderID, green);
    247 		sglr::drawQuad(context, shaderID, Vec3(0.0f, 0.0f, -1.0f),	Vec3(1.0f, 1.0f, 1.0f));
    248 	}
    249 
    250 private:
    251 	deUint32	m_compareOp;
    252 };
    253 
    254 DepthTests::DepthTests (Context& context)
    255 	: TestCaseGroup(context, "depth", "Depth Tests")
    256 {
    257 }
    258 
    259 DepthTests::~DepthTests (void)
    260 {
    261 }
    262 
    263 void DepthTests::init (void)
    264 {
    265 	addChild(new DepthCompareCase(m_context, "cmp_always",				"Always pass depth test",				GL_ALWAYS));
    266 	addChild(new DepthCompareCase(m_context, "cmp_never",				"Never pass depth test",				GL_NEVER));
    267 	addChild(new DepthCompareCase(m_context, "cmp_equal",				"Depth compare: equal",					GL_EQUAL));
    268 	addChild(new DepthCompareCase(m_context, "cmp_not_equal",			"Depth compare: not equal",				GL_NOTEQUAL));
    269 	addChild(new DepthCompareCase(m_context, "cmp_less_than",			"Depth compare: less than",				GL_LESS));
    270 	addChild(new DepthCompareCase(m_context, "cmp_less_or_equal",		"Depth compare: less than or equal",	GL_LEQUAL));
    271 	addChild(new DepthCompareCase(m_context, "cmp_greater_than",		"Depth compare: greater than",			GL_GREATER));
    272 	addChild(new DepthCompareCase(m_context, "cmp_greater_or_equal",	"Depth compare: greater than or equal",	GL_GEQUAL));
    273 }
    274 
    275 } // Functional
    276 } // gles2
    277 } // deqp
    278