]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/compiler.cpp
Move the StageType enum inside the Stage struct
[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 <msp/strings/regex.h>
5 #include <msp/strings/utils.h>
6 #include "compatibility.h"
7 #include "compiler.h"
8 #include "error.h"
9 #include "generate.h"
10 #include "optimize.h"
11 #include "output.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         resources(0),
24         module(0)
25 { }
26
27 Compiler::~Compiler()
28 {
29         delete module;
30 }
31
32 void Compiler::compile(const string &source, const string &src_name)
33 {
34         resources = 0;
35         delete module;
36         module = new Module();
37         Parser parser;
38         imported_names.push_back(src_name);
39         append_module(parser.parse(source, src_name, 1));
40         process();
41 }
42
43 void Compiler::compile(IO::Base &io, Resources *res, const string &src_name)
44 {
45         resources = res;
46         delete module;
47         module = new Module();
48         Parser parser;
49         imported_names.push_back(src_name);
50         append_module(parser.parse(io, src_name, 1));
51         process();
52 }
53
54 void Compiler::compile(IO::Base &io, const string &src_name)
55 {
56         compile(io, 0, src_name);
57 }
58
59 void Compiler::add_shaders(Program &program)
60 {
61         if(!module)
62                 throw invalid_operation("Compiler::add_shaders");
63
64         try
65         {
66                 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
67                 {
68                         if(i->type==Stage::VERTEX)
69                         {
70                                 program.attach_shader_owned(new VertexShader(apply<Formatter>(*i)));
71                                 for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
72                                         program.bind_attribute(j->second, j->first);
73                         }
74                         else if(i->type==Stage::GEOMETRY)
75                                 program.attach_shader_owned(new GeometryShader(apply<Formatter>(*i)));
76                         else if(i->type==Stage::FRAGMENT)
77                         {
78                                 program.attach_shader_owned(new FragmentShader(apply<Formatter>(*i)));
79                                 if(EXT_gpu_shader4)
80                                 {
81                                         for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
82                                                 program.bind_fragment_data(j->second, j->first);
83                                 }
84                         }
85                 }
86         }
87         catch(const compile_error &e)
88         {
89                 static const Regex r_message("^(([0-9]+)\\(([0-9]+)\\) :|ERROR: ([0-9]+):([0-9]+):) (.*)$");
90                 vector<string> lines = split(e.what(), '\n');
91                 string translated;
92                 for(vector<string>::const_iterator i=lines.begin(); i!=lines.end(); ++i)
93                 {
94                         RegMatch m = r_message.match(*i);
95                         if(m)
96                         {
97                                 unsigned index = 0;
98                                 unsigned line = 0;
99                                 if(m[2])
100                                 {
101                                         index = lexical_cast<unsigned>(m[2].str);
102                                         line = lexical_cast<unsigned>(m[3].str);
103                                 }
104                                 else if(m[4])
105                                 {
106                                         index = lexical_cast<unsigned>(m[4].str);
107                                         line = lexical_cast<unsigned>(m[5].str);
108                                 }
109                                 const char *src = "<unknown>";
110                                 if(index==0)
111                                         src = "<generated>";
112                                 else if(index-1<imported_names.size())
113                                         src = imported_names[index-1].c_str();
114                                 translated += format("%s:%d: %s", src, line, m[6].str);
115                         }
116                         else
117                                 translated += *i;
118                         translated += '\n';
119                 }
120
121                 throw compile_error(translated);
122         }
123 }
124
125 void Compiler::append_module(Module &mod)
126 {
127         vector<Import *> imports = apply<NodeGatherer<Import> >(mod.shared);
128         for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
129                 import((*i)->module);
130         apply<NodeRemover>(mod.shared, set<Node *>(imports.begin(), imports.end()));
131
132         append_stage(mod.shared);
133         for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
134                 append_stage(*i);
135 }
136
137 void Compiler::append_stage(Stage &stage)
138 {
139         Stage *target = 0;
140         if(stage.type==Stage::SHARED)
141                 target = &module->shared;
142         else
143         {
144                 list<Stage>::iterator i;
145                 for(i=module->stages.begin(); (i!=module->stages.end() && i->type<stage.type); ++i) ;
146                 if(i==module->stages.end() || i->type>stage.type)
147                 {
148                         list<Stage>::iterator j = module->stages.insert(i, stage.type);
149                         if(i!=module->stages.end())
150                                 i->previous = &*j;
151                         i = j;
152                         if(i!=module->stages.begin())
153                                 i->previous = &*--j;
154                 }
155
156                 target = &*i;
157         }
158
159         if(stage.required_version>target->required_version)
160                 target->required_version = stage.required_version;
161         for(NodeList<Statement>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
162                 target->content.body.push_back(*i);
163         apply<DeclarationCombiner>(*target);
164 }
165
166 void Compiler::process()
167 {
168         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
169                 generate(*i);
170         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
171         {
172                 if(optimize(*i))
173                         i = module->stages.begin();
174                 else
175                         ++i;
176         }
177         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
178                 finalize(*i);
179 }
180
181 void Compiler::import(const string &name)
182 {
183         string fn = name+".glsl";
184         if(find(imported_names, fn)!=imported_names.end())
185                 return;
186         imported_names.push_back(fn);
187
188         RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
189         if(!io)
190                 throw runtime_error(format("module %s not found", name));
191         Parser import_parser;
192         append_module(import_parser.parse(*io, fn, imported_names.size()));
193 }
194
195 void Compiler::generate(Stage &stage)
196 {
197         if(module->shared.required_version>stage.required_version)
198                 stage.required_version = module->shared.required_version;
199         inject_block(stage.content, module->shared.content);
200
201         apply<DeclarationReorderer>(stage);
202         apply<FunctionResolver>(stage);
203         apply<VariableResolver>(stage);
204         apply<InterfaceGenerator>(stage);
205         apply<VariableResolver>(stage);
206         apply<DeclarationReorderer>(stage);
207         apply<FunctionResolver>(stage);
208         apply<LegacyConverter>(stage);
209 }
210
211 bool Compiler::optimize(Stage &stage)
212 {
213         apply<ConstantConditionEliminator>(stage);
214
215         set<FunctionDeclaration *> inlineable = apply<InlineableFunctionLocator>(stage);
216         apply<FunctionInliner>(stage, inlineable);
217
218         set<Node *> unused = apply<UnusedVariableLocator>(stage);
219         set<Node *> unused2 = apply<UnusedFunctionLocator>(stage);
220         unused.insert(unused2.begin(), unused2.end());
221         apply<NodeRemover>(stage, unused);
222
223         return !unused.empty();
224 }
225
226 void Compiler::finalize(Stage &stage)
227 {
228         if(get_gl_api()==OPENGL_ES2)
229                 apply<DefaultPrecisionGenerator>(stage);
230         else
231                 apply<PrecisionRemover>(stage);
232 }
233
234 void Compiler::inject_block(Block &target, const Block &source)
235 {
236         NodeList<Statement>::iterator insert_point = target.body.begin();
237         for(NodeList<Statement>::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
238                 target.body.insert(insert_point, (*i)->clone());
239 }
240
241 template<typename T>
242 typename T::ResultType Compiler::apply(Stage &stage)
243 {
244         T visitor;
245         visitor.apply(stage);
246         return visitor.get_result();
247 }
248
249 template<typename T, typename A>
250 typename T::ResultType Compiler::apply(Stage &stage, const A &arg)
251 {
252         T visitor(arg);
253         visitor.apply(stage);
254         return visitor.get_result();
255 }
256
257 } // namespace SL
258 } // namespace GL
259 } // namespace Msp