]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/output.cpp
Inject builtins into the module
[libs/gl.git] / source / glsl / output.cpp
1 #include <msp/core/raii.h>
2 #include <msp/strings/format.h>
3 #include "output.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9 namespace SL {
10
11 Formatter::Formatter():
12         stage(0),
13         mode(Compiler::PROGRAM),
14         source_index(0),
15         source_line(1),
16         indent(0),
17         parameter_list(false)
18 { }
19
20 const string &Formatter::apply(Stage &s, Compiler::Mode m)
21 {
22         mode = m;
23         stage = &s;
24
25         const Version &ver = s.required_features.glsl_version;
26
27         if(ver)
28         {
29                 append(format("#version %d%02d", ver.major, ver.minor));
30                 if(s.required_features.gl_api==OPENGL_ES2 && ver>=Version(3, 0))
31                         append(" es");
32                 formatted += '\n';
33         }
34
35         if(s.required_features.arb_explicit_attrib_location)
36                 append("#extension arb_explicit_attrib_location: require\n");
37         if(s.required_features.arb_gpu_shader5)
38                 append("#extension arb_gpu_shader5: require\n");
39         if(s.required_features.arb_uniform_buffer_object)
40                 append("#extension arb_uniform_buffer_object: require\n");
41         if(s.required_features.ext_gpu_shader4)
42                 append("#extension ext_gpu_shader4: require\n");
43         if(s.required_features.ext_texture_array)
44                 append("#extension ext_texture_array: require\n");
45         formatted += '\n';
46
47         s.content.visit(*this);
48
49         return formatted;
50 }
51
52 void Formatter::append(const string &text)
53 {
54         formatted += text;
55         for(string::const_iterator i=text.begin(); i!=text.end(); ++i)
56                 if(*i=='\n')
57                         ++source_line;
58 }
59
60 void Formatter::append(char c)
61 {
62         formatted += c;
63         if(c=='\n')
64                 ++source_line;
65 }
66
67 void Formatter::set_source(unsigned index, unsigned line)
68 {
69         if(index!=source_index || (index && line!=source_line))
70         {
71                 if(index==source_index && line==source_line+1)
72                         formatted += '\n';
73                 else
74                 {
75                         unsigned l = line;
76                         if(mode==Compiler::PROGRAM && stage && stage->required_features.glsl_version<Version(3, 30))
77                                 --l;
78                         formatted += format("#line %d %d\n", l, index);
79                 }
80         }
81         source_index = index;
82         source_line = line;
83 }
84
85 void Formatter::visit(Literal &literal)
86 {
87         append(literal.token);
88 }
89
90 void Formatter::visit(ParenthesizedExpression &parexpr)
91 {
92         append('(');
93         parexpr.expression->visit(*this);
94         append(')');
95 }
96
97 void Formatter::visit(VariableReference &var)
98 {
99         append(var.name);
100 }
101
102 void Formatter::visit(InterfaceBlockReference &iface)
103 {
104         append(iface.name);
105 }
106
107 void Formatter::visit(MemberAccess &memacc)
108 {
109         memacc.left->visit(*this);
110         append(format(".%s", memacc.member));
111 }
112
113 void Formatter::visit(UnaryExpression &unary)
114 {
115         if(unary.prefix)
116                 append(unary.oper);
117         unary.expression->visit(*this);
118         if(!unary.prefix)
119                 append(unary.oper);
120 }
121
122 void Formatter::visit(BinaryExpression &binary)
123 {
124         binary.left->visit(*this);
125         append(binary.oper);
126         binary.right->visit(*this);
127         append(binary.after);
128 }
129
130 void Formatter::visit(Assignment &assign)
131 {
132         assign.left->visit(*this);
133         append(format(" %s ", assign.oper));
134         assign.right->visit(*this);
135 }
136
137 void Formatter::visit(FunctionCall &call)
138 {
139         append(format("%s(", call.name));
140         for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
141         {
142                 if(i!=call.arguments.begin())
143                         append(", ");
144                 (*i)->visit(*this);
145         }
146         append(')');
147 }
148
149 void Formatter::visit(ExpressionStatement &expr)
150 {
151         expr.expression->visit(*this);
152         append(';');
153 }
154
155 void Formatter::visit(Block &block)
156 {
157         unsigned brace_indent = indent;
158         bool use_braces = (block.use_braces || (indent && block.body.size()!=1));
159         if(use_braces)
160                 append(format("%s{\n", string(brace_indent*2, ' ')));
161
162         SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
163         string spaces(indent*2, ' ');
164         bool first = true;
165         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
166         {
167                 if((*i)->source==BUILTIN_SOURCE)
168                         continue;
169                 if(!first)
170                         append('\n');
171                 first = false;
172                 set_source((*i)->source, (*i)->line);
173                 append(spaces);
174                 (*i)->visit(*this);
175         }
176
177         if(use_braces)
178                 append(format("\n%s}", string(brace_indent*2, ' ')));
179 }
180
181 void Formatter::visit(Import &import)
182 {
183         append(format("import %s;", import.module));
184 }
185
186 void Formatter::visit(Precision &prec)
187 {
188         append(format("precision %s %s;", prec.precision, prec.type));
189 }
190
191 void Formatter::visit(Layout &layout)
192 {
193         append("layout(");
194         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
195         {
196                 if(i!=layout.qualifiers.begin())
197                         append(", ");
198                 append(i->name);
199                 if(i->has_value)
200                         append(format("=%d", i->value));
201         }
202         append(')');
203 }
204
205 void Formatter::visit(InterfaceLayout &layout)
206 {
207         layout.layout.visit(*this);
208         append(format(" %s;", layout.interface));
209 }
210
211 void Formatter::visit(StructDeclaration &strct)
212 {
213         append(format("struct %s\n", strct.name));
214         strct.members.visit(*this);
215         append(';');
216 }
217
218 void Formatter::visit(VariableDeclaration &var)
219 {
220         if(var.layout)
221         {
222                 var.layout->visit(*this);
223                 append(' ');
224         }
225         if(var.constant)
226                 append("const ");
227         if(!var.interpolation.empty())
228                 append(format("%s ", var.interpolation));
229         if(!var.sampling.empty())
230                 append(format("%s ", var.sampling));
231         if(!var.interface.empty() && var.interface!=block_interface)
232         {
233                 string interface = var.interface;
234                 if(mode==Compiler::PROGRAM && stage && stage->required_features.glsl_version<Version(1, 30))
235                 {
236                         if(stage->type==Stage::VERTEX && var.interface=="in")
237                                 interface = "attribute";
238                         else if((stage->type==Stage::VERTEX && var.interface=="out") || (stage->type==Stage::FRAGMENT && var.interface=="in"))
239                                 interface = "varying";
240                 }
241                 append(format("%s ", interface));
242         }
243         if(!var.precision.empty())
244                 append(format("%s ", var.precision));
245         append(format("%s %s", var.type, var.name));
246         if(var.array)
247         {
248                 append('[');
249                 if(var.array_size)
250                         var.array_size->visit(*this);
251                 append(']');
252         }
253         if(var.init_expression)
254         {
255                 append(" = ");
256                 var.init_expression->visit(*this);
257         }
258         if(!parameter_list)
259                 append(';');
260 }
261
262 void Formatter::visit(InterfaceBlock &iface)
263 {
264         SetForScope<string> set(block_interface, iface.interface);
265         append(format("%s %s\n", iface.interface, iface.name));
266         iface.members.visit(*this);
267         if(!iface.instance_name.empty())
268         {
269                 append(' ');
270                 append(iface.instance_name);
271                 if(iface.array)
272                         append("[]");
273         }
274         append(';');
275 }
276
277 void Formatter::visit(FunctionDeclaration &func)
278 {
279         append(format("%s %s(", func.return_type, func.name));
280         for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
281         {
282                 if(i!=func.parameters.begin())
283                         append(", ");
284                 SetFlag set(parameter_list);
285                 (*i)->visit(*this);
286         }
287         append(')');
288         if(func.definition==&func)
289         {
290                 append('\n');
291                 func.body.visit(*this);
292         }
293         else
294                 append(';');
295 }
296
297 void Formatter::visit(Conditional &cond)
298 {
299         append("if(");
300         cond.condition->visit(*this);
301         append(")\n");
302
303         cond.body.visit(*this);
304         if(!cond.else_body.body.empty())
305         {
306                 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
307                 if(cond.else_body.body.size()==1 && else_cond)
308                 {
309                         append('\n');
310                         set_source(else_cond->source, else_cond->line);
311                         append(format("%selse ", string(indent*2, ' ')));
312                         else_cond->visit(*this);
313                 }
314                 else
315                 {
316                         append(format("\n%selse\n", string(indent*2, ' ')));
317                         cond.else_body.visit(*this);
318                 }
319         }
320 }
321
322 void Formatter::visit(Iteration &iter)
323 {
324         if(!iter.init_statement && iter.condition && !iter.loop_expression)
325         {
326                 append("while(");
327                 iter.condition->visit(*this);
328                 append(')');
329         }
330         else
331         {
332                 append("for(");
333                 if(iter.init_statement)
334                         iter.init_statement->visit(*this);
335                 else
336                         append(';');
337                 if(iter.condition)
338                 {
339                         append(' ');
340                         iter.condition->visit(*this);
341                 }
342                 append(';');
343                 if(iter.loop_expression)
344                 {
345                         append(' ');
346                         iter.loop_expression->visit(*this);
347                 }
348                 append(')');
349         }
350
351         if(iter.body.body.empty())
352                 append(" { }");
353         else
354         {
355                 append('\n');
356                 iter.body.visit(*this);
357         }
358 }
359
360 void Formatter::visit(Passthrough &pass)
361 {
362         append("passthrough");
363         if(pass.subscript)
364         {
365                 append('[');
366                 pass.subscript->visit(*this);
367                 append(']');
368         }
369         append(';');
370 }
371
372 void Formatter::visit(Return &ret)
373 {
374         append("return");
375         if(ret.expression)
376         {
377                 append(' ');
378                 ret.expression->visit(*this);
379         }
380         append(';');
381 }
382
383 void Formatter::visit(Jump &jump)
384 {
385         append(jump.keyword);
386         append(';');
387 }
388
389 } // namespace SL
390 } // namespace GL
391 } // namespace Msp