1 /**************************************************************************
2 * Copyright 2012 Intel corporation
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 **************************************************************************/
28 #include "trace_analyzer.hpp"
30 #define MAX(a, b) ((a) > (b) ? (a) : (b))
31 #define STRNCMP_LITERAL(var, literal) strncmp((var), (literal), sizeof (literal) -1)
33 /* Rendering often has no side effects, but it can in some cases,
34 * (such as when transform feedback is active, or when rendering
35 * targets a framebuffer object). */
37 TraceAnalyzer::renderingHasSideEffect(void)
39 return transformFeedbackActive || framebufferObjectActive;
42 /* Provide: Record that the given call affects the given resource
43 * as a side effect. */
45 TraceAnalyzer::provide(std::string resource, trace::CallNo call_no)
47 resources[resource].insert(call_no);
50 /* Like provide, but with a simply-formatted string, (appending an
51 * integer to the given string). */
53 TraceAnalyzer::providef(std::string resource,
55 trace::CallNo call_no)
58 ss << resource << resource_no;
59 provide(ss.str(), call_no);
62 /* Link: Establish a dependency between resource 'resource' and
63 * resource 'dependency'. This dependency is captured by name so
64 * that if the list of calls that provide 'dependency' grows
65 * before 'resource' is consumed, those calls will still be
68 TraceAnalyzer::link(std::string resource, std::string dependency)
70 dependencies[resource].insert(dependency);
73 /* Like link, but with a simply-formatted string, (appending an
74 * integer to the given string). */
76 TraceAnalyzer::linkf(std::string resource, std::string dependency, int dep_no)
80 ss << dependency << dep_no;
81 link(resource, ss.str());
84 /* Unlink: Remove dependency from 'resource' on 'dependency'. */
86 TraceAnalyzer::unlink(std::string resource, std::string dependency)
88 dependencies[resource].erase(dependency);
89 if (dependencies[resource].size() == 0) {
90 dependencies.erase(resource);
94 /* Like unlink, but with a simply-formated string, (appending an
95 * integer to the given string). */
97 TraceAnalyzer::unlinkf(std::string resource, std::string dependency, int dep_no)
100 std::stringstream ss;
101 ss << dependency << dep_no;
102 unlink(resource, ss.str());
105 /* Unlink all: Remove dependencies from 'resource' to all other
108 TraceAnalyzer::unlinkAll(std::string resource)
110 dependencies.erase(resource);
113 /* Resolve: Recursively compute all calls providing 'resource',
114 * (including linked dependencies of 'resource' on other
117 TraceAnalyzer::resolve(std::string resource)
119 std::set<std::string> *deps;
120 std::set<std::string>::iterator dep;
122 std::set<unsigned> *calls;
123 std::set<unsigned>::iterator call;
125 std::set<unsigned> result, deps_set;
127 /* Recursively chase dependencies. */
128 if (dependencies.count(resource)) {
129 deps = &dependencies[resource];
130 for (dep = deps->begin(); dep != deps->end(); dep++) {
131 deps_set = resolve(*dep);
132 for (call = deps_set.begin(); call != deps_set.end(); call++) {
133 result.insert(*call);
138 /* Also look for calls that directly provide 'resource' */
139 if (resources.count(resource)) {
140 calls = &resources[resource];
141 for (call = calls->begin(); call != calls->end(); call++) {
142 result.insert(*call);
149 /* Consume: Resolve all calls that provide the given resource, and
150 * add them to the required list. Then clear the call list for
151 * 'resource' along with any dependencies. */
153 TraceAnalyzer::consume(std::string resource)
156 std::set<unsigned> calls;
157 std::set<unsigned>::iterator call;
159 calls = resolve(resource);
161 dependencies.erase(resource);
162 resources.erase(resource);
164 for (call = calls.begin(); call != calls.end(); call++) {
170 TraceAnalyzer::stateTrackPreCall(trace::Call *call)
173 const char *name = call->name();
175 if (strcmp(name, "glBegin") == 0) {
176 insideBeginEnd = true;
180 if (strcmp(name, "glBeginTransformFeedback") == 0) {
181 transformFeedbackActive = true;
185 if (strcmp(name, "glActiveTexture") == 0) {
186 activeTextureUnit = static_cast<GLenum>(call->arg(0).toSInt());
190 if (strcmp(name, "glBindTexture") == 0) {
194 target = static_cast<GLenum>(call->arg(0).toSInt());
195 texture = call->arg(1).toUInt();
198 texture_map.erase(target);
200 texture_map[target] = texture;
206 if (strcmp(name, "glUseProgram") == 0) {
207 activeProgram = call->arg(0).toUInt();
210 if (strcmp(name, "glBindFramebuffer") == 0) {
214 target = static_cast<GLenum>(call->arg(0).toSInt());
215 framebuffer = call->arg(1).toUInt();
217 if (target == GL_FRAMEBUFFER || target == GL_DRAW_FRAMEBUFFER) {
218 if (framebuffer == 0) {
219 framebufferObjectActive = false;
221 framebufferObjectActive = true;
227 if (strcmp(name, "glNewList") == 0) {
228 GLuint list = call->arg(0).toUInt();
230 insideNewEndList = list;
235 TraceAnalyzer::stateTrackPostCall(trace::Call *call)
238 const char *name = call->name();
240 if (strcmp(name, "glEnd") == 0) {
241 insideBeginEnd = false;
245 if (strcmp(name, "glEndTransformFeedback") == 0) {
246 transformFeedbackActive = false;
250 /* If this swapbuffers was included in the trace then it will
251 * have already consumed all framebuffer dependencies. If not,
252 * then clear them now so that they don't carry over into the
254 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
255 call->flags & trace::CALL_FLAG_END_FRAME) {
256 dependencies.erase("framebuffer");
257 resources.erase("framebuffer");
261 if (strcmp(name, "glEndList") == 0) {
262 insideNewEndList = 0;
267 TraceAnalyzer::callHasNoSideEffects(trace::Call *call, const char *name)
269 /* If call is flagged as no side effects, then we are done here. */
270 if (call->flags & trace::CALL_FLAG_NO_SIDE_EFFECTS) {
274 /* Not known as a no-side-effect call. Return false for more analysis. */
279 TraceAnalyzer::recordTextureSideEffects(trace::Call *call, const char *name)
281 if (strcmp(name, "glGenTextures") == 0) {
282 const trace::Array *textures = dynamic_cast<const trace::Array *>(&call->arg(1));
287 for (i = 0; i < textures->size(); i++) {
288 texture = textures->values[i]->toUInt();
289 providef("texture-", texture, call->no);
295 /* FIXME: When we start tracking framebuffer objects as their own
296 * resources, we will want to link the FBO to the given texture
297 * resource, (and to this call). For now, just link render state
298 * to the texture, and force this call to be required. */
299 if (strcmp(name, "glFramebufferTexture2D") == 0) {
302 texture = call->arg(3).toUInt();
304 linkf("render-state", "texture-", texture);
306 provide("state", call->no);
309 if (strcmp(name, "glBindTexture") == 0) {
313 std::stringstream ss_target, ss_texture;
315 target = static_cast<GLenum>(call->arg(0).toSInt());
316 texture = call->arg(1).toUInt();
318 ss_target << "texture-unit-" << activeTextureUnit << "-target-" << target;
319 ss_texture << "texture-" << texture;
321 resources.erase(ss_target.str());
322 provide(ss_target.str(), call->no);
324 unlinkAll(ss_target.str());
325 link(ss_target.str(), ss_texture.str());
327 /* FIXME: This really shouldn't be necessary. The effect
328 * this provide() has is that all glBindTexture calls will
329 * be preserved in the output trace (never trimmed). Carl
330 * has a trace ("btr") where a glBindTexture call should
331 * not be necessary at all, (it's immediately followed
332 * with a glBindTexture to a different texture and no
333 * intervening texture-related calls), yet this 'provide'
334 * makes the difference between a trim_stress test failing
337 * More investigation is necessary, but for now, be
338 * conservative and don't trim. */
339 provide("state", call->no);
344 /* FIXME: Need to handle glMultiTexImage and friends. */
345 if (STRNCMP_LITERAL(name, "glTexImage") == 0 ||
346 STRNCMP_LITERAL(name, "glTexSubImage") == 0 ||
347 STRNCMP_LITERAL(name, "glCopyTexImage") == 0 ||
348 STRNCMP_LITERAL(name, "glCopyTexSubImage") == 0 ||
349 STRNCMP_LITERAL(name, "glCompressedTexImage") == 0 ||
350 STRNCMP_LITERAL(name, "glCompressedTexSubImage") == 0 ||
351 strcmp(name, "glInvalidateTexImage") == 0 ||
352 strcmp(name, "glInvalidateTexSubImage") == 0) {
354 std::set<unsigned> *calls;
355 std::set<unsigned>::iterator c;
356 std::stringstream ss_target, ss_texture;
358 GLenum target = static_cast<GLenum>(call->arg(0).toSInt());
360 ss_target << "texture-unit-" << activeTextureUnit << "-target-" << target;
361 ss_texture << "texture-" << texture_map[target];
363 /* The texture resource depends on this call and any calls
364 * providing the given texture target. */
365 provide(ss_texture.str(), call->no);
367 if (resources.count(ss_target.str())) {
368 calls = &resources[ss_target.str()];
369 for (c = calls->begin(); c != calls->end(); c++) {
370 provide(ss_texture.str(), *c);
377 if (strcmp(name, "glEnable") == 0) {
380 cap = static_cast<GLenum>(call->arg(0).toSInt());
382 if (cap == GL_TEXTURE_1D ||
383 cap == GL_TEXTURE_2D ||
384 cap == GL_TEXTURE_3D ||
385 cap == GL_TEXTURE_CUBE_MAP)
387 std::stringstream ss;
389 ss << "texture-unit-" << activeTextureUnit << "-target-" << cap;
391 link("render-state", ss.str());
394 provide("state", call->no);
398 if (strcmp(name, "glDisable") == 0) {
401 cap = static_cast<GLenum>(call->arg(0).toSInt());
403 if (cap == GL_TEXTURE_1D ||
404 cap == GL_TEXTURE_2D ||
405 cap == GL_TEXTURE_3D ||
406 cap == GL_TEXTURE_CUBE_MAP)
408 std::stringstream ss;
410 ss << "texture-unit-" << activeTextureUnit << "-target-" << cap;
412 unlink("render-state", ss.str());
415 provide("state", call->no);
419 /* No known texture-related side effects. Return false for more analysis. */
424 TraceAnalyzer::recordShaderSideEffects(trace::Call *call, const char *name)
426 if (strcmp(name, "glCreateShader") == 0 ||
427 strcmp(name, "glCreateShaderObjectARB") == 0) {
429 GLuint shader = call->ret->toUInt();
430 providef("shader-", shader, call->no);
434 if (strcmp(name, "glShaderSource") == 0 ||
435 strcmp(name, "glShaderSourceARB") == 0 ||
436 strcmp(name, "glCompileShader") == 0 ||
437 strcmp(name, "glCompileShaderARB") == 0 ||
438 strcmp(name, "glGetShaderiv") == 0 ||
439 strcmp(name, "glGetShaderInfoLog") == 0) {
441 GLuint shader = call->arg(0).toUInt();
442 providef("shader-", shader, call->no);
446 if (strcmp(name, "glCreateProgram") == 0 ||
447 strcmp(name, "glCreateProgramObjectARB") == 0) {
449 GLuint program = call->ret->toUInt();
450 providef("program-", program, call->no);
454 if (strcmp(name, "glAttachShader") == 0 ||
455 strcmp(name, "glAttachObjectARB") == 0) {
457 GLuint program, shader;
458 std::stringstream ss_program, ss_shader;
460 program = call->arg(0).toUInt();
461 shader = call->arg(1).toUInt();
463 ss_program << "program-" << program;
464 ss_shader << "shader-" << shader;
466 link(ss_program.str(), ss_shader.str());
467 provide(ss_program.str(), call->no);
472 if (strcmp(name, "glDetachShader") == 0 ||
473 strcmp(name, "glDetachObjectARB") == 0) {
475 GLuint program, shader;
476 std::stringstream ss_program, ss_shader;
478 program = call->arg(0).toUInt();
479 shader = call->arg(1).toUInt();
481 ss_program << "program-" << program;
482 ss_shader << "shader-" << shader;
484 unlink(ss_program.str(), ss_shader.str());
489 if (strcmp(name, "glUseProgram") == 0 ||
490 strcmp(name, "glUseProgramObjectARB") == 0) {
494 program = call->arg(0).toUInt();
496 unlinkAll("render-program-state");
499 unlink("render-state", "render-program-state");
500 provide("state", call->no);
502 std::stringstream ss;
504 ss << "program-" << program;
506 link("render-state", "render-program-state");
507 link("render-program-state", ss.str());
509 provide(ss.str(), call->no);
515 if (strcmp(name, "glGetUniformLocation") == 0 ||
516 strcmp(name, "glGetUniformLocationARB") == 0 ||
517 strcmp(name, "glGetFragDataLocation") == 0 ||
518 strcmp(name, "glGetFragDataLocationEXT") == 0 ||
519 strcmp(name, "glGetSubroutineUniformLocation") == 0 ||
520 strcmp(name, "glGetProgramResourceLocation") == 0 ||
521 strcmp(name, "glGetProgramResourceLocationIndex") == 0 ||
522 strcmp(name, "glGetVaryingLocationNV") == 0) {
524 GLuint program = call->arg(0).toUInt();
526 providef("program-", program, call->no);
531 /* For any call that accepts 'location' as its first argument,
532 * perform a lookup in our location->program map and add a
533 * dependence on the program we find there. */
534 if (call->sig->num_args > 0 &&
535 strcmp(call->sig->arg_names[0], "location") == 0) {
537 providef("program-", activeProgram, call->no);
539 /* We can't easily tell if this uniform is being used to
540 * associate a sampler in the shader with a texture
541 * unit. The conservative option is to assume that it is
542 * and create a link from the active program to any bound
543 * textures for the given unit number.
545 * FIXME: We should be doing the same thing for calls to
547 if (strcmp(name, "glUniform1i") == 0 ||
548 strcmp(name, "glUniform1iARB") == 0) {
550 GLint max_unit = MAX(GL_MAX_TEXTURE_COORDS, GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS);
552 GLint unit = call->arg(1).toSInt();
553 std::stringstream ss_program;
554 std::stringstream ss_texture;
556 if (unit < max_unit) {
558 ss_program << "program-" << activeProgram;
560 ss_texture << "texture-unit-" << GL_TEXTURE0 + unit << "-target-";
562 /* We don't know what target(s) might get bound to
563 * this texture unit, so conservatively link to
564 * all. Only bound textures will actually get inserted
565 * into the output call stream. */
566 linkf(ss_program.str(), ss_texture.str(), GL_TEXTURE_1D);
567 linkf(ss_program.str(), ss_texture.str(), GL_TEXTURE_2D);
568 linkf(ss_program.str(), ss_texture.str(), GL_TEXTURE_3D);
569 linkf(ss_program.str(), ss_texture.str(), GL_TEXTURE_CUBE_MAP);
576 /* FIXME: We cut a huge swath by assuming that any unhandled
577 * call that has a first argument named "program" should not
578 * be included in the trimmed output unless the program of
579 * that number is also included.
581 * This heuristic is correct for many cases, but we should
582 * actually carefully verify if this includes some calls
583 * inappropriately, or if it misses some.
585 if (strcmp(name, "glLinkProgram") == 0 ||
586 strcmp(name, "glLinkProgramARB") == 0 ||
587 (call->sig->num_args > 0 &&
588 (strcmp(call->sig->arg_names[0], "program") == 0 ||
589 strcmp(call->sig->arg_names[0], "programObj") == 0))) {
591 GLuint program = call->arg(0).toUInt();
592 providef("program-", program, call->no);
596 /* No known shader-related side effects. Return false for more analysis. */
601 TraceAnalyzer::recordDrawingSideEffects(trace::Call *call, const char *name)
603 /* Handle all rendering operations, (even though only glEnd is
604 * flagged as a rendering operation we treat everything from
605 * glBegin through glEnd as a rendering operation). */
606 if (call->flags & trace::CALL_FLAG_RENDER ||
609 std::set<unsigned> calls;
610 std::set<unsigned>::iterator c;
612 provide("framebuffer", call->no);
614 calls = resolve("render-state");
616 for (c = calls.begin(); c != calls.end(); c++) {
617 provide("framebuffer", *c);
620 /* In some cases, rendering has side effects beyond the
621 * framebuffer update. */
622 if (renderingHasSideEffect()) {
623 provide("state", call->no);
624 for (c = calls.begin(); c != calls.end(); c++) {
625 provide("state", *c);
632 /* Though it's not flagged as a "RENDER" operation, we also want
633 * to trim swapbuffers calls when trimming drawing operations. */
634 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
635 call->flags & trace::CALL_FLAG_END_FRAME) {
639 /* No known drawing-related side effects. Return false for more analysis. */
644 TraceAnalyzer::recordSideEffects(trace::Call *call)
647 const char *name = call->name();
649 /* FIXME: If we encode the list of commands that are executed
650 * immediately (as opposed to those that are compiled into a
651 * display list) then we could generate a "display-list-X"
652 * resource just as we do for "texture-X" resources and only
653 * emit it in the trace if a glCallList(X) is emitted. For
654 * now, simply punt and include anything within glNewList and
655 * glEndList in the trim output. This guarantees that display
656 * lists will work, but does not trim out unused display
658 if (insideNewEndList != 0) {
659 provide("state", call->no);
661 /* Also, any texture bound inside a display list is
662 * conservatively considered required. */
663 if (strcmp(name, "glBindTexture") == 0) {
664 GLuint texture = call->arg(1).toUInt();
666 linkf("state", "texture-", texture);
672 if (trimFlags & TRIM_FLAG_NO_SIDE_EFFECTS) {
674 if (callHasNoSideEffects(call, name)) {
679 if (trimFlags & TRIM_FLAG_TEXTURES) {
681 if (recordTextureSideEffects(call, name)) {
686 if (trimFlags & TRIM_FLAG_SHADERS) {
688 if (recordShaderSideEffects(call, name)) {
693 if (trimFlags & TRIM_FLAG_DRAWING) {
695 if (recordDrawingSideEffects(call, name)) {
700 /* By default, assume this call affects the state somehow. */
701 resources["state"].insert(call->no);
705 TraceAnalyzer::requireDependencies(trace::Call *call)
708 /* Swap-buffers calls depend on framebuffer state. */
709 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
710 call->flags & trace::CALL_FLAG_END_FRAME) {
711 consume("framebuffer");
714 /* By default, just assume this call depends on generic state. */
718 TraceAnalyzer::TraceAnalyzer(TrimFlags trimFlagsOpt):
719 transformFeedbackActive(false),
720 framebufferObjectActive(false),
721 insideBeginEnd(false),
723 activeTextureUnit(GL_TEXTURE0),
724 trimFlags(trimFlagsOpt)
726 /* Nothing needed. */
729 TraceAnalyzer::~TraceAnalyzer()
731 /* Nothing needed. */
734 /* Analyze this call by tracking state and recording all the
735 * resources provided by this call as side effects.. */
737 TraceAnalyzer::analyze(trace::Call *call)
740 stateTrackPreCall(call);
742 recordSideEffects(call);
744 stateTrackPostCall(call);
747 /* Require this call and all of its dependencies to be included in
748 * the final trace. */
750 TraceAnalyzer::require(trace::Call *call)
753 /* First, find and insert all calls that this call depends on. */
754 requireDependencies(call);
756 /* Then insert this call itself. */
757 required.add(call->no);
760 /* Return a set of all the required calls, (both those calls added
761 * explicitly with require() and those implicitly depended
764 TraceAnalyzer::get_required(void)