]> git.cworth.org Git - apitrace/blob - glretrace_main.cpp
apitrace diff-images: Print one line for each file being compared.
[apitrace] / 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 "os_binary.hpp"
30 #include "os_string.hpp"
31 #include "os_time.hpp"
32 #include "image.hpp"
33 #include "retrace.hpp"
34 #include "trace_callset.hpp"
35 #include "glproc.hpp"
36 #include "glstate.hpp"
37 #include "glretrace.hpp"
38
39
40 namespace glretrace {
41
42 bool double_buffer = true;
43 bool insideGlBeginEnd = false;
44 trace::Parser parser;
45 glws::Profile defaultProfile = glws::PROFILE_COMPAT;
46 glws::Visual *visual[glws::PROFILE_MAX];
47 glws::Drawable *drawable = NULL;
48 glws::Context *context = NULL;
49
50 unsigned frame = 0;
51 long long startTime = 0;
52 bool wait = false;
53
54 bool benchmark = false;
55 static const char *compare_prefix = NULL;
56 static const char *snapshot_prefix = NULL;
57 static trace::CallSet snapshot_frequency;
58 static trace::CallSet compare_frequency;
59
60 unsigned dump_state = ~0;
61
62 void
63 checkGlError(trace::Call &call) {
64     GLenum error = glGetError();
65     if (error == GL_NO_ERROR) {
66         return;
67     }
68
69     std::ostream & os = retrace::warning(call);
70
71     os << "glGetError(";
72     os << call.name();
73     os << ") = ";
74
75     switch (error) {
76     case GL_INVALID_ENUM:
77         os << "GL_INVALID_ENUM";
78         break;
79     case GL_INVALID_VALUE:
80         os << "GL_INVALID_VALUE";
81         break;
82     case GL_INVALID_OPERATION:
83         os << "GL_INVALID_OPERATION";
84         break;
85     case GL_STACK_OVERFLOW:
86         os << "GL_STACK_OVERFLOW";
87         break;
88     case GL_STACK_UNDERFLOW:
89         os << "GL_STACK_UNDERFLOW";
90         break;
91     case GL_OUT_OF_MEMORY:
92         os << "GL_OUT_OF_MEMORY";
93         break;
94     case GL_INVALID_FRAMEBUFFER_OPERATION:
95         os << "GL_INVALID_FRAMEBUFFER_OPERATION";
96         break;
97     case GL_TABLE_TOO_LARGE:
98         os << "GL_TABLE_TOO_LARGE";
99         break;
100     default:
101         os << error;
102         break;
103     }
104     os << "\n";
105 }
106
107 /**
108  * Grow the current drawble.
109  *
110  * We need to infer the drawable size from GL calls because the drawable sizes
111  * are specified by OS specific calls which we do not trace.
112  */
113 void
114 updateDrawable(int width, int height) {
115     if (!drawable) {
116         return;
117     }
118
119     if (drawable->visible &&
120         width  <= drawable->width &&
121         height <= drawable->height) {
122         return;
123     }
124
125     // Ignore zero area viewports
126     if (width == 0 || height == 0) {
127         return;
128     }
129
130     // Check for bound framebuffer last, as this may have a performance impact.
131     GLint draw_framebuffer = 0;
132     glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
133     if (draw_framebuffer != 0) {
134         return;
135     }
136
137     drawable->resize(width, height);
138     drawable->show();
139
140     glScissor(0, 0, width, height);
141 }
142
143
144 static void
145 snapshot(unsigned call_no) {
146     assert(snapshot_prefix || compare_prefix);
147
148     if (!drawable) {
149         return;
150     }
151
152     image::Image *ref = NULL;
153
154     if (compare_prefix) {
155         os::String filename = os::String::format("%s%010u.png", compare_prefix, call_no);
156         ref = image::readPNG(filename);
157         if (!ref) {
158             return;
159         }
160         if (retrace::verbosity >= 0) {
161             std::cout << "Read " << filename << "\n";
162         }
163     }
164
165     image::Image *src = glstate::getDrawBufferImage();
166     if (!src) {
167         return;
168     }
169
170     if (snapshot_prefix) {
171         if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
172             char comment[21];
173             snprintf(comment, sizeof comment, "%u", call_no);
174             src->writePNM(std::cout, comment);
175         } else {
176             os::String filename = os::String::format("%s%010u.png", snapshot_prefix, call_no);
177             if (src->writePNG(filename) && retrace::verbosity >= 0) {
178                 std::cout << "Wrote " << filename << "\n";
179             }
180         }
181     }
182
183     if (ref) {
184         std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
185         delete ref;
186     }
187
188     delete src;
189 }
190
191
192 void frame_complete(trace::Call &call) {
193     ++frame;
194
195     if (!drawable) {
196         return;
197     }
198
199     if (!drawable->visible) {
200         retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
201     }
202 }
203
204
205 static void display(void) {
206     retrace::Retracer retracer;
207
208     retracer.addCallbacks(gl_callbacks);
209     retracer.addCallbacks(glx_callbacks);
210     retracer.addCallbacks(wgl_callbacks);
211     retracer.addCallbacks(cgl_callbacks);
212     retracer.addCallbacks(egl_callbacks);
213
214     startTime = os::getTime();
215     trace::Call *call;
216
217     while ((call = parser.parse_call())) {
218         bool swapRenderTarget = call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET;
219         bool doSnapshot =
220             snapshot_frequency.contains(*call) ||
221             compare_frequency.contains(*call)
222         ;
223
224         // For calls which cause rendertargets to be swaped, we take the
225         // snapshot _before_ swapping the rendertargets.
226         if (doSnapshot && swapRenderTarget) {
227             if (call->flags & trace::CALL_FLAG_END_FRAME) {
228                 // For swapbuffers/presents we still use this call number,
229                 // spite not have been executed yet.
230                 snapshot(call->no);
231             } else {
232                 // Whereas for ordinate fbo/rendertarget changes we use the
233                 // previous call's number.
234                 snapshot(call->no - 1);
235             }
236         }
237
238         retracer.retrace(*call);
239
240         if (doSnapshot && !swapRenderTarget) {
241             snapshot(call->no);
242         }
243
244         if (!insideGlBeginEnd &&
245             drawable && context &&
246             call->no >= dump_state) {
247             glstate::dumpCurrentContext(std::cout);
248             exit(0);
249         }
250
251         delete call;
252     }
253
254     // Reached the end of trace
255     glFlush();
256
257     long long endTime = os::getTime();
258     float timeInterval = (endTime - startTime) * (1.0 / os::timeFrequency);
259
260     if ((retrace::verbosity >= -1) || (retrace::profiling)) {
261         std::cout << 
262             "Rendered " << frame << " frames"
263             " in " <<  timeInterval << " secs,"
264             " average of " << (frame/timeInterval) << " fps\n";
265     }
266
267     if (wait) {
268         while (glws::processEvents()) {}
269     } else {
270         exit(0);
271     }
272 }
273
274
275 static void usage(void) {
276     std::cout << 
277         "Usage: glretrace [OPTION] TRACE\n"
278         "Replay TRACE.\n"
279         "\n"
280         "  -b           benchmark mode (no error checking or warning messages)\n"
281         "  -p           profiling mode (run whole trace, dump profiling info)\n"
282         "  -c PREFIX    compare against snapshots\n"
283         "  -C CALLSET   calls to compare (default is every frame)\n"
284         "  -core        use core profile\n"
285         "  -db          use a double buffer visual (default)\n"
286         "  -sb          use a single buffer visual\n"
287         "  -s PREFIX    take snapshots; `-` for PNM stdout output\n"
288         "  -S CALLSET   calls to snapshot (default is every frame)\n"
289         "  -v           increase output verbosity\n"
290         "  -D CALLNO    dump state at specific call no\n"
291         "  -w           wait on final frame\n";
292 }
293
294 extern "C"
295 int main(int argc, char **argv)
296 {
297     assert(compare_frequency.empty());
298     assert(snapshot_frequency.empty());
299
300     int i;
301     for (i = 1; i < argc; ++i) {
302         const char *arg = argv[i];
303
304         if (arg[0] != '-') {
305             break;
306         }
307
308         if (!strcmp(arg, "--")) {
309             break;
310         } else if (!strcmp(arg, "-b")) {
311             benchmark = true;
312             retrace::verbosity = -1;
313             glws::debug = false;
314         } else if (!strcmp(arg, "-p")) {
315             retrace::profiling = true;
316             retrace::verbosity = -1;
317             glws::debug = false;
318         } else if (!strcmp(arg, "-c")) {
319             compare_prefix = argv[++i];
320             if (compare_frequency.empty()) {
321                 compare_frequency = trace::CallSet(trace::FREQUENCY_FRAME);
322             }
323         } else if (!strcmp(arg, "-C")) {
324             compare_frequency = trace::CallSet(argv[++i]);
325             if (compare_prefix == NULL) {
326                 compare_prefix = "";
327             }
328         } else if (!strcmp(arg, "-D")) {
329             dump_state = atoi(argv[++i]);
330             retrace::verbosity = -2;
331         } else if (!strcmp(arg, "-core")) {
332             defaultProfile = glws::PROFILE_CORE;
333         } else if (!strcmp(arg, "-db")) {
334             double_buffer = true;
335         } else if (!strcmp(arg, "-sb")) {
336             double_buffer = false;
337         } else if (!strcmp(arg, "--help")) {
338             usage();
339             return 0;
340         } else if (!strcmp(arg, "-s")) {
341             snapshot_prefix = argv[++i];
342             if (snapshot_frequency.empty()) {
343                 snapshot_frequency = trace::CallSet(trace::FREQUENCY_FRAME);
344             }
345             if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
346                 os::setBinaryMode(stdout);
347                 retrace::verbosity = -2;
348             }
349         } else if (!strcmp(arg, "-S")) {
350             snapshot_frequency = trace::CallSet(argv[++i]);
351             if (snapshot_prefix == NULL) {
352                 snapshot_prefix = "";
353             }
354         } else if (!strcmp(arg, "-v")) {
355             ++retrace::verbosity;
356         } else if (!strcmp(arg, "-w")) {
357             wait = true;
358         } else {
359             std::cerr << "error: unknown option " << arg << "\n";
360             usage();
361             return 1;
362         }
363     }
364
365     glws::init();
366     visual[glws::PROFILE_COMPAT] = glws::createVisual(double_buffer, glws::PROFILE_COMPAT);
367     visual[glws::PROFILE_CORE] = glws::createVisual(double_buffer, glws::PROFILE_CORE);
368     visual[glws::PROFILE_ES1] = glws::createVisual(double_buffer, glws::PROFILE_ES1);
369     visual[glws::PROFILE_ES2] = glws::createVisual(double_buffer, glws::PROFILE_ES2);
370
371     for ( ; i < argc; ++i) {
372         if (!parser.open(argv[i])) {
373             std::cerr << "error: failed to open " << argv[i] << "\n";
374             return 1;
375         }
376
377         display();
378
379         parser.close();
380     }
381
382     for (int n = 0; n < glws::PROFILE_MAX; n++) {
383         delete visual[n];
384     }
385
386     glws::cleanup();
387
388     return 0;
389 }
390
391 } /* namespace glretrace */