1 #include <msp/core/algorithm.h>
2 #include <msp/strings/format.h>
3 #include <msp/strings/utils.h>
5 #include "compatibility.h"
10 #include "glsl_error.h"
13 #include "resources.h"
25 features(Features::from_context()),
30 Compiler::Compiler(const Features &f):
41 void Compiler::clear()
44 module = new Module();
45 imported_names.clear();
46 module->source_map.set_name(0, "<generated>");
49 void Compiler::set_source(const string &source, const string &src_name)
53 imported_names.push_back(src_name);
54 append_module(parser.parse(source, src_name, 1), 0);
57 void Compiler::load_source(IO::Base &io, DataFile::Collection *res, const string &src_name)
61 imported_names.push_back(src_name);
62 append_module(parser.parse(io, src_name, 1), res);
65 void Compiler::load_source(IO::Base &io, const string &src_name)
67 load_source(io, 0, src_name);
70 void Compiler::specialize(const map<string, int> &sv)
76 void Compiler::compile(Mode mode)
78 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
82 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
86 throw invalid_shader_source(get_diagnostics());
89 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++n)
91 OptimizeResult result = optimize(*i);
92 if(result==REDO_PREVIOUS)
93 i = module->stages.begin();
94 else if(result!=REDO_STAGE)
97 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
101 string Compiler::get_combined_glsl() const
105 unsigned source_count = module->source_map.get_count();
106 for(unsigned i=1; i<source_count; ++i)
107 glsl += format("#pragma MSP source(%d, \"%s\")\n", i, module->source_map.get_name(i));
108 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
110 glsl += format("#pragma MSP stage(%s)\n", Stage::get_stage_name(i->type));
111 glsl += Formatter().apply(*i, MODULE);
118 vector<Stage::Type> Compiler::get_stages() const
120 vector<Stage::Type> stage_types;
121 stage_types.reserve(module->stages.size());
122 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
123 stage_types.push_back(i->type);
127 string Compiler::get_stage_glsl(Stage::Type stage_type) const
129 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
130 if(i->type==stage_type)
131 return Formatter().apply(*i, PROGRAM);
132 throw key_error(Stage::get_stage_name(stage_type));
135 const map<string, unsigned> &Compiler::get_vertex_attributes() const
137 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
138 if(i->type==Stage::VERTEX)
140 throw invalid_operation("Compiler::get_vertex_attributes");
143 const map<string, unsigned> &Compiler::get_fragment_outputs() const
145 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
146 if(i->type==Stage::FRAGMENT)
148 throw invalid_operation("Compiler::get_fragment_outputs");
151 const SourceMap &Compiler::get_source_map() const
153 return module->source_map;
156 string Compiler::get_stage_debug(Stage::Type stage_type) const
158 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
159 if(i->type==stage_type)
160 return DumpTree().apply(*i);
161 throw key_error(Stage::get_stage_name(stage_type));
164 string Compiler::get_diagnostics() const
167 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
168 for(vector<Diagnostic>::const_iterator j=i->diagnostics.begin(); j!=i->diagnostics.end(); ++j)
169 append(combined, "\n", format("%s:%d: %s", module->source_map.get_name(j->source), j->line, j->message));
173 void Compiler::append_module(Module &mod, DataFile::Collection *res)
175 module->source_map.merge_from(mod.source_map);
177 vector<Import *> imports = NodeGatherer<Import>().apply(mod.shared);
178 for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
179 import(res, (*i)->module);
180 NodeRemover().apply(mod.shared, set<Node *>(imports.begin(), imports.end()));
182 append_stage(mod.shared);
183 for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
187 void Compiler::append_stage(Stage &stage)
190 if(stage.type==Stage::SHARED)
191 target = &module->shared;
194 list<Stage>::iterator i;
195 for(i=module->stages.begin(); (i!=module->stages.end() && i->type<stage.type); ++i) ;
196 if(i==module->stages.end() || i->type>stage.type)
198 list<Stage>::iterator j = module->stages.insert(i, stage.type);
199 if(i!=module->stages.end())
202 if(i!=module->stages.begin())
209 if(stage.required_features.glsl_version>target->required_features.glsl_version)
210 target->required_features.glsl_version = stage.required_features.glsl_version;
211 for(NodeList<Statement>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
212 target->content.body.push_back(*i);
213 DeclarationCombiner().apply(*target);
216 void Compiler::import(DataFile::Collection *resources, const string &name)
218 string fn = name+".glsl";
219 if(find(imported_names, fn)!=imported_names.end())
221 imported_names.push_back(fn);
223 RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
225 throw runtime_error(format("module %s not found", name));
226 Parser import_parser;
227 append_module(import_parser.parse(*io, fn, module->source_map.get_count()), resources);
230 void Compiler::generate(Stage &stage, Mode mode)
232 stage.required_features.gl_api = features.gl_api;
233 if(module->shared.required_features.glsl_version>stage.required_features.glsl_version)
234 stage.required_features.glsl_version = module->shared.required_features.glsl_version;
236 inject_block(stage.content, module->shared.content);
237 if(const Stage *builtins = get_builtins(stage.type))
238 inject_block(stage.content, builtins->content);
239 if(const Stage *builtins = get_builtins(Stage::SHARED))
240 inject_block(stage.content, builtins->content);
242 // Initial resolving pass
243 BlockHierarchyResolver().apply(stage);
244 TypeResolver().apply(stage);
245 FunctionResolver().apply(stage);
246 VariableResolver().apply(stage);
248 /* All variables local to a stage have been resolved. Resolve non-local
249 variables through interfaces. */
250 InterfaceGenerator().apply(stage);
251 TypeResolver().apply(stage);
252 VariableResolver().apply(stage);
253 ExpressionResolver().apply(stage);
255 FunctionResolver().apply(stage);
256 ConstantSpecializer().apply(stage, (mode==PROGRAM && specialized ? &spec_values : 0));
258 LegacyConverter().apply(stage, features);
261 bool Compiler::validate(Stage &stage)
263 TypeValidator().apply(stage);
264 DeclarationValidator().apply(stage);
265 ReferenceValidator().apply(stage);
266 ExpressionValidator().apply(stage);
268 for(vector<Diagnostic>::const_iterator i=stage.diagnostics.begin(); i!=stage.diagnostics.end(); ++i)
269 if(i->severity==Diagnostic::ERR)
275 Compiler::OptimizeResult Compiler::optimize(Stage &stage)
277 ConstantConditionEliminator().apply(stage);
279 bool any_inlined = FunctionInliner().apply(stage);
280 any_inlined |= ExpressionInliner().apply(stage);
283 TypeResolver().apply(stage);
284 VariableResolver().apply(stage);
285 FunctionResolver().apply(stage);
286 ExpressionResolver().apply(stage);
289 /* Removing variables or functions may cause things from the previous stage
291 bool any_removed = UnusedVariableRemover().apply(stage);
292 any_removed |= UnusedFunctionRemover().apply(stage);
293 any_removed |= UnusedTypeRemover().apply(stage);
295 return any_removed ? REDO_PREVIOUS : any_inlined ? REDO_STAGE : NEXT_STAGE;
298 void Compiler::finalize(Stage &stage, Mode mode)
300 if(get_gl_api()==OPENGL_ES2 && mode==PROGRAM)
301 DefaultPrecisionGenerator().apply(stage);
302 else if(mode==MODULE)
303 PrecisionRemover().apply(stage);
306 void Compiler::inject_block(Block &target, const Block &source)
308 NodeList<Statement>::iterator insert_point = target.body.begin();
309 for(NodeList<Statement>::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
310 target.body.insert(insert_point, (*i)->clone());