]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/generate.h
Support specialization constants in the GLSL compiler
[libs/gl.git] / source / glsl / generate.h
1 #ifndef MSP_GL_SL_GENERATE_H_
2 #define MSP_GL_SL_GENERATE_H_
3
4 #include <map>
5 #include <set>
6 #include <string>
7 #include <vector>
8 #include "visitor.h"
9
10 namespace Msp {
11 namespace GL {
12 namespace SL {
13
14 class DeclarationCombiner: private TraversingVisitor
15 {
16 private:
17         std::map<std::string, std::vector<FunctionDeclaration *> > functions;
18         std::map<std::string, VariableDeclaration *> variables;
19         std::set<Node *> nodes_to_remove;
20
21 public:
22         void apply(Stage &);
23
24 private:
25         virtual void visit(Block &);
26         virtual void visit(FunctionDeclaration &);
27         virtual void visit(VariableDeclaration &);
28 };
29
30 class ConstantSpecializer: private TraversingVisitor
31 {
32 private:
33         const std::map<std::string, int> *values;
34
35 public:
36         ConstantSpecializer();
37
38         void apply(Stage &, const std::map<std::string, int> *);
39
40 private:
41         virtual void visit(VariableDeclaration &);
42 };
43
44 class BlockResolver: private TraversingVisitor
45 {
46 public:
47         void apply(Stage &s) { s.content.visit(*this); }
48
49 private:
50         virtual void enter(Block &);
51 };
52
53 class VariableResolver: private TraversingVisitor
54 {
55 private:
56         Stage *stage;
57         Block *builtins;
58         std::map<std::string, VariableDeclaration *> *members;
59         RefPtr<InterfaceBlockReference> iface_ref;
60         std::string block_interface;
61         bool record_target;
62         VariableDeclaration *assignment_target;
63         bool self_referencing;
64
65 public:
66         VariableResolver();
67
68         void apply(Stage &);
69
70 private:
71         Block *next_block(Block &);
72
73         virtual void enter(Block &);
74         virtual void visit(VariableReference &);
75         virtual void visit(InterfaceBlockReference &);
76         virtual void visit(MemberAccess &);
77         virtual void visit(BinaryExpression &);
78         virtual void visit(Assignment &);
79         virtual void visit(StructDeclaration &);
80         virtual void visit(VariableDeclaration &);
81         virtual void visit(InterfaceBlock &);
82 };
83
84 class FunctionResolver: private TraversingVisitor
85 {
86 private:
87         std::map<std::string, std::vector<FunctionDeclaration *> > functions;
88
89 public:
90         void apply(Stage &s) { s.content.visit(*this); }
91
92 private:
93         virtual void visit(FunctionCall &);
94         virtual void visit(FunctionDeclaration &);
95 };
96
97 class InterfaceGenerator: private TraversingVisitor
98 {
99 private:
100         Stage *stage;
101         std::string in_prefix;
102         std::string out_prefix;
103         bool function_scope;
104         InterfaceBlock *iface_block;
105         bool copy_block;
106         Block *iface_target_block;
107         NodeList<Statement>::iterator iface_insert_point;
108         NodeList<Statement>::iterator assignment_insert_point;
109         std::set<Node *> nodes_to_remove;
110
111 public:
112         InterfaceGenerator();
113
114         void apply(Stage &);
115
116 private:
117         static std::string get_out_prefix(Stage::Type);
118         std::string change_prefix(const std::string &, const std::string &) const;
119         virtual void visit(Block &);
120         bool generate_interface(VariableDeclaration &, const std::string &, const std::string &);
121         bool generate_interface(InterfaceBlock &);
122         ExpressionStatement &insert_assignment(const std::string &, Expression *);
123         virtual void visit(VariableReference &);
124         virtual void visit(VariableDeclaration &);
125         virtual void visit(InterfaceBlock &);
126         virtual void visit(FunctionDeclaration &);
127         virtual void visit(Passthrough &);
128 };
129
130 class DeclarationReorderer: private TraversingVisitor
131 {
132 private:
133         enum DeclarationKind
134         {
135                 NO_DECLARATION,
136                 LAYOUT,
137                 STRUCT,
138                 VARIABLE,
139                 FUNCTION
140         };
141
142         DeclarationKind kind;
143         std::set<Node *> ordered_funcs;
144         std::set<Node *> needed_funcs;
145
146 public:
147         DeclarationReorderer();
148
149         void apply(Stage &s) { s.content.visit(*this); }
150
151 private:
152         virtual void visit(Block &);
153         virtual void visit(FunctionCall &);
154         virtual void visit(InterfaceLayout &) { kind = LAYOUT; }
155         virtual void visit(StructDeclaration &) { kind = STRUCT; }
156         virtual void visit(VariableDeclaration &);
157         virtual void visit(InterfaceBlock &) { kind = VARIABLE; }
158         virtual void visit(FunctionDeclaration &);
159 };
160
161 } // namespace SL
162 } // namespace GL
163 } // namespace Msp
164
165 #endif