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