1 /* $Id: texdown.c,v 1.1 2000/01/28 16:25:44 brianp Exp $ */
4 * Copyright (C) 1999 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Measure texture download speed.
29 * Use keyboard to change texture size, format, datatype, scale/bias,
32 * Brian Paul 28 January 2000
42 static GLsizei MaxSize
= 1024;
43 static GLsizei TexWidth
= 256, TexHeight
= 256, TexBorder
= 0;
44 static GLenum TexFormat
= GL_RGBA
, TexType
= GL_UNSIGNED_BYTE
;
45 static GLenum TexIntFormat
= GL_RGBA
;
46 static GLboolean ScaleAndBias
= GL_FALSE
;
47 static GLboolean SubImage
= GL_FALSE
;
48 static GLdouble DownloadRate
= 0.0; /* texels/sec */
50 static GLuint Mode
= 0;
54 BytesPerTexel(GLenum format
, GLenum type
)
59 case GL_UNSIGNED_BYTE
:
62 case GL_UNSIGNED_SHORT
:
85 FormatStr(GLenum format
)
102 case GL_UNSIGNED_BYTE
:
104 case GL_UNSIGNED_SHORT
:
113 MeasureDownloadRate(void)
115 const int w
= TexWidth
+ 2 * TexBorder
;
116 const int h
= TexHeight
+ 2 * TexBorder
;
117 const int bytes
= w
* h
* BytesPerTexel(TexFormat
, TexType
);
118 GLubyte
*texImage
, *getImage
;
119 GLdouble t0
, t1
, time
;
123 texImage
= (GLubyte
*) malloc(bytes
);
124 getImage
= (GLubyte
*) malloc(bytes
);
125 if (!texImage
|| !getImage
) {
130 for (i
= 0; i
< bytes
; i
++) {
131 texImage
[i
] = i
& 0xff;
134 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
135 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
138 glPixelTransferf(GL_RED_SCALE
, 0.5);
139 glPixelTransferf(GL_GREEN_SCALE
, 0.5);
140 glPixelTransferf(GL_BLUE_SCALE
, 0.5);
141 glPixelTransferf(GL_RED_BIAS
, 0.5);
142 glPixelTransferf(GL_GREEN_BIAS
, 0.5);
143 glPixelTransferf(GL_BLUE_BIAS
, 0.5);
146 glPixelTransferf(GL_RED_SCALE
, 1.0);
147 glPixelTransferf(GL_GREEN_SCALE
, 1.0);
148 glPixelTransferf(GL_BLUE_SCALE
, 1.0);
149 glPixelTransferf(GL_RED_BIAS
, 0.0);
150 glPixelTransferf(GL_GREEN_BIAS
, 0.0);
151 glPixelTransferf(GL_BLUE_BIAS
, 0.0);
155 t0
= glutGet(GLUT_ELAPSED_TIME
) * 0.001;
157 if (SubImage
&& count
> 0) {
158 glTexSubImage2D(GL_TEXTURE_2D
, 0, -TexBorder
, -TexBorder
, w
, h
,
159 TexFormat
, TexType
, texImage
);
162 glTexImage2D(GL_TEXTURE_2D
, 0, TexIntFormat
, w
, h
,
163 TexBorder
, TexFormat
, TexType
, texImage
);
166 t1
= glutGet(GLUT_ELAPSED_TIME
) * 0.001;
169 } while (time
< 3.0);
171 printf("w*h=%d count=%d time=%f\n", w
*h
, count
, time
);
172 DownloadRate
= w
* h
* count
/ time
;
176 /* verify texture readback */
177 glGetTexImage(GL_TEXTURE_2D
, 0, TexFormat
, TexType
, getImage
);
178 for (i
= 0; i
< w
* h
; i
++) {
179 if (texImage
[i
] != getImage
[i
]) {
180 printf("[%d] %d != %d\n", i
, texImage
[i
], getImage
[i
]);
190 GLint err
= glGetError();
192 printf("GL error %d\n", err
);
198 PrintString(const char *s
)
201 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
210 const int w
= TexWidth
+ 2 * TexBorder
;
211 const int h
= TexHeight
+ 2 * TexBorder
;
214 glClear(GL_COLOR_BUFFER_BIT
);
216 glRasterPos2i(10, 70);
217 sprintf(s
, "Texture size[cursor]: %d x %d Border[b]: %d", w
, h
, TexBorder
);
220 glRasterPos2i(10, 55);
221 sprintf(s
, "Texture format[f]: %s Type[t]: %s IntFormat[i]: %s",
222 FormatStr(TexFormat
), TypeStr(TexType
), FormatStr(TexIntFormat
));
225 glRasterPos2i(10, 40);
226 sprintf(s
, "Pixel Scale&Bias[p]: %s TexSubImage[s]: %s",
227 ScaleAndBias
? "Yes" : "No",
228 SubImage
? "Yes" : "No");
232 glRasterPos2i(200, 10);
233 sprintf(s
, "...Measuring...");
239 else if (Mode
== 1) {
240 MeasureDownloadRate();
246 glRasterPos2i(10, 10);
247 sprintf(s
, "Download rate: %g Mtexels/second %g MB/second",
248 DownloadRate
/ 1000000.0,
249 DownloadRate
* BytesPerTexel(TexFormat
, TexType
) / 1000000.0);
257 Reshape(int width
, int height
)
259 glViewport( 0, 0, width
, height
);
260 glMatrixMode( GL_PROJECTION
);
262 glOrtho(0, width
, 0, height
, -1, 1);
263 glMatrixMode( GL_MODELVIEW
);
269 NextFormat(GLenum format
)
271 if (format
== GL_RGB
) {
274 else if (format
== GL_RGBA
) {
284 NextType(GLenum type
)
286 if (type
== GL_UNSIGNED_BYTE
) {
287 return GL_UNSIGNED_SHORT
;
289 else if (type
== GL_UNSIGNED_SHORT
) {
290 return GL_UNSIGNED_BYTE
;
293 return GL_UNSIGNED_SHORT
;
299 Key(unsigned char key
, int x
, int y
)
308 TexBorder
= 1 - TexBorder
;
313 TexFormat
= NextFormat(TexFormat
);
317 /* change internal format */
318 TexIntFormat
= NextFormat(TexIntFormat
);
323 ScaleAndBias
= !ScaleAndBias
;
327 SubImage
= !SubImage
;
332 TexType
= NextType(TexType
);
344 SpecialKey(int key
, int x
, int y
)
350 if (TexHeight
< MaxSize
)
362 if (TexWidth
< MaxSize
)
374 printf("GL_VENDOR = %s\n", (const char *) glGetString(GL_VENDOR
));
375 printf("GL_VERSION = %s\n", (const char *) glGetString(GL_VERSION
));
376 printf("GL_RENDERER = %s\n", (const char *) glGetString(GL_RENDERER
));
381 main(int argc
, char *argv
[])
383 glutInit( &argc
, argv
);
384 glutInitWindowPosition( 0, 0 );
385 glutInitWindowSize( 600, 100 );
386 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
387 glutCreateWindow(argv
[0]);
388 glutReshapeFunc( Reshape
);
389 glutKeyboardFunc( Key
);
390 glutSpecialFunc( SpecialKey
);
391 glutDisplayFunc( Display
);