706e698bd90363de00f30cb391273a09d584faad
[mesa.git] / progs / demos / clearspd.c
1 /* $Id: clearspd.c,v 1.4 2002/04/22 16:03:37 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 #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 if (t1-t0 < MinPeriod) {
65 /* Next time do more clears to get longer elapsed time */
66 Loops *= 2;
67 return;
68 }
69
70 clearRate = Loops / (t1-t0);
71 pixelRate = clearRate * Width * Height;
72 if (SwapFlag) {
73 printf("Rate: %d clears+swaps in %gs = %g clears+swaps/s %g pixels/s\n",
74 Loops, t1-t0, clearRate, pixelRate );
75 }
76 else {
77 printf("Rate: %d clears in %gs = %g clears/s %g pixels/s\n",
78 Loops, t1-t0, clearRate, pixelRate);
79 }
80 }
81
82
83 static void Reshape( int width, int height )
84 {
85 Width = width;
86 Height = height;
87 glViewport( 0, 0, width, height );
88 glMatrixMode( GL_PROJECTION );
89 glLoadIdentity();
90 glOrtho(0.0, width, 0.0, height, -1.0, 1.0);
91 glMatrixMode( GL_MODELVIEW );
92 glLoadIdentity();
93 }
94
95
96 static void Key( unsigned char key, int x, int y )
97 {
98 (void) x;
99 (void) y;
100 switch (key) {
101 case 27:
102 exit(0);
103 break;
104 }
105 glutPostRedisplay();
106 }
107
108
109 static void Init( int argc, char *argv[] )
110 {
111 int i;
112 for (i=1; i<argc; i++) {
113 if (strcmp(argv[i],"+rgb")==0)
114 ColorMode = GLUT_RGB;
115 else if (strcmp(argv[i],"+ci")==0)
116 ColorMode = GLUT_INDEX;
117 else if (strcmp(argv[i],"-color")==0)
118 BufferMask = 0;
119 else if (strcmp(argv[i],"+depth")==0)
120 BufferMask |= GL_DEPTH_BUFFER_BIT;
121 else if (strcmp(argv[i],"+alpha")==0)
122 ColorMode = GLUT_RGB | GLUT_ALPHA;
123 else if (strcmp(argv[i],"+stencil")==0)
124 BufferMask |= GL_STENCIL_BUFFER_BIT;
125 else if (strcmp(argv[i],"+accum")==0)
126 BufferMask |= GL_ACCUM_BUFFER_BIT;
127 else if (strcmp(argv[i],"-width")==0) {
128 Width = atoi(argv[i+1]);
129 i++;
130 }
131 else if (strcmp(argv[i],"-height")==0) {
132 Height = atoi(argv[i+1]);
133 i++;
134 }
135 else if (strcmp(argv[i],"+swap")==0) {
136 SwapFlag = GL_TRUE;
137 }
138 else if (strcmp(argv[i],"-swap")==0) {
139 SwapFlag = GL_FALSE;
140 }
141 else
142 printf("Unknown option: %s\n", argv[i]);
143 }
144
145 if (ColorMode & GLUT_ALPHA)
146 printf("Mode: RGB + Alpha\n");
147 else if (ColorMode==GLUT_RGB)
148 printf("Mode: RGB\n");
149 else
150 printf("Mode: Color Index\n");
151 printf("SwapBuffers: %s\n", SwapFlag ? "yes" : "no" );
152 printf("Size: %d x %d\n", Width, Height);
153 printf("Buffers: ");
154 if (BufferMask & GL_COLOR_BUFFER_BIT) printf("color ");
155 if (BufferMask & GL_DEPTH_BUFFER_BIT) printf("depth ");
156 if (BufferMask & GL_STENCIL_BUFFER_BIT) printf("stencil ");
157 if (BufferMask & GL_ACCUM_BUFFER_BIT) printf("accum ");
158 printf("\n");
159 }
160
161
162 static void Help( const char *program )
163 {
164 printf("%s options:\n", program);
165 printf(" +rgb RGB mode\n");
166 printf(" +ci color index mode\n");
167 printf(" -color don't clear color buffer\n");
168 printf(" +alpha clear alpha buffer\n");
169 printf(" +depth clear depth buffer\n");
170 printf(" +stencil clear stencil buffer\n");
171 printf(" +accum clear accum buffer\n");
172 printf(" +swap also do SwapBuffers\n");
173 printf(" -swap don't do SwapBuffers\n");
174 }
175
176
177 int main( int argc, char *argv[] )
178 {
179 GLint mode;
180
181 printf("For options: %s -help\n", argv[0]);
182
183 Init( argc, argv );
184
185 glutInit( &argc, argv );
186 glutInitWindowSize( (int) Width, (int) Height );
187 glutInitWindowPosition( 0, 0 );
188
189 mode = ColorMode | GLUT_DOUBLE;
190 if (BufferMask & GL_STENCIL_BUFFER_BIT)
191 mode |= GLUT_STENCIL;
192 if (BufferMask & GL_ACCUM_BUFFER_BIT)
193 mode |= GLUT_ACCUM;
194 if (BufferMask & GL_DEPTH_BUFFER_BIT)
195 mode |= GLUT_DEPTH;
196
197 glutInitDisplayMode(mode);
198
199 glutCreateWindow( argv[0] );
200
201 if (argc==2 && strcmp(argv[1],"-help")==0) {
202 Help(argv[0]);
203 return 0;
204 }
205
206 glutReshapeFunc( Reshape );
207 glutKeyboardFunc( Key );
208 glutDisplayFunc( Display );
209 glutIdleFunc( Idle );
210
211 glutMainLoop();
212 return 0;
213 }