]> git.tdb.fi Git - libs/gl.git/blob - source/programbuilder.h
The GLSL texture function returns a float for shadow samplers
[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                 std::string resolved_block;
91                 bool fuzzy_space;
92                 std::string resolved_space;
93                 bool array_sum;
94                 std::string array_subscript;
95                 unsigned array_size;
96                 std::list<ShaderVariable *> referenced_vars;
97                 std::list<ShaderVariable *> referenced_by;
98                 bool inlined;
99                 bool inline_parens;
100                 bool in_loop;
101
102                 ShaderVariable(const std::string &);
103
104                 void resolve(const VariableDefinition &);
105                 void resolve(ShaderVariable &);
106                 void resolve_type(const VariableDefinition &);
107                 void resolve_space(const std::string &);
108                 void resolve_array(const StandardFeatures &, unsigned = 0);
109                 void add_reference(ShaderVariable &);
110                 void update_reference(ShaderVariable &, ShaderVariable &);
111                 void check_inline(bool, bool);
112                 bool is_referenced_from(VariableScope) const;
113                 InterfaceFlags get_interface_flags(VariableScope) const;
114                 std::string create_type_declaration() const;
115                 std::string create_declaration(char = 0, bool = false) const;
116                 std::string create_replacement(VariableScope, const char * = 0) const;
117                 std::string create_expression(const char * = 0) const;
118         };
119
120         enum MatchType
121         {
122                 NO_MATCH,
123                 EXACT,
124                 FUZZY,
125                 ARRAY
126         };
127
128         StandardFeatures features;
129         std::list<VariableDefinition> custom_variables;
130         std::string feature_flags;
131         std::map<std::string, std::string> aliases;
132         bool optimize;
133
134         static const VariableDefinition standard_variables[];
135         static const char interfaces[];
136
137 public:
138         ProgramBuilder(const StandardFeatures &);
139
140         void set_optimize(bool);
141         Program *create_program() const;
142         void add_shaders(Program &) const;
143 private:
144         std::string create_source(const std::list<ShaderVariable *> &, VariableScope) const;
145         bool evaluate_flags(const char *) const;
146         static const char *unqualified_name(const char *);
147         static MatchType name_match(const char *, const char *, const char ** = 0);
148         static bool parse_identifier(const char *, unsigned &, unsigned &);
149         static std::vector<std::string> extract_identifiers(const char *);
150         static std::string replace_identifiers(const char *, const std::map<std::string, std::string> &, bool = false);
151         std::string create_expression(const ShaderVariable &, const char * = 0) const;
152 };
153
154 } // namespace GL
155 } // namespace Msp
156
157 #endif