X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fprogramcompiler.cpp;h=92dec9c778913388620f2bcd0dde7e6903a4a276;hb=a36992487d018d8801ead6980b362b00a2f5f5c5;hp=1914831289a9dde32c77892cb3bfdc5411fd4900;hpb=73ce62f3b9c2bbfc1e655a9df343389a733dc795;p=libs%2Fgl.git diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index 19148312..92dec9c7 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -1,3 +1,4 @@ +#include #include #include #include "error.h" @@ -19,11 +20,13 @@ ProgramCompiler::ProgramCompiler(): void ProgramCompiler::compile(const string &source) { module = &parser.parse(source); + process(); } void ProgramCompiler::compile(IO::Base &io) { module = &parser.parse(io); + process(); } void ProgramCompiler::add_shaders(Program &program) @@ -31,13 +34,13 @@ void ProgramCompiler::add_shaders(Program &program) if(!module) throw invalid_operation("ProgramCompiler::add_shaders"); - string global_source = "#version 150\n"+format_context(module->global_context); + string head = "#version 150\n"; if(module->vertex_context.present) - program.attach_shader_owned(new VertexShader(global_source+"\n"+format_context(module->vertex_context))); + program.attach_shader_owned(new VertexShader(head+format_context(module->vertex_context))); if(module->geometry_context.present) - program.attach_shader_owned(new GeometryShader(global_source+"\n"+format_context(module->geometry_context))); + program.attach_shader_owned(new GeometryShader(head+format_context(module->geometry_context))); if(module->fragment_context.present) - program.attach_shader_owned(new FragmentShader(global_source+"\n"+format_context(module->fragment_context))); + program.attach_shader_owned(new FragmentShader(head+format_context(module->fragment_context))); program.bind_attribute(VERTEX4, "vertex"); program.bind_attribute(NORMAL3, "normal"); @@ -45,6 +48,44 @@ void ProgramCompiler::add_shaders(Program &program) program.bind_attribute(TEXCOORD4, "texcoord"); } +void ProgramCompiler::process() +{ + if(module->vertex_context.present) + process(module->vertex_context); + if(module->geometry_context.present) + process(module->geometry_context); + if(module->fragment_context.present) + process(module->fragment_context); +} + +void ProgramCompiler::process(Context &context) +{ + inject_block(context.content, module->global_context.content); + + VariableResolver resolver; + context.content.visit(resolver); + + while(1) + { + UnusedVariableLocator unused_locator; + context.content.visit(unused_locator); + + NodeRemover remover; + remover.to_remove.insert(unused_locator.unused_variables.begin(), unused_locator.unused_variables.end()); + context.content.visit(remover); + + if(!remover.n_removed) + break; + } +} + +void ProgramCompiler::inject_block(Block &target, const Block &source) +{ + list >::iterator insert_point = target.body.begin(); + for(list >::const_iterator i=source.body.begin(); i!=source.body.end(); ++i) + target.body.insert(insert_point, (*i)->clone()); +} + string ProgramCompiler::format_context(Context &context) { Formatter formatter; @@ -135,7 +176,7 @@ void ProgramCompiler::Formatter::visit(Block &block) bool change_indent = (!formatted.empty() && !else_if); indent += change_indent; string spaces(indent*2, ' '); - for(vector >::iterator i=block.body.begin(); i!=block.body.end(); ++i) + for(list >::iterator i=block.body.begin(); i!=block.body.end(); ++i) { if(i!=block.body.begin()) formatted += '\n'; @@ -205,14 +246,13 @@ void ProgramCompiler::Formatter::visit(InterfaceBlock &iface) void ProgramCompiler::Formatter::visit(FunctionDeclaration &func) { formatted += format("%s %s(", func.return_type, func.name); - parameter_list = true; for(vector >::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i) { if(i!=func.parameters.begin()) formatted += ", "; + SetFlag set(parameter_list); (*i)->visit(*this); } - parameter_list = false; formatted += ')'; if(func.definition) { @@ -239,9 +279,8 @@ void ProgramCompiler::Formatter::visit(Conditional &cond) if(!cond.else_body.body.empty()) { formatted += format("\n%selse", string(indent*2, ' ')); - else_if = true; + SetFlag set(else_if); cond.else_body.visit(*this); - else_if = false; } } @@ -264,5 +303,157 @@ void ProgramCompiler::Formatter::visit(Return &ret) formatted += ';'; } + +ProgramCompiler::VariableResolver::VariableResolver(): + anonymous(false) +{ } + +void ProgramCompiler::VariableResolver::visit(Block &block) +{ + blocks.push_back(&block); + block.variables.clear(); + TraversingVisitor::visit(block); + blocks.pop_back(); +} + +void ProgramCompiler::VariableResolver::visit(VariableReference &var) +{ + var.declaration = 0; + type = 0; + for(vector::iterator i=blocks.end(); i!=blocks.begin(); ) + { + --i; + map::iterator j = (*i)->variables.find(var.name); + if(j!=(*i)->variables.end()) + { + var.declaration = j->second; + type = j->second->type_declaration; + break; + } + } +} + +void ProgramCompiler::VariableResolver::visit(MemberAccess &memacc) +{ + type = 0; + TraversingVisitor::visit(memacc); + memacc.declaration = 0; + if(type) + { + map::iterator i = type->members.variables.find(memacc.member); + if(i!=type->members.variables.end()) + { + memacc.declaration = i->second; + type = i->second->type_declaration; + } + else + type = 0; + } +} + +void ProgramCompiler::VariableResolver::visit(BinaryExpression &binary) +{ + if(binary.oper=="[") + { + binary.right->visit(*this); + type = 0; + binary.left->visit(*this); + } + else + { + TraversingVisitor::visit(binary); + type = 0; + } +} + +void ProgramCompiler::VariableResolver::visit(StructDeclaration &strct) +{ + TraversingVisitor::visit(strct); + blocks.back()->types[strct.name] = &strct; +} + +void ProgramCompiler::VariableResolver::visit(VariableDeclaration &var) +{ + for(vector::iterator i=blocks.end(); i!=blocks.begin(); ) + { + --i; + map::iterator j = (*i)->types.find(var.type); + if(j!=(*i)->types.end()) + var.type_declaration = j->second; + } + + TraversingVisitor::visit(var); + blocks.back()->variables[var.name] = &var; + if(anonymous && blocks.size()>1) + blocks[blocks.size()-2]->variables[var.name] = &var; +} + +void ProgramCompiler::VariableResolver::visit(InterfaceBlock &iface) +{ + SetFlag set(anonymous); + TraversingVisitor::visit(iface); +} + + +void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var) +{ + unused_variables.erase(var.declaration); +} + +void ProgramCompiler::UnusedVariableLocator::visit(MemberAccess &memacc) +{ + TraversingVisitor::visit(memacc); + unused_variables.erase(memacc.declaration); +} + +void ProgramCompiler::UnusedVariableLocator::visit(VariableDeclaration &var) +{ + unused_variables.insert(&var); + TraversingVisitor::visit(var); +} + + +ProgramCompiler::NodeRemover::NodeRemover(): + n_removed(0), + immutable_block(false), + remove_block(false) +{ } + +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 + { + remove_block = false; + (*i)->visit(*this); + remove = remove_block; + } + + if(remove) + block.body.erase(i++); + else + ++i; + + n_removed += remove; + } +} + +void ProgramCompiler::NodeRemover::visit(StructDeclaration &strct) +{ + SetFlag set(immutable_block); + TraversingVisitor::visit(strct); +} + +void ProgramCompiler::NodeRemover::visit(InterfaceBlock &iface) +{ + SetFlag set(immutable_block); + TraversingVisitor::visit(iface); +} + } // namespace GL } // namespace Msp