]> git.tdb.fi Git - libs/gl.git/blob - source/programbuilder.h
Refactor certain variable operations into functions
[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 invalid_variable_definition: public std::invalid_argument
15 {
16 public:
17         invalid_variable_definition(const std::string &w): std::invalid_argument(w) { }
18         virtual ~invalid_variable_definition() throw() { }
19 };
20
21 class ProgramBuilder
22 {
23 public:
24         struct StandardFeatures
25         {
26                 class Loader: public DataFile::ObjectLoader<StandardFeatures>
27                 {
28                 public:
29                         Loader(StandardFeatures &);
30                 };
31
32                 bool texture;
33                 bool material;
34                 bool lighting;
35                 bool specular;
36                 bool normalmap;
37                 bool shadow;
38                 bool reflection;
39                 bool legacy;
40                 std::string custom;
41
42                 StandardFeatures();
43
44                 std::string create_flags() const;
45         };
46
47 private:
48         enum VariableScope
49         {
50                 NO_SCOPE,
51                 UNIFORM,
52                 ATTRIBUTE,
53                 VERTEX,
54                 FRAGMENT
55         };
56
57         struct VariableDefinition
58         {
59                 VariableScope scope;
60                 const char *name;
61                 const char *type;
62                 const char *expression;
63                 const char *flags;
64         };
65
66         struct ShaderVariable
67         {
68                 std::string name;
69                 const VariableDefinition *variable;
70                 std::string resolved_name;
71                 bool fuzzy_space;
72                 std::string resolved_space;
73                 std::list<ShaderVariable *> referenced_vars;
74                 std::list<ShaderVariable *> referenced_by;
75                 bool inlined;
76                 bool inline_parens;
77
78                 ShaderVariable(const std::string &);
79
80                 void resolve(const VariableDefinition &);
81                 void resolve(ShaderVariable &);
82                 void resolve_space(const std::string &);
83                 void add_reference(ShaderVariable &);
84                 void update_reference(ShaderVariable &, ShaderVariable &);
85                 void check_inline(bool, bool);
86                 bool is_referenced_from(VariableScope) const;
87                 std::string create_declaration(char = 0) const;
88                 std::string create_replacement(VariableScope) const;
89                 std::string create_expression() const;
90         };
91
92         enum MatchLevel
93         {
94                 NO_MATCH,
95                 EXACT,
96                 FUZZY
97         };
98
99         StandardFeatures features;
100         std::list<VariableDefinition> custom_variables;
101         std::string feature_flags;
102         bool optimize;
103
104         static const VariableDefinition standard_variables[];
105
106 public:
107         ProgramBuilder(const StandardFeatures &);
108
109         void set_optimize(bool);
110         Program *create_program() const;
111         void add_shaders(Program &) const;
112 private:
113         std::string create_source(const std::list<ShaderVariable *> &, VariableScope) const;
114         bool evaluate_flags(const char *) const;
115         static MatchLevel name_match(const char *, const char *, const char ** = 0);
116         static bool parse_identifier(const char *, unsigned &, unsigned &);
117         static std::vector<std::string> extract_identifiers(const char *);
118         static std::string replace_identifiers(const char *, const std::map<std::string, std::string> &);
119 };
120
121 } // namespace GL
122 } // namespace Msp
123
124 #endif