2 * Copyright (C) 2019, 2020, 2021, 2022 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 /** @file depth-clear-precision-check.c
27 * Tests that glClearDepth store on gpu exactly the transferred values
28 * with different depth's values and with different depth formats.
31 #include "piglit-util-gl.h"
33 const struct piglit_subtest subtests
[];
34 enum piglit_result
test(void *data
);
35 static struct piglit_gl_test_config
*piglit_config
;
37 PIGLIT_GL_TEST_CONFIG_BEGIN
39 piglit_config
= &config
;
40 config
.subtests
= subtests
;
41 config
.supports_gl_compat_version
= 10;
42 config
.window_width
= 20;
43 config
.window_height
= 20;
44 config
.window_visual
= PIGLIT_GL_VISUAL_DOUBLE
;
45 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
47 PIGLIT_GL_TEST_CONFIG_END
49 const struct piglit_subtest subtests
[] = {
54 (void *)GL_DEPTH_COMPONENT16
60 (void *)GL_DEPTH_COMPONENT24
66 (void *)GL_DEPTH_COMPONENT32
72 (void *)GL_DEPTH24_STENCIL8
78 (void *)GL_DEPTH_COMPONENT32F
84 (void *)GL_DEPTH32F_STENCIL8
90 static const char *vs_source
=
94 " gl_Position = gl_Vertex;\n"
97 static const char *fs_source
=
99 "uniform float depth;\n"
102 " gl_FragColor = vec4(0.0, 1.0, 0.0, 0.0);\n"
103 " gl_FragDepth = depth;\n"
107 * number of iteration must be below than all possible,
108 * because for big format (like 32bit) if testing every value,
109 * time of test approaches infinity. iteration_pow controls
110 * sampling rate, we test 2^iteration_pow number of values,
111 * independent of format and evenly chosen in all possible values
114 static const uint32_t iteration_pow
= 8;
117 depth_num_of_bits(uint32_t depth
)
120 case GL_DEPTH_COMPONENT16
: return 16;
121 case GL_DEPTH_COMPONENT24
: return 24;
122 case GL_DEPTH_COMPONENT32
: return 32;
123 case GL_DEPTH24_STENCIL8
: return 24;
124 case GL_DEPTH_COMPONENT32F
: return 32;
125 case GL_DEPTH32F_STENCIL8
: return 32;
131 check_color(float depth
)
134 static const GLfloat green
[] = {0.0, 1.0, 0.0, 1.0};
137 glClearColor(1.0, 0.0, 0.0, 1.0);
138 glClear(GL_DEPTH_BUFFER_BIT
| GL_COLOR_BUFFER_BIT
);
140 depth_location
= glGetUniformLocation(prog
, "depth");
141 glUniform1f(depth_location
, depth
);
143 piglit_draw_rect(-1, -1, 2, 2);
145 return piglit_probe_pixel_rgb(0, 0, green
);
153 GLenum depth_format
= (long unsigned)data
;
157 if (depth_format
== GL_DEPTH24_STENCIL8
)
158 if (!piglit_is_extension_supported(
159 "GL_EXT_packed_depth_stencil"))
162 if (depth_format
== GL_DEPTH_COMPONENT32F
||
163 depth_format
== GL_DEPTH32F_STENCIL8
)
164 if (!piglit_is_extension_supported(
165 "GL_ARB_depth_buffer_float"))
170 /* Create the FBO. */
171 glGenRenderbuffers(1, &db
);
172 glBindRenderbuffer(GL_RENDERBUFFER
, db
);
174 glRenderbufferStorage(GL_RENDERBUFFER
, depth_format
,
175 piglit_width
, piglit_height
);
177 glGenRenderbuffers(1, &cb
);
178 glBindRenderbuffer(GL_RENDERBUFFER
, cb
);
180 glRenderbufferStorage(GL_RENDERBUFFER
, GL_RGBA8
,
181 piglit_width
, piglit_height
);
183 glBindRenderbuffer(GL_RENDERBUFFER
, 0);
185 glGenFramebuffers(1, &fb
);
186 glBindFramebuffer(GL_FRAMEBUFFER
, fb
);
188 glFramebufferRenderbuffer(GL_FRAMEBUFFER
, GL_DEPTH_ATTACHMENT
,
189 GL_RENDERBUFFER
, db
);
191 glFramebufferRenderbuffer(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
192 GL_RENDERBUFFER
, cb
);
194 status
= glCheckFramebufferStatus(GL_FRAMEBUFFER
);
196 if (status
== GL_FRAMEBUFFER_UNSUPPORTED
)
199 if (status
!= GL_FRAMEBUFFER_COMPLETE
) {
200 printf("FBO incomplete status %s\n",
201 piglit_get_gl_enum_name(status
));
207 glBindFramebuffer(GL_DRAW_FRAMEBUFFER
, fb
);
208 glEnable(GL_DEPTH_TEST
);
209 glDepthFunc(GL_EQUAL
);
211 uint32_t numOfBits
= depth_num_of_bits(depth_format
);
212 uint32_t maxStoreableValue
= (1l << numOfBits
) - 1;
213 uint32_t step
= 1 << (numOfBits
- iteration_pow
);
215 for (uint64_t depth
= 0; depth
< maxStoreableValue
; depth
+= step
) {
216 float fdepth
= depth
/ (float)maxStoreableValue
;
218 assert(maxStoreableValue
* fdepth
== depth
);
220 pass
= check_color(fdepth
);
223 printf("Testing format: %s.\n"
224 "Mismatch between value set with "
225 "glClearDepth and set in shader. "
227 piglit_get_gl_enum_name(depth_format
),
234 glDeleteRenderbuffers(1, &cb
);
235 glDeleteRenderbuffers(1, &db
);
236 glDeleteFramebuffers(1, &fb
);
238 if (!piglit_check_gl_error(GL_NO_ERROR
))
241 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
247 enum piglit_result pass
= PIGLIT_SKIP
;
250 vs
= piglit_compile_shader_text(GL_VERTEX_SHADER
, vs_source
);
251 fs
= piglit_compile_shader_text(GL_FRAGMENT_SHADER
, fs_source
);
252 prog
= piglit_link_simple_program(vs
, fs
);
254 pass
= piglit_run_selected_subtests(piglit_config
->subtests
,
255 piglit_config
->selected_subtests
,
256 piglit_config
->num_selected_subtests
,
259 glDeleteProgram(prog
);
266 void piglit_init(int argc
, char **argv
)
268 piglit_require_extension("GL_ARB_framebuffer_object");