]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/optimize.cpp
Remove output variables which are not referenced
[libs/gl.git] / source / glsl / optimize.cpp
index 42e4a86ec7feb3466f7e0a119b4bf1a4feb28916..1e974c9f01f8eee6d3eea5faabc00b20759ffd37 100644 (file)
@@ -717,6 +717,11 @@ void AggregateDismantler::visit(VariableDeclaration &var)
        }
 }
 
+void AggregateDismantler::visit(FunctionDeclaration &func)
+{
+       func.body.visit(*this);
+}
+
 
 template<typename T>
 T ConstantFolder::evaluate_logical(char oper, T left, T right)
@@ -1255,23 +1260,15 @@ bool UnusedVariableRemover::apply(Stage &s)
 
        for(const auto &kvp: variables)
        {
-               if(kvp.second.output)
+               if(!kvp.second.referenced)
+                       unused_nodes.insert(kvp.first);
+               else if(kvp.second.output)
                {
                        /* The last visible assignments of output variables are used by the
                        next stage or the API. */
                        for(AssignmentInfo *a: kvp.second.assignments)
                                unused_nodes.erase(a->node);
                }
-
-               if(!kvp.second.output && !kvp.second.referenced)
-               {
-                       // Don't remove variables from inside interface blocks.
-                       if(!kvp.second.interface_block)
-                               unused_nodes.insert(kvp.first);
-               }
-               else if(kvp.second.interface_block)
-                       // Interface blocks are kept if even one member is used.
-                       unused_nodes.erase(kvp.second.interface_block);
        }
 
        NodeRemover().apply(s, unused_nodes);
@@ -1488,14 +1485,14 @@ void UnusedVariableRemover::visit(VariableDeclaration &var)
                return;
 
        VariableInfo &var_info = variables[&var];
-       var_info.interface_block = interface_block;
 
        /* Mark variables as output if they're used by the next stage or the
        graphics API. */
-       if(interface_block)
-               var_info.output = (interface_block->interface=="out" && (interface_block->linked_block || !interface_block->block_name.compare(0, 3, "gl_")));
-       else
-               var_info.output = (var.interface=="out" && (stage->type==Stage::FRAGMENT || var.linked_declaration || !var.name.compare(0, 3, "gl_")));
+       var_info.output = (var.interface=="out" && (stage->type==Stage::FRAGMENT || var.linked_declaration || !var.name.compare(0, 3, "gl_")));
+
+       // Linked outputs are automatically referenced.
+       if(var_info.output && var.linked_declaration)
+               var_info.referenced = true;
 
        if(var.init_expression)
        {