Eliminate more string comparisons.
authorJosé Fonseca <jfonseca@vmware.com>
Sun, 28 Nov 2010 00:08:23 +0000 (00:08 +0000)
committerJosé Fonseca <jfonseca@vmware.com>
Sun, 28 Nov 2010 00:08:23 +0000 (00:08 +0000)
glretrace.py
retrace.py

index 29ba78f..ff22e99 100644 (file)
@@ -40,6 +40,9 @@ class GlRetracer(Retracer):
             "glBufferRegionEnabled",
         ]
 
+    def retrace_function(self, function):
+        Retracer.retrace_function(self, function)
+
     def call_function(self, function):
         if function.name in ("glDrawArrays", "glDrawElements", "glDrawRangeElements", "glMultiDrawElements"):
             print '    GLint __array_buffer = 0;'
@@ -48,7 +51,15 @@ class GlRetracer(Retracer):
             self.fail_function(function)
             print '    }'
 
+        if function.name == "glEnd":
+            print '    insideGlBeginEnd = false;'
         Retracer.call_function(self, function)
+        if function.name == "glBegin":
+            print '    insideGlBeginEnd = true;'
+        else:
+            # glGetError is not allowed inside glBegin/glEnd
+            print '    checkGlError();'
+
 
     def extract_arg(self, function, arg, arg_type, lvalue, rvalue):
         if function.name in [
@@ -76,8 +87,8 @@ class GlRetracer(Retracer):
 
 if __name__ == '__main__':
     print
-    print '#include <stdlib.h>'
     print '#include <string.h>'
+    print '#include <iostream>'
     print
     print '#ifdef WIN32'
     print '#include <windows.h>'
@@ -87,136 +98,135 @@ if __name__ == '__main__':
     print '#include <GL/glut.h>'
     print
     print 'static bool double_buffer = false;'
+    print 'static bool insideGlBeginEnd = false;'
     print
+    print '''
+static void
+checkGlError(void) {
+    if (insideGlBeginEnd) {
+        return;
+    }
+
+    GLenum error = glGetError();
+    if (error == GL_NO_ERROR) {
+        return;
+    }
+
+    std::cerr << "warning: glGetError() = ";
+    switch (error) {
+    case GL_INVALID_ENUM:
+        std::cerr << "GL_INVALID_ENUM";
+        break;
+    case GL_INVALID_VALUE:
+        std::cerr << "GL_INVALID_VALUE";
+        break;
+    case GL_INVALID_OPERATION:
+        std::cerr << "GL_INVALID_OPERATION";
+        break;
+    case GL_STACK_OVERFLOW:
+        std::cerr << "GL_STACK_OVERFLOW";
+        break;
+    case GL_STACK_UNDERFLOW:
+        std::cerr << "GL_STACK_UNDERFLOW";
+        break;
+    case GL_OUT_OF_MEMORY:
+        std::cerr << "GL_OUT_OF_MEMORY";
+        break;
+    case GL_INVALID_FRAMEBUFFER_OPERATION:
+        std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
+        break;
+    case GL_TABLE_TOO_LARGE:
+        std::cerr << "GL_TABLE_TOO_LARGE";
+        break;
+    default:
+        std::cerr << error;
+        break;
+    }
+    std::cerr << "\\n";
+}
+'''
     api = glapi.glapi
     retracer = GlRetracer()
     retracer.retrace_api(glapi.glapi)
     print '''
 
-Trace::Parser parser;
-
-static bool insideGlBeginEnd;
+static Trace::Parser parser;
 
 static void display(void) {
-   Trace::Call *call;
-
-   while ((call = parser.parse_call())) {
-      if (call->name() == "glFlush") {
-         glFlush();
-         return;
-      }
-      
-      if (call->name() == "glXSwapBuffers" ||
-          call->name() == "wglSwapBuffers") {
-         if (double_buffer)
-            glutSwapBuffers();
-         else
+    Trace::Call *call;
+
+    while ((call = parser.parse_call())) {
+        if (call->name() == "glFlush") {
             glFlush();
-         return;
-      }
-      
-      retrace_call(*call);
-
-      if (call->name() == "glBegin") {
-         insideGlBeginEnd = true;
-      }
-      
-      if (call->name() == "glEnd") {
-         insideGlBeginEnd = false;
-      }
-
-      if (!insideGlBeginEnd) {
-         GLenum error = glGetError();
-         if (error != GL_NO_ERROR) {
-            std::cerr << "warning: glGetError() = ";
-            switch (error) {
-            case GL_INVALID_ENUM:
-               std::cerr << "GL_INVALID_ENUM";
-               break;
-            case GL_INVALID_VALUE:
-               std::cerr << "GL_INVALID_VALUE";
-               break;
-            case GL_INVALID_OPERATION:
-               std::cerr << "GL_INVALID_OPERATION";
-               break;
-            case GL_STACK_OVERFLOW:
-               std::cerr << "GL_STACK_OVERFLOW";
-               break;
-            case GL_STACK_UNDERFLOW:
-               std::cerr << "GL_STACK_UNDERFLOW";
-               break;
-            case GL_OUT_OF_MEMORY:
-               std::cerr << "GL_OUT_OF_MEMORY";
-               break;
-            case GL_INVALID_FRAMEBUFFER_OPERATION:
-               std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
-               break;
-            case GL_TABLE_TOO_LARGE:
-               std::cerr << "GL_TABLE_TOO_LARGE";
-               break;
-            default:
-               std::cerr << error;
-               break;
+            return;
+        }
+        
+        if (!retrace_call(*call)) {
+            if (call->name() == "glXSwapBuffers" ||
+                call->name() == "wglSwapBuffers") {
+                if (double_buffer)
+                    glutSwapBuffers();
+                else
+                    glFlush();
+                return;
             }
-            std::cerr << "\\n";
-         }
-      }
-   }
+        }
+    }
 
-   glFlush();
-   glutIdleFunc(NULL);
+    glFlush();
+    glutIdleFunc(NULL);
 }
 
 static void idle(void) {
-   glutPostRedisplay();
+    glutPostRedisplay();
 }
 
 int main(int argc, char **argv)
 {
 
-   int i;
-   for (i = 1; i < argc; ++i) {
-      const char *arg = argv[i];
-
-      if (arg[0] != '-') {
-         break;
-      }
-
-      if (!strcmp(arg, "--")) {
-         break;
-      }
-      else if (!strcmp(arg, "-db")) {
-         double_buffer = true;
-      } else if (!strcmp(arg, "-v")) {
-         ++verbosity;
-      } else {
-         std::cerr << "error: unknown option " << arg << "\\n";
-         return 1;
-      }
-   }
-
-   glutInit(&argc, argv);
-   glutInitWindowPosition(0, 0);
-   glutInitWindowSize(800, 600);
-   glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | (double_buffer ? GLUT_DOUBLE : GLUT_SINGLE));
-   glutCreateWindow(argv[0]);
-   glewInit();
-
-   glutDisplayFunc(&display);
-   glutIdleFunc(&idle);
-
-   for (GLuint h = 0; h < 1024; ++h) {
-      __list_map[h] = h;
-   }
-
-   for ( ; i < argc; ++i) {
-      if (parser.open(argv[i])) {
-         glutMainLoop();
-         parser.close();
-      }
-   }
-
-   return 0;
+    int i;
+    for (i = 1; i < argc; ++i) {
+        const char *arg = argv[i];
+
+        if (arg[0] != '-') {
+            break;
+        }
+
+        if (!strcmp(arg, "--")) {
+            break;
+        }
+        else if (!strcmp(arg, "-db")) {
+            double_buffer = true;
+        } else if (!strcmp(arg, "-v")) {
+            ++verbosity;
+        } else {
+            std::cerr << "error: unknown option " << arg << "\\n";
+            return 1;
+        }
+    }
+
+    glutInit(&argc, argv);
+    glutInitWindowPosition(0, 0);
+    glutInitWindowSize(800, 600);
+    glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | (double_buffer ? GLUT_DOUBLE : GLUT_SINGLE));
+    glutCreateWindow(argv[0]);
+    glewInit();
+
+    glutDisplayFunc(&display);
+    glutIdleFunc(&idle);
+
+    for (GLuint h = 0; h < 1024; ++h) {
+        __list_map[h] = h;
+    }
+
+    for ( ; i < argc; ++i) {
+        if (parser.open(argv[i])) {
+            glutMainLoop();
+            parser.close();
+        }
+    }
+
+    return 0;
 }
 
-'''   
+'''    
index 9eb5d0d..b1e72fd 100644 (file)
@@ -263,132 +263,3 @@ class Retracer:
 
         self.retrace_functions(api.functions)
 
-
-if __name__ == '__main__':
-    print
-    print '#include <stdlib.h>'
-    print '#include <string.h>'
-    print
-    print '#ifdef WIN32'
-    print '#include <windows.h>'
-    print '#endif'
-    print
-    print '#include <GL/glew.h>'
-    print '#include <GL/glut.h>'
-    print
-    retrace_api(glapi.glapi)
-    print '''
-
-Trace::Parser parser;
-
-static bool insideGlBeginEnd;
-
-static void display(void) {
-   Trace::Call *call;
-
-   while ((call = parser.parse_call())) {
-      if (call->name() == "glFlush" ||
-          call->name() == "glXSwapBuffers" ||
-          call->name() == "wglSwapBuffers") {
-         glFlush();
-         return;
-      }
-      
-      retrace_call(*call);
-
-      if (call->name() == "glBegin") {
-         insideGlBeginEnd = true;
-      }
-      
-      if (call->name() == "glEnd") {
-         insideGlBeginEnd = false;
-      }
-
-      if (!insideGlBeginEnd) {
-         GLenum error = glGetError();
-         if (error != GL_NO_ERROR) {
-            std::cerr << "warning: glGetError() = ";
-            switch (error) {
-            case GL_INVALID_ENUM:
-               std::cerr << "GL_INVALID_ENUM";
-               break;
-            case GL_INVALID_VALUE:
-               std::cerr << "GL_INVALID_VALUE";
-               break;
-            case GL_INVALID_OPERATION:
-               std::cerr << "GL_INVALID_OPERATION";
-               break;
-            case GL_STACK_OVERFLOW:
-               std::cerr << "GL_STACK_OVERFLOW";
-               break;
-            case GL_STACK_UNDERFLOW:
-               std::cerr << "GL_STACK_UNDERFLOW";
-               break;
-            case GL_OUT_OF_MEMORY:
-               std::cerr << "GL_OUT_OF_MEMORY";
-               break;
-            case GL_INVALID_FRAMEBUFFER_OPERATION:
-               std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
-               break;
-            case GL_TABLE_TOO_LARGE:
-               std::cerr << "GL_TABLE_TOO_LARGE";
-               break;
-            default:
-               std::cerr << error;
-               break;
-            }
-            std::cerr << "\\n";
-         }
-      }
-   }
-
-   glFlush();
-   glutIdleFunc(NULL);
-}
-
-static void idle(void) {
-   glutPostRedisplay();
-}
-
-int main(int argc, char **argv)
-{
-   glutInit(&argc, argv);
-   glutInitWindowPosition(0, 0);
-   glutInitWindowSize(800, 600);
-   glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE);
-   glutCreateWindow(argv[0]);
-   glewInit();
-
-   glutDisplayFunc(&display);
-   glutIdleFunc(&idle);
-
-    int i;
-    for (i = 1; i < argc; ++i) {
-      const char *arg = argv[i];
-
-      if (arg[0] != '-') {
-         break;
-      }
-
-      if (!strcmp(arg, "--")) {
-         break;
-      }
-      else if (!strcmp(arg, "-v")) {
-         ++verbosity;
-      } else {
-         std::cerr << "error: unknown option " << arg << "\\n";
-         return 1;
-      }
-   }
-
-   for ( ; i < argc; ++i) {
-      if (parser.open(argv[i])) {
-         glutMainLoop();
-         parser.close();
-      }
-   }
-
-   return 0;
-}
-
-'''