]> git.cworth.org Git - apitrace/blob - retrace/glretrace_main.cpp
d0298fcb74e76e4dc8f99d6ab103ab8f65e5b0e5
[apitrace] / retrace / glretrace_main.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include <string.h>
28
29 #include "retrace.hpp"
30 #include "glproc.hpp"
31 #include "glstate.hpp"
32 #include "glretrace.hpp"
33 #include "os_time.hpp"
34
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.
38  */
39 #define DEBUG_OUTPUT_SYNCHRONOUS 0
40
41 namespace glretrace {
42
43 bool insideList = false;
44 bool insideGlBeginEnd = false;
45
46 enum {
47     GPU_START = 0,
48     GPU_DURATION,
49     OCCLUSION,
50     NUM_QUERIES,
51 };
52
53 struct CallQuery
54 {
55     GLuint ids[NUM_QUERIES];
56     unsigned call;
57     bool isDraw;
58     GLuint program;
59     const trace::FunctionSig *sig;
60     int64_t cpuStart;
61     int64_t cpuEnd;
62 };
63
64 static bool supportsElapsed = true;
65 static bool supportsTimestamp = true;
66 static bool supportsOcclusion = true;
67 static bool supportsDebugOutput = true;
68
69 static std::list<CallQuery> callQueries;
70
71 static void APIENTRY
72 debugOutputCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, GLvoid* userParam);
73
74 void
75 checkGlError(trace::Call &call) {
76     GLenum error = glGetError();
77     while (error != GL_NO_ERROR) {
78         std::ostream & os = retrace::warning(call);
79
80         os << "glGetError(";
81         os << call.name();
82         os << ") = ";
83
84         switch (error) {
85         case GL_INVALID_ENUM:
86             os << "GL_INVALID_ENUM";
87             break;
88         case GL_INVALID_VALUE:
89             os << "GL_INVALID_VALUE";
90             break;
91         case GL_INVALID_OPERATION:
92             os << "GL_INVALID_OPERATION";
93             break;
94         case GL_STACK_OVERFLOW:
95             os << "GL_STACK_OVERFLOW";
96             break;
97         case GL_STACK_UNDERFLOW:
98             os << "GL_STACK_UNDERFLOW";
99             break;
100         case GL_OUT_OF_MEMORY:
101             os << "GL_OUT_OF_MEMORY";
102             break;
103         case GL_INVALID_FRAMEBUFFER_OPERATION:
104             os << "GL_INVALID_FRAMEBUFFER_OPERATION";
105             break;
106         case GL_TABLE_TOO_LARGE:
107             os << "GL_TABLE_TOO_LARGE";
108             break;
109         default:
110             os << error;
111             break;
112         }
113         os << "\n";
114     
115         error = glGetError();
116     }
117 }
118
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, &timestamp);
125         return timestamp;
126     } else {
127         return os::getTime();
128     }
129 }
130
131 static inline int64_t
132 getTimeFrequency(void) {
133     if (retrace::profilingGpuTimes && supportsTimestamp) {
134         return 1000000000;
135     } else {
136         return os::timeFrequency;
137     }
138 }
139
140 static void
141 completeCallQuery(CallQuery& query) {
142     /* Get call start and duration */
143     int64_t gpuStart = 0, gpuDuration = 0, cpuDuration = 0, pixels = 0;
144
145     if (query.isDraw) {
146         if (retrace::profilingGpuTimes) {
147             if (supportsTimestamp) {
148                 glGetQueryObjecti64vEXT(query.ids[GPU_START], GL_QUERY_RESULT, &gpuStart);
149             }
150
151             glGetQueryObjecti64vEXT(query.ids[GPU_DURATION], GL_QUERY_RESULT, &gpuDuration);
152         }
153
154         if (retrace::profilingPixelsDrawn) {
155             glGetQueryObjecti64vEXT(query.ids[OCCLUSION], GL_QUERY_RESULT, &pixels);
156         }
157
158     } else {
159         pixels = -1;
160     }
161
162     if (retrace::profilingCpuTimes) {
163         cpuDuration = query.cpuEnd - query.cpuStart;
164     }
165
166     glDeleteQueries(NUM_QUERIES, query.ids);
167
168     /* Add call to profile */
169     retrace::profiler.addCall(query.call, query.sig->name, query.program, pixels, gpuStart, gpuDuration, query.cpuStart, cpuDuration);
170 }
171
172 void
173 flushQueries() {
174     for (std::list<CallQuery>::iterator itr = callQueries.begin(); itr != callQueries.end(); ++itr) {
175         completeCallQuery(*itr);
176     }
177
178     callQueries.clear();
179 }
180
181 void
182 beginProfile(trace::Call &call, bool isDraw) {
183     glretrace::Context *currentContext = glretrace::getCurrentContext();
184
185     /* Create call query */
186     CallQuery query;
187     query.isDraw = isDraw;
188     query.call = call.no;
189     query.sig = call.sig;
190     query.program = currentContext ? currentContext->activeProgram : 0;
191
192     glGenQueries(NUM_QUERIES, query.ids);
193
194     /* GPU profiling only for draw calls */
195     if (isDraw) {
196         if (retrace::profilingGpuTimes) {
197             if (supportsTimestamp) {
198                 glQueryCounter(query.ids[GPU_START], GL_TIMESTAMP);
199             }
200
201             glBeginQuery(GL_TIME_ELAPSED, query.ids[GPU_DURATION]);
202         }
203
204         if (retrace::profilingPixelsDrawn) {
205             glBeginQuery(GL_SAMPLES_PASSED, query.ids[OCCLUSION]);
206         }
207     }
208
209     callQueries.push_back(query);
210
211     /* CPU profiling for all calls */
212     if (retrace::profilingCpuTimes) {
213         CallQuery& query = callQueries.back();
214         query.cpuStart = getCurrentTime();
215     }
216 }
217
218 void
219 endProfile(trace::Call &call, bool isDraw) {
220
221     /* CPU profiling for all calls */
222     if (retrace::profilingCpuTimes) {
223         CallQuery& query = callQueries.back();
224         query.cpuEnd = getCurrentTime();
225     }
226
227     /* GPU profiling only for draw calls */
228     if (isDraw) {
229         if (retrace::profilingGpuTimes) {
230             glEndQuery(GL_TIME_ELAPSED);
231         }
232
233         if (retrace::profilingPixelsDrawn) {
234             glEndQuery(GL_SAMPLES_PASSED);
235         }
236     }
237 }
238
239 void
240 initContext() {
241     glretrace::Context *currentContext = glretrace::getCurrentContext();
242
243     /* Ensure we have adequate extension support */
244     assert(currentContext);
245     supportsTimestamp   = currentContext->hasExtension("GL_ARB_timer_query");
246     supportsElapsed     = currentContext->hasExtension("GL_EXT_timer_query") || supportsTimestamp;
247     supportsOcclusion   = currentContext->hasExtension("GL_ARB_occlusion_query");
248     supportsDebugOutput = currentContext->hasExtension("GL_ARB_debug_output");
249
250     /* Check for timer query support */
251     if (retrace::profilingGpuTimes) {
252         if (!supportsTimestamp && !supportsElapsed) {
253             std::cout << "Error: Cannot run profile, GL_EXT_timer_query extension is not supported." << std::endl;
254             exit(-1);
255         }
256
257         GLint bits = 0;
258         glGetQueryiv(GL_TIME_ELAPSED, GL_QUERY_COUNTER_BITS, &bits);
259
260         if (!bits) {
261             std::cout << "Error: Cannot run profile, GL_QUERY_COUNTER_BITS == 0." << std::endl;
262             exit(-1);
263         }
264     }
265
266     /* Check for occlusion query support */
267     if (retrace::profilingPixelsDrawn && !supportsOcclusion) {
268         std::cout << "Error: Cannot run profile, GL_ARB_occlusion_query extension is not supported." << std::endl;
269         exit(-1);
270     }
271
272     /* Setup debug message call back */
273     if (retrace::debug && supportsDebugOutput) {
274         glretrace::Context *currentContext = glretrace::getCurrentContext();
275         glDebugMessageCallbackARB(&debugOutputCallback, currentContext);
276
277         if (DEBUG_OUTPUT_SYNCHRONOUS) {
278             glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
279         }
280     }
281
282     /* Sync the gpu and cpu start times */
283     if (retrace::profilingCpuTimes || retrace::profilingGpuTimes) {
284         if (!retrace::profiler.hasBaseTimes()) {
285             GLint64 currentTime = getCurrentTime();
286             retrace::profiler.setBaseCpuTime(currentTime);
287             retrace::profiler.setBaseGpuTime(currentTime);
288         }
289     }
290 }
291
292 void
293 frame_complete(trace::Call &call) {
294     if (retrace::profiling) {
295         /* Complete any remaining queries */
296         flushQueries();
297
298         /* Indicate end of current frame */
299         retrace::profiler.addFrameEnd();
300     }
301
302     retrace::frameComplete(call);
303
304     glretrace::Context *currentContext = glretrace::getCurrentContext();
305     if (!currentContext) {
306         return;
307     }
308
309     assert(currentContext->drawable);
310     if (retrace::debug && !currentContext->drawable->visible) {
311         retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
312     }
313 }
314
315 static const char*
316 getDebugOutputSource(GLenum source) {
317     switch(source) {
318     case GL_DEBUG_SOURCE_API_ARB:
319         return "API";
320     case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB:
321         return "Window System";
322     case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB:
323         return "Shader Compiler";
324     case GL_DEBUG_SOURCE_THIRD_PARTY_ARB:
325         return "Third Party";
326     case GL_DEBUG_SOURCE_APPLICATION_ARB:
327         return "Application";
328     case GL_DEBUG_SOURCE_OTHER_ARB:
329     default:
330         return "";
331     }
332 }
333
334 static const char*
335 getDebugOutputType(GLenum type) {
336     switch(type) {
337     case GL_DEBUG_TYPE_ERROR_ARB:
338         return "error";
339     case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB:
340         return "deprecated behaviour";
341     case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB:
342         return "undefined behaviour";
343     case GL_DEBUG_TYPE_PORTABILITY_ARB:
344         return "portability issue";
345     case GL_DEBUG_TYPE_PERFORMANCE_ARB:
346         return "performance issue";
347     case GL_DEBUG_TYPE_OTHER_ARB:
348     default:
349         return "unknown issue";
350     }
351 }
352
353 static const char*
354 getDebugOutputSeverity(GLenum severity) {
355     switch(severity) {
356     case GL_DEBUG_SEVERITY_HIGH_ARB:
357         return "High";
358     case GL_DEBUG_SEVERITY_MEDIUM_ARB:
359         return "Medium";
360     case GL_DEBUG_SEVERITY_LOW_ARB:
361         return "Low";
362     default:
363         return "usnknown";
364     }
365 }
366
367 static void APIENTRY
368 debugOutputCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, GLvoid* userParam) {
369     std::cerr << retrace::callNo << ": ";
370     std::cerr << "glDebugOutputCallback: ";
371     std::cerr << getDebugOutputSeverity(severity) << " severity ";
372     std::cerr << getDebugOutputSource(source) << " " << getDebugOutputType(type);
373     std::cerr << " " << id;
374     std::cerr << ", " << message;
375     std::cerr << std::endl;
376 }
377
378 } /* namespace glretrace */
379
380
381 class GLDumper : public retrace::Dumper {
382 public:
383     image::Image *
384     getSnapshot(void) {
385         if (!glretrace::getCurrentContext()) {
386             return NULL;
387         }
388         return glstate::getDrawBufferImage();
389     }
390
391     bool
392     dumpState(std::ostream &os) {
393         glretrace::Context *currentContext = glretrace::getCurrentContext();
394         if (glretrace::insideGlBeginEnd ||
395             !currentContext) {
396             return false;
397         }
398         glstate::dumpCurrentContext(os);
399         return true;
400     }
401 };
402
403 static GLDumper glDumper;
404
405
406 void
407 retrace::setUp(void) {
408     glws::init();
409     dumper = &glDumper;
410 }
411
412
413 void
414 retrace::addCallbacks(retrace::Retracer &retracer)
415 {
416     retracer.addCallbacks(glretrace::gl_callbacks);
417     retracer.addCallbacks(glretrace::glx_callbacks);
418     retracer.addCallbacks(glretrace::wgl_callbacks);
419     retracer.addCallbacks(glretrace::cgl_callbacks);
420     retracer.addCallbacks(glretrace::egl_callbacks);
421 }
422
423
424 void
425 retrace::flushRendering(void) {
426     glretrace::Context *currentContext = glretrace::getCurrentContext();
427     if (currentContext) {
428         glretrace::flushQueries();
429         glFlush();
430     }
431 }
432
433 void
434 retrace::waitForInput(void) {
435     while (glws::processEvents()) {
436         os::sleep(100*1000);
437     }
438 }
439
440 void
441 retrace::cleanUp(void) {
442     glws::cleanup();
443 }