]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/output.cpp
Check the flat qualifier from the correct member
[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         r_empty_name = var.name.find(' ')!=string::npos;
148         if(!r_empty_name)
149                 append(var.name);
150 }
151
152 void Formatter::visit(MemberAccess &memacc)
153 {
154         visit_expression(*memacc.left, memacc.oper, false);
155         if(!r_empty_name)
156                 append('.');
157         append(memacc.member);
158         r_empty_name = false;
159 }
160
161 void Formatter::visit(Swizzle &swizzle)
162 {
163         visit_expression(*swizzle.left, swizzle.oper, false);
164         append(format(".%s", swizzle.component_group));
165 }
166
167 void Formatter::visit(UnaryExpression &unary)
168 {
169         if(unary.oper->type==Operator::PREFIX)
170                 append(unary.oper->token);
171         visit_expression(*unary.expression, unary.oper, unary.oper->type==Operator::PREFIX);
172         if(unary.oper->type==Operator::POSTFIX)
173                 append(unary.oper->token);
174 }
175
176 void Formatter::visit(BinaryExpression &binary)
177 {
178         visit_expression(*binary.left, binary.oper, false);
179         append(binary.oper->token);
180         visit_expression(*binary.right, binary.oper, true);
181         if(binary.oper->token2[0])
182                 append(binary.oper->token2);
183 }
184
185 void Formatter::visit(Assignment &assign)
186 {
187         visit_expression(*assign.left, assign.oper, false);
188         append(format(" %s ", assign.oper->token));
189         visit_expression(*assign.right, assign.oper, true);
190 }
191
192 void Formatter::visit(TernaryExpression &ternary)
193 {
194         visit_expression(*ternary.condition, ternary.oper, false);
195         append(ternary.oper->token);
196         visit_expression(*ternary.true_expr, ternary.oper, false);
197         if(ternary.oper->token2[0])
198                 append(ternary.oper->token2);
199         visit_expression(*ternary.false_expr, ternary.oper, true);
200 }
201
202 void Formatter::visit(FunctionCall &call)
203 {
204         append(format("%s(", call.name));
205         for(auto i=call.arguments.begin(); i!=call.arguments.end(); ++i)
206         {
207                 if(i!=call.arguments.begin())
208                         append(", ");
209                 (*i)->visit(*this);
210         }
211         append(')');
212 }
213
214 void Formatter::visit(ExpressionStatement &expr)
215 {
216         expr.expression->visit(*this);
217         append(';');
218 }
219
220 void Formatter::visit(Import &import)
221 {
222         append(format("import %s;", import.module));
223 }
224
225 void Formatter::visit(Precision &prec)
226 {
227         append(format("precision %s %s;", prec.precision, prec.type));
228 }
229
230 void Formatter::visit(Layout &layout)
231 {
232         append("layout(");
233         for(auto i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
234         {
235                 if(i!=layout.qualifiers.begin())
236                         append(", ");
237                 append(i->name);
238                 if(i->has_value)
239                         append(format("=%d", i->value));
240         }
241         append(')');
242 }
243
244 void Formatter::visit(InterfaceLayout &layout)
245 {
246         layout.layout.visit(*this);
247         append(format(" %s;", layout.interface));
248 }
249
250 void Formatter::visit(StructDeclaration &strct)
251 {
252         if(!strct.block_name.empty())
253                 return;
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
288         if(var.block_declaration)
289         {
290                 append(format("%s\n", var.block_declaration->block_name));
291                 var.block_declaration->members.visit(*this);
292                 if(var.name.find(' ')==string::npos)
293                         append(format(" %s", var.name));
294         }
295         else
296         {
297                 string type_name = var.type_declaration->name;
298                 if(var.array)
299                         type_name = type_name.substr(0, type_name.find('['));
300                 append(format("%s %s", type_name, var.name));
301         }
302
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(FunctionDeclaration &func)
320 {
321         append(format("%s %s(", func.return_type_declaration->name, func.name));
322         for(auto i=func.parameters.begin(); i!=func.parameters.end(); ++i)
323         {
324                 if(i!=func.parameters.begin())
325                         append(", ");
326                 SetFlag set(parameter_list);
327                 (*i)->visit(*this);
328         }
329         append(')');
330         if(func.definition==&func)
331         {
332                 append('\n');
333                 func.body.visit(*this);
334         }
335         else
336                 append(';');
337 }
338
339 void Formatter::visit(Conditional &cond)
340 {
341         append("if(");
342         cond.condition->visit(*this);
343         append(")\n");
344
345         cond.body.visit(*this);
346         if(!cond.else_body.body.empty())
347         {
348                 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
349                 if(cond.else_body.body.size()==1 && else_cond)
350                 {
351                         append('\n');
352                         set_source(else_cond->source, else_cond->line);
353                         append(format("%selse ", string(indent*2, ' ')));
354                         else_cond->visit(*this);
355                 }
356                 else
357                 {
358                         append(format("\n%selse\n", string(indent*2, ' ')));
359                         cond.else_body.visit(*this);
360                 }
361         }
362 }
363
364 void Formatter::visit(Iteration &iter)
365 {
366         if(!iter.init_statement && iter.condition && !iter.loop_expression)
367         {
368                 append("while(");
369                 iter.condition->visit(*this);
370                 append(')');
371         }
372         else
373         {
374                 append("for(");
375                 if(iter.init_statement)
376                         iter.init_statement->visit(*this);
377                 else
378                         append(';');
379                 if(iter.condition)
380                 {
381                         append(' ');
382                         iter.condition->visit(*this);
383                 }
384                 append(';');
385                 if(iter.loop_expression)
386                 {
387                         append(' ');
388                         iter.loop_expression->visit(*this);
389                 }
390                 append(')');
391         }
392
393         if(iter.body.body.empty())
394                 append(" { }");
395         else
396         {
397                 append('\n');
398                 iter.body.visit(*this);
399         }
400 }
401
402 void Formatter::visit(Passthrough &pass)
403 {
404         append("passthrough");
405         if(pass.subscript)
406         {
407                 append('[');
408                 pass.subscript->visit(*this);
409                 append(']');
410         }
411         append(';');
412 }
413
414 void Formatter::visit(Return &ret)
415 {
416         append("return");
417         if(ret.expression)
418         {
419                 append(' ');
420                 ret.expression->visit(*this);
421         }
422         append(';');
423 }
424
425 void Formatter::visit(Jump &jump)
426 {
427         append(jump.keyword);
428         append(';');
429 }
430
431 } // namespace SL
432 } // namespace GL
433 } // namespace Msp