]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/compiler.cpp
Also allocate a buffer when mapping it
[libs/gl.git] / source / glsl / compiler.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/strings/format.h>
3 #include <msp/strings/utils.h>
4 #include "builtin.h"
5 #include "compiler.h"
6 #include "debug.h"
7 #include "error.h"
8 #include "finalize.h"
9 #include "generate.h"
10 #include "glsl_error.h"
11 #include "optimize.h"
12 #include "output.h"
13 #include "resolve.h"
14 #include "resources.h"
15 #include "validate.h"
16
17 #undef interface
18
19 using namespace std;
20
21 namespace Msp {
22 namespace GL {
23 namespace SL {
24
25 Compiler::Compiler():
26         features(Features::from_context()),
27         module(0),
28         compiled(false),
29         specialized(false)
30 { }
31
32 Compiler::Compiler(const Features &f):
33         features(f),
34         module(0),
35         compiled(false),
36         specialized(false)
37 { }
38
39 Compiler::~Compiler()
40 {
41         delete module;
42 }
43
44 void Compiler::clear()
45 {
46         delete module;
47         module = new Module();
48         imported_names.clear();
49         module->source_map.set_name(0, "<generated>");
50 }
51
52 void Compiler::set_source(const string &source, const string &src_name)
53 {
54         clear();
55         Parser parser;
56         imported_names.push_back(src_name);
57         append_module(parser.parse(source, src_name, 1), 0);
58 }
59
60 void Compiler::load_source(IO::Base &io, DataFile::Collection *res, const string &src_name)
61 {
62         clear();
63         Parser parser;
64         imported_names.push_back(src_name);
65         append_module(parser.parse(io, src_name, 1), res);
66 }
67
68 void Compiler::load_source(IO::Base &io, const string &src_name)
69 {
70         load_source(io, 0, src_name);
71 }
72
73 void Compiler::specialize(const map<string, int> &sv)
74 {
75         specialized = true;
76         spec_values = sv;
77 }
78
79 void Compiler::compile(Mode mode)
80 {
81         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
82                 generate(*i);
83         ConstantIdAssigner().apply(*module, features);
84
85         bool valid = true;
86         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
87                 if(!validate(*i))
88                         valid = false;
89         if(!valid)
90                 throw invalid_shader_source(get_diagnostics());
91
92         if(mode==PROGRAM && specialized)
93         {
94                 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
95                         ConstantSpecializer().apply(*i, spec_values);
96         }
97         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
98         {
99                 OptimizeResult result = optimize(*i);
100                 if(result==REDO_PREVIOUS)
101                         i = module->stages.begin();
102                 else if(result!=REDO_STAGE)
103                         ++i;
104         }
105         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
106                 finalize(*i, mode);
107
108         compiled = true;
109 }
110
111 string Compiler::get_combined_glsl() const
112 {
113         if(!compiled)
114                 throw invalid_operation("Compiler::get_combined_glsl");
115
116         string glsl;
117
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(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
122         {
123                 glsl += format("#pragma MSP stage(%s)\n", Stage::get_stage_name(i->type));
124                 glsl += Formatter().apply(*i);
125                 glsl += '\n';
126         }
127
128         return glsl;
129 }
130
131 vector<Stage::Type> Compiler::get_stages() const
132 {
133         vector<Stage::Type> stage_types;
134         stage_types.reserve(module->stages.size());
135         for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
136                 stage_types.push_back(i->type);
137         return stage_types;
138 }
139
140 string Compiler::get_stage_glsl(Stage::Type stage_type) const
141 {
142         if(!compiled)
143                 throw invalid_operation("Compiler::get_stage_glsl");
144         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
145                 if(i->type==stage_type)
146                         return Formatter().apply(*i);
147         throw key_error(Stage::get_stage_name(stage_type));
148 }
149
150 const map<string, unsigned> &Compiler::get_vertex_attributes() const
151 {
152         if(!compiled)
153                 throw invalid_operation("Compiler::get_vertex_attributes");
154         for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
155                 if(i->type==Stage::VERTEX)
156                         return i->locations;
157         throw invalid_operation("Compiler::get_vertex_attributes");
158 }
159
160 const map<string, unsigned> &Compiler::get_fragment_outputs() const
161 {
162         if(!compiled)
163                 throw invalid_operation("Compiler::get_fragment_outputs");
164         for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
165                 if(i->type==Stage::FRAGMENT)
166                         return i->locations;
167         throw invalid_operation("Compiler::get_fragment_outputs");
168 }
169
170 const SourceMap &Compiler::get_source_map() const
171 {
172         return module->source_map;
173 }
174
175 string Compiler::get_stage_debug(Stage::Type stage_type) const
176 {
177         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
178                 if(i->type==stage_type)
179                         return DumpTree().apply(*i);
180         throw key_error(Stage::get_stage_name(stage_type));
181 }
182
183 string Compiler::get_diagnostics() const
184 {
185         string combined;
186         for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
187                 for(vector<Diagnostic>::const_iterator j=i->diagnostics.begin(); j!=i->diagnostics.end(); ++j)
188                         if(j->source!=INTERNAL_SOURCE)
189                                 append(combined, "\n", format("%s:%d: %s", module->source_map.get_name(j->source), j->line, j->message));
190         return combined;
191 }
192
193 void Compiler::append_module(Module &mod, DataFile::Collection *res)
194 {
195         module->source_map.merge_from(mod.source_map);
196
197         vector<Import *> imports;
198         for(NodeList<Statement>::const_iterator i=mod.shared.content.body.begin(); i!=mod.shared.content.body.end(); ++i)
199                 if(Import *imp = dynamic_cast<Import *>(i->get()))
200                         imports.push_back(imp);
201         for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
202                 import(res, (*i)->module);
203         NodeRemover().apply(mod.shared, set<Node *>(imports.begin(), imports.end()));
204
205         append_stage(mod.shared);
206         for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
207                 append_stage(*i);
208 }
209
210 void Compiler::append_stage(Stage &stage)
211 {
212         Stage *target = 0;
213         if(stage.type==Stage::SHARED)
214                 target = &module->shared;
215         else
216         {
217                 list<Stage>::iterator i;
218                 for(i=module->stages.begin(); (i!=module->stages.end() && i->type<stage.type); ++i) ;
219                 if(i==module->stages.end() || i->type>stage.type)
220                 {
221                         list<Stage>::iterator j = module->stages.insert(i, stage.type);
222                         if(i!=module->stages.end())
223                                 i->previous = &*j;
224                         i = j;
225                         if(i!=module->stages.begin())
226                                 i->previous = &*--j;
227                 }
228
229                 target = &*i;
230         }
231
232         if(stage.required_features.glsl_version>target->required_features.glsl_version)
233                 target->required_features.glsl_version = stage.required_features.glsl_version;
234         for(NodeList<Statement>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
235                 target->content.body.push_back(*i);
236 }
237
238 void Compiler::import(DataFile::Collection *resources, const string &name)
239 {
240         string fn = name+".glsl";
241         if(find(imported_names, fn)!=imported_names.end())
242                 return;
243         imported_names.push_back(fn);
244
245         RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
246         if(!io)
247                 throw runtime_error(format("module %s not found", name));
248         Parser import_parser;
249         append_module(import_parser.parse(*io, fn, module->source_map.get_count()), resources);
250 }
251
252 void Compiler::generate(Stage &stage)
253 {
254         stage.required_features.gl_api = features.gl_api;
255         if(module->shared.required_features.glsl_version>stage.required_features.glsl_version)
256                 stage.required_features.glsl_version = module->shared.required_features.glsl_version;
257
258         inject_block(stage.content, module->shared.content);
259         if(const Stage *builtins = get_builtins(stage.type))
260                 inject_block(stage.content, builtins->content);
261         if(const Stage *builtins = get_builtins(Stage::SHARED))
262                 inject_block(stage.content, builtins->content);
263
264         // Initial resolving pass
265         resolve(stage);
266
267         /* All variables local to a stage have been resolved.  Resolve non-local
268         variables through interfaces. */
269         InterfaceGenerator().apply(stage);
270         resolve(stage, RESOLVE_BLOCKS|RESOLVE_TYPES|RESOLVE_VARIABLES);
271 }
272
273 template<typename T>
274 bool Compiler::resolve(Stage &stage, unsigned &flags, unsigned bit)
275 {
276         if(!(flags&bit))
277                 return false;
278
279         flags &= ~bit;
280         return T().apply(stage);
281 }
282
283 void Compiler::resolve(Stage &stage, unsigned flags)
284 {
285         while(flags)
286         {
287                 if(resolve<BlockHierarchyResolver>(stage, flags, RESOLVE_BLOCKS))
288                         ;
289                 else if(resolve<TypeResolver>(stage, flags, RESOLVE_TYPES))
290                         flags |= RESOLVE_BLOCKS|RESOLVE_VARIABLES|RESOLVE_EXPRESSIONS;
291                 else if(resolve<VariableResolver>(stage, flags, RESOLVE_VARIABLES))
292                         flags |= RESOLVE_EXPRESSIONS;
293                 else if(resolve<FunctionResolver>(stage, flags, RESOLVE_FUNCTIONS))
294                         flags |= RESOLVE_EXPRESSIONS;
295                 else if(resolve<ExpressionResolver>(stage, flags, RESOLVE_EXPRESSIONS))
296                         flags |= RESOLVE_VARIABLES|RESOLVE_FUNCTIONS;
297         }
298 }
299
300 bool Compiler::validate(Stage &stage)
301 {
302         DeclarationValidator().apply(stage);
303         IdentifierValidator().apply(stage);
304         ReferenceValidator().apply(stage);
305         ExpressionValidator().apply(stage);
306
307         stable_sort(stage.diagnostics, &diagnostic_line_order);
308
309         for(vector<Diagnostic>::const_iterator i=stage.diagnostics.begin(); i!=stage.diagnostics.end(); ++i)
310                 if(i->severity==Diagnostic::ERR)
311                         return false;
312
313         return true;
314 }
315
316 bool Compiler::diagnostic_line_order(const Diagnostic &diag1, const Diagnostic &diag2)
317 {
318         if(diag1.provoking_source!=diag2.provoking_source)
319         {
320                 // Sort builtins first and imported modules according to import order.
321                 if(diag1.provoking_source<=BUILTIN_SOURCE)
322                         return diag1.provoking_source<diag2.provoking_source;
323                 else if(diag2.provoking_source<=BUILTIN_SOURCE)
324                         return false;
325                 else
326                         return diag1.provoking_source>diag2.provoking_source;
327         }
328         return diag1.provoking_line<diag2.provoking_line;
329 }
330
331 Compiler::OptimizeResult Compiler::optimize(Stage &stage)
332 {
333         if(ConstantFolder().apply(stage))
334                 resolve(stage, RESOLVE_EXPRESSIONS);
335         ConstantConditionEliminator().apply(stage);
336
337         bool any_inlined = false;
338         if(FunctionInliner().apply(stage))
339         {
340                 resolve(stage, RESOLVE_TYPES|RESOLVE_VARIABLES|RESOLVE_FUNCTIONS|RESOLVE_EXPRESSIONS);
341                 any_inlined = true;
342         }
343         if(ExpressionInliner().apply(stage))
344         {
345                 resolve(stage, RESOLVE_VARIABLES|RESOLVE_FUNCTIONS|RESOLVE_EXPRESSIONS);
346                 any_inlined = true;
347         }
348
349         /* Removing variables or functions may cause things from the previous stage
350         to become unused. */
351         bool any_removed = UnusedVariableRemover().apply(stage);
352         any_removed |= UnusedFunctionRemover().apply(stage);
353         any_removed |= UnusedTypeRemover().apply(stage);
354
355         return any_removed ? REDO_PREVIOUS : any_inlined ? REDO_STAGE : NEXT_STAGE;
356 }
357
358 void Compiler::finalize(Stage &stage, Mode mode)
359 {
360         if(mode==PROGRAM)
361         {
362                 LegacyConverter().apply(stage, features);
363                 resolve(stage, RESOLVE_VARIABLES|RESOLVE_FUNCTIONS);
364                 PrecisionConverter().apply(stage);
365         }
366 }
367
368 void Compiler::inject_block(Block &target, const Block &source)
369 {
370         NodeList<Statement>::iterator insert_point = target.body.begin();
371         for(NodeList<Statement>::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
372                 target.body.insert(insert_point, (*i)->clone());
373 }
374
375 } // namespace SL
376 } // namespace GL
377 } // namespace Msp