X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fprogramcompiler.cpp;h=f30ccfa6233ad6b6dc8115488210c7f1dbd4c7a5;hb=refs%2Fheads%2Fmaster;hp=b08f1737e4d542610d1a9401f37ac3df90e580cd;hpb=6e6ee01b68056b23c6709d7f60396710dd7623b9;p=libs%2Fgl.git diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp deleted file mode 100644 index b08f1737..00000000 --- a/source/programcompiler.cpp +++ /dev/null @@ -1,199 +0,0 @@ -#include -#include -#include "error.h" -#include "program.h" -#include "programcompiler.h" -#include "shader.h" - -using namespace std; - -namespace Msp { -namespace GL { - -using namespace ProgramSyntax; - -ProgramCompiler::ProgramCompiler(): - module(0) -{ } - -void ProgramCompiler::compile(const string &source) -{ - module = &parser.parse(source); -} - -void ProgramCompiler::compile(IO::Base &io) -{ - module = &parser.parse(io); -} - -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); - if(module->vertex_context.present) - program.attach_shader_owned(new VertexShader(global_source+"\n"+format_context(module->vertex_context))); - if(module->geometry_context.present) - program.attach_shader_owned(new GeometryShader(global_source+"\n"+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.bind_attribute(VERTEX4, "vertex"); - program.bind_attribute(NORMAL3, "normal"); - program.bind_attribute(COLOR4_FLOAT, "color"); - program.bind_attribute(TEXCOORD4, "texcoord"); -} - -string ProgramCompiler::format_context(Context &context) -{ - Formatter formatter; - context.content.visit(formatter); - return formatter.formatted; -} - - -ProgramCompiler::Formatter::Formatter(): - indent(0), - parameter_list(false), - else_if(false) -{ } - -string ProgramCompiler::Formatter::format_expression(Expression &expr) -{ - return join(expr.tokens.begin(), expr.tokens.end(), string()); -} - -void ProgramCompiler::Formatter::visit(ExpressionStatement &expr) -{ - formatted += format("%s;", format_expression(expr.expression)); -} - -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, ' ')); - } - - bool change_indent = (!formatted.empty() && !else_if); - indent += change_indent; - string spaces(indent*2, ' '); - for(vector::const_iterator i=block.body.begin(); i!=block.body.end(); ++i) - { - if(i!=block.body.begin()) - formatted += '\n'; - if(!else_if) - formatted += spaces; - (*i)->visit(*this); - } - indent -= change_indent; - - if(block.use_braces) - formatted += format("\n%s}", string(indent*2, ' ')); -} - -void ProgramCompiler::Formatter::visit(Layout &layout) -{ - formatted += "layout("; - for(vector::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i) - { - if(i!=layout.qualifiers.begin()) - formatted += ", "; - formatted += i->identifier; - if(!i->value.empty()) - formatted += format("=%s", i->value); - } - formatted += format(") %s;", layout.interface); -} - -void ProgramCompiler::Formatter::visit(StructDeclaration &strct) -{ - formatted += format("struct %s\n", strct.name); - strct.members.visit(*this); - formatted += ';'; -} - -void ProgramCompiler::Formatter::visit(VariableDeclaration &var) -{ - if(var.constant) - formatted += "const "; - if(!var.sampling.empty()) - formatted += format("%s ", var.sampling); - if(!var.interface.empty()) - formatted += format("%s ", var.interface); - formatted += format("%s %s", var.type, var.name); - if(var.array) - formatted += format("[%s]", format_expression(var.array_size)); - if(!var.init_expression.empty()) - formatted += format(" = %s", format_expression(var.init_expression)); - if(!parameter_list) - formatted += ';'; -} - -void ProgramCompiler::Formatter::visit(InterfaceBlock &iface) -{ - formatted += format("%s %s\n", iface.interface, iface.name); - iface.members.visit(*this); - formatted += ';'; -} - -void ProgramCompiler::Formatter::visit(FunctionDeclaration &func) -{ - formatted += format("%s %s(", func.return_type, func.name); - parameter_list = true; - for(vector::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i) - { - if(i!=func.parameters.begin()) - formatted += ", "; - (*i)->visit(*this); - } - parameter_list = false; - formatted += ')'; - if(func.definition) - { - formatted += '\n'; - func.body.visit(*this); - } - else - formatted += ';'; -} - -void ProgramCompiler::Formatter::visit(Conditional &cond) -{ - if(else_if) - { - formatted += ' '; - else_if = false; - } - formatted += format("if(%s)\n", format_expression(cond.condition)); - cond.body.visit(*this); - if(!cond.else_body.body.empty()) - { - formatted += format("\n%selse", string(indent*2, ' ')); - else_if = true; - cond.else_body.visit(*this); - else_if = false; - } -} - -void ProgramCompiler::Formatter::visit(Iteration &iter) -{ - formatted += "for("; - iter.init_statement->visit(*this); - formatted += format(" %s; %s)\n", format_expression(iter.condition), format_expression(iter.loop_expression)); - iter.body.visit(*this); -} - -void ProgramCompiler::Formatter::visit(Return &ret) -{ - formatted += format("return %s;", format_expression(ret.expression)); -} - -} // namespace GL -} // namespace Msp