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 "glretrace.hpp"
37 bool double_buffer = false;
38 bool insideGlBeginEnd = false;
40 glws::WindowSystem *ws = NULL;
41 glws::Visual *visual = NULL;
42 glws::Drawable *drawable = NULL;
43 glws::Context *context = NULL;
45 int window_width = 256, window_height = 256;
48 long long startTime = 0;
51 bool benchmark = false;
52 const char *compare_prefix = NULL;
53 const char *snapshot_prefix = NULL;
55 unsigned dump_state = ~0;
58 checkGlError(const Trace::Call &call) {
59 if (benchmark || insideGlBeginEnd) {
63 GLenum error = glGetError();
64 if (error == GL_NO_ERROR) {
68 std::cerr << call.no << ": ";
69 std::cerr << "warning: glGetError(";
70 std::cerr << call.name();
75 std::cerr << "GL_INVALID_ENUM";
77 case GL_INVALID_VALUE:
78 std::cerr << "GL_INVALID_VALUE";
80 case GL_INVALID_OPERATION:
81 std::cerr << "GL_INVALID_OPERATION";
83 case GL_STACK_OVERFLOW:
84 std::cerr << "GL_STACK_OVERFLOW";
86 case GL_STACK_UNDERFLOW:
87 std::cerr << "GL_STACK_UNDERFLOW";
89 case GL_OUT_OF_MEMORY:
90 std::cerr << "GL_OUT_OF_MEMORY";
92 case GL_INVALID_FRAMEBUFFER_OPERATION:
93 std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
95 case GL_TABLE_TOO_LARGE:
96 std::cerr << "GL_TABLE_TOO_LARGE";
106 static void snapshot(Image::Image &image) {
107 GLint drawbuffer = double_buffer ? GL_BACK : GL_FRONT;
108 GLint readbuffer = double_buffer ? GL_BACK : GL_FRONT;
109 glGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);
110 glGetIntegerv(GL_READ_BUFFER, &readbuffer);
111 glReadBuffer(drawbuffer);
112 glReadPixels(0, 0, image.width, image.height, GL_RGBA, GL_UNSIGNED_BYTE, image.pixels);
113 glReadBuffer(readbuffer);
117 void frame_complete(unsigned call_no) {
120 if (snapshot_prefix || compare_prefix) {
121 Image::Image *ref = NULL;
122 if (compare_prefix) {
123 char filename[PATH_MAX];
124 snprintf(filename, sizeof filename, "%s%010u.png", compare_prefix, call_no);
125 ref = Image::readPNG(filename);
129 if (retrace::verbosity >= 0)
130 std::cout << "Read " << filename << "\n";
133 Image::Image src(window_width, window_height, true);
136 if (snapshot_prefix) {
137 char filename[PATH_MAX];
138 snprintf(filename, sizeof filename, "%s%010u.png", snapshot_prefix, call_no);
139 if (src.writePNG(filename) && retrace::verbosity >= 0) {
140 std::cout << "Wrote " << filename << "\n";
145 std::cout << "Snapshot " << call_no << " average precision of " << src.compare(*ref) << " bits\n";
153 static void display(void) {
156 while ((call = parser.parse_call())) {
157 const std::string &name = call->name();
159 if (retrace::verbosity >= 1) {
164 if (name[0] == 'w' && name[1] == 'g' && name[2] == 'l') {
165 glretrace::retrace_call_wgl(*call);
167 else if (name[0] == 'g' && name[1] == 'l' && name[2] == 'X') {
168 glretrace::retrace_call_glx(*call);
170 retrace::retrace_call(*call);
173 if (!insideGlBeginEnd && call->no >= dump_state) {
174 state_dump(std::cout);
181 // Reached the end of trace
184 long long endTime = OS::GetTime();
185 float timeInterval = (endTime - startTime) * 1.0E-6;
187 if (retrace::verbosity >= -1) {
189 "Rendered " << frame << " frames"
190 " in " << timeInterval << " secs,"
191 " average of " << (frame/timeInterval) << " fps\n";
195 while (ws->processEvents()) {}
202 static void usage(void) {
204 "Usage: glretrace [OPTION] TRACE\n"
207 " -b benchmark (no glgeterror; no messages)\n"
208 " -c PREFIX compare against snapshots\n"
209 " -db use a double buffer visual\n"
210 " -s PREFIX take snapshots\n"
211 " -v verbose output\n"
212 " -D CALLNO dump state at specific call no\n"
213 " -w wait on final frame\n";
217 int main(int argc, char **argv)
221 for (i = 1; i < argc; ++i) {
222 const char *arg = argv[i];
228 if (!strcmp(arg, "--")) {
230 } else if (!strcmp(arg, "-b")) {
232 retrace::verbosity = -1;
233 } else if (!strcmp(arg, "-c")) {
234 compare_prefix = argv[++i];
235 } else if (!strcmp(arg, "-D")) {
236 dump_state = atoi(argv[++i]);
237 retrace::verbosity = -2;
238 } else if (!strcmp(arg, "-db")) {
239 double_buffer = true;
240 } else if (!strcmp(arg, "--help")) {
243 } else if (!strcmp(arg, "-s")) {
244 snapshot_prefix = argv[++i];
245 } else if (!strcmp(arg, "-v")) {
246 ++retrace::verbosity;
247 } else if (!strcmp(arg, "-w")) {
250 std::cerr << "error: unknown option " << arg << "\n";
256 ws = glws::createNativeWindowSystem();
257 visual = ws->createVisual(double_buffer);
258 drawable = ws->createDrawable(visual);
259 drawable->resize(window_width, window_height);
260 context = ws->createContext(visual);
261 ws->makeCurrent(drawable, context);
263 for ( ; i < argc; ++i) {
264 if (parser.open(argv[i])) {
265 startTime = OS::GetTime();
274 } /* namespace glretrace */