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