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