]> git.tdb.fi Git - libs/gl.git/blob - source/programbuilder.h
Shader optimization
[libs/gl.git] / source / programbuilder.h
1 #ifndef MSP_GL_PROGRAMBUILDER_H_
2 #define MSP_GL_PROGRAMBUILDER_H_
3
4 #include <map>
5 #include <string>
6 #include <vector>
7 #include <msp/datafile/objectloader.h>
8
9 namespace Msp {
10 namespace GL {
11
12 class Program;
13
14 class ProgramBuilder
15 {
16 public:
17         struct StandardFeatures
18         {
19                 class Loader: public DataFile::ObjectLoader<StandardFeatures>
20                 {
21                 public:
22                         Loader(StandardFeatures &);
23                 };
24
25                 bool texture;
26                 bool material;
27                 bool lighting;
28                 bool specular;
29                 bool normalmap;
30                 bool shadow;
31                 bool reflection;
32                 bool transform;
33
34                 StandardFeatures();
35
36                 std::string create_flags() const;
37         };
38
39         enum VariableScope
40         {
41                 NO_SCOPE,
42                 UNIFORM,
43                 ATTRIBUTE,
44                 VERTEX,
45                 FRAGMENT
46         };
47
48 private:
49         struct StandardVariable
50         {
51                 VariableScope scope;
52                 const char *name;
53                 const char *type;
54                 const char *expression;
55                 const char *flags;
56         };
57
58         struct ShaderVariable
59         {
60                 std::string name;
61                 const StandardVariable *variable;
62                 std::string resolved_name;
63                 bool fuzzy_space;
64                 std::string resolved_space;
65                 std::list<ShaderVariable *> referenced_vars;
66                 std::list<ShaderVariable *> referenced_by;
67                 bool inlined;
68                 bool inline_parens;
69
70                 ShaderVariable(const std::string &);
71
72                 void resolve(const StandardVariable &);
73                 void resolve(ShaderVariable &);
74                 void resolve_space(const std::string &);
75                 void add_reference(ShaderVariable &);
76                 void update_reference(ShaderVariable &, ShaderVariable &);
77                 void check_inline();
78                 bool is_referenced_from(VariableScope) const;
79                 std::string get_expression() const;
80         };
81
82         enum MatchLevel
83         {
84                 NO_MATCH,
85                 EXACT,
86                 FUZZY
87         };
88
89         StandardFeatures features;
90         std::string feature_flags;
91         bool optimize;
92
93         static const StandardVariable standard_variables[];
94
95 public:
96         ProgramBuilder(const StandardFeatures &);
97
98         void set_optimize(bool);
99         Program *create_program() const;
100         void add_shaders(Program &) const;
101 private:
102         std::string create_source(const std::list<ShaderVariable *> &, VariableScope) const;
103         bool evaluate_flags(const char *) const;
104         static MatchLevel name_match(const char *, const char *, const char ** = 0);
105         static bool parse_identifier(const char *, unsigned &, unsigned &);
106         static std::vector<std::string> extract_identifiers(const char *);
107         static std::string replace_identifiers(const char *, const std::map<std::string, std::string> &);
108 };
109
110 } // namespace GL
111 } // namespace Msp
112
113 #endif