]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/output.cpp
Clear load ID when assigning to a component
[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 string Formatter::apply(Stage &s)
12 {
13         stage = &s;
14         omit_builtin = true;
15
16         const Version &ver = s.required_features.glsl_version;
17
18         if(ver)
19         {
20                 append(format("#version %d%02d", ver.major, ver.minor));
21                 if(s.required_features.target_api==OPENGL_ES && ver>=Version(3, 0))
22                         append(" es");
23                 formatted += '\n';
24         }
25
26         if(s.required_features.arb_enhanced_layouts)
27                 append("#extension GL_ARB_enhanced_layouts: require\n");
28         if(s.required_features.arb_explicit_attrib_location)
29                 append("#extension GL_ARB_explicit_attrib_location: require\n");
30         if(s.required_features.arb_explicit_uniform_location)
31                 append("#extension GL_ARB_explicit_uniform_location: require\n");
32         if(s.required_features.arb_gpu_shader5)
33                 append("#extension GL_ARB_gpu_shader5: require\n");
34         if(s.required_features.arb_separate_shader_objects)
35                 append("#extension GL_ARB_separate_shader_objects: require\n");
36         if(s.required_features.arb_uniform_buffer_object)
37                 append("#extension GL_ARB_uniform_buffer_object: require\n");
38         if(s.required_features.ext_gpu_shader4)
39                 append("#extension GL_EXT_gpu_shader4: require\n");
40         if(s.required_features.ext_texture_array)
41                 append("#extension GL_EXT_texture_array: require\n");
42         formatted += '\n';
43
44         s.content.visit(*this);
45
46         return formatted;
47 }
48
49 void Formatter::append(const string &text)
50 {
51         formatted += text;
52         for(char c: text)
53                 if(c=='\n')
54                         ++source_line;
55 }
56
57 void Formatter::append(char c)
58 {
59         formatted += c;
60         if(c=='\n')
61                 ++source_line;
62 }
63
64 void Formatter::set_source(unsigned index, unsigned line)
65 {
66         if(index!=source_index || (index && line!=source_line))
67         {
68                 if(index==source_index && line==source_line+1)
69                         formatted += '\n';
70                 else
71                 {
72                         unsigned l = line;
73                         if(stage && stage->required_features.glsl_version && stage->required_features.glsl_version<Version(3, 30))
74                                 --l;
75                         formatted += format("#line %d %d\n", l, index);
76                 }
77         }
78         source_index = index;
79         source_line = line;
80 }
81
82 void Formatter::visit(Block &block)
83 {
84         unsigned brace_indent = indent;
85         bool use_braces = (block.use_braces || (indent && block.body.size()!=1));
86         if(use_braces)
87                 append(format("%s{\n", string(brace_indent*2, ' ')));
88
89         SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
90         string spaces(indent*2, ' ');
91         bool first = true;
92         for(const RefPtr<Statement> &s: block.body)
93         {
94                 if(omit_builtin && s->source<=BUILTIN_SOURCE)
95                         continue;
96                 if(!first)
97                         append('\n');
98                 first = false;
99                 set_source(s->source, s->line);
100                 append(spaces);
101                 s->visit(*this);
102         }
103
104         if(use_braces)
105                 append(format("\n%s}", string(brace_indent*2, ' ')));
106 }
107
108 void Formatter::visit_expression(Expression &expr, const Operator *outer_oper, bool on_rhs)
109 {
110         unsigned outer_precedence = (outer_oper ? outer_oper->precedence : 20);
111         unsigned inner_precedence = (expr.oper ? expr.oper->precedence : 0);
112
113         bool needs_parentheses = (inner_precedence>=outer_precedence);
114
115         // Omit parentheses if the outer operator encloses this operand.
116         if(outer_oper && outer_oper->type==Operator::BINARY && outer_oper->token2[0] && on_rhs)
117                 needs_parentheses = false;
118
119         if(expr.oper)
120         {
121                 /* Omit parentheses if the inner expression's operator sits between the
122                 expression and the outer operator. */
123                 bool oper_on_left = expr.oper->type==Operator::PREFIX;
124                 bool oper_on_right = expr.oper->type==Operator::POSTFIX || (expr.oper->type==Operator::BINARY && expr.oper->token2[0]);
125                 if(expr.oper && ((oper_on_left && on_rhs) || (oper_on_right && !on_rhs)))
126                         needs_parentheses = false;
127
128                 // Omit parentheses if the operator's natural grouping works out.
129                 if(expr.oper==outer_oper)
130                         needs_parentheses = (expr.oper->assoc!=Operator::ASSOCIATIVE && on_rhs!=(expr.oper->assoc==Operator::RIGHT_TO_LEFT));
131         }
132
133         if(needs_parentheses)
134                 append('(');
135         expr.visit(*this);
136         if(needs_parentheses)
137                 append(')');
138 }
139
140 void Formatter::visit(Literal &literal)
141 {
142         append(literal.token);
143 }
144
145 void Formatter::visit(VariableReference &var)
146 {
147         r_empty_name = var.name.find(' ')!=string::npos;
148         if(!r_empty_name)
149                 append(var.name);
150 }
151
152 void Formatter::visit(MemberAccess &memacc)
153 {
154         visit_expression(*memacc.left, memacc.oper, false);
155         if(!r_empty_name)
156                 append('.');
157         append(memacc.member);
158         r_empty_name = false;
159 }
160
161 void Formatter::visit(Swizzle &swizzle)
162 {
163         visit_expression(*swizzle.left, swizzle.oper, false);
164         append(format(".%s", swizzle.component_group));
165 }
166
167 void Formatter::visit(UnaryExpression &unary)
168 {
169         if(unary.oper->type==Operator::PREFIX)
170                 append(unary.oper->token);
171         visit_expression(*unary.expression, unary.oper, unary.oper->type==Operator::PREFIX);
172         if(unary.oper->type==Operator::POSTFIX)
173                 append(unary.oper->token);
174 }
175
176 void Formatter::visit(BinaryExpression &binary)
177 {
178         visit_expression(*binary.left, binary.oper, false);
179         append(binary.oper->token);
180         visit_expression(*binary.right, binary.oper, true);
181         if(binary.oper->token2[0])
182                 append(binary.oper->token2);
183 }
184
185 void Formatter::visit(Assignment &assign)
186 {
187         visit_expression(*assign.left, assign.oper, false);
188         append(format(" %s ", assign.oper->token));
189         visit_expression(*assign.right, assign.oper, true);
190 }
191
192 void Formatter::visit(TernaryExpression &ternary)
193 {
194         visit_expression(*ternary.condition, ternary.oper, false);
195         append(ternary.oper->token);
196         visit_expression(*ternary.true_expr, ternary.oper, false);
197         if(ternary.oper->token2[0])
198                 append(ternary.oper->token2);
199         visit_expression(*ternary.false_expr, ternary.oper, true);
200 }
201
202 void Formatter::visit(FunctionCall &call)
203 {
204         append(format("%s(", call.name));
205         for(auto i=call.arguments.begin(); i!=call.arguments.end(); ++i)
206         {
207                 if(i!=call.arguments.begin())
208                         append(", ");
209                 (*i)->visit(*this);
210         }
211         append(')');
212 }
213
214 void Formatter::visit(ExpressionStatement &expr)
215 {
216         expr.expression->visit(*this);
217         append(';');
218 }
219
220 void Formatter::visit(Import &import)
221 {
222         append(format("import %s;", import.module));
223 }
224
225 void Formatter::visit(Precision &prec)
226 {
227         append(format("precision %s %s;", prec.precision, prec.type));
228 }
229
230 void Formatter::visit(Layout &layout)
231 {
232         append("layout(");
233         for(auto i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
234         {
235                 if(i!=layout.qualifiers.begin())
236                         append(", ");
237                 append(i->name);
238                 if(i->has_value)
239                         append(format("=%d", i->value));
240         }
241         append(')');
242 }
243
244 void Formatter::visit(InterfaceLayout &layout)
245 {
246         layout.layout.visit(*this);
247         append(format(" %s;", layout.interface));
248 }
249
250 void Formatter::visit(StructDeclaration &strct)
251 {
252         if(!strct.block_name.empty())
253                 return;
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
288         if(var.block_declaration)
289         {
290                 append(format("%s\n", var.block_declaration->block_name));
291                 var.block_declaration->members.visit(*this);
292                 if(var.name.find(' ')==string::npos)
293                         append(format(" %s", var.name));
294         }
295         else
296         {
297                 string type_name = var.type_declaration->name;
298                 if(const ImageTypeDeclaration *image = dynamic_cast<const ImageTypeDeclaration *>(var.type_declaration))
299                         if(image->base_image)
300                                 type_name = image->base_image->name;
301                 if(var.array)
302                         type_name = type_name.substr(0, type_name.find('['));
303                 append(format("%s %s", type_name, var.name));
304         }
305
306         if(var.array)
307         {
308                 append('[');
309                 if(var.array_size)
310                         var.array_size->visit(*this);
311                 append(']');
312         }
313         if(var.init_expression)
314         {
315                 append(" = ");
316                 var.init_expression->visit(*this);
317         }
318         if(!parameter_list)
319                 append(';');
320 }
321
322 void Formatter::visit(FunctionDeclaration &func)
323 {
324         append(format("%s %s(", func.return_type_declaration->name, func.name));
325         for(auto i=func.parameters.begin(); i!=func.parameters.end(); ++i)
326         {
327                 if(i!=func.parameters.begin())
328                         append(", ");
329                 SetFlag set(parameter_list);
330                 (*i)->visit(*this);
331         }
332         append(')');
333         if(func.definition==&func)
334         {
335                 append('\n');
336                 func.body.visit(*this);
337         }
338         else
339                 append(';');
340 }
341
342 void Formatter::visit(Conditional &cond)
343 {
344         append("if(");
345         cond.condition->visit(*this);
346         append(")\n");
347
348         cond.body.visit(*this);
349         if(!cond.else_body.body.empty())
350         {
351                 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
352                 if(cond.else_body.body.size()==1 && else_cond)
353                 {
354                         append('\n');
355                         set_source(else_cond->source, else_cond->line);
356                         append(format("%selse ", string(indent*2, ' ')));
357                         else_cond->visit(*this);
358                 }
359                 else
360                 {
361                         append(format("\n%selse\n", string(indent*2, ' ')));
362                         cond.else_body.visit(*this);
363                 }
364         }
365 }
366
367 void Formatter::visit(Iteration &iter)
368 {
369         if(!iter.init_statement && iter.condition && !iter.loop_expression)
370         {
371                 append("while(");
372                 iter.condition->visit(*this);
373                 append(')');
374         }
375         else
376         {
377                 append("for(");
378                 if(iter.init_statement)
379                         iter.init_statement->visit(*this);
380                 else
381                         append(';');
382                 if(iter.condition)
383                 {
384                         append(' ');
385                         iter.condition->visit(*this);
386                 }
387                 append(';');
388                 if(iter.loop_expression)
389                 {
390                         append(' ');
391                         iter.loop_expression->visit(*this);
392                 }
393                 append(')');
394         }
395
396         if(iter.body.body.empty())
397                 append(" { }");
398         else
399         {
400                 append('\n');
401                 iter.body.visit(*this);
402         }
403 }
404
405 void Formatter::visit(Passthrough &pass)
406 {
407         append("passthrough");
408         if(pass.subscript)
409         {
410                 append('[');
411                 pass.subscript->visit(*this);
412                 append(']');
413         }
414         append(';');
415 }
416
417 void Formatter::visit(Return &ret)
418 {
419         append("return");
420         if(ret.expression)
421         {
422                 append(' ');
423                 ret.expression->visit(*this);
424         }
425         append(';');
426 }
427
428 void Formatter::visit(Jump &jump)
429 {
430         append(jump.keyword);
431         append(';');
432 }
433
434 } // namespace SL
435 } // namespace GL
436 } // namespace Msp