From: Mikko Rasa Date: Fri, 11 Nov 2016 13:59:38 +0000 (+0200) Subject: Rename ProgramSyntax::Context to Stage X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=fd103d76d7546f7e22aefc18c090a844fc67409f;p=libs%2Fgl.git Rename ProgramSyntax::Context to Stage --- 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; } diff --git a/source/programcompiler.h b/source/programcompiler.h index 6bac5386..ecae3dc1 100644 --- a/source/programcompiler.h +++ b/source/programcompiler.h @@ -61,7 +61,7 @@ private: struct InterfaceGenerator: ProgramSyntax::TraversingVisitor { - ProgramSyntax::Context *context; + ProgramSyntax::Stage *stage; std::string in_prefix; std::string out_prefix; unsigned scope_level; @@ -71,8 +71,8 @@ private: InterfaceGenerator(); - static std::string get_out_prefix(ProgramSyntax::ContextType); - void visit(ProgramSyntax::Context &); + static std::string get_out_prefix(ProgramSyntax::StageType); + void visit(ProgramSyntax::Stage &); virtual void visit(ProgramSyntax::Block &); std::string change_prefix(const std::string &, const std::string &) const; bool generate_interface(ProgramSyntax::VariableDeclaration &, const std::string &, const std::string &); @@ -90,7 +90,7 @@ private: struct UnusedVariableLocator: ProgramSyntax::TraversingVisitor { - ProgramSyntax::Context *context; + ProgramSyntax::Stage *stage; std::set unused_nodes; std::map assignments; bool assignment; @@ -98,7 +98,7 @@ private: UnusedVariableLocator(); - void visit(ProgramSyntax::Context &); + void visit(ProgramSyntax::Stage &); virtual void visit(ProgramSyntax::VariableReference &); virtual void visit(ProgramSyntax::MemberAccess &); virtual void visit(ProgramSyntax::BinaryExpression &); @@ -108,7 +108,7 @@ private: struct NodeRemover: ProgramSyntax::TraversingVisitor { - ProgramSyntax::Context *context; + ProgramSyntax::Stage *stage; std::set to_remove; unsigned n_removed; bool immutable_block; @@ -116,7 +116,7 @@ private: NodeRemover(); - void visit(ProgramSyntax::Context &); + void visit(ProgramSyntax::Stage &); virtual void visit(ProgramSyntax::Block &); virtual void visit(ProgramSyntax::StructDeclaration &); virtual void visit(ProgramSyntax::VariableDeclaration &); @@ -135,11 +135,11 @@ public: private: void process(); - void generate(ProgramSyntax::Context &); - void optimize(ProgramSyntax::Context &); + void generate(ProgramSyntax::Stage &); + void optimize(ProgramSyntax::Stage &); static void inject_block(ProgramSyntax::Block &, const ProgramSyntax::Block &); - static void resolve_variables(ProgramSyntax::Context &); - std::string format_context(ProgramSyntax::Context &); + static void resolve_variables(ProgramSyntax::Stage &); + std::string format_stage(ProgramSyntax::Stage &); }; } // namespace GL diff --git a/source/programparser.cpp b/source/programparser.cpp index 58649ec0..d1826ad6 100644 --- a/source/programparser.cpp +++ b/source/programparser.cpp @@ -81,30 +81,30 @@ Module &ProgramParser::parse(IO::Base &io) void ProgramParser::parse_source(Module &module) { cur_module = &module; - cur_context = &module.global_context; + cur_stage = &module.shared; iter = source.begin(); while(1) { while(Node *statement = parse_global_declaration()) - cur_context->content.body.push_back(statement); - cur_context->present = !cur_context->content.body.empty(); + cur_stage->content.body.push_back(statement); + cur_stage->present = !cur_stage->content.body.empty(); - Context *prev_context = cur_context; + Stage *prev_stage = cur_stage; parse_token(); string token = parse_token(); if(token.empty()) break; else if(token=="global") - cur_context = &module.global_context; + cur_stage = &module.shared; else if(token=="vertex") - cur_context = &module.vertex_context; + cur_stage = &module.vertex_stage; else if(token=="geometry") - cur_context = &module.geometry_context; + cur_stage = &module.geometry_stage; else if(token=="fragment") - cur_context = &module.fragment_context; + cur_stage = &module.fragment_stage; else - throw runtime_error(format("Parse error at '%s': expected context identifier", token)); - cur_context->previous = prev_context; + throw runtime_error(format("Parse error at '%s': expected stage identifier", token)); + cur_stage->previous = prev_stage; for(; (iter!=source.end() && *iter!='\n'); ++iter) ; } @@ -701,7 +701,7 @@ Passthrough *ProgramParser::parse_passthrough() { expect("passthrough"); RefPtr pass = new Passthrough; - if(cur_context->type==GEOMETRY) + if(cur_stage->type==GEOMETRY) { expect("["); pass->subscript = parse_expression(); diff --git a/source/programparser.h b/source/programparser.h index 40fdcd89..1ca1a44c 100644 --- a/source/programparser.h +++ b/source/programparser.h @@ -41,7 +41,7 @@ private: std::deque next_tokens; ProgramSyntax::Module main_module; ProgramSyntax::Module *cur_module; - ProgramSyntax::Context *cur_context; + ProgramSyntax::Stage *cur_stage; std::set declared_types; static Operator operators[]; diff --git a/source/programsyntax.cpp b/source/programsyntax.cpp index 299e23b4..7ca71d57 100644 --- a/source/programsyntax.cpp +++ b/source/programsyntax.cpp @@ -248,7 +248,7 @@ void TraversingVisitor::visit(Return &ret) } -Context::Context(ContextType t): +Stage::Stage(StageType t): type(t), present(false), previous(0) @@ -256,10 +256,10 @@ Context::Context(ContextType t): Module::Module(): - global_context(GLOBAL), - vertex_context(VERTEX), - geometry_context(GEOMETRY), - fragment_context(FRAGMENT) + shared(SHARED), + vertex_stage(VERTEX), + geometry_stage(GEOMETRY), + fragment_stage(FRAGMENT) { } } // namespace ProgramSyntax diff --git a/source/programsyntax.h b/source/programsyntax.h index c8b0e1f7..86cb6b90 100644 --- a/source/programsyntax.h +++ b/source/programsyntax.h @@ -301,32 +301,32 @@ struct TraversingVisitor: NodeVisitor virtual void visit(Return &); }; -enum ContextType +enum StageType { - GLOBAL, + SHARED, VERTEX, GEOMETRY, FRAGMENT }; -struct Context +struct Stage { - ContextType type; + StageType type; bool present; - Context *previous; + Stage *previous; ProgramSyntax::Block content; std::map in_variables; std::map out_variables; - Context(ContextType); + Stage(StageType); }; struct Module { - Context global_context; - Context vertex_context; - Context geometry_context; - Context fragment_context; + Stage shared; + Stage vertex_stage; + Stage geometry_stage; + Stage fragment_stage; Module(); };