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