]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/compiler.cpp
Reduce coupling between the GLSL compiler and the graphics engine
[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 "error.h"
7 #include "generate.h"
8 #include "optimize.h"
9 #include "output.h"
10 #include "resources.h"
11 #include "shader.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 { }
25
26 Compiler::Compiler(const Features &f):
27         features(f),
28         module(0)
29 { }
30
31 Compiler::~Compiler()
32 {
33         delete module;
34 }
35
36 void Compiler::clear()
37 {
38         delete module;
39         module = new Module();
40         imported_names.clear();
41         module->source_map.set_name(0, "<generated>");
42 }
43
44 void Compiler::set_source(const string &source, const string &src_name)
45 {
46         clear();
47         Parser parser;
48         imported_names.push_back(src_name);
49         append_module(parser.parse(source, src_name, 1), 0);
50 }
51
52 void Compiler::load_source(IO::Base &io, DataFile::Collection *res, const string &src_name)
53 {
54         clear();
55         Parser parser;
56         imported_names.push_back(src_name);
57         append_module(parser.parse(io, src_name, 1), res);
58 }
59
60 void Compiler::load_source(IO::Base &io, const string &src_name)
61 {
62         load_source(io, 0, src_name);
63 }
64
65 void Compiler::compile(Mode mode)
66 {
67         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
68                 generate(*i, mode);
69         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
70         {
71                 if(optimize(*i))
72                         i = module->stages.begin();
73                 else
74                         ++i;
75         }
76         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
77                 finalize(*i, mode);
78 }
79
80 string Compiler::get_combined_glsl() const
81 {
82         string glsl;
83
84         unsigned source_count = module->source_map.get_count();
85         for(unsigned i=1; i<source_count; ++i)
86                 glsl += format("#pragma MSP source(%d, \"%s\")\n", i, module->source_map.get_name(i));
87         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
88         {
89                 glsl += format("#pragma MSP stage(%s)\n", Stage::get_stage_name(i->type));
90                 glsl += Formatter().apply(*i, MODULE);
91                 glsl += '\n';
92         }
93
94         return glsl;
95 }
96
97 vector<Stage::Type> Compiler::get_stages() const
98 {
99         vector<Stage::Type> stage_types;
100         stage_types.reserve(module->stages.size());
101         for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
102                 stage_types.push_back(i->type);
103         return stage_types;
104 }
105
106 string Compiler::get_stage_glsl(Stage::Type stage_type) const
107 {
108         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
109                 if(i->type==stage_type)
110                         return Formatter().apply(*i, PROGRAM);
111         throw key_error(Stage::get_stage_name(stage_type));
112 }
113
114 const map<string, unsigned> &Compiler::get_vertex_attributes() const
115 {
116         for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
117                 if(i->type==Stage::VERTEX)
118                         return i->locations;
119         throw invalid_operation("Compiler::get_vertex_attributes");
120 }
121
122 const map<string, unsigned> &Compiler::get_fragment_outputs() const
123 {
124         for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
125                 if(i->type==Stage::FRAGMENT)
126                         return i->locations;
127         throw invalid_operation("Compiler::get_fragment_outputs");
128 }
129
130 const SourceMap &Compiler::get_source_map() const
131 {
132         return module->source_map;
133 }
134
135 void Compiler::append_module(Module &mod, DataFile::Collection *res)
136 {
137         module->source_map.merge_from(mod.source_map);
138
139         vector<Import *> imports = NodeGatherer<Import>().apply(mod.shared);
140         for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
141                 import(res, (*i)->module);
142         NodeRemover().apply(mod.shared, set<Node *>(imports.begin(), imports.end()));
143
144         append_stage(mod.shared);
145         for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
146                 append_stage(*i);
147 }
148
149 void Compiler::append_stage(Stage &stage)
150 {
151         Stage *target = 0;
152         if(stage.type==Stage::SHARED)
153                 target = &module->shared;
154         else
155         {
156                 list<Stage>::iterator i;
157                 for(i=module->stages.begin(); (i!=module->stages.end() && i->type<stage.type); ++i) ;
158                 if(i==module->stages.end() || i->type>stage.type)
159                 {
160                         list<Stage>::iterator j = module->stages.insert(i, stage.type);
161                         if(i!=module->stages.end())
162                                 i->previous = &*j;
163                         i = j;
164                         if(i!=module->stages.begin())
165                                 i->previous = &*--j;
166                 }
167
168                 target = &*i;
169         }
170
171         if(stage.required_features.glsl_version>target->required_features.glsl_version)
172                 target->required_features.glsl_version = stage.required_features.glsl_version;
173         for(NodeList<Statement>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
174                 target->content.body.push_back(*i);
175         DeclarationCombiner().apply(*target);
176 }
177
178 void Compiler::import(DataFile::Collection *resources, const string &name)
179 {
180         string fn = name+".glsl";
181         if(find(imported_names, fn)!=imported_names.end())
182                 return;
183         imported_names.push_back(fn);
184
185         RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
186         if(!io)
187                 throw runtime_error(format("module %s not found", name));
188         Parser import_parser;
189         append_module(import_parser.parse(*io, fn, module->source_map.get_count()), resources);
190 }
191
192 void Compiler::generate(Stage &stage, Mode mode)
193 {
194         stage.required_features.gl_api = features.gl_api;
195         if(module->shared.required_features.glsl_version>stage.required_features.glsl_version)
196                 stage.required_features.glsl_version = module->shared.required_features.glsl_version;
197         inject_block(stage.content, module->shared.content);
198
199         DeclarationReorderer().apply(stage);
200         BlockResolver().apply(stage);
201         FunctionResolver().apply(stage);
202         VariableResolver().apply(stage);
203         InterfaceGenerator().apply(stage);
204         VariableResolver().apply(stage);
205         DeclarationReorderer().apply(stage);
206         FunctionResolver().apply(stage);
207         if(mode==PROGRAM)
208                 LegacyConverter().apply(stage, features);
209 }
210
211 bool Compiler::optimize(Stage &stage)
212 {
213         ConstantConditionEliminator().apply(stage);
214
215         FunctionInliner().apply(stage);
216         BlockResolver().apply(stage);
217         VariableResolver().apply(stage);
218
219         bool result = UnusedVariableRemover().apply(stage);
220         result |= UnusedFunctionRemover().apply(stage);
221
222         return result;
223 }
224
225 void Compiler::finalize(Stage &stage, Mode mode)
226 {
227         if(get_gl_api()==OPENGL_ES2 && mode==PROGRAM)
228                 DefaultPrecisionGenerator().apply(stage);
229         else if(mode==MODULE)
230                 PrecisionRemover().apply(stage);
231 }
232
233 void Compiler::inject_block(Block &target, const Block &source)
234 {
235         NodeList<Statement>::iterator insert_point = target.body.begin();
236         for(NodeList<Statement>::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
237                 target.body.insert(insert_point, (*i)->clone());
238 }
239
240 } // namespace SL
241 } // namespace GL
242 } // namespace Msp