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
virtual void visit(Passthrough &);
};
+/**
+Assigns sizes to arrays which don't have a size. Geometry shader inputs are
+sized by topology. Other arrays are sized by their use with literal indices.
+*/
+class ArraySizer: private TraversingVisitor
+{
+private:
+ std::map<VariableDeclaration *, int> max_indices;
+ unsigned input_size = 0;
+ VariableDeclaration *r_declaration;
+
+public:
+ void apply(Stage &);
+
+private:
+ virtual void visit(VariableReference &);
+ virtual void visit(MemberAccess &);
+ virtual void visit(Swizzle &);
+ virtual void visit(UnaryExpression&);
+ virtual void visit(BinaryExpression &);
+ virtual void visit(TernaryExpression &);
+ virtual void visit(FunctionCall &);
+ virtual void visit(InterfaceLayout &);
+ virtual void visit(VariableDeclaration &);
+};
+
} // namespace SL
} // namespace GL
} // namespace Msp
if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(var.type_declaration))
if(basic->kind==BasicTypeDeclaration::ARRAY)
{
- Id size_id = 0;
- if(var.array_size)
- {
- SetFlag set_const(constant_expression);
- r_expression_result_id = 0;
- var.array_size->visit(*this);
- size_id = r_expression_result_id;
- }
- else
- size_id = get_constant_id(get_standard_type_id(BasicTypeDeclaration::INT, 1), 1);
- return get_array_type_id(*basic->base_type, size_id, basic->extended_alignment);
+ if(!var.array_size)
+ throw logic_error("array without size");
+
+ SetFlag set_const(constant_expression);
+ r_expression_result_id = 0;
+ var.array_size->visit(*this);
+ return get_array_type_id(*basic->base_type, r_expression_result_id, basic->extended_alignment);
}
return get_id(*var.type_declaration);