]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/output.cpp
Split the Light class into subclasses by light type
[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 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.target_api==OPENGL_ES && 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(char c: text)
63                 if(c=='\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(const RefPtr<Statement> &s: block.body)
103         {
104                 if(omit_builtin && s->source<=BUILTIN_SOURCE)
105                         continue;
106                 if(!first)
107                         append('\n');
108                 first = false;
109                 set_source(s->source, s->line);
110                 append(spaces);
111                 s->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         r_empty_name = false;
175 }
176
177 void Formatter::visit(Swizzle &swizzle)
178 {
179         visit_expression(*swizzle.left, swizzle.oper, false);
180         append(format(".%s", swizzle.component_group));
181 }
182
183 void Formatter::visit(UnaryExpression &unary)
184 {
185         if(unary.oper->type==Operator::PREFIX)
186                 append(unary.oper->token);
187         visit_expression(*unary.expression, unary.oper, unary.oper->type==Operator::PREFIX);
188         if(unary.oper->type==Operator::POSTFIX)
189                 append(unary.oper->token);
190 }
191
192 void Formatter::visit(BinaryExpression &binary)
193 {
194         visit_expression(*binary.left, binary.oper, false);
195         append(binary.oper->token);
196         visit_expression(*binary.right, binary.oper, true);
197         if(binary.oper->token2[0])
198                 append(binary.oper->token2);
199 }
200
201 void Formatter::visit(Assignment &assign)
202 {
203         visit_expression(*assign.left, assign.oper, false);
204         append(format(" %s ", assign.oper->token));
205         visit_expression(*assign.right, assign.oper, true);
206 }
207
208 void Formatter::visit(TernaryExpression &ternary)
209 {
210         visit_expression(*ternary.condition, ternary.oper, false);
211         append(ternary.oper->token);
212         visit_expression(*ternary.true_expr, ternary.oper, false);
213         if(ternary.oper->token2)
214                 append(ternary.oper->token2);
215         visit_expression(*ternary.false_expr, ternary.oper, true);
216 }
217
218 void Formatter::visit(FunctionCall &call)
219 {
220         append(format("%s(", call.name));
221         for(auto i=call.arguments.begin(); i!=call.arguments.end(); ++i)
222         {
223                 if(i!=call.arguments.begin())
224                         append(", ");
225                 (*i)->visit(*this);
226         }
227         append(')');
228 }
229
230 void Formatter::visit(ExpressionStatement &expr)
231 {
232         expr.expression->visit(*this);
233         append(';');
234 }
235
236 void Formatter::visit(Import &import)
237 {
238         append(format("import %s;", import.module));
239 }
240
241 void Formatter::visit(Precision &prec)
242 {
243         append(format("precision %s %s;", prec.precision, prec.type));
244 }
245
246 void Formatter::visit(Layout &layout)
247 {
248         append("layout(");
249         for(auto i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
250         {
251                 if(i!=layout.qualifiers.begin())
252                         append(", ");
253                 append(i->name);
254                 if(i->has_value)
255                         append(format("=%d", i->value));
256         }
257         append(')');
258 }
259
260 void Formatter::visit(InterfaceLayout &layout)
261 {
262         layout.layout.visit(*this);
263         append(format(" %s;", layout.interface));
264 }
265
266 void Formatter::visit(StructDeclaration &strct)
267 {
268         append(format("struct %s\n", strct.name));
269         strct.members.visit(*this);
270         append(';');
271 }
272
273 void Formatter::visit(VariableDeclaration &var)
274 {
275         if(var.layout)
276         {
277                 var.layout->visit(*this);
278                 append(' ');
279         }
280         if(var.constant)
281                 append("const ");
282         if(!var.interpolation.empty())
283                 append(format("%s ", var.interpolation));
284         if(!var.sampling.empty())
285                 append(format("%s ", var.sampling));
286         if(!var.interface.empty())
287         {
288                 string interface = var.interface;
289                 if(stage && stage->required_features.glsl_version && stage->required_features.glsl_version<Version(1, 30))
290                 {
291                         if(stage->type==Stage::VERTEX && var.interface=="in")
292                                 interface = "attribute";
293                         else if((stage->type==Stage::VERTEX && var.interface=="out") || (stage->type==Stage::FRAGMENT && var.interface=="in"))
294                                 interface = "varying";
295                 }
296                 append(format("%s ", interface));
297         }
298         if(!var.precision.empty())
299                 append(format("%s ", var.precision));
300         string type_name = var.type_declaration->name;
301         if(var.array)
302                 type_name = type_name.substr(0, type_name.find('['));
303         append(format("%s %s", type_name, var.name));
304         if(var.array)
305         {
306                 append('[');
307                 if(var.array_size)
308                         var.array_size->visit(*this);
309                 append(']');
310         }
311         if(var.init_expression)
312         {
313                 append(" = ");
314                 var.init_expression->visit(*this);
315         }
316         if(!parameter_list)
317                 append(';');
318 }
319
320 void Formatter::visit(InterfaceBlock &iface)
321 {
322         if(iface.layout)
323         {
324                 iface.layout->visit(*this);
325                 append(' ');
326         }
327         append(format("%s %s\n", iface.interface, iface.block_name));
328         if(iface.struct_declaration)
329                 iface.struct_declaration->members.visit(*this);
330         if(!iface.instance_name.empty())
331         {
332                 append(' ');
333                 append(iface.instance_name);
334                 if(iface.array)
335                         append("[]");
336         }
337         append(';');
338 }
339
340 void Formatter::visit(FunctionDeclaration &func)
341 {
342         append(format("%s %s(", func.return_type_declaration->name, func.name));
343         for(auto i=func.parameters.begin(); i!=func.parameters.end(); ++i)
344         {
345                 if(i!=func.parameters.begin())
346                         append(", ");
347                 SetFlag set(parameter_list);
348                 (*i)->visit(*this);
349         }
350         append(')');
351         if(func.definition==&func)
352         {
353                 append('\n');
354                 func.body.visit(*this);
355         }
356         else
357                 append(';');
358 }
359
360 void Formatter::visit(Conditional &cond)
361 {
362         append("if(");
363         cond.condition->visit(*this);
364         append(")\n");
365
366         cond.body.visit(*this);
367         if(!cond.else_body.body.empty())
368         {
369                 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
370                 if(cond.else_body.body.size()==1 && else_cond)
371                 {
372                         append('\n');
373                         set_source(else_cond->source, else_cond->line);
374                         append(format("%selse ", string(indent*2, ' ')));
375                         else_cond->visit(*this);
376                 }
377                 else
378                 {
379                         append(format("\n%selse\n", string(indent*2, ' ')));
380                         cond.else_body.visit(*this);
381                 }
382         }
383 }
384
385 void Formatter::visit(Iteration &iter)
386 {
387         if(!iter.init_statement && iter.condition && !iter.loop_expression)
388         {
389                 append("while(");
390                 iter.condition->visit(*this);
391                 append(')');
392         }
393         else
394         {
395                 append("for(");
396                 if(iter.init_statement)
397                         iter.init_statement->visit(*this);
398                 else
399                         append(';');
400                 if(iter.condition)
401                 {
402                         append(' ');
403                         iter.condition->visit(*this);
404                 }
405                 append(';');
406                 if(iter.loop_expression)
407                 {
408                         append(' ');
409                         iter.loop_expression->visit(*this);
410                 }
411                 append(')');
412         }
413
414         if(iter.body.body.empty())
415                 append(" { }");
416         else
417         {
418                 append('\n');
419                 iter.body.visit(*this);
420         }
421 }
422
423 void Formatter::visit(Passthrough &pass)
424 {
425         append("passthrough");
426         if(pass.subscript)
427         {
428                 append('[');
429                 pass.subscript->visit(*this);
430                 append(']');
431         }
432         append(';');
433 }
434
435 void Formatter::visit(Return &ret)
436 {
437         append("return");
438         if(ret.expression)
439         {
440                 append(' ');
441                 ret.expression->visit(*this);
442         }
443         append(';');
444 }
445
446 void Formatter::visit(Jump &jump)
447 {
448         append(jump.keyword);
449         append(';');
450 }
451
452 } // namespace SL
453 } // namespace GL
454 } // namespace Msp