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