]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/generate.cpp
Automatically determine array sizes in GLSL if possible
[libs/gl.git] / source / glsl / generate.cpp
index 0a9e8e85a81380977cdf6cbc27f767d61fcf4ebc..fe2d56b8a4737bba58f6086a070ecc3da281b691 100644 (file)
@@ -338,6 +338,119 @@ void InterfaceGenerator::visit(Passthrough &pass)
        nodes_to_remove.insert(&pass);
 }
 
+
+void ArraySizer::apply(Stage &stage)
+{
+       stage.content.visit(*this);
+       for(const auto &kvp: max_indices)
+               if(kvp.first->array && !kvp.first->array_size)
+               {
+                       int size = 0;
+                       if(stage.type==Stage::GEOMETRY && kvp.first->interface=="in")
+                               size = input_size;
+                       else if(kvp.second>=0)
+                               size = kvp.second+1;
+                       else if(!kvp.first->name.compare(0, 3, "gl_"))
+                               size = 1;
+
+                       if(size>0)
+                       {
+                               Literal *literal_size = new Literal;
+                               literal_size->token = lexical_cast<string>(size);
+                               literal_size->value = size;
+                               kvp.first->array_size = literal_size;
+                       }
+               }
+}
+
+void ArraySizer::visit(VariableReference &var)
+{
+       r_declaration = var.declaration;
+}
+
+void ArraySizer::visit(MemberAccess &memacc)
+{
+       r_declaration = 0;
+       TraversingVisitor::visit(memacc);
+       VariableDeclaration *member_declaration = 0;
+       if(r_declaration)
+               if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(r_declaration->type_declaration))
+               {
+                       auto i = strct->members.variables.find(memacc.member);
+                       if(i!=strct->members.variables.end())
+                               member_declaration = i->second;
+               }
+       r_declaration = member_declaration;
+}
+
+void ArraySizer::visit(Swizzle &swizzle)
+{
+       TraversingVisitor::visit(swizzle);
+       r_declaration = 0;
+}
+
+void ArraySizer::visit(UnaryExpression &unary)
+{
+       TraversingVisitor::visit(unary);
+       r_declaration = 0;
+}
+
+void ArraySizer::visit(BinaryExpression &binary)
+{
+       if(binary.oper->token[0]=='[')
+               if(const Literal *literal_index = dynamic_cast<const Literal *>(binary.right.get()))
+                       if(literal_index->value.check_type<int>())
+                       {
+                               r_declaration = 0;
+                               binary.left->visit(*this);
+                               if(r_declaration)
+                               {
+                                       max_indices[r_declaration] = literal_index->value.value<int>();
+                                       return;
+                               }
+                       }
+
+       TraversingVisitor::visit(binary);
+}
+
+void ArraySizer::visit(TernaryExpression &ternary)
+{
+       TraversingVisitor::visit(ternary);
+       r_declaration = 0;
+}
+
+void ArraySizer::visit(FunctionCall &call)
+{
+       TraversingVisitor::visit(call);
+       r_declaration = 0;
+}
+
+void ArraySizer::visit(InterfaceLayout &layout)
+{
+       if(layout.interface=="in")
+       {
+               for(const Layout::Qualifier &q: layout.layout.qualifiers)
+               {
+                       if(q.name=="points")
+                               input_size = 1;
+                       else if(q.name=="lines")
+                               input_size = 2;
+                       else if(q.name=="triangles")
+                               input_size = 3;
+                       else if(q.name=="lines_adjacency")
+                               input_size = 4;
+                       else if(q.name=="triangles_adjacency")
+                               input_size = 6;
+               }
+       }
+}
+
+void ArraySizer::visit(VariableDeclaration &var)
+{
+       if(var.array && !var.array_size)
+               max_indices[&var] = 0;
+}
+
 } // namespace SL
 } // namespace GL
 } // namespace Msp