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