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 dumpingState = false;
59 bool doubleBuffer = true;
60 bool coreProfile = false;
62 bool profiling = false;
63 bool profilingGpuTimes = false;
64 bool profilingCpuTimes = false;
65 bool profilingPixelsDrawn = false;
71 frameComplete(trace::Call &call) {
77 takeSnapshot(unsigned call_no) {
78 assert(snapshotPrefix || comparePrefix);
80 image::Image *ref = NULL;
83 os::String filename = os::String::format("%s%010u.png", comparePrefix, call_no);
84 ref = image::readPNG(filename);
88 if (retrace::verbosity >= 0) {
89 std::cout << "Read " << filename << "\n";
93 image::Image *src = getSnapshot();
99 if (snapshotPrefix[0] == '-' && snapshotPrefix[1] == 0) {
101 snprintf(comment, sizeof comment, "%u", call_no);
102 src->writePNM(std::cout, comment);
104 os::String filename = os::String::format("%s%010u.png", snapshotPrefix, call_no);
105 if (src->writePNG(filename) && retrace::verbosity >= 0) {
106 std::cout << "Wrote " << filename << "\n";
112 std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
124 retrace::Retracer retracer;
126 addCallbacks(retracer);
128 long long startTime = 0;
131 startTime = os::getTime();
134 while ((call = retrace::parser.parse_call())) {
135 bool swapRenderTarget = call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET;
137 snapshotFrequency.contains(*call) ||
138 compareFrequency.contains(*call)
141 // For calls which cause rendertargets to be swaped, we take the
142 // snapshot _before_ swapping the rendertargets.
143 if (doSnapshot && swapRenderTarget) {
144 if (call->flags & trace::CALL_FLAG_END_FRAME) {
145 // For swapbuffers/presents we still use this call number,
146 // spite not have been executed yet.
147 takeSnapshot(call->no);
149 // Whereas for ordinate fbo/rendertarget changes we use the
150 // previous call's number.
151 takeSnapshot(call->no - 1);
155 retracer.retrace(*call);
157 if (doSnapshot && !swapRenderTarget) {
158 takeSnapshot(call->no);
161 if (call->no >= dumpStateCallNo &&
162 dumpState(std::cout)) {
169 // Reached the end of trace
172 long long endTime = os::getTime();
173 float timeInterval = (endTime - startTime) * (1.0 / os::timeFrequency);
175 if ((retrace::verbosity >= -1) || (retrace::profiling)) {
177 "Rendered " << frameNo << " frames"
178 " in " << timeInterval << " secs,"
179 " average of " << (frameNo/timeInterval) << " fps\n";
190 } /* namespace retrace */
194 usage(const char *argv0) {
196 "Usage: " << argv0 << " [OPTION] TRACE [...]\n"
199 " -b benchmark mode (no error checking or warning messages)\n"
200 " -pcpu cpu profiling (cpu times per call)\n"
201 " -pgpu gpu profiling (gpu times per draw call)\n"
202 " -ppd pixels drawn profiling (pixels drawn per draw call)\n"
203 " -c PREFIX compare against snapshots\n"
204 " -C CALLSET calls to compare (default is every frame)\n"
205 " -core use core profile\n"
206 " -db use a double buffer visual (default)\n"
207 " -sb use a single buffer visual\n"
208 " -s PREFIX take snapshots; `-` for PNM stdout output\n"
209 " -S CALLSET calls to snapshot (default is every frame)\n"
210 " -v increase output verbosity\n"
211 " -D CALLNO dump state at specific call no\n"
212 " -w waitOnFinish on final frame\n";
217 int main(int argc, char **argv)
219 using namespace retrace;
221 assert(compareFrequency.empty());
222 assert(snapshotFrequency.empty());
225 for (i = 1; i < argc; ++i) {
226 const char *arg = argv[i];
232 if (!strcmp(arg, "--")) {
234 } else if (!strcmp(arg, "-b")) {
235 retrace::debug = false;
236 retrace::verbosity = -1;
237 } else if (!strcmp(arg, "-c")) {
238 comparePrefix = argv[++i];
239 if (compareFrequency.empty()) {
240 compareFrequency = trace::CallSet(trace::FREQUENCY_FRAME);
242 } else if (!strcmp(arg, "-C")) {
243 compareFrequency = trace::CallSet(argv[++i]);
244 if (comparePrefix == NULL) {
247 } else if (!strcmp(arg, "-D")) {
248 dumpStateCallNo = atoi(argv[++i]);
250 retrace::verbosity = -2;
251 } else if (!strcmp(arg, "-core")) {
252 retrace::coreProfile = true;
253 } else if (!strcmp(arg, "-db")) {
254 retrace::doubleBuffer = true;
255 } else if (!strcmp(arg, "-sb")) {
256 retrace::doubleBuffer = false;
257 } else if (!strcmp(arg, "--help")) {
260 } else if (!strcmp(arg, "-s")) {
261 snapshotPrefix = argv[++i];
262 if (snapshotFrequency.empty()) {
263 snapshotFrequency = trace::CallSet(trace::FREQUENCY_FRAME);
265 if (snapshotPrefix[0] == '-' && snapshotPrefix[1] == 0) {
266 os::setBinaryMode(stdout);
267 retrace::verbosity = -2;
269 } else if (!strcmp(arg, "-S")) {
270 snapshotFrequency = trace::CallSet(argv[++i]);
271 if (snapshotPrefix == NULL) {
274 } else if (!strcmp(arg, "-v")) {
275 ++retrace::verbosity;
276 } else if (!strcmp(arg, "-w")) {
278 } else if (arg[1] == 'p') {
279 retrace::debug = false;
280 retrace::profiling = true;
281 retrace::verbosity = -1;
283 if (!strcmp(arg, "-pcpu")) {
284 retrace::profilingCpuTimes = true;
285 } else if (!strcmp(arg, "-pgpu")) {
286 retrace::profilingGpuTimes = true;
287 } else if (!strcmp(arg, "-ppd")) {
288 retrace::profilingPixelsDrawn = true;
291 std::cerr << "error: unknown option " << arg << "\n";
298 if (retrace::profiling) {
299 retrace::profiler.setup(retrace::profilingCpuTimes, retrace::profilingGpuTimes, retrace::profilingPixelsDrawn);
302 for ( ; i < argc; ++i) {
303 if (!retrace::parser.open(argv[i])) {
304 std::cerr << "error: failed to open " << argv[i] << "\n";
310 retrace::parser.close();
313 // XXX: X often hangs on XCloseDisplay
314 //retrace::cleanUp();