1 #include <msp/core/algorithm.h>
2 #include <msp/strings/format.h>
3 #include <msp/strings/utils.h>
10 #include "glsl_error.h"
14 #include "resources.h"
26 features(Features::from_context()),
32 Compiler::Compiler(const Features &f):
44 void Compiler::clear()
47 module = new Module();
48 imported_names.clear();
49 module->source_map.set_name(0, "<generated>");
52 void Compiler::set_source(const string &source, const string &src_name)
56 imported_names.push_back(src_name);
57 append_module(parser.parse(source, src_name, 1), 0);
60 void Compiler::load_source(IO::Base &io, DataFile::Collection *res, const string &src_name)
64 imported_names.push_back(src_name);
65 append_module(parser.parse(io, src_name, 1), res);
68 void Compiler::load_source(IO::Base &io, const string &src_name)
70 load_source(io, 0, src_name);
73 void Compiler::specialize(const map<string, int> &sv)
79 void Compiler::compile(Mode mode)
81 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
83 ConstantIdAssigner().apply(*module, features);
85 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
87 GlobalInterfaceValidator().apply(*module);
90 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
94 throw invalid_shader_source(get_diagnostics());
96 if(mode==PROGRAM && specialized)
98 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
99 ConstantSpecializer().apply(*i, spec_values);
101 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
103 OptimizeResult result = optimize(*i);
104 if(result==REDO_PREVIOUS)
105 i = module->stages.begin();
106 else if(result!=REDO_STAGE)
110 LocationAllocator().apply(*module, features);
111 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
117 string Compiler::get_combined_glsl() const
120 throw invalid_operation("Compiler::get_combined_glsl");
124 unsigned source_count = module->source_map.get_count();
125 for(unsigned i=1; i<source_count; ++i)
126 glsl += format("#pragma MSP source(%d, \"%s\")\n", i, module->source_map.get_name(i));
127 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
129 glsl += format("#pragma MSP stage(%s)\n", Stage::get_stage_name(i->type));
130 glsl += Formatter().apply(*i);
137 vector<Stage::Type> Compiler::get_stages() const
139 vector<Stage::Type> stage_types;
140 stage_types.reserve(module->stages.size());
141 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
142 stage_types.push_back(i->type);
146 string Compiler::get_stage_glsl(Stage::Type stage_type) const
149 throw invalid_operation("Compiler::get_stage_glsl");
150 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
151 if(i->type==stage_type)
152 return Formatter().apply(*i);
153 throw key_error(Stage::get_stage_name(stage_type));
156 const map<string, unsigned> &Compiler::get_vertex_attributes() const
159 throw invalid_operation("Compiler::get_vertex_attributes");
160 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
161 if(i->type==Stage::VERTEX)
163 throw invalid_operation("Compiler::get_vertex_attributes");
166 const map<string, unsigned> &Compiler::get_fragment_outputs() const
169 throw invalid_operation("Compiler::get_fragment_outputs");
170 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
171 if(i->type==Stage::FRAGMENT)
173 throw invalid_operation("Compiler::get_fragment_outputs");
176 const map<string, unsigned> &Compiler::get_texture_bindings() const
179 throw invalid_operation("Compiler::get_texture_bindings");
180 return module->shared.texture_bindings;
183 const map<string, unsigned> &Compiler::get_uniform_block_bindings() const
186 throw invalid_operation("Compiler::get_uniform_block_bindings");
187 return module->shared.uniform_block_bindings;
190 const SourceMap &Compiler::get_source_map() const
192 return module->source_map;
195 string Compiler::get_stage_debug(Stage::Type stage_type) const
197 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
198 if(i->type==stage_type)
199 return DumpTree().apply(*i);
200 throw key_error(Stage::get_stage_name(stage_type));
203 string Compiler::get_diagnostics() const
206 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
207 for(vector<Diagnostic>::const_iterator j=i->diagnostics.begin(); j!=i->diagnostics.end(); ++j)
208 if(j->source!=INTERNAL_SOURCE)
209 append(combined, "\n", format("%s:%d: %s", module->source_map.get_name(j->source), j->line, j->message));
213 void Compiler::append_module(Module &mod, DataFile::Collection *res)
215 module->source_map.merge_from(mod.source_map);
217 vector<Import *> imports;
218 for(NodeList<Statement>::const_iterator i=mod.shared.content.body.begin(); i!=mod.shared.content.body.end(); ++i)
219 if(Import *imp = dynamic_cast<Import *>(i->get()))
220 imports.push_back(imp);
221 for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
222 import(res, (*i)->module);
223 NodeRemover().apply(mod.shared, set<Node *>(imports.begin(), imports.end()));
225 append_stage(mod.shared);
226 for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
230 void Compiler::append_stage(Stage &stage)
233 if(stage.type==Stage::SHARED)
234 target = &module->shared;
237 list<Stage>::iterator i;
238 for(i=module->stages.begin(); (i!=module->stages.end() && i->type<stage.type); ++i) ;
239 if(i==module->stages.end() || i->type>stage.type)
241 list<Stage>::iterator j = module->stages.insert(i, stage.type);
242 if(i!=module->stages.end())
245 if(i!=module->stages.begin())
252 if(stage.required_features.glsl_version>target->required_features.glsl_version)
253 target->required_features.glsl_version = stage.required_features.glsl_version;
254 for(NodeList<Statement>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
255 target->content.body.push_back(*i);
258 void Compiler::import(DataFile::Collection *resources, const string &name)
260 string fn = name+".glsl";
261 if(find(imported_names, fn)!=imported_names.end())
263 imported_names.push_back(fn);
265 RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
267 throw runtime_error(format("module %s not found", name));
268 Parser import_parser;
269 append_module(import_parser.parse(*io, fn, module->source_map.get_count()), resources);
272 void Compiler::generate(Stage &stage)
274 stage.required_features.gl_api = features.gl_api;
275 if(module->shared.required_features.glsl_version>stage.required_features.glsl_version)
276 stage.required_features.glsl_version = module->shared.required_features.glsl_version;
278 inject_block(stage.content, module->shared.content);
279 if(const Stage *builtins = get_builtins(stage.type))
280 inject_block(stage.content, builtins->content);
281 if(const Stage *builtins = get_builtins(Stage::SHARED))
282 inject_block(stage.content, builtins->content);
284 // Initial resolving pass
287 /* All variables local to a stage have been resolved. Resolve non-local
288 variables through interfaces. */
289 InterfaceGenerator().apply(stage);
290 resolve(stage, RESOLVE_BLOCKS|RESOLVE_TYPES|RESOLVE_VARIABLES);
294 bool Compiler::resolve(Stage &stage, unsigned &flags, unsigned bit)
300 return T().apply(stage);
303 void Compiler::resolve(Stage &stage, unsigned flags)
307 if(resolve<BlockHierarchyResolver>(stage, flags, RESOLVE_BLOCKS))
309 else if(resolve<TypeResolver>(stage, flags, RESOLVE_TYPES))
310 flags |= RESOLVE_BLOCKS|RESOLVE_VARIABLES|RESOLVE_EXPRESSIONS;
311 else if(resolve<VariableResolver>(stage, flags, RESOLVE_VARIABLES))
312 flags |= RESOLVE_EXPRESSIONS;
313 else if(resolve<FunctionResolver>(stage, flags, RESOLVE_FUNCTIONS))
314 flags |= RESOLVE_EXPRESSIONS;
315 else if(resolve<ExpressionResolver>(stage, flags, RESOLVE_EXPRESSIONS))
316 flags |= RESOLVE_VARIABLES|RESOLVE_FUNCTIONS;
320 void Compiler::validate(Stage &stage)
322 DeclarationValidator().apply(stage);
323 IdentifierValidator().apply(stage);
324 ReferenceValidator().apply(stage);
325 ExpressionValidator().apply(stage);
326 StageInterfaceValidator().apply(stage);
329 bool Compiler::check_errors(Stage &stage)
331 stable_sort(stage.diagnostics, &diagnostic_line_order);
333 for(vector<Diagnostic>::const_iterator i=stage.diagnostics.begin(); i!=stage.diagnostics.end(); ++i)
334 if(i->severity==Diagnostic::ERR)
340 bool Compiler::diagnostic_line_order(const Diagnostic &diag1, const Diagnostic &diag2)
342 if(diag1.provoking_source!=diag2.provoking_source)
344 // Sort builtins first and imported modules according to import order.
345 if(diag1.provoking_source<=BUILTIN_SOURCE)
346 return diag1.provoking_source<diag2.provoking_source;
347 else if(diag2.provoking_source<=BUILTIN_SOURCE)
350 return diag1.provoking_source>diag2.provoking_source;
352 return diag1.provoking_line<diag2.provoking_line;
355 Compiler::OptimizeResult Compiler::optimize(Stage &stage)
357 if(ConstantFolder().apply(stage))
358 resolve(stage, RESOLVE_EXPRESSIONS);
359 ConstantConditionEliminator().apply(stage);
361 bool any_inlined = false;
362 if(FunctionInliner().apply(stage))
364 resolve(stage, RESOLVE_TYPES|RESOLVE_VARIABLES|RESOLVE_FUNCTIONS|RESOLVE_EXPRESSIONS);
367 if(ExpressionInliner().apply(stage))
369 resolve(stage, RESOLVE_VARIABLES|RESOLVE_FUNCTIONS|RESOLVE_EXPRESSIONS);
373 /* Removing variables or functions may cause things from the previous stage
375 bool any_removed = UnusedVariableRemover().apply(stage);
376 any_removed |= UnusedFunctionRemover().apply(stage);
377 any_removed |= UnusedTypeRemover().apply(stage);
379 return any_removed ? REDO_PREVIOUS : any_inlined ? REDO_STAGE : NEXT_STAGE;
382 void Compiler::finalize(Stage &stage, Mode mode)
386 LegacyConverter().apply(stage, features);
387 resolve(stage, RESOLVE_VARIABLES|RESOLVE_FUNCTIONS);
388 PrecisionConverter().apply(stage);
391 // Collect bindings from all stages into the shared stage's maps
392 module->shared.texture_bindings.insert(stage.texture_bindings.begin(), stage.texture_bindings.end());
393 module->shared.uniform_block_bindings.insert(stage.uniform_block_bindings.begin(), stage.uniform_block_bindings.end());
396 void Compiler::inject_block(Block &target, const Block &source)
398 NodeList<Statement>::iterator insert_point = target.body.begin();
399 for(NodeList<Statement>::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
400 target.body.insert(insert_point, (*i)->clone());