]> 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 c75d3683ec1fec3b9421a24025ebcbff3b498e88..6dd44eba76060e1ef0513695f6374dae2a370420 100644 (file)
@@ -34,6 +34,7 @@
 
 namespace glretrace {
 
+bool insideList = false;
 bool insideGlBeginEnd = false;
 
 
@@ -82,19 +83,108 @@ checkGlError(trace::Call &call) {
     os << "\n";
 }
 
+struct CallQuery
+{
+   GLuint ids[2];
+   unsigned call;
+   const trace::FunctionSig *sig;
+};
+
+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();
+    }
+
     retrace::frameComplete(call);
 
+    /* Indicate start of next frame */
+    frame_start();
+
     if (!currentDrawable) {
         return;
     }
 
-    if (!currentDrawable->visible) {
+    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;
+
+      /* 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);
+
+      /* 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();
+   }
+
+   CallQuery query;
+   query.call = call.no;
+   query.sig = call.sig;
+
+   /* Create start and duration queries */
+   glGenQueries(2, query.ids);
+   glQueryCounter(query.ids[0], GL_TIMESTAMP);
+   glBeginQuery(GL_TIME_ELAPSED, query.ids[1]);
+
+   callQueries.push_back(query);
+}
+
+void
+endProfileGPU(trace::Call &call) {
+   glEndQuery(GL_TIME_ELAPSED);
+}
 
 } /* namespace glretrace */