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_binary.hpp"
30 #include "os_string.hpp"
31 #include "os_time.hpp"
33 #include "retrace.hpp"
34 #include "trace_callset.hpp"
36 #include "glstate.hpp"
37 #include "glretrace.hpp"
42 bool double_buffer = true;
43 bool insideGlBeginEnd = false;
45 glws::Profile defaultProfile = glws::PROFILE_COMPAT;
46 glws::Visual *visual[glws::PROFILE_MAX];
47 glws::Drawable *drawable = NULL;
48 glws::Context *context = NULL;
51 long long startTime = 0;
54 bool benchmark = false;
55 static const char *compare_prefix = NULL;
56 static const char *snapshot_prefix = NULL;
57 static trace::CallSet snapshot_frequency;
58 static trace::CallSet compare_frequency;
60 unsigned dump_state = ~0;
63 checkGlError(trace::Call &call) {
64 GLenum error = glGetError();
65 if (error == GL_NO_ERROR) {
69 std::ostream & os = retrace::warning(call);
77 os << "GL_INVALID_ENUM";
79 case GL_INVALID_VALUE:
80 os << "GL_INVALID_VALUE";
82 case GL_INVALID_OPERATION:
83 os << "GL_INVALID_OPERATION";
85 case GL_STACK_OVERFLOW:
86 os << "GL_STACK_OVERFLOW";
88 case GL_STACK_UNDERFLOW:
89 os << "GL_STACK_UNDERFLOW";
91 case GL_OUT_OF_MEMORY:
92 os << "GL_OUT_OF_MEMORY";
94 case GL_INVALID_FRAMEBUFFER_OPERATION:
95 os << "GL_INVALID_FRAMEBUFFER_OPERATION";
97 case GL_TABLE_TOO_LARGE:
98 os << "GL_TABLE_TOO_LARGE";
108 * Grow the current drawble.
110 * We need to infer the drawable size from GL calls because the drawable sizes
111 * are specified by OS specific calls which we do not trace.
114 updateDrawable(int width, int height) {
119 if (drawable->visible &&
120 width <= drawable->width &&
121 height <= drawable->height) {
125 // Ignore zero area viewports
126 if (width == 0 || height == 0) {
130 // Check for bound framebuffer last, as this may have a performance impact.
131 GLint draw_framebuffer = 0;
132 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
133 if (draw_framebuffer != 0) {
137 drawable->resize(width, height);
140 glScissor(0, 0, width, height);
145 snapshot(unsigned call_no) {
146 assert(snapshot_prefix || compare_prefix);
152 image::Image *ref = NULL;
154 if (compare_prefix) {
155 os::String filename = os::String::format("%s%010u.png", compare_prefix, call_no);
156 ref = image::readPNG(filename);
160 if (retrace::verbosity >= 0) {
161 std::cout << "Read " << filename << "\n";
165 image::Image *src = glstate::getDrawBufferImage();
170 if (snapshot_prefix) {
171 if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
173 snprintf(comment, sizeof comment, "%u", call_no);
174 src->writePNM(std::cout, comment);
176 os::String filename = os::String::format("%s%010u.png", snapshot_prefix, call_no);
177 if (src->writePNG(filename) && retrace::verbosity >= 0) {
178 std::cout << "Wrote " << filename << "\n";
184 std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
192 void frame_complete(trace::Call &call) {
199 if (!drawable->visible) {
200 retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
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 bool swapRenderTarget = call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET;
220 snapshot_frequency.contains(*call) ||
221 compare_frequency.contains(*call)
224 // For calls which cause rendertargets to be swaped, we take the
225 // snapshot _before_ swapping the rendertargets.
226 if (doSnapshot && swapRenderTarget) {
227 if (call->flags & trace::CALL_FLAG_END_FRAME) {
228 // For swapbuffers/presents we still use this call number,
229 // spite not have been executed yet.
232 // Whereas for ordinate fbo/rendertarget changes we use the
233 // previous call's number.
234 snapshot(call->no - 1);
238 retracer.retrace(*call);
240 if (doSnapshot && !swapRenderTarget) {
244 if (!insideGlBeginEnd &&
245 drawable && context &&
246 call->no >= dump_state) {
247 glstate::dumpCurrentContext(std::cout);
254 // Reached the end of trace
257 long long endTime = os::getTime();
258 float timeInterval = (endTime - startTime) * (1.0 / os::timeFrequency);
260 if ((retrace::verbosity >= -1) || (retrace::profiling)) {
262 "Rendered " << frame << " frames"
263 " in " << timeInterval << " secs,"
264 " average of " << (frame/timeInterval) << " fps\n";
268 while (glws::processEvents()) {}
275 static void usage(void) {
277 "Usage: glretrace [OPTION] TRACE\n"
280 " -b benchmark mode (no error checking or warning messages)\n"
281 " -p profiling mode (run whole trace, dump profiling info)\n"
282 " -c PREFIX compare against snapshots\n"
283 " -C CALLSET calls to compare (default is every frame)\n"
284 " -core use core profile\n"
285 " -db use a double buffer visual (default)\n"
286 " -sb use a single buffer visual\n"
287 " -s PREFIX take snapshots; `-` for PNM stdout output\n"
288 " -S CALLSET calls to snapshot (default is every frame)\n"
289 " -v increase output verbosity\n"
290 " -D CALLNO dump state at specific call no\n"
291 " -w wait on final frame\n";
295 int main(int argc, char **argv)
297 assert(compare_frequency.empty());
298 assert(snapshot_frequency.empty());
301 for (i = 1; i < argc; ++i) {
302 const char *arg = argv[i];
308 if (!strcmp(arg, "--")) {
310 } else if (!strcmp(arg, "-b")) {
312 retrace::verbosity = -1;
314 } else if (!strcmp(arg, "-p")) {
315 retrace::profiling = true;
316 retrace::verbosity = -1;
318 } else if (!strcmp(arg, "-c")) {
319 compare_prefix = argv[++i];
320 if (compare_frequency.empty()) {
321 compare_frequency = trace::CallSet(trace::FREQUENCY_FRAME);
323 } else if (!strcmp(arg, "-C")) {
324 compare_frequency = trace::CallSet(argv[++i]);
325 if (compare_prefix == NULL) {
328 } else if (!strcmp(arg, "-D")) {
329 dump_state = atoi(argv[++i]);
330 retrace::verbosity = -2;
331 } else if (!strcmp(arg, "-core")) {
332 defaultProfile = glws::PROFILE_CORE;
333 } else if (!strcmp(arg, "-db")) {
334 double_buffer = true;
335 } else if (!strcmp(arg, "-sb")) {
336 double_buffer = false;
337 } else if (!strcmp(arg, "--help")) {
340 } else if (!strcmp(arg, "-s")) {
341 snapshot_prefix = argv[++i];
342 if (snapshot_frequency.empty()) {
343 snapshot_frequency = trace::CallSet(trace::FREQUENCY_FRAME);
345 if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
346 os::setBinaryMode(stdout);
347 retrace::verbosity = -2;
349 } else if (!strcmp(arg, "-S")) {
350 snapshot_frequency = trace::CallSet(argv[++i]);
351 if (snapshot_prefix == NULL) {
352 snapshot_prefix = "";
354 } else if (!strcmp(arg, "-v")) {
355 ++retrace::verbosity;
356 } else if (!strcmp(arg, "-w")) {
359 std::cerr << "error: unknown option " << arg << "\n";
366 visual[glws::PROFILE_COMPAT] = glws::createVisual(double_buffer, glws::PROFILE_COMPAT);
367 visual[glws::PROFILE_CORE] = glws::createVisual(double_buffer, glws::PROFILE_CORE);
368 visual[glws::PROFILE_ES1] = glws::createVisual(double_buffer, glws::PROFILE_ES1);
369 visual[glws::PROFILE_ES2] = glws::createVisual(double_buffer, glws::PROFILE_ES2);
371 for ( ; i < argc; ++i) {
372 if (!parser.open(argv[i])) {
373 std::cerr << "error: failed to open " << argv[i] << "\n";
382 for (int n = 0; n < glws::PROFILE_MAX; n++) {
391 } /* namespace glretrace */