X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fprogramcompiler.cpp;h=d021ab4f75732769855e9370fe0c8eec990aca20;hp=5027dc9b79788783c0e2cc5860fa91c39b232ef2;hb=bec07999d95b76f4b47cffcc564d0cd0afc0435e;hpb=8468279a03d906f79fb6158162c7970d4d7fff60 diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index 5027dc9b..d021ab4f 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -4,7 +4,9 @@ #include #include #include +#include #include +#include #include #include "error.h" #include "program.h" @@ -57,8 +59,8 @@ void ProgramCompiler::compile(const string &source, const string &src_name) delete module; module = new Module(); ProgramParser parser; - imported_names.insert(src_name); - append_module(parser.parse(source, src_name)); + imported_names.push_back(src_name); + append_module(parser.parse(source, src_name, 1)); process(); } @@ -68,8 +70,8 @@ void ProgramCompiler::compile(IO::Base &io, Resources *res, const string &src_na delete module; module = new Module(); ProgramParser parser; - imported_names.insert(src_name); - append_module(parser.parse(io, src_name)); + imported_names.push_back(src_name); + append_module(parser.parse(io, src_name, 1)); process(); } @@ -83,25 +85,64 @@ void ProgramCompiler::add_shaders(Program &program) if(!module) throw invalid_operation("ProgramCompiler::add_shaders"); - for(list::iterator i=module->stages.begin(); i!=module->stages.end(); ++i) + try { - if(i->type==VERTEX) + for(list::iterator i=module->stages.begin(); i!=module->stages.end(); ++i) { - program.attach_shader_owned(new VertexShader(apply(*i))); - for(map::iterator j=i->locations.begin(); j!=i->locations.end(); ++j) - program.bind_attribute(j->second, j->first); + if(i->type==VERTEX) + { + program.attach_shader_owned(new VertexShader(apply(*i))); + for(map::iterator j=i->locations.begin(); j!=i->locations.end(); ++j) + program.bind_attribute(j->second, j->first); + } + else if(i->type==GEOMETRY) + program.attach_shader_owned(new GeometryShader(apply(*i))); + else if(i->type==FRAGMENT) + { + program.attach_shader_owned(new FragmentShader(apply(*i))); + if(EXT_gpu_shader4) + { + for(map::iterator j=i->locations.begin(); j!=i->locations.end(); ++j) + program.bind_fragment_data(j->second, j->first); + } + } } - else if(i->type==GEOMETRY) - program.attach_shader_owned(new GeometryShader(apply(*i))); - else if(i->type==FRAGMENT) + } + catch(const compile_error &e) + { + static const Regex r_message("^(([0-9]+)\\(([0-9]+)\\) :|ERROR: ([0-9]+):([0-9]+):) (.*)$"); + vector lines = split(e.what(), '\n'); + string translated; + for(vector::const_iterator i=lines.begin(); i!=lines.end(); ++i) { - program.attach_shader_owned(new FragmentShader(apply(*i))); - if(EXT_gpu_shader4) + RegMatch m = r_message.match(*i); + if(m) { - for(map::iterator j=i->locations.begin(); j!=i->locations.end(); ++j) - program.bind_fragment_data(j->second, j->first); + unsigned index = 0; + unsigned line = 0; + if(m[2]) + { + index = lexical_cast(m[2].str); + line = lexical_cast(m[3].str); + } + else if(m[4]) + { + index = lexical_cast(m[4].str); + line = lexical_cast(m[5].str); + } + const char *src = ""; + if(index==0) + src = ""; + else if(index-1target->required_version) + target->required_version = stage.required_version; for(NodeList::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i) target->content.body.push_back(*i); apply(*target); @@ -191,19 +234,21 @@ void ProgramCompiler::process() void ProgramCompiler::import(const string &name) { string fn = name+".glsl"; - if(imported_names.count(fn)) + if(find(imported_names, fn)!=imported_names.end()) return; - imported_names.insert(fn); + imported_names.push_back(fn); RefPtr io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn)); if(!io) throw runtime_error(format("module %s not found", name)); ProgramParser import_parser; - append_module(import_parser.parse(*io, fn)); + append_module(import_parser.parse(*io, fn, imported_names.size())); } void ProgramCompiler::generate(Stage &stage) { + if(module->shared.required_version>stage.required_version) + stage.required_version = module->shared.required_version; inject_block(stage.content, module->shared.content); apply(stage); @@ -307,9 +352,10 @@ void ProgramCompiler::BlockModifier::visit(Block &block) ProgramCompiler::Formatter::Formatter(): + source_index(0), + source_line(1), indent(0), - parameter_list(false), - else_if(0) + parameter_list(false) { } void ProgramCompiler::Formatter::apply(ProgramSyntax::Stage &s) @@ -319,145 +365,175 @@ void ProgramCompiler::Formatter::apply(ProgramSyntax::Stage &s) if(ver) { - formatted += format("#version %d%02d", ver.major, ver.minor); + append(format("#version %d%02d", ver.major, ver.minor)); if(api==OPENGL_ES2 && ver>=Version(3, 0)) - formatted += " es"; + append(" es"); formatted += '\n'; } for(vector::const_iterator i=s.required_extensions.begin(); i!=s.required_extensions.end(); ++i) - formatted += format("#extension %s: require\n", (*i)->get_name()); + append(format("#extension %s: require\n", (*i)->get_name())); if(!s.required_extensions.empty()) formatted += '\n'; Visitor::apply(s); } +void ProgramCompiler::Formatter::append(const string &text) +{ + formatted += text; + for(string::const_iterator i=text.begin(); i!=text.end(); ++i) + if(*i=='\n') + ++source_line; +} + +void ProgramCompiler::Formatter::append(char c) +{ + formatted += c; + if(c=='\n') + ++source_line; +} + +void ProgramCompiler::Formatter::set_source(unsigned index, unsigned line) +{ + if(index!=source_index || (index && line!=source_line)) + { + if(index==source_index && line==source_line+1) + formatted += '\n'; + else + { + unsigned l = line; + if(stage->required_versionvisit(*this); - formatted += ')'; + append(')'); } void ProgramCompiler::Formatter::visit(VariableReference &var) { - formatted += var.name; + append(var.name); } void ProgramCompiler::Formatter::visit(MemberAccess &memacc) { memacc.left->visit(*this); - formatted += format(".%s", memacc.member); + append(format(".%s", memacc.member)); } void ProgramCompiler::Formatter::visit(UnaryExpression &unary) { if(unary.prefix) - formatted += unary.oper; + append(unary.oper); unary.expression->visit(*this); if(!unary.prefix) - formatted += unary.oper; + append(unary.oper); } void ProgramCompiler::Formatter::visit(BinaryExpression &binary) { binary.left->visit(*this); - formatted += binary.oper; + append(binary.oper); binary.right->visit(*this); - formatted += binary.after; + append(binary.after); } void ProgramCompiler::Formatter::visit(Assignment &assign) { assign.left->visit(*this); - formatted += format(" %s ", assign.oper); + append(format(" %s ", assign.oper)); assign.right->visit(*this); } void ProgramCompiler::Formatter::visit(FunctionCall &call) { - formatted += format("%s(", call.name); + append(format("%s(", call.name)); for(NodeArray::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i) { if(i!=call.arguments.begin()) - formatted += ", "; + append(", "); (*i)->visit(*this); } - formatted += ')'; + append(')'); } void ProgramCompiler::Formatter::visit(ExpressionStatement &expr) { expr.expression->visit(*this); - formatted += ';'; + append(';'); } void ProgramCompiler::Formatter::visit(Block &block) { - 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, ' ')); + append(format("%s{\n", string(brace_indent*2, ' '))); SetForScope set(indent, indent+(indent>0 || use_braces)); string spaces(indent*2, ' '); for(NodeList::iterator i=block.body.begin(); i!=block.body.end(); ++i) { if(i!=block.body.begin()) - formatted += '\n'; - formatted += spaces; + append('\n'); + set_source((*i)->source, (*i)->line); + append(spaces); (*i)->visit(*this); - else_if = 0; } if(use_braces) - formatted += format("\n%s}", string(brace_indent*2, ' ')); + append(format("\n%s}", string(brace_indent*2, ' '))); } void ProgramCompiler::Formatter::visit(Import &import) { - formatted += format("import %s;", import.module); + append(format("import %s;", import.module)); } void ProgramCompiler::Formatter::visit(Precision &prec) { - formatted += format("precision %s %s;", prec.precision, prec.type); + append(format("precision %s %s;", prec.precision, prec.type)); } void ProgramCompiler::Formatter::visit(Layout &layout) { - formatted += "layout("; + append("layout("); for(vector::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i) { if(i!=layout.qualifiers.begin()) - formatted += ", "; - formatted += i->identifier; + append(", "); + append(i->identifier); if(!i->value.empty()) - formatted += format("=%s", i->value); + append(format("=%s", i->value)); } - formatted += ')'; + append(')'); } void ProgramCompiler::Formatter::visit(InterfaceLayout &layout) { layout.layout.visit(*this); - formatted += format(" %s;", layout.interface); + append(format(" %s;", layout.interface)); } void ProgramCompiler::Formatter::visit(StructDeclaration &strct) { - formatted += format("struct %s\n", strct.name); + append(format("struct %s\n", strct.name)); strct.members.visit(*this); - formatted += ';'; + append(';'); } void ProgramCompiler::Formatter::visit(VariableDeclaration &var) @@ -465,14 +541,14 @@ void ProgramCompiler::Formatter::visit(VariableDeclaration &var) if(var.layout) { var.layout->visit(*this); - formatted += ' '; + append(' '); } if(var.constant) - formatted += "const "; + append("const "); if(!var.interpolation.empty()) - formatted += format("%s ", var.interpolation); + append(format("%s ", var.interpolation)); if(!var.sampling.empty()) - formatted += format("%s ", var.sampling); + append(format("%s ", var.sampling)); if(!var.interface.empty() && var.interface!=block_interface) { string interface = var.interface; @@ -483,72 +559,77 @@ void ProgramCompiler::Formatter::visit(VariableDeclaration &var) else if((stage->type==VERTEX && var.interface=="out") || (stage->type==FRAGMENT && var.interface=="in")) interface = "varying"; } - formatted += format("%s ", interface); + append(format("%s ", interface)); } if(!var.precision.empty()) - formatted += format("%s ", var.precision); - formatted += format("%s %s", var.type, var.name); + append(format("%s ", var.precision)); + append(format("%s %s", var.type, var.name)); if(var.array) { - formatted += '['; + append('['); if(var.array_size) var.array_size->visit(*this); - formatted += ']'; + append(']'); } if(var.init_expression) { - formatted += " = "; + append(" = "); var.init_expression->visit(*this); } if(!parameter_list) - formatted += ';'; + append(';'); } void ProgramCompiler::Formatter::visit(InterfaceBlock &iface) { SetForScope set(block_interface, iface.interface); - formatted += format("%s %s\n", iface.interface, iface.name); + append(format("%s %s\n", iface.interface, iface.name)); iface.members.visit(*this); - formatted += ';'; + append(';'); } void ProgramCompiler::Formatter::visit(FunctionDeclaration &func) { - formatted += format("%s %s(", func.return_type, func.name); + append(format("%s %s(", func.return_type, func.name)); for(NodeArray::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i) { if(i!=func.parameters.begin()) - formatted += ", "; + append(", "); SetFlag set(parameter_list); (*i)->visit(*this); } - formatted += ')'; + append(')'); if(func.definition==&func) { - formatted += '\n'; + append('\n'); func.body.visit(*this); } else - formatted += ';'; + append(';'); } void ProgramCompiler::Formatter::visit(Conditional &cond) { - if(else_if) - formatted.replace(formatted.rfind('\n'), string::npos, 1, ' '); - - indent -= else_if; - - formatted += "if("; + append("if("); cond.condition->visit(*this); - formatted += ")\n"; + append(")\n"); cond.body.visit(*this); if(!cond.else_body.body.empty()) { - formatted += format("\n%selse\n", string(indent*2, ' ')); - SetForScope set(else_if, 2); - cond.else_body.visit(*this); + Conditional *else_cond = dynamic_cast(cond.else_body.body.front().get()); + if(cond.else_body.body.size()==1 && else_cond) + { + append('\n'); + set_source(else_cond->source, else_cond->line); + append(format("%selse ", string(indent*2, ' '))); + else_cond->visit(*this); + } + else + { + append(format("\n%selse\n", string(indent*2, ' '))); + cond.else_body.visit(*this); + } } } @@ -556,55 +637,55 @@ void ProgramCompiler::Formatter::visit(Iteration &iter) { if(!iter.init_statement && iter.condition && !iter.loop_expression) { - formatted += "while("; + append("while("); iter.condition->visit(*this); - formatted += ')'; + append(')'); } else { - formatted += "for("; + append("for("); if(iter.init_statement) iter.init_statement->visit(*this); else - formatted += ';'; + append(';'); if(iter.condition) { - formatted += ' '; + append(' '); iter.condition->visit(*this); } - formatted += ';'; + append(';'); if(iter.loop_expression) { - formatted += ' '; + append(' '); iter.loop_expression->visit(*this); } - formatted += ')'; + append(')'); } if(iter.body.body.empty()) - formatted += " { }"; + append(" { }"); else { - formatted += '\n'; + append('\n'); iter.body.visit(*this); } } void ProgramCompiler::Formatter::visit(Return &ret) { - formatted += "return"; + append("return"); if(ret.expression) { - formatted += ' '; + append(' '); ret.expression->visit(*this); } - formatted += ';'; + append(';'); } void ProgramCompiler::Formatter::visit(Jump &jump) { - formatted += jump.keyword; - formatted += ';'; + append(jump.keyword); + append(';'); } @@ -914,7 +995,7 @@ bool ProgramCompiler::InterfaceGenerator::generate_interface(VariableDeclaration return true; } -void ProgramCompiler::InterfaceGenerator::insert_assignment(const string &left, ProgramSyntax::Expression *right) +ExpressionStatement &ProgramCompiler::InterfaceGenerator::insert_assignment(const string &left, ProgramSyntax::Expression *right) { Assignment *assign = new Assignment; VariableReference *ref = new VariableReference; @@ -927,6 +1008,8 @@ void ProgramCompiler::InterfaceGenerator::insert_assignment(const string &left, stmt->expression = assign; stmt->visit(*this); insert_nodes.push_back(stmt); + + return *stmt; } void ProgramCompiler::InterfaceGenerator::visit(VariableReference &var) @@ -958,7 +1041,9 @@ void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var) remove_node = true; if(var.init_expression) { - insert_assignment(var.name, var.init_expression->clone()); + ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone()); + stmt.source = var.source; + stmt.line = var.line; return; } } @@ -1596,7 +1681,7 @@ void ProgramCompiler::UnusedVariableLocator::merge_down_variables() { if(!i->second.referenced) unused_nodes.insert(i->first); - clear_assignments(i->second, true); + clear_assignments(i->second, i->first->interface!="out"); continue; } @@ -1869,10 +1954,32 @@ void ProgramCompiler::LegacyConverter::visit(FunctionCall &call) call.name = "texture2D"; else if(type=="sampler3D") call.name = "texture3D"; + else if(type=="samplerCube") + call.name = "textureCube"; else if(type=="sampler1DShadow") call.name = "shadow1D"; else if(type=="sampler2DShadow") call.name = "shadow2D"; + else if(type=="sampler1DArray") + { + check_extension(EXT_texture_array); + call.name = "texture1DArray"; + } + else if(type=="sampler2DArray") + { + check_extension(EXT_texture_array); + call.name = "texture2DArray"; + } + else if(type=="sampler1DArrayShadow") + { + check_extension(EXT_texture_array); + call.name = "shadow1DArray"; + } + else if(type=="sampler2DArrayShadow") + { + check_extension(EXT_texture_array); + call.name = "shadow2DArray"; + } for(; i!=call.arguments.end(); ++i) (*i)->visit(*this);