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