fbo-mrt-alphatest: Actually require MRTs to be available.
[piglit.git] / tests / general / depth-clear-precision-check.c
blob318df08d1bf4a402105972d1c9fb0c6dbc80f203
1 /*
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
13 * Software.
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
21 * IN THE SOFTWARE.
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[] = {
51 "depth16",
52 "depth16",
53 test,
54 (void *)GL_DEPTH_COMPONENT16
57 "depth24",
58 "depth24",
59 test,
60 (void *)GL_DEPTH_COMPONENT24
63 "depth32",
64 "depth32",
65 test,
66 (void *)GL_DEPTH_COMPONENT32
69 "depth24_stencil8",
70 "depth24_stencil8",
71 test,
72 (void *)GL_DEPTH24_STENCIL8
75 "depth32f",
76 "depth32f",
77 test,
78 (void *)GL_DEPTH_COMPONENT32F
81 "depth32f_stencil8",
82 "depth32f_stencil8",
83 test,
84 (void *)GL_DEPTH32F_STENCIL8
86 {0}};
88 static GLuint prog;
90 static const char *vs_source =
91 "#version 110 \n"
92 "void main() \n"
93 "{ \n"
94 " gl_Position = gl_Vertex;\n"
95 "} \n";
97 static const char *fs_source =
98 "#version 110 \n"
99 "uniform float depth;\n"
100 "void main() \n"
101 "{ \n"
102 " gl_FragColor = vec4(0.0, 1.0, 0.0, 0.0);\n"
103 " gl_FragDepth = depth;\n"
104 "} \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
112 * for this format
114 static const uint32_t iteration_pow = 8;
116 uint32_t
117 depth_num_of_bits(uint32_t depth)
119 switch (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;
126 default: return 8;
130 bool
131 check_color(float depth)
133 int depth_location;
134 static const GLfloat green[] = {0.0, 1.0, 0.0, 1.0};
136 glClearDepth(depth);
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);
148 enum piglit_result
149 test(void *data)
151 GLuint fb;
152 GLuint db, cb;
153 GLenum depth_format = (long unsigned)data;
155 GLenum status;
157 if (depth_format == GL_DEPTH24_STENCIL8)
158 if (!piglit_is_extension_supported(
159 "GL_EXT_packed_depth_stencil"))
160 return PIGLIT_SKIP;
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"))
166 return PIGLIT_SKIP;
168 glUseProgram(prog);
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)
197 return PIGLIT_SKIP;
199 if (status != GL_FRAMEBUFFER_COMPLETE) {
200 printf("FBO incomplete status %s\n",
201 piglit_get_gl_enum_name(status));
202 return PIGLIT_FAIL;
205 bool pass = true;
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);
222 if (!pass) {
223 printf("Testing format: %s.\n"
224 "Mismatch between value set with "
225 "glClearDepth and set in shader. "
226 "Depth: %f\n",
227 piglit_get_gl_enum_name(depth_format),
228 fdepth);
229 break;
233 /* Cleanup. */
234 glDeleteRenderbuffers(1, &cb);
235 glDeleteRenderbuffers(1, &db);
236 glDeleteFramebuffers(1, &fb);
238 if (!piglit_check_gl_error(GL_NO_ERROR))
239 pass = false;
241 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
244 enum piglit_result
245 piglit_display(void)
247 enum piglit_result pass = PIGLIT_SKIP;
248 GLuint fs, vs;
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,
257 pass);
259 glDeleteProgram(prog);
260 glDeleteShader(fs);
261 glDeleteShader(vs);
263 return pass;
266 void piglit_init(int argc, char **argv)
268 piglit_require_extension("GL_ARB_framebuffer_object");