1 #include <msp/core/raii.h>
2 #include <msp/strings/format.h>
11 Formatter::Formatter():
13 mode(Compiler::PROGRAM),
20 const string &Formatter::apply(Stage &s, Compiler::Mode m)
25 const Version &ver = s.required_features.glsl_version;
29 append(format("#version %d%02d", ver.major, ver.minor));
30 if(s.required_features.gl_api==OPENGL_ES2 && ver>=Version(3, 0))
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");
47 s.content.visit(*this);
52 void Formatter::append(const string &text)
55 for(string::const_iterator i=text.begin(); i!=text.end(); ++i)
60 void Formatter::append(char c)
67 void Formatter::set_source(unsigned index, unsigned line)
69 if(index!=source_index || (index && line!=source_line))
71 if(index==source_index && line==source_line+1)
76 if(mode==Compiler::PROGRAM && stage && stage->required_features.glsl_version<Version(3, 30))
78 formatted += format("#line %d %d\n", l, index);
85 void Formatter::visit(Block &block)
87 unsigned brace_indent = indent;
88 bool use_braces = (block.use_braces || (indent && block.body.size()!=1));
90 append(format("%s{\n", string(brace_indent*2, ' ')));
92 SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
93 string spaces(indent*2, ' ');
95 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
97 if((*i)->source<=BUILTIN_SOURCE)
102 set_source((*i)->source, (*i)->line);
108 append(format("\n%s}", string(brace_indent*2, ' ')));
111 void Formatter::visit(Literal &literal)
113 append(literal.token);
116 void Formatter::visit(ParenthesizedExpression &parexpr)
119 parexpr.expression->visit(*this);
123 void Formatter::visit(VariableReference &var)
128 void Formatter::visit(InterfaceBlockReference &iface)
133 void Formatter::visit(MemberAccess &memacc)
135 memacc.left->visit(*this);
136 append(format(".%s", memacc.member));
139 void Formatter::visit(Swizzle &swizzle)
141 swizzle.left->visit(*this);
142 append(format(".%s", swizzle.component_group));
145 void Formatter::visit(UnaryExpression &unary)
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);
154 void Formatter::visit(BinaryExpression &binary)
156 binary.left->visit(*this);
157 append(binary.oper->token);
158 binary.right->visit(*this);
159 if(binary.oper->token[0]=='[')
163 void Formatter::visit(Assignment &assign)
165 assign.left->visit(*this);
166 append(format(" %s ", assign.oper->token));
167 assign.right->visit(*this);
170 void Formatter::visit(FunctionCall &call)
172 append(format("%s(", call.name));
173 for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
175 if(i!=call.arguments.begin())
182 void Formatter::visit(ExpressionStatement &expr)
184 expr.expression->visit(*this);
188 void Formatter::visit(Import &import)
190 append(format("import %s;", import.module));
193 void Formatter::visit(Precision &prec)
195 append(format("precision %s %s;", prec.precision, prec.type));
198 void Formatter::visit(Layout &layout)
201 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
203 if(i!=layout.qualifiers.begin())
207 append(format("=%d", i->value));
212 void Formatter::visit(InterfaceLayout &layout)
214 layout.layout.visit(*this);
215 append(format(" %s;", layout.interface));
218 void Formatter::visit(StructDeclaration &strct)
220 append(format("struct %s\n", strct.name));
221 strct.members.visit(*this);
225 void Formatter::visit(VariableDeclaration &var)
229 var.layout->visit(*this);
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())
240 string interface = var.interface;
241 if(mode==Compiler::PROGRAM && stage && stage->required_features.glsl_version<Version(1, 30))
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";
248 append(format("%s ", interface));
250 if(!var.precision.empty())
251 append(format("%s ", var.precision));
252 string type_name = var.type_declaration->name;
254 type_name = type_name.substr(0, type_name.find('['));
255 append(format("%s %s", type_name, var.name));
260 var.array_size->visit(*this);
263 if(var.init_expression)
266 var.init_expression->visit(*this);
272 void Formatter::visit(InterfaceBlock &iface)
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())
280 append(iface.instance_name);
287 void Formatter::visit(FunctionDeclaration &func)
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)
292 if(i!=func.parameters.begin())
294 SetFlag set(parameter_list);
298 if(func.definition==&func)
301 func.body.visit(*this);
307 void Formatter::visit(Conditional &cond)
310 cond.condition->visit(*this);
313 cond.body.visit(*this);
314 if(!cond.else_body.body.empty())
316 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
317 if(cond.else_body.body.size()==1 && else_cond)
320 set_source(else_cond->source, else_cond->line);
321 append(format("%selse ", string(indent*2, ' ')));
322 else_cond->visit(*this);
326 append(format("\n%selse\n", string(indent*2, ' ')));
327 cond.else_body.visit(*this);
332 void Formatter::visit(Iteration &iter)
334 if(!iter.init_statement && iter.condition && !iter.loop_expression)
337 iter.condition->visit(*this);
343 if(iter.init_statement)
344 iter.init_statement->visit(*this);
350 iter.condition->visit(*this);
353 if(iter.loop_expression)
356 iter.loop_expression->visit(*this);
361 if(iter.body.body.empty())
366 iter.body.visit(*this);
370 void Formatter::visit(Passthrough &pass)
372 append("passthrough");
376 pass.subscript->visit(*this);
382 void Formatter::visit(Return &ret)
388 ret.expression->visit(*this);
393 void Formatter::visit(Jump &jump)
395 append(jump.keyword);