]> git.cworth.org Git - apitrace/blob - glretrace_main.cpp
Remove spurious tag in snapdiff output.
[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(void) {
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%04u.png", compare_prefix, frame);
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%04u.png", snapshot_prefix, frame);
136             if (src.writePNG(filename) && retrace::verbosity >= 0) {
137                 std::cout << "Wrote " << filename << "\n";
138             }
139         }
140
141         if (ref) {
142             std::cout << "Frame " << frame << " average precision of " << src.compare(*ref) << " bits\n";
143             delete ref;
144         }
145     }
146     
147     ws->processEvents();
148 }
149
150
151 static void display(void) {
152     Trace::Call *call;
153
154     while ((call = parser.parse_call())) {
155         const std::string &name = call->name();
156
157         if ((name[0] == 'w' && name[1] == 'g' && name[2] == 'l') ||
158             (name[0] == 'g' && name[1] == 'l' && name[2] == 'X')) {
159             // XXX: We ignore the majority of the OS-specific calls for now
160             if (name == "glXSwapBuffers" ||
161                 name == "wglSwapBuffers") {
162                 if (retrace::verbosity >= 1) {
163                     std::cout << *call;
164                     std::cout.flush();
165                 };
166                 frame_complete();
167                 if (double_buffer)
168                     drawable->swapBuffers();
169                 else
170                     glFlush();
171             } else if (name == "glXMakeCurrent" ||
172                        name == "wglMakeCurrent") {
173                 glFlush();
174                 if (!double_buffer) {
175                     frame_complete();
176                 }
177             } else {
178                 continue;
179             }
180         }
181
182         if (name == "glFlush") {
183             glFlush();
184             if (!double_buffer) {
185                 frame_complete();
186             }
187         }
188         
189         retrace::retrace_call(*call);
190
191         if (!insideGlBeginEnd && call->no >= dump_state) {
192             state_dump(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 (no glgeterror; no messages)\n"
226         "  -c PREFIX    compare against snapshots\n"
227         "  -db          use a double buffer visual\n"
228         "  -s PREFIX    take snapshots\n"
229         "  -v           verbose output\n"
230         "  -D CALLNO    dump state at specific call no\n"
231         "  -w           wait on final frame\n";
232 }
233
234 extern "C"
235 int main(int argc, char **argv)
236 {
237
238     int i;
239     for (i = 1; i < argc; ++i) {
240         const char *arg = argv[i];
241
242         if (arg[0] != '-') {
243             break;
244         }
245
246         if (!strcmp(arg, "--")) {
247             break;
248         } else if (!strcmp(arg, "-b")) {
249             benchmark = true;
250             retrace::verbosity = -1;
251         } else if (!strcmp(arg, "-c")) {
252             compare_prefix = argv[++i];
253         } else if (!strcmp(arg, "-D")) {
254             dump_state = atoi(argv[++i]);
255             retrace::verbosity = -2;
256         } else if (!strcmp(arg, "-db")) {
257             double_buffer = true;
258         } else if (!strcmp(arg, "--help")) {
259             usage();
260             return 0;
261         } else if (!strcmp(arg, "-s")) {
262             snapshot_prefix = argv[++i];
263         } else if (!strcmp(arg, "-v")) {
264             ++retrace::verbosity;
265         } else if (!strcmp(arg, "-w")) {
266             wait = true;
267         } else {
268             std::cerr << "error: unknown option " << arg << "\n";
269             usage();
270             return 1;
271         }
272     }
273
274     ws = glws::createNativeWindowSystem();
275     visual = ws->createVisual(double_buffer);
276     drawable = ws->createDrawable(visual);
277     drawable->resize(window_width, window_height);
278     context = ws->createContext(visual);
279     ws->makeCurrent(drawable, context);
280
281     for ( ; i < argc; ++i) {
282         if (parser.open(argv[i])) {
283             startTime = OS::GetTime();
284             display();
285             parser.close();
286         }
287     }
288
289     return 0;
290 }
291
292 } /* namespace glretrace */