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