]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/output.cpp
Improve support for 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         mode(Compiler::PROGRAM),
14         source_index(0),
15         source_line(1),
16         indent(0),
17         parameter_list(false)
18 { }
19
20 const string &Formatter::apply(Stage &s, Compiler::Mode m)
21 {
22         mode = m;
23         stage = &s;
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(mode==Compiler::PROGRAM && stage && 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(Literal &literal)
86 {
87         append(literal.token);
88 }
89
90 void Formatter::visit(ParenthesizedExpression &parexpr)
91 {
92         append('(');
93         parexpr.expression->visit(*this);
94         append(')');
95 }
96
97 void Formatter::visit(VariableReference &var)
98 {
99         append(var.name);
100 }
101
102 void Formatter::visit(InterfaceBlockReference &iface)
103 {
104         append(iface.name);
105 }
106
107 void Formatter::visit(MemberAccess &memacc)
108 {
109         memacc.left->visit(*this);
110         append(format(".%s", memacc.member));
111 }
112
113 void Formatter::visit(UnaryExpression &unary)
114 {
115         if(unary.prefix)
116                 append(unary.oper);
117         unary.expression->visit(*this);
118         if(!unary.prefix)
119                 append(unary.oper);
120 }
121
122 void Formatter::visit(BinaryExpression &binary)
123 {
124         binary.left->visit(*this);
125         append(binary.oper);
126         binary.right->visit(*this);
127         append(binary.after);
128 }
129
130 void Formatter::visit(Assignment &assign)
131 {
132         assign.left->visit(*this);
133         append(format(" %s ", assign.oper));
134         assign.right->visit(*this);
135 }
136
137 void Formatter::visit(FunctionCall &call)
138 {
139         append(format("%s(", call.name));
140         for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
141         {
142                 if(i!=call.arguments.begin())
143                         append(", ");
144                 (*i)->visit(*this);
145         }
146         append(')');
147 }
148
149 void Formatter::visit(ExpressionStatement &expr)
150 {
151         expr.expression->visit(*this);
152         append(';');
153 }
154
155 void Formatter::visit(Block &block)
156 {
157         unsigned brace_indent = indent;
158         bool use_braces = (block.use_braces || (indent && block.body.size()!=1));
159         if(use_braces)
160                 append(format("%s{\n", string(brace_indent*2, ' ')));
161
162         SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
163         string spaces(indent*2, ' ');
164         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
165         {
166                 if(i!=block.body.begin())
167                         append('\n');
168                 set_source((*i)->source, (*i)->line);
169                 append(spaces);
170                 (*i)->visit(*this);
171         }
172
173         if(use_braces)
174                 append(format("\n%s}", string(brace_indent*2, ' ')));
175 }
176
177 void Formatter::visit(Import &import)
178 {
179         append(format("import %s;", import.module));
180 }
181
182 void Formatter::visit(Precision &prec)
183 {
184         append(format("precision %s %s;", prec.precision, prec.type));
185 }
186
187 void Formatter::visit(Layout &layout)
188 {
189         append("layout(");
190         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
191         {
192                 if(i!=layout.qualifiers.begin())
193                         append(", ");
194                 append(i->name);
195                 if(i->has_value)
196                         append(format("=%d", i->value));
197         }
198         append(')');
199 }
200
201 void Formatter::visit(InterfaceLayout &layout)
202 {
203         layout.layout.visit(*this);
204         append(format(" %s;", layout.interface));
205 }
206
207 void Formatter::visit(StructDeclaration &strct)
208 {
209         append(format("struct %s\n", strct.name));
210         strct.members.visit(*this);
211         append(';');
212 }
213
214 void Formatter::visit(VariableDeclaration &var)
215 {
216         if(var.layout)
217         {
218                 var.layout->visit(*this);
219                 append(' ');
220         }
221         if(var.constant)
222                 append("const ");
223         if(!var.interpolation.empty())
224                 append(format("%s ", var.interpolation));
225         if(!var.sampling.empty())
226                 append(format("%s ", var.sampling));
227         if(!var.interface.empty() && var.interface!=block_interface)
228         {
229                 string interface = var.interface;
230                 if(mode==Compiler::PROGRAM && stage && stage->required_features.glsl_version<Version(1, 30))
231                 {
232                         if(stage->type==Stage::VERTEX && var.interface=="in")
233                                 interface = "attribute";
234                         else if((stage->type==Stage::VERTEX && var.interface=="out") || (stage->type==Stage::FRAGMENT && var.interface=="in"))
235                                 interface = "varying";
236                 }
237                 append(format("%s ", interface));
238         }
239         if(!var.precision.empty())
240                 append(format("%s ", var.precision));
241         append(format("%s %s", var.type, var.name));
242         if(var.array)
243         {
244                 append('[');
245                 if(var.array_size)
246                         var.array_size->visit(*this);
247                 append(']');
248         }
249         if(var.init_expression)
250         {
251                 append(" = ");
252                 var.init_expression->visit(*this);
253         }
254         if(!parameter_list)
255                 append(';');
256 }
257
258 void Formatter::visit(InterfaceBlock &iface)
259 {
260         SetForScope<string> set(block_interface, iface.interface);
261         append(format("%s %s\n", iface.interface, iface.name));
262         iface.members.visit(*this);
263         if(!iface.instance_name.empty())
264         {
265                 append(' ');
266                 append(iface.instance_name);
267                 if(iface.array)
268                         append("[]");
269         }
270         append(';');
271 }
272
273 void Formatter::visit(FunctionDeclaration &func)
274 {
275         append(format("%s %s(", func.return_type, func.name));
276         for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
277         {
278                 if(i!=func.parameters.begin())
279                         append(", ");
280                 SetFlag set(parameter_list);
281                 (*i)->visit(*this);
282         }
283         append(')');
284         if(func.definition==&func)
285         {
286                 append('\n');
287                 func.body.visit(*this);
288         }
289         else
290                 append(';');
291 }
292
293 void Formatter::visit(Conditional &cond)
294 {
295         append("if(");
296         cond.condition->visit(*this);
297         append(")\n");
298
299         cond.body.visit(*this);
300         if(!cond.else_body.body.empty())
301         {
302                 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
303                 if(cond.else_body.body.size()==1 && else_cond)
304                 {
305                         append('\n');
306                         set_source(else_cond->source, else_cond->line);
307                         append(format("%selse ", string(indent*2, ' ')));
308                         else_cond->visit(*this);
309                 }
310                 else
311                 {
312                         append(format("\n%selse\n", string(indent*2, ' ')));
313                         cond.else_body.visit(*this);
314                 }
315         }
316 }
317
318 void Formatter::visit(Iteration &iter)
319 {
320         if(!iter.init_statement && iter.condition && !iter.loop_expression)
321         {
322                 append("while(");
323                 iter.condition->visit(*this);
324                 append(')');
325         }
326         else
327         {
328                 append("for(");
329                 if(iter.init_statement)
330                         iter.init_statement->visit(*this);
331                 else
332                         append(';');
333                 if(iter.condition)
334                 {
335                         append(' ');
336                         iter.condition->visit(*this);
337                 }
338                 append(';');
339                 if(iter.loop_expression)
340                 {
341                         append(' ');
342                         iter.loop_expression->visit(*this);
343                 }
344                 append(')');
345         }
346
347         if(iter.body.body.empty())
348                 append(" { }");
349         else
350         {
351                 append('\n');
352                 iter.body.visit(*this);
353         }
354 }
355
356 void Formatter::visit(Passthrough &pass)
357 {
358         append("passthrough");
359         if(pass.subscript)
360         {
361                 append('[');
362                 pass.subscript->visit(*this);
363                 append(']');
364         }
365         append(';');
366 }
367
368 void Formatter::visit(Return &ret)
369 {
370         append("return");
371         if(ret.expression)
372         {
373                 append(' ');
374                 ret.expression->visit(*this);
375         }
376         append(';');
377 }
378
379 void Formatter::visit(Jump &jump)
380 {
381         append(jump.keyword);
382         append(';');
383 }
384
385 } // namespace SL
386 } // namespace GL
387 } // namespace Msp