blob: 45934e0b166840b29653d0460177ad8f292cf75b [file] [log] [blame]
Carl Worth4c5f6fa2011-11-14 14:50:07 -08001/**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * Copyright 2011 Intel corporation
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 *
25 **************************************************************************/
26
Carl Worth33b92632012-08-14 14:11:19 -070027#include <sstream>
Carl Worth4c5f6fa2011-11-14 14:50:07 -080028#include <string.h>
José Fonsecab682b672012-02-15 07:13:31 +000029#include <limits.h> // for CHAR_MAX
30#include <getopt.h>
Carl Worth4c5f6fa2011-11-14 14:50:07 -080031
Carl Worthcc6e51c2012-08-13 14:35:43 -070032#include <GL/gl.h>
33#include <GL/glext.h>
34
Carl Worth163b22c2012-08-10 10:15:30 -070035#include <set>
36
Carl Worth4c5f6fa2011-11-14 14:50:07 -080037#include "cli.hpp"
38
39#include "os_string.hpp"
40
José Fonsecad3c00132012-01-27 22:43:53 +000041#include "trace_callset.hpp"
Carl Worth4c5f6fa2011-11-14 14:50:07 -080042#include "trace_parser.hpp"
José Fonseca630471a2012-01-27 22:06:51 +000043#include "trace_writer.hpp"
Carl Worth4c5f6fa2011-11-14 14:50:07 -080044
Carl Worth81bda5a2012-08-17 08:09:19 -070045#define MAX(a, b) ((a) > (b) ? (a) : (b))
Carl Worth33b92632012-08-14 14:11:19 -070046#define STRNCMP_LITERAL(var, literal) strncmp((var), (literal), sizeof (literal) -1)
47
Carl Worth4c5f6fa2011-11-14 14:50:07 -080048static const char *synopsis = "Create a new trace by trimming an existing trace.";
49
50static void
51usage(void)
52{
53 std::cout
José Fonsecab682b672012-02-15 07:13:31 +000054 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
José Fonsecad3c00132012-01-27 22:43:53 +000055 << synopsis << "\n"
56 "\n"
Carl Worth86464432012-08-11 11:46:54 -070057 " -h, --help Show detailed help for trim options and exit\n"
58 " --calls=CALLSET Include specified calls in the trimmed output.\n"
Carl Worth46abfd12012-08-14 22:32:29 -070059 " --frames=FRAMESET Include specified frames in the trimmed output.\n"
Carl Worth86464432012-08-11 11:46:54 -070060 " --deps Include additional calls to satisfy dependencies\n"
61 " --no-deps Do not include calls from dependency analysis\n"
62 " --prune Omit uninteresting calls from the trace output\n"
63 " --no-prune Do not prune uninteresting calls from the trace.\n"
Carl Worth46abfd12012-08-14 22:32:29 -070064 " -x, --exact Trim exactly to calls specified in --calls/--frames\n"
Carl Worth86464432012-08-11 11:46:54 -070065 " Equivalent to both --no-deps and --no-prune\n"
Carl Worthab82b3b2012-08-15 16:35:38 -070066 " --print-callset Print the final set of calls included in output\n"
Carl Worth86464432012-08-11 11:46:54 -070067 " --thread=THREAD_ID Only retain calls from specified thread\n"
68 " -o, --output=TRACE_FILE Output trace file\n"
69 ;
70}
71
72static void
73help()
74{
75 std::cout
76 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
77 << synopsis << "\n"
78 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -070079 " -h, --help Show this help message and exit\n"
Carl Worth16b18db2012-08-11 11:33:12 -070080 "\n"
81 " --calls=CALLSET Include specified calls in the trimmed output.\n"
Carl Worth46abfd12012-08-14 22:32:29 -070082 " --frames=FRAMESET Include specified frames in the trimmed output.\n"
Carl Worth16b18db2012-08-11 11:33:12 -070083 " Note that due to dependency analysis and pruning\n"
84 " of uninteresting calls the resulting trace may\n"
85 " include more and less calls than specified.\n"
86 " See --no-deps, --no-prune, and --exact to change\n"
87 " this behavior.\n"
88 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -070089 " --deps Perform dependency analysis and include dependent\n"
Carl Worth16b18db2012-08-11 11:33:12 -070090 " calls as needed, (even if those calls were not\n"
Carl Worth46abfd12012-08-14 22:32:29 -070091 " explicitly requested with --calls or --frames).\n"
92 " This is the default behavior. See --no-deps and\n"
93 " --exact to change the behavior.\n"
Carl Worth16b18db2012-08-11 11:33:12 -070094 "\n"
95 " --no-deps Do not perform dependency analysis. In this mode\n"
96 " the trimmed trace will never include calls from\n"
Carl Worth46abfd12012-08-14 22:32:29 -070097 " outside what is specified in --calls or --frames.\n"
Carl Worth16b18db2012-08-11 11:33:12 -070098 "\n"
Carl Worth46abfd12012-08-14 22:32:29 -070099 " --prune Omit calls with no side effects, even if the call\n"
100 " is within the range specified by --calls/--frames.\n"
101 " This is the default behavior. See --no-prune.\n"
Carl Worth16b18db2012-08-11 11:33:12 -0700102 "\n"
103 " --no-prune Do not prune uninteresting calls from the trace.\n"
104 " In this mode the trimmed trace will never omit\n"
Carl Worth46abfd12012-08-14 22:32:29 -0700105 " any calls within the user-specified range.\n"
Carl Worth16b18db2012-08-11 11:33:12 -0700106 "\n"
107 " -x, --exact Trim the trace to exactly the calls specified in\n"
Carl Worth46abfd12012-08-14 22:32:29 -0700108 " --calls and --frames. This option is equivalent\n"
109 " to passing both --no-deps and --no-prune.\n"
Carl Worth16b18db2012-08-11 11:33:12 -0700110 "\n"
Carl Worthab82b3b2012-08-15 16:35:38 -0700111 " --print-callset Print to stdout the final set of calls included\n"
112 " in the trim output. This can be useful for\n"
113 " debugging trim operations by using a modified\n"
114 " callset on the command-line along with --exact.\n"
115 " Use --calls=@<file> to read callset from a file.\n"
116 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -0700117 " --thread=THREAD_ID Only retain calls from specified thread\n"
Carl Worth16b18db2012-08-11 11:33:12 -0700118 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -0700119 " -o, --output=TRACE_FILE Output trace file\n"
José Fonsecad3c00132012-01-27 22:43:53 +0000120 "\n"
121 ;
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800122}
123
José Fonsecab682b672012-02-15 07:13:31 +0000124enum {
Imre Deak6f07a842012-05-08 15:20:43 +0300125 CALLS_OPT = CHAR_MAX + 1,
Carl Worth46abfd12012-08-14 22:32:29 -0700126 FRAMES_OPT,
Carl Worth163b22c2012-08-10 10:15:30 -0700127 DEPS_OPT,
128 NO_DEPS_OPT,
Carl Worth16b18db2012-08-11 11:33:12 -0700129 PRUNE_OPT,
130 NO_PRUNE_OPT,
Imre Deak6f07a842012-05-08 15:20:43 +0300131 THREAD_OPT,
Carl Worthab82b3b2012-08-15 16:35:38 -0700132 PRINT_CALLSET_OPT,
José Fonsecab682b672012-02-15 07:13:31 +0000133};
134
135const static char *
Carl Worth16b18db2012-08-11 11:33:12 -0700136shortOptions = "ho:x";
José Fonsecab682b672012-02-15 07:13:31 +0000137
138const static struct option
139longOptions[] = {
140 {"help", no_argument, 0, 'h'},
141 {"calls", required_argument, 0, CALLS_OPT},
Carl Worth46abfd12012-08-14 22:32:29 -0700142 {"frames", required_argument, 0, FRAMES_OPT},
Carl Worth163b22c2012-08-10 10:15:30 -0700143 {"deps", no_argument, 0, DEPS_OPT},
144 {"no-deps", no_argument, 0, NO_DEPS_OPT},
Carl Worth16b18db2012-08-11 11:33:12 -0700145 {"prune", no_argument, 0, PRUNE_OPT},
146 {"no-prune", no_argument, 0, NO_PRUNE_OPT},
147 {"exact", no_argument, 0, 'x'},
Imre Deak6f07a842012-05-08 15:20:43 +0300148 {"thread", required_argument, 0, THREAD_OPT},
149 {"output", required_argument, 0, 'o'},
Carl Worthab82b3b2012-08-15 16:35:38 -0700150 {"print-callset", no_argument, 0, PRINT_CALLSET_OPT},
José Fonsecab682b672012-02-15 07:13:31 +0000151 {0, 0, 0, 0}
152};
153
Carl Worth163b22c2012-08-10 10:15:30 -0700154struct stringCompare {
155 bool operator() (const char *a, const char *b) const {
156 return strcmp(a, b) < 0;
157 }
158};
159
160class TraceAnalyzer {
Carl Worth33b92632012-08-14 14:11:19 -0700161 /* Maps for tracking resource dependencies between calls. */
162 std::map<std::string, std::set<unsigned> > resources;
163 std::map<std::string, std::set<std::string> > dependencies;
164
165 /* Maps for tracking OpenGL state. */
166 std::map<GLenum, unsigned> texture_map;
Carl Worth163b22c2012-08-10 10:15:30 -0700167
168 /* The final set of calls required. This consists of calls added
169 * explicitly with the require() method as well as all calls
170 * implicitly required by those through resource dependencies. */
171 std::set<unsigned> required;
172
Carl Worthcc6e51c2012-08-13 14:35:43 -0700173 bool transformFeedbackActive;
174 bool framebufferObjectActive;
175 bool insideBeginEnd;
Carl Worth5ff38572012-09-04 11:49:13 -0700176 GLuint activeProgram;
Carl Worth65692822012-08-17 08:06:51 -0700177 GLenum activeTextureUnit;
Carl Worth163b22c2012-08-10 10:15:30 -0700178
Carl Worthcc6e51c2012-08-13 14:35:43 -0700179 /* Rendering often has no side effects, but it can in some cases,
180 * (such as when transform feedback is active, or when rendering
181 * targets a framebuffer object). */
182 bool renderingHasSideEffect() {
183 return transformFeedbackActive || framebufferObjectActive;
184 }
185
186 /* Provide: Record that the given call affects the given resource
187 * as a side effect. */
Carl Worth33b92632012-08-14 14:11:19 -0700188 void provide(std::string resource, trace::CallNo call_no) {
Carl Worthcc6e51c2012-08-13 14:35:43 -0700189 resources[resource].insert(call_no);
190 }
191
Carl Worth619a0be2012-08-14 17:13:30 -0700192 /* Like provide, but with a simply-formatted string, (appending an
193 * integer to the given string). */
194 void providef(std::string resource, int resource_no, trace::CallNo call_no) {
195 std::stringstream ss;
196 ss << resource << resource_no;
197 provide(ss.str(), call_no);
198 }
199
Carl Worth33b92632012-08-14 14:11:19 -0700200 /* Link: Establish a dependency between resource 'resource' and
201 * resource 'dependency'. This dependency is captured by name so
202 * that if the list of calls that provide 'dependency' grows
203 * before 'resource' is consumed, those calls will still be
204 * captured. */
205 void link(std::string resource, std::string dependency) {
206 dependencies[resource].insert(dependency);
207 }
208
Carl Worth619a0be2012-08-14 17:13:30 -0700209 /* Like link, but with a simply-formatted string, (appending an
210 * integer to the given string). */
211 void linkf(std::string resource, std::string dependency, int dep_no) {
212
213 std::stringstream ss;
214 ss << dependency << dep_no;
215 link(resource, ss.str());
216 }
217
Carl Worth33b92632012-08-14 14:11:19 -0700218 /* Unlink: Remove dependency from 'resource' on 'dependency'. */
219 void unlink(std::string resource, std::string dependency) {
220 dependencies[resource].erase(dependency);
221 if (dependencies[resource].size() == 0) {
222 dependencies.erase(resource);
223 }
224 }
225
Carl Worth619a0be2012-08-14 17:13:30 -0700226 /* Like unlink, but with a simply-formated string, (appending an
227 * integer to the given string). */
228 void unlinkf(std::string resource, std::string dependency, int dep_no) {
229
230 std::stringstream ss;
231 ss << dependency << dep_no;
232 unlink(resource, ss.str());
233 }
234
Carl Worth33b92632012-08-14 14:11:19 -0700235 /* Unlink all: Remove dependencies from 'resource' to all other
236 * resources. */
237 void unlinkAll(std::string resource) {
238 dependencies.erase(resource);
239 }
240
241 /* Resolve: Recursively compute all calls providing 'resource',
242 * (including linked dependencies of 'resource' on other
243 * resources). */
244 std::set<unsigned> resolve(std::string resource) {
245 std::set<std::string> *deps;
246 std::set<std::string>::iterator dep;
Carl Worthcc6e51c2012-08-13 14:35:43 -0700247
248 std::set<unsigned> *calls;
249 std::set<unsigned>::iterator call;
250
Carl Worth33b92632012-08-14 14:11:19 -0700251 std::set<unsigned> result, deps_set;
252
253 /* Recursively chase dependencies. */
254 if (dependencies.count(resource)) {
255 deps = &dependencies[resource];
256 for (dep = deps->begin(); dep != deps->end(); dep++) {
257 deps_set = resolve(*dep);
258 for (call = deps_set.begin(); call != deps_set.end(); call++) {
259 result.insert(*call);
260 }
261 }
262 }
263
264 /* Also look for calls that directly provide 'resource' */
Carl Worthcc6e51c2012-08-13 14:35:43 -0700265 if (resources.count(resource)) {
266 calls = &resources[resource];
267 for (call = calls->begin(); call != calls->end(); call++) {
Carl Worth33b92632012-08-14 14:11:19 -0700268 result.insert(*call);
Carl Worthcc6e51c2012-08-13 14:35:43 -0700269 }
Carl Worth33b92632012-08-14 14:11:19 -0700270 }
271
272 return result;
273 }
274
275 /* Consume: Resolve all calls that provide the given resource, and
276 * add them to the required list. Then clear the call list for
277 * 'resource' along with any dependencies. */
278 void consume(std::string resource) {
279
280 std::set<unsigned> calls;
281 std::set<unsigned>::iterator call;
282
283 calls = resolve(resource);
284
285 dependencies.erase(resource);
286 resources.erase(resource);
287
288 for (call = calls.begin(); call != calls.end(); call++) {
289 required.insert(*call);
Carl Worthcc6e51c2012-08-13 14:35:43 -0700290 }
291 }
292
293 void stateTrackPreCall(trace::Call *call) {
294
Carl Worth33b92632012-08-14 14:11:19 -0700295 const char *name = call->name();
296
297 if (strcmp(name, "glBegin") == 0) {
Carl Worthcc6e51c2012-08-13 14:35:43 -0700298 insideBeginEnd = true;
299 return;
300 }
301
Carl Worth33b92632012-08-14 14:11:19 -0700302 if (strcmp(name, "glBeginTransformFeedback") == 0) {
Carl Worthcc6e51c2012-08-13 14:35:43 -0700303 transformFeedbackActive = true;
304 return;
305 }
306
Carl Worth65692822012-08-17 08:06:51 -0700307 if (strcmp(name, "glActiveTexture") == 0) {
308 activeTextureUnit = static_cast<GLenum>(call->arg(0).toSInt());
309 return;
310 }
311
Carl Worth33b92632012-08-14 14:11:19 -0700312 if (strcmp(name, "glBindTexture") == 0) {
313 GLenum target;
314 GLuint texture;
315
316 target = static_cast<GLenum>(call->arg(0).toSInt());
317 texture = call->arg(1).toUInt();
318
319 if (texture == 0) {
320 texture_map.erase(target);
321 } else {
322 texture_map[target] = texture;
323 }
324
325 return;
326 }
327
Carl Worth5ff38572012-09-04 11:49:13 -0700328 if (strcmp(name, "glUseProgram") == 0) {
329 activeProgram = call->arg(0).toUInt();
330 }
331
Carl Worth33b92632012-08-14 14:11:19 -0700332 if (strcmp(name, "glBindFramebuffer") == 0) {
Carl Worthcc6e51c2012-08-13 14:35:43 -0700333 GLenum target;
334 GLuint framebuffer;
335
336 target = static_cast<GLenum>(call->arg(0).toSInt());
337 framebuffer = call->arg(1).toUInt();
338
339 if (target == GL_FRAMEBUFFER || target == GL_DRAW_FRAMEBUFFER) {
340 if (framebuffer == 0) {
341 framebufferObjectActive = false;
342 } else {
343 framebufferObjectActive = true;
344 }
345 }
346 return;
347 }
348 }
349
350 void stateTrackPostCall(trace::Call *call) {
351
Carl Worth33b92632012-08-14 14:11:19 -0700352 const char *name = call->name();
353
354 if (strcmp(name, "glEnd") == 0) {
Carl Worthcc6e51c2012-08-13 14:35:43 -0700355 insideBeginEnd = false;
356 return;
357 }
358
Carl Worth33b92632012-08-14 14:11:19 -0700359 if (strcmp(name, "glEndTransformFeedback") == 0) {
Carl Worthcc6e51c2012-08-13 14:35:43 -0700360 transformFeedbackActive = false;
361 return;
362 }
363
Carl Worth33b92632012-08-14 14:11:19 -0700364 /* If this swapbuffers was included in the trace then it will
365 * have already consumed all framebuffer dependencies. If not,
366 * then clear them now so that they don't carry over into the
367 * next frame. */
Carl Worthcc6e51c2012-08-13 14:35:43 -0700368 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
369 call->flags & trace::CALL_FLAG_END_FRAME) {
Carl Worth33b92632012-08-14 14:11:19 -0700370 dependencies.erase("framebuffer");
Carl Worthcc6e51c2012-08-13 14:35:43 -0700371 resources.erase("framebuffer");
372 return;
373 }
374 }
375
376 void recordSideEffects(trace::Call *call) {
Carl Worth33b92632012-08-14 14:11:19 -0700377
378 const char *name = call->name();
379
Carl Worthcc6e51c2012-08-13 14:35:43 -0700380 /* If call is flagged as no side effects, then we are done here. */
Carl Worth5b827e12012-08-12 20:41:50 -0700381 if (call->flags & trace::CALL_FLAG_NO_SIDE_EFFECTS) {
382 return;
383 }
384
Carl Worthcc6e51c2012-08-13 14:35:43 -0700385 /* Similarly, swap-buffers calls don't have interesting side effects. */
Carl Worth5b827e12012-08-12 20:41:50 -0700386 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
387 call->flags & trace::CALL_FLAG_END_FRAME) {
388 return;
389 }
390
Carl Worth33b92632012-08-14 14:11:19 -0700391 if (strcmp(name, "glGenTextures") == 0) {
392 const trace::Array *textures = dynamic_cast<const trace::Array *>(&call->arg(1));
393 size_t i;
394 GLuint texture;
395
396 if (textures) {
397 for (i = 0; i < textures->size(); i++) {
Carl Worth33b92632012-08-14 14:11:19 -0700398 texture = textures->values[i]->toUInt();
Carl Worth619a0be2012-08-14 17:13:30 -0700399 providef("texture-", texture, call->no);
Carl Worth33b92632012-08-14 14:11:19 -0700400 }
401 }
402 return;
403 }
404
405 if (strcmp(name, "glBindTexture") == 0) {
406 GLenum target;
407 GLuint texture;
408
409 std::stringstream ss_target, ss_texture;
410
411 target = static_cast<GLenum>(call->arg(0).toSInt());
412 texture = call->arg(1).toUInt();
413
Carl Worth65692822012-08-17 08:06:51 -0700414 ss_target << "texture-unit-" << activeTextureUnit << "-target-" << target;
Carl Worth33b92632012-08-14 14:11:19 -0700415 ss_texture << "texture-" << texture;
416
417 resources.erase(ss_target.str());
418 provide(ss_target.str(), call->no);
419
420 unlinkAll(ss_target.str());
421 link(ss_target.str(), ss_texture.str());
422
423 return;
424 }
425
426 /* FIXME: Need to handle glMultTexImage and friends. */
427 if (STRNCMP_LITERAL(name, "glTexImage") == 0 ||
428 STRNCMP_LITERAL(name, "glTexSubImage") == 0 ||
429 STRNCMP_LITERAL(name, "glCopyTexImage") == 0 ||
430 STRNCMP_LITERAL(name, "glCopyTexSubImage") == 0 ||
431 STRNCMP_LITERAL(name, "glCompressedTexImage") == 0 ||
432 STRNCMP_LITERAL(name, "glCompressedTexSubImage") == 0 ||
433 strcmp(name, "glInvalidateTexImage") == 0 ||
434 strcmp(name, "glInvalidateTexSubImage") == 0) {
435
436 std::set<unsigned> *calls;
437 std::set<unsigned>::iterator c;
438 std::stringstream ss_target, ss_texture;
439
440 GLenum target = static_cast<GLenum>(call->arg(0).toSInt());
441
Carl Worth65692822012-08-17 08:06:51 -0700442 ss_target << "texture-unit-" << activeTextureUnit << "-target-" << target;
Carl Worth33b92632012-08-14 14:11:19 -0700443 ss_texture << "texture-" << texture_map[target];
444
445 /* The texture resource depends on this call and any calls
446 * providing the given texture target. */
447 provide(ss_texture.str(), call->no);
448
449 if (resources.count(ss_target.str())) {
450 calls = &resources[ss_target.str()];
451 for (c = calls->begin(); c != calls->end(); c++) {
452 provide(ss_texture.str(), *c);
453 }
454 }
455
456 return;
457 }
458
459 if (strcmp(name, "glEnable") == 0) {
460 GLenum cap;
461
462 cap = static_cast<GLenum>(call->arg(0).toSInt());
463
464 if (cap == GL_TEXTURE_1D ||
465 cap == GL_TEXTURE_2D ||
466 cap == GL_TEXTURE_3D ||
467 cap == GL_TEXTURE_CUBE_MAP)
468 {
Carl Worth65692822012-08-17 08:06:51 -0700469 std::stringstream ss;
470
471 ss << "texture-unit-" << activeTextureUnit << "-target-" << cap;
472
473 link("render-state", ss.str());
Carl Worth33b92632012-08-14 14:11:19 -0700474 }
475
476 provide("state", call->no);
Carl Worth33b92632012-08-14 14:11:19 -0700477 return;
478 }
479
480 if (strcmp(name, "glDisable") == 0) {
481 GLenum cap;
482
483 cap = static_cast<GLenum>(call->arg(0).toSInt());
484
485 if (cap == GL_TEXTURE_1D ||
486 cap == GL_TEXTURE_2D ||
487 cap == GL_TEXTURE_3D ||
488 cap == GL_TEXTURE_CUBE_MAP)
489 {
Carl Worth65692822012-08-17 08:06:51 -0700490 std::stringstream ss;
491
492 ss << "texture-unit-" << activeTextureUnit << "-target-" << cap;
493
494 unlink("render-state", ss.str());
Carl Worth33b92632012-08-14 14:11:19 -0700495 }
496
497 provide("state", call->no);
Carl Worth33b92632012-08-14 14:11:19 -0700498 return;
499 }
500
Carl Worthbbbbe702012-08-14 15:20:56 -0700501 if (strcmp(name, "glCreateShader") == 0 ||
502 strcmp(name, "glCreateShaderObjectARB") == 0) {
503
Carl Worth619a0be2012-08-14 17:13:30 -0700504 GLuint shader = call->ret->toUInt();
505 providef("shader-", shader, call->no);
Carl Worthbbbbe702012-08-14 15:20:56 -0700506 return;
507 }
508
509 if (strcmp(name, "glShaderSource") == 0 ||
510 strcmp(name, "glShaderSourceARB") == 0 ||
511 strcmp(name, "glCompileShader") == 0 ||
512 strcmp(name, "glCompileShaderARB") == 0 ||
513 strcmp(name, "glGetShaderiv") == 0 ||
514 strcmp(name, "glGetShaderInfoLog") == 0) {
515
Carl Worth619a0be2012-08-14 17:13:30 -0700516 GLuint shader = call->arg(0).toUInt();
517 providef("shader-", shader, call->no);
Carl Worthbbbbe702012-08-14 15:20:56 -0700518 return;
519 }
520
521 if (strcmp(name, "glCreateProgram") == 0 ||
522 strcmp(name, "glCreateProgramObjectARB") == 0) {
523
Carl Worth619a0be2012-08-14 17:13:30 -0700524 GLuint program = call->ret->toUInt();
525 providef("program-", program, call->no);
Carl Worthbbbbe702012-08-14 15:20:56 -0700526 return;
527 }
528
529 if (strcmp(name, "glAttachShader") == 0 ||
530 strcmp(name, "glAttachObjectARB") == 0) {
531
532 GLuint program, shader;
533 std::stringstream ss_program, ss_shader;
534
535 program = call->arg(0).toUInt();
536 shader = call->arg(1).toUInt();
537
538 ss_program << "program-" << program;
539 ss_shader << "shader-" << shader;
540
541 link(ss_program.str(), ss_shader.str());
542 provide(ss_program.str(), call->no);
543
544 return;
545 }
546
547 if (strcmp(name, "glDetachShader") == 0 ||
548 strcmp(name, "glDetachObjectARB") == 0) {
549
550 GLuint program, shader;
551 std::stringstream ss_program, ss_shader;
552
553 program = call->arg(0).toUInt();
554 shader = call->arg(1).toUInt();
555
556 ss_program << "program-" << program;
557 ss_shader << "shader-" << shader;
558
559 unlink(ss_program.str(), ss_shader.str());
560
561 return;
562 }
563
564 if (strcmp(name, "glUseProgram") == 0 ||
565 strcmp(name, "glUseProgramObjectARB") == 0) {
566
567 GLuint program;
568
569 program = call->arg(0).toUInt();
570
571 unlinkAll("render-program-state");
572
573 if (program == 0) {
574 unlink("render-state", "render-program-state");
575 provide("state", call->no);
576 } else {
577 std::stringstream ss;
578
579 ss << "program-" << program;
580
581 link("render-state", "render-program-state");
582 link("render-program-state", ss.str());
583
584 provide(ss.str(), call->no);
585 }
586
587 return;
588 }
589
590 if (strcmp(name, "glGetUniformLocation") == 0 ||
591 strcmp(name, "glGetUniformLocationARB") == 0 ||
592 strcmp(name, "glGetFragDataLocation") == 0 ||
593 strcmp(name, "glGetFragDataLocationEXT") == 0 ||
594 strcmp(name, "glGetSubroutineUniformLocation") == 0 ||
595 strcmp(name, "glGetProgramResourceLocation") == 0 ||
596 strcmp(name, "glGetProgramResourceLocationIndex") == 0 ||
597 strcmp(name, "glGetVaryingLocationNV") == 0) {
598
Carl Worth619a0be2012-08-14 17:13:30 -0700599 GLuint program = call->arg(0).toUInt();
Carl Worthbbbbe702012-08-14 15:20:56 -0700600
Carl Worth619a0be2012-08-14 17:13:30 -0700601 providef("program-", program, call->no);
Carl Worthbbbbe702012-08-14 15:20:56 -0700602
603 return;
604 }
605
606 /* For any call that accepts 'location' as its first argument,
607 * perform a lookup in our location->program map and add a
608 * dependence on the program we find there. */
609 if (call->sig->num_args > 0 &&
610 strcmp(call->sig->arg_names[0], "location") == 0) {
611
Carl Worth619a0be2012-08-14 17:13:30 -0700612 providef("program-", activeProgram, call->no);
Carl Worth81bda5a2012-08-17 08:09:19 -0700613
614 /* We can't easily tell if this uniform is being used to
615 * associate a sampler in the shader with a texture
616 * unit. The conservative option is to assume that it is
617 * and create a link from the active program to any bound
618 * textures for the given unit number.
619 *
620 * FIXME: We should be doing the same thing for calls to
621 * glUniform1iv. */
622 if (strcmp(name, "glUniform1i") == 0 ||
623 strcmp(name, "glUniform1iARB") == 0) {
624
625 GLint max_unit = MAX(GL_MAX_TEXTURE_COORDS, GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS);
626
627 GLint unit = call->arg(1).toSInt();
628 std::stringstream ss_program;
629 std::stringstream ss_texture;
630
631 if (unit < max_unit) {
632
633 ss_program << "program-" << activeProgram;
634
635 ss_texture << "texture-unit-" << GL_TEXTURE0 + unit << "-target-";
636
637 /* We don't know what target(s) might get bound to
638 * this texture unit, so conservatively link to
639 * all. Only bound textures will actually get inserted
640 * into the output call stream. */
641 linkf(ss_program.str(), ss_texture.str(), GL_TEXTURE_1D);
642 linkf(ss_program.str(), ss_texture.str(), GL_TEXTURE_2D);
643 linkf(ss_program.str(), ss_texture.str(), GL_TEXTURE_3D);
644 linkf(ss_program.str(), ss_texture.str(), GL_TEXTURE_CUBE_MAP);
645 }
646 }
647
Carl Worthbbbbe702012-08-14 15:20:56 -0700648 return;
649 }
650
651 /* FIXME: We cut a huge swath by assuming that any unhandled
652 * call that has a first argument named "program" should not
653 * be included in the trimmed output unless the program of
654 * that number is also included.
655 *
656 * This heuristic is correct for many cases, but we should
657 * actually carefully verify if this includes some calls
658 * inappropriately, or if it misses some.
659 */
660 if (strcmp(name, "glLinkProgram") == 0 ||
661 strcmp(name, "glLinkProgramARB") == 0 ||
662 (call->sig->num_args > 0 &&
663 (strcmp(call->sig->arg_names[0], "program") == 0 ||
664 strcmp(call->sig->arg_names[0], "programObj") == 0))) {
665
Carl Worth619a0be2012-08-14 17:13:30 -0700666 GLuint program = call->arg(0).toUInt();
667 providef("program-", program, call->no);
Carl Worthbbbbe702012-08-14 15:20:56 -0700668 return;
669 }
670
Carl Worthcc6e51c2012-08-13 14:35:43 -0700671 /* Handle all rendering operations, (even though only glEnd is
672 * flagged as a rendering operation we treat everything from
673 * glBegin through glEnd as a rendering operation). */
674 if (call->flags & trace::CALL_FLAG_RENDER ||
675 insideBeginEnd) {
676
Carl Worth33b92632012-08-14 14:11:19 -0700677 std::set<unsigned> calls;
678 std::set<unsigned>::iterator c;
679
Carl Worthcc6e51c2012-08-13 14:35:43 -0700680 provide("framebuffer", call->no);
681
Carl Worth33b92632012-08-14 14:11:19 -0700682 calls = resolve("render-state");
683
684 for (c = calls.begin(); c != calls.end(); c++) {
685 provide("framebuffer", *c);
686 }
687
Carl Worthcc6e51c2012-08-13 14:35:43 -0700688 /* In some cases, rendering has side effects beyond the
689 * framebuffer update. */
690 if (renderingHasSideEffect()) {
691 provide("state", call->no);
Carl Worth33b92632012-08-14 14:11:19 -0700692 for (c = calls.begin(); c != calls.end(); c++) {
693 provide("state", *c);
694 }
Carl Worthcc6e51c2012-08-13 14:35:43 -0700695 }
696
697 return;
698 }
699
Carl Worth5b827e12012-08-12 20:41:50 -0700700 /* By default, assume this call affects the state somehow. */
Carl Worth163b22c2012-08-10 10:15:30 -0700701 resources["state"].insert(call->no);
702 }
703
Carl Worthcc6e51c2012-08-13 14:35:43 -0700704 void requireDependencies(trace::Call *call) {
705
706 /* Swap-buffers calls depend on framebuffer state. */
707 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
708 call->flags & trace::CALL_FLAG_END_FRAME) {
709 consume("framebuffer");
710 }
711
712 /* By default, just assume this call depends on generic state. */
713 consume("state");
714 }
715
716
717public:
718 TraceAnalyzer(): transformFeedbackActive(false),
719 framebufferObjectActive(false),
Carl Worth65692822012-08-17 08:06:51 -0700720 insideBeginEnd(false),
721 activeTextureUnit(GL_TEXTURE0)
Carl Worthcc6e51c2012-08-13 14:35:43 -0700722 {}
723
724 ~TraceAnalyzer() {}
725
726 /* Analyze this call by tracking state and recording all the
727 * resources provided by this call as side effects.. */
728 void analyze(trace::Call *call) {
729
730 stateTrackPreCall(call);
731
732 recordSideEffects(call);
733
734 stateTrackPostCall(call);
735 }
736
Carl Worth163b22c2012-08-10 10:15:30 -0700737 /* Require this call and all of its dependencies to be included in
738 * the final trace. */
739 void require(trace::Call *call) {
Carl Worth163b22c2012-08-10 10:15:30 -0700740
741 /* First, find and insert all calls that this call depends on. */
Carl Worthcc6e51c2012-08-13 14:35:43 -0700742 requireDependencies(call);
Carl Worth163b22c2012-08-10 10:15:30 -0700743
744 /* Then insert this call itself. */
745 required.insert(call->no);
746 }
747
748 /* Return a set of all the required calls, (both those calls added
749 * explicitly with require() and those implicitly depended
750 * upon. */
751 std::set<unsigned> *get_required(void) {
752 return &required;
753 }
754};
755
756struct trim_options {
757 /* Calls to be included in trace. */
758 trace::CallSet calls;
759
Carl Worth46abfd12012-08-14 22:32:29 -0700760 /* Frames to be included in trace. */
761 trace::CallSet frames;
762
Carl Worth163b22c2012-08-10 10:15:30 -0700763 /* Whether dependency analysis should be performed. */
764 bool dependency_analysis;
765
Carl Worth16b18db2012-08-11 11:33:12 -0700766 /* Whether uninteresting calls should be pruned.. */
767 bool prune_uninteresting;
768
Carl Worth163b22c2012-08-10 10:15:30 -0700769 /* Output filename */
770 std::string output;
771
772 /* Emit only calls from this thread (-1 == all threads) */
773 int thread;
Carl Worthab82b3b2012-08-15 16:35:38 -0700774
775 /* Print resulting callset */
776 int print_callset;
Carl Worth163b22c2012-08-10 10:15:30 -0700777};
778
779static int
780trim_trace(const char *filename, struct trim_options *options)
781{
782 trace::ParseBookmark beginning;
783 trace::Parser p;
784 TraceAnalyzer analyzer;
785 std::set<unsigned> *required;
Carl Worth46abfd12012-08-14 22:32:29 -0700786 unsigned frame;
Carl Worthab82b3b2012-08-15 16:35:38 -0700787 int call_range_first, call_range_last;
Carl Worth163b22c2012-08-10 10:15:30 -0700788
789 if (!p.open(filename)) {
790 std::cerr << "error: failed to open " << filename << "\n";
791 return 1;
792 }
793
794 /* Mark the beginning so we can return here for pass 2. */
795 p.getBookmark(beginning);
796
797 /* In pass 1, analyze which calls are needed. */
Carl Worth46abfd12012-08-14 22:32:29 -0700798 frame = 0;
Carl Worth163b22c2012-08-10 10:15:30 -0700799 trace::Call *call;
800 while ((call = p.parse_call())) {
Carl Worth5b827e12012-08-12 20:41:50 -0700801
Carl Worth46abfd12012-08-14 22:32:29 -0700802 /* There's no use doing any work past the last call or frame
803 * requested by the user. */
804 if (call->no > options->calls.getLast() ||
805 frame > options->frames.getLast()) {
806
Carl Worth42249012012-08-14 10:26:11 -0700807 delete call;
Carl Worth5b827e12012-08-12 20:41:50 -0700808 break;
Carl Worth42249012012-08-14 10:26:11 -0700809 }
Carl Worth5b827e12012-08-12 20:41:50 -0700810
Carl Worth163b22c2012-08-10 10:15:30 -0700811 /* If requested, ignore all calls not belonging to the specified thread. */
Carl Worth42249012012-08-14 10:26:11 -0700812 if (options->thread != -1 && call->thread_id != options->thread) {
Carl Worth46abfd12012-08-14 22:32:29 -0700813 goto NEXT;
Carl Worth42249012012-08-14 10:26:11 -0700814 }
Carl Worth163b22c2012-08-10 10:15:30 -0700815
Carl Worth16b18db2012-08-11 11:33:12 -0700816 /* Also, prune if uninteresting (unless the user asked for no pruning. */
817 if (options->prune_uninteresting && call->flags & trace::CALL_FLAG_UNINTERESTING) {
Carl Worth46abfd12012-08-14 22:32:29 -0700818 goto NEXT;
Carl Worth16b18db2012-08-11 11:33:12 -0700819 }
820
Carl Worthcc6e51c2012-08-13 14:35:43 -0700821 /* If this call is included in the user-specified call set,
822 * then require it (and all dependencies) in the trimmed
823 * output. */
Carl Worth46abfd12012-08-14 22:32:29 -0700824 if (options->calls.contains(*call) ||
825 options->frames.contains(frame, call->flags)) {
826
Carl Worth163b22c2012-08-10 10:15:30 -0700827 analyzer.require(call);
Carl Worthcc6e51c2012-08-13 14:35:43 -0700828 }
829
830 /* Regardless of whether we include this call or not, we do
831 * some dependency tracking (unless disabled by the user). We
832 * do this even for calls we have included in the output so
833 * that any state updates get performed. */
834 if (options->dependency_analysis) {
835 analyzer.analyze(call);
Carl Worth163b22c2012-08-10 10:15:30 -0700836 }
Carl Worth42249012012-08-14 10:26:11 -0700837
Carl Worth46abfd12012-08-14 22:32:29 -0700838 NEXT:
839 if (call->flags & trace::CALL_FLAG_END_FRAME)
840 frame++;
841
Carl Worth42249012012-08-14 10:26:11 -0700842 delete call;
Carl Worth163b22c2012-08-10 10:15:30 -0700843 }
844
845 /* Prepare output file and writer for output. */
846 if (options->output.empty()) {
847 os::String base(filename);
848 base.trimExtension();
849
850 options->output = std::string(base.str()) + std::string("-trim.trace");
851 }
852
853 trace::Writer writer;
854 if (!writer.open(options->output.c_str())) {
855 std::cerr << "error: failed to create " << filename << "\n";
856 return 1;
857 }
858
859 /* Reset bookmark for pass 2. */
860 p.setBookmark(beginning);
861
862 /* In pass 2, emit the calls that are required. */
863 required = analyzer.get_required();
864
Carl Worth46abfd12012-08-14 22:32:29 -0700865 frame = 0;
Carl Worthab82b3b2012-08-15 16:35:38 -0700866 call_range_first = -1;
867 call_range_last = -1;
Carl Worth163b22c2012-08-10 10:15:30 -0700868 while ((call = p.parse_call())) {
Carl Worth5b827e12012-08-12 20:41:50 -0700869
Carl Worth46abfd12012-08-14 22:32:29 -0700870 /* There's no use doing any work past the last call or frame
871 * requested by the user. */
872 if (call->no > options->calls.getLast() ||
873 frame > options->frames.getLast()) {
874
Carl Worth5b827e12012-08-12 20:41:50 -0700875 break;
Carl Worth46abfd12012-08-14 22:32:29 -0700876 }
Carl Worth5b827e12012-08-12 20:41:50 -0700877
Carl Worth163b22c2012-08-10 10:15:30 -0700878 if (required->find(call->no) != required->end()) {
879 writer.writeCall(call);
Carl Worthab82b3b2012-08-15 16:35:38 -0700880
881 if (options->print_callset) {
882 if (call_range_first < 0) {
883 call_range_first = call->no;
884 printf ("%d", call_range_first);
885 } else if (call->no != call_range_last + 1) {
886 if (call_range_last != call_range_first)
887 printf ("-%d", call_range_last);
888 call_range_first = call->no;
889 printf (",%d", call_range_first);
890 }
891 call_range_last = call->no;
892 }
Carl Worth163b22c2012-08-10 10:15:30 -0700893 }
Carl Worth46abfd12012-08-14 22:32:29 -0700894
895 if (call->flags & trace::CALL_FLAG_END_FRAME) {
896 frame++;
897 }
898
Carl Worth163b22c2012-08-10 10:15:30 -0700899 delete call;
900 }
901
Carl Worthab82b3b2012-08-15 16:35:38 -0700902 if (options->print_callset) {
903 if (call_range_last != call_range_first)
904 printf ("-%d\n", call_range_last);
905 }
906
Carl Worth163b22c2012-08-10 10:15:30 -0700907 std::cout << "Trimmed trace is available as " << options->output << "\n";
908
909 return 0;
910}
911
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800912static int
913command(int argc, char *argv[])
914{
Carl Worth163b22c2012-08-10 10:15:30 -0700915 struct trim_options options;
916
Carl Worth46abfd12012-08-14 22:32:29 -0700917 options.calls = trace::CallSet(trace::FREQUENCY_NONE);
918 options.frames = trace::CallSet(trace::FREQUENCY_NONE);
Carl Worth163b22c2012-08-10 10:15:30 -0700919 options.dependency_analysis = true;
Carl Worth16b18db2012-08-11 11:33:12 -0700920 options.prune_uninteresting = true;
Carl Worth163b22c2012-08-10 10:15:30 -0700921 options.output = "";
922 options.thread = -1;
Carl Worthab82b3b2012-08-15 16:35:38 -0700923 options.print_callset = 0;
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800924
José Fonsecab682b672012-02-15 07:13:31 +0000925 int opt;
926 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
927 switch (opt) {
928 case 'h':
Carl Worth86464432012-08-11 11:46:54 -0700929 help();
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800930 return 0;
José Fonsecab682b672012-02-15 07:13:31 +0000931 case CALLS_OPT:
Carl Worth163b22c2012-08-10 10:15:30 -0700932 options.calls = trace::CallSet(optarg);
933 break;
Carl Worth46abfd12012-08-14 22:32:29 -0700934 case FRAMES_OPT:
935 options.frames = trace::CallSet(optarg);
936 break;
Carl Worth163b22c2012-08-10 10:15:30 -0700937 case DEPS_OPT:
938 options.dependency_analysis = true;
939 break;
940 case NO_DEPS_OPT:
941 options.dependency_analysis = false;
José Fonsecab682b672012-02-15 07:13:31 +0000942 break;
Carl Worth16b18db2012-08-11 11:33:12 -0700943 case PRUNE_OPT:
944 options.prune_uninteresting = true;
945 break;
946 case NO_PRUNE_OPT:
947 options.prune_uninteresting = false;
948 break;
949 case 'x':
950 options.dependency_analysis = false;
951 options.prune_uninteresting = false;
952 break;
Imre Deak6f07a842012-05-08 15:20:43 +0300953 case THREAD_OPT:
Carl Worth163b22c2012-08-10 10:15:30 -0700954 options.thread = atoi(optarg);
Imre Deak6f07a842012-05-08 15:20:43 +0300955 break;
José Fonsecab682b672012-02-15 07:13:31 +0000956 case 'o':
Carl Worth163b22c2012-08-10 10:15:30 -0700957 options.output = optarg;
José Fonsecab682b672012-02-15 07:13:31 +0000958 break;
Carl Worthab82b3b2012-08-15 16:35:38 -0700959 case PRINT_CALLSET_OPT:
960 options.print_callset = 1;
961 break;
José Fonsecab682b672012-02-15 07:13:31 +0000962 default:
963 std::cerr << "error: unexpected option `" << opt << "`\n";
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800964 usage();
965 return 1;
966 }
967 }
968
Carl Worth46abfd12012-08-14 22:32:29 -0700969 /* If neither of --calls nor --frames was set, default to the
970 * entire set of calls. */
971 if (options.calls.empty() && options.frames.empty()) {
972 options.calls = trace::CallSet(trace::FREQUENCY_ALL);
973 }
974
José Fonsecab682b672012-02-15 07:13:31 +0000975 if (optind >= argc) {
976 std::cerr << "error: apitrace trim requires a trace file as an argument.\n";
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800977 usage();
978 return 1;
979 }
980
Carl Worthf630d9d2012-09-04 16:48:00 -0700981 if (argc > optind + 1) {
982 std::cerr << "error: extraneous arguments:";
983 for (int i = optind + 1; i < argc; i++) {
984 std::cerr << " " << argv[i];
985 }
986 std::cerr << "\n";
987 usage();
988 return 1;
989 }
990
Carl Worth163b22c2012-08-10 10:15:30 -0700991 return trim_trace(argv[optind], &options);
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800992}
993
994const Command trim_command = {
995 "trim",
996 synopsis,
Carl Worth86464432012-08-11 11:46:54 -0700997 help,
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800998 command
999};