X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Fresolve.cpp;h=f2670b53e6805a78f7a2ce6627882299904ff254;hb=59347f76bc985e8c9c769d4a3eee672cba9c920b;hp=05aead3c5fa369f55529c02c05c34fe7a6997dba;hpb=bbe2fb7bc1384d7683f1795b5cfa9168df18c580;p=libs%2Fgl.git diff --git a/source/glsl/resolve.cpp b/source/glsl/resolve.cpp index 05aead3c..f2670b53 100644 --- a/source/glsl/resolve.cpp +++ b/source/glsl/resolve.cpp @@ -1,6 +1,7 @@ #include #include #include +#include "reflect.h" #include "resolve.h" using namespace std; @@ -225,29 +226,43 @@ void VariableResolver::visit(VariableReference &var) if(!declaration) { const map &blocks = stage->interface_blocks; - map::const_iterator i = blocks.find("_"+var.name); + map::const_iterator i = blocks.find(var.name); + if(i==blocks.end()) + { + // Look for the variable in anonymous interface blocks. + for(i=blocks.begin(); i!=blocks.end(); ++i) + if(i->second->instance_name.empty() && i->second->struct_declaration) + if(i->second->struct_declaration->members.variables.count(var.name)) + break; + } + 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. */ + /* The name refers to either an interface block with an instance name + or a variable declared inside an anonymous interface block. Prepare + new syntax tree nodes 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; - } + + if(i->second->instance_name.empty()) + { + iface_ref->name = format("%s %s", i->second->interface, i->second->block_name); + + MemberAccess *memacc = new MemberAccess; + memacc->source = var.source; + memacc->line = var.line; + memacc->left = iface_ref; + memacc->member = var.name; + + r_replacement_expr = memacc; + } + else + { + iface_ref->name = var.name; + r_replacement_expr = iface_ref; + } } } @@ -259,7 +274,7 @@ void VariableResolver::visit(VariableReference &var) void VariableResolver::visit(InterfaceBlockReference &iface) { - map::iterator i = stage->interface_blocks.find("_"+iface.name); + 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; @@ -267,33 +282,24 @@ void VariableResolver::visit(InterfaceBlockReference &iface) 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 VariableResolver::visit(MemberAccess &memacc) { TraversingVisitor::visit(memacc); VariableDeclaration *declaration = 0; + int index = -1; 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; + index = 0; + for(NodeList::const_iterator j=strct->members.body.begin(); (j!=strct->members.body.end() && j->get()!=i->second); ++j) + ++index; 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); - } + add_to_chain(r_assignment_target, Assignment::Target::MEMBER, index); } } else if(BasicTypeDeclaration *basic = dynamic_cast(memacc.left->type)) @@ -324,8 +330,9 @@ void VariableResolver::visit(MemberAccess &memacc) } } - r_any_resolved |= (declaration!=memacc.declaration); + r_any_resolved |= (declaration!=memacc.declaration || index!=memacc.index); memacc.declaration = declaration; + memacc.index = index; } void VariableResolver::visit(Swizzle &swizzle) @@ -337,7 +344,7 @@ void VariableResolver::visit(Swizzle &swizzle) unsigned mask = 0; for(unsigned i=0; i(binary.right.get())) if(literal_subscript->value.check_type()) index = literal_subscript->value.value(); - add_to_chain(Assignment::Target::ARRAY, index); + add_to_chain(r_assignment_target, Assignment::Target::ARRAY, index); } } else @@ -432,9 +439,9 @@ 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)); + stage->interface_blocks.insert(make_pair(format("%s %s", iface.interface, iface.block_name), &iface)); if(!iface.instance_name.empty()) - stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface)); + stage->interface_blocks.insert(make_pair(iface.instance_name, &iface)); TraversingVisitor::visit(iface); } @@ -453,43 +460,6 @@ bool ExpressionResolver::apply(Stage &s) 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) @@ -502,10 +472,10 @@ ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTyp return NOT_COMPATIBLE; } -BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size) +BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size, bool sign) { for(vector::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i) - if((*i)->kind==kind && (*i)->size==size) + if((*i)->kind==kind && (*i)->size==size && (*i)->sign==sign) return *i; return 0; } @@ -591,7 +561,9 @@ 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); + resolve(literal, find_type(BasicTypeDeclaration::INT, 32, true), false); + else if(literal.value.check_type()) + resolve(literal, find_type(BasicTypeDeclaration::INT, 32, false), false); else if(literal.value.check_type()) resolve(literal, find_type(BasicTypeDeclaration::FLOAT, 32), false); } @@ -739,6 +711,17 @@ void ExpressionResolver::visit(BinaryExpression &binary, bool assign) if(!left_size || (left_size==1 && right_size!=1) || (left_size>1 && right_size!=1 && right_size!=left_size)) return; + /* If the left operand is a vector and right is scalar, convert the right + operand to a vector too. */ + if(left_size>1 && right_size==1) + { + BasicTypeDeclaration *vec_right = find_type(*elem_right, basic_left->kind, basic_left->size); + if(!vec_right) + return; + + convert_to(binary.right, *vec_right); + } + type = basic_left; // Don't perform conversion even if the operands are of different sizes. compat = SAME_TYPE; @@ -877,6 +860,8 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) map::const_iterator i = stage->types.find(call.name); if(i==stage->types.end()) return; + else if(call.arguments.size()==1 && i->second==call.arguments[0]->type) + ; else if(BasicTypeDeclaration *basic = dynamic_cast(i->second)) { BasicTypeDeclaration *elem = get_element_type(*basic); @@ -1119,6 +1104,29 @@ bool FunctionResolver::apply(Stage &s) return r_any_resolved; } +bool FunctionResolver::can_convert_arguments(const FunctionCall &call, const FunctionDeclaration &decl) +{ + if(decl.parameters.size()!=call.arguments.size()) + return false; + + for(unsigned j=0; jtype; + const TypeDeclaration *param_type = decl.parameters[j]->type_declaration; + if(arg_type==param_type) + continue; + + const BasicTypeDeclaration *arg_basic = dynamic_cast(arg_type); + const BasicTypeDeclaration *param_basic = dynamic_cast(param_type); + if(arg_basic && param_basic && can_convert(*arg_basic, *param_basic)) + continue; + + return false; + } + + return true; +} + void FunctionResolver::visit(FunctionCall &call) { FunctionDeclaration *declaration = 0; @@ -1140,6 +1148,21 @@ void FunctionResolver::visit(FunctionCall &call) { map::iterator i = stage->functions.find(format("%s(%s)", call.name, arg_types)); declaration = (i!=stage->functions.end() ? i->second : 0); + + if(!declaration) + { + for(i=stage->functions.lower_bound(call.name+"("); (i!=stage->functions.end() && i->second->name==call.name); ++i) + if(can_convert_arguments(call, *i->second)) + { + if(declaration) + { + declaration = 0; + break; + } + else + declaration = i->second; + } + } } }