fbo-mrt-alphatest: Actually require MRTs to be available.
[piglit.git] / tests / general / readpix-z.c
blob08e4a381d8b3e7945bce70175dded9abfd3ec3e6
1 /*
2 * Copyright 2012 VMware, Inc.
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * 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, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 /**
25 * Test glReadPixels(GL_DEPTH_COMPONENT)
26 * Brian Paul
27 * June 2012
30 #include "piglit-util-gl.h"
32 PIGLIT_GL_TEST_CONFIG_BEGIN
34 config.supports_gl_compat_version = 10;
36 config.window_width = 200;
37 config.window_height = 200;
38 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_DEPTH;
39 config.khr_no_error_support = PIGLIT_NO_ERRORS;
41 PIGLIT_GL_TEST_CONFIG_END
45 /** Display contents of the depth buffer as grayscale color */
46 static void
47 display_depth(const GLfloat *buf)
49 glWindowPos2i(0, 0);
50 glDrawPixels(piglit_width, piglit_height,
51 GL_LUMINANCE, GL_FLOAT, buf);
55 /** Test glClear(GL_DEPTH_BUFFER_BIT) + glReadPixels */
56 static bool
57 test_z_clear(void)
59 GLfloat *buf = malloc(piglit_width * piglit_height * sizeof(GLfloat));
60 float z;
61 double diff, tolerance;
62 GLint zBits, i;
64 glGetIntegerv(GL_DEPTH_BITS, &zBits);
66 /* allow 1-bit error */
67 tolerance = 1.0 / (1 << (zBits - 1));
69 for (z = 0.0f; z <= 1.0f; z += 0.125) {
70 glClearDepth(z);
71 glClear(GL_DEPTH_BUFFER_BIT);
73 glReadPixels(0, 0, piglit_width, piglit_height,
74 GL_DEPTH_COMPONENT, GL_FLOAT, buf);
76 if (!piglit_automatic) {
77 display_depth(buf);
78 piglit_present_results();
81 /* Make sure all the values are the same */
82 for (i = 1; i < piglit_width * piglit_height; i++) {
83 if (buf[i] != buf[0]) {
84 printf("depth[%d]=%f != depth[0]=%f\n",
85 i, buf[i], buf[0]);
86 free(buf);
87 return false;
91 /* Check that the depth value read back is within tolerance */
92 diff = buf[0] - z;
93 if (diff > tolerance) {
94 printf("Depth buffer clear failed!\n");
95 printf("Expected %f, found %f\n", z, buf[0]);
96 free(buf);
97 return false;
101 free(buf);
102 return true;
106 static void
107 draw_z_gradient(GLfloat zLeft, GLfloat zRight)
109 GLfloat verts[4][3];
111 verts[0][0] = -1.0; verts[0][1] = -1.0; verts[0][2] = zLeft;
112 verts[1][0] = 1.0; verts[1][1] = -1.0; verts[1][2] = zRight;
113 verts[2][0] = 1.0; verts[2][1] = 1.0; verts[2][2] = zRight;
114 verts[3][0] = -1.0; verts[3][1] = 1.0; verts[3][2] = zLeft;
116 glEnableClientState(GL_VERTEX_ARRAY);
117 glVertexPointer(3, GL_FLOAT, 0, verts);
118 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
122 static bool
123 test_z_gradient(void)
125 const GLfloat epsilon = 2.0 / piglit_width;
126 GLfloat *buf, *row;
127 bool pass = true;
128 int pos, i;
130 /* draw full-window quad with z increasing left to right */
131 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
132 glEnable(GL_DEPTH_TEST);
133 draw_z_gradient(-1.0, 1.0);
134 glDisable(GL_DEPTH_TEST);
136 buf = (GLfloat *) malloc(piglit_width * piglit_height * sizeof(GLfloat));
138 /* read whole buffer */
139 glReadPixels(0, 0, piglit_width, piglit_height,
140 GL_DEPTH_COMPONENT, GL_FLOAT, buf);
142 /* examine a horizontal row at mid-Y */
143 row = buf + piglit_width * piglit_height / 2;
145 pos = 0;
146 if (fabs(row[pos] - 0.0) > epsilon) {
147 printf("Left-most Z value should be close to 0.0, found %f\n",
148 row[pos]);
149 pass = false;
152 pos = piglit_width / 2;
153 if (fabs(row[pos] - 0.5) > epsilon) {
154 printf("Middle Z value should be close to 0.5, found %f\n",
155 row[pos]);
156 pass = false;
159 pos = piglit_width - 1;
160 if (fabs(row[pos] - 1.0) > epsilon) {
161 printf("Left-most Z value should be close to 1.0, found %f\n",
162 row[pos]);
163 pass = false;
166 /* check for monotonicity */
167 for (i = 1; i < piglit_width; i++) {
168 if (row[i - 1] > row[i]) {
169 printf("Z values aren't increasing from left to right. row[%d]=%f > row[%d]=%f\n",
170 i-1, row[i-1], i, row[i]);
171 pass = false;
172 break;
176 if (!piglit_automatic) {
177 display_depth(buf);
178 piglit_present_results();
181 free(buf);
183 return pass;
187 enum piglit_result
188 piglit_display(void)
190 if (!test_z_clear())
191 return PIGLIT_FAIL;
193 if (!test_z_gradient())
194 return PIGLIT_FAIL;
196 return PIGLIT_PASS;
200 void
201 piglit_init(int argc, char **argv)