X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fprogramcompiler.cpp;h=2dae07c8f4d3ecc049c153e66de8793d347e3134;hp=ecdcd519f69c1cdb4410ee377b0073e9772028ea;hb=fd103d76d7546f7e22aefc18c090a844fc67409f;hpb=009918a51aa309eeceb6c02ecaebe45e8d357033 diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index ecdcd519..2dae07c8 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -35,12 +35,12 @@ void ProgramCompiler::add_shaders(Program &program) throw invalid_operation("ProgramCompiler::add_shaders"); string head = "#version 150\n"; - if(module->vertex_context.present) - program.attach_shader_owned(new VertexShader(head+format_context(module->vertex_context))); - if(module->geometry_context.present) - program.attach_shader_owned(new GeometryShader(head+format_context(module->geometry_context))); - if(module->fragment_context.present) - program.attach_shader_owned(new FragmentShader(head+format_context(module->fragment_context))); + 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))); program.bind_attribute(VERTEX4, "vertex"); program.bind_attribute(NORMAL3, "normal"); @@ -50,46 +50,46 @@ void ProgramCompiler::add_shaders(Program &program) void ProgramCompiler::process() { - if(module->vertex_context.present) - generate(module->vertex_context); - if(module->geometry_context.present) - generate(module->geometry_context); - if(module->fragment_context.present) - generate(module->fragment_context); + 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_context.present) - optimize(module->vertex_context); - if(module->geometry_context.present) - optimize(module->geometry_context); - if(module->fragment_context.present) - optimize(module->fragment_context); + 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); } -void ProgramCompiler::generate(Context &context) +void ProgramCompiler::generate(Stage &stage) { - inject_block(context.content, module->global_context.content); + inject_block(stage.content, module->shared.content); - resolve_variables(context); + resolve_variables(stage); InterfaceGenerator generator; - generator.visit(context); + generator.visit(stage); - resolve_variables(context); + resolve_variables(stage); VariableRenamer renamer; - context.content.visit(renamer); + stage.content.visit(renamer); } -void ProgramCompiler::optimize(Context &context) +void ProgramCompiler::optimize(Stage &stage) { while(1) { UnusedVariableLocator unused_locator; - unused_locator.visit(context); + unused_locator.visit(stage); NodeRemover remover; remover.to_remove = unused_locator.unused_nodes; - remover.visit(context); + remover.visit(stage); if(!remover.n_removed) break; @@ -103,16 +103,16 @@ void ProgramCompiler::inject_block(Block &target, const Block &source) target.body.insert(insert_point, (*i)->clone()); } -void ProgramCompiler::resolve_variables(Context &context) +void ProgramCompiler::resolve_variables(Stage &stage) { VariableResolver resolver; - context.content.visit(resolver); + stage.content.visit(resolver); } -string ProgramCompiler::format_context(Context &context) +string ProgramCompiler::format_stage(Stage &stage) { Formatter formatter; - context.content.visit(formatter); + stage.content.visit(formatter); return formatter.formatted; } @@ -419,12 +419,12 @@ void ProgramCompiler::VariableResolver::visit(InterfaceBlock &iface) ProgramCompiler::InterfaceGenerator::InterfaceGenerator(): - context(0), + stage(0), scope_level(0), remove_node(false) { } -string ProgramCompiler::InterfaceGenerator::get_out_prefix(ContextType type) +string ProgramCompiler::InterfaceGenerator::get_out_prefix(StageType type) { if(type==VERTEX) return "_vs_out_"; @@ -434,13 +434,13 @@ string ProgramCompiler::InterfaceGenerator::get_out_prefix(ContextType type) return string(); } -void ProgramCompiler::InterfaceGenerator::visit(Context &ctx) +void ProgramCompiler::InterfaceGenerator::visit(Stage &s) { - 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); + SetForScope set(stage, &s); + if(stage->previous) + in_prefix = get_out_prefix(stage->previous->type); + out_prefix = get_out_prefix(stage->type); + stage->content.visit(*this); } void ProgramCompiler::InterfaceGenerator::visit(Block &block) @@ -480,8 +480,8 @@ string ProgramCompiler::InterfaceGenerator::change_prefix(const string &name, co 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)) + 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; @@ -490,7 +490,7 @@ bool ProgramCompiler::InterfaceGenerator::generate_interface(VariableDeclaration 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 = (out.array || (stage->type==GEOMETRY && iface=="in")); iface_var->array_size = out.array_size; if(iface=="in") iface_var->linked_declaration = &out; @@ -516,12 +516,12 @@ void ProgramCompiler::InterfaceGenerator::insert_assignment(const string &left, void ProgramCompiler::InterfaceGenerator::visit(VariableReference &var) { - if(var.declaration || !context->previous) + if(var.declaration || !stage->previous) return; if(iface_declarations.count(var.name)) return; - const map &prev_out = context->previous->out_variables; + const map &prev_out = stage->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); @@ -534,7 +534,7 @@ void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var) if(var.interface=="out") { if(scope_level==1) - context->out_variables[var.name] = &var; + stage->out_variables[var.name] = &var; else if(generate_interface(var, "out", change_prefix(var.name, string()))) { remove_node = true; @@ -544,12 +544,12 @@ void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var) } else if(var.interface=="in") { - context->in_variables[var.name] = &var; + stage->in_variables[var.name] = &var; if(var.linked_declaration) var.linked_declaration->linked_declaration = &var; - else if(context->previous) + else if(stage->previous) { - const map &prev_out = context->previous->out_variables; + const map &prev_out = stage->previous->out_variables; map::const_iterator i = prev_out.find(var.name); if(i!=prev_out.end()) { @@ -564,9 +564,9 @@ void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var) void ProgramCompiler::InterfaceGenerator::visit(Passthrough &pass) { - if(context->previous) + if(stage->previous) { - const map &prev_out = context->previous->out_variables; + 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); @@ -608,15 +608,15 @@ void ProgramCompiler::VariableRenamer::visit(VariableDeclaration &var) ProgramCompiler::UnusedVariableLocator::UnusedVariableLocator(): - context(0), + stage(0), assignment(false), assignment_target(0) { } -void ProgramCompiler::UnusedVariableLocator::visit(Context &ctx) +void ProgramCompiler::UnusedVariableLocator::visit(Stage &s) { - context = &ctx; - ctx.content.visit(*this); + stage = &s; + stage->content.visit(*this); } void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var) @@ -657,7 +657,7 @@ void ProgramCompiler::UnusedVariableLocator::visit(ExpressionStatement &expr) TraversingVisitor::visit(expr); if(assignment && assignment_target) { - if(assignment_target->interface!="out" || (context->type!=FRAGMENT && !assignment_target->linked_declaration)) + if(assignment_target->interface!="out" || (stage->type!=FRAGMENT && !assignment_target->linked_declaration)) { unused_nodes.insert(&expr); assignments[assignment_target] = &expr; @@ -676,16 +676,16 @@ void ProgramCompiler::UnusedVariableLocator::visit(VariableDeclaration &var) ProgramCompiler::NodeRemover::NodeRemover(): - context(0), + stage(0), n_removed(0), immutable_block(false), remove_block(false) { } -void ProgramCompiler::NodeRemover::visit(Context &ctx) +void ProgramCompiler::NodeRemover::visit(Stage &s) { - context = &ctx; - ctx.content.visit(*this); + stage = &s; + stage->content.visit(*this); } void ProgramCompiler::NodeRemover::visit(Block &block) @@ -718,8 +718,8 @@ void ProgramCompiler::NodeRemover::visit(VariableDeclaration &var) { if(to_remove.count(&var)) { - context->in_variables.erase(var.name); - context->out_variables.erase(var.name); + stage->in_variables.erase(var.name); + stage->out_variables.erase(var.name); if(var.linked_declaration) var.linked_declaration->linked_declaration = 0; }