]> git.cworth.org Git - apitrace/blob - glretrace_main.cpp
Parse doubles to a new Double class rather than to the Float class.
[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_path.hpp"
30 #include "image.hpp"
31 #include "retrace.hpp"
32 #include "glproc.hpp"
33 #include "glstate.hpp"
34 #include "glretrace.hpp"
35
36
37 namespace glretrace {
38
39 bool double_buffer = true;
40 bool insideGlBeginEnd = false;
41 trace::Parser parser;
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 (drawable->visible &&
115         width  <= drawable->width &&
116         height <= drawable->height) {
117         return;
118     }
119
120     // Check for bound framebuffer last, as this may have a performance impact.
121     GLint draw_framebuffer = 0;
122     glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
123     if (draw_framebuffer != 0) {
124         return;
125     }
126
127     drawable->resize(width, height);
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         os::Path filename = os::Path::format("%s%010u.png", compare_prefix, call_no);
144         ref = image::readPNG(filename);
145         if (!ref) {
146             return;
147         }
148         if (retrace::verbosity >= 0) {
149             std::cout << "Read " << filename << "\n";
150         }
151     }
152
153     image::Image *src = glstate::getDrawBufferImage(GL_RGBA);
154     if (!src) {
155         return;
156     }
157
158     if (snapshot_prefix) {
159         if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
160             char comment[21];
161             snprintf(comment, sizeof comment, "%u", call_no);
162             src->writePNM(std::cout, comment);
163         } else {
164             os::Path filename = os::Path::format("%s%010u.png", snapshot_prefix, call_no);
165             if (src->writePNG(filename) && retrace::verbosity >= 0) {
166                 std::cout << "Wrote " << filename << "\n";
167             }
168         }
169     }
170
171     if (ref) {
172         std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
173         delete ref;
174     }
175
176     delete src;
177 }
178
179
180 void frame_complete(trace::Call &call) {
181     ++frame;
182
183     if (!drawable) {
184         return;
185     }
186
187     if (!drawable->visible) {
188         retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
189     }
190
191     if (snapshot_frequency == FREQUENCY_FRAME ||
192         snapshot_frequency == FREQUENCY_FRAMEBUFFER) {
193         snapshot(call.no);
194     }
195 }
196
197
198 static void display(void) {
199     retrace::Retracer retracer;
200
201     retracer.addCallbacks(gl_callbacks);
202     retracer.addCallbacks(glx_callbacks);
203     retracer.addCallbacks(wgl_callbacks);
204     retracer.addCallbacks(cgl_callbacks);
205     retracer.addCallbacks(egl_callbacks);
206
207     startTime = os::getTime();
208     trace::Call *call;
209
210     while ((call = parser.parse_call())) {
211         retracer.retrace(*call);
212
213         if (!insideGlBeginEnd &&
214             drawable && context &&
215             call->no >= dump_state) {
216             glstate::dumpCurrentContext(std::cout);
217             exit(0);
218         }
219
220         delete call;
221     }
222
223     // Reached the end of trace
224     glFlush();
225
226     long long endTime = os::getTime();
227     float timeInterval = (endTime - startTime) * 1.0E-6;
228
229     if (retrace::verbosity >= -1) { 
230         std::cout << 
231             "Rendered " << frame << " frames"
232             " in " <<  timeInterval << " secs,"
233             " average of " << (frame/timeInterval) << " fps\n";
234     }
235
236     if (wait) {
237         while (glws::processEvents()) {}
238     } else {
239         exit(0);
240     }
241 }
242
243
244 static void usage(void) {
245     std::cout << 
246         "Usage: glretrace [OPTION] TRACE\n"
247         "Replay TRACE.\n"
248         "\n"
249         "  -b           benchmark mode (no error checking or warning messages)\n"
250         "  -c PREFIX    compare against snapshots\n"
251         "  -db          use a double buffer visual (default)\n"
252         "  -sb          use a single buffer visual\n"
253         "  -s PREFIX    take snapshots; `-` for PNM stdout output\n"
254         "  -S FREQUENCY snapshot frequency: frame (default), framebuffer, or draw\n"
255         "  -v           verbose output\n"
256         "  -D CALLNO    dump state at specific call no\n"
257         "  -w           wait on final frame\n";
258 }
259
260 extern "C"
261 int main(int argc, char **argv)
262 {
263
264     int i;
265     for (i = 1; i < argc; ++i) {
266         const char *arg = argv[i];
267
268         if (arg[0] != '-') {
269             break;
270         }
271
272         if (!strcmp(arg, "--")) {
273             break;
274         } else if (!strcmp(arg, "-b")) {
275             benchmark = true;
276             retrace::verbosity = -1;
277             glws::debug = false;
278         } else if (!strcmp(arg, "-c")) {
279             compare_prefix = argv[++i];
280             if (snapshot_frequency == FREQUENCY_NEVER) {
281                 snapshot_frequency = FREQUENCY_FRAME;
282             }
283         } else if (!strcmp(arg, "-D")) {
284             dump_state = atoi(argv[++i]);
285             retrace::verbosity = -2;
286         } else if (!strcmp(arg, "-db")) {
287             double_buffer = true;
288         } else if (!strcmp(arg, "-sb")) {
289             double_buffer = false;
290         } else if (!strcmp(arg, "--help")) {
291             usage();
292             return 0;
293         } else if (!strcmp(arg, "-s")) {
294             snapshot_prefix = argv[++i];
295             if (snapshot_frequency == FREQUENCY_NEVER) {
296                 snapshot_frequency = FREQUENCY_FRAME;
297             }
298             if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
299                 retrace::verbosity = -2;
300             }
301         } else if (!strcmp(arg, "-S")) {
302             arg = argv[++i];
303             if (!strcmp(arg, "frame")) {
304                 snapshot_frequency = FREQUENCY_FRAME;
305             } else if (!strcmp(arg, "framebuffer")) {
306                 snapshot_frequency = FREQUENCY_FRAMEBUFFER;
307             } else if (!strcmp(arg, "draw")) {
308                 snapshot_frequency = FREQUENCY_DRAW;
309             } else {
310                 std::cerr << "error: unknown frequency " << arg << "\n";
311                 usage();
312                 return 1;
313             }
314             if (snapshot_prefix == NULL) {
315                 snapshot_prefix = "";
316             }
317         } else if (!strcmp(arg, "-v")) {
318             ++retrace::verbosity;
319         } else if (!strcmp(arg, "-w")) {
320             wait = true;
321         } else {
322             std::cerr << "error: unknown option " << arg << "\n";
323             usage();
324             return 1;
325         }
326     }
327
328     glws::init();
329     visual = glws::createVisual(double_buffer);
330
331     for ( ; i < argc; ++i) {
332         if (!parser.open(argv[i])) {
333             std::cerr << "error: failed to open " << argv[i] << "\n";
334             return 1;
335         }
336
337         display();
338
339         parser.close();
340     }
341     
342     delete visual;
343     glws::cleanup();
344
345     return 0;
346 }
347
348 } /* namespace glretrace */