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 glDrawPixels(GL_DEPTH_COMPONENT)
29 * We don't rely on glReadPixels(GL_DEPTH_COMPONENT) in case it's not
30 * working. Instead we test by drawing an image into the depth buffer
31 * while setting the color buffer to white. Next, we draw quads just
32 * in front and behind where we expect the Z values to be. The quad
33 * behind should be invisible while the quad in front should be totally
37 #include "piglit-util-gl.h"
39 PIGLIT_GL_TEST_CONFIG_BEGIN
41 config
.supports_gl_compat_version
= 10;
43 config
.window_width
= 200;
44 config
.window_height
= 200;
45 config
.window_visual
= PIGLIT_GL_VISUAL_RGB
| PIGLIT_GL_VISUAL_DOUBLE
| PIGLIT_GL_VISUAL_DEPTH
;
46 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
48 PIGLIT_GL_TEST_CONFIG_END
51 draw_z_gradient(GLfloat zLeft
, GLfloat zRight
)
55 verts
[0][0] = -1.0; verts
[0][1] = -1.0; verts
[0][2] = zLeft
;
56 verts
[1][0] = 1.0; verts
[1][1] = -1.0; verts
[1][2] = zRight
;
57 verts
[2][0] = 1.0; verts
[2][1] = 1.0; verts
[2][2] = zRight
;
58 verts
[3][0] = -1.0; verts
[3][1] = 1.0; verts
[3][2] = zLeft
;
60 glEnableClientState(GL_VERTEX_ARRAY
);
61 glVertexPointer(3, GL_FLOAT
, 0, verts
);
62 glDrawArrays(GL_TRIANGLE_FAN
, 0, 4);
69 /* a tight epsilon isn't important for this test */
70 const GLfloat epsilon
= 4.0 / piglit_width
;
71 static const GLfloat white
[4] = { 1, 1, 1, 1 };
72 static const GLfloat green
[4] = { 0, 1, 0, 1 };
73 static const GLfloat red
[4] = { 1, 0, 0, 1 };
79 /* For both glDrawPixels and the polygon rendering below we
80 * use a range of Z values in [0, 1] where 0=near and 1=far.
81 * So object Z coords are the same as normalized depth coords.
85 zRight
= 1.0 - epsilon
;
87 /* create image of Z values increasing from left to right */
89 malloc(piglit_width
* piglit_height
* sizeof(GLfloat
));
90 for (j
= 0; j
< piglit_height
; j
++) {
91 for (i
= 0; i
< piglit_width
; i
++) {
92 float z
= i
/ (float) (piglit_width
- 1);
93 z
= zLeft
+ z
* (zRight
- zLeft
);
94 buf
[j
* piglit_width
+ i
] = z
;
98 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
100 /* glDrawPixels the Z gradient image */
103 glDrawPixels(piglit_width
, piglit_height
,
104 GL_DEPTH_COMPONENT
, GL_FLOAT
, buf
);
108 /* draw a red quad behind the Z gradient - it should not be visible */
110 draw_z_gradient(zLeft
+ epsilon
, zRight
+ epsilon
);
111 if (!piglit_probe_rect_rgb(0, 0, piglit_width
, piglit_height
, white
)) {
112 printf("Quad behind test failed\n");
116 /* draw green quad in front of the Z gradient - it should be visible */
118 draw_z_gradient(zLeft
- epsilon
, zRight
- epsilon
);
119 if (!piglit_probe_rect_rgb(0, 0, piglit_width
, piglit_height
, green
)) {
120 printf("Quad in front test failed\n");
124 piglit_present_results();
126 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
131 piglit_init(int argc
, char **argv
)
133 /* Setup projection matrix such that zObj=0 becomes zBuffer=0
134 * and zObj=1 becomes zBuffer=1 (identity transform).
135 * So, glOrtho maps zObj=0 to zNDC=-1 and maps zObj=1 to zNDC=1.
136 * Then, zNDC=-1 maps to zBuffer=0 and zNDC=1 maps to zBuffer=1.
138 glMatrixMode(GL_PROJECTION
);
140 glOrtho(-1.0, 1.0, -1.0, 1.0, 0.0, -1.0);
142 glEnable(GL_DEPTH_TEST
);