101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2011 Intel Corporation
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2101e04c3fSmrg * DEALINGS IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg
2401e04c3fSmrg/**
2501e04c3fSmrg * \file lower_distance.cpp
2601e04c3fSmrg *
2701e04c3fSmrg * This pass accounts for the difference between the way
2801e04c3fSmrg * gl_ClipDistance is declared in standard GLSL (as an array of
2901e04c3fSmrg * floats), and the way it is frequently implemented in hardware (as
3001e04c3fSmrg * a pair of vec4s, with four clip distances packed into each).
3101e04c3fSmrg *
3201e04c3fSmrg * The declaration of gl_ClipDistance is replaced with a declaration
3301e04c3fSmrg * of gl_ClipDistanceMESA, and any references to gl_ClipDistance are
3401e04c3fSmrg * translated to refer to gl_ClipDistanceMESA with the appropriate
3501e04c3fSmrg * swizzling of array indices.  For instance:
3601e04c3fSmrg *
3701e04c3fSmrg *   gl_ClipDistance[i]
3801e04c3fSmrg *
3901e04c3fSmrg * is translated into:
4001e04c3fSmrg *
4101e04c3fSmrg *   gl_ClipDistanceMESA[i>>2][i&3]
4201e04c3fSmrg *
4301e04c3fSmrg * Since some hardware may not internally represent gl_ClipDistance as a pair
4401e04c3fSmrg * of vec4's, this lowering pass is optional.  To enable it, set the
4501e04c3fSmrg * LowerCombinedClipCullDistance flag in gl_shader_compiler_options to true.
4601e04c3fSmrg */
4701e04c3fSmrg
4801e04c3fSmrg#include "main/macros.h"
4901e04c3fSmrg#include "glsl_symbol_table.h"
5001e04c3fSmrg#include "ir_rvalue_visitor.h"
5101e04c3fSmrg#include "ir.h"
5201e04c3fSmrg#include "program/prog_instruction.h" /* For WRITEMASK_* */
5301e04c3fSmrg#include "main/mtypes.h"
5401e04c3fSmrg
5501e04c3fSmrg#define GLSL_CLIP_VAR_NAME "gl_ClipDistanceMESA"
5601e04c3fSmrg
5701e04c3fSmrgnamespace {
5801e04c3fSmrg
5901e04c3fSmrgclass lower_distance_visitor : public ir_rvalue_visitor {
6001e04c3fSmrgpublic:
6101e04c3fSmrg   explicit lower_distance_visitor(gl_shader_stage shader_stage,
6201e04c3fSmrg                                   const char *in_name, int total_size,
6301e04c3fSmrg                                   int offset)
6401e04c3fSmrg      : progress(false), old_distance_out_var(NULL),
6501e04c3fSmrg        old_distance_in_var(NULL), new_distance_out_var(NULL),
6601e04c3fSmrg        new_distance_in_var(NULL), shader_stage(shader_stage),
6701e04c3fSmrg        in_name(in_name), total_size(total_size), offset(offset)
6801e04c3fSmrg   {
6901e04c3fSmrg   }
7001e04c3fSmrg
7101e04c3fSmrg   explicit lower_distance_visitor(gl_shader_stage shader_stage,
7201e04c3fSmrg                                   const char *in_name,
7301e04c3fSmrg                                   const lower_distance_visitor *orig,
7401e04c3fSmrg                                   int offset)
7501e04c3fSmrg      : progress(false),
7601e04c3fSmrg        old_distance_out_var(NULL),
7701e04c3fSmrg        old_distance_in_var(NULL),
7801e04c3fSmrg        new_distance_out_var(orig->new_distance_out_var),
7901e04c3fSmrg        new_distance_in_var(orig->new_distance_in_var),
8001e04c3fSmrg        shader_stage(shader_stage),
8101e04c3fSmrg        in_name(in_name),
8201e04c3fSmrg        total_size(orig->total_size),
8301e04c3fSmrg        offset(offset)
8401e04c3fSmrg   {
8501e04c3fSmrg   }
8601e04c3fSmrg
8701e04c3fSmrg   virtual ir_visitor_status visit(ir_variable *);
8801e04c3fSmrg   void create_indices(ir_rvalue*, ir_rvalue *&, ir_rvalue *&);
8901e04c3fSmrg   bool is_distance_vec8(ir_rvalue *ir);
9001e04c3fSmrg   ir_rvalue *lower_distance_vec8(ir_rvalue *ir);
9101e04c3fSmrg   virtual ir_visitor_status visit_leave(ir_assignment *);
9201e04c3fSmrg   void visit_new_assignment(ir_assignment *ir);
9301e04c3fSmrg   virtual ir_visitor_status visit_leave(ir_call *);
9401e04c3fSmrg
9501e04c3fSmrg   virtual void handle_rvalue(ir_rvalue **rvalue);
9601e04c3fSmrg
9701e04c3fSmrg   void fix_lhs(ir_assignment *);
9801e04c3fSmrg
9901e04c3fSmrg   bool progress;
10001e04c3fSmrg
10101e04c3fSmrg   /**
10201e04c3fSmrg    * Pointer to the declaration of gl_ClipDistance, if found.
10301e04c3fSmrg    *
10401e04c3fSmrg    * Note:
10501e04c3fSmrg    *
10601e04c3fSmrg    * - the in_var is for geometry and both tessellation shader inputs only.
10701e04c3fSmrg    *
10801e04c3fSmrg    * - since gl_ClipDistance is available in tessellation control,
10901e04c3fSmrg    *   tessellation evaluation and geometry shaders as both an input
11001e04c3fSmrg    *   and an output, it's possible for both old_distance_out_var
11101e04c3fSmrg    *   and old_distance_in_var to be non-null.
11201e04c3fSmrg    */
11301e04c3fSmrg   ir_variable *old_distance_out_var;
11401e04c3fSmrg   ir_variable *old_distance_in_var;
11501e04c3fSmrg
11601e04c3fSmrg   /**
11701e04c3fSmrg    * Pointer to the newly-created gl_ClipDistanceMESA variable.
11801e04c3fSmrg    */
11901e04c3fSmrg   ir_variable *new_distance_out_var;
12001e04c3fSmrg   ir_variable *new_distance_in_var;
12101e04c3fSmrg
12201e04c3fSmrg   /**
12301e04c3fSmrg    * Type of shader we are compiling (e.g. MESA_SHADER_VERTEX)
12401e04c3fSmrg    */
12501e04c3fSmrg   const gl_shader_stage shader_stage;
12601e04c3fSmrg   const char *in_name;
12701e04c3fSmrg   int total_size;
12801e04c3fSmrg   int offset;
12901e04c3fSmrg};
13001e04c3fSmrg
13101e04c3fSmrg} /* anonymous namespace */
13201e04c3fSmrg
13301e04c3fSmrg/**
13401e04c3fSmrg * Replace any declaration of 'in_name' as an array of floats with a
13501e04c3fSmrg * declaration of gl_ClipDistanceMESA as an array of vec4's.
13601e04c3fSmrg */
13701e04c3fSmrgir_visitor_status
13801e04c3fSmrglower_distance_visitor::visit(ir_variable *ir)
13901e04c3fSmrg{
14001e04c3fSmrg   ir_variable **old_var;
14101e04c3fSmrg   ir_variable **new_var;
14201e04c3fSmrg
14301e04c3fSmrg   if (!ir->name || strcmp(ir->name, in_name) != 0)
14401e04c3fSmrg      return visit_continue;
14501e04c3fSmrg   assert (ir->type->is_array());
14601e04c3fSmrg
14701e04c3fSmrg   if (ir->data.mode == ir_var_shader_out) {
14801e04c3fSmrg      if (this->old_distance_out_var)
14901e04c3fSmrg         return visit_continue;
15001e04c3fSmrg      old_var = &old_distance_out_var;
15101e04c3fSmrg      new_var = &new_distance_out_var;
15201e04c3fSmrg   } else if (ir->data.mode == ir_var_shader_in) {
15301e04c3fSmrg      if (this->old_distance_in_var)
15401e04c3fSmrg         return visit_continue;
15501e04c3fSmrg      old_var = &old_distance_in_var;
15601e04c3fSmrg      new_var = &new_distance_in_var;
15701e04c3fSmrg   } else {
15801e04c3fSmrg      unreachable("not reached");
15901e04c3fSmrg   }
16001e04c3fSmrg
16101e04c3fSmrg   this->progress = true;
16201e04c3fSmrg
16301e04c3fSmrg   *old_var = ir;
16401e04c3fSmrg
16501e04c3fSmrg   if (!(*new_var)) {
16601e04c3fSmrg      unsigned new_size = (total_size + 3) / 4;
16701e04c3fSmrg
16801e04c3fSmrg      /* Clone the old var so that we inherit all of its properties */
16901e04c3fSmrg      *new_var = ir->clone(ralloc_parent(ir), NULL);
17001e04c3fSmrg      (*new_var)->name = ralloc_strdup(*new_var, GLSL_CLIP_VAR_NAME);
17101e04c3fSmrg      (*new_var)->data.location = VARYING_SLOT_CLIP_DIST0;
17201e04c3fSmrg
17301e04c3fSmrg      if (!ir->type->fields.array->is_array()) {
17401e04c3fSmrg         /* gl_ClipDistance (used for vertex, tessellation evaluation and
17501e04c3fSmrg          * geometry output, and fragment input).
17601e04c3fSmrg          */
17701e04c3fSmrg         assert((ir->data.mode == ir_var_shader_in &&
17801e04c3fSmrg                 this->shader_stage == MESA_SHADER_FRAGMENT) ||
17901e04c3fSmrg                (ir->data.mode == ir_var_shader_out &&
18001e04c3fSmrg                 (this->shader_stage == MESA_SHADER_VERTEX ||
18101e04c3fSmrg                  this->shader_stage == MESA_SHADER_TESS_EVAL ||
18201e04c3fSmrg                  this->shader_stage == MESA_SHADER_GEOMETRY)));
18301e04c3fSmrg
18401e04c3fSmrg         assert (ir->type->fields.array == glsl_type::float_type);
18501e04c3fSmrg         (*new_var)->data.max_array_access = new_size - 1;
18601e04c3fSmrg
18701e04c3fSmrg         /* And change the properties that we need to change */
18801e04c3fSmrg         (*new_var)->type = glsl_type::get_array_instance(glsl_type::vec4_type,
18901e04c3fSmrg                                                          new_size);
19001e04c3fSmrg      } else {
19101e04c3fSmrg         /* 2D gl_ClipDistance (used for tessellation control, tessellation
19201e04c3fSmrg          * evaluation and geometry input, and tessellation control output).
19301e04c3fSmrg          */
19401e04c3fSmrg         assert((ir->data.mode == ir_var_shader_in &&
19501e04c3fSmrg                 (this->shader_stage == MESA_SHADER_GEOMETRY ||
19601e04c3fSmrg                  this->shader_stage == MESA_SHADER_TESS_EVAL)) ||
19701e04c3fSmrg                this->shader_stage == MESA_SHADER_TESS_CTRL);
19801e04c3fSmrg
19901e04c3fSmrg         assert (ir->type->fields.array->fields.array == glsl_type::float_type);
20001e04c3fSmrg
20101e04c3fSmrg         /* And change the properties that we need to change */
20201e04c3fSmrg         (*new_var)->type = glsl_type::get_array_instance(
20301e04c3fSmrg                            glsl_type::get_array_instance(glsl_type::vec4_type,
20401e04c3fSmrg                                                          new_size),
20501e04c3fSmrg                            ir->type->array_size());
20601e04c3fSmrg      }
20701e04c3fSmrg      ir->replace_with(*new_var);
20801e04c3fSmrg   } else {
20901e04c3fSmrg      ir->remove();
21001e04c3fSmrg   }
21101e04c3fSmrg
21201e04c3fSmrg   return visit_continue;
21301e04c3fSmrg}
21401e04c3fSmrg
21501e04c3fSmrg
21601e04c3fSmrg/**
21701e04c3fSmrg * Create the necessary GLSL rvalues to index into gl_ClipDistanceMESA based
21801e04c3fSmrg * on the rvalue previously used to index into gl_ClipDistance.
21901e04c3fSmrg *
22001e04c3fSmrg * \param array_index Selects one of the vec4's in gl_ClipDistanceMESA
22101e04c3fSmrg * \param swizzle_index Selects a component within the vec4 selected by
22201e04c3fSmrg *        array_index.
22301e04c3fSmrg */
22401e04c3fSmrgvoid
22501e04c3fSmrglower_distance_visitor::create_indices(ir_rvalue *old_index,
22601e04c3fSmrg                                            ir_rvalue *&array_index,
22701e04c3fSmrg                                            ir_rvalue *&swizzle_index)
22801e04c3fSmrg{
22901e04c3fSmrg   void *ctx = ralloc_parent(old_index);
23001e04c3fSmrg
23101e04c3fSmrg   /* Make sure old_index is a signed int so that the bitwise "shift" and
23201e04c3fSmrg    * "and" operations below type check properly.
23301e04c3fSmrg    */
23401e04c3fSmrg   if (old_index->type != glsl_type::int_type) {
23501e04c3fSmrg      assert (old_index->type == glsl_type::uint_type);
23601e04c3fSmrg      old_index = new(ctx) ir_expression(ir_unop_u2i, old_index);
23701e04c3fSmrg   }
23801e04c3fSmrg
23901e04c3fSmrg   ir_constant *old_index_constant =
24001e04c3fSmrg      old_index->constant_expression_value(ctx);
24101e04c3fSmrg   if (old_index_constant) {
24201e04c3fSmrg      /* gl_ClipDistance is being accessed via a constant index.  Don't bother
24301e04c3fSmrg       * creating expressions to calculate the lowered indices.  Just create
24401e04c3fSmrg       * constants.
24501e04c3fSmrg       */
24601e04c3fSmrg      int const_val = old_index_constant->get_int_component(0) + offset;
24701e04c3fSmrg      array_index = new(ctx) ir_constant(const_val / 4);
24801e04c3fSmrg      swizzle_index = new(ctx) ir_constant(const_val % 4);
24901e04c3fSmrg   } else {
25001e04c3fSmrg      /* Create a variable to hold the value of old_index (so that we
25101e04c3fSmrg       * don't compute it twice).
25201e04c3fSmrg       */
25301e04c3fSmrg      ir_variable *old_index_var = new(ctx) ir_variable(
25401e04c3fSmrg         glsl_type::int_type, "distance_index", ir_var_temporary);
25501e04c3fSmrg      this->base_ir->insert_before(old_index_var);
25601e04c3fSmrg      this->base_ir->insert_before(new(ctx) ir_assignment(
25701e04c3fSmrg         new(ctx) ir_dereference_variable(old_index_var), old_index));
25801e04c3fSmrg
25901e04c3fSmrg      /* Create the expression distance_index / 4.  Do this as a bit
26001e04c3fSmrg       * shift because that's likely to be more efficient.
26101e04c3fSmrg       */
26201e04c3fSmrg      array_index = new(ctx) ir_expression(
26301e04c3fSmrg         ir_binop_rshift,
26401e04c3fSmrg         new(ctx) ir_expression(ir_binop_add,
26501e04c3fSmrg                                new(ctx) ir_dereference_variable(old_index_var),
26601e04c3fSmrg                                new(ctx) ir_constant(offset)),
26701e04c3fSmrg         new(ctx) ir_constant(2));
26801e04c3fSmrg
26901e04c3fSmrg      /* Create the expression distance_index % 4.  Do this as a bitwise
27001e04c3fSmrg       * AND because that's likely to be more efficient.
27101e04c3fSmrg       */
27201e04c3fSmrg      swizzle_index = new(ctx) ir_expression(
27301e04c3fSmrg         ir_binop_bit_and,
27401e04c3fSmrg         new(ctx) ir_expression(ir_binop_add,
27501e04c3fSmrg                                new(ctx) ir_dereference_variable(old_index_var),
27601e04c3fSmrg                                new(ctx) ir_constant(offset)),
27701e04c3fSmrg         new(ctx) ir_constant(3));
27801e04c3fSmrg   }
27901e04c3fSmrg}
28001e04c3fSmrg
28101e04c3fSmrg
28201e04c3fSmrg/**
28301e04c3fSmrg * Determine whether the given rvalue describes an array of 8 floats that
28401e04c3fSmrg * needs to be lowered to an array of 2 vec4's; that is, determine whether it
28501e04c3fSmrg * matches one of the following patterns:
28601e04c3fSmrg *
28701e04c3fSmrg * - gl_ClipDistance (if gl_ClipDistance is 1D)
28801e04c3fSmrg * - gl_ClipDistance[i] (if gl_ClipDistance is 2D)
28901e04c3fSmrg */
29001e04c3fSmrgbool
29101e04c3fSmrglower_distance_visitor::is_distance_vec8(ir_rvalue *ir)
29201e04c3fSmrg{
29301e04c3fSmrg   /* Note that geometry shaders contain gl_ClipDistance both as an input
29401e04c3fSmrg    * (which is a 2D array) and an output (which is a 1D array), so it's
29501e04c3fSmrg    * possible for both this->old_distance_out_var and
29601e04c3fSmrg    * this->old_distance_in_var to be non-NULL in the same shader.
29701e04c3fSmrg    */
29801e04c3fSmrg
29901e04c3fSmrg   if (!ir->type->is_array())
30001e04c3fSmrg      return false;
30101e04c3fSmrg   if (ir->type->fields.array != glsl_type::float_type)
30201e04c3fSmrg      return false;
30301e04c3fSmrg
30401e04c3fSmrg   if (this->old_distance_out_var) {
30501e04c3fSmrg      if (ir->variable_referenced() == this->old_distance_out_var)
30601e04c3fSmrg         return true;
30701e04c3fSmrg   }
30801e04c3fSmrg   if (this->old_distance_in_var) {
30901e04c3fSmrg      assert(this->shader_stage == MESA_SHADER_TESS_CTRL ||
31001e04c3fSmrg             this->shader_stage == MESA_SHADER_TESS_EVAL ||
31101e04c3fSmrg             this->shader_stage == MESA_SHADER_GEOMETRY ||
31201e04c3fSmrg             this->shader_stage == MESA_SHADER_FRAGMENT);
31301e04c3fSmrg
31401e04c3fSmrg      if (ir->variable_referenced() == this->old_distance_in_var)
31501e04c3fSmrg         return true;
31601e04c3fSmrg   }
31701e04c3fSmrg   return false;
31801e04c3fSmrg}
31901e04c3fSmrg
32001e04c3fSmrg
32101e04c3fSmrg/**
32201e04c3fSmrg * If the given ir satisfies is_distance_vec8(), return new ir
32301e04c3fSmrg * representing its lowered equivalent.  That is, map:
32401e04c3fSmrg *
32501e04c3fSmrg * - gl_ClipDistance    => gl_ClipDistanceMESA    (if gl_ClipDistance is 1D)
32601e04c3fSmrg * - gl_ClipDistance[i] => gl_ClipDistanceMESA[i] (if gl_ClipDistance is 2D)
32701e04c3fSmrg *
32801e04c3fSmrg * Otherwise return NULL.
32901e04c3fSmrg */
33001e04c3fSmrgir_rvalue *
33101e04c3fSmrglower_distance_visitor::lower_distance_vec8(ir_rvalue *ir)
33201e04c3fSmrg{
33301e04c3fSmrg   if (!ir->type->is_array())
33401e04c3fSmrg      return NULL;
33501e04c3fSmrg   if (ir->type->fields.array != glsl_type::float_type)
33601e04c3fSmrg      return NULL;
33701e04c3fSmrg
33801e04c3fSmrg   ir_variable **new_var = NULL;
33901e04c3fSmrg   if (this->old_distance_out_var) {
34001e04c3fSmrg      if (ir->variable_referenced() == this->old_distance_out_var)
34101e04c3fSmrg         new_var = &this->new_distance_out_var;
34201e04c3fSmrg   }
34301e04c3fSmrg   if (this->old_distance_in_var) {
34401e04c3fSmrg      if (ir->variable_referenced() == this->old_distance_in_var)
34501e04c3fSmrg         new_var = &this->new_distance_in_var;
34601e04c3fSmrg   }
34701e04c3fSmrg   if (new_var == NULL)
34801e04c3fSmrg      return NULL;
34901e04c3fSmrg
35001e04c3fSmrg   if (ir->as_dereference_variable()) {
35101e04c3fSmrg      return new(ralloc_parent(ir)) ir_dereference_variable(*new_var);
35201e04c3fSmrg   } else {
35301e04c3fSmrg      ir_dereference_array *array_ref = ir->as_dereference_array();
35401e04c3fSmrg      assert(array_ref);
35501e04c3fSmrg      assert(array_ref->array->as_dereference_variable());
35601e04c3fSmrg
35701e04c3fSmrg      return new(ralloc_parent(ir))
35801e04c3fSmrg         ir_dereference_array(*new_var, array_ref->array_index);
35901e04c3fSmrg   }
36001e04c3fSmrg}
36101e04c3fSmrg
36201e04c3fSmrg
36301e04c3fSmrgvoid
36401e04c3fSmrglower_distance_visitor::handle_rvalue(ir_rvalue **rv)
36501e04c3fSmrg{
36601e04c3fSmrg   if (*rv == NULL)
36701e04c3fSmrg      return;
36801e04c3fSmrg
36901e04c3fSmrg   ir_dereference_array *const array_deref = (*rv)->as_dereference_array();
37001e04c3fSmrg   if (array_deref == NULL)
37101e04c3fSmrg      return;
37201e04c3fSmrg
37301e04c3fSmrg   /* Replace any expression that indexes one of the floats in gl_ClipDistance
37401e04c3fSmrg    * with an expression that indexes into one of the vec4's in
37501e04c3fSmrg    * gl_ClipDistanceMESA and accesses the appropriate component.
37601e04c3fSmrg    */
37701e04c3fSmrg   ir_rvalue *lowered_vec8 =
37801e04c3fSmrg      this->lower_distance_vec8(array_deref->array);
37901e04c3fSmrg   if (lowered_vec8 != NULL) {
38001e04c3fSmrg      this->progress = true;
38101e04c3fSmrg      ir_rvalue *array_index;
38201e04c3fSmrg      ir_rvalue *swizzle_index;
38301e04c3fSmrg      this->create_indices(array_deref->array_index, array_index, swizzle_index);
38401e04c3fSmrg      void *mem_ctx = ralloc_parent(array_deref);
38501e04c3fSmrg
38601e04c3fSmrg      ir_dereference_array *const new_array_deref =
38701e04c3fSmrg         new(mem_ctx) ir_dereference_array(lowered_vec8, array_index);
38801e04c3fSmrg
38901e04c3fSmrg      ir_expression *const expr =
39001e04c3fSmrg         new(mem_ctx) ir_expression(ir_binop_vector_extract,
39101e04c3fSmrg                                    new_array_deref,
39201e04c3fSmrg                                    swizzle_index);
39301e04c3fSmrg
39401e04c3fSmrg      *rv = expr;
39501e04c3fSmrg   }
39601e04c3fSmrg}
39701e04c3fSmrg
39801e04c3fSmrgvoid
39901e04c3fSmrglower_distance_visitor::fix_lhs(ir_assignment *ir)
40001e04c3fSmrg{
40101e04c3fSmrg   if (ir->lhs->ir_type == ir_type_expression) {
40201e04c3fSmrg      void *mem_ctx = ralloc_parent(ir);
40301e04c3fSmrg      ir_expression *const expr = (ir_expression *) ir->lhs;
40401e04c3fSmrg
40501e04c3fSmrg      /* The expression must be of the form:
40601e04c3fSmrg       *
40701e04c3fSmrg       *     (vector_extract gl_ClipDistanceMESA[i], j).
40801e04c3fSmrg       */
40901e04c3fSmrg      assert(expr->operation == ir_binop_vector_extract);
41001e04c3fSmrg      assert(expr->operands[0]->ir_type == ir_type_dereference_array);
41101e04c3fSmrg      assert(expr->operands[0]->type == glsl_type::vec4_type);
41201e04c3fSmrg
41301e04c3fSmrg      ir_dereference *const new_lhs = (ir_dereference *) expr->operands[0];
41401e04c3fSmrg      ir->rhs = new(mem_ctx) ir_expression(ir_triop_vector_insert,
41501e04c3fSmrg                                           glsl_type::vec4_type,
41601e04c3fSmrg                                           new_lhs->clone(mem_ctx, NULL),
41701e04c3fSmrg                                           ir->rhs,
41801e04c3fSmrg                                           expr->operands[1]);
41901e04c3fSmrg      ir->set_lhs(new_lhs);
42001e04c3fSmrg      ir->write_mask = WRITEMASK_XYZW;
42101e04c3fSmrg   }
42201e04c3fSmrg}
42301e04c3fSmrg
42401e04c3fSmrg/**
42501e04c3fSmrg * Replace any assignment having the 1D gl_ClipDistance (undereferenced) as
42601e04c3fSmrg * its LHS or RHS with a sequence of assignments, one for each component of
42701e04c3fSmrg * the array.  Each of these assignments is lowered to refer to
42801e04c3fSmrg * gl_ClipDistanceMESA as appropriate.
42901e04c3fSmrg *
43001e04c3fSmrg * We need to do a similar replacement for 2D gl_ClipDistance, however since
43101e04c3fSmrg * it's an input, the only case we need to address is where a 1D slice of it
43201e04c3fSmrg * is the entire RHS of an assignment, e.g.:
43301e04c3fSmrg *
43401e04c3fSmrg *     foo = gl_in[i].gl_ClipDistance
43501e04c3fSmrg */
43601e04c3fSmrgir_visitor_status
43701e04c3fSmrglower_distance_visitor::visit_leave(ir_assignment *ir)
43801e04c3fSmrg{
43901e04c3fSmrg   /* First invoke the base class visitor.  This causes handle_rvalue() to be
44001e04c3fSmrg    * called on ir->rhs and ir->condition.
44101e04c3fSmrg    */
44201e04c3fSmrg   ir_rvalue_visitor::visit_leave(ir);
44301e04c3fSmrg
44401e04c3fSmrg   if (this->is_distance_vec8(ir->lhs) ||
44501e04c3fSmrg       this->is_distance_vec8(ir->rhs)) {
44601e04c3fSmrg      /* LHS or RHS of the assignment is the entire 1D gl_ClipDistance array
44701e04c3fSmrg       * (or a 1D slice of a 2D gl_ClipDistance input array).  Since we are
44801e04c3fSmrg       * reshaping gl_ClipDistance from an array of floats to an array of
44901e04c3fSmrg       * vec4's, this isn't going to work as a bulk assignment anymore, so
45001e04c3fSmrg       * unroll it to element-by-element assignments and lower each of them.
45101e04c3fSmrg       *
45201e04c3fSmrg       * Note: to unroll into element-by-element assignments, we need to make
45301e04c3fSmrg       * clones of the LHS and RHS.  This is safe because expressions and
45401e04c3fSmrg       * l-values are side-effect free.
45501e04c3fSmrg       */
45601e04c3fSmrg      void *ctx = ralloc_parent(ir);
45701e04c3fSmrg      int array_size = ir->lhs->type->array_size();
45801e04c3fSmrg      for (int i = 0; i < array_size; ++i) {
45901e04c3fSmrg         ir_dereference_array *new_lhs = new(ctx) ir_dereference_array(
46001e04c3fSmrg            ir->lhs->clone(ctx, NULL), new(ctx) ir_constant(i));
46101e04c3fSmrg         ir_dereference_array *new_rhs = new(ctx) ir_dereference_array(
46201e04c3fSmrg            ir->rhs->clone(ctx, NULL), new(ctx) ir_constant(i));
46301e04c3fSmrg         this->handle_rvalue((ir_rvalue **) &new_rhs);
46401e04c3fSmrg
46501e04c3fSmrg         /* Handle the LHS after creating the new assignment.  This must
46601e04c3fSmrg          * happen in this order because handle_rvalue may replace the old LHS
46701e04c3fSmrg          * with an ir_expression of ir_binop_vector_extract.  Since this is
46801e04c3fSmrg          * not a valide l-value, this will cause an assertion in the
46901e04c3fSmrg          * ir_assignment constructor to fail.
47001e04c3fSmrg          *
47101e04c3fSmrg          * If this occurs, replace the mangled LHS with a dereference of the
47201e04c3fSmrg          * vector, and replace the RHS with an ir_triop_vector_insert.
47301e04c3fSmrg          */
47401e04c3fSmrg         ir_assignment *const assign = new(ctx) ir_assignment(new_lhs, new_rhs);
47501e04c3fSmrg         this->handle_rvalue((ir_rvalue **) &assign->lhs);
47601e04c3fSmrg         this->fix_lhs(assign);
47701e04c3fSmrg
47801e04c3fSmrg         this->base_ir->insert_before(assign);
47901e04c3fSmrg      }
48001e04c3fSmrg      ir->remove();
48101e04c3fSmrg
48201e04c3fSmrg      return visit_continue;
48301e04c3fSmrg   }
48401e04c3fSmrg
48501e04c3fSmrg   /* Handle the LHS as if it were an r-value.  Normally
48601e04c3fSmrg    * rvalue_visit(ir_assignment *) only visits the RHS, but we need to lower
48701e04c3fSmrg    * expressions in the LHS as well.
48801e04c3fSmrg    *
48901e04c3fSmrg    * This may cause the LHS to get replaced with an ir_expression of
49001e04c3fSmrg    * ir_binop_vector_extract.  If this occurs, replace it with a dereference
49101e04c3fSmrg    * of the vector, and replace the RHS with an ir_triop_vector_insert.
49201e04c3fSmrg    */
49301e04c3fSmrg   handle_rvalue((ir_rvalue **)&ir->lhs);
49401e04c3fSmrg   this->fix_lhs(ir);
49501e04c3fSmrg
49601e04c3fSmrg   return rvalue_visit(ir);
49701e04c3fSmrg}
49801e04c3fSmrg
49901e04c3fSmrg
50001e04c3fSmrg/**
50101e04c3fSmrg * Set up base_ir properly and call visit_leave() on a newly created
50201e04c3fSmrg * ir_assignment node.  This is used in cases where we have to insert an
50301e04c3fSmrg * ir_assignment in a place where we know the hierarchical visitor won't see
50401e04c3fSmrg * it.
50501e04c3fSmrg */
50601e04c3fSmrgvoid
50701e04c3fSmrglower_distance_visitor::visit_new_assignment(ir_assignment *ir)
50801e04c3fSmrg{
50901e04c3fSmrg   ir_instruction *old_base_ir = this->base_ir;
51001e04c3fSmrg   this->base_ir = ir;
51101e04c3fSmrg   ir->accept(this);
51201e04c3fSmrg   this->base_ir = old_base_ir;
51301e04c3fSmrg}
51401e04c3fSmrg
51501e04c3fSmrg
51601e04c3fSmrg/**
51701e04c3fSmrg * If a 1D gl_ClipDistance variable appears as an argument in an ir_call
51801e04c3fSmrg * expression, replace it with a temporary variable, and make sure the ir_call
51901e04c3fSmrg * is preceded and/or followed by assignments that copy the contents of the
52001e04c3fSmrg * temporary variable to and/or from gl_ClipDistance.  Each of these
52101e04c3fSmrg * assignments is then lowered to refer to gl_ClipDistanceMESA.
52201e04c3fSmrg *
52301e04c3fSmrg * We need to do a similar replacement for 2D gl_ClipDistance, however since
52401e04c3fSmrg * it's an input, the only case we need to address is where a 1D slice of it
52501e04c3fSmrg * is passed as an "in" parameter to an ir_call, e.g.:
52601e04c3fSmrg *
52701e04c3fSmrg *     foo(gl_in[i].gl_ClipDistance)
52801e04c3fSmrg */
52901e04c3fSmrgir_visitor_status
53001e04c3fSmrglower_distance_visitor::visit_leave(ir_call *ir)
53101e04c3fSmrg{
53201e04c3fSmrg   void *ctx = ralloc_parent(ir);
53301e04c3fSmrg
53401e04c3fSmrg   const exec_node *formal_param_node = ir->callee->parameters.get_head_raw();
53501e04c3fSmrg   const exec_node *actual_param_node = ir->actual_parameters.get_head_raw();
53601e04c3fSmrg   while (!actual_param_node->is_tail_sentinel()) {
53701e04c3fSmrg      ir_variable *formal_param = (ir_variable *) formal_param_node;
53801e04c3fSmrg      ir_rvalue *actual_param = (ir_rvalue *) actual_param_node;
53901e04c3fSmrg
54001e04c3fSmrg      /* Advance formal_param_node and actual_param_node now so that we can
54101e04c3fSmrg       * safely replace actual_param with another node, if necessary, below.
54201e04c3fSmrg       */
54301e04c3fSmrg      formal_param_node = formal_param_node->next;
54401e04c3fSmrg      actual_param_node = actual_param_node->next;
54501e04c3fSmrg
54601e04c3fSmrg      if (this->is_distance_vec8(actual_param)) {
54701e04c3fSmrg         /* User is trying to pass the whole 1D gl_ClipDistance array (or a 1D
54801e04c3fSmrg          * slice of a 2D gl_ClipDistance array) to a function call.  Since we
54901e04c3fSmrg          * are reshaping gl_ClipDistance from an array of floats to an array
55001e04c3fSmrg          * of vec4's, this isn't going to work anymore, so use a temporary
55101e04c3fSmrg          * array instead.
55201e04c3fSmrg          */
55301e04c3fSmrg         ir_variable *temp_clip_distance = new(ctx) ir_variable(
55401e04c3fSmrg            actual_param->type, "temp_clip_distance", ir_var_temporary);
55501e04c3fSmrg         this->base_ir->insert_before(temp_clip_distance);
55601e04c3fSmrg         actual_param->replace_with(
55701e04c3fSmrg            new(ctx) ir_dereference_variable(temp_clip_distance));
55801e04c3fSmrg         if (formal_param->data.mode == ir_var_function_in
55901e04c3fSmrg             || formal_param->data.mode == ir_var_function_inout) {
56001e04c3fSmrg            /* Copy from gl_ClipDistance to the temporary before the call.
56101e04c3fSmrg             * Since we are going to insert this copy before the current
56201e04c3fSmrg             * instruction, we need to visit it afterwards to make sure it
56301e04c3fSmrg             * gets lowered.
56401e04c3fSmrg             */
56501e04c3fSmrg            ir_assignment *new_assignment = new(ctx) ir_assignment(
56601e04c3fSmrg               new(ctx) ir_dereference_variable(temp_clip_distance),
56701e04c3fSmrg               actual_param->clone(ctx, NULL));
56801e04c3fSmrg            this->base_ir->insert_before(new_assignment);
56901e04c3fSmrg            this->visit_new_assignment(new_assignment);
57001e04c3fSmrg         }
57101e04c3fSmrg         if (formal_param->data.mode == ir_var_function_out
57201e04c3fSmrg             || formal_param->data.mode == ir_var_function_inout) {
57301e04c3fSmrg            /* Copy from the temporary to gl_ClipDistance after the call.
57401e04c3fSmrg             * Since visit_list_elements() has already decided which
57501e04c3fSmrg             * instruction it's going to visit next, we need to visit
57601e04c3fSmrg             * afterwards to make sure it gets lowered.
57701e04c3fSmrg             */
57801e04c3fSmrg            ir_assignment *new_assignment = new(ctx) ir_assignment(
57901e04c3fSmrg               actual_param->clone(ctx, NULL),
58001e04c3fSmrg               new(ctx) ir_dereference_variable(temp_clip_distance));
58101e04c3fSmrg            this->base_ir->insert_after(new_assignment);
58201e04c3fSmrg            this->visit_new_assignment(new_assignment);
58301e04c3fSmrg         }
58401e04c3fSmrg      }
58501e04c3fSmrg   }
58601e04c3fSmrg
58701e04c3fSmrg   return rvalue_visit(ir);
58801e04c3fSmrg}
58901e04c3fSmrg
59001e04c3fSmrgnamespace {
59101e04c3fSmrgclass lower_distance_visitor_counter : public ir_rvalue_visitor {
59201e04c3fSmrgpublic:
59301e04c3fSmrg   explicit lower_distance_visitor_counter(void)
59401e04c3fSmrg      : in_clip_size(0), in_cull_size(0),
59501e04c3fSmrg        out_clip_size(0), out_cull_size(0)
59601e04c3fSmrg   {
59701e04c3fSmrg   }
59801e04c3fSmrg
59901e04c3fSmrg   virtual ir_visitor_status visit(ir_variable *);
60001e04c3fSmrg   virtual void handle_rvalue(ir_rvalue **rvalue);
60101e04c3fSmrg
60201e04c3fSmrg   int in_clip_size;
60301e04c3fSmrg   int in_cull_size;
60401e04c3fSmrg   int out_clip_size;
60501e04c3fSmrg   int out_cull_size;
60601e04c3fSmrg};
60701e04c3fSmrg
60801e04c3fSmrg}
60901e04c3fSmrg/**
61001e04c3fSmrg * Count gl_ClipDistance and gl_CullDistance sizes.
61101e04c3fSmrg */
61201e04c3fSmrgir_visitor_status
61301e04c3fSmrglower_distance_visitor_counter::visit(ir_variable *ir)
61401e04c3fSmrg{
61501e04c3fSmrg   int *clip_size, *cull_size;
61601e04c3fSmrg
61701e04c3fSmrg   if (!ir->name)
61801e04c3fSmrg      return visit_continue;
61901e04c3fSmrg
62001e04c3fSmrg   if (ir->data.mode == ir_var_shader_out) {
62101e04c3fSmrg      clip_size = &out_clip_size;
62201e04c3fSmrg      cull_size = &out_cull_size;
62301e04c3fSmrg   } else if (ir->data.mode == ir_var_shader_in) {
62401e04c3fSmrg      clip_size = &in_clip_size;
62501e04c3fSmrg      cull_size = &in_cull_size;
62601e04c3fSmrg   } else
62701e04c3fSmrg      return visit_continue;
62801e04c3fSmrg
62901e04c3fSmrg   if (ir->type->is_unsized_array())
63001e04c3fSmrg      return visit_continue;
63101e04c3fSmrg
63201e04c3fSmrg   if (*clip_size == 0) {
63301e04c3fSmrg      if (!strcmp(ir->name, "gl_ClipDistance")) {
63401e04c3fSmrg         if (!ir->type->fields.array->is_array())
63501e04c3fSmrg            *clip_size = ir->type->array_size();
63601e04c3fSmrg         else
63701e04c3fSmrg            *clip_size = ir->type->fields.array->array_size();
63801e04c3fSmrg      }
63901e04c3fSmrg   }
64001e04c3fSmrg
64101e04c3fSmrg   if (*cull_size == 0) {
64201e04c3fSmrg      if (!strcmp(ir->name, "gl_CullDistance")) {
64301e04c3fSmrg         if (!ir->type->fields.array->is_array())
64401e04c3fSmrg            *cull_size = ir->type->array_size();
64501e04c3fSmrg         else
64601e04c3fSmrg            *cull_size = ir->type->fields.array->array_size();
64701e04c3fSmrg      }
64801e04c3fSmrg   }
64901e04c3fSmrg   return visit_continue;
65001e04c3fSmrg}
65101e04c3fSmrg
65201e04c3fSmrgvoid
65301e04c3fSmrglower_distance_visitor_counter::handle_rvalue(ir_rvalue **)
65401e04c3fSmrg{
65501e04c3fSmrg   return;
65601e04c3fSmrg}
65701e04c3fSmrg
65801e04c3fSmrgbool
65901e04c3fSmrglower_clip_cull_distance(struct gl_shader_program *prog,
66001e04c3fSmrg                         struct gl_linked_shader *shader)
66101e04c3fSmrg{
66201e04c3fSmrg   int clip_size, cull_size;
66301e04c3fSmrg
66401e04c3fSmrg   lower_distance_visitor_counter count;
66501e04c3fSmrg   visit_list_elements(&count, shader->ir);
66601e04c3fSmrg
66701e04c3fSmrg   clip_size = MAX2(count.in_clip_size, count.out_clip_size);
66801e04c3fSmrg   cull_size = MAX2(count.in_cull_size, count.out_cull_size);
66901e04c3fSmrg
67001e04c3fSmrg   if (clip_size == 0 && cull_size == 0)
67101e04c3fSmrg      return false;
67201e04c3fSmrg
67301e04c3fSmrg   lower_distance_visitor v(shader->Stage, "gl_ClipDistance", clip_size + cull_size, 0);
67401e04c3fSmrg   visit_list_elements(&v, shader->ir);
67501e04c3fSmrg
67601e04c3fSmrg   lower_distance_visitor v2(shader->Stage, "gl_CullDistance", &v, clip_size);
67701e04c3fSmrg   visit_list_elements(&v2, shader->ir);
67801e04c3fSmrg
67901e04c3fSmrg   if (v2.new_distance_out_var)
68001e04c3fSmrg      shader->symbols->add_variable(v2.new_distance_out_var);
68101e04c3fSmrg   if (v2.new_distance_in_var)
68201e04c3fSmrg      shader->symbols->add_variable(v2.new_distance_in_var);
68301e04c3fSmrg
68401e04c3fSmrg   return v2.progress;
68501e04c3fSmrg}
686