]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/compiler.cpp
Remove unnecessary includes
[libs/gl.git] / source / glsl / compiler.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/strings/format.h>
3 #include "builtin.h"
4 #include "compatibility.h"
5 #include "compiler.h"
6 #include "debug.h"
7 #include "error.h"
8 #include "generate.h"
9 #include "optimize.h"
10 #include "output.h"
11 #include "resources.h"
12
13 #undef interface
14
15 using namespace std;
16
17 namespace Msp {
18 namespace GL {
19 namespace SL {
20
21 Compiler::Compiler():
22         features(Features::from_context()),
23         module(0),
24         specialized(false)
25 { }
26
27 Compiler::Compiler(const Features &f):
28         features(f),
29         module(0),
30         specialized(false)
31 { }
32
33 Compiler::~Compiler()
34 {
35         delete module;
36 }
37
38 void Compiler::clear()
39 {
40         delete module;
41         module = new Module();
42         imported_names.clear();
43         module->source_map.set_name(0, "<generated>");
44 }
45
46 void Compiler::set_source(const string &source, const string &src_name)
47 {
48         clear();
49         Parser parser;
50         imported_names.push_back(src_name);
51         append_module(parser.parse(source, src_name, 1), 0);
52 }
53
54 void Compiler::load_source(IO::Base &io, DataFile::Collection *res, const string &src_name)
55 {
56         clear();
57         Parser parser;
58         imported_names.push_back(src_name);
59         append_module(parser.parse(io, src_name, 1), res);
60 }
61
62 void Compiler::load_source(IO::Base &io, const string &src_name)
63 {
64         load_source(io, 0, src_name);
65 }
66
67 void Compiler::specialize(const map<string, int> &sv)
68 {
69         specialized = true;
70         spec_values = sv;
71 }
72
73 void Compiler::compile(Mode mode)
74 {
75         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
76                 generate(*i, mode);
77         unsigned n = 0;
78         for(list<Stage>::iterator i=module->stages.begin(); (i!=module->stages.end() && n<10000); ++n)
79         {
80                 OptimizeResult result = optimize(*i);
81                 if(result==REDO_PREVIOUS)
82                         i = module->stages.begin();
83                 else if(result!=REDO_STAGE)
84                         ++i;
85         }
86         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
87                 finalize(*i, mode);
88 }
89
90 string Compiler::get_combined_glsl() const
91 {
92         string glsl;
93
94         unsigned source_count = module->source_map.get_count();
95         for(unsigned i=1; i<source_count; ++i)
96                 glsl += format("#pragma MSP source(%d, \"%s\")\n", i, module->source_map.get_name(i));
97         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
98         {
99                 glsl += format("#pragma MSP stage(%s)\n", Stage::get_stage_name(i->type));
100                 glsl += Formatter().apply(*i, MODULE);
101                 glsl += '\n';
102         }
103
104         return glsl;
105 }
106
107 vector<Stage::Type> Compiler::get_stages() const
108 {
109         vector<Stage::Type> stage_types;
110         stage_types.reserve(module->stages.size());
111         for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
112                 stage_types.push_back(i->type);
113         return stage_types;
114 }
115
116 string Compiler::get_stage_glsl(Stage::Type stage_type) const
117 {
118         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
119                 if(i->type==stage_type)
120                         return Formatter().apply(*i, PROGRAM);
121         throw key_error(Stage::get_stage_name(stage_type));
122 }
123
124 const map<string, unsigned> &Compiler::get_vertex_attributes() const
125 {
126         for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
127                 if(i->type==Stage::VERTEX)
128                         return i->locations;
129         throw invalid_operation("Compiler::get_vertex_attributes");
130 }
131
132 const map<string, unsigned> &Compiler::get_fragment_outputs() const
133 {
134         for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
135                 if(i->type==Stage::FRAGMENT)
136                         return i->locations;
137         throw invalid_operation("Compiler::get_fragment_outputs");
138 }
139
140 const SourceMap &Compiler::get_source_map() const
141 {
142         return module->source_map;
143 }
144
145 string Compiler::get_stage_debug(Stage::Type stage_type) const
146 {
147         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
148                 if(i->type==stage_type)
149                         return DumpTree().apply(*i);
150         throw key_error(Stage::get_stage_name(stage_type));
151 }
152
153 void Compiler::append_module(Module &mod, DataFile::Collection *res)
154 {
155         module->source_map.merge_from(mod.source_map);
156
157         vector<Import *> imports = NodeGatherer<Import>().apply(mod.shared);
158         for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
159                 import(res, (*i)->module);
160         NodeRemover().apply(mod.shared, set<Node *>(imports.begin(), imports.end()));
161
162         append_stage(mod.shared);
163         for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
164                 append_stage(*i);
165 }
166
167 void Compiler::append_stage(Stage &stage)
168 {
169         Stage *target = 0;
170         if(stage.type==Stage::SHARED)
171                 target = &module->shared;
172         else
173         {
174                 list<Stage>::iterator i;
175                 for(i=module->stages.begin(); (i!=module->stages.end() && i->type<stage.type); ++i) ;
176                 if(i==module->stages.end() || i->type>stage.type)
177                 {
178                         list<Stage>::iterator j = module->stages.insert(i, stage.type);
179                         if(i!=module->stages.end())
180                                 i->previous = &*j;
181                         i = j;
182                         if(i!=module->stages.begin())
183                                 i->previous = &*--j;
184                 }
185
186                 target = &*i;
187         }
188
189         if(target->content.body.empty())
190         {
191                 Stage *builtins = get_builtins(stage.type);
192                 if(builtins && builtins!=&stage)
193                         append_stage(*builtins);
194         }
195
196         if(stage.required_features.glsl_version>target->required_features.glsl_version)
197                 target->required_features.glsl_version = stage.required_features.glsl_version;
198         for(NodeList<Statement>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
199                 target->content.body.push_back(*i);
200         DeclarationCombiner().apply(*target);
201 }
202
203 void Compiler::import(DataFile::Collection *resources, const string &name)
204 {
205         string fn = name+".glsl";
206         if(find(imported_names, fn)!=imported_names.end())
207                 return;
208         imported_names.push_back(fn);
209
210         RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
211         if(!io)
212                 throw runtime_error(format("module %s not found", name));
213         Parser import_parser;
214         append_module(import_parser.parse(*io, fn, module->source_map.get_count()), resources);
215 }
216
217 void Compiler::generate(Stage &stage, Mode mode)
218 {
219         stage.required_features.gl_api = features.gl_api;
220         if(module->shared.required_features.glsl_version>stage.required_features.glsl_version)
221                 stage.required_features.glsl_version = module->shared.required_features.glsl_version;
222         inject_block(stage.content, module->shared.content);
223
224         // Initial resolving pass
225         BlockHierarchyResolver().apply(stage);
226         FunctionResolver().apply(stage);
227         VariableResolver().apply(stage);
228
229         /* All variables local to a stage have been resolved.  Resolve non-local
230         variables through interfaces. */
231         InterfaceGenerator().apply(stage);
232         VariableResolver().apply(stage);
233
234         FunctionResolver().apply(stage);
235         ConstantSpecializer().apply(stage, (mode==PROGRAM && specialized ? &spec_values : 0));
236         if(mode==PROGRAM)
237                 LegacyConverter().apply(stage, features);
238 }
239
240 Compiler::OptimizeResult Compiler::optimize(Stage &stage)
241 {
242         ConstantConditionEliminator().apply(stage);
243
244         bool any_inlined = FunctionInliner().apply(stage);
245         any_inlined |= ExpressionInliner().apply(stage);
246         if(any_inlined)
247         {
248                 VariableResolver().apply(stage);
249                 FunctionResolver().apply(stage);
250         }
251
252         /* Removing variables or functions may cause things from the previous stage
253         to become unused. */
254         bool any_removed = UnusedVariableRemover().apply(stage);
255         any_removed |= UnusedFunctionRemover().apply(stage);
256
257         return any_removed ? REDO_PREVIOUS : any_inlined ? REDO_STAGE : NEXT_STAGE;
258 }
259
260 void Compiler::finalize(Stage &stage, Mode mode)
261 {
262         if(get_gl_api()==OPENGL_ES2 && mode==PROGRAM)
263                 DefaultPrecisionGenerator().apply(stage);
264         else if(mode==MODULE)
265                 PrecisionRemover().apply(stage);
266 }
267
268 void Compiler::inject_block(Block &target, const Block &source)
269 {
270         NodeList<Statement>::iterator insert_point = target.body.begin();
271         for(NodeList<Statement>::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
272                 target.body.insert(insert_point, (*i)->clone());
273 }
274
275 } // namespace SL
276 } // namespace GL
277 } // namespace Msp