]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/generate.cpp
Store types and interface blocks in Stage
[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         if(iface=="in")
365         {
366                 iface_var->linked_declaration = &var;
367                 var.linked_declaration = iface_var;
368         }
369
370         iface_target_block->body.insert(iface_insert_point, iface_var);
371         iface_target_block->variables[name] = iface_var;
372
373         return true;
374 }
375
376 bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
377 {
378         if(stage->interface_blocks.count(out_block.name))
379                 return false;
380
381         InterfaceBlock *in_block = new InterfaceBlock;
382         in_block->interface = "in";
383         in_block->name = out_block.name;
384         in_block->instance_name = out_block.instance_name;
385         if(stage->type==Stage::GEOMETRY)
386                 in_block->array = true;
387         else
388                 in_block->array = out_block.array;
389         in_block->linked_block = &out_block;
390         out_block.linked_block = in_block;
391
392         {
393                 SetFlag set_copy(copy_block, true);
394                 SetForScope<Block *> set_target(iface_target_block, &in_block->members);
395                 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
396                 out_block.members.visit(*this);
397         }
398
399         iface_target_block->body.insert(iface_insert_point, in_block);
400         stage->interface_blocks[in_block->name] = in_block;
401         if(!in_block->instance_name.empty())
402                 stage->interface_blocks[in_block->instance_name] = in_block;
403
404         SetFlag set_scope(function_scope, false);
405         SetForScope<Block *> set_block(current_block, &stage->content);
406         in_block->visit(*this);
407
408         return true;
409 }
410
411 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
412 {
413         Assignment *assign = new Assignment;
414         VariableReference *ref = new VariableReference;
415         ref->name = left;
416         assign->left = ref;
417         assign->oper = "=";
418         assign->right = right;
419
420         ExpressionStatement *stmt = new ExpressionStatement;
421         stmt->expression = assign;
422         current_block->body.insert(assignment_insert_point, stmt);
423         stmt->visit(*this);
424
425         return *stmt;
426 }
427
428 void InterfaceGenerator::visit(VariableReference &var)
429 {
430         if(var.declaration || !stage->previous)
431                 return;
432         /* Don't pull a variable from previous stage if we just generated an out
433         interface in this stage */
434         if(stage->content.variables.count(var.name))
435                 return;
436
437         const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
438         map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
439         if(i==prev_vars.end() || i->second->interface!="out")
440                 i = prev_vars.find(in_prefix+var.name);
441         if(i!=prev_vars.end() && i->second->interface=="out")
442         {
443                 generate_interface(*i->second, "in", i->second->name);
444                 var.name = i->second->name;
445                 return;
446         }
447
448         const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
449         map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find(var.name);
450         if(j!=prev_blocks.end() && j->second->interface=="out" && j->second->instance_name==var.name)
451         {
452                 generate_interface(*j->second);
453                 return;
454         }
455
456         for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
457                 if(j->second->instance_name.empty())
458                 {
459                         i = j->second->members.variables.find(var.name);
460                         if(i!=j->second->members.variables.end())
461                         {
462                                 generate_interface(*j->second);
463                                 return;
464                         }
465                 }
466 }
467
468 void InterfaceGenerator::visit(VariableDeclaration &var)
469 {
470         if(copy_block)
471         {
472                 generate_interface(var, "in", var.name);
473                 return;
474         }
475
476         if(iface_block)
477         {
478                 if(iface_block->linked_block)
479                 {
480                         const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
481                         map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
482                         if(i!=linked_vars.end())
483                                 var.linked_declaration = i->second;
484                 }
485                 return;
486         }
487
488         if(var.interface=="out")
489         {
490                 /* For out variables in function scope, generate a global interface and
491                 replace the local declaration with an assignment. */
492                 if(function_scope && generate_interface(var, "out", var.name))
493                 {
494                         nodes_to_remove.insert(&var);
495                         if(var.init_expression)
496                         {
497                                 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
498                                 stmt.source = var.source;
499                                 stmt.line = var.line;
500                                 return;
501                         }
502                 }
503         }
504         else if(var.interface=="in")
505         {
506                 /* Try to link in variables in global scope with out variables from
507                 previous stage */
508                 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
509                 {
510                         const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
511                         map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
512                         if(i!=prev_vars.end() && i->second->interface=="out")
513                         {
514                                 var.linked_declaration = i->second;
515                                 i->second->linked_declaration = &var;
516                         }
517                 }
518         }
519
520         TraversingVisitor::visit(var);
521 }
522
523 void InterfaceGenerator::visit(InterfaceBlock &iface)
524 {
525         if(iface.interface=="in")
526         {
527                 if(!iface.linked_block && stage->previous)
528                 {
529                         const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
530                         map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find(iface.name);
531                         if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name)
532                         {
533                                 iface.linked_block = i->second;
534                                 i->second->linked_block = &iface;
535                         }
536                 }
537         }
538
539         SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
540         TraversingVisitor::visit(iface);
541 }
542
543 void InterfaceGenerator::visit(FunctionDeclaration &func)
544 {
545         SetFlag set_scope(function_scope, true);
546         // Skip parameters because they're not useful here
547         func.body.visit(*this);
548 }
549
550 void InterfaceGenerator::visit(Passthrough &pass)
551 {
552         vector<VariableDeclaration *> pass_vars;
553
554         for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
555                 if(i->second->interface=="in")
556                         pass_vars.push_back(i->second);
557
558         if(stage->previous)
559         {
560                 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
561                 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
562                 {
563                         bool linked = false;
564                         for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
565                                 linked = ((*j)->linked_declaration==i->second);
566
567                         if(!linked && generate_interface(*i->second, "in", i->second->name))
568                                 pass_vars.push_back(i->second);
569                 }
570         }
571
572         if(stage->type==Stage::GEOMETRY)
573         {
574                 InterfaceBlockReference *ref = new InterfaceBlockReference;
575                 ref->name = "gl_in";
576
577                 BinaryExpression *subscript = new BinaryExpression;
578                 subscript->left = ref;
579                 subscript->oper = "[";
580                 subscript->right = pass.subscript;
581                 subscript->after = "]";
582
583                 MemberAccess *memacc = new MemberAccess;
584                 memacc->left = subscript;
585                 memacc->member = "gl_Position";
586
587                 insert_assignment("gl_Position", memacc);
588         }
589
590         for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
591         {
592                 string out_name = change_prefix((*i)->name, out_prefix);
593                 generate_interface(**i, "out", out_name);
594
595                 VariableReference *ref = new VariableReference;
596                 ref->name = (*i)->name;
597                 if(pass.subscript)
598                 {
599                         BinaryExpression *subscript = new BinaryExpression;
600                         subscript->left = ref;
601                         subscript->oper = "[";
602                         subscript->right = pass.subscript;
603                         subscript->after = "]";
604                         insert_assignment(out_name, subscript);
605                 }
606                 else
607                         insert_assignment(out_name, ref);
608         }
609
610         nodes_to_remove.insert(&pass);
611 }
612
613
614 DeclarationReorderer::DeclarationReorderer():
615         kind(NO_DECLARATION)
616 { }
617
618 void DeclarationReorderer::visit(FunctionCall &call)
619 {
620         FunctionDeclaration *def = call.declaration;
621         if(def)
622                 def = def->definition;
623         if(def && !ordered_funcs.count(def))
624                 needed_funcs.insert(def);
625 }
626
627 void DeclarationReorderer::visit(Block &block)
628 {
629         if(block.parent)
630                 return TraversingVisitor::visit(block);
631
632         NodeList<Statement>::iterator struct_insert_point = block.body.end();
633         NodeList<Statement>::iterator variable_insert_point = block.body.end();
634         NodeList<Statement>::iterator function_insert_point = block.body.end();
635         unsigned unordered_func_count = 0;
636         bool ordered_any_funcs = false;
637
638         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
639         {
640                 kind = NO_DECLARATION;
641                 (*i)->visit(*this);
642
643                 bool moved = false;
644                 if(kind==STRUCT && struct_insert_point!=block.body.end())
645                 {
646                         block.body.insert(struct_insert_point, *i);
647                         moved = true;
648                 }
649                 else if(kind>STRUCT && struct_insert_point==block.body.end())
650                         struct_insert_point = i;
651
652                 if(kind==VARIABLE && variable_insert_point!=block.body.end())
653                 {
654                         block.body.insert(variable_insert_point, *i);
655                         moved = true;
656                 }
657                 else if(kind>VARIABLE && variable_insert_point==block.body.end())
658                         variable_insert_point = i;
659
660                 if(kind==FUNCTION)
661                 {
662                         if(function_insert_point==block.body.end())
663                                 function_insert_point = i;
664
665                         if(needed_funcs.empty())
666                         {
667                                 ordered_funcs.insert(i->get());
668                                 if(i!=function_insert_point)
669                                 {
670                                         block.body.insert(function_insert_point, *i);
671                                         moved = true;
672                                 }
673                                 else
674                                         ++function_insert_point;
675                                 ordered_any_funcs = true;
676                         }
677                         else
678                                 ++unordered_func_count;
679                 }
680
681                 if(moved)
682                 {
683                         if(function_insert_point==i)
684                                 ++function_insert_point;
685                         block.body.erase(i++);
686                 }
687                 else
688                         ++i;
689
690                 if(i==block.body.end() && unordered_func_count)
691                 {
692                         if(!ordered_any_funcs)
693                                 // A subset of the remaining functions forms a recursive loop
694                                 /* TODO pick a function and move it up, adding any necessary
695                                 declarations */
696                                 break;
697
698                         i = function_insert_point;
699                         unordered_func_count = 0;
700                 }
701         }
702 }
703
704 void DeclarationReorderer::visit(VariableDeclaration &var)
705 {
706         TraversingVisitor::visit(var);
707         kind = VARIABLE;
708 }
709
710 void DeclarationReorderer::visit(FunctionDeclaration &func)
711 {
712         needed_funcs.clear();
713         func.body.visit(*this);
714         needed_funcs.erase(&func);
715         kind = FUNCTION;
716 }
717
718 } // namespace SL
719 } // namespace GL
720 } // namespace Msp