]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/compiler.cpp
Add some documentation to the GLSL compiler
[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 "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 #include "shader.h"
13
14 #undef interface
15
16 using namespace std;
17
18 namespace Msp {
19 namespace GL {
20 namespace SL {
21
22 Compiler::Compiler():
23         features(Features::from_context()),
24         module(0),
25         specialized(false)
26 { }
27
28 Compiler::Compiler(const Features &f):
29         features(f),
30         module(0),
31         specialized(false)
32 { }
33
34 Compiler::~Compiler()
35 {
36         delete module;
37 }
38
39 void Compiler::clear()
40 {
41         delete module;
42         module = new Module();
43         imported_names.clear();
44         module->source_map.set_name(0, "<generated>");
45 }
46
47 void Compiler::set_source(const string &source, const string &src_name)
48 {
49         clear();
50         Parser parser;
51         imported_names.push_back(src_name);
52         append_module(parser.parse(source, src_name, 1), 0);
53 }
54
55 void Compiler::load_source(IO::Base &io, DataFile::Collection *res, const string &src_name)
56 {
57         clear();
58         Parser parser;
59         imported_names.push_back(src_name);
60         append_module(parser.parse(io, src_name, 1), res);
61 }
62
63 void Compiler::load_source(IO::Base &io, const string &src_name)
64 {
65         load_source(io, 0, src_name);
66 }
67
68 void Compiler::specialize(const map<string, int> &sv)
69 {
70         specialized = true;
71         spec_values = sv;
72 }
73
74 void Compiler::compile(Mode mode)
75 {
76         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
77                 generate(*i, mode);
78         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
79         {
80                 if(optimize(*i))
81                         i = module->stages.begin();
82                 else
83                         ++i;
84         }
85         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
86                 finalize(*i, mode);
87 }
88
89 string Compiler::get_combined_glsl() const
90 {
91         string glsl;
92
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)
97         {
98                 glsl += format("#pragma MSP stage(%s)\n", Stage::get_stage_name(i->type));
99                 glsl += Formatter().apply(*i, MODULE);
100                 glsl += '\n';
101         }
102
103         return glsl;
104 }
105
106 vector<Stage::Type> Compiler::get_stages() const
107 {
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);
112         return stage_types;
113 }
114
115 string Compiler::get_stage_glsl(Stage::Type stage_type) const
116 {
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));
121 }
122
123 const map<string, unsigned> &Compiler::get_vertex_attributes() const
124 {
125         for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
126                 if(i->type==Stage::VERTEX)
127                         return i->locations;
128         throw invalid_operation("Compiler::get_vertex_attributes");
129 }
130
131 const map<string, unsigned> &Compiler::get_fragment_outputs() const
132 {
133         for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
134                 if(i->type==Stage::FRAGMENT)
135                         return i->locations;
136         throw invalid_operation("Compiler::get_fragment_outputs");
137 }
138
139 const SourceMap &Compiler::get_source_map() const
140 {
141         return module->source_map;
142 }
143
144 string Compiler::get_stage_debug(Stage::Type stage_type) const
145 {
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));
150 }
151
152 void Compiler::append_module(Module &mod, DataFile::Collection *res)
153 {
154         module->source_map.merge_from(mod.source_map);
155
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()));
160
161         append_stage(mod.shared);
162         for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
163                 append_stage(*i);
164 }
165
166 void Compiler::append_stage(Stage &stage)
167 {
168         Stage *target = 0;
169         if(stage.type==Stage::SHARED)
170                 target = &module->shared;
171         else
172         {
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)
176                 {
177                         list<Stage>::iterator j = module->stages.insert(i, stage.type);
178                         if(i!=module->stages.end())
179                                 i->previous = &*j;
180                         i = j;
181                         if(i!=module->stages.begin())
182                                 i->previous = &*--j;
183                 }
184
185                 target = &*i;
186         }
187
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);
193 }
194
195 void Compiler::import(DataFile::Collection *resources, const string &name)
196 {
197         string fn = name+".glsl";
198         if(find(imported_names, fn)!=imported_names.end())
199                 return;
200         imported_names.push_back(fn);
201
202         RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
203         if(!io)
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);
207 }
208
209 void Compiler::generate(Stage &stage, Mode mode)
210 {
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);
215
216         DeclarationReorderer().apply(stage);
217
218         // Initial resolving pass
219         BlockHierarchyResolver().apply(stage);
220         FunctionResolver().apply(stage);
221         VariableResolver().apply(stage);
222
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);
227
228         DeclarationReorderer().apply(stage);
229         FunctionResolver().apply(stage);
230         ConstantSpecializer().apply(stage, (mode==PROGRAM && specialized ? &spec_values : 0));
231         if(mode==PROGRAM)
232                 LegacyConverter().apply(stage, features);
233 }
234
235 bool Compiler::optimize(Stage &stage)
236 {
237         ConstantConditionEliminator().apply(stage);
238
239         FunctionInliner().apply(stage);
240         BlockHierarchyResolver().apply(stage);
241         VariableResolver().apply(stage);
242
243         /* Removing variables or functions may cause things from the previous stage
244         to become unused. */
245         bool result = UnusedVariableRemover().apply(stage);
246         result |= UnusedFunctionRemover().apply(stage);
247
248         return result;
249 }
250
251 void Compiler::finalize(Stage &stage, Mode mode)
252 {
253         if(get_gl_api()==OPENGL_ES2 && mode==PROGRAM)
254                 DefaultPrecisionGenerator().apply(stage);
255         else if(mode==MODULE)
256                 PrecisionRemover().apply(stage);
257 }
258
259 void Compiler::inject_block(Block &target, const Block &source)
260 {
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());
264 }
265
266 } // namespace SL
267 } // namespace GL
268 } // namespace Msp