1 #include <msp/core/algorithm.h>
2 #include <msp/gl/extensions/ext_gpu_shader4.h>
3 #include <msp/strings/format.h>
5 #include "compatibility.h"
12 #include "resources.h"
24 features(Features::from_context()),
29 Compiler::Compiler(const Features &f):
40 void Compiler::clear()
43 module = new Module();
44 imported_names.clear();
45 module->source_map.set_name(0, "<generated>");
48 void Compiler::set_source(const string &source, const string &src_name)
52 imported_names.push_back(src_name);
53 append_module(parser.parse(source, src_name, 1), 0);
56 void Compiler::load_source(IO::Base &io, DataFile::Collection *res, const string &src_name)
60 imported_names.push_back(src_name);
61 append_module(parser.parse(io, src_name, 1), res);
64 void Compiler::load_source(IO::Base &io, const string &src_name)
66 load_source(io, 0, src_name);
69 void Compiler::specialize(const map<string, int> &sv)
75 void Compiler::compile(Mode mode)
77 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
79 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
82 i = module->stages.begin();
86 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
90 string Compiler::get_combined_glsl() const
94 unsigned source_count = module->source_map.get_count();
95 for(unsigned i=1; i<source_count; ++i)
96 glsl += format("#pragma MSP source(%d, \"%s\")\n", i, module->source_map.get_name(i));
97 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
99 glsl += format("#pragma MSP stage(%s)\n", Stage::get_stage_name(i->type));
100 glsl += Formatter().apply(*i, MODULE);
107 vector<Stage::Type> Compiler::get_stages() const
109 vector<Stage::Type> stage_types;
110 stage_types.reserve(module->stages.size());
111 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
112 stage_types.push_back(i->type);
116 string Compiler::get_stage_glsl(Stage::Type stage_type) const
118 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
119 if(i->type==stage_type)
120 return Formatter().apply(*i, PROGRAM);
121 throw key_error(Stage::get_stage_name(stage_type));
124 const map<string, unsigned> &Compiler::get_vertex_attributes() const
126 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
127 if(i->type==Stage::VERTEX)
129 throw invalid_operation("Compiler::get_vertex_attributes");
132 const map<string, unsigned> &Compiler::get_fragment_outputs() const
134 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
135 if(i->type==Stage::FRAGMENT)
137 throw invalid_operation("Compiler::get_fragment_outputs");
140 const SourceMap &Compiler::get_source_map() const
142 return module->source_map;
145 string Compiler::get_stage_debug(Stage::Type stage_type) const
147 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
148 if(i->type==stage_type)
149 return DumpTree().apply(*i);
150 throw key_error(Stage::get_stage_name(stage_type));
153 void Compiler::append_module(Module &mod, DataFile::Collection *res)
155 module->source_map.merge_from(mod.source_map);
157 vector<Import *> imports = NodeGatherer<Import>().apply(mod.shared);
158 for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
159 import(res, (*i)->module);
160 NodeRemover().apply(mod.shared, set<Node *>(imports.begin(), imports.end()));
162 append_stage(mod.shared);
163 for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
167 void Compiler::append_stage(Stage &stage)
170 if(stage.type==Stage::SHARED)
171 target = &module->shared;
174 list<Stage>::iterator i;
175 for(i=module->stages.begin(); (i!=module->stages.end() && i->type<stage.type); ++i) ;
176 if(i==module->stages.end() || i->type>stage.type)
178 list<Stage>::iterator j = module->stages.insert(i, stage.type);
179 if(i!=module->stages.end())
182 if(i!=module->stages.begin())
189 if(target->content.body.empty())
191 Stage *builtins = get_builtins(stage.type);
192 if(builtins && builtins!=&stage)
193 append_stage(*builtins);
196 if(stage.required_features.glsl_version>target->required_features.glsl_version)
197 target->required_features.glsl_version = stage.required_features.glsl_version;
198 for(NodeList<Statement>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
199 target->content.body.push_back(*i);
200 DeclarationCombiner().apply(*target);
203 void Compiler::import(DataFile::Collection *resources, const string &name)
205 string fn = name+".glsl";
206 if(find(imported_names, fn)!=imported_names.end())
208 imported_names.push_back(fn);
210 RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
212 throw runtime_error(format("module %s not found", name));
213 Parser import_parser;
214 append_module(import_parser.parse(*io, fn, module->source_map.get_count()), resources);
217 void Compiler::generate(Stage &stage, Mode mode)
219 stage.required_features.gl_api = features.gl_api;
220 if(module->shared.required_features.glsl_version>stage.required_features.glsl_version)
221 stage.required_features.glsl_version = module->shared.required_features.glsl_version;
222 inject_block(stage.content, module->shared.content);
224 DeclarationReorderer().apply(stage);
226 // Initial resolving pass
227 BlockHierarchyResolver().apply(stage);
228 FunctionResolver().apply(stage);
229 VariableResolver().apply(stage);
231 /* All variables local to a stage have been resolved. Resolve non-local
232 variables through interfaces. */
233 InterfaceGenerator().apply(stage);
234 VariableResolver().apply(stage);
236 DeclarationReorderer().apply(stage);
237 FunctionResolver().apply(stage);
238 ConstantSpecializer().apply(stage, (mode==PROGRAM && specialized ? &spec_values : 0));
240 LegacyConverter().apply(stage, features);
243 bool Compiler::optimize(Stage &stage)
245 ConstantConditionEliminator().apply(stage);
247 FunctionInliner().apply(stage);
248 BlockHierarchyResolver().apply(stage);
249 VariableResolver().apply(stage);
251 /* Removing variables or functions may cause things from the previous stage
253 bool result = UnusedVariableRemover().apply(stage);
254 result |= UnusedFunctionRemover().apply(stage);
259 void Compiler::finalize(Stage &stage, Mode mode)
261 if(get_gl_api()==OPENGL_ES2 && mode==PROGRAM)
262 DefaultPrecisionGenerator().apply(stage);
263 else if(mode==MODULE)
264 PrecisionRemover().apply(stage);
267 void Compiler::inject_block(Block &target, const Block &source)
269 NodeList<Statement>::iterator insert_point = target.body.begin();
270 for(NodeList<Statement>::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
271 target.body.insert(insert_point, (*i)->clone());