#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"
+#include "os_time.hpp"
namespace glretrace {
-bool double_buffer = true;
+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;
-unsigned frame = 0;
-long long startTime = 0;
-bool wait = false;
+struct CallQuery
+{
+ GLuint ids[3];
+ unsigned call;
+ GLuint program;
+ const trace::FunctionSig *sig;
+ uint64_t start;
+ uint64_t duration;
+};
+
+static bool supportsElapsed = true;
+static bool supportsTimestamp = true;
+static bool supportsOcclusion = true;
-static const char *compare_prefix = NULL;
-static const char *snapshot_prefix = NULL;
-static trace::CallSet snapshot_frequency;
-static trace::CallSet compare_frequency;
+static bool firstFrame = true;
+static std::list<CallQuery> callQueries;
-unsigned dump_state = ~0;
void
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;
+static GLuint64
+getGpuTimestamp() {
+ GLuint query = 0;
+ GLuint64 timestamp = 0;
+
+ if (retrace::profilingGpuTimes && supportsTimestamp) {
+ glGenQueries(1, &query);
+ glQueryCounter(query, GL_TIMESTAMP);
+ glGetQueryObjectui64vEXT(query, GL_QUERY_RESULT, ×tamp);
+ glDeleteQueries(1, &query);
}
- if (drawable->visible &&
- width <= drawable->width &&
- height <= drawable->height) {
- return;
+ return timestamp;
+}
+
+static GLuint64
+getCpuTimestamp() {
+ if (retrace::profilingCpuTimes) {
+ return os::getTime() * (1.0E9 / os::timeFrequency);
+ } else {
+ return 0;
}
+}
- // Ignore zero area viewports
- if (width == 0 || height == 0) {
- return;
+static void
+completeCallQuery(CallQuery& query) {
+ /* Get call start and duration */
+ GLuint64 timestamp = 0, duration = 0, samples = 0;
+
+ if (retrace::profilingGpuTimes) {
+ if (supportsTimestamp) {
+ glGetQueryObjectui64vEXT(query.ids[0], GL_QUERY_RESULT, ×tamp);
+ }
+
+ if (supportsElapsed) {
+ glGetQueryObjectui64vEXT(query.ids[1], GL_QUERY_RESULT, &duration);
+ }
}
- // 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::profilingPixelsDrawn && supportsOcclusion) {
+ glGetQueryObjectui64vEXT(query.ids[2], GL_QUERY_RESULT, &samples);
}
- drawable->resize(width, height);
- drawable->show();
+ glDeleteQueries(3, query.ids);
- glScissor(0, 0, width, height);
+ /* Add call to profile */
+ retrace::profiler.addCall(query.call, query.sig->name, query.program, samples, timestamp, duration, query.start, query.duration);
}
+void
+flushQueries() {
+ for (std::list<CallQuery>::iterator itr = callQueries.begin(); itr != callQueries.end(); ++itr) {
+ completeCallQuery(*itr);
+ }
-static void
-snapshot(unsigned call_no) {
- assert(snapshot_prefix || compare_prefix);
+ callQueries.clear();
+}
- if (!drawable) {
- return;
+void
+beginProfile(trace::Call &call) {
+ if (firstFrame) {
+ frame_start();
}
- image::Image *ref = NULL;
+ /* Create call query */
+ CallQuery query;
+ query.call = call.no;
+ query.sig = call.sig;
+ query.program = glretrace::currentContext ? glretrace::currentContext->activeProgram : 0;
- 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";
+ glGenQueries(3, query.ids);
+
+ if (retrace::profilingGpuTimes) {
+ if (supportsTimestamp) {
+ glQueryCounter(query.ids[0], GL_TIMESTAMP);
}
- }
- image::Image *src = glstate::getDrawBufferImage();
- if (!src) {
- return;
+ if (supportsElapsed) {
+ glBeginQuery(GL_TIME_ELAPSED, query.ids[1]);
+ }
}
- 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 (retrace::profilingPixelsDrawn && supportsOcclusion) {
+ glBeginQuery(GL_SAMPLES_PASSED, query.ids[2]);
}
- if (ref) {
- std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
- delete ref;
+ if (retrace::profilingCpuTimes) {
+ query.start = os::getTime();
}
- delete src;
+ callQueries.push_back(query);
}
+void
+endProfile(trace::Call &call) {
+ if (retrace::profilingCpuTimes) {
+ CallQuery& query = callQueries.back();
+ query.duration = (os::getTime() - query.start) * (1.0E9 / os::timeFrequency);
+ }
-void frame_complete(trace::Call &call) {
- ++frame;
-
- if (!drawable) {
- return;
+ if (retrace::profilingGpuTimes && supportsElapsed) {
+ glEndQuery(GL_TIME_ELAPSED);
}
- if (!drawable->visible) {
- retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
+ if (retrace::profilingPixelsDrawn && supportsOcclusion) {
+ glEndQuery(GL_SAMPLES_PASSED);
}
}
-
-static void display(void) {
- 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 = retrace::parser.parse_call())) {
- 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 {
- // Whereas for ordinate fbo/rendertarget changes we use the
- // previous call's number.
- snapshot(call->no - 1);
- }
+void
+initContext() {
+ /* Check for extension support */
+ const char* extensions = (const char*)glGetString(GL_EXTENSIONS);
+ GLint bits;
+
+ supportsTimestamp = glws::checkExtension("GL_ARB_timer_query", extensions);
+ supportsElapsed = glws::checkExtension("GL_EXT_timer_query", extensions) || supportsTimestamp;
+ supportsOcclusion = glws::checkExtension("GL_ARB_occlusion_query", extensions);
+
+ if (retrace::profilingGpuTimes) {
+ if (!supportsTimestamp && !supportsElapsed) {
+ std::cout << "Error: Cannot run profile, GL_EXT_timer_query extension is not supported." << std::endl;
+ exit(-1);
}
- retracer.retrace(*call);
+ glGetQueryiv(GL_TIME_ELAPSED, GL_QUERY_COUNTER_BITS, &bits);
- if (doSnapshot && !swapRenderTarget) {
- snapshot(call->no);
+ if (!bits) {
+ std::cout << "Error: Cannot run profile, GL_QUERY_COUNTER_BITS == 0." << std::endl;
+ exit(-1);
}
+ }
- if (!insideGlBeginEnd &&
- drawable && context &&
- call->no >= dump_state) {
- glstate::dumpCurrentContext(std::cout);
- exit(0);
- }
+ if (retrace::profilingPixelsDrawn && !supportsOcclusion) {
+ std::cout << "Error: Cannot run profile, GL_ARB_occlusion_query extension is not supported." << std::endl;
+ exit(-1);
+ }
+}
+
+void
+frame_start() {
+ firstFrame = false;
+
+ if (retrace::profiling) {
+ retrace::profiler.addFrameStart(retrace::frameNo, getGpuTimestamp(), getCpuTimestamp());
+ }
+}
- delete call;
+void
+frame_complete(trace::Call &call) {
+ if (retrace::profiling) {
+ /* Complete any remaining queries */
+ flushQueries();
+
+ /* Indicate end of current frame */
+ retrace::profiler.addFrameEnd(getGpuTimestamp(), getCpuTimestamp());
}
- // Reached the end of trace
- glFlush();
+ retrace::frameComplete(call);
- long long endTime = os::getTime();
- float timeInterval = (endTime - startTime) * (1.0 / os::timeFrequency);
+ /* Indicate start of next frame */
+ frame_start();
- if ((retrace::verbosity >= -1) || (retrace::profiling)) {
- std::cout <<
- "Rendered " << frame << " frames"
- " in " << timeInterval << " secs,"
- " average of " << (frame/timeInterval) << " fps\n";
+ if (!currentDrawable) {
+ return;
}
- if (wait) {
- while (glws::processEvents()) {}
- } else {
- exit(0);
+ if (retrace::debug && !currentDrawable->visible) {
+ retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
}
}
+} /* namespace glretrace */
+
-static void usage(void) {
- std::cout <<
- "Usage: glretrace [OPTION] TRACE\n"
- "Replay TRACE.\n"
- "\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"
- " -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";
+void
+retrace::setUp(void) {
+ glws::init();
}
-extern "C"
-int main(int argc, char **argv)
-{
- assert(compare_frequency.empty());
- assert(snapshot_frequency.empty());
- int i;
- for (i = 1; i < argc; ++i) {
- const char *arg = argv[i];
+void
+retrace::addCallbacks(retrace::Retracer &retracer)
+{
+ retracer.addCallbacks(glretrace::gl_callbacks);
+ retracer.addCallbacks(glretrace::glx_callbacks);
+ retracer.addCallbacks(glretrace::wgl_callbacks);
+ retracer.addCallbacks(glretrace::cgl_callbacks);
+ retracer.addCallbacks(glretrace::egl_callbacks);
+}
- if (arg[0] != '-') {
- break;
- }
- if (!strcmp(arg, "--")) {
- break;
- } else if (!strcmp(arg, "-b")) {
- retrace::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")) {
- wait = true;
- } else {
- std::cerr << "error: unknown option " << arg << "\n";
- usage();
- return 1;
- }
+image::Image *
+retrace::getSnapshot(void) {
+ if (!glretrace::currentDrawable) {
+ return NULL;
}
- 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 (!retrace::parser.open(argv[i])) {
- std::cerr << "error: failed to open " << argv[i] << "\n";
- return 1;
- }
+ return glstate::getDrawBufferImage();
+}
- display();
- retrace::parser.close();
+bool
+retrace::dumpState(std::ostream &os)
+{
+ if (glretrace::insideGlBeginEnd ||
+ !glretrace::currentDrawable ||
+ !glretrace::currentContext) {
+ return false;
}
- for (int n = 0; n < glws::PROFILE_MAX; n++) {
- delete visual[n];
- }
+ glstate::dumpCurrentContext(os);
- glws::cleanup();
+ return true;
+}
- return 0;
+void
+retrace::flushRendering(void) {
+ glretrace::flushQueries();
+ glFlush();
}
-} /* namespace glretrace */
+void
+retrace::waitForInput(void) {
+ while (glws::processEvents()) {
+ }
+}
+
+void
+retrace::cleanUp(void) {
+ glws::cleanup();
+}