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;
103 literal->token = (i->second ? "true" : "false");
104 literal->value = static_cast<bool>(i->second);
106 else if(var.type=="int")
108 literal->token = lexical_cast<string>(i->second);
109 literal->value = i->second;
111 var.init_expression = literal;
117 void BlockHierarchyResolver::enter(Block &block)
119 block.parent = current_block;
123 TypeResolver::TypeResolver():
127 void TypeResolver::apply(Stage &s)
131 s.content.visit(*this);
134 TypeDeclaration *TypeResolver::resolve_type(const string &name)
136 map<string, TypeDeclaration *>::iterator i = stage->types.find(name);
137 if(i!=stage->types.end())
139 map<TypeDeclaration *, TypeDeclaration *>::iterator j = alias_map.find(i->second);
140 return (j!=alias_map.end() ? j->second : i->second);
146 void TypeResolver::visit(Block &block)
148 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
151 type_insert_point = i;
156 void TypeResolver::visit(BasicTypeDeclaration &type)
158 type.base_type = resolve_type(type.base);
160 if(type.kind==BasicTypeDeclaration::VECTOR && type.base_type)
161 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
162 if(basic_base->kind==BasicTypeDeclaration::VECTOR)
164 type.kind = BasicTypeDeclaration::MATRIX;
165 type.size |= basic_base->size<<16;
168 if(type.kind==BasicTypeDeclaration::ALIAS && type.base_type)
169 alias_map[&type] = type.base_type;
170 else if(type.kind==BasicTypeDeclaration::ARRAY && type.base_type)
171 array_types[type.base_type] = &type;
173 stage->types.insert(make_pair(type.name, &type));
176 void TypeResolver::visit(ImageTypeDeclaration &type)
178 type.base_type = resolve_type(type.base);
179 stage->types.insert(make_pair(type.name, &type));
182 void TypeResolver::visit(StructDeclaration &strct)
184 stage->types.insert(make_pair(strct.name, &strct));
185 TraversingVisitor::visit(strct);
188 void TypeResolver::visit(VariableDeclaration &var)
190 TypeDeclaration *type = resolve_type(var.type);
191 if(var.array && type)
193 map<TypeDeclaration *, TypeDeclaration *>::iterator i = array_types.find(type);
194 if(i==array_types.end())
196 BasicTypeDeclaration *array = new BasicTypeDeclaration;
197 array->source = BUILTIN_SOURCE;
198 array->name = type->name+"[]";
199 array->kind = BasicTypeDeclaration::ARRAY;
200 array->base = type->name;
201 array->base_type = type;
202 stage->content.body.insert(type_insert_point, array);
209 var.type_declaration = type;
212 void TypeResolver::visit(FunctionDeclaration &func)
214 func.return_type_declaration = resolve_type(func.return_type);
215 TraversingVisitor::visit(func);
219 VariableResolver::VariableResolver():
222 record_target(false),
223 r_self_referencing(false),
224 r_assignment_target(0)
227 void VariableResolver::apply(Stage &s)
230 s.interface_blocks.clear();
231 s.content.visit(*this);
234 void VariableResolver::enter(Block &block)
236 block.variables.clear();
239 void VariableResolver::visit(VariableReference &var)
243 /* Look for variable declarations in the block hierarchy first. Interface
244 blocks are always defined in the top level so we can't accidentally skip
246 for(Block *block=current_block; (!var.declaration && block); block=block->parent)
248 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
249 if(i!=block->variables.end())
250 var.declaration = i->second;
255 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
256 map<string, InterfaceBlock *>::const_iterator i = blocks.find("_"+var.name);
259 /* The name refers to an interface block with an instance name rather
260 than a variable. Prepare a new syntax tree node accordingly. */
261 r_iface_ref = new InterfaceBlockReference;
262 r_iface_ref->source = var.source;
263 r_iface_ref->line = var.line;
264 r_iface_ref->name = var.name;
265 r_iface_ref->declaration = i->second;
266 r_members = &i->second->members.variables;
270 // Look for the variable in anonymous interface blocks.
271 for(i=blocks.begin(); (!var.declaration && i!=blocks.end()); ++i)
272 if(i->second->instance_name.empty())
274 map<string, VariableDeclaration *>::iterator j = i->second->members.variables.find(var.name);
275 if(j!=i->second->members.variables.end())
276 var.declaration = j->second;
282 if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(var.declaration->type_declaration))
283 r_members = &strct->members.variables;
287 if(r_assignment_target)
289 /* More than one variable reference found in assignment target.
290 Unable to determine what the primary target is. */
291 record_target = false;
292 r_assignment_target = 0;
295 r_assignment_target = var.declaration;
297 else if(var.declaration && var.declaration==r_assignment_target)
298 r_self_referencing = true;
301 void VariableResolver::visit(InterfaceBlockReference &iface)
303 iface.declaration = 0;
304 for(Block *block=current_block; block; block=block->parent)
306 map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find("_"+iface.name);
307 if(i!=stage->interface_blocks.end())
309 iface.declaration = i->second;
310 r_members = &i->second->members.variables;
316 void VariableResolver::visit(MemberAccess &memacc)
320 memacc.left->visit(*this);
323 memacc.left = r_iface_ref;
326 memacc.declaration = 0;
329 map<string, VariableDeclaration *>::iterator i = r_members->find(memacc.member);
330 if(i!=r_members->end())
332 memacc.declaration = i->second;
333 if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(i->second->type_declaration))
334 r_members = &strct->members.variables;
341 void VariableResolver::visit(UnaryExpression &unary)
343 TraversingVisitor::visit(unary);
348 void VariableResolver::visit(BinaryExpression &binary)
350 if(binary.oper->token[0]=='[')
353 /* The subscript expression is not a part of the primary assignment
355 SetFlag set(record_target, false);
356 binary.right->visit(*this);
360 binary.left->visit(*this);
362 binary.left = r_iface_ref;
366 TraversingVisitor::visit(binary);
373 void VariableResolver::visit(Assignment &assign)
376 SetFlag set(record_target);
377 r_assignment_target = 0;
378 assign.left->visit(*this);
379 assign.target_declaration = r_assignment_target;
382 r_self_referencing = false;
383 assign.right->visit(*this);
384 assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='=');
390 void VariableResolver::visit(FunctionCall &call)
392 TraversingVisitor::visit(call);
397 void VariableResolver::visit(VariableDeclaration &var)
399 if(!block_interface.empty() && var.interface.empty())
400 var.interface = block_interface;
402 TraversingVisitor::visit(var);
403 current_block->variables.insert(make_pair(var.name, &var));
406 void VariableResolver::visit(InterfaceBlock &iface)
408 /* Block names can be reused in different interfaces. Prefix the name with
409 the first character of the interface to avoid conflicts. */
410 stage->interface_blocks.insert(make_pair(iface.interface+iface.name, &iface));
411 if(!iface.instance_name.empty())
412 stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface));
414 SetForScope<string> set_iface(block_interface, iface.interface);
415 TraversingVisitor::visit(iface);
419 void FunctionResolver::apply(Stage &s)
423 s.content.visit(*this);
426 void FunctionResolver::visit(FunctionCall &call)
428 map<string, FunctionDeclaration *>::iterator i = stage->functions.find(call.name);
429 if(i!=stage->functions.end())
430 call.declaration = i->second;
432 TraversingVisitor::visit(call);
435 void FunctionResolver::visit(FunctionDeclaration &func)
437 FunctionDeclaration *&stage_decl = stage->functions[func.name];
438 vector<FunctionDeclaration *> &decls = declarations[func.name];
439 if(func.definition==&func)
443 // Set all previous declarations to use this definition.
444 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
446 (*i)->definition = func.definition;
447 (*i)->body.body.clear();
456 func.definition = stage_decl->definition;
458 decls.push_back(&func);
460 TraversingVisitor::visit(func);
464 InterfaceGenerator::InterfaceGenerator():
466 function_scope(false),
469 iface_target_block(0)
472 string InterfaceGenerator::get_out_prefix(Stage::Type type)
474 if(type==Stage::VERTEX)
476 else if(type==Stage::GEOMETRY)
482 void InterfaceGenerator::apply(Stage &s)
485 iface_target_block = &stage->content;
487 in_prefix = get_out_prefix(stage->previous->type);
488 out_prefix = get_out_prefix(stage->type);
489 s.content.visit(*this);
490 NodeRemover().apply(s, nodes_to_remove);
493 void InterfaceGenerator::visit(Block &block)
495 SetForScope<Block *> set_block(current_block, &block);
496 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
498 assignment_insert_point = i;
499 if(&block==&stage->content)
500 iface_insert_point = i;
506 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
508 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
509 return prefix+name.substr(offset);
512 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
514 if(stage->content.variables.count(name))
517 VariableDeclaration* iface_var = new VariableDeclaration;
518 iface_var->sampling = var.sampling;
519 iface_var->interface = iface;
520 iface_var->type = var.type;
521 iface_var->name = name;
522 /* Geometry shader inputs are always arrays. But if we're bringing in an
523 entire block, the array is on the block and not individual variables. */
524 if(stage->type==Stage::GEOMETRY && !copy_block)
525 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
527 iface_var->array = var.array;
529 iface_var->array_size = var.array_size;
532 iface_var->layout = var.layout;
533 iface_var->linked_declaration = &var;
534 var.linked_declaration = iface_var;
537 iface_target_block->body.insert(iface_insert_point, iface_var);
538 iface_target_block->variables.insert(make_pair(name, iface_var));
543 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
545 if(stage->interface_blocks.count("in"+out_block.name))
548 InterfaceBlock *in_block = new InterfaceBlock;
549 in_block->interface = "in";
550 in_block->name = out_block.name;
551 in_block->instance_name = out_block.instance_name;
552 if(stage->type==Stage::GEOMETRY)
553 in_block->array = true;
555 in_block->array = out_block.array;
556 in_block->linked_block = &out_block;
557 out_block.linked_block = in_block;
560 SetFlag set_copy(copy_block, true);
561 SetForScope<Block *> set_target(iface_target_block, &in_block->members);
562 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
563 out_block.members.visit(*this);
566 iface_target_block->body.insert(iface_insert_point, in_block);
567 stage->interface_blocks.insert(make_pair("in"+in_block->name, in_block));
568 if(!in_block->instance_name.empty())
569 stage->interface_blocks.insert(make_pair("_"+in_block->instance_name, in_block));
571 SetFlag set_scope(function_scope, false);
572 SetForScope<Block *> set_block(current_block, &stage->content);
573 in_block->visit(*this);
578 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
580 Assignment *assign = new Assignment;
581 VariableReference *ref = new VariableReference;
584 assign->oper = &Operator::get_operator("=", Operator::BINARY);
585 assign->right = right;
587 ExpressionStatement *stmt = new ExpressionStatement;
588 stmt->expression = assign;
589 current_block->body.insert(assignment_insert_point, stmt);
595 void InterfaceGenerator::visit(VariableReference &var)
597 if(var.declaration || !stage->previous)
599 /* Don't pull a variable from previous stage if we just generated an output
600 interface in this stage */
601 if(stage->content.variables.count(var.name))
604 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
605 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
606 if(i==prev_vars.end() || i->second->interface!="out")
607 i = prev_vars.find(in_prefix+var.name);
608 if(i!=prev_vars.end() && i->second->interface=="out")
610 generate_interface(*i->second, "in", i->second->name);
611 var.name = i->second->name;
615 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
616 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find("_"+var.name);
617 if(j!=prev_blocks.end() && j->second->interface=="out")
619 generate_interface(*j->second);
620 /* Let VariableResolver convert the variable reference into an interface
625 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
626 if(j->second->instance_name.empty())
628 i = j->second->members.variables.find(var.name);
629 if(i!=j->second->members.variables.end())
631 generate_interface(*j->second);
637 void InterfaceGenerator::visit(VariableDeclaration &var)
641 generate_interface(var, "in", var.name);
647 if(iface_block->linked_block)
649 // Link all variables to their counterparts in the linked block.
650 const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
651 map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
652 if(i!=linked_vars.end())
654 var.linked_declaration = i->second;
655 var.linked_declaration->linked_declaration = &var;
661 if(var.interface=="out")
663 /* For output variables in function scope, generate a global interface
664 and replace the local declaration with an assignment. */
665 VariableDeclaration *out_var = 0;
666 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
668 out_var->source = var.source;
669 out_var->line = var.line;
670 nodes_to_remove.insert(&var);
671 if(var.init_expression)
673 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
674 stmt.source = var.source;
675 stmt.line = var.line;
680 else if(var.interface=="in")
682 /* Try to link input variables in global scope with output variables from
684 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
686 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
687 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
688 if(i!=prev_vars.end() && i->second->interface=="out")
690 var.linked_declaration = i->second;
691 i->second->linked_declaration = &var;
696 TraversingVisitor::visit(var);
699 void InterfaceGenerator::visit(InterfaceBlock &iface)
701 if(iface.interface=="in")
703 /* Try to link input blocks with output blocks sharing the same block
704 name from previous stage. */
705 if(!iface.linked_block && stage->previous)
707 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
708 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find("out"+iface.name);
709 if(i!=prev_blocks.end())
711 iface.linked_block = i->second;
712 i->second->linked_block = &iface;
717 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
718 TraversingVisitor::visit(iface);
721 void InterfaceGenerator::visit(FunctionDeclaration &func)
723 SetFlag set_scope(function_scope, true);
724 // Skip parameters because they're not useful here
725 func.body.visit(*this);
728 void InterfaceGenerator::visit(Passthrough &pass)
730 vector<VariableDeclaration *> pass_vars;
732 // Pass through all input variables of this stage.
733 for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
734 if(i->second->interface=="in")
735 pass_vars.push_back(i->second);
739 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
740 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
742 if(i->second->interface!="out")
745 /* Pass through output variables from the previous stage, but only
746 those which are not already linked to an input here. */
747 if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name))
748 pass_vars.push_back(i->second);
752 if(stage->type==Stage::GEOMETRY)
754 /* Special case for geometry shader: copy gl_Position from input to
756 InterfaceBlockReference *ref = new InterfaceBlockReference;
759 BinaryExpression *subscript = new BinaryExpression;
760 subscript->left = ref;
761 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
762 subscript->right = pass.subscript;
764 MemberAccess *memacc = new MemberAccess;
765 memacc->left = subscript;
766 memacc->member = "gl_Position";
768 insert_assignment("gl_Position", memacc);
771 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
773 string out_name = change_prefix((*i)->name, out_prefix);
774 generate_interface(**i, "out", out_name);
776 VariableReference *ref = new VariableReference;
777 ref->name = (*i)->name;
780 BinaryExpression *subscript = new BinaryExpression;
781 subscript->left = ref;
782 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
783 subscript->right = pass.subscript;
784 insert_assignment(out_name, subscript);
787 insert_assignment(out_name, ref);
790 nodes_to_remove.insert(&pass);