]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/compiler.cpp
Further improve inlining of GLSL functions
[libs/gl.git] / source / glsl / compiler.cpp
index c68b67bd0cd629f81db2838c60840b43c1abe720..1eeb36252ba899a8f8851798f2a393a1223cc05d 100644 (file)
@@ -76,11 +76,13 @@ void Compiler::compile(Mode mode)
 {
        for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
                generate(*i, mode);
-       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
+       unsigned n = 0;
+       for(list<Stage>::iterator i=module->stages.begin(); (i!=module->stages.end() && n<10000); ++n)
        {
-               if(optimize(*i))
+               OptimizeResult result = optimize(*i);
+               if(result==REDO_PREVIOUS)
                        i = module->stages.begin();
-               else
+               else if(result!=REDO_STAGE)
                        ++i;
        }
        for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
@@ -221,8 +223,6 @@ void Compiler::generate(Stage &stage, Mode mode)
                stage.required_features.glsl_version = module->shared.required_features.glsl_version;
        inject_block(stage.content, module->shared.content);
 
-       DeclarationReorderer().apply(stage);
-
        // Initial resolving pass
        BlockHierarchyResolver().apply(stage);
        FunctionResolver().apply(stage);
@@ -233,27 +233,29 @@ void Compiler::generate(Stage &stage, Mode mode)
        InterfaceGenerator().apply(stage);
        VariableResolver().apply(stage);
 
-       DeclarationReorderer().apply(stage);
        FunctionResolver().apply(stage);
        ConstantSpecializer().apply(stage, (mode==PROGRAM && specialized ? &spec_values : 0));
        if(mode==PROGRAM)
                LegacyConverter().apply(stage, features);
 }
 
-bool Compiler::optimize(Stage &stage)
+Compiler::OptimizeResult Compiler::optimize(Stage &stage)
 {
        ConstantConditionEliminator().apply(stage);
 
-       FunctionInliner().apply(stage);
-       BlockHierarchyResolver().apply(stage);
-       VariableResolver().apply(stage);
+       bool any_inlined = FunctionInliner().apply(stage);
+       if(any_inlined)
+       {
+               VariableResolver().apply(stage);
+               FunctionResolver().apply(stage);
+       }
 
        /* Removing variables or functions may cause things from the previous stage
        to become unused. */
-       bool result = UnusedVariableRemover().apply(stage);
-       result |= UnusedFunctionRemover().apply(stage);
+       bool any_removed = UnusedVariableRemover().apply(stage);
+       any_removed |= UnusedFunctionRemover().apply(stage);
 
-       return result;
+       return any_removed ? REDO_PREVIOUS : any_inlined ? REDO_STAGE : NEXT_STAGE;
 }
 
 void Compiler::finalize(Stage &stage, Mode mode)