X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fprogramcompiler.cpp;h=934416c28684f250fa8c37b9d842f5f05c39b7a9;hb=96881e56ececfa3f4b0707dc168c687b35d249a0;hp=2dae07c8f4d3ecc049c153e66de8793d347e3134;hpb=fd103d76d7546f7e22aefc18c090a844fc67409f;p=libs%2Fgl.git diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index 2dae07c8..934416c2 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -35,12 +35,15 @@ void ProgramCompiler::add_shaders(Program &program) throw invalid_operation("ProgramCompiler::add_shaders"); string head = "#version 150\n"; - if(module->vertex_stage.present) - program.attach_shader_owned(new VertexShader(head+format_stage(module->vertex_stage))); - if(module->geometry_stage.present) - program.attach_shader_owned(new GeometryShader(head+format_stage(module->geometry_stage))); - if(module->fragment_stage.present) - program.attach_shader_owned(new FragmentShader(head+format_stage(module->fragment_stage))); + 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))); + else if(i->type==GEOMETRY) + program.attach_shader_owned(new GeometryShader(head+create_source(*i))); + else if(i->type==FRAGMENT) + program.attach_shader_owned(new FragmentShader(head+create_source(*i))); + } program.bind_attribute(VERTEX4, "vertex"); program.bind_attribute(NORMAL3, "normal"); @@ -50,50 +53,37 @@ void ProgramCompiler::add_shaders(Program &program) void ProgramCompiler::process() { - if(module->vertex_stage.present) - generate(module->vertex_stage); - if(module->geometry_stage.present) - generate(module->geometry_stage); - if(module->fragment_stage.present) - generate(module->fragment_stage); - - if(module->vertex_stage.present) - optimize(module->vertex_stage); - if(module->geometry_stage.present) - optimize(module->geometry_stage); - if(module->fragment_stage.present) - optimize(module->fragment_stage); + for(list::iterator i=module->stages.begin(); i!=module->stages.end(); ++i) + generate(*i); + for(list::iterator i=module->stages.begin(); i!=module->stages.end(); ) + { + if(optimize(*i)) + i = module->stages.begin(); + else + ++i; + } } void ProgramCompiler::generate(Stage &stage) { inject_block(stage.content, module->shared.content); - resolve_variables(stage); - - InterfaceGenerator generator; - generator.visit(stage); - - resolve_variables(stage); - - VariableRenamer renamer; - stage.content.visit(renamer); + apply(stage); + apply(stage); + apply(stage); + apply(stage); } -void ProgramCompiler::optimize(Stage &stage) +bool ProgramCompiler::optimize(Stage &stage) { - while(1) - { - UnusedVariableLocator unused_locator; - unused_locator.visit(stage); + UnusedVariableLocator unused_locator; + unused_locator.apply(stage); - NodeRemover remover; - remover.to_remove = unused_locator.unused_nodes; - remover.visit(stage); + NodeRemover remover; + remover.to_remove = unused_locator.unused_nodes; + remover.apply(stage); - if(!remover.n_removed) - break; - } + return remover.n_removed; } void ProgramCompiler::inject_block(Block &target, const Block &source) @@ -103,20 +93,32 @@ void ProgramCompiler::inject_block(Block &target, const Block &source) target.body.insert(insert_point, (*i)->clone()); } -void ProgramCompiler::resolve_variables(Stage &stage) +template +void ProgramCompiler::apply(Stage &stage) { - VariableResolver resolver; - stage.content.visit(resolver); + T visitor; + visitor.apply(stage); } -string ProgramCompiler::format_stage(Stage &stage) +string ProgramCompiler::create_source(Stage &stage) { Formatter formatter; - stage.content.visit(formatter); + formatter.apply(stage); return formatter.formatted; } +ProgramCompiler::Visitor::Visitor(): + stage(0) +{ } + +void ProgramCompiler::Visitor::apply(Stage &s) +{ + SetForScope set(stage, &s); + stage->content.visit(*this); +} + + ProgramCompiler::Formatter::Formatter(): indent(0), parameter_list(false), @@ -419,7 +421,6 @@ void ProgramCompiler::VariableResolver::visit(InterfaceBlock &iface) ProgramCompiler::InterfaceGenerator::InterfaceGenerator(): - stage(0), scope_level(0), remove_node(false) { } @@ -434,7 +435,7 @@ string ProgramCompiler::InterfaceGenerator::get_out_prefix(StageType type) return string(); } -void ProgramCompiler::InterfaceGenerator::visit(Stage &s) +void ProgramCompiler::InterfaceGenerator::apply(Stage &s) { SetForScope set(stage, &s); if(stage->previous) @@ -452,7 +453,7 @@ void ProgramCompiler::InterfaceGenerator::visit(Block &block) if(scope_level==1) { - for(map >::iterator j=iface_declarations.begin(); j!=iface_declarations.end(); ++j) + for(map::iterator j=iface_declarations.begin(); j!=iface_declarations.end(); ++j) { list >::iterator k = block.body.insert(i, j->second); (*k)->visit(*this); @@ -460,7 +461,7 @@ void ProgramCompiler::InterfaceGenerator::visit(Block &block) iface_declarations.clear(); } - for(list >::iterator j=insert_nodes.begin(); j!=insert_nodes.end(); ++j) + for(list::iterator j=insert_nodes.begin(); j!=insert_nodes.end(); ++j) block.body.insert(i, *j); insert_nodes.clear(); @@ -478,23 +479,23 @@ string ProgramCompiler::InterfaceGenerator::change_prefix(const string &name, co return prefix+name.substr(offset); } -bool ProgramCompiler::InterfaceGenerator::generate_interface(VariableDeclaration &out, const string &iface, const string &name) +bool ProgramCompiler::InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name) { const map &stage_vars = (iface=="in" ? stage->in_variables : stage->out_variables); if(stage_vars.count(name) || iface_declarations.count(name)) return false; VariableDeclaration* iface_var = new VariableDeclaration; - iface_var->sampling = out.sampling; + iface_var->sampling = var.sampling; iface_var->interface = iface; - iface_var->type = out.type; - iface_var->type_declaration = out.type_declaration; + iface_var->type = var.type; + iface_var->type_declaration = var.type_declaration; iface_var->name = name; - iface_var->array = (out.array || (stage->type==GEOMETRY && iface=="in")); - iface_var->array_size = out.array_size; + iface_var->array = (var.array || (stage->type==GEOMETRY && iface=="in")); + iface_var->array_size = var.array_size; if(iface=="in") - iface_var->linked_declaration = &out; - iface_declarations[iface_var->name] = iface_var; + iface_var->linked_declaration = &var; + iface_declarations[name] = iface_var; return true; } @@ -564,31 +565,48 @@ void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var) void ProgramCompiler::InterfaceGenerator::visit(Passthrough &pass) { + vector pass_vars; + + for(map::const_iterator i=stage->in_variables.begin(); i!=stage->in_variables.end(); ++i) + pass_vars.push_back(i->second); + for(map::const_iterator i=iface_declarations.begin(); i!=iface_declarations.end(); ++i) + if(i->second->interface=="in") + pass_vars.push_back(i->second); + if(stage->previous) { const map &prev_out = stage->previous->out_variables; for(map::const_iterator i=prev_out.begin(); i!=prev_out.end(); ++i) { - string out_name = change_prefix(i->second->name, out_prefix); - generate_interface(*i->second, "in", i->second->name); - generate_interface(*i->second, "out", out_name); + bool linked = false; + for(vector::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j) + linked = ((*j)->linked_declaration==i->second); - VariableReference *ref = new VariableReference; - ref->name = i->first; - if(pass.subscript) - { - BinaryExpression *subscript = new BinaryExpression; - subscript->left = ref; - subscript->oper = "["; - subscript->right = pass.subscript; - subscript->after = "]"; - insert_assignment(out_name, subscript); - } - else - insert_assignment(out_name, ref); + if(!linked && generate_interface(*i->second, "in", i->second->name)) + pass_vars.push_back(i->second); } } + for(vector::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i) + { + string out_name = change_prefix((*i)->name, out_prefix); + generate_interface(**i, "out", out_name); + + VariableReference *ref = new VariableReference; + ref->name = (*i)->name; + if(pass.subscript) + { + BinaryExpression *subscript = new BinaryExpression; + subscript->left = ref; + subscript->oper = "["; + subscript->right = pass.subscript; + subscript->after = "]"; + insert_assignment(out_name, subscript); + } + else + insert_assignment(out_name, ref); + } + remove_node = true; } @@ -608,17 +626,10 @@ void ProgramCompiler::VariableRenamer::visit(VariableDeclaration &var) ProgramCompiler::UnusedVariableLocator::UnusedVariableLocator(): - stage(0), assignment(false), assignment_target(0) { } -void ProgramCompiler::UnusedVariableLocator::visit(Stage &s) -{ - stage = &s; - stage->content.visit(*this); -} - void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var) { if(assignment) @@ -628,7 +639,10 @@ void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var) unused_nodes.erase(var.declaration); map::iterator i = assignments.find(var.declaration); if(i!=assignments.end()) + { unused_nodes.erase(i->second); + assignments.erase(i); + } } } @@ -657,11 +671,12 @@ void ProgramCompiler::UnusedVariableLocator::visit(ExpressionStatement &expr) TraversingVisitor::visit(expr); if(assignment && assignment_target) { + Node *&assign = assignments[assignment_target]; + if(assign) + unused_nodes.insert(assign); + assign = &expr; if(assignment_target->interface!="out" || (stage->type!=FRAGMENT && !assignment_target->linked_declaration)) - { unused_nodes.insert(&expr); - assignments[assignment_target] = &expr; - } else unused_nodes.erase(assignment_target); } @@ -676,18 +691,11 @@ void ProgramCompiler::UnusedVariableLocator::visit(VariableDeclaration &var) ProgramCompiler::NodeRemover::NodeRemover(): - stage(0), n_removed(0), immutable_block(false), remove_block(false) { } -void ProgramCompiler::NodeRemover::visit(Stage &s) -{ - stage = &s; - stage->content.visit(*this); -} - void ProgramCompiler::NodeRemover::visit(Block &block) { remove_block = immutable_block;