#include "generate.h"
#include "optimize.h"
#include "output.h"
+#include "resources.h"
#include "shader.h"
#undef interface
namespace SL {
Compiler::Compiler():
- resources(0),
module(0)
{ }
delete module;
}
-void Compiler::compile(const string &source, const string &src_name)
+void Compiler::clear()
{
- resources = 0;
delete module;
module = new Module();
+ imported_names.clear();
+}
+
+void Compiler::set_source(const string &source, const string &src_name)
+{
+ clear();
Parser parser;
imported_names.push_back(src_name);
- append_module(parser.parse(source, src_name, 1));
- process();
+ append_module(parser.parse(source, src_name, 1), 0);
}
-void Compiler::compile(IO::Base &io, Resources *res, const string &src_name)
+void Compiler::load_source(IO::Base &io, DataFile::Collection *res, const string &src_name)
{
- resources = res;
- delete module;
- module = new Module();
+ clear();
Parser parser;
imported_names.push_back(src_name);
- append_module(parser.parse(io, src_name, 1));
- process();
+ append_module(parser.parse(io, src_name, 1), res);
}
-void Compiler::compile(IO::Base &io, const string &src_name)
+void Compiler::load_source(IO::Base &io, const string &src_name)
{
- compile(io, 0, src_name);
+ load_source(io, 0, src_name);
+}
+
+void Compiler::compile()
+{
+ for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+ generate(*i);
+ for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
+ {
+ if(optimize(*i))
+ i = module->stages.begin();
+ else
+ ++i;
+ }
+ for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+ finalize(*i);
}
void Compiler::add_shaders(Program &program)
{
for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
{
+ string stage_src = Formatter().apply(*i);
+
if(i->type==Stage::VERTEX)
{
- program.attach_shader_owned(new VertexShader(apply<Formatter>(*i)));
+ program.attach_shader_owned(new VertexShader(stage_src));
for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
program.bind_attribute(j->second, j->first);
}
else if(i->type==Stage::GEOMETRY)
- program.attach_shader_owned(new GeometryShader(apply<Formatter>(*i)));
+ program.attach_shader_owned(new GeometryShader(stage_src));
else if(i->type==Stage::FRAGMENT)
{
- program.attach_shader_owned(new FragmentShader(apply<Formatter>(*i)));
+ program.attach_shader_owned(new FragmentShader(stage_src));
if(EXT_gpu_shader4)
{
for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
}
}
-void Compiler::append_module(Module &mod)
+void Compiler::append_module(Module &mod, DataFile::Collection *res)
{
- vector<Import *> imports = apply<NodeGatherer<Import> >(mod.shared);
+ vector<Import *> imports = NodeGatherer<Import>().apply(mod.shared);
for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
- import((*i)->module);
- apply<NodeRemover>(mod.shared, set<Node *>(imports.begin(), imports.end()));
+ import(res, (*i)->module);
+ NodeRemover(set<Node *>(imports.begin(), imports.end())).apply(mod.shared);
append_stage(mod.shared);
for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
target->required_version = stage.required_version;
for(NodeList<Statement>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
target->content.body.push_back(*i);
- apply<DeclarationCombiner>(*target);
+ DeclarationCombiner().apply(*target);
}
-void Compiler::process()
-{
- for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
- generate(*i);
- for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
- {
- if(optimize(*i))
- i = module->stages.begin();
- else
- ++i;
- }
- for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
- finalize(*i);
-}
-
-void Compiler::import(const string &name)
+void Compiler::import(DataFile::Collection *resources, const string &name)
{
string fn = name+".glsl";
if(find(imported_names, fn)!=imported_names.end())
if(!io)
throw runtime_error(format("module %s not found", name));
Parser import_parser;
- append_module(import_parser.parse(*io, fn, imported_names.size()));
+ append_module(import_parser.parse(*io, fn, imported_names.size()), resources);
}
void Compiler::generate(Stage &stage)
stage.required_version = module->shared.required_version;
inject_block(stage.content, module->shared.content);
- apply<DeclarationReorderer>(stage);
- apply<FunctionResolver>(stage);
- apply<VariableResolver>(stage);
- apply<InterfaceGenerator>(stage);
- apply<VariableResolver>(stage);
- apply<DeclarationReorderer>(stage);
- apply<FunctionResolver>(stage);
- apply<LegacyConverter>(stage);
+ DeclarationReorderer().apply(stage);
+ FunctionResolver().apply(stage);
+ VariableResolver().apply(stage);
+ InterfaceGenerator().apply(stage);
+ VariableResolver().apply(stage);
+ DeclarationReorderer().apply(stage);
+ FunctionResolver().apply(stage);
+ LegacyConverter().apply(stage);
}
bool Compiler::optimize(Stage &stage)
{
- apply<ConstantConditionEliminator>(stage);
+ ConstantConditionEliminator().apply(stage);
- set<FunctionDeclaration *> inlineable = apply<InlineableFunctionLocator>(stage);
- apply<FunctionInliner>(stage, inlineable);
+ set<FunctionDeclaration *> inlineable = InlineableFunctionLocator().apply(stage);
+ FunctionInliner(inlineable).apply(stage);
- set<Node *> unused = apply<UnusedVariableLocator>(stage);
- set<Node *> unused2 = apply<UnusedFunctionLocator>(stage);
+ set<Node *> unused = UnusedVariableLocator().apply(stage);
+ set<Node *> unused2 = UnusedFunctionLocator().apply(stage);
unused.insert(unused2.begin(), unused2.end());
- apply<NodeRemover>(stage, unused);
+ NodeRemover(unused).apply(stage);
return !unused.empty();
}
void Compiler::finalize(Stage &stage)
{
if(get_gl_api()==OPENGL_ES2)
- apply<DefaultPrecisionGenerator>(stage);
+ DefaultPrecisionGenerator().apply(stage);
else
- apply<PrecisionRemover>(stage);
+ PrecisionRemover().apply(stage);
}
void Compiler::inject_block(Block &target, const Block &source)
target.body.insert(insert_point, (*i)->clone());
}
-template<typename T>
-typename T::ResultType Compiler::apply(Stage &stage)
-{
- T visitor;
- visitor.apply(stage);
- return visitor.get_result();
-}
-
-template<typename T, typename A>
-typename T::ResultType Compiler::apply(Stage &stage, const A &arg)
-{
- T visitor(arg);
- visitor.apply(stage);
- return visitor.get_result();
-}
-
} // namespace SL
} // namespace GL
} // namespace Msp