1 /* $Id: texdown.c,v 1.3 2000/03/29 18:02:52 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 GLboolean ScaleAndBias
= GL_FALSE
;
45 static GLboolean SubImage
= GL_FALSE
;
46 static GLdouble DownloadRate
= 0.0; /* texels/sec */
48 static GLuint Mode
= 0;
60 static const struct FormatRec FormatTable
[NUM_FORMATS
] = {
61 /* Format Type IntFormat TexelSize */
62 { GL_RGB
, GL_UNSIGNED_BYTE
, GL_RGB
, 3 },
63 { GL_RGBA
, GL_UNSIGNED_BYTE
, GL_RGBA
, 4 },
64 { GL_RGBA
, GL_UNSIGNED_BYTE
, GL_RGB
, 4 },
65 { GL_RGB
, GL_UNSIGNED_SHORT_5_6_5
, GL_RGB
, 2 },
71 BytesPerTexel(GLint format
)
73 return FormatTable
[format
].TexelSize
;
78 FormatStr(GLenum format
)
95 case GL_UNSIGNED_BYTE
:
96 return "GL_UNSIGNED_BYTE";
97 case GL_UNSIGNED_SHORT
:
98 return "GL_UNSIGNED_SHORT";
99 case GL_UNSIGNED_SHORT_5_6_5
:
100 return "GL_UNSIGNED_SHORT_5_6_5";
101 case GL_UNSIGNED_SHORT_5_6_5_REV
:
102 return "GL_UNSIGNED_SHORT_5_6_5_REV";
110 MeasureDownloadRate(void)
112 const int w
= TexWidth
+ 2 * TexBorder
;
113 const int h
= TexHeight
+ 2 * TexBorder
;
114 const int bytes
= w
* h
* BytesPerTexel(Format
);
115 GLubyte
*texImage
, *getImage
;
116 GLdouble t0
, t1
, time
;
120 texImage
= (GLubyte
*) malloc(bytes
);
121 getImage
= (GLubyte
*) malloc(bytes
);
122 if (!texImage
|| !getImage
) {
127 for (i
= 0; i
< bytes
; i
++) {
128 texImage
[i
] = i
& 0xff;
131 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
132 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
135 glPixelTransferf(GL_RED_SCALE
, 0.5);
136 glPixelTransferf(GL_GREEN_SCALE
, 0.5);
137 glPixelTransferf(GL_BLUE_SCALE
, 0.5);
138 glPixelTransferf(GL_RED_BIAS
, 0.5);
139 glPixelTransferf(GL_GREEN_BIAS
, 0.5);
140 glPixelTransferf(GL_BLUE_BIAS
, 0.5);
143 glPixelTransferf(GL_RED_SCALE
, 1.0);
144 glPixelTransferf(GL_GREEN_SCALE
, 1.0);
145 glPixelTransferf(GL_BLUE_SCALE
, 1.0);
146 glPixelTransferf(GL_RED_BIAS
, 0.0);
147 glPixelTransferf(GL_GREEN_BIAS
, 0.0);
148 glPixelTransferf(GL_BLUE_BIAS
, 0.0);
152 t0
= glutGet(GLUT_ELAPSED_TIME
) * 0.001;
154 if (SubImage
&& count
> 0) {
155 glTexSubImage2D(GL_TEXTURE_2D
, 0, -TexBorder
, -TexBorder
, w
, h
,
156 FormatTable
[Format
].Format
,
157 FormatTable
[Format
].Type
, texImage
);
160 glTexImage2D(GL_TEXTURE_2D
, 0,
161 FormatTable
[Format
].IntFormat
, w
, h
, TexBorder
,
162 FormatTable
[Format
].Format
,
163 FormatTable
[Format
].Type
, texImage
);
167 /* draw a tiny polygon to force texture into texram */
168 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
169 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
170 glEnable(GL_TEXTURE_2D
);
171 glBegin(GL_TRIANGLES
);
172 glTexCoord2f(0, 0); glVertex2f(1, 1);
173 glTexCoord2f(1, 0); glVertex2f(3, 1);
174 glTexCoord2f(0.5, 1); glVertex2f(2, 3);
176 glDisable(GL_TEXTURE_2D
);
179 t1
= glutGet(GLUT_ELAPSED_TIME
) * 0.001;
182 } while (time
< 3.0);
184 printf("w*h=%d count=%d time=%f\n", w
*h
, count
, time
);
185 DownloadRate
= w
* h
* count
/ time
;
189 /* verify texture readback */
190 glGetTexImage(GL_TEXTURE_2D
, 0,
191 FormatTable
[Format
].Format
,
192 FormatTable
[Format
].Type
, getImage
);
193 for (i
= 0; i
< w
* h
; i
++) {
194 if (texImage
[i
] != getImage
[i
]) {
195 printf("[%d] %d != %d\n", i
, texImage
[i
], getImage
[i
]);
205 GLint err
= glGetError();
207 printf("GL error %d\n", err
);
213 PrintString(const char *s
)
216 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
225 const int w
= TexWidth
+ 2 * TexBorder
;
226 const int h
= TexHeight
+ 2 * TexBorder
;
229 glClear(GL_COLOR_BUFFER_BIT
);
231 glRasterPos2i(10, 80);
232 sprintf(s
, "Texture size[cursor]: %d x %d Border[b]: %d", w
, h
, TexBorder
);
235 glRasterPos2i(10, 65);
236 sprintf(s
, "Format[f]: %s Type: %s IntFormat: %s",
237 FormatStr(FormatTable
[Format
].Format
),
238 TypeStr( FormatTable
[Format
].Type
),
239 FormatStr(FormatTable
[Format
].IntFormat
));
242 glRasterPos2i(10, 50);
243 sprintf(s
, "Pixel Scale&Bias[p]: %s TexSubImage[s]: %s",
244 ScaleAndBias
? "Yes" : "No",
245 SubImage
? "Yes" : "No");
249 glRasterPos2i(200, 10);
250 sprintf(s
, "...Measuring...");
256 else if (Mode
== 1) {
257 MeasureDownloadRate();
263 glRasterPos2i(10, 10);
264 sprintf(s
, "Download rate: %g Mtexels/second %g MB/second",
265 DownloadRate
/ 1000000.0,
266 DownloadRate
* BytesPerTexel(Format
) / 1000000.0);
269 GLint r
, g
, b
, a
, l
, i
;
270 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_RED_SIZE
, &r
);
271 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_GREEN_SIZE
, &g
);
272 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_BLUE_SIZE
, &b
);
273 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_ALPHA_SIZE
, &a
);
274 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_LUMINANCE_SIZE
, &l
);
275 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_INTENSITY_SIZE
, &i
);
276 sprintf(s
, "TexelBits: R=%d G=%d B=%d A=%d L=%d I=%d", r
, g
, b
, a
, l
, i
);
277 glRasterPos2i(10, 25);
287 Reshape(int width
, int height
)
289 glViewport( 0, 0, width
, height
);
290 glMatrixMode( GL_PROJECTION
);
292 glOrtho(0, width
, 0, height
, -1, 1);
293 glMatrixMode( GL_MODELVIEW
);
300 Key(unsigned char key
, int x
, int y
)
310 TexBorder
= 1 - TexBorder
;
315 Format
= (Format
+ 1) % NUM_FORMATS
;
320 ScaleAndBias
= !ScaleAndBias
;
324 SubImage
= !SubImage
;
336 SpecialKey(int key
, int x
, int y
)
342 if (TexHeight
< MaxSize
)
354 if (TexWidth
< MaxSize
)
366 printf("GL_VENDOR = %s\n", (const char *) glGetString(GL_VENDOR
));
367 printf("GL_VERSION = %s\n", (const char *) glGetString(GL_VERSION
));
368 printf("GL_RENDERER = %s\n", (const char *) glGetString(GL_RENDERER
));
373 main(int argc
, char *argv
[])
375 glutInit( &argc
, argv
);
376 glutInitWindowPosition( 0, 0 );
377 glutInitWindowSize( 600, 100 );
378 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
379 glutCreateWindow(argv
[0]);
380 glutReshapeFunc( Reshape
);
381 glutKeyboardFunc( Key
);
382 glutSpecialFunc( SpecialKey
);
383 glutDisplayFunc( Display
);