]> git.cworth.org Git - apitrace/blobdiff - glretrace.py
Tweaks.
[apitrace] / glretrace.py
index e42a92200ec47fd75180f60eb5efdc57b983f29d..20e48752f937c39e033451a5a198aca83a972830 100644 (file)
 
 import stdapi
 import glapi
-
-
-
-class ConstRemover(stdapi.Rebuilder):
-
-    def visit_const(self, const):
-        return const.type
-
-    def visit_opaque(self, opaque):
-        expr = opaque.expr
-        if expr.startswith('const '):
-            expr = expr[6:]
-        return stdapi.Opaque(expr)
-
-
-class ValueExtractor(stdapi.Visitor):
-
-    def visit_literal(self, literal, lvalue, rvalue):
-        if literal.format == 'Bool':
-            print '    %s = static_cast<bool>(%s);' % (lvalue, rvalue)
+from retrace import Retracer
+
+
+class GlRetracer(Retracer):
+
+    def extract_arg(self, function, arg, arg_type, lvalue, rvalue):
+        if function.name in [
+            "glColorPointer",
+            "glEdgeFlagPointer",
+            "glIndexPointer",
+            "glNormalPointer",
+            "glTexCoordPointer",
+            "glVertexPointer",
+            "glFogCoordPointer",
+            "glSecondaryColorPointer",
+            "glVertexAttribPointer",
+        ] and arg.name == 'pointer':
+            self.extract_pointer(function, arg, arg_type, lvalue, rvalue)
         else:
-            print '    %s = %s;' % (lvalue, rvalue)
-
-    def visit_const(self, const, lvalue, rvalue):
-        self.visit(const.type, lvalue, rvalue)
-
-    def visit_alias(self, alias, lvalue, rvalue):
-        self.visit(alias.type, lvalue, rvalue)
-    
-    def visit_enum(self, enum, lvalue, rvalue):
-        print '    %s = %s;' % (lvalue, rvalue)
-
-    def visit_bitmask(self, bitmask, lvalue, rvalue):
-        self.visit(bitmask.type, lvalue, rvalue)
-
-    def visit_array(self, array, lvalue, rvalue):
-        print '    const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (array.id, rvalue)
-        print '    if (__a%s) {' % (array.id)
-        length = '__a%s->values.size()' % array.id
-        print '        %s = new %s[%s];' % (lvalue, array.type, length)
-        index = '__j' + array.id
-        print '        for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length)
-        try:
-            self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
-        finally:
-            print '        }'
-            print '    } else {'
-            print '        %s = NULL;' % lvalue
-            print '    }'
-    
-    def visit_pointer(self, pointer, lvalue, rvalue):
-        print '    const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (pointer.id, rvalue)
-        print '    if (__a%s) {' % (pointer.id)
-        print '        %s = new %s;' % (lvalue, pointer.type)
-        try:
-            self.visit(pointer.type, '%s[0]' % (lvalue,), '*__a%s->values[0]' % (pointer.id,))
-        finally:
-            print '    } else {'
-            print '        %s = NULL;' % lvalue
-            print '    }'
-
-    def visit_handle(self, handle, lvalue, rvalue):
-        self.visit(handle.type, lvalue, "__%s_map[%s]" %(handle.name, rvalue));
-        print '    if (verbosity >= 2)'
-        print '        std::cout << "%s " << static_cast<%s>(%s) << " <- " << %s << "\\n";' % (handle.name, handle.type, rvalue, lvalue)
-    
-    def visit_blob(self, blob, lvalue, rvalue):
-        print '    %s = static_cast<%s>((%s).blob());' % (lvalue, blob, rvalue)
-    
-    def visit_string(self, string, lvalue, rvalue):
-        print '    %s = (%s)((%s).string());' % (lvalue, string.expr, rvalue)
-
-
-
-class ValueWrapper(stdapi.Visitor):
-
-    def visit_literal(self, literal, lvalue, rvalue):
-        pass
-
-    def visit_alias(self, alias, lvalue, rvalue):
-        self.visit(alias.type, lvalue, rvalue)
-    
-    def visit_enum(self, enum, lvalue, rvalue):
-        pass
-
-    def visit_bitmask(self, bitmask, lvalue, rvalue):
-        pass
-
-    def visit_array(self, array, lvalue, rvalue):
-        print '    const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (array.id, rvalue)
-        print '    if (__a%s) {' % (array.id)
-        length = '__a%s->values.size()' % array.id
-        index = '__j' + array.id
-        print '        for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length)
-        try:
-            self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
-        finally:
-            print '        }'
-            print '    }'
-    
-    def visit_pointer(self, pointer, lvalue, rvalue):
-        print '    const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (pointer.id, rvalue)
-        print '    if (__a%s) {' % (pointer.id)
-        try:
-            self.visit(pointer.type, '%s[0]' % (lvalue,), '*__a%s->values[0]' % (pointer.id,))
-        finally:
-            print '    }'
-    
-
-    def visit_handle(self, handle, lvalue, rvalue):
-        print "    __%s_map[static_cast<%s>(%s)] = %s;" % (handle.name, handle.type, rvalue, lvalue)
-        print '    if (verbosity >= 2)'
-        print '        std::cout << "%s " << static_cast<%s>(%s) << " -> " << %s << "\\n";' % (handle.name, handle.type, rvalue, lvalue)
-    
-    def visit_blob(self, blob, lvalue, rvalue):
-        pass
-    
-    def visit_string(self, string, lvalue, rvalue):
-        pass
-
-
-
-def retrace_function(function):
-    print 'static void retrace_%s(Trace::Call &call) {' % function.name
-    success = True
-    for arg in function.args:
-        arg_type = ConstRemover().visit(arg.type)
-        #print '    // %s ->  %s' % (arg.type, arg_type)
-        print '    %s %s;' % (arg_type, arg.name)
-        rvalue = 'call.arg(%u)' % (arg.index,)
-        lvalue = arg.name
-        try:
-            ValueExtractor().visit(arg_type, lvalue, rvalue)
-        except NotImplementedError:
-            success = False
-            print '    %s = 0; // FIXME' % arg.name
-    if not success:
-        print '    std::cerr << "warning: unsupported call %s\\n";' % function.name
-        print '    return;'
-    arg_names = ", ".join([arg.name for arg in function.args])
-    if function.type is not stdapi.Void:
-        print '    %s __result;' % (function.type)
-        print '    __result = %s(%s);' % (function.name, arg_names)
-    else:
-        print '    %s(%s);' % (function.name, arg_names)
-    for arg in function.args:
-        if arg.output:
-            arg_type = ConstRemover().visit(arg.type)
-            rvalue = 'call.arg(%u)' % (arg.index,)
-            lvalue = arg.name
-            try:
-                ValueWrapper().visit(arg_type, lvalue, rvalue)
-            except NotImplementedError:
-                print '   // FIXME: %s' % arg.name
-    if function.type is not stdapi.Void:
-        rvalue = '*call.ret'
-        lvalue = '__result'
-        try:
-            ValueWrapper().visit(function.type, lvalue, rvalue)
-        except NotImplementedError:
-            print '   // FIXME: result'
-    print '}'
-    print
-
-
-def retrace_functions(functions):
-    for function in functions:
-        if function.sideeffects:
-            retrace_function(function)
-
-    print 'static bool retrace_call(Trace::Call &call) {'
-    for function in functions:
-        if not function.sideeffects:
-            print '    if (call.name == "%s") {' % function.name
-            print '        return true;'
-            print '    }'
-    print
-    print '    if (verbosity >=1 ) {'
-    print '        std::cout << call;'
-    print '        std::cout.flush();'
-    print '    };'
-    print
-    for function in functions:
-        if function.sideeffects:
-            print '    if (call.name == "%s") {' % function.name
-            print '        retrace_%s(call);' % function.name
-            print '        return true;'
-            print '    }'
-    print '    std::cerr << "warning: unknown call " << call.name << "\\n";'
-    print '    return false;'
-    print '}'
-    print
-
-
-def retrace_api(api):
-    types = api.all_types()
-
-    handles = [type for type in types if isinstance(type, stdapi.Handle)]
-    for handle in handles:
-        print 'static std::map<%s, %s> __%s_map;' % (handle.type, handle.type, handle.name)
-    print
+            Retracer.extract_arg(self, function, arg, arg_type, lvalue, rvalue)
 
-    retrace_functions(api.functions)
+    def extract_pointer(self, function, arg, arg_type, lvalue, rvalue):
+        print '    if (dynamic_cast<Trace::Null *>(&%s)) {' % rvalue
+        print '        %s = 0;' % (lvalue)
+        print '    } else {'
+        print '        %s = (%s)(uintptr_t)(%s);' % (lvalue, arg_type, rvalue)
+        print '    }'
 
 
 if __name__ == '__main__':
@@ -243,11 +67,11 @@ if __name__ == '__main__':
     print '#include <GL/glew.h>'
     print '#include <GL/glut.h>'
     print
-    print '#include "trace_parser.hpp"'
-    print
-    print 'unsigned verbosity = 0;'
+    print 'static bool double_buffer = false;'
     print
-    retrace_api(glapi.glapi)
+    api = glapi.glapi
+    retracer = GlRetracer()
+    retracer.retrace_api(glapi.glapi)
     print '''
 
 Trace::Parser parser;
@@ -258,13 +82,20 @@ static void display(void) {
    Trace::Call *call;
 
    while ((call = parser.parse_call())) {
-      if (call->name == "glFlush" ||
-          call->name == "glXSwapBuffers" ||
-          call->name == "wglSwapBuffers") {
+      if (call->name == "glFlush") {
          glFlush();
          return;
       }
       
+      if (call->name == "glXSwapBuffers" ||
+          call->name == "wglSwapBuffers") {
+         if (double_buffer)
+            glutSwapBuffers();
+         else
+            glFlush();
+         return;
+      }
+      
       retrace_call(*call);
 
       if (call->name == "glBegin") {
@@ -323,18 +154,9 @@ static void idle(void) {
 
 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) {
+   int i;
+   for (i = 1; i < argc; ++i) {
       const char *arg = argv[i];
 
       if (arg[0] != '-') {
@@ -344,7 +166,9 @@ int main(int argc, char **argv)
       if (!strcmp(arg, "--")) {
          break;
       }
-      else if (!strcmp(arg, "-v")) {
+      else if (!strcmp(arg, "-db")) {
+         double_buffer = true;
+      } else if (!strcmp(arg, "-v")) {
          ++verbosity;
       } else {
          std::cerr << "error: unknown option " << arg << "\\n";
@@ -352,6 +176,20 @@ int main(int argc, char **argv)
       }
    }
 
+   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();