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