X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fprogramcompiler.cpp;h=ecdcd519f69c1cdb4410ee377b0073e9772028ea;hb=009918a51aa309eeceb6c02ecaebe45e8d357033;hp=92dec9c778913388620f2bcd0dde7e6903a4a276;hpb=a36992487d018d8801ead6980b362b00a2f5f5c5;p=libs%2Fgl.git diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index 92dec9c7..ecdcd519 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -51,28 +51,45 @@ void ProgramCompiler::add_shaders(Program &program) void ProgramCompiler::process() { if(module->vertex_context.present) - process(module->vertex_context); + generate(module->vertex_context); if(module->geometry_context.present) - process(module->geometry_context); + generate(module->geometry_context); if(module->fragment_context.present) - process(module->fragment_context); + generate(module->fragment_context); + + if(module->vertex_context.present) + optimize(module->vertex_context); + if(module->geometry_context.present) + optimize(module->geometry_context); + if(module->fragment_context.present) + optimize(module->fragment_context); } -void ProgramCompiler::process(Context &context) +void ProgramCompiler::generate(Context &context) { inject_block(context.content, module->global_context.content); - VariableResolver resolver; - context.content.visit(resolver); + resolve_variables(context); + + InterfaceGenerator generator; + generator.visit(context); + + resolve_variables(context); + VariableRenamer renamer; + context.content.visit(renamer); +} + +void ProgramCompiler::optimize(Context &context) +{ while(1) { UnusedVariableLocator unused_locator; - context.content.visit(unused_locator); + unused_locator.visit(context); NodeRemover remover; - remover.to_remove.insert(unused_locator.unused_variables.begin(), unused_locator.unused_variables.end()); - context.content.visit(remover); + remover.to_remove = unused_locator.unused_nodes; + remover.visit(context); if(!remover.n_removed) break; @@ -86,6 +103,12 @@ void ProgramCompiler::inject_block(Block &target, const Block &source) target.body.insert(insert_point, (*i)->clone()); } +void ProgramCompiler::resolve_variables(Context &context) +{ + VariableResolver resolver; + context.content.visit(resolver); +} + string ProgramCompiler::format_context(Context &context) { Formatter formatter; @@ -395,51 +418,293 @@ void ProgramCompiler::VariableResolver::visit(InterfaceBlock &iface) } +ProgramCompiler::InterfaceGenerator::InterfaceGenerator(): + context(0), + scope_level(0), + remove_node(false) +{ } + +string ProgramCompiler::InterfaceGenerator::get_out_prefix(ContextType type) +{ + if(type==VERTEX) + return "_vs_out_"; + else if(type==GEOMETRY) + return "_gs_out_"; + else + return string(); +} + +void ProgramCompiler::InterfaceGenerator::visit(Context &ctx) +{ + SetForScope set(context, &ctx); + if(context->previous) + in_prefix = get_out_prefix(context->previous->type); + out_prefix = get_out_prefix(context->type); + ctx.content.visit(*this); +} + +void ProgramCompiler::InterfaceGenerator::visit(Block &block) +{ + SetForScope set(scope_level, scope_level+1); + for(list >::iterator i=block.body.begin(); i!=block.body.end(); ) + { + (*i)->visit(*this); + + if(scope_level==1) + { + for(map >::iterator j=iface_declarations.begin(); j!=iface_declarations.end(); ++j) + { + list >::iterator k = block.body.insert(i, j->second); + (*k)->visit(*this); + } + iface_declarations.clear(); + } + + for(list >::iterator j=insert_nodes.begin(); j!=insert_nodes.end(); ++j) + block.body.insert(i, *j); + insert_nodes.clear(); + + if(remove_node) + block.body.erase(i++); + else + ++i; + remove_node = false; + } +} + +string ProgramCompiler::InterfaceGenerator::change_prefix(const string &name, const string &prefix) const +{ + unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size()); + return prefix+name.substr(offset); +} + +bool ProgramCompiler::InterfaceGenerator::generate_interface(VariableDeclaration &out, const string &iface, const string &name) +{ + const map &context_vars = (iface=="in" ? context->in_variables : context->out_variables); + if(context_vars.count(name) || iface_declarations.count(name)) + return false; + + VariableDeclaration* iface_var = new VariableDeclaration; + iface_var->sampling = out.sampling; + iface_var->interface = iface; + iface_var->type = out.type; + iface_var->type_declaration = out.type_declaration; + iface_var->name = name; + iface_var->array = (out.array || (context->type==GEOMETRY && iface=="in")); + iface_var->array_size = out.array_size; + if(iface=="in") + iface_var->linked_declaration = &out; + iface_declarations[iface_var->name] = iface_var; + + return true; +} + +void ProgramCompiler::InterfaceGenerator::insert_assignment(const string &left, ProgramSyntax::Expression *right) +{ + BinaryExpression *assign = new BinaryExpression; + VariableReference *ref = new VariableReference; + ref->name = left; + assign->left = ref; + assign->oper = "="; + assign->right = right; + assign->assignment = true; + + ExpressionStatement *stmt = new ExpressionStatement; + stmt->expression = assign; + insert_nodes.push_back(stmt); +} + +void ProgramCompiler::InterfaceGenerator::visit(VariableReference &var) +{ + if(var.declaration || !context->previous) + return; + if(iface_declarations.count(var.name)) + return; + + const map &prev_out = context->previous->out_variables; + map::const_iterator i = prev_out.find(var.name); + if(i==prev_out.end()) + i = prev_out.find(in_prefix+var.name); + if(i!=prev_out.end()) + generate_interface(*i->second, "in", var.name); +} + +void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var) +{ + if(var.interface=="out") + { + if(scope_level==1) + context->out_variables[var.name] = &var; + else if(generate_interface(var, "out", change_prefix(var.name, string()))) + { + remove_node = true; + if(var.init_expression) + insert_assignment(var.name, var.init_expression->clone()); + } + } + else if(var.interface=="in") + { + context->in_variables[var.name] = &var; + if(var.linked_declaration) + var.linked_declaration->linked_declaration = &var; + else if(context->previous) + { + const map &prev_out = context->previous->out_variables; + map::const_iterator i = prev_out.find(var.name); + if(i!=prev_out.end()) + { + var.linked_declaration = i->second; + i->second->linked_declaration = &var; + } + } + } + + TraversingVisitor::visit(var); +} + +void ProgramCompiler::InterfaceGenerator::visit(Passthrough &pass) +{ + if(context->previous) + { + const map &prev_out = context->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); + + 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); + } + } + + remove_node = true; +} + + +void ProgramCompiler::VariableRenamer::visit(VariableReference &var) +{ + if(var.declaration) + var.name = var.declaration->name; +} + +void ProgramCompiler::VariableRenamer::visit(VariableDeclaration &var) +{ + if(var.linked_declaration) + var.name = var.linked_declaration->name; + TraversingVisitor::visit(var); +} + + +ProgramCompiler::UnusedVariableLocator::UnusedVariableLocator(): + context(0), + assignment(false), + assignment_target(0) +{ } + +void ProgramCompiler::UnusedVariableLocator::visit(Context &ctx) +{ + context = &ctx; + ctx.content.visit(*this); +} + void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var) { - unused_variables.erase(var.declaration); + if(assignment) + assignment_target = var.declaration; + else + { + unused_nodes.erase(var.declaration); + map::iterator i = assignments.find(var.declaration); + if(i!=assignments.end()) + unused_nodes.erase(i->second); + } } void ProgramCompiler::UnusedVariableLocator::visit(MemberAccess &memacc) { TraversingVisitor::visit(memacc); - unused_variables.erase(memacc.declaration); + unused_nodes.erase(memacc.declaration); +} + +void ProgramCompiler::UnusedVariableLocator::visit(BinaryExpression &binary) +{ + if(binary.assignment) + { + binary.right->visit(*this); + assignment = true; + binary.left->visit(*this); + } + else + TraversingVisitor::visit(binary); +} + +void ProgramCompiler::UnusedVariableLocator::visit(ExpressionStatement &expr) +{ + assignment = false; + assignment_target = 0; + TraversingVisitor::visit(expr); + if(assignment && assignment_target) + { + if(assignment_target->interface!="out" || (context->type!=FRAGMENT && !assignment_target->linked_declaration)) + { + unused_nodes.insert(&expr); + assignments[assignment_target] = &expr; + } + else + unused_nodes.erase(assignment_target); + } + assignment = false; } void ProgramCompiler::UnusedVariableLocator::visit(VariableDeclaration &var) { - unused_variables.insert(&var); + unused_nodes.insert(&var); TraversingVisitor::visit(var); } ProgramCompiler::NodeRemover::NodeRemover(): + context(0), n_removed(0), immutable_block(false), remove_block(false) { } +void ProgramCompiler::NodeRemover::visit(Context &ctx) +{ + context = &ctx; + ctx.content.visit(*this); +} + void ProgramCompiler::NodeRemover::visit(Block &block) { remove_block = immutable_block; for(list >::iterator i=block.body.begin(); i!=block.body.end(); ) { - bool remove = false; - if(to_remove.count(&**i)) - remove = !immutable_block; - else - { + bool remove = to_remove.count(&**i); + if(!remove) remove_block = false; - (*i)->visit(*this); - remove = remove_block; - } + (*i)->visit(*this); - if(remove) + if(remove ? !immutable_block : remove_block) + { block.body.erase(i++); + ++n_removed; + } else ++i; - - n_removed += remove; } } @@ -449,6 +714,17 @@ void ProgramCompiler::NodeRemover::visit(StructDeclaration &strct) TraversingVisitor::visit(strct); } +void ProgramCompiler::NodeRemover::visit(VariableDeclaration &var) +{ + if(to_remove.count(&var)) + { + context->in_variables.erase(var.name); + context->out_variables.erase(var.name); + if(var.linked_declaration) + var.linked_declaration->linked_declaration = 0; + } +} + void ProgramCompiler::NodeRemover::visit(InterfaceBlock &iface) { SetFlag set(immutable_block);