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