home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / mesa / gdemos / clearspd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-31  |  4.8 KB  |  201 lines

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