2 * Copyright (c) 2011 Christoph Bumiller
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NON-INFRINGEMENT. IN NO EVENT SHALL VMWARE AND/OR THEIR SUPPLIERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * Test for bugs with early depth testing and early depth update.
30 #include "piglit-util-gl.h"
32 PIGLIT_GL_TEST_CONFIG_BEGIN
34 config
.supports_gl_compat_version
= 10;
36 config
.window_visual
= PIGLIT_GL_VISUAL_RGBA
| PIGLIT_GL_VISUAL_DOUBLE
| PIGLIT_GL_VISUAL_DEPTH
;
38 PIGLIT_GL_TEST_CONFIG_END
42 static const char *fpFragDepthText
=
43 "uniform float zval; \n"
46 " gl_FragColor = vec4(gl_Color.rgb, 1.0); \n"
47 " gl_FragDepth = zval; \n"
50 static const char *fpDiscardText
=
53 " if (gl_Color.r > 0.25) \n"
55 " gl_FragColor = vec4(gl_Color.rgb, 1.0); \n"
58 static const char *fpAlphaText
=
61 " gl_FragColor = vec4(gl_Color.rgb, 0.0); \n"
65 static GLuint shader
[3];
66 static GLuint program
[3];
68 static void quad(const GLfloat z
, uint32_t colour
)
70 glColor3ub(colour
& 0xff, (colour
>> 8) & 0xff, (colour
>> 16) & 0xff);
72 glVertex3f(-1.0f
, -1.0f
, z
);
73 glVertex3f(-1.0f
, 1.0f
, z
);
74 glVertex3f( 1.0f
, 1.0f
, z
);
75 glVertex3f( 1.0f
, -1.0f
, z
);
80 test_early_depth(void)
82 glClearColor(0.0, 0.0, 0.0, 0.0);
84 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
86 glEnable(GL_DEPTH_TEST
);
90 /* 1. (blue) depth should be adjusted to 0.8 by the FP */
91 glUseProgram(program
[0]);
92 glUniform1f(u_zval
, 0.8f
);
95 /* 2. (red) should be discarded, no depth value written */
96 glUseProgram(program
[1]);
99 /* 3. (white) should be discarded by the alpha test, no depth written */
100 glUseProgram(program
[2]);
101 glEnable(GL_ALPHA_TEST
);
102 glAlphaFunc(GL_GREATER
, 0.5f
);
104 glDisable(GL_ALPHA_TEST
);
106 /* 4. (green) should be drawn because depth is 0.8 and FragDepth is 0.5 */
107 glUseProgram(program
[0]);
108 glUniform1f(u_zval
, 0.5);
111 /* 5. (yellow) should be discarded because program sets depth to 0.9 */
112 glUniform1f(u_zval
, 0.9);
116 const GLfloat color
[4] = { 0.0f
, 1.0f
, 0.0f
, 1.0f
};
119 /* use glRasterPos to determine where to read a sample pixel */
120 glRasterPos2f(0.0f
, 0.0f
);
121 glGetIntegerv(GL_CURRENT_RASTER_POSITION
, pos
);
123 if (!piglit_probe_pixel_rgba(pos
[0], pos
[1], color
)) {
124 piglit_present_results();
129 piglit_present_results();
138 if (!test_early_depth())
146 piglit_init(int argc
, char **argv
)
150 piglit_require_gl_version(20);
152 shader
[0] = piglit_compile_shader_text(GL_FRAGMENT_SHADER
, fpFragDepthText
);
155 shader
[1] = piglit_compile_shader_text(GL_FRAGMENT_SHADER
, fpDiscardText
);
158 shader
[2] = piglit_compile_shader_text(GL_FRAGMENT_SHADER
, fpAlphaText
);
161 for (i
= 0; i
< 3; ++i
)
162 program
[i
] = piglit_link_simple_program(shader
[i
], 0);
164 u_zval
= glGetUniformLocation(program
[0], "zval");