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++) {
165 required.insert(*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::recordSideEffects(trace::Call *call)
270 const char *name = call->name();
272 /* Handle display lists before any other processing. */
274 /* FIXME: If we encode the list of commands that are executed
275 * immediately (as opposed to those that are compiled into a
276 * display list) then we could generate a "display-list-X"
277 * resource just as we do for "texture-X" resources and only
278 * emit it in the trace if a glCallList(X) is emitted. For
279 * now, simply punt and include anything within glNewList and
280 * glEndList in the trim output. This guarantees that display
281 * lists will work, but does not trim out unused display
283 if (insideNewEndList != 0) {
284 provide("state", call->no);
286 /* Also, any texture bound inside a display list is
287 * conservatively considered required. */
288 if (strcmp(name, "glBindTexture") == 0) {
289 GLuint texture = call->arg(1).toUInt();
291 linkf("state", "texture-", texture);
297 /* If call is flagged as no side effects, then we are done here. */
298 if (call->flags & trace::CALL_FLAG_NO_SIDE_EFFECTS) {
302 /* Similarly, swap-buffers calls don't have interesting side effects. */
303 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
304 call->flags & trace::CALL_FLAG_END_FRAME) {
308 if (strcmp(name, "glGenTextures") == 0) {
309 const trace::Array *textures = dynamic_cast<const trace::Array *>(&call->arg(1));
314 for (i = 0; i < textures->size(); i++) {
315 texture = textures->values[i]->toUInt();
316 providef("texture-", texture, call->no);
322 /* FIXME: When we start tracking framebuffer objects as their own
323 * resources, we will want to link the FBO to the given texture
324 * resource, (and to this call). For now, just link render state
325 * to the texture, and force this call to be required. */
326 if (strcmp(name, "glFramebufferTexture2D") == 0) {
329 texture = call->arg(3).toUInt();
331 linkf("render-state", "texture-", texture);
333 required.insert(call->no);
336 if (strcmp(name, "glBindTexture") == 0) {
340 std::stringstream ss_target, ss_texture;
342 target = static_cast<GLenum>(call->arg(0).toSInt());
343 texture = call->arg(1).toUInt();
345 ss_target << "texture-unit-" << activeTextureUnit << "-target-" << target;
346 ss_texture << "texture-" << texture;
348 resources.erase(ss_target.str());
349 provide(ss_target.str(), call->no);
351 unlinkAll(ss_target.str());
352 link(ss_target.str(), ss_texture.str());
354 /* FIXME: This really shouldn't be necessary. The effect
355 * this provide() has is that all glBindTexture calls will
356 * be preserved in the output trace (never trimmed). Carl
357 * has a trace ("btr") where a glBindTexture call should
358 * not be necessary at all, (it's immediately followed
359 * with a glBindTexture to a different texture and no
360 * intervening texture-related calls), yet this 'provide'
361 * makes the difference between a trim_stress test failing
364 * More investigation is necessary, but for now, be
365 * conservative and don't trim. */
366 provide("state", call->no);
371 /* FIXME: Need to handle glMultTetImage and friends. */
372 if (STRNCMP_LITERAL(name, "glTexImage") == 0 ||
373 STRNCMP_LITERAL(name, "glTexSubImage") == 0 ||
374 STRNCMP_LITERAL(name, "glCopyTexImage") == 0 ||
375 STRNCMP_LITERAL(name, "glCopyTexSubImage") == 0 ||
376 STRNCMP_LITERAL(name, "glCompressedTexImage") == 0 ||
377 STRNCMP_LITERAL(name, "glCompressedTexSubImage") == 0 ||
378 strcmp(name, "glInvalidateTexImage") == 0 ||
379 strcmp(name, "glInvalidateTexSubImage") == 0) {
381 std::set<unsigned> *calls;
382 std::set<unsigned>::iterator c;
383 std::stringstream ss_target, ss_texture;
385 GLenum target = static_cast<GLenum>(call->arg(0).toSInt());
387 ss_target << "texture-unit-" << activeTextureUnit << "-target-" << target;
388 ss_texture << "texture-" << texture_map[target];
390 /* The texture resource depends on this call and any calls
391 * providing the given texture target. */
392 provide(ss_texture.str(), call->no);
394 if (resources.count(ss_target.str())) {
395 calls = &resources[ss_target.str()];
396 for (c = calls->begin(); c != calls->end(); c++) {
397 provide(ss_texture.str(), *c);
404 if (strcmp(name, "glEnable") == 0) {
407 cap = static_cast<GLenum>(call->arg(0).toSInt());
409 if (cap == GL_TEXTURE_1D ||
410 cap == GL_TEXTURE_2D ||
411 cap == GL_TEXTURE_3D ||
412 cap == GL_TEXTURE_CUBE_MAP)
414 std::stringstream ss;
416 ss << "texture-unit-" << activeTextureUnit << "-target-" << cap;
418 link("render-state", ss.str());
421 provide("state", call->no);
425 if (strcmp(name, "glDisable") == 0) {
428 cap = static_cast<GLenum>(call->arg(0).toSInt());
430 if (cap == GL_TEXTURE_1D ||
431 cap == GL_TEXTURE_2D ||
432 cap == GL_TEXTURE_3D ||
433 cap == GL_TEXTURE_CUBE_MAP)
435 std::stringstream ss;
437 ss << "texture-unit-" << activeTextureUnit << "-target-" << cap;
439 unlink("render-state", ss.str());
442 provide("state", call->no);
446 if (strcmp(name, "glCreateShader") == 0 ||
447 strcmp(name, "glCreateShaderObjectARB") == 0) {
449 GLuint shader = call->ret->toUInt();
450 providef("shader-", shader, call->no);
454 if (strcmp(name, "glShaderSource") == 0 ||
455 strcmp(name, "glShaderSourceARB") == 0 ||
456 strcmp(name, "glCompileShader") == 0 ||
457 strcmp(name, "glCompileShaderARB") == 0 ||
458 strcmp(name, "glGetShaderiv") == 0 ||
459 strcmp(name, "glGetShaderInfoLog") == 0) {
461 GLuint shader = call->arg(0).toUInt();
462 providef("shader-", shader, call->no);
466 if (strcmp(name, "glCreateProgram") == 0 ||
467 strcmp(name, "glCreateProgramObjectARB") == 0) {
469 GLuint program = call->ret->toUInt();
470 providef("program-", program, call->no);
474 if (strcmp(name, "glAttachShader") == 0 ||
475 strcmp(name, "glAttachObjectARB") == 0) {
477 GLuint program, shader;
478 std::stringstream ss_program, ss_shader;
480 program = call->arg(0).toUInt();
481 shader = call->arg(1).toUInt();
483 ss_program << "program-" << program;
484 ss_shader << "shader-" << shader;
486 link(ss_program.str(), ss_shader.str());
487 provide(ss_program.str(), call->no);
492 if (strcmp(name, "glDetachShader") == 0 ||
493 strcmp(name, "glDetachObjectARB") == 0) {
495 GLuint program, shader;
496 std::stringstream ss_program, ss_shader;
498 program = call->arg(0).toUInt();
499 shader = call->arg(1).toUInt();
501 ss_program << "program-" << program;
502 ss_shader << "shader-" << shader;
504 unlink(ss_program.str(), ss_shader.str());
509 if (strcmp(name, "glUseProgram") == 0 ||
510 strcmp(name, "glUseProgramObjectARB") == 0) {
514 program = call->arg(0).toUInt();
516 unlinkAll("render-program-state");
519 unlink("render-state", "render-program-state");
520 provide("state", call->no);
522 std::stringstream ss;
524 ss << "program-" << program;
526 link("render-state", "render-program-state");
527 link("render-program-state", ss.str());
529 provide(ss.str(), call->no);
535 if (strcmp(name, "glGetUniformLocation") == 0 ||
536 strcmp(name, "glGetUniformLocationARB") == 0 ||
537 strcmp(name, "glGetFragDataLocation") == 0 ||
538 strcmp(name, "glGetFragDataLocationEXT") == 0 ||
539 strcmp(name, "glGetSubroutineUniformLocation") == 0 ||
540 strcmp(name, "glGetProgramResourceLocation") == 0 ||
541 strcmp(name, "glGetProgramResourceLocationIndex") == 0 ||
542 strcmp(name, "glGetVaryingLocationNV") == 0) {
544 GLuint program = call->arg(0).toUInt();
546 providef("program-", program, call->no);
551 /* For any call that accepts 'location' as its first argument,
552 * perform a lookup in our location->program map and add a
553 * dependence on the program we find there. */
554 if (call->sig->num_args > 0 &&
555 strcmp(call->sig->arg_names[0], "location") == 0) {
557 providef("program-", activeProgram, call->no);
559 /* We can't easily tell if this uniform is being used to
560 * associate a sampler in the shader with a texture
561 * unit. The conservative option is to assume that it is
562 * and create a link from the active program to any bound
563 * textures for the given unit number.
565 * FIXME: We should be doing the same thing for calls to
567 if (strcmp(name, "glUniform1i") == 0 ||
568 strcmp(name, "glUniform1iARB") == 0) {
570 GLint max_unit = MAX(GL_MAX_TEXTURE_COORDS, GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS);
572 GLint unit = call->arg(1).toSInt();
573 std::stringstream ss_program;
574 std::stringstream ss_texture;
576 if (unit < max_unit) {
578 ss_program << "program-" << activeProgram;
580 ss_texture << "texture-unit-" << GL_TEXTURE0 + unit << "-target-";
582 /* We don't know what target(s) might get bound to
583 * this texture unit, so conservatively link to
584 * all. Only bound textures will actually get inserted
585 * into the output call stream. */
586 linkf(ss_program.str(), ss_texture.str(), GL_TEXTURE_1D);
587 linkf(ss_program.str(), ss_texture.str(), GL_TEXTURE_2D);
588 linkf(ss_program.str(), ss_texture.str(), GL_TEXTURE_3D);
589 linkf(ss_program.str(), ss_texture.str(), GL_TEXTURE_CUBE_MAP);
596 /* FIXME: We cut a huge swath by assuming that any unhandled
597 * call that has a first argument named "program" should not
598 * be included in the trimmed output unless the program of
599 * that number is also included.
601 * This heuristic is correct for many cases, but we should
602 * actually carefully verify if this includes some calls
603 * inappropriately, or if it misses some.
605 if (strcmp(name, "glLinkProgram") == 0 ||
606 strcmp(name, "glLinkProgramARB") == 0 ||
607 (call->sig->num_args > 0 &&
608 (strcmp(call->sig->arg_names[0], "program") == 0 ||
609 strcmp(call->sig->arg_names[0], "programObj") == 0))) {
611 GLuint program = call->arg(0).toUInt();
612 providef("program-", program, call->no);
616 /* Handle all rendering operations, (even though only glEnd is
617 * flagged as a rendering operation we treat everything from
618 * glBegin through glEnd as a rendering operation). */
619 if (call->flags & trace::CALL_FLAG_RENDER ||
622 std::set<unsigned> calls;
623 std::set<unsigned>::iterator c;
625 provide("framebuffer", call->no);
627 calls = resolve("render-state");
629 for (c = calls.begin(); c != calls.end(); c++) {
630 provide("framebuffer", *c);
633 /* In some cases, rendering has side effects beyond the
634 * framebuffer update. */
635 if (renderingHasSideEffect()) {
636 provide("state", call->no);
637 for (c = calls.begin(); c != calls.end(); c++) {
638 provide("state", *c);
645 /* By default, assume this call affects the state somehow. */
646 resources["state"].insert(call->no);
650 TraceAnalyzer::requireDependencies(trace::Call *call)
653 /* Swap-buffers calls depend on framebuffer state. */
654 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
655 call->flags & trace::CALL_FLAG_END_FRAME) {
656 consume("framebuffer");
659 /* By default, just assume this call depends on generic state. */
663 TraceAnalyzer::TraceAnalyzer(): transformFeedbackActive(false),
664 framebufferObjectActive(false),
665 insideBeginEnd(false),
667 activeTextureUnit(GL_TEXTURE0)
669 /* Nothing needed. */
672 TraceAnalyzer::~TraceAnalyzer()
674 /* Nothing needed. */
677 /* Analyze this call by tracking state and recording all the
678 * resources provided by this call as side effects.. */
680 TraceAnalyzer::analyze(trace::Call *call)
683 stateTrackPreCall(call);
685 recordSideEffects(call);
687 stateTrackPostCall(call);
690 /* Require this call and all of its dependencies to be included in
691 * the final trace. */
693 TraceAnalyzer::require(trace::Call *call)
696 /* First, find and insert all calls that this call depends on. */
697 requireDependencies(call);
699 /* Then insert this call itself. */
700 required.insert(call->no);
703 /* Return a set of all the required calls, (both those calls added
704 * explicitly with require() and those implicitly depended
707 TraceAnalyzer::get_required(void)