]> git.cworth.org Git - apitrace/blob - glretrace_main.cpp
Use call number instead of frame number for snapshot filenames.
[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 "glretrace.hpp"
33
34
35 namespace glretrace {
36
37 bool double_buffer = false;
38 bool insideGlBeginEnd = false;
39 Trace::Parser parser;
40 glws::WindowSystem *ws = NULL;
41 glws::Visual *visual = NULL;
42 glws::Drawable *drawable = NULL;
43 glws::Context *context = NULL;
44
45 int window_width = 256, window_height = 256;
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
55 unsigned dump_state = ~0;
56
57 void
58 checkGlError(void) {
59     if (benchmark || insideGlBeginEnd) {
60         return;
61     }
62
63     GLenum error = glGetError();
64     if (error == GL_NO_ERROR) {
65         return;
66     }
67
68     std::cerr << "warning: glGetError() = ";
69     switch (error) {
70     case GL_INVALID_ENUM:
71         std::cerr << "GL_INVALID_ENUM";
72         break;
73     case GL_INVALID_VALUE:
74         std::cerr << "GL_INVALID_VALUE";
75         break;
76     case GL_INVALID_OPERATION:
77         std::cerr << "GL_INVALID_OPERATION";
78         break;
79     case GL_STACK_OVERFLOW:
80         std::cerr << "GL_STACK_OVERFLOW";
81         break;
82     case GL_STACK_UNDERFLOW:
83         std::cerr << "GL_STACK_UNDERFLOW";
84         break;
85     case GL_OUT_OF_MEMORY:
86         std::cerr << "GL_OUT_OF_MEMORY";
87         break;
88     case GL_INVALID_FRAMEBUFFER_OPERATION:
89         std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
90         break;
91     case GL_TABLE_TOO_LARGE:
92         std::cerr << "GL_TABLE_TOO_LARGE";
93         break;
94     default:
95         std::cerr << error;
96         break;
97     }
98     std::cerr << "\n";
99 }
100
101
102 static void snapshot(Image::Image &image) {
103     GLint drawbuffer = double_buffer ? GL_BACK : GL_FRONT;
104     GLint readbuffer = double_buffer ? GL_BACK : GL_FRONT;
105     glGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);
106     glGetIntegerv(GL_READ_BUFFER, &readbuffer);
107     glReadBuffer(drawbuffer);
108     glReadPixels(0, 0, image.width, image.height, GL_RGBA, GL_UNSIGNED_BYTE, image.pixels);
109     checkGlError();
110     glReadBuffer(readbuffer);
111 }
112
113
114 static void frame_complete(unsigned call_no) {
115     ++frame;
116     
117     if (snapshot_prefix || compare_prefix) {
118         Image::Image *ref = NULL;
119         if (compare_prefix) {
120             char filename[PATH_MAX];
121             snprintf(filename, sizeof filename, "%s%010u.png", compare_prefix, call_no);
122             ref = Image::readPNG(filename);
123             if (!ref) {
124                 return;
125             }
126             if (retrace::verbosity >= 0)
127                 std::cout << "Read " << filename << "\n";
128         }
129         
130         Image::Image src(window_width, window_height, true);
131         snapshot(src);
132
133         if (snapshot_prefix) {
134             char filename[PATH_MAX];
135             snprintf(filename, sizeof filename, "%s%010u.png", snapshot_prefix, call_no);
136             if (src.writePNG(filename) && retrace::verbosity >= 0) {
137                 std::cout << "Wrote " << filename << "\n";
138             }
139         }
140
141         if (ref) {
142             std::cout << "Snapshot " << call_no << " average precision of " << src.compare(*ref) << " bits\n";
143             delete ref;
144         }
145     }
146
147 }
148
149
150 static void display(void) {
151     Trace::Call *call;
152
153     while ((call = parser.parse_call())) {
154         const std::string &name = call->name();
155
156         if ((name[0] == 'w' && name[1] == 'g' && name[2] == 'l') ||
157             (name[0] == 'g' && name[1] == 'l' && name[2] == 'X')) {
158             // XXX: We ignore the majority of the OS-specific calls for now
159             if (name == "glXSwapBuffers" ||
160                 name == "wglSwapBuffers") {
161                 if (retrace::verbosity >= 1) {
162                     std::cout << *call;
163                     std::cout.flush();
164                 };
165                 frame_complete(call->no);
166                 if (double_buffer)
167                     drawable->swapBuffers();
168                 else
169                     glFlush();
170             } else if (name == "glXMakeCurrent" ||
171                        name == "wglMakeCurrent") {
172                 glFlush();
173                 if (!double_buffer) {
174                     frame_complete(call->no);
175                 }
176             } else {
177                 continue;
178             }
179         }
180
181         if (name == "glFlush") {
182             glFlush();
183             if (!double_buffer) {
184                 frame_complete(call->no);
185             }
186         }
187         
188         retrace::retrace_call(*call);
189
190         if (!insideGlBeginEnd && call->no >= dump_state) {
191             state_dump(std::cout);
192             exit(0);
193         }
194
195         delete call;
196     }
197
198     // Reached the end of trace
199     glFlush();
200
201     long long endTime = OS::GetTime();
202     float timeInterval = (endTime - startTime) * 1.0E-6;
203
204     if (retrace::verbosity >= -1) { 
205         std::cout << 
206             "Rendered " << frame << " frames"
207             " in " <<  timeInterval << " secs,"
208             " average of " << (frame/timeInterval) << " fps\n";
209     }
210
211     if (wait) {
212         while (ws->processEvents()) {}
213     } else {
214         exit(0);
215     }
216 }
217
218
219 static void usage(void) {
220     std::cout << 
221         "Usage: glretrace [OPTION] TRACE\n"
222         "Replay TRACE.\n"
223         "\n"
224         "  -b           benchmark (no glgeterror; no messages)\n"
225         "  -c PREFIX    compare against snapshots\n"
226         "  -db          use a double buffer visual\n"
227         "  -s PREFIX    take snapshots\n"
228         "  -v           verbose output\n"
229         "  -D CALLNO    dump state at specific call no\n"
230         "  -w           wait on final frame\n";
231 }
232
233 extern "C"
234 int main(int argc, char **argv)
235 {
236
237     int i;
238     for (i = 1; i < argc; ++i) {
239         const char *arg = argv[i];
240
241         if (arg[0] != '-') {
242             break;
243         }
244
245         if (!strcmp(arg, "--")) {
246             break;
247         } else if (!strcmp(arg, "-b")) {
248             benchmark = true;
249             retrace::verbosity = -1;
250         } else if (!strcmp(arg, "-c")) {
251             compare_prefix = argv[++i];
252         } else if (!strcmp(arg, "-D")) {
253             dump_state = atoi(argv[++i]);
254             retrace::verbosity = -2;
255         } else if (!strcmp(arg, "-db")) {
256             double_buffer = true;
257         } else if (!strcmp(arg, "--help")) {
258             usage();
259             return 0;
260         } else if (!strcmp(arg, "-s")) {
261             snapshot_prefix = argv[++i];
262         } else if (!strcmp(arg, "-v")) {
263             ++retrace::verbosity;
264         } else if (!strcmp(arg, "-w")) {
265             wait = true;
266         } else {
267             std::cerr << "error: unknown option " << arg << "\n";
268             usage();
269             return 1;
270         }
271     }
272
273     ws = glws::createNativeWindowSystem();
274     visual = ws->createVisual(double_buffer);
275     drawable = ws->createDrawable(visual);
276     drawable->resize(window_width, window_height);
277     context = ws->createContext(visual);
278     ws->makeCurrent(drawable, context);
279
280     for ( ; i < argc; ++i) {
281         if (parser.open(argv[i])) {
282             startTime = OS::GetTime();
283             display();
284             parser.close();
285         }
286     }
287
288     return 0;
289 }
290
291 } /* namespace glretrace */