1 #include <msp/core/algorithm.h>
2 #include <msp/strings/format.h>
3 #include <msp/strings/utils.h>
7 #include "deviceinfo.h"
11 #include "glsl_error.h"
12 #include "modulecache.h"
28 features(DeviceInfo::get_global().glsl_features)
31 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)
51 imported_names.push_back(src_name);
52 ModuleCache mod_cache(0);
53 append_module(mod_cache.add_module(source, src_name), mod_cache);
56 void Compiler::load_source(IO::Base &io, DataFile::Collection *res, const string &src_name)
59 imported_names.push_back(src_name);
60 ModuleCache mod_cache(res);
61 append_module(mod_cache.add_module(io, src_name), mod_cache);
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 if(specialized && mode!=PROGRAM)
78 throw invalid_operation("Compiler::compile");
80 for(Stage &s: module->stages)
82 ConstantIdAssigner().apply(*module, features);
84 for(Stage &s: module->stages)
86 GlobalInterfaceValidator().apply(*module);
89 for(Stage &s: module->stages)
93 throw invalid_shader_source(get_diagnostics());
97 for(Stage &s: module->stages)
98 ConstantSpecializer().apply(s, spec_values);
100 for(auto i=module->stages.begin(); i!=module->stages.end(); )
102 OptimizeResult result = optimize(*i);
103 if(result==REDO_PREVIOUS)
104 i = module->stages.begin();
105 else if(result!=REDO_STAGE)
109 LocationAllocator().apply(*module, features);
110 for(Stage &s: module->stages)
116 string Compiler::get_combined_glsl() const
119 throw invalid_operation("Compiler::get_combined_glsl");
123 unsigned source_count = module->source_map.get_count();
124 for(unsigned i=1; i<source_count; ++i)
125 glsl += format("#pragma MSP source(%d, \"%s\")\n", i, module->source_map.get_name(i));
126 for(Stage &s: module->stages)
128 glsl += format("#pragma MSP stage(%s)\n", Stage::get_stage_name(s.type));
129 glsl += Formatter().apply(s);
136 vector<Stage::Type> Compiler::get_stages() const
138 vector<Stage::Type> stage_types;
139 stage_types.reserve(module->stages.size());
140 for(const Stage &s: module->stages)
141 stage_types.push_back(s.type);
145 string Compiler::get_stage_glsl(Stage::Type stage_type) const
148 throw invalid_operation("Compiler::get_stage_glsl");
149 auto i = find_member(module->stages, stage_type, &Stage::type);
150 if(i!=module->stages.end())
151 return Formatter().apply(*i);
152 throw key_error(Stage::get_stage_name(stage_type));
155 vector<uint32_t> Compiler::get_combined_spirv() const
158 throw invalid_operation("Compiler::get_combined_spirv");
161 return gen.get_code();
164 const map<string, unsigned> &Compiler::get_vertex_attributes() const
167 throw invalid_operation("Compiler::get_vertex_attributes");
168 auto i = find_member(module->stages, Stage::VERTEX, &Stage::type);
169 if(i!=module->stages.end())
171 throw invalid_operation("Compiler::get_vertex_attributes");
174 const map<string, unsigned> &Compiler::get_fragment_outputs() const
177 throw invalid_operation("Compiler::get_fragment_outputs");
178 auto i = find_member(module->stages, Stage::FRAGMENT, &Stage::type);
179 if(i!=module->stages.end())
181 throw invalid_operation("Compiler::get_fragment_outputs");
184 const map<string, unsigned> &Compiler::get_texture_bindings() const
187 throw invalid_operation("Compiler::get_texture_bindings");
188 return module->shared.texture_bindings;
191 const map<string, unsigned> &Compiler::get_uniform_block_bindings() const
194 throw invalid_operation("Compiler::get_uniform_block_bindings");
195 return module->shared.uniform_block_bindings;
198 unsigned Compiler::get_n_clip_distances() const
201 throw invalid_operation("Compiler::get_n_clip_distances");
202 auto i = find_member(module->stages, Stage::VERTEX, &Stage::type);
203 return (i!=module->stages.end() ? i->n_clip_distances : 0);
206 const SourceMap &Compiler::get_source_map() const
208 return module->source_map;
211 string Compiler::get_stage_debug(Stage::Type stage_type) const
213 auto i = find_member(module->stages, stage_type, &Stage::type);
214 if(i!=module->stages.end())
215 return DumpTree().apply(*i);
216 throw key_error(Stage::get_stage_name(stage_type));
219 string Compiler::get_diagnostics() const
222 for(const Stage &s: module->stages)
223 for(const Diagnostic &d: s.diagnostics)
224 if(d.source!=INTERNAL_SOURCE)
225 append(combined, "\n", format("%s:%d: %s", module->source_map.get_name(d.source), d.line, d.message));
229 void Compiler::append_module(const Module &mod, ModuleCache &mod_cache)
231 module->source_map.merge_from(mod.source_map);
233 vector<Import *> imports;
234 for(const RefPtr<Statement> &s: mod.shared.content.body)
235 if(Import *imp = dynamic_cast<Import *>(s.get()))
236 imports.push_back(imp);
237 for(Import *i: imports)
238 import(mod_cache, i->module);
240 append_stage(mod.shared);
241 for(const Stage &s: mod.stages)
245 void Compiler::append_stage(const Stage &stage)
248 if(stage.type==Stage::SHARED)
249 target = &module->shared;
252 auto i = find_if(module->stages, [&stage](const Stage &s){ return s.type>=stage.type; });
253 if(i==module->stages.end() || i->type>stage.type)
255 auto j = module->stages.insert(i, stage.type);
256 if(i!=module->stages.end())
259 if(i!=module->stages.begin())
266 if(stage.required_features.glsl_version>target->required_features.glsl_version)
267 target->required_features.glsl_version = stage.required_features.glsl_version;
268 for(const RefPtr<Statement> &s: stage.content.body)
269 if(!dynamic_cast<Import *>(s.get()))
270 target->content.body.push_back(s);
273 void Compiler::import(ModuleCache &mod_cache, const string &name)
275 if(find(imported_names, name)!=imported_names.end())
277 imported_names.push_back(name);
279 append_module(mod_cache.get_module(name), mod_cache);
282 void Compiler::generate(Stage &stage)
284 stage.required_features.target_api = features.target_api;
285 if(module->shared.required_features.glsl_version>stage.required_features.glsl_version)
286 stage.required_features.glsl_version = module->shared.required_features.glsl_version;
288 inject_block(stage.content, module->shared.content);
289 if(const Stage *builtins = get_builtins(stage.type))
290 inject_block(stage.content, builtins->content);
291 if(const Stage *builtins = get_builtins(Stage::SHARED))
292 inject_block(stage.content, builtins->content);
294 // Initial resolving pass
297 /* All variables local to a stage have been resolved. Resolve non-local
298 variables through interfaces. */
299 InterfaceGenerator().apply(stage);
300 resolve(stage, RESOLVE_BLOCKS|RESOLVE_TYPES|RESOLVE_VARIABLES);
304 bool Compiler::resolve(Stage &stage, unsigned &flags, unsigned bit)
310 return T().apply(stage);
313 void Compiler::resolve(Stage &stage, unsigned flags)
317 if(resolve<BlockHierarchyResolver>(stage, flags, RESOLVE_BLOCKS))
319 else if(resolve<TypeResolver>(stage, flags, RESOLVE_TYPES))
320 flags |= RESOLVE_BLOCKS|RESOLVE_VARIABLES|RESOLVE_EXPRESSIONS;
321 else if(resolve<VariableResolver>(stage, flags, RESOLVE_VARIABLES))
322 flags |= RESOLVE_EXPRESSIONS;
323 else if(resolve<FunctionResolver>(stage, flags, RESOLVE_FUNCTIONS))
324 flags |= RESOLVE_EXPRESSIONS;
325 else if(resolve<ExpressionResolver>(stage, flags, RESOLVE_EXPRESSIONS))
326 flags |= RESOLVE_VARIABLES|RESOLVE_FUNCTIONS;
330 void Compiler::validate(Stage &stage)
332 DeclarationValidator().apply(stage);
333 IdentifierValidator().apply(stage);
334 ReferenceValidator().apply(stage);
335 ExpressionValidator().apply(stage);
336 FlowControlValidator().apply(stage);
337 StageInterfaceValidator().apply(stage);
340 bool Compiler::check_errors(Stage &stage)
342 stable_sort(stage.diagnostics, &diagnostic_line_order);
343 return !any_of(stage.diagnostics.begin(), stage.diagnostics.end(),
344 [](const Diagnostic &d){ return d.severity==Diagnostic::ERR; });
347 bool Compiler::diagnostic_line_order(const Diagnostic &diag1, const Diagnostic &diag2)
349 if(diag1.provoking_source!=diag2.provoking_source)
351 // Sort builtins first and imported modules according to import order.
352 if(diag1.provoking_source<=BUILTIN_SOURCE)
353 return diag1.provoking_source<diag2.provoking_source;
354 else if(diag2.provoking_source<=BUILTIN_SOURCE)
357 return diag1.provoking_source>diag2.provoking_source;
359 return diag1.provoking_line<diag2.provoking_line;
362 Compiler::OptimizeResult Compiler::optimize(Stage &stage)
364 if(ConstantFolder().apply(stage))
365 resolve(stage, RESOLVE_EXPRESSIONS);
366 ConstantConditionEliminator().apply(stage);
368 bool any_inlined = false;
369 if(FunctionInliner().apply(stage))
371 resolve(stage, RESOLVE_TYPES|RESOLVE_VARIABLES|RESOLVE_FUNCTIONS|RESOLVE_EXPRESSIONS);
374 if(AggregateDismantler().apply(stage))
376 resolve(stage, RESOLVE_TYPES|RESOLVE_VARIABLES|RESOLVE_FUNCTIONS|RESOLVE_EXPRESSIONS);
379 if(ExpressionInliner().apply(stage))
381 resolve(stage, RESOLVE_VARIABLES|RESOLVE_FUNCTIONS|RESOLVE_EXPRESSIONS);
385 /* Removing variables or functions may cause things from the previous stage
387 bool any_removed = UnreachableCodeRemover().apply(stage);
388 any_removed |= UnusedVariableRemover().apply(stage);
389 any_removed |= UnusedFunctionRemover().apply(stage);
390 any_removed |= UnusedTypeRemover().apply(stage);
392 return any_removed ? REDO_PREVIOUS : any_inlined ? REDO_STAGE : NEXT_STAGE;
395 void Compiler::finalize(Stage &stage, Mode mode)
399 LegacyConverter().apply(stage, features);
400 resolve(stage, RESOLVE_VARIABLES|RESOLVE_FUNCTIONS);
401 PrecisionConverter().apply(stage);
404 StructOrganizer().apply(stage);
406 // Collect bindings from all stages into the shared stage's maps
407 module->shared.texture_bindings.insert(stage.texture_bindings.begin(), stage.texture_bindings.end());
408 module->shared.uniform_block_bindings.insert(stage.uniform_block_bindings.begin(), stage.uniform_block_bindings.end());
411 void Compiler::inject_block(Block &target, const Block &source)
413 auto insert_point = target.body.begin();
414 for(const RefPtr<Statement> &s: source.body)
415 target.body.insert(insert_point, s->clone());