e06a28e08f2e1665250fd9d6d31a7df5ae3773ce
[mesa.git] / progs / demos / clearspd.c
1 /* $Id: clearspd.c,v 1.3 2000/12/07 21:50:39 brianp 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 * $Log: clearspd.c,v $
10 * Revision 1.3 2000/12/07 21:50:39 brianp
11 * call glFinish() before getting t1 time
12 *
13 * Revision 1.2 2000/04/10 16:25:15 brianp
14 * fixed visual selection and reporting results
15 *
16 * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
17 * Imported sources
18 *
19 * Revision 3.3 1999/03/28 18:18:33 brianp
20 * minor clean-up
21 *
22 * Revision 3.2 1999/03/18 08:16:34 joukj
23 *
24 * cmpstr needs string.h to included to avoid warnings
25 *
26 * Revision 3.1 1998/06/29 02:38:30 brianp
27 * removed unneeded includes
28 *
29 * Revision 3.0 1998/02/14 18:42:29 brianp
30 * initial rev
31 *
32 */
33
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <math.h>
38 #include <string.h>
39 #include <GL/glut.h>
40
41
42 static float MinPeriod = 2.0; /* 2 seconds */
43 static int ColorMode = GLUT_RGB;
44 static int Width = 400.0;
45 static int Height = 400.0;
46 static int Loops = 100;
47 static float ClearColor = 0.0;
48 static GLbitfield BufferMask = GL_COLOR_BUFFER_BIT;
49 static GLboolean SwapFlag = GL_FALSE;
50
51
52
53 static void Idle( void )
54 {
55 glutPostRedisplay();
56 }
57
58
59 static void Display( void )
60 {
61 double t0, t1;
62 double clearRate;
63 double pixelRate;
64 int i;
65
66 glClearColor( ClearColor, ClearColor, ClearColor, 0.0 );
67 ClearColor += 0.1;
68 if (ClearColor>1.0)
69 ClearColor = 0.0;
70
71 if (SwapFlag) {
72 t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
73 for (i=0;i<Loops;i++) {
74 glClear( BufferMask );
75 glutSwapBuffers();
76 }
77 glFinish();
78 t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
79 }
80 else {
81 t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
82 for (i=0;i<Loops;i++) {
83 glClear( BufferMask );
84 }
85 glFinish();
86 t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
87 glutSwapBuffers();
88 }
89
90 if (t1-t0 < MinPeriod) {
91 /* Next time do more clears to get longer elapsed time */
92 Loops *= 2;
93 return;
94 }
95
96 clearRate = Loops / (t1-t0);
97 pixelRate = clearRate * Width * Height;
98 if (SwapFlag) {
99 printf("Rate: %d clears+swaps in %gs = %g clears+swaps/s %g pixels/s\n",
100 Loops, t1-t0, clearRate, pixelRate );
101 }
102 else {
103 printf("Rate: %d clears in %gs = %g clears/s %g pixels/s\n",
104 Loops, t1-t0, clearRate, pixelRate);
105 }
106 }
107
108
109 static void Reshape( int width, int height )
110 {
111 Width = width;
112 Height = height;
113 glViewport( 0, 0, width, height );
114 glMatrixMode( GL_PROJECTION );
115 glLoadIdentity();
116 glOrtho(0.0, width, 0.0, height, -1.0, 1.0);
117 glMatrixMode( GL_MODELVIEW );
118 glLoadIdentity();
119 }
120
121
122 static void Key( unsigned char key, int x, int y )
123 {
124 (void) x;
125 (void) y;
126 switch (key) {
127 case 27:
128 exit(0);
129 break;
130 }
131 glutPostRedisplay();
132 }
133
134
135 static void Init( int argc, char *argv[] )
136 {
137 int i;
138 for (i=1; i<argc; i++) {
139 if (strcmp(argv[i],"+rgb")==0)
140 ColorMode = GLUT_RGB;
141 else if (strcmp(argv[i],"+ci")==0)
142 ColorMode = GLUT_INDEX;
143 else if (strcmp(argv[i],"-color")==0)
144 BufferMask = 0;
145 else if (strcmp(argv[i],"+depth")==0)
146 BufferMask |= GL_DEPTH_BUFFER_BIT;
147 else if (strcmp(argv[i],"+alpha")==0)
148 ColorMode = GLUT_RGB | GLUT_ALPHA;
149 else if (strcmp(argv[i],"+stencil")==0)
150 BufferMask |= GL_STENCIL_BUFFER_BIT;
151 else if (strcmp(argv[i],"+accum")==0)
152 BufferMask |= GL_ACCUM_BUFFER_BIT;
153 else if (strcmp(argv[i],"-width")==0) {
154 Width = atoi(argv[i+1]);
155 i++;
156 }
157 else if (strcmp(argv[i],"-height")==0) {
158 Height = atoi(argv[i+1]);
159 i++;
160 }
161 else if (strcmp(argv[i],"+swap")==0) {
162 SwapFlag = GL_TRUE;
163 }
164 else if (strcmp(argv[i],"-swap")==0) {
165 SwapFlag = GL_FALSE;
166 }
167 else
168 printf("Unknown option: %s\n", argv[i]);
169 }
170
171 if (ColorMode & GLUT_ALPHA)
172 printf("Mode: RGB + Alpha\n");
173 else if (ColorMode==GLUT_RGB)
174 printf("Mode: RGB\n");
175 else
176 printf("Mode: Color Index\n");
177 printf("SwapBuffers: %s\n", SwapFlag ? "yes" : "no" );
178 printf("Size: %d x %d\n", Width, Height);
179 printf("Buffers: ");
180 if (BufferMask & GL_COLOR_BUFFER_BIT) printf("color ");
181 if (BufferMask & GL_DEPTH_BUFFER_BIT) printf("depth ");
182 if (BufferMask & GL_STENCIL_BUFFER_BIT) printf("stencil ");
183 if (BufferMask & GL_ACCUM_BUFFER_BIT) printf("accum ");
184 printf("\n");
185 }
186
187
188 static void Help( const char *program )
189 {
190 printf("%s options:\n", program);
191 printf(" +rgb RGB mode\n");
192 printf(" +ci color index mode\n");
193 printf(" -color don't clear color buffer\n");
194 printf(" +alpha clear alpha buffer\n");
195 printf(" +depth clear depth buffer\n");
196 printf(" +stencil clear stencil buffer\n");
197 printf(" +accum clear accum buffer\n");
198 printf(" +swap also do SwapBuffers\n");
199 printf(" -swap don't do SwapBuffers\n");
200 }
201
202
203 int main( int argc, char *argv[] )
204 {
205 GLint mode;
206
207 printf("For options: %s -help\n", argv[0]);
208
209 Init( argc, argv );
210
211 glutInit( &argc, argv );
212 glutInitWindowSize( (int) Width, (int) Height );
213 glutInitWindowPosition( 0, 0 );
214
215 mode = ColorMode | GLUT_DOUBLE;
216 if (BufferMask & GL_STENCIL_BUFFER_BIT)
217 mode |= GLUT_STENCIL;
218 if (BufferMask & GL_ACCUM_BUFFER_BIT)
219 mode |= GLUT_ACCUM;
220 if (BufferMask & GL_DEPTH_BUFFER_BIT)
221 mode |= GLUT_DEPTH;
222
223 glutInitDisplayMode(mode);
224
225 glutCreateWindow( argv[0] );
226
227 if (argc==2 && strcmp(argv[1],"-help")==0) {
228 Help(argv[0]);
229 return 0;
230 }
231
232 glutReshapeFunc( Reshape );
233 glutKeyboardFunc( Key );
234 glutDisplayFunc( Display );
235 glutIdleFunc( Idle );
236
237 glutMainLoop();
238 return 0;
239 }