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 bool ExpressionResolver::is_scalar(BasicTypeDeclaration &type)
421 return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT);
424 bool ExpressionResolver::is_vector_or_matrix(BasicTypeDeclaration &type)
426 return (type.kind==BasicTypeDeclaration::VECTOR || type.kind==BasicTypeDeclaration::MATRIX);
429 BasicTypeDeclaration *ExpressionResolver::get_element_type(BasicTypeDeclaration &type)
431 if(is_vector_or_matrix(type) || type.kind==BasicTypeDeclaration::ARRAY)
433 BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
434 return (basic_base ? get_element_type(*basic_base) : 0);
440 bool ExpressionResolver::can_convert(BasicTypeDeclaration &from, BasicTypeDeclaration &to)
442 if(from.kind==BasicTypeDeclaration::INT && to.kind==BasicTypeDeclaration::FLOAT)
443 return from.size<=to.size;
444 else if(from.kind!=to.kind)
446 else if((from.kind==BasicTypeDeclaration::VECTOR || from.kind==BasicTypeDeclaration::MATRIX) && from.size==to.size)
448 BasicTypeDeclaration *from_base = dynamic_cast<BasicTypeDeclaration *>(from.base_type);
449 BasicTypeDeclaration *to_base = dynamic_cast<BasicTypeDeclaration *>(to.base_type);
450 return (from_base && to_base && can_convert(*from_base, *to_base));
456 ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTypeDeclaration &left, BasicTypeDeclaration &right)
460 else if(can_convert(left, right))
461 return LEFT_CONVERTIBLE;
462 else if(can_convert(right, left))
463 return RIGHT_CONVERTIBLE;
465 return NOT_COMPATIBLE;
468 BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size)
470 for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
471 if((*i)->kind==kind && (*i)->size==size)
476 BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration &elem_type, BasicTypeDeclaration::Kind kind, unsigned size)
478 for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
479 if(get_element_type(**i)==&elem_type && (*i)->kind==kind && (*i)->size==size)
484 void ExpressionResolver::convert_to(RefPtr<Expression> &expr, BasicTypeDeclaration &type)
486 RefPtr<FunctionCall> call = new FunctionCall;
487 call->name = type.name;
488 call->constructor = true;
489 call->arguments.push_back(0);
490 call->arguments.back() = expr;
495 bool ExpressionResolver::convert_to_element(RefPtr<Expression> &expr, BasicTypeDeclaration &elem_type)
497 if(BasicTypeDeclaration *expr_type = dynamic_cast<BasicTypeDeclaration *>(expr->type))
499 BasicTypeDeclaration *to_type = &elem_type;
500 if(is_vector_or_matrix(*expr_type))
501 to_type = find_type(elem_type, expr_type->kind, expr_type->size);
504 convert_to(expr, *to_type);
512 void ExpressionResolver::visit(Literal &literal)
514 if(literal.value.check_type<bool>())
515 literal.type = find_type(BasicTypeDeclaration::BOOL, 1);
516 else if(literal.value.check_type<int>())
517 literal.type = find_type(BasicTypeDeclaration::INT, 32);
518 else if(literal.value.check_type<float>())
519 literal.type = find_type(BasicTypeDeclaration::FLOAT, 32);
522 void ExpressionResolver::visit(ParenthesizedExpression &parexpr)
524 TraversingVisitor::visit(parexpr);
526 parexpr.type = parexpr.expression->type;
527 parexpr.lvalue = parexpr.expression->lvalue;
530 void ExpressionResolver::visit(VariableReference &var)
533 var.type = var.declaration->type_declaration;
537 void ExpressionResolver::visit(InterfaceBlockReference &iface)
542 void ExpressionResolver::visit(MemberAccess &memacc)
544 TraversingVisitor::visit(memacc);
546 if(memacc.declaration)
547 memacc.type = memacc.declaration->type_declaration;
548 memacc.lvalue = memacc.left->lvalue;
551 void ExpressionResolver::visit(UnaryExpression &unary)
553 TraversingVisitor::visit(unary);
555 BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(unary.expression->type);
559 char oper = unary.oper->token[0];
562 if(basic->kind==BasicTypeDeclaration::BOOL)
567 if(basic->kind==BasicTypeDeclaration::INT)
570 else if(oper=='+' || oper=='-')
572 BasicTypeDeclaration *elem = get_element_type(*basic);
573 if(elem && is_scalar(*elem))
576 unary.lvalue = unary.expression->lvalue;
579 void ExpressionResolver::visit(BinaryExpression &binary)
581 TraversingVisitor::visit(binary);
583 /* Binary operators are only defined for basic types (not for image or
585 BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(binary.left->type);
586 BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(binary.right->type);
587 if(!basic_left || !basic_right)
590 binary.lvalue = false;
592 char oper = binary.oper->token[0];
595 /* Subscripting operates on vectors, matrices and arrays, and the right
596 operand must be an integer. */
597 if((!is_vector_or_matrix(*basic_left) && basic_left->kind!=BasicTypeDeclaration::ARRAY) || basic_right->kind!=BasicTypeDeclaration::INT)
600 binary.type = basic_left->base_type;
601 binary.lvalue = binary.left->lvalue;
604 else if(basic_left->kind==BasicTypeDeclaration::ARRAY || basic_right->kind==BasicTypeDeclaration::ARRAY)
605 // No other binary operator can be used with arrays.
608 BasicTypeDeclaration *elem_left = get_element_type(*basic_left);
609 BasicTypeDeclaration *elem_right = get_element_type(*basic_right);
610 if(!elem_left || !elem_right)
613 Compatibility compat = get_compatibility(*basic_left, *basic_right);
614 Compatibility elem_compat = get_compatibility(*elem_left, *elem_right);
615 if(elem_compat==NOT_COMPATIBLE)
618 char oper2 = binary.oper->token[1];
619 if((oper=='<' && oper2!='<') || (oper=='>' && oper2!='>'))
621 /* Relational operators compare two scalar integer or floating-point
623 if(!is_scalar(*elem_left) || !is_scalar(*elem_right) || compat==NOT_COMPATIBLE)
626 binary.type = find_type(BasicTypeDeclaration::BOOL, 1);
628 else if((oper=='=' || oper=='!') && oper2=='=')
630 // Equality comparison can be done on any compatible types.
631 if(compat==NOT_COMPATIBLE)
634 binary.type = find_type(BasicTypeDeclaration::BOOL, 1);
636 else if(oper2=='&' || oper2=='|' || oper2=='^')
638 // Logical operators can only be applied to booleans.
639 if(basic_left->kind!=BasicTypeDeclaration::BOOL || basic_right->kind!=BasicTypeDeclaration::BOOL)
642 binary.type = basic_left;
644 else if((oper=='&' || oper=='|' || oper=='^' || oper=='%') && !oper2)
646 // Bitwise operators and modulo can only be applied to integers.
647 if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT)
650 binary.type = (compat==LEFT_CONVERTIBLE ? basic_right : basic_left);
652 else if((oper=='<' || oper=='>') && oper2==oper)
654 // Shifts only apply to integers.
655 if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT)
658 binary.type = basic_left;
660 else if(oper=='+' || oper=='-' || oper=='*' || oper=='/')
662 // Arithmetic operators require scalar elements.
663 if(!is_scalar(*elem_left) || !is_scalar(*elem_right))
666 if(oper=='*' && is_vector_or_matrix(*basic_left) && is_vector_or_matrix(*basic_right) &&
667 (basic_left->kind==BasicTypeDeclaration::MATRIX || basic_right->kind==BasicTypeDeclaration::MATRIX))
669 /* Multiplication has special rules when at least one operand is a
670 matrix and the other is a vector or a matrix. */
671 unsigned left_columns = basic_left->size&0xFFFF;
672 unsigned right_rows = basic_right->size;
673 if(basic_right->kind==BasicTypeDeclaration::MATRIX)
675 if(left_columns!=right_rows)
678 BasicTypeDeclaration *elem_result = (elem_compat==LEFT_CONVERTIBLE ? elem_right : elem_left);
680 if(basic_left->kind==BasicTypeDeclaration::VECTOR)
681 binary.type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_right->size&0xFFFF);
682 else if(basic_right->kind==BasicTypeDeclaration::VECTOR)
683 binary.type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_left->size>>16);
685 binary.type = find_type(*elem_result, BasicTypeDeclaration::MATRIX, (basic_left->size&0xFFFF0000)|(basic_right->size&0xFFFF));
687 else if(compat==NOT_COMPATIBLE)
689 // Arithmetic between scalars and matrices or vectors is supported.
690 if(is_scalar(*basic_left) && is_vector_or_matrix(*basic_right))
691 binary.type = (elem_compat==RIGHT_CONVERTIBLE ? find_type(*elem_left, basic_right->kind, basic_right->size) : basic_right);
692 else if(is_vector_or_matrix(*basic_left) && is_scalar(*basic_right))
693 binary.type = (elem_compat==LEFT_CONVERTIBLE ? find_type(*elem_right, basic_left->kind, basic_left->size) : basic_left);
697 else if(compat==LEFT_CONVERTIBLE)
698 binary.type = basic_right;
700 binary.type = basic_left;
705 bool converted = true;
706 if(compat==LEFT_CONVERTIBLE)
707 convert_to(binary.left, *basic_right);
708 else if(compat==RIGHT_CONVERTIBLE)
709 convert_to(binary.right, *basic_left);
710 else if(elem_compat==LEFT_CONVERTIBLE)
711 converted = convert_to_element(binary.left, *elem_right);
712 else if(elem_compat==RIGHT_CONVERTIBLE)
713 converted = convert_to_element(binary.right, *elem_left);
719 void ExpressionResolver::visit(Assignment &assign)
721 TraversingVisitor::visit(assign);
722 assign.type = assign.left->type;
723 assign.lvalue = true;
726 void ExpressionResolver::visit(FunctionCall &call)
728 TraversingVisitor::visit(call);
731 call.type = call.declaration->return_type_declaration;
732 else if(call.constructor)
734 map<string, TypeDeclaration *>::const_iterator i=stage->types.find(call.name);
735 call.type = (i!=stage->types.end() ? i->second : 0);
740 void ExpressionResolver::visit(BasicTypeDeclaration &type)
742 basic_types.push_back(&type);
745 void ExpressionResolver::visit(VariableDeclaration &var)
747 TraversingVisitor::visit(var);
748 if(!var.init_expression)
751 BasicTypeDeclaration *var_basic = dynamic_cast<BasicTypeDeclaration *>(var.type_declaration);
752 BasicTypeDeclaration *init_basic = dynamic_cast<BasicTypeDeclaration *>(var.init_expression->type);
753 if(!var_basic || !init_basic)
756 Compatibility compat = get_compatibility(*var_basic, *init_basic);
757 if(compat==RIGHT_CONVERTIBLE)
758 convert_to(var.init_expression, *var_basic);
762 void FunctionResolver::apply(Stage &s)
766 s.content.visit(*this);
769 void FunctionResolver::visit(FunctionCall &call)
771 map<string, FunctionDeclaration *>::iterator i = stage->functions.find(call.name);
772 if(i!=stage->functions.end())
773 call.declaration = i->second;
775 TraversingVisitor::visit(call);
778 void FunctionResolver::visit(FunctionDeclaration &func)
780 FunctionDeclaration *&stage_decl = stage->functions[func.name];
781 vector<FunctionDeclaration *> &decls = declarations[func.name];
782 if(func.definition==&func)
786 // Set all previous declarations to use this definition.
787 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
789 (*i)->definition = func.definition;
790 (*i)->body.body.clear();
799 func.definition = stage_decl->definition;
801 decls.push_back(&func);
803 TraversingVisitor::visit(func);
807 InterfaceGenerator::InterfaceGenerator():
809 function_scope(false),
812 iface_target_block(0)
815 string InterfaceGenerator::get_out_prefix(Stage::Type type)
817 if(type==Stage::VERTEX)
819 else if(type==Stage::GEOMETRY)
825 void InterfaceGenerator::apply(Stage &s)
828 iface_target_block = &stage->content;
830 in_prefix = get_out_prefix(stage->previous->type);
831 out_prefix = get_out_prefix(stage->type);
832 s.content.visit(*this);
833 NodeRemover().apply(s, nodes_to_remove);
836 void InterfaceGenerator::visit(Block &block)
838 SetForScope<Block *> set_block(current_block, &block);
839 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
841 assignment_insert_point = i;
842 if(&block==&stage->content)
843 iface_insert_point = i;
849 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
851 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
852 return prefix+name.substr(offset);
855 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
857 if(stage->content.variables.count(name))
860 VariableDeclaration* iface_var = new VariableDeclaration;
861 iface_var->sampling = var.sampling;
862 iface_var->interface = iface;
863 iface_var->type = var.type;
864 iface_var->name = name;
865 /* Geometry shader inputs are always arrays. But if we're bringing in an
866 entire block, the array is on the block and not individual variables. */
867 if(stage->type==Stage::GEOMETRY && !copy_block)
868 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
870 iface_var->array = var.array;
872 iface_var->array_size = var.array_size;
875 iface_var->layout = var.layout;
876 iface_var->linked_declaration = &var;
877 var.linked_declaration = iface_var;
880 iface_target_block->body.insert(iface_insert_point, iface_var);
881 iface_target_block->variables.insert(make_pair(name, iface_var));
886 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
888 if(stage->interface_blocks.count("in"+out_block.name))
891 InterfaceBlock *in_block = new InterfaceBlock;
892 in_block->interface = "in";
893 in_block->name = out_block.name;
894 in_block->instance_name = out_block.instance_name;
895 if(stage->type==Stage::GEOMETRY)
896 in_block->array = true;
898 in_block->array = out_block.array;
899 in_block->linked_block = &out_block;
900 out_block.linked_block = in_block;
903 SetFlag set_copy(copy_block, true);
904 SetForScope<Block *> set_target(iface_target_block, &in_block->members);
905 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
906 out_block.members.visit(*this);
909 iface_target_block->body.insert(iface_insert_point, in_block);
910 stage->interface_blocks.insert(make_pair("in"+in_block->name, in_block));
911 if(!in_block->instance_name.empty())
912 stage->interface_blocks.insert(make_pair("_"+in_block->instance_name, in_block));
914 SetFlag set_scope(function_scope, false);
915 SetForScope<Block *> set_block(current_block, &stage->content);
916 in_block->visit(*this);
921 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
923 Assignment *assign = new Assignment;
924 VariableReference *ref = new VariableReference;
927 assign->oper = &Operator::get_operator("=", Operator::BINARY);
928 assign->right = right;
930 ExpressionStatement *stmt = new ExpressionStatement;
931 stmt->expression = assign;
932 current_block->body.insert(assignment_insert_point, stmt);
938 void InterfaceGenerator::visit(VariableReference &var)
940 if(var.declaration || !stage->previous)
942 /* Don't pull a variable from previous stage if we just generated an output
943 interface in this stage */
944 if(stage->content.variables.count(var.name))
947 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
948 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
949 if(i==prev_vars.end() || i->second->interface!="out")
950 i = prev_vars.find(in_prefix+var.name);
951 if(i!=prev_vars.end() && i->second->interface=="out")
953 generate_interface(*i->second, "in", i->second->name);
954 var.name = i->second->name;
958 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
959 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find("_"+var.name);
960 if(j!=prev_blocks.end() && j->second->interface=="out")
962 generate_interface(*j->second);
963 /* Let VariableResolver convert the variable reference into an interface
968 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
969 if(j->second->instance_name.empty())
971 i = j->second->members.variables.find(var.name);
972 if(i!=j->second->members.variables.end())
974 generate_interface(*j->second);
980 void InterfaceGenerator::visit(VariableDeclaration &var)
984 generate_interface(var, "in", var.name);
990 if(iface_block->linked_block)
992 // Link all variables to their counterparts in the linked block.
993 const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
994 map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
995 if(i!=linked_vars.end())
997 var.linked_declaration = i->second;
998 var.linked_declaration->linked_declaration = &var;
1004 if(var.interface=="out")
1006 /* For output variables in function scope, generate a global interface
1007 and replace the local declaration with an assignment. */
1008 VariableDeclaration *out_var = 0;
1009 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
1011 out_var->source = var.source;
1012 out_var->line = var.line;
1013 nodes_to_remove.insert(&var);
1014 if(var.init_expression)
1016 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
1017 stmt.source = var.source;
1018 stmt.line = var.line;
1023 else if(var.interface=="in")
1025 /* Try to link input variables in global scope with output variables from
1027 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
1029 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1030 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
1031 if(i!=prev_vars.end() && i->second->interface=="out")
1033 var.linked_declaration = i->second;
1034 i->second->linked_declaration = &var;
1039 TraversingVisitor::visit(var);
1042 void InterfaceGenerator::visit(InterfaceBlock &iface)
1044 if(iface.interface=="in")
1046 /* Try to link input blocks with output blocks sharing the same block
1047 name from previous stage. */
1048 if(!iface.linked_block && stage->previous)
1050 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
1051 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find("out"+iface.name);
1052 if(i!=prev_blocks.end())
1054 iface.linked_block = i->second;
1055 i->second->linked_block = &iface;
1060 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
1061 TraversingVisitor::visit(iface);
1064 void InterfaceGenerator::visit(FunctionDeclaration &func)
1066 SetFlag set_scope(function_scope, true);
1067 // Skip parameters because they're not useful here
1068 func.body.visit(*this);
1071 void InterfaceGenerator::visit(Passthrough &pass)
1073 vector<VariableDeclaration *> pass_vars;
1075 // Pass through all input variables of this stage.
1076 for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
1077 if(i->second->interface=="in")
1078 pass_vars.push_back(i->second);
1082 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1083 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
1085 if(i->second->interface!="out")
1088 /* Pass through output variables from the previous stage, but only
1089 those which are not already linked to an input here. */
1090 if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name))
1091 pass_vars.push_back(i->second);
1095 if(stage->type==Stage::GEOMETRY)
1097 /* Special case for geometry shader: copy gl_Position from input to
1099 InterfaceBlockReference *ref = new InterfaceBlockReference;
1100 ref->name = "gl_in";
1102 BinaryExpression *subscript = new BinaryExpression;
1103 subscript->left = ref;
1104 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
1105 subscript->right = pass.subscript;
1107 MemberAccess *memacc = new MemberAccess;
1108 memacc->left = subscript;
1109 memacc->member = "gl_Position";
1111 insert_assignment("gl_Position", memacc);
1114 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
1116 string out_name = change_prefix((*i)->name, out_prefix);
1117 generate_interface(**i, "out", out_name);
1119 VariableReference *ref = new VariableReference;
1120 ref->name = (*i)->name;
1123 BinaryExpression *subscript = new BinaryExpression;
1124 subscript->left = ref;
1125 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
1126 subscript->right = pass.subscript;
1127 insert_assignment(out_name, subscript);
1130 insert_assignment(out_name, ref);
1133 nodes_to_remove.insert(&pass);