Add more structure constructor tests.
[piglit/hramrach.git] / tests / fbo / fbo-generatemipmap-nonsquare.c
blobba389d8ba1f673af9a8fbf3c71188b55df8858de
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.
32 * Modified by Marek Olšák <maraeo@gmail.com> to reproduce FDO bug #27338.
35 #include "piglit-util.h"
37 #define TEX_WIDTH 256
38 #define TEX_HEIGHT 64
39 #define TEX_DIFF (TEX_WIDTH/TEX_HEIGHT)
40 int piglit_width = 700;
41 int piglit_height = 300;
42 int piglit_window_mode = GLUT_DOUBLE | GLUT_RGB;
44 static const float red[] = {1, 0, 0, 0};
45 static const float green[] = {0, 1, 0, 0};
46 static const float blue[] = {0, 0, 1, 0};
47 static const float white[] = {1, 1, 1, 1};
49 static int
50 create_fbo(void)
52 GLuint tex, fb;
53 GLenum status;
54 int i, dim, dimy;
56 glGenTextures(1, &tex);
57 glBindTexture(GL_TEXTURE_2D, tex);
59 for (i = 0, dim = TEX_WIDTH; dim >0; i++, dim /= 2) {
60 dimy = dim/TEX_DIFF;
61 if (dimy == 0) dimy = 1;
63 glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA,
64 dim, dimy,
66 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
68 assert(glGetError() == 0);
70 glGenFramebuffersEXT(1, &fb);
71 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
72 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
73 GL_COLOR_ATTACHMENT0_EXT,
74 GL_TEXTURE_2D,
75 tex,
76 0);
77 assert(glGetError() == 0);
79 status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
80 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
81 fprintf(stderr, "FBO incomplete\n");
82 goto done;
85 glViewport(0, 0, TEX_WIDTH, TEX_HEIGHT);
86 piglit_ortho_projection(TEX_WIDTH, TEX_HEIGHT, GL_FALSE);
88 glColor4fv(red);
89 piglit_draw_rect(0, 0, TEX_WIDTH / 2, TEX_HEIGHT / 2);
90 glColor4fv(green);
91 piglit_draw_rect(TEX_WIDTH / 2, 0, TEX_WIDTH, TEX_HEIGHT / 2);
92 glColor4fv(blue);
93 piglit_draw_rect(0, TEX_HEIGHT / 2, TEX_WIDTH/2, TEX_HEIGHT);
94 glColor4fv(white);
95 piglit_draw_rect(TEX_WIDTH / 2, TEX_HEIGHT / 2, TEX_WIDTH, TEX_HEIGHT);
97 glGenerateMipmapEXT(GL_TEXTURE_2D);
98 done:
99 glDeleteFramebuffersEXT(1, &fb);
101 return tex;
104 static void
105 draw_mipmap(int x, int y, int dim)
107 glViewport(0, 0, piglit_width, piglit_height);
108 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
110 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
112 glEnable(GL_TEXTURE_2D);
113 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
114 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
115 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
117 piglit_draw_rect_tex(x, y, dim, dim,
118 0, 0, 1, 1);
121 static GLboolean
122 test_mipmap_drawing(int start_x, int start_y, int dim)
124 GLboolean pass = GL_TRUE;
125 int x, y;
127 for (y = 0; y < dim; y++) {
128 for (x = 0; x < dim; x++) {
129 const float *expected;
131 if (x < dim / 2 && y < dim / 2)
132 expected = red;
133 else if (y < dim / 2)
134 expected = green;
135 else if (x < dim / 2)
136 expected = blue;
137 else
138 expected = white;
140 pass = pass && piglit_probe_pixel_rgb(start_x + x,
141 start_y + y,
142 expected);
146 return pass;
149 enum piglit_result
150 piglit_display(void)
152 GLboolean pass = GL_TRUE;
153 int dim;
154 GLuint tex;
155 int x;
157 glClearColor(0.5, 0.5, 0.5, 0.5);
158 glClear(GL_COLOR_BUFFER_BIT);
160 tex = create_fbo();
162 x = 1;
163 for (dim = TEX_WIDTH; dim > TEX_DIFF; dim /= 2) {
164 draw_mipmap(x, 1, dim);
165 x += dim + 1;
168 x = 1;
169 for (dim = TEX_WIDTH; dim > TEX_DIFF; dim /= 2) {
170 pass &= test_mipmap_drawing(x, 1, dim);
171 x += dim + 1;
174 glDeleteTextures(1, &tex);
176 glutSwapBuffers();
178 return pass ? PIGLIT_SUCCESS : PIGLIT_FAILURE;
181 void piglit_init(int argc, char **argv)
183 piglit_require_extension("GL_EXT_framebuffer_object");