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