glsl: Make a function to express a GLSL version ir human-readable form.
[mesa.git] / src / glsl / lower_vec_index_to_cond_assign.cpp
index 15992e272880df9675f474003929157192d41d7c..f85875f49fa4598e1077bb7915e7e6b982e4ba87 100644 (file)
@@ -68,12 +68,10 @@ ir_rvalue *
 ir_vec_index_to_cond_assign_visitor::convert_vec_index_to_cond_assign(ir_rvalue *ir)
 {
    ir_dereference_array *orig_deref = ir->as_dereference_array();
-   ir_assignment *assign;
-   ir_variable *index, *var;
-   ir_dereference *deref;
-   ir_expression *condition;
-   ir_swizzle *swizzle;
-   int i;
+   ir_assignment *assign, *value_assign;
+   ir_variable *index, *var, *value;
+   ir_dereference *deref, *deref_value;
+   unsigned i;
 
    if (!orig_deref)
       return ir;
@@ -86,39 +84,60 @@ ir_vec_index_to_cond_assign_visitor::convert_vec_index_to_cond_assign(ir_rvalue
 
    assert(orig_deref->array_index->type->base_type == GLSL_TYPE_INT);
 
+   exec_list list;
+
    /* Store the index to a temporary to avoid reusing its tree. */
    index = new(base_ir) ir_variable(glsl_type::int_type,
                                    "vec_index_tmp_i",
                                    ir_var_temporary);
-   base_ir->insert_before(index);
+   list.push_tail(index);
    deref = new(base_ir) ir_dereference_variable(index);
    assign = new(base_ir) ir_assignment(deref, orig_deref->array_index, NULL);
-   base_ir->insert_before(assign);
+   list.push_tail(assign);
+
+   /* Store the value inside a temp, thus avoiding matrixes duplication */
+   value = new(base_ir) ir_variable(orig_deref->array->type, "vec_value_tmp",
+                                   ir_var_temporary);
+   list.push_tail(value);
+   deref_value = new(base_ir) ir_dereference_variable(value);
+   value_assign = new(base_ir) ir_assignment(deref_value, orig_deref->array);
+   list.push_tail(value_assign);
 
    /* Temporary where we store whichever value we swizzle out. */
    var = new(base_ir) ir_variable(ir->type, "vec_index_tmp_v",
                                  ir_var_temporary);
-   base_ir->insert_before(var);
+   list.push_tail(var);
+
+   /* Generate a single comparison condition "mask" for all of the components
+    * in the vector.
+    */
+   ir_rvalue *const cond_deref =
+      compare_index_block(&list, index, 0,
+                         orig_deref->array->type->vector_elements,
+                         mem_ctx);
 
    /* Generate a conditional move of each vector element to the temp. */
    for (i = 0; i < orig_deref->array->type->vector_elements; i++) {
-      deref = new(base_ir) ir_dereference_variable(index);
-      condition = new(base_ir) ir_expression(ir_binop_equal,
-                                            glsl_type::bool_type,
-                                            deref,
-                                            new(base_ir) ir_constant(i));
+      ir_rvalue *condition_swizzle =
+        new(base_ir) ir_swizzle(cond_deref->clone(ir, NULL), i, 0, 0, 0, 1);
 
       /* Just clone the rest of the deref chain when trying to get at the
        * underlying variable.
        */
-      swizzle = new(base_ir) ir_swizzle(orig_deref->array->clone(mem_ctx, NULL),
-                                       i, 0, 0, 0, 1);
+      ir_rvalue *swizzle =
+        new(base_ir) ir_swizzle(deref_value->clone(mem_ctx, NULL),
+                                i, 0, 0, 0, 1);
 
       deref = new(base_ir) ir_dereference_variable(var);
-      assign = new(base_ir) ir_assignment(deref, swizzle, condition);
-      base_ir->insert_before(assign);
+      assign = new(base_ir) ir_assignment(deref, swizzle, condition_swizzle);
+      list.push_tail(assign);
    }
 
+   /* Put all of the new instructions in the IR stream before the old
+    * instruction.
+    */
+   base_ir->insert_before(&list);
+
    this->progress = true;
    return new(base_ir) ir_dereference_variable(var);
 }
@@ -153,7 +172,7 @@ ir_vec_index_to_cond_assign_visitor::visit_leave(ir_assignment *ir)
    ir_variable *index, *var;
    ir_dereference_variable *deref;
    ir_assignment *assign;
-   int i;
+   unsigned i;
 
    ir->rhs = convert_vec_index_to_cond_assign(ir->rhs);
    if (ir->condition)
@@ -189,24 +208,29 @@ ir_vec_index_to_cond_assign_visitor::visit_leave(ir_assignment *ir)
    assign = new(ir) ir_assignment(deref, ir->rhs, NULL);
    list.push_tail(assign);
 
+   /* Generate a single comparison condition "mask" for all of the components
+    * in the vector.
+    */
+   ir_rvalue *const cond_deref =
+      compare_index_block(&list, index, 0,
+                         orig_deref->array->type->vector_elements,
+                         mem_ctx);
+
    /* Generate a conditional move of each vector element to the temp. */
    for (i = 0; i < orig_deref->array->type->vector_elements; i++) {
-      ir_rvalue *condition, *swizzle;
+      ir_rvalue *condition_swizzle =
+        new(ir) ir_swizzle(cond_deref->clone(ir, NULL), i, 0, 0, 0, 1);
 
-      deref = new(ir) ir_dereference_variable(index);
-      condition = new(ir) ir_expression(ir_binop_equal,
-                                       glsl_type::bool_type,
-                                       deref,
-                                       new(ir) ir_constant(i));
 
       /* Just clone the rest of the deref chain when trying to get at the
        * underlying variable.
        */
-      swizzle = new(ir) ir_swizzle(orig_deref->array->clone(mem_ctx, NULL),
-                                  i, 0, 0, 0, 1);
+      ir_rvalue *swizzle =
+        new(ir) ir_swizzle(orig_deref->array->clone(mem_ctx, NULL),
+                           i, 0, 0, 0, 1);
 
       deref = new(ir) ir_dereference_variable(var);
-      assign = new(ir) ir_assignment(swizzle, deref, condition);
+      assign = new(ir) ir_assignment(swizzle, deref, condition_swizzle);
       list.push_tail(assign);
    }