glsl: Make ir_algebraic new expressions allocate out of the parent.
[mesa.git] / src / glsl / ir_constant_folding.cpp
index 492036e9a6bfd68cc6d28a4f834cbc988fd771b4..11260423d90b81ccb0dc7669f7332b502509b6a4 100644 (file)
@@ -39,7 +39,7 @@ class ir_constant_folding_visitor : public ir_visitor {
 public:
    ir_constant_folding_visitor()
    {
-      /* empty */
+      this->progress = false;
    }
 
    virtual ~ir_constant_folding_visitor()
@@ -75,17 +75,20 @@ public:
    /*@}*/
 
    void fold_constant(ir_rvalue **rvalue);
+
+   bool progress;
 };
 
 void
 ir_constant_folding_visitor::fold_constant(ir_rvalue **rvalue)
 {
-   if ((*rvalue)->ir_type == ir_type_constant)
+   if (*rvalue == NULL || (*rvalue)->ir_type == ir_type_constant)
       return;
 
    ir_constant *constant = (*rvalue)->constant_expression_value();
    if (constant) {
       *rvalue = constant;
+      this->progress = true;
    } else {
       (*rvalue)->accept(this);
    }
@@ -128,15 +131,32 @@ ir_constant_folding_visitor::visit(ir_expression *ir)
 void
 ir_constant_folding_visitor::visit(ir_texture *ir)
 {
-   // FINISHME: Do stuff with texture lookups
-   (void) ir;
+   fold_constant(&ir->coordinate);
+   fold_constant(&ir->projector);
+   fold_constant(&ir->shadow_comparitor);
+
+   switch (ir->op) {
+   case ir_tex:
+      break;
+   case ir_txb:
+      fold_constant(&ir->lod_info.bias);
+      break;
+   case ir_txf:
+   case ir_txl:
+      fold_constant(&ir->lod_info.lod);
+      break;
+   case ir_txd:
+      fold_constant(&ir->lod_info.grad.dPdx);
+      fold_constant(&ir->lod_info.grad.dPdy);
+      break;
+   }
 }
 
 
 void
 ir_constant_folding_visitor::visit(ir_swizzle *ir)
 {
-   ir->val->accept(this);
+   fold_constant(&ir->val);
 }
 
 
@@ -177,6 +197,7 @@ ir_constant_folding_visitor::visit(ir_assignment *ir)
            ir->condition = NULL;
         else
            ir->remove();
+        this->progress = true;
       }
    }
 }
@@ -192,14 +213,22 @@ ir_constant_folding_visitor::visit(ir_constant *ir)
 void
 ir_constant_folding_visitor::visit(ir_call *ir)
 {
-   (void) ir;
+   foreach_iter(exec_list_iterator, iter, *ir) {
+      ir_rvalue *param = (ir_rvalue *)iter.get();
+      ir_rvalue *new_param = param;
+      fold_constant(&new_param);
+
+      if (new_param != param) {
+        param->replace_with(new_param);
+      }
+   }
 }
 
 
 void
 ir_constant_folding_visitor::visit(ir_return *ir)
 {
-   (void) ir;
+   fold_constant(&ir->value);
 }
 
 
@@ -240,6 +269,5 @@ do_constant_folding(exec_list *instructions)
 
    visit_exec_list(instructions, &constant_folding);
 
-   /* FINISHME: Return real progress. */
-   return false;
+   return constant_folding.progress;
 }