c9a4e2630f87d940f90592f9fb2c527eef8b8d79
[mesa.git] / progs / demos / clearspd.c
1 /* $Id: clearspd.c,v 1.5 2002/10/31 12:38:32 keithw Exp $ */
2
3 /*
4 * Simple GLUT program to measure glClear() and glutSwapBuffers() speed.
5 * Brian Paul February 15, 1997 This file in public domain.
6 */
7
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <math.h>
12 #include <string.h>
13 #include <GL/glut.h>
14
15
16 static float MinPeriod = 2.0; /* 2 seconds */
17 static int ColorMode = GLUT_RGB;
18 static int Width = 400.0;
19 static int Height = 400.0;
20 static int Loops = 100;
21 static float ClearColor = 0.0;
22 static GLbitfield BufferMask = GL_COLOR_BUFFER_BIT;
23 static GLboolean SwapFlag = GL_FALSE;
24
25
26
27 static void Idle( void )
28 {
29 glutPostRedisplay();
30 }
31
32
33 static void Display( void )
34 {
35 double t0, t1;
36 double clearRate;
37 double pixelRate;
38 int i;
39
40 glClearColor( ClearColor, ClearColor, ClearColor, 0.0 );
41 ClearColor += 0.1;
42 if (ClearColor>1.0)
43 ClearColor = 0.0;
44
45 if (SwapFlag) {
46 t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
47 for (i=0;i<Loops;i++) {
48 glClear( BufferMask );
49 glutSwapBuffers();
50 }
51 glFinish();
52 t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
53 }
54 else {
55 t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
56 for (i=0;i<Loops;i++) {
57 glClear( BufferMask );
58 }
59 glFinish();
60 t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
61 glutSwapBuffers();
62 }
63
64 /* NOTE: If clearspd doesn't map it's window immediately on
65 * starting, swaps will be istantaneous, so this will send Loops
66 * towards infinity. When a window is finally mapped, it may be
67 * minutes before the first call to glutSwapBuffers, making it look
68 * like there's a driver bug.
69 */
70 if (t1-t0 < MinPeriod) {
71 /* Next time do more clears to get longer elapsed time */
72 Loops *= 2;
73 return;
74 }
75
76 clearRate = Loops / (t1-t0);
77 pixelRate = clearRate * Width * Height;
78 if (SwapFlag) {
79 printf("Rate: %d clears+swaps in %gs = %g clears+swaps/s %g pixels/s\n",
80 Loops, t1-t0, clearRate, pixelRate );
81 }
82 else {
83 printf("Rate: %d clears in %gs = %g clears/s %g pixels/s\n",
84 Loops, t1-t0, clearRate, pixelRate);
85 }
86 }
87
88
89 static void Reshape( int width, int height )
90 {
91 Width = width;
92 Height = height;
93 glViewport( 0, 0, width, height );
94 glMatrixMode( GL_PROJECTION );
95 glLoadIdentity();
96 glOrtho(0.0, width, 0.0, height, -1.0, 1.0);
97 glMatrixMode( GL_MODELVIEW );
98 glLoadIdentity();
99 }
100
101
102 static void Key( unsigned char key, int x, int y )
103 {
104 (void) x;
105 (void) y;
106 switch (key) {
107 case 27:
108 exit(0);
109 break;
110 }
111 glutPostRedisplay();
112 }
113
114
115 static void Init( int argc, char *argv[] )
116 {
117 int i;
118 for (i=1; i<argc; i++) {
119 if (strcmp(argv[i],"+rgb")==0)
120 ColorMode = GLUT_RGB;
121 else if (strcmp(argv[i],"+ci")==0)
122 ColorMode = GLUT_INDEX;
123 else if (strcmp(argv[i],"-color")==0)
124 BufferMask = 0;
125 else if (strcmp(argv[i],"+depth")==0)
126 BufferMask |= GL_DEPTH_BUFFER_BIT;
127 else if (strcmp(argv[i],"+alpha")==0)
128 ColorMode = GLUT_RGB | GLUT_ALPHA;
129 else if (strcmp(argv[i],"+stencil")==0)
130 BufferMask |= GL_STENCIL_BUFFER_BIT;
131 else if (strcmp(argv[i],"+accum")==0)
132 BufferMask |= GL_ACCUM_BUFFER_BIT;
133 else if (strcmp(argv[i],"-width")==0) {
134 Width = atoi(argv[i+1]);
135 i++;
136 }
137 else if (strcmp(argv[i],"-height")==0) {
138 Height = atoi(argv[i+1]);
139 i++;
140 }
141 else if (strcmp(argv[i],"+swap")==0) {
142 SwapFlag = GL_TRUE;
143 }
144 else if (strcmp(argv[i],"-swap")==0) {
145 SwapFlag = GL_FALSE;
146 }
147 else
148 printf("Unknown option: %s\n", argv[i]);
149 }
150
151 if (ColorMode & GLUT_ALPHA)
152 printf("Mode: RGB + Alpha\n");
153 else if (ColorMode==GLUT_RGB)
154 printf("Mode: RGB\n");
155 else
156 printf("Mode: Color Index\n");
157 printf("SwapBuffers: %s\n", SwapFlag ? "yes" : "no" );
158 printf("Size: %d x %d\n", Width, Height);
159 printf("Buffers: ");
160 if (BufferMask & GL_COLOR_BUFFER_BIT) printf("color ");
161 if (BufferMask & GL_DEPTH_BUFFER_BIT) printf("depth ");
162 if (BufferMask & GL_STENCIL_BUFFER_BIT) printf("stencil ");
163 if (BufferMask & GL_ACCUM_BUFFER_BIT) printf("accum ");
164 printf("\n");
165 }
166
167
168 static void Help( const char *program )
169 {
170 printf("%s options:\n", program);
171 printf(" +rgb RGB mode\n");
172 printf(" +ci color index mode\n");
173 printf(" -color don't clear color buffer\n");
174 printf(" +alpha clear alpha buffer\n");
175 printf(" +depth clear depth buffer\n");
176 printf(" +stencil clear stencil buffer\n");
177 printf(" +accum clear accum buffer\n");
178 printf(" +swap also do SwapBuffers\n");
179 printf(" -swap don't do SwapBuffers\n");
180 }
181
182
183 int main( int argc, char *argv[] )
184 {
185 GLint mode;
186
187 printf("For options: %s -help\n", argv[0]);
188
189 Init( argc, argv );
190
191 glutInit( &argc, argv );
192 glutInitWindowSize( (int) Width, (int) Height );
193 glutInitWindowPosition( 0, 0 );
194
195 mode = ColorMode | GLUT_DOUBLE;
196 if (BufferMask & GL_STENCIL_BUFFER_BIT)
197 mode |= GLUT_STENCIL;
198 if (BufferMask & GL_ACCUM_BUFFER_BIT)
199 mode |= GLUT_ACCUM;
200 if (BufferMask & GL_DEPTH_BUFFER_BIT)
201 mode |= GLUT_DEPTH;
202
203 glutInitDisplayMode(mode);
204
205 glutCreateWindow( argv[0] );
206
207 if (argc==2 && strcmp(argv[1],"-help")==0) {
208 Help(argv[0]);
209 return 0;
210 }
211
212 glutReshapeFunc( Reshape );
213 glutKeyboardFunc( Key );
214 glutDisplayFunc( Display );
215 glutIdleFunc( Idle );
216
217 glutMainLoop();
218 return 0;
219 }