2 * Copyright © 2009 Intel Corporation
3 * Copyright © 2011 VMware, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * Eric Anholt <eric@anholt.net>
30 /** @file glsl-fs-fragcoord-zw-perspective.c
32 * Tests that gl_FragCoord.zw produces the expected output in a fragment shader
33 * with a perspective projection.
36 #include "piglit-util-gl.h"
38 PIGLIT_GL_TEST_CONFIG_BEGIN
40 config
.supports_gl_compat_version
= 10;
42 config
.window_width
= 256;
43 config
.window_height
= 256;
44 config
.window_visual
= PIGLIT_GL_VISUAL_RGB
| PIGLIT_GL_VISUAL_DOUBLE
| PIGLIT_GL_VISUAL_DEPTH
;
45 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
47 PIGLIT_GL_TEST_CONFIG_END
49 static GLfloat Znear
= 1.0, Zfar
= 10.0;
53 * Draw a quad where the bottom edge is on the near clip plane and the
54 * top edge is on the far clip plane. Adjust the far coordinates' X and Y
55 * values to fill the window. So the quad doesn't look like it's drawn in
56 * perspective, but it is (the top is much wider than the bottom).
61 float xBotLeft
= -Znear
, xBotRight
= Znear
;
62 float xTopLeft
= -Zfar
, xTopRight
= Zfar
;
63 float yBotLeft
= -Znear
, yBotRight
= -Znear
;
64 float yTopLeft
= Zfar
, yTopRight
= Zfar
;
65 float zBottom
= -Znear
, zTop
= -Zfar
;
68 verts
[0][0] = xBotLeft
;
69 verts
[0][1] = yBotLeft
;
70 verts
[0][2] = zBottom
;
72 verts
[1][0] = xBotRight
;
73 verts
[1][1] = yBotRight
;
74 verts
[1][2] = zBottom
;
76 verts
[2][0] = xTopRight
;
77 verts
[2][1] = yTopRight
;
80 verts
[3][0] = xTopLeft
;
81 verts
[3][1] = yTopLeft
;
85 glVertexPointer(4, GL_FLOAT
, 0, verts
);
86 glEnableClientState(GL_VERTEX_ARRAY
);
88 glDrawArrays(GL_TRIANGLE_FAN
, 0, 4);
90 glDisableClientState(GL_VERTEX_ARRAY
);
94 /** Convert t in [0,1] into a 1/w value */
98 float zEye
= -(Znear
+ t
* (Zfar
- Znear
));
108 GLboolean pass
= GL_TRUE
;
111 glClearColor(0.5, 0.5, 0.5, 0.5);
112 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
116 /* Spot-test Z values */
117 pass
= piglit_probe_pixel_depth(0, 0, 0.0) && pass
;
118 pass
= piglit_probe_pixel_depth(piglit_width
-1, 0, 0.0) && pass
;
119 pass
= piglit_probe_pixel_depth(piglit_width
-2, piglit_height
-1, 1.0) && pass
;
120 pass
= piglit_probe_pixel_depth(1, piglit_height
-2, 1.0) && pass
;
125 /* Test a column of pixel colors */
126 for (y
= 8; y
< piglit_height
; y
+= 16) {
128 float t
= y
/ (float) (piglit_height
- 1);
129 expected
[0] = t
; /* gl_FragCoord.z */
130 expected
[1] = w0
+ t
* (w1
- w0
); /* gl_FragCoord.w */
133 pass
= piglit_probe_pixel_rgb(piglit_width
/2, y
, expected
) & pass
;
136 piglit_present_results();
138 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
142 piglit_init(int argc
, char **argv
)
146 piglit_require_gl_version(20);
148 glMatrixMode(GL_PROJECTION
);
150 glFrustum(-1.0, 1.0, -1.0, 1.0, Znear
, Zfar
);
151 glMatrixMode(GL_MODELVIEW
);
154 vs
= piglit_compile_shader(GL_VERTEX_SHADER
,
155 "shaders/glsl-mvp.vert");
156 fs
= piglit_compile_shader(GL_FRAGMENT_SHADER
,
157 "shaders/glsl-fs-fragcoord-zw.frag");
159 prog
= piglit_link_simple_program(vs
, fs
);
163 glEnable(GL_DEPTH_TEST
);