- 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 void
+getCurrentTimes(int64_t& cpuTime, int64_t& gpuTime) {
+ GLuint query;
+
+ if (retrace::profilingGpuTimes && supportsTimestamp) {
+ glGenQueries(1, &query);
+ glQueryCounter(query, GL_TIMESTAMP);
+ glGetQueryObjecti64vEXT(query, GL_QUERY_RESULT, &gpuTime);
+ } else {
+ gpuTime = 0;
+ }
+
+ if (retrace::profilingCpuTimes) {
+ cpuTime = os::getTime();
+ } 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[0], GL_QUERY_RESULT, &gpuStart);
+ }
+
+ glGetQueryObjecti64vEXT(query.ids[1], GL_QUERY_RESULT, &gpuDuration);
+ }
+
+ if (retrace::profilingPixelsDrawn) {
+ glGetQueryObjecti64vEXT(query.ids[2], GL_QUERY_RESULT, &pixels);
+ }
+
+ glDeleteQueries(3, query.ids);
+ } else {
+ pixels = -1;
+ }
+
+ if (retrace::profilingCpuTimes) {
+ cpuDuration = query.cpuEnd - query.cpuStart;
+ }
+
+ /* 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) {
+ /* Create call query */
+ CallQuery query;
+ query.isDraw = isDraw;
+ query.call = call.no;
+ query.sig = call.sig;
+ query.program = glretrace::currentContext ? glretrace::currentContext->activeProgram : 0;
+
+ /* GPU profiling only for draw calls */
+ if (isDraw) {
+ glGenQueries(3, query.ids);
+
+ if (retrace::profilingGpuTimes) {
+ if (supportsTimestamp) {
+ glQueryCounter(query.ids[0], GL_TIMESTAMP);
+ }
+
+ glBeginQuery(GL_TIME_ELAPSED, query.ids[1]);
+ }
+
+ if (retrace::profilingPixelsDrawn) {
+ glBeginQuery(GL_SAMPLES_PASSED, query.ids[2]);
+ }
+ }
+
+ callQueries.push_back(query);
+
+ /* CPU profiling for all calls */
+ if (retrace::profilingCpuTimes) {
+ callQueries.back().cpuStart = os::getTime();
+ }
+}
+
+void
+endProfile(trace::Call &call, bool isDraw) {
+ GLint64 time = os::getTime();
+
+ /* CPU profiling for all calls */
+ if (retrace::profilingCpuTimes) {
+ CallQuery& query = callQueries.back();
+ query.cpuEnd = time;
+ }
+
+ /* GPU profiling only for draw calls */
+ if (isDraw) {
+ if (retrace::profilingGpuTimes) {
+ glEndQuery(GL_TIME_ELAPSED);
+ }
+
+ if (retrace::profilingPixelsDrawn) {
+ glEndQuery(GL_SAMPLES_PASSED);
+ }