for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
{
if(i->type==VERTEX)
- program.attach_shader_owned(new VertexShader(head+create_source(*i)));
+ program.attach_shader_owned(new VertexShader(head+apply<Formatter>(*i)));
else if(i->type==GEOMETRY)
- program.attach_shader_owned(new GeometryShader(head+create_source(*i)));
+ program.attach_shader_owned(new GeometryShader(head+apply<Formatter>(*i)));
else if(i->type==FRAGMENT)
- program.attach_shader_owned(new FragmentShader(head+create_source(*i)));
+ program.attach_shader_owned(new FragmentShader(head+apply<Formatter>(*i)));
}
program.bind_attribute(VERTEX4, "vertex");
bool ProgramCompiler::optimize(Stage &stage)
{
- UnusedVariableLocator unused_locator;
- unused_locator.apply(stage);
+ set<Node *> unused = apply<UnusedVariableLocator>(stage);
+ apply<NodeRemover>(stage, unused);
- NodeRemover remover;
- remover.to_remove = unused_locator.unused_nodes;
- remover.apply(stage);
-
- return !unused_locator.unused_nodes.empty();
+ return !unused.empty();
}
void ProgramCompiler::inject_block(Block &target, const Block &source)
}
template<typename T>
-void ProgramCompiler::apply(Stage &stage)
+typename T::ResultType ProgramCompiler::apply(Stage &stage)
{
T visitor;
visitor.apply(stage);
+ return visitor.get_result();
}
-string ProgramCompiler::create_source(Stage &stage)
+template<typename T, typename A>
+typename T::ResultType ProgramCompiler::apply(Stage &stage, const A &arg)
{
- Formatter formatter;
- formatter.apply(stage);
- return formatter.formatted;
+ T visitor(arg);
+ visitor.apply(stage);
+ return visitor.get_result();
}
}
+ProgramCompiler::NodeRemover::NodeRemover(const set<Node *> &r):
+ to_remove(r)
+{ }
+
void ProgramCompiler::NodeRemover::visit(Block &block)
{
for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
private:
struct Visitor: ProgramSyntax::TraversingVisitor
{
+ typedef void ResultType;
+
ProgramSyntax::Stage *stage;
Visitor();
virtual void apply(ProgramSyntax::Stage &);
+ void get_result() const { }
};
struct Formatter: Visitor
{
+ typedef std::string ResultType;
+
std::string formatted;
unsigned indent;
bool parameter_list;
Formatter();
+ const std::string &get_result() const { return formatted; }
virtual void visit(ProgramSyntax::Block &);
virtual void visit(ProgramSyntax::Literal &);
virtual void visit(ProgramSyntax::ParenthesizedExpression &);
bool self_referencing;
};
+ typedef std::set<ProgramSyntax::Node *> ResultType;
typedef std::map<ProgramSyntax::VariableDeclaration *, AssignmentList> BlockAssignmentMap;
std::set<ProgramSyntax::Node *> unused_nodes;
UnusedVariableLocator();
virtual void apply(ProgramSyntax::Stage &);
+ const ResultType &get_result() const { return unused_nodes; }
virtual void visit(ProgramSyntax::VariableReference &);
virtual void visit(ProgramSyntax::MemberAccess &);
virtual void visit(ProgramSyntax::BinaryExpression &);
{
std::set<ProgramSyntax::Node *> to_remove;
+ NodeRemover() { }
+ NodeRemover(const std::set<ProgramSyntax::Node *> &);
+
virtual void visit(ProgramSyntax::Block &);
virtual void visit(ProgramSyntax::VariableDeclaration &);
};
bool optimize(ProgramSyntax::Stage &);
static void inject_block(ProgramSyntax::Block &, const ProgramSyntax::Block &);
template<typename T>
- static void apply(ProgramSyntax::Stage &);
- std::string create_source(ProgramSyntax::Stage &);
+ static typename T::ResultType apply(ProgramSyntax::Stage &);
+ template<typename T, typename A>
+ static typename T::ResultType apply(ProgramSyntax::Stage &, const A &);
};
} // namespace GL