1 #include <msp/core/algorithm.h>
2 #include <msp/strings/format.h>
4 #include "compatibility.h"
9 #include "glsl_error.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)
81 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
85 throw invalid_shader_source(get_diagnostics());
88 for(list<Stage>::iterator i=module->stages.begin(); (i!=module->stages.end() && n<10000); ++n)
90 OptimizeResult result = optimize(*i);
91 if(result==REDO_PREVIOUS)
92 i = module->stages.begin();
93 else if(result!=REDO_STAGE)
96 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
100 string Compiler::get_combined_glsl() const
104 unsigned source_count = module->source_map.get_count();
105 for(unsigned i=1; i<source_count; ++i)
106 glsl += format("#pragma MSP source(%d, \"%s\")\n", i, module->source_map.get_name(i));
107 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
109 glsl += format("#pragma MSP stage(%s)\n", Stage::get_stage_name(i->type));
110 glsl += Formatter().apply(*i, MODULE);
117 vector<Stage::Type> Compiler::get_stages() const
119 vector<Stage::Type> stage_types;
120 stage_types.reserve(module->stages.size());
121 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
122 stage_types.push_back(i->type);
126 string Compiler::get_stage_glsl(Stage::Type stage_type) const
128 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
129 if(i->type==stage_type)
130 return Formatter().apply(*i, PROGRAM);
131 throw key_error(Stage::get_stage_name(stage_type));
134 const map<string, unsigned> &Compiler::get_vertex_attributes() const
136 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
137 if(i->type==Stage::VERTEX)
139 throw invalid_operation("Compiler::get_vertex_attributes");
142 const map<string, unsigned> &Compiler::get_fragment_outputs() const
144 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
145 if(i->type==Stage::FRAGMENT)
147 throw invalid_operation("Compiler::get_fragment_outputs");
150 const SourceMap &Compiler::get_source_map() const
152 return module->source_map;
155 string Compiler::get_stage_debug(Stage::Type stage_type) const
157 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
158 if(i->type==stage_type)
159 return DumpTree().apply(*i);
160 throw key_error(Stage::get_stage_name(stage_type));
163 string Compiler::get_diagnostics() const
166 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
167 for(vector<Diagnostic>::const_iterator j=i->diagnostics.begin(); j!=i->diagnostics.end(); ++j)
168 combined += format("%s:%d: %s\n", module->source_map.get_name(j->source), j->line, j->message);
172 void Compiler::append_module(Module &mod, DataFile::Collection *res)
174 module->source_map.merge_from(mod.source_map);
176 vector<Import *> imports = NodeGatherer<Import>().apply(mod.shared);
177 for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
178 import(res, (*i)->module);
179 NodeRemover().apply(mod.shared, set<Node *>(imports.begin(), imports.end()));
181 append_stage(mod.shared);
182 for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
186 void Compiler::append_stage(Stage &stage)
189 if(stage.type==Stage::SHARED)
190 target = &module->shared;
193 list<Stage>::iterator i;
194 for(i=module->stages.begin(); (i!=module->stages.end() && i->type<stage.type); ++i) ;
195 if(i==module->stages.end() || i->type>stage.type)
197 list<Stage>::iterator j = module->stages.insert(i, stage.type);
198 if(i!=module->stages.end())
201 if(i!=module->stages.begin())
208 if(target->content.body.empty())
210 Stage *builtins = get_builtins(stage.type);
211 if(builtins && builtins!=&stage)
212 append_stage(*builtins);
215 if(stage.required_features.glsl_version>target->required_features.glsl_version)
216 target->required_features.glsl_version = stage.required_features.glsl_version;
217 for(NodeList<Statement>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
218 target->content.body.push_back(*i);
219 DeclarationCombiner().apply(*target);
222 void Compiler::import(DataFile::Collection *resources, const string &name)
224 string fn = name+".glsl";
225 if(find(imported_names, fn)!=imported_names.end())
227 imported_names.push_back(fn);
229 RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
231 throw runtime_error(format("module %s not found", name));
232 Parser import_parser;
233 append_module(import_parser.parse(*io, fn, module->source_map.get_count()), resources);
236 void Compiler::generate(Stage &stage, Mode mode)
238 stage.required_features.gl_api = features.gl_api;
239 if(module->shared.required_features.glsl_version>stage.required_features.glsl_version)
240 stage.required_features.glsl_version = module->shared.required_features.glsl_version;
241 inject_block(stage.content, module->shared.content);
243 // Initial resolving pass
244 BlockHierarchyResolver().apply(stage);
245 FunctionResolver().apply(stage);
246 VariableResolver().apply(stage);
248 /* All variables local to a stage have been resolved. Resolve non-local
249 variables through interfaces. */
250 InterfaceGenerator().apply(stage);
251 VariableResolver().apply(stage);
253 FunctionResolver().apply(stage);
254 ConstantSpecializer().apply(stage, (mode==PROGRAM && specialized ? &spec_values : 0));
256 LegacyConverter().apply(stage, features);
259 bool Compiler::validate(Stage &stage)
261 DeclarationValidator().apply(stage);
263 for(vector<Diagnostic>::const_iterator i=stage.diagnostics.begin(); i!=stage.diagnostics.end(); ++i)
264 if(i->severity==Diagnostic::ERR)
270 Compiler::OptimizeResult Compiler::optimize(Stage &stage)
272 ConstantConditionEliminator().apply(stage);
274 bool any_inlined = FunctionInliner().apply(stage);
275 any_inlined |= ExpressionInliner().apply(stage);
278 VariableResolver().apply(stage);
279 FunctionResolver().apply(stage);
282 /* Removing variables or functions may cause things from the previous stage
284 bool any_removed = UnusedVariableRemover().apply(stage);
285 any_removed |= UnusedFunctionRemover().apply(stage);
287 return any_removed ? REDO_PREVIOUS : any_inlined ? REDO_STAGE : NEXT_STAGE;
290 void Compiler::finalize(Stage &stage, Mode mode)
292 if(get_gl_api()==OPENGL_ES2 && mode==PROGRAM)
293 DefaultPrecisionGenerator().apply(stage);
294 else if(mode==MODULE)
295 PrecisionRemover().apply(stage);
298 void Compiler::inject_block(Block &target, const Block &source)
300 NodeList<Statement>::iterator insert_point = target.body.begin();
301 for(NodeList<Statement>::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
302 target.body.insert(insert_point, (*i)->clone());