]> git.tdb.fi Git - libs/gl.git/blob - source/programbuilder.h
More flexible system for customizing generated shaders
[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);
86                 bool is_referenced_from(VariableScope) const;
87                 std::string get_expression() const;
88         };
89
90         enum MatchLevel
91         {
92                 NO_MATCH,
93                 EXACT,
94                 FUZZY
95         };
96
97         StandardFeatures features;
98         std::list<VariableDefinition> custom_variables;
99         std::string feature_flags;
100         bool optimize;
101
102         static const VariableDefinition standard_variables[];
103
104 public:
105         ProgramBuilder(const StandardFeatures &);
106
107         void set_optimize(bool);
108         Program *create_program() const;
109         void add_shaders(Program &) const;
110 private:
111         std::string create_source(const std::list<ShaderVariable *> &, VariableScope) const;
112         bool evaluate_flags(const char *) const;
113         static MatchLevel name_match(const char *, const char *, const char ** = 0);
114         static bool parse_identifier(const char *, unsigned &, unsigned &);
115         static std::vector<std::string> extract_identifiers(const char *);
116         static std::string replace_identifiers(const char *, const std::map<std::string, std::string> &);
117 };
118
119 } // namespace GL
120 } // namespace Msp
121
122 #endif