1 #include <msp/core/algorithm.h>
2 #include <msp/gl/extensions/ext_gpu_shader4.h>
3 #include <msp/strings/format.h>
4 #include <msp/strings/regex.h>
5 #include <msp/strings/utils.h>
6 #include "compatibility.h"
32 void Compiler::compile(const string &source, const string &src_name)
36 module = new Module();
38 imported_names.push_back(src_name);
39 append_module(parser.parse(source, src_name, 1));
43 void Compiler::compile(IO::Base &io, Resources *res, const string &src_name)
47 module = new Module();
49 imported_names.push_back(src_name);
50 append_module(parser.parse(io, src_name, 1));
54 void Compiler::compile(IO::Base &io, const string &src_name)
56 compile(io, 0, src_name);
59 void Compiler::add_shaders(Program &program)
62 throw invalid_operation("Compiler::add_shaders");
66 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
68 if(i->type==Stage::VERTEX)
70 program.attach_shader_owned(new VertexShader(apply<Formatter>(*i)));
71 for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
72 program.bind_attribute(j->second, j->first);
74 else if(i->type==Stage::GEOMETRY)
75 program.attach_shader_owned(new GeometryShader(apply<Formatter>(*i)));
76 else if(i->type==Stage::FRAGMENT)
78 program.attach_shader_owned(new FragmentShader(apply<Formatter>(*i)));
81 for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
82 program.bind_fragment_data(j->second, j->first);
87 catch(const compile_error &e)
89 static const Regex r_message("^(([0-9]+)\\(([0-9]+)\\) :|ERROR: ([0-9]+):([0-9]+):) (.*)$");
90 vector<string> lines = split(e.what(), '\n');
92 for(vector<string>::const_iterator i=lines.begin(); i!=lines.end(); ++i)
94 RegMatch m = r_message.match(*i);
101 index = lexical_cast<unsigned>(m[2].str);
102 line = lexical_cast<unsigned>(m[3].str);
106 index = lexical_cast<unsigned>(m[4].str);
107 line = lexical_cast<unsigned>(m[5].str);
109 const char *src = "<unknown>";
112 else if(index-1<imported_names.size())
113 src = imported_names[index-1].c_str();
114 translated += format("%s:%d: %s", src, line, m[6].str);
121 throw compile_error(translated);
125 void Compiler::append_module(Module &mod)
127 vector<Import *> imports = apply<NodeGatherer<Import> >(mod.shared);
128 for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
129 import((*i)->module);
130 apply<NodeRemover>(mod.shared, set<Node *>(imports.begin(), imports.end()));
132 append_stage(mod.shared);
133 for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
137 void Compiler::append_stage(Stage &stage)
140 if(stage.type==Stage::SHARED)
141 target = &module->shared;
144 list<Stage>::iterator i;
145 for(i=module->stages.begin(); (i!=module->stages.end() && i->type<stage.type); ++i) ;
146 if(i==module->stages.end() || i->type>stage.type)
148 list<Stage>::iterator j = module->stages.insert(i, stage.type);
149 if(i!=module->stages.end())
152 if(i!=module->stages.begin())
159 if(stage.required_version>target->required_version)
160 target->required_version = stage.required_version;
161 for(NodeList<Statement>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
162 target->content.body.push_back(*i);
163 apply<DeclarationCombiner>(*target);
166 void Compiler::process()
168 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
170 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
173 i = module->stages.begin();
177 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
181 void Compiler::import(const string &name)
183 string fn = name+".glsl";
184 if(find(imported_names, fn)!=imported_names.end())
186 imported_names.push_back(fn);
188 RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
190 throw runtime_error(format("module %s not found", name));
191 Parser import_parser;
192 append_module(import_parser.parse(*io, fn, imported_names.size()));
195 void Compiler::generate(Stage &stage)
197 if(module->shared.required_version>stage.required_version)
198 stage.required_version = module->shared.required_version;
199 inject_block(stage.content, module->shared.content);
201 apply<DeclarationReorderer>(stage);
202 apply<FunctionResolver>(stage);
203 apply<VariableResolver>(stage);
204 apply<InterfaceGenerator>(stage);
205 apply<VariableResolver>(stage);
206 apply<DeclarationReorderer>(stage);
207 apply<FunctionResolver>(stage);
208 apply<LegacyConverter>(stage);
211 bool Compiler::optimize(Stage &stage)
213 apply<ConstantConditionEliminator>(stage);
215 set<FunctionDeclaration *> inlineable = apply<InlineableFunctionLocator>(stage);
216 apply<FunctionInliner>(stage, inlineable);
218 set<Node *> unused = apply<UnusedVariableLocator>(stage);
219 set<Node *> unused2 = apply<UnusedFunctionLocator>(stage);
220 unused.insert(unused2.begin(), unused2.end());
221 apply<NodeRemover>(stage, unused);
223 return !unused.empty();
226 void Compiler::finalize(Stage &stage)
228 if(get_gl_api()==OPENGL_ES2)
229 apply<DefaultPrecisionGenerator>(stage);
231 apply<PrecisionRemover>(stage);
234 void Compiler::inject_block(Block &target, const Block &source)
236 NodeList<Statement>::iterator insert_point = target.body.begin();
237 for(NodeList<Statement>::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
238 target.body.insert(insert_point, (*i)->clone());
242 typename T::ResultType Compiler::apply(Stage &stage)
245 visitor.apply(stage);
246 return visitor.get_result();
249 template<typename T, typename A>
250 typename T::ResultType Compiler::apply(Stage &stage, const A &arg)
253 visitor.apply(stage);
254 return visitor.get_result();