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 = NULL;
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 // Check for bound framebuffer last, as this may have a performance impact.
122 GLint draw_framebuffer = 0;
123 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
124 if (draw_framebuffer != 0) {
128 drawable->resize(width, height);
131 glScissor(0, 0, width, height);
135 void snapshot(unsigned call_no) {
137 (!snapshot_prefix && !compare_prefix)) {
141 image::Image *ref = NULL;
143 if (compare_prefix) {
144 os::String filename = os::String::format("%s%010u.png", compare_prefix, call_no);
145 ref = image::readPNG(filename);
149 if (retrace::verbosity >= 0) {
150 std::cout << "Read " << filename << "\n";
154 image::Image *src = glstate::getDrawBufferImage(GL_RGBA);
159 if (snapshot_prefix) {
160 if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
162 snprintf(comment, sizeof comment, "%u", call_no);
163 src->writePNM(std::cout, comment);
165 os::String filename = os::String::format("%s%010u.png", snapshot_prefix, call_no);
166 if (src->writePNG(filename) && retrace::verbosity >= 0) {
167 std::cout << "Wrote " << filename << "\n";
173 std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
181 void frame_complete(trace::Call &call) {
188 if (!drawable->visible) {
189 retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
192 if (snapshot_frequency == FREQUENCY_FRAME ||
193 snapshot_frequency == FREQUENCY_FRAMEBUFFER) {
199 static void display(void) {
200 retrace::Retracer retracer;
202 retracer.addCallbacks(gl_callbacks);
203 retracer.addCallbacks(glx_callbacks);
204 retracer.addCallbacks(wgl_callbacks);
205 retracer.addCallbacks(cgl_callbacks);
206 retracer.addCallbacks(egl_callbacks);
208 startTime = os::getTime();
211 while ((call = parser.parse_call())) {
212 retracer.retrace(*call);
214 if (!insideGlBeginEnd &&
215 drawable && context &&
216 call->no >= dump_state) {
217 glstate::dumpCurrentContext(std::cout);
224 // Reached the end of trace
227 long long endTime = os::getTime();
228 float timeInterval = (endTime - startTime) * 1.0E-6;
230 if (retrace::verbosity >= -1) {
232 "Rendered " << frame << " frames"
233 " in " << timeInterval << " secs,"
234 " average of " << (frame/timeInterval) << " fps\n";
238 while (glws::processEvents()) {}
245 static void usage(void) {
247 "Usage: glretrace [OPTION] TRACE\n"
250 " -b benchmark mode (no error checking or warning messages)\n"
251 " -c PREFIX compare against snapshots\n"
252 " -core use core profile\n"
253 " -db use a double buffer visual (default)\n"
254 " -sb use a single buffer visual\n"
255 " -s PREFIX take snapshots; `-` for PNM stdout output\n"
256 " -S FREQUENCY snapshot frequency: frame (default), framebuffer, or draw\n"
257 " -v verbose output\n"
258 " -D CALLNO dump state at specific call no\n"
259 " -w wait on final frame\n";
263 int main(int argc, char **argv)
267 for (i = 1; i < argc; ++i) {
268 const char *arg = argv[i];
274 if (!strcmp(arg, "--")) {
276 } else if (!strcmp(arg, "-b")) {
278 retrace::verbosity = -1;
280 } else if (!strcmp(arg, "-c")) {
281 compare_prefix = argv[++i];
282 if (snapshot_frequency == FREQUENCY_NEVER) {
283 snapshot_frequency = FREQUENCY_FRAME;
285 } else if (!strcmp(arg, "-D")) {
286 dump_state = atoi(argv[++i]);
287 retrace::verbosity = -2;
288 } else if (!strcmp(arg, "-core")) {
289 defaultProfile = glws::PROFILE_CORE;
290 } else if (!strcmp(arg, "-db")) {
291 double_buffer = true;
292 } else if (!strcmp(arg, "-sb")) {
293 double_buffer = false;
294 } else if (!strcmp(arg, "--help")) {
297 } else if (!strcmp(arg, "-s")) {
298 snapshot_prefix = argv[++i];
299 if (snapshot_frequency == FREQUENCY_NEVER) {
300 snapshot_frequency = FREQUENCY_FRAME;
302 if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
303 retrace::verbosity = -2;
305 } else if (!strcmp(arg, "-S")) {
307 if (!strcmp(arg, "frame")) {
308 snapshot_frequency = FREQUENCY_FRAME;
309 } else if (!strcmp(arg, "framebuffer")) {
310 snapshot_frequency = FREQUENCY_FRAMEBUFFER;
311 } else if (!strcmp(arg, "draw")) {
312 snapshot_frequency = FREQUENCY_DRAW;
314 std::cerr << "error: unknown frequency " << arg << "\n";
318 if (snapshot_prefix == NULL) {
319 snapshot_prefix = "";
321 } else if (!strcmp(arg, "-v")) {
322 ++retrace::verbosity;
323 } else if (!strcmp(arg, "-w")) {
326 std::cerr << "error: unknown option " << arg << "\n";
333 visual = glws::createVisual(double_buffer);
335 for ( ; i < argc; ++i) {
336 if (!parser.open(argv[i])) {
337 std::cerr << "error: failed to open " << argv[i] << "\n";
352 } /* namespace glretrace */