Add more structure constructor tests.
[piglit/hramrach.git] / tests / texturing / levelclamp.c
blob4d6d6b3b957b72c4b7b0ae40883f3cc28114ec15
1 /*
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
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 /**
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"
35 #define MAX_SIZE 32
36 #define MAX_LEVEL 5
37 #define PAD 5
39 int piglit_window_mode = GLUT_DOUBLE | GLUT_RGB;
40 int piglit_width = 500;
41 int piglit_height = 600;
43 static GLfloat colors[][3] = {
44 {1.0, 0.0, 0.0},
45 {0.0, 1.0, 0.0},
46 {0.0, 0.0, 1.0},
47 {1.0, 1.0, 0.0},
48 {1.0, 0.0, 1.0},
49 {0.0, 1.0, 1.0},
52 static void
53 set_level_color(int level, int size, int color)
55 GLfloat *tex;
56 int x, y;
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,
69 size, size, 0,
70 GL_RGB, GL_FLOAT, tex);
72 free(tex);
75 /**
76 * Tests that the mipmap drawn at (x,y)-(x+size,y+size) has the color for the
77 * clamped level.
79 static GLboolean
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;
85 int clamped_level;
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;
93 if (size == 1) {
94 pass = pass && piglit_probe_pixel_rgb(x1, y1, colors[clamped_level]);
95 } else {
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]);
102 if (!pass) {
103 printf("failed at level %d (%dx%d) with level clamped to "
104 "(%d,%d)\n",
105 level, size, size, min_level, max_level);
108 return pass;
111 static GLboolean
112 draw_and_test(int x_offset, int y_offset, float min_level, float max_level)
114 GLfloat y;
115 int dim;
116 int 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);
122 y = y_offset;
123 for (level = 0, dim = MAX_SIZE; dim > 0; level++, dim /= 2) {
124 piglit_draw_rect_tex(x_offset, y, dim, dim,
125 0.0, 0.0, 1.0, 1.0);
127 y += dim + PAD;
130 y = y_offset;
131 for (level = 0, dim = MAX_SIZE; dim > 0; level++, dim /= 2) {
132 pass = pass && test_results(x_offset, y,
133 dim, level,
134 min_level, max_level) && pass;
136 y += dim + PAD;
139 return pass;
143 enum piglit_result
144 piglit_display(void)
146 int dim;
147 GLboolean pass = GL_TRUE;
148 int level, min_level, max_level, x_offset, y_offset;
149 GLuint tex;
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);
174 y_offset = 10;
175 for (min_level = 0; min_level <= MAX_LEVEL; min_level++) {
176 x_offset = 10;
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);
189 glutSwapBuffers();
191 return pass ? PIGLIT_SUCCESS : PIGLIT_FAILURE;
194 void
195 piglit_init(int argc, char **argv)