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