]> git.tdb.fi Git - libs/gl.git/blob - source/programbuilder.h
Support generating modern, GLSL 1.30+ shaders with ProgramBuilder
[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 ProgramBuilder
15 {
16 public:
17         struct StandardFeatures
18         {
19                 class Loader: public DataFile::ObjectLoader<StandardFeatures>
20                 {
21                 public:
22                         Loader(StandardFeatures &);
23                 };
24
25                 bool texture;
26                 bool material;
27                 bool lighting;
28                 bool specular;
29                 bool normalmap;
30                 bool shadow;
31                 bool reflection;
32                 bool transform;
33                 bool colorify;
34                 bool legacy;
35
36                 StandardFeatures();
37
38                 std::string create_flags() const;
39         };
40
41         enum VariableScope
42         {
43                 NO_SCOPE,
44                 UNIFORM,
45                 ATTRIBUTE,
46                 VERTEX,
47                 FRAGMENT
48         };
49
50 private:
51         struct StandardVariable
52         {
53                 VariableScope scope;
54                 const char *name;
55                 const char *type;
56                 const char *expression;
57                 const char *flags;
58         };
59
60         struct ShaderVariable
61         {
62                 std::string name;
63                 const StandardVariable *variable;
64                 std::string resolved_name;
65                 bool fuzzy_space;
66                 std::string resolved_space;
67                 std::list<ShaderVariable *> referenced_vars;
68                 std::list<ShaderVariable *> referenced_by;
69                 bool inlined;
70                 bool inline_parens;
71
72                 ShaderVariable(const std::string &);
73
74                 void resolve(const StandardVariable &);
75                 void resolve(ShaderVariable &);
76                 void resolve_space(const std::string &);
77                 void add_reference(ShaderVariable &);
78                 void update_reference(ShaderVariable &, ShaderVariable &);
79                 void check_inline(bool);
80                 bool is_referenced_from(VariableScope) const;
81                 std::string get_expression() const;
82         };
83
84         enum MatchLevel
85         {
86                 NO_MATCH,
87                 EXACT,
88                 FUZZY
89         };
90
91         StandardFeatures features;
92         std::string feature_flags;
93         bool optimize;
94
95         static const StandardVariable standard_variables[];
96
97 public:
98         ProgramBuilder(const StandardFeatures &);
99
100         void set_optimize(bool);
101         Program *create_program() const;
102         void add_shaders(Program &) const;
103 private:
104         std::string create_source(const std::list<ShaderVariable *> &, VariableScope) const;
105         bool evaluate_flags(const char *) const;
106         static MatchLevel name_match(const char *, const char *, const char ** = 0);
107         static bool parse_identifier(const char *, unsigned &, unsigned &);
108         static std::vector<std::string> extract_identifiers(const char *);
109         static std::string replace_identifiers(const char *, const std::map<std::string, std::string> &);
110 };
111
112 } // namespace GL
113 } // namespace Msp
114
115 #endif