Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

clearspd.c 5.4KB

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