X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Foptimize.cpp;h=6e4b56771fa3222ce778a53d37b828b5d34554cf;hb=1a5dafe20e47c764f2914c341fb7b8f1fba59fb8;hp=1e974c9f01f8eee6d3eea5faabc00b20759ffd37;hpb=209fc09bcf2947ca60ddf15b8903d0c3194c7d5d;p=libs%2Fgl.git diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 1e974c9f..6e4b5677 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -114,9 +114,17 @@ string InlineContentInjector::apply(Stage &stage, FunctionDeclaration &target_fu source_func = call.declaration->definition; /* Populate referenced_names from the target function so we can rename - variables from the inlined function that would conflict. */ + variables from the inlined function that would conflict. Only consider + names declared in blocks linearly related to the target block. */ pass = REFERENCED; - target_func.visit(*this); + tgt_blk.visit(*this); + for(const Block *b=&tgt_blk; b; b=b->parent) + for(const auto &kvp: b->variables) + referenced_names.insert(kvp.first); + for(const auto &kvp: stage.interface_blocks) + if(kvp.second->name.find(' ')!=string::npos) + for(const auto &kvp2: kvp.second->block_declaration->members.variables) + referenced_names.insert(kvp2.first); /* Inline and rename passes must be interleaved so used variable names are known when inlining the return statement. */ @@ -187,12 +195,6 @@ void InlineContentInjector::visit(VariableReference &var) referenced_names.insert(var.name); } -void InlineContentInjector::visit(InterfaceBlockReference &iface) -{ - if(pass==REFERENCED) - referenced_names.insert(iface.name); -} - void InlineContentInjector::visit(FunctionCall &call) { if(pass==REFERENCED) @@ -356,7 +358,7 @@ void ExpressionInliner::visit(RefPtr &expr) ExpressionUse use; use.reference = &expr; use.ref_scope = current_block; - use.blocked = access_write; + use.blocked = access_write || r_ref_info->blocked; if(iteration_body && !r_ref_info->trivial) { @@ -402,7 +404,7 @@ void ExpressionInliner::visit(Swizzle &swizzle) void ExpressionInliner::visit(UnaryExpression &unary) { - SetFlag set_write(access_write, access_write || unary.oper->token[1]=='+' || unary.oper->token[1]=='-'); + SetFlag set_write(access_write, (unary.oper->token[1]=='+' || unary.oper->token[1]=='-')); visit(unary.expression); r_trivial = false; } @@ -428,10 +430,10 @@ void ExpressionInliner::visit(Assignment &assign) r_trivial = true; visit(assign.right); - auto i = assignments.find(assign.target); + auto i = assignments.find(assign.target.declaration); if(i!=assignments.end()) { - if(iteration_body && i->second->expression) + if(iteration_body && i->second && i->second->expression) { /* Block inlining into previous references within the iteration statement. On iterations after the first they would refer to the @@ -441,7 +443,11 @@ void ExpressionInliner::visit(Assignment &assign) u.blocked = (k==iteration_body); } - expressions.push_back(ExpressionInfo()); + for(; (i!=assignments.end() && i->first.declaration==assign.target.declaration); ++i) + if(targets_overlap(i->first, assign.target)) + i->second->blocked = true; + + expressions.emplace_back(); ExpressionInfo &info = expressions.back(); info.target = assign.target; // Self-referencing assignments can't be inlined without additional work. @@ -450,7 +456,7 @@ void ExpressionInliner::visit(Assignment &assign) info.assign_scope = current_block; info.trivial = r_trivial; - i->second = &info; + assignments[assign.target] = &info; } r_trivial = false; @@ -488,7 +494,7 @@ void ExpressionInliner::visit(VariableDeclaration &var) analyze and non-trivial expressions could be expensive to inline. */ if((current_block->parent || (constant && r_trivial)) && var.interface.empty()) { - expressions.push_back(ExpressionInfo()); + expressions.emplace_back(); ExpressionInfo &info = expressions.back(); info.target = &var; /* Assume variables declared in an iteration initialization statement @@ -1049,10 +1055,11 @@ void ConstantFolder::visit(Iteration &iter) } -void ConstantConditionEliminator::apply(Stage &stage) +bool ConstantConditionEliminator::apply(Stage &stage) { stage.content.visit(*this); NodeRemover().apply(stage, nodes_to_remove); + return !nodes_to_remove.empty(); } ConstantConditionEliminator::ConstantStatus ConstantConditionEliminator::check_constant_condition(const Expression &expr) @@ -1237,11 +1244,6 @@ void UnusedTypeRemover::visit(VariableDeclaration &var) TraversingVisitor::visit(var); } -void UnusedTypeRemover::visit(InterfaceBlock &iface) -{ - unused_nodes.erase(iface.type_declaration); -} - void UnusedTypeRemover::visit(FunctionDeclaration &func) { unused_nodes.erase(func.return_type_declaration); @@ -1284,36 +1286,12 @@ void UnusedVariableRemover::referenced(const Assignment::Target &target, Node &n { bool loop_external = false; for(AssignmentInfo *a: var_info.assignments) - { - bool covered = true; - for(unsigned j=0; (covered && jtarget.chain_len && j(a->target.chain[j]&0xC0); - Assignment::Target::ChainType type2 = static_cast(target.chain[j]&0xC0); - unsigned index1 = a->target.chain[j]&0x3F; - unsigned index2 = target.chain[j]&0x3F; - if(type1==Assignment::Target::SWIZZLE || type2==Assignment::Target::SWIZZLE) - { - if(type1==Assignment::Target::SWIZZLE && type2==Assignment::Target::SWIZZLE) - covered = index1&index2; - else if(type1==Assignment::Target::ARRAY && index1<4) - covered = index2&(1<target, target)) { a->used_by.push_back(&node); if(a->in_loop clear_reference(r_reference, Assignment::Target()); binary.right->visit(*this); } @@ -1434,7 +1405,7 @@ void UnusedVariableRemover::visit(FunctionCall &call) void UnusedVariableRemover::record_assignment(const Assignment::Target &target, Node &node) { - assignments.push_back(AssignmentInfo()); + assignments.emplace_back(); AssignmentInfo &assign_info = assignments.back(); assign_info.node = &node; assign_info.target = target; @@ -1488,7 +1459,8 @@ void UnusedVariableRemover::visit(VariableDeclaration &var) /* Mark variables as output if they're used by the next stage or the graphics API. */ - var_info.output = (var.interface=="out" && (stage->type==Stage::FRAGMENT || var.linked_declaration || !var.name.compare(0, 3, "gl_"))); + bool builtin = (!var.name.compare(0, 3, "gl_") || (var.block_declaration && !var.block_declaration->block_name.compare(0, 3, "gl_"))); + var_info.output = (var.interface=="out" && (stage->type==Stage::FRAGMENT || var.linked_declaration || builtin)); // Linked outputs are automatically referenced. if(var_info.output && var.linked_declaration) @@ -1501,12 +1473,6 @@ void UnusedVariableRemover::visit(VariableDeclaration &var) } } -void UnusedVariableRemover::visit(InterfaceBlock &iface) -{ - VariableInfo &var_info = variables[&iface]; - var_info.output = (iface.interface=="out" && (iface.linked_block || !iface.block_name.compare(0, 3, "gl_"))); -} - void UnusedVariableRemover::merge_variables(const BlockVariableMap &other_vars) { for(const auto &kvp: other_vars) @@ -1573,8 +1539,14 @@ void UnusedVariableRemover::visit(Iteration &iter) vector saved_refs; swap(loop_ext_refs, saved_refs); { + if(iter.init_statement) + iter.init_statement->visit(*this); SetForScope set_loop(in_loop, in_loop+1); - TraversingVisitor::visit(iter); + if(iter.condition) + iter.condition->visit(*this); + iter.body.visit(*this); + if(iter.loop_expression) + iter.loop_expression->visit(*this); } swap(loop_ext_refs, saved_refs);