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 **************************************************************************/
30 #include "retrace.hpp"
32 #include "glstate.hpp"
33 #include "glretrace.hpp"
38 bool double_buffer = true;
39 bool insideGlBeginEnd = false;
41 glws::WindowSystem *ws = NULL;
42 glws::Visual *visual = NULL;
43 glws::Drawable *drawable = NULL;
44 glws::Context *context = NULL;
47 long long startTime = 0;
50 bool benchmark = false;
51 const char *compare_prefix = NULL;
52 const char *snapshot_prefix = NULL;
53 enum frequency snapshot_frequency = FREQUENCY_NEVER;
55 unsigned dump_state = ~0;
58 checkGlError(Trace::Call &call) {
59 GLenum error = glGetError();
60 if (error == GL_NO_ERROR) {
64 if (retrace::verbosity == 0) {
69 std::cerr << call.no << ": ";
70 std::cerr << "warning: glGetError(";
71 std::cerr << call.name();
76 std::cerr << "GL_INVALID_ENUM";
78 case GL_INVALID_VALUE:
79 std::cerr << "GL_INVALID_VALUE";
81 case GL_INVALID_OPERATION:
82 std::cerr << "GL_INVALID_OPERATION";
84 case GL_STACK_OVERFLOW:
85 std::cerr << "GL_STACK_OVERFLOW";
87 case GL_STACK_UNDERFLOW:
88 std::cerr << "GL_STACK_UNDERFLOW";
90 case GL_OUT_OF_MEMORY:
91 std::cerr << "GL_OUT_OF_MEMORY";
93 case GL_INVALID_FRAMEBUFFER_OPERATION:
94 std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
96 case GL_TABLE_TOO_LARGE:
97 std::cerr << "GL_TABLE_TOO_LARGE";
107 void snapshot(unsigned call_no) {
109 (!snapshot_prefix && !compare_prefix)) {
113 Image::Image *ref = NULL;
115 if (compare_prefix) {
116 char filename[PATH_MAX];
117 snprintf(filename, sizeof filename, "%s%010u.png", compare_prefix, call_no);
118 ref = Image::readPNG(filename);
122 if (retrace::verbosity >= 0) {
123 std::cout << "Read " << filename << "\n";
127 Image::Image *src = glstate::getDrawBufferImage(GL_RGBA);
132 if (snapshot_prefix) {
133 if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
134 src->writePNM(std::cout);
136 char filename[PATH_MAX];
137 snprintf(filename, sizeof filename, "%s%010u.png", snapshot_prefix, call_no);
138 if (src->writePNG(filename) && retrace::verbosity >= 0) {
139 std::cout << "Wrote " << filename << "\n";
145 std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
153 void frame_complete(unsigned call_no) {
156 if (snapshot_frequency == FREQUENCY_FRAME ||
157 snapshot_frequency == FREQUENCY_FRAMEBUFFER) {
163 static void display(void) {
164 startTime = OS::GetTime();
167 while ((call = parser.parse_call())) {
168 const char *name = call->name();
170 if (retrace::verbosity >= 1) {
175 if (name[0] == 'C' && name[1] == 'G' && name[2] == 'L') {
176 glretrace::retrace_call_cgl(*call);
178 else if (name[0] == 'w' && name[1] == 'g' && name[2] == 'l') {
179 glretrace::retrace_call_wgl(*call);
181 else if (name[0] == 'g' && name[1] == 'l' && name[2] == 'X') {
182 glretrace::retrace_call_glx(*call);
184 retrace::retrace_call(*call);
187 if (!insideGlBeginEnd &&
188 drawable && context &&
189 call->no >= dump_state) {
190 glstate::dumpCurrentContext(std::cout);
197 // Reached the end of trace
200 long long endTime = OS::GetTime();
201 float timeInterval = (endTime - startTime) * 1.0E-6;
203 if (retrace::verbosity >= -1) {
205 "Rendered " << frame << " frames"
206 " in " << timeInterval << " secs,"
207 " average of " << (frame/timeInterval) << " fps\n";
211 while (ws->processEvents()) {}
218 static void usage(void) {
220 "Usage: glretrace [OPTION] TRACE\n"
223 " -b benchmark mode (no error checking or warning messages)\n"
224 " -c PREFIX compare against snapshots\n"
225 " -db use a double buffer visual (default)\n"
226 " -sb use a single buffer visual\n"
227 " -s PREFIX take snapshots; `-` for PNM stdout output\n"
228 " -S FREQUENCY snapshot frequency: frame (default), framebuffer, or draw\n"
229 " -v verbose output\n"
230 " -D CALLNO dump state at specific call no\n"
231 " -w wait on final frame\n";
235 int main(int argc, char **argv)
239 for (i = 1; i < argc; ++i) {
240 const char *arg = argv[i];
246 if (!strcmp(arg, "--")) {
248 } else if (!strcmp(arg, "-b")) {
250 retrace::verbosity = -1;
251 } else if (!strcmp(arg, "-c")) {
252 compare_prefix = argv[++i];
253 if (snapshot_frequency == FREQUENCY_NEVER) {
254 snapshot_frequency = FREQUENCY_FRAME;
256 } else if (!strcmp(arg, "-D")) {
257 dump_state = atoi(argv[++i]);
258 retrace::verbosity = -2;
259 } else if (!strcmp(arg, "-db")) {
260 double_buffer = true;
261 } else if (!strcmp(arg, "-sb")) {
262 double_buffer = false;
263 } else if (!strcmp(arg, "--help")) {
266 } else if (!strcmp(arg, "-s")) {
267 snapshot_prefix = argv[++i];
268 if (snapshot_frequency == FREQUENCY_NEVER) {
269 snapshot_frequency = FREQUENCY_FRAME;
271 if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
272 retrace::verbosity = -2;
274 } else if (!strcmp(arg, "-S")) {
276 if (!strcmp(arg, "frame")) {
277 snapshot_frequency = FREQUENCY_FRAME;
278 } else if (!strcmp(arg, "framebuffer")) {
279 snapshot_frequency = FREQUENCY_FRAMEBUFFER;
280 } else if (!strcmp(arg, "draw")) {
281 snapshot_frequency = FREQUENCY_DRAW;
283 std::cerr << "error: unknown frequency " << arg << "\n";
287 if (snapshot_prefix == NULL) {
288 snapshot_prefix = "";
290 } else if (!strcmp(arg, "-v")) {
291 ++retrace::verbosity;
292 } else if (!strcmp(arg, "-w")) {
295 std::cerr << "error: unknown option " << arg << "\n";
301 ws = glws::createNativeWindowSystem();
302 visual = ws->createVisual(double_buffer);
304 for ( ; i < argc; ++i) {
305 if (!parser.open(argv[i])) {
306 std::cerr << "error: failed to open " << argv[i] << "\n";
318 } /* namespace glretrace */