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