1 #include <msp/core/hash.h>
2 #include <msp/core/raii.h>
3 #include <msp/strings/lexicalcast.h>
13 void DeclarationCombiner::apply(Stage &stage)
15 stage.content.visit(*this);
16 NodeRemover().apply(stage, nodes_to_remove);
19 void DeclarationCombiner::visit(Block &block)
24 TraversingVisitor::visit(block);
27 void DeclarationCombiner::visit(VariableDeclaration &var)
29 VariableDeclaration *&ptr = variables[var.name];
33 if(var.init_expression)
34 ptr->init_expression = var.init_expression;
39 for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
42 for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
45 j->has_value = i->value;
51 ptr->layout->qualifiers.push_back(*i);
55 ptr->layout = var.layout;
57 nodes_to_remove.insert(&var);
64 ConstantSpecializer::ConstantSpecializer():
68 void ConstantSpecializer::apply(Stage &stage, const map<string, int> *v)
71 stage.content.visit(*this);
74 void ConstantSpecializer::visit(VariableDeclaration &var)
76 bool specializable = false;
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")
87 i->value = hash32(var.name)&0x7FFFFFFF;
91 if(qualifiers.empty())
95 if(specializable && values)
97 map<string, int>::const_iterator i = values->find(var.name);
100 RefPtr<Literal> literal = new Literal;
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;
111 void BlockHierarchyResolver::enter(Block &block)
113 block.parent = current_block;
117 VariableResolver::VariableResolver():
120 record_target(false),
121 r_self_referencing(false),
122 r_assignment_target(0)
125 void VariableResolver::apply(Stage &s)
129 s.interface_blocks.clear();
130 s.content.visit(*this);
133 void VariableResolver::enter(Block &block)
135 block.variables.clear();
138 void VariableResolver::visit(VariableReference &var)
142 /* Look for variable declarations in the block hierarchy first. Interface
143 blocks are always defined in the top level so we can't accidentally skip
145 for(Block *block=current_block; (!var.declaration && block); block=block->parent)
147 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
148 if(i!=block->variables.end())
149 var.declaration = i->second;
154 if(var.declaration->type_declaration)
155 r_members = &var.declaration->type_declaration->members.variables;
159 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
160 map<string, InterfaceBlock *>::const_iterator i = blocks.find(var.name);
161 if(i!=blocks.end() && i->second->instance_name==var.name)
163 /* The name refers to an interface block with an instance name rather
164 than a variable. Prepare a new syntax tree node accordingly. */
165 r_iface_ref = new InterfaceBlockReference;
166 r_iface_ref->name = var.name;
167 r_iface_ref->declaration = i->second;
168 r_members = &i->second->members.variables;
172 // Look for the variable in anonymous interface blocks.
173 for(i=blocks.begin(); (!var.declaration && i!=blocks.end()); ++i)
174 if(i->second->instance_name.empty())
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;
185 if(r_assignment_target)
187 /* More than one variable reference found in assignment target.
188 Unable to determine what the primary target is. */
189 record_target = false;
190 r_assignment_target = 0;
193 r_assignment_target = var.declaration;
195 else if(var.declaration && var.declaration==r_assignment_target)
196 r_self_referencing = true;
199 void VariableResolver::visit(InterfaceBlockReference &iface)
201 iface.declaration = 0;
202 for(Block *block=current_block; block; block=block->parent)
204 map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find(iface.name);
205 if(i!=stage->interface_blocks.end())
207 iface.declaration = i->second;
208 r_members = &i->second->members.variables;
214 void VariableResolver::visit(MemberAccess &memacc)
218 memacc.left->visit(*this);
221 memacc.left = r_iface_ref;
224 memacc.declaration = 0;
227 map<string, VariableDeclaration *>::iterator i = r_members->find(memacc.member);
228 if(i!=r_members->end())
230 memacc.declaration = i->second;
231 if(i->second->type_declaration)
232 r_members = &i->second->type_declaration->members.variables;
239 void VariableResolver::visit(UnaryExpression &unary)
241 TraversingVisitor::visit(unary);
246 void VariableResolver::visit(BinaryExpression &binary)
248 if(binary.oper->token[0]=='[')
251 /* The subscript expression is not a part of the primary assignment
253 SetFlag set(record_target, false);
254 binary.right->visit(*this);
258 binary.left->visit(*this);
260 binary.left = r_iface_ref;
264 TraversingVisitor::visit(binary);
271 void VariableResolver::visit(Assignment &assign)
274 SetFlag set(record_target);
275 r_assignment_target = 0;
276 assign.left->visit(*this);
277 assign.target_declaration = r_assignment_target;
280 r_self_referencing = false;
281 assign.right->visit(*this);
282 assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='=');
288 void VariableResolver::visit(FunctionCall &call)
290 TraversingVisitor::visit(call);
295 void VariableResolver::visit(StructDeclaration &strct)
297 TraversingVisitor::visit(strct);
298 stage->types.insert(make_pair(strct.name, &strct));
301 void VariableResolver::visit(VariableDeclaration &var)
303 map<string, StructDeclaration *>::iterator i = stage->types.find(var.type);
304 if(i!=stage->types.end())
305 var.type_declaration = i->second;
307 if(!block_interface.empty() && var.interface.empty())
308 var.interface = block_interface;
310 TraversingVisitor::visit(var);
311 current_block->variables.insert(make_pair(var.name, &var));
314 void VariableResolver::visit(InterfaceBlock &iface)
316 /* Block names can't be used for any other identifiers so we can put them
317 in the same map with instance names. */
318 stage->interface_blocks.insert(make_pair(iface.name, &iface));
319 if(!iface.instance_name.empty())
320 stage->interface_blocks.insert(make_pair(iface.instance_name, &iface));
322 SetForScope<string> set_iface(block_interface, iface.interface);
323 TraversingVisitor::visit(iface);
327 void FunctionResolver::apply(Stage &s)
331 s.content.visit(*this);
334 void FunctionResolver::visit(FunctionCall &call)
336 map<string, FunctionDeclaration *>::iterator i = stage->functions.find(call.name);
337 if(i!=stage->functions.end())
338 call.declaration = i->second;
340 TraversingVisitor::visit(call);
343 void FunctionResolver::visit(FunctionDeclaration &func)
345 FunctionDeclaration *&stage_decl = stage->functions[func.name];
346 vector<FunctionDeclaration *> &decls = declarations[func.name];
347 if(func.definition==&func)
351 // Set all previous declarations to use this definition.
352 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
354 (*i)->definition = func.definition;
355 (*i)->body.body.clear();
364 func.definition = stage_decl->definition;
366 decls.push_back(&func);
368 TraversingVisitor::visit(func);
372 InterfaceGenerator::InterfaceGenerator():
374 function_scope(false),
377 iface_target_block(0)
380 string InterfaceGenerator::get_out_prefix(Stage::Type type)
382 if(type==Stage::VERTEX)
384 else if(type==Stage::GEOMETRY)
390 void InterfaceGenerator::apply(Stage &s)
393 iface_target_block = &stage->content;
395 in_prefix = get_out_prefix(stage->previous->type);
396 out_prefix = get_out_prefix(stage->type);
397 s.content.visit(*this);
398 NodeRemover().apply(s, nodes_to_remove);
401 void InterfaceGenerator::visit(Block &block)
403 SetForScope<Block *> set_block(current_block, &block);
404 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
406 assignment_insert_point = i;
407 if(&block==&stage->content)
408 iface_insert_point = i;
414 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
416 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
417 return prefix+name.substr(offset);
420 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
422 if(stage->content.variables.count(name))
425 VariableDeclaration* iface_var = new VariableDeclaration;
426 iface_var->sampling = var.sampling;
427 iface_var->interface = iface;
428 iface_var->type = var.type;
429 iface_var->name = name;
430 /* Geometry shader inputs are always arrays. But if we're bringing in an
431 entire block, the array is on the block and not individual variables. */
432 if(stage->type==Stage::GEOMETRY && !copy_block)
433 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
435 iface_var->array = var.array;
437 iface_var->array_size = var.array_size;
440 iface_var->layout = var.layout;
441 iface_var->linked_declaration = &var;
442 var.linked_declaration = iface_var;
445 iface_target_block->body.insert(iface_insert_point, iface_var);
446 iface_target_block->variables.insert(make_pair(name, iface_var));
451 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
453 if(stage->interface_blocks.count(out_block.name))
456 InterfaceBlock *in_block = new InterfaceBlock;
457 in_block->interface = "in";
458 in_block->name = out_block.name;
459 in_block->instance_name = out_block.instance_name;
460 if(stage->type==Stage::GEOMETRY)
461 in_block->array = true;
463 in_block->array = out_block.array;
464 in_block->linked_block = &out_block;
465 out_block.linked_block = in_block;
468 SetFlag set_copy(copy_block, true);
469 SetForScope<Block *> set_target(iface_target_block, &in_block->members);
470 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
471 out_block.members.visit(*this);
474 iface_target_block->body.insert(iface_insert_point, in_block);
475 stage->interface_blocks.insert(make_pair(in_block->name, in_block));
476 if(!in_block->instance_name.empty())
477 stage->interface_blocks.insert(make_pair(in_block->instance_name, in_block));
479 SetFlag set_scope(function_scope, false);
480 SetForScope<Block *> set_block(current_block, &stage->content);
481 in_block->visit(*this);
486 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
488 Assignment *assign = new Assignment;
489 VariableReference *ref = new VariableReference;
492 assign->oper = &Operator::get_operator("=", Operator::BINARY);
493 assign->right = right;
495 ExpressionStatement *stmt = new ExpressionStatement;
496 stmt->expression = assign;
497 current_block->body.insert(assignment_insert_point, stmt);
503 void InterfaceGenerator::visit(VariableReference &var)
505 if(var.declaration || !stage->previous)
507 /* Don't pull a variable from previous stage if we just generated an output
508 interface in this stage */
509 if(stage->content.variables.count(var.name))
512 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
513 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
514 if(i==prev_vars.end() || i->second->interface!="out")
515 i = prev_vars.find(in_prefix+var.name);
516 if(i!=prev_vars.end() && i->second->interface=="out")
518 generate_interface(*i->second, "in", i->second->name);
519 var.name = i->second->name;
523 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
524 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find(var.name);
525 if(j!=prev_blocks.end() && j->second->interface=="out" && j->second->instance_name==var.name)
527 generate_interface(*j->second);
531 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
532 if(j->second->instance_name.empty())
534 i = j->second->members.variables.find(var.name);
535 if(i!=j->second->members.variables.end())
537 generate_interface(*j->second);
543 void InterfaceGenerator::visit(VariableDeclaration &var)
547 generate_interface(var, "in", var.name);
553 if(iface_block->linked_block)
555 // Link all variables to their counterparts in the linked block.
556 const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
557 map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
558 if(i!=linked_vars.end())
559 var.linked_declaration = i->second;
564 if(var.interface=="out")
566 /* For output variables in function scope, generate a global interface
567 and replace the local declaration with an assignment. */
568 VariableDeclaration *out_var = 0;
569 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
571 out_var->source = var.source;
572 out_var->line = var.line;
573 nodes_to_remove.insert(&var);
574 if(var.init_expression)
576 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
577 stmt.source = var.source;
578 stmt.line = var.line;
583 else if(var.interface=="in")
585 /* Try to link input variables in global scope with output variables from
587 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
589 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
590 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
591 if(i!=prev_vars.end() && i->second->interface=="out")
593 var.linked_declaration = i->second;
594 i->second->linked_declaration = &var;
599 TraversingVisitor::visit(var);
602 void InterfaceGenerator::visit(InterfaceBlock &iface)
604 if(iface.interface=="in")
606 /* Try to link input blocks with output blocks sharing the same block
607 name from previous stage. */
608 if(!iface.linked_block && stage->previous)
610 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
611 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find(iface.name);
612 if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name)
614 iface.linked_block = i->second;
615 i->second->linked_block = &iface;
620 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
621 TraversingVisitor::visit(iface);
624 void InterfaceGenerator::visit(FunctionDeclaration &func)
626 SetFlag set_scope(function_scope, true);
627 // Skip parameters because they're not useful here
628 func.body.visit(*this);
631 void InterfaceGenerator::visit(Passthrough &pass)
633 vector<VariableDeclaration *> pass_vars;
635 // Pass through all input variables of this stage.
636 for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
637 if(i->second->interface=="in")
638 pass_vars.push_back(i->second);
642 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
643 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
645 if(i->second->interface!="out")
648 /* Pass through output variables from the previous stage, but only
649 those which are not already linked to an input here. */
650 if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name))
651 pass_vars.push_back(i->second);
655 if(stage->type==Stage::GEOMETRY)
657 /* Special case for geometry shader: copy gl_Position from input to
659 InterfaceBlockReference *ref = new InterfaceBlockReference;
662 BinaryExpression *subscript = new BinaryExpression;
663 subscript->left = ref;
664 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
665 subscript->right = pass.subscript;
667 MemberAccess *memacc = new MemberAccess;
668 memacc->left = subscript;
669 memacc->member = "gl_Position";
671 insert_assignment("gl_Position", memacc);
674 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
676 string out_name = change_prefix((*i)->name, out_prefix);
677 generate_interface(**i, "out", out_name);
679 VariableReference *ref = new VariableReference;
680 ref->name = (*i)->name;
683 BinaryExpression *subscript = new BinaryExpression;
684 subscript->left = ref;
685 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
686 subscript->right = pass.subscript;
687 insert_assignment(out_name, subscript);
690 insert_assignment(out_name, ref);
693 nodes_to_remove.insert(&pass);