]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/output.cpp
Implement the ternary operator in GLSL
[libs/gl.git] / source / glsl / output.cpp
index 46314588858667c059a80c131bb6f527b3ba9bc6..364e5e4b23c02356a2c72743103ba212a32abd91 100644 (file)
@@ -10,17 +10,17 @@ namespace SL {
 
 Formatter::Formatter():
        stage(0),
-       mode(Compiler::PROGRAM),
        source_index(0),
        source_line(1),
        indent(0),
-       parameter_list(false)
+       parameter_list(false),
+       omit_builtin(false)
 { }
 
-const string &Formatter::apply(Stage &s, Compiler::Mode m)
+const string &Formatter::apply(Stage &s)
 {
-       mode = m;
        stage = &s;
+       omit_builtin = true;
 
        const Version &ver = s.required_features.glsl_version;
 
@@ -44,7 +44,7 @@ const string &Formatter::apply(Stage &s, Compiler::Mode m)
                append("#extension ext_texture_array: require\n");
        formatted += '\n';
 
-       visit(s.content);
+       s.content.visit(*this);
 
        return formatted;
 }
@@ -73,7 +73,7 @@ void Formatter::set_source(unsigned index, unsigned line)
                else
                {
                        unsigned l = line;
-                       if(mode==Compiler::PROGRAM && stage && stage->required_features.glsl_version<Version(3, 30))
+                       if(stage && stage->required_features.glsl_version && stage->required_features.glsl_version<Version(3, 30))
                                --l;
                        formatted += format("#line %d %d\n", l, index);
                }
@@ -82,6 +82,32 @@ void Formatter::set_source(unsigned index, unsigned line)
        source_line = line;
 }
 
+void Formatter::visit(Block &block)
+{
+       unsigned brace_indent = indent;
+       bool use_braces = (block.use_braces || (indent && block.body.size()!=1));
+       if(use_braces)
+               append(format("%s{\n", string(brace_indent*2, ' ')));
+
+       SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
+       string spaces(indent*2, ' ');
+       bool first = true;
+       for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+       {
+               if(omit_builtin && (*i)->source<=BUILTIN_SOURCE)
+                       continue;
+               if(!first)
+                       append('\n');
+               first = false;
+               set_source((*i)->source, (*i)->line);
+               append(spaces);
+               (*i)->visit(*this);
+       }
+
+       if(use_braces)
+               append(format("\n%s}", string(brace_indent*2, ' ')));
+}
+
 void Formatter::visit(Literal &literal)
 {
        append(literal.token);
@@ -99,36 +125,58 @@ void Formatter::visit(VariableReference &var)
        append(var.name);
 }
 
+void Formatter::visit(InterfaceBlockReference &iface)
+{
+       append(iface.name);
+}
+
 void Formatter::visit(MemberAccess &memacc)
 {
        memacc.left->visit(*this);
        append(format(".%s", memacc.member));
 }
 
+void Formatter::visit(Swizzle &swizzle)
+{
+       swizzle.left->visit(*this);
+       append(format(".%s", swizzle.component_group));
+}
+
 void Formatter::visit(UnaryExpression &unary)
 {
-       if(unary.prefix)
-               append(unary.oper);
+       if(unary.oper->type==Operator::PREFIX)
+               append(unary.oper->token);
        unary.expression->visit(*this);
-       if(!unary.prefix)
-               append(unary.oper);
+       if(unary.oper->type==Operator::POSTFIX)
+               append(unary.oper->token);
 }
 
 void Formatter::visit(BinaryExpression &binary)
 {
        binary.left->visit(*this);
-       append(binary.oper);
+       append(binary.oper->token);
        binary.right->visit(*this);
-       append(binary.after);
+       if(binary.oper->token[0]=='[')
+               append(']');
 }
 
 void Formatter::visit(Assignment &assign)
 {
        assign.left->visit(*this);
-       append(format(" %s ", assign.oper));
+       append(format(" %s ", assign.oper->token));
        assign.right->visit(*this);
 }
 
+void Formatter::visit(TernaryExpression &ternary)
+{
+       ternary.condition->visit(*this);
+       append(ternary.oper->token);
+       ternary.true_expr->visit(*this);
+       if(ternary.oper->token[0]=='?')
+               append(':');
+       ternary.false_expr->visit(*this);
+}
+
 void Formatter::visit(FunctionCall &call)
 {
        append(format("%s(", call.name));
@@ -147,28 +195,6 @@ void Formatter::visit(ExpressionStatement &expr)
        append(';');
 }
 
-void Formatter::visit(Block &block)
-{
-       unsigned brace_indent = indent;
-       bool use_braces = (block.use_braces || (indent && block.body.size()!=1));
-       if(use_braces)
-               append(format("%s{\n", string(brace_indent*2, ' ')));
-
-       SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
-       string spaces(indent*2, ' ');
-       for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
-       {
-               if(i!=block.body.begin())
-                       append('\n');
-               set_source((*i)->source, (*i)->line);
-               append(spaces);
-               (*i)->visit(*this);
-       }
-
-       if(use_braces)
-               append(format("\n%s}", string(brace_indent*2, ' ')));
-}
-
 void Formatter::visit(Import &import)
 {
        append(format("import %s;", import.module));
@@ -219,10 +245,10 @@ void Formatter::visit(VariableDeclaration &var)
                append(format("%s ", var.interpolation));
        if(!var.sampling.empty())
                append(format("%s ", var.sampling));
-       if(!var.interface.empty() && var.interface!=block_interface)
+       if(!var.interface.empty())
        {
                string interface = var.interface;
-               if(mode==Compiler::PROGRAM && stage && stage->required_features.glsl_version<Version(1, 30))
+               if(stage && stage->required_features.glsl_version && stage->required_features.glsl_version<Version(1, 30))
                {
                        if(stage->type==Stage::VERTEX && var.interface=="in")
                                interface = "attribute";
@@ -233,7 +259,10 @@ void Formatter::visit(VariableDeclaration &var)
        }
        if(!var.precision.empty())
                append(format("%s ", var.precision));
-       append(format("%s %s", var.type, var.name));
+       string type_name = var.type_declaration->name;
+       if(var.array)
+               type_name = type_name.substr(0, type_name.find('['));
+       append(format("%s %s", type_name, var.name));
        if(var.array)
        {
                append('[');
@@ -252,15 +281,22 @@ void Formatter::visit(VariableDeclaration &var)
 
 void Formatter::visit(InterfaceBlock &iface)
 {
-       SetForScope<string> set(block_interface, iface.interface);
        append(format("%s %s\n", iface.interface, iface.name));
-       iface.members.visit(*this);
+       if(iface.struct_declaration)
+               iface.struct_declaration->members.visit(*this);
+       if(!iface.instance_name.empty())
+       {
+               append(' ');
+               append(iface.instance_name);
+               if(iface.array)
+                       append("[]");
+       }
        append(';');
 }
 
 void Formatter::visit(FunctionDeclaration &func)
 {
-       append(format("%s %s(", func.return_type, func.name));
+       append(format("%s %s(", func.return_type_declaration->name, func.name));
        for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
        {
                if(i!=func.parameters.begin())