cmake: respect indentation
[piglit.git] / tests / texturing / mipmap-setup.c
blobbb5ba9bb15b5ac0dca2404d441648b982c94a49e
1 /*
2 * Copyright 2010 VMware, Inc.
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
21 * DEALINGS IN THE SOFTWARE.
25 * Test building a mipmap in various orders:
26 * From largest to smallest
27 * From smallest to largest
28 * Random order.
30 * Author: Brian Paul
34 #include "piglit-util-gl.h"
36 PIGLIT_GL_TEST_CONFIG_BEGIN
38 config.supports_gl_compat_version = 10;
40 config.window_width = 200;
41 config.window_height = 200;
42 config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGB;
43 config.khr_no_error_support = PIGLIT_NO_ERRORS;
45 PIGLIT_GL_TEST_CONFIG_END
47 #define MAX_LEVEL 7
49 enum order {
50 NORMAL,
51 REVERSE,
52 RANDOM
56 static GLubyte
57 level_intensity(GLint level)
59 return 100 + level * 20;
63 static void
64 setup_tex_image(GLint level)
66 GLint size = 1 << (MAX_LEVEL - level);
67 GLubyte *img = malloc(size * size * 4);
68 GLubyte val = level_intensity(level);
69 memset(img, val, size * size * 4);
70 glTexImage2D(GL_TEXTURE_2D, level, GL_RGB, size, size, 0,
71 GL_RGBA, GL_UNSIGNED_BYTE, img);
72 free(img);
77 * Generate a mipmapped texture.
78 * Define the mipmap levels/images in the order specified by 'ord'.
80 static void
81 generate_mipmap(enum order ord)
83 GLuint tex;
84 GLint level;
86 glGenTextures(1, &tex);
87 glBindTexture(GL_TEXTURE_2D, tex);
89 /* hint to the driver that there won't be a mipmap (but that's a lie) */
90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
93 /* Set each mipmap level to a different intensity, starting with the
94 * smallest mipmap level and ending with the largest at level=0.
96 if (ord == NORMAL) {
97 for (level = 0; level <= MAX_LEVEL; level++) {
98 setup_tex_image(level);
101 else if (ord == REVERSE) {
102 for (level = MAX_LEVEL; level >= 0; level--) {
103 setup_tex_image(level);
106 else {
107 static const GLuint rand[MAX_LEVEL + 1] = {3, 1, 2, 4, 7, 0, 6, 5};
108 assert(ord == RANDOM);
110 for (level = 0; level <= MAX_LEVEL; level++) {
111 setup_tex_image(rand[level]);
115 /* use mipmap filtering */
116 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
117 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
121 static GLboolean
122 test(enum order ord, const char *order)
124 const int px = piglit_width / 2, py = piglit_height / 2;
125 GLboolean pass = GL_TRUE;
126 GLuint level;
128 generate_mipmap(ord);
130 glEnable(GL_TEXTURE_2D);
132 /* render a test polygon for each mipmap level */
133 for (level = 0; level <= MAX_LEVEL; level++) {
134 float expected[4];
135 int p;
137 expected[0] = expected[1] = expected[2] = level_intensity(level) / 255.0;
139 /* force sampling from a specific mipmap level */
140 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, level);
141 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, level);
143 glClear(GL_COLOR_BUFFER_BIT);
145 glBegin(GL_POLYGON);
146 glTexCoord2f(0, 0);
147 glVertex2f(-1, -1);
148 glTexCoord2f(1, 0);
149 glVertex2f(1, -1);
150 glTexCoord2f(1, 1);
151 glVertex2f(1, 1);
152 glTexCoord2f(0, 1);
153 glVertex2f(-1, 1);
154 glEnd();
156 p = piglit_probe_pixel_rgb(px, py, expected);
157 if (!p) {
158 printf(" At mipmap level %d, order = %s\n", level, order);
161 pass &= p;
163 piglit_present_results();
166 return pass;
170 void
171 piglit_init(int argc, char **argv)
173 /* nothing */
177 enum piglit_result
178 piglit_display(void)
180 if (!test(NORMAL, "Normal"))
181 return PIGLIT_FAIL;
183 if (!test(REVERSE, "Reverse"))
184 return PIGLIT_FAIL;
186 if (!test(RANDOM, "Random"))
187 return PIGLIT_FAIL;
189 return PIGLIT_PASS;