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