1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
29 #include "retrace.hpp"
31 #include "glstate.hpp"
32 #include "glretrace.hpp"
33 #include "os_time.hpp"
35 /* Synchronous debug output may reduce performance however,
36 * without it the callNo in the callback may be inaccurate
37 * as the callback may be called at any time.
39 #define DEBUG_OUTPUT_SYNCHRONOUS 0
43 bool insideList = false;
44 bool insideGlBeginEnd = false;
55 GLuint ids[NUM_QUERIES];
59 const trace::FunctionSig *sig;
64 static bool supportsElapsed = true;
65 static bool supportsTimestamp = true;
66 static bool supportsOcclusion = true;
67 static bool supportsDebugOutput = true;
69 static std::list<CallQuery> callQueries;
72 debugOutputCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, GLvoid* userParam);
75 checkGlError(trace::Call &call) {
76 GLenum error = glGetError();
77 while (error != GL_NO_ERROR) {
78 std::ostream & os = retrace::warning(call);
86 os << "GL_INVALID_ENUM";
88 case GL_INVALID_VALUE:
89 os << "GL_INVALID_VALUE";
91 case GL_INVALID_OPERATION:
92 os << "GL_INVALID_OPERATION";
94 case GL_STACK_OVERFLOW:
95 os << "GL_STACK_OVERFLOW";
97 case GL_STACK_UNDERFLOW:
98 os << "GL_STACK_UNDERFLOW";
100 case GL_OUT_OF_MEMORY:
101 os << "GL_OUT_OF_MEMORY";
103 case GL_INVALID_FRAMEBUFFER_OPERATION:
104 os << "GL_INVALID_FRAMEBUFFER_OPERATION";
106 case GL_TABLE_TOO_LARGE:
107 os << "GL_TABLE_TOO_LARGE";
115 error = glGetError();
119 static inline int64_t
120 getCurrentTime(void) {
121 if (retrace::profilingGpuTimes && supportsTimestamp) {
122 /* Get the current GL time without stalling */
123 GLint64 timestamp = 0;
124 glGetInteger64v(GL_TIMESTAMP, ×tamp);
127 return os::getTime();
131 static inline int64_t
132 getTimeFrequency(void) {
133 if (retrace::profilingGpuTimes && supportsTimestamp) {
136 return os::timeFrequency;
141 getCurrentTimes(int64_t& cpuTime, int64_t& gpuTime) {
144 if (retrace::profilingGpuTimes && supportsTimestamp) {
145 glGenQueries(1, &query);
146 glQueryCounter(query, GL_TIMESTAMP);
147 GLint64 timestamp = 0;
148 glGetQueryObjecti64vEXT(query, GL_QUERY_RESULT, ×tamp);
154 if (retrace::profilingCpuTimes) {
155 cpuTime = getCurrentTime();
160 if (retrace::profilingGpuTimes && supportsTimestamp) {
161 glDeleteQueries(1, &query);
166 completeCallQuery(CallQuery& query) {
167 /* Get call start and duration */
168 int64_t gpuStart = 0, gpuDuration = 0, cpuDuration = 0, pixels = 0;
171 if (retrace::profilingGpuTimes) {
172 if (supportsTimestamp) {
173 glGetQueryObjecti64vEXT(query.ids[GPU_START], GL_QUERY_RESULT, &gpuStart);
176 glGetQueryObjecti64vEXT(query.ids[GPU_DURATION], GL_QUERY_RESULT, &gpuDuration);
179 if (retrace::profilingPixelsDrawn) {
180 glGetQueryObjecti64vEXT(query.ids[OCCLUSION], GL_QUERY_RESULT, &pixels);
187 if (retrace::profilingCpuTimes) {
188 cpuDuration = query.cpuEnd - query.cpuStart;
191 glDeleteQueries(NUM_QUERIES, query.ids);
193 /* Add call to profile */
194 retrace::profiler.addCall(query.call, query.sig->name, query.program, pixels, gpuStart, gpuDuration, query.cpuStart, cpuDuration);
199 for (std::list<CallQuery>::iterator itr = callQueries.begin(); itr != callQueries.end(); ++itr) {
200 completeCallQuery(*itr);
207 beginProfile(trace::Call &call, bool isDraw) {
208 glretrace::Context *currentContext = glretrace::getCurrentContext();
210 /* Create call query */
212 query.isDraw = isDraw;
213 query.call = call.no;
214 query.sig = call.sig;
215 query.program = currentContext ? currentContext->activeProgram : 0;
217 glGenQueries(NUM_QUERIES, query.ids);
219 /* GPU profiling only for draw calls */
221 if (retrace::profilingGpuTimes) {
222 if (supportsTimestamp) {
223 glQueryCounter(query.ids[GPU_START], GL_TIMESTAMP);
226 glBeginQuery(GL_TIME_ELAPSED, query.ids[GPU_DURATION]);
229 if (retrace::profilingPixelsDrawn) {
230 glBeginQuery(GL_SAMPLES_PASSED, query.ids[OCCLUSION]);
234 callQueries.push_back(query);
236 /* CPU profiling for all calls */
237 if (retrace::profilingCpuTimes) {
238 CallQuery& query = callQueries.back();
239 query.cpuStart = getCurrentTime();
244 endProfile(trace::Call &call, bool isDraw) {
246 /* CPU profiling for all calls */
247 if (retrace::profilingCpuTimes) {
248 CallQuery& query = callQueries.back();
249 query.cpuEnd = getCurrentTime();
252 /* GPU profiling only for draw calls */
254 if (retrace::profilingGpuTimes) {
255 glEndQuery(GL_TIME_ELAPSED);
258 if (retrace::profilingPixelsDrawn) {
259 glEndQuery(GL_SAMPLES_PASSED);
266 glretrace::Context *currentContext = glretrace::getCurrentContext();
268 /* Ensure we have adequate extension support */
269 assert(currentContext);
270 supportsTimestamp = currentContext->hasExtension("GL_ARB_timer_query");
271 supportsElapsed = currentContext->hasExtension("GL_EXT_timer_query") || supportsTimestamp;
272 supportsOcclusion = currentContext->hasExtension("GL_ARB_occlusion_query");
273 supportsDebugOutput = currentContext->hasExtension("GL_ARB_debug_output");
275 /* Check for timer query support */
276 if (retrace::profilingGpuTimes) {
277 if (!supportsTimestamp && !supportsElapsed) {
278 std::cout << "Error: Cannot run profile, GL_EXT_timer_query extension is not supported." << std::endl;
283 glGetQueryiv(GL_TIME_ELAPSED, GL_QUERY_COUNTER_BITS, &bits);
286 std::cout << "Error: Cannot run profile, GL_QUERY_COUNTER_BITS == 0." << std::endl;
291 /* Check for occlusion query support */
292 if (retrace::profilingPixelsDrawn && !supportsOcclusion) {
293 std::cout << "Error: Cannot run profile, GL_ARB_occlusion_query extension is not supported." << std::endl;
297 /* Setup debug message call back */
298 if (retrace::debug && supportsDebugOutput) {
299 glretrace::Context *currentContext = glretrace::getCurrentContext();
300 glDebugMessageCallbackARB(&debugOutputCallback, currentContext);
302 if (DEBUG_OUTPUT_SYNCHRONOUS) {
303 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
307 /* Sync the gpu and cpu start times */
308 if (retrace::profilingCpuTimes || retrace::profilingGpuTimes) {
309 if (!retrace::profiler.hasBaseTimes()) {
310 GLint64 gpuTime, cpuTime;
312 getCurrentTimes(cpuTime, gpuTime);
313 retrace::profiler.setBaseCpuTime(cpuTime);
314 retrace::profiler.setBaseGpuTime(gpuTime);
320 frame_complete(trace::Call &call) {
321 if (retrace::profiling) {
322 /* Complete any remaining queries */
325 /* GPU time drifts due to being relative times, not absolute and can be
326 * affected by the gpu switch between processes.
328 * To attempt to compensate we resynchronise on frame end however there is
329 * still noticeable drift within a single frame which we do not account for.
331 if (retrace::profilingCpuTimes || retrace::profilingGpuTimes) {
332 int64_t cpuTime, gpuTime, error;
334 getCurrentTimes(cpuTime, gpuTime);
335 cpuTime = cpuTime - retrace::profiler.getBaseCpuTime();
336 gpuTime = gpuTime - retrace::profiler.getBaseGpuTime();
337 error = gpuTime - cpuTime * (1.0E9 / getTimeFrequency());
338 std::cerr << "error = " << error << "\n";
341 retrace::profiler.setBaseGpuTime(retrace::profiler.getBaseGpuTime() + error);
345 /* Indicate end of current frame */
346 retrace::profiler.addFrameEnd();
349 retrace::frameComplete(call);
351 glretrace::Context *currentContext = glretrace::getCurrentContext();
352 if (!currentContext) {
356 assert(currentContext->drawable);
357 if (retrace::debug && !currentContext->drawable->visible) {
358 retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
363 getDebugOutputSource(GLenum source) {
365 case GL_DEBUG_SOURCE_API_ARB:
367 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB:
368 return "Window System";
369 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB:
370 return "Shader Compiler";
371 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB:
372 return "Third Party";
373 case GL_DEBUG_SOURCE_APPLICATION_ARB:
374 return "Application";
375 case GL_DEBUG_SOURCE_OTHER_ARB:
382 getDebugOutputType(GLenum type) {
384 case GL_DEBUG_TYPE_ERROR_ARB:
386 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB:
387 return "deprecated behaviour";
388 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB:
389 return "undefined behaviour";
390 case GL_DEBUG_TYPE_PORTABILITY_ARB:
391 return "portability issue";
392 case GL_DEBUG_TYPE_PERFORMANCE_ARB:
393 return "performance issue";
394 case GL_DEBUG_TYPE_OTHER_ARB:
396 return "unknown issue";
401 getDebugOutputSeverity(GLenum severity) {
403 case GL_DEBUG_SEVERITY_HIGH_ARB:
405 case GL_DEBUG_SEVERITY_MEDIUM_ARB:
407 case GL_DEBUG_SEVERITY_LOW_ARB:
415 debugOutputCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, GLvoid* userParam) {
416 std::cerr << retrace::callNo << ": ";
417 std::cerr << "glDebugOutputCallback: ";
418 std::cerr << getDebugOutputSeverity(severity) << " severity ";
419 std::cerr << getDebugOutputSource(source) << " " << getDebugOutputType(type);
420 std::cerr << " " << id;
421 std::cerr << ", " << message;
422 std::cerr << std::endl;
425 } /* namespace glretrace */
428 class GLDumper : public retrace::Dumper {
432 if (!glretrace::getCurrentContext()) {
435 return glstate::getDrawBufferImage();
439 dumpState(std::ostream &os) {
440 glretrace::Context *currentContext = glretrace::getCurrentContext();
441 if (glretrace::insideGlBeginEnd ||
445 glstate::dumpCurrentContext(os);
450 static GLDumper glDumper;
454 retrace::setUp(void) {
461 retrace::addCallbacks(retrace::Retracer &retracer)
463 retracer.addCallbacks(glretrace::gl_callbacks);
464 retracer.addCallbacks(glretrace::glx_callbacks);
465 retracer.addCallbacks(glretrace::wgl_callbacks);
466 retracer.addCallbacks(glretrace::cgl_callbacks);
467 retracer.addCallbacks(glretrace::egl_callbacks);
472 retrace::flushRendering(void) {
473 glretrace::Context *currentContext = glretrace::getCurrentContext();
474 if (currentContext) {
475 glretrace::flushQueries();
481 retrace::waitForInput(void) {
482 while (glws::processEvents()) {
488 retrace::cleanUp(void) {