]> git.cworth.org Git - apitrace/blobdiff - retrace/glretrace_main.cpp
Add support for basic GPU profiling of draw calls to retrace.
[apitrace] / retrace / glretrace_main.cpp
index 429208b009ec3204da174577720605aa6a8f21dc..6dd44eba76060e1ef0513695f6374dae2a370420 100644 (file)
 
 namespace glretrace {
 
+bool insideList = false;
 bool insideGlBeginEnd = false;
-glws::Profile defaultProfile = glws::PROFILE_COMPAT;
-glws::Visual *visual[glws::PROFILE_MAX];
-glws::Drawable *drawable = NULL;
-glws::Context *context = NULL;
+
 
 void
 checkGlError(trace::Call &call) {
@@ -85,71 +83,115 @@ checkGlError(trace::Call &call) {
     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;
-    }
+struct CallQuery
+{
+   GLuint ids[2];
+   unsigned call;
+   const trace::FunctionSig *sig;
+};
 
-    if (drawable->visible &&
-        width  <= drawable->width &&
-        height <= drawable->height) {
-        return;
+static std::vector<CallQuery> callQueries;
+static GLuint frameQueries[2] = { 0, 0 };
+
+void frame_start() {
+   if (retrace::profileGPU) {
+      glGenQueries(2, frameQueries);
+
+      /* Query frame start time */
+      glQueryCounter(frameQueries[0], GL_TIMESTAMP);
+   }
+}
+
+void frame_complete(trace::Call &call) {
+    if (retrace::profileGPU) {
+        /* Query frame end time */
+       glQueryCounter(frameQueries[1], GL_TIMESTAMP);
+
+       completeQueries();
     }
 
-    // Ignore zero area viewports
-    if (width == 0 || height == 0) {
+    retrace::frameComplete(call);
+
+    /* Indicate start of next frame */
+    frame_start();
+
+    if (!currentDrawable) {
         return;
     }
 
-    // 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;
+    if (retrace::debug && !currentDrawable->visible) {
+        retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
     }
+}
+
+void
+completeQueries()
+{
+   if (callQueries.size() == 0)
+      return;
+
+   GLint available;
+   GLuint64 frameBegin, frameEnd;
+
+   /* Wait for frame to finish */
+   do {
+      glGetQueryObjectiv(frameQueries[1], GL_QUERY_RESULT_AVAILABLE, &available);
+   } while(!available);
+
+   /* Get frame start and end */
+   glGetQueryObjectui64vEXT(frameQueries[0], GL_QUERY_RESULT, &frameBegin);
+   glGetQueryObjectui64vEXT(frameQueries[1], GL_QUERY_RESULT, &frameEnd);
+   glDeleteQueries(2, frameQueries);
+
+   /* Add frame to profile */
+   retrace::profiler.addFrame(trace::Profiler::Frame(retrace::frameNo, frameBegin, frameEnd - frameBegin));
+
+   /* Loop through all active call queries */
+   for (std::vector<CallQuery>::iterator itr = callQueries.begin(); itr != callQueries.end(); ++itr) {
+      CallQuery& query = *itr;
+      GLuint64 timestamp, duration;
 
-    drawable->resize(width, height);
-    drawable->show();
+      /* Get queue start and duration */
+      glGetQueryObjectui64vEXT(query.ids[0], GL_QUERY_RESULT, &timestamp);
+      glGetQueryObjectui64vEXT(query.ids[1], GL_QUERY_RESULT, &duration);
+      glDeleteQueries(2, query.ids);
 
-    glScissor(0, 0, width, height);
+      /* Add call to profile */
+      retrace::profiler.addCall(trace::Profiler::Call(query.call, query.sig->name, timestamp, duration));
+   }
+
+   callQueries.clear();
 }
 
+void
+beginProfileGPU(trace::Call &call) {
+   if (frameQueries[0] == 0) {
+      frame_start();
+   }
 
-void frame_complete(trace::Call &call) {
-    retrace::frameComplete(call);
+   CallQuery query;
+   query.call = call.no;
+   query.sig = call.sig;
 
-    if (!drawable) {
-        return;
-    }
+   /* Create start and duration queries */
+   glGenQueries(2, query.ids);
+   glQueryCounter(query.ids[0], GL_TIMESTAMP);
+   glBeginQuery(GL_TIME_ELAPSED, query.ids[1]);
 
-    if (!drawable->visible) {
-        retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
-    }
+   callQueries.push_back(query);
 }
 
+void
+endProfileGPU(trace::Call &call) {
+   glEndQuery(GL_TIME_ELAPSED);
+}
 
 } /* namespace glretrace */
 
 
 void
 retrace::setUp(void) {
-    if (retrace::coreProfile) {
-        glretrace::defaultProfile = glws::PROFILE_CORE;
-    }
-
     glws::init();
-
-    glretrace::visual[glws::PROFILE_COMPAT] = glws::createVisual(retrace::doubleBuffer, glws::PROFILE_COMPAT);
-    glretrace::visual[glws::PROFILE_CORE] = glws::createVisual(retrace::doubleBuffer, glws::PROFILE_CORE);
-    glretrace::visual[glws::PROFILE_ES1] = glws::createVisual(retrace::doubleBuffer, glws::PROFILE_ES1);
-    glretrace::visual[glws::PROFILE_ES2] = glws::createVisual(retrace::doubleBuffer, glws::PROFILE_ES2);
 }
 
 
@@ -166,7 +208,7 @@ retrace::addCallbacks(retrace::Retracer &retracer)
 
 image::Image *
 retrace::getSnapshot(void) {
-    if (!glretrace::drawable) {
+    if (!glretrace::currentDrawable) {
         return NULL;
     }
 
@@ -178,8 +220,8 @@ bool
 retrace::dumpState(std::ostream &os)
 {
     if (glretrace::insideGlBeginEnd ||
-        !glretrace::drawable ||
-        !glretrace::context) {
+        !glretrace::currentDrawable ||
+        !glretrace::currentContext) {
         return false;
     }
 
@@ -201,9 +243,5 @@ retrace::waitForInput(void) {
 
 void
 retrace::cleanUp(void) {
-    for (int n = 0; n < glws::PROFILE_MAX; n++) {
-        delete glretrace::visual[n];
-    }
-
     glws::cleanup();
 }