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"
33 #include "trace_callset.hpp"
35 #include "glstate.hpp"
36 #include "glretrace.hpp"
41 bool double_buffer = true;
42 bool insideGlBeginEnd = false;
44 glws::Profile defaultProfile = glws::PROFILE_COMPAT;
45 glws::Visual *visual[glws::PROFILE_MAX];
46 glws::Drawable *drawable = NULL;
47 glws::Context *context = NULL;
50 long long startTime = 0;
53 bool benchmark = false;
54 static const char *compare_prefix = NULL;
55 static const char *snapshot_prefix = NULL;
56 static trace::CallSet snapshot_frequency;
57 static trace::CallSet compare_frequency;
59 unsigned dump_state = ~0;
62 checkGlError(trace::Call &call) {
63 GLenum error = glGetError();
64 if (error == GL_NO_ERROR) {
68 std::ostream & os = retrace::warning(call);
76 os << "GL_INVALID_ENUM";
78 case GL_INVALID_VALUE:
79 os << "GL_INVALID_VALUE";
81 case GL_INVALID_OPERATION:
82 os << "GL_INVALID_OPERATION";
84 case GL_STACK_OVERFLOW:
85 os << "GL_STACK_OVERFLOW";
87 case GL_STACK_UNDERFLOW:
88 os << "GL_STACK_UNDERFLOW";
90 case GL_OUT_OF_MEMORY:
91 os << "GL_OUT_OF_MEMORY";
93 case GL_INVALID_FRAMEBUFFER_OPERATION:
94 os << "GL_INVALID_FRAMEBUFFER_OPERATION";
96 case GL_TABLE_TOO_LARGE:
97 os << "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 (drawable->visible &&
119 width <= drawable->width &&
120 height <= drawable->height) {
124 // Ignore zero area viewports
125 if (width == 0 || height == 0) {
129 // Check for bound framebuffer last, as this may have a performance impact.
130 GLint draw_framebuffer = 0;
131 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
132 if (draw_framebuffer != 0) {
136 drawable->resize(width, height);
139 glScissor(0, 0, width, height);
144 snapshot(unsigned call_no) {
145 assert(snapshot_prefix || compare_prefix);
151 image::Image *ref = NULL;
153 if (compare_prefix) {
154 os::String filename = os::String::format("%s%010u.png", compare_prefix, call_no);
155 ref = image::readPNG(filename);
159 if (retrace::verbosity >= 0) {
160 std::cout << "Read " << filename << "\n";
164 image::Image *src = glstate::getDrawBufferImage();
169 if (snapshot_prefix) {
170 if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
172 snprintf(comment, sizeof comment, "%u", call_no);
173 src->writePNM(std::cout, comment);
175 os::String filename = os::String::format("%s%010u.png", snapshot_prefix, call_no);
176 if (src->writePNG(filename) && retrace::verbosity >= 0) {
177 std::cout << "Wrote " << filename << "\n";
183 std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
191 void frame_complete(trace::Call &call) {
198 if (!drawable->visible) {
199 retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
204 static void display(void) {
205 retrace::Retracer retracer;
207 retracer.addCallbacks(gl_callbacks);
208 retracer.addCallbacks(glx_callbacks);
209 retracer.addCallbacks(wgl_callbacks);
210 retracer.addCallbacks(cgl_callbacks);
211 retracer.addCallbacks(egl_callbacks);
213 startTime = os::getTime();
216 while ((call = parser.parse_call())) {
217 bool swapRenderTarget = call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET;
219 snapshot_frequency.contains(*call) ||
220 compare_frequency.contains(*call)
223 // For calls which cause rendertargets to be swaped, we take the
224 // snapshot _before_ swapping the rendertargets.
225 if (doSnapshot && swapRenderTarget) {
226 if (call->flags & trace::CALL_FLAG_END_FRAME) {
227 // For swapbuffers/presents we still use this call number,
228 // spite not have been executed yet.
231 // Whereas for ordinate fbo/rendertarget changes we use the
232 // previous call's number.
233 snapshot(call->no - 1);
237 retracer.retrace(*call);
239 if (doSnapshot && !swapRenderTarget) {
243 if (!insideGlBeginEnd &&
244 drawable && context &&
245 call->no >= dump_state) {
246 glstate::dumpCurrentContext(std::cout);
253 // Reached the end of trace
256 long long endTime = os::getTime();
257 float timeInterval = (endTime - startTime) * (1.0 / os::timeFrequency);
259 if (retrace::verbosity >= -1) {
261 "Rendered " << frame << " frames"
262 " in " << timeInterval << " secs,"
263 " average of " << (frame/timeInterval) << " fps\n";
267 while (glws::processEvents()) {}
274 static void usage(void) {
276 "Usage: glretrace [OPTION] TRACE\n"
279 " -b benchmark mode (no error checking or warning messages)\n"
280 " -c PREFIX compare against snapshots\n"
281 " -C CALLSET calls to compare (default is every frame)\n"
282 " -core use core profile\n"
283 " -db use a double buffer visual (default)\n"
284 " -sb use a single buffer visual\n"
285 " -s PREFIX take snapshots; `-` for PNM stdout output\n"
286 " -S CALLSET calls to snapshot (default is every frame)\n"
287 " -v increase output verbosity\n"
288 " -D CALLNO dump state at specific call no\n"
289 " -w wait on final frame\n";
293 int main(int argc, char **argv)
295 assert(compare_frequency.empty());
296 assert(snapshot_frequency.empty());
299 for (i = 1; i < argc; ++i) {
300 const char *arg = argv[i];
306 if (!strcmp(arg, "--")) {
308 } else if (!strcmp(arg, "-b")) {
310 retrace::verbosity = -1;
312 } else if (!strcmp(arg, "-c")) {
313 compare_prefix = argv[++i];
314 if (compare_frequency.empty()) {
315 compare_frequency = trace::CallSet(trace::FREQUENCY_FRAME);
317 } else if (!strcmp(arg, "-C")) {
318 compare_frequency = trace::CallSet(argv[++i]);
319 if (compare_prefix == NULL) {
322 } else if (!strcmp(arg, "-D")) {
323 dump_state = atoi(argv[++i]);
324 retrace::verbosity = -2;
325 } else if (!strcmp(arg, "-core")) {
326 defaultProfile = glws::PROFILE_CORE;
327 } else if (!strcmp(arg, "-db")) {
328 double_buffer = true;
329 } else if (!strcmp(arg, "-sb")) {
330 double_buffer = false;
331 } else if (!strcmp(arg, "--help")) {
334 } else if (!strcmp(arg, "-s")) {
335 snapshot_prefix = argv[++i];
336 if (snapshot_frequency.empty()) {
337 snapshot_frequency = trace::CallSet(trace::FREQUENCY_FRAME);
339 if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
340 retrace::verbosity = -2;
342 } else if (!strcmp(arg, "-S")) {
343 snapshot_frequency = trace::CallSet(argv[++i]);
344 if (snapshot_prefix == NULL) {
345 snapshot_prefix = "";
347 } else if (!strcmp(arg, "-v")) {
348 ++retrace::verbosity;
349 } else if (!strcmp(arg, "-w")) {
352 std::cerr << "error: unknown option " << arg << "\n";
359 visual[glws::PROFILE_COMPAT] = glws::createVisual(double_buffer, glws::PROFILE_COMPAT);
360 visual[glws::PROFILE_CORE] = glws::createVisual(double_buffer, glws::PROFILE_CORE);
361 visual[glws::PROFILE_ES1] = glws::createVisual(double_buffer, glws::PROFILE_ES1);
362 visual[glws::PROFILE_ES2] = glws::createVisual(double_buffer, glws::PROFILE_ES2);
364 for ( ; i < argc; ++i) {
365 if (!parser.open(argv[i])) {
366 std::cerr << "error: failed to open " << argv[i] << "\n";
375 for (int n = 0; n < glws::PROFILE_MAX; n++) {
384 } /* namespace glretrace */