]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/generate.cpp
Refactor FunctionInliner to do any necessary declaration reordering
[libs/gl.git] / source / glsl / generate.cpp
1 #include <msp/core/hash.h>
2 #include <msp/core/raii.h>
3 #include <msp/strings/lexicalcast.h>
4 #include "builtin.h"
5 #include "generate.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11 namespace SL {
12
13 void DeclarationCombiner::apply(Stage &stage)
14 {
15         stage.content.visit(*this);
16         NodeRemover().apply(stage, nodes_to_remove);
17 }
18
19 void DeclarationCombiner::visit(Block &block)
20 {
21         if(current_block)
22                 return;
23
24         TraversingVisitor::visit(block);
25 }
26
27 void DeclarationCombiner::visit(VariableDeclaration &var)
28 {
29         VariableDeclaration *&ptr = variables[var.name];
30         if(ptr)
31         {
32                 ptr->type = var.type;
33                 if(var.init_expression)
34                         ptr->init_expression = var.init_expression;
35                 if(var.layout)
36                 {
37                         if(ptr->layout)
38                         {
39                                 for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
40                                 {
41                                         bool found = false;
42                                         for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
43                                                 if(j->name==i->name)
44                                                 {
45                                                         j->has_value = i->value;
46                                                         j->value = i->value;
47                                                         found = true;
48                                                 }
49
50                                         if(!found)
51                                                 ptr->layout->qualifiers.push_back(*i);
52                                 }
53                         }
54                         else
55                                 ptr->layout = var.layout;
56                 }
57                 nodes_to_remove.insert(&var);
58         }
59         else
60                 ptr = &var;
61 }
62
63
64 ConstantSpecializer::ConstantSpecializer():
65         values(0)
66 { }
67
68 void ConstantSpecializer::apply(Stage &stage, const map<string, int> *v)
69 {
70         values = v;
71         stage.content.visit(*this);
72 }
73
74 void ConstantSpecializer::visit(VariableDeclaration &var)
75 {
76         bool specializable = false;
77         if(var.layout)
78         {
79                 vector<Layout::Qualifier> &qualifiers = var.layout->qualifiers;
80                 for(vector<Layout::Qualifier>::iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i)
81                         if(i->name=="constant_id")
82                         {
83                                 specializable = true;
84                                 if(values)
85                                         qualifiers.erase(i);
86                                 else if(i->value==-1)
87                                         i->value = hash32(var.name)&0x7FFFFFFF;
88                                 break;
89                         }
90
91                 if(qualifiers.empty())
92                         var.layout = 0;
93         }
94
95         if(specializable && values)
96         {
97                 map<string, int>::const_iterator i = values->find(var.name);
98                 if(i!=values->end())
99                 {
100                         RefPtr<Literal> literal = new Literal;
101                         if(var.type=="bool")
102                                 literal->token = (i->second ? "true" : "false");
103                         else if(var.type=="int")
104                                 literal->token = lexical_cast<string>(i->second);
105                         var.init_expression = literal;
106                 }
107         }
108 }
109
110
111 void BlockHierarchyResolver::enter(Block &block)
112 {
113         block.parent = current_block;
114 }
115
116
117 VariableResolver::VariableResolver():
118         stage(0),
119         members(0),
120         record_target(false),
121         assignment_target(0),
122         self_referencing(false)
123 { }
124
125 void VariableResolver::apply(Stage &s)
126 {
127         stage = &s;
128         s.types.clear();
129         s.interface_blocks.clear();
130         s.content.visit(*this);
131 }
132
133 void VariableResolver::enter(Block &block)
134 {
135         block.variables.clear();
136 }
137
138 void VariableResolver::visit(VariableReference &var)
139 {
140         var.declaration = 0;
141         members = 0;
142         for(Block *block=current_block; (!var.declaration && block); block=block->parent)
143         {
144                 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
145                 if(i!=block->variables.end())
146                         var.declaration = i->second;
147         }
148
149         if(var.declaration)
150         {
151                 if(var.declaration->type_declaration)
152                         members = &var.declaration->type_declaration->members.variables;
153         }
154         else
155         {
156                 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
157                 map<string, InterfaceBlock *>::const_iterator i = blocks.find(var.name);
158                 if(i!=blocks.end() && i->second->instance_name==var.name)
159                 {
160                         iface_ref = new InterfaceBlockReference;
161                         iface_ref->name = var.name;
162                         iface_ref->declaration = i->second;
163                         members = &i->second->members.variables;
164                 }
165                 else
166                 {
167                         for(i=blocks.begin(); (!var.declaration && i!=blocks.end()); ++i)
168                                 if(i->second->instance_name.empty())
169                                 {
170                                         map<string, VariableDeclaration *>::iterator j = i->second->members.variables.find(var.name);
171                                         if(j!=i->second->members.variables.end())
172                                                 var.declaration = j->second;
173                                 }
174                 }
175         }
176
177         if(record_target)
178         {
179                 if(assignment_target)
180                 {
181                         record_target = false;
182                         assignment_target = 0;
183                 }
184                 else
185                         assignment_target = var.declaration;
186         }
187         else if(var.declaration && var.declaration==assignment_target)
188                 self_referencing = true;
189 }
190
191 void VariableResolver::visit(InterfaceBlockReference &iface)
192 {
193         iface.declaration = 0;
194         for(Block *block=current_block; block; block=block->parent)
195         {
196                 map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find(iface.name);
197                 if(i!=stage->interface_blocks.end())
198                 {
199                         iface.declaration = i->second;
200                         members = &i->second->members.variables;
201                         break;
202                 }
203         }
204 }
205
206 void VariableResolver::visit(MemberAccess &memacc)
207 {
208         members = 0;
209         iface_ref = 0;
210         memacc.left->visit(*this);
211
212         if(iface_ref)
213                 memacc.left = iface_ref;
214         iface_ref = 0;
215
216         memacc.declaration = 0;
217         if(members)
218         {
219                 map<string, VariableDeclaration *>::iterator i = members->find(memacc.member);
220                 if(i!=members->end())
221                 {
222                         memacc.declaration = i->second;
223                         if(i->second->type_declaration)
224                                 members = &i->second->type_declaration->members.variables;
225                 }
226                 else
227                         members = 0;
228         }
229 }
230
231 void VariableResolver::visit(BinaryExpression &binary)
232 {
233         if(binary.oper=="[")
234         {
235                 {
236                         SetForScope<bool> set(record_target, false);
237                         binary.right->visit(*this);
238                 }
239                 members = 0;
240                 iface_ref = 0;
241                 binary.left->visit(*this);
242                 if(iface_ref)
243                         binary.left = iface_ref;
244                 iface_ref = 0;
245         }
246         else
247         {
248                 TraversingVisitor::visit(binary);
249                 members = 0;
250         }
251 }
252
253 void VariableResolver::visit(Assignment &assign)
254 {
255         {
256                 SetFlag set(record_target);
257                 assignment_target = 0;
258                 assign.left->visit(*this);
259         }
260
261         self_referencing = false;
262         assign.right->visit(*this);
263
264         assign.self_referencing = (self_referencing || assign.oper!="=");
265         assign.target_declaration = assignment_target;
266 }
267
268 void VariableResolver::visit(StructDeclaration &strct)
269 {
270         TraversingVisitor::visit(strct);
271         stage->types[strct.name] = &strct;
272 }
273
274 void VariableResolver::visit(VariableDeclaration &var)
275 {
276         map<string, StructDeclaration *>::iterator i = stage->types.find(var.type);
277         if(i!=stage->types.end())
278                 var.type_declaration = i->second;
279
280         if(!block_interface.empty() && var.interface.empty())
281                 var.interface = block_interface;
282
283         TraversingVisitor::visit(var);
284         current_block->variables[var.name] = &var;
285 }
286
287 void VariableResolver::visit(InterfaceBlock &iface)
288 {
289         /* Block names can't be used for any other identifiers so we can put them
290         in the same map with instance names. */
291         stage->interface_blocks[iface.name] = &iface;
292         if(!iface.instance_name.empty())
293                 stage->interface_blocks[iface.instance_name] = &iface;
294
295         SetForScope<string> set_iface(block_interface, iface.interface);
296         TraversingVisitor::visit(iface);
297 }
298
299
300 void FunctionResolver::apply(Stage &s)
301 {
302         stage = &s;
303         s.functions.clear();
304         s.content.visit(*this);
305 }
306
307 void FunctionResolver::visit(FunctionCall &call)
308 {
309         map<string, FunctionDeclaration *>::iterator i = stage->functions.find(call.name);
310         if(i!=stage->functions.end())
311                 call.declaration = i->second;
312
313         TraversingVisitor::visit(call);
314 }
315
316 void FunctionResolver::visit(FunctionDeclaration &func)
317 {
318         FunctionDeclaration *&stage_decl = stage->functions[func.name];
319         vector<FunctionDeclaration *> &decls = declarations[func.name];
320         if(func.definition==&func)
321         {
322                 stage_decl = &func;
323
324                 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
325                 {
326                         (*i)->definition = func.definition;
327                         (*i)->body.body.clear();
328                 }
329         }
330         else
331         {
332                 if(!stage_decl)
333                         stage_decl = &func;
334
335                 func.definition = stage_decl->definition;
336         }
337         decls.push_back(&func);
338
339         TraversingVisitor::visit(func);
340 }
341
342
343 InterfaceGenerator::InterfaceGenerator():
344         stage(0),
345         function_scope(false),
346         iface_block(0),
347         copy_block(false),
348         iface_target_block(0)
349 { }
350
351 string InterfaceGenerator::get_out_prefix(Stage::Type type)
352 {
353         if(type==Stage::VERTEX)
354                 return "_vs_out_";
355         else if(type==Stage::GEOMETRY)
356                 return "_gs_out_";
357         else
358                 return string();
359 }
360
361 void InterfaceGenerator::apply(Stage &s)
362 {
363         stage = &s;
364         iface_target_block = &stage->content;
365         if(stage->previous)
366                 in_prefix = get_out_prefix(stage->previous->type);
367         out_prefix = get_out_prefix(stage->type);
368         s.content.visit(*this);
369         NodeRemover().apply(s, nodes_to_remove);
370 }
371
372 void InterfaceGenerator::visit(Block &block)
373 {
374         SetForScope<Block *> set_block(current_block, &block);
375         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
376         {
377                 assignment_insert_point = i;
378                 if(&block==&stage->content)
379                         iface_insert_point = i;
380
381                 (*i)->visit(*this);
382         }
383 }
384
385 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
386 {
387         unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
388         return prefix+name.substr(offset);
389 }
390
391 bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
392 {
393         if(stage->content.variables.count(name))
394                 return false;
395
396         VariableDeclaration* iface_var = new VariableDeclaration;
397         iface_var->sampling = var.sampling;
398         iface_var->interface = iface;
399         iface_var->type = var.type;
400         iface_var->type_declaration = var.type_declaration;
401         iface_var->name = name;
402         if(stage->type==Stage::GEOMETRY && !copy_block)
403                 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
404         else
405                 iface_var->array = var.array;
406         if(iface_var->array)
407                 iface_var->array_size = var.array_size;
408         if(iface=="in")
409         {
410                 iface_var->layout = var.layout;
411                 iface_var->linked_declaration = &var;
412                 var.linked_declaration = iface_var;
413         }
414
415         iface_target_block->body.insert(iface_insert_point, iface_var);
416         iface_target_block->variables[name] = iface_var;
417
418         return true;
419 }
420
421 bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
422 {
423         if(stage->interface_blocks.count(out_block.name))
424                 return false;
425
426         InterfaceBlock *in_block = new InterfaceBlock;
427         in_block->interface = "in";
428         in_block->name = out_block.name;
429         in_block->instance_name = out_block.instance_name;
430         if(stage->type==Stage::GEOMETRY)
431                 in_block->array = true;
432         else
433                 in_block->array = out_block.array;
434         in_block->linked_block = &out_block;
435         out_block.linked_block = in_block;
436
437         {
438                 SetFlag set_copy(copy_block, true);
439                 SetForScope<Block *> set_target(iface_target_block, &in_block->members);
440                 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
441                 out_block.members.visit(*this);
442         }
443
444         iface_target_block->body.insert(iface_insert_point, in_block);
445         stage->interface_blocks[in_block->name] = in_block;
446         if(!in_block->instance_name.empty())
447                 stage->interface_blocks[in_block->instance_name] = in_block;
448
449         SetFlag set_scope(function_scope, false);
450         SetForScope<Block *> set_block(current_block, &stage->content);
451         in_block->visit(*this);
452
453         return true;
454 }
455
456 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
457 {
458         Assignment *assign = new Assignment;
459         VariableReference *ref = new VariableReference;
460         ref->name = left;
461         assign->left = ref;
462         assign->oper = "=";
463         assign->right = right;
464
465         ExpressionStatement *stmt = new ExpressionStatement;
466         stmt->expression = assign;
467         current_block->body.insert(assignment_insert_point, stmt);
468         stmt->visit(*this);
469
470         return *stmt;
471 }
472
473 void InterfaceGenerator::visit(VariableReference &var)
474 {
475         if(var.declaration || !stage->previous)
476                 return;
477         /* Don't pull a variable from previous stage if we just generated an out
478         interface in this stage */
479         if(stage->content.variables.count(var.name))
480                 return;
481
482         const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
483         map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
484         if(i==prev_vars.end() || i->second->interface!="out")
485                 i = prev_vars.find(in_prefix+var.name);
486         if(i!=prev_vars.end() && i->second->interface=="out")
487         {
488                 generate_interface(*i->second, "in", i->second->name);
489                 var.name = i->second->name;
490                 return;
491         }
492
493         const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
494         map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find(var.name);
495         if(j!=prev_blocks.end() && j->second->interface=="out" && j->second->instance_name==var.name)
496         {
497                 generate_interface(*j->second);
498                 return;
499         }
500
501         for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
502                 if(j->second->instance_name.empty())
503                 {
504                         i = j->second->members.variables.find(var.name);
505                         if(i!=j->second->members.variables.end())
506                         {
507                                 generate_interface(*j->second);
508                                 return;
509                         }
510                 }
511 }
512
513 void InterfaceGenerator::visit(VariableDeclaration &var)
514 {
515         if(copy_block)
516         {
517                 generate_interface(var, "in", var.name);
518                 return;
519         }
520
521         if(iface_block)
522         {
523                 if(iface_block->linked_block)
524                 {
525                         const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
526                         map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
527                         if(i!=linked_vars.end())
528                                 var.linked_declaration = i->second;
529                 }
530                 return;
531         }
532
533         if(var.interface=="out")
534         {
535                 /* For out variables in function scope, generate a global interface and
536                 replace the local declaration with an assignment. */
537                 if(function_scope && generate_interface(var, "out", var.name))
538                 {
539                         nodes_to_remove.insert(&var);
540                         if(var.init_expression)
541                         {
542                                 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
543                                 stmt.source = var.source;
544                                 stmt.line = var.line;
545                                 return;
546                         }
547                 }
548         }
549         else if(var.interface=="in")
550         {
551                 /* Try to link in variables in global scope with out variables from
552                 previous stage */
553                 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
554                 {
555                         const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
556                         map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
557                         if(i!=prev_vars.end() && i->second->interface=="out")
558                         {
559                                 var.linked_declaration = i->second;
560                                 i->second->linked_declaration = &var;
561                         }
562                 }
563         }
564
565         TraversingVisitor::visit(var);
566 }
567
568 void InterfaceGenerator::visit(InterfaceBlock &iface)
569 {
570         if(iface.interface=="in")
571         {
572                 if(!iface.linked_block && stage->previous)
573                 {
574                         const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
575                         map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find(iface.name);
576                         if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name)
577                         {
578                                 iface.linked_block = i->second;
579                                 i->second->linked_block = &iface;
580                         }
581                 }
582         }
583
584         SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
585         TraversingVisitor::visit(iface);
586 }
587
588 void InterfaceGenerator::visit(FunctionDeclaration &func)
589 {
590         SetFlag set_scope(function_scope, true);
591         // Skip parameters because they're not useful here
592         func.body.visit(*this);
593 }
594
595 void InterfaceGenerator::visit(Passthrough &pass)
596 {
597         vector<VariableDeclaration *> pass_vars;
598
599         for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
600                 if(i->second->interface=="in")
601                         pass_vars.push_back(i->second);
602
603         if(stage->previous)
604         {
605                 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
606                 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
607                 {
608                         bool linked = false;
609                         for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
610                                 linked = ((*j)->linked_declaration==i->second);
611
612                         if(!linked && generate_interface(*i->second, "in", i->second->name))
613                                 pass_vars.push_back(i->second);
614                 }
615         }
616
617         if(stage->type==Stage::GEOMETRY)
618         {
619                 InterfaceBlockReference *ref = new InterfaceBlockReference;
620                 ref->name = "gl_in";
621
622                 BinaryExpression *subscript = new BinaryExpression;
623                 subscript->left = ref;
624                 subscript->oper = "[";
625                 subscript->right = pass.subscript;
626                 subscript->after = "]";
627
628                 MemberAccess *memacc = new MemberAccess;
629                 memacc->left = subscript;
630                 memacc->member = "gl_Position";
631
632                 insert_assignment("gl_Position", memacc);
633         }
634
635         for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
636         {
637                 string out_name = change_prefix((*i)->name, out_prefix);
638                 generate_interface(**i, "out", out_name);
639
640                 VariableReference *ref = new VariableReference;
641                 ref->name = (*i)->name;
642                 if(pass.subscript)
643                 {
644                         BinaryExpression *subscript = new BinaryExpression;
645                         subscript->left = ref;
646                         subscript->oper = "[";
647                         subscript->right = pass.subscript;
648                         subscript->after = "]";
649                         insert_assignment(out_name, subscript);
650                 }
651                 else
652                         insert_assignment(out_name, ref);
653         }
654
655         nodes_to_remove.insert(&pass);
656 }
657
658 } // namespace SL
659 } // namespace GL
660 } // namespace Msp