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;
52 const trace::FunctionSig *sig;
57 static bool supportsElapsed = true;
58 static bool supportsTimestamp = true;
59 static bool supportsOcclusion = true;
60 static bool supportsDebugOutput = true;
62 static std::list<CallQuery> callQueries;
65 debugOutputCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, GLvoid* userParam);
68 checkGlError(trace::Call &call) {
69 GLenum error = glGetError();
70 if (error == GL_NO_ERROR) {
74 std::ostream & os = retrace::warning(call);
82 os << "GL_INVALID_ENUM";
84 case GL_INVALID_VALUE:
85 os << "GL_INVALID_VALUE";
87 case GL_INVALID_OPERATION:
88 os << "GL_INVALID_OPERATION";
90 case GL_STACK_OVERFLOW:
91 os << "GL_STACK_OVERFLOW";
93 case GL_STACK_UNDERFLOW:
94 os << "GL_STACK_UNDERFLOW";
96 case GL_OUT_OF_MEMORY:
97 os << "GL_OUT_OF_MEMORY";
99 case GL_INVALID_FRAMEBUFFER_OPERATION:
100 os << "GL_INVALID_FRAMEBUFFER_OPERATION";
102 case GL_TABLE_TOO_LARGE:
103 os << "GL_TABLE_TOO_LARGE";
113 getCurrentTimes(int64_t& cpuTime, int64_t& gpuTime) {
116 if (retrace::profilingGpuTimes && supportsTimestamp) {
117 glGenQueries(1, &query);
118 glQueryCounter(query, GL_TIMESTAMP);
119 glGetQueryObjecti64vEXT(query, GL_QUERY_RESULT, &gpuTime);
124 if (retrace::profilingCpuTimes) {
125 cpuTime = os::getTime();
130 if (retrace::profilingGpuTimes && supportsTimestamp) {
131 glDeleteQueries(1, &query);
136 completeCallQuery(CallQuery& query) {
137 /* Get call start and duration */
138 int64_t gpuStart = 0, gpuDuration = 0, cpuDuration = 0, pixels = 0;
141 if (retrace::profilingGpuTimes) {
142 if (supportsTimestamp) {
143 glGetQueryObjecti64vEXT(query.ids[0], GL_QUERY_RESULT, &gpuStart);
146 glGetQueryObjecti64vEXT(query.ids[1], GL_QUERY_RESULT, &gpuDuration);
149 if (retrace::profilingPixelsDrawn) {
150 glGetQueryObjecti64vEXT(query.ids[2], GL_QUERY_RESULT, &pixels);
153 glDeleteQueries(3, query.ids);
158 if (retrace::profilingCpuTimes) {
159 cpuDuration = query.cpuEnd - query.cpuStart;
162 /* Add call to profile */
163 retrace::profiler.addCall(query.call, query.sig->name, query.program, pixels, gpuStart, gpuDuration, query.cpuStart, cpuDuration);
168 for (std::list<CallQuery>::iterator itr = callQueries.begin(); itr != callQueries.end(); ++itr) {
169 completeCallQuery(*itr);
176 beginProfile(trace::Call &call, bool isDraw) {
177 /* Create call query */
179 query.isDraw = isDraw;
180 query.call = call.no;
181 query.sig = call.sig;
182 query.program = glretrace::currentContext ? glretrace::currentContext->activeProgram : 0;
184 /* GPU profiling only for draw calls */
186 glGenQueries(3, query.ids);
188 if (retrace::profilingGpuTimes) {
189 if (supportsTimestamp) {
190 glQueryCounter(query.ids[0], GL_TIMESTAMP);
193 glBeginQuery(GL_TIME_ELAPSED, query.ids[1]);
196 if (retrace::profilingPixelsDrawn) {
197 glBeginQuery(GL_SAMPLES_PASSED, query.ids[2]);
201 callQueries.push_back(query);
203 /* CPU profiling for all calls */
204 if (retrace::profilingCpuTimes) {
205 callQueries.back().cpuStart = os::getTime();
210 endProfile(trace::Call &call, bool isDraw) {
211 GLint64 time = os::getTime();
213 /* CPU profiling for all calls */
214 if (retrace::profilingCpuTimes) {
215 CallQuery& query = callQueries.back();
219 /* GPU profiling only for draw calls */
221 if (retrace::profilingGpuTimes) {
222 glEndQuery(GL_TIME_ELAPSED);
225 if (retrace::profilingPixelsDrawn) {
226 glEndQuery(GL_SAMPLES_PASSED);
233 const char* extensions = (const char*)glGetString(GL_EXTENSIONS);
235 /* Ensure we have adequate extension support */
236 supportsTimestamp = glws::checkExtension("GL_ARB_timer_query", extensions);
237 supportsElapsed = glws::checkExtension("GL_EXT_timer_query", extensions) || supportsTimestamp;
238 supportsOcclusion = glws::checkExtension("GL_ARB_occlusion_query", extensions);
239 supportsDebugOutput = glws::checkExtension("GL_ARB_debug_output", extensions);
241 /* Check for timer query support */
242 if (retrace::profilingGpuTimes) {
243 if (!supportsTimestamp && !supportsElapsed) {
244 std::cout << "Error: Cannot run profile, GL_EXT_timer_query extension is not supported." << std::endl;
249 glGetQueryiv(GL_TIME_ELAPSED, GL_QUERY_COUNTER_BITS, &bits);
252 std::cout << "Error: Cannot run profile, GL_QUERY_COUNTER_BITS == 0." << std::endl;
257 /* Check for occlusion query support */
258 if (retrace::profilingPixelsDrawn && !supportsOcclusion) {
259 std::cout << "Error: Cannot run profile, GL_ARB_occlusion_query extension is not supported." << std::endl;
263 /* Setup debug message call back */
264 if (retrace::debug && supportsDebugOutput) {
265 glDebugMessageCallbackARB(&debugOutputCallback, currentContext);
267 if (DEBUG_OUTPUT_SYNCHRONOUS) {
268 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
272 /* Sync the gpu and cpu start times */
273 if (retrace::profilingCpuTimes || retrace::profilingGpuTimes) {
274 if (!retrace::profiler.hasBaseTimes()) {
275 GLint64 gpuTime, cpuTime;
277 getCurrentTimes(cpuTime, gpuTime);
278 retrace::profiler.setBaseCpuTime(cpuTime);
279 retrace::profiler.setBaseGpuTime(gpuTime);
285 frame_complete(trace::Call &call) {
286 if (retrace::profiling) {
287 /* Complete any remaining queries */
290 /* GPU time drifts due to being relative times, not absolute and can be
291 * affected by the gpu switch between processes.
293 * To attempt to compensate we resynchronise on frame end however there is
294 * still noticeable drift within a single frame which we do not account for.
296 if (retrace::profilingCpuTimes || retrace::profilingGpuTimes) {
297 int64_t cpuTime, gpuTime, error;
299 getCurrentTimes(cpuTime, gpuTime);
300 cpuTime = cpuTime - retrace::profiler.getBaseCpuTime();
301 gpuTime = gpuTime - retrace::profiler.getBaseGpuTime();
302 error = gpuTime - cpuTime * (1.0E9 / os::timeFrequency);
304 retrace::profiler.setBaseGpuTime(retrace::profiler.getBaseGpuTime() + error);
307 /* Indicate end of current frame */
308 retrace::profiler.addFrameEnd();
311 retrace::frameComplete(call);
313 if (!currentDrawable) {
317 if (retrace::debug && !currentDrawable->visible) {
318 retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
323 getDebugOutputSource(GLenum source) {
325 case GL_DEBUG_SOURCE_API_ARB:
327 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB:
328 return "Window System";
329 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB:
330 return "Shader Compiler";
331 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB:
332 return "Third Party";
333 case GL_DEBUG_SOURCE_APPLICATION_ARB:
334 return "Application";
335 case GL_DEBUG_SOURCE_OTHER_ARB:
342 getDebugOutputType(GLenum type) {
344 case GL_DEBUG_TYPE_ERROR_ARB:
346 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB:
347 return "deprecated behaviour";
348 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB:
349 return "undefined behaviour";
350 case GL_DEBUG_TYPE_PORTABILITY_ARB:
351 return "portability issue";
352 case GL_DEBUG_TYPE_PERFORMANCE_ARB:
353 return "performance issue";
354 case GL_DEBUG_TYPE_OTHER_ARB:
356 return "unknown issue";
361 getDebugOutputSeverity(GLenum severity) {
363 case GL_DEBUG_SEVERITY_HIGH_ARB:
365 case GL_DEBUG_SEVERITY_MEDIUM_ARB:
367 case GL_DEBUG_SEVERITY_LOW_ARB:
375 debugOutputCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, GLvoid* userParam) {
376 std::cerr << retrace::callNo << ": ";
377 std::cerr << "glDebugOutputCallback: ";
378 std::cerr << getDebugOutputSeverity(severity) << " severity ";
379 std::cerr << getDebugOutputSource(source) << " " << getDebugOutputType(type);
380 std::cerr << " " << id;
381 std::cerr << ", " << message;
382 std::cerr << std::endl;
385 } /* namespace glretrace */
389 retrace::setUp(void) {
395 retrace::addCallbacks(retrace::Retracer &retracer)
397 retracer.addCallbacks(glretrace::gl_callbacks);
398 retracer.addCallbacks(glretrace::glx_callbacks);
399 retracer.addCallbacks(glretrace::wgl_callbacks);
400 retracer.addCallbacks(glretrace::cgl_callbacks);
401 retracer.addCallbacks(glretrace::egl_callbacks);
406 retrace::getSnapshot(void) {
407 if (!glretrace::currentDrawable) {
411 return glstate::getDrawBufferImage();
416 retrace::dumpState(std::ostream &os)
418 if (glretrace::insideGlBeginEnd ||
419 !glretrace::currentDrawable ||
420 !glretrace::currentContext) {
424 glstate::dumpCurrentContext(os);
430 retrace::flushRendering(void) {
431 glretrace::flushQueries();
436 retrace::waitForInput(void) {
437 while (glws::processEvents()) {
442 retrace::cleanUp(void) {