]> git.tdb.fi Git - libs/gl.git/blob - source/programbuilder.h
Make it possible to customize texture sampling in standard shaders
[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
35                 StandardFeatures();
36
37                 std::string create_flags() const;
38         };
39
40         enum VariableScope
41         {
42                 NO_SCOPE,
43                 UNIFORM,
44                 ATTRIBUTE,
45                 VERTEX,
46                 FRAGMENT
47         };
48
49 private:
50         struct StandardVariable
51         {
52                 VariableScope scope;
53                 const char *name;
54                 const char *type;
55                 const char *expression;
56                 const char *flags;
57         };
58
59         struct ShaderVariable
60         {
61                 std::string name;
62                 const StandardVariable *variable;
63                 std::string resolved_name;
64                 bool fuzzy_space;
65                 std::string resolved_space;
66                 std::list<ShaderVariable *> referenced_vars;
67                 std::list<ShaderVariable *> referenced_by;
68                 bool inlined;
69                 bool inline_parens;
70
71                 ShaderVariable(const std::string &);
72
73                 void resolve(const StandardVariable &);
74                 void resolve(ShaderVariable &);
75                 void resolve_space(const std::string &);
76                 void add_reference(ShaderVariable &);
77                 void update_reference(ShaderVariable &, ShaderVariable &);
78                 void check_inline();
79                 bool is_referenced_from(VariableScope) const;
80                 std::string get_expression() const;
81         };
82
83         enum MatchLevel
84         {
85                 NO_MATCH,
86                 EXACT,
87                 FUZZY
88         };
89
90         StandardFeatures features;
91         std::string feature_flags;
92         bool optimize;
93
94         static const StandardVariable standard_variables[];
95
96 public:
97         ProgramBuilder(const StandardFeatures &);
98
99         void set_optimize(bool);
100         Program *create_program() const;
101         void add_shaders(Program &) const;
102 private:
103         std::string create_source(const std::list<ShaderVariable *> &, VariableScope) const;
104         bool evaluate_flags(const char *) const;
105         static MatchLevel name_match(const char *, const char *, const char ** = 0);
106         static bool parse_identifier(const char *, unsigned &, unsigned &);
107         static std::vector<std::string> extract_identifiers(const char *);
108         static std::string replace_identifiers(const char *, const std::map<std::string, std::string> &);
109 };
110
111 } // namespace GL
112 } // namespace Msp
113
114 #endif