exercise glDeleteProgramsARB
[mesa.git] / progs / demos / readpix.c
index b6b067502d5a4454e0373690773b7a5e907cb797..784e4c88d7b81c39112217cac8d35c602810d526 100644 (file)
@@ -1,4 +1,3 @@
-/* $Id: readpix.c,v 1.1 2000/03/01 16:23:14 brianp Exp $ */
 
 /*
  * glReadPixels and glCopyPixels test
@@ -6,14 +5,6 @@
  * Brian Paul   March 1, 2000  This file is in the public domain.
  */
 
-/*
- * $Log: readpix.c,v $
- * Revision 1.1  2000/03/01 16:23:14  brianp
- * test glDraw/Read/CopyPixels()
- *
- */
-
-
 #include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -34,9 +25,34 @@ static int CPosX, CPosY;  /* copypixels */
 
 static GLboolean DrawFront = GL_FALSE;
 static GLboolean ScaleAndBias = GL_FALSE;
-
+static GLboolean Benchmark = GL_FALSE;
 static GLubyte *TempImage = NULL;
 
+#if 0
+#define ReadFormat ImgFormat
+#define ReadType GL_UNSIGNED_BYTE
+#endif
+#if 1
+static GLenum ReadFormat = GL_RGBA;
+static GLenum ReadType = GL_UNSIGNED_BYTE;
+#endif
+#if 0
+static GLenum ReadFormat = GL_RGB;
+static GLenum ReadType = GL_UNSIGNED_SHORT_5_6_5;
+#endif
+#if 0
+static GLenum ReadFormat = GL_RGBA;
+static GLenum ReadType = GL_UNSIGNED_SHORT_1_5_5_5_REV;
+#endif
+#if 0
+static GLenum ReadFormat = GL_BGRA;
+static GLenum ReadType = GL_UNSIGNED_SHORT_5_5_5_1;
+#endif
+#if 0
+static GLenum ReadFormat = GL_BGRA;
+static GLenum ReadType = GL_UNSIGNED_SHORT_4_4_4_4_REV;
+#endif
+
 
 static void
 Reset( void )
@@ -82,10 +98,11 @@ SetupPixelTransfer(GLboolean invert)
 static void
 Display( void )
 {
+   glClearColor(.3, .3, .3, 1);
    glClear( GL_COLOR_BUFFER_BIT );
 
    glRasterPos2i(5, ImgHeight+25);
-   PrintString("f = toggle front/back  b = toggle scale/bias");
+   PrintString("f = toggle front/back  s = toggle scale/bias  b = benchmark");
 
    /* draw original image */
    glRasterPos2i(APosX, 5);
@@ -99,12 +116,34 @@ Display( void )
    glRasterPos2i(BPosX, 5);
    PrintString("Read/DrawPixels");
    SetupPixelTransfer(ScaleAndBias);
-   glReadPixels(APosX, APosY, ImgWidth, ImgHeight,
-                ImgFormat, GL_UNSIGNED_BYTE, TempImage);
+   if (Benchmark) {
+      GLint reads = 0;
+      GLint endTime;
+      GLint startTime = glutGet(GLUT_ELAPSED_TIME);
+      GLdouble seconds, pixelsPerSecond;
+      printf("Benchmarking...\n");
+      do {
+         glReadPixels(APosX, APosY, ImgWidth, ImgHeight,
+                      ReadFormat, ReadType, TempImage);
+         reads++;
+         endTime = glutGet(GLUT_ELAPSED_TIME);
+      } while (endTime - startTime < 4000);   /* 4 seconds */
+      seconds = (double) (endTime - startTime) / 1000.0;
+      pixelsPerSecond = reads * ImgWidth * ImgHeight / seconds;
+      printf("Result:  %d reads in %f seconds = %f pixels/sec\n",
+             reads, seconds, pixelsPerSecond);
+      Benchmark = GL_FALSE;
+   }
+   else {
+      /* clear the temporary image to white (helpful for debugging */
+      memset(TempImage, 255, ImgWidth * ImgHeight * 4);
+      glReadPixels(APosX, APosY, ImgWidth, ImgHeight,
+                   ReadFormat, ReadType, TempImage);
+   }
    glRasterPos2i(BPosX, BPosY);
    glDisable(GL_DITHER);
    SetupPixelTransfer(GL_FALSE);
-   glDrawPixels(ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, TempImage);
+   glDrawPixels(ImgWidth, ImgHeight, ReadFormat, ReadType, TempImage);
 
    /* do copypixels */
    glRasterPos2i(CPosX, 5);
@@ -116,10 +155,13 @@ Display( void )
 
    if (!DrawFront)
       glutSwapBuffers();
+   else
+      glFinish();
 }
 
 
-static void Reshape( int width, int height )
+static void
+Reshape( int width, int height )
 {
    glViewport( 0, 0, width, height );
    glMatrixMode( GL_PROJECTION );
@@ -130,20 +172,28 @@ static void Reshape( int width, int height )
 }
 
 
-static void Key( unsigned char key, int x, int y )
+static void
+Key( unsigned char key, int x, int y )
 {
    (void) x;
    (void) y;
    switch (key) {
       case 'b':
+         Benchmark = GL_TRUE;
+         break;
+      case 's':
          ScaleAndBias = !ScaleAndBias;
          break;
       case 'f':
          DrawFront = !DrawFront;
-         if (DrawFront)
+         if (DrawFront) {
             glDrawBuffer(GL_FRONT);
-         else
+            glReadBuffer(GL_FRONT);
+         }
+         else {
             glDrawBuffer(GL_BACK);
+            glReadBuffer(GL_BACK);
+         }
          printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK");
          break;
       case 27:
@@ -154,30 +204,6 @@ static void Key( unsigned char key, int x, int y )
 }
 
 
-static void SpecialKey( int key, int x, int y )
-{
-   (void) x;
-   (void) y;
-#if 0
-   switch (key) {
-      case GLUT_KEY_UP:
-         Ypos += 1;
-         break;
-      case GLUT_KEY_DOWN:
-         Ypos -= 1;
-         break;
-      case GLUT_KEY_LEFT:
-         Xpos -= 1;
-         break;
-      case GLUT_KEY_RIGHT:
-         Xpos += 1;
-         break;
-   }
-#endif
-   glutPostRedisplay();
-}
-
-
 static void
 Init( GLboolean ciMode )
 {
@@ -192,7 +218,7 @@ Init( GLboolean ciMode )
 
    if (ciMode) {
       /* Convert RGB image to grayscale */
-      GLubyte *indexImage = malloc( ImgWidth * ImgHeight );
+      GLubyte *indexImage = (GLubyte *) malloc( ImgWidth * ImgHeight );
       GLint i;
       for (i=0; i<ImgWidth*ImgHeight; i++) {
          int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2];
@@ -222,32 +248,25 @@ Init( GLboolean ciMode )
 }
 
 
-int main( int argc, char *argv[] )
+int
+main( int argc, char *argv[] )
 {
    GLboolean ciMode = GL_FALSE;
-
    if (argc > 1 && strcmp(argv[1], "-ci")==0) {
       ciMode = GL_TRUE;
    }
-
    glutInit( &argc, argv );
    glutInitWindowPosition( 0, 0 );
    glutInitWindowSize( 750, 250 );
-
    if (ciMode)
       glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE );
    else
       glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
-
    glutCreateWindow(argv[0]);
-
    Init(ciMode);
-
    glutReshapeFunc( Reshape );
    glutKeyboardFunc( Key );
-   glutSpecialFunc( SpecialKey );
    glutDisplayFunc( Display );
-
    glutMainLoop();
    return 0;
 }