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;
46 long long startTime = 0;
49 bool benchmark = false;
50 const char *compare_prefix = NULL;
51 const char *snapshot_prefix = NULL;
53 unsigned dump_state = ~0;
56 checkGlError(Trace::Call &call) {
57 if (benchmark || insideGlBeginEnd) {
61 GLenum error = glGetError();
62 if (error == GL_NO_ERROR) {
66 if (retrace::verbosity == 0) {
71 std::cerr << call.no << ": ";
72 std::cerr << "warning: glGetError(";
73 std::cerr << call.name();
78 std::cerr << "GL_INVALID_ENUM";
80 case GL_INVALID_VALUE:
81 std::cerr << "GL_INVALID_VALUE";
83 case GL_INVALID_OPERATION:
84 std::cerr << "GL_INVALID_OPERATION";
86 case GL_STACK_OVERFLOW:
87 std::cerr << "GL_STACK_OVERFLOW";
89 case GL_STACK_UNDERFLOW:
90 std::cerr << "GL_STACK_UNDERFLOW";
92 case GL_OUT_OF_MEMORY:
93 std::cerr << "GL_OUT_OF_MEMORY";
95 case GL_INVALID_FRAMEBUFFER_OPERATION:
96 std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
98 case GL_TABLE_TOO_LARGE:
99 std::cerr << "GL_TABLE_TOO_LARGE";
109 static void snapshot(Image::Image &image) {
110 GLint drawbuffer = double_buffer ? GL_BACK : GL_FRONT;
111 GLint readbuffer = double_buffer ? GL_BACK : GL_FRONT;
112 glGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);
113 glGetIntegerv(GL_READ_BUFFER, &readbuffer);
114 glReadBuffer(drawbuffer);
115 glReadPixels(0, 0, image.width, image.height, GL_RGBA, GL_UNSIGNED_BYTE, image.pixels);
116 glReadBuffer(readbuffer);
120 void frame_complete(unsigned call_no) {
124 (snapshot_prefix || compare_prefix)) {
125 Image::Image *ref = NULL;
126 if (compare_prefix) {
127 char filename[PATH_MAX];
128 snprintf(filename, sizeof filename, "%s%010u.png", compare_prefix, call_no);
129 ref = Image::readPNG(filename);
133 if (retrace::verbosity >= 0)
134 std::cout << "Read " << filename << "\n";
137 Image::Image src(drawable->width, drawable->height, true);
140 if (snapshot_prefix) {
141 char filename[PATH_MAX];
142 snprintf(filename, sizeof filename, "%s%010u.png", snapshot_prefix, call_no);
143 if (src.writePNG(filename) && retrace::verbosity >= 0) {
144 std::cout << "Wrote " << filename << "\n";
149 std::cout << "Snapshot " << call_no << " average precision of " << src.compare(*ref) << " bits\n";
157 static void display(void) {
160 while ((call = parser.parse_call())) {
161 const std::string &name = call->name();
163 if (retrace::verbosity >= 1) {
168 if (name[0] == 'w' && name[1] == 'g' && name[2] == 'l') {
169 glretrace::retrace_call_wgl(*call);
171 else if (name[0] == 'g' && name[1] == 'l' && name[2] == 'X') {
172 glretrace::retrace_call_glx(*call);
174 retrace::retrace_call(*call);
177 if (!insideGlBeginEnd &&
178 drawable && context &&
179 call->no >= dump_state) {
180 state_dump(std::cout);
187 // Reached the end of trace
190 long long endTime = OS::GetTime();
191 float timeInterval = (endTime - startTime) * 1.0E-6;
193 if (retrace::verbosity >= -1) {
195 "Rendered " << frame << " frames"
196 " in " << timeInterval << " secs,"
197 " average of " << (frame/timeInterval) << " fps\n";
201 while (ws->processEvents()) {}
208 static void usage(void) {
210 "Usage: glretrace [OPTION] TRACE\n"
213 " -b benchmark (no glgeterror; no messages)\n"
214 " -c PREFIX compare against snapshots\n"
215 " -db use a double buffer visual\n"
216 " -s PREFIX take snapshots\n"
217 " -v verbose output\n"
218 " -D CALLNO dump state at specific call no\n"
219 " -w wait on final frame\n";
223 int main(int argc, char **argv)
227 for (i = 1; i < argc; ++i) {
228 const char *arg = argv[i];
234 if (!strcmp(arg, "--")) {
236 } else if (!strcmp(arg, "-b")) {
238 retrace::verbosity = -1;
239 } else if (!strcmp(arg, "-c")) {
240 compare_prefix = argv[++i];
241 } else if (!strcmp(arg, "-D")) {
242 dump_state = atoi(argv[++i]);
243 retrace::verbosity = -2;
244 } else if (!strcmp(arg, "-db")) {
245 double_buffer = true;
246 } else if (!strcmp(arg, "--help")) {
249 } else if (!strcmp(arg, "-s")) {
250 snapshot_prefix = argv[++i];
251 } else if (!strcmp(arg, "-v")) {
252 ++retrace::verbosity;
253 } else if (!strcmp(arg, "-w")) {
256 std::cerr << "error: unknown option " << arg << "\n";
262 ws = glws::createNativeWindowSystem();
263 visual = ws->createVisual(double_buffer);
265 for ( ; i < argc; ++i) {
266 if (parser.open(argv[i])) {
267 startTime = OS::GetTime();
276 } /* namespace glretrace */