summaryrefslogtreecommitdiff
path: root/cli/trace_analyzer.hpp
blob: 3c1d3f7d687f9decd36184e2ecc667cdaea8cc9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**************************************************************************
 * Copyright 2012 Intel corporation
 *
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 **************************************************************************/

#include <set>

#include <GL/gl.h>
#include <GL/glext.h>

#include "trace_callset.hpp"
#include "trim_callset.hpp"
#include "trace_parser.hpp"

typedef unsigned TrimFlags;

/**
 * Trim flags
 */
enum {

    /* Whether to trim calls that have no side effects. */
    TRIM_FLAG_NO_SIDE_EFFECTS		= (1 << 0),

    /* Whether to trim calls to setup textures that are never used. */
    TRIM_FLAG_TEXTURES		        = (1 << 1),

    /* Whether to trim calls to setup shaders that are never used. */
    TRIM_FLAG_SHADERS			= (1 << 2),

    /* Whether to trim drawing operations outside of the desired call-set. */
    TRIM_FLAG_DRAWING			= (1 << 3),
};

class TraceAnalyzer {
private:
    std::map<std::string, std::set<unsigned> > resources;
    std::map<std::string, std::set<std::string> > dependencies;

    std::map<GLenum, unsigned> texture_map;

    trim::CallSet required;

    bool transformFeedbackActive;
    bool framebufferObjectActive;
    bool insideBeginEnd;
    GLuint insideNewEndList;
    GLenum activeTextureUnit;
    GLuint activeProgram;
    unsigned int trimFlags;

    void provide(std::string resource, trace::CallNo call_no);
    void providef(std::string resource, int resource_no, trace::CallNo call_no);

    void link(std::string resource, std::string dependency);
    void linkf(std::string resource, std::string dependency, int dep_no);

    void unlink(std::string resource, std::string dependency);
    void unlinkf(std::string resource, std::string dependency, int dep_no);
    void unlinkAll(std::string resource);

    void stateTrackPreCall(trace::Call *call);

    void recordSideEffects(trace::Call *call);
    bool callHasNoSideEffects(trace::Call *call, const char *name);
    bool recordTextureSideEffects(trace::Call *call, const char *name);
    bool recordShaderSideEffects(trace::Call *call, const char *name);
    bool recordDrawingSideEffects(trace::Call *call, const char *name);

    void stateTrackPostCall(trace::Call *call);

    bool renderingHasSideEffect(void);
    std::set<unsigned> resolve(std::string resource);

    void consume(std::string resource);
    void requireDependencies(trace::Call *call);

public:
    TraceAnalyzer(TrimFlags trimFlags = -1);
    ~TraceAnalyzer();

    /* Analyze this call by tracking state and recording all the
     * resources provided by this call as side effects.. */
    void analyze(trace::Call *call);

    /* Require this call and all of its dependencies to be included in
     * the final trace. */
    void require(trace::Call *call);

    /* Return a set of all the required calls, (both those calls added
     * explicitly with require() and those implicitly depended
     * upon. */
    trim::CallSet *get_required(void);
};