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