]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/output.cpp
43f337cabc3e6296e9afdc1f91130e3d8bd7b56f
[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 { }
18
19 const string &Formatter::apply(Stage &s)
20 {
21         stage = &s;
22
23         const Version &ver = s.required_features.glsl_version;
24
25         if(ver)
26         {
27                 append(format("#version %d%02d", ver.major, ver.minor));
28                 if(s.required_features.gl_api==OPENGL_ES2 && ver>=Version(3, 0))
29                         append(" es");
30                 formatted += '\n';
31         }
32
33         if(s.required_features.arb_explicit_attrib_location)
34                 append("#extension arb_explicit_attrib_location: require\n");
35         if(s.required_features.arb_gpu_shader5)
36                 append("#extension arb_gpu_shader5: require\n");
37         if(s.required_features.arb_uniform_buffer_object)
38                 append("#extension arb_uniform_buffer_object: require\n");
39         if(s.required_features.ext_gpu_shader4)
40                 append("#extension ext_gpu_shader4: require\n");
41         if(s.required_features.ext_texture_array)
42                 append("#extension ext_texture_array: require\n");
43         formatted += '\n';
44
45         s.content.visit(*this);
46
47         return formatted;
48 }
49
50 void Formatter::append(const string &text)
51 {
52         formatted += text;
53         for(string::const_iterator i=text.begin(); i!=text.end(); ++i)
54                 if(*i=='\n')
55                         ++source_line;
56 }
57
58 void Formatter::append(char c)
59 {
60         formatted += c;
61         if(c=='\n')
62                 ++source_line;
63 }
64
65 void Formatter::set_source(unsigned index, unsigned line)
66 {
67         if(index!=source_index || (index && line!=source_line))
68         {
69                 if(index==source_index && line==source_line+1)
70                         formatted += '\n';
71                 else
72                 {
73                         unsigned l = line;
74                         if(stage && stage->required_features.glsl_version && stage->required_features.glsl_version<Version(3, 30))
75                                 --l;
76                         formatted += format("#line %d %d\n", l, index);
77                 }
78         }
79         source_index = index;
80         source_line = line;
81 }
82
83 void Formatter::visit(Block &block)
84 {
85         unsigned brace_indent = indent;
86         bool use_braces = (block.use_braces || (indent && block.body.size()!=1));
87         if(use_braces)
88                 append(format("%s{\n", string(brace_indent*2, ' ')));
89
90         SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
91         string spaces(indent*2, ' ');
92         bool first = true;
93         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
94         {
95                 if((*i)->source<=BUILTIN_SOURCE)
96                         continue;
97                 if(!first)
98                         append('\n');
99                 first = false;
100                 set_source((*i)->source, (*i)->line);
101                 append(spaces);
102                 (*i)->visit(*this);
103         }
104
105         if(use_braces)
106                 append(format("\n%s}", string(brace_indent*2, ' ')));
107 }
108
109 void Formatter::visit(Literal &literal)
110 {
111         append(literal.token);
112 }
113
114 void Formatter::visit(ParenthesizedExpression &parexpr)
115 {
116         append('(');
117         parexpr.expression->visit(*this);
118         append(')');
119 }
120
121 void Formatter::visit(VariableReference &var)
122 {
123         append(var.name);
124 }
125
126 void Formatter::visit(InterfaceBlockReference &iface)
127 {
128         append(iface.name);
129 }
130
131 void Formatter::visit(MemberAccess &memacc)
132 {
133         memacc.left->visit(*this);
134         append(format(".%s", memacc.member));
135 }
136
137 void Formatter::visit(Swizzle &swizzle)
138 {
139         swizzle.left->visit(*this);
140         append(format(".%s", swizzle.component_group));
141 }
142
143 void Formatter::visit(UnaryExpression &unary)
144 {
145         if(unary.oper->type==Operator::PREFIX)
146                 append(unary.oper->token);
147         unary.expression->visit(*this);
148         if(unary.oper->type==Operator::POSTFIX)
149                 append(unary.oper->token);
150 }
151
152 void Formatter::visit(BinaryExpression &binary)
153 {
154         binary.left->visit(*this);
155         append(binary.oper->token);
156         binary.right->visit(*this);
157         if(binary.oper->token[0]=='[')
158                 append(']');
159 }
160
161 void Formatter::visit(Assignment &assign)
162 {
163         assign.left->visit(*this);
164         append(format(" %s ", assign.oper->token));
165         assign.right->visit(*this);
166 }
167
168 void Formatter::visit(FunctionCall &call)
169 {
170         append(format("%s(", call.name));
171         for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
172         {
173                 if(i!=call.arguments.begin())
174                         append(", ");
175                 (*i)->visit(*this);
176         }
177         append(')');
178 }
179
180 void Formatter::visit(ExpressionStatement &expr)
181 {
182         expr.expression->visit(*this);
183         append(';');
184 }
185
186 void Formatter::visit(Import &import)
187 {
188         append(format("import %s;", import.module));
189 }
190
191 void Formatter::visit(Precision &prec)
192 {
193         append(format("precision %s %s;", prec.precision, prec.type));
194 }
195
196 void Formatter::visit(Layout &layout)
197 {
198         append("layout(");
199         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
200         {
201                 if(i!=layout.qualifiers.begin())
202                         append(", ");
203                 append(i->name);
204                 if(i->has_value)
205                         append(format("=%d", i->value));
206         }
207         append(')');
208 }
209
210 void Formatter::visit(InterfaceLayout &layout)
211 {
212         layout.layout.visit(*this);
213         append(format(" %s;", layout.interface));
214 }
215
216 void Formatter::visit(StructDeclaration &strct)
217 {
218         append(format("struct %s\n", strct.name));
219         strct.members.visit(*this);
220         append(';');
221 }
222
223 void Formatter::visit(VariableDeclaration &var)
224 {
225         if(var.layout)
226         {
227                 var.layout->visit(*this);
228                 append(' ');
229         }
230         if(var.constant)
231                 append("const ");
232         if(!var.interpolation.empty())
233                 append(format("%s ", var.interpolation));
234         if(!var.sampling.empty())
235                 append(format("%s ", var.sampling));
236         if(!var.interface.empty())
237         {
238                 string interface = var.interface;
239                 if(stage && stage->required_features.glsl_version && stage->required_features.glsl_version<Version(1, 30))
240                 {
241                         if(stage->type==Stage::VERTEX && var.interface=="in")
242                                 interface = "attribute";
243                         else if((stage->type==Stage::VERTEX && var.interface=="out") || (stage->type==Stage::FRAGMENT && var.interface=="in"))
244                                 interface = "varying";
245                 }
246                 append(format("%s ", interface));
247         }
248         if(!var.precision.empty())
249                 append(format("%s ", var.precision));
250         string type_name = var.type_declaration->name;
251         if(var.array)
252                 type_name = type_name.substr(0, type_name.find('['));
253         append(format("%s %s", type_name, var.name));
254         if(var.array)
255         {
256                 append('[');
257                 if(var.array_size)
258                         var.array_size->visit(*this);
259                 append(']');
260         }
261         if(var.init_expression)
262         {
263                 append(" = ");
264                 var.init_expression->visit(*this);
265         }
266         if(!parameter_list)
267                 append(';');
268 }
269
270 void Formatter::visit(InterfaceBlock &iface)
271 {
272         append(format("%s %s\n", iface.interface, iface.name));
273         if(iface.struct_declaration)
274                 iface.struct_declaration->members.visit(*this);
275         if(!iface.instance_name.empty())
276         {
277                 append(' ');
278                 append(iface.instance_name);
279                 if(iface.array)
280                         append("[]");
281         }
282         append(';');
283 }
284
285 void Formatter::visit(FunctionDeclaration &func)
286 {
287         append(format("%s %s(", func.return_type_declaration->name, func.name));
288         for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
289         {
290                 if(i!=func.parameters.begin())
291                         append(", ");
292                 SetFlag set(parameter_list);
293                 (*i)->visit(*this);
294         }
295         append(')');
296         if(func.definition==&func)
297         {
298                 append('\n');
299                 func.body.visit(*this);
300         }
301         else
302                 append(';');
303 }
304
305 void Formatter::visit(Conditional &cond)
306 {
307         append("if(");
308         cond.condition->visit(*this);
309         append(")\n");
310
311         cond.body.visit(*this);
312         if(!cond.else_body.body.empty())
313         {
314                 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
315                 if(cond.else_body.body.size()==1 && else_cond)
316                 {
317                         append('\n');
318                         set_source(else_cond->source, else_cond->line);
319                         append(format("%selse ", string(indent*2, ' ')));
320                         else_cond->visit(*this);
321                 }
322                 else
323                 {
324                         append(format("\n%selse\n", string(indent*2, ' ')));
325                         cond.else_body.visit(*this);
326                 }
327         }
328 }
329
330 void Formatter::visit(Iteration &iter)
331 {
332         if(!iter.init_statement && iter.condition && !iter.loop_expression)
333         {
334                 append("while(");
335                 iter.condition->visit(*this);
336                 append(')');
337         }
338         else
339         {
340                 append("for(");
341                 if(iter.init_statement)
342                         iter.init_statement->visit(*this);
343                 else
344                         append(';');
345                 if(iter.condition)
346                 {
347                         append(' ');
348                         iter.condition->visit(*this);
349                 }
350                 append(';');
351                 if(iter.loop_expression)
352                 {
353                         append(' ');
354                         iter.loop_expression->visit(*this);
355                 }
356                 append(')');
357         }
358
359         if(iter.body.body.empty())
360                 append(" { }");
361         else
362         {
363                 append('\n');
364                 iter.body.visit(*this);
365         }
366 }
367
368 void Formatter::visit(Passthrough &pass)
369 {
370         append("passthrough");
371         if(pass.subscript)
372         {
373                 append('[');
374                 pass.subscript->visit(*this);
375                 append(']');
376         }
377         append(';');
378 }
379
380 void Formatter::visit(Return &ret)
381 {
382         append("return");
383         if(ret.expression)
384         {
385                 append(' ');
386                 ret.expression->visit(*this);
387         }
388         append(';');
389 }
390
391 void Formatter::visit(Jump &jump)
392 {
393         append(jump.keyword);
394         append(';');
395 }
396
397 } // namespace SL
398 } // namespace GL
399 } // namespace Msp