]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/generate.cpp
Split the Light class into subclasses by light type
[libs/gl.git] / source / glsl / generate.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/core/hash.h>
3 #include <msp/core/raii.h>
4 #include "generate.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10 namespace SL {
11
12 void ConstantIdAssigner::apply(Module &module, const Features &features)
13 {
14         for(Stage &s: module.stages)
15                 s.content.visit(*this);
16
17         for(VariableDeclaration *v: auto_constants)
18         {
19                 unsigned id = hash32(v->name)%features.constant_id_range;
20                 while(used_ids.count(id))
21                         id = (id+1)%features.constant_id_range;
22
23                 auto i = find_member(v->layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
24                 if(i!=v->layout->qualifiers.end())
25                         i->value = id;
26
27                 used_ids.insert(id);
28         }
29 }
30
31 void ConstantIdAssigner::visit(VariableDeclaration &var)
32 {
33         if(var.layout)
34         {
35                 auto i = find_member(var.layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
36                 if(i!=var.layout->qualifiers.end() && i->has_value)
37                 {
38                         if(i->value==-1)
39                                 auto_constants.push_back(&var);
40                         else
41                                 used_ids.insert(i->value);
42                 }
43         }
44 }
45
46
47 InterfaceGenerator::InterfaceGenerator():
48         stage(0),
49         function_scope(false),
50         copy_block(false),
51         iface_target_block(0)
52 { }
53
54 string InterfaceGenerator::get_out_prefix(Stage::Type type)
55 {
56         if(type==Stage::VERTEX)
57                 return "_vs_out_";
58         else if(type==Stage::GEOMETRY)
59                 return "_gs_out_";
60         else
61                 return string();
62 }
63
64 void InterfaceGenerator::apply(Stage &s)
65 {
66         stage = &s;
67         iface_target_block = &stage->content;
68         if(stage->previous)
69                 in_prefix = get_out_prefix(stage->previous->type);
70         out_prefix = get_out_prefix(stage->type);
71         s.content.visit(*this);
72         NodeRemover().apply(s, nodes_to_remove);
73 }
74
75 void InterfaceGenerator::visit(Block &block)
76 {
77         SetForScope<Block *> set_block(current_block, &block);
78         for(auto i=block.body.begin(); i!=block.body.end(); ++i)
79         {
80                 assignment_insert_point = i;
81                 if(&block==&stage->content)
82                         iface_insert_point = i;
83
84                 (*i)->visit(*this);
85         }
86 }
87
88 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
89 {
90         unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
91         return prefix+name.substr(offset);
92 }
93
94 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
95 {
96         if(stage->content.variables.count(name))
97                 return 0;
98
99         if(stage->type==Stage::GEOMETRY && !copy_block && var.interface=="out" && var.array)
100                 return 0;
101
102         VariableDeclaration* iface_var = new VariableDeclaration;
103         iface_var->sampling = var.sampling;
104         iface_var->interface = iface;
105         iface_var->type = var.type;
106         iface_var->name = name;
107         /* Geometry shader inputs are always arrays.  But if we're bringing in an
108         entire block, the array is on the block and not individual variables. */
109         if(stage->type==Stage::GEOMETRY && !copy_block)
110                 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
111         else
112                 iface_var->array = var.array;
113         if(iface_var->array)
114                 iface_var->array_size = var.array_size;
115         if(iface=="in")
116         {
117                 iface_var->layout = var.layout;
118                 iface_var->linked_declaration = &var;
119                 var.linked_declaration = iface_var;
120         }
121
122         iface_target_block->body.insert(iface_insert_point, iface_var);
123         iface_target_block->variables.insert(make_pair(name, iface_var));
124         if(iface_target_block==&stage->content && iface=="in")
125                 declared_inputs.push_back(iface_var);
126
127         return iface_var;
128 }
129
130 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
131 {
132         if(stage->interface_blocks.count("in "+out_block.block_name))
133                 return 0;
134
135         InterfaceBlock *in_block = new InterfaceBlock;
136         in_block->interface = "in";
137         in_block->block_name = out_block.block_name;
138         in_block->members = new Block;
139         in_block->instance_name = out_block.instance_name;
140         if(stage->type==Stage::GEOMETRY)
141                 in_block->array = true;
142         else
143                 in_block->array = out_block.array;
144         in_block->linked_block = &out_block;
145         out_block.linked_block = in_block;
146
147         {
148                 SetFlag set_copy(copy_block, true);
149                 SetForScope<Block *> set_target(iface_target_block, in_block->members.get());
150                 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end());
151                 if(out_block.struct_declaration)
152                         out_block.struct_declaration->members.visit(*this);
153                 else if(out_block.members)
154                         out_block.members->visit(*this);
155         }
156
157         iface_target_block->body.insert(iface_insert_point, in_block);
158         stage->interface_blocks.insert(make_pair("in "+in_block->block_name, in_block));
159         if(!in_block->instance_name.empty())
160                 stage->interface_blocks.insert(make_pair(in_block->instance_name, in_block));
161
162         SetFlag set_scope(function_scope, false);
163         SetForScope<Block *> set_block(current_block, &stage->content);
164         in_block->visit(*this);
165
166         return in_block;
167 }
168
169 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
170 {
171         Assignment *assign = new Assignment;
172         VariableReference *ref = new VariableReference;
173         ref->name = left;
174         assign->left = ref;
175         assign->oper = &Operator::get_operator("=", Operator::BINARY);
176         assign->right = right;
177
178         ExpressionStatement *stmt = new ExpressionStatement;
179         stmt->expression = assign;
180         current_block->body.insert(assignment_insert_point, stmt);
181         stmt->visit(*this);
182
183         return *stmt;
184 }
185
186 void InterfaceGenerator::visit(VariableReference &var)
187 {
188         if(var.declaration || !stage->previous)
189                 return;
190         /* Don't pull a variable from previous stage if we just generated an output
191         interface in this stage */
192         if(stage->content.variables.count(var.name))
193                 return;
194
195         const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
196         auto i = prev_vars.find(var.name);
197         if(i==prev_vars.end() || i->second->interface!="out")
198                 i = prev_vars.find(in_prefix+var.name);
199         if(i!=prev_vars.end() && i->second->interface=="out")
200         {
201                 if(stage->type==Stage::GEOMETRY && i->second->array)
202                         stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line,
203                                 format("Can't access '%s' through automatic interface because it's an array", var.name)));
204                 else
205                 {
206                         generate_interface(*i->second, "in", i->second->name);
207                         var.name = i->second->name;
208                 }
209                 return;
210         }
211
212         const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
213         auto j = prev_blocks.find(var.name);
214         if(j!=prev_blocks.end() && j->second->interface=="out")
215         {
216                 generate_interface(*j->second);
217                 /* Let VariableResolver convert the variable reference into an interface
218                 block reference. */
219                 return;
220         }
221
222         for(const auto &kvp: prev_blocks)
223                 if(kvp.second->instance_name.empty() && kvp.second->struct_declaration)
224                 {
225                         const map<string, VariableDeclaration *> &iface_vars = kvp.second->struct_declaration->members.variables;
226                         i = iface_vars.find(var.name);
227                         if(i!=iface_vars.end())
228                         {
229                                 generate_interface(*kvp.second);
230                                 return;
231                         }
232                 }
233 }
234
235 void InterfaceGenerator::visit(VariableDeclaration &var)
236 {
237         if(copy_block)
238                 generate_interface(var, "in", var.name);
239         else if(var.interface=="out")
240         {
241                 /* For output variables in function scope, generate a global interface
242                 and replace the local declaration with an assignment. */
243                 VariableDeclaration *out_var = 0;
244                 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
245                 {
246                         out_var->source = var.source;
247                         out_var->line = var.line;
248                         nodes_to_remove.insert(&var);
249                         if(var.init_expression)
250                         {
251                                 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
252                                 stmt.source = var.source;
253                                 stmt.line = var.line;
254                                 return;
255                         }
256                 }
257         }
258         else if(var.interface=="in" && current_block==&stage->content)
259         {
260                 if(var.name.compare(0, 3, "gl_"))
261                         declared_inputs.push_back(&var);
262
263                 /* Try to link input variables in global scope with output variables from
264                 previous stage. */
265                 if(!var.linked_declaration && stage->previous)
266                 {
267                         const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
268                         auto i = prev_vars.find(var.name);
269                         if(i!=prev_vars.end() && i->second->interface=="out")
270                         {
271                                 var.linked_declaration = i->second;
272                                 i->second->linked_declaration = &var;
273                         }
274                 }
275         }
276
277         TraversingVisitor::visit(var);
278 }
279
280 void InterfaceGenerator::visit(InterfaceBlock &iface)
281 {
282         if(iface.interface=="in")
283         {
284                 /* Try to link input blocks with output blocks sharing the same block
285                 name from previous stage. */
286                 if(!iface.linked_block && stage->previous)
287                 {
288                         const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
289                         auto i = prev_blocks.find("out "+iface.block_name);
290                         if(i!=prev_blocks.end())
291                         {
292                                 iface.linked_block = i->second;
293                                 i->second->linked_block = &iface;
294                         }
295                 }
296         }
297
298         TraversingVisitor::visit(iface);
299 }
300
301 void InterfaceGenerator::visit(FunctionDeclaration &func)
302 {
303         SetFlag set_scope(function_scope, true);
304         // Skip parameters because they're not useful here
305         func.body.visit(*this);
306 }
307
308 void InterfaceGenerator::visit(Passthrough &pass)
309 {
310         // Pass through all input variables declared so far.
311         vector<VariableDeclaration *> pass_vars = declared_inputs;
312
313         if(stage->previous)
314         {
315                 for(const auto &kvp: stage->previous->content.variables)
316                 {
317                         if(kvp.second->interface!="out")
318                                 continue;
319
320                         /* Pass through output variables from the previous stage, but only
321                         those which are not already linked to an input here. */
322                         if(!kvp.second->linked_declaration && generate_interface(*kvp.second, "in", kvp.second->name))
323                                 pass_vars.push_back(kvp.second);
324                 }
325         }
326
327         if(stage->type==Stage::GEOMETRY)
328         {
329                 /* Special case for geometry shader: copy gl_Position from input to
330                 output. */
331                 InterfaceBlockReference *ref = new InterfaceBlockReference;
332                 ref->name = "gl_in";
333
334                 BinaryExpression *subscript = new BinaryExpression;
335                 subscript->left = ref;
336                 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
337                 subscript->right = pass.subscript;
338
339                 MemberAccess *memacc = new MemberAccess;
340                 memacc->left = subscript;
341                 memacc->member = "gl_Position";
342
343                 insert_assignment("gl_Position", memacc);
344         }
345
346         for(VariableDeclaration *v: pass_vars)
347         {
348                 string out_name = change_prefix(v->name, out_prefix);
349                 generate_interface(*v, "out", out_name);
350
351                 VariableReference *ref = new VariableReference;
352                 ref->name = v->name;
353                 if(pass.subscript)
354                 {
355                         BinaryExpression *subscript = new BinaryExpression;
356                         subscript->left = ref;
357                         subscript->oper = &Operator::get_operator("[", Operator::BINARY);
358                         subscript->right = pass.subscript;
359                         insert_assignment(out_name, subscript);
360                 }
361                 else
362                         insert_assignment(out_name, ref);
363         }
364
365         nodes_to_remove.insert(&pass);
366 }
367
368 } // namespace SL
369 } // namespace GL
370 } // namespace Msp