]> git.cworth.org Git - apitrace/blob - glretrace_main.cpp
Add an option to indent the glsl code in the shader viewer.
[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     if (retrace::verbosity == 0) {
65         std::cout << call;
66         std::cout.flush();
67     }
68
69     std::cerr << call.no << ": ";
70     std::cerr << "warning: glGetError(";
71     std::cerr << call.name();
72     std::cerr << ") = ";
73
74     switch (error) {
75     case GL_INVALID_ENUM:
76         std::cerr << "GL_INVALID_ENUM";
77         break;
78     case GL_INVALID_VALUE:
79         std::cerr << "GL_INVALID_VALUE";
80         break;
81     case GL_INVALID_OPERATION:
82         std::cerr << "GL_INVALID_OPERATION";
83         break;
84     case GL_STACK_OVERFLOW:
85         std::cerr << "GL_STACK_OVERFLOW";
86         break;
87     case GL_STACK_UNDERFLOW:
88         std::cerr << "GL_STACK_UNDERFLOW";
89         break;
90     case GL_OUT_OF_MEMORY:
91         std::cerr << "GL_OUT_OF_MEMORY";
92         break;
93     case GL_INVALID_FRAMEBUFFER_OPERATION:
94         std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
95         break;
96     case GL_TABLE_TOO_LARGE:
97         std::cerr << "GL_TABLE_TOO_LARGE";
98         break;
99     default:
100         std::cerr << error;
101         break;
102     }
103     std::cerr << "\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 (width  <= glretrace::drawable->width &&
119         height <= glretrace::drawable->height) {
120         return;
121     }
122
123     // Check for bound framebuffer last, as this may have a performance impact.
124     GLint draw_framebuffer = 0;
125     glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
126     if (draw_framebuffer != 0) {
127         return;
128     }
129
130     glretrace::drawable->resize(width, height);
131     if (!drawable->visible) {
132         drawable->show();
133     }
134     glScissor(0, 0, width, height);
135 }
136
137
138 void snapshot(unsigned call_no) {
139     if (!drawable ||
140         (!snapshot_prefix && !compare_prefix)) {
141         return;
142     }
143
144     Image::Image *ref = NULL;
145
146     if (compare_prefix) {
147         char filename[PATH_MAX];
148         snprintf(filename, sizeof filename, "%s%010u.png", compare_prefix, call_no);
149         ref = Image::readPNG(filename);
150         if (!ref) {
151             return;
152         }
153         if (retrace::verbosity >= 0) {
154             std::cout << "Read " << filename << "\n";
155         }
156     }
157
158     Image::Image *src = glstate::getDrawBufferImage(GL_RGBA);
159     if (!src) {
160         return;
161     }
162
163     if (snapshot_prefix) {
164         if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
165             char comment[21];
166             snprintf(comment, sizeof comment, "%u", call_no);
167             src->writePNM(std::cout, comment);
168         } else {
169             char filename[PATH_MAX];
170             snprintf(filename, sizeof filename, "%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(unsigned call_no) {
187     ++frame;
188
189     if (snapshot_frequency == FREQUENCY_FRAME ||
190         snapshot_frequency == FREQUENCY_FRAMEBUFFER) {
191         snapshot(call_no);
192     }
193 }
194
195
196 static void display(void) {
197     startTime = OS::GetTime();
198     Trace::Call *call;
199
200     while ((call = parser.parse_call())) {
201         const char *name = call->name();
202
203         if (retrace::verbosity >= 1) {
204             std::cout << *call;
205             std::cout.flush();
206         }
207
208         if (name[0] == 'C' && name[1] == 'G' && name[2] == 'L') {
209             glretrace::retrace_call_cgl(*call);
210         }
211         else if (name[0] == 'w' && name[1] == 'g' && name[2] == 'l') {
212             glretrace::retrace_call_wgl(*call);
213         }
214         else if (name[0] == 'g' && name[1] == 'l' && name[2] == 'X') {
215             glretrace::retrace_call_glx(*call);
216         } else {
217             retrace::retrace_call(*call);
218         }
219
220         if (!insideGlBeginEnd &&
221             drawable && context &&
222             call->no >= dump_state) {
223             glstate::dumpCurrentContext(std::cout);
224             exit(0);
225         }
226
227         delete call;
228     }
229
230     // Reached the end of trace
231     glFlush();
232
233     long long endTime = OS::GetTime();
234     float timeInterval = (endTime - startTime) * 1.0E-6;
235
236     if (retrace::verbosity >= -1) { 
237         std::cout << 
238             "Rendered " << frame << " frames"
239             " in " <<  timeInterval << " secs,"
240             " average of " << (frame/timeInterval) << " fps\n";
241     }
242
243     if (wait) {
244         while (ws->processEvents()) {}
245     } else {
246         exit(0);
247     }
248 }
249
250
251 static void usage(void) {
252     std::cout << 
253         "Usage: glretrace [OPTION] TRACE\n"
254         "Replay TRACE.\n"
255         "\n"
256         "  -b           benchmark mode (no error checking or warning messages)\n"
257         "  -c PREFIX    compare against snapshots\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           verbose output\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         } else if (!strcmp(arg, "-c")) {
285             compare_prefix = argv[++i];
286             if (snapshot_frequency == FREQUENCY_NEVER) {
287                 snapshot_frequency = FREQUENCY_FRAME;
288             }
289         } else if (!strcmp(arg, "-D")) {
290             dump_state = atoi(argv[++i]);
291             retrace::verbosity = -2;
292         } else if (!strcmp(arg, "-db")) {
293             double_buffer = true;
294         } else if (!strcmp(arg, "-sb")) {
295             double_buffer = false;
296         } else if (!strcmp(arg, "--help")) {
297             usage();
298             return 0;
299         } else if (!strcmp(arg, "-s")) {
300             snapshot_prefix = argv[++i];
301             if (snapshot_frequency == FREQUENCY_NEVER) {
302                 snapshot_frequency = FREQUENCY_FRAME;
303             }
304             if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
305                 retrace::verbosity = -2;
306             }
307         } else if (!strcmp(arg, "-S")) {
308             arg = argv[++i];
309             if (!strcmp(arg, "frame")) {
310                 snapshot_frequency = FREQUENCY_FRAME;
311             } else if (!strcmp(arg, "framebuffer")) {
312                 snapshot_frequency = FREQUENCY_FRAMEBUFFER;
313             } else if (!strcmp(arg, "draw")) {
314                 snapshot_frequency = FREQUENCY_DRAW;
315             } else {
316                 std::cerr << "error: unknown frequency " << arg << "\n";
317                 usage();
318                 return 1;
319             }
320             if (snapshot_prefix == NULL) {
321                 snapshot_prefix = "";
322             }
323         } else if (!strcmp(arg, "-v")) {
324             ++retrace::verbosity;
325         } else if (!strcmp(arg, "-w")) {
326             wait = true;
327         } else {
328             std::cerr << "error: unknown option " << arg << "\n";
329             usage();
330             return 1;
331         }
332     }
333
334     ws = glws::createNativeWindowSystem();
335     visual = ws->createVisual(double_buffer);
336
337     for ( ; i < argc; ++i) {
338         if (!parser.open(argv[i])) {
339             std::cerr << "error: failed to open " << argv[i] << "\n";
340             return 1;
341         }
342
343         display();
344
345         parser.close();
346     }
347
348     return 0;
349 }
350
351 } /* namespace glretrace */