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