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_string.hpp"
30 #include "os_time.hpp"
32 #include "retrace.hpp"
34 #include "glstate.hpp"
35 #include "glretrace.hpp"
40 bool double_buffer = true;
41 bool insideGlBeginEnd = false;
43 glws::Profile defaultProfile = glws::PROFILE_COMPAT;
44 glws::Visual *visual[glws::PROFILE_MAX];
45 glws::Drawable *drawable = NULL;
46 glws::Context *context = NULL;
49 long long startTime = 0;
52 bool benchmark = false;
53 const char *compare_prefix = NULL;
54 const char *snapshot_prefix = NULL;
55 enum frequency snapshot_frequency = FREQUENCY_NEVER;
57 unsigned dump_state = ~0;
60 checkGlError(trace::Call &call) {
61 GLenum error = glGetError();
62 if (error == GL_NO_ERROR) {
66 std::ostream & os = retrace::warning(call);
74 os << "GL_INVALID_ENUM";
76 case GL_INVALID_VALUE:
77 os << "GL_INVALID_VALUE";
79 case GL_INVALID_OPERATION:
80 os << "GL_INVALID_OPERATION";
82 case GL_STACK_OVERFLOW:
83 os << "GL_STACK_OVERFLOW";
85 case GL_STACK_UNDERFLOW:
86 os << "GL_STACK_UNDERFLOW";
88 case GL_OUT_OF_MEMORY:
89 os << "GL_OUT_OF_MEMORY";
91 case GL_INVALID_FRAMEBUFFER_OPERATION:
92 os << "GL_INVALID_FRAMEBUFFER_OPERATION";
94 case GL_TABLE_TOO_LARGE:
95 os << "GL_TABLE_TOO_LARGE";
105 * Grow the current drawble.
107 * We need to infer the drawable size from GL calls because the drawable sizes
108 * are specified by OS specific calls which we do not trace.
111 updateDrawable(int width, int height) {
116 if (drawable->visible &&
117 width <= drawable->width &&
118 height <= drawable->height) {
122 // Ignore zero area viewports
123 if (width == 0 || height == 0) {
127 // Check for bound framebuffer last, as this may have a performance impact.
128 GLint draw_framebuffer = 0;
129 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
130 if (draw_framebuffer != 0) {
134 drawable->resize(width, height);
137 glScissor(0, 0, width, height);
141 void snapshot(unsigned call_no) {
143 (!snapshot_prefix && !compare_prefix)) {
147 image::Image *ref = NULL;
149 if (compare_prefix) {
150 os::String filename = os::String::format("%s%010u.png", compare_prefix, call_no);
151 ref = image::readPNG(filename);
155 if (retrace::verbosity >= 0) {
156 std::cout << "Read " << filename << "\n";
160 image::Image *src = glstate::getDrawBufferImage();
165 if (snapshot_prefix) {
166 if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
168 snprintf(comment, sizeof comment, "%u", call_no);
169 src->writePNM(std::cout, comment);
171 os::String filename = os::String::format("%s%010u.png", snapshot_prefix, call_no);
172 if (src->writePNG(filename) && retrace::verbosity >= 0) {
173 std::cout << "Wrote " << filename << "\n";
179 std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
187 void frame_complete(trace::Call &call) {
194 if (!drawable->visible) {
195 retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
198 if (snapshot_frequency == FREQUENCY_FRAME ||
199 snapshot_frequency == FREQUENCY_FRAMEBUFFER) {
205 static void display(void) {
206 retrace::Retracer retracer;
208 retracer.addCallbacks(gl_callbacks);
209 retracer.addCallbacks(glx_callbacks);
210 retracer.addCallbacks(wgl_callbacks);
211 retracer.addCallbacks(cgl_callbacks);
212 retracer.addCallbacks(egl_callbacks);
214 startTime = os::getTime();
217 while ((call = parser.parse_call())) {
218 retracer.retrace(*call);
220 if (!insideGlBeginEnd &&
221 drawable && context &&
222 call->no >= dump_state) {
223 glstate::dumpCurrentContext(std::cout);
230 // Reached the end of trace
233 long long endTime = os::getTime();
234 float timeInterval = (endTime - startTime) * (1.0 / os::timeFrequency);
236 if (retrace::verbosity >= -1) {
238 "Rendered " << frame << " frames"
239 " in " << timeInterval << " secs,"
240 " average of " << (frame/timeInterval) << " fps\n";
244 while (glws::processEvents()) {}
251 static void usage(void) {
253 "Usage: glretrace [OPTION] TRACE\n"
256 " -b benchmark mode (no error checking or warning messages)\n"
257 " -c PREFIX compare against snapshots\n"
258 " -core use core profile\n"
259 " -db use a double buffer visual (default)\n"
260 " -sb use a single buffer visual\n"
261 " -s PREFIX take snapshots; `-` for PNM stdout output\n"
262 " -S FREQUENCY snapshot frequency: frame (default), framebuffer, or draw\n"
263 " -v increase output verbosity\n"
264 " -D CALLNO dump state at specific call no\n"
265 " -w wait on final frame\n";
269 int main(int argc, char **argv)
273 for (i = 1; i < argc; ++i) {
274 const char *arg = argv[i];
280 if (!strcmp(arg, "--")) {
282 } else if (!strcmp(arg, "-b")) {
284 retrace::verbosity = -1;
286 } else if (!strcmp(arg, "-c")) {
287 compare_prefix = argv[++i];
288 if (snapshot_frequency == FREQUENCY_NEVER) {
289 snapshot_frequency = FREQUENCY_FRAME;
291 } else if (!strcmp(arg, "-D")) {
292 dump_state = atoi(argv[++i]);
293 retrace::verbosity = -2;
294 } else if (!strcmp(arg, "-core")) {
295 defaultProfile = glws::PROFILE_CORE;
296 } else if (!strcmp(arg, "-db")) {
297 double_buffer = true;
298 } else if (!strcmp(arg, "-sb")) {
299 double_buffer = false;
300 } else if (!strcmp(arg, "--help")) {
303 } else if (!strcmp(arg, "-s")) {
304 snapshot_prefix = argv[++i];
305 if (snapshot_frequency == FREQUENCY_NEVER) {
306 snapshot_frequency = FREQUENCY_FRAME;
308 if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
309 retrace::verbosity = -2;
311 } else if (!strcmp(arg, "-S")) {
313 if (!strcmp(arg, "frame")) {
314 snapshot_frequency = FREQUENCY_FRAME;
315 } else if (!strcmp(arg, "framebuffer")) {
316 snapshot_frequency = FREQUENCY_FRAMEBUFFER;
317 } else if (!strcmp(arg, "draw")) {
318 snapshot_frequency = FREQUENCY_DRAW;
320 std::cerr << "error: unknown frequency " << arg << "\n";
324 if (snapshot_prefix == NULL) {
325 snapshot_prefix = "";
327 } else if (!strcmp(arg, "-v")) {
328 ++retrace::verbosity;
329 } else if (!strcmp(arg, "-w")) {
332 std::cerr << "error: unknown option " << arg << "\n";
339 visual[glws::PROFILE_COMPAT] = glws::createVisual(double_buffer, glws::PROFILE_COMPAT);
340 visual[glws::PROFILE_CORE] = glws::createVisual(double_buffer, glws::PROFILE_CORE);
341 visual[glws::PROFILE_ES1] = glws::createVisual(double_buffer, glws::PROFILE_ES1);
342 visual[glws::PROFILE_ES2] = glws::createVisual(double_buffer, glws::PROFILE_ES2);
344 for ( ; i < argc; ++i) {
345 if (!parser.open(argv[i])) {
346 std::cerr << "error: failed to open " << argv[i] << "\n";
355 for (int n = 0; n < glws::PROFILE_MAX; n++) {
364 } /* namespace glretrace */