X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fglsl%2Fgenerate.cpp;h=d4623a7fd85064acf8eb0860c9e9dae149e2798c;hp=ca4d41e64c7a8c63bf51fd8d446e5ccb4dc8bd5a;hb=d8bdf61007978e2c3670a22a58e2f105e8347537;hpb=5c33b56c3b97ca0381ac216a603c7553f4bea499 diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index ca4d41e6..d4623a7f 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -1,4 +1,8 @@ +#include +#include #include +#include +#include #include "builtin.h" #include "generate.h" @@ -8,265 +12,1259 @@ namespace Msp { namespace GL { namespace SL { -void DeclarationCombiner::apply(Stage &stage) +ConstantSpecializer::ConstantSpecializer(): + values(0) +{ } + +void ConstantSpecializer::apply(Stage &stage, const map *v) { + values = v; stage.content.visit(*this); - NodeRemover().apply(stage, nodes_to_remove); } -void DeclarationCombiner::visit(Block &block) +void ConstantSpecializer::visit(VariableDeclaration &var) { - if(current_block) - return; + bool specializable = false; + if(var.layout) + { + vector &qualifiers = var.layout->qualifiers; + for(vector::iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i) + if(i->name=="constant_id") + { + specializable = true; + if(values) + qualifiers.erase(i); + else if(i->value==-1) + i->value = hash32(var.name)&0x7FFFFFFF; + break; + } + + if(qualifiers.empty()) + var.layout = 0; + } - TraversingVisitor::visit(block); + if(specializable && values) + { + map::const_iterator i = values->find(var.name); + if(i!=values->end()) + { + RefPtr literal = new Literal; + if(var.type=="bool") + { + literal->token = (i->second ? "true" : "false"); + literal->value = static_cast(i->second); + } + else if(var.type=="int") + { + literal->token = lexical_cast(i->second); + literal->value = i->second; + } + var.init_expression = literal; + } + } } -void DeclarationCombiner::visit(FunctionDeclaration &func) + +void BlockHierarchyResolver::enter(Block &block) { - vector &decls = functions[func.name]; - if(func.definition) + r_any_resolved |= (current_block!=block.parent); + block.parent = current_block; +} + + +TypeResolver::TypeResolver(): + stage(0), + iface_block(0), + r_any_resolved(false) +{ } + +bool TypeResolver::apply(Stage &s) +{ + stage = &s; + s.types.clear(); + r_any_resolved = false; + s.content.visit(*this); + return r_any_resolved; +} + +TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type) +{ + map::iterator i = array_types.find(&type); + if(i!=array_types.end()) + return i->second; + + BasicTypeDeclaration *array = new BasicTypeDeclaration; + array->source = BUILTIN_SOURCE; + array->name = type.name+"[]"; + array->kind = BasicTypeDeclaration::ARRAY; + array->base = type.name; + array->base_type = &type; + stage->content.body.insert(type_insert_point, array); + array_types[&type] = array; + return array; +} + +void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool array) +{ + TypeDeclaration *resolved = 0; + map::iterator i = stage->types.find(name); + if(i!=stage->types.end()) { - for(vector::iterator i=decls.begin(); i!=decls.end(); ++i) + map::iterator j = alias_map.find(i->second); + resolved = (j!=alias_map.end() ? j->second : i->second); + } + + if(resolved && array) + resolved = get_or_create_array_type(*resolved); + + r_any_resolved |= (resolved!=type); + type=resolved; +} + +void TypeResolver::visit(Block &block) +{ + for(NodeList::iterator i=block.body.begin(); i!=block.body.end(); ++i) + { + if(!block.parent) + type_insert_point = i; + (*i)->visit(*this); + } +} + +void TypeResolver::visit(BasicTypeDeclaration &type) +{ + resolve_type(type.base_type, type.base, false); + + if(type.kind==BasicTypeDeclaration::VECTOR && type.base_type) + if(BasicTypeDeclaration *basic_base = dynamic_cast(type.base_type)) + if(basic_base->kind==BasicTypeDeclaration::VECTOR) + { + type.kind = BasicTypeDeclaration::MATRIX; + /* A matrix's base type is its column vector type. This will put + the column vector's size, i.e. the matrix's row count, in the high + half of the size. */ + type.size |= basic_base->size<<16; + } + + if(type.kind==BasicTypeDeclaration::ALIAS && type.base_type) + alias_map[&type] = type.base_type; + else if(type.kind==BasicTypeDeclaration::ARRAY && type.base_type) + array_types[type.base_type] = &type; + + stage->types.insert(make_pair(type.name, &type)); +} + +void TypeResolver::visit(ImageTypeDeclaration &type) +{ + resolve_type(type.base_type, type.base, false); + stage->types.insert(make_pair(type.name, &type)); +} + +void TypeResolver::visit(StructDeclaration &strct) +{ + stage->types.insert(make_pair(strct.name, &strct)); + TraversingVisitor::visit(strct); +} + +void TypeResolver::visit(VariableDeclaration &var) +{ + resolve_type(var.type_declaration, var.type, var.array); + if(iface_block && var.interface==iface_block->interface) + var.interface.clear(); +} + +void TypeResolver::visit(InterfaceBlock &iface) +{ + if(iface.members) + { + SetForScope set_iface(iface_block, &iface); + iface.members->visit(*this); + + StructDeclaration *strct = new StructDeclaration; + strct->source = INTERNAL_SOURCE; + strct->name = format("_%s_%s", iface.interface, iface.block_name); + strct->members.body.splice(strct->members.body.begin(), iface.members->body); + stage->content.body.insert(type_insert_point, strct); + stage->types.insert(make_pair(strct->name, strct)); + + iface.members = 0; + strct->interface_block = &iface; + iface.struct_declaration = strct; + } + + TypeDeclaration *type = iface.struct_declaration; + if(type && iface.array) + type = get_or_create_array_type(*type); + r_any_resolved = (type!=iface.type_declaration); + iface.type_declaration = type; +} + +void TypeResolver::visit(FunctionDeclaration &func) +{ + resolve_type(func.return_type_declaration, func.return_type, false); + TraversingVisitor::visit(func); +} + + +VariableResolver::VariableResolver(): + stage(0), + r_any_resolved(false), + record_target(false), + r_self_referencing(false) +{ } + +bool VariableResolver::apply(Stage &s) +{ + stage = &s; + s.interface_blocks.clear(); + r_any_resolved = false; + s.content.visit(*this); + for(vector::const_iterator i=redeclared_builtins.begin(); i!=redeclared_builtins.end(); ++i) + (*i)->source = GENERATED_SOURCE; + NodeRemover().apply(s, nodes_to_remove); + return r_any_resolved; +} + +void VariableResolver::enter(Block &block) +{ + block.variables.clear(); +} + +void VariableResolver::visit(RefPtr &expr) +{ + r_replacement_expr = 0; + expr->visit(*this); + if(r_replacement_expr) + { + expr = r_replacement_expr; + /* Don't record assignment target when doing a replacement, because chain + information won't be correct. */ + r_assignment_target.declaration = 0; + r_any_resolved = true; + } + r_replacement_expr = 0; +} + +void VariableResolver::check_assignment_target(Statement *declaration) +{ + if(record_target) + { + if(r_assignment_target.declaration) { - (*i)->definition = func.definition; - (*i)->body.body.clear(); + /* More than one reference found in assignment target. Unable to + determine what the primary target is. */ + record_target = false; + r_assignment_target.declaration = 0; } + else + r_assignment_target.declaration = declaration; } - decls.push_back(&func); + // TODO This check is overly broad and may prevent some optimizations. + else if(declaration && declaration==r_assignment_target.declaration) + r_self_referencing = true; +} + +void VariableResolver::visit(VariableReference &var) +{ + VariableDeclaration *declaration = 0; + + /* Look for variable declarations in the block hierarchy first. Interface + blocks are always defined in the top level so we can't accidentally skip + one. */ + for(Block *block=current_block; (!declaration && block); block=block->parent) + { + map::iterator i = block->variables.find(var.name); + if(i!=block->variables.end()) + declaration = i->second; + } + + if(!declaration) + { + const map &blocks = stage->interface_blocks; + map::const_iterator i = blocks.find("_"+var.name); + if(i!=blocks.end()) + { + /* The name refers to an interface block with an instance name rather + than a variable. Prepare a new syntax tree node accordingly. */ + InterfaceBlockReference *iface_ref = new InterfaceBlockReference; + iface_ref->source = var.source; + iface_ref->line = var.line; + iface_ref->name = var.name; + iface_ref->declaration = i->second; + r_replacement_expr = iface_ref; + } + else + { + // Look for the variable in anonymous interface blocks. + for(i=blocks.begin(); (!declaration && i!=blocks.end()); ++i) + if(i->second->instance_name.empty() && i->second->struct_declaration) + { + const map &iface_vars = i->second->struct_declaration->members.variables; + map::const_iterator j = iface_vars.find(var.name); + if(j!=iface_vars.end()) + declaration = j->second; + } + } + } + + r_any_resolved |= (declaration!=var.declaration); + var.declaration = declaration; + + check_assignment_target(var.declaration); +} + +void VariableResolver::visit(InterfaceBlockReference &iface) +{ + map::iterator i = stage->interface_blocks.find("_"+iface.name); + InterfaceBlock *declaration = (i!=stage->interface_blocks.end() ? i->second : 0); + r_any_resolved |= (declaration!=iface.declaration); + iface.declaration = declaration; + + check_assignment_target(iface.declaration); +} + +void VariableResolver::add_to_chain(Assignment::Target::ChainType type, unsigned index) +{ + if(r_assignment_target.chain_len<7) + r_assignment_target.chain[r_assignment_target.chain_len] = type | min(index, 0x3F); + ++r_assignment_target.chain_len; } -void DeclarationCombiner::visit(VariableDeclaration &var) +void VariableResolver::visit(MemberAccess &memacc) +{ + TraversingVisitor::visit(memacc); + + VariableDeclaration *declaration = 0; + if(StructDeclaration *strct = dynamic_cast(memacc.left->type)) + { + map::iterator i = strct->members.variables.find(memacc.member); + if(i!=strct->members.variables.end()) + { + declaration = i->second; + + if(record_target) + { + unsigned index = 0; + for(NodeList::const_iterator j=strct->members.body.begin(); (j!=strct->members.body.end() && j->get()!=i->second); ++j) + ++index; + + add_to_chain(Assignment::Target::MEMBER, index); + } + } + } + else if(BasicTypeDeclaration *basic = dynamic_cast(memacc.left->type)) + { + bool scalar_swizzle = ((basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT) && memacc.member.size()==1); + bool vector_swizzle = (basic->kind==BasicTypeDeclaration::VECTOR && memacc.member.size()<=4); + if(scalar_swizzle || vector_swizzle) + { + static const char component_names[] = { 'x', 'r', 's', 'y', 'g', 't', 'z', 'b', 'p', 'w', 'a', 'q' }; + + bool ok = true; + UInt8 components[4] = { }; + for(unsigned i=0; (ok && isource = memacc.source; + swizzle->line = memacc.line; + swizzle->oper = memacc.oper; + swizzle->left = memacc.left; + swizzle->component_group = memacc.member; + swizzle->count = memacc.member.size(); + copy(components, components+memacc.member.size(), swizzle->components); + r_replacement_expr = swizzle; + } + } + } + + r_any_resolved |= (declaration!=memacc.declaration); + memacc.declaration = declaration; +} + +void VariableResolver::visit(Swizzle &swizzle) { - VariableDeclaration *&ptr = variables[var.name]; - if(ptr) + TraversingVisitor::visit(swizzle); + + if(record_target) + { + unsigned mask = 0; + for(unsigned i=0; itoken[0]=='[') + { + { + /* The subscript expression is not a part of the primary assignment + target. */ + SetFlag set(record_target, false); + visit(binary.right); + } + visit(binary.left); + + if(record_target) + { + unsigned index = 0x3F; + if(Literal *literal_subscript = dynamic_cast(binary.right.get())) + if(literal_subscript->value.check_type()) + index = literal_subscript->value.value(); + add_to_chain(Assignment::Target::ARRAY, index); + } + } + else + TraversingVisitor::visit(binary); +} + +void VariableResolver::visit(Assignment &assign) +{ + { + SetFlag set(record_target); + r_assignment_target = Assignment::Target(); + visit(assign.left); + r_any_resolved |= (r_assignment_targettoken[0]!='='); +} + +void VariableResolver::merge_layouts(Layout &to_layout, const Layout &from_layout) +{ + for(vector::const_iterator i=from_layout.qualifiers.begin(); i!=from_layout.qualifiers.end(); ++i) + { + bool found = false; + for(vector::iterator j=to_layout.qualifiers.begin(); (!found && j!=to_layout.qualifiers.end()); ++j) + if(j->name==i->name) + { + j->has_value = i->value; + j->value = i->value; + found = true; + } + + if(!found) + to_layout.qualifiers.push_back(*i); + } +} + +void VariableResolver::visit(VariableDeclaration &var) +{ + TraversingVisitor::visit(var); + VariableDeclaration *&ptr = current_block->variables[var.name]; + if(!ptr) + ptr = &var; + else if(!current_block->parent && ptr->interface==var.interface && ptr->type==var.type) + { + if(ptr->source==BUILTIN_SOURCE) + redeclared_builtins.push_back(&var); + else + stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line, + format("Redeclaring non-builtin variable '%s' is deprecated", var.name))); + + if(var.init_expression) + ptr->init_expression = var.init_expression; + if(var.layout) + { + if(ptr->layout) + merge_layouts(*ptr->layout, *var.layout); + else + ptr->layout = var.layout; + } + nodes_to_remove.insert(&var); + + r_any_resolved = true; + } +} + +void VariableResolver::visit(InterfaceBlock &iface) +{ + /* Block names can be reused in different interfaces. Prefix the name with + the first character of the interface to avoid conflicts. */ + stage->interface_blocks.insert(make_pair(iface.interface+iface.block_name, &iface)); + if(!iface.instance_name.empty()) + stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface)); + + TraversingVisitor::visit(iface); +} + + +ExpressionResolver::ExpressionResolver(): + stage(0), + r_any_resolved(false) +{ } + +bool ExpressionResolver::apply(Stage &s) +{ + stage = &s; + r_any_resolved = false; + s.content.visit(*this); + return r_any_resolved; +} + +bool ExpressionResolver::is_scalar(BasicTypeDeclaration &type) +{ + return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT); +} + +bool ExpressionResolver::is_vector_or_matrix(BasicTypeDeclaration &type) +{ + return (type.kind==BasicTypeDeclaration::VECTOR || type.kind==BasicTypeDeclaration::MATRIX); +} + +BasicTypeDeclaration *ExpressionResolver::get_element_type(BasicTypeDeclaration &type) +{ + if(is_vector_or_matrix(type) || type.kind==BasicTypeDeclaration::ARRAY) + { + BasicTypeDeclaration *basic_base = dynamic_cast(type.base_type); + return (basic_base ? get_element_type(*basic_base) : 0); + } + else + return &type; +} + +bool ExpressionResolver::can_convert(BasicTypeDeclaration &from, BasicTypeDeclaration &to) +{ + if(from.kind==BasicTypeDeclaration::INT && to.kind==BasicTypeDeclaration::FLOAT) + return from.size<=to.size; + else if(from.kind!=to.kind) + return false; + else if((from.kind==BasicTypeDeclaration::VECTOR || from.kind==BasicTypeDeclaration::MATRIX) && from.size==to.size) + { + BasicTypeDeclaration *from_base = dynamic_cast(from.base_type); + BasicTypeDeclaration *to_base = dynamic_cast(to.base_type); + return (from_base && to_base && can_convert(*from_base, *to_base)); + } + else + return false; +} + +ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTypeDeclaration &left, BasicTypeDeclaration &right) +{ + if(&left==&right) + return SAME_TYPE; + else if(can_convert(left, right)) + return LEFT_CONVERTIBLE; + else if(can_convert(right, left)) + return RIGHT_CONVERTIBLE; + else + return NOT_COMPATIBLE; +} + +BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size) +{ + for(vector::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i) + if((*i)->kind==kind && (*i)->size==size) + return *i; + return 0; +} + +BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration &elem_type, BasicTypeDeclaration::Kind kind, unsigned size) +{ + for(vector::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i) + if(get_element_type(**i)==&elem_type && (*i)->kind==kind && (*i)->size==size) + return *i; + return 0; +} + +void ExpressionResolver::convert_to(RefPtr &expr, BasicTypeDeclaration &type) +{ + RefPtr call = new FunctionCall; + call->name = type.name; + call->constructor = true; + call->arguments.push_back_nocopy(expr); + call->type = &type; + expr = call; +} + +bool ExpressionResolver::convert_to_element(RefPtr &expr, BasicTypeDeclaration &elem_type) +{ + if(BasicTypeDeclaration *expr_basic = dynamic_cast(expr->type)) + { + BasicTypeDeclaration *to_type = &elem_type; + if(is_vector_or_matrix(*expr_basic)) + to_type = find_type(elem_type, expr_basic->kind, expr_basic->size); + if(to_type) + { + convert_to(expr, *to_type); + return true; + } + } + + return false; +} + +bool ExpressionResolver::truncate_vector(RefPtr &expr, unsigned size) +{ + if(BasicTypeDeclaration *expr_basic = dynamic_cast(expr->type)) + if(BasicTypeDeclaration *expr_elem = get_element_type(*expr_basic)) + { + RefPtr swizzle = new Swizzle; + swizzle->left = expr; + swizzle->oper = &Operator::get_operator(".", Operator::POSTFIX); + swizzle->component_group = string("xyzw", size); + swizzle->count = size; + for(unsigned i=0; icomponents[i] = i; + if(size==1) + swizzle->type = expr_elem; + else + swizzle->type = find_type(*expr_elem, BasicTypeDeclaration::VECTOR, size); + expr = swizzle; + + return true; + } + + return false; +} + +void ExpressionResolver::resolve(Expression &expr, TypeDeclaration *type, bool lvalue) +{ + r_any_resolved |= (type!=expr.type || lvalue!=expr.lvalue); + expr.type = type; + expr.lvalue = lvalue; +} + +void ExpressionResolver::visit(Block &block) +{ + SetForScope set_block(current_block, &block); + for(NodeList::iterator i=block.body.begin(); i!=block.body.end(); ++i) + { + insert_point = i; + (*i)->visit(*this); + } +} + +void ExpressionResolver::visit(Literal &literal) +{ + if(literal.value.check_type()) + resolve(literal, find_type(BasicTypeDeclaration::BOOL, 1), false); + else if(literal.value.check_type()) + resolve(literal, find_type(BasicTypeDeclaration::INT, 32), false); + else if(literal.value.check_type()) + resolve(literal, find_type(BasicTypeDeclaration::FLOAT, 32), false); +} + +void ExpressionResolver::visit(VariableReference &var) +{ + if(var.declaration) + resolve(var, var.declaration->type_declaration, true); +} + +void ExpressionResolver::visit(InterfaceBlockReference &iface) +{ + if(iface.declaration) + resolve(iface, iface.declaration->type_declaration, true); +} + +void ExpressionResolver::visit(MemberAccess &memacc) +{ + TraversingVisitor::visit(memacc); + + if(memacc.declaration) + resolve(memacc, memacc.declaration->type_declaration, memacc.left->lvalue); +} + +void ExpressionResolver::visit(Swizzle &swizzle) +{ + TraversingVisitor::visit(swizzle); + + if(BasicTypeDeclaration *left_basic = dynamic_cast(swizzle.left->type)) + { + BasicTypeDeclaration *left_elem = get_element_type(*left_basic); + if(swizzle.count==1) + resolve(swizzle, left_elem, swizzle.left->lvalue); + else if(left_basic->kind==BasicTypeDeclaration::VECTOR && left_elem) + resolve(swizzle, find_type(*left_elem, left_basic->kind, swizzle.count), swizzle.left->lvalue); + } +} + +void ExpressionResolver::visit(UnaryExpression &unary) +{ + TraversingVisitor::visit(unary); + + BasicTypeDeclaration *basic = dynamic_cast(unary.expression->type); + if(!basic) + return; + + char oper = unary.oper->token[0]; + if(oper=='!') + { + if(basic->kind!=BasicTypeDeclaration::BOOL) + return; + } + else if(oper=='~') + { + if(basic->kind!=BasicTypeDeclaration::INT) + return; + } + else if(oper=='+' || oper=='-') + { + BasicTypeDeclaration *elem = get_element_type(*basic); + if(!elem || !is_scalar(*elem)) + return; + } + resolve(unary, basic, unary.expression->lvalue); +} + +void ExpressionResolver::visit(BinaryExpression &binary, bool assign) +{ + /* Binary operators are only defined for basic types (not for image or + structure types). */ + BasicTypeDeclaration *basic_left = dynamic_cast(binary.left->type); + BasicTypeDeclaration *basic_right = dynamic_cast(binary.right->type); + if(!basic_left || !basic_right) + return; + + char oper = binary.oper->token[0]; + if(oper=='[') + { + /* Subscripting operates on vectors, matrices and arrays, and the right + operand must be an integer. */ + if((!is_vector_or_matrix(*basic_left) && basic_left->kind!=BasicTypeDeclaration::ARRAY) || basic_right->kind!=BasicTypeDeclaration::INT) + return; + + resolve(binary, basic_left->base_type, binary.left->lvalue); + return; + } + else if(basic_left->kind==BasicTypeDeclaration::ARRAY || basic_right->kind==BasicTypeDeclaration::ARRAY) + // No other binary operator can be used with arrays. + return; + + BasicTypeDeclaration *elem_left = get_element_type(*basic_left); + BasicTypeDeclaration *elem_right = get_element_type(*basic_right); + if(!elem_left || !elem_right) + return; + + Compatibility compat = get_compatibility(*basic_left, *basic_right); + Compatibility elem_compat = get_compatibility(*elem_left, *elem_right); + if(elem_compat==NOT_COMPATIBLE) + return; + if(assign && (compat==LEFT_CONVERTIBLE || elem_compat==LEFT_CONVERTIBLE)) + return; + + TypeDeclaration *type = 0; + char oper2 = binary.oper->token[1]; + if((oper=='<' && oper2!='<') || (oper=='>' && oper2!='>')) + { + /* Relational operators compare two scalar integer or floating-point + values. */ + if(!is_scalar(*elem_left) || !is_scalar(*elem_right) || compat==NOT_COMPATIBLE) + return; + + type = find_type(BasicTypeDeclaration::BOOL, 1); + } + else if((oper=='=' || oper=='!') && oper2=='=') + { + // Equality comparison can be done on any compatible types. + if(compat==NOT_COMPATIBLE) + return; + + type = find_type(BasicTypeDeclaration::BOOL, 1); + } + else if(oper2=='&' || oper2=='|' || oper2=='^') + { + // Logical operators can only be applied to booleans. + if(basic_left->kind!=BasicTypeDeclaration::BOOL || basic_right->kind!=BasicTypeDeclaration::BOOL) + return; + + type = basic_left; + } + else if((oper=='&' || oper=='|' || oper=='^' || oper=='%') && !oper2) + { + // Bitwise operators and modulo can only be applied to integers. + if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT) + return; + + type = (compat==LEFT_CONVERTIBLE ? basic_right : basic_left); + } + else if((oper=='<' || oper=='>') && oper2==oper) { - ptr->type = var.type; - if(var.init_expression) - ptr->init_expression = var.init_expression; - if(var.layout) + // Shifts apply to integer scalars and vectors, with some restrictions. + if(elem_left->kind!=BasicTypeDeclaration::INT || elem_right->kind!=BasicTypeDeclaration::INT) + return; + unsigned left_size = (basic_left->kind==BasicTypeDeclaration::INT ? 1 : basic_left->kind==BasicTypeDeclaration::VECTOR ? basic_left->size : 0); + unsigned right_size = (basic_right->kind==BasicTypeDeclaration::INT ? 1 : basic_right->kind==BasicTypeDeclaration::VECTOR ? basic_right->size : 0); + if(!left_size || (left_size==1 && right_size!=1) || (left_size>1 && right_size!=1 && right_size!=left_size)) + return; + + type = basic_left; + // Don't perform conversion even if the operands are of different sizes. + compat = SAME_TYPE; + } + else if(oper=='+' || oper=='-' || oper=='*' || oper=='/') + { + // Arithmetic operators require scalar elements. + if(!is_scalar(*elem_left) || !is_scalar(*elem_right)) + return; + + if(oper=='*' && is_vector_or_matrix(*basic_left) && is_vector_or_matrix(*basic_right) && + (basic_left->kind==BasicTypeDeclaration::MATRIX || basic_right->kind==BasicTypeDeclaration::MATRIX)) { - if(ptr->layout) - { - for(vector::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i) - { - bool found = false; - for(vector::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j) - if(j->name==i->name) - { - j->has_value = i->value; - j->value = i->value; - found = true; - } + /* Multiplication has special rules when at least one operand is a + matrix and the other is a vector or a matrix. */ + unsigned left_columns = basic_left->size&0xFFFF; + unsigned right_rows = basic_right->size; + if(basic_right->kind==BasicTypeDeclaration::MATRIX) + right_rows >>= 16; + if(left_columns!=right_rows) + return; - if(!found) - ptr->layout->qualifiers.push_back(*i); - } - } + BasicTypeDeclaration *elem_result = (elem_compat==LEFT_CONVERTIBLE ? elem_right : elem_left); + + if(basic_left->kind==BasicTypeDeclaration::VECTOR) + type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_right->size&0xFFFF); + else if(basic_right->kind==BasicTypeDeclaration::VECTOR) + type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_left->size>>16); else - ptr->layout = var.layout; + type = find_type(*elem_result, BasicTypeDeclaration::MATRIX, (basic_left->size&0xFFFF0000)|(basic_right->size&0xFFFF)); } - nodes_to_remove.insert(&var); + else if(compat==NOT_COMPATIBLE) + { + // Arithmetic between scalars and matrices or vectors is supported. + if(is_scalar(*basic_left) && is_vector_or_matrix(*basic_right)) + type = (elem_compat==RIGHT_CONVERTIBLE ? find_type(*elem_left, basic_right->kind, basic_right->size) : basic_right); + else if(is_vector_or_matrix(*basic_left) && is_scalar(*basic_right)) + type = (elem_compat==LEFT_CONVERTIBLE ? find_type(*elem_right, basic_left->kind, basic_left->size) : basic_left); + else + return; + } + else if(compat==LEFT_CONVERTIBLE) + type = basic_right; + else + type = basic_left; } else - ptr = &var; -} + return; + if(assign && type!=basic_left) + return; -void BlockResolver::visit(Block &block) -{ - block.parent = current_block; - TraversingVisitor::visit(block); + bool converted = true; + if(compat==LEFT_CONVERTIBLE) + convert_to(binary.left, *basic_right); + else if(compat==RIGHT_CONVERTIBLE) + convert_to(binary.right, *basic_left); + else if(elem_compat==LEFT_CONVERTIBLE) + converted = convert_to_element(binary.left, *elem_right); + else if(elem_compat==RIGHT_CONVERTIBLE) + converted = convert_to_element(binary.right, *elem_left); + + if(!converted) + type = 0; + + resolve(binary, type, assign); } -void BlockResolver::visit(InterfaceBlock &iface) +void ExpressionResolver::visit(BinaryExpression &binary) { - iface.members.anonymous = true; - TraversingVisitor::visit(iface); + TraversingVisitor::visit(binary); + visit(binary, false); } +void ExpressionResolver::visit(Assignment &assign) +{ + TraversingVisitor::visit(assign); -VariableResolver::VariableResolver(): - record_target(false), - assignment_target(0), - self_referencing(false) -{ } + if(assign.oper->token[0]!='=') + return visit(assign, true); + else if(assign.left->type!=assign.right->type) + { + BasicTypeDeclaration *basic_left = dynamic_cast(assign.left->type); + BasicTypeDeclaration *basic_right = dynamic_cast(assign.right->type); + if(!basic_left || !basic_right) + return; + + Compatibility compat = get_compatibility(*basic_left, *basic_right); + if(compat==RIGHT_CONVERTIBLE) + convert_to(assign.right, *basic_left); + else if(compat!=SAME_TYPE) + return; + } -void VariableResolver::apply(Stage &stage) -{ - Stage *builtin_stage = get_builtins(stage.type); - builtins = (builtin_stage ? &builtin_stage->content : 0); - stage.content.visit(*this); + resolve(assign, assign.left->type, true); } -Block *VariableResolver::next_block(Block &block) +void ExpressionResolver::visit(TernaryExpression &ternary) { - return block.parent ? block.parent : &block!=builtins ? builtins : 0; -} + TraversingVisitor::visit(ternary); -void VariableResolver::visit(Block &block) -{ - if(current_block!=&block) - block.variables.clear(); + BasicTypeDeclaration *basic_cond = dynamic_cast(ternary.condition->type); + if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL) + return; + + TypeDeclaration *type = 0; + if(ternary.true_expr->type==ternary.false_expr->type) + type = ternary.true_expr->type; + else + { + BasicTypeDeclaration *basic_true = dynamic_cast(ternary.true_expr->type); + BasicTypeDeclaration *basic_false = dynamic_cast(ternary.false_expr->type); + if(!basic_true || !basic_false) + return; + + Compatibility compat = get_compatibility(*basic_true, *basic_false); + if(compat==NOT_COMPATIBLE) + return; + + type = (compat==LEFT_CONVERTIBLE ? basic_true : basic_false); + + if(compat==LEFT_CONVERTIBLE) + convert_to(ternary.true_expr, *basic_false); + else if(compat==RIGHT_CONVERTIBLE) + convert_to(ternary.false_expr, *basic_true); + } - TraversingVisitor::visit(block); + resolve(ternary, type, false); } -void VariableResolver::visit(VariableReference &var) +void ExpressionResolver::visit_constructor(FunctionCall &call) { - var.declaration = 0; - type = 0; - for(Block *block=current_block; block; block=next_block(*block)) + if(call.arguments.empty()) + return; + + map::const_iterator i = stage->types.find(call.name); + if(i==stage->types.end()) + return; + else if(BasicTypeDeclaration *basic = dynamic_cast(i->second)) { - map::iterator j = block->variables.find(var.name); - if(j!=block->variables.end()) + BasicTypeDeclaration *elem = get_element_type(*basic); + if(!elem) + return; + + vector args; + args.reserve(call.arguments.size()); + unsigned arg_component_total = 0; + bool has_matrices = false; + for(NodeArray::const_iterator j=call.arguments.begin(); j!=call.arguments.end(); ++j) { - var.declaration = j->second; - type = j->second->type_declaration; - break; + ArgumentInfo info; + if(!(info.type=dynamic_cast((*j)->type))) + return; + if(is_scalar(*info.type) || info.type->kind==BasicTypeDeclaration::BOOL) + info.component_count = 1; + else if(info.type->kind==BasicTypeDeclaration::VECTOR) + info.component_count = info.type->size; + else if(info.type->kind==BasicTypeDeclaration::MATRIX) + { + info.component_count = (info.type->size>>16)*(info.type->size&0xFFFF); + has_matrices = true; + } + else + return; + arg_component_total += info.component_count; + args.push_back(info); } - } - if(record_target) - { - if(assignment_target) + bool convert_args = false; + if((is_scalar(*basic) || basic->kind==BasicTypeDeclaration::BOOL) && call.arguments.size()==1 && !has_matrices) { - record_target = false; - assignment_target = 0; + if(arg_component_total>1) + truncate_vector(call.arguments.front(), 1); + + /* Single-element type constructors never need to convert their + arguments because the constructor *is* the conversion. */ } - else - assignment_target = var.declaration; - } - else if(var.declaration && var.declaration==assignment_target) - self_referencing = true; -} + else if(basic->kind==BasicTypeDeclaration::VECTOR && !has_matrices) + { + /* Vector constructors need either a single scalar argument or + enough components to fill out the vector. */ + if(arg_component_total!=1 && arg_component_totalsize) + return; -void VariableResolver::visit(MemberAccess &memacc) -{ - type = 0; - TraversingVisitor::visit(memacc); - memacc.declaration = 0; - if(type) - { - map::iterator i = type->members.variables.find(memacc.member); - if(i!=type->members.variables.end()) + /* A vector of same size can be converted directly. For other + combinations the individual arguments need to be converted. */ + if(call.arguments.size()==1) + { + if(arg_component_total==1) + convert_args = true; + else if(arg_component_total>basic->size) + truncate_vector(call.arguments.front(), basic->size); + } + else if(arg_component_total==basic->size) + convert_args = true; + else + return; + } + else if(basic->kind==BasicTypeDeclaration::MATRIX) { - memacc.declaration = i->second; - type = i->second->type_declaration; + unsigned column_count = basic->size&0xFFFF; + unsigned row_count = basic->size>>16; + if(call.arguments.size()==1) + { + /* A matrix can be constructed from a single element or another + matrix of sufficient size. */ + if(arg_component_total==1) + convert_args = true; + else if(args.front().type->kind==BasicTypeDeclaration::MATRIX) + { + unsigned arg_columns = args.front().type->size&0xFFFF; + unsigned arg_rows = args.front().type->size>>16; + if(arg_columns temporary = new VariableDeclaration; + temporary->type = args.front().type->name; + temporary->name = get_unused_variable_name(*current_block, "_temp"); + temporary->init_expression = call.arguments.front(); + current_block->body.insert(insert_point, temporary); + + // Create expressions to build each column. + vector > columns; + columns.reserve(column_count); + for(unsigned j=0; j ref = new VariableReference; + ref->name = temporary->name; + + RefPtr index = new Literal; + index->token = lexical_cast(j); + index->value = static_cast(j); + + RefPtr subscript = new BinaryExpression; + subscript->left = ref; + subscript->oper = &Operator::get_operator("[", Operator::BINARY); + subscript->right = index; + subscript->type = args.front().type->base_type; + + columns.push_back(subscript); + if(arg_rows>row_count) + truncate_vector(columns.back(), row_count); + } + + call.arguments.resize(column_count); + copy(columns.begin(), columns.end(), call.arguments.begin()); + + /* Let VariableResolver process the new nodes and finish + resolving the constructor on the next pass. */ + r_any_resolved = true; + return; + } + else + return; + } + else if(arg_component_total==column_count*row_count && !has_matrices) + { + /* Construct a matrix from individual components in column-major + order. Arguments must align at column boundaries. */ + vector > columns; + columns.reserve(column_count); + + vector > column_args; + column_args.reserve(row_count); + unsigned column_component_count = 0; + + for(unsigned j=0; jkind==BasicTypeDeclaration::VECTOR && info.component_count==row_count) + // A vector filling the entire column can be used as is. + columns.push_back(call.arguments[j]); + else + { + column_args.push_back(call.arguments[j]); + column_component_count += info.component_count; + if(column_component_count==row_count) + { + /* The column has filled up. Create a vector constructor + for it.*/ + RefPtr column_call = new FunctionCall; + column_call->name = basic->base_type->name; + column_call->constructor = true; + column_call->arguments.resize(column_args.size()); + copy(column_args.begin(), column_args.end(), column_call->arguments.begin()); + column_call->type = basic->base_type; + visit_constructor(*column_call); + columns.push_back(column_call); + + column_args.clear(); + column_component_count = 0; + } + else if(column_component_count>row_count) + // Argument alignment mismatch. + return; + } + } + } + else + return; } else - type = 0; - } -} + return; -void VariableResolver::visit(BinaryExpression &binary) -{ - if(binary.oper=="[") - { + if(convert_args) { - SetForScope set(record_target, false); - binary.right->visit(*this); + // The argument list may have changed so can't rely on args. + for(NodeArray::iterator j=call.arguments.begin(); j!=call.arguments.end(); ++j) + if(BasicTypeDeclaration *basic_arg = dynamic_cast((*j)->type)) + { + BasicTypeDeclaration *elem_arg = get_element_type(*basic_arg); + if(elem_arg!=elem) + convert_to_element(*j, *elem); + } } - type = 0; - binary.left->visit(*this); } - else + else if(StructDeclaration *strct = dynamic_cast(i->second)) { - TraversingVisitor::visit(binary); - type = 0; + if(call.arguments.size()!=strct->members.body.size()) + return; + + unsigned k = 0; + for(NodeList::const_iterator j=strct->members.body.begin(); j!=strct->members.body.end(); ++j, ++k) + { + if(VariableDeclaration *var = dynamic_cast(j->get())) + { + if(!call.arguments[k]->type || call.arguments[k]->type!=var->type_declaration) + return; + } + else + return; + } } + + resolve(call, i->second, false); } -void VariableResolver::visit(Assignment &assign) +void ExpressionResolver::visit(FunctionCall &call) { - { - SetFlag set(record_target); - assignment_target = 0; - assign.left->visit(*this); - } - - self_referencing = false; - assign.right->visit(*this); + TraversingVisitor::visit(call); - assign.self_referencing = (self_referencing || assign.oper!="="); - assign.target_declaration = assignment_target; + if(call.declaration) + resolve(call, call.declaration->return_type_declaration, false); + else if(call.constructor) + visit_constructor(call); } -void VariableResolver::visit(StructDeclaration &strct) +void ExpressionResolver::visit(BasicTypeDeclaration &type) { - TraversingVisitor::visit(strct); - current_block->types[strct.name] = &strct; + basic_types.push_back(&type); } -void VariableResolver::visit(VariableDeclaration &var) +void ExpressionResolver::visit(VariableDeclaration &var) { - for(Block *block=current_block; block; block=next_block(*block)) - { - map::iterator j = block->types.find(var.type); - if(j!=block->types.end()) - var.type_declaration = j->second; - } + TraversingVisitor::visit(var); + if(!var.init_expression) + return; - if(!block_interface.empty() && var.interface.empty()) - var.interface = block_interface; + BasicTypeDeclaration *var_basic = dynamic_cast(var.type_declaration); + BasicTypeDeclaration *init_basic = dynamic_cast(var.init_expression->type); + if(!var_basic || !init_basic) + return; - TraversingVisitor::visit(var); - current_block->variables[var.name] = &var; - if(current_block->anonymous && current_block->parent) - current_block->parent->variables[var.name] = &var; + Compatibility compat = get_compatibility(*var_basic, *init_basic); + if(compat==RIGHT_CONVERTIBLE) + convert_to(var.init_expression, *var_basic); } -void VariableResolver::visit(InterfaceBlock &iface) -{ - SetForScope set_iface(block_interface, iface.interface); - TraversingVisitor::visit(iface); -} -void VariableResolver::visit(FunctionDeclaration &func) +bool FunctionResolver::apply(Stage &s) { - SetForScope set_block(current_block, &func.body); - func.body.variables.clear(); - TraversingVisitor::visit(func); + stage = &s; + s.functions.clear(); + r_any_resolved = false; + s.content.visit(*this); + return r_any_resolved; } -void VariableResolver::visit(Iteration &iter) +void FunctionResolver::visit(FunctionCall &call) { - SetForScope set_block(current_block, &iter.body); - iter.body.variables.clear(); - TraversingVisitor::visit(iter); -} + FunctionDeclaration *declaration = 0; + if(stage->types.count(call.name)) + call.constructor = true; + else + { + string arg_types; + bool has_signature = true; + for(NodeArray::const_iterator i=call.arguments.begin(); (has_signature && i!=call.arguments.end()); ++i) + { + if((*i)->type) + append(arg_types, ",", (*i)->type->name); + else + has_signature = false; + } + if(has_signature) + { + map::iterator i = stage->functions.find(format("%s(%s)", call.name, arg_types)); + declaration = (i!=stage->functions.end() ? i->second : 0); + } + } -void FunctionResolver::visit(FunctionCall &call) -{ - map >::iterator i = functions.find(call.name); - if(i!=functions.end()) - call.declaration = i->second.back(); + r_any_resolved |= (declaration!=call.declaration); + call.declaration = declaration; TraversingVisitor::visit(call); } void FunctionResolver::visit(FunctionDeclaration &func) { - vector &decls = functions[func.name]; - if(func.definition) + if(func.signature.empty()) { + string param_types; + for(NodeArray::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i) + { + if((*i)->type_declaration) + append(param_types, ",", (*i)->type_declaration->name); + else + return; + } + func.signature = format("(%s)", param_types); + r_any_resolved = true; + } + + string key = func.name+func.signature; + FunctionDeclaration *&stage_decl = stage->functions[key]; + vector &decls = declarations[key]; + if(func.definition==&func) + { + if(stage_decl && stage_decl->definition) + { + if(!func.overrd) + stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, func.source, func.line, + format("Overriding function '%s' without the override keyword is deprecated", key))); + if(!stage_decl->definition->virtua) + stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, func.source, func.line, + format("Overriding function '%s' not declared as virtual is deprecated", key))); + } + stage_decl = &func; + + // Set all previous declarations to use this definition. for(vector::iterator i=decls.begin(); i!=decls.end(); ++i) + { + r_any_resolved |= (func.definition!=(*i)->definition); (*i)->definition = func.definition; - decls.clear(); - decls.push_back(&func); + (*i)->body.body.clear(); + } } - else if(!decls.empty() && decls.back()->definition) - func.definition = decls.back()->definition; else - decls.push_back(&func); + { + FunctionDeclaration *definition = (stage_decl ? stage_decl->definition : 0); + r_any_resolved |= (definition!=func.definition); + func.definition = definition; + + if(!stage_decl) + stage_decl = &func; + } + decls.push_back(&func); TraversingVisitor::visit(func); } InterfaceGenerator::InterfaceGenerator(): - stage(0) + stage(0), + function_scope(false), + copy_block(false), + iface_target_block(0) { } string InterfaceGenerator::get_out_prefix(Stage::Type type) @@ -282,6 +1280,7 @@ string InterfaceGenerator::get_out_prefix(Stage::Type type) void InterfaceGenerator::apply(Stage &s) { stage = &s; + iface_target_block = &stage->content; if(stage->previous) in_prefix = get_out_prefix(stage->previous->type); out_prefix = get_out_prefix(stage->type); @@ -308,33 +1307,79 @@ string InterfaceGenerator::change_prefix(const string &name, const string &prefi return prefix+name.substr(offset); } -bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name) +VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name) { - const map &stage_vars = (iface=="in" ? stage->in_variables : stage->out_variables); - if(stage_vars.count(name)) - return false; + if(stage->content.variables.count(name)) + return 0; + + if(stage->type==Stage::GEOMETRY && !copy_block && var.interface=="out" && var.array) + return 0; VariableDeclaration* iface_var = new VariableDeclaration; iface_var->sampling = var.sampling; iface_var->interface = iface; iface_var->type = var.type; - iface_var->type_declaration = var.type_declaration; iface_var->name = name; - if(stage->type==Stage::GEOMETRY) + /* Geometry shader inputs are always arrays. But if we're bringing in an + entire block, the array is on the block and not individual variables. */ + if(stage->type==Stage::GEOMETRY && !copy_block) iface_var->array = ((var.array && var.interface!="in") || iface=="in"); else iface_var->array = var.array; if(iface_var->array) iface_var->array_size = var.array_size; if(iface=="in") + { + iface_var->layout = var.layout; iface_var->linked_declaration = &var; - stage->content.body.insert(iface_insert_point, iface_var); + var.linked_declaration = iface_var; + } + + iface_target_block->body.insert(iface_insert_point, iface_var); + iface_target_block->variables.insert(make_pair(name, iface_var)); + if(iface_target_block==&stage->content && iface=="in") + declared_inputs.push_back(iface_var); + + return iface_var; +} + +InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block) +{ + if(stage->interface_blocks.count("in"+out_block.block_name)) + return 0; + + InterfaceBlock *in_block = new InterfaceBlock; + in_block->interface = "in"; + in_block->block_name = out_block.block_name; + in_block->members = new Block; + in_block->instance_name = out_block.instance_name; + if(stage->type==Stage::GEOMETRY) + in_block->array = true; + else + in_block->array = out_block.array; + in_block->linked_block = &out_block; + out_block.linked_block = in_block; + { - SetForScope set_block(current_block, &stage->content); - iface_var->visit(*this); + SetFlag set_copy(copy_block, true); + SetForScope set_target(iface_target_block, in_block->members.get()); + SetForScope::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end()); + if(out_block.struct_declaration) + out_block.struct_declaration->members.visit(*this); + else if(out_block.members) + out_block.members->visit(*this); } - return true; + iface_target_block->body.insert(iface_insert_point, in_block); + stage->interface_blocks.insert(make_pair("in"+in_block->block_name, in_block)); + if(!in_block->instance_name.empty()) + stage->interface_blocks.insert(make_pair("_"+in_block->instance_name, in_block)); + + SetFlag set_scope(function_scope, false); + SetForScope set_block(current_block, &stage->content); + in_block->visit(*this); + + return in_block; } ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right) @@ -343,7 +1388,7 @@ ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, E VariableReference *ref = new VariableReference; ref->name = left; assign->left = ref; - assign->oper = "="; + assign->oper = &Operator::get_operator("=", Operator::BINARY); assign->right = right; ExpressionStatement *stmt = new ExpressionStatement; @@ -358,26 +1403,64 @@ void InterfaceGenerator::visit(VariableReference &var) { if(var.declaration || !stage->previous) return; + /* Don't pull a variable from previous stage if we just generated an output + interface in this stage */ + if(stage->content.variables.count(var.name)) + return; + + const map &prev_vars = stage->previous->content.variables; + map::const_iterator i = prev_vars.find(var.name); + if(i==prev_vars.end() || i->second->interface!="out") + i = prev_vars.find(in_prefix+var.name); + if(i!=prev_vars.end() && i->second->interface=="out") + { + if(stage->type==Stage::GEOMETRY && i->second->array) + stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line, + format("Can't access '%s' through automatic interface because it's an array", var.name))); + else + { + generate_interface(*i->second, "in", i->second->name); + var.name = i->second->name; + } + return; + } - const map &prev_out = stage->previous->out_variables; - map::const_iterator i = prev_out.find(var.name); - if(i==prev_out.end()) - i = prev_out.find(in_prefix+var.name); - if(i!=prev_out.end()) + const map &prev_blocks = stage->previous->interface_blocks; + map::const_iterator j = prev_blocks.find("_"+var.name); + if(j!=prev_blocks.end() && j->second->interface=="out") { - generate_interface(*i->second, "in", i->second->name); - var.name = i->second->name; + generate_interface(*j->second); + /* Let VariableResolver convert the variable reference into an interface + block reference. */ + return; } + + for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j) + if(j->second->instance_name.empty() && j->second->struct_declaration) + { + const map &iface_vars = j->second->struct_declaration->members.variables; + i = iface_vars.find(var.name); + if(i!=iface_vars.end()) + { + generate_interface(*j->second); + return; + } + } } void InterfaceGenerator::visit(VariableDeclaration &var) { - if(var.interface=="out") + if(copy_block) + generate_interface(var, "in", var.name); + else if(var.interface=="out") { - if(current_block==&stage->content) - stage->out_variables[var.name] = &var; - else if(generate_interface(var, "out", change_prefix(var.name, string()))) + /* For output variables in function scope, generate a global interface + and replace the local declaration with an assignment. */ + VariableDeclaration *out_var = 0; + if(function_scope && (out_var=generate_interface(var, "out", var.name))) { + out_var->source = var.source; + out_var->line = var.line; nodes_to_remove.insert(&var); if(var.init_expression) { @@ -388,16 +1471,18 @@ void InterfaceGenerator::visit(VariableDeclaration &var) } } } - else if(var.interface=="in") + else if(var.interface=="in" && current_block==&stage->content) { - stage->in_variables[var.name] = &var; - if(var.linked_declaration) - var.linked_declaration->linked_declaration = &var; - else if(stage->previous) + if(var.name.compare(0, 3, "gl_")) + declared_inputs.push_back(&var); + + /* Try to link input variables in global scope with output variables from + previous stage. */ + if(!var.linked_declaration && stage->previous) { - const map &prev_out = stage->previous->out_variables; - map::const_iterator i = prev_out.find(var.name); - if(i!=prev_out.end()) + const map &prev_vars = stage->previous->content.variables; + map::const_iterator i = prev_vars.find(var.name); + if(i!=prev_vars.end() && i->second->interface=="out") { var.linked_declaration = i->second; i->second->linked_declaration = &var; @@ -408,37 +1493,65 @@ void InterfaceGenerator::visit(VariableDeclaration &var) TraversingVisitor::visit(var); } -void InterfaceGenerator::visit(Passthrough &pass) +void InterfaceGenerator::visit(InterfaceBlock &iface) +{ + if(iface.interface=="in") + { + /* Try to link input blocks with output blocks sharing the same block + name from previous stage. */ + if(!iface.linked_block && stage->previous) + { + const map &prev_blocks = stage->previous->interface_blocks; + map::const_iterator i = prev_blocks.find("out"+iface.block_name); + if(i!=prev_blocks.end()) + { + iface.linked_block = i->second; + i->second->linked_block = &iface; + } + } + } + + TraversingVisitor::visit(iface); +} + +void InterfaceGenerator::visit(FunctionDeclaration &func) { - vector pass_vars; + SetFlag set_scope(function_scope, true); + // Skip parameters because they're not useful here + func.body.visit(*this); +} - for(map::const_iterator i=stage->in_variables.begin(); i!=stage->in_variables.end(); ++i) - pass_vars.push_back(i->second); +void InterfaceGenerator::visit(Passthrough &pass) +{ + // Pass through all input variables declared so far. + vector pass_vars = declared_inputs; if(stage->previous) { - const map &prev_out = stage->previous->out_variables; - for(map::const_iterator i=prev_out.begin(); i!=prev_out.end(); ++i) + const map &prev_vars = stage->previous->content.variables; + for(map::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i) { - bool linked = false; - for(vector::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j) - linked = ((*j)->linked_declaration==i->second); + if(i->second->interface!="out") + continue; - if(!linked && generate_interface(*i->second, "in", i->second->name)) + /* Pass through output variables from the previous stage, but only + those which are not already linked to an input here. */ + if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name)) pass_vars.push_back(i->second); } } if(stage->type==Stage::GEOMETRY) { - VariableReference *ref = new VariableReference; + /* Special case for geometry shader: copy gl_Position from input to + output. */ + InterfaceBlockReference *ref = new InterfaceBlockReference; ref->name = "gl_in"; BinaryExpression *subscript = new BinaryExpression; subscript->left = ref; - subscript->oper = "["; + subscript->oper = &Operator::get_operator("[", Operator::BINARY); subscript->right = pass.subscript; - subscript->after = "]"; MemberAccess *memacc = new MemberAccess; memacc->left = subscript; @@ -458,9 +1571,8 @@ void InterfaceGenerator::visit(Passthrough &pass) { BinaryExpression *subscript = new BinaryExpression; subscript->left = ref; - subscript->oper = "["; + subscript->oper = &Operator::get_operator("[", Operator::BINARY); subscript->right = pass.subscript; - subscript->after = "]"; insert_assignment(out_name, subscript); } else @@ -470,111 +1582,6 @@ void InterfaceGenerator::visit(Passthrough &pass) nodes_to_remove.insert(&pass); } - -DeclarationReorderer::DeclarationReorderer(): - kind(NO_DECLARATION) -{ } - -void DeclarationReorderer::visit(FunctionCall &call) -{ - FunctionDeclaration *def = call.declaration; - if(def) - def = def->definition; - if(def && !ordered_funcs.count(def)) - needed_funcs.insert(def); -} - -void DeclarationReorderer::visit(Block &block) -{ - if(block.parent) - return TraversingVisitor::visit(block); - - NodeList::iterator struct_insert_point = block.body.end(); - NodeList::iterator variable_insert_point = block.body.end(); - NodeList::iterator function_insert_point = block.body.end(); - unsigned unordered_func_count = 0; - bool ordered_any_funcs = false; - - for(NodeList::iterator i=block.body.begin(); i!=block.body.end(); ) - { - kind = NO_DECLARATION; - (*i)->visit(*this); - - bool moved = false; - if(kind==STRUCT && struct_insert_point!=block.body.end()) - { - block.body.insert(struct_insert_point, *i); - moved = true; - } - else if(kind>STRUCT && struct_insert_point==block.body.end()) - struct_insert_point = i; - - if(kind==VARIABLE && variable_insert_point!=block.body.end()) - { - block.body.insert(variable_insert_point, *i); - moved = true; - } - else if(kind>VARIABLE && variable_insert_point==block.body.end()) - variable_insert_point = i; - - if(kind==FUNCTION) - { - if(function_insert_point==block.body.end()) - function_insert_point = i; - - if(needed_funcs.empty()) - { - ordered_funcs.insert(i->get()); - if(i!=function_insert_point) - { - block.body.insert(function_insert_point, *i); - moved = true; - } - else - ++function_insert_point; - ordered_any_funcs = true; - } - else - ++unordered_func_count; - } - - if(moved) - { - if(function_insert_point==i) - ++function_insert_point; - block.body.erase(i++); - } - else - ++i; - - if(i==block.body.end() && unordered_func_count) - { - if(!ordered_any_funcs) - // A subset of the remaining functions forms a recursive loop - /* TODO pick a function and move it up, adding any necessary - declarations */ - break; - - i = function_insert_point; - unordered_func_count = 0; - } - } -} - -void DeclarationReorderer::visit(VariableDeclaration &var) -{ - TraversingVisitor::visit(var); - kind = VARIABLE; -} - -void DeclarationReorderer::visit(FunctionDeclaration &func) -{ - needed_funcs.clear(); - func.body.visit(*this); - needed_funcs.erase(&func); - kind = FUNCTION; -} - } // namespace SL } // namespace GL } // namespace Msp