interface_block(0),
r_assignment(0),
assignment_target(false),
- r_side_effects(false)
+ r_side_effects(false),
+ composite_reference(false)
{ }
bool UnusedVariableRemover::apply(Stage &s)
if(!assignment_target)
{
for(vector<AssignmentInfo *>::const_iterator i=var_info.assignments.begin(); i!=var_info.assignments.end(); ++i)
- (*i)->used_by.push_back(&node);
+ {
+ bool covered = true;
+ for(unsigned j=0; (covered && j<(*i)->target.chain_len && j<target.chain_len); ++j)
+ {
+ Assignment::Target::ChainType type1 = static_cast<Assignment::Target::ChainType>((*i)->target.chain[j]&0xC0);
+ Assignment::Target::ChainType type2 = static_cast<Assignment::Target::ChainType>(target.chain[j]&0xC0);
+ if(type1==Assignment::Target::SWIZZLE || type2==Assignment::Target::SWIZZLE)
+ {
+ unsigned index1 = (*i)->target.chain[j]&0x3F;
+ unsigned index2 = target.chain[j]&0x3F;
+ if(type1==Assignment::Target::SWIZZLE && type2==Assignment::Target::SWIZZLE)
+ covered = index1&index2;
+ else if(type1==Assignment::Target::ARRAY && index1<4)
+ covered = index2&(1<<index1);
+ else if(type2==Assignment::Target::ARRAY && index2<4)
+ covered = index1&(1<<index2);
+ /* If it's some other combination (shouldn't happen), leave
+ covered as true */
+ }
+ else
+ covered = ((*i)->target.chain[j]==target.chain[j]);
+ }
+ if(covered)
+ (*i)->used_by.push_back(&node);
+ }
}
}
void UnusedVariableRemover::visit(VariableReference &var)
{
- referenced(var.declaration, var);
+ if(composite_reference)
+ r_reference.declaration = var.declaration;
+ else
+ referenced(var.declaration, var);
}
void UnusedVariableRemover::visit(InterfaceBlockReference &iface)
{
- referenced(iface.declaration, iface);
+ if(composite_reference)
+ r_reference.declaration = iface.declaration;
+ else
+ referenced(iface.declaration, iface);
+}
+
+void UnusedVariableRemover::visit_composite(Expression &expr)
+{
+ if(!composite_reference)
+ r_reference = Assignment::Target();
+
+ SetFlag set_composite(composite_reference);
+ expr.visit(*this);
+}
+
+void UnusedVariableRemover::visit(MemberAccess &memacc)
+{
+ visit_composite(*memacc.left);
+
+ add_to_chain(r_reference, Assignment::Target::MEMBER, memacc.index);
+
+ if(!composite_reference && r_reference.declaration)
+ referenced(r_reference, memacc);
+}
+
+void UnusedVariableRemover::visit(Swizzle &swizzle)
+{
+ visit_composite(*swizzle.left);
+
+ unsigned mask = 0;
+ for(unsigned i=0; i<swizzle.count; ++i)
+ mask |= 1<<swizzle.components[i];
+ add_to_chain(r_reference, Assignment::Target::SWIZZLE, mask);
+
+ if(!composite_reference && r_reference.declaration)
+ referenced(r_reference, swizzle);
}
void UnusedVariableRemover::visit(UnaryExpression &unary)
{
if(binary.oper->token[0]=='[')
{
- binary.left->visit(*this);
- SetFlag set(assignment_target, false);
- binary.right->visit(*this);
+ visit_composite(*binary.left);
+
+ {
+ SetFlag clear_assignment(assignment_target, false);
+ SetFlag clear_composite(composite_reference, false);
+ binary.right->visit(*this);
+ }
+
+ add_to_chain(r_reference, Assignment::Target::ARRAY, 0x3F);
+
+ if(!composite_reference && r_reference.declaration)
+ referenced(r_reference, binary);
}
else
+ {
+ SetFlag clear_composite(composite_reference, false);
TraversingVisitor::visit(binary);
+ }
+}
+
+void UnusedVariableRemover::visit(TernaryExpression &ternary)
+{
+ SetFlag clear_composite(composite_reference, false);
+ TraversingVisitor::visit(ternary);
}
void UnusedVariableRemover::visit(Assignment &assign)
void UnusedVariableRemover::visit(FunctionCall &call)
{
+ SetFlag clear_composite(composite_reference, false);
TraversingVisitor::visit(call);
/* Treat function calls as having side effects so expression statements
consisting of nothing but a function call won't be optimized away. */
Assignment *r_assignment;
bool assignment_target;
bool r_side_effects;
+ bool composite_reference;
+ Assignment::Target r_reference;
std::set<Node *> unused_nodes;
public:
void referenced(const Assignment::Target &, Node &);
virtual void visit(VariableReference &);
virtual void visit(InterfaceBlockReference &);
+ void visit_composite(Expression &);
+ virtual void visit(MemberAccess &);
+ virtual void visit(Swizzle &);
virtual void visit(UnaryExpression &);
virtual void visit(BinaryExpression &);
virtual void visit(Assignment &);
+ virtual void visit(TernaryExpression &);
virtual void visit(FunctionCall &);
void record_assignment(const Assignment::Target &, Node &);
virtual void visit(ExpressionStatement &);
--- /dev/null
+uniform mat4 mvp;
+
+#pragma MSP stage(vertex)
+layout(location=0) in vec4 position;
+int main()
+{
+ vec4 c0 = mvp[0]*position;
+ vec4 c1 = mvp[1]*position;
+ vec4 c2 = mvp[2]*position;
+ vec4 c3 = mvp[3]*position;
+ vec4 clip_pos;
+ clip_pos.x = c0.x+c1.x+c2.x+c3.x;
+ clip_pos.y = c0.y+c1.y+c2.y+c3.y;
+ clip_pos.z = c0.z+c1.z+c2.z+c3.z;
+ clip_pos.w = c0.w+c1.w+c2.w+c3.w;
+ gl_Position = vec4(clip_pos.xy, 0.0, 1.0);
+}
+
+/* Expected output: vertex
+layout(location=0) uniform mat4 mvp;
+layout(location=0) in vec4 position;
+int main()
+{
+ vec4 c0 = mvp[0]*position;
+ vec4 c1 = mvp[1]*position;
+ vec4 c2 = mvp[2]*position;
+ vec4 c3 = mvp[3]*position;
+ vec4 clip_pos;
+ clip_pos.x = c0.x+c1.x+c2.x+c3.x;
+ clip_pos.y = c0.y+c1.y+c2.y+c3.y;
+ gl_Position = vec4(clip_pos.xy, 0.0, 1.0);
+}
+*/