2 * Copyright © 2008-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
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
24 * Eric Anholt <eric@anholt.net>
29 * Tests that all the appropriate values of GL_TEXTURE_BASE_LEVEL and
30 * GL_TEXTURE_MAX_LEVEL work on a mipmapped 2D texture.
33 #include "piglit-util.h"
39 int piglit_window_mode
= GLUT_DOUBLE
| GLUT_RGB
;
40 int piglit_width
= 500;
41 int piglit_height
= 600;
43 static GLfloat colors
[][3] = {
53 set_level_color(int level
, int size
, int color
)
58 tex
= malloc(size
* size
* 3 * sizeof(GLfloat
));
60 for (y
= 0; y
< size
; y
++) {
61 for (x
= 0; x
< size
; x
++) {
62 tex
[(y
* size
+ x
) * 3 + 0] = colors
[color
][0];
63 tex
[(y
* size
+ x
) * 3 + 1] = colors
[color
][1];
64 tex
[(y
* size
+ x
) * 3 + 2] = colors
[color
][2];
68 glTexImage2D(GL_TEXTURE_2D
, level
, GL_RGB
,
70 GL_RGB
, GL_FLOAT
, tex
);
76 * Tests that the mipmap drawn at (x,y)-(x+size,y+size) has the color for the
80 test_results(int x
, int y
, int size
, int level
, int min_level
, int max_level
)
82 GLboolean pass
= GL_TRUE
;
83 int x1
= x
+ size
/ 4, x2
= x
+ size
* 3 / 4;
84 int y1
= y
+ size
/ 4, y2
= y
+ size
* 3 / 4;
87 clamped_level
= level
;
88 if (clamped_level
> max_level
)
89 clamped_level
= max_level
;
90 if (clamped_level
< min_level
)
91 clamped_level
= min_level
;
94 pass
= pass
&& piglit_probe_pixel_rgb(x1
, y1
, colors
[clamped_level
]);
96 pass
= pass
&& piglit_probe_pixel_rgb(x1
, y1
, colors
[clamped_level
]);
97 pass
= pass
&& piglit_probe_pixel_rgb(x2
, y1
, colors
[clamped_level
]);
98 pass
= pass
&& piglit_probe_pixel_rgb(x2
, y2
, colors
[clamped_level
]);
99 pass
= pass
&& piglit_probe_pixel_rgb(x1
, y2
, colors
[clamped_level
]);
103 printf("failed at level %d (%dx%d) with level clamped to "
105 level
, size
, size
, min_level
, max_level
);
112 draw_and_test(int x_offset
, int y_offset
, float min_level
, float max_level
)
117 GLboolean pass
= GL_TRUE
;
119 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MAX_LEVEL
, max_level
);
120 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_BASE_LEVEL
, min_level
);
123 for (level
= 0, dim
= MAX_SIZE
; dim
> 0; level
++, dim
/= 2) {
124 piglit_draw_rect_tex(x_offset
, y
, dim
, dim
,
131 for (level
= 0, dim
= MAX_SIZE
; dim
> 0; level
++, dim
/= 2) {
132 pass
= pass
&& test_results(x_offset
, y
,
134 min_level
, max_level
) && pass
;
147 GLboolean pass
= GL_TRUE
;
148 int level
, min_level
, max_level
, x_offset
, y_offset
;
151 piglit_ortho_projection(piglit_width
, piglit_height
, GL_FALSE
);
153 /* Clear background to gray */
154 glClearColor(0.5, 0.5, 0.5, 1.0);
155 glClear(GL_COLOR_BUFFER_BIT
);
157 /* Create the texture. */
158 glGenTextures(1, &tex
);
159 glBindTexture(GL_TEXTURE_2D
, tex
);
161 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
162 GL_NEAREST_MIPMAP_NEAREST
);
163 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
164 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
165 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
167 /* Fill in each level */
168 for (level
= 0, dim
= MAX_SIZE
; dim
> 0; level
++, dim
/= 2) {
169 set_level_color(level
, dim
, level
);
172 /* Draw all the levels with varying clamp ranges. */
173 glEnable(GL_TEXTURE_2D
);
175 for (min_level
= 0; min_level
<= MAX_LEVEL
; min_level
++) {
178 for (max_level
= MAX_LEVEL
; max_level
>= min_level
; max_level
--) {
179 pass
= draw_and_test(x_offset
, y_offset
,
180 min_level
, max_level
) && pass
;
181 x_offset
+= MAX_SIZE
+ PAD
;
184 y_offset
+= (MAX_SIZE
* 2 + PAD
* 7);
187 glDeleteTextures(1, &tex
);
191 return pass
? PIGLIT_SUCCESS
: PIGLIT_FAILURE
;
195 piglit_init(int argc
, char **argv
)