blob: 5fecd9a1489ea70d56f6ac50227e7c92c36f8a2a [file] [log] [blame]
Brian Paul215ff222000-01-28 16:25:44 +00001
2/*
3 * Copyright (C) 1999 Brian Paul All Rights Reserved.
Gareth Hughesdde2da62001-02-07 03:04:58 +00004 *
Brian Paul215ff222000-01-28 16:25:44 +00005 * 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:
Gareth Hughesdde2da62001-02-07 03:04:58 +000011 *
Brian Paul215ff222000-01-28 16:25:44 +000012 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
Gareth Hughesdde2da62001-02-07 03:04:58 +000014 *
Brian Paul215ff222000-01-28 16:25:44 +000015 * 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
Keith Whitwell48e6fff2006-11-01 14:26:10 +000041static GLsizei MaxSize = 2048;
42static GLsizei TexWidth = 1024, TexHeight = 1024, TexBorder = 0;
Brian Paul215ff222000-01-28 16:25:44 +000043static GLboolean ScaleAndBias = GL_FALSE;
44static GLboolean SubImage = GL_FALSE;
45static GLdouble DownloadRate = 0.0; /* texels/sec */
46
47static GLuint Mode = 0;
48
49
Keith Whitwell48e6fff2006-11-01 14:26:10 +000050/* 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
73static GLuint TexObj[NR_TEXOBJ];
74
75
Brian Paul7d69e9e2000-03-29 15:47:48 +000076struct FormatRec {
77 GLenum Format;
78 GLenum Type;
79 GLenum IntFormat;
80 GLint TexelSize;
81};
82
83
Keith Whitwell46d50d92005-03-22 13:32:35 +000084static const struct FormatRec FormatTable[] = {
Brian Paul7d69e9e2000-03-29 15:47:48 +000085 /* Format Type IntFormat TexelSize */
Keith Whitwell46d50d92005-03-22 13:32:35 +000086 { GL_BGRA, GL_UNSIGNED_BYTE, GL_RGBA, 4 },
Brian Paul7d69e9e2000-03-29 15:47:48 +000087 { 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 },
Keith Whitwell46d50d92005-03-22 13:32:35 +000091 { 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 },
Brian Paul7d69e9e2000-03-29 15:47:48 +000094};
95static GLint Format;
96
Keith Whitwell46d50d92005-03-22 13:32:35 +000097#define NUM_FORMATS (sizeof(FormatTable)/sizeof(FormatTable[0]))
Brian Paul7d69e9e2000-03-29 15:47:48 +000098
Brian Paul215ff222000-01-28 16:25:44 +000099static int
Brian Paul7d69e9e2000-03-29 15:47:48 +0000100BytesPerTexel(GLint format)
Brian Paul215ff222000-01-28 16:25:44 +0000101{
Brian Paul7d69e9e2000-03-29 15:47:48 +0000102 return FormatTable[format].TexelSize;
Brian Paul215ff222000-01-28 16:25:44 +0000103}
104
105
106static const char *
107FormatStr(GLenum format)
108{
109 switch (format) {
110 case GL_RGB:
111 return "GL_RGB";
112 case GL_RGBA:
113 return "GL_RGBA";
Keith Whitwell46d50d92005-03-22 13:32:35 +0000114 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";
Brian Paul215ff222000-01-28 16:25:44 +0000122 default:
123 return "";
124 }
125}
126
127
128static const char *
129TypeStr(GLenum type)
130{
131 switch (type) {
132 case GL_UNSIGNED_BYTE:
Brian Paul7d69e9e2000-03-29 15:47:48 +0000133 return "GL_UNSIGNED_BYTE";
Brian Paul215ff222000-01-28 16:25:44 +0000134 case GL_UNSIGNED_SHORT:
Brian Paul7d69e9e2000-03-29 15:47:48 +0000135 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";
Brian Paul215ff222000-01-28 16:25:44 +0000140 default:
141 return "";
142 }
143}
144
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000145/* 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
Xiang, Haihaoafba8f02007-01-17 10:17:10 +0800152static unsigned long align(unsigned long value, unsigned long a)
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000153{
154 return (value + a - 1) & ~(a-1);
155}
156
157static int MIN2(int a, int b)
158{
159 return a < b ? a : b;
160}
Brian Paul215ff222000-01-28 16:25:44 +0000161
162static void
163MeasureDownloadRate(void)
164{
165 const int w = TexWidth + 2 * TexBorder;
166 const int h = TexHeight + 2 * TexBorder;
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000167 const int image_bytes = align(w * h * BytesPerTexel(Format), ALIGN);
168 const int bytes = image_bytes * NR_TEXOBJ;
169 GLubyte *orig_texImage, *orig_getImage;
Brian Paul215ff222000-01-28 16:25:44 +0000170 GLubyte *texImage, *getImage;
171 GLdouble t0, t1, time;
172 int count;
173 int i;
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000174 int offset = 0;
175 GLdouble total = 0; /* ints will tend to overflow */
Brian Paul215ff222000-01-28 16:25:44 +0000176
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000177 printf("allocating %d bytes for %d %dx%d images\n",
178 bytes, NR_TEXOBJ, w, h);
179
180 orig_texImage = (GLubyte *) malloc(bytes + ALIGN);
181 orig_getImage = (GLubyte *) malloc(image_bytes + ALIGN);
182 if (!orig_texImage || !orig_getImage) {
Brian Paul215ff222000-01-28 16:25:44 +0000183 DownloadRate = 0.0;
184 return;
185 }
186
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000187 printf("alloc %p %p\n", orig_texImage, orig_getImage);
188
Xiang, Haihaoafba8f02007-01-17 10:17:10 +0800189 texImage = (GLubyte *)align((unsigned long)orig_texImage, ALIGN);
190 getImage = (GLubyte *)align((unsigned long)orig_getImage, ALIGN);
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000191
Xiang, Haihaoafba8f02007-01-17 10:17:10 +0800192 for (i = 1; !(((unsigned long)texImage) & i); i<<=1)
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000193 ;
194 printf("texture image alignment: %d bytes (%p)\n", i, texImage);
195
Brian Paul215ff222000-01-28 16:25:44 +0000196 for (i = 0; i < bytes; i++) {
197 texImage[i] = i & 0xff;
198 }
199
200 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
201 glPixelStorei(GL_PACK_ALIGNMENT, 1);
202
203 if (ScaleAndBias) {
204 glPixelTransferf(GL_RED_SCALE, 0.5);
205 glPixelTransferf(GL_GREEN_SCALE, 0.5);
206 glPixelTransferf(GL_BLUE_SCALE, 0.5);
207 glPixelTransferf(GL_RED_BIAS, 0.5);
208 glPixelTransferf(GL_GREEN_BIAS, 0.5);
209 glPixelTransferf(GL_BLUE_BIAS, 0.5);
210 }
211 else {
212 glPixelTransferf(GL_RED_SCALE, 1.0);
213 glPixelTransferf(GL_GREEN_SCALE, 1.0);
214 glPixelTransferf(GL_BLUE_SCALE, 1.0);
215 glPixelTransferf(GL_RED_BIAS, 0.0);
216 glPixelTransferf(GL_GREEN_BIAS, 0.0);
217 glPixelTransferf(GL_BLUE_BIAS, 0.0);
218 }
219
Gareth Hughesdde2da62001-02-07 03:04:58 +0000220 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
221 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
222 glEnable(GL_TEXTURE_2D);
223
Brian Paul215ff222000-01-28 16:25:44 +0000224 count = 0;
225 t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
226 do {
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000227 int img = count%NR_TEXOBJ;
228 GLubyte *img_ptr = texImage + img * image_bytes;
229
230 glBindTexture(GL_TEXTURE_2D, TexObj[img]);
231
Brian Paul215ff222000-01-28 16:25:44 +0000232 if (SubImage && count > 0) {
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000233 /* Only update a portion of the image each iteration. This
234 * is presumably why you'd want to use texsubimage, otherwise
235 * you may as well just call teximage again.
236 *
237 * A bigger question is whether to use a pointer that moves
238 * with each call, ie does the incoming data come from L2
239 * cache under normal circumstances, or is it pulled from
240 * uncached memory?
241 *
242 * There's a good argument to say L2 cache, ie you'd expect
243 * the data to have been recently generated. It's possible
244 * that it could have come from a file read, which may or may
245 * not have gone through the cpu.
246 */
247 glTexSubImage2D(GL_TEXTURE_2D, 0,
248 -TexBorder,
249 -TexBorder + offset * h/8,
250 w,
251 h/8,
Brian Paul7d69e9e2000-03-29 15:47:48 +0000252 FormatTable[Format].Format,
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000253 FormatTable[Format].Type,
254#if 1
255 texImage /* likely in L2$ */
256#else
257 img_ptr + offset * bytes/8 /* unlikely in L2$ */
258#endif
259 );
260 offset += 1;
261 offset %= 8;
262 total += w * h / 8;
Brian Paul215ff222000-01-28 16:25:44 +0000263 }
264 else {
Brian Paul7d69e9e2000-03-29 15:47:48 +0000265 glTexImage2D(GL_TEXTURE_2D, 0,
266 FormatTable[Format].IntFormat, w, h, TexBorder,
267 FormatTable[Format].Format,
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000268 FormatTable[Format].Type,
269 img_ptr);
270 total += w*h;
Brian Paul7d69e9e2000-03-29 15:47:48 +0000271 }
272
Gareth Hughesdde2da62001-02-07 03:04:58 +0000273 /* draw a tiny polygon to force texture into texram */
274 glBegin(GL_TRIANGLES);
275 glTexCoord2f(0, 0); glVertex2f(1, 1);
276 glTexCoord2f(1, 0); glVertex2f(3, 1);
277 glTexCoord2f(0.5, 1); glVertex2f(2, 3);
278 glEnd();
Brian Paul215ff222000-01-28 16:25:44 +0000279
280 t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
281 time = t1 - t0;
282 count++;
283 } while (time < 3.0);
284
Gareth Hughesdde2da62001-02-07 03:04:58 +0000285 glDisable(GL_TEXTURE_2D);
286
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000287 printf("total texels=%f time=%f\n", total, time);
288 DownloadRate = total / time;
Brian Paul215ff222000-01-28 16:25:44 +0000289
Brian Paul215ff222000-01-28 16:25:44 +0000290
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000291 free(orig_texImage);
292 free(orig_getImage);
Brian Paul215ff222000-01-28 16:25:44 +0000293
294 {
295 GLint err = glGetError();
296 if (err)
297 printf("GL error %d\n", err);
298 }
299}
300
301
302static void
303PrintString(const char *s)
304{
305 while (*s) {
306 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
307 s++;
308 }
309}
310
311
312static void
313Display(void)
314{
315 const int w = TexWidth + 2 * TexBorder;
316 const int h = TexHeight + 2 * TexBorder;
317 char s[1000];
318
319 glClear(GL_COLOR_BUFFER_BIT);
320
Brian Paul7d69e9e2000-03-29 15:47:48 +0000321 glRasterPos2i(10, 80);
Brian Paul215ff222000-01-28 16:25:44 +0000322 sprintf(s, "Texture size[cursor]: %d x %d Border[b]: %d", w, h, TexBorder);
323 PrintString(s);
324
Brian Paul7d69e9e2000-03-29 15:47:48 +0000325 glRasterPos2i(10, 65);
326 sprintf(s, "Format[f]: %s Type: %s IntFormat: %s",
327 FormatStr(FormatTable[Format].Format),
328 TypeStr( FormatTable[Format].Type),
329 FormatStr(FormatTable[Format].IntFormat));
Brian Paul215ff222000-01-28 16:25:44 +0000330 PrintString(s);
331
Brian Paul7d69e9e2000-03-29 15:47:48 +0000332 glRasterPos2i(10, 50);
Brian Paul215ff222000-01-28 16:25:44 +0000333 sprintf(s, "Pixel Scale&Bias[p]: %s TexSubImage[s]: %s",
334 ScaleAndBias ? "Yes" : "No",
335 SubImage ? "Yes" : "No");
336 PrintString(s);
337
338 if (Mode == 0) {
339 glRasterPos2i(200, 10);
340 sprintf(s, "...Measuring...");
341 PrintString(s);
342 glutSwapBuffers();
343 glutPostRedisplay();
344 Mode++;
345 }
346 else if (Mode == 1) {
347 MeasureDownloadRate();
348 glutPostRedisplay();
349 Mode++;
350 }
351 else {
352 /* show results */
353 glRasterPos2i(10, 10);
354 sprintf(s, "Download rate: %g Mtexels/second %g MB/second",
355 DownloadRate / 1000000.0,
Brian Paul7d69e9e2000-03-29 15:47:48 +0000356 DownloadRate * BytesPerTexel(Format) / 1000000.0);
Brian Paul215ff222000-01-28 16:25:44 +0000357 PrintString(s);
Brian Paul7d69e9e2000-03-29 15:47:48 +0000358 {
359 GLint r, g, b, a, l, i;
360 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_RED_SIZE, &r);
361 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_SIZE, &g);
362 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_SIZE, &b);
363 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_SIZE, &a);
364 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_LUMINANCE_SIZE, &l);
365 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTENSITY_SIZE, &i);
366 sprintf(s, "TexelBits: R=%d G=%d B=%d A=%d L=%d I=%d", r, g, b, a, l, i);
367 glRasterPos2i(10, 25);
368 PrintString(s);
369 }
370
Brian Paul215ff222000-01-28 16:25:44 +0000371 glutSwapBuffers();
372 }
373}
374
375
376static void
377Reshape(int width, int height)
378{
379 glViewport( 0, 0, width, height );
380 glMatrixMode( GL_PROJECTION );
381 glLoadIdentity();
382 glOrtho(0, width, 0, height, -1, 1);
383 glMatrixMode( GL_MODELVIEW );
384 glLoadIdentity();
385}
386
387
Brian Paul215ff222000-01-28 16:25:44 +0000388
389static void
390Key(unsigned char key, int x, int y)
391{
392 (void) x;
393 (void) y;
394 switch (key) {
395 case ' ':
Brian Paul8fd9fcb2000-03-29 18:02:52 +0000396 Mode = 0;
Brian Paul215ff222000-01-28 16:25:44 +0000397 break;
398 case 'b':
399 /* toggle border */
400 TexBorder = 1 - TexBorder;
401 Mode = 0;
402 break;
403 case 'f':
404 /* change format */
Brian Paul7d69e9e2000-03-29 15:47:48 +0000405 Format = (Format + 1) % NUM_FORMATS;
Brian Paul215ff222000-01-28 16:25:44 +0000406 Mode = 0;
407 break;
Keith Whitwell46d50d92005-03-22 13:32:35 +0000408 case 'F':
409 /* change format */
410 Format = (Format - 1) % NUM_FORMATS;
411 Mode = 0;
412 break;
Brian Paul215ff222000-01-28 16:25:44 +0000413 case 'p':
414 /* toggle border */
415 ScaleAndBias = !ScaleAndBias;
416 Mode = 0;
417 break;
418 case 's':
419 SubImage = !SubImage;
420 Mode = 0;
421 break;
Brian Paul215ff222000-01-28 16:25:44 +0000422 case 27:
423 exit(0);
424 break;
425 }
426 glutPostRedisplay();
427}
428
429
430static void
431SpecialKey(int key, int x, int y)
432{
433 (void) x;
434 (void) y;
435 switch (key) {
436 case GLUT_KEY_UP:
437 if (TexHeight < MaxSize)
438 TexHeight *= 2;
439 break;
440 case GLUT_KEY_DOWN:
441 if (TexHeight > 1)
442 TexHeight /= 2;
443 break;
444 case GLUT_KEY_LEFT:
445 if (TexWidth > 1)
446 TexWidth /= 2;
447 break;
448 case GLUT_KEY_RIGHT:
449 if (TexWidth < MaxSize)
450 TexWidth *= 2;
451 break;
452 }
453 Mode = 0;
454 glutPostRedisplay();
455}
456
457
458static void
459Init(void)
460{
461 printf("GL_VENDOR = %s\n", (const char *) glGetString(GL_VENDOR));
462 printf("GL_VERSION = %s\n", (const char *) glGetString(GL_VERSION));
463 printf("GL_RENDERER = %s\n", (const char *) glGetString(GL_RENDERER));
464}
465
466
467int
468main(int argc, char *argv[])
469{
470 glutInit( &argc, argv );
471 glutInitWindowPosition( 0, 0 );
472 glutInitWindowSize( 600, 100 );
473 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
474 glutCreateWindow(argv[0]);
475 glutReshapeFunc( Reshape );
476 glutKeyboardFunc( Key );
477 glutSpecialFunc( SpecialKey );
478 glutDisplayFunc( Display );
479 Init();
480 glutMainLoop();
481 return 0;
482}