1 #include <msp/core/algorithm.h>
2 #include <msp/strings/format.h>
3 #include <msp/strings/utils.h>
10 #include "glsl_error.h"
11 #include "modulecache.h"
27 features(Features::from_context()),
33 Compiler::Compiler(const Features &f):
45 void Compiler::clear()
48 module = new Module();
49 imported_names.clear();
50 module->source_map.set_name(0, "<generated>");
53 void Compiler::set_source(const string &source, const string &src_name)
56 imported_names.push_back(src_name);
57 ModuleCache mod_cache(0);
58 append_module(mod_cache.add_module(source, src_name), mod_cache);
61 void Compiler::load_source(IO::Base &io, DataFile::Collection *res, const string &src_name)
64 imported_names.push_back(src_name);
65 ModuleCache mod_cache(res);
66 append_module(mod_cache.add_module(io, src_name), mod_cache);
69 void Compiler::load_source(IO::Base &io, const string &src_name)
71 load_source(io, 0, src_name);
74 void Compiler::specialize(const map<string, int> &sv)
80 void Compiler::compile(Mode mode)
82 if(specialized && mode!=PROGRAM)
83 throw invalid_operation("Compiler::compile");
85 for(Stage &s: module->stages)
87 ConstantIdAssigner().apply(*module, features);
89 for(Stage &s: module->stages)
91 GlobalInterfaceValidator().apply(*module);
94 for(Stage &s: module->stages)
98 throw invalid_shader_source(get_diagnostics());
102 for(Stage &s: module->stages)
103 ConstantSpecializer().apply(s, spec_values);
105 for(auto i=module->stages.begin(); i!=module->stages.end(); )
107 OptimizeResult result = optimize(*i);
108 if(result==REDO_PREVIOUS)
109 i = module->stages.begin();
110 else if(result!=REDO_STAGE)
114 LocationAllocator().apply(*module, features);
115 for(Stage &s: module->stages)
121 string Compiler::get_combined_glsl() const
124 throw invalid_operation("Compiler::get_combined_glsl");
128 unsigned source_count = module->source_map.get_count();
129 for(unsigned i=1; i<source_count; ++i)
130 glsl += format("#pragma MSP source(%d, \"%s\")\n", i, module->source_map.get_name(i));
131 for(Stage &s: module->stages)
133 glsl += format("#pragma MSP stage(%s)\n", Stage::get_stage_name(s.type));
134 glsl += Formatter().apply(s);
141 vector<Stage::Type> Compiler::get_stages() const
143 vector<Stage::Type> stage_types;
144 stage_types.reserve(module->stages.size());
145 for(const Stage &s: module->stages)
146 stage_types.push_back(s.type);
150 string Compiler::get_stage_glsl(Stage::Type stage_type) const
153 throw invalid_operation("Compiler::get_stage_glsl");
154 auto i = find_member(module->stages, stage_type, &Stage::type);
155 if(i!=module->stages.end())
156 return Formatter().apply(*i);
157 throw key_error(Stage::get_stage_name(stage_type));
160 vector<uint32_t> Compiler::get_combined_spirv() const
163 throw invalid_operation("Compiler::get_combined_spirv");
166 return gen.get_code();
169 const map<string, unsigned> &Compiler::get_vertex_attributes() const
172 throw invalid_operation("Compiler::get_vertex_attributes");
173 auto i = find_member(module->stages, Stage::VERTEX, &Stage::type);
174 if(i!=module->stages.end())
176 throw invalid_operation("Compiler::get_vertex_attributes");
179 const map<string, unsigned> &Compiler::get_fragment_outputs() const
182 throw invalid_operation("Compiler::get_fragment_outputs");
183 auto i = find_member(module->stages, Stage::FRAGMENT, &Stage::type);
184 if(i!=module->stages.end())
186 throw invalid_operation("Compiler::get_fragment_outputs");
189 const map<string, unsigned> &Compiler::get_texture_bindings() const
192 throw invalid_operation("Compiler::get_texture_bindings");
193 return module->shared.texture_bindings;
196 const map<string, unsigned> &Compiler::get_uniform_block_bindings() const
199 throw invalid_operation("Compiler::get_uniform_block_bindings");
200 return module->shared.uniform_block_bindings;
203 const SourceMap &Compiler::get_source_map() const
205 return module->source_map;
208 string Compiler::get_stage_debug(Stage::Type stage_type) const
210 auto i = find_member(module->stages, stage_type, &Stage::type);
211 if(i!=module->stages.end())
212 return DumpTree().apply(*i);
213 throw key_error(Stage::get_stage_name(stage_type));
216 string Compiler::get_diagnostics() const
219 for(const Stage &s: module->stages)
220 for(const Diagnostic &d: s.diagnostics)
221 if(d.source!=INTERNAL_SOURCE)
222 append(combined, "\n", format("%s:%d: %s", module->source_map.get_name(d.source), d.line, d.message));
226 void Compiler::append_module(const Module &mod, ModuleCache &mod_cache)
228 module->source_map.merge_from(mod.source_map);
230 vector<Import *> imports;
231 for(const RefPtr<Statement> &s: mod.shared.content.body)
232 if(Import *imp = dynamic_cast<Import *>(s.get()))
233 imports.push_back(imp);
234 for(Import *i: imports)
235 import(mod_cache, i->module);
237 append_stage(mod.shared);
238 for(const Stage &s: mod.stages)
242 void Compiler::append_stage(const Stage &stage)
245 if(stage.type==Stage::SHARED)
246 target = &module->shared;
249 auto i = find_if(module->stages, [&stage](const Stage &s){ return s.type>=stage.type; });
250 if(i==module->stages.end() || i->type>stage.type)
252 auto j = module->stages.insert(i, stage.type);
253 if(i!=module->stages.end())
256 if(i!=module->stages.begin())
263 if(stage.required_features.glsl_version>target->required_features.glsl_version)
264 target->required_features.glsl_version = stage.required_features.glsl_version;
265 for(const RefPtr<Statement> &s: stage.content.body)
266 if(!dynamic_cast<Import *>(s.get()))
267 target->content.body.push_back(s);
270 void Compiler::import(ModuleCache &mod_cache, const string &name)
272 if(find(imported_names, name)!=imported_names.end())
274 imported_names.push_back(name);
276 append_module(mod_cache.get_module(name), mod_cache);
279 void Compiler::generate(Stage &stage)
281 stage.required_features.gl_api = features.gl_api;
282 if(module->shared.required_features.glsl_version>stage.required_features.glsl_version)
283 stage.required_features.glsl_version = module->shared.required_features.glsl_version;
285 inject_block(stage.content, module->shared.content);
286 if(const Stage *builtins = get_builtins(stage.type))
287 inject_block(stage.content, builtins->content);
288 if(const Stage *builtins = get_builtins(Stage::SHARED))
289 inject_block(stage.content, builtins->content);
291 // Initial resolving pass
294 /* All variables local to a stage have been resolved. Resolve non-local
295 variables through interfaces. */
296 InterfaceGenerator().apply(stage);
297 resolve(stage, RESOLVE_BLOCKS|RESOLVE_TYPES|RESOLVE_VARIABLES);
301 bool Compiler::resolve(Stage &stage, unsigned &flags, unsigned bit)
307 return T().apply(stage);
310 void Compiler::resolve(Stage &stage, unsigned flags)
314 if(resolve<BlockHierarchyResolver>(stage, flags, RESOLVE_BLOCKS))
316 else if(resolve<TypeResolver>(stage, flags, RESOLVE_TYPES))
317 flags |= RESOLVE_BLOCKS|RESOLVE_VARIABLES|RESOLVE_EXPRESSIONS;
318 else if(resolve<VariableResolver>(stage, flags, RESOLVE_VARIABLES))
319 flags |= RESOLVE_EXPRESSIONS;
320 else if(resolve<FunctionResolver>(stage, flags, RESOLVE_FUNCTIONS))
321 flags |= RESOLVE_EXPRESSIONS;
322 else if(resolve<ExpressionResolver>(stage, flags, RESOLVE_EXPRESSIONS))
323 flags |= RESOLVE_VARIABLES|RESOLVE_FUNCTIONS;
327 void Compiler::validate(Stage &stage)
329 DeclarationValidator().apply(stage);
330 IdentifierValidator().apply(stage);
331 ReferenceValidator().apply(stage);
332 ExpressionValidator().apply(stage);
333 FlowControlValidator().apply(stage);
334 StageInterfaceValidator().apply(stage);
337 bool Compiler::check_errors(Stage &stage)
339 stable_sort(stage.diagnostics, &diagnostic_line_order);
340 return !any_of(stage.diagnostics.begin(), stage.diagnostics.end(),
341 [](const Diagnostic &d){ return d.severity==Diagnostic::ERR; });
344 bool Compiler::diagnostic_line_order(const Diagnostic &diag1, const Diagnostic &diag2)
346 if(diag1.provoking_source!=diag2.provoking_source)
348 // Sort builtins first and imported modules according to import order.
349 if(diag1.provoking_source<=BUILTIN_SOURCE)
350 return diag1.provoking_source<diag2.provoking_source;
351 else if(diag2.provoking_source<=BUILTIN_SOURCE)
354 return diag1.provoking_source>diag2.provoking_source;
356 return diag1.provoking_line<diag2.provoking_line;
359 Compiler::OptimizeResult Compiler::optimize(Stage &stage)
361 if(ConstantFolder().apply(stage))
362 resolve(stage, RESOLVE_EXPRESSIONS);
363 ConstantConditionEliminator().apply(stage);
365 bool any_inlined = false;
366 if(FunctionInliner().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());