glsl: rename record_location_offset() -> struct_location_offset()
[mesa.git] / src / compiler / glsl / lower_distance.cpp
index 9546434d96b0195c730de398fd7555e6d8fc5a75..b4e730c64fbd7280717d45c2ddba2fe9cb1802d5 100644 (file)
  * \file lower_distance.cpp
  *
  * This pass accounts for the difference between the way
- * gl_ClipDistance or gl_CullDistance is declared in standard GLSL
- * (as an array of floats), and the way it is frequently implemented
- * in hardware (as a pair of vec4s, with four clip or cull distances
- * packed into each).
+ * gl_ClipDistance is declared in standard GLSL (as an array of
+ * floats), and the way it is frequently implemented in hardware (as
+ * a pair of vec4s, with four clip distances packed into each).
  *
- * The declarations of gl_ClipDistance or gl_CullDistance are replaced
- * with a single declaration of gl_ClipDistanceMESA.
- * Any references to the original gl_ClipDistance or gl_CullDistance
- * are translated to refer to gl_ClipDistanceMESA with the appropriate
- * swizzling of array indices.
- * For instance:
+ * The declaration of gl_ClipDistance is replaced with a declaration
+ * of gl_ClipDistanceMESA, and any references to gl_ClipDistance are
+ * translated to refer to gl_ClipDistanceMESA with the appropriate
+ * swizzling of array indices.  For instance:
  *
  *   gl_ClipDistance[i]
  *
  *
  *   gl_ClipDistanceMESA[i>>2][i&3]
  *
- * Since some hardware may not internally represent these arrays as a
- * pair of vec4's, this lowering pass is optional.  To enable it, set
- * the LowerCombinedClipCullDistance flag in gl_shader_compiler_options to true.
+ * Since some hardware may not internally represent gl_ClipDistance as a pair
+ * of vec4's, this lowering pass is optional.  To enable it, set the
+ * LowerCombinedClipCullDistance flag in gl_shader_compiler_options to true.
  */
 
+#include "main/macros.h"
 #include "glsl_symbol_table.h"
 #include "ir_rvalue_visitor.h"
 #include "ir.h"
 #include "program/prog_instruction.h" /* For WRITEMASK_* */
+#include "main/mtypes.h"
+
+#define GLSL_CLIP_VAR_NAME "gl_ClipDistanceMESA"
 
 namespace {
 
 class lower_distance_visitor : public ir_rvalue_visitor {
 public:
    explicit lower_distance_visitor(gl_shader_stage shader_stage,
-                                   const char *in_name, ir_variable *out_var,
-                                   int num_clip_dist, int num_cull_dist,
-                                   bool is_cull, bool replace_var)
+                                   const char *in_name, int total_size,
+                                   int offset)
       : progress(false), old_distance_out_var(NULL),
         old_distance_in_var(NULL), new_distance_out_var(NULL),
         new_distance_in_var(NULL), shader_stage(shader_stage),
-        in_name(in_name), out_var(out_var),
-        num_clip_dist(num_clip_dist), num_cull_dist(num_cull_dist),
-        is_cull(is_cull), replace_var(replace_var)
+        in_name(in_name), total_size(total_size), offset(offset)
+   {
+   }
+
+   explicit lower_distance_visitor(gl_shader_stage shader_stage,
+                                   const char *in_name,
+                                   const lower_distance_visitor *orig,
+                                   int offset)
+      : progress(false),
+        old_distance_out_var(NULL),
+        old_distance_in_var(NULL),
+        new_distance_out_var(orig->new_distance_out_var),
+        new_distance_in_var(orig->new_distance_in_var),
+        shader_stage(shader_stage),
+        in_name(in_name),
+        total_size(orig->total_size),
+        offset(offset)
    {
    }
 
@@ -85,7 +99,7 @@ public:
    bool progress;
 
    /**
-    * Pointer to the declaration of ou arrays, if found.
+    * Pointer to the declaration of gl_ClipDistance, if found.
     *
     * Note:
     *
@@ -100,7 +114,7 @@ public:
    ir_variable *old_distance_in_var;
 
    /**
-    * Pointer to the newly-created variable.
+    * Pointer to the newly-created gl_ClipDistanceMESA variable.
     */
    ir_variable *new_distance_out_var;
    ir_variable *new_distance_in_var;
@@ -109,26 +123,16 @@ public:
     * Type of shader we are compiling (e.g. MESA_SHADER_VERTEX)
     */
    const gl_shader_stage shader_stage;
-
-   /**
-    *  Identifier of the variables we manipulate
-    */
-
    const char *in_name;
-
-   ir_variable *out_var;
-   uint8_t num_clip_dist;
-   uint8_t num_cull_dist;
-   bool is_cull;
-   bool replace_var;
+   int total_size;
+   int offset;
 };
 
 } /* anonymous namespace */
 
-
 /**
- * Replace any declaration of in_name as an array of floats with a
- * declaration of out_name as an array of vec4's.
+ * Replace any declaration of 'in_name' as an array of floats with a
+ * declaration of gl_ClipDistanceMESA as an array of vec4's.
  */
 ir_visitor_status
 lower_distance_visitor::visit(ir_variable *ir)
@@ -136,7 +140,7 @@ lower_distance_visitor::visit(ir_variable *ir)
    ir_variable **old_var;
    ir_variable **new_var;
 
-   if (!ir->name || strcmp(in_name, ir->name) != 0)
+   if (!ir->name || strcmp(ir->name, in_name) != 0)
       return visit_continue;
    assert (ir->type->is_array());
 
@@ -156,45 +160,53 @@ lower_distance_visitor::visit(ir_variable *ir)
 
    this->progress = true;
 
-   if (!ir->type->fields.array->is_array()) {
-      /* gl_ClipDistance / gl_CullDistance (used for vertex, tessellation
-       * evaluation and geometry output, and fragment input).
-       */
-      assert((ir->data.mode == ir_var_shader_in &&
-              this->shader_stage == MESA_SHADER_FRAGMENT) ||
-             (ir->data.mode == ir_var_shader_out &&
-              (this->shader_stage == MESA_SHADER_VERTEX ||
-               this->shader_stage == MESA_SHADER_TESS_EVAL ||
-               this->shader_stage == MESA_SHADER_GEOMETRY)));
-
-      *old_var = ir;
-      assert (ir->type->fields.array == glsl_type::float_type);
-      *new_var = out_var;
-      if (replace_var == true) {
-         ir->replace_with(*new_var);
-         replace_var = false;
+   *old_var = ir;
+
+   if (!(*new_var)) {
+      unsigned new_size = (total_size + 3) / 4;
+
+      /* Clone the old var so that we inherit all of its properties */
+      *new_var = ir->clone(ralloc_parent(ir), NULL);
+      (*new_var)->name = ralloc_strdup(*new_var, GLSL_CLIP_VAR_NAME);
+      (*new_var)->data.location = VARYING_SLOT_CLIP_DIST0;
+
+      if (!ir->type->fields.array->is_array()) {
+         /* gl_ClipDistance (used for vertex, tessellation evaluation and
+          * geometry output, and fragment input).
+          */
+         assert((ir->data.mode == ir_var_shader_in &&
+                 this->shader_stage == MESA_SHADER_FRAGMENT) ||
+                (ir->data.mode == ir_var_shader_out &&
+                 (this->shader_stage == MESA_SHADER_VERTEX ||
+                  this->shader_stage == MESA_SHADER_TESS_EVAL ||
+                  this->shader_stage == MESA_SHADER_GEOMETRY)));
+
+         assert (ir->type->fields.array == glsl_type::float_type);
+         (*new_var)->data.max_array_access = new_size - 1;
+
+         /* And change the properties that we need to change */
+         (*new_var)->type = glsl_type::get_array_instance(glsl_type::vec4_type,
+                                                          new_size);
       } else {
-         ir->remove();
+         /* 2D gl_ClipDistance (used for tessellation control, tessellation
+          * evaluation and geometry input, and tessellation control output).
+          */
+         assert((ir->data.mode == ir_var_shader_in &&
+                 (this->shader_stage == MESA_SHADER_GEOMETRY ||
+                  this->shader_stage == MESA_SHADER_TESS_EVAL)) ||
+                this->shader_stage == MESA_SHADER_TESS_CTRL);
+
+         assert (ir->type->fields.array->fields.array == glsl_type::float_type);
+
+         /* And change the properties that we need to change */
+         (*new_var)->type = glsl_type::get_array_instance(
+                            glsl_type::get_array_instance(glsl_type::vec4_type,
+                                                          new_size),
+                            ir->type->array_size());
       }
+      ir->replace_with(*new_var);
    } else {
-      /* 2D gl_ClipDistance / gl_CullDistance (used for tessellation control,
-       * tessellation evaluation and geometry input, and tessellation control
-       * output).
-       */
-      assert((ir->data.mode == ir_var_shader_in &&
-              (this->shader_stage == MESA_SHADER_GEOMETRY ||
-               this->shader_stage == MESA_SHADER_TESS_EVAL)) ||
-             this->shader_stage == MESA_SHADER_TESS_CTRL);
-
-      *old_var = ir;
-      assert (ir->type->fields.array->fields.array == glsl_type::float_type);
-      *new_var = out_var;
-      if (replace_var == true) {
-         ir->replace_with(*new_var);
-         replace_var = false;
-      } else {
-         ir->remove();
-      }
+      ir->remove();
    }
 
    return visit_continue;
@@ -202,10 +214,10 @@ lower_distance_visitor::visit(ir_variable *ir)
 
 
 /**
- * Create the necessary GLSL rvalues to index into out_name based
+ * Create the necessary GLSL rvalues to index into gl_ClipDistanceMESA based
  * on the rvalue previously used to index into gl_ClipDistance.
  *
- * \param array_index Selects one of the vec4's in out_name
+ * \param array_index Selects one of the vec4's in gl_ClipDistanceMESA
  * \param swizzle_index Selects a component within the vec4 selected by
  *        array_index.
  */
@@ -224,16 +236,16 @@ lower_distance_visitor::create_indices(ir_rvalue *old_index,
       old_index = new(ctx) ir_expression(ir_unop_u2i, old_index);
    }
 
-   ir_constant *old_index_constant = old_index->constant_expression_value();
+   ir_constant *old_index_constant =
+      old_index->constant_expression_value(ctx);
    if (old_index_constant) {
-      /* gl_ClipDistance / gl_CullDistance is being accessed via a constant
-       * index.  Don't bother creating expressions to calculate the lowered
-       * indices.  Just create constants.
+      /* gl_ClipDistance is being accessed via a constant index.  Don't bother
+       * creating expressions to calculate the lowered indices.  Just create
+       * constants.
        */
-      int const_val = old_index_constant->get_int_component(0);
-      uint8_t offset = is_cull ? num_clip_dist : 0;
-      array_index = new(ctx) ir_constant((const_val + offset) / 4);
-      swizzle_index = new(ctx) ir_constant((const_val + offset) % 4);
+      int const_val = old_index_constant->get_int_component(0) + offset;
+      array_index = new(ctx) ir_constant(const_val / 4);
+      swizzle_index = new(ctx) ir_constant(const_val % 4);
    } else {
       /* Create a variable to hold the value of old_index (so that we
        * don't compute it twice).
@@ -244,18 +256,24 @@ lower_distance_visitor::create_indices(ir_rvalue *old_index,
       this->base_ir->insert_before(new(ctx) ir_assignment(
          new(ctx) ir_dereference_variable(old_index_var), old_index));
 
-      /* Create the expression distance_index / 4.  Do this as a bit shift
-       * because that's likely to be more efficient.
+      /* Create the expression distance_index / 4.  Do this as a bit
+       * shift because that's likely to be more efficient.
        */
       array_index = new(ctx) ir_expression(
-         ir_binop_rshift, new(ctx) ir_dereference_variable(old_index_var),
+         ir_binop_rshift,
+         new(ctx) ir_expression(ir_binop_add,
+                                new(ctx) ir_dereference_variable(old_index_var),
+                                new(ctx) ir_constant(offset)),
          new(ctx) ir_constant(2));
 
-      /* Create the expression distance_index % 4.  Do this as a bitwise AND
-       * because that's likely to be more efficient.
+      /* Create the expression distance_index % 4.  Do this as a bitwise
+       * AND because that's likely to be more efficient.
        */
       swizzle_index = new(ctx) ir_expression(
-         ir_binop_bit_and, new(ctx) ir_dereference_variable(old_index_var),
+         ir_binop_bit_and,
+         new(ctx) ir_expression(ir_binop_add,
+                                new(ctx) ir_dereference_variable(old_index_var),
+                                new(ctx) ir_constant(offset)),
          new(ctx) ir_constant(3));
    }
 }
@@ -272,9 +290,9 @@ lower_distance_visitor::create_indices(ir_rvalue *old_index,
 bool
 lower_distance_visitor::is_distance_vec8(ir_rvalue *ir)
 {
-   /* Note that geometry shaders contain in_name
-    * both as an input (which is a 2D array) and an output (which is a 1D
-    * array), so it's possible for both this->old_distance_out_var and
+   /* Note that geometry shaders contain gl_ClipDistance both as an input
+    * (which is a 2D array) and an output (which is a 1D array), so it's
+    * possible for both this->old_distance_out_var and
     * this->old_distance_in_var to be non-NULL in the same shader.
     */
 
@@ -352,9 +370,9 @@ lower_distance_visitor::handle_rvalue(ir_rvalue **rv)
    if (array_deref == NULL)
       return;
 
-   /* Replace any expression that indexes one of the floats in in_name
-    * or in_name with an expression that indexes into one of the vec4's
-    * in out_name and accesses the appropriate component.
+   /* Replace any expression that indexes one of the floats in gl_ClipDistance
+    * with an expression that indexes into one of the vec4's in
+    * gl_ClipDistanceMESA and accesses the appropriate component.
     */
    ir_rvalue *lowered_vec8 =
       this->lower_distance_vec8(array_deref->array);
@@ -394,22 +412,22 @@ lower_distance_visitor::fix_lhs(ir_assignment *ir)
 
       ir_dereference *const new_lhs = (ir_dereference *) expr->operands[0];
       ir->rhs = new(mem_ctx) ir_expression(ir_triop_vector_insert,
-                                          glsl_type::vec4_type,
-                                          new_lhs->clone(mem_ctx, NULL),
-                                          ir->rhs,
-                                          expr->operands[1]);
+                                           glsl_type::vec4_type,
+                                           new_lhs->clone(mem_ctx, NULL),
+                                           ir->rhs,
+                                           expr->operands[1]);
       ir->set_lhs(new_lhs);
       ir->write_mask = WRITEMASK_XYZW;
    }
 }
 
 /**
- * Replace any assignment having the 1D in_name (undereferenced) as
+ * Replace any assignment having the 1D gl_ClipDistance (undereferenced) as
  * its LHS or RHS with a sequence of assignments, one for each component of
  * the array.  Each of these assignments is lowered to refer to
- * out_name as appropriate.
+ * gl_ClipDistanceMESA as appropriate.
  *
- * We need to do a similar replacement for 2D in_name, however since
+ * We need to do a similar replacement for 2D gl_ClipDistance, however since
  * it's an input, the only case we need to address is where a 1D slice of it
  * is the entire RHS of an assignment, e.g.:
  *
@@ -425,9 +443,9 @@ lower_distance_visitor::visit_leave(ir_assignment *ir)
 
    if (this->is_distance_vec8(ir->lhs) ||
        this->is_distance_vec8(ir->rhs)) {
-      /* LHS or RHS of the assignment is the entire 1D in_name array
-       * (or a 1D slice of a 2D in_name input array).  Since we are
-       * reshaping in_name from an array of floats to an array of
+      /* LHS or RHS of the assignment is the entire 1D gl_ClipDistance array
+       * (or a 1D slice of a 2D gl_ClipDistance input array).  Since we are
+       * reshaping gl_ClipDistance from an array of floats to an array of
        * vec4's, this isn't going to work as a bulk assignment anymore, so
        * unroll it to element-by-element assignments and lower each of them.
        *
@@ -496,13 +514,13 @@ lower_distance_visitor::visit_new_assignment(ir_assignment *ir)
 
 
 /**
- * If a 1D in_name variable appears as an argument in an ir_call
+ * If a 1D gl_ClipDistance variable appears as an argument in an ir_call
  * expression, replace it with a temporary variable, and make sure the ir_call
  * is preceded and/or followed by assignments that copy the contents of the
- * temporary variable to and/or from in_name.  Each of these
- * assignments is then lowered to refer to out_name.
+ * temporary variable to and/or from gl_ClipDistance.  Each of these
+ * assignments is then lowered to refer to gl_ClipDistanceMESA.
  *
- * We need to do a similar replacement for 2D in_name, however since
+ * We need to do a similar replacement for 2D gl_ClipDistance, however since
  * it's an input, the only case we need to address is where a 1D slice of it
  * is passed as an "in" parameter to an ir_call, e.g.:
  *
@@ -513,8 +531,8 @@ lower_distance_visitor::visit_leave(ir_call *ir)
 {
    void *ctx = ralloc_parent(ir);
 
-   const exec_node *formal_param_node = ir->callee->parameters.head;
-   const exec_node *actual_param_node = ir->actual_parameters.head;
+   const exec_node *formal_param_node = ir->callee->parameters.get_head_raw();
+   const exec_node *actual_param_node = ir->actual_parameters.get_head_raw();
    while (!actual_param_node->is_tail_sentinel()) {
       ir_variable *formal_param = (ir_variable *) formal_param_node;
       ir_rvalue *actual_param = (ir_rvalue *) actual_param_node;
@@ -526,40 +544,40 @@ lower_distance_visitor::visit_leave(ir_call *ir)
       actual_param_node = actual_param_node->next;
 
       if (this->is_distance_vec8(actual_param)) {
-         /* User is trying to pass the whole 1D in_name array (or a 1D
-          * slice of a 2D in_name array) to a function call.  Since we
-          * are reshaping in_name from an array of floats to an array
+         /* User is trying to pass the whole 1D gl_ClipDistance array (or a 1D
+          * slice of a 2D gl_ClipDistance array) to a function call.  Since we
+          * are reshaping gl_ClipDistance from an array of floats to an array
           * of vec4's, this isn't going to work anymore, so use a temporary
           * array instead.
           */
-         ir_variable *temp_distance = new(ctx) ir_variable(
-            actual_param->type, "temp_distance", ir_var_temporary);
-         this->base_ir->insert_before(temp_distance);
+         ir_variable *temp_clip_distance = new(ctx) ir_variable(
+            actual_param->type, "temp_clip_distance", ir_var_temporary);
+         this->base_ir->insert_before(temp_clip_distance);
          actual_param->replace_with(
-            new(ctx) ir_dereference_variable(temp_distance));
+            new(ctx) ir_dereference_variable(temp_clip_distance));
          if (formal_param->data.mode == ir_var_function_in
              || formal_param->data.mode == ir_var_function_inout) {
-            /* Copy from in_name to the temporary before the call.
+            /* Copy from gl_ClipDistance to the temporary before the call.
              * Since we are going to insert this copy before the current
              * instruction, we need to visit it afterwards to make sure it
              * gets lowered.
              */
             ir_assignment *new_assignment = new(ctx) ir_assignment(
-               new(ctx) ir_dereference_variable(temp_distance),
+               new(ctx) ir_dereference_variable(temp_clip_distance),
                actual_param->clone(ctx, NULL));
             this->base_ir->insert_before(new_assignment);
             this->visit_new_assignment(new_assignment);
          }
          if (formal_param->data.mode == ir_var_function_out
              || formal_param->data.mode == ir_var_function_inout) {
-            /* Copy from the temporary to in_name after the call.
+            /* Copy from the temporary to gl_ClipDistance after the call.
              * Since visit_list_elements() has already decided which
              * instruction it's going to visit next, we need to visit
              * afterwards to make sure it gets lowered.
              */
             ir_assignment *new_assignment = new(ctx) ir_assignment(
                actual_param->clone(ctx, NULL),
-               new(ctx) ir_dereference_variable(temp_distance));
+               new(ctx) ir_dereference_variable(temp_clip_distance));
             this->base_ir->insert_after(new_assignment);
             this->visit_new_assignment(new_assignment);
          }
@@ -569,67 +587,99 @@ lower_distance_visitor::visit_leave(ir_call *ir)
    return rvalue_visit(ir);
 }
 
-static ir_variable *
-create_clip_distance_mesa(ir_variable *base, int new_size)
+namespace {
+class lower_distance_visitor_counter : public ir_rvalue_visitor {
+public:
+   explicit lower_distance_visitor_counter(void)
+      : in_clip_size(0), in_cull_size(0),
+        out_clip_size(0), out_cull_size(0)
+   {
+   }
+
+   virtual ir_visitor_status visit(ir_variable *);
+   virtual void handle_rvalue(ir_rvalue **rvalue);
+
+   int in_clip_size;
+   int in_cull_size;
+   int out_clip_size;
+   int out_cull_size;
+};
+
+}
+/**
+ * Count gl_ClipDistance and gl_CullDistance sizes.
+ */
+ir_visitor_status
+lower_distance_visitor_counter::visit(ir_variable *ir)
 {
-   ir_variable *new_var;
-
-   if (!base->type->fields.array->is_array()) {
-     new_var = base->clone(ralloc_parent(base), NULL);
-     new_var->name = ralloc_strdup(new_var, "gl_ClipDistanceMESA");
-     new_var->type = glsl_type::get_array_instance(glsl_type::vec4_type,
-                                                   new_size);
-     new_var->data.max_array_access = base->data.max_array_access / 4;
-   } else {
-      /* Clone the old var so that we inherit all of its properties */
-      new_var = base->clone(ralloc_parent(base), NULL);
-      /* And change the properties that we need to change */
-      new_var->name = ralloc_strdup(new_var, "gl_ClipDistanceMESA");
-      new_var->type = glsl_type::get_array_instance(
-                                                    glsl_type::get_array_instance(glsl_type::vec4_type,
-                                                                                  new_size),
-                                                    base->type->array_size());
-      new_var->data.max_array_access = base->data.max_array_access / 4;
+   int *clip_size, *cull_size;
+
+   if (!ir->name)
+      return visit_continue;
+
+   if (ir->data.mode == ir_var_shader_out) {
+      clip_size = &out_clip_size;
+      cull_size = &out_cull_size;
+   } else if (ir->data.mode == ir_var_shader_in) {
+      clip_size = &in_clip_size;
+      cull_size = &in_cull_size;
+   } else
+      return visit_continue;
+
+   if (ir->type->is_unsized_array())
+      return visit_continue;
+
+   if (*clip_size == 0) {
+      if (!strcmp(ir->name, "gl_ClipDistance")) {
+         if (!ir->type->fields.array->is_array())
+            *clip_size = ir->type->array_size();
+         else
+            *clip_size = ir->type->fields.array->array_size();
+      }
+   }
+
+   if (*cull_size == 0) {
+      if (!strcmp(ir->name, "gl_CullDistance")) {
+         if (!ir->type->fields.array->is_array())
+            *cull_size = ir->type->array_size();
+         else
+            *cull_size = ir->type->fields.array->array_size();
+      }
    }
-   return new_var;
+   return visit_continue;
+}
+
+void
+lower_distance_visitor_counter::handle_rvalue(ir_rvalue **)
+{
+   return;
 }
 
 bool
-lower_combined_clip_cull_distance(gl_shader *shader,
-                                       uint8_t ClipDistanceArraySize,
-                                       uint8_t CullDistanceArraySize)
+lower_clip_cull_distance(struct gl_shader_program *prog,
+                         struct gl_linked_shader *shader)
 {
-   ir_variable *clipdist = shader->symbols->get_variable("gl_ClipDistance");
-   ir_variable *culldist = shader->symbols->get_variable("gl_CullDistance");
-   ir_variable *new_var;
-   unsigned new_size = ((ClipDistanceArraySize + CullDistanceArraySize) + 3) / 4;
-   bool progress = false;
-   bool replace_var = true;
-
-   if (ClipDistanceArraySize == 0 && CullDistanceArraySize == 0)
-      return false;
+   int clip_size, cull_size;
 
-   new_var = create_clip_distance_mesa(clipdist ? clipdist : culldist, new_size);
+   lower_distance_visitor_counter count;
+   visit_list_elements(&count, shader->ir);
 
-   if (ClipDistanceArraySize) {
-      lower_distance_visitor v(shader->Stage, "gl_ClipDistance",
-                               new_var, ClipDistanceArraySize, CullDistanceArraySize, false, replace_var);
+   clip_size = MAX2(count.in_clip_size, count.out_clip_size);
+   cull_size = MAX2(count.in_cull_size, count.out_cull_size);
 
-      visit_list_elements(&v, shader->ir);
-      replace_var = v.replace_var;
-      progress = v.progress;
-   }
+   if (clip_size == 0 && cull_size == 0)
+      return false;
 
-   if (CullDistanceArraySize) {
-      lower_distance_visitor v2(shader->Stage, "gl_CullDistance",
-                                new_var, ClipDistanceArraySize, CullDistanceArraySize, true, replace_var);
+   lower_distance_visitor v(shader->Stage, "gl_ClipDistance", clip_size + cull_size, 0);
+   visit_list_elements(&v, shader->ir);
 
-      visit_list_elements(&v2, shader->ir);
-      progress |= v2.progress;
-   }
+   lower_distance_visitor v2(shader->Stage, "gl_CullDistance", &v, clip_size);
+   visit_list_elements(&v2, shader->ir);
 
-   shader->symbols->add_variable(new_var);
+   if (v2.new_distance_out_var)
+      shader->symbols->add_variable(v2.new_distance_out_var);
+   if (v2.new_distance_in_var)
+      shader->symbols->add_variable(v2.new_distance_in_var);
 
-   validate_ir_tree(shader->ir);
-   return progress;
+   return v2.progress;
 }