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