]> git.cworth.org Git - apitrace/blob - retrace/glretrace_main.cpp
Move benchmark flag into common retrace code.
[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 "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 glws::Profile defaultProfile = glws::PROFILE_COMPAT;
45 glws::Visual *visual[glws::PROFILE_MAX];
46 glws::Drawable *drawable = NULL;
47 glws::Context *context = NULL;
48
49 unsigned frame = 0;
50 long long startTime = 0;
51 bool wait = false;
52
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;
57
58 unsigned dump_state = ~0;
59
60 void
61 checkGlError(trace::Call &call) {
62     GLenum error = glGetError();
63     if (error == GL_NO_ERROR) {
64         return;
65     }
66
67     std::ostream & os = retrace::warning(call);
68
69     os << "glGetError(";
70     os << call.name();
71     os << ") = ";
72
73     switch (error) {
74     case GL_INVALID_ENUM:
75         os << "GL_INVALID_ENUM";
76         break;
77     case GL_INVALID_VALUE:
78         os << "GL_INVALID_VALUE";
79         break;
80     case GL_INVALID_OPERATION:
81         os << "GL_INVALID_OPERATION";
82         break;
83     case GL_STACK_OVERFLOW:
84         os << "GL_STACK_OVERFLOW";
85         break;
86     case GL_STACK_UNDERFLOW:
87         os << "GL_STACK_UNDERFLOW";
88         break;
89     case GL_OUT_OF_MEMORY:
90         os << "GL_OUT_OF_MEMORY";
91         break;
92     case GL_INVALID_FRAMEBUFFER_OPERATION:
93         os << "GL_INVALID_FRAMEBUFFER_OPERATION";
94         break;
95     case GL_TABLE_TOO_LARGE:
96         os << "GL_TABLE_TOO_LARGE";
97         break;
98     default:
99         os << error;
100         break;
101     }
102     os << "\n";
103 }
104
105 /**
106  * Grow the current drawble.
107  *
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.
110  */
111 void
112 updateDrawable(int width, int height) {
113     if (!drawable) {
114         return;
115     }
116
117     if (drawable->visible &&
118         width  <= drawable->width &&
119         height <= drawable->height) {
120         return;
121     }
122
123     // Ignore zero area viewports
124     if (width == 0 || height == 0) {
125         return;
126     }
127
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) {
132         return;
133     }
134
135     drawable->resize(width, height);
136     drawable->show();
137
138     glScissor(0, 0, width, height);
139 }
140
141
142 static void
143 snapshot(unsigned call_no) {
144     assert(snapshot_prefix || compare_prefix);
145
146     if (!drawable) {
147         return;
148     }
149
150     image::Image *ref = NULL;
151
152     if (compare_prefix) {
153         os::String filename = os::String::format("%s%010u.png", compare_prefix, call_no);
154         ref = image::readPNG(filename);
155         if (!ref) {
156             return;
157         }
158         if (retrace::verbosity >= 0) {
159             std::cout << "Read " << filename << "\n";
160         }
161     }
162
163     image::Image *src = glstate::getDrawBufferImage();
164     if (!src) {
165         return;
166     }
167
168     if (snapshot_prefix) {
169         if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
170             char comment[21];
171             snprintf(comment, sizeof comment, "%u", call_no);
172             src->writePNM(std::cout, comment);
173         } else {
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";
177             }
178         }
179     }
180
181     if (ref) {
182         std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
183         delete ref;
184     }
185
186     delete src;
187 }
188
189
190 void frame_complete(trace::Call &call) {
191     ++frame;
192
193     if (!drawable) {
194         return;
195     }
196
197     if (!drawable->visible) {
198         retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
199     }
200 }
201
202
203 static void display(void) {
204     retrace::Retracer retracer;
205
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);
211
212     startTime = os::getTime();
213     trace::Call *call;
214
215     while ((call = retrace::parser.parse_call())) {
216         bool swapRenderTarget = call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET;
217         bool doSnapshot =
218             snapshot_frequency.contains(*call) ||
219             compare_frequency.contains(*call)
220         ;
221
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.
228                 snapshot(call->no);
229             } else {
230                 // Whereas for ordinate fbo/rendertarget changes we use the
231                 // previous call's number.
232                 snapshot(call->no - 1);
233             }
234         }
235
236         retracer.retrace(*call);
237
238         if (doSnapshot && !swapRenderTarget) {
239             snapshot(call->no);
240         }
241
242         if (!insideGlBeginEnd &&
243             drawable && context &&
244             call->no >= dump_state) {
245             glstate::dumpCurrentContext(std::cout);
246             exit(0);
247         }
248
249         delete call;
250     }
251
252     // Reached the end of trace
253     glFlush();
254
255     long long endTime = os::getTime();
256     float timeInterval = (endTime - startTime) * (1.0 / os::timeFrequency);
257
258     if ((retrace::verbosity >= -1) || (retrace::profiling)) {
259         std::cout << 
260             "Rendered " << frame << " frames"
261             " in " <<  timeInterval << " secs,"
262             " average of " << (frame/timeInterval) << " fps\n";
263     }
264
265     if (wait) {
266         while (glws::processEvents()) {}
267     } else {
268         exit(0);
269     }
270 }
271
272
273 static void usage(void) {
274     std::cout << 
275         "Usage: glretrace [OPTION] TRACE\n"
276         "Replay TRACE.\n"
277         "\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";
290 }
291
292 extern "C"
293 int main(int argc, char **argv)
294 {
295     assert(compare_frequency.empty());
296     assert(snapshot_frequency.empty());
297
298     int i;
299     for (i = 1; i < argc; ++i) {
300         const char *arg = argv[i];
301
302         if (arg[0] != '-') {
303             break;
304         }
305
306         if (!strcmp(arg, "--")) {
307             break;
308         } else if (!strcmp(arg, "-b")) {
309             retrace::benchmark = true;
310             retrace::verbosity = -1;
311             glws::debug = false;
312         } else if (!strcmp(arg, "-p")) {
313             retrace::profiling = true;
314             retrace::verbosity = -1;
315             glws::debug = false;
316         } else if (!strcmp(arg, "-c")) {
317             compare_prefix = argv[++i];
318             if (compare_frequency.empty()) {
319                 compare_frequency = trace::CallSet(trace::FREQUENCY_FRAME);
320             }
321         } else if (!strcmp(arg, "-C")) {
322             compare_frequency = trace::CallSet(argv[++i]);
323             if (compare_prefix == NULL) {
324                 compare_prefix = "";
325             }
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")) {
336             usage();
337             return 0;
338         } else if (!strcmp(arg, "-s")) {
339             snapshot_prefix = argv[++i];
340             if (snapshot_frequency.empty()) {
341                 snapshot_frequency = trace::CallSet(trace::FREQUENCY_FRAME);
342             }
343             if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
344                 os::setBinaryMode(stdout);
345                 retrace::verbosity = -2;
346             }
347         } else if (!strcmp(arg, "-S")) {
348             snapshot_frequency = trace::CallSet(argv[++i]);
349             if (snapshot_prefix == NULL) {
350                 snapshot_prefix = "";
351             }
352         } else if (!strcmp(arg, "-v")) {
353             ++retrace::verbosity;
354         } else if (!strcmp(arg, "-w")) {
355             wait = true;
356         } else {
357             std::cerr << "error: unknown option " << arg << "\n";
358             usage();
359             return 1;
360         }
361     }
362
363     glws::init();
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);
368
369     for ( ; i < argc; ++i) {
370         if (!retrace::parser.open(argv[i])) {
371             std::cerr << "error: failed to open " << argv[i] << "\n";
372             return 1;
373         }
374
375         display();
376
377         retrace::parser.close();
378     }
379
380     for (int n = 0; n < glws::PROFILE_MAX; n++) {
381         delete visual[n];
382     }
383
384     glws::cleanup();
385
386     return 0;
387 }
388
389 } /* namespace glretrace */