- std::ostream & os = retrace::warning(call);
-
- os << "glGetError(";
- os << call.name();
- os << ") = ";
-
- switch (error) {
- case GL_INVALID_ENUM:
- os << "GL_INVALID_ENUM";
- break;
- case GL_INVALID_VALUE:
- os << "GL_INVALID_VALUE";
- break;
- case GL_INVALID_OPERATION:
- os << "GL_INVALID_OPERATION";
- break;
- case GL_STACK_OVERFLOW:
- os << "GL_STACK_OVERFLOW";
- break;
- case GL_STACK_UNDERFLOW:
- os << "GL_STACK_UNDERFLOW";
- break;
- case GL_OUT_OF_MEMORY:
- os << "GL_OUT_OF_MEMORY";
- break;
- case GL_INVALID_FRAMEBUFFER_OPERATION:
- os << "GL_INVALID_FRAMEBUFFER_OPERATION";
- break;
- case GL_TABLE_TOO_LARGE:
- os << "GL_TABLE_TOO_LARGE";
- break;
- default:
- os << error;
- break;
+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 inline void
+getCurrentVsize(int64_t& vsize) {
+ vsize = os::getVsize();
+}
+
+static inline void
+getCurrentRss(int64_t& rss) {
+ rss = os::getRss();
+}
+
+static void
+completeCallQuery(CallQuery& query) {
+ /* Get call start and duration */
+ int64_t gpuStart = 0, gpuDuration = 0, cpuDuration = 0, pixels = 0, vsizeDuration = 0, rssDuration = 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) {
+ double cpuTimeScale = 1.0E9 / getTimeFrequency();
+ cpuDuration = (query.cpuEnd - query.cpuStart) * cpuTimeScale;
+ query.cpuStart *= cpuTimeScale;
+ }
+
+ if (retrace::profilingMemoryUsage) {
+ vsizeDuration = query.vsizeEnd - query.vsizeStart;
+ rssDuration = query.rssEnd - query.rssStart;
+ }
+
+ 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, query.vsizeStart, vsizeDuration, query.rssStart, rssDuration);
+}
+
+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();
+ }
+
+ if (retrace::profilingMemoryUsage) {
+ CallQuery& query = callQueries.back();
+ query.vsizeStart = os::getVsize();
+ query.rssStart = os::getRss();