]> git.notmuchmail.org Git - apitrace/blob - glretrace_main.cpp
Uniformize warning output.
[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::WindowSystem *ws = NULL;
42 glws::Visual *visual = NULL;
43 glws::Drawable *drawable = NULL;
44 glws::Context *context = NULL;
45
46 unsigned frame = 0;
47 long long startTime = 0;
48 bool wait = false;
49
50 bool benchmark = false;
51 const char *compare_prefix = NULL;
52 const char *snapshot_prefix = NULL;
53 enum frequency snapshot_frequency = FREQUENCY_NEVER;
54
55 unsigned dump_state = ~0;
56
57 void
58 checkGlError(Trace::Call &call) {
59     GLenum error = glGetError();
60     if (error == GL_NO_ERROR) {
61         return;
62     }
63
64     std::ostream & os = retrace::warning(call);
65
66     os << "glGetError(";
67     os << call.name();
68     os << ") = ";
69
70     switch (error) {
71     case GL_INVALID_ENUM:
72         os << "GL_INVALID_ENUM";
73         break;
74     case GL_INVALID_VALUE:
75         os << "GL_INVALID_VALUE";
76         break;
77     case GL_INVALID_OPERATION:
78         os << "GL_INVALID_OPERATION";
79         break;
80     case GL_STACK_OVERFLOW:
81         os << "GL_STACK_OVERFLOW";
82         break;
83     case GL_STACK_UNDERFLOW:
84         os << "GL_STACK_UNDERFLOW";
85         break;
86     case GL_OUT_OF_MEMORY:
87         os << "GL_OUT_OF_MEMORY";
88         break;
89     case GL_INVALID_FRAMEBUFFER_OPERATION:
90         os << "GL_INVALID_FRAMEBUFFER_OPERATION";
91         break;
92     case GL_TABLE_TOO_LARGE:
93         os << "GL_TABLE_TOO_LARGE";
94         break;
95     default:
96         os << error;
97         break;
98     }
99     os << "\n";
100 }
101
102 /**
103  * Grow the current drawble.
104  *
105  * We need to infer the drawable size from GL calls because the drawable sizes
106  * are specified by OS specific calls which we do not trace.
107  */
108 void
109 updateDrawable(int width, int height) {
110     if (!drawable) {
111         return;
112     }
113
114     if (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(unsigned call_no) {
183     ++frame;
184
185     if (snapshot_frequency == FREQUENCY_FRAME ||
186         snapshot_frequency == FREQUENCY_FRAMEBUFFER) {
187         snapshot(call_no);
188     }
189 }
190
191
192 static void display(void) {
193     retrace::Retracer retracer;
194
195     retracer.addCallbacks(gl_callbacks);
196     retracer.addCallbacks(glx_callbacks);
197     retracer.addCallbacks(wgl_callbacks);
198     retracer.addCallbacks(cgl_callbacks);
199
200     startTime = OS::GetTime();
201     Trace::Call *call;
202
203     while ((call = parser.parse_call())) {
204         retracer.retrace(*call);
205
206         if (!insideGlBeginEnd &&
207             drawable && context &&
208             call->no >= dump_state) {
209             glstate::dumpCurrentContext(std::cout);
210             exit(0);
211         }
212
213         delete call;
214     }
215
216     // Reached the end of trace
217     glFlush();
218
219     long long endTime = OS::GetTime();
220     float timeInterval = (endTime - startTime) * 1.0E-6;
221
222     if (retrace::verbosity >= -1) { 
223         std::cout << 
224             "Rendered " << frame << " frames"
225             " in " <<  timeInterval << " secs,"
226             " average of " << (frame/timeInterval) << " fps\n";
227     }
228
229     if (wait) {
230         while (ws->processEvents()) {}
231     } else {
232         exit(0);
233     }
234 }
235
236
237 static void usage(void) {
238     std::cout << 
239         "Usage: glretrace [OPTION] TRACE\n"
240         "Replay TRACE.\n"
241         "\n"
242         "  -b           benchmark mode (no error checking or warning messages)\n"
243         "  -c PREFIX    compare against snapshots\n"
244         "  -db          use a double buffer visual (default)\n"
245         "  -sb          use a single buffer visual\n"
246         "  -s PREFIX    take snapshots; `-` for PNM stdout output\n"
247         "  -S FREQUENCY snapshot frequency: frame (default), framebuffer, or draw\n"
248         "  -v           verbose output\n"
249         "  -D CALLNO    dump state at specific call no\n"
250         "  -w           wait on final frame\n";
251 }
252
253 extern "C"
254 int main(int argc, char **argv)
255 {
256
257     int i;
258     for (i = 1; i < argc; ++i) {
259         const char *arg = argv[i];
260
261         if (arg[0] != '-') {
262             break;
263         }
264
265         if (!strcmp(arg, "--")) {
266             break;
267         } else if (!strcmp(arg, "-b")) {
268             benchmark = true;
269             retrace::verbosity = -1;
270         } else if (!strcmp(arg, "-c")) {
271             compare_prefix = argv[++i];
272             if (snapshot_frequency == FREQUENCY_NEVER) {
273                 snapshot_frequency = FREQUENCY_FRAME;
274             }
275         } else if (!strcmp(arg, "-D")) {
276             dump_state = atoi(argv[++i]);
277             retrace::verbosity = -2;
278         } else if (!strcmp(arg, "-db")) {
279             double_buffer = true;
280         } else if (!strcmp(arg, "-sb")) {
281             double_buffer = false;
282         } else if (!strcmp(arg, "--help")) {
283             usage();
284             return 0;
285         } else if (!strcmp(arg, "-s")) {
286             snapshot_prefix = argv[++i];
287             if (snapshot_frequency == FREQUENCY_NEVER) {
288                 snapshot_frequency = FREQUENCY_FRAME;
289             }
290             if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
291                 retrace::verbosity = -2;
292             }
293         } else if (!strcmp(arg, "-S")) {
294             arg = argv[++i];
295             if (!strcmp(arg, "frame")) {
296                 snapshot_frequency = FREQUENCY_FRAME;
297             } else if (!strcmp(arg, "framebuffer")) {
298                 snapshot_frequency = FREQUENCY_FRAMEBUFFER;
299             } else if (!strcmp(arg, "draw")) {
300                 snapshot_frequency = FREQUENCY_DRAW;
301             } else {
302                 std::cerr << "error: unknown frequency " << arg << "\n";
303                 usage();
304                 return 1;
305             }
306             if (snapshot_prefix == NULL) {
307                 snapshot_prefix = "";
308             }
309         } else if (!strcmp(arg, "-v")) {
310             ++retrace::verbosity;
311         } else if (!strcmp(arg, "-w")) {
312             wait = true;
313         } else {
314             std::cerr << "error: unknown option " << arg << "\n";
315             usage();
316             return 1;
317         }
318     }
319
320     ws = glws::createNativeWindowSystem();
321     visual = ws->createVisual(double_buffer);
322
323     for ( ; i < argc; ++i) {
324         if (!parser.open(argv[i])) {
325             std::cerr << "error: failed to open " << argv[i] << "\n";
326             return 1;
327         }
328
329         display();
330
331         parser.close();
332     }
333
334     return 0;
335 }
336
337 } /* namespace glretrace */