]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/output.cpp
Transform interface block contents into structs
[libs/gl.git] / source / glsl / output.cpp
index 9d4383e4de979b91c6febdab5e0cd02ca599e4e8..52371d1bfdd79f9f7de91ff457d10001952f6c62 100644 (file)
@@ -22,23 +22,29 @@ const string &Formatter::apply(Stage &s, Compiler::Mode m)
        mode = m;
        stage = &s;
 
-       GLApi api = get_gl_api();
-       const Version &ver = s.required_version;
+       const Version &ver = s.required_features.glsl_version;
 
        if(ver)
        {
                append(format("#version %d%02d", ver.major, ver.minor));
-               if(api==OPENGL_ES2 && ver>=Version(3, 0))
+               if(s.required_features.gl_api==OPENGL_ES2 && ver>=Version(3, 0))
                        append(" es");
                formatted += '\n';
        }
 
-       for(vector<const Extension *>::const_iterator i=s.required_extensions.begin(); i!=s.required_extensions.end(); ++i)
-               append(format("#extension %s: require\n", (*i)->get_name()));
-       if(!s.required_extensions.empty())
-               formatted += '\n';
+       if(s.required_features.arb_explicit_attrib_location)
+               append("#extension arb_explicit_attrib_location: require\n");
+       if(s.required_features.arb_gpu_shader5)
+               append("#extension arb_gpu_shader5: require\n");
+       if(s.required_features.arb_uniform_buffer_object)
+               append("#extension arb_uniform_buffer_object: require\n");
+       if(s.required_features.ext_gpu_shader4)
+               append("#extension ext_gpu_shader4: require\n");
+       if(s.required_features.ext_texture_array)
+               append("#extension ext_texture_array: require\n");
+       formatted += '\n';
 
-       visit(s.content);
+       s.content.visit(*this);
 
        return formatted;
 }
@@ -67,7 +73,7 @@ void Formatter::set_source(unsigned index, unsigned line)
                else
                {
                        unsigned l = line;
-                       if(mode==Compiler::PROGRAM && stage && stage->required_version<Version(3, 30))
+                       if(mode==Compiler::PROGRAM && stage && stage->required_features.glsl_version<Version(3, 30))
                                --l;
                        formatted += format("#line %d %d\n", l, index);
                }
@@ -76,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((*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);
@@ -93,6 +125,11 @@ 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);
@@ -101,25 +138,26 @@ void Formatter::visit(MemberAccess &memacc)
 
 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);
 }
 
@@ -141,28 +179,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));
@@ -180,9 +196,9 @@ void Formatter::visit(Layout &layout)
        {
                if(i!=layout.qualifiers.begin())
                        append(", ");
-               append(i->identifier);
-               if(!i->value.empty())
-                       append(format("=%s", i->value));
+               append(i->name);
+               if(i->has_value)
+                       append(format("=%d", i->value));
        }
        append(')');
 }
@@ -213,10 +229,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_version<Version(1, 30))
+               if(mode==Compiler::PROGRAM && stage && stage->required_features.glsl_version<Version(1, 30))
                {
                        if(stage->type==Stage::VERTEX && var.interface=="in")
                                interface = "attribute";
@@ -227,7 +243,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('[');
@@ -246,15 +265,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())
@@ -335,6 +361,18 @@ void Formatter::visit(Iteration &iter)
        }
 }
 
+void Formatter::visit(Passthrough &pass)
+{
+       append("passthrough");
+       if(pass.subscript)
+       {
+               append('[');
+               pass.subscript->visit(*this);
+               append(']');
+       }
+       append(';');
+}
+
 void Formatter::visit(Return &ret)
 {
        append("return");