arb_copy_image: test copying of different mipmap levels of a texture
[piglit.git] / tests / fbo / fbo-maxsize.c
blob698d91421a1b67a8fbc428e50e89d31fa25a70ea
1 /*
2 * Copyright © 2010 Marek Olšák <maraeo@gmail.com>
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.
25 /** @file fbo-maxsize.c
27 * Tests that rendering to a texture of maximum size works.
30 #include "piglit-util-gl.h"
32 PIGLIT_GL_TEST_CONFIG_BEGIN
34 config.supports_gl_compat_version = 10;
36 config.window_width = 256;
37 config.window_height = 256;
38 config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGB;
39 config.khr_no_error_support = PIGLIT_NO_ERRORS;
41 PIGLIT_GL_TEST_CONFIG_END
43 static int
44 find_max_texture_size(void)
46 GLint maxsize;
48 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxsize);
50 while (maxsize > 1) {
51 GLint w, h;
52 glTexImage2D(GL_PROXY_TEXTURE_2D, 0, GL_RGBA,
53 maxsize, maxsize, 0,
54 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
55 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0,
56 GL_TEXTURE_WIDTH, &w);
57 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0,
58 GL_TEXTURE_WIDTH, &h);
59 if (w == maxsize && h == maxsize) {
60 return maxsize;
62 else {
63 maxsize /= 2;
66 return maxsize;
70 static void
71 draw_color_sub_rect(int x, int y, int texsize)
73 int k = texsize / 64;
74 int x0 = x - k;
75 int x1 = x + k;
76 int y1 = y - k;
77 int y2 = y + k;
79 float r0 = (float) x0 / texsize;
80 float r1 = (float) x1 / texsize;
82 float g0 = (float) y1 / texsize;
83 float g1 = (float) y2 / texsize;
85 float b0 = (float) y1 / texsize;
86 float b1 = (float) y2 / texsize;
88 glBegin(GL_POLYGON);
89 glColor3f(r0, g0, b0); glVertex2i(x0, y1);
90 glColor3f(r1, g0, b0); glVertex2i(x1, y1);
91 glColor3f(r1, g1, b1); glVertex2i(x1, y2);
92 glColor3f(r0, g1, b1); glVertex2i(x0, y2);
93 glEnd();
97 static int create_fbo(void)
99 GLuint tex, fb;
100 GLint maxsize;
101 GLenum status;
102 int x0, x1, y0, y1;
103 GLenum glerror;
105 maxsize = find_max_texture_size();
106 printf("max 2D texture size: %d x %d\n", maxsize, maxsize);
108 glGenTextures(1, &tex);
109 glBindTexture(GL_TEXTURE_2D, tex);
111 /* Turn off mipmap filtering as a hint to the driver that we don't
112 * need multiple mipmap levels (as those can increase the memory
113 * requirements by 50%, and we don't need them in this test.
115 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
116 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, maxsize, maxsize,
117 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
119 glerror = glGetError();
121 switch (glerror) {
122 case GL_NO_ERROR:
123 break;
124 case GL_OUT_OF_MEMORY:
125 puts("Got GL_OUT_OF_MEMORY.");
126 piglit_report_result(PIGLIT_PASS);
127 default:
128 printf("Unexpected error: %s\n",
129 piglit_get_gl_enum_name(glerror));
130 piglit_report_result(PIGLIT_FAIL);
133 glGenFramebuffersEXT(1, &fb);
134 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
135 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
136 GL_COLOR_ATTACHMENT0_EXT,
137 GL_TEXTURE_2D,
138 tex, 0);
139 if (!piglit_check_gl_error(GL_NO_ERROR))
140 piglit_report_result(PIGLIT_FAIL);
142 status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
143 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
144 printf("FBO incomplete\n");
145 piglit_report_result(PIGLIT_SKIP);
148 glViewport(0, 0, maxsize, maxsize);
149 piglit_ortho_projection(maxsize, maxsize, GL_FALSE);
151 glClearColor(1.0, 1.0, 1.0, 1.0);
152 glClear(GL_COLOR_BUFFER_BIT);
154 x0 = maxsize / 4;
155 x1 = maxsize * 3 / 4;
156 y0 = maxsize / 4;
157 y1 = maxsize * 3 / 4;
158 draw_color_sub_rect(x0, y0, maxsize);
159 draw_color_sub_rect(x1, y0, maxsize);
160 draw_color_sub_rect(x1, y1, maxsize);
161 draw_color_sub_rect(x0, y1, maxsize);
163 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, piglit_winsys_fbo);
164 glDeleteFramebuffersEXT(1, &fb);
166 return tex;
170 /** draw textured rect centered on the given pixel.
172 static void
173 draw_tex_sub_rect(int x, int y)
175 int x0 = x - 16;
176 int x1 = x + 16;
177 int y1 = y - 16;
178 int y2 = y + 16;
180 float s0 = (float) x0 / piglit_width;
181 float s1 = (float) x1 / piglit_width;
182 float t0 = (float) y1 / piglit_height;
183 float t1 = (float) y2 / piglit_height;
185 glBegin(GL_POLYGON);
186 glTexCoord2f(s0, t0); glVertex2i(x0, y1);
187 glTexCoord2f(s1, t0); glVertex2i(x1, y1);
188 glTexCoord2f(s1, t1); glVertex2i(x1, y2);
189 glTexCoord2f(s0, t1); glVertex2i(x0, y2);
190 glEnd();
194 enum piglit_result
195 piglit_display(void)
197 GLboolean pass = GL_TRUE;
198 GLuint tex;
199 int x1 = piglit_width / 4;
200 int x2 = (piglit_width / 4) * 3;
201 int y1 = piglit_height / 4;
202 int y2 = (piglit_height / 4) * 3;
203 int cx = piglit_width / 2;
204 int cy = piglit_height / 2;
205 float c1[3] = {0.25, 0.25, 0.25};
206 float c2[3] = {0.75, 0.25, 0.25};
207 float c3[3] = {0.25, 0.75, 0.75};
208 float c4[3] = {0.75, 0.75, 0.75};
209 float white[3] = {1.0, 1.0, 1.0};
211 glClearColor(0.5, 0.5, 0.5, 0.5);
212 glClear(GL_COLOR_BUFFER_BIT);
214 tex = create_fbo();
215 glViewport(0, 0, piglit_width, piglit_height);
216 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
218 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, piglit_winsys_fbo);
220 glEnable(GL_TEXTURE_2D);
221 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
222 glBindTexture(GL_TEXTURE_2D, tex);
223 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
224 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
226 draw_tex_sub_rect(x1, y1);
227 draw_tex_sub_rect(x2, y1);
228 draw_tex_sub_rect(x1, y2);
229 draw_tex_sub_rect(x2, y2);
230 draw_tex_sub_rect(cx, cy);
232 pass &= piglit_probe_pixel_rgb(x1, y1, c1);
233 pass &= piglit_probe_pixel_rgb(x2, y1, c2);
234 pass &= piglit_probe_pixel_rgb(x1, y2, c3);
235 pass &= piglit_probe_pixel_rgb(x2, y2, c4);
236 pass &= piglit_probe_pixel_rgb(cx, cy, white);
238 glDeleteTextures(1, &tex);
239 glDisable(GL_TEXTURE_2D);
241 piglit_present_results();
243 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
246 void piglit_init(int argc, char **argv)
248 piglit_require_extension("GL_EXT_framebuffer_object");