+static inline int64_t
+getCurrentTime(void) {
+ if (retrace::profilingGpuTimes && supportsTimestamp) {
+ /* Get the current GL time without stalling */
+ GLint64 timestamp = 0;
+ glGetInteger64v(GL_TIMESTAMP, ×tamp);
+ return timestamp;
+ } else {
+ return os::getTime();
+ }
+}
+
+static inline int64_t
+getTimeFrequency(void) {
+ if (retrace::profilingGpuTimes && supportsTimestamp) {
+ return 1000000000;
+ } else {
+ return os::timeFrequency;
+ }
+}
+
+static void
+getCurrentTimes(int64_t& cpuTime, int64_t& gpuTime) {
+ GLuint query = 0;
+
+ if (retrace::profilingGpuTimes && supportsTimestamp) {
+ glGenQueries(1, &query);
+ glQueryCounter(query, GL_TIMESTAMP);
+ GLint64 timestamp = 0;
+ glGetQueryObjecti64vEXT(query, GL_QUERY_RESULT, ×tamp);
+ gpuTime = timestamp;
+ } else {
+ gpuTime = 0;
+ }
+
+ if (retrace::profilingCpuTimes) {
+ cpuTime = getCurrentTime();
+ } else {
+ cpuTime = 0;
+ }
+
+ if (retrace::profilingGpuTimes && supportsTimestamp) {
+ glDeleteQueries(1, &query);
+ }
+}
+
+static void
+completeCallQuery(CallQuery& query) {
+ /* Get call start and duration */
+ int64_t gpuStart = 0, gpuDuration = 0, cpuDuration = 0, pixels = 0;
+
+ if (query.isDraw) {
+ if (retrace::profilingGpuTimes) {
+ if (supportsTimestamp) {
+ glGetQueryObjecti64vEXT(query.ids[GPU_START], GL_QUERY_RESULT, &gpuStart);
+ }
+
+ glGetQueryObjecti64vEXT(query.ids[GPU_DURATION], GL_QUERY_RESULT, &gpuDuration);
+ }
+
+ if (retrace::profilingPixelsDrawn) {
+ glGetQueryObjecti64vEXT(query.ids[OCCLUSION], GL_QUERY_RESULT, &pixels);
+ }
+
+ } else {
+ pixels = -1;
+ }
+
+ if (retrace::profilingCpuTimes) {
+ cpuDuration = query.cpuEnd - query.cpuStart;
+ }
+
+ glDeleteQueries(NUM_QUERIES, query.ids);
+
+ /* Add call to profile */
+ retrace::profiler.addCall(query.call, query.sig->name, query.program, pixels, gpuStart, gpuDuration, query.cpuStart, cpuDuration);
+}
+
+void
+flushQueries() {
+ for (std::list<CallQuery>::iterator itr = callQueries.begin(); itr != callQueries.end(); ++itr) {
+ completeCallQuery(*itr);
+ }
+
+ callQueries.clear();
+}
+
+void
+beginProfile(trace::Call &call, bool isDraw) {
+ glretrace::Context *currentContext = glretrace::getCurrentContext();
+
+ /* Create call query */
+ CallQuery query;
+ query.isDraw = isDraw;
+ query.call = call.no;
+ query.sig = call.sig;
+ query.program = currentContext ? currentContext->activeProgram : 0;
+
+ glGenQueries(NUM_QUERIES, query.ids);
+
+ /* GPU profiling only for draw calls */
+ if (isDraw) {
+ if (retrace::profilingGpuTimes) {
+ if (supportsTimestamp) {
+ glQueryCounter(query.ids[GPU_START], GL_TIMESTAMP);
+ }
+
+ glBeginQuery(GL_TIME_ELAPSED, query.ids[GPU_DURATION]);
+ }
+
+ if (retrace::profilingPixelsDrawn) {
+ glBeginQuery(GL_SAMPLES_PASSED, query.ids[OCCLUSION]);
+ }
+ }
+
+ callQueries.push_back(query);
+
+ /* CPU profiling for all calls */
+ if (retrace::profilingCpuTimes) {
+ CallQuery& query = callQueries.back();
+ query.cpuStart = getCurrentTime();
+ }
+}
+
+void
+endProfile(trace::Call &call, bool isDraw) {
+
+ /* CPU profiling for all calls */
+ if (retrace::profilingCpuTimes) {
+ CallQuery& query = callQueries.back();
+ query.cpuEnd = getCurrentTime();
+ }
+
+ /* GPU profiling only for draw calls */
+ if (isDraw) {
+ if (retrace::profilingGpuTimes) {
+ glEndQuery(GL_TIME_ELAPSED);
+ }
+
+ if (retrace::profilingPixelsDrawn) {
+ glEndQuery(GL_SAMPLES_PASSED);
+ }
+ }
+}
+
+void
+initContext() {
+ glretrace::Context *currentContext = glretrace::getCurrentContext();
+
+ /* Ensure we have adequate extension support */
+ assert(currentContext);
+ supportsTimestamp = currentContext->hasExtension("GL_ARB_timer_query");
+ supportsElapsed = currentContext->hasExtension("GL_EXT_timer_query") || supportsTimestamp;
+ supportsOcclusion = currentContext->hasExtension("GL_ARB_occlusion_query");
+ supportsDebugOutput = currentContext->hasExtension("GL_ARB_debug_output");
+
+ /* Check for timer query support */
+ if (retrace::profilingGpuTimes) {
+ if (!supportsTimestamp && !supportsElapsed) {
+ std::cout << "Error: Cannot run profile, GL_EXT_timer_query extension is not supported." << std::endl;
+ exit(-1);
+ }
+
+ GLint bits = 0;
+ glGetQueryiv(GL_TIME_ELAPSED, GL_QUERY_COUNTER_BITS, &bits);
+
+ if (!bits) {
+ std::cout << "Error: Cannot run profile, GL_QUERY_COUNTER_BITS == 0." << std::endl;
+ exit(-1);
+ }
+ }
+
+ /* Check for occlusion query support */
+ if (retrace::profilingPixelsDrawn && !supportsOcclusion) {
+ std::cout << "Error: Cannot run profile, GL_ARB_occlusion_query extension is not supported." << std::endl;
+ exit(-1);
+ }
+
+ /* Setup debug message call back */
+ if (retrace::debug && supportsDebugOutput) {
+ glretrace::Context *currentContext = glretrace::getCurrentContext();
+ glDebugMessageCallbackARB(&debugOutputCallback, currentContext);
+
+ if (DEBUG_OUTPUT_SYNCHRONOUS) {
+ glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
+ }
+ }
+
+ /* Sync the gpu and cpu start times */
+ if (retrace::profilingCpuTimes || retrace::profilingGpuTimes) {
+ if (!retrace::profiler.hasBaseTimes()) {
+ GLint64 gpuTime, cpuTime;
+
+ getCurrentTimes(cpuTime, gpuTime);
+ retrace::profiler.setBaseCpuTime(cpuTime);
+ retrace::profiler.setBaseGpuTime(gpuTime);
+ }