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::Visual *visual = NULL;
42 glws::Drawable *drawable = NULL;
43 glws::Context *context = NULL;
46 long long startTime = 0;
49 bool benchmark = false;
50 const char *compare_prefix = NULL;
51 const char *snapshot_prefix = NULL;
52 enum frequency snapshot_frequency = FREQUENCY_NEVER;
54 unsigned dump_state = ~0;
57 checkGlError(trace::Call &call) {
58 GLenum error = glGetError();
59 if (error == GL_NO_ERROR) {
63 std::ostream & os = retrace::warning(call);
71 os << "GL_INVALID_ENUM";
73 case GL_INVALID_VALUE:
74 os << "GL_INVALID_VALUE";
76 case GL_INVALID_OPERATION:
77 os << "GL_INVALID_OPERATION";
79 case GL_STACK_OVERFLOW:
80 os << "GL_STACK_OVERFLOW";
82 case GL_STACK_UNDERFLOW:
83 os << "GL_STACK_UNDERFLOW";
85 case GL_OUT_OF_MEMORY:
86 os << "GL_OUT_OF_MEMORY";
88 case GL_INVALID_FRAMEBUFFER_OPERATION:
89 os << "GL_INVALID_FRAMEBUFFER_OPERATION";
91 case GL_TABLE_TOO_LARGE:
92 os << "GL_TABLE_TOO_LARGE";
102 * Grow the current drawble.
104 * We need to infer the drawable size from GL calls because the drawable sizes
105 * are specified by OS specific calls which we do not trace.
108 updateDrawable(int width, int height) {
113 if (drawable->visible &&
114 width <= drawable->width &&
115 height <= drawable->height) {
119 // Check for bound framebuffer last, as this may have a performance impact.
120 GLint draw_framebuffer = 0;
121 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
122 if (draw_framebuffer != 0) {
126 drawable->resize(width, height);
129 glScissor(0, 0, width, height);
133 void snapshot(unsigned call_no) {
135 (!snapshot_prefix && !compare_prefix)) {
139 image::Image *ref = NULL;
141 if (compare_prefix) {
142 char filename[PATH_MAX];
143 snprintf(filename, sizeof filename, "%s%010u.png", compare_prefix, call_no);
144 ref = image::readPNG(filename);
148 if (retrace::verbosity >= 0) {
149 std::cout << "Read " << filename << "\n";
153 image::Image *src = glstate::getDrawBufferImage(GL_RGBA);
158 if (snapshot_prefix) {
159 if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
161 snprintf(comment, sizeof comment, "%u", call_no);
162 src->writePNM(std::cout, comment);
164 char filename[PATH_MAX];
165 snprintf(filename, sizeof filename, "%s%010u.png", snapshot_prefix, call_no);
166 if (src->writePNG(filename) && retrace::verbosity >= 0) {
167 std::cout << "Wrote " << filename << "\n";
173 std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
181 void frame_complete(trace::Call &call) {
188 if (!drawable->visible) {
189 retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
192 if (snapshot_frequency == FREQUENCY_FRAME ||
193 snapshot_frequency == FREQUENCY_FRAMEBUFFER) {
199 static void display(void) {
200 retrace::Retracer retracer;
202 retracer.addCallbacks(gl_callbacks);
203 retracer.addCallbacks(glx_callbacks);
204 retracer.addCallbacks(wgl_callbacks);
205 retracer.addCallbacks(cgl_callbacks);
207 startTime = os::getTime();
210 while ((call = parser.parse_call())) {
211 retracer.retrace(*call);
213 if (!insideGlBeginEnd &&
214 drawable && context &&
215 call->no >= dump_state) {
216 glstate::dumpCurrentContext(std::cout);
223 // Reached the end of trace
226 long long endTime = os::getTime();
227 float timeInterval = (endTime - startTime) * 1.0E-6;
229 if (retrace::verbosity >= -1) {
231 "Rendered " << frame << " frames"
232 " in " << timeInterval << " secs,"
233 " average of " << (frame/timeInterval) << " fps\n";
237 while (glws::processEvents()) {}
244 static void usage(void) {
246 "Usage: glretrace [OPTION] TRACE\n"
249 " -b benchmark mode (no error checking or warning messages)\n"
250 " -c PREFIX compare against snapshots\n"
251 " -db use a double buffer visual (default)\n"
252 " -sb use a single buffer visual\n"
253 " -s PREFIX take snapshots; `-` for PNM stdout output\n"
254 " -S FREQUENCY snapshot frequency: frame (default), framebuffer, or draw\n"
255 " -v verbose output\n"
256 " -D CALLNO dump state at specific call no\n"
257 " -w wait on final frame\n";
261 int main(int argc, char **argv)
265 for (i = 1; i < argc; ++i) {
266 const char *arg = argv[i];
272 if (!strcmp(arg, "--")) {
274 } else if (!strcmp(arg, "-b")) {
276 retrace::verbosity = -1;
278 } else if (!strcmp(arg, "-c")) {
279 compare_prefix = argv[++i];
280 if (snapshot_frequency == FREQUENCY_NEVER) {
281 snapshot_frequency = FREQUENCY_FRAME;
283 } else if (!strcmp(arg, "-D")) {
284 dump_state = atoi(argv[++i]);
285 retrace::verbosity = -2;
286 } else if (!strcmp(arg, "-db")) {
287 double_buffer = true;
288 } else if (!strcmp(arg, "-sb")) {
289 double_buffer = false;
290 } else if (!strcmp(arg, "--help")) {
293 } else if (!strcmp(arg, "-s")) {
294 snapshot_prefix = argv[++i];
295 if (snapshot_frequency == FREQUENCY_NEVER) {
296 snapshot_frequency = FREQUENCY_FRAME;
298 if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
299 retrace::verbosity = -2;
301 } else if (!strcmp(arg, "-S")) {
303 if (!strcmp(arg, "frame")) {
304 snapshot_frequency = FREQUENCY_FRAME;
305 } else if (!strcmp(arg, "framebuffer")) {
306 snapshot_frequency = FREQUENCY_FRAMEBUFFER;
307 } else if (!strcmp(arg, "draw")) {
308 snapshot_frequency = FREQUENCY_DRAW;
310 std::cerr << "error: unknown frequency " << arg << "\n";
314 if (snapshot_prefix == NULL) {
315 snapshot_prefix = "";
317 } else if (!strcmp(arg, "-v")) {
318 ++retrace::verbosity;
319 } else if (!strcmp(arg, "-w")) {
322 std::cerr << "error: unknown option " << arg << "\n";
329 visual = glws::createVisual(double_buffer);
331 for ( ; i < argc; ++i) {
332 if (!parser.open(argv[i])) {
333 std::cerr << "error: failed to open " << argv[i] << "\n";
348 } /* namespace glretrace */