shader_runner: don't use deleted query objects
[piglit.git] / tests / fbo / fbo-readpixels-depth-formats.c
blobcc4ef9374731ea79ffaa78ce7637ae0a314f2e1b
1 /*
2 * Copyright © 2011 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.
24 /** @file fbo-readpixels-depth-formats.c
26 * Tests that various formats of depth renderbuffers can be read
27 * correctly using glReadPixels() with various format/type
28 * combinations.
31 #include "piglit-util-gl.h"
33 #define BUF_WIDTH 15
34 #define BUF_HEIGHT 15
36 PIGLIT_GL_TEST_CONFIG_BEGIN
38 config.supports_gl_compat_version = 10;
40 config.window_width = BUF_WIDTH;
41 config.window_height = BUF_WIDTH;
42 config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
43 config.khr_no_error_support = PIGLIT_NO_ERRORS;
45 PIGLIT_GL_TEST_CONFIG_END
47 /* Width of our stripes of z = 0.0, 0.5, 1.0 */
48 static int w = BUF_WIDTH / 3;
50 int depth_bits;
52 static bool
53 test_float(int x, int y, void *values)
55 GLfloat value = ((GLfloat *)values)[y * BUF_WIDTH + x];
56 GLfloat expected;
57 GLfloat limit;
59 if (x < w)
60 expected = 0.0;
61 else if (x < w * 2)
62 expected = 0.5;
63 else
64 expected = 1.0;
66 /* Default OpenGL "1 in 10^5" */
67 limit = .00001;
68 /* Framebuffer precision */
69 if (depth_bits < 24)
70 limit = 1.0 / (1 << depth_bits);
72 if (fabs(value - expected) > limit) {
73 fprintf(stderr,
74 " GL_FLOAT: "
75 "Expected %f at (%d,%d), found %f\n",
76 expected, x, y, value);
77 return false;
80 return true;
83 static bool
84 test_unsigned_int(int x, int y, void *values)
86 GLuint value = ((GLuint *)values)[y * BUF_WIDTH + x];
87 GLuint expected;
88 GLuint high_bits, low_bits;
90 if (x < w)
91 expected = 0x0;
92 else if (x < w * 2)
93 expected = 0x80000000;
94 else
95 expected = 0xffffffff;
97 low_bits = (1 << (32 - depth_bits)) - 1;
98 high_bits = ~low_bits;
100 expected &= high_bits;
101 expected |= expected >> depth_bits;
103 if ((GLint)(value - expected) > 1 << (32 - depth_bits)) {
104 fprintf(stderr,
105 " GL_UNSIGNED_INT: "
106 "Expected 0x%08x at (%d,%d), found 0x%08x\n",
107 expected, x, y, value);
108 return false;
111 return true;
114 static bool
115 test_unsigned_short(int x, int y, void *values)
117 GLushort value = ((GLushort *)values)[y * BUF_WIDTH + x];
118 GLushort expected;
120 if (x < w)
121 expected = 0x0000;
122 else if (x < w * 2)
123 expected = 0x8000;
124 else
125 expected = 0xffff;
127 if ((GLshort)(value - expected) > 1) {
128 fprintf(stderr,
129 " GL_UNSIGNED_SHORT: "
130 "Expected 0x%04x at (%d,%d), found 0x%04x\n",
131 expected, x, y, value);
132 return false;
135 return true;
138 static bool
139 test_unsigned_byte(int x, int y, void *values)
141 GLubyte value = ((GLubyte *)values)[y * BUF_WIDTH + x];
142 GLubyte expected;
144 if (x < w)
145 expected = 0x00;
146 else if (x < w * 2)
147 expected = 0x80;
148 else
149 expected = 0xff;
151 if ((GLbyte)(value - expected) > 1) {
152 fprintf(stderr,
153 " GL_UNSIGNED_BYTE: "
154 "Expected 0x%02x at (%d,%d), found 0x%02x\n",
155 expected, x, y, value);
156 return false;
159 return true;
162 struct {
163 GLenum token;
164 bool (*test)(int x, int y, void *values);
165 } read_formats[] = {
166 { GL_FLOAT, test_float },
167 { GL_UNSIGNED_INT, test_unsigned_int },
168 { GL_UNSIGNED_SHORT, test_unsigned_short },
169 { GL_UNSIGNED_BYTE, test_unsigned_byte },
172 static bool
173 test_with_format(GLenum internal_format, const char *name)
175 GLuint rb, fb;
176 GLenum status;
177 bool pass = true;
178 /* Storage for the values read. The largest type is
179 * GLuint-sized, so this will be big enough for all types.
181 GLuint values[BUF_WIDTH * BUF_HEIGHT];
182 int i;
184 printf("testing %s:\n", name);
186 glGenFramebuffersEXT(1, &fb);
187 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
188 if (!piglit_check_gl_error(GL_NO_ERROR))
189 piglit_report_result(PIGLIT_FAIL);
191 glGenRenderbuffersEXT(1, &rb);
192 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rb);
193 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, internal_format,
194 BUF_WIDTH, BUF_HEIGHT);
195 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
196 GL_DEPTH_ATTACHMENT_EXT,
197 GL_RENDERBUFFER_EXT,
198 rb);
200 glDrawBuffer(GL_NONE);
201 glReadBuffer(GL_NONE);
203 status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
204 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
205 fprintf(stderr, "framebuffer incomplete\n");
206 piglit_report_subtest_result(PIGLIT_SKIP, "%s", name);
207 goto done;
210 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
212 glEnable(GL_DEPTH_TEST);
213 glDepthFunc(GL_ALWAYS);
214 glViewport(0, 0, BUF_WIDTH, BUF_HEIGHT);
215 piglit_ortho_projection(BUF_WIDTH, BUF_HEIGHT, false);
216 piglit_draw_rect_z(1.0, 0, 0, w, BUF_HEIGHT);
217 piglit_draw_rect_z(0.0, w, 0, w * 2, BUF_HEIGHT);
218 piglit_draw_rect_z(-1.0, w * 2, 0, w * 3, BUF_HEIGHT);
220 glPixelStorei(GL_PACK_ALIGNMENT, 1);
222 for (i = 0; i < ARRAY_SIZE(read_formats); i++) {
223 int x, y;
224 bool format_passed = true;
226 glReadPixels(0, 0, BUF_WIDTH, BUF_HEIGHT,
227 GL_DEPTH_COMPONENT, read_formats[i].token, values);
229 for (y = 0; y < BUF_HEIGHT; y++) {
230 for (x = 0; x < BUF_WIDTH; x++) {
231 if (!read_formats[i].test(x, y, values)) {
232 format_passed = false;
233 break;
236 if (x != BUF_WIDTH)
237 break;
241 piglit_report_subtest_result((format_passed ?
242 PIGLIT_PASS : PIGLIT_FAIL),
243 "%s/%s",
244 name,
245 piglit_get_gl_enum_name(
246 read_formats[i].token));
247 pass = format_passed && pass;
250 done:
251 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, piglit_winsys_fbo);
252 glDeleteFramebuffersEXT(1, &fb);
253 glDeleteRenderbuffersEXT(1, &rb);
254 return pass;
257 GLenum rb_internal_formats[] = {
258 GL_DEPTH_COMPONENT,
259 GL_DEPTH_COMPONENT32,
260 GL_DEPTH_COMPONENT24,
261 GL_DEPTH_COMPONENT16,
262 GL_DEPTH_STENCIL_EXT,
263 GL_DEPTH24_STENCIL8_EXT,
266 void piglit_init(int argc, char **argv)
268 int i;
269 bool pass = true;
271 piglit_require_extension("GL_EXT_framebuffer_object");
272 piglit_require_extension("GL_EXT_packed_depth_stencil");
274 for (i = 0; i < ARRAY_SIZE(rb_internal_formats); i++) {
275 const char *name =
276 piglit_get_gl_enum_name(rb_internal_formats[i]);
277 pass = test_with_format(rb_internal_formats[i],
278 name) && pass;
281 piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
284 enum piglit_result
285 piglit_display(void)
287 /* UNREACHED */
288 return PIGLIT_FAIL;