1 /*-------------------------------------------------------------------------
2 * OpenGL Conformance Test Suite
3 * -----------------------------
5 * Copyright (c) 2016 Google Inc.
6 * Copyright (c) 2016 The Khronos Group Inc.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 * \brief gl_FragDepth tests.
23 */ /*-------------------------------------------------------------------*/
25 #include "glcFragDepthTests.hpp"
27 #include "deRandom.hpp"
29 #include "gluDrawUtil.hpp"
30 #include "gluPixelTransfer.hpp"
31 #include "gluShaderProgram.hpp"
32 #include "glwEnums.hpp"
33 #include "glwFunctions.hpp"
34 #include "tcuImageCompare.hpp"
35 #include "tcuRenderTarget.hpp"
36 #include "tcuStringTemplate.hpp"
37 #include "tcuSurface.hpp"
38 #include "tcuTestLog.hpp"
39 #include "tcuVector.hpp"
41 // For setupDefaultUniforms()
42 #include "glcShaderRenderCase.hpp"
54 typedef float (*EvalFragDepthFunc)(const Vec2& coord);
56 static const char* s_vertexShaderSrc = "${VERSION_DECL}\n"
57 "in highp vec4 a_position;\n"
58 "in highp vec2 a_coord;\n"
59 "out highp vec2 v_coord;\n"
62 " gl_Position = a_position;\n"
63 " v_coord = a_coord;\n"
65 static const char* s_defaultFragmentShaderSrc = "${VERSION_DECL}\n"
66 "uniform highp vec4 u_color;\n"
67 "layout(location = 0) out mediump vec4 o_color;\n"
70 " o_color = u_color;\n"
74 static inline bool compare(deUint32 func, T a, T b)
100 static std::string specializeVersion(const std::string& source, glu::GLSLVersion version)
102 DE_ASSERT(version == glu::GLSL_VERSION_300_ES || version == glu::GLSL_VERSION_310_ES ||
103 version >= glu::GLSL_VERSION_330);
104 std::map<std::string, std::string> args;
105 args["VERSION_DECL"] = glu::getGLSLVersionDeclaration(version);
106 return tcu::StringTemplate(source.c_str()).specialize(args);
109 class FragDepthCompareCase : public TestCase
112 FragDepthCompareCase(Context& context, const char* name, const char* desc, glu::GLSLVersion glslVersion,
113 const char* fragSrc, EvalFragDepthFunc evalFunc, deUint32 compareFunc);
114 ~FragDepthCompareCase(void);
116 IterateResult iterate(void);
119 glu::GLSLVersion m_glslVersion;
121 EvalFragDepthFunc m_evalFunc;
122 deUint32 m_compareFunc;
125 FragDepthCompareCase::FragDepthCompareCase(Context& context, const char* name, const char* desc,
126 glu::GLSLVersion glslVersion, const char* fragSrc,
127 EvalFragDepthFunc evalFunc, deUint32 compareFunc)
128 : TestCase(context, name, desc)
129 , m_glslVersion(glslVersion)
131 , m_evalFunc(evalFunc)
132 , m_compareFunc(compareFunc)
136 FragDepthCompareCase::~FragDepthCompareCase(void)
140 FragDepthCompareCase::IterateResult FragDepthCompareCase::iterate(void)
142 TestLog& log = m_testCtx.getLog();
143 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
144 de::Random rnd(deStringHash(getName()));
145 const tcu::RenderTarget& renderTarget = m_context.getRenderContext().getRenderTarget();
146 int viewportW = de::min(128, renderTarget.getWidth());
147 int viewportH = de::min(128, renderTarget.getHeight());
148 int viewportX = rnd.getInt(0, renderTarget.getWidth() - viewportW);
149 int viewportY = rnd.getInt(0, renderTarget.getHeight() - viewportH);
150 tcu::Surface renderedFrame(viewportW, viewportH);
151 tcu::Surface referenceFrame(viewportW, viewportH);
152 const float constDepth = 0.1f;
154 if (renderTarget.getDepthBits() == 0)
155 throw tcu::NotSupportedError("Depth buffer is required", "", __FILE__, __LINE__);
157 gl.viewport(viewportX, viewportY, viewportW, viewportH);
158 gl.clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
159 gl.enable(GL_DEPTH_TEST);
161 static const deUint16 quadIndices[] = { 0, 1, 2, 2, 1, 3 };
163 // Fill viewport with 2 quads - one with constant depth and another with d = [-1..1]
165 glu::ShaderProgram basicQuadProgram(
166 m_context.getRenderContext(),
167 glu::makeVtxFragSources(specializeVersion(s_vertexShaderSrc, m_glslVersion).c_str(),
168 specializeVersion(s_defaultFragmentShaderSrc, m_glslVersion).c_str()));
170 if (!basicQuadProgram.isOk())
172 log << basicQuadProgram;
173 TCU_FAIL("Compile failed");
176 const float constDepthCoord[] = { -1.0f, -1.0f, constDepth, 1.0f, -1.0f, +1.0f, constDepth, 1.0f,
177 0.0f, -1.0f, constDepth, 1.0f, 0.0f, +1.0f, constDepth, 1.0f };
178 const float varyingDepthCoord[] = { 0.0f, -1.0f, +1.0f, 1.0f, 0.0f, +1.0f, 0.0f, 1.0f,
179 +1.0f, -1.0f, 0.0f, 1.0f, +1.0f, +1.0f, -1.0f, 1.0f };
181 gl.useProgram(basicQuadProgram.getProgram());
182 gl.uniform4f(gl.getUniformLocation(basicQuadProgram.getProgram(), "u_color"), 0.0f, 0.0f, 1.0f, 1.0f);
183 gl.depthFunc(GL_ALWAYS);
186 glu::VertexArrayBinding posBinding = glu::va::Float("a_position", 4, 4, 0, &constDepthCoord[0]);
187 glu::draw(m_context.getRenderContext(), basicQuadProgram.getProgram(), 1, &posBinding,
188 glu::pr::Triangles(DE_LENGTH_OF_ARRAY(quadIndices), &quadIndices[0]));
192 glu::VertexArrayBinding posBinding = glu::va::Float("a_position", 4, 4, 0, &varyingDepthCoord[0]);
193 glu::draw(m_context.getRenderContext(), basicQuadProgram.getProgram(), 1, &posBinding,
194 glu::pr::Triangles(DE_LENGTH_OF_ARRAY(quadIndices), &quadIndices[0]));
197 GLU_EXPECT_NO_ERROR(gl.getError(), "Draw base quads");
200 // Render with depth test.
202 glu::ShaderProgram program(m_context.getRenderContext(),
203 glu::makeVtxFragSources(specializeVersion(s_vertexShaderSrc, m_glslVersion).c_str(),
204 specializeVersion(m_fragSrc, m_glslVersion).c_str()));
208 TCU_FAIL("Compile failed");
210 const float coord[] = { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f };
211 const float position[] = { -1.0f, -1.0f, +1.0f, 1.0f, -1.0f, +1.0f, 0.0f, 1.0f,
212 +1.0f, -1.0f, 0.0f, 1.0f, +1.0f, +1.0f, -1.0f, 1.0f };
214 gl.useProgram(program.getProgram());
215 gl.depthFunc(m_compareFunc);
216 gl.uniform4f(gl.getUniformLocation(program.getProgram(), "u_color"), 0.0f, 1.0f, 0.0f, 1.0f);
218 // Setup default helper uniforms.
219 setupDefaultUniforms(m_context.getRenderContext(), program.getProgram());
222 glu::VertexArrayBinding vertexArrays[] = { glu::va::Float("a_position", 4, 4, 0, &position[0]),
223 glu::va::Float("a_coord", 2, 4, 0, &coord[0]) };
224 glu::draw(m_context.getRenderContext(), program.getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays),
225 &vertexArrays[0], glu::pr::Triangles(DE_LENGTH_OF_ARRAY(quadIndices), &quadIndices[0]));
228 GLU_EXPECT_NO_ERROR(gl.getError(), "Draw test quad");
231 glu::readPixels(m_context.getRenderContext(), viewportX, viewportY, renderedFrame.getAccess());
234 for (int y = 0; y < referenceFrame.getHeight(); y++)
236 float yf = ((float)y + 0.5f) / (float)referenceFrame.getHeight();
237 int half = de::clamp((int)((float)referenceFrame.getWidth() * 0.5f + 0.5f), 0, referenceFrame.getWidth());
239 // Fill left half - comparison to constant 0.5
240 for (int x = 0; x < half; x++)
242 float xf = ((float)x + 0.5f) / (float)referenceFrame.getWidth();
243 float d = m_evalFunc(Vec2(xf, yf));
244 bool dpass = compare(m_compareFunc, d, constDepth * 0.5f + 0.5f);
246 referenceFrame.setPixel(x, y, dpass ? tcu::RGBA::green() : tcu::RGBA::blue());
249 // Fill right half - comparison to interpolated depth
250 for (int x = half; x < referenceFrame.getWidth(); x++)
252 float xf = ((float)x + 0.5f) / (float)referenceFrame.getWidth();
253 float xh = ((float)x - (float)half + 0.5f) / (float)(referenceFrame.getWidth() - half);
254 float rd = 1.0f - (xh + yf) * 0.5f;
255 float d = m_evalFunc(Vec2(xf, yf));
256 bool dpass = compare(m_compareFunc, d, rd);
258 referenceFrame.setPixel(x, y, dpass ? tcu::RGBA::green() : tcu::RGBA::blue());
262 bool isOk = tcu::fuzzyCompare(log, "Result", "Image comparison result", referenceFrame, renderedFrame, 0.05f,
263 tcu::COMPARE_LOG_RESULT);
264 m_testCtx.setTestResult(isOk ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL, isOk ? "Pass" : "Fail");
268 class FragDepthWriteCase : public TestCase
271 FragDepthWriteCase(Context& context, const char* name, const char* desc, glu::GLSLVersion glslVersion,
272 const char* fragSrc, EvalFragDepthFunc evalFunc);
273 ~FragDepthWriteCase(void);
275 IterateResult iterate(void);
278 glu::GLSLVersion m_glslVersion;
280 EvalFragDepthFunc m_evalFunc;
283 FragDepthWriteCase::FragDepthWriteCase(Context& context, const char* name, const char* desc,
284 glu::GLSLVersion glslVersion, const char* fragSrc, EvalFragDepthFunc evalFunc)
285 : TestCase(context, name, desc), m_glslVersion(glslVersion), m_fragSrc(fragSrc), m_evalFunc(evalFunc)
289 FragDepthWriteCase::~FragDepthWriteCase(void)
293 FragDepthWriteCase::IterateResult FragDepthWriteCase::iterate(void)
295 TestLog& log = m_testCtx.getLog();
296 const glw::Functions& gl = m_context.getRenderContext().getFunctions();
297 de::Random rnd(deStringHash(getName()));
298 const tcu::RenderTarget& renderTarget = m_context.getRenderContext().getRenderTarget();
299 int viewportW = de::min(128, renderTarget.getWidth());
300 int viewportH = de::min(128, renderTarget.getHeight());
301 int viewportX = rnd.getInt(0, renderTarget.getWidth() - viewportW);
302 int viewportY = rnd.getInt(0, renderTarget.getHeight() - viewportH);
303 tcu::Surface renderedFrame(viewportW, viewportH);
304 tcu::Surface referenceFrame(viewportW, viewportH);
305 const int numDepthSteps = 16;
306 const float depthStep = 1.0f / (float)(numDepthSteps - 1);
308 if (renderTarget.getDepthBits() == 0)
309 throw tcu::NotSupportedError("Depth buffer is required", "", __FILE__, __LINE__);
311 gl.viewport(viewportX, viewportY, viewportW, viewportH);
312 gl.clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
313 gl.enable(GL_DEPTH_TEST);
314 gl.depthFunc(GL_LESS);
316 static const deUint16 quadIndices[] = { 0, 1, 2, 2, 1, 3 };
318 // Render with given shader.
320 glu::ShaderProgram program(m_context.getRenderContext(),
321 glu::makeVtxFragSources(specializeVersion(s_vertexShaderSrc, m_glslVersion).c_str(),
322 specializeVersion(m_fragSrc, m_glslVersion).c_str()));
326 TCU_FAIL("Compile failed");
328 const float coord[] = { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f };
329 const float position[] = { -1.0f, -1.0f, +1.0f, 1.0f, -1.0f, +1.0f, 0.0f, 1.0f,
330 +1.0f, -1.0f, 0.0f, 1.0f, +1.0f, +1.0f, -1.0f, 1.0f };
332 gl.useProgram(program.getProgram());
333 gl.uniform4f(gl.getUniformLocation(program.getProgram(), "u_color"), 0.0f, 1.0f, 0.0f, 1.0f);
335 // Setup default helper uniforms.
336 setupDefaultUniforms(m_context.getRenderContext(), program.getProgram());
339 glu::VertexArrayBinding vertexArrays[] = { glu::va::Float("a_position", 4, 4, 0, &position[0]),
340 glu::va::Float("a_coord", 2, 4, 0, &coord[0]) };
341 glu::draw(m_context.getRenderContext(), program.getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays),
342 &vertexArrays[0], glu::pr::Triangles(DE_LENGTH_OF_ARRAY(quadIndices), &quadIndices[0]));
345 GLU_EXPECT_NO_ERROR(gl.getError(), "Draw test quad");
348 // Visualize by rendering full-screen quads with increasing depth and color.
350 glu::ShaderProgram program(
351 m_context.getRenderContext(),
352 glu::makeVtxFragSources(specializeVersion(s_vertexShaderSrc, m_glslVersion).c_str(),
353 specializeVersion(s_defaultFragmentShaderSrc, m_glslVersion).c_str()));
357 TCU_FAIL("Compile failed");
360 int posLoc = gl.getAttribLocation(program.getProgram(), "a_position");
361 int colorLoc = gl.getUniformLocation(program.getProgram(), "u_color");
363 gl.useProgram(program.getProgram());
364 gl.depthMask(GL_FALSE);
366 for (int stepNdx = 0; stepNdx < numDepthSteps; stepNdx++)
368 float f = (float)stepNdx * depthStep;
369 float depth = f * 2.0f - 1.0f;
370 Vec4 color = Vec4(f, f, f, 1.0f);
372 const float position[] = { -1.0f, -1.0f, depth, 1.0f, -1.0f, +1.0f, depth, 1.0f,
373 +1.0f, -1.0f, depth, 1.0f, +1.0f, +1.0f, depth, 1.0f };
374 glu::VertexArrayBinding posBinding = glu::va::Float(posLoc, 4, 4, 0, &position[0]);
376 gl.uniform4fv(colorLoc, 1, color.getPtr());
377 glu::draw(m_context.getRenderContext(), program.getProgram(), 1, &posBinding,
378 glu::pr::Triangles(DE_LENGTH_OF_ARRAY(quadIndices), &quadIndices[0]));
381 GLU_EXPECT_NO_ERROR(gl.getError(), "Visualization draw");
384 glu::readPixels(m_context.getRenderContext(), viewportX, viewportY, renderedFrame.getAccess());
387 for (int y = 0; y < referenceFrame.getHeight(); y++)
389 for (int x = 0; x < referenceFrame.getWidth(); x++)
391 float xf = ((float)x + 0.5f) / (float)referenceFrame.getWidth();
392 float yf = ((float)y + 0.5f) / (float)referenceFrame.getHeight();
393 float d = m_evalFunc(Vec2(xf, yf));
394 int step = (int)deFloatFloor(d / depthStep);
395 int col = de::clamp(deRoundFloatToInt32((float)step * depthStep * 255.0f), 0, 255);
397 referenceFrame.setPixel(x, y, tcu::RGBA(col, col, col, 0xff));
401 bool isOk = tcu::fuzzyCompare(log, "Result", "Image comparison result", referenceFrame, renderedFrame, 0.05f,
402 tcu::COMPARE_LOG_RESULT);
403 m_testCtx.setTestResult(isOk ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL, isOk ? "Pass" : "Fail");
407 FragDepthTests::FragDepthTests(Context& context, glu::GLSLVersion glslVersion)
408 : TestCaseGroup(context, "fragdepth", "gl_FragDepth tests"), m_glslVersion(glslVersion)
412 FragDepthTests::~FragDepthTests(void)
416 static float evalConstDepth(const Vec2& coord)
421 static float evalDynamicDepth(const Vec2& coord)
423 return (coord.x() + coord.y()) * 0.5f;
425 static float evalNoWrite(const Vec2& coord)
427 return 1.0f - (coord.x() + coord.y()) * 0.5f;
430 static float evalDynamicConditionalDepth(const Vec2& coord)
432 float d = (coord.x() + coord.y()) * 0.5f;
433 if (coord.y() < 0.5f)
439 void FragDepthTests::init(void)
445 EvalFragDepthFunc evalFunc;
448 { "no_write", "No gl_FragDepth write", evalNoWrite, "${VERSION_DECL}\n"
449 "uniform highp vec4 u_color;\n"
450 "layout(location = 0) out mediump vec4 o_color;\n"
453 " o_color = u_color;\n"
455 { "const", "Const depth write", evalConstDepth, "${VERSION_DECL}\n"
456 "uniform highp vec4 u_color;\n"
457 "layout(location = 0) out mediump vec4 o_color;\n"
460 " o_color = u_color;\n"
461 " gl_FragDepth = 0.5;\n"
463 { "uniform", "Uniform depth write", evalConstDepth, "${VERSION_DECL}\n"
464 "uniform highp vec4 u_color;\n"
465 "uniform highp float uf_half;\n"
466 "layout(location = 0) out mediump vec4 o_color;\n"
469 " o_color = u_color;\n"
470 " gl_FragDepth = uf_half;\n"
472 { "dynamic", "Dynamic depth write", evalDynamicDepth, "${VERSION_DECL}\n"
473 "uniform highp vec4 u_color;\n"
474 "in highp vec2 v_coord;\n"
475 "layout(location = 0) out mediump vec4 o_color;\n"
478 " o_color = u_color;\n"
479 " gl_FragDepth = (v_coord.x+v_coord.y)*0.5;\n"
481 { "fragcoord_z", "gl_FragDepth write from gl_FragCoord.z", evalNoWrite,
483 "uniform highp vec4 u_color;\n"
484 "layout(location = 0) out mediump vec4 o_color;\n"
487 " o_color = u_color;\n"
488 " gl_FragDepth = gl_FragCoord.z;\n"
490 { "uniform_conditional_write", "Uniform conditional write", evalDynamicDepth,
492 "uniform highp vec4 u_color;\n"
493 "uniform bool ub_true;\n"
494 "in highp vec2 v_coord;\n"
495 "layout(location = 0) out mediump vec4 o_color;\n"
498 " o_color = u_color;\n"
500 " gl_FragDepth = (v_coord.x+v_coord.y)*0.5;\n"
502 { "dynamic_conditional_write", "Uniform conditional write", evalDynamicConditionalDepth,
504 "uniform highp vec4 u_color;\n"
505 "uniform bool ub_true;\n"
506 "in highp vec2 v_coord;\n"
507 "layout(location = 0) out mediump vec4 o_color;\n"
510 " o_color = u_color;\n"
511 " mediump float d = (v_coord.x+v_coord.y)*0.5f;\n"
512 " if (v_coord.y < 0.5)\n"
513 " gl_FragDepth = d;\n"
515 " gl_FragDepth = 1.0 - d;\n"
517 { "uniform_loop_write", "Uniform loop write", evalConstDepth, "${VERSION_DECL}\n"
518 "uniform highp vec4 u_color;\n"
519 "uniform int ui_two;\n"
520 "uniform highp float uf_fourth;\n"
521 "in highp vec2 v_coord;\n"
522 "layout(location = 0) out mediump vec4 o_color;\n"
525 " o_color = u_color;\n"
526 " gl_FragDepth = 0.0;\n"
527 " for (int i = 0; i < ui_two; i++)\n"
528 " gl_FragDepth += uf_fourth;\n"
530 { "write_in_function", "Uniform loop write", evalDynamicDepth,
532 "uniform highp vec4 u_color;\n"
533 "uniform highp float uf_half;\n"
534 "in highp vec2 v_coord;\n"
535 "layout(location = 0) out mediump vec4 o_color;\n"
536 "void myfunc (highp vec2 coord)\n"
538 " gl_FragDepth = (coord.x+coord.y)*0.5;\n"
542 " o_color = u_color;\n"
543 " myfunc(v_coord);\n"
548 tcu::TestCaseGroup* writeGroup = new tcu::TestCaseGroup(m_testCtx, "write", "gl_FragDepth write tests");
549 addChild(writeGroup);
550 for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(cases); ndx++)
551 writeGroup->addChild(new FragDepthWriteCase(m_context, cases[ndx].name, cases[ndx].desc, m_glslVersion,
552 cases[ndx].fragSrc, cases[ndx].evalFunc));
555 tcu::TestCaseGroup* compareGroup =
556 new tcu::TestCaseGroup(m_testCtx, "compare", "gl_FragDepth used with depth comparison");
557 addChild(compareGroup);
558 for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(cases); ndx++)
559 compareGroup->addChild(new FragDepthCompareCase(m_context, cases[ndx].name, cases[ndx].desc, m_glslVersion,
560 cases[ndx].fragSrc, cases[ndx].evalFunc, GL_LESS));