Merge commit 'origin/gallium-master-merge'
[mesa.git] / progs / tests / manytex.c
1
2 /*
3 * test handling of many texture maps
4 * Also tests texture priority and residency.
5 *
6 * Brian Paul
7 * August 2, 2000
8 */
9
10
11 #include <assert.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <math.h>
16 #include <GL/glut.h>
17
18
19 static GLint NumTextures = 20;
20 static GLuint *TextureID = NULL;
21 static GLint *TextureWidth = NULL, *TextureHeight = NULL;
22 static GLboolean *TextureResidency = NULL;
23 static GLint TexWidth = 128, TexHeight = 128;
24 static GLfloat Zrot = 0;
25 static GLboolean Anim = GL_TRUE;
26 static GLint WinWidth = 500, WinHeight = 400;
27 static GLboolean MipMap = GL_FALSE;
28 static GLboolean LinearFilter = GL_FALSE;
29 static GLboolean RandomSize = GL_FALSE;
30 static GLint Rows, Columns;
31 static GLint LowPriorityCount = 0;
32 static GLint Win;
33
34
35 static void Idle( void )
36 {
37 Zrot += 1.0;
38 glutPostRedisplay();
39 }
40
41
42 static void Display( void )
43 {
44 GLfloat spacing = WinWidth / Columns;
45 GLfloat size = spacing * 0.4;
46 GLint i;
47
48 /* test residency */
49 if (0)
50 {
51 GLboolean b;
52 GLint i, resident;
53 b = glAreTexturesResident(NumTextures, TextureID, TextureResidency);
54 if (b) {
55 printf("all resident\n");
56 }
57 else {
58 resident = 0;
59 for (i = 0; i < NumTextures; i++) {
60 if (TextureResidency[i]) {
61 resident++;
62 }
63 }
64 printf("%d of %d texture resident\n", resident, NumTextures);
65 }
66 }
67
68 /* render the textured quads */
69 glClear( GL_COLOR_BUFFER_BIT );
70 for (i = 0; i < NumTextures; i++) {
71 GLint row = i / Columns;
72 GLint col = i % Columns;
73 GLfloat x = col * spacing + spacing * 0.5;
74 GLfloat y = row * spacing + spacing * 0.5;
75
76 GLfloat maxDim = (TextureWidth[i] > TextureHeight[i])
77 ? TextureWidth[i] : TextureHeight[i];
78 GLfloat w = TextureWidth[i] / maxDim;
79 GLfloat h = TextureHeight[i] / maxDim;
80
81 glPushMatrix();
82 glTranslatef(x, y, 0.0);
83 glRotatef(Zrot, 0, 0, 1);
84 glScalef(size, size, 1);
85
86 glBindTexture(GL_TEXTURE_2D, TextureID[i]);
87 glBegin(GL_POLYGON);
88 #if 0
89 glTexCoord2f(0, 0); glVertex2f(-1, -1);
90 glTexCoord2f(1, 0); glVertex2f( 1, -1);
91 glTexCoord2f(1, 1); glVertex2f( 1, 1);
92 glTexCoord2f(0, 1); glVertex2f(-1, 1);
93 #else
94 glTexCoord2f(0, 0); glVertex2f(-w, -h);
95 glTexCoord2f(1, 0); glVertex2f( w, -h);
96 glTexCoord2f(1, 1); glVertex2f( w, h);
97 glTexCoord2f(0, 1); glVertex2f(-w, h);
98 #endif
99 glEnd();
100 glPopMatrix();
101 }
102
103 glutSwapBuffers();
104 }
105
106
107 static void Reshape( int width, int height )
108 {
109 WinWidth = width;
110 WinHeight = height;
111 glViewport( 0, 0, width, height );
112 glMatrixMode( GL_PROJECTION );
113 glLoadIdentity();
114 glOrtho(0, width, 0, height, -1, 1);
115 glMatrixMode( GL_MODELVIEW );
116 glLoadIdentity();
117 }
118
119
120 /*
121 * Return a random int in [min, max].
122 */
123 static int RandomInt(int min, int max)
124 {
125 int i = rand();
126 int j = i % (max - min + 1);
127 return min + j;
128 }
129
130
131 static void DeleteTextures(void)
132 {
133 glDeleteTextures(NumTextures, TextureID);
134 free(TextureID);
135 TextureID = NULL;
136 }
137
138
139
140 static void Init( void )
141 {
142 GLint i;
143
144 if (RandomSize) {
145 printf("Creating %d %s random-size textures, ", NumTextures,
146 MipMap ? "Mipmapped" : "non-Mipmapped");
147 }
148 else {
149 printf("Creating %d %s %d x %d textures, ", NumTextures,
150 MipMap ? "Mipmapped" : "non-Mipmapped",
151 TexWidth, TexHeight);
152 }
153
154 if (LinearFilter) {
155 printf("bilinear filtering\n");
156 }
157 else {
158 printf("nearest filtering\n");
159 }
160
161
162 /* compute number of rows and columns of rects */
163 {
164 GLfloat area = (GLfloat) (WinWidth * WinHeight) / (GLfloat) NumTextures;
165 GLfloat edgeLen = sqrt(area);
166
167 Columns = WinWidth / edgeLen;
168 Rows = (NumTextures + Columns - 1) / Columns;
169 printf("Rows: %d Cols: %d\n", Rows, Columns);
170 }
171
172
173 if (!TextureID) {
174 TextureID = (GLuint *) malloc(sizeof(GLuint) * NumTextures);
175 assert(TextureID);
176 glGenTextures(NumTextures, TextureID);
177 }
178
179 if (!TextureResidency) {
180 TextureResidency = (GLboolean *) malloc(sizeof(GLboolean) * NumTextures);
181 assert(TextureResidency);
182 }
183
184 if (!TextureWidth) {
185 TextureWidth = (GLint *) malloc(sizeof(GLint) * NumTextures);
186 assert(TextureWidth);
187 }
188 if (!TextureHeight) {
189 TextureHeight = (GLint *) malloc(sizeof(GLint) * NumTextures);
190 assert(TextureHeight);
191 }
192
193 for (i = 0; i < NumTextures; i++) {
194 GLubyte color[4];
195 GLubyte *texImage;
196 GLint j, row, col;
197
198 row = i / Columns;
199 col = i % Columns;
200
201 glBindTexture(GL_TEXTURE_2D, TextureID[i]);
202
203 if (i < LowPriorityCount)
204 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 0.5F);
205
206 if (RandomSize) {
207 #if 0
208 int k = (glutGet(GLUT_ELAPSED_TIME) % 7) + 2;
209 TexWidth = 1 << k;
210 TexHeight = 1 << k;
211 #else
212 TexWidth = 1 << RandomInt(2, 7);
213 TexHeight = 1 << RandomInt(2, 7);
214 printf("Random size of %3d: %d x %d\n", i, TexWidth, TexHeight);
215 #endif
216 }
217
218 TextureWidth[i] = TexWidth;
219 TextureHeight[i] = TexHeight;
220
221 texImage = (GLubyte*) malloc(4 * TexWidth * TexHeight * sizeof(GLubyte));
222 assert(texImage);
223
224 /* determine texture color */
225 color[0] = (GLint) (255.0 * ((float) col / (Columns - 1)));
226 color[1] = 127;
227 color[2] = (GLint) (255.0 * ((float) row / (Rows - 1)));
228 color[3] = 255;
229
230 /* fill in solid-colored teximage */
231 for (j = 0; j < TexWidth * TexHeight; j++) {
232 texImage[j*4+0] = color[0];
233 texImage[j*4+1] = color[1];
234 texImage[j*4+2] = color[2];
235 texImage[j*4+3] = color[3];
236 }
237
238 if (MipMap) {
239 GLint level = 0;
240 GLint w = TexWidth, h = TexHeight;
241 while (1) {
242 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0,
243 GL_RGBA, GL_UNSIGNED_BYTE, texImage);
244 if (w == 1 && h == 1)
245 break;
246 if (w > 1)
247 w /= 2;
248 if (h > 1)
249 h /= 2;
250 level++;
251 /*printf("%d: %d x %d\n", level, w, h);*/
252 }
253 if (LinearFilter) {
254 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
255 GL_LINEAR_MIPMAP_LINEAR);
256 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
257 }
258 else {
259 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
260 GL_NEAREST_MIPMAP_NEAREST);
261 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
262 }
263 }
264 else {
265 /* Set corners to white */
266 int k = 0;
267 texImage[k+0] = texImage[k+1] = texImage[k+2] = texImage[k+3] = 255;
268 k = (TexWidth - 1) * 4;
269 texImage[k+0] = texImage[k+1] = texImage[k+2] = texImage[k+3] = 255;
270 k = (TexWidth * TexHeight - TexWidth) * 4;
271 texImage[k+0] = texImage[k+1] = texImage[k+2] = texImage[k+3] = 255;
272 k = (TexWidth * TexHeight - 1) * 4;
273 texImage[k+0] = texImage[k+1] = texImage[k+2] = texImage[k+3] = 255;
274
275 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TexWidth, TexHeight, 0,
276 GL_RGBA, GL_UNSIGNED_BYTE, texImage);
277 if (LinearFilter) {
278 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
279 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
280 }
281 else {
282 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
283 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
284 }
285 }
286
287 free(texImage);
288 }
289
290 glEnable(GL_TEXTURE_2D);
291 }
292
293
294 static void Key( unsigned char key, int x, int y )
295 {
296 const GLfloat step = 3.0;
297 (void) x;
298 (void) y;
299 switch (key) {
300 case 'a':
301 Anim = !Anim;
302 if (Anim)
303 glutIdleFunc(Idle);
304 else
305 glutIdleFunc(NULL);
306 break;
307 case 's':
308 Idle();
309 break;
310 case 'z':
311 Zrot -= step;
312 break;
313 case 'Z':
314 Zrot += step;
315 break;
316 case ' ':
317 DeleteTextures();
318 Init();
319 break;
320 case 27:
321 DeleteTextures();
322 glutDestroyWindow(Win);
323 exit(0);
324 break;
325 }
326 glutPostRedisplay();
327 }
328
329
330 int main( int argc, char *argv[] )
331 {
332 GLint i;
333
334 glutInit( &argc, argv );
335 glutInitWindowPosition( 0, 0 );
336 glutInitWindowSize( WinWidth, WinHeight );
337 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
338 Win = glutCreateWindow(argv[0]);
339 glutReshapeFunc( Reshape );
340 glutKeyboardFunc( Key );
341 glutDisplayFunc( Display );
342 if (Anim)
343 glutIdleFunc(Idle);
344
345 for (i = 1; i < argc; i++) {
346 if (strcmp(argv[i], "-n") == 0) {
347 NumTextures = atoi(argv[i+1]);
348 if (NumTextures <= 0) {
349 printf("Error, bad number of textures\n");
350 return 1;
351 }
352 i++;
353 }
354 else if (strcmp(argv[i], "-mipmap") == 0) {
355 MipMap = GL_TRUE;
356 }
357 else if (strcmp(argv[i], "-linear") == 0) {
358 LinearFilter = GL_TRUE;
359 }
360 else if (strcmp(argv[i], "-size") == 0) {
361 TexWidth = atoi(argv[i+1]);
362 TexHeight = atoi(argv[i+2]);
363 assert(TexWidth >= 1);
364 assert(TexHeight >= 1);
365 i += 2;
366 }
367 else if (strcmp(argv[i], "-randomsize") == 0) {
368 RandomSize = GL_TRUE;
369 }
370 else if (strcmp(argv[i], "-lowpri") == 0) {
371 LowPriorityCount = atoi(argv[i+1]);
372 i++;
373 }
374 else {
375 printf("Usage:\n");
376 printf(" manytex [options]\n");
377 printf("Options:\n");
378 printf(" -n <number of texture objects>\n");
379 printf(" -size <width> <height> - specify texture size\n");
380 printf(" -randomsize - use random size textures\n");
381 printf(" -mipmap - generate mipmaps\n");
382 printf(" -linear - use linear filtering instead of nearest\n");
383 printf(" -lowpri <n> - Set lower priority on <n> textures\n");
384 return 0;
385 }
386 }
387
388 Init();
389
390 glutMainLoop();
391
392 return 0;
393 }