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_wrap.h"
39 #include "glut_wrap.h"
40 
41 
42 static GLsizei MaxSize = 2048;
43 static GLsizei TexWidth = 1024, TexHeight = 1024, TexBorder = 0;
44 static GLboolean ScaleAndBias = GL_FALSE;
45 static GLboolean SubImage = GL_FALSE;
46 static GLdouble DownloadRate = 0.0;  /* texels/sec */
47 
48 static GLuint Mode = 0;
49 
50 
51 /* Try and avoid L2 cache effects by cycling through a small number of
52  * textures.
53  *
54  * At the initial size of 1024x1024x4 == 4mbyte, say 8 textures will
55  * keep us out of most caches at 32mb total.
56  *
57  * This turns into a fairly interesting question of what exactly you
58  * expect to be in cache in normal usage, and what you think should be
59  * outside.  There's no rules for this, no reason to favour one usage
60  * over another except what the application you care about happens to
61  * resemble most closely.
62  *
63  * - Should the client texture image be in L2 cache?  Has it just been
64  *   generated or read from disk?
65  * - Does the application really use >1 texture, or is it constantly
66  *   updating one image in-place?
67  *
68  * Different answers will favour different texture upload mechanisms.
69  * To upload an image that is purely outside of cache, a DMA-based
70  * upload will probably win, whereas for small, in-cache textures,
71  * copying looks good.
72  */
73 #define NR_TEXOBJ 4
74 static GLuint TexObj[NR_TEXOBJ];
75 
76 
77 struct FormatRec {
78    GLenum Format;
79    GLenum Type;
80    GLenum IntFormat;
81    GLint TexelSize;
82 };
83 
84 
85 static const struct FormatRec FormatTable[] = {
86   /* Format   Type                         IntFormat   TexelSize */
87    { GL_BGRA, GL_UNSIGNED_BYTE,            GL_RGBA,        4    },
88    { GL_RGB,  GL_UNSIGNED_BYTE,            GL_RGB,         3    },
89    { GL_RGBA, GL_UNSIGNED_BYTE,            GL_RGBA,        4    },
90    { GL_RGBA, GL_UNSIGNED_BYTE,            GL_RGB,         4    },
91    { GL_RGB,  GL_UNSIGNED_SHORT_5_6_5,     GL_RGB,         2    },
92    { GL_LUMINANCE, GL_UNSIGNED_BYTE,       GL_LUMINANCE,   1    },
93    { GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, GL_LUMINANCE_ALPHA,   2    },
94    { GL_ALPHA, GL_UNSIGNED_BYTE,           GL_ALPHA,       1    },
95 };
96 static GLint Format;
97 
98 #define NUM_FORMATS (sizeof(FormatTable)/sizeof(FormatTable[0]))
99 
100 static int
BytesPerTexel(GLint format)101 BytesPerTexel(GLint format)
102 {
103    return FormatTable[format].TexelSize;
104 }
105 
106 
107 static const char *
FormatStr(GLenum format)108 FormatStr(GLenum format)
109 {
110    switch (format) {
111       case GL_RGB:
112          return "GL_RGB";
113       case GL_RGBA:
114          return "GL_RGBA";
115       case GL_BGRA:
116          return "GL_BGRA";
117       case GL_LUMINANCE:
118          return "GL_LUMINANCE";
119       case GL_LUMINANCE_ALPHA:
120          return "GL_LUMINANCE_ALPHA";
121       case GL_ALPHA:
122          return "GL_ALPHA";
123       default:
124          return "";
125    }
126 }
127 
128 
129 static const char *
TypeStr(GLenum type)130 TypeStr(GLenum type)
131 {
132    switch (type) {
133       case GL_UNSIGNED_BYTE:
134          return "GL_UNSIGNED_BYTE";
135       case GL_UNSIGNED_SHORT:
136          return "GL_UNSIGNED_SHORT";
137       case GL_UNSIGNED_SHORT_5_6_5:
138          return "GL_UNSIGNED_SHORT_5_6_5";
139       case GL_UNSIGNED_SHORT_5_6_5_REV:
140          return "GL_UNSIGNED_SHORT_5_6_5_REV";
141       default:
142          return "";
143    }
144 }
145 
146 /* On x86, there is a performance cliff for memcpy to texture memory
147  * for sources below 64 byte alignment.  We do our best with this in
148  * the driver, but it is better if the images are correctly aligned to
149  * start with:
150  */
151 #define ALIGN (1<<12)
152 
align(unsigned long value,unsigned long a)153 static unsigned long align(unsigned long value, unsigned long a)
154 {
155    return (value + a - 1) & ~(a-1);
156 }
157 
158 static void
MeasureDownloadRate(void)159 MeasureDownloadRate(void)
160 {
161    const int w = TexWidth + 2 * TexBorder;
162    const int h = TexHeight + 2 * TexBorder;
163    const int image_bytes = align(w * h * BytesPerTexel(Format), ALIGN);
164    const int bytes = image_bytes * NR_TEXOBJ;
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 #ifdef _WIN32
176    texImage = (GLubyte *) _aligned_malloc(bytes, ALIGN);
177 #else
178    texImage = (GLubyte *) aligned_alloc(ALIGN, bytes);
179 #endif
180    if (!texImage) {
181       DownloadRate = 0.0;
182       return;
183    }
184 
185    printf("alloc %p\n", texImage);
186 
187    for (i = 1; !(((unsigned long)texImage) & i); i<<=1)
188       ;
189    printf("texture image alignment: %d bytes (%p)\n", i, texImage);
190 
191    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 
215    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 
219    count = 0;
220    t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
221    do {
222       int img = count%NR_TEXOBJ;
223       GLubyte *img_ptr = texImage + img * image_bytes;
224 
225       glBindTexture(GL_TEXTURE_2D, TexObj[img]);
226 
227       if (SubImage && count > 0) {
228 	 /* 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,
247                          FormatTable[Format].Format,
248                          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;
258       }
259       else {
260          glTexImage2D(GL_TEXTURE_2D, 0,
261                       FormatTable[Format].IntFormat, w, h, TexBorder,
262                       FormatTable[Format].Format,
263                       FormatTable[Format].Type,
264 		      img_ptr);
265 	 total += w*h;
266       }
267 
268       /* 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();
274 
275       t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
276       time = t1 - t0;
277       count++;
278    } while (time < 3.0);
279 
280    glDisable(GL_TEXTURE_2D);
281 
282    printf("total texels=%f  time=%f\n", total, time);
283    DownloadRate = total / time;
284 
285 
286 #ifdef _WIN32
287    _aligned_free(texImage);
288 #else
289    free(texImage);
290 #endif
291 
292    {
293       GLint err = glGetError();
294       if (err)
295          printf("GL error %d\n", err);
296    }
297 }
298 
299 
300 static void
PrintString(const char * s)301 PrintString(const char *s)
302 {
303    while (*s) {
304       glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
305       s++;
306    }
307 }
308 
309 
310 static void
Display(void)311 Display(void)
312 {
313    const int w = TexWidth + 2 * TexBorder;
314    const int h = TexHeight + 2 * TexBorder;
315    char s[1000];
316 
317    glClear(GL_COLOR_BUFFER_BIT);
318 
319    glRasterPos2i(10, 80);
320    sprintf(s, "Texture size[cursor]: %d x %d  Border[b]: %d", w, h, TexBorder);
321    PrintString(s);
322 
323    glRasterPos2i(10, 65);
324    sprintf(s, "Format[f]: %s  Type: %s  IntFormat: %s",
325            FormatStr(FormatTable[Format].Format),
326            TypeStr(  FormatTable[Format].Type),
327            FormatStr(FormatTable[Format].IntFormat));
328    PrintString(s);
329 
330    glRasterPos2i(10, 50);
331    sprintf(s, "Pixel Scale&Bias[p]: %s   TexSubImage[s]: %s",
332            ScaleAndBias ? "Yes" : "No",
333            SubImage ? "Yes" : "No");
334    PrintString(s);
335 
336    if (Mode == 0) {
337       glRasterPos2i(200, 10);
338       sprintf(s, "...Measuring...");
339       PrintString(s);
340       glutSwapBuffers();
341       glutPostRedisplay();
342       Mode++;
343    }
344    else if (Mode == 1) {
345       MeasureDownloadRate();
346       glutPostRedisplay();
347       Mode++;
348    }
349    else {
350       /* show results */
351       glRasterPos2i(10, 10);
352       sprintf(s, "Download rate: %g Mtexels/second  %g MB/second",
353               DownloadRate / 1000000.0,
354               DownloadRate * BytesPerTexel(Format) / 1000000.0);
355       PrintString(s);
356       {
357          GLint r, g, b, a, l, i;
358          glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_RED_SIZE, &r);
359          glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_SIZE, &g);
360          glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_SIZE, &b);
361          glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_SIZE, &a);
362          glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_LUMINANCE_SIZE, &l);
363          glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTENSITY_SIZE, &i);
364          sprintf(s, "TexelBits: R=%d G=%d B=%d A=%d L=%d I=%d", r, g, b, a, l, i);
365          glRasterPos2i(10, 25);
366          PrintString(s);
367       }
368 
369       glutSwapBuffers();
370    }
371 }
372 
373 
374 static void
Reshape(int width,int height)375 Reshape(int width, int height)
376 {
377    glViewport( 0, 0, width, height );
378    glMatrixMode( GL_PROJECTION );
379    glLoadIdentity();
380    glOrtho(0, width, 0, height, -1, 1);
381    glMatrixMode( GL_MODELVIEW );
382    glLoadIdentity();
383 }
384 
385 
386 
387 static void
Key(unsigned char key,int x,int y)388 Key(unsigned char key, int x, int y)
389 {
390    (void) x;
391    (void) y;
392    switch (key) {
393       case ' ':
394          Mode = 0;
395          break;
396       case 'b':
397          /* toggle border */
398          TexBorder = 1 - TexBorder;
399          Mode = 0;
400          break;
401       case 'f':
402          /* change format */
403          Format = (Format + 1) % NUM_FORMATS;
404          Mode = 0;
405          break;
406       case 'F':
407          /* change format */
408          Format = (Format - 1) % NUM_FORMATS;
409          Mode = 0;
410          break;
411       case 'p':
412          /* toggle border */
413          ScaleAndBias = !ScaleAndBias;
414          Mode = 0;
415          break;
416       case 's':
417          SubImage = !SubImage;
418          Mode = 0;
419          break;
420       case 27:
421          exit(0);
422          break;
423    }
424    glutPostRedisplay();
425 }
426 
427 
428 static void
SpecialKey(int key,int x,int y)429 SpecialKey(int key, int x, int y)
430 {
431    (void) x;
432    (void) y;
433    switch (key) {
434       case GLUT_KEY_UP:
435          if (TexHeight < MaxSize)
436             TexHeight *= 2;
437          break;
438       case GLUT_KEY_DOWN:
439          if (TexHeight > 1)
440             TexHeight /= 2;
441          break;
442       case GLUT_KEY_LEFT:
443          if (TexWidth > 1)
444             TexWidth /= 2;
445          break;
446       case GLUT_KEY_RIGHT:
447          if (TexWidth < MaxSize)
448             TexWidth *= 2;
449          break;
450    }
451    Mode = 0;
452    glutPostRedisplay();
453 }
454 
455 
456 static void
Init(void)457 Init(void)
458 {
459    printf("GL_VENDOR = %s\n", (const char *) glGetString(GL_VENDOR));
460    printf("GL_VERSION = %s\n", (const char *) glGetString(GL_VERSION));
461    printf("GL_RENDERER = %s\n", (const char *) glGetString(GL_RENDERER));
462 }
463 
464 
465 int
main(int argc,char * argv[])466 main(int argc, char *argv[])
467 {
468    glutInit( &argc, argv );
469    glutInitWindowPosition( 0, 0 );
470    glutInitWindowSize( 600, 100 );
471    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
472    glutCreateWindow(argv[0]);
473    glutReshapeFunc( Reshape );
474    glutKeyboardFunc( Key );
475    glutSpecialFunc( SpecialKey );
476    glutDisplayFunc( Display );
477    Init();
478    glutMainLoop();
479    return 0;
480 }
481