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 = false;
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;
54 unsigned dump_state = ~0;
57 checkGlError(Trace::Call &call) {
58 if (benchmark || insideGlBeginEnd) {
62 GLenum error = glGetError();
63 if (error == GL_NO_ERROR) {
67 if (retrace::verbosity == 0) {
72 std::cerr << call.no << ": ";
73 std::cerr << "warning: glGetError(";
74 std::cerr << call.name();
79 std::cerr << "GL_INVALID_ENUM";
81 case GL_INVALID_VALUE:
82 std::cerr << "GL_INVALID_VALUE";
84 case GL_INVALID_OPERATION:
85 std::cerr << "GL_INVALID_OPERATION";
87 case GL_STACK_OVERFLOW:
88 std::cerr << "GL_STACK_OVERFLOW";
90 case GL_STACK_UNDERFLOW:
91 std::cerr << "GL_STACK_UNDERFLOW";
93 case GL_OUT_OF_MEMORY:
94 std::cerr << "GL_OUT_OF_MEMORY";
96 case GL_INVALID_FRAMEBUFFER_OPERATION:
97 std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
99 case GL_TABLE_TOO_LARGE:
100 std::cerr << "GL_TABLE_TOO_LARGE";
110 static void color_snapshot(Image::Image &image) {
111 GLint drawbuffer = double_buffer ? GL_BACK : GL_FRONT;
112 GLint readbuffer = double_buffer ? GL_BACK : GL_FRONT;
113 glGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);
114 glGetIntegerv(GL_READ_BUFFER, &readbuffer);
115 glReadBuffer(drawbuffer);
117 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
118 glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
119 glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
120 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
121 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
122 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
123 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
124 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
125 glPixelStorei(GL_PACK_ALIGNMENT, 1);
127 glReadPixels(0, 0, image.width, image.height, GL_RGBA, GL_UNSIGNED_BYTE, image.pixels);
131 glReadBuffer(readbuffer);
135 void snapshot(unsigned call_no) {
137 (!snapshot_prefix && !compare_prefix)) {
141 Image::Image *ref = NULL;
143 if (compare_prefix) {
144 char filename[PATH_MAX];
145 snprintf(filename, sizeof filename, "%s%010u.png", compare_prefix, call_no);
146 ref = Image::readPNG(filename);
150 if (retrace::verbosity >= 0) {
151 std::cout << "Read " << filename << "\n";
155 Image::Image src(drawable->width, drawable->height, true);
158 if (snapshot_prefix) {
159 char filename[PATH_MAX];
160 snprintf(filename, sizeof filename, "%s%010u.png", snapshot_prefix, call_no);
161 if (src.writePNG(filename) && retrace::verbosity >= 0) {
162 std::cout << "Wrote " << filename << "\n";
167 std::cout << "Snapshot " << call_no << " average precision of " << src.compare(*ref) << " bits\n";
173 void frame_complete(unsigned call_no) {
180 static void display(void) {
183 while ((call = parser.parse_call())) {
184 const std::string &name = call->name();
186 if (retrace::verbosity >= 1) {
191 if (name[0] == 'C' && name[1] == 'G' && name[2] == 'L') {
192 glretrace::retrace_call_cgl(*call);
194 else if (name[0] == 'w' && name[1] == 'g' && name[2] == 'l') {
195 glretrace::retrace_call_wgl(*call);
197 else if (name[0] == 'g' && name[1] == 'l' && name[2] == 'X') {
198 glretrace::retrace_call_glx(*call);
200 retrace::retrace_call(*call);
203 if (!insideGlBeginEnd &&
204 drawable && context &&
205 call->no >= dump_state) {
206 glstate::state_dump(std::cout);
213 // Reached the end of trace
216 long long endTime = OS::GetTime();
217 float timeInterval = (endTime - startTime) * 1.0E-6;
219 if (retrace::verbosity >= -1) {
221 "Rendered " << frame << " frames"
222 " in " << timeInterval << " secs,"
223 " average of " << (frame/timeInterval) << " fps\n";
227 while (ws->processEvents()) {}
234 static void usage(void) {
236 "Usage: glretrace [OPTION] TRACE\n"
239 " -b benchmark (no glgeterror; no messages)\n"
240 " -c PREFIX compare against snapshots\n"
241 " -db use a double buffer visual\n"
242 " -s PREFIX take snapshots\n"
243 " -v verbose output\n"
244 " -D CALLNO dump state at specific call no\n"
245 " -w wait on final frame\n";
249 int main(int argc, char **argv)
253 for (i = 1; i < argc; ++i) {
254 const char *arg = argv[i];
260 if (!strcmp(arg, "--")) {
262 } else if (!strcmp(arg, "-b")) {
264 retrace::verbosity = -1;
265 } else if (!strcmp(arg, "-c")) {
266 compare_prefix = argv[++i];
267 } else if (!strcmp(arg, "-D")) {
268 dump_state = atoi(argv[++i]);
269 retrace::verbosity = -2;
270 } else if (!strcmp(arg, "-db")) {
271 double_buffer = true;
272 } else if (!strcmp(arg, "--help")) {
275 } else if (!strcmp(arg, "-s")) {
276 snapshot_prefix = argv[++i];
277 } else if (!strcmp(arg, "-v")) {
278 ++retrace::verbosity;
279 } else if (!strcmp(arg, "-w")) {
282 std::cerr << "error: unknown option " << arg << "\n";
288 ws = glws::createNativeWindowSystem();
289 visual = ws->createVisual(double_buffer);
291 for ( ; i < argc; ++i) {
292 if (parser.open(argv[i])) {
293 startTime = OS::GetTime();
302 } /* namespace glretrace */