X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fglsl%2Fgenerate.cpp;h=158bb4cc91b67b39239358c70bf0237ef5821ed0;hp=8547152e45df9edb714f06ae8c2c83cccae40350;hb=7335009e18ecbf53ad9f59d64eed2ed5abbe7b8b;hpb=cc3f4aaaf8d2b34347f69b026c10f82797059aa4 diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index 8547152e..158bb4cc 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "builtin.h" #include "generate.h" @@ -190,6 +191,9 @@ void TypeResolver::visit(BasicTypeDeclaration &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; } @@ -274,7 +278,7 @@ void VariableResolver::enter(Block &block) block.variables.clear(); } -void VariableResolver::visit_and_replace(RefPtr &expr) +void VariableResolver::visit(RefPtr &expr) { r_replacement_expr = 0; expr->visit(*this); @@ -376,7 +380,7 @@ void VariableResolver::add_to_chain(Assignment::Target::ChainType type, unsigned void VariableResolver::visit(MemberAccess &memacc) { - visit_and_replace(memacc.left); + TraversingVisitor::visit(memacc); VariableDeclaration *declaration = 0; if(StructDeclaration *strct = dynamic_cast(memacc.left->type)) @@ -430,7 +434,7 @@ void VariableResolver::visit(MemberAccess &memacc) void VariableResolver::visit(Swizzle &swizzle) { - visit_and_replace(swizzle.left); + TraversingVisitor::visit(swizzle); if(record_target) { @@ -441,11 +445,6 @@ void VariableResolver::visit(Swizzle &swizzle) } } -void VariableResolver::visit(UnaryExpression &unary) -{ - visit_and_replace(unary.expression); -} - void VariableResolver::visit(BinaryExpression &binary) { if(binary.oper->token[0]=='[') @@ -454,9 +453,9 @@ void VariableResolver::visit(BinaryExpression &binary) /* The subscript expression is not a part of the primary assignment target. */ SetFlag set(record_target, false); - visit_and_replace(binary.right); + visit(binary.right); } - visit_and_replace(binary.left); + visit(binary.left); if(record_target) { @@ -468,10 +467,7 @@ void VariableResolver::visit(BinaryExpression &binary) } } else - { - visit_and_replace(binary.left); - visit_and_replace(binary.right); - } + TraversingVisitor::visit(binary); } void VariableResolver::visit(Assignment &assign) @@ -479,30 +475,19 @@ void VariableResolver::visit(Assignment &assign) { SetFlag set(record_target); r_assignment_target = Assignment::Target(); - visit_and_replace(assign.left); + visit(assign.left); r_any_resolved |= (r_assignment_targettoken[0]!='='); } -void VariableResolver::visit(FunctionCall &call) -{ - for(NodeArray::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i) - visit_and_replace(*i); -} - void VariableResolver::visit(VariableDeclaration &var) { - if(var.layout) - var.layout->visit(*this); - if(var.array_size) - visit_and_replace(var.array_size); - if(var.init_expression) - visit_and_replace(var.init_expression); + TraversingVisitor::visit(var); current_block->variables.insert(make_pair(var.name, &var)); } @@ -609,11 +594,11 @@ void ExpressionResolver::convert_to(RefPtr &expr, BasicTypeDeclarati bool ExpressionResolver::convert_to_element(RefPtr &expr, BasicTypeDeclaration &elem_type) { - if(BasicTypeDeclaration *expr_type = dynamic_cast(expr->type)) + if(BasicTypeDeclaration *expr_basic = dynamic_cast(expr->type)) { BasicTypeDeclaration *to_type = &elem_type; - if(is_vector_or_matrix(*expr_type)) - to_type = find_type(elem_type, expr_type->kind, expr_type->size); + 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); @@ -935,17 +920,47 @@ bool FunctionResolver::apply(Stage &s) void FunctionResolver::visit(FunctionCall &call) { - map::iterator i = stage->functions.find(call.name); - if(i!=stage->functions.end()) - call.declaration = i->second; + 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; + } + + FunctionDeclaration *declaration = 0; + if(has_signature) + { + map::iterator i = stage->functions.find(format("%s(%s)", call.name, arg_types)); + declaration = (i!=stage->functions.end() ? i->second : 0); + } + r_any_resolved |= (declaration!=call.declaration); + call.declaration = declaration; TraversingVisitor::visit(call); } void FunctionResolver::visit(FunctionDeclaration &func) { - FunctionDeclaration *&stage_decl = stage->functions[func.name]; - vector &decls = declarations[func.name]; + 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) { stage_decl = &func; @@ -953,17 +968,19 @@ void FunctionResolver::visit(FunctionDeclaration &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; (*i)->body.body.clear(); } } else { - func.definition = 0; + FunctionDeclaration *definition = (stage_decl ? stage_decl->definition : 0); + r_any_resolved |= (definition!=func.definition); + func.definition = definition; + if(!stage_decl) stage_decl = &func; - else - func.definition = stage_decl->definition; } decls.push_back(&func);