]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/output.cpp
38adb5bd6b799f54f165299657b90f09830293de
[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         source_index(0),
14         source_line(1),
15         indent(0),
16         parameter_list(false),
17         omit_builtin(false),
18         r_empty_name(false)
19 { }
20
21 const string &Formatter::apply(Stage &s)
22 {
23         stage = &s;
24         omit_builtin = true;
25
26         const Version &ver = s.required_features.glsl_version;
27
28         if(ver)
29         {
30                 append(format("#version %d%02d", ver.major, ver.minor));
31                 if(s.required_features.gl_api==OPENGL_ES2 && ver>=Version(3, 0))
32                         append(" es");
33                 formatted += '\n';
34         }
35
36         if(s.required_features.arb_enhanced_layouts)
37                 append("#extension GL_ARB_enhanced_layouts: require\n");
38         if(s.required_features.arb_explicit_attrib_location)
39                 append("#extension GL_ARB_explicit_attrib_location: require\n");
40         if(s.required_features.arb_explicit_uniform_location)
41                 append("#extension GL_ARB_explicit_uniform_location: require\n");
42         if(s.required_features.arb_gpu_shader5)
43                 append("#extension GL_ARB_gpu_shader5: require\n");
44         if(s.required_features.arb_separate_shader_objects)
45                 append("#extension GL_ARB_separate_shader_objects: require\n");
46         if(s.required_features.arb_uniform_buffer_object)
47                 append("#extension GL_ARB_uniform_buffer_object: require\n");
48         if(s.required_features.ext_gpu_shader4)
49                 append("#extension GL_EXT_gpu_shader4: require\n");
50         if(s.required_features.ext_texture_array)
51                 append("#extension GL_EXT_texture_array: require\n");
52         formatted += '\n';
53
54         s.content.visit(*this);
55
56         return formatted;
57 }
58
59 void Formatter::append(const string &text)
60 {
61         formatted += text;
62         for(string::const_iterator i=text.begin(); i!=text.end(); ++i)
63                 if(*i=='\n')
64                         ++source_line;
65 }
66
67 void Formatter::append(char c)
68 {
69         formatted += c;
70         if(c=='\n')
71                 ++source_line;
72 }
73
74 void Formatter::set_source(unsigned index, unsigned line)
75 {
76         if(index!=source_index || (index && line!=source_line))
77         {
78                 if(index==source_index && line==source_line+1)
79                         formatted += '\n';
80                 else
81                 {
82                         unsigned l = line;
83                         if(stage && stage->required_features.glsl_version && stage->required_features.glsl_version<Version(3, 30))
84                                 --l;
85                         formatted += format("#line %d %d\n", l, index);
86                 }
87         }
88         source_index = index;
89         source_line = line;
90 }
91
92 void Formatter::visit(Block &block)
93 {
94         unsigned brace_indent = indent;
95         bool use_braces = (block.use_braces || (indent && block.body.size()!=1));
96         if(use_braces)
97                 append(format("%s{\n", string(brace_indent*2, ' ')));
98
99         SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
100         string spaces(indent*2, ' ');
101         bool first = true;
102         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
103         {
104                 if(omit_builtin && (*i)->source<=BUILTIN_SOURCE)
105                         continue;
106                 if(!first)
107                         append('\n');
108                 first = false;
109                 set_source((*i)->source, (*i)->line);
110                 append(spaces);
111                 (*i)->visit(*this);
112         }
113
114         if(use_braces)
115                 append(format("\n%s}", string(brace_indent*2, ' ')));
116 }
117
118 void Formatter::visit_expression(Expression &expr, const Operator *outer_oper, bool on_rhs)
119 {
120         unsigned outer_precedence = (outer_oper ? outer_oper->precedence : 20);
121         unsigned inner_precedence = (expr.oper ? expr.oper->precedence : 0);
122
123         bool needs_parentheses = (inner_precedence>=outer_precedence);
124
125         // Omit parentheses if the outer operator encloses this operand.
126         if(outer_oper && outer_oper->type==Operator::BINARY && outer_oper->token2[0] && on_rhs)
127                 needs_parentheses = false;
128
129         if(expr.oper)
130         {
131                 /* Omit parentheses if the inner expression's operator sits between the
132                 expression and the outer operator. */
133                 bool oper_on_left = expr.oper->type==Operator::PREFIX;
134                 bool oper_on_right = expr.oper->type==Operator::POSTFIX || (expr.oper->type==Operator::BINARY && expr.oper->token2[0]);
135                 if(expr.oper && ((oper_on_left && on_rhs) || (oper_on_right && !on_rhs)))
136                         needs_parentheses = false;
137
138                 // Omit parentheses if the operator's natural grouping works out.
139                 if(expr.oper==outer_oper)
140                         needs_parentheses = (expr.oper->assoc!=Operator::ASSOCIATIVE && on_rhs!=(expr.oper->assoc==Operator::RIGHT_TO_LEFT));
141         }
142
143         if(needs_parentheses)
144                 append('(');
145         expr.visit(*this);
146         if(needs_parentheses)
147                 append(')');
148 }
149
150 void Formatter::visit(Literal &literal)
151 {
152         append(literal.token);
153 }
154
155 void Formatter::visit(VariableReference &var)
156 {
157         append(var.name);
158         r_empty_name = false;
159 }
160
161 void Formatter::visit(InterfaceBlockReference &iface)
162 {
163         r_empty_name = iface.declaration->instance_name.empty();
164         if(!r_empty_name)
165                 append(iface.declaration->instance_name);
166 }
167
168 void Formatter::visit(MemberAccess &memacc)
169 {
170         visit_expression(*memacc.left, memacc.oper, false);
171         if(!r_empty_name)
172                 append('.');
173         append(memacc.member);
174 }
175
176 void Formatter::visit(Swizzle &swizzle)
177 {
178         visit_expression(*swizzle.left, swizzle.oper, false);
179         append(format(".%s", swizzle.component_group));
180 }
181
182 void Formatter::visit(UnaryExpression &unary)
183 {
184         if(unary.oper->type==Operator::PREFIX)
185                 append(unary.oper->token);
186         visit_expression(*unary.expression, unary.oper, unary.oper->type==Operator::PREFIX);
187         if(unary.oper->type==Operator::POSTFIX)
188                 append(unary.oper->token);
189 }
190
191 void Formatter::visit(BinaryExpression &binary)
192 {
193         visit_expression(*binary.left, binary.oper, false);
194         append(binary.oper->token);
195         visit_expression(*binary.right, binary.oper, true);
196         if(binary.oper->token2[0])
197                 append(binary.oper->token2);
198 }
199
200 void Formatter::visit(Assignment &assign)
201 {
202         visit_expression(*assign.left, assign.oper, false);
203         append(format(" %s ", assign.oper->token));
204         visit_expression(*assign.right, assign.oper, true);
205 }
206
207 void Formatter::visit(TernaryExpression &ternary)
208 {
209         visit_expression(*ternary.condition, ternary.oper, false);
210         append(ternary.oper->token);
211         visit_expression(*ternary.true_expr, ternary.oper, false);
212         if(ternary.oper->token2)
213                 append(ternary.oper->token2);
214         visit_expression(*ternary.false_expr, ternary.oper, true);
215 }
216
217 void Formatter::visit(FunctionCall &call)
218 {
219         append(format("%s(", call.name));
220         for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
221         {
222                 if(i!=call.arguments.begin())
223                         append(", ");
224                 (*i)->visit(*this);
225         }
226         append(')');
227 }
228
229 void Formatter::visit(ExpressionStatement &expr)
230 {
231         expr.expression->visit(*this);
232         append(';');
233 }
234
235 void Formatter::visit(Import &import)
236 {
237         append(format("import %s;", import.module));
238 }
239
240 void Formatter::visit(Precision &prec)
241 {
242         append(format("precision %s %s;", prec.precision, prec.type));
243 }
244
245 void Formatter::visit(Layout &layout)
246 {
247         append("layout(");
248         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
249         {
250                 if(i!=layout.qualifiers.begin())
251                         append(", ");
252                 append(i->name);
253                 if(i->has_value)
254                         append(format("=%d", i->value));
255         }
256         append(')');
257 }
258
259 void Formatter::visit(InterfaceLayout &layout)
260 {
261         layout.layout.visit(*this);
262         append(format(" %s;", layout.interface));
263 }
264
265 void Formatter::visit(StructDeclaration &strct)
266 {
267         append(format("struct %s\n", strct.name));
268         strct.members.visit(*this);
269         append(';');
270 }
271
272 void Formatter::visit(VariableDeclaration &var)
273 {
274         if(var.layout)
275         {
276                 var.layout->visit(*this);
277                 append(' ');
278         }
279         if(var.constant)
280                 append("const ");
281         if(!var.interpolation.empty())
282                 append(format("%s ", var.interpolation));
283         if(!var.sampling.empty())
284                 append(format("%s ", var.sampling));
285         if(!var.interface.empty())
286         {
287                 string interface = var.interface;
288                 if(stage && stage->required_features.glsl_version && stage->required_features.glsl_version<Version(1, 30))
289                 {
290                         if(stage->type==Stage::VERTEX && var.interface=="in")
291                                 interface = "attribute";
292                         else if((stage->type==Stage::VERTEX && var.interface=="out") || (stage->type==Stage::FRAGMENT && var.interface=="in"))
293                                 interface = "varying";
294                 }
295                 append(format("%s ", interface));
296         }
297         if(!var.precision.empty())
298                 append(format("%s ", var.precision));
299         string type_name = var.type_declaration->name;
300         if(var.array)
301                 type_name = type_name.substr(0, type_name.find('['));
302         append(format("%s %s", type_name, var.name));
303         if(var.array)
304         {
305                 append('[');
306                 if(var.array_size)
307                         var.array_size->visit(*this);
308                 append(']');
309         }
310         if(var.init_expression)
311         {
312                 append(" = ");
313                 var.init_expression->visit(*this);
314         }
315         if(!parameter_list)
316                 append(';');
317 }
318
319 void Formatter::visit(InterfaceBlock &iface)
320 {
321         if(iface.layout)
322         {
323                 iface.layout->visit(*this);
324                 append(' ');
325         }
326         append(format("%s %s\n", iface.interface, iface.block_name));
327         if(iface.struct_declaration)
328                 iface.struct_declaration->members.visit(*this);
329         if(!iface.instance_name.empty())
330         {
331                 append(' ');
332                 append(iface.instance_name);
333                 if(iface.array)
334                         append("[]");
335         }
336         append(';');
337 }
338
339 void Formatter::visit(FunctionDeclaration &func)
340 {
341         append(format("%s %s(", func.return_type_declaration->name, func.name));
342         for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
343         {
344                 if(i!=func.parameters.begin())
345                         append(", ");
346                 SetFlag set(parameter_list);
347                 (*i)->visit(*this);
348         }
349         append(')');
350         if(func.definition==&func)
351         {
352                 append('\n');
353                 func.body.visit(*this);
354         }
355         else
356                 append(';');
357 }
358
359 void Formatter::visit(Conditional &cond)
360 {
361         append("if(");
362         cond.condition->visit(*this);
363         append(")\n");
364
365         cond.body.visit(*this);
366         if(!cond.else_body.body.empty())
367         {
368                 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
369                 if(cond.else_body.body.size()==1 && else_cond)
370                 {
371                         append('\n');
372                         set_source(else_cond->source, else_cond->line);
373                         append(format("%selse ", string(indent*2, ' ')));
374                         else_cond->visit(*this);
375                 }
376                 else
377                 {
378                         append(format("\n%selse\n", string(indent*2, ' ')));
379                         cond.else_body.visit(*this);
380                 }
381         }
382 }
383
384 void Formatter::visit(Iteration &iter)
385 {
386         if(!iter.init_statement && iter.condition && !iter.loop_expression)
387         {
388                 append("while(");
389                 iter.condition->visit(*this);
390                 append(')');
391         }
392         else
393         {
394                 append("for(");
395                 if(iter.init_statement)
396                         iter.init_statement->visit(*this);
397                 else
398                         append(';');
399                 if(iter.condition)
400                 {
401                         append(' ');
402                         iter.condition->visit(*this);
403                 }
404                 append(';');
405                 if(iter.loop_expression)
406                 {
407                         append(' ');
408                         iter.loop_expression->visit(*this);
409                 }
410                 append(')');
411         }
412
413         if(iter.body.body.empty())
414                 append(" { }");
415         else
416         {
417                 append('\n');
418                 iter.body.visit(*this);
419         }
420 }
421
422 void Formatter::visit(Passthrough &pass)
423 {
424         append("passthrough");
425         if(pass.subscript)
426         {
427                 append('[');
428                 pass.subscript->visit(*this);
429                 append(']');
430         }
431         append(';');
432 }
433
434 void Formatter::visit(Return &ret)
435 {
436         append("return");
437         if(ret.expression)
438         {
439                 append(' ');
440                 ret.expression->visit(*this);
441         }
442         append(';');
443 }
444
445 void Formatter::visit(Jump &jump)
446 {
447         append(jump.keyword);
448         append(';');
449 }
450
451 } // namespace SL
452 } // namespace GL
453 } // namespace Msp