fbo-mrt-alphatest: Actually require MRTs to be available.
[piglit.git] / tests / general / early-z.c
blobec6790073913eba321423cd6e750f327accc7b71
1 /*
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
13 * Software.
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
22 * SOFTWARE.
25 /**
26 * @file
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
40 GLint u_zval;
42 static const char *fpFragDepthText =
43 "uniform float zval; \n"
44 "void main() \n"
45 "{ \n"
46 " gl_FragColor = vec4(gl_Color.rgb, 1.0); \n"
47 " gl_FragDepth = zval; \n"
48 "} \n";
50 static const char *fpDiscardText =
51 "void main() \n"
52 "{ \n"
53 " if (gl_Color.r > 0.25) \n"
54 " discard; \n"
55 " gl_FragColor = vec4(gl_Color.rgb, 1.0); \n"
56 "} \n";
58 static const char *fpAlphaText =
59 "void main() \n"
60 "{ \n"
61 " gl_FragColor = vec4(gl_Color.rgb, 0.0); \n"
62 "} \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);
71 glBegin(GL_QUADS);
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);
76 glEnd();
79 static GLboolean
80 test_early_depth(void)
82 glClearColor(0.0, 0.0, 0.0, 0.0);
83 glClearDepth(1.0);
84 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
86 glEnable(GL_DEPTH_TEST);
87 glDepthMask(GL_TRUE);
88 glDepthFunc(GL_LESS);
90 /* 1. (blue) depth should be adjusted to 0.8 by the FP */
91 glUseProgram(program[0]);
92 glUniform1f(u_zval, 0.8f);
93 quad(0.3, 0xff0000);
95 /* 2. (red) should be discarded, no depth value written */
96 glUseProgram(program[1]);
97 quad(0.2, 0x0000ff);
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);
103 quad(0.1, 0xffffff);
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);
109 quad(0.9, 0x00ff00);
111 /* 5. (yellow) should be discarded because program sets depth to 0.9 */
112 glUniform1f(u_zval, 0.9);
113 quad(0.2, 0x00ffff);
116 const GLfloat color[4] = { 0.0f, 1.0f, 0.0f, 1.0f };
117 GLint pos[4];
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();
125 return GL_FALSE;
129 piglit_present_results();
131 return GL_TRUE;
135 enum piglit_result
136 piglit_display(void)
138 if (!test_early_depth())
139 return PIGLIT_FAIL;
141 return PIGLIT_PASS;
145 void
146 piglit_init(int argc, char **argv)
148 int i;
150 piglit_require_gl_version(20);
152 shader[0] = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fpFragDepthText);
153 assert(shader[0]);
155 shader[1] = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fpDiscardText);
156 assert(shader[1]);
158 shader[2] = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fpAlphaText);
159 assert(shader[2]);
161 for (i = 0; i < 3; ++i)
162 program[i] = piglit_link_simple_program(shader[i], 0);
164 u_zval = glGetUniformLocation(program[0], "zval");