]> git.tdb.fi Git - libs/gl.git/blob - source/programbuilder.h
Add support for array and multiple lights in 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                 unsigned max_lights;
36                 bool specular;
37                 bool normalmap;
38                 bool shadow;
39                 bool reflection;
40                 bool legacy;
41                 std::string custom;
42
43                 StandardFeatures();
44
45                 std::string create_flags() const;
46         };
47
48 private:
49         enum VariableScope
50         {
51                 NO_SCOPE,
52                 UNIFORM,
53                 ATTRIBUTE,
54                 VERTEX,
55                 FRAGMENT
56         };
57
58         enum InterfaceFlags
59         {
60                 NO_INTERFACE = 0,
61                 INPUT = 1,
62                 OUTPUT = 2,
63                 PASSTHROUGH = INPUT|OUTPUT,
64                 GOAL = 4
65         };
66
67         struct VariableDefinition
68         {
69                 VariableScope scope;
70                 const char *name;
71                 const char *type;
72                 const char *expression;
73                 const char *flags;
74         };
75
76         struct ShaderVariable
77         {
78                 std::string name;
79                 const VariableDefinition *variable;
80                 std::string resolved_name;
81                 bool fuzzy_space;
82                 std::string resolved_space;
83                 bool array_sum;
84                 std::string array_subscript;
85                 unsigned array_size;
86                 std::list<ShaderVariable *> referenced_vars;
87                 std::list<ShaderVariable *> referenced_by;
88                 bool inlined;
89                 bool inline_parens;
90                 bool in_loop;
91
92                 ShaderVariable(const std::string &);
93
94                 void resolve(const VariableDefinition &);
95                 void resolve(ShaderVariable &);
96                 void resolve_space(const std::string &);
97                 void resolve_array(const StandardFeatures &, unsigned = 0);
98                 void add_reference(ShaderVariable &);
99                 void update_reference(ShaderVariable &, ShaderVariable &);
100                 void check_inline(bool, bool);
101                 bool is_referenced_from(VariableScope) const;
102                 InterfaceFlags get_interface_flags(VariableScope) const;
103                 std::string create_declaration(char = 0, bool = false) const;
104                 std::string create_replacement(VariableScope, const char * = 0) const;
105                 std::string create_expression(const char * = 0) const;
106         };
107
108         enum MatchType
109         {
110                 NO_MATCH,
111                 EXACT,
112                 FUZZY,
113                 ARRAY
114         };
115
116         StandardFeatures features;
117         std::list<VariableDefinition> custom_variables;
118         std::string feature_flags;
119         bool optimize;
120
121         static const VariableDefinition standard_variables[];
122         static const char interfaces[];
123
124 public:
125         ProgramBuilder(const StandardFeatures &);
126
127         void set_optimize(bool);
128         Program *create_program() const;
129         void add_shaders(Program &) const;
130 private:
131         std::string create_source(const std::list<ShaderVariable *> &, VariableScope) const;
132         bool evaluate_flags(const char *) const;
133         static MatchType name_match(const char *, const char *, const char ** = 0);
134         static bool parse_identifier(const char *, unsigned &, unsigned &);
135         static std::vector<std::string> extract_identifiers(const char *);
136         static std::string replace_identifiers(const char *, const std::map<std::string, std::string> &);
137 };
138
139 } // namespace GL
140 } // namespace Msp
141
142 #endif