X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fprogramcompiler.cpp;h=63939409fc4c7d28c6f38cc031925cacb919257f;hb=7434b80c452eb8f7ac8200a6f693a9eb668421f0;hp=fdbc83c7d4be404d7f07fe5a7c4757ac74596926;hpb=2b073e0a3808f8ece4b93669395e4b812214cf5d;p=libs%2Fgl.git diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index fdbc83c7..63939409 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -58,11 +58,11 @@ void ProgramCompiler::add_shaders(Program &program) for(list::iterator i=module->stages.begin(); i!=module->stages.end(); ++i) { if(i->type==VERTEX) - program.attach_shader_owned(new VertexShader(head+create_source(*i))); + program.attach_shader_owned(new VertexShader(head+apply(*i))); else if(i->type==GEOMETRY) - program.attach_shader_owned(new GeometryShader(head+create_source(*i))); + program.attach_shader_owned(new GeometryShader(head+apply(*i))); else if(i->type==FRAGMENT) - program.attach_shader_owned(new FragmentShader(head+create_source(*i))); + program.attach_shader_owned(new FragmentShader(head+apply(*i))); } program.bind_attribute(VERTEX4, "vertex"); @@ -125,14 +125,10 @@ void ProgramCompiler::generate(Stage &stage) bool ProgramCompiler::optimize(Stage &stage) { - UnusedVariableLocator unused_locator; - unused_locator.apply(stage); + set unused = apply(stage); + apply(stage, unused); - NodeRemover remover; - remover.to_remove = unused_locator.unused_nodes; - remover.apply(stage); - - return !unused_locator.unused_nodes.empty(); + return !unused.empty(); } void ProgramCompiler::inject_block(Block &target, const Block &source) @@ -143,17 +139,19 @@ void ProgramCompiler::inject_block(Block &target, const Block &source) } template -void ProgramCompiler::apply(Stage &stage) +typename T::ResultType ProgramCompiler::apply(Stage &stage) { T visitor; visitor.apply(stage); + return visitor.get_result(); } -string ProgramCompiler::create_source(Stage &stage) +template +typename T::ResultType ProgramCompiler::apply(Stage &stage, const A &arg) { - Formatter formatter; - formatter.apply(stage); - return formatter.formatted; + T visitor(arg); + visitor.apply(stage); + return visitor.get_result(); } @@ -171,7 +169,7 @@ void ProgramCompiler::Visitor::apply(Stage &s) ProgramCompiler::Formatter::Formatter(): indent(0), parameter_list(false), - else_if(false) + else_if(0) { } void ProgramCompiler::Formatter::visit(Literal &literal) @@ -241,31 +239,27 @@ void ProgramCompiler::Formatter::visit(ExpressionStatement &expr) void ProgramCompiler::Formatter::visit(Block &block) { - if(block.use_braces) - { - if(else_if) - { - formatted += '\n'; - else_if = false; - } - formatted += format("%s{\n", string(indent*2, ' ')); - } + if(else_if) + --else_if; + + unsigned brace_indent = indent; + bool use_braces = (block.use_braces || (indent && block.body.size()!=1)); + if(use_braces) + formatted += format("%s{\n", string(brace_indent*2, ' ')); - bool change_indent = (!formatted.empty() && !else_if); - indent += change_indent; + SetForScope set(indent, indent+!formatted.empty()); string spaces(indent*2, ' '); for(list >::iterator i=block.body.begin(); i!=block.body.end(); ++i) { if(i!=block.body.begin()) formatted += '\n'; - if(!else_if) - formatted += spaces; + formatted += spaces; (*i)->visit(*this); + else_if = 0; } - indent -= change_indent; - if(block.use_braces) - formatted += format("\n%s}", string(indent*2, ' ')); + if(use_braces) + formatted += format("\n%s}", string(brace_indent*2, ' ')); } void ProgramCompiler::Formatter::visit(Layout &layout) @@ -345,10 +339,9 @@ void ProgramCompiler::Formatter::visit(FunctionDeclaration &func) void ProgramCompiler::Formatter::visit(Conditional &cond) { if(else_if) - { - formatted += ' '; - else_if = false; - } + formatted.replace(formatted.rfind('\n'), string::npos, 1, ' '); + + indent -= else_if; formatted += "if("; cond.condition->visit(*this); @@ -357,8 +350,8 @@ void ProgramCompiler::Formatter::visit(Conditional &cond) cond.body.visit(*this); if(!cond.else_body.body.empty()) { - formatted += format("\n%selse", string(indent*2, ' ')); - SetFlag set(else_if); + formatted += format("\n%selse\n", string(indent*2, ' ')); + SetForScope set(else_if, 2); cond.else_body.visit(*this); } } @@ -949,6 +942,10 @@ void ProgramCompiler::UnusedVariableLocator::visit(Iteration &iter) } +ProgramCompiler::NodeRemover::NodeRemover(const set &r): + to_remove(r) +{ } + void ProgramCompiler::NodeRemover::visit(Block &block) { for(list >::iterator i=block.body.begin(); i!=block.body.end(); )