]> git.cworth.org Git - apitrace/blobdiff - glretrace.py
Handle VBO draw elements .
[apitrace] / glretrace.py
index 39a818a47f8a78582815772e7ec88609e4b0834c..fde4f0fc70d10e1bb0e9a63c283ea7e2a0858f34 100644 (file)
@@ -31,15 +31,6 @@ from retrace import Retracer
 
 class GlRetracer(Retracer):
 
-    def filter_function(self, function):
-        return function.name not in [
-            "glNewBufferRegion",
-            "glDeleteBufferRegion",
-            "glReadBufferRegion",
-            "glDrawBufferRegion",
-            "glBufferRegionEnabled",
-        ]
-
     def retrace_function(self, function):
         Retracer.retrace_function(self, function)
 
@@ -51,9 +42,21 @@ class GlRetracer(Retracer):
             self.fail_function(function)
             print '    }'
 
+        if function.name == "glViewport":
+            print '    if (x + width > __window_width) {'
+            print '        __window_width = x + width;'
+            print '        __reshape_window = true;'
+            print '    }'
+            print '    if (y + height > __window_height) {'
+            print '        __window_height = y + height;'
+            print '        __reshape_window = true;'
+            print '    }'
+
         if function.name == "glEnd":
             print '    insideGlBeginEnd = false;'
+        
         Retracer.call_function(self, function)
+
         if function.name == "glBegin":
             print '    insideGlBeginEnd = true;'
         else:
@@ -64,14 +67,26 @@ class GlRetracer(Retracer):
     def extract_arg(self, function, arg, arg_type, lvalue, rvalue):
         if function.name in [
             "glColorPointer",
+            "glColorPointerEXT",
             "glEdgeFlagPointer",
+            "glEdgeFlagPointerEXT",
+            "glFogCoordPointer",
+            "glFogCoordPointerEXT",
             "glIndexPointer",
+            "glIndexPointerEXT",
+            "glMatrixIndexPointerARB",
             "glNormalPointer",
-            "glTexCoordPointer",
-            "glVertexPointer",
-            "glFogCoordPointer",
+            "glNormalPointerEXT",
             "glSecondaryColorPointer",
+            "glSecondaryColorPointerEXT",
+            "glTexCoordPointer",
+            "glTexCoordPointerEXT",
+            "glVertexAttribLPointer",
             "glVertexAttribPointer",
+            "glVertexAttribPointerARB",
+            "glVertexAttribPointerNV",
+            "glVertexPointer",
+            "glVertexPointerEXT",
         ] and arg.name == 'pointer':
             self.extract_pointer(function, arg, arg_type, lvalue, rvalue)
         else:
@@ -86,17 +101,24 @@ class GlRetracer(Retracer):
 
 
 if __name__ == '__main__':
-    print
-    print '#include <string.h>'
-    print '#include <iostream>'
-    print
-    print '#include "glproc.hpp"'
-    print '#include <GL/glut.h>'
-    print
-    print 'static bool double_buffer = false;'
-    print 'static bool insideGlBeginEnd = false;'
-    print
-    print '''
+    print r'''
+#include <string.h>
+#include <iostream>
+
+#include "glproc.hpp"
+#include <GL/glut.h>
+
+static bool double_buffer = false;
+static bool insideGlBeginEnd = false;
+
+static int __window_width = 256, __window_height = 256;
+bool __reshape_window = false;
+
+unsigned __frame = 0;
+long long __startTime = 0;
+bool __screenshots = 0;
+
+
 static void
 checkGlError(void) {
     if (insideGlBeginEnd) {
@@ -138,23 +160,43 @@ checkGlError(void) {
         std::cerr << error;
         break;
     }
-    std::cerr << "\\n";
+    std::cerr << "\n";
 }
 '''
     api = glapi.glapi
     retracer = GlRetracer()
     retracer.retrace_api(glapi.glapi)
-    print '''
+    print r'''
 
 static Trace::Parser parser;
 
+static void display_noop(void) {
+}
+
+#include "bmp.hpp"
+
+static void frame_complete(void) {
+    ++__frame;
+    
+    if (__screenshots && !__reshape_window) {
+        char filename[PATH_MAX];
+        snprintf(filename, sizeof filename, "screenshot_%04u.bmp", __frame);
+        unsigned char *pixels = new unsigned char[__window_height*__window_width*4];
+        glReadPixels(0, 0, __window_width, __window_height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
+        BMP::write(filename, pixels, __window_width, __window_height, __window_width*4);
+    }
+
+}
+
 static void display(void) {
     Trace::Call *call;
 
     while ((call = parser.parse_call())) {
         if (call->name() == "glFlush") {
             glFlush();
-            return;
+            if (!double_buffer) {
+                frame_complete();
+            }
         }
         
         if (!retrace_call(*call)) {
@@ -164,16 +206,33 @@ static void display(void) {
                     glutSwapBuffers();
                 else
                     glFlush();
+                frame_complete();
                 return;
             }
         }
     }
 
+    // Reached the end of trace
     glFlush();
+
+    long long endTime = OS::GetTime();
+    float timeInterval = (endTime - __startTime) * 1.0E-6;
+
+    std::cout << 
+        "Rendered " << __frame << " frames"
+        " in " <<  timeInterval << " secs,"
+        " average of " << (__frame/timeInterval) << " fps\n";
+
+    glutDisplayFunc(&display_noop);
     glutIdleFunc(NULL);
 }
 
 static void idle(void) {
+    if (__reshape_window) {
+        // XXX: doesn't quite work
+        glutReshapeWindow(__window_width, __window_height);
+        __reshape_window = false;
+    }
     glutPostRedisplay();
 }
 
@@ -190,20 +249,21 @@ int main(int argc, char **argv)
 
         if (!strcmp(arg, "--")) {
             break;
-        }
-        else if (!strcmp(arg, "-db")) {
+        } else if (!strcmp(arg, "-db")) {
             double_buffer = true;
+        } else if (!strcmp(arg, "-s")) {
+            __screenshots = true;
         } else if (!strcmp(arg, "-v")) {
             ++verbosity;
         } else {
-            std::cerr << "error: unknown option " << arg << "\\n";
+            std::cerr << "error: unknown option " << arg << "\n";
             return 1;
         }
     }
 
     glutInit(&argc, argv);
     glutInitWindowPosition(0, 0);
-    glutInitWindowSize(800, 600);
+    glutInitWindowSize(__window_width, __window_height);
     glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | (double_buffer ? GLUT_DOUBLE : GLUT_SINGLE));
     glutCreateWindow(argv[0]);
 
@@ -216,6 +276,7 @@ int main(int argc, char **argv)
 
     for ( ; i < argc; ++i) {
         if (parser.open(argv[i])) {
+            __startTime = OS::GetTime();
             glutMainLoop();
             parser.close();
         }