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