]> git.cworth.org Git - apitrace/blob - glretrace_main.cpp
Fix and cleanup state lookups on frames.
[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 void snapshot(unsigned call_no) {
108     if (!drawable ||
109         (!snapshot_prefix && !compare_prefix)) {
110         return;
111     }
112
113     Image::Image *ref = NULL;
114
115     if (compare_prefix) {
116         char filename[PATH_MAX];
117         snprintf(filename, sizeof filename, "%s%010u.png", compare_prefix, call_no);
118         ref = Image::readPNG(filename);
119         if (!ref) {
120             return;
121         }
122         if (retrace::verbosity >= 0) {
123             std::cout << "Read " << filename << "\n";
124         }
125     }
126
127     Image::Image *src = glstate::getDrawBufferImage(GL_RGBA);
128     if (!src) {
129         return;
130     }
131
132     if (snapshot_prefix) {
133         if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
134             char comment[21];
135             snprintf(comment, sizeof comment, "%u", call_no);
136             src->writePNM(std::cout, comment);
137         } else {
138             char filename[PATH_MAX];
139             snprintf(filename, sizeof filename, "%s%010u.png", snapshot_prefix, call_no);
140             if (src->writePNG(filename) && retrace::verbosity >= 0) {
141                 std::cout << "Wrote " << filename << "\n";
142             }
143         }
144     }
145
146     if (ref) {
147         std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
148         delete ref;
149     }
150
151     delete src;
152 }
153
154
155 void frame_complete(unsigned call_no) {
156     ++frame;
157
158     if (snapshot_frequency == FREQUENCY_FRAME ||
159         snapshot_frequency == FREQUENCY_FRAMEBUFFER) {
160         snapshot(call_no);
161     }
162 }
163
164
165 static void display(void) {
166     startTime = OS::GetTime();
167     Trace::Call *call;
168
169     while ((call = parser.parse_call())) {
170         const char *name = call->name();
171
172         if (retrace::verbosity >= 1) {
173             std::cout << *call;
174             std::cout.flush();
175         }
176
177         if (name[0] == 'C' && name[1] == 'G' && name[2] == 'L') {
178             glretrace::retrace_call_cgl(*call);
179         }
180         else if (name[0] == 'w' && name[1] == 'g' && name[2] == 'l') {
181             glretrace::retrace_call_wgl(*call);
182         }
183         else if (name[0] == 'g' && name[1] == 'l' && name[2] == 'X') {
184             glretrace::retrace_call_glx(*call);
185         } else {
186             retrace::retrace_call(*call);
187         }
188
189         if (!insideGlBeginEnd &&
190             drawable && context &&
191             call->no >= dump_state) {
192             glstate::dumpCurrentContext(std::cout);
193             exit(0);
194         }
195
196         delete call;
197     }
198
199     // Reached the end of trace
200     glFlush();
201
202     long long endTime = OS::GetTime();
203     float timeInterval = (endTime - startTime) * 1.0E-6;
204
205     if (retrace::verbosity >= -1) { 
206         std::cout << 
207             "Rendered " << frame << " frames"
208             " in " <<  timeInterval << " secs,"
209             " average of " << (frame/timeInterval) << " fps\n";
210     }
211
212     if (wait) {
213         while (ws->processEvents()) {}
214     } else {
215         exit(0);
216     }
217 }
218
219
220 static void usage(void) {
221     std::cout << 
222         "Usage: glretrace [OPTION] TRACE\n"
223         "Replay TRACE.\n"
224         "\n"
225         "  -b           benchmark mode (no error checking or warning messages)\n"
226         "  -c PREFIX    compare against snapshots\n"
227         "  -db          use a double buffer visual (default)\n"
228         "  -sb          use a single buffer visual\n"
229         "  -s PREFIX    take snapshots; `-` for PNM stdout output\n"
230         "  -S FREQUENCY snapshot frequency: frame (default), framebuffer, or draw\n"
231         "  -v           verbose output\n"
232         "  -D CALLNO    dump state at specific call no\n"
233         "  -w           wait on final frame\n";
234 }
235
236 extern "C"
237 int main(int argc, char **argv)
238 {
239
240     int i;
241     for (i = 1; i < argc; ++i) {
242         const char *arg = argv[i];
243
244         if (arg[0] != '-') {
245             break;
246         }
247
248         if (!strcmp(arg, "--")) {
249             break;
250         } else if (!strcmp(arg, "-b")) {
251             benchmark = true;
252             retrace::verbosity = -1;
253         } else if (!strcmp(arg, "-c")) {
254             compare_prefix = argv[++i];
255             if (snapshot_frequency == FREQUENCY_NEVER) {
256                 snapshot_frequency = FREQUENCY_FRAME;
257             }
258         } else if (!strcmp(arg, "-D")) {
259             dump_state = atoi(argv[++i]);
260             retrace::verbosity = -2;
261         } else if (!strcmp(arg, "-db")) {
262             double_buffer = true;
263         } else if (!strcmp(arg, "-sb")) {
264             double_buffer = false;
265         } else if (!strcmp(arg, "--help")) {
266             usage();
267             return 0;
268         } else if (!strcmp(arg, "-s")) {
269             snapshot_prefix = argv[++i];
270             if (snapshot_frequency == FREQUENCY_NEVER) {
271                 snapshot_frequency = FREQUENCY_FRAME;
272             }
273             if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
274                 retrace::verbosity = -2;
275             }
276         } else if (!strcmp(arg, "-S")) {
277             arg = argv[++i];
278             if (!strcmp(arg, "frame")) {
279                 snapshot_frequency = FREQUENCY_FRAME;
280             } else if (!strcmp(arg, "framebuffer")) {
281                 snapshot_frequency = FREQUENCY_FRAMEBUFFER;
282             } else if (!strcmp(arg, "draw")) {
283                 snapshot_frequency = FREQUENCY_DRAW;
284             } else {
285                 std::cerr << "error: unknown frequency " << arg << "\n";
286                 usage();
287                 return 1;
288             }
289             if (snapshot_prefix == NULL) {
290                 snapshot_prefix = "";
291             }
292         } else if (!strcmp(arg, "-v")) {
293             ++retrace::verbosity;
294         } else if (!strcmp(arg, "-w")) {
295             wait = true;
296         } else {
297             std::cerr << "error: unknown option " << arg << "\n";
298             usage();
299             return 1;
300         }
301     }
302
303     ws = glws::createNativeWindowSystem();
304     visual = ws->createVisual(double_buffer);
305
306     for ( ; i < argc; ++i) {
307         if (!parser.open(argv[i])) {
308             std::cerr << "error: failed to open " << argv[i] << "\n";
309             return 1;
310         }
311
312         display();
313
314         parser.close();
315     }
316
317     return 0;
318 }
319
320 } /* namespace glretrace */