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)
80 for(list<Stage>::iterator i=module->stages.begin(); (i!=module->stages.end() && n<10000); ++n)
82 OptimizeResult result = optimize(*i);
83 if(result==REDO_PREVIOUS)
84 i = module->stages.begin();
85 else if(result!=REDO_STAGE)
88 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
92 string Compiler::get_combined_glsl() const
96 unsigned source_count = module->source_map.get_count();
97 for(unsigned i=1; i<source_count; ++i)
98 glsl += format("#pragma MSP source(%d, \"%s\")\n", i, module->source_map.get_name(i));
99 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
101 glsl += format("#pragma MSP stage(%s)\n", Stage::get_stage_name(i->type));
102 glsl += Formatter().apply(*i, MODULE);
109 vector<Stage::Type> Compiler::get_stages() const
111 vector<Stage::Type> stage_types;
112 stage_types.reserve(module->stages.size());
113 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
114 stage_types.push_back(i->type);
118 string Compiler::get_stage_glsl(Stage::Type stage_type) const
120 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
121 if(i->type==stage_type)
122 return Formatter().apply(*i, PROGRAM);
123 throw key_error(Stage::get_stage_name(stage_type));
126 const map<string, unsigned> &Compiler::get_vertex_attributes() const
128 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
129 if(i->type==Stage::VERTEX)
131 throw invalid_operation("Compiler::get_vertex_attributes");
134 const map<string, unsigned> &Compiler::get_fragment_outputs() const
136 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
137 if(i->type==Stage::FRAGMENT)
139 throw invalid_operation("Compiler::get_fragment_outputs");
142 const SourceMap &Compiler::get_source_map() const
144 return module->source_map;
147 string Compiler::get_stage_debug(Stage::Type stage_type) const
149 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
150 if(i->type==stage_type)
151 return DumpTree().apply(*i);
152 throw key_error(Stage::get_stage_name(stage_type));
155 void Compiler::append_module(Module &mod, DataFile::Collection *res)
157 module->source_map.merge_from(mod.source_map);
159 vector<Import *> imports = NodeGatherer<Import>().apply(mod.shared);
160 for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
161 import(res, (*i)->module);
162 NodeRemover().apply(mod.shared, set<Node *>(imports.begin(), imports.end()));
164 append_stage(mod.shared);
165 for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
169 void Compiler::append_stage(Stage &stage)
172 if(stage.type==Stage::SHARED)
173 target = &module->shared;
176 list<Stage>::iterator i;
177 for(i=module->stages.begin(); (i!=module->stages.end() && i->type<stage.type); ++i) ;
178 if(i==module->stages.end() || i->type>stage.type)
180 list<Stage>::iterator j = module->stages.insert(i, stage.type);
181 if(i!=module->stages.end())
184 if(i!=module->stages.begin())
191 if(target->content.body.empty())
193 Stage *builtins = get_builtins(stage.type);
194 if(builtins && builtins!=&stage)
195 append_stage(*builtins);
198 if(stage.required_features.glsl_version>target->required_features.glsl_version)
199 target->required_features.glsl_version = stage.required_features.glsl_version;
200 for(NodeList<Statement>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
201 target->content.body.push_back(*i);
202 DeclarationCombiner().apply(*target);
205 void Compiler::import(DataFile::Collection *resources, const string &name)
207 string fn = name+".glsl";
208 if(find(imported_names, fn)!=imported_names.end())
210 imported_names.push_back(fn);
212 RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
214 throw runtime_error(format("module %s not found", name));
215 Parser import_parser;
216 append_module(import_parser.parse(*io, fn, module->source_map.get_count()), resources);
219 void Compiler::generate(Stage &stage, Mode mode)
221 stage.required_features.gl_api = features.gl_api;
222 if(module->shared.required_features.glsl_version>stage.required_features.glsl_version)
223 stage.required_features.glsl_version = module->shared.required_features.glsl_version;
224 inject_block(stage.content, module->shared.content);
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 FunctionResolver().apply(stage);
237 ConstantSpecializer().apply(stage, (mode==PROGRAM && specialized ? &spec_values : 0));
239 LegacyConverter().apply(stage, features);
242 Compiler::OptimizeResult Compiler::optimize(Stage &stage)
244 ConstantConditionEliminator().apply(stage);
246 bool any_inlined = FunctionInliner().apply(stage);
247 BlockHierarchyResolver().apply(stage);
248 VariableResolver().apply(stage);
250 /* Removing variables or functions may cause things from the previous stage
252 bool any_removed = UnusedVariableRemover().apply(stage);
253 any_removed |= UnusedFunctionRemover().apply(stage);
255 return any_removed ? REDO_PREVIOUS : any_inlined ? REDO_STAGE : NEXT_STAGE;
258 void Compiler::finalize(Stage &stage, Mode mode)
260 if(get_gl_api()==OPENGL_ES2 && mode==PROGRAM)
261 DefaultPrecisionGenerator().apply(stage);
262 else if(mode==MODULE)
263 PrecisionRemover().apply(stage);
266 void Compiler::inject_block(Block &target, const Block &source)
268 NodeList<Statement>::iterator insert_point = target.body.begin();
269 for(NodeList<Statement>::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
270 target.body.insert(insert_point, (*i)->clone());