]> git.tdb.fi Git - libs/gl.git/blob - source/programbuilder.h
Emit separate declarations for struct types
[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                 TYPE,
60                 UNIFORM,
61                 ATTRIBUTE,
62                 VERTEX,
63                 FRAGMENT
64         };
65
66         enum InterfaceFlags
67         {
68                 NO_INTERFACE = 0,
69                 INPUT = 1,
70                 OUTPUT = 2,
71                 PASSTHROUGH = INPUT|OUTPUT,
72                 GOAL = 4
73         };
74
75         struct VariableDefinition
76         {
77                 VariableScope scope;
78                 const char *name;
79                 const char *type;
80                 const char *expression;
81                 const char *flags;
82         };
83
84         struct ShaderVariable
85         {
86                 std::string name;
87                 const VariableDefinition *variable;
88                 const VariableDefinition *type;
89                 std::string resolved_name;
90                 bool fuzzy_space;
91                 std::string resolved_space;
92                 bool array_sum;
93                 std::string array_subscript;
94                 unsigned array_size;
95                 std::list<ShaderVariable *> referenced_vars;
96                 std::list<ShaderVariable *> referenced_by;
97                 bool inlined;
98                 bool inline_parens;
99                 bool in_loop;
100
101                 ShaderVariable(const std::string &);
102
103                 void resolve(const VariableDefinition &);
104                 void resolve(ShaderVariable &);
105                 void resolve_type(const VariableDefinition &);
106                 void resolve_space(const std::string &);
107                 void resolve_array(const StandardFeatures &, unsigned = 0);
108                 void add_reference(ShaderVariable &);
109                 void update_reference(ShaderVariable &, ShaderVariable &);
110                 void check_inline(bool, bool);
111                 bool is_referenced_from(VariableScope) const;
112                 InterfaceFlags get_interface_flags(VariableScope) const;
113                 std::string create_type_declaration() const;
114                 std::string create_declaration(char = 0, bool = false) const;
115                 std::string create_replacement(VariableScope, const char * = 0) const;
116                 std::string create_expression(const char * = 0) const;
117         };
118
119         enum MatchType
120         {
121                 NO_MATCH,
122                 EXACT,
123                 FUZZY,
124                 ARRAY
125         };
126
127         StandardFeatures features;
128         std::list<VariableDefinition> custom_variables;
129         std::string feature_flags;
130         bool optimize;
131
132         static const VariableDefinition standard_variables[];
133         static const char interfaces[];
134
135 public:
136         ProgramBuilder(const StandardFeatures &);
137
138         void set_optimize(bool);
139         Program *create_program() const;
140         void add_shaders(Program &) const;
141 private:
142         std::string create_source(const std::list<ShaderVariable *> &, VariableScope) const;
143         bool evaluate_flags(const char *) const;
144         static MatchType name_match(const char *, const char *, const char ** = 0);
145         static bool parse_identifier(const char *, unsigned &, unsigned &);
146         static std::vector<std::string> extract_identifiers(const char *);
147         static std::string replace_identifiers(const char *, const std::map<std::string, std::string> &);
148 };
149
150 } // namespace GL
151 } // namespace Msp
152
153 #endif