X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Foptimize.cpp;h=c4118dee205d09f2f84c1e468001b6c6706de1c9;hb=0388083;hp=0d18b76c311b98c3ab7fbae14eea36cb1f80ba42;hpb=bde40be11da83206f3d33a8225963d47a413662e;p=libs%2Fgl.git diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 0d18b76c..c4118dee 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -74,6 +74,10 @@ const string &InlineContentInjector::apply(Stage &stage, FunctionDeclaration &ta { target_block = &tgt_blk; source_func = &src; + remap_prefix = source_func->name; + + vector > inlined; + inlined.reserve(src.body.body.size()); for(NodeList::iterator i=src.body.body.begin(); i!=src.body.body.end(); ++i) { r_inlined_statement = 0; @@ -81,30 +85,23 @@ const string &InlineContentInjector::apply(Stage &stage, FunctionDeclaration &ta if(!r_inlined_statement) r_inlined_statement = (*i)->clone(); - SetFlag set_remap(remap_names); + SetForScope set_remap(remap_names, 2); r_inlined_statement->visit(*this); - tgt_blk.body.insert(ins_pt, r_inlined_statement); + inlined.push_back(r_inlined_statement); } + SetForScope set_remap(remap_names, 1); + SetForScope set_prefix(remap_prefix, target_func.name); + variable_map.clear(); + target_func.visit(*this); + + tgt_blk.body.insert(ins_pt, inlined.begin(), inlined.end()); + NodeReorderer().apply(stage, target_func, dependencies); return r_result_name; } -string InlineContentInjector::create_unused_name(const string &base, bool always_prefix) -{ - string result = base; - if(always_prefix || target_block->variables.count(result)) - result = format("_%s_%s", source_func->name, base); - unsigned initial_size = result.size(); - for(unsigned i=1; target_block->variables.count(result); ++i) - { - result.erase(initial_size); - result += format("_%d", i); - } - return result; -} - void InlineContentInjector::visit(VariableReference &var) { if(remap_names) @@ -116,7 +113,11 @@ void InlineContentInjector::visit(VariableReference &var) else if(var.declaration) { SetFlag set_deps(deps_only); - dependencies.insert(var.declaration); + if(!variable_map.count(var.name)) + { + dependencies.insert(var.declaration); + referenced_names.insert(var.name); + } var.declaration->visit(*this); } } @@ -127,6 +128,7 @@ void InlineContentInjector::visit(InterfaceBlockReference &iface) { SetFlag set_deps(deps_only); dependencies.insert(iface.declaration); + referenced_names.insert(iface.name); iface.declaration->visit(*this); } } @@ -134,7 +136,10 @@ void InlineContentInjector::visit(InterfaceBlockReference &iface) void InlineContentInjector::visit(FunctionCall &call) { if(!remap_names && call.declaration) + { dependencies.insert(call.declaration); + referenced_names.insert(call.name); + } TraversingVisitor::visit(call); } @@ -142,32 +147,33 @@ void InlineContentInjector::visit(VariableDeclaration &var) { TraversingVisitor::visit(var); - if(var.type_declaration) + if(remap_names) + { + if(remap_names==2 || referenced_names.count(var.name)) + { + string mapped_name = get_unused_variable_name(*target_block, var.name, remap_prefix); + variable_map[var.name] = &var; + var.name = mapped_name; + } + } + else if(var.type_declaration) { SetFlag set_deps(deps_only); dependencies.insert(var.type_declaration); + referenced_names.insert(var.type_declaration->name); var.type_declaration->visit(*this); } - - if(!remap_names && !deps_only) - { - RefPtr inlined_var = var.clone(); - inlined_var->name = create_unused_name(var.name, false); - r_inlined_statement = inlined_var; - - variable_map[var.name] = inlined_var.get(); - } } void InlineContentInjector::visit(Return &ret) { TraversingVisitor::visit(ret); - if(ret.expression) + if(!remap_names && ret.expression) { /* Create a new variable to hold the return value of the inlined function. */ - r_result_name = create_unused_name("return", true); + r_result_name = get_unused_variable_name(*target_block, "_return", source_func->name); RefPtr var = new VariableDeclaration; var->source = ret.source; var->line = ret.line; @@ -289,33 +295,6 @@ bool ExpressionInliner::apply(Stage &s) return r_any_inlined; } -void ExpressionInliner::visit_and_record(RefPtr &ptr) -{ - r_ref_info = 0; - ptr->visit(*this); - if(r_ref_info && r_ref_info->expression && r_ref_info->available) - { - if(iteration_body && !r_ref_info->trivial) - { - /* Don't inline non-trivial expressions which were assigned outside - an iteration statement. The iteration may run multiple times, which - would cause the expression to also be evaluated multiple times. */ - Block *i = r_ref_info->assign_scope; - for(; (i && i!=iteration_body); i=i->parent) ; - if(!i) - return; - } - - if(r_ref_info->trivial) - inline_expression(*r_ref_info->expression, ptr); - else - /* Record the inline point for a non-trivial expression but don't - inline it yet. It might turn out it shouldn't be inlined after all. */ - r_ref_info->inline_point = &ptr; - } - r_ref_info = 0; -} - void ExpressionInliner::inline_expression(Expression &expr, RefPtr &ptr) { ptr = expr.clone(); @@ -348,7 +327,30 @@ void ExpressionInliner::visit(Block &block) void ExpressionInliner::visit(RefPtr &expr) { - visit_and_record(expr); + r_ref_info = 0; + expr->visit(*this); + if(r_ref_info && r_ref_info->expression && r_ref_info->available) + { + if(iteration_body && !r_ref_info->trivial) + { + /* Don't inline non-trivial expressions which were assigned outside + an iteration statement. The iteration may run multiple times, which + would cause the expression to also be evaluated multiple times. */ + Block *i = r_ref_info->assign_scope; + for(; (i && i!=iteration_body); i=i->parent) ; + if(!i) + return; + } + + if(r_ref_info->trivial) + inline_expression(*r_ref_info->expression, expr); + else + /* Record the inline point for a non-trivial expression but don't + inline it yet. It might turn out it shouldn't be inlined after all. */ + r_ref_info->inline_point = &expr; + } + r_oper = expr->oper; + r_ref_info = 0; } void ExpressionInliner::visit(VariableReference &var) @@ -373,34 +375,30 @@ void ExpressionInliner::visit(VariableReference &var) void ExpressionInliner::visit(MemberAccess &memacc) { - visit_and_record(memacc.left); - r_oper = memacc.oper; + visit(memacc.left); r_trivial = false; } void ExpressionInliner::visit(Swizzle &swizzle) { - visit_and_record(swizzle.left); - r_oper = swizzle.oper; + visit(swizzle.left); r_trivial = false; } void ExpressionInliner::visit(UnaryExpression &unary) { SetFlag set_target(mutating, mutating || unary.oper->token[1]=='+' || unary.oper->token[1]=='-'); - visit_and_record(unary.expression); - r_oper = unary.oper; + visit(unary.expression); r_trivial = false; } void ExpressionInliner::visit(BinaryExpression &binary) { - visit_and_record(binary.left); + visit(binary.left); { SetFlag clear_target(mutating, false); - visit_and_record(binary.right); + visit(binary.right); } - r_oper = binary.oper; r_trivial = false; } @@ -408,10 +406,10 @@ void ExpressionInliner::visit(Assignment &assign) { { SetFlag set_target(mutating); - visit_and_record(assign.left); + visit(assign.left); } r_oper = 0; - visit_and_record(assign.right); + visit(assign.right); map::iterator i = expressions.find(assign.target); if(i!=expressions.end()) @@ -424,23 +422,20 @@ void ExpressionInliner::visit(Assignment &assign) i->second.available = true; } - r_oper = assign.oper; r_trivial = false; } void ExpressionInliner::visit(TernaryExpression &ternary) { - visit_and_record(ternary.condition); - visit_and_record(ternary.true_expr); - visit_and_record(ternary.false_expr); - r_oper = ternary.oper; + visit(ternary.condition); + visit(ternary.true_expr); + visit(ternary.false_expr); r_trivial = false; } void ExpressionInliner::visit(FunctionCall &call) { TraversingVisitor::visit(call); - r_oper = 0; r_trivial = false; }