Add more structure constructor tests.
[piglit/hramrach.git] / tests / fbo / fbo-blit.c
blob989566e2597e92f33de328db026ec88fb60fbc67
1 /*
2 * Copyright © 2010 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>
25 * Brian Paul
28 /** @file fbo-blit.c
30 * Tests EXT_framebuffer_blit with various combinations of window system and
31 * FBO objects. Because FBOs are generally stored inverted relative to
32 * window system frambuffers, this could catch flipping failures in blit paths.
34 * See also fbo-readdrawpix.c and fbo-copypix.c
37 #include "piglit-util.h"
39 int piglit_width = 150;
40 int piglit_height = 150;
41 int piglit_window_mode = GLUT_RGB | GLUT_DOUBLE;
42 #define PAD 10
43 #define SIZE 20
45 /* size of texture/renderbuffer (power of two) */
46 #define FBO_SIZE 64
49 static GLuint
50 make_fbo(int w, int h)
52 GLuint tex;
53 GLuint fb;
54 GLenum status;
56 glGenFramebuffersEXT(1, &fb);
57 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
59 glGenTextures(1, &tex);
60 glBindTexture(GL_TEXTURE_2D, tex);
61 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
62 w, h, 0,
63 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
65 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
66 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
68 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
69 GL_COLOR_ATTACHMENT0_EXT,
70 GL_TEXTURE_2D,
71 tex,
72 0);
73 assert(glGetError() == 0);
75 status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
76 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
77 fprintf(stderr, "fbo incomplete (status = 0x%04x)\n", status);
78 piglit_report_result(PIGLIT_SKIP);
81 return fb;
84 static void
85 draw_color_rect(int x, int y, int w, int h)
87 int x1 = x;
88 int x2 = x + w / 2;
89 int y1 = y;
90 int y2 = y + h / 2;
92 glColor4f(1.0, 0.0, 0.0, 0.0);
93 piglit_draw_rect(x1, y1, w / 2, h / 2);
94 glColor4f(0.0, 1.0, 0.0, 0.0);
95 piglit_draw_rect(x2, y1, w / 2, h / 2);
96 glColor4f(0.0, 0.0, 1.0, 0.0);
97 piglit_draw_rect(x1, y2, w / 2, h / 2);
98 glColor4f(1.0, 1.0, 1.0, 0.0);
99 piglit_draw_rect(x2, y2, w / 2, h / 2);
102 static GLboolean
103 verify_color_rect(int start_x, int start_y, int w, int h)
105 float red[] = {1, 0, 0, 0};
106 float green[] = {0, 1, 0, 0};
107 float blue[] = {0, 0, 1, 0};
108 float white[] = {1, 1, 1, 0};
109 int x, y;
111 for (y = 0; y < h; y++) {
112 for (x = 0; x < w; x++) {
113 float *expected;
115 if ((y < h / 2) && (x < w / 2))
116 expected = red;
117 else if (y < h / 2)
118 expected = green;
119 else if (x < w / 2)
120 expected = blue;
121 else
122 expected = white;
124 if (!piglit_probe_pixel_rgb(start_x + x, start_y + y,
125 expected))
126 return GL_FALSE;
130 return GL_TRUE;
134 static void
135 copy(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
136 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1)
138 glBlitFramebufferEXT(srcX0, srcY0, srcX1, srcY1,
139 dstX0, dstY0, dstX1, dstY1,
140 GL_COLOR_BUFFER_BIT, GL_NEAREST);
144 static GLboolean
145 run_test(void)
147 GLboolean pass = GL_TRUE;
148 GLuint fbo;
149 int fbo_width = FBO_SIZE;
150 int fbo_height = FBO_SIZE;
151 int x0 = PAD;
152 int y0 = PAD;
153 int y1 = PAD * 2 + SIZE;
154 int y2 = PAD * 3 + SIZE * 2;
156 glViewport(0, 0, piglit_width, piglit_height);
157 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
159 glClearColor(0.5, 0.5, 0.5, 0.5);
160 glClear(GL_COLOR_BUFFER_BIT);
162 /* Draw the color rect in the window system window */
163 draw_color_rect(x0, y0, SIZE, SIZE);
165 fbo = make_fbo(fbo_width, fbo_height);
167 glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, fbo);
168 glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
169 glViewport(0, 0, fbo_width, fbo_height);
170 piglit_ortho_projection(fbo_width, fbo_height, GL_FALSE);
171 glClearColor(1.0, 0.0, 1.0, 0.0);
172 glClear(GL_COLOR_BUFFER_BIT);
174 /* Draw the color rect in the FBO */
175 draw_color_rect(x0, y0, SIZE, SIZE);
177 /* Now that we have correct samples, blit things around.
178 * FBO(bottom) -> WIN(middle)
180 glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
181 glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, fbo);
182 copy(x0, y0, x0 + SIZE, y0 + SIZE,
183 x0, y1, x0 + SIZE, y1 + SIZE);
185 /* WIN(bottom) -> FBO(middle) */
186 glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, fbo);
187 glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
188 copy(x0, y0, x0 + SIZE, y0 + SIZE,
189 x0, y1, x0 + SIZE, y1 + SIZE);
191 /* FBO(middle) -> WIN(top) back to verify WIN -> FBO */
192 glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
193 glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, fbo);
194 copy(x0, y1, x0 + SIZE, y1 + SIZE,
195 x0, y2, x0 + SIZE, y2 + SIZE);
197 glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
198 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
200 pass = verify_color_rect(PAD, y0, SIZE, SIZE) && pass;
201 pass = verify_color_rect(PAD, y1, SIZE, SIZE) && pass;
202 pass = verify_color_rect(PAD, y2, SIZE, SIZE) && pass;
203 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
204 pass = verify_color_rect(PAD, y0, SIZE, SIZE) && pass;
205 pass = verify_color_rect(PAD, y1, SIZE, SIZE) && pass;
206 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
208 glutSwapBuffers();
210 return pass;
214 enum piglit_result
215 piglit_display(void)
217 GLboolean pass = run_test();
219 return pass ? PIGLIT_SUCCESS : PIGLIT_FAILURE;
223 void
224 piglit_init(int argc, char **argv)
226 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
228 piglit_require_extension("GL_EXT_framebuffer_object");
229 piglit_require_extension("GL_EXT_framebuffer_blit");