cl: Don't use device_infos if num_device_infos == 0
[piglit.git] / tests / general / drawpix-z.c
blob597489b5a845cbd693225403020d36a9ad7325a4
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 glDrawPixels(GL_DEPTH_COMPONENT)
26 * Brian Paul
27 * June 2012
29 * We don't rely on glReadPixels(GL_DEPTH_COMPONENT) in case it's not
30 * working. Instead we test by drawing an image into the depth buffer
31 * while setting the color buffer to white. Next, we draw quads just
32 * in front and behind where we expect the Z values to be. The quad
33 * behind should be invisible while the quad in front should be totally
34 * visible.
37 #include "piglit-util-gl.h"
39 PIGLIT_GL_TEST_CONFIG_BEGIN
41 config.supports_gl_compat_version = 10;
43 config.window_width = 200;
44 config.window_height = 200;
45 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_DEPTH;
46 config.khr_no_error_support = PIGLIT_NO_ERRORS;
48 PIGLIT_GL_TEST_CONFIG_END
50 static void
51 draw_z_gradient(GLfloat zLeft, GLfloat zRight)
53 GLfloat verts[4][3];
55 verts[0][0] = -1.0; verts[0][1] = -1.0; verts[0][2] = zLeft;
56 verts[1][0] = 1.0; verts[1][1] = -1.0; verts[1][2] = zRight;
57 verts[2][0] = 1.0; verts[2][1] = 1.0; verts[2][2] = zRight;
58 verts[3][0] = -1.0; verts[3][1] = 1.0; verts[3][2] = zLeft;
60 glEnableClientState(GL_VERTEX_ARRAY);
61 glVertexPointer(3, GL_FLOAT, 0, verts);
62 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
66 enum piglit_result
67 piglit_display(void)
69 /* a tight epsilon isn't important for this test */
70 const GLfloat epsilon = 4.0 / piglit_width;
71 static const GLfloat white[4] = { 1, 1, 1, 1 };
72 static const GLfloat green[4] = { 0, 1, 0, 1 };
73 static const GLfloat red[4] = { 1, 0, 0, 1 };
74 GLfloat *buf;
75 bool pass = true;
76 int i, j;
77 float zLeft, zRight;
79 /* For both glDrawPixels and the polygon rendering below we
80 * use a range of Z values in [0, 1] where 0=near and 1=far.
81 * So object Z coords are the same as normalized depth coords.
84 zLeft = epsilon;
85 zRight = 1.0 - epsilon;
87 /* create image of Z values increasing from left to right */
88 buf = (GLfloat *)
89 malloc(piglit_width * piglit_height * sizeof(GLfloat));
90 for (j = 0; j < piglit_height; j++) {
91 for (i = 0; i < piglit_width; i++) {
92 float z = i / (float) (piglit_width - 1);
93 z = zLeft + z * (zRight - zLeft);
94 buf[j * piglit_width + i] = z;
98 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
100 /* glDrawPixels the Z gradient image */
101 glColor4fv(white);
102 glWindowPos2i(0, 0);
103 glDrawPixels(piglit_width, piglit_height,
104 GL_DEPTH_COMPONENT, GL_FLOAT, buf);
106 free(buf);
108 /* draw a red quad behind the Z gradient - it should not be visible */
109 glColor4fv(red);
110 draw_z_gradient(zLeft + epsilon, zRight + epsilon);
111 if (!piglit_probe_rect_rgb(0, 0, piglit_width, piglit_height, white)) {
112 printf("Quad behind test failed\n");
113 pass = false;
116 /* draw green quad in front of the Z gradient - it should be visible */
117 glColor4fv(green);
118 draw_z_gradient(zLeft - epsilon, zRight - epsilon);
119 if (!piglit_probe_rect_rgb(0, 0, piglit_width, piglit_height, green)) {
120 printf("Quad in front test failed\n");
121 pass = false;
124 piglit_present_results();
126 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
130 void
131 piglit_init(int argc, char **argv)
133 /* Setup projection matrix such that zObj=0 becomes zBuffer=0
134 * and zObj=1 becomes zBuffer=1 (identity transform).
135 * So, glOrtho maps zObj=0 to zNDC=-1 and maps zObj=1 to zNDC=1.
136 * Then, zNDC=-1 maps to zBuffer=0 and zNDC=1 maps to zBuffer=1.
138 glMatrixMode(GL_PROJECTION);
139 glLoadIdentity();
140 glOrtho(-1.0, 1.0, -1.0, 1.0, 0.0, -1.0);
142 glEnable(GL_DEPTH_TEST);