[Mesa-dev] [PATCH 5/6] glsl: Extend lowering pass for gl_ClipDistance to support other arrays (v3)

Dave Airlie airlied at gmail.com
Fri May 13 04:41:25 UTC 2016


From: Tobias Klausmann <tobias.johannes.klausmann at mni.thm.de>

This will come in handy when we want to lower gl_CullDistance into
gl_CullDistanceMESA.

[airlied: drop separate APIs for clip/cull - just use single API
to call both passes.]

v3: reexamine my sanity, this was pretty broken, the new code
creates one copy of gl_ClipDistanceMESA, as the clip distance
varying and lowers everything into that in two passes, one for clips
one for culls.

Signed-off-by: Tobias Klausmann <tobias.johannes.klausmann at mni.thm.de>
Signed-off-by: Dave Airlie <airlied at redhat.com>
---
 src/compiler/glsl/ir_optimization.h  |   3 +-
 src/compiler/glsl/linker.cpp         |   2 +-
 src/compiler/glsl/lower_distance.cpp | 338 +++++++++++++++++++++--------------
 3 files changed, 203 insertions(+), 140 deletions(-)

diff --git a/src/compiler/glsl/ir_optimization.h b/src/compiler/glsl/ir_optimization.h
index f9599a3..9843442 100644
--- a/src/compiler/glsl/ir_optimization.h
+++ b/src/compiler/glsl/ir_optimization.h
@@ -117,7 +117,8 @@ bool lower_variable_index_to_cond_assign(gl_shader_stage stage,
     bool lower_temp, bool lower_uniform);
 bool lower_quadop_vector(exec_list *instructions, bool dont_lower_swz);
 bool lower_const_arrays_to_uniforms(exec_list *instructions);
-bool lower_clip_distance(gl_shader *shader);
+bool lower_combined_clip_cull_distance(gl_shader *shader,
+                                       uint8_t clipDistanceArraySize);
 void lower_output_reads(unsigned stage, exec_list *instructions);
 bool lower_packing_builtins(exec_list *instructions, int op_mask);
 void lower_shared_reference(struct gl_shader *shader, unsigned *shared_size);
diff --git a/src/compiler/glsl/linker.cpp b/src/compiler/glsl/linker.cpp
index 2a520bc..38b9dcd 100644
--- a/src/compiler/glsl/linker.cpp
+++ b/src/compiler/glsl/linker.cpp
@@ -4561,7 +4561,7 @@ link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
 	 goto done;
 
       if (ctx->Const.ShaderCompilerOptions[i].LowerCombinedClipCullDistance) {
-         lower_clip_distance(prog->_LinkedShaders[i]);
+         lower_combined_clip_cull_distance(prog->_LinkedShaders[i], 0);
       }
 
       if (ctx->Const.LowerTessLevel) {
diff --git a/src/compiler/glsl/lower_distance.cpp b/src/compiler/glsl/lower_distance.cpp
index 5d9468d..fda011f 100644
--- a/src/compiler/glsl/lower_distance.cpp
+++ b/src/compiler/glsl/lower_distance.cpp
@@ -22,17 +22,20 @@
  */
 
 /**
- * \file lower_clip_distance.cpp
+ * \file lower_distance.cpp
  *
  * This pass accounts for the difference between the way
- * 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).
+ * 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).
  *
- * 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:
+ * The declaration of gl_ClipDistance or gl_CullDistance is replaced
+ * with a declaration of gl_ClipDistanceMESA or gl_CullDistanceMESA
+ * respectively, and any references to the original gl_ClipDistance
+ * or gl_CullDistance are translated to refer to gl_ClipDistanceMESA
+ * or gl_CullDistanceMESA with the appropriate swizzling of array
+ * indices.  For instance:
  *
  *   gl_ClipDistance[i]
  *
@@ -40,11 +43,12 @@
  *
  *   gl_ClipDistanceMESA[i>>2][i&3]
  *
- * 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.
+ * 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.
  */
 
+#include <string>
 #include "glsl_symbol_table.h"
 #include "ir_rvalue_visitor.h"
 #include "ir.h"
@@ -52,19 +56,25 @@
 
 namespace {
 
-class lower_clip_distance_visitor : public ir_rvalue_visitor {
+class lower_distance_visitor : public ir_rvalue_visitor {
 public:
-   explicit lower_clip_distance_visitor(gl_shader_stage shader_stage)
-      : progress(false), old_clip_distance_out_var(NULL),
-        old_clip_distance_in_var(NULL), new_clip_distance_out_var(NULL),
-        new_clip_distance_in_var(NULL), shader_stage(shader_stage)
+   explicit lower_distance_visitor(gl_shader_stage shader_stage,
+                                   std::string in_name, ir_variable *out_var,
+                                   int num_clip_dist, int num_cull_dist,
+                                   bool is_cull, bool replace_var)
+      : 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)
    {
    }
 
    virtual ir_visitor_status visit(ir_variable *);
    void create_indices(ir_rvalue*, ir_rvalue *&, ir_rvalue *&);
-   bool is_clip_distance_vec8(ir_rvalue *ir);
-   ir_rvalue *lower_clip_distance_vec8(ir_rvalue *ir);
+   bool is_distance_vec8(ir_rvalue *ir);
+   ir_rvalue *lower_distance_vec8(ir_rvalue *ir);
    virtual ir_visitor_status visit_leave(ir_assignment *);
    void visit_new_assignment(ir_assignment *ir);
    virtual ir_visitor_status visit_leave(ir_call *);
@@ -76,7 +86,7 @@ public:
    bool progress;
 
    /**
-    * Pointer to the declaration of gl_ClipDistance, if found.
+    * Pointer to the declaration of ou arrays, if found.
     *
     * Note:
     *
@@ -84,50 +94,63 @@ public:
     *
     * - since gl_ClipDistance is available in tessellation control,
     *   tessellation evaluation and geometry shaders as both an input
-    *   and an output, it's possible for both old_clip_distance_out_var
-    *   and old_clip_distance_in_var to be non-null.
+    *   and an output, it's possible for both old_distance_out_var
+    *   and old_distance_in_var to be non-null.
     */
-   ir_variable *old_clip_distance_out_var;
-   ir_variable *old_clip_distance_in_var;
+   ir_variable *old_distance_out_var;
+   ir_variable *old_distance_in_var;
 
    /**
-    * Pointer to the newly-created gl_ClipDistanceMESA variable.
+    * Pointer to the newly-created variable.
     */
-   ir_variable *new_clip_distance_out_var;
-   ir_variable *new_clip_distance_in_var;
+   ir_variable *new_distance_out_var;
+   ir_variable *new_distance_in_var;
 
    /**
     * Type of shader we are compiling (e.g. MESA_SHADER_VERTEX)
     */
    const gl_shader_stage shader_stage;
+
+   /**
+    *  Identifier of the variables we manipulate
+    */
+
+   std::string in_name;
+
+   ir_variable *out_var;
+   uint8_t num_clip_dist;
+   uint8_t num_cull_dist;
+   bool is_cull;
+   bool replace_var;
 };
 
 } /* anonymous namespace */
 
+
 /**
- * Replace any declaration of gl_ClipDistance as an array of floats with a
- * declaration of gl_ClipDistanceMESA as an array of vec4's.
+ * Replace any declaration of in_name as an array of floats with a
+ * declaration of out_name as an array of vec4's.
  */
 ir_visitor_status
-lower_clip_distance_visitor::visit(ir_variable *ir)
+lower_distance_visitor::visit(ir_variable *ir)
 {
    ir_variable **old_var;
    ir_variable **new_var;
 
-   if (!ir->name || strcmp(ir->name, "gl_ClipDistance") != 0)
+   if (!ir->name || in_name.compare(ir->name) != 0)
       return visit_continue;
    assert (ir->type->is_array());
 
    if (ir->data.mode == ir_var_shader_out) {
-      if (this->old_clip_distance_out_var)
+      if (this->old_distance_out_var)
          return visit_continue;
-      old_var = &old_clip_distance_out_var;
-      new_var = &new_clip_distance_out_var;
+      old_var = &old_distance_out_var;
+      new_var = &new_distance_out_var;
    } else if (ir->data.mode == ir_var_shader_in) {
-      if (this->old_clip_distance_in_var)
+      if (this->old_distance_in_var)
          return visit_continue;
-      old_var = &old_clip_distance_in_var;
-      new_var = &new_clip_distance_in_var;
+      old_var = &old_distance_in_var;
+      new_var = &new_distance_in_var;
    } else {
       unreachable("not reached");
    }
@@ -135,8 +158,8 @@ lower_clip_distance_visitor::visit(ir_variable *ir)
    this->progress = true;
 
    if (!ir->type->fields.array->is_array()) {
-      /* gl_ClipDistance (used for vertex, tessellation evaluation and
-       * geometry output, and fragment input).
+      /* 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) ||
@@ -147,21 +170,17 @@ lower_clip_distance_visitor::visit(ir_variable *ir)
 
       *old_var = ir;
       assert (ir->type->fields.array == glsl_type::float_type);
-      unsigned new_size = (ir->type->array_size() + 3) / 4;
-
-      /* Clone the old var so that we inherit all of its properties */
-      *new_var = ir->clone(ralloc_parent(ir), 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::vec4_type,
-                                                       new_size);
-      (*new_var)->data.max_array_access = ir->data.max_array_access / 4;
-
-      ir->replace_with(*new_var);
+      *new_var = out_var;
+      if (replace_var == true) {
+         ir->replace_with(*new_var);
+         replace_var = false;
+      } else {
+         ir->remove();
+      }
    } else {
-      /* 2D gl_ClipDistance (used for tessellation control, tessellation
-       * evaluation and geometry input, and tessellation control output).
+      /* 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 ||
@@ -170,20 +189,13 @@ lower_clip_distance_visitor::visit(ir_variable *ir)
 
       *old_var = ir;
       assert (ir->type->fields.array->fields.array == glsl_type::float_type);
-      unsigned new_size = (ir->type->fields.array->array_size() + 3) / 4;
-
-      /* Clone the old var so that we inherit all of its properties */
-      *new_var = ir->clone(ralloc_parent(ir), 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),
-         ir->type->array_size());
-      (*new_var)->data.max_array_access = ir->data.max_array_access / 4;
-
-      ir->replace_with(*new_var);
+      *new_var = out_var;
+      if (replace_var == true) {
+         ir->replace_with(*new_var);
+         replace_var = false;
+      } else {
+         ir->remove();
+      }
    }
 
    return visit_continue;
@@ -191,15 +203,15 @@ lower_clip_distance_visitor::visit(ir_variable *ir)
 
 
 /**
- * Create the necessary GLSL rvalues to index into gl_ClipDistanceMESA based
+ * Create the necessary GLSL rvalues to index into out_name based
  * on the rvalue previously used to index into gl_ClipDistance.
  *
- * \param array_index Selects one of the vec4's in gl_ClipDistanceMESA
+ * \param array_index Selects one of the vec4's in out_name
  * \param swizzle_index Selects a component within the vec4 selected by
  *        array_index.
  */
 void
-lower_clip_distance_visitor::create_indices(ir_rvalue *old_index,
+lower_distance_visitor::create_indices(ir_rvalue *old_index,
                                             ir_rvalue *&array_index,
                                             ir_rvalue *&swizzle_index)
 {
@@ -215,32 +227,33 @@ lower_clip_distance_visitor::create_indices(ir_rvalue *old_index,
 
    ir_constant *old_index_constant = old_index->constant_expression_value();
    if (old_index_constant) {
-      /* gl_ClipDistance is being accessed via a constant index.  Don't bother
-       * creating expressions to calculate the lowered indices.  Just create
-       * constants.
+      /* gl_ClipDistance / gl_CullDistance 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);
-      array_index = new(ctx) ir_constant(const_val / 4);
-      swizzle_index = new(ctx) ir_constant(const_val % 4);
+      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);
    } else {
       /* Create a variable to hold the value of old_index (so that we
        * don't compute it twice).
        */
       ir_variable *old_index_var = new(ctx) ir_variable(
-         glsl_type::int_type, "clip_distance_index", ir_var_temporary);
+         glsl_type::int_type, "distance_index", ir_var_temporary);
       this->base_ir->insert_before(old_index_var);
       this->base_ir->insert_before(new(ctx) ir_assignment(
          new(ctx) ir_dereference_variable(old_index_var), old_index));
 
-      /* Create the expression clip_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),
          new(ctx) ir_constant(2));
 
-      /* Create the expression clip_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),
@@ -258,12 +271,12 @@ lower_clip_distance_visitor::create_indices(ir_rvalue *old_index,
  * - gl_ClipDistance[i] (if gl_ClipDistance is 2D)
  */
 bool
-lower_clip_distance_visitor::is_clip_distance_vec8(ir_rvalue *ir)
+lower_distance_visitor::is_distance_vec8(ir_rvalue *ir)
 {
-   /* 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_clip_distance_out_var and
-    * this->old_clip_distance_in_var to be non-NULL in the same shader.
+   /* 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
+    * this->old_distance_in_var to be non-NULL in the same shader.
     */
 
    if (!ir->type->is_array())
@@ -271,17 +284,17 @@ lower_clip_distance_visitor::is_clip_distance_vec8(ir_rvalue *ir)
    if (ir->type->fields.array != glsl_type::float_type)
       return false;
 
-   if (this->old_clip_distance_out_var) {
-      if (ir->variable_referenced() == this->old_clip_distance_out_var)
+   if (this->old_distance_out_var) {
+      if (ir->variable_referenced() == this->old_distance_out_var)
          return true;
    }
-   if (this->old_clip_distance_in_var) {
+   if (this->old_distance_in_var) {
       assert(this->shader_stage == MESA_SHADER_TESS_CTRL ||
              this->shader_stage == MESA_SHADER_TESS_EVAL ||
              this->shader_stage == MESA_SHADER_GEOMETRY ||
              this->shader_stage == MESA_SHADER_FRAGMENT);
 
-      if (ir->variable_referenced() == this->old_clip_distance_in_var)
+      if (ir->variable_referenced() == this->old_distance_in_var)
          return true;
    }
    return false;
@@ -289,7 +302,7 @@ lower_clip_distance_visitor::is_clip_distance_vec8(ir_rvalue *ir)
 
 
 /**
- * If the given ir satisfies is_clip_distance_vec8(), return new ir
+ * If the given ir satisfies is_distance_vec8(), return new ir
  * representing its lowered equivalent.  That is, map:
  *
  * - gl_ClipDistance    => gl_ClipDistanceMESA    (if gl_ClipDistance is 1D)
@@ -298,7 +311,7 @@ lower_clip_distance_visitor::is_clip_distance_vec8(ir_rvalue *ir)
  * Otherwise return NULL.
  */
 ir_rvalue *
-lower_clip_distance_visitor::lower_clip_distance_vec8(ir_rvalue *ir)
+lower_distance_visitor::lower_distance_vec8(ir_rvalue *ir)
 {
    if (!ir->type->is_array())
       return NULL;
@@ -306,13 +319,13 @@ lower_clip_distance_visitor::lower_clip_distance_vec8(ir_rvalue *ir)
       return NULL;
 
    ir_variable **new_var = NULL;
-   if (this->old_clip_distance_out_var) {
-      if (ir->variable_referenced() == this->old_clip_distance_out_var)
-         new_var = &this->new_clip_distance_out_var;
+   if (this->old_distance_out_var) {
+      if (ir->variable_referenced() == this->old_distance_out_var)
+         new_var = &this->new_distance_out_var;
    }
-   if (this->old_clip_distance_in_var) {
-      if (ir->variable_referenced() == this->old_clip_distance_in_var)
-         new_var = &this->new_clip_distance_in_var;
+   if (this->old_distance_in_var) {
+      if (ir->variable_referenced() == this->old_distance_in_var)
+         new_var = &this->new_distance_in_var;
    }
    if (new_var == NULL)
       return NULL;
@@ -331,7 +344,7 @@ lower_clip_distance_visitor::lower_clip_distance_vec8(ir_rvalue *ir)
 
 
 void
-lower_clip_distance_visitor::handle_rvalue(ir_rvalue **rv)
+lower_distance_visitor::handle_rvalue(ir_rvalue **rv)
 {
    if (*rv == NULL)
       return;
@@ -340,12 +353,12 @@ lower_clip_distance_visitor::handle_rvalue(ir_rvalue **rv)
    if (array_deref == NULL)
       return;
 
-   /* 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.
+   /* 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.
     */
    ir_rvalue *lowered_vec8 =
-      this->lower_clip_distance_vec8(array_deref->array);
+      this->lower_distance_vec8(array_deref->array);
    if (lowered_vec8 != NULL) {
       this->progress = true;
       ir_rvalue *array_index;
@@ -366,7 +379,7 @@ lower_clip_distance_visitor::handle_rvalue(ir_rvalue **rv)
 }
 
 void
-lower_clip_distance_visitor::fix_lhs(ir_assignment *ir)
+lower_distance_visitor::fix_lhs(ir_assignment *ir)
 {
    if (ir->lhs->ir_type == ir_type_expression) {
       void *mem_ctx = ralloc_parent(ir);
@@ -392,30 +405,30 @@ lower_clip_distance_visitor::fix_lhs(ir_assignment *ir)
 }
 
 /**
- * Replace any assignment having the 1D gl_ClipDistance (undereferenced) as
+ * Replace any assignment having the 1D in_name (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
- * gl_ClipDistanceMESA as appropriate.
+ * out_name as appropriate.
  *
- * We need to do a similar replacement for 2D gl_ClipDistance, however since
+ * We need to do a similar replacement for 2D in_name, 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.:
  *
  *     foo = gl_in[i].gl_ClipDistance
  */
 ir_visitor_status
-lower_clip_distance_visitor::visit_leave(ir_assignment *ir)
+lower_distance_visitor::visit_leave(ir_assignment *ir)
 {
    /* First invoke the base class visitor.  This causes handle_rvalue() to be
     * called on ir->rhs and ir->condition.
     */
    ir_rvalue_visitor::visit_leave(ir);
 
-   if (this->is_clip_distance_vec8(ir->lhs) ||
-       this->is_clip_distance_vec8(ir->rhs)) {
-      /* 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
+   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
        * 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.
        *
@@ -474,7 +487,7 @@ lower_clip_distance_visitor::visit_leave(ir_assignment *ir)
  * it.
  */
 void
-lower_clip_distance_visitor::visit_new_assignment(ir_assignment *ir)
+lower_distance_visitor::visit_new_assignment(ir_assignment *ir)
 {
    ir_instruction *old_base_ir = this->base_ir;
    this->base_ir = ir;
@@ -484,20 +497,20 @@ lower_clip_distance_visitor::visit_new_assignment(ir_assignment *ir)
 
 
 /**
- * If a 1D gl_ClipDistance variable appears as an argument in an ir_call
+ * If a 1D in_name 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 gl_ClipDistance.  Each of these
- * assignments is then lowered to refer to gl_ClipDistanceMESA.
+ * temporary variable to and/or from in_name.  Each of these
+ * assignments is then lowered to refer to out_name.
  *
- * We need to do a similar replacement for 2D gl_ClipDistance, however since
+ * We need to do a similar replacement for 2D in_name, 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.:
  *
  *     foo(gl_in[i].gl_ClipDistance)
  */
 ir_visitor_status
-lower_clip_distance_visitor::visit_leave(ir_call *ir)
+lower_distance_visitor::visit_leave(ir_call *ir)
 {
    void *ctx = ralloc_parent(ir);
 
@@ -513,41 +526,41 @@ lower_clip_distance_visitor::visit_leave(ir_call *ir)
       formal_param_node = formal_param_node->next;
       actual_param_node = actual_param_node->next;
 
-      if (this->is_clip_distance_vec8(actual_param)) {
-         /* 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
+      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
           * of vec4's, this isn't going to work anymore, so use a temporary
           * array instead.
           */
-         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);
+         ir_variable *temp_distance = new(ctx) ir_variable(
+            actual_param->type, "temp_distance", ir_var_temporary);
+         this->base_ir->insert_before(temp_distance);
          actual_param->replace_with(
-            new(ctx) ir_dereference_variable(temp_clip_distance));
+            new(ctx) ir_dereference_variable(temp_distance));
          if (formal_param->data.mode == ir_var_function_in
              || formal_param->data.mode == ir_var_function_inout) {
-            /* Copy from gl_ClipDistance to the temporary before the call.
+            /* Copy from in_name 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_clip_distance),
+               new(ctx) ir_dereference_variable(temp_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 gl_ClipDistance after the call.
+            /* Copy from the temporary to in_name 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_clip_distance));
+               new(ctx) ir_dereference_variable(temp_distance));
             this->base_ir->insert_after(new_assignment);
             this->visit_new_assignment(new_assignment);
          }
@@ -557,18 +570,67 @@ lower_clip_distance_visitor::visit_leave(ir_call *ir)
    return rvalue_visit(ir);
 }
 
+static ir_variable *create_clip_distance_mesa(ir_variable *base,
+                                              int new_size)
+{
+   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;
+   }
+   return new_var;
+}
 
-bool
-lower_clip_distance(gl_shader *shader)
+bool lower_combined_clip_cull_distance(gl_shader *shader,
+                                       uint8_t ClipDistanceArraySize)
 {
-   lower_clip_distance_visitor v(shader->Stage);
+  ir_variable *clipdist = shader->symbols->get_variable("gl_ClipDistance");
+  ir_variable *culldist = shader->symbols->get_variable("gl_CullDistance");
+  int num_clip_dist = clipdist ? clipdist->type->array_size() : 0;
+  int num_cull_dist = culldist ? culldist->type->array_size() : 0;
+  ir_variable *new_var;
+  unsigned new_size = ((num_clip_dist + num_cull_dist) + 3) / 4;
+  bool progress = false;
+  bool replace_var = true;
+
+  if (num_clip_dist == 0 && num_cull_dist == 0)
+      return false;
+
+  new_var = create_clip_distance_mesa(clipdist ? clipdist : culldist, new_size);
+
+  if (num_clip_dist) {
+     lower_distance_visitor v(shader->Stage, "gl_ClipDistance",
+                              new_var, num_clip_dist, num_cull_dist, false, replace_var);
+
+     visit_list_elements(&v, shader->ir);
+     replace_var = v.replace_var;
+     progress = v.progress;
+  }
+
+  if (num_cull_dist) {
+     lower_distance_visitor v2(shader->Stage, "gl_CullDistance",
+                               new_var, num_clip_dist, num_cull_dist, true, replace_var);
 
-   visit_list_elements(&v, shader->ir);
+     visit_list_elements(&v2, shader->ir);
+     progress |= v2.progress;
+  }
 
-   if (v.new_clip_distance_out_var)
-      shader->symbols->add_variable(v.new_clip_distance_out_var);
-   if (v.new_clip_distance_in_var)
-      shader->symbols->add_variable(v.new_clip_distance_in_var);
+   shader->symbols->add_variable(new_var);
 
-   return v.progress;
+   validate_ir_tree(shader->ir);
+   return progress;
 }
-- 
2.1.0



More information about the mesa-dev mailing list