1 #include <msp/core/algorithm.h>
2 #include <msp/strings/format.h>
3 #include <msp/strings/utils.h>
5 #include "compatibility.h"
10 #include "glsl_error.h"
13 #include "resources.h"
25 features(Features::from_context()),
31 Compiler::Compiler(const Features &f):
43 void Compiler::clear()
46 module = new Module();
47 imported_names.clear();
48 module->source_map.set_name(0, "<generated>");
51 void Compiler::set_source(const string &source, const string &src_name)
55 imported_names.push_back(src_name);
56 append_module(parser.parse(source, src_name, 1), 0);
59 void Compiler::load_source(IO::Base &io, DataFile::Collection *res, const string &src_name)
63 imported_names.push_back(src_name);
64 append_module(parser.parse(io, src_name, 1), res);
67 void Compiler::load_source(IO::Base &io, const string &src_name)
69 load_source(io, 0, src_name);
72 void Compiler::specialize(const map<string, int> &sv)
78 void Compiler::compile(Mode mode)
80 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
84 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
88 throw invalid_shader_source(get_diagnostics());
91 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++n)
93 OptimizeResult result = optimize(*i);
94 if(result==REDO_PREVIOUS)
95 i = module->stages.begin();
96 else if(result!=REDO_STAGE)
99 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
105 string Compiler::get_combined_glsl() const
108 throw invalid_operation("Compiler::get_combined_glsl");
112 unsigned source_count = module->source_map.get_count();
113 for(unsigned i=1; i<source_count; ++i)
114 glsl += format("#pragma MSP source(%d, \"%s\")\n", i, module->source_map.get_name(i));
115 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
117 glsl += format("#pragma MSP stage(%s)\n", Stage::get_stage_name(i->type));
118 glsl += Formatter().apply(*i);
125 vector<Stage::Type> Compiler::get_stages() const
127 vector<Stage::Type> stage_types;
128 stage_types.reserve(module->stages.size());
129 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
130 stage_types.push_back(i->type);
134 string Compiler::get_stage_glsl(Stage::Type stage_type) const
137 throw invalid_operation("Compiler::get_stage_glsl");
138 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
139 if(i->type==stage_type)
140 return Formatter().apply(*i);
141 throw key_error(Stage::get_stage_name(stage_type));
144 const map<string, unsigned> &Compiler::get_vertex_attributes() const
147 throw invalid_operation("Compiler::get_vertex_attributes");
148 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
149 if(i->type==Stage::VERTEX)
151 throw invalid_operation("Compiler::get_vertex_attributes");
154 const map<string, unsigned> &Compiler::get_fragment_outputs() const
157 throw invalid_operation("Compiler::get_fragment_outputs");
158 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
159 if(i->type==Stage::FRAGMENT)
161 throw invalid_operation("Compiler::get_fragment_outputs");
164 const SourceMap &Compiler::get_source_map() const
166 return module->source_map;
169 string Compiler::get_stage_debug(Stage::Type stage_type) const
171 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
172 if(i->type==stage_type)
173 return DumpTree().apply(*i);
174 throw key_error(Stage::get_stage_name(stage_type));
177 string Compiler::get_diagnostics() const
180 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
181 for(vector<Diagnostic>::const_iterator j=i->diagnostics.begin(); j!=i->diagnostics.end(); ++j)
182 if(j->source!=INTERNAL_SOURCE)
183 append(combined, "\n", format("%s:%d: %s", module->source_map.get_name(j->source), j->line, j->message));
187 void Compiler::append_module(Module &mod, DataFile::Collection *res)
189 module->source_map.merge_from(mod.source_map);
191 vector<Import *> imports;
192 for(NodeList<Statement>::const_iterator i=mod.shared.content.body.begin(); i!=mod.shared.content.body.end(); ++i)
193 if(Import *imp = dynamic_cast<Import *>(i->get()))
194 imports.push_back(imp);
195 for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
196 import(res, (*i)->module);
197 NodeRemover().apply(mod.shared, set<Node *>(imports.begin(), imports.end()));
199 append_stage(mod.shared);
200 for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
204 void Compiler::append_stage(Stage &stage)
207 if(stage.type==Stage::SHARED)
208 target = &module->shared;
211 list<Stage>::iterator i;
212 for(i=module->stages.begin(); (i!=module->stages.end() && i->type<stage.type); ++i) ;
213 if(i==module->stages.end() || i->type>stage.type)
215 list<Stage>::iterator j = module->stages.insert(i, stage.type);
216 if(i!=module->stages.end())
219 if(i!=module->stages.begin())
226 if(stage.required_features.glsl_version>target->required_features.glsl_version)
227 target->required_features.glsl_version = stage.required_features.glsl_version;
228 for(NodeList<Statement>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
229 target->content.body.push_back(*i);
232 void Compiler::import(DataFile::Collection *resources, const string &name)
234 string fn = name+".glsl";
235 if(find(imported_names, fn)!=imported_names.end())
237 imported_names.push_back(fn);
239 RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
241 throw runtime_error(format("module %s not found", name));
242 Parser import_parser;
243 append_module(import_parser.parse(*io, fn, module->source_map.get_count()), resources);
246 void Compiler::generate(Stage &stage, Mode mode)
248 stage.required_features.gl_api = features.gl_api;
249 if(module->shared.required_features.glsl_version>stage.required_features.glsl_version)
250 stage.required_features.glsl_version = module->shared.required_features.glsl_version;
252 inject_block(stage.content, module->shared.content);
253 if(const Stage *builtins = get_builtins(stage.type))
254 inject_block(stage.content, builtins->content);
255 if(const Stage *builtins = get_builtins(Stage::SHARED))
256 inject_block(stage.content, builtins->content);
258 // Initial resolving pass
261 /* All variables local to a stage have been resolved. Resolve non-local
262 variables through interfaces. */
263 InterfaceGenerator().apply(stage);
264 resolve(stage, RESOLVE_BLOCKS|RESOLVE_TYPES|RESOLVE_VARIABLES);
266 ConstantSpecializer().apply(stage, (mode==PROGRAM && specialized ? &spec_values : 0));
269 LegacyConverter().apply(stage, features);
270 resolve(stage, RESOLVE_VARIABLES|RESOLVE_FUNCTIONS);
275 bool Compiler::resolve(Stage &stage, unsigned &flags, unsigned bit)
281 return T().apply(stage);
284 void Compiler::resolve(Stage &stage, unsigned flags)
288 if(resolve<BlockHierarchyResolver>(stage, flags, RESOLVE_BLOCKS))
290 else if(resolve<TypeResolver>(stage, flags, RESOLVE_TYPES))
291 flags |= RESOLVE_BLOCKS|RESOLVE_VARIABLES|RESOLVE_EXPRESSIONS;
292 else if(resolve<VariableResolver>(stage, flags, RESOLVE_VARIABLES))
293 flags |= RESOLVE_EXPRESSIONS;
294 else if(resolve<FunctionResolver>(stage, flags, RESOLVE_FUNCTIONS))
295 flags |= RESOLVE_EXPRESSIONS;
296 else if(resolve<ExpressionResolver>(stage, flags, RESOLVE_EXPRESSIONS))
297 flags |= RESOLVE_VARIABLES|RESOLVE_FUNCTIONS;
301 bool Compiler::validate(Stage &stage)
303 TypeValidator().apply(stage);
304 DeclarationValidator().apply(stage);
305 ReferenceValidator().apply(stage);
306 ExpressionValidator().apply(stage);
308 stable_sort(stage.diagnostics, &diagnostic_line_order);
310 for(vector<Diagnostic>::const_iterator i=stage.diagnostics.begin(); i!=stage.diagnostics.end(); ++i)
311 if(i->severity==Diagnostic::ERR)
317 bool Compiler::diagnostic_line_order(const Diagnostic &diag1, const Diagnostic &diag2)
319 if(diag1.provoking_source!=diag2.provoking_source)
321 // Sort builtins first and imported modules according to import order.
322 if(diag1.provoking_source<=BUILTIN_SOURCE)
323 return diag1.provoking_source<diag2.provoking_source;
324 else if(diag2.provoking_source<=BUILTIN_SOURCE)
327 return diag1.provoking_source>diag2.provoking_source;
329 return diag1.provoking_line<diag2.provoking_line;
332 Compiler::OptimizeResult Compiler::optimize(Stage &stage)
334 if(ConstantFolder().apply(stage))
335 resolve(stage, RESOLVE_EXPRESSIONS);
336 ConstantConditionEliminator().apply(stage);
338 bool any_inlined = false;
339 if(FunctionInliner().apply(stage))
341 resolve(stage, RESOLVE_TYPES|RESOLVE_VARIABLES|RESOLVE_FUNCTIONS|RESOLVE_EXPRESSIONS);
344 if(ExpressionInliner().apply(stage))
346 resolve(stage, RESOLVE_VARIABLES|RESOLVE_FUNCTIONS|RESOLVE_EXPRESSIONS);
350 /* Removing variables or functions may cause things from the previous stage
352 bool any_removed = UnusedVariableRemover().apply(stage);
353 any_removed |= UnusedFunctionRemover().apply(stage);
354 any_removed |= UnusedTypeRemover().apply(stage);
356 return any_removed ? REDO_PREVIOUS : any_inlined ? REDO_STAGE : NEXT_STAGE;
359 void Compiler::finalize(Stage &stage, Mode mode)
361 if(get_gl_api()==OPENGL_ES2 && mode==PROGRAM)
362 DefaultPrecisionGenerator().apply(stage);
363 else if(mode==MODULE)
364 PrecisionRemover().apply(stage);
367 void Compiler::inject_block(Block &target, const Block &source)
369 NodeList<Statement>::iterator insert_point = target.body.begin();
370 for(NodeList<Statement>::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
371 target.body.insert(insert_point, (*i)->clone());