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 "os_binary.hpp"
31 #include "os_time.hpp"
33 #include "trace_callset.hpp"
34 #include "trace_dump.hpp"
35 #include "retrace.hpp"
38 static bool waitOnFinish = false;
40 static const char *comparePrefix = NULL;
41 static const char *snapshotPrefix = NULL;
42 static trace::CallSet snapshotFrequency;
43 static trace::CallSet compareFrequency;
45 static unsigned dumpStateCallNo = ~0;
52 trace::Profiler profiler;
57 bool profiling = false;
58 bool profileGPU = false;
59 bool dumpingState = false;
62 bool doubleBuffer = true;
63 bool coreProfile = false;
70 frameComplete(trace::Call &call) {
76 takeSnapshot(unsigned call_no) {
77 assert(snapshotPrefix || comparePrefix);
79 image::Image *ref = NULL;
82 os::String filename = os::String::format("%s%010u.png", comparePrefix, call_no);
83 ref = image::readPNG(filename);
87 if (retrace::verbosity >= 0) {
88 std::cout << "Read " << filename << "\n";
92 image::Image *src = getSnapshot();
98 if (snapshotPrefix[0] == '-' && snapshotPrefix[1] == 0) {
100 snprintf(comment, sizeof comment, "%u", call_no);
101 src->writePNM(std::cout, comment);
103 os::String filename = os::String::format("%s%010u.png", snapshotPrefix, call_no);
104 if (src->writePNG(filename) && retrace::verbosity >= 0) {
105 std::cout << "Wrote " << filename << "\n";
111 std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
123 retrace::Retracer retracer;
125 addCallbacks(retracer);
127 long long startTime = 0;
130 startTime = os::getTime();
133 while ((call = retrace::parser.parse_call())) {
134 bool swapRenderTarget = call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET;
136 snapshotFrequency.contains(*call) ||
137 compareFrequency.contains(*call)
140 // For calls which cause rendertargets to be swaped, we take the
141 // snapshot _before_ swapping the rendertargets.
142 if (doSnapshot && swapRenderTarget) {
143 if (call->flags & trace::CALL_FLAG_END_FRAME) {
144 // For swapbuffers/presents we still use this call number,
145 // spite not have been executed yet.
146 takeSnapshot(call->no);
148 // Whereas for ordinate fbo/rendertarget changes we use the
149 // previous call's number.
150 takeSnapshot(call->no - 1);
154 retracer.retrace(*call);
156 if (doSnapshot && !swapRenderTarget) {
157 takeSnapshot(call->no);
160 if (call->no >= dumpStateCallNo &&
161 dumpState(std::cout)) {
168 // Reached the end of trace
171 long long endTime = os::getTime();
172 float timeInterval = (endTime - startTime) * (1.0 / os::timeFrequency);
174 if ((retrace::verbosity >= -1) || (retrace::profiling) || (retrace::profileGPU)) {
176 "Rendered " << frameNo << " frames"
177 " in " << timeInterval << " secs,"
178 " average of " << (frameNo/timeInterval) << " fps\n";
189 } /* namespace retrace */
193 usage(const char *argv0) {
195 "Usage: " << argv0 << " [OPTION] TRACE [...]\n"
198 " -b benchmark mode (no error checking or warning messages)\n"
199 " -p profiling mode (run whole trace, dump profiling info)\n"
200 " -pgpu gpu profiling mode (run whole trace, dump gpu profiling info)\n"
201 " -c PREFIX compare against snapshots\n"
202 " -C CALLSET calls to compare (default is every frame)\n"
203 " -core use core profile\n"
204 " -db use a double buffer visual (default)\n"
205 " -sb use a single buffer visual\n"
206 " -s PREFIX take snapshots; `-` for PNM stdout output\n"
207 " -S CALLSET calls to snapshot (default is every frame)\n"
208 " -v increase output verbosity\n"
209 " -D CALLNO dump state at specific call no\n"
210 " -w waitOnFinish on final frame\n";
215 int main(int argc, char **argv)
217 using namespace retrace;
219 assert(compareFrequency.empty());
220 assert(snapshotFrequency.empty());
223 for (i = 1; i < argc; ++i) {
224 const char *arg = argv[i];
230 if (!strcmp(arg, "--")) {
232 } else if (!strcmp(arg, "-b")) {
233 retrace::debug = false;
234 retrace::verbosity = -1;
235 } else if (!strcmp(arg, "-p")) {
236 retrace::debug = false;
237 retrace::profiling = true;
238 retrace::verbosity = -1;
239 } else if (!strcmp(arg, "-pgpu")) {
240 retrace::profileGPU = true;
242 retrace::debug = false;
243 retrace::profiling = false;
244 retrace::verbosity = -1;
245 } else if (!strcmp(arg, "-c")) {
246 comparePrefix = argv[++i];
247 if (compareFrequency.empty()) {
248 compareFrequency = trace::CallSet(trace::FREQUENCY_FRAME);
250 } else if (!strcmp(arg, "-C")) {
251 compareFrequency = trace::CallSet(argv[++i]);
252 if (comparePrefix == NULL) {
255 } else if (!strcmp(arg, "-D")) {
256 dumpStateCallNo = atoi(argv[++i]);
258 retrace::verbosity = -2;
259 } else if (!strcmp(arg, "-core")) {
260 retrace::coreProfile = true;
261 } else if (!strcmp(arg, "-db")) {
262 retrace::doubleBuffer = true;
263 } else if (!strcmp(arg, "-sb")) {
264 retrace::doubleBuffer = false;
265 } else if (!strcmp(arg, "--help")) {
268 } else if (!strcmp(arg, "-s")) {
269 snapshotPrefix = argv[++i];
270 if (snapshotFrequency.empty()) {
271 snapshotFrequency = trace::CallSet(trace::FREQUENCY_FRAME);
273 if (snapshotPrefix[0] == '-' && snapshotPrefix[1] == 0) {
274 os::setBinaryMode(stdout);
275 retrace::verbosity = -2;
277 } else if (!strcmp(arg, "-S")) {
278 snapshotFrequency = trace::CallSet(argv[++i]);
279 if (snapshotPrefix == NULL) {
282 } else if (!strcmp(arg, "-v")) {
283 ++retrace::verbosity;
284 } else if (!strcmp(arg, "-w")) {
287 std::cerr << "error: unknown option " << arg << "\n";
295 for ( ; i < argc; ++i) {
296 if (!retrace::parser.open(argv[i])) {
297 std::cerr << "error: failed to open " << argv[i] << "\n";
303 retrace::parser.close();
306 // XXX: X often hangs on XCloseDisplay
307 //retrace::cleanUp();