132001f49Smrg
232001f49Smrg/*
332001f49Smrg * Simple GLUT program to measure glClear() and glutSwapBuffers() speed.
432001f49Smrg * Brian Paul  February 15, 1997  This file in public domain.
532001f49Smrg */
632001f49Smrg
732001f49Smrg
832001f49Smrg#include <stdio.h>
932001f49Smrg#include <stdlib.h>
1032001f49Smrg#include <math.h>
1132001f49Smrg#include <string.h>
1232001f49Smrg#include "glut_wrap.h"
1332001f49Smrg
1432001f49Smrg
1532001f49Smrgstatic float MinPeriod = 2.0;   /* 2 seconds */
1632001f49Smrgstatic int ColorMode = GLUT_RGB;
1732001f49Smrgstatic int Width = 400.0;
1832001f49Smrgstatic int Height = 400.0;
1932001f49Smrgstatic int Loops = 100;
2032001f49Smrgstatic float ClearColor = 0.0;
2132001f49Smrgstatic GLbitfield BufferMask = GL_COLOR_BUFFER_BIT;
2232001f49Smrgstatic GLboolean SwapFlag = GL_FALSE;
2332001f49Smrg
2432001f49Smrg
2532001f49Smrg
2632001f49Smrgstatic void Idle( void )
2732001f49Smrg{
2832001f49Smrg   glutPostRedisplay();
2932001f49Smrg}
3032001f49Smrg
3132001f49Smrg
3232001f49Smrgstatic void Display( void )
3332001f49Smrg{
3432001f49Smrg   double t0, t1;
3532001f49Smrg   double clearRate;
3632001f49Smrg   double pixelRate;
3732001f49Smrg   int i;
3832001f49Smrg
3932001f49Smrg   glClearColor( ClearColor, ClearColor, ClearColor, 0.0 );
4032001f49Smrg   ClearColor += 0.1;
4132001f49Smrg   if (ClearColor>1.0)
4232001f49Smrg      ClearColor = 0.0;
4332001f49Smrg
4432001f49Smrg   if (SwapFlag) {
4532001f49Smrg      t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
4632001f49Smrg      for (i=0;i<Loops;i++) {
4732001f49Smrg         glClear( BufferMask );
4832001f49Smrg         glutSwapBuffers();
4932001f49Smrg      }
5032001f49Smrg      glFinish();
5132001f49Smrg      t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
5232001f49Smrg   }
5332001f49Smrg   else {
5432001f49Smrg      t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
5532001f49Smrg      for (i=0;i<Loops;i++) {
5632001f49Smrg         glClear( BufferMask );
5732001f49Smrg      }
5832001f49Smrg      glFinish();
5932001f49Smrg      t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
6032001f49Smrg      glutSwapBuffers();
6132001f49Smrg   }
6232001f49Smrg
6332001f49Smrg   /* NOTE: If clearspd doesn't map it's window immediately on
6432001f49Smrg    * starting, swaps will be istantaneous, so this will send Loops
6532001f49Smrg    * towards infinity.  When a window is finally mapped, it may be
6632001f49Smrg    * minutes before the first call to glutSwapBuffers, making it look
6732001f49Smrg    * like there's a driver bug.
6832001f49Smrg    */
6932001f49Smrg   if (t1-t0 < MinPeriod) {
7032001f49Smrg      /* Next time do more clears to get longer elapsed time */
7132001f49Smrg      Loops *= 2;
7232001f49Smrg      return;
7332001f49Smrg   }
7432001f49Smrg
7532001f49Smrg   clearRate = Loops / (t1-t0);
7632001f49Smrg   pixelRate = clearRate * Width * Height;
7732001f49Smrg   if (SwapFlag) {
7832001f49Smrg      printf("Rate: %d clears+swaps in %gs = %g clears+swaps/s   %g pixels/s\n",
7932001f49Smrg             Loops, t1-t0, clearRate, pixelRate );
8032001f49Smrg   }
8132001f49Smrg   else {
8232001f49Smrg      printf("Rate: %d clears in %gs = %g clears/s   %g pixels/s\n",
8332001f49Smrg             Loops, t1-t0, clearRate, pixelRate);
8432001f49Smrg   }
8532001f49Smrg}
8632001f49Smrg
8732001f49Smrg
8832001f49Smrgstatic void Reshape( int width, int height )
8932001f49Smrg{
9032001f49Smrg   Width = width;
9132001f49Smrg   Height = height;
9232001f49Smrg   glViewport( 0, 0, width, height );
9332001f49Smrg   glMatrixMode( GL_PROJECTION );
9432001f49Smrg   glLoadIdentity();
9532001f49Smrg   glOrtho(0.0, width, 0.0, height, -1.0, 1.0);
9632001f49Smrg   glMatrixMode( GL_MODELVIEW );
9732001f49Smrg   glLoadIdentity();
9832001f49Smrg}
9932001f49Smrg
10032001f49Smrg
10132001f49Smrgstatic void Key( unsigned char key, int x, int y )
10232001f49Smrg{
10332001f49Smrg   (void) x;
10432001f49Smrg   (void) y;
10532001f49Smrg   switch (key) {
10632001f49Smrg      case 27:
10732001f49Smrg         exit(0);
10832001f49Smrg         break;
10932001f49Smrg   }
11032001f49Smrg   glutPostRedisplay();
11132001f49Smrg}
11232001f49Smrg
11332001f49Smrg
11432001f49Smrgstatic void Init( int argc, char *argv[] )
11532001f49Smrg{
11632001f49Smrg   int i;
11732001f49Smrg   for (i=1; i<argc; i++) {
11832001f49Smrg      if (strcmp(argv[i],"+rgb")==0)
11932001f49Smrg         ColorMode = GLUT_RGB;
12032001f49Smrg      else if (strcmp(argv[i],"+ci")==0)
12132001f49Smrg         ColorMode = GLUT_INDEX;
12232001f49Smrg      else if (strcmp(argv[i],"-color")==0)
12332001f49Smrg         BufferMask = 0;
12432001f49Smrg      else if (strcmp(argv[i],"+depth")==0)
12532001f49Smrg         BufferMask |= GL_DEPTH_BUFFER_BIT;
12632001f49Smrg      else if (strcmp(argv[i],"+alpha")==0)
12732001f49Smrg         ColorMode = GLUT_RGB | GLUT_ALPHA;
12832001f49Smrg      else if (strcmp(argv[i],"+stencil")==0)
12932001f49Smrg         BufferMask |= GL_STENCIL_BUFFER_BIT;
13032001f49Smrg      else if (strcmp(argv[i],"+accum")==0)
13132001f49Smrg         BufferMask |= GL_ACCUM_BUFFER_BIT;
13232001f49Smrg      else if (strcmp(argv[i],"-width")==0) {
13332001f49Smrg         Width = atoi(argv[i+1]);
13432001f49Smrg         i++;
13532001f49Smrg      }
13632001f49Smrg      else if (strcmp(argv[i],"-height")==0) {
13732001f49Smrg         Height = atoi(argv[i+1]);
13832001f49Smrg         i++;
13932001f49Smrg      }
14032001f49Smrg      else if (strcmp(argv[i],"+swap")==0) {
14132001f49Smrg         SwapFlag = GL_TRUE;
14232001f49Smrg      }
14332001f49Smrg      else if (strcmp(argv[i],"-swap")==0) {
14432001f49Smrg         SwapFlag = GL_FALSE;
14532001f49Smrg      }
14632001f49Smrg      else
14732001f49Smrg         printf("Unknown option: %s\n", argv[i]);
14832001f49Smrg   }
14932001f49Smrg
15032001f49Smrg   if (ColorMode & GLUT_ALPHA)
15132001f49Smrg      printf("Mode:  RGB + Alpha\n");
15232001f49Smrg   else if (ColorMode==GLUT_RGB)
15332001f49Smrg      printf("Mode:  RGB\n");
15432001f49Smrg   else
15532001f49Smrg      printf("Mode:  Color Index\n");
15632001f49Smrg   printf("SwapBuffers: %s\n", SwapFlag ? "yes" : "no" );
15732001f49Smrg   printf("Size: %d x %d\n", Width, Height);
15832001f49Smrg   printf("Buffers: ");
15932001f49Smrg   if (BufferMask & GL_COLOR_BUFFER_BIT)  printf("color ");
16032001f49Smrg   if (BufferMask & GL_DEPTH_BUFFER_BIT)  printf("depth ");
16132001f49Smrg   if (BufferMask & GL_STENCIL_BUFFER_BIT)  printf("stencil ");
16232001f49Smrg   if (BufferMask & GL_ACCUM_BUFFER_BIT)  printf("accum ");
16332001f49Smrg   printf("\n");
16432001f49Smrg}
16532001f49Smrg
16632001f49Smrg
16732001f49Smrgstatic void Help( const char *program )
16832001f49Smrg{
16932001f49Smrg   printf("%s options:\n", program);
17032001f49Smrg   printf("  +rgb       RGB mode\n");
17132001f49Smrg   printf("  +ci        color index mode\n");
17232001f49Smrg   printf("  -color     don't clear color buffer\n");
17332001f49Smrg   printf("  +alpha     clear alpha buffer\n");
17432001f49Smrg   printf("  +depth     clear depth buffer\n");
17532001f49Smrg   printf("  +stencil   clear stencil buffer\n");
17632001f49Smrg   printf("  +accum     clear accum buffer\n");
17732001f49Smrg   printf("  +swap      also do SwapBuffers\n");
17832001f49Smrg   printf("  -swap      don't do SwapBuffers\n");
17932001f49Smrg}
18032001f49Smrg
18132001f49Smrg
18232001f49Smrgint main( int argc, char *argv[] )
18332001f49Smrg{
18432001f49Smrg   GLint mode;
18532001f49Smrg
18632001f49Smrg   printf("For options:  %s -help\n", argv[0]);
18732001f49Smrg
18832001f49Smrg   Init( argc, argv );
18932001f49Smrg
19032001f49Smrg   glutInitWindowSize( (int) Width, (int) Height );
19132001f49Smrg   glutInit( &argc, argv );
19232001f49Smrg
19332001f49Smrg   mode = ColorMode | GLUT_DOUBLE;
19432001f49Smrg   if (BufferMask & GL_STENCIL_BUFFER_BIT)
19532001f49Smrg      mode |= GLUT_STENCIL;
19632001f49Smrg   if (BufferMask & GL_ACCUM_BUFFER_BIT)
19732001f49Smrg      mode |= GLUT_ACCUM;
19832001f49Smrg   if (BufferMask & GL_DEPTH_BUFFER_BIT)
19932001f49Smrg      mode |= GLUT_DEPTH;
20032001f49Smrg
20132001f49Smrg   glutInitDisplayMode(mode);
20232001f49Smrg
20332001f49Smrg   glutCreateWindow( argv[0] );
20432001f49Smrg
20532001f49Smrg   if (argc==2 && strcmp(argv[1],"-help")==0) {
20632001f49Smrg      Help(argv[0]);
20732001f49Smrg      return 0;
20832001f49Smrg   }
20932001f49Smrg
21032001f49Smrg   glutReshapeFunc( Reshape );
21132001f49Smrg   glutKeyboardFunc( Key );
21232001f49Smrg   glutDisplayFunc( Display );
21332001f49Smrg   glutIdleFunc( Idle );
21432001f49Smrg
21532001f49Smrg   glutMainLoop();
21632001f49Smrg   return 0;
21732001f49Smrg}
218