Add more structure constructor tests.
[piglit/hramrach.git] / tests / fbo / fbo-readpixels.c
blob988f3dd6cf2a74a489c0a6a998de26103615e0f4
1 /*
2 * Copyright © 2009 Intel Corporation
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 DEALINGS
21 * IN THE SOFTWARE.
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
28 /** @file fbo-readpixels.c
30 * Tests that various formats of color renderbuffer get correct results from
31 * glReadPixels() versus glClear and immediate mode rendering.
34 #include "piglit-util.h"
36 #define BUF_WIDTH 32
37 #define BUF_HEIGHT 32
38 int piglit_width = 100;
39 int piglit_height = 200;
40 int piglit_window_mode = GLUT_DOUBLE | GLUT_RGB;
42 static GLboolean
43 test_with_format(GLenum internal_format, GLenum format,
44 float results_x, float results_y)
46 GLuint tex, fb;
47 GLenum status;
48 GLboolean pass = GL_TRUE;
49 int subrect_w = BUF_WIDTH / 5;
50 int subrect_h = BUF_HEIGHT / 5;
51 int x, y;
52 int rbits, gbits, bbits, abits;
54 glGenTextures(1, &tex);
55 glBindTexture(GL_TEXTURE_2D, tex);
56 glTexImage2D(GL_TEXTURE_2D, 0, internal_format,
57 BUF_WIDTH, BUF_HEIGHT, 0,
58 format, GL_UNSIGNED_BYTE, NULL);
60 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
63 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_RED_SIZE,
64 &rbits);
65 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_SIZE,
66 &gbits);
67 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_SIZE,
68 &bbits);
69 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_SIZE,
70 &abits);
72 printf("testing with format 0x%04x, 0x%04x "
73 "(%d,%d,%d,%d rgba)\n",
74 internal_format, format,
75 rbits, gbits, bbits, abits);
77 glGenFramebuffersEXT(1, &fb);
78 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
79 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
80 GL_COLOR_ATTACHMENT0_EXT,
81 GL_TEXTURE_2D,
82 tex,
83 0);
84 assert(glGetError() == 0);
86 status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
87 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
88 fprintf(stderr, "texture for internalformat 0x%04x. "
89 "format 0x%04x is framebuffer "
90 "incomplete (status = 0x%04x)\n",
91 internal_format, format, status);
92 goto done;
95 /* Set matrices */
96 glViewport(0, 0, BUF_WIDTH, BUF_HEIGHT);
97 piglit_ortho_projection(BUF_WIDTH, BUF_HEIGHT, GL_FALSE);
99 /* clear background to purple */
100 glClearColor(1.0, 0.0, 1.0, 0.0);
101 glClear(GL_COLOR_BUFFER_BIT);
103 /* lower-left square: red */
104 glColor4f(1.0, 0.0, 0.0, 0.0);
105 piglit_draw_rect(subrect_w * 1, subrect_h * 1,
106 subrect_w, subrect_h);
108 /* lower-right square: green */
109 glColor4f(0.0, 1.0, 0.0, 0.0);
110 piglit_draw_rect(subrect_w * 3, subrect_h * 1,
111 subrect_w, subrect_h);
113 /* upper-left square: blue */
114 glColor4f(0.0, 0.0, 1.0, 0.0);
115 piglit_draw_rect(subrect_w * 1, subrect_h * 3,
116 subrect_w, subrect_h);
118 /* upper-right square: black */
119 glColor4f(0.0, 0.0, 0.0, 1.0);
120 piglit_draw_rect(subrect_w * 3, subrect_h * 3,
121 subrect_w, subrect_h);
123 for (y = 0; y < BUF_HEIGHT; y++) {
124 for (x = 0; x < BUF_WIDTH; x++) {
125 float expected[4];
127 if (x >= subrect_w * 1 && x < subrect_w * 2 &&
128 y >= subrect_h * 1 && y < subrect_h * 2) {
129 expected[0] = 1.0;
130 expected[1] = 0.0;
131 expected[2] = 0.0;
132 expected[3] = 0.0;
133 } else if (x >= subrect_w * 3 && x < subrect_w * 4 &&
134 y >= subrect_h * 1 && y < subrect_h * 2) {
135 expected[0] = 0.0;
136 expected[1] = 1.0;
137 expected[2] = 0.0;
138 expected[3] = 0.0;
139 } else if (x >= subrect_w * 1 && x < subrect_w * 2 &&
140 y >= subrect_h * 3 && y < subrect_h * 4) {
141 expected[0] = 0.0;
142 expected[1] = 0.0;
143 expected[2] = 1.0;
144 expected[3] = 0.0;
145 } else if (x >= subrect_w * 3 && x < subrect_w * 4 &&
146 y >= subrect_h * 3 && y < subrect_h * 4) {
147 expected[0] = 0.0;
148 expected[1] = 0.0;
149 expected[2] = 0.0;
150 expected[3] = 1.0;
151 } else {
152 expected[0] = 1.0;
153 expected[1] = 0.0;
154 expected[2] = 1.0;
155 expected[3] = 0.0;
157 pass &= piglit_probe_pixel_rgb(x, y, expected);
161 /* display the texture by drawing a quad */
162 glViewport(0, 0, piglit_width, piglit_height);
163 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
165 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
166 glEnable(GL_TEXTURE_2D);
167 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
169 piglit_draw_rect_tex(results_x, results_y, BUF_WIDTH, BUF_HEIGHT,
170 0, 0, 1, 1);
171 glDisable(GL_TEXTURE_2D);
173 done:
174 glDeleteFramebuffersEXT(1, &fb);
175 glDeleteTextures(1, &tex);
176 return pass;
179 enum piglit_result
180 piglit_display(void)
182 GLboolean pass = GL_TRUE;
184 glClearColor(1.0, 1.0, 1.0, 1.0);
185 glClear(GL_COLOR_BUFFER_BIT);
187 pass &= test_with_format(GL_RGBA8, GL_BGRA,
188 0, 0);
189 pass &= test_with_format(GL_RGB5, GL_RGB,
190 0, BUF_HEIGHT + 1);
191 pass &= test_with_format(GL_RGBA4, GL_BGRA,
192 0, (BUF_HEIGHT + 1) * 2);
193 pass &= test_with_format(GL_RGB5_A1, GL_BGRA,
194 0, (BUF_HEIGHT + 1) * 3);
195 glutSwapBuffers();
197 return pass ? PIGLIT_SUCCESS : PIGLIT_FAILURE;
200 void piglit_init(int argc, char **argv)
202 piglit_require_extension("GL_EXT_framebuffer_object");