Fix PowerPC related typo in spantmp2.h
[mesa.git] / progs / tests / mipmap_comp.c
1 /* Copyright (c) Mark J. Kilgard, 1994. */
2 /*
3 * (c) Copyright 1993, Silicon Graphics, Inc.
4 * ALL RIGHTS RESERVED
5 * Permission to use, copy, modify, and distribute this software for
6 * any purpose and without fee is hereby granted, provided that the above
7 * copyright notice appear in all copies and that both the copyright notice
8 * and this permission notice appear in supporting documentation, and that
9 * the name of Silicon Graphics, Inc. not be used in advertising
10 * or publicity pertaining to distribution of the software without specific,
11 * written prior permission.
12 *
13 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
14 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
16 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
17 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
18 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
19 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
20 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
21 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
22 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
23 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
24 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
25 *
26 * US Government Users Restricted Rights
27 * Use, duplication, or disclosure by the Government is subject to
28 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
29 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
30 * clause at DFARS 252.227-7013 and/or in similar or successor
31 * clauses in the FAR or the DOD or NASA FAR Supplement.
32 * Unpublished-- rights reserved under the copyright laws of the
33 * United States. Contractor/manufacturer is Silicon Graphics,
34 * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
35 *
36 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
37 */
38
39 /* mipmap_comp
40 * Test compressed texture mipmaps
41 *
42 * Based on mipmap_limits
43 */
44
45 #include <string.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <GL/glew.h>
49 #include <GL/glut.h>
50
51 #include "readtex.h"
52
53 #define SIZE 16 /* not larger then 16 */
54
55 static GLint BaseLevel = 0, MaxLevel = 9;
56 static GLfloat MinLod = -1, MaxLod = 9;
57 static GLfloat LodBias = 0.0;
58 static GLboolean NearestFilter = GL_TRUE;
59 static GLuint texImage;
60
61
62 static void
63 initValues(void)
64 {
65 BaseLevel = 0;
66 MaxLevel = 9;
67 MinLod = -1;
68 MaxLod = 2;
69 LodBias = 5.0;
70 NearestFilter = GL_TRUE;
71 }
72
73
74 static void
75 makeImage(int level, int width, int height)
76 {
77 #if 0
78 GLubyte img[SIZE*SIZE*3];
79 int i, j;
80
81 (void)size;
82 for (i = 0; i < height; i++) {
83 for (j = 0; j < width; j++) {
84 int k = (i * width + j) * 3;
85 img[k + 0] = 255 * ((level + 1) % 2);
86 img[k + 1] = 255 * ((level + 1) % 2);
87 img[k + 2] = 255 * ((level + 1) % 2);
88 }
89 }
90
91 glTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, width, height, 0,
92 GL_RGB, GL_UNSIGNED_BYTE, img);
93 #else
94 GLubyte img[128];
95 GLint size[] = {
96 128, /* 16x16 */
97 32, /* 8x8 */
98 8, /* 4x4 */
99 8, /* 2x2 */
100 8, /* 1x1 */
101 };
102 int i;
103 int value = ((level + 1) % 2) * 0xffffffff;
104 memset(img, 0, 128);
105
106 /* generate black and white mipmap levels */
107 if (value)
108 for (i = 0; i < size[level] / 4; i += 2)
109 ((int*)img)[i] = value;
110
111 glCompressedTexImage2D(GL_TEXTURE_2D, level,
112 GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
113 width, height, 0,
114 size[level], img);
115 #endif
116 }
117
118
119 static void
120 makeImages(void)
121 {
122 int i, sz;
123
124 for (i = 0, sz = SIZE; sz >= 1; i++, sz /= 2) {
125 makeImage(i, sz, sz);
126 printf("Level %d size: %d x %d\n", i, sz, sz);
127 }
128 }
129
130
131 static void
132 myInit(void)
133 {
134
135 initValues();
136
137 glEnable(GL_DEPTH_TEST);
138 glDepthFunc(GL_LESS);
139 glShadeModel(GL_FLAT);
140
141 glTranslatef(0.0, 0.0, -3.6);
142
143 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
144 glGenTextures(1, &texImage);
145 glBindTexture(GL_TEXTURE_2D, texImage);
146 makeImages();
147
148 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
149 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
150 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
151 glEnable(GL_TEXTURE_2D);
152 }
153
154
155 static void
156 display(void)
157 {
158 GLfloat tcm = 1.0;
159 glBindTexture(GL_TEXTURE_2D, texImage);
160
161 printf("BASE_LEVEL=%d MAX_LEVEL=%d MIN_LOD=%.2g MAX_LOD=%.2g Bias=%.2g Filter=%s\n",
162 BaseLevel, MaxLevel, MinLod, MaxLod, LodBias,
163 NearestFilter ? "NEAREST" : "LINEAR");
164 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, BaseLevel);
165 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, MaxLevel);
166
167 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, MinLod);
168 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, MaxLod);
169
170 if (NearestFilter) {
171 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
172 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
173 GL_NEAREST_MIPMAP_NEAREST);
174 }
175 else {
176 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
177 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
178 GL_LINEAR_MIPMAP_LINEAR);
179 }
180
181 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, LodBias);
182
183 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
184 glBegin(GL_QUADS);
185 glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
186 glTexCoord2f(0.0, tcm); glVertex3f(-2.0, 1.0, 0.0);
187 glTexCoord2f(tcm * 3000.0, tcm); glVertex3f(3000.0, 1.0, -6000.0);
188 glTexCoord2f(tcm * 3000.0, 0.0); glVertex3f(3000.0, -1.0, -6000.0);
189 glEnd();
190 glFlush();
191 }
192
193
194 static void
195 myReshape(int w, int h)
196 {
197 glViewport(0, 0, w, h);
198 glMatrixMode(GL_PROJECTION);
199 glLoadIdentity();
200 gluPerspective(60.0, 1.0*(GLfloat)w/(GLfloat)h, 1.0, 30000.0);
201 glMatrixMode(GL_MODELVIEW);
202 glLoadIdentity();
203 }
204
205
206 static void
207 key(unsigned char k, int x, int y)
208 {
209 (void) x;
210 (void) y;
211 switch (k) {
212 case 'b':
213 BaseLevel--;
214 if (BaseLevel < 0)
215 BaseLevel = 0;
216 break;
217 case 'B':
218 BaseLevel++;
219 if (BaseLevel > 10)
220 BaseLevel = 10;
221 break;
222 case 'm':
223 MaxLevel--;
224 if (MaxLevel < 0)
225 MaxLevel = 0;
226 break;
227 case 'M':
228 MaxLevel++;
229 if (MaxLevel > 10)
230 MaxLevel = 10;
231 break;
232 case 'l':
233 LodBias -= 0.25;
234 break;
235 case 'L':
236 LodBias += 0.25;
237 break;
238 case 'n':
239 MinLod -= 0.25;
240 break;
241 case 'N':
242 MinLod += 0.25;
243 break;
244 case 'x':
245 MaxLod -= 0.25;
246 break;
247 case 'X':
248 MaxLod += 0.25;
249 break;
250 case 'f':
251 NearestFilter = !NearestFilter;
252 break;
253 case ' ':
254 initValues();
255 break;
256 case 27: /* Escape */
257 exit(0);
258 break;
259 default:
260 return;
261 }
262 glutPostRedisplay();
263 }
264
265
266 static void
267 usage(void)
268 {
269 printf("usage:\n");
270 printf(" b/B decrease/increase GL_TEXTURE_BASE_LEVEL\n");
271 printf(" m/M decrease/increase GL_TEXTURE_MAX_LEVEL\n");
272 printf(" n/N decrease/increase GL_TEXTURE_MIN_LOD\n");
273 printf(" x/X decrease/increase GL_TEXTURE_MAX_LOD\n");
274 printf(" l/L decrease/increase GL_TEXTURE_LOD_BIAS\n");
275 printf(" f toggle nearest/linear filtering\n");
276 printf(" SPACE reset values\n");
277 }
278
279
280 int
281 main(int argc, char** argv)
282 {
283 glutInit(&argc, argv);
284 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
285 glutInitWindowSize (600, 600);
286 glutCreateWindow (argv[0]);
287 glewInit();
288
289 if (!glutExtensionSupported("GL_EXT_texture_compression_s3tc")) {
290 fprintf(stderr, "This test requires GL_EXT_texture_compression_s3tc.\n");
291 exit(1);
292 }
293
294 myInit();
295 glutReshapeFunc (myReshape);
296 glutDisplayFunc(display);
297 glutKeyboardFunc(key);
298 usage();
299 glutMainLoop();
300 return 0; /* ANSI C requires main to return int. */
301 }