1 #include <msp/core/algorithm.h>
2 #include <msp/gl/extensions/ext_gpu_shader4.h>
3 #include <msp/strings/format.h>
4 #include "compatibility.h"
11 #include "resources.h"
23 features(Features::from_context()),
28 Compiler::Compiler(const Features &f):
39 void Compiler::clear()
42 module = new Module();
43 imported_names.clear();
44 module->source_map.set_name(0, "<generated>");
47 void Compiler::set_source(const string &source, const string &src_name)
51 imported_names.push_back(src_name);
52 append_module(parser.parse(source, src_name, 1), 0);
55 void Compiler::load_source(IO::Base &io, DataFile::Collection *res, const string &src_name)
59 imported_names.push_back(src_name);
60 append_module(parser.parse(io, src_name, 1), res);
63 void Compiler::load_source(IO::Base &io, const string &src_name)
65 load_source(io, 0, src_name);
68 void Compiler::specialize(const map<string, int> &sv)
74 void Compiler::compile(Mode mode)
76 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
78 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
81 i = module->stages.begin();
85 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
89 string Compiler::get_combined_glsl() const
93 unsigned source_count = module->source_map.get_count();
94 for(unsigned i=1; i<source_count; ++i)
95 glsl += format("#pragma MSP source(%d, \"%s\")\n", i, module->source_map.get_name(i));
96 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
98 glsl += format("#pragma MSP stage(%s)\n", Stage::get_stage_name(i->type));
99 glsl += Formatter().apply(*i, MODULE);
106 vector<Stage::Type> Compiler::get_stages() const
108 vector<Stage::Type> stage_types;
109 stage_types.reserve(module->stages.size());
110 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
111 stage_types.push_back(i->type);
115 string Compiler::get_stage_glsl(Stage::Type stage_type) const
117 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
118 if(i->type==stage_type)
119 return Formatter().apply(*i, PROGRAM);
120 throw key_error(Stage::get_stage_name(stage_type));
123 const map<string, unsigned> &Compiler::get_vertex_attributes() const
125 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
126 if(i->type==Stage::VERTEX)
128 throw invalid_operation("Compiler::get_vertex_attributes");
131 const map<string, unsigned> &Compiler::get_fragment_outputs() const
133 for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
134 if(i->type==Stage::FRAGMENT)
136 throw invalid_operation("Compiler::get_fragment_outputs");
139 const SourceMap &Compiler::get_source_map() const
141 return module->source_map;
144 string Compiler::get_stage_debug(Stage::Type stage_type) const
146 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
147 if(i->type==stage_type)
148 return DumpTree().apply(*i);
149 throw key_error(Stage::get_stage_name(stage_type));
152 void Compiler::append_module(Module &mod, DataFile::Collection *res)
154 module->source_map.merge_from(mod.source_map);
156 vector<Import *> imports = NodeGatherer<Import>().apply(mod.shared);
157 for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
158 import(res, (*i)->module);
159 NodeRemover().apply(mod.shared, set<Node *>(imports.begin(), imports.end()));
161 append_stage(mod.shared);
162 for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
166 void Compiler::append_stage(Stage &stage)
169 if(stage.type==Stage::SHARED)
170 target = &module->shared;
173 list<Stage>::iterator i;
174 for(i=module->stages.begin(); (i!=module->stages.end() && i->type<stage.type); ++i) ;
175 if(i==module->stages.end() || i->type>stage.type)
177 list<Stage>::iterator j = module->stages.insert(i, stage.type);
178 if(i!=module->stages.end())
181 if(i!=module->stages.begin())
188 if(stage.required_features.glsl_version>target->required_features.glsl_version)
189 target->required_features.glsl_version = stage.required_features.glsl_version;
190 for(NodeList<Statement>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
191 target->content.body.push_back(*i);
192 DeclarationCombiner().apply(*target);
195 void Compiler::import(DataFile::Collection *resources, const string &name)
197 string fn = name+".glsl";
198 if(find(imported_names, fn)!=imported_names.end())
200 imported_names.push_back(fn);
202 RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
204 throw runtime_error(format("module %s not found", name));
205 Parser import_parser;
206 append_module(import_parser.parse(*io, fn, module->source_map.get_count()), resources);
209 void Compiler::generate(Stage &stage, Mode mode)
211 stage.required_features.gl_api = features.gl_api;
212 if(module->shared.required_features.glsl_version>stage.required_features.glsl_version)
213 stage.required_features.glsl_version = module->shared.required_features.glsl_version;
214 inject_block(stage.content, module->shared.content);
216 DeclarationReorderer().apply(stage);
218 // Initial resolving pass
219 BlockHierarchyResolver().apply(stage);
220 FunctionResolver().apply(stage);
221 VariableResolver().apply(stage);
223 /* All variables local to a stage have been resolved. Resolve non-local
224 variables through interfaces. */
225 InterfaceGenerator().apply(stage);
226 VariableResolver().apply(stage);
228 DeclarationReorderer().apply(stage);
229 FunctionResolver().apply(stage);
230 ConstantSpecializer().apply(stage, (mode==PROGRAM && specialized ? &spec_values : 0));
232 LegacyConverter().apply(stage, features);
235 bool Compiler::optimize(Stage &stage)
237 ConstantConditionEliminator().apply(stage);
239 FunctionInliner().apply(stage);
240 BlockHierarchyResolver().apply(stage);
241 VariableResolver().apply(stage);
243 /* Removing variables or functions may cause things from the previous stage
245 bool result = UnusedVariableRemover().apply(stage);
246 result |= UnusedFunctionRemover().apply(stage);
251 void Compiler::finalize(Stage &stage, Mode mode)
253 if(get_gl_api()==OPENGL_ES2 && mode==PROGRAM)
254 DefaultPrecisionGenerator().apply(stage);
255 else if(mode==MODULE)
256 PrecisionRemover().apply(stage);
259 void Compiler::inject_block(Block &target, const Block &source)
261 NodeList<Statement>::iterator insert_point = target.body.begin();
262 for(NodeList<Statement>::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
263 target.body.insert(insert_point, (*i)->clone());