1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
30 #include "retrace.hpp"
32 #include "glstate.hpp"
33 #include "glretrace.hpp"
38 bool double_buffer = true;
39 bool insideGlBeginEnd = false;
41 glws::WindowSystem *ws = NULL;
42 glws::Visual *visual = NULL;
43 glws::Drawable *drawable = NULL;
44 glws::Context *context = NULL;
47 long long startTime = 0;
50 bool benchmark = false;
51 const char *compare_prefix = NULL;
52 const char *snapshot_prefix = NULL;
53 enum frequency snapshot_frequency = FREQUENCY_NEVER;
55 unsigned dump_state = ~0;
58 checkGlError(Trace::Call &call) {
59 GLenum error = glGetError();
60 if (error == GL_NO_ERROR) {
64 if (retrace::verbosity == 0) {
69 std::cerr << call.no << ": ";
70 std::cerr << "warning: glGetError(";
71 std::cerr << call.name();
76 std::cerr << "GL_INVALID_ENUM";
78 case GL_INVALID_VALUE:
79 std::cerr << "GL_INVALID_VALUE";
81 case GL_INVALID_OPERATION:
82 std::cerr << "GL_INVALID_OPERATION";
84 case GL_STACK_OVERFLOW:
85 std::cerr << "GL_STACK_OVERFLOW";
87 case GL_STACK_UNDERFLOW:
88 std::cerr << "GL_STACK_UNDERFLOW";
90 case GL_OUT_OF_MEMORY:
91 std::cerr << "GL_OUT_OF_MEMORY";
93 case GL_INVALID_FRAMEBUFFER_OPERATION:
94 std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
96 case GL_TABLE_TOO_LARGE:
97 std::cerr << "GL_TABLE_TOO_LARGE";
107 * Grow the current drawble.
109 * We need to infer the drawable size from GL calls because the drawable sizes
110 * are specified by OS specific calls which we do not trace.
113 updateDrawable(int width, int height) {
118 if (width <= glretrace::drawable->width &&
119 height <= glretrace::drawable->height) {
123 // Check for bound framebuffer last, as this may have a performance impact.
124 GLint draw_framebuffer = 0;
125 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
126 if (draw_framebuffer != 0) {
130 glretrace::drawable->resize(width, height);
131 if (!drawable->visible) {
134 glScissor(0, 0, width, height);
138 void snapshot(unsigned call_no) {
140 (!snapshot_prefix && !compare_prefix)) {
144 Image::Image *ref = NULL;
146 if (compare_prefix) {
147 char filename[PATH_MAX];
148 snprintf(filename, sizeof filename, "%s%010u.png", compare_prefix, call_no);
149 ref = Image::readPNG(filename);
153 if (retrace::verbosity >= 0) {
154 std::cout << "Read " << filename << "\n";
158 Image::Image *src = glstate::getDrawBufferImage(GL_RGBA);
163 if (snapshot_prefix) {
164 if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
166 snprintf(comment, sizeof comment, "%u", call_no);
167 src->writePNM(std::cout, comment);
169 char filename[PATH_MAX];
170 snprintf(filename, sizeof filename, "%s%010u.png", snapshot_prefix, call_no);
171 if (src->writePNG(filename) && retrace::verbosity >= 0) {
172 std::cout << "Wrote " << filename << "\n";
178 std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
186 void frame_complete(unsigned call_no) {
189 if (snapshot_frequency == FREQUENCY_FRAME ||
190 snapshot_frequency == FREQUENCY_FRAMEBUFFER) {
196 static void display(void) {
197 retrace::Retracer retracer;
199 retracer.addCallbacks(gl_callbacks);
200 retracer.addCallbacks(glx_callbacks);
201 retracer.addCallbacks(wgl_callbacks);
202 retracer.addCallbacks(cgl_callbacks);
204 startTime = OS::GetTime();
207 while ((call = parser.parse_call())) {
208 if (retrace::verbosity >= 1) {
213 retracer.retrace(*call);
215 if (!insideGlBeginEnd &&
216 drawable && context &&
217 call->no >= dump_state) {
218 glstate::dumpCurrentContext(std::cout);
225 // Reached the end of trace
228 long long endTime = OS::GetTime();
229 float timeInterval = (endTime - startTime) * 1.0E-6;
231 if (retrace::verbosity >= -1) {
233 "Rendered " << frame << " frames"
234 " in " << timeInterval << " secs,"
235 " average of " << (frame/timeInterval) << " fps\n";
239 while (ws->processEvents()) {}
246 static void usage(void) {
248 "Usage: glretrace [OPTION] TRACE\n"
251 " -b benchmark mode (no error checking or warning messages)\n"
252 " -c PREFIX compare against snapshots\n"
253 " -db use a double buffer visual (default)\n"
254 " -sb use a single buffer visual\n"
255 " -s PREFIX take snapshots; `-` for PNM stdout output\n"
256 " -S FREQUENCY snapshot frequency: frame (default), framebuffer, or draw\n"
257 " -v verbose output\n"
258 " -D CALLNO dump state at specific call no\n"
259 " -w wait on final frame\n";
263 int main(int argc, char **argv)
267 for (i = 1; i < argc; ++i) {
268 const char *arg = argv[i];
274 if (!strcmp(arg, "--")) {
276 } else if (!strcmp(arg, "-b")) {
278 retrace::verbosity = -1;
279 } else if (!strcmp(arg, "-c")) {
280 compare_prefix = argv[++i];
281 if (snapshot_frequency == FREQUENCY_NEVER) {
282 snapshot_frequency = FREQUENCY_FRAME;
284 } else if (!strcmp(arg, "-D")) {
285 dump_state = atoi(argv[++i]);
286 retrace::verbosity = -2;
287 } else if (!strcmp(arg, "-db")) {
288 double_buffer = true;
289 } else if (!strcmp(arg, "-sb")) {
290 double_buffer = false;
291 } else if (!strcmp(arg, "--help")) {
294 } else if (!strcmp(arg, "-s")) {
295 snapshot_prefix = argv[++i];
296 if (snapshot_frequency == FREQUENCY_NEVER) {
297 snapshot_frequency = FREQUENCY_FRAME;
299 if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
300 retrace::verbosity = -2;
302 } else if (!strcmp(arg, "-S")) {
304 if (!strcmp(arg, "frame")) {
305 snapshot_frequency = FREQUENCY_FRAME;
306 } else if (!strcmp(arg, "framebuffer")) {
307 snapshot_frequency = FREQUENCY_FRAMEBUFFER;
308 } else if (!strcmp(arg, "draw")) {
309 snapshot_frequency = FREQUENCY_DRAW;
311 std::cerr << "error: unknown frequency " << arg << "\n";
315 if (snapshot_prefix == NULL) {
316 snapshot_prefix = "";
318 } else if (!strcmp(arg, "-v")) {
319 ++retrace::verbosity;
320 } else if (!strcmp(arg, "-w")) {
323 std::cerr << "error: unknown option " << arg << "\n";
329 ws = glws::createNativeWindowSystem();
330 visual = ws->createVisual(double_buffer);
332 for ( ; i < argc; ++i) {
333 if (!parser.open(argv[i])) {
334 std::cerr << "error: failed to open " << argv[i] << "\n";
346 } /* namespace glretrace */