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"
31 #include "retrace.hpp"
33 #include "glstate.hpp"
34 #include "glretrace.hpp"
39 bool double_buffer = true;
40 bool insideGlBeginEnd = false;
42 glws::Profile defaultProfile = glws::PROFILE_COMPAT;
43 glws::Visual *visual[glws::PROFILE_MAX];
44 glws::Drawable *drawable = NULL;
45 glws::Context *context = NULL;
48 long long startTime = 0;
51 bool benchmark = false;
52 const char *compare_prefix = NULL;
53 const char *snapshot_prefix = NULL;
54 enum frequency snapshot_frequency = FREQUENCY_NEVER;
56 unsigned dump_state = ~0;
59 checkGlError(trace::Call &call) {
60 GLenum error = glGetError();
61 if (error == GL_NO_ERROR) {
65 std::ostream & os = retrace::warning(call);
73 os << "GL_INVALID_ENUM";
75 case GL_INVALID_VALUE:
76 os << "GL_INVALID_VALUE";
78 case GL_INVALID_OPERATION:
79 os << "GL_INVALID_OPERATION";
81 case GL_STACK_OVERFLOW:
82 os << "GL_STACK_OVERFLOW";
84 case GL_STACK_UNDERFLOW:
85 os << "GL_STACK_UNDERFLOW";
87 case GL_OUT_OF_MEMORY:
88 os << "GL_OUT_OF_MEMORY";
90 case GL_INVALID_FRAMEBUFFER_OPERATION:
91 os << "GL_INVALID_FRAMEBUFFER_OPERATION";
93 case GL_TABLE_TOO_LARGE:
94 os << "GL_TABLE_TOO_LARGE";
104 * Grow the current drawble.
106 * We need to infer the drawable size from GL calls because the drawable sizes
107 * are specified by OS specific calls which we do not trace.
110 updateDrawable(int width, int height) {
115 if (drawable->visible &&
116 width <= drawable->width &&
117 height <= drawable->height) {
121 // Ignore zero area viewports
122 if (width == 0 || height == 0) {
126 // Check for bound framebuffer last, as this may have a performance impact.
127 GLint draw_framebuffer = 0;
128 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
129 if (draw_framebuffer != 0) {
133 drawable->resize(width, height);
136 glScissor(0, 0, width, height);
140 void snapshot(unsigned call_no) {
142 (!snapshot_prefix && !compare_prefix)) {
146 image::Image *ref = NULL;
148 if (compare_prefix) {
149 os::String filename = os::String::format("%s%010u.png", compare_prefix, call_no);
150 ref = image::readPNG(filename);
154 if (retrace::verbosity >= 0) {
155 std::cout << "Read " << filename << "\n";
159 image::Image *src = glstate::getDrawBufferImage(GL_RGBA);
164 if (snapshot_prefix) {
165 if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
167 snprintf(comment, sizeof comment, "%u", call_no);
168 src->writePNM(std::cout, comment);
170 os::String filename = os::String::format("%s%010u.png", snapshot_prefix, call_no);
171 if (src->writePNG(filename) && retrace::verbosity >= 0) {
172 std::cout << "Wrote " << filename << "\n";
178 std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
186 void frame_complete(trace::Call &call) {
193 if (!drawable->visible) {
194 retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
197 if (snapshot_frequency == FREQUENCY_FRAME ||
198 snapshot_frequency == FREQUENCY_FRAMEBUFFER) {
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 retracer.retrace(*call);
219 if (!insideGlBeginEnd &&
220 drawable && context &&
221 call->no >= dump_state) {
222 glstate::dumpCurrentContext(std::cout);
229 // Reached the end of trace
232 long long endTime = os::getTime();
233 float timeInterval = (endTime - startTime) * 1.0E-6;
235 if (retrace::verbosity >= -1) {
237 "Rendered " << frame << " frames"
238 " in " << timeInterval << " secs,"
239 " average of " << (frame/timeInterval) << " fps\n";
243 while (glws::processEvents()) {}
250 static void usage(void) {
252 "Usage: glretrace [OPTION] TRACE\n"
255 " -b benchmark mode (no error checking or warning messages)\n"
256 " -c PREFIX compare against snapshots\n"
257 " -core use core profile\n"
258 " -db use a double buffer visual (default)\n"
259 " -sb use a single buffer visual\n"
260 " -s PREFIX take snapshots; `-` for PNM stdout output\n"
261 " -S FREQUENCY snapshot frequency: frame (default), framebuffer, or draw\n"
262 " -v increase output verbosity\n"
263 " -D CALLNO dump state at specific call no\n"
264 " -w wait on final frame\n";
268 int main(int argc, char **argv)
272 for (i = 1; i < argc; ++i) {
273 const char *arg = argv[i];
279 if (!strcmp(arg, "--")) {
281 } else if (!strcmp(arg, "-b")) {
283 retrace::verbosity = -1;
285 } else if (!strcmp(arg, "-c")) {
286 compare_prefix = argv[++i];
287 if (snapshot_frequency == FREQUENCY_NEVER) {
288 snapshot_frequency = FREQUENCY_FRAME;
290 } else if (!strcmp(arg, "-D")) {
291 dump_state = atoi(argv[++i]);
292 retrace::verbosity = -2;
293 } else if (!strcmp(arg, "-core")) {
294 defaultProfile = glws::PROFILE_CORE;
295 } else if (!strcmp(arg, "-db")) {
296 double_buffer = true;
297 } else if (!strcmp(arg, "-sb")) {
298 double_buffer = false;
299 } else if (!strcmp(arg, "--help")) {
302 } else if (!strcmp(arg, "-s")) {
303 snapshot_prefix = argv[++i];
304 if (snapshot_frequency == FREQUENCY_NEVER) {
305 snapshot_frequency = FREQUENCY_FRAME;
307 if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
308 retrace::verbosity = -2;
310 } else if (!strcmp(arg, "-S")) {
312 if (!strcmp(arg, "frame")) {
313 snapshot_frequency = FREQUENCY_FRAME;
314 } else if (!strcmp(arg, "framebuffer")) {
315 snapshot_frequency = FREQUENCY_FRAMEBUFFER;
316 } else if (!strcmp(arg, "draw")) {
317 snapshot_frequency = FREQUENCY_DRAW;
319 std::cerr << "error: unknown frequency " << arg << "\n";
323 if (snapshot_prefix == NULL) {
324 snapshot_prefix = "";
326 } else if (!strcmp(arg, "-v")) {
327 ++retrace::verbosity;
328 } else if (!strcmp(arg, "-w")) {
331 std::cerr << "error: unknown option " << arg << "\n";
338 visual[glws::PROFILE_COMPAT] = glws::createVisual(double_buffer, glws::PROFILE_COMPAT);
339 visual[glws::PROFILE_CORE] = glws::createVisual(double_buffer, glws::PROFILE_CORE);
340 visual[glws::PROFILE_ES1] = glws::createVisual(double_buffer, glws::PROFILE_ES1);
341 visual[glws::PROFILE_ES2] = glws::createVisual(double_buffer, glws::PROFILE_ES2);
343 for ( ; i < argc; ++i) {
344 if (!parser.open(argv[i])) {
345 std::cerr << "error: failed to open " << argv[i] << "\n";
354 for (int n = 0; n < glws::PROFILE_MAX; n++) {
363 } /* namespace glretrace */