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"
26 Compiler::Compiler(const Features &f):
35 void Compiler::clear()
38 module = new Module();
39 imported_names.clear();
40 module->source_map.set_name(0, "<generated>");
43 void Compiler::set_source(const string &source, const string &src_name)
46 imported_names.push_back(src_name);
47 ModuleCache mod_cache(0);
48 append_module(mod_cache.add_module(source, src_name), mod_cache);
51 void Compiler::load_source(IO::Base &io, DataFile::Collection *res, const string &src_name)
54 imported_names.push_back(src_name);
55 ModuleCache mod_cache(res);
56 append_module(mod_cache.add_module(io, src_name), mod_cache);
59 void Compiler::load_source(IO::Base &io, const string &src_name)
61 load_source(io, 0, src_name);
64 void Compiler::specialize(const map<string, int> &sv)
70 void Compiler::compile(Mode mode)
72 if(specialized && mode!=PROGRAM)
73 throw invalid_operation("Compiler::compile");
75 for(Stage &s: module->stages)
77 ConstantIdAssigner().apply(*module, features);
79 for(Stage &s: module->stages)
81 GlobalInterfaceValidator().apply(*module);
84 for(Stage &s: module->stages)
88 throw invalid_shader_source(get_diagnostics());
92 for(Stage &s: module->stages)
93 ConstantSpecializer().apply(s, spec_values);
95 for(auto i=module->stages.begin(); i!=module->stages.end(); )
97 OptimizeResult result = optimize(*i);
98 if(result==REDO_PREVIOUS)
99 i = module->stages.begin();
100 else if(result!=REDO_STAGE)
104 LocationAllocator().apply(*module, features);
105 for(Stage &s: module->stages)
111 string Compiler::get_combined_glsl() const
114 throw invalid_operation("Compiler::get_combined_glsl");
118 unsigned source_count = module->source_map.get_count();
119 for(unsigned i=1; i<source_count; ++i)
120 glsl += format("#pragma MSP source(%d, \"%s\")\n", i, module->source_map.get_name(i));
121 for(Stage &s: module->stages)
123 glsl += format("#pragma MSP stage(%s)\n", Stage::get_stage_name(s.type));
124 glsl += Formatter().apply(s);
131 vector<Stage::Type> Compiler::get_stages() const
133 vector<Stage::Type> stage_types;
134 stage_types.reserve(module->stages.size());
135 for(const Stage &s: module->stages)
136 stage_types.push_back(s.type);
140 string Compiler::get_stage_glsl(Stage::Type stage_type) const
143 throw invalid_operation("Compiler::get_stage_glsl");
144 auto i = find_member(module->stages, stage_type, &Stage::type);
145 if(i!=module->stages.end())
146 return Formatter().apply(*i);
147 throw key_error(Stage::get_stage_name(stage_type));
150 vector<uint32_t> Compiler::get_combined_spirv() const
153 throw invalid_operation("Compiler::get_combined_spirv");
156 return gen.get_code();
159 const map<string, unsigned> &Compiler::get_vertex_attributes() const
162 throw invalid_operation("Compiler::get_vertex_attributes");
163 auto i = find_member(module->stages, Stage::VERTEX, &Stage::type);
164 if(i!=module->stages.end())
166 throw invalid_operation("Compiler::get_vertex_attributes");
169 const map<string, unsigned> &Compiler::get_fragment_outputs() const
172 throw invalid_operation("Compiler::get_fragment_outputs");
173 auto i = find_member(module->stages, Stage::FRAGMENT, &Stage::type);
174 if(i!=module->stages.end())
176 throw invalid_operation("Compiler::get_fragment_outputs");
179 const map<string, unsigned> &Compiler::get_texture_bindings() const
182 throw invalid_operation("Compiler::get_texture_bindings");
183 return module->shared.texture_bindings;
186 const map<string, unsigned> &Compiler::get_uniform_block_bindings() const
189 throw invalid_operation("Compiler::get_uniform_block_bindings");
190 return module->shared.uniform_block_bindings;
193 unsigned Compiler::get_n_clip_distances() const
196 throw invalid_operation("Compiler::get_n_clip_distances");
197 auto i = find_member(module->stages, Stage::VERTEX, &Stage::type);
198 return (i!=module->stages.end() ? i->n_clip_distances : 0);
201 const SourceMap &Compiler::get_source_map() const
203 return module->source_map;
206 string Compiler::get_stage_debug(Stage::Type stage_type) const
208 auto i = find_member(module->stages, stage_type, &Stage::type);
209 if(i!=module->stages.end())
210 return DumpTree().apply(*i);
211 throw key_error(Stage::get_stage_name(stage_type));
214 string Compiler::get_diagnostics() const
217 for(const Stage &s: module->stages)
218 for(const Diagnostic &d: s.diagnostics)
219 if(d.source!=INTERNAL_SOURCE)
220 append(combined, "\n", format("%s:%d: %s", module->source_map.get_name(d.source), d.line, d.message));
224 void Compiler::append_module(const Module &mod, ModuleCache &mod_cache)
226 module->source_map.merge_from(mod.source_map);
228 vector<Import *> imports;
229 for(const RefPtr<Statement> &s: mod.shared.content.body)
230 if(Import *imp = dynamic_cast<Import *>(s.get()))
231 imports.push_back(imp);
232 for(Import *i: imports)
233 import(mod_cache, i->module);
235 append_stage(mod.shared);
236 for(const Stage &s: mod.stages)
240 void Compiler::append_stage(const Stage &stage)
243 if(stage.type==Stage::SHARED)
244 target = &module->shared;
247 auto i = find_if(module->stages, [&stage](const Stage &s){ return s.type>=stage.type; });
248 if(i==module->stages.end() || i->type>stage.type)
250 auto j = module->stages.insert(i, stage.type);
251 if(i!=module->stages.end())
254 if(i!=module->stages.begin())
261 if(stage.required_features.glsl_version>target->required_features.glsl_version)
262 target->required_features.glsl_version = stage.required_features.glsl_version;
263 for(const RefPtr<Statement> &s: stage.content.body)
264 if(!dynamic_cast<Import *>(s.get()))
265 target->content.body.push_back(s);
268 void Compiler::import(ModuleCache &mod_cache, const string &name)
270 if(find(imported_names, name)!=imported_names.end())
272 imported_names.push_back(name);
274 append_module(mod_cache.get_module(name), mod_cache);
277 void Compiler::generate(Stage &stage)
279 stage.required_features.target_api = features.target_api;
280 if(module->shared.required_features.glsl_version>stage.required_features.glsl_version)
281 stage.required_features.glsl_version = module->shared.required_features.glsl_version;
283 inject_block(stage.content, module->shared.content);
284 if(const Stage *builtins = get_builtins(stage.type))
285 inject_block(stage.content, builtins->content);
286 if(const Stage *builtins = get_builtins(Stage::SHARED))
287 inject_block(stage.content, builtins->content);
289 // Initial resolving pass
292 /* All variables local to a stage have been resolved. Resolve non-local
293 variables through interfaces. */
294 InterfaceGenerator().apply(stage);
295 resolve(stage, RESOLVE_BLOCKS|RESOLVE_TYPES|RESOLVE_VARIABLES);
299 bool Compiler::resolve(Stage &stage, unsigned &flags, unsigned bit)
305 return T().apply(stage);
308 void Compiler::resolve(Stage &stage, unsigned flags)
312 if(resolve<BlockHierarchyResolver>(stage, flags, RESOLVE_BLOCKS))
314 else if(resolve<TypeResolver>(stage, flags, RESOLVE_TYPES))
315 flags |= RESOLVE_BLOCKS|RESOLVE_VARIABLES|RESOLVE_EXPRESSIONS;
316 else if(resolve<VariableResolver>(stage, flags, RESOLVE_VARIABLES))
317 flags |= RESOLVE_EXPRESSIONS;
318 else if(resolve<FunctionResolver>(stage, flags, RESOLVE_FUNCTIONS))
319 flags |= RESOLVE_EXPRESSIONS;
320 else if(resolve<ExpressionResolver>(stage, flags, RESOLVE_EXPRESSIONS))
321 flags |= RESOLVE_VARIABLES|RESOLVE_FUNCTIONS;
325 void Compiler::validate(Stage &stage)
327 DeclarationValidator().apply(stage);
328 IdentifierValidator().apply(stage);
329 ReferenceValidator().apply(stage);
330 ExpressionValidator().apply(stage);
331 FlowControlValidator().apply(stage);
332 StageInterfaceValidator().apply(stage);
335 bool Compiler::check_errors(Stage &stage)
337 stable_sort(stage.diagnostics, &diagnostic_line_order);
338 return !any_of(stage.diagnostics.begin(), stage.diagnostics.end(),
339 [](const Diagnostic &d){ return d.severity==Diagnostic::ERR; });
342 bool Compiler::diagnostic_line_order(const Diagnostic &diag1, const Diagnostic &diag2)
344 if(diag1.provoking_source!=diag2.provoking_source)
346 // Sort builtins first and imported modules according to import order.
347 if(diag1.provoking_source<=BUILTIN_SOURCE)
348 return diag1.provoking_source<diag2.provoking_source;
349 else if(diag2.provoking_source<=BUILTIN_SOURCE)
352 return diag1.provoking_source>diag2.provoking_source;
354 return diag1.provoking_line<diag2.provoking_line;
357 Compiler::OptimizeResult Compiler::optimize(Stage &stage)
359 if(ConstantFolder().apply(stage))
360 resolve(stage, RESOLVE_EXPRESSIONS);
361 ConstantConditionEliminator().apply(stage);
363 bool any_inlined = false;
364 if(FunctionInliner().apply(stage))
366 resolve(stage, RESOLVE_TYPES|RESOLVE_VARIABLES|RESOLVE_FUNCTIONS|RESOLVE_EXPRESSIONS);
369 if(AggregateDismantler().apply(stage))
371 resolve(stage, RESOLVE_TYPES|RESOLVE_VARIABLES|RESOLVE_FUNCTIONS|RESOLVE_EXPRESSIONS);
374 if(ExpressionInliner().apply(stage))
376 resolve(stage, RESOLVE_VARIABLES|RESOLVE_FUNCTIONS|RESOLVE_EXPRESSIONS);
380 /* Removing variables or functions may cause things from the previous stage
382 bool any_removed = UnreachableCodeRemover().apply(stage);
383 any_removed |= UnusedVariableRemover().apply(stage);
384 any_removed |= UnusedFunctionRemover().apply(stage);
385 any_removed |= UnusedTypeRemover().apply(stage);
387 return any_removed ? REDO_PREVIOUS : any_inlined ? REDO_STAGE : NEXT_STAGE;
390 void Compiler::finalize(Stage &stage, Mode mode)
394 StructuralFeatureConverter().apply(stage, features);
395 QualifierConverter().apply(stage, features);
396 resolve(stage, RESOLVE_VARIABLES|RESOLVE_FUNCTIONS);
397 PrecisionConverter().apply(stage);
400 StructOrganizer().apply(stage);
402 // Collect bindings from all stages into the shared stage's maps
403 module->shared.texture_bindings.insert(stage.texture_bindings.begin(), stage.texture_bindings.end());
404 module->shared.uniform_block_bindings.insert(stage.uniform_block_bindings.begin(), stage.uniform_block_bindings.end());
407 void Compiler::inject_block(Block &target, const Block &source)
409 auto insert_point = target.body.begin();
410 for(const RefPtr<Statement> &s: source.body)
411 target.body.insert(insert_point, s->clone());