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