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 "os_binary.hpp"
30 #include "os_string.hpp"
31 #include "os_time.hpp"
33 #include "retrace.hpp"
34 #include "trace_callset.hpp"
36 #include "glstate.hpp"
37 #include "glretrace.hpp"
42 bool double_buffer = true;
43 bool insideGlBeginEnd = false;
44 glws::Profile defaultProfile = glws::PROFILE_COMPAT;
45 glws::Visual *visual[glws::PROFILE_MAX];
46 glws::Drawable *drawable = NULL;
47 glws::Context *context = NULL;
50 long long startTime = 0;
53 static const char *compare_prefix = NULL;
54 static const char *snapshot_prefix = NULL;
55 static trace::CallSet snapshot_frequency;
56 static trace::CallSet compare_frequency;
58 unsigned dump_state = ~0;
61 checkGlError(trace::Call &call) {
62 GLenum error = glGetError();
63 if (error == GL_NO_ERROR) {
67 std::ostream & os = retrace::warning(call);
75 os << "GL_INVALID_ENUM";
77 case GL_INVALID_VALUE:
78 os << "GL_INVALID_VALUE";
80 case GL_INVALID_OPERATION:
81 os << "GL_INVALID_OPERATION";
83 case GL_STACK_OVERFLOW:
84 os << "GL_STACK_OVERFLOW";
86 case GL_STACK_UNDERFLOW:
87 os << "GL_STACK_UNDERFLOW";
89 case GL_OUT_OF_MEMORY:
90 os << "GL_OUT_OF_MEMORY";
92 case GL_INVALID_FRAMEBUFFER_OPERATION:
93 os << "GL_INVALID_FRAMEBUFFER_OPERATION";
95 case GL_TABLE_TOO_LARGE:
96 os << "GL_TABLE_TOO_LARGE";
106 * Grow the current drawble.
108 * We need to infer the drawable size from GL calls because the drawable sizes
109 * are specified by OS specific calls which we do not trace.
112 updateDrawable(int width, int height) {
117 if (drawable->visible &&
118 width <= drawable->width &&
119 height <= drawable->height) {
123 // Ignore zero area viewports
124 if (width == 0 || height == 0) {
128 // Check for bound framebuffer last, as this may have a performance impact.
129 GLint draw_framebuffer = 0;
130 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
131 if (draw_framebuffer != 0) {
135 drawable->resize(width, height);
138 glScissor(0, 0, width, height);
143 snapshot(unsigned call_no) {
144 assert(snapshot_prefix || compare_prefix);
150 image::Image *ref = NULL;
152 if (compare_prefix) {
153 os::String filename = os::String::format("%s%010u.png", compare_prefix, call_no);
154 ref = image::readPNG(filename);
158 if (retrace::verbosity >= 0) {
159 std::cout << "Read " << filename << "\n";
163 image::Image *src = glstate::getDrawBufferImage();
168 if (snapshot_prefix) {
169 if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
171 snprintf(comment, sizeof comment, "%u", call_no);
172 src->writePNM(std::cout, comment);
174 os::String filename = os::String::format("%s%010u.png", snapshot_prefix, call_no);
175 if (src->writePNG(filename) && retrace::verbosity >= 0) {
176 std::cout << "Wrote " << filename << "\n";
182 std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
190 void frame_complete(trace::Call &call) {
197 if (!drawable->visible) {
198 retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
203 static void display(void) {
204 retrace::Retracer retracer;
206 retracer.addCallbacks(gl_callbacks);
207 retracer.addCallbacks(glx_callbacks);
208 retracer.addCallbacks(wgl_callbacks);
209 retracer.addCallbacks(cgl_callbacks);
210 retracer.addCallbacks(egl_callbacks);
212 startTime = os::getTime();
215 while ((call = retrace::parser.parse_call())) {
216 bool swapRenderTarget = call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET;
218 snapshot_frequency.contains(*call) ||
219 compare_frequency.contains(*call)
222 // For calls which cause rendertargets to be swaped, we take the
223 // snapshot _before_ swapping the rendertargets.
224 if (doSnapshot && swapRenderTarget) {
225 if (call->flags & trace::CALL_FLAG_END_FRAME) {
226 // For swapbuffers/presents we still use this call number,
227 // spite not have been executed yet.
230 // Whereas for ordinate fbo/rendertarget changes we use the
231 // previous call's number.
232 snapshot(call->no - 1);
236 retracer.retrace(*call);
238 if (doSnapshot && !swapRenderTarget) {
242 if (!insideGlBeginEnd &&
243 drawable && context &&
244 call->no >= dump_state) {
245 glstate::dumpCurrentContext(std::cout);
252 // Reached the end of trace
255 long long endTime = os::getTime();
256 float timeInterval = (endTime - startTime) * (1.0 / os::timeFrequency);
258 if ((retrace::verbosity >= -1) || (retrace::profiling)) {
260 "Rendered " << frame << " frames"
261 " in " << timeInterval << " secs,"
262 " average of " << (frame/timeInterval) << " fps\n";
266 while (glws::processEvents()) {}
273 static void usage(void) {
275 "Usage: glretrace [OPTION] TRACE\n"
278 " -b benchmark mode (no error checking or warning messages)\n"
279 " -p profiling mode (run whole trace, dump profiling info)\n"
280 " -c PREFIX compare against snapshots\n"
281 " -C CALLSET calls to compare (default is every frame)\n"
282 " -core use core profile\n"
283 " -db use a double buffer visual (default)\n"
284 " -sb use a single buffer visual\n"
285 " -s PREFIX take snapshots; `-` for PNM stdout output\n"
286 " -S CALLSET calls to snapshot (default is every frame)\n"
287 " -v increase output verbosity\n"
288 " -D CALLNO dump state at specific call no\n"
289 " -w wait on final frame\n";
293 int main(int argc, char **argv)
295 assert(compare_frequency.empty());
296 assert(snapshot_frequency.empty());
299 for (i = 1; i < argc; ++i) {
300 const char *arg = argv[i];
306 if (!strcmp(arg, "--")) {
308 } else if (!strcmp(arg, "-b")) {
309 retrace::benchmark = true;
310 retrace::verbosity = -1;
312 } else if (!strcmp(arg, "-p")) {
313 retrace::profiling = true;
314 retrace::verbosity = -1;
316 } else if (!strcmp(arg, "-c")) {
317 compare_prefix = argv[++i];
318 if (compare_frequency.empty()) {
319 compare_frequency = trace::CallSet(trace::FREQUENCY_FRAME);
321 } else if (!strcmp(arg, "-C")) {
322 compare_frequency = trace::CallSet(argv[++i]);
323 if (compare_prefix == NULL) {
326 } else if (!strcmp(arg, "-D")) {
327 dump_state = atoi(argv[++i]);
328 retrace::verbosity = -2;
329 } else if (!strcmp(arg, "-core")) {
330 defaultProfile = glws::PROFILE_CORE;
331 } else if (!strcmp(arg, "-db")) {
332 double_buffer = true;
333 } else if (!strcmp(arg, "-sb")) {
334 double_buffer = false;
335 } else if (!strcmp(arg, "--help")) {
338 } else if (!strcmp(arg, "-s")) {
339 snapshot_prefix = argv[++i];
340 if (snapshot_frequency.empty()) {
341 snapshot_frequency = trace::CallSet(trace::FREQUENCY_FRAME);
343 if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
344 os::setBinaryMode(stdout);
345 retrace::verbosity = -2;
347 } else if (!strcmp(arg, "-S")) {
348 snapshot_frequency = trace::CallSet(argv[++i]);
349 if (snapshot_prefix == NULL) {
350 snapshot_prefix = "";
352 } else if (!strcmp(arg, "-v")) {
353 ++retrace::verbosity;
354 } else if (!strcmp(arg, "-w")) {
357 std::cerr << "error: unknown option " << arg << "\n";
364 visual[glws::PROFILE_COMPAT] = glws::createVisual(double_buffer, glws::PROFILE_COMPAT);
365 visual[glws::PROFILE_CORE] = glws::createVisual(double_buffer, glws::PROFILE_CORE);
366 visual[glws::PROFILE_ES1] = glws::createVisual(double_buffer, glws::PROFILE_ES1);
367 visual[glws::PROFILE_ES2] = glws::createVisual(double_buffer, glws::PROFILE_ES2);
369 for ( ; i < argc; ++i) {
370 if (!retrace::parser.open(argv[i])) {
371 std::cerr << "error: failed to open " << argv[i] << "\n";
377 retrace::parser.close();
380 for (int n = 0; n < glws::PROFILE_MAX; n++) {
389 } /* namespace glretrace */