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;
59 if (benchmark || insideGlBeginEnd) {
63 GLenum error = glGetError();
64 if (error == GL_NO_ERROR) {
68 std::cerr << "warning: glGetError() = ";
71 std::cerr << "GL_INVALID_ENUM";
73 case GL_INVALID_VALUE:
74 std::cerr << "GL_INVALID_VALUE";
76 case GL_INVALID_OPERATION:
77 std::cerr << "GL_INVALID_OPERATION";
79 case GL_STACK_OVERFLOW:
80 std::cerr << "GL_STACK_OVERFLOW";
82 case GL_STACK_UNDERFLOW:
83 std::cerr << "GL_STACK_UNDERFLOW";
85 case GL_OUT_OF_MEMORY:
86 std::cerr << "GL_OUT_OF_MEMORY";
88 case GL_INVALID_FRAMEBUFFER_OPERATION:
89 std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
91 case GL_TABLE_TOO_LARGE:
92 std::cerr << "GL_TABLE_TOO_LARGE";
102 static void snapshot(Image::Image &image) {
103 GLint drawbuffer = double_buffer ? GL_BACK : GL_FRONT;
104 GLint readbuffer = double_buffer ? GL_BACK : GL_FRONT;
105 glGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);
106 glGetIntegerv(GL_READ_BUFFER, &readbuffer);
107 glReadBuffer(drawbuffer);
108 glReadPixels(0, 0, image.width, image.height, GL_RGBA, GL_UNSIGNED_BYTE, image.pixels);
110 glReadBuffer(readbuffer);
114 static void frame_complete(void) {
117 if (snapshot_prefix || compare_prefix) {
118 Image::Image *ref = NULL;
119 if (compare_prefix) {
120 char filename[PATH_MAX];
121 snprintf(filename, sizeof filename, "%s%04u.png", compare_prefix, frame);
122 ref = Image::readPNG(filename);
126 if (retrace::verbosity >= 0)
127 std::cout << "Read " << filename << "\n";
130 Image::Image src(window_width, window_height, true);
133 if (snapshot_prefix) {
134 char filename[PATH_MAX];
135 snprintf(filename, sizeof filename, "%s%04u.png", snapshot_prefix, frame);
136 if (src.writePNG(filename) && retrace::verbosity >= 0) {
137 std::cout << "Wrote " << filename << "\n";
142 std::cout << "Frame " << frame << " average precision of " << src.compare(*ref) << " bits\n";
151 static void display(void) {
154 while ((call = parser.parse_call())) {
155 const std::string &name = call->name();
157 if ((name[0] == 'w' && name[1] == 'g' && name[2] == 'l') ||
158 (name[0] == 'g' && name[1] == 'l' && name[2] == 'X')) {
159 // XXX: We ignore the majority of the OS-specific calls for now
160 if (name == "glXSwapBuffers" ||
161 name == "wglSwapBuffers") {
162 if (retrace::verbosity >= 1) {
168 drawable->swapBuffers();
171 } else if (name == "glXMakeCurrent" ||
172 name == "wglMakeCurrent") {
174 if (!double_buffer) {
182 if (name == "glFlush") {
184 if (!double_buffer) {
189 retrace::retrace_call(*call);
191 if (!insideGlBeginEnd && call->no >= dump_state) {
192 state_dump(std::cout);
199 // Reached the end of trace
202 long long endTime = OS::GetTime();
203 float timeInterval = (endTime - startTime) * 1.0E-6;
205 if (retrace::verbosity >= -1) {
207 "Rendered " << frame << " frames"
208 " in " << timeInterval << " secs,"
209 " average of " << (frame/timeInterval) << " fps\n";
213 while (ws->processEvents()) {}
220 static void usage(void) {
222 "Usage: glretrace [OPTION] TRACE\n"
225 " -b benchmark (no glgeterror; no messages)\n"
226 " -c PREFIX compare against snapshots\n"
227 " -db use a double buffer visual\n"
228 " -s PREFIX take snapshots\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 } else if (!strcmp(arg, "-D")) {
254 dump_state = atoi(argv[++i]);
255 retrace::verbosity = -2;
256 } else if (!strcmp(arg, "-db")) {
257 double_buffer = true;
258 } else if (!strcmp(arg, "--help")) {
261 } else if (!strcmp(arg, "-s")) {
262 snapshot_prefix = argv[++i];
263 } else if (!strcmp(arg, "-v")) {
264 ++retrace::verbosity;
265 } else if (!strcmp(arg, "-w")) {
268 std::cerr << "error: unknown option " << arg << "\n";
274 ws = glws::createNativeWindowSystem();
275 visual = ws->createVisual(double_buffer);
276 drawable = ws->createDrawable(visual);
277 drawable->resize(window_width, window_height);
278 context = ws->createContext(visual);
279 ws->makeCurrent(drawable, context);
281 for ( ; i < argc; ++i) {
282 if (parser.open(argv[i])) {
283 startTime = OS::GetTime();
292 } /* namespace glretrace */