Merge branch '7.8'
[mesa.git] / progs / tests / texdown.c
1
2 /*
3 * Copyright (C) 1999 Brian Paul All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23
24 /*
25 * texdown
26 *
27 * Measure texture download speed.
28 * Use keyboard to change texture size, format, datatype, scale/bias,
29 * subimageload, etc.
30 *
31 * Brian Paul 28 January 2000
32 */
33
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <math.h>
38 #include <GL/glut.h>
39
40
41 static GLsizei MaxSize = 2048;
42 static GLsizei TexWidth = 1024, TexHeight = 1024, TexBorder = 0;
43 static GLboolean ScaleAndBias = GL_FALSE;
44 static GLboolean SubImage = GL_FALSE;
45 static GLdouble DownloadRate = 0.0; /* texels/sec */
46
47 static GLuint Mode = 0;
48
49
50 /* Try and avoid L2 cache effects by cycling through a small number of
51 * textures.
52 *
53 * At the initial size of 1024x1024x4 == 4mbyte, say 8 textures will
54 * keep us out of most caches at 32mb total.
55 *
56 * This turns into a fairly interesting question of what exactly you
57 * expect to be in cache in normal usage, and what you think should be
58 * outside. There's no rules for this, no reason to favour one usage
59 * over another except what the application you care about happens to
60 * resemble most closely.
61 *
62 * - Should the client texture image be in L2 cache? Has it just been
63 * generated or read from disk?
64 * - Does the application really use >1 texture, or is it constantly
65 * updating one image in-place?
66 *
67 * Different answers will favour different texture upload mechanisms.
68 * To upload an image that is purely outside of cache, a DMA-based
69 * upload will probably win, whereas for small, in-cache textures,
70 * copying looks good.
71 */
72 #define NR_TEXOBJ 4
73 static GLuint TexObj[NR_TEXOBJ];
74
75
76 struct FormatRec {
77 GLenum Format;
78 GLenum Type;
79 GLenum IntFormat;
80 GLint TexelSize;
81 };
82
83
84 static const struct FormatRec FormatTable[] = {
85 /* Format Type IntFormat TexelSize */
86 { GL_BGRA, GL_UNSIGNED_BYTE, GL_RGBA, 4 },
87 { GL_RGB, GL_UNSIGNED_BYTE, GL_RGB, 3 },
88 { GL_RGBA, GL_UNSIGNED_BYTE, GL_RGBA, 4 },
89 { GL_RGBA, GL_UNSIGNED_BYTE, GL_RGB, 4 },
90 { GL_RGB, GL_UNSIGNED_SHORT_5_6_5, GL_RGB, 2 },
91 { GL_LUMINANCE, GL_UNSIGNED_BYTE, GL_LUMINANCE, 1 },
92 { GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, GL_LUMINANCE_ALPHA, 2 },
93 { GL_ALPHA, GL_UNSIGNED_BYTE, GL_ALPHA, 1 },
94 };
95 static GLint Format;
96
97 #define NUM_FORMATS (sizeof(FormatTable)/sizeof(FormatTable[0]))
98
99 static int
100 BytesPerTexel(GLint format)
101 {
102 return FormatTable[format].TexelSize;
103 }
104
105
106 static const char *
107 FormatStr(GLenum format)
108 {
109 switch (format) {
110 case GL_RGB:
111 return "GL_RGB";
112 case GL_RGBA:
113 return "GL_RGBA";
114 case GL_BGRA:
115 return "GL_BGRA";
116 case GL_LUMINANCE:
117 return "GL_LUMINANCE";
118 case GL_LUMINANCE_ALPHA:
119 return "GL_LUMINANCE_ALPHA";
120 case GL_ALPHA:
121 return "GL_ALPHA";
122 default:
123 return "";
124 }
125 }
126
127
128 static const char *
129 TypeStr(GLenum type)
130 {
131 switch (type) {
132 case GL_UNSIGNED_BYTE:
133 return "GL_UNSIGNED_BYTE";
134 case GL_UNSIGNED_SHORT:
135 return "GL_UNSIGNED_SHORT";
136 case GL_UNSIGNED_SHORT_5_6_5:
137 return "GL_UNSIGNED_SHORT_5_6_5";
138 case GL_UNSIGNED_SHORT_5_6_5_REV:
139 return "GL_UNSIGNED_SHORT_5_6_5_REV";
140 default:
141 return "";
142 }
143 }
144
145 /* On x86, there is a performance cliff for memcpy to texture memory
146 * for sources below 64 byte alignment. We do our best with this in
147 * the driver, but it is better if the images are correctly aligned to
148 * start with:
149 */
150 #define ALIGN (1<<12)
151
152 static unsigned long align(unsigned long value, unsigned long a)
153 {
154 return (value + a - 1) & ~(a-1);
155 }
156
157 static void
158 MeasureDownloadRate(void)
159 {
160 const int w = TexWidth + 2 * TexBorder;
161 const int h = TexHeight + 2 * TexBorder;
162 const int image_bytes = align(w * h * BytesPerTexel(Format), ALIGN);
163 const int bytes = image_bytes * NR_TEXOBJ;
164 GLubyte *orig_texImage, *orig_getImage;
165 GLubyte *texImage;
166 GLdouble t0, t1, time;
167 int count;
168 int i;
169 int offset = 0;
170 GLdouble total = 0; /* ints will tend to overflow */
171
172 printf("allocating %d bytes for %d %dx%d images\n",
173 bytes, NR_TEXOBJ, w, h);
174
175 orig_texImage = (GLubyte *) malloc(bytes + ALIGN);
176 orig_getImage = (GLubyte *) malloc(image_bytes + ALIGN);
177 if (!orig_texImage || !orig_getImage) {
178 DownloadRate = 0.0;
179 free(orig_texImage);
180 free(orig_getImage);
181 return;
182 }
183
184 printf("alloc %p %p\n", orig_texImage, orig_getImage);
185
186 texImage = (GLubyte *)align((unsigned long)orig_texImage, ALIGN);
187
188 for (i = 1; !(((unsigned long)texImage) & i); i<<=1)
189 ;
190 printf("texture image alignment: %d bytes (%p)\n", i, texImage);
191
192 for (i = 0; i < bytes; i++) {
193 texImage[i] = i & 0xff;
194 }
195
196 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
197 glPixelStorei(GL_PACK_ALIGNMENT, 1);
198
199 if (ScaleAndBias) {
200 glPixelTransferf(GL_RED_SCALE, 0.5);
201 glPixelTransferf(GL_GREEN_SCALE, 0.5);
202 glPixelTransferf(GL_BLUE_SCALE, 0.5);
203 glPixelTransferf(GL_RED_BIAS, 0.5);
204 glPixelTransferf(GL_GREEN_BIAS, 0.5);
205 glPixelTransferf(GL_BLUE_BIAS, 0.5);
206 }
207 else {
208 glPixelTransferf(GL_RED_SCALE, 1.0);
209 glPixelTransferf(GL_GREEN_SCALE, 1.0);
210 glPixelTransferf(GL_BLUE_SCALE, 1.0);
211 glPixelTransferf(GL_RED_BIAS, 0.0);
212 glPixelTransferf(GL_GREEN_BIAS, 0.0);
213 glPixelTransferf(GL_BLUE_BIAS, 0.0);
214 }
215
216 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
217 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
218 glEnable(GL_TEXTURE_2D);
219
220 count = 0;
221 t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
222 do {
223 int img = count%NR_TEXOBJ;
224 GLubyte *img_ptr = texImage + img * image_bytes;
225
226 glBindTexture(GL_TEXTURE_2D, TexObj[img]);
227
228 if (SubImage && count > 0) {
229 /* Only update a portion of the image each iteration. This
230 * is presumably why you'd want to use texsubimage, otherwise
231 * you may as well just call teximage again.
232 *
233 * A bigger question is whether to use a pointer that moves
234 * with each call, ie does the incoming data come from L2
235 * cache under normal circumstances, or is it pulled from
236 * uncached memory?
237 *
238 * There's a good argument to say L2 cache, ie you'd expect
239 * the data to have been recently generated. It's possible
240 * that it could have come from a file read, which may or may
241 * not have gone through the cpu.
242 */
243 glTexSubImage2D(GL_TEXTURE_2D, 0,
244 -TexBorder,
245 -TexBorder + offset * h/8,
246 w,
247 h/8,
248 FormatTable[Format].Format,
249 FormatTable[Format].Type,
250 #if 1
251 texImage /* likely in L2$ */
252 #else
253 img_ptr + offset * bytes/8 /* unlikely in L2$ */
254 #endif
255 );
256 offset += 1;
257 offset %= 8;
258 total += w * h / 8;
259 }
260 else {
261 glTexImage2D(GL_TEXTURE_2D, 0,
262 FormatTable[Format].IntFormat, w, h, TexBorder,
263 FormatTable[Format].Format,
264 FormatTable[Format].Type,
265 img_ptr);
266 total += w*h;
267 }
268
269 /* draw a tiny polygon to force texture into texram */
270 glBegin(GL_TRIANGLES);
271 glTexCoord2f(0, 0); glVertex2f(1, 1);
272 glTexCoord2f(1, 0); glVertex2f(3, 1);
273 glTexCoord2f(0.5, 1); glVertex2f(2, 3);
274 glEnd();
275
276 t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
277 time = t1 - t0;
278 count++;
279 } while (time < 3.0);
280
281 glDisable(GL_TEXTURE_2D);
282
283 printf("total texels=%f time=%f\n", total, time);
284 DownloadRate = total / time;
285
286
287 free(orig_texImage);
288 free(orig_getImage);
289
290 {
291 GLint err = glGetError();
292 if (err)
293 printf("GL error %d\n", err);
294 }
295 }
296
297
298 static void
299 PrintString(const char *s)
300 {
301 while (*s) {
302 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
303 s++;
304 }
305 }
306
307
308 static void
309 Display(void)
310 {
311 const int w = TexWidth + 2 * TexBorder;
312 const int h = TexHeight + 2 * TexBorder;
313 char s[1000];
314
315 glClear(GL_COLOR_BUFFER_BIT);
316
317 glRasterPos2i(10, 80);
318 sprintf(s, "Texture size[cursor]: %d x %d Border[b]: %d", w, h, TexBorder);
319 PrintString(s);
320
321 glRasterPos2i(10, 65);
322 sprintf(s, "Format[f]: %s Type: %s IntFormat: %s",
323 FormatStr(FormatTable[Format].Format),
324 TypeStr( FormatTable[Format].Type),
325 FormatStr(FormatTable[Format].IntFormat));
326 PrintString(s);
327
328 glRasterPos2i(10, 50);
329 sprintf(s, "Pixel Scale&Bias[p]: %s TexSubImage[s]: %s",
330 ScaleAndBias ? "Yes" : "No",
331 SubImage ? "Yes" : "No");
332 PrintString(s);
333
334 if (Mode == 0) {
335 glRasterPos2i(200, 10);
336 sprintf(s, "...Measuring...");
337 PrintString(s);
338 glutSwapBuffers();
339 glutPostRedisplay();
340 Mode++;
341 }
342 else if (Mode == 1) {
343 MeasureDownloadRate();
344 glutPostRedisplay();
345 Mode++;
346 }
347 else {
348 /* show results */
349 glRasterPos2i(10, 10);
350 sprintf(s, "Download rate: %g Mtexels/second %g MB/second",
351 DownloadRate / 1000000.0,
352 DownloadRate * BytesPerTexel(Format) / 1000000.0);
353 PrintString(s);
354 {
355 GLint r, g, b, a, l, i;
356 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_RED_SIZE, &r);
357 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_SIZE, &g);
358 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_SIZE, &b);
359 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_SIZE, &a);
360 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_LUMINANCE_SIZE, &l);
361 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTENSITY_SIZE, &i);
362 sprintf(s, "TexelBits: R=%d G=%d B=%d A=%d L=%d I=%d", r, g, b, a, l, i);
363 glRasterPos2i(10, 25);
364 PrintString(s);
365 }
366
367 glutSwapBuffers();
368 }
369 }
370
371
372 static void
373 Reshape(int width, int height)
374 {
375 glViewport( 0, 0, width, height );
376 glMatrixMode( GL_PROJECTION );
377 glLoadIdentity();
378 glOrtho(0, width, 0, height, -1, 1);
379 glMatrixMode( GL_MODELVIEW );
380 glLoadIdentity();
381 }
382
383
384
385 static void
386 Key(unsigned char key, int x, int y)
387 {
388 (void) x;
389 (void) y;
390 switch (key) {
391 case ' ':
392 Mode = 0;
393 break;
394 case 'b':
395 /* toggle border */
396 TexBorder = 1 - TexBorder;
397 Mode = 0;
398 break;
399 case 'f':
400 /* change format */
401 Format = (Format + 1) % NUM_FORMATS;
402 Mode = 0;
403 break;
404 case 'F':
405 /* change format */
406 Format = (Format - 1) % NUM_FORMATS;
407 Mode = 0;
408 break;
409 case 'p':
410 /* toggle border */
411 ScaleAndBias = !ScaleAndBias;
412 Mode = 0;
413 break;
414 case 's':
415 SubImage = !SubImage;
416 Mode = 0;
417 break;
418 case 27:
419 exit(0);
420 break;
421 }
422 glutPostRedisplay();
423 }
424
425
426 static void
427 SpecialKey(int key, int x, int y)
428 {
429 (void) x;
430 (void) y;
431 switch (key) {
432 case GLUT_KEY_UP:
433 if (TexHeight < MaxSize)
434 TexHeight *= 2;
435 break;
436 case GLUT_KEY_DOWN:
437 if (TexHeight > 1)
438 TexHeight /= 2;
439 break;
440 case GLUT_KEY_LEFT:
441 if (TexWidth > 1)
442 TexWidth /= 2;
443 break;
444 case GLUT_KEY_RIGHT:
445 if (TexWidth < MaxSize)
446 TexWidth *= 2;
447 break;
448 }
449 Mode = 0;
450 glutPostRedisplay();
451 }
452
453
454 static void
455 Init(void)
456 {
457 printf("GL_VENDOR = %s\n", (const char *) glGetString(GL_VENDOR));
458 printf("GL_VERSION = %s\n", (const char *) glGetString(GL_VERSION));
459 printf("GL_RENDERER = %s\n", (const char *) glGetString(GL_RENDERER));
460 }
461
462
463 int
464 main(int argc, char *argv[])
465 {
466 glutInit( &argc, argv );
467 glutInitWindowPosition( 0, 0 );
468 glutInitWindowSize( 600, 100 );
469 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
470 glutCreateWindow(argv[0]);
471 glutReshapeFunc( Reshape );
472 glutKeyboardFunc( Key );
473 glutSpecialFunc( SpecialKey );
474 glutDisplayFunc( Display );
475 Init();
476 glutMainLoop();
477 return 0;
478 }