glx-multithread-texture: Avoid front-buffer rendering.
[piglit.git] / tests / shaders / glsl-fs-fragcoord-zw-perspective.c
blob6e7cb793b319f8372f5888477dcc2cbb2f97ef61
1 /*
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
14 * Software.
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
22 * IN THE SOFTWARE.
24 * Authors:
25 * Eric Anholt <eric@anholt.net>
26 * Brian Paul
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;
52 /**
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).
58 static void
59 draw_quad(void)
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;
66 float verts[4][4];
68 verts[0][0] = xBotLeft;
69 verts[0][1] = yBotLeft;
70 verts[0][2] = zBottom;
71 verts[0][3] = 1.0;
72 verts[1][0] = xBotRight;
73 verts[1][1] = yBotRight;
74 verts[1][2] = zBottom;
75 verts[1][3] = 1.0;
76 verts[2][0] = xTopRight;
77 verts[2][1] = yTopRight;
78 verts[2][2] = zTop;
79 verts[2][3] = 1.0;
80 verts[3][0] = xTopLeft;
81 verts[3][1] = yTopLeft;
82 verts[3][2] = zTop;
83 verts[3][3] = 1.0;
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 */
95 static float
96 t_to_w(float t)
98 float zEye = -(Znear + t * (Zfar - Znear));
99 float wClip = -zEye;
100 return 1.0 / wClip;
104 enum piglit_result
105 piglit_display(void)
107 float w0, w1;
108 GLboolean pass = GL_TRUE;
109 int y;
111 glClearColor(0.5, 0.5, 0.5, 0.5);
112 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
114 draw_quad();
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;
122 w0 = t_to_w(0.0);
123 w1 = t_to_w(1.0);
125 /* Test a column of pixel colors */
126 for (y = 8; y < piglit_height; y += 16) {
127 float expected[3];
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 */
131 expected[2] = 0.0;
133 pass = piglit_probe_pixel_rgb(piglit_width/2, y, expected) & pass;
136 piglit_present_results();
138 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
141 void
142 piglit_init(int argc, char **argv)
144 GLint vs, fs, prog;
146 piglit_require_gl_version(20);
148 glMatrixMode(GL_PROJECTION);
149 glLoadIdentity();
150 glFrustum(-1.0, 1.0, -1.0, 1.0, Znear, Zfar);
151 glMatrixMode(GL_MODELVIEW);
152 glLoadIdentity();
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);
161 glUseProgram(prog);
163 glEnable(GL_DEPTH_TEST);