Add more structure constructor tests.
[piglit/hramrach.git] / tests / fbo / fbo-generatemipmap-npot.c
blob15ae1d8bf0331f5bc1d263f5a97df33f1160d182
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-generatemipmap.c
30 * Tests that glGenerateMipmapEXT works correctly on a 2D texture.
33 #include "piglit-util.h"
35 #define TEX_WIDTH 254
36 #define TEX_HEIGHT 254
37 int piglit_width = 700;
38 int piglit_height = 300;
39 int piglit_window_mode = GLUT_DOUBLE | GLUT_RGB;
41 static const float red[] = {1, 0, 0, 0};
42 static const float green[] = {0, 1, 0, 0};
43 static const float blue[] = {0, 0, 1, 0};
44 static const float white[] = {1, 1, 1, 1};
46 static int
47 create_fbo(void)
49 GLuint tex, fb;
50 GLenum status;
51 int i, dim;
53 glGenTextures(1, &tex);
54 glBindTexture(GL_TEXTURE_2D, tex);
56 for (i = 0, dim = TEX_WIDTH; dim >0; i++, dim /= 2) {
57 glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA,
58 dim, dim,
60 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
62 assert(glGetError() == 0);
64 glGenFramebuffersEXT(1, &fb);
65 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
66 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
67 GL_COLOR_ATTACHMENT0_EXT,
68 GL_TEXTURE_2D,
69 tex,
70 0);
71 assert(glGetError() == 0);
73 status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
74 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
75 fprintf(stderr, "FBO incomplete\n");
76 goto done;
79 glViewport(0, 0, TEX_WIDTH, TEX_HEIGHT);
80 piglit_ortho_projection(TEX_WIDTH, TEX_HEIGHT, GL_FALSE);
82 glColor4fv(red);
83 piglit_draw_rect(0, 0, TEX_WIDTH / 2, TEX_HEIGHT / 2);
84 glColor4fv(green);
85 piglit_draw_rect(TEX_WIDTH / 2, 0, TEX_WIDTH, TEX_HEIGHT / 2);
86 glColor4fv(blue);
87 piglit_draw_rect(0, TEX_HEIGHT / 2, TEX_WIDTH/2, TEX_HEIGHT);
88 glColor4fv(white);
89 piglit_draw_rect(TEX_WIDTH / 2, TEX_HEIGHT / 2, TEX_WIDTH, TEX_HEIGHT);
91 glGenerateMipmapEXT(GL_TEXTURE_2D);
92 done:
93 glDeleteFramebuffersEXT(1, &fb);
95 return tex;
98 static void
99 draw_mipmap(int x, int y, int dim)
101 glViewport(0, 0, piglit_width, piglit_height);
102 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
104 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
106 glEnable(GL_TEXTURE_2D);
107 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
108 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
109 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
111 piglit_draw_rect_tex(x, y, dim, dim,
112 0, 0, 1, 1);
115 static GLboolean
116 test_mipmap_drawing(int start_x, int start_y, int dim)
118 GLboolean pass = GL_TRUE;
119 int x1 = start_x + dim / 4;
120 int x2 = start_x + dim * 3 / 4;
121 int y1 = start_y + dim / 4;
122 int y2 = start_y + dim * 3 / 4;
124 pass &= piglit_probe_pixel_rgb(x1, y1, red);
125 pass &= piglit_probe_pixel_rgb(x2, y1, green);
126 pass &= piglit_probe_pixel_rgb(x1, y2, blue);
127 pass &= piglit_probe_pixel_rgb(x2, y2, white);
129 return pass;
132 enum piglit_result
133 piglit_display(void)
135 GLboolean pass = GL_TRUE;
136 int dim;
137 GLuint tex;
138 int x;
140 glClearColor(0.5, 0.5, 0.5, 0.5);
141 glClear(GL_COLOR_BUFFER_BIT);
143 tex = create_fbo();
145 x = 1;
146 for (dim = TEX_WIDTH; dim > 1; dim /= 2) {
147 draw_mipmap(x, 1, dim);
148 x += dim + 1;
151 x = 1;
152 for (dim = TEX_WIDTH; dim > 8; dim /= 2) {
153 pass &= test_mipmap_drawing(x, 1, dim);
154 x += dim + 1;
157 glDeleteTextures(1, &tex);
159 glutSwapBuffers();
161 return pass ? PIGLIT_SUCCESS : PIGLIT_FAILURE;
164 void piglit_init(int argc, char **argv)
166 piglit_require_extension("GL_EXT_framebuffer_object");
167 piglit_require_extension("GL_ARB_texture_non_power_of_two");