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(Literal &literal)
87 append(literal.token);
90 void Formatter::visit(ParenthesizedExpression &parexpr)
93 parexpr.expression->visit(*this);
97 void Formatter::visit(VariableReference &var)
102 void Formatter::visit(InterfaceBlockReference &iface)
107 void Formatter::visit(MemberAccess &memacc)
109 memacc.left->visit(*this);
110 append(format(".%s", memacc.member));
113 void Formatter::visit(UnaryExpression &unary)
117 unary.expression->visit(*this);
122 void Formatter::visit(BinaryExpression &binary)
124 binary.left->visit(*this);
126 binary.right->visit(*this);
127 append(binary.after);
130 void Formatter::visit(Assignment &assign)
132 assign.left->visit(*this);
133 append(format(" %s ", assign.oper));
134 assign.right->visit(*this);
137 void Formatter::visit(FunctionCall &call)
139 append(format("%s(", call.name));
140 for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
142 if(i!=call.arguments.begin())
149 void Formatter::visit(ExpressionStatement &expr)
151 expr.expression->visit(*this);
155 void Formatter::visit(Block &block)
157 unsigned brace_indent = indent;
158 bool use_braces = (block.use_braces || (indent && block.body.size()!=1));
160 append(format("%s{\n", string(brace_indent*2, ' ')));
162 SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
163 string spaces(indent*2, ' ');
164 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
166 if(i!=block.body.begin())
168 set_source((*i)->source, (*i)->line);
174 append(format("\n%s}", string(brace_indent*2, ' ')));
177 void Formatter::visit(Import &import)
179 append(format("import %s;", import.module));
182 void Formatter::visit(Precision &prec)
184 append(format("precision %s %s;", prec.precision, prec.type));
187 void Formatter::visit(Layout &layout)
190 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
192 if(i!=layout.qualifiers.begin())
196 append(format("=%d", i->value));
201 void Formatter::visit(InterfaceLayout &layout)
203 layout.layout.visit(*this);
204 append(format(" %s;", layout.interface));
207 void Formatter::visit(StructDeclaration &strct)
209 append(format("struct %s\n", strct.name));
210 strct.members.visit(*this);
214 void Formatter::visit(VariableDeclaration &var)
218 var.layout->visit(*this);
223 if(!var.interpolation.empty())
224 append(format("%s ", var.interpolation));
225 if(!var.sampling.empty())
226 append(format("%s ", var.sampling));
227 if(!var.interface.empty() && var.interface!=block_interface)
229 string interface = var.interface;
230 if(mode==Compiler::PROGRAM && stage && stage->required_features.glsl_version<Version(1, 30))
232 if(stage->type==Stage::VERTEX && var.interface=="in")
233 interface = "attribute";
234 else if((stage->type==Stage::VERTEX && var.interface=="out") || (stage->type==Stage::FRAGMENT && var.interface=="in"))
235 interface = "varying";
237 append(format("%s ", interface));
239 if(!var.precision.empty())
240 append(format("%s ", var.precision));
241 append(format("%s %s", var.type, var.name));
246 var.array_size->visit(*this);
249 if(var.init_expression)
252 var.init_expression->visit(*this);
258 void Formatter::visit(InterfaceBlock &iface)
260 SetForScope<string> set(block_interface, iface.interface);
261 append(format("%s %s\n", iface.interface, iface.name));
262 iface.members.visit(*this);
263 if(!iface.instance_name.empty())
266 append(iface.instance_name);
273 void Formatter::visit(FunctionDeclaration &func)
275 append(format("%s %s(", func.return_type, func.name));
276 for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
278 if(i!=func.parameters.begin())
280 SetFlag set(parameter_list);
284 if(func.definition==&func)
287 func.body.visit(*this);
293 void Formatter::visit(Conditional &cond)
296 cond.condition->visit(*this);
299 cond.body.visit(*this);
300 if(!cond.else_body.body.empty())
302 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
303 if(cond.else_body.body.size()==1 && else_cond)
306 set_source(else_cond->source, else_cond->line);
307 append(format("%selse ", string(indent*2, ' ')));
308 else_cond->visit(*this);
312 append(format("\n%selse\n", string(indent*2, ' ')));
313 cond.else_body.visit(*this);
318 void Formatter::visit(Iteration &iter)
320 if(!iter.init_statement && iter.condition && !iter.loop_expression)
323 iter.condition->visit(*this);
329 if(iter.init_statement)
330 iter.init_statement->visit(*this);
336 iter.condition->visit(*this);
339 if(iter.loop_expression)
342 iter.loop_expression->visit(*this);
347 if(iter.body.body.empty())
352 iter.body.visit(*this);
356 void Formatter::visit(Passthrough &pass)
358 append("passthrough");
362 pass.subscript->visit(*this);
368 void Formatter::visit(Return &ret)
374 ret.expression->visit(*this);
379 void Formatter::visit(Jump &jump)
381 append(jump.keyword);