1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES 3.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.
22 *//*--------------------------------------------------------------------*/
24 #include "es3fDepthTests.hpp"
26 #include "tcuTestLog.hpp"
27 #include "gluPixelTransfer.hpp"
28 #include "tcuImageCompare.hpp"
29 #include "tcuRenderTarget.hpp"
30 #include "gluStrUtil.hpp"
32 #include "sglrContextUtil.hpp"
33 #include "sglrReferenceContext.hpp"
34 #include "sglrGLContext.hpp"
36 #include "deRandom.hpp"
38 #include "glwEnums.hpp"
49 class DepthShader : public sglr::ShaderProgram
54 void setColor (sglr::Context& ctx, deUint32 programID, const tcu::Vec4& color);
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;
60 const sglr::UniformSlot& u_color;
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("#version 300 es\n"
69 "in highp vec4 a_position;\n"
72 " gl_Position = a_position;\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"
79 " o_color = u_color;\n"
81 , u_color(getUniformByName("u_color"))
85 void DepthShader::setColor (sglr::Context& ctx, deUint32 programID, const tcu::Vec4& color)
87 ctx.useProgram(programID);
88 ctx.uniform4fv(ctx.getUniformLocation(programID, "u_color"), 1, color.getPtr());
91 void DepthShader::shadeVertices (const rr::VertexAttrib* inputs, rr::VertexPacket* const* packets, const int numPackets) const
93 for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx)
94 packets[packetNdx]->position = rr::readVertexAttribFloat(inputs[0], packets[packetNdx]->instanceNdx, packets[packetNdx]->vertexNdx);
97 void DepthShader::shadeFragments (rr::FragmentPacket* packets, const int numPackets, const rr::FragmentShadingContext& context) const
99 const tcu::Vec4 color(u_color.value.f4);
103 for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx)
104 for (int fragNdx = 0; fragNdx < 4; ++fragNdx)
105 rr::writeFragmentOutput(context, packetNdx, fragNdx, 0, color);
108 // \todo [2011-07-11 pyry] This code is duplicated in a quite many places. Move it to some utility?
109 class DepthCase : public TestCase
112 DepthCase (Context& context, const char* name, const char* description);
113 virtual ~DepthCase (void) {}
115 virtual IterateResult iterate (void);
116 virtual void render (sglr::Context& context) = DE_NULL;
119 DepthCase::DepthCase (Context& context, const char* name, const char* description)
120 : TestCase(context, name, description)
124 TestCase::IterateResult DepthCase::iterate (void)
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;
132 // Position & size for context
133 de::Random rnd(deStringHash(getName()));
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);
140 tcu::Surface gles2Frame (width, height);
141 tcu::Surface refFrame (width, height);
145 // Render using GLES2
147 sglr::GLContext context(renderCtx, log, sglr::GLCONTEXT_LOG_CALLS, tcu::IVec4(x, y, width, height));
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);
152 render(context); // Call actual render func
153 context.readPixels(gles2Frame, 0, 0, width, height);
154 gles2Error = context.getError();
157 // Render reference image
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());
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);
166 context.readPixels(refFrame, 0, 0, width, height);
167 refError = context.getError();
170 // Compare error codes
171 bool errorCodesOk = (gles2Error == refError);
175 log << tcu::TestLog::Message << "Error code mismatch: got " << glu::getErrorStr(gles2Error) << ", expected " << glu::getErrorStr(refError) << tcu::TestLog::EndMessage;
176 failReason = "Got unexpected error";
180 const float threshold = 0.02f;
181 bool imagesOk = tcu::fuzzyCompare(log, "ComparisonResult", "Image comparison result", refFrame, gles2Frame, threshold, tcu::COMPARE_LOG_RESULT);
183 if (!imagesOk && !failReason)
184 failReason = "Image comparison failed";
187 bool isOk = errorCodesOk && imagesOk;
188 m_testCtx.setTestResult(isOk ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL,
189 isOk ? "Pass" : failReason);
194 class DepthCompareCase : public DepthCase
197 DepthCompareCase (Context& context, const char* name, const char* description, deUint32 compareOp)
198 : DepthCase (context, name, description)
199 , m_compareOp (compareOp)
203 void render (sglr::Context& context)
208 deUint32 shaderID = context.createProgram(&shader);
210 tcu::Vec4 red (1.0f, 0.0f, 0.0f, 1.0);
211 tcu::Vec4 green (0.0f, 1.0f, 0.0f, 1.0f);
214 context.clearDepthf(1.0f);
215 context.clear(GL_DEPTH_BUFFER_BIT);
217 // Enable depth test.
218 context.enable(GL_DEPTH_TEST);
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));
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));
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));
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));
254 deUint32 m_compareOp;
257 DepthTests::DepthTests (Context& context)
258 : TestCaseGroup(context, "depth", "Depth Tests")
262 DepthTests::~DepthTests (void)
266 void DepthTests::init (void)
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));