]> git.cworth.org Git - apitrace/blobdiff - glretrace_main.cpp
Merge branch 'master' into d3dretrace
[apitrace] / glretrace_main.cpp
index 6d42cb546e3564645bd512ce56f3b7cd58407dab..16d3ec0de8599fd3ce5cf0379f4439b35578eeb6 100644 (file)
 
 #include <string.h>
 
+#include "os_binary.hpp"
+#include "os_string.hpp"
+#include "os_time.hpp"
 #include "image.hpp"
 #include "retrace.hpp"
+#include "trace_callset.hpp"
 #include "glproc.hpp"
+#include "glstate.hpp"
 #include "glretrace.hpp"
 
 
 namespace glretrace {
 
-bool double_buffer = false;
+bool double_buffer = true;
 bool insideGlBeginEnd = false;
-Trace::Parser parser;
-glws::WindowSystem *ws = NULL;
-glws::Visual *visual = NULL;
+glws::Profile defaultProfile = glws::PROFILE_COMPAT;
+glws::Visual *visual[glws::PROFILE_MAX];
 glws::Drawable *drawable = NULL;
 glws::Context *context = NULL;
 
-int window_width = 256, window_height = 256;
-
 unsigned frame = 0;
 long long startTime = 0;
 bool wait = false;
 
 bool benchmark = false;
-const char *compare_prefix = NULL;
-const char *snapshot_prefix = NULL;
+static const char *compare_prefix = NULL;
+static const char *snapshot_prefix = NULL;
+static trace::CallSet snapshot_frequency;
+static trace::CallSet compare_frequency;
 
 unsigned dump_state = ~0;
 
 void
-checkGlError(int callIdx) {
-    if (benchmark || insideGlBeginEnd) {
-        return;
-    }
-
+checkGlError(trace::Call &call) {
     GLenum error = glGetError();
     if (error == GL_NO_ERROR) {
         return;
     }
 
-    if (callIdx >= 0) {
-        std::cerr << callIdx << ": ";
-    }
+    std::ostream & os = retrace::warning(call);
+
+    os << "glGetError(";
+    os << call.name();
+    os << ") = ";
 
-    std::cerr << "warning: glGetError() = ";
     switch (error) {
     case GL_INVALID_ENUM:
-        std::cerr << "GL_INVALID_ENUM";
+        os << "GL_INVALID_ENUM";
         break;
     case GL_INVALID_VALUE:
-        std::cerr << "GL_INVALID_VALUE";
+        os << "GL_INVALID_VALUE";
         break;
     case GL_INVALID_OPERATION:
-        std::cerr << "GL_INVALID_OPERATION";
+        os << "GL_INVALID_OPERATION";
         break;
     case GL_STACK_OVERFLOW:
-        std::cerr << "GL_STACK_OVERFLOW";
+        os << "GL_STACK_OVERFLOW";
         break;
     case GL_STACK_UNDERFLOW:
-        std::cerr << "GL_STACK_UNDERFLOW";
+        os << "GL_STACK_UNDERFLOW";
         break;
     case GL_OUT_OF_MEMORY:
-        std::cerr << "GL_OUT_OF_MEMORY";
+        os << "GL_OUT_OF_MEMORY";
         break;
     case GL_INVALID_FRAMEBUFFER_OPERATION:
-        std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
+        os << "GL_INVALID_FRAMEBUFFER_OPERATION";
         break;
     case GL_TABLE_TOO_LARGE:
-        std::cerr << "GL_TABLE_TOO_LARGE";
+        os << "GL_TABLE_TOO_LARGE";
         break;
     default:
-        std::cerr << error;
+        os << error;
         break;
     }
-    std::cerr << "\n";
+    os << "\n";
 }
 
+/**
+ * Grow the current drawble.
+ *
+ * We need to infer the drawable size from GL calls because the drawable sizes
+ * are specified by OS specific calls which we do not trace.
+ */
+void
+updateDrawable(int width, int height) {
+    if (!drawable) {
+        return;
+    }
+
+    if (drawable->visible &&
+        width  <= drawable->width &&
+        height <= drawable->height) {
+        return;
+    }
+
+    // Ignore zero area viewports
+    if (width == 0 || height == 0) {
+        return;
+    }
 
-static void snapshot(Image::Image &image) {
-    GLint drawbuffer = double_buffer ? GL_BACK : GL_FRONT;
-    GLint readbuffer = double_buffer ? GL_BACK : GL_FRONT;
-    glGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);
-    glGetIntegerv(GL_READ_BUFFER, &readbuffer);
-    glReadBuffer(drawbuffer);
-    glReadPixels(0, 0, image.width, image.height, GL_RGBA, GL_UNSIGNED_BYTE, image.pixels);
-    checkGlError();
-    glReadBuffer(readbuffer);
+    // Check for bound framebuffer last, as this may have a performance impact.
+    GLint draw_framebuffer = 0;
+    glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
+    if (draw_framebuffer != 0) {
+        return;
+    }
+
+    drawable->resize(width, height);
+    drawable->show();
+
+    glScissor(0, 0, width, height);
 }
 
 
-static void frame_complete(unsigned call_no) {
-    ++frame;
-    
-    if (snapshot_prefix || compare_prefix) {
-        Image::Image *ref = NULL;
-        if (compare_prefix) {
-            char filename[PATH_MAX];
-            snprintf(filename, sizeof filename, "%s%010u.png", compare_prefix, call_no);
-            ref = Image::readPNG(filename);
-            if (!ref) {
-                return;
-            }
-            if (retrace::verbosity >= 0)
-                std::cout << "Read " << filename << "\n";
+static void
+snapshot(unsigned call_no) {
+    assert(snapshot_prefix || compare_prefix);
+
+    if (!drawable) {
+        return;
+    }
+
+    image::Image *ref = NULL;
+
+    if (compare_prefix) {
+        os::String filename = os::String::format("%s%010u.png", compare_prefix, call_no);
+        ref = image::readPNG(filename);
+        if (!ref) {
+            return;
+        }
+        if (retrace::verbosity >= 0) {
+            std::cout << "Read " << filename << "\n";
         }
-        
-        Image::Image src(window_width, window_height, true);
-        snapshot(src);
-
-        if (snapshot_prefix) {
-            char filename[PATH_MAX];
-            snprintf(filename, sizeof filename, "%s%010u.png", snapshot_prefix, call_no);
-            if (src.writePNG(filename) && retrace::verbosity >= 0) {
+    }
+
+    image::Image *src = glstate::getDrawBufferImage();
+    if (!src) {
+        return;
+    }
+
+    if (snapshot_prefix) {
+        if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
+            char comment[21];
+            snprintf(comment, sizeof comment, "%u", call_no);
+            src->writePNM(std::cout, comment);
+        } else {
+            os::String filename = os::String::format("%s%010u.png", snapshot_prefix, call_no);
+            if (src->writePNG(filename) && retrace::verbosity >= 0) {
                 std::cout << "Wrote " << filename << "\n";
             }
         }
+    }
 
-        if (ref) {
-            std::cout << "Snapshot " << call_no << " average precision of " << src.compare(*ref) << " bits\n";
-            delete ref;
-        }
+    if (ref) {
+        std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
+        delete ref;
     }
 
+    delete src;
+}
+
+
+void frame_complete(trace::Call &call) {
+    ++frame;
+
+    if (!drawable) {
+        return;
+    }
+
+    if (!drawable->visible) {
+        retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
+    }
 }
 
 
 static void display(void) {
-    Trace::Call *call;
+    retrace::Retracer retracer;
+
+    retracer.addCallbacks(gl_callbacks);
+    retracer.addCallbacks(glx_callbacks);
+    retracer.addCallbacks(wgl_callbacks);
+    retracer.addCallbacks(cgl_callbacks);
+    retracer.addCallbacks(egl_callbacks);
+
+    startTime = os::getTime();
+    trace::Call *call;
 
     while ((call = parser.parse_call())) {
-        const std::string &name = call->name();
-
-        if ((name[0] == 'w' && name[1] == 'g' && name[2] == 'l') ||
-            (name[0] == 'g' && name[1] == 'l' && name[2] == 'X')) {
-            // XXX: We ignore the majority of the OS-specific calls for now
-            if (name == "glXSwapBuffers" ||
-                name == "wglSwapBuffers") {
-                if (retrace::verbosity >= 1) {
-                    std::cout << *call;
-                    std::cout.flush();
-                };
-                frame_complete(call->no);
-                if (double_buffer)
-                    drawable->swapBuffers();
-                else
-                    glFlush();
-            } else if (name == "glXMakeCurrent" ||
-                       name == "wglMakeCurrent") {
-                glFlush();
-                if (!double_buffer) {
-                    frame_complete(call->no);
-                }
+        bool swapRenderTarget = call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET;
+        bool doSnapshot =
+            snapshot_frequency.contains(*call) ||
+            compare_frequency.contains(*call)
+        ;
+
+        // For calls which cause rendertargets to be swaped, we take the
+        // snapshot _before_ swapping the rendertargets.
+        if (doSnapshot && swapRenderTarget) {
+            if (call->flags & trace::CALL_FLAG_END_FRAME) {
+                // For swapbuffers/presents we still use this call number,
+                // spite not have been executed yet.
+                snapshot(call->no);
             } else {
-                continue;
+                // Whereas for ordinate fbo/rendertarget changes we use the
+                // previous call's number.
+                snapshot(call->no - 1);
             }
         }
 
-        if (name == "glFlush") {
-            glFlush();
-            if (!double_buffer) {
-                frame_complete(call->no);
-            }
+        retracer.retrace(*call);
+
+        if (doSnapshot && !swapRenderTarget) {
+            snapshot(call->no);
         }
-        
-        retrace::retrace_call(*call);
 
-        if (!insideGlBeginEnd && call->no >= dump_state) {
-            state_dump(std::cout);
+        if (!insideGlBeginEnd &&
+            drawable && context &&
+            call->no >= dump_state) {
+            glstate::dumpCurrentContext(std::cout);
             exit(0);
         }
 
@@ -202,10 +253,10 @@ static void display(void) {
     // Reached the end of trace
     glFlush();
 
-    long long endTime = OS::GetTime();
-    float timeInterval = (endTime - startTime) * 1.0E-6;
+    long long endTime = os::getTime();
+    float timeInterval = (endTime - startTime) * (1.0 / os::timeFrequency);
 
-    if (retrace::verbosity >= -1) { 
+    if ((retrace::verbosity >= -1) || (retrace::profiling)) {
         std::cout << 
             "Rendered " << frame << " frames"
             " in " <<  timeInterval << " secs,"
@@ -213,7 +264,7 @@ static void display(void) {
     }
 
     if (wait) {
-        while (ws->processEvents()) {}
+        while (glws::processEvents()) {}
     } else {
         exit(0);
     }
@@ -225,11 +276,16 @@ static void usage(void) {
         "Usage: glretrace [OPTION] TRACE\n"
         "Replay TRACE.\n"
         "\n"
-        "  -b           benchmark (no glgeterror; no messages)\n"
+        "  -b           benchmark mode (no error checking or warning messages)\n"
+        "  -p           profiling mode (run whole trace, dump profiling info)\n"
         "  -c PREFIX    compare against snapshots\n"
-        "  -db          use a double buffer visual\n"
-        "  -s PREFIX    take snapshots\n"
-        "  -v           verbose output\n"
+        "  -C CALLSET   calls to compare (default is every frame)\n"
+        "  -core        use core profile\n"
+        "  -db          use a double buffer visual (default)\n"
+        "  -sb          use a single buffer visual\n"
+        "  -s PREFIX    take snapshots; `-` for PNM stdout output\n"
+        "  -S CALLSET   calls to snapshot (default is every frame)\n"
+        "  -v           increase output verbosity\n"
         "  -D CALLNO    dump state at specific call no\n"
         "  -w           wait on final frame\n";
 }
@@ -237,6 +293,8 @@ static void usage(void) {
 extern "C"
 int main(int argc, char **argv)
 {
+    assert(compare_frequency.empty());
+    assert(snapshot_frequency.empty());
 
     int i;
     for (i = 1; i < argc; ++i) {
@@ -251,18 +309,47 @@ int main(int argc, char **argv)
         } else if (!strcmp(arg, "-b")) {
             benchmark = true;
             retrace::verbosity = -1;
+            glws::debug = false;
+        } else if (!strcmp(arg, "-p")) {
+            retrace::profiling = true;
+            retrace::verbosity = -1;
+            glws::debug = false;
         } else if (!strcmp(arg, "-c")) {
             compare_prefix = argv[++i];
+            if (compare_frequency.empty()) {
+                compare_frequency = trace::CallSet(trace::FREQUENCY_FRAME);
+            }
+        } else if (!strcmp(arg, "-C")) {
+            compare_frequency = trace::CallSet(argv[++i]);
+            if (compare_prefix == NULL) {
+                compare_prefix = "";
+            }
         } else if (!strcmp(arg, "-D")) {
             dump_state = atoi(argv[++i]);
             retrace::verbosity = -2;
+        } else if (!strcmp(arg, "-core")) {
+            defaultProfile = glws::PROFILE_CORE;
         } else if (!strcmp(arg, "-db")) {
             double_buffer = true;
+        } else if (!strcmp(arg, "-sb")) {
+            double_buffer = false;
         } else if (!strcmp(arg, "--help")) {
             usage();
             return 0;
         } else if (!strcmp(arg, "-s")) {
             snapshot_prefix = argv[++i];
+            if (snapshot_frequency.empty()) {
+                snapshot_frequency = trace::CallSet(trace::FREQUENCY_FRAME);
+            }
+            if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
+                os::setBinaryMode(stdout);
+                retrace::verbosity = -2;
+            }
+        } else if (!strcmp(arg, "-S")) {
+            snapshot_frequency = trace::CallSet(argv[++i]);
+            if (snapshot_prefix == NULL) {
+                snapshot_prefix = "";
+            }
         } else if (!strcmp(arg, "-v")) {
             ++retrace::verbosity;
         } else if (!strcmp(arg, "-w")) {
@@ -274,21 +361,29 @@ int main(int argc, char **argv)
         }
     }
 
-    ws = glws::createNativeWindowSystem();
-    visual = ws->createVisual(double_buffer);
-    drawable = ws->createDrawable(visual);
-    drawable->resize(window_width, window_height);
-    context = ws->createContext(visual);
-    ws->makeCurrent(drawable, context);
+    glws::init();
+    visual[glws::PROFILE_COMPAT] = glws::createVisual(double_buffer, glws::PROFILE_COMPAT);
+    visual[glws::PROFILE_CORE] = glws::createVisual(double_buffer, glws::PROFILE_CORE);
+    visual[glws::PROFILE_ES1] = glws::createVisual(double_buffer, glws::PROFILE_ES1);
+    visual[glws::PROFILE_ES2] = glws::createVisual(double_buffer, glws::PROFILE_ES2);
 
     for ( ; i < argc; ++i) {
-        if (parser.open(argv[i])) {
-            startTime = OS::GetTime();
-            display();
-            parser.close();
+        if (!parser.open(argv[i])) {
+            std::cerr << "error: failed to open " << argv[i] << "\n";
+            return 1;
         }
+
+        display();
+
+        parser.close();
     }
 
+    for (int n = 0; n < glws::PROFILE_MAX; n++) {
+        delete visual[n];
+    }
+
+    glws::cleanup();
+
     return 0;
 }