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;
56 bool profiling = false;
59 bool doubleBuffer = true;
60 bool coreProfile = false;
63 static unsigned frameNo = 0;
67 frameComplete(trace::Call &call) {
73 takeSnapshot(unsigned call_no) {
74 assert(snapshotPrefix || comparePrefix);
76 image::Image *ref = NULL;
79 os::String filename = os::String::format("%s%010u.png", comparePrefix, call_no);
80 ref = image::readPNG(filename);
84 if (retrace::verbosity >= 0) {
85 std::cout << "Read " << filename << "\n";
89 image::Image *src = getSnapshot();
95 if (snapshotPrefix[0] == '-' && snapshotPrefix[1] == 0) {
97 snprintf(comment, sizeof comment, "%u", call_no);
98 src->writePNM(std::cout, comment);
100 os::String filename = os::String::format("%s%010u.png", snapshotPrefix, call_no);
101 if (src->writePNG(filename) && retrace::verbosity >= 0) {
102 std::cout << "Wrote " << filename << "\n";
108 std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
120 retrace::Retracer retracer;
122 addCallbacks(retracer);
124 long long startTime = 0;
126 startTime = os::getTime();
129 while ((call = retrace::parser.parse_call())) {
130 bool swapRenderTarget = call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET;
132 snapshotFrequency.contains(*call) ||
133 compareFrequency.contains(*call)
136 // For calls which cause rendertargets to be swaped, we take the
137 // snapshot _before_ swapping the rendertargets.
138 if (doSnapshot && swapRenderTarget) {
139 if (call->flags & trace::CALL_FLAG_END_FRAME) {
140 // For swapbuffers/presents we still use this call number,
141 // spite not have been executed yet.
142 takeSnapshot(call->no);
144 // Whereas for ordinate fbo/rendertarget changes we use the
145 // previous call's number.
146 takeSnapshot(call->no - 1);
150 retracer.retrace(*call);
152 if (doSnapshot && !swapRenderTarget) {
153 takeSnapshot(call->no);
156 if (call->no >= dumpStateCallNo &&
157 dumpState(std::cout)) {
164 // Reached the end of trace
167 long long endTime = os::getTime();
168 float timeInterval = (endTime - startTime) * (1.0 / os::timeFrequency);
170 if ((retrace::verbosity >= -1) || (retrace::profiling)) {
172 "Rendered " << frameNo << " frames"
173 " in " << timeInterval << " secs,"
174 " average of " << (frameNo/timeInterval) << " fps\n";
185 } /* namespace retrace */
189 usage(const char *argv0) {
191 "Usage: " << argv0 << " [OPTION] TRACE\n"
194 " -b benchmark mode (no error checking or warning messages)\n"
195 " -p profiling mode (run whole trace, dump profiling info)\n"
196 " -c PREFIX compare against snapshots\n"
197 " -C CALLSET calls to compare (default is every frame)\n"
198 " -core use core profile\n"
199 " -db use a double buffer visual (default)\n"
200 " -sb use a single buffer visual\n"
201 " -s PREFIX take snapshots; `-` for PNM stdout output\n"
202 " -S CALLSET calls to snapshot (default is every frame)\n"
203 " -v increase output verbosity\n"
204 " -D CALLNO dump state at specific call no\n"
205 " -w waitOnFinish on final frame\n";
210 int main(int argc, char **argv)
212 assert(compareFrequency.empty());
213 assert(snapshotFrequency.empty());
216 for (i = 1; i < argc; ++i) {
217 const char *arg = argv[i];
223 if (!strcmp(arg, "--")) {
225 } else if (!strcmp(arg, "-b")) {
226 retrace::debug = false;
227 retrace::verbosity = -1;
228 } else if (!strcmp(arg, "-p")) {
229 retrace::debug = false;
230 retrace::profiling = true;
231 retrace::verbosity = -1;
232 } else if (!strcmp(arg, "-c")) {
233 comparePrefix = argv[++i];
234 if (compareFrequency.empty()) {
235 compareFrequency = trace::CallSet(trace::FREQUENCY_FRAME);
237 } else if (!strcmp(arg, "-C")) {
238 compareFrequency = trace::CallSet(argv[++i]);
239 if (comparePrefix == NULL) {
242 } else if (!strcmp(arg, "-D")) {
243 dumpStateCallNo = atoi(argv[++i]);
244 retrace::verbosity = -2;
245 } else if (!strcmp(arg, "-core")) {
246 retrace::coreProfile = true;
247 } else if (!strcmp(arg, "-db")) {
248 retrace::doubleBuffer = true;
249 } else if (!strcmp(arg, "-sb")) {
250 retrace::doubleBuffer = false;
251 } else if (!strcmp(arg, "--help")) {
254 } else if (!strcmp(arg, "-s")) {
255 snapshotPrefix = argv[++i];
256 if (snapshotFrequency.empty()) {
257 snapshotFrequency = trace::CallSet(trace::FREQUENCY_FRAME);
259 if (snapshotPrefix[0] == '-' && snapshotPrefix[1] == 0) {
260 os::setBinaryMode(stdout);
261 retrace::verbosity = -2;
263 } else if (!strcmp(arg, "-S")) {
264 snapshotFrequency = trace::CallSet(argv[++i]);
265 if (snapshotPrefix == NULL) {
268 } else if (!strcmp(arg, "-v")) {
269 ++retrace::verbosity;
270 } else if (!strcmp(arg, "-w")) {
273 std::cerr << "error: unknown option " << arg << "\n";
281 for ( ; i < argc; ++i) {
282 if (!retrace::parser.open(argv[i])) {
283 std::cerr << "error: failed to open " << argv[i] << "\n";
289 retrace::parser.close();