]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/compiler.cpp
Refactor the way of applying visitors to stages
[libs/gl.git] / source / glsl / compiler.cpp
index e6c035775a89b0ff1aaa98464755d031a73fed98..21060cb4b783975f72da2a039de2f6a07b86888f 100644 (file)
@@ -65,17 +65,19 @@ void Compiler::add_shaders(Program &program)
        {
                for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
                {
+                       string stage_src = Formatter().apply(*i);
+
                        if(i->type==Stage::VERTEX)
                        {
-                               program.attach_shader_owned(new VertexShader(apply<Formatter>(*i)));
+                               program.attach_shader_owned(new VertexShader(stage_src));
                                for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
                                        program.bind_attribute(j->second, j->first);
                        }
                        else if(i->type==Stage::GEOMETRY)
-                               program.attach_shader_owned(new GeometryShader(apply<Formatter>(*i)));
+                               program.attach_shader_owned(new GeometryShader(stage_src));
                        else if(i->type==Stage::FRAGMENT)
                        {
-                               program.attach_shader_owned(new FragmentShader(apply<Formatter>(*i)));
+                               program.attach_shader_owned(new FragmentShader(stage_src));
                                if(EXT_gpu_shader4)
                                {
                                        for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
@@ -124,10 +126,10 @@ void Compiler::add_shaders(Program &program)
 
 void Compiler::append_module(Module &mod)
 {
-       vector<Import *> imports = apply<NodeGatherer<Import> >(mod.shared);
+       vector<Import *> imports = NodeGatherer<Import>().apply(mod.shared);
        for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
                import((*i)->module);
-       apply<NodeRemover>(mod.shared, set<Node *>(imports.begin(), imports.end()));
+       NodeRemover(set<Node *>(imports.begin(), imports.end())).apply(mod.shared);
 
        append_stage(mod.shared);
        for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
@@ -160,7 +162,7 @@ void Compiler::append_stage(Stage &stage)
                target->required_version = stage.required_version;
        for(NodeList<Statement>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
                target->content.body.push_back(*i);
-       apply<DeclarationCombiner>(*target);
+       DeclarationCombiner().apply(*target);
 }
 
 void Compiler::process()
@@ -198,27 +200,27 @@ void Compiler::generate(Stage &stage)
                stage.required_version = module->shared.required_version;
        inject_block(stage.content, module->shared.content);
 
-       apply<DeclarationReorderer>(stage);
-       apply<FunctionResolver>(stage);
-       apply<VariableResolver>(stage);
-       apply<InterfaceGenerator>(stage);
-       apply<VariableResolver>(stage);
-       apply<DeclarationReorderer>(stage);
-       apply<FunctionResolver>(stage);
-       apply<LegacyConverter>(stage);
+       DeclarationReorderer().apply(stage);
+       FunctionResolver().apply(stage);
+       VariableResolver().apply(stage);
+       InterfaceGenerator().apply(stage);
+       VariableResolver().apply(stage);
+       DeclarationReorderer().apply(stage);
+       FunctionResolver().apply(stage);
+       LegacyConverter().apply(stage);
 }
 
 bool Compiler::optimize(Stage &stage)
 {
-       apply<ConstantConditionEliminator>(stage);
+       ConstantConditionEliminator().apply(stage);
 
-       set<FunctionDeclaration *> inlineable = apply<InlineableFunctionLocator>(stage);
-       apply<FunctionInliner>(stage, inlineable);
+       set<FunctionDeclaration *> inlineable = InlineableFunctionLocator().apply(stage);
+       FunctionInliner(inlineable).apply(stage);
 
-       set<Node *> unused = apply<UnusedVariableLocator>(stage);
-       set<Node *> unused2 = apply<UnusedFunctionLocator>(stage);
+       set<Node *> unused = UnusedVariableLocator().apply(stage);
+       set<Node *> unused2 = UnusedFunctionLocator().apply(stage);
        unused.insert(unused2.begin(), unused2.end());
-       apply<NodeRemover>(stage, unused);
+       NodeRemover(unused).apply(stage);
 
        return !unused.empty();
 }
@@ -226,9 +228,9 @@ bool Compiler::optimize(Stage &stage)
 void Compiler::finalize(Stage &stage)
 {
        if(get_gl_api()==OPENGL_ES2)
-               apply<DefaultPrecisionGenerator>(stage);
+               DefaultPrecisionGenerator().apply(stage);
        else
-               apply<PrecisionRemover>(stage);
+               PrecisionRemover().apply(stage);
 }
 
 void Compiler::inject_block(Block &target, const Block &source)
@@ -238,22 +240,6 @@ void Compiler::inject_block(Block &target, const Block &source)
                target.body.insert(insert_point, (*i)->clone());
 }
 
-template<typename T>
-typename T::ResultType Compiler::apply(Stage &stage)
-{
-       T visitor;
-       visitor.apply(stage);
-       return visitor.get_result();
-}
-
-template<typename T, typename A>
-typename T::ResultType Compiler::apply(Stage &stage, const A &arg)
-{
-       T visitor(arg);
-       visitor.apply(stage);
-       return visitor.get_result();
-}
-
 } // namespace SL
 } // namespace GL
 } // namespace Msp