cmake: move defaults into the per-platform section
[piglit.git] / tests / general / masked-clear.c
blobd7c49653606deaed594eec60dc8e7d210de08baa
1 /*
2 * Port of Glean maskedClear test to piglit. Original copyright follows.
3 *
4 * Copyright (C) 1999 Allen Akin All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
13 * conditions:
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the
17 * Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
20 * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
21 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ALLEN AKIN BE
23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
24 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
25 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
30 * Test color/depth/stencil masking with glClear.
34 #include "piglit-util-gl.h"
36 PIGLIT_GL_TEST_CONFIG_BEGIN
37 config.supports_gl_compat_version = 10;
38 config.window_visual = (PIGLIT_GL_VISUAL_RGB |
39 PIGLIT_GL_VISUAL_DEPTH |
40 PIGLIT_GL_VISUAL_STENCIL |
41 PIGLIT_GL_VISUAL_DOUBLE);
42 config.requires_displayed_window = true;
43 config.khr_no_error_support = PIGLIT_NO_ERRORS;
44 PIGLIT_GL_TEST_CONFIG_END
47 static void
48 failRGB(GLint chan, GLfloat expected,
49 GLfloat actual, GLenum buffer)
51 static const char *chanNames[] = { "Red", "Green", "Blue", "Alpha" };
52 GLboolean mask[4];
53 glGetBooleanv(GL_COLOR_WRITEMASK, mask);
54 fprintf(stderr, "masked-clear: %s is %f, expected %f in %s\n",
55 chanNames[chan], actual, expected,
56 piglit_get_gl_enum_name(buffer));
57 fprintf(stderr, "\tGL_COLOR_WRITEMASK = (%s, %s, %s, %s)\n",
58 (mask[0] ? "GL_TRUE" : "GL_FALSE"),
59 (mask[1] ? "GL_TRUE" : "GL_FALSE"),
60 (mask[2] ? "GL_TRUE" : "GL_FALSE"),
61 (mask[3] ? "GL_TRUE" : "GL_FALSE"));
65 static void
66 failZ(GLfloat expected, GLfloat actual)
68 GLboolean mask;
69 glGetBooleanv(GL_DEPTH_WRITEMASK, &mask);
70 fprintf(stderr, "masked-clear: depth buffer value is %f, expected %f\n",
71 actual, expected);
72 fprintf(stderr, "\tGL_DEPTH_WRITEMASK = %s\n",
73 mask ? "GL_TRUE" : "GL_FALSE");
77 static void
78 failStencil(GLuint expected, GLuint actual)
80 GLint mask;
81 glGetIntegerv(GL_STENCIL_WRITEMASK, &mask);
82 fprintf(stderr, "masked-clear: stencil buffer value is %d, expected %d\n",
83 actual, expected);
84 fprintf(stderr, "\tGL_STENCIL_WRITEMASK = 0x%x\n", mask);
88 static bool
89 test_color_masking(GLenum buffer)
91 GLint a;
92 int chan, comp, numChannels;
93 bool passed = true;
95 assert(buffer == GL_FRONT || buffer == GL_BACK);
97 glReadBuffer(buffer);
98 glDrawBuffer(buffer);
100 glGetIntegerv(GL_ALPHA_BITS, &a);
101 numChannels = a ? 4 : 3;
103 for (chan = 0; chan < numChannels && passed; chan++) {
104 GLfloat pixel[4];
106 /* clear to black */
107 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
108 glClearColor(0.0, 0.0, 0.0, 0.0);
109 glClear(GL_COLOR_BUFFER_BIT);
111 /* select one channel to "clear" to 1.0 */
112 glColorMask(chan == 0, chan == 1, chan == 2, chan == 3);
114 /* try to clear surface to white */
115 glClearColor(1.0, 1.0, 1.0, 1.0);
116 glClear(GL_COLOR_BUFFER_BIT);
118 /* read 1x1 image at (x,y)=(4,4) */
119 glReadPixels(4, 4, 1, 1, GL_RGBA, GL_FLOAT, pixel);
121 if (!piglit_automatic)
122 piglit_present_results();
124 /* test results */
125 for (comp = 0; comp < numChannels && passed; comp++) {
126 if (comp == chan) {
127 /* component should be 1.0 */
128 if (pixel[comp] < 0.5) {
129 passed = false;
130 failRGB(comp, 1.0,
131 pixel[comp], buffer);
133 } else {
134 /* component should be 0.0 */
135 if (pixel[comp] > 0.5) {
136 passed = false;
137 failRGB(comp, 0.0,
138 pixel[comp], buffer);
144 return passed;
148 static bool
149 test_depth_masking(void)
151 GLfloat depth;
152 bool passed = true;
154 /* clear depth buffer to zero */
155 glDepthMask(GL_TRUE);
156 glClearDepth(0.0);
157 glClear(GL_DEPTH_BUFFER_BIT);
159 /* disable Z writes, try to clear to one */
160 glDepthMask(GL_FALSE);
161 glClearDepth(1.0);
162 glClear(GL_DEPTH_BUFFER_BIT);
164 /* read 1x1 image at (x,y)=(4,4); */
165 glReadPixels(4, 4, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
167 /* test result */
168 if (depth != 0.0) {
169 passed = false;
170 failZ(0.0, depth);
173 return passed;
177 static bool
178 test_stencil_masking(void)
180 GLint stencilBits;
181 int bit;
182 bool passed = true;
184 glGetIntegerv(GL_STENCIL_BITS, &stencilBits);
186 /* We just run <stencilBits> tests rather than 2^stencilBits */
187 for (bit = 0; bit < stencilBits; bit++) {
188 GLuint stencil;
190 /* clear to 0 */
191 glStencilMask(~0);
192 glClearStencil(0);
193 glClear(GL_STENCIL_BUFFER_BIT);
195 /* select one bit to "clear" to 1 */
196 glStencilMask(1 << bit);
198 /* try to clear stencil buffer to ~0 */
199 glClearStencil(~0);
200 glClear(GL_STENCIL_BUFFER_BIT);
202 /* read 1x1 image at (x,y)=(4,4) */
203 glReadPixels(4, 4, 1, 1,
204 GL_STENCIL_INDEX, GL_UNSIGNED_INT, &stencil);
206 /* test results */
207 if (stencil != (1U << bit)) {
208 passed = false;
209 failStencil(1 << bit, stencil);
213 return passed;
217 enum piglit_result
218 piglit_display(void)
220 bool pass = true;
222 pass = test_color_masking(GL_FRONT) && pass;
224 pass = test_color_masking(GL_BACK) && pass;
226 pass = test_depth_masking() && pass;
228 pass = test_stencil_masking() && pass;
230 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
234 void
235 piglit_init(int argc, char **argv)
237 /* nothing */