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