]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/generate.cpp
Rewrite syntax tree modifications
[libs/gl.git] / source / glsl / generate.cpp
1 #include <msp/core/raii.h>
2 #include "builtin.h"
3 #include "generate.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9 namespace SL {
10
11 DeclarationCombiner::DeclarationCombiner():
12         toplevel(true)
13 { }
14
15 void DeclarationCombiner::apply(Stage &stage)
16 {
17         visit(stage.content);
18         NodeRemover().apply(stage, nodes_to_remove);
19 }
20
21 void DeclarationCombiner::visit(Block &block)
22 {
23         if(!toplevel)
24                 return;
25
26         SetForScope<bool> set(toplevel, false);
27         TraversingVisitor::visit(block);
28 }
29
30 void DeclarationCombiner::visit(FunctionDeclaration &func)
31 {
32         vector<FunctionDeclaration *> &decls = functions[func.name];
33         if(func.definition)
34         {
35                 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
36                 {
37                         (*i)->definition = func.definition;
38                         (*i)->body.body.clear();
39                 }
40         }
41         decls.push_back(&func);
42 }
43
44 void DeclarationCombiner::visit(VariableDeclaration &var)
45 {
46         VariableDeclaration *&ptr = variables[var.name];
47         if(ptr)
48         {
49                 ptr->type = var.type;
50                 if(var.init_expression)
51                         ptr->init_expression = var.init_expression;
52                 if(var.layout)
53                 {
54                         if(ptr->layout)
55                         {
56                                 for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
57                                 {
58                                         bool found = false;
59                                         for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
60                                                 if(j->identifier==i->identifier)
61                                                 {
62                                                         j->value = i->value;
63                                                         found = true;
64                                                 }
65
66                                         if(!found)
67                                                 ptr->layout->qualifiers.push_back(*i);
68                                 }
69                         }
70                         else
71                                 ptr->layout = var.layout;
72                 }
73                 nodes_to_remove.insert(&var);
74         }
75         else
76                 ptr = &var;
77 }
78
79
80 VariableResolver::VariableResolver():
81         anonymous(false),
82         record_target(false),
83         assignment_target(0),
84         self_referencing(false)
85 { }
86
87 void VariableResolver::apply(Stage &stage)
88 {
89         Stage *builtins = get_builtins(stage.type);
90         if(builtins)
91                 blocks.push_back(&builtins->content);
92         visit(stage.content);
93         if(builtins)
94                 blocks.pop_back();
95 }
96
97 void VariableResolver::visit(Block &block)
98 {
99         blocks.push_back(&block);
100         block.variables.clear();
101         TraversingVisitor::visit(block);
102         blocks.pop_back();
103 }
104
105 void VariableResolver::visit(VariableReference &var)
106 {
107         var.declaration = 0;
108         type = 0;
109         for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
110         {
111                 --i;
112                 map<string, VariableDeclaration *>::iterator j = (*i)->variables.find(var.name);
113                 if(j!=(*i)->variables.end())
114                 {
115                         var.declaration = j->second;
116                         type = j->second->type_declaration;
117                         break;
118                 }
119         }
120
121         if(record_target)
122         {
123                 if(assignment_target)
124                 {
125                         record_target = false;
126                         assignment_target = 0;
127                 }
128                 else
129                         assignment_target = var.declaration;
130         }
131         else if(var.declaration && var.declaration==assignment_target)
132                 self_referencing = true;
133 }
134
135 void VariableResolver::visit(MemberAccess &memacc)
136 {
137         type = 0;
138         TraversingVisitor::visit(memacc);
139         memacc.declaration = 0;
140         if(type)
141         {
142                 map<string, VariableDeclaration *>::iterator i = type->members.variables.find(memacc.member);
143                 if(i!=type->members.variables.end())
144                 {
145                         memacc.declaration = i->second;
146                         type = i->second->type_declaration;
147                 }
148                 else
149                         type = 0;
150         }
151 }
152
153 void VariableResolver::visit(BinaryExpression &binary)
154 {
155         if(binary.oper=="[")
156         {
157                 {
158                         SetForScope<bool> set(record_target, false);
159                         binary.right->visit(*this);
160                 }
161                 type = 0;
162                 binary.left->visit(*this);
163         }
164         else
165         {
166                 TraversingVisitor::visit(binary);
167                 type = 0;
168         }
169 }
170
171 void VariableResolver::visit(Assignment &assign)
172 {
173         {
174                 SetFlag set(record_target);
175                 assignment_target = 0;
176                 assign.left->visit(*this);
177         }
178
179         self_referencing = false;
180         assign.right->visit(*this);
181
182         assign.self_referencing = (self_referencing || assign.oper!="=");
183         assign.target_declaration = assignment_target;
184 }
185
186 void VariableResolver::visit(StructDeclaration &strct)
187 {
188         TraversingVisitor::visit(strct);
189         blocks.back()->types[strct.name] = &strct;
190 }
191
192 void VariableResolver::visit(VariableDeclaration &var)
193 {
194         for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
195         {
196                 --i;
197                 map<string, StructDeclaration *>::iterator j = (*i)->types.find(var.type);
198                 if(j!=(*i)->types.end())
199                         var.type_declaration = j->second;
200         }
201
202         if(!block_interface.empty() && var.interface.empty())
203                 var.interface = block_interface;
204
205         TraversingVisitor::visit(var);
206         blocks.back()->variables[var.name] = &var;
207         if(anonymous && blocks.size()>1)
208                 blocks[blocks.size()-2]->variables[var.name] = &var;
209 }
210
211 void VariableResolver::visit(InterfaceBlock &iface)
212 {
213         SetFlag set(anonymous);
214         SetForScope<string> set2(block_interface, iface.interface);
215         TraversingVisitor::visit(iface);
216 }
217
218
219 void FunctionResolver::visit(FunctionCall &call)
220 {
221         map<string, vector<FunctionDeclaration *> >::iterator i = functions.find(call.name);
222         if(i!=functions.end())
223                 call.declaration = i->second.back();
224
225         TraversingVisitor::visit(call);
226 }
227
228 void FunctionResolver::visit(FunctionDeclaration &func)
229 {
230         vector<FunctionDeclaration *> &decls = functions[func.name];
231         if(func.definition)
232         {
233                 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
234                         (*i)->definition = func.definition;
235                 decls.clear();
236                 decls.push_back(&func);
237         }
238         else if(!decls.empty() && decls.back()->definition)
239                 func.definition = decls.back()->definition;
240         else
241                 decls.push_back(&func);
242
243         TraversingVisitor::visit(func);
244 }
245
246
247 InterfaceGenerator::InterfaceGenerator():
248         stage(0),
249         scope_level(0),
250         current_block(0)
251 { }
252
253 string InterfaceGenerator::get_out_prefix(Stage::Type type)
254 {
255         if(type==Stage::VERTEX)
256                 return "_vs_out_";
257         else if(type==Stage::GEOMETRY)
258                 return "_gs_out_";
259         else
260                 return string();
261 }
262
263 void InterfaceGenerator::apply(Stage &s)
264 {
265         stage = &s;
266         if(stage->previous)
267                 in_prefix = get_out_prefix(stage->previous->type);
268         out_prefix = get_out_prefix(stage->type);
269         visit(s.content);
270         NodeRemover().apply(s, nodes_to_remove);
271 }
272
273 void InterfaceGenerator::visit(Block &block)
274 {
275         SetForScope<unsigned> set(scope_level, scope_level+1);
276         SetForScope<Block *> set_block(current_block, &block);
277         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
278         {
279                 assignment_insert_point = i;
280                 if(scope_level==1)
281                         iface_insert_point = i;
282
283                 (*i)->visit(*this);
284         }
285 }
286
287 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
288 {
289         unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
290         return prefix+name.substr(offset);
291 }
292
293 bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
294 {
295         const map<string, VariableDeclaration *> &stage_vars = (iface=="in" ? stage->in_variables : stage->out_variables);
296         if(stage_vars.count(name))
297                 return false;
298
299         VariableDeclaration* iface_var = new VariableDeclaration;
300         iface_var->sampling = var.sampling;
301         iface_var->interface = iface;
302         iface_var->type = var.type;
303         iface_var->type_declaration = var.type_declaration;
304         iface_var->name = name;
305         if(stage->type==Stage::GEOMETRY)
306                 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
307         else
308                 iface_var->array = var.array;
309         if(iface_var->array)
310                 iface_var->array_size = var.array_size;
311         if(iface=="in")
312                 iface_var->linked_declaration = &var;
313         stage->content.body.insert(iface_insert_point, iface_var);
314         {
315                 SetForScope<unsigned> set_level(scope_level, 1);
316                 SetForScope<Block *> set_block(current_block, &stage->content);
317                 iface_var->visit(*this);
318         }
319
320         return true;
321 }
322
323 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
324 {
325         Assignment *assign = new Assignment;
326         VariableReference *ref = new VariableReference;
327         ref->name = left;
328         assign->left = ref;
329         assign->oper = "=";
330         assign->right = right;
331
332         ExpressionStatement *stmt = new ExpressionStatement;
333         stmt->expression = assign;
334         current_block->body.insert(assignment_insert_point, stmt);
335         stmt->visit(*this);
336
337         return *stmt;
338 }
339
340 void InterfaceGenerator::visit(VariableReference &var)
341 {
342         if(var.declaration || !stage->previous)
343                 return;
344
345         const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
346         map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
347         if(i==prev_out.end())
348                 i = prev_out.find(in_prefix+var.name);
349         if(i!=prev_out.end())
350         {
351                 generate_interface(*i->second, "in", i->second->name);
352                 var.name = i->second->name;
353         }
354 }
355
356 void InterfaceGenerator::visit(VariableDeclaration &var)
357 {
358         if(var.interface=="out")
359         {
360                 if(scope_level==1)
361                         stage->out_variables[var.name] = &var;
362                 else if(generate_interface(var, "out", change_prefix(var.name, string())))
363                 {
364                         nodes_to_remove.insert(&var);
365                         if(var.init_expression)
366                         {
367                                 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
368                                 stmt.source = var.source;
369                                 stmt.line = var.line;
370                                 return;
371                         }
372                 }
373         }
374         else if(var.interface=="in")
375         {
376                 stage->in_variables[var.name] = &var;
377                 if(var.linked_declaration)
378                         var.linked_declaration->linked_declaration = &var;
379                 else if(stage->previous)
380                 {
381                         const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
382                         map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
383                         if(i!=prev_out.end())
384                         {
385                                 var.linked_declaration = i->second;
386                                 i->second->linked_declaration = &var;
387                         }
388                 }
389         }
390
391         TraversingVisitor::visit(var);
392 }
393
394 void InterfaceGenerator::visit(Passthrough &pass)
395 {
396         vector<VariableDeclaration *> pass_vars;
397
398         for(map<string, VariableDeclaration *>::const_iterator i=stage->in_variables.begin(); i!=stage->in_variables.end(); ++i)
399                 pass_vars.push_back(i->second);
400
401         if(stage->previous)
402         {
403                 const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
404                 for(map<string, VariableDeclaration *>::const_iterator i=prev_out.begin(); i!=prev_out.end(); ++i)
405                 {
406                         bool linked = false;
407                         for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
408                                 linked = ((*j)->linked_declaration==i->second);
409
410                         if(!linked && generate_interface(*i->second, "in", i->second->name))
411                                 pass_vars.push_back(i->second);
412                 }
413         }
414
415         if(stage->type==Stage::GEOMETRY)
416         {
417                 VariableReference *ref = new VariableReference;
418                 ref->name = "gl_in";
419
420                 BinaryExpression *subscript = new BinaryExpression;
421                 subscript->left = ref;
422                 subscript->oper = "[";
423                 subscript->right = pass.subscript;
424                 subscript->after = "]";
425
426                 MemberAccess *memacc = new MemberAccess;
427                 memacc->left = subscript;
428                 memacc->member = "gl_Position";
429
430                 insert_assignment("gl_Position", memacc);
431         }
432
433         for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
434         {
435                 string out_name = change_prefix((*i)->name, out_prefix);
436                 generate_interface(**i, "out", out_name);
437
438                 VariableReference *ref = new VariableReference;
439                 ref->name = (*i)->name;
440                 if(pass.subscript)
441                 {
442                         BinaryExpression *subscript = new BinaryExpression;
443                         subscript->left = ref;
444                         subscript->oper = "[";
445                         subscript->right = pass.subscript;
446                         subscript->after = "]";
447                         insert_assignment(out_name, subscript);
448                 }
449                 else
450                         insert_assignment(out_name, ref);
451         }
452
453         nodes_to_remove.insert(&pass);
454 }
455
456
457 DeclarationReorderer::DeclarationReorderer():
458         scope_level(0),
459         kind(NO_DECLARATION)
460 { }
461
462 void DeclarationReorderer::visit(FunctionCall &call)
463 {
464         FunctionDeclaration *def = call.declaration;
465         if(def)
466                 def = def->definition;
467         if(def && !ordered_funcs.count(def))
468                 needed_funcs.insert(def);
469 }
470
471 void DeclarationReorderer::visit(Block &block)
472 {
473         SetForScope<unsigned> set(scope_level, scope_level+1);
474         if(scope_level>1)
475                 return TraversingVisitor::visit(block);
476
477         NodeList<Statement>::iterator struct_insert_point = block.body.end();
478         NodeList<Statement>::iterator variable_insert_point = block.body.end();
479         NodeList<Statement>::iterator function_insert_point = block.body.end();
480         unsigned unordered_func_count = 0;
481         bool ordered_any_funcs = false;
482
483         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
484         {
485                 kind = NO_DECLARATION;
486                 (*i)->visit(*this);
487
488                 bool moved = false;
489                 if(kind==STRUCT && struct_insert_point!=block.body.end())
490                 {
491                         block.body.insert(struct_insert_point, *i);
492                         moved = true;
493                 }
494                 else if(kind>STRUCT && struct_insert_point==block.body.end())
495                         struct_insert_point = i;
496
497                 if(kind==VARIABLE && variable_insert_point!=block.body.end())
498                 {
499                         block.body.insert(variable_insert_point, *i);
500                         moved = true;
501                 }
502                 else if(kind>VARIABLE && variable_insert_point==block.body.end())
503                         variable_insert_point = i;
504
505                 if(kind==FUNCTION)
506                 {
507                         if(function_insert_point==block.body.end())
508                                 function_insert_point = i;
509
510                         if(needed_funcs.empty())
511                         {
512                                 ordered_funcs.insert(i->get());
513                                 if(i!=function_insert_point)
514                                 {
515                                         block.body.insert(function_insert_point, *i);
516                                         moved = true;
517                                 }
518                                 else
519                                         ++function_insert_point;
520                                 ordered_any_funcs = true;
521                         }
522                         else
523                                 ++unordered_func_count;
524                 }
525
526                 if(moved)
527                 {
528                         if(function_insert_point==i)
529                                 ++function_insert_point;
530                         block.body.erase(i++);
531                 }
532                 else
533                         ++i;
534
535                 if(i==block.body.end() && unordered_func_count)
536                 {
537                         if(!ordered_any_funcs)
538                                 // A subset of the remaining functions forms a recursive loop
539                                 /* TODO pick a function and move it up, adding any necessary
540                                 declarations */
541                                 break;
542
543                         i = function_insert_point;
544                         unordered_func_count = 0;
545                 }
546         }
547 }
548
549 void DeclarationReorderer::visit(VariableDeclaration &var)
550 {
551         TraversingVisitor::visit(var);
552         kind = VARIABLE;
553 }
554
555 void DeclarationReorderer::visit(FunctionDeclaration &func)
556 {
557         needed_funcs.clear();
558         func.body.visit(*this);
559         needed_funcs.erase(&func);
560         kind = FUNCTION;
561 }
562
563 } // namespace SL
564 } // namespace GL
565 } // namespace Msp