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