blob: 7e46045832545f9fe43c7a08ddd334a0ad709bec [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
Brian Paul215ff222000-01-28 16:25:44 +0000157static void
158MeasureDownloadRate(void)
159{
160 const int w = TexWidth + 2 * TexBorder;
161 const int h = TexHeight + 2 * TexBorder;
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000162 const int image_bytes = align(w * h * BytesPerTexel(Format), ALIGN);
163 const int bytes = image_bytes * NR_TEXOBJ;
164 GLubyte *orig_texImage, *orig_getImage;
Brian Paul215ff222000-01-28 16:25:44 +0000165 GLubyte *texImage, *getImage;
166 GLdouble t0, t1, time;
167 int count;
168 int i;
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000169 int offset = 0;
170 GLdouble total = 0; /* ints will tend to overflow */
Brian Paul215ff222000-01-28 16:25:44 +0000171
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000172 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) {
Brian Paul215ff222000-01-28 16:25:44 +0000178 DownloadRate = 0.0;
179 return;
180 }
181
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000182 printf("alloc %p %p\n", orig_texImage, orig_getImage);
183
Xiang, Haihaoafba8f02007-01-17 10:17:10 +0800184 texImage = (GLubyte *)align((unsigned long)orig_texImage, ALIGN);
185 getImage = (GLubyte *)align((unsigned long)orig_getImage, ALIGN);
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000186
Xiang, Haihaoafba8f02007-01-17 10:17:10 +0800187 for (i = 1; !(((unsigned long)texImage) & i); i<<=1)
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000188 ;
189 printf("texture image alignment: %d bytes (%p)\n", i, texImage);
190
Brian Paul215ff222000-01-28 16:25:44 +0000191 for (i = 0; i < bytes; i++) {
192 texImage[i] = i & 0xff;
193 }
194
195 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
196 glPixelStorei(GL_PACK_ALIGNMENT, 1);
197
198 if (ScaleAndBias) {
199 glPixelTransferf(GL_RED_SCALE, 0.5);
200 glPixelTransferf(GL_GREEN_SCALE, 0.5);
201 glPixelTransferf(GL_BLUE_SCALE, 0.5);
202 glPixelTransferf(GL_RED_BIAS, 0.5);
203 glPixelTransferf(GL_GREEN_BIAS, 0.5);
204 glPixelTransferf(GL_BLUE_BIAS, 0.5);
205 }
206 else {
207 glPixelTransferf(GL_RED_SCALE, 1.0);
208 glPixelTransferf(GL_GREEN_SCALE, 1.0);
209 glPixelTransferf(GL_BLUE_SCALE, 1.0);
210 glPixelTransferf(GL_RED_BIAS, 0.0);
211 glPixelTransferf(GL_GREEN_BIAS, 0.0);
212 glPixelTransferf(GL_BLUE_BIAS, 0.0);
213 }
214
Gareth Hughesdde2da62001-02-07 03:04:58 +0000215 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
216 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
217 glEnable(GL_TEXTURE_2D);
218
Brian Paul215ff222000-01-28 16:25:44 +0000219 count = 0;
220 t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
221 do {
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000222 int img = count%NR_TEXOBJ;
223 GLubyte *img_ptr = texImage + img * image_bytes;
224
225 glBindTexture(GL_TEXTURE_2D, TexObj[img]);
226
Brian Paul215ff222000-01-28 16:25:44 +0000227 if (SubImage && count > 0) {
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000228 /* Only update a portion of the image each iteration. This
229 * is presumably why you'd want to use texsubimage, otherwise
230 * you may as well just call teximage again.
231 *
232 * A bigger question is whether to use a pointer that moves
233 * with each call, ie does the incoming data come from L2
234 * cache under normal circumstances, or is it pulled from
235 * uncached memory?
236 *
237 * There's a good argument to say L2 cache, ie you'd expect
238 * the data to have been recently generated. It's possible
239 * that it could have come from a file read, which may or may
240 * not have gone through the cpu.
241 */
242 glTexSubImage2D(GL_TEXTURE_2D, 0,
243 -TexBorder,
244 -TexBorder + offset * h/8,
245 w,
246 h/8,
Brian Paul7d69e9e2000-03-29 15:47:48 +0000247 FormatTable[Format].Format,
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000248 FormatTable[Format].Type,
249#if 1
250 texImage /* likely in L2$ */
251#else
252 img_ptr + offset * bytes/8 /* unlikely in L2$ */
253#endif
254 );
255 offset += 1;
256 offset %= 8;
257 total += w * h / 8;
Brian Paul215ff222000-01-28 16:25:44 +0000258 }
259 else {
Brian Paul7d69e9e2000-03-29 15:47:48 +0000260 glTexImage2D(GL_TEXTURE_2D, 0,
261 FormatTable[Format].IntFormat, w, h, TexBorder,
262 FormatTable[Format].Format,
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000263 FormatTable[Format].Type,
264 img_ptr);
265 total += w*h;
Brian Paul7d69e9e2000-03-29 15:47:48 +0000266 }
267
Gareth Hughesdde2da62001-02-07 03:04:58 +0000268 /* draw a tiny polygon to force texture into texram */
269 glBegin(GL_TRIANGLES);
270 glTexCoord2f(0, 0); glVertex2f(1, 1);
271 glTexCoord2f(1, 0); glVertex2f(3, 1);
272 glTexCoord2f(0.5, 1); glVertex2f(2, 3);
273 glEnd();
Brian Paul215ff222000-01-28 16:25:44 +0000274
275 t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
276 time = t1 - t0;
277 count++;
278 } while (time < 3.0);
279
Gareth Hughesdde2da62001-02-07 03:04:58 +0000280 glDisable(GL_TEXTURE_2D);
281
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000282 printf("total texels=%f time=%f\n", total, time);
283 DownloadRate = total / time;
Brian Paul215ff222000-01-28 16:25:44 +0000284
Brian Paul215ff222000-01-28 16:25:44 +0000285
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000286 free(orig_texImage);
287 free(orig_getImage);
Brian Paul215ff222000-01-28 16:25:44 +0000288
289 {
290 GLint err = glGetError();
291 if (err)
292 printf("GL error %d\n", err);
293 }
294}
295
296
297static void
298PrintString(const char *s)
299{
300 while (*s) {
301 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
302 s++;
303 }
304}
305
306
307static void
308Display(void)
309{
310 const int w = TexWidth + 2 * TexBorder;
311 const int h = TexHeight + 2 * TexBorder;
312 char s[1000];
313
314 glClear(GL_COLOR_BUFFER_BIT);
315
Brian Paul7d69e9e2000-03-29 15:47:48 +0000316 glRasterPos2i(10, 80);
Brian Paul215ff222000-01-28 16:25:44 +0000317 sprintf(s, "Texture size[cursor]: %d x %d Border[b]: %d", w, h, TexBorder);
318 PrintString(s);
319
Brian Paul7d69e9e2000-03-29 15:47:48 +0000320 glRasterPos2i(10, 65);
321 sprintf(s, "Format[f]: %s Type: %s IntFormat: %s",
322 FormatStr(FormatTable[Format].Format),
323 TypeStr( FormatTable[Format].Type),
324 FormatStr(FormatTable[Format].IntFormat));
Brian Paul215ff222000-01-28 16:25:44 +0000325 PrintString(s);
326
Brian Paul7d69e9e2000-03-29 15:47:48 +0000327 glRasterPos2i(10, 50);
Brian Paul215ff222000-01-28 16:25:44 +0000328 sprintf(s, "Pixel Scale&Bias[p]: %s TexSubImage[s]: %s",
329 ScaleAndBias ? "Yes" : "No",
330 SubImage ? "Yes" : "No");
331 PrintString(s);
332
333 if (Mode == 0) {
334 glRasterPos2i(200, 10);
335 sprintf(s, "...Measuring...");
336 PrintString(s);
337 glutSwapBuffers();
338 glutPostRedisplay();
339 Mode++;
340 }
341 else if (Mode == 1) {
342 MeasureDownloadRate();
343 glutPostRedisplay();
344 Mode++;
345 }
346 else {
347 /* show results */
348 glRasterPos2i(10, 10);
349 sprintf(s, "Download rate: %g Mtexels/second %g MB/second",
350 DownloadRate / 1000000.0,
Brian Paul7d69e9e2000-03-29 15:47:48 +0000351 DownloadRate * BytesPerTexel(Format) / 1000000.0);
Brian Paul215ff222000-01-28 16:25:44 +0000352 PrintString(s);
Brian Paul7d69e9e2000-03-29 15:47:48 +0000353 {
354 GLint r, g, b, a, l, i;
355 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_RED_SIZE, &r);
356 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_SIZE, &g);
357 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_SIZE, &b);
358 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_SIZE, &a);
359 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_LUMINANCE_SIZE, &l);
360 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTENSITY_SIZE, &i);
361 sprintf(s, "TexelBits: R=%d G=%d B=%d A=%d L=%d I=%d", r, g, b, a, l, i);
362 glRasterPos2i(10, 25);
363 PrintString(s);
364 }
365
Brian Paul215ff222000-01-28 16:25:44 +0000366 glutSwapBuffers();
367 }
368}
369
370
371static void
372Reshape(int width, int height)
373{
374 glViewport( 0, 0, width, height );
375 glMatrixMode( GL_PROJECTION );
376 glLoadIdentity();
377 glOrtho(0, width, 0, height, -1, 1);
378 glMatrixMode( GL_MODELVIEW );
379 glLoadIdentity();
380}
381
382
Brian Paul215ff222000-01-28 16:25:44 +0000383
384static void
385Key(unsigned char key, int x, int y)
386{
387 (void) x;
388 (void) y;
389 switch (key) {
390 case ' ':
Brian Paul8fd9fcb2000-03-29 18:02:52 +0000391 Mode = 0;
Brian Paul215ff222000-01-28 16:25:44 +0000392 break;
393 case 'b':
394 /* toggle border */
395 TexBorder = 1 - TexBorder;
396 Mode = 0;
397 break;
398 case 'f':
399 /* change format */
Brian Paul7d69e9e2000-03-29 15:47:48 +0000400 Format = (Format + 1) % NUM_FORMATS;
Brian Paul215ff222000-01-28 16:25:44 +0000401 Mode = 0;
402 break;
Keith Whitwell46d50d92005-03-22 13:32:35 +0000403 case 'F':
404 /* change format */
405 Format = (Format - 1) % NUM_FORMATS;
406 Mode = 0;
407 break;
Brian Paul215ff222000-01-28 16:25:44 +0000408 case 'p':
409 /* toggle border */
410 ScaleAndBias = !ScaleAndBias;
411 Mode = 0;
412 break;
413 case 's':
414 SubImage = !SubImage;
415 Mode = 0;
416 break;
Brian Paul215ff222000-01-28 16:25:44 +0000417 case 27:
418 exit(0);
419 break;
420 }
421 glutPostRedisplay();
422}
423
424
425static void
426SpecialKey(int key, int x, int y)
427{
428 (void) x;
429 (void) y;
430 switch (key) {
431 case GLUT_KEY_UP:
432 if (TexHeight < MaxSize)
433 TexHeight *= 2;
434 break;
435 case GLUT_KEY_DOWN:
436 if (TexHeight > 1)
437 TexHeight /= 2;
438 break;
439 case GLUT_KEY_LEFT:
440 if (TexWidth > 1)
441 TexWidth /= 2;
442 break;
443 case GLUT_KEY_RIGHT:
444 if (TexWidth < MaxSize)
445 TexWidth *= 2;
446 break;
447 }
448 Mode = 0;
449 glutPostRedisplay();
450}
451
452
453static void
454Init(void)
455{
456 printf("GL_VENDOR = %s\n", (const char *) glGetString(GL_VENDOR));
457 printf("GL_VERSION = %s\n", (const char *) glGetString(GL_VERSION));
458 printf("GL_RENDERER = %s\n", (const char *) glGetString(GL_RENDERER));
459}
460
461
462int
463main(int argc, char *argv[])
464{
465 glutInit( &argc, argv );
466 glutInitWindowPosition( 0, 0 );
467 glutInitWindowSize( 600, 100 );
468 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
469 glutCreateWindow(argv[0]);
470 glutReshapeFunc( Reshape );
471 glutKeyboardFunc( Key );
472 glutSpecialFunc( SpecialKey );
473 glutDisplayFunc( Display );
474 Init();
475 glutMainLoop();
476 return 0;
477}