2 * Copyright 2012 VMware, Inc.
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
21 * DEALINGS IN THE SOFTWARE.
25 * Test glReadPixels(GL_DEPTH_COMPONENT)
30 #include "piglit-util-gl.h"
32 PIGLIT_GL_TEST_CONFIG_BEGIN
34 config
.supports_gl_compat_version
= 10;
36 config
.window_width
= 200;
37 config
.window_height
= 200;
38 config
.window_visual
= PIGLIT_GL_VISUAL_RGB
| PIGLIT_GL_VISUAL_DOUBLE
| PIGLIT_GL_VISUAL_DEPTH
;
39 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
41 PIGLIT_GL_TEST_CONFIG_END
45 /** Display contents of the depth buffer as grayscale color */
47 display_depth(const GLfloat
*buf
)
50 glDrawPixels(piglit_width
, piglit_height
,
51 GL_LUMINANCE
, GL_FLOAT
, buf
);
55 /** Test glClear(GL_DEPTH_BUFFER_BIT) + glReadPixels */
59 GLfloat
*buf
= malloc(piglit_width
* piglit_height
* sizeof(GLfloat
));
61 double diff
, tolerance
;
64 glGetIntegerv(GL_DEPTH_BITS
, &zBits
);
66 /* allow 1-bit error */
67 tolerance
= 1.0 / (1 << (zBits
- 1));
69 for (z
= 0.0f
; z
<= 1.0f
; z
+= 0.125) {
71 glClear(GL_DEPTH_BUFFER_BIT
);
73 glReadPixels(0, 0, piglit_width
, piglit_height
,
74 GL_DEPTH_COMPONENT
, GL_FLOAT
, buf
);
76 if (!piglit_automatic
) {
78 piglit_present_results();
81 /* Make sure all the values are the same */
82 for (i
= 1; i
< piglit_width
* piglit_height
; i
++) {
83 if (buf
[i
] != buf
[0]) {
84 printf("depth[%d]=%f != depth[0]=%f\n",
91 /* Check that the depth value read back is within tolerance */
93 if (diff
> tolerance
) {
94 printf("Depth buffer clear failed!\n");
95 printf("Expected %f, found %f\n", z
, buf
[0]);
107 draw_z_gradient(GLfloat zLeft
, GLfloat zRight
)
111 verts
[0][0] = -1.0; verts
[0][1] = -1.0; verts
[0][2] = zLeft
;
112 verts
[1][0] = 1.0; verts
[1][1] = -1.0; verts
[1][2] = zRight
;
113 verts
[2][0] = 1.0; verts
[2][1] = 1.0; verts
[2][2] = zRight
;
114 verts
[3][0] = -1.0; verts
[3][1] = 1.0; verts
[3][2] = zLeft
;
116 glEnableClientState(GL_VERTEX_ARRAY
);
117 glVertexPointer(3, GL_FLOAT
, 0, verts
);
118 glDrawArrays(GL_TRIANGLE_FAN
, 0, 4);
123 test_z_gradient(void)
125 const GLfloat epsilon
= 2.0 / piglit_width
;
130 /* draw full-window quad with z increasing left to right */
131 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
132 glEnable(GL_DEPTH_TEST
);
133 draw_z_gradient(-1.0, 1.0);
134 glDisable(GL_DEPTH_TEST
);
136 buf
= (GLfloat
*) malloc(piglit_width
* piglit_height
* sizeof(GLfloat
));
138 /* read whole buffer */
139 glReadPixels(0, 0, piglit_width
, piglit_height
,
140 GL_DEPTH_COMPONENT
, GL_FLOAT
, buf
);
142 /* examine a horizontal row at mid-Y */
143 row
= buf
+ piglit_width
* piglit_height
/ 2;
146 if (fabs(row
[pos
] - 0.0) > epsilon
) {
147 printf("Left-most Z value should be close to 0.0, found %f\n",
152 pos
= piglit_width
/ 2;
153 if (fabs(row
[pos
] - 0.5) > epsilon
) {
154 printf("Middle Z value should be close to 0.5, found %f\n",
159 pos
= piglit_width
- 1;
160 if (fabs(row
[pos
] - 1.0) > epsilon
) {
161 printf("Left-most Z value should be close to 1.0, found %f\n",
166 /* check for monotonicity */
167 for (i
= 1; i
< piglit_width
; i
++) {
168 if (row
[i
- 1] > row
[i
]) {
169 printf("Z values aren't increasing from left to right. row[%d]=%f > row[%d]=%f\n",
170 i
-1, row
[i
-1], i
, row
[i
]);
176 if (!piglit_automatic
) {
178 piglit_present_results();
193 if (!test_z_gradient())
201 piglit_init(int argc
, char **argv
)