]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/output.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / glsl / output.cpp
index c15c8c0421b9aae64add279cf11b9c05a03753f6..d28de156c5a881fede9fb116052370102338c80a 100644 (file)
@@ -8,16 +8,6 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
-Formatter::Formatter():
-       stage(0),
-       source_index(0),
-       source_line(1),
-       indent(0),
-       parameter_list(false),
-       omit_builtin(false),
-       r_empty_name(false)
-{ }
-
 string Formatter::apply(Stage &s)
 {
        stage = &s;
@@ -28,7 +18,7 @@ string Formatter::apply(Stage &s)
        if(ver)
        {
                append(format("#version %d%02d", ver.major, ver.minor));
-               if(s.required_features.gl_api==OPENGL_ES2 && ver>=Version(3, 0))
+               if(s.required_features.target_api==OPENGL_ES && ver>=Version(3, 0))
                        append(" es");
                formatted += '\n';
        }
@@ -59,8 +49,8 @@ string Formatter::apply(Stage &s)
 void Formatter::append(const string &text)
 {
        formatted += text;
-       for(string::const_iterator i=text.begin(); i!=text.end(); ++i)
-               if(*i=='\n')
+       for(char c: text)
+               if(c=='\n')
                        ++source_line;
 }
 
@@ -99,16 +89,16 @@ void Formatter::visit(Block &block)
        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)
+       for(const RefPtr<Statement> &s: block.body)
        {
-               if(omit_builtin && (*i)->source<=BUILTIN_SOURCE)
+               if(omit_builtin && s->source<=BUILTIN_SOURCE)
                        continue;
                if(!first)
                        append('\n');
                first = false;
-               set_source((*i)->source, (*i)->line);
+               set_source(s->source, s->line);
                append(spaces);
-               (*i)->visit(*this);
+               s->visit(*this);
        }
 
        if(use_braces)
@@ -154,15 +144,9 @@ void Formatter::visit(Literal &literal)
 
 void Formatter::visit(VariableReference &var)
 {
-       append(var.name);
-       r_empty_name = false;
-}
-
-void Formatter::visit(InterfaceBlockReference &iface)
-{
-       r_empty_name = iface.declaration->instance_name.empty();
+       r_empty_name = var.name.find(' ')!=string::npos;
        if(!r_empty_name)
-               append(iface.declaration->instance_name);
+               append(var.name);
 }
 
 void Formatter::visit(MemberAccess &memacc)
@@ -171,6 +155,7 @@ void Formatter::visit(MemberAccess &memacc)
        if(!r_empty_name)
                append('.');
        append(memacc.member);
+       r_empty_name = false;
 }
 
 void Formatter::visit(Swizzle &swizzle)
@@ -209,7 +194,7 @@ void Formatter::visit(TernaryExpression &ternary)
        visit_expression(*ternary.condition, ternary.oper, false);
        append(ternary.oper->token);
        visit_expression(*ternary.true_expr, ternary.oper, false);
-       if(ternary.oper->token2)
+       if(ternary.oper->token2[0])
                append(ternary.oper->token2);
        visit_expression(*ternary.false_expr, ternary.oper, true);
 }
@@ -217,7 +202,7 @@ void Formatter::visit(TernaryExpression &ternary)
 void Formatter::visit(FunctionCall &call)
 {
        append(format("%s(", call.name));
-       for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
+       for(auto i=call.arguments.begin(); i!=call.arguments.end(); ++i)
        {
                if(i!=call.arguments.begin())
                        append(", ");
@@ -245,7 +230,7 @@ void Formatter::visit(Precision &prec)
 void Formatter::visit(Layout &layout)
 {
        append("layout(");
-       for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
+       for(auto i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
        {
                if(i!=layout.qualifiers.begin())
                        append(", ");
@@ -264,6 +249,9 @@ void Formatter::visit(InterfaceLayout &layout)
 
 void Formatter::visit(StructDeclaration &strct)
 {
+       if(!strct.block_name.empty())
+               return;
+
        append(format("struct %s\n", strct.name));
        strct.members.visit(*this);
        append(';');
@@ -296,10 +284,25 @@ void Formatter::visit(VariableDeclaration &var)
        }
        if(!var.precision.empty())
                append(format("%s ", var.precision));
-       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.block_declaration)
+       {
+               append(format("%s\n", var.block_declaration->block_name));
+               var.block_declaration->members.visit(*this);
+               if(var.name.find(' ')==string::npos)
+                       append(format(" %s", var.name));
+       }
+       else
+       {
+               string type_name = var.type_declaration->name;
+               if(const ImageTypeDeclaration *image = dynamic_cast<const ImageTypeDeclaration *>(var.type_declaration))
+                       if(image->base_image)
+                               type_name = image->base_image->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('[');
@@ -316,30 +319,10 @@ void Formatter::visit(VariableDeclaration &var)
                append(';');
 }
 
-void Formatter::visit(InterfaceBlock &iface)
-{
-       if(iface.layout)
-       {
-               iface.layout->visit(*this);
-               append(' ');
-       }
-       append(format("%s %s\n", iface.interface, iface.block_name));
-       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_declaration->name, func.name));
-       for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
+       for(auto i=func.parameters.begin(); i!=func.parameters.end(); ++i)
        {
                if(i!=func.parameters.begin())
                        append(", ");