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 string stage_src = Formatter().apply(*i);
70 if(i->type==Stage::VERTEX)
72 program.attach_shader_owned(new VertexShader(stage_src));
73 for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
74 program.bind_attribute(j->second, j->first);
76 else if(i->type==Stage::GEOMETRY)
77 program.attach_shader_owned(new GeometryShader(stage_src));
78 else if(i->type==Stage::FRAGMENT)
80 program.attach_shader_owned(new FragmentShader(stage_src));
83 for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
84 program.bind_fragment_data(j->second, j->first);
89 catch(const compile_error &e)
91 static const Regex r_message("^(([0-9]+)\\(([0-9]+)\\) :|ERROR: ([0-9]+):([0-9]+):) (.*)$");
92 vector<string> lines = split(e.what(), '\n');
94 for(vector<string>::const_iterator i=lines.begin(); i!=lines.end(); ++i)
96 RegMatch m = r_message.match(*i);
103 index = lexical_cast<unsigned>(m[2].str);
104 line = lexical_cast<unsigned>(m[3].str);
108 index = lexical_cast<unsigned>(m[4].str);
109 line = lexical_cast<unsigned>(m[5].str);
111 const char *src = "<unknown>";
114 else if(index-1<imported_names.size())
115 src = imported_names[index-1].c_str();
116 translated += format("%s:%d: %s", src, line, m[6].str);
123 throw compile_error(translated);
127 void Compiler::append_module(Module &mod)
129 vector<Import *> imports = NodeGatherer<Import>().apply(mod.shared);
130 for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
131 import((*i)->module);
132 NodeRemover(set<Node *>(imports.begin(), imports.end())).apply(mod.shared);
134 append_stage(mod.shared);
135 for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
139 void Compiler::append_stage(Stage &stage)
142 if(stage.type==Stage::SHARED)
143 target = &module->shared;
146 list<Stage>::iterator i;
147 for(i=module->stages.begin(); (i!=module->stages.end() && i->type<stage.type); ++i) ;
148 if(i==module->stages.end() || i->type>stage.type)
150 list<Stage>::iterator j = module->stages.insert(i, stage.type);
151 if(i!=module->stages.end())
154 if(i!=module->stages.begin())
161 if(stage.required_version>target->required_version)
162 target->required_version = stage.required_version;
163 for(NodeList<Statement>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
164 target->content.body.push_back(*i);
165 DeclarationCombiner().apply(*target);
168 void Compiler::process()
170 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
172 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
175 i = module->stages.begin();
179 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
183 void Compiler::import(const string &name)
185 string fn = name+".glsl";
186 if(find(imported_names, fn)!=imported_names.end())
188 imported_names.push_back(fn);
190 RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
192 throw runtime_error(format("module %s not found", name));
193 Parser import_parser;
194 append_module(import_parser.parse(*io, fn, imported_names.size()));
197 void Compiler::generate(Stage &stage)
199 if(module->shared.required_version>stage.required_version)
200 stage.required_version = module->shared.required_version;
201 inject_block(stage.content, module->shared.content);
203 DeclarationReorderer().apply(stage);
204 FunctionResolver().apply(stage);
205 VariableResolver().apply(stage);
206 InterfaceGenerator().apply(stage);
207 VariableResolver().apply(stage);
208 DeclarationReorderer().apply(stage);
209 FunctionResolver().apply(stage);
210 LegacyConverter().apply(stage);
213 bool Compiler::optimize(Stage &stage)
215 ConstantConditionEliminator().apply(stage);
217 set<FunctionDeclaration *> inlineable = InlineableFunctionLocator().apply(stage);
218 FunctionInliner(inlineable).apply(stage);
220 set<Node *> unused = UnusedVariableLocator().apply(stage);
221 set<Node *> unused2 = UnusedFunctionLocator().apply(stage);
222 unused.insert(unused2.begin(), unused2.end());
223 NodeRemover(unused).apply(stage);
225 return !unused.empty();
228 void Compiler::finalize(Stage &stage)
230 if(get_gl_api()==OPENGL_ES2)
231 DefaultPrecisionGenerator().apply(stage);
233 PrecisionRemover().apply(stage);
236 void Compiler::inject_block(Block &target, const Block &source)
238 NodeList<Statement>::iterator insert_point = target.body.begin();
239 for(NodeList<Statement>::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
240 target.body.insert(insert_point, (*i)->clone());