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 const SourceMap &Compiler::get_source_map() const
200 return module->source_map;
203 string Compiler::get_stage_debug(Stage::Type stage_type) const
205 auto i = find_member(module->stages, stage_type, &Stage::type);
206 if(i!=module->stages.end())
207 return DumpTree().apply(*i);
208 throw key_error(Stage::get_stage_name(stage_type));
211 string Compiler::get_diagnostics() const
214 for(const Stage &s: module->stages)
215 for(const Diagnostic &d: s.diagnostics)
216 if(d.source!=INTERNAL_SOURCE)
217 append(combined, "\n", format("%s:%d: %s", module->source_map.get_name(d.source), d.line, d.message));
221 void Compiler::append_module(const Module &mod, ModuleCache &mod_cache)
223 module->source_map.merge_from(mod.source_map);
225 vector<Import *> imports;
226 for(const RefPtr<Statement> &s: mod.shared.content.body)
227 if(Import *imp = dynamic_cast<Import *>(s.get()))
228 imports.push_back(imp);
229 for(Import *i: imports)
230 import(mod_cache, i->module);
232 append_stage(mod.shared);
233 for(const Stage &s: mod.stages)
237 void Compiler::append_stage(const Stage &stage)
240 if(stage.type==Stage::SHARED)
241 target = &module->shared;
244 auto i = find_if(module->stages, [&stage](const Stage &s){ return s.type>=stage.type; });
245 if(i==module->stages.end() || i->type>stage.type)
247 auto j = module->stages.insert(i, stage.type);
248 if(i!=module->stages.end())
251 if(i!=module->stages.begin())
258 if(stage.required_features.glsl_version>target->required_features.glsl_version)
259 target->required_features.glsl_version = stage.required_features.glsl_version;
260 for(const RefPtr<Statement> &s: stage.content.body)
261 if(!dynamic_cast<Import *>(s.get()))
262 target->content.body.push_back(s);
265 void Compiler::import(ModuleCache &mod_cache, const string &name)
267 if(find(imported_names, name)!=imported_names.end())
269 imported_names.push_back(name);
271 append_module(mod_cache.get_module(name), mod_cache);
274 void Compiler::generate(Stage &stage)
276 stage.required_features.target_api = features.target_api;
277 if(module->shared.required_features.glsl_version>stage.required_features.glsl_version)
278 stage.required_features.glsl_version = module->shared.required_features.glsl_version;
280 inject_block(stage.content, module->shared.content);
281 if(const Stage *builtins = get_builtins(stage.type))
282 inject_block(stage.content, builtins->content);
283 if(const Stage *builtins = get_builtins(Stage::SHARED))
284 inject_block(stage.content, builtins->content);
286 // Initial resolving pass
289 /* All variables local to a stage have been resolved. Resolve non-local
290 variables through interfaces. */
291 InterfaceGenerator().apply(stage);
292 resolve(stage, RESOLVE_BLOCKS|RESOLVE_TYPES|RESOLVE_VARIABLES);
296 bool Compiler::resolve(Stage &stage, unsigned &flags, unsigned bit)
302 return T().apply(stage);
305 void Compiler::resolve(Stage &stage, unsigned flags)
309 if(resolve<BlockHierarchyResolver>(stage, flags, RESOLVE_BLOCKS))
311 else if(resolve<TypeResolver>(stage, flags, RESOLVE_TYPES))
312 flags |= RESOLVE_BLOCKS|RESOLVE_VARIABLES|RESOLVE_EXPRESSIONS;
313 else if(resolve<VariableResolver>(stage, flags, RESOLVE_VARIABLES))
314 flags |= RESOLVE_EXPRESSIONS;
315 else if(resolve<FunctionResolver>(stage, flags, RESOLVE_FUNCTIONS))
316 flags |= RESOLVE_EXPRESSIONS;
317 else if(resolve<ExpressionResolver>(stage, flags, RESOLVE_EXPRESSIONS))
318 flags |= RESOLVE_VARIABLES|RESOLVE_FUNCTIONS;
322 void Compiler::validate(Stage &stage)
324 DeclarationValidator().apply(stage);
325 IdentifierValidator().apply(stage);
326 ReferenceValidator().apply(stage);
327 ExpressionValidator().apply(stage);
328 FlowControlValidator().apply(stage);
329 StageInterfaceValidator().apply(stage);
332 bool Compiler::check_errors(Stage &stage)
334 stable_sort(stage.diagnostics, &diagnostic_line_order);
335 return !any_of(stage.diagnostics.begin(), stage.diagnostics.end(),
336 [](const Diagnostic &d){ return d.severity==Diagnostic::ERR; });
339 bool Compiler::diagnostic_line_order(const Diagnostic &diag1, const Diagnostic &diag2)
341 if(diag1.provoking_source!=diag2.provoking_source)
343 // Sort builtins first and imported modules according to import order.
344 if(diag1.provoking_source<=BUILTIN_SOURCE)
345 return diag1.provoking_source<diag2.provoking_source;
346 else if(diag2.provoking_source<=BUILTIN_SOURCE)
349 return diag1.provoking_source>diag2.provoking_source;
351 return diag1.provoking_line<diag2.provoking_line;
354 Compiler::OptimizeResult Compiler::optimize(Stage &stage)
356 if(ConstantFolder().apply(stage))
357 resolve(stage, RESOLVE_EXPRESSIONS);
358 ConstantConditionEliminator().apply(stage);
360 bool any_inlined = false;
361 if(FunctionInliner().apply(stage))
363 resolve(stage, RESOLVE_TYPES|RESOLVE_VARIABLES|RESOLVE_FUNCTIONS|RESOLVE_EXPRESSIONS);
366 if(AggregateDismantler().apply(stage))
368 resolve(stage, RESOLVE_TYPES|RESOLVE_VARIABLES|RESOLVE_FUNCTIONS|RESOLVE_EXPRESSIONS);
371 if(ExpressionInliner().apply(stage))
373 resolve(stage, RESOLVE_VARIABLES|RESOLVE_FUNCTIONS|RESOLVE_EXPRESSIONS);
377 /* Removing variables or functions may cause things from the previous stage
379 bool any_removed = UnreachableCodeRemover().apply(stage);
380 any_removed |= UnusedVariableRemover().apply(stage);
381 any_removed |= UnusedFunctionRemover().apply(stage);
382 any_removed |= UnusedTypeRemover().apply(stage);
384 return any_removed ? REDO_PREVIOUS : any_inlined ? REDO_STAGE : NEXT_STAGE;
387 void Compiler::finalize(Stage &stage, Mode mode)
391 LegacyConverter().apply(stage, features);
392 resolve(stage, RESOLVE_VARIABLES|RESOLVE_FUNCTIONS);
393 PrecisionConverter().apply(stage);
396 StructOrganizer().apply(stage);
398 // Collect bindings from all stages into the shared stage's maps
399 module->shared.texture_bindings.insert(stage.texture_bindings.begin(), stage.texture_bindings.end());
400 module->shared.uniform_block_bindings.insert(stage.uniform_block_bindings.begin(), stage.uniform_block_bindings.end());
403 void Compiler::inject_block(Block &target, const Block &source)
405 auto insert_point = target.body.begin();
406 for(const RefPtr<Statement> &s: source.body)
407 target.body.insert(insert_point, s->clone());