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