]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/output.cpp
Support layout qualifiers on GLSL interface blocks
[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         if(iface.layout)
310         {
311                 iface.layout->visit(*this);
312                 append(' ');
313         }
314         append(format("%s %s\n", iface.interface, iface.block_name));
315         if(iface.struct_declaration)
316                 iface.struct_declaration->members.visit(*this);
317         if(!iface.instance_name.empty())
318         {
319                 append(' ');
320                 append(iface.instance_name);
321                 if(iface.array)
322                         append("[]");
323         }
324         append(';');
325 }
326
327 void Formatter::visit(FunctionDeclaration &func)
328 {
329         append(format("%s %s(", func.return_type_declaration->name, func.name));
330         for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
331         {
332                 if(i!=func.parameters.begin())
333                         append(", ");
334                 SetFlag set(parameter_list);
335                 (*i)->visit(*this);
336         }
337         append(')');
338         if(func.definition==&func)
339         {
340                 append('\n');
341                 func.body.visit(*this);
342         }
343         else
344                 append(';');
345 }
346
347 void Formatter::visit(Conditional &cond)
348 {
349         append("if(");
350         cond.condition->visit(*this);
351         append(")\n");
352
353         cond.body.visit(*this);
354         if(!cond.else_body.body.empty())
355         {
356                 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
357                 if(cond.else_body.body.size()==1 && else_cond)
358                 {
359                         append('\n');
360                         set_source(else_cond->source, else_cond->line);
361                         append(format("%selse ", string(indent*2, ' ')));
362                         else_cond->visit(*this);
363                 }
364                 else
365                 {
366                         append(format("\n%selse\n", string(indent*2, ' ')));
367                         cond.else_body.visit(*this);
368                 }
369         }
370 }
371
372 void Formatter::visit(Iteration &iter)
373 {
374         if(!iter.init_statement && iter.condition && !iter.loop_expression)
375         {
376                 append("while(");
377                 iter.condition->visit(*this);
378                 append(')');
379         }
380         else
381         {
382                 append("for(");
383                 if(iter.init_statement)
384                         iter.init_statement->visit(*this);
385                 else
386                         append(';');
387                 if(iter.condition)
388                 {
389                         append(' ');
390                         iter.condition->visit(*this);
391                 }
392                 append(';');
393                 if(iter.loop_expression)
394                 {
395                         append(' ');
396                         iter.loop_expression->visit(*this);
397                 }
398                 append(')');
399         }
400
401         if(iter.body.body.empty())
402                 append(" { }");
403         else
404         {
405                 append('\n');
406                 iter.body.visit(*this);
407         }
408 }
409
410 void Formatter::visit(Passthrough &pass)
411 {
412         append("passthrough");
413         if(pass.subscript)
414         {
415                 append('[');
416                 pass.subscript->visit(*this);
417                 append(']');
418         }
419         append(';');
420 }
421
422 void Formatter::visit(Return &ret)
423 {
424         append("return");
425         if(ret.expression)
426         {
427                 append(' ');
428                 ret.expression->visit(*this);
429         }
430         append(';');
431 }
432
433 void Formatter::visit(Jump &jump)
434 {
435         append(jump.keyword);
436         append(';');
437 }
438
439 } // namespace SL
440 } // namespace GL
441 } // namespace Msp