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