1 #include <msp/core/raii.h>
2 #include <msp/strings/format.h>
11 string Formatter::apply(Stage &s)
16 const Version &ver = s.required_features.glsl_version;
20 append(format("#version %d%02d", ver.major, ver.minor));
21 if(s.required_features.target_api==OPENGL_ES && ver>=Version(3, 0))
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");
44 s.content.visit(*this);
49 void Formatter::append(const string &text)
57 void Formatter::append(char c)
64 void Formatter::set_source(unsigned index, unsigned line)
66 if(index!=source_index || (index && line!=source_line))
68 if(index==source_index && line==source_line+1)
73 if(stage && stage->required_features.glsl_version && stage->required_features.glsl_version<Version(3, 30))
75 formatted += format("#line %d %d\n", l, index);
82 void Formatter::visit(Block &block)
84 unsigned brace_indent = indent;
85 bool use_braces = (block.use_braces || (indent && block.body.size()!=1));
87 append(format("%s{\n", string(brace_indent*2, ' ')));
89 SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
90 string spaces(indent*2, ' ');
92 for(const RefPtr<Statement> &s: block.body)
94 if(omit_builtin && s->source<=BUILTIN_SOURCE)
99 set_source(s->source, s->line);
105 append(format("\n%s}", string(brace_indent*2, ' ')));
108 void Formatter::visit_expression(Expression &expr, const Operator *outer_oper, bool on_rhs)
110 unsigned outer_precedence = (outer_oper ? outer_oper->precedence : 20);
111 unsigned inner_precedence = (expr.oper ? expr.oper->precedence : 0);
113 bool needs_parentheses = (inner_precedence>=outer_precedence);
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;
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;
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));
133 if(needs_parentheses)
136 if(needs_parentheses)
140 void Formatter::visit(Literal &literal)
142 append(literal.token);
145 void Formatter::visit(VariableReference &var)
147 r_empty_name = var.name.find(' ')!=string::npos;
152 void Formatter::visit(MemberAccess &memacc)
154 visit_expression(*memacc.left, memacc.oper, false);
157 append(memacc.member);
158 r_empty_name = false;
161 void Formatter::visit(Swizzle &swizzle)
163 visit_expression(*swizzle.left, swizzle.oper, false);
164 append(format(".%s", swizzle.component_group));
167 void Formatter::visit(UnaryExpression &unary)
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);
176 void Formatter::visit(BinaryExpression &binary)
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);
185 void Formatter::visit(Assignment &assign)
187 visit_expression(*assign.left, assign.oper, false);
188 append(format(" %s ", assign.oper->token));
189 visit_expression(*assign.right, assign.oper, true);
192 void Formatter::visit(TernaryExpression &ternary)
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);
202 void Formatter::visit(FunctionCall &call)
204 append(format("%s(", call.name));
205 for(auto i=call.arguments.begin(); i!=call.arguments.end(); ++i)
207 if(i!=call.arguments.begin())
214 void Formatter::visit(ExpressionStatement &expr)
216 expr.expression->visit(*this);
220 void Formatter::visit(Import &import)
222 append(format("import %s;", import.module));
225 void Formatter::visit(Precision &prec)
227 append(format("precision %s %s;", prec.precision, prec.type));
230 void Formatter::visit(Layout &layout)
233 for(auto i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
235 if(i!=layout.qualifiers.begin())
239 append(format("=%d", i->value));
244 void Formatter::visit(InterfaceLayout &layout)
246 layout.layout.visit(*this);
247 append(format(" %s;", layout.interface));
250 void Formatter::visit(StructDeclaration &strct)
252 if(!strct.block_name.empty())
255 append(format("struct %s\n", strct.name));
256 strct.members.visit(*this);
260 void Formatter::visit(VariableDeclaration &var)
264 var.layout->visit(*this);
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())
275 string interface = var.interface;
276 if(stage && stage->required_features.glsl_version && stage->required_features.glsl_version<Version(1, 30))
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";
283 append(format("%s ", interface));
285 if(!var.precision.empty())
286 append(format("%s ", var.precision));
288 if(var.block_declaration)
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));
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;
302 type_name = type_name.substr(0, type_name.find('['));
303 append(format("%s %s", type_name, var.name));
310 var.array_size->visit(*this);
313 if(var.init_expression)
316 var.init_expression->visit(*this);
322 void Formatter::visit(FunctionDeclaration &func)
324 append(format("%s %s(", func.return_type_declaration->name, func.name));
325 for(auto i=func.parameters.begin(); i!=func.parameters.end(); ++i)
327 if(i!=func.parameters.begin())
329 SetFlag set(parameter_list);
333 if(func.definition==&func)
336 func.body.visit(*this);
342 void Formatter::visit(Conditional &cond)
345 cond.condition->visit(*this);
348 cond.body.visit(*this);
349 if(!cond.else_body.body.empty())
351 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
352 if(cond.else_body.body.size()==1 && else_cond)
355 set_source(else_cond->source, else_cond->line);
356 append(format("%selse ", string(indent*2, ' ')));
357 else_cond->visit(*this);
361 append(format("\n%selse\n", string(indent*2, ' ')));
362 cond.else_body.visit(*this);
367 void Formatter::visit(Iteration &iter)
369 if(!iter.init_statement && iter.condition && !iter.loop_expression)
372 iter.condition->visit(*this);
378 if(iter.init_statement)
379 iter.init_statement->visit(*this);
385 iter.condition->visit(*this);
388 if(iter.loop_expression)
391 iter.loop_expression->visit(*this);
396 if(iter.body.body.empty())
401 iter.body.visit(*this);
405 void Formatter::visit(Passthrough &pass)
407 append("passthrough");
411 pass.subscript->visit(*this);
417 void Formatter::visit(Return &ret)
423 ret.expression->visit(*this);
428 void Formatter::visit(Jump &jump)
430 append(jump.keyword);