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
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
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
31 #include "piglit-util-gl.h"
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;
53 test_float(int x
, int y
, void *values
)
55 GLfloat value
= ((GLfloat
*)values
)[y
* BUF_WIDTH
+ x
];
66 /* Default OpenGL "1 in 10^5" */
68 /* Framebuffer precision */
70 limit
= 1.0 / (1 << depth_bits
);
72 if (fabs(value
- expected
) > limit
) {
75 "Expected %f at (%d,%d), found %f\n",
76 expected
, x
, y
, value
);
84 test_unsigned_int(int x
, int y
, void *values
)
86 GLuint value
= ((GLuint
*)values
)[y
* BUF_WIDTH
+ x
];
88 GLuint high_bits
, low_bits
;
93 expected
= 0x80000000;
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
)) {
106 "Expected 0x%08x at (%d,%d), found 0x%08x\n",
107 expected
, x
, y
, value
);
115 test_unsigned_short(int x
, int y
, void *values
)
117 GLushort value
= ((GLushort
*)values
)[y
* BUF_WIDTH
+ x
];
127 if ((GLshort
)(value
- expected
) > 1) {
129 " GL_UNSIGNED_SHORT: "
130 "Expected 0x%04x at (%d,%d), found 0x%04x\n",
131 expected
, x
, y
, value
);
139 test_unsigned_byte(int x
, int y
, void *values
)
141 GLubyte value
= ((GLubyte
*)values
)[y
* BUF_WIDTH
+ x
];
151 if ((GLbyte
)(value
- expected
) > 1) {
153 " GL_UNSIGNED_BYTE: "
154 "Expected 0x%02x at (%d,%d), found 0x%02x\n",
155 expected
, x
, y
, value
);
164 bool (*test
)(int x
, int y
, void *values
);
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
},
173 test_with_format(GLenum internal_format
, const char *name
)
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
];
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
,
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
);
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
++) {
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;
241 piglit_report_subtest_result((format_passed
?
242 PIGLIT_PASS
: PIGLIT_FAIL
),
245 piglit_get_gl_enum_name(
246 read_formats
[i
].token
));
247 pass
= format_passed
&& pass
;
251 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, piglit_winsys_fbo
);
252 glDeleteFramebuffersEXT(1, &fb
);
253 glDeleteRenderbuffersEXT(1, &rb
);
257 GLenum rb_internal_formats
[] = {
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
)
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
++) {
276 piglit_get_gl_enum_name(rb_internal_formats
[i
]);
277 pass
= test_with_format(rb_internal_formats
[i
],
281 piglit_report_result(pass
? PIGLIT_PASS
: PIGLIT_FAIL
);