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 **************************************************************************/
29 #include "os_path.hpp"
31 #include "retrace.hpp"
33 #include "glstate.hpp"
34 #include "glretrace.hpp"
39 bool double_buffer = true;
40 bool insideGlBeginEnd = false;
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 std::ostream & os = retrace::warning(call);
72 os << "GL_INVALID_ENUM";
74 case GL_INVALID_VALUE:
75 os << "GL_INVALID_VALUE";
77 case GL_INVALID_OPERATION:
78 os << "GL_INVALID_OPERATION";
80 case GL_STACK_OVERFLOW:
81 os << "GL_STACK_OVERFLOW";
83 case GL_STACK_UNDERFLOW:
84 os << "GL_STACK_UNDERFLOW";
86 case GL_OUT_OF_MEMORY:
87 os << "GL_OUT_OF_MEMORY";
89 case GL_INVALID_FRAMEBUFFER_OPERATION:
90 os << "GL_INVALID_FRAMEBUFFER_OPERATION";
92 case GL_TABLE_TOO_LARGE:
93 os << "GL_TABLE_TOO_LARGE";
103 * Grow the current drawble.
105 * We need to infer the drawable size from GL calls because the drawable sizes
106 * are specified by OS specific calls which we do not trace.
109 updateDrawable(int width, int height) {
114 if (drawable->visible &&
115 width <= drawable->width &&
116 height <= drawable->height) {
120 // Check for bound framebuffer last, as this may have a performance impact.
121 GLint draw_framebuffer = 0;
122 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
123 if (draw_framebuffer != 0) {
127 drawable->resize(width, height);
130 glScissor(0, 0, width, height);
134 void snapshot(unsigned call_no) {
136 (!snapshot_prefix && !compare_prefix)) {
140 image::Image *ref = NULL;
142 if (compare_prefix) {
143 os::Path filename = os::Path::format("%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 os::Path filename = os::Path::format("%s%010u.png", snapshot_prefix, call_no);
165 if (src->writePNG(filename) && retrace::verbosity >= 0) {
166 std::cout << "Wrote " << filename << "\n";
172 std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
180 void frame_complete(trace::Call &call) {
187 if (!drawable->visible) {
188 retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
191 if (snapshot_frequency == FREQUENCY_FRAME ||
192 snapshot_frequency == FREQUENCY_FRAMEBUFFER) {
198 static void display(void) {
199 retrace::Retracer retracer;
201 retracer.addCallbacks(gl_callbacks);
202 retracer.addCallbacks(glx_callbacks);
203 retracer.addCallbacks(wgl_callbacks);
204 retracer.addCallbacks(cgl_callbacks);
206 startTime = os::getTime();
209 while ((call = parser.parse_call())) {
210 retracer.retrace(*call);
212 if (!insideGlBeginEnd &&
213 drawable && context &&
214 call->no >= dump_state) {
215 glstate::dumpCurrentContext(std::cout);
222 // Reached the end of trace
225 long long endTime = os::getTime();
226 float timeInterval = (endTime - startTime) * 1.0E-6;
228 if (retrace::verbosity >= -1) {
230 "Rendered " << frame << " frames"
231 " in " << timeInterval << " secs,"
232 " average of " << (frame/timeInterval) << " fps\n";
236 while (glws::processEvents()) {}
243 static void usage(void) {
245 "Usage: glretrace [OPTION] TRACE\n"
248 " -b benchmark mode (no error checking or warning messages)\n"
249 " -c PREFIX compare against snapshots\n"
250 " -db use a double buffer visual (default)\n"
251 " -sb use a single buffer visual\n"
252 " -s PREFIX take snapshots; `-` for PNM stdout output\n"
253 " -S FREQUENCY snapshot frequency: frame (default), framebuffer, or draw\n"
254 " -v verbose output\n"
255 " -D CALLNO dump state at specific call no\n"
256 " -w wait on final frame\n";
260 int main(int argc, char **argv)
264 for (i = 1; i < argc; ++i) {
265 const char *arg = argv[i];
271 if (!strcmp(arg, "--")) {
273 } else if (!strcmp(arg, "-b")) {
275 retrace::verbosity = -1;
277 } else if (!strcmp(arg, "-c")) {
278 compare_prefix = argv[++i];
279 if (snapshot_frequency == FREQUENCY_NEVER) {
280 snapshot_frequency = FREQUENCY_FRAME;
282 } else if (!strcmp(arg, "-D")) {
283 dump_state = atoi(argv[++i]);
284 retrace::verbosity = -2;
285 } else if (!strcmp(arg, "-db")) {
286 double_buffer = true;
287 } else if (!strcmp(arg, "-sb")) {
288 double_buffer = false;
289 } else if (!strcmp(arg, "--help")) {
292 } else if (!strcmp(arg, "-s")) {
293 snapshot_prefix = argv[++i];
294 if (snapshot_frequency == FREQUENCY_NEVER) {
295 snapshot_frequency = FREQUENCY_FRAME;
297 if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
298 retrace::verbosity = -2;
300 } else if (!strcmp(arg, "-S")) {
302 if (!strcmp(arg, "frame")) {
303 snapshot_frequency = FREQUENCY_FRAME;
304 } else if (!strcmp(arg, "framebuffer")) {
305 snapshot_frequency = FREQUENCY_FRAMEBUFFER;
306 } else if (!strcmp(arg, "draw")) {
307 snapshot_frequency = FREQUENCY_DRAW;
309 std::cerr << "error: unknown frequency " << arg << "\n";
313 if (snapshot_prefix == NULL) {
314 snapshot_prefix = "";
316 } else if (!strcmp(arg, "-v")) {
317 ++retrace::verbosity;
318 } else if (!strcmp(arg, "-w")) {
321 std::cerr << "error: unknown option " << arg << "\n";
328 visual = glws::createVisual(double_buffer);
330 for ( ; i < argc; ++i) {
331 if (!parser.open(argv[i])) {
332 std::cerr << "error: failed to open " << argv[i] << "\n";
347 } /* namespace glretrace */