101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2014 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
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_tess_level.cpp
2601e04c3fSmrg *
2701e04c3fSmrg * This pass accounts for the difference between the way gl_TessLevelOuter
2801e04c3fSmrg * and gl_TessLevelInner is declared in standard GLSL (as an array of
2901e04c3fSmrg * floats), and the way it is frequently implemented in hardware (as a vec4
3001e04c3fSmrg * and vec2).
3101e04c3fSmrg *
3201e04c3fSmrg * The declaration of gl_TessLevel* is replaced with a declaration
3301e04c3fSmrg * of gl_TessLevel*MESA, and any references to gl_TessLevel* are
3401e04c3fSmrg * translated to refer to gl_TessLevel*MESA with the appropriate
3501e04c3fSmrg * swizzling of array indices.  For instance:
3601e04c3fSmrg *
3701e04c3fSmrg *   gl_TessLevelOuter[i]
3801e04c3fSmrg *
3901e04c3fSmrg * is translated into:
4001e04c3fSmrg *
4101e04c3fSmrg *   gl_TessLevelOuterMESA[i]
4201e04c3fSmrg *
4301e04c3fSmrg * Since some hardware may not internally represent gl_TessLevel* as a pair
4401e04c3fSmrg * of vec4's, this lowering pass is optional.  To enable it, set the
4501e04c3fSmrg * LowerTessLevel flag in gl_shader_compiler_options to true.
4601e04c3fSmrg */
4701e04c3fSmrg
4801e04c3fSmrg#include "glsl_symbol_table.h"
4901e04c3fSmrg#include "ir_rvalue_visitor.h"
5001e04c3fSmrg#include "ir.h"
5101e04c3fSmrg#include "program/prog_instruction.h" /* For WRITEMASK_* */
5201e04c3fSmrg#include "main/mtypes.h"
5301e04c3fSmrg
5401e04c3fSmrgnamespace {
5501e04c3fSmrg
5601e04c3fSmrgclass lower_tess_level_visitor : public ir_rvalue_visitor {
5701e04c3fSmrgpublic:
5801e04c3fSmrg   explicit lower_tess_level_visitor(gl_shader_stage shader_stage)
5901e04c3fSmrg      : progress(false), old_tess_level_outer_var(NULL),
6001e04c3fSmrg        old_tess_level_inner_var(NULL), new_tess_level_outer_var(NULL),
6101e04c3fSmrg        new_tess_level_inner_var(NULL), shader_stage(shader_stage)
6201e04c3fSmrg   {
6301e04c3fSmrg   }
6401e04c3fSmrg
6501e04c3fSmrg   virtual ir_visitor_status visit(ir_variable *);
6601e04c3fSmrg   bool is_tess_level_array(ir_rvalue *ir);
6701e04c3fSmrg   ir_rvalue *lower_tess_level_array(ir_rvalue *ir);
6801e04c3fSmrg   virtual ir_visitor_status visit_leave(ir_assignment *);
6901e04c3fSmrg   void visit_new_assignment(ir_assignment *ir);
7001e04c3fSmrg   virtual ir_visitor_status visit_leave(ir_call *);
7101e04c3fSmrg
7201e04c3fSmrg   virtual void handle_rvalue(ir_rvalue **rvalue);
7301e04c3fSmrg
7401e04c3fSmrg   void fix_lhs(ir_assignment *);
7501e04c3fSmrg
7601e04c3fSmrg   bool progress;
7701e04c3fSmrg
7801e04c3fSmrg   /**
7901e04c3fSmrg    * Pointer to the declaration of gl_TessLevel*, if found.
8001e04c3fSmrg    */
8101e04c3fSmrg   ir_variable *old_tess_level_outer_var;
8201e04c3fSmrg   ir_variable *old_tess_level_inner_var;
8301e04c3fSmrg
8401e04c3fSmrg   /**
8501e04c3fSmrg    * Pointer to the newly-created gl_TessLevel*MESA variables.
8601e04c3fSmrg    */
8701e04c3fSmrg   ir_variable *new_tess_level_outer_var;
8801e04c3fSmrg   ir_variable *new_tess_level_inner_var;
8901e04c3fSmrg
9001e04c3fSmrg   /**
9101e04c3fSmrg    * Type of shader we are compiling (e.g. MESA_SHADER_TESS_CTRL)
9201e04c3fSmrg    */
9301e04c3fSmrg   const gl_shader_stage shader_stage;
9401e04c3fSmrg};
9501e04c3fSmrg
9601e04c3fSmrg} /* anonymous namespace */
9701e04c3fSmrg
9801e04c3fSmrg/**
9901e04c3fSmrg * Replace any declaration of gl_TessLevel* as an array of floats with a
10001e04c3fSmrg * declaration of gl_TessLevel*MESA as a vec4.
10101e04c3fSmrg */
10201e04c3fSmrgir_visitor_status
10301e04c3fSmrglower_tess_level_visitor::visit(ir_variable *ir)
10401e04c3fSmrg{
10501e04c3fSmrg   if ((!ir->name) ||
10601e04c3fSmrg       ((strcmp(ir->name, "gl_TessLevelInner") != 0) &&
10701e04c3fSmrg        (strcmp(ir->name, "gl_TessLevelOuter") != 0)))
10801e04c3fSmrg      return visit_continue;
10901e04c3fSmrg
11001e04c3fSmrg   assert (ir->type->is_array());
11101e04c3fSmrg
11201e04c3fSmrg   if (strcmp(ir->name, "gl_TessLevelOuter") == 0) {
11301e04c3fSmrg      if (this->old_tess_level_outer_var)
11401e04c3fSmrg         return visit_continue;
11501e04c3fSmrg
11601e04c3fSmrg      old_tess_level_outer_var = ir;
11701e04c3fSmrg      assert(ir->type->fields.array == glsl_type::float_type);
11801e04c3fSmrg
11901e04c3fSmrg      /* Clone the old var so that we inherit all of its properties */
12001e04c3fSmrg      new_tess_level_outer_var = ir->clone(ralloc_parent(ir), NULL);
12101e04c3fSmrg
12201e04c3fSmrg      /* And change the properties that we need to change */
12301e04c3fSmrg      new_tess_level_outer_var->name = ralloc_strdup(new_tess_level_outer_var,
12401e04c3fSmrg                                                "gl_TessLevelOuterMESA");
12501e04c3fSmrg      new_tess_level_outer_var->type = glsl_type::vec4_type;
12601e04c3fSmrg      new_tess_level_outer_var->data.max_array_access = 0;
12701e04c3fSmrg
12801e04c3fSmrg      ir->replace_with(new_tess_level_outer_var);
12901e04c3fSmrg   } else if (strcmp(ir->name, "gl_TessLevelInner") == 0) {
13001e04c3fSmrg      if (this->old_tess_level_inner_var)
13101e04c3fSmrg         return visit_continue;
13201e04c3fSmrg
13301e04c3fSmrg      old_tess_level_inner_var = ir;
13401e04c3fSmrg      assert(ir->type->fields.array == glsl_type::float_type);
13501e04c3fSmrg
13601e04c3fSmrg      /* Clone the old var so that we inherit all of its properties */
13701e04c3fSmrg      new_tess_level_inner_var = ir->clone(ralloc_parent(ir), NULL);
13801e04c3fSmrg
13901e04c3fSmrg      /* And change the properties that we need to change */
14001e04c3fSmrg      new_tess_level_inner_var->name = ralloc_strdup(new_tess_level_inner_var,
14101e04c3fSmrg                                                "gl_TessLevelInnerMESA");
14201e04c3fSmrg      new_tess_level_inner_var->type = glsl_type::vec2_type;
14301e04c3fSmrg      new_tess_level_inner_var->data.max_array_access = 0;
14401e04c3fSmrg
14501e04c3fSmrg      ir->replace_with(new_tess_level_inner_var);
14601e04c3fSmrg   } else {
14701e04c3fSmrg      assert(0);
14801e04c3fSmrg   }
14901e04c3fSmrg
15001e04c3fSmrg   this->progress = true;
15101e04c3fSmrg
15201e04c3fSmrg   return visit_continue;
15301e04c3fSmrg}
15401e04c3fSmrg
15501e04c3fSmrg
15601e04c3fSmrg/**
15701e04c3fSmrg * Determine whether the given rvalue describes an array of floats that
15801e04c3fSmrg * needs to be lowered to a vec4; that is, determine whether it
15901e04c3fSmrg * matches one of the following patterns:
16001e04c3fSmrg *
16101e04c3fSmrg * - gl_TessLevelOuter
16201e04c3fSmrg * - gl_TessLevelInner
16301e04c3fSmrg */
16401e04c3fSmrgbool
16501e04c3fSmrglower_tess_level_visitor::is_tess_level_array(ir_rvalue *ir)
16601e04c3fSmrg{
16701e04c3fSmrg   if (!ir->type->is_array())
16801e04c3fSmrg      return false;
16901e04c3fSmrg   if (ir->type->fields.array != glsl_type::float_type)
17001e04c3fSmrg      return false;
17101e04c3fSmrg
17201e04c3fSmrg   if (this->old_tess_level_outer_var) {
17301e04c3fSmrg      if (ir->variable_referenced() == this->old_tess_level_outer_var)
17401e04c3fSmrg         return true;
17501e04c3fSmrg   }
17601e04c3fSmrg   if (this->old_tess_level_inner_var) {
17701e04c3fSmrg      if (ir->variable_referenced() == this->old_tess_level_inner_var)
17801e04c3fSmrg         return true;
17901e04c3fSmrg   }
18001e04c3fSmrg   return false;
18101e04c3fSmrg}
18201e04c3fSmrg
18301e04c3fSmrg
18401e04c3fSmrg/**
18501e04c3fSmrg * If the given ir satisfies is_tess_level_array(), return new ir
18601e04c3fSmrg * representing its lowered equivalent.  That is, map:
18701e04c3fSmrg *
18801e04c3fSmrg * - gl_TessLevelOuter => gl_TessLevelOuterMESA
18901e04c3fSmrg * - gl_TessLevelInner => gl_TessLevelInnerMESA
19001e04c3fSmrg *
19101e04c3fSmrg * Otherwise return NULL.
19201e04c3fSmrg */
19301e04c3fSmrgir_rvalue *
19401e04c3fSmrglower_tess_level_visitor::lower_tess_level_array(ir_rvalue *ir)
19501e04c3fSmrg{
19601e04c3fSmrg   if (!ir->type->is_array())
19701e04c3fSmrg      return NULL;
19801e04c3fSmrg   if (ir->type->fields.array != glsl_type::float_type)
19901e04c3fSmrg      return NULL;
20001e04c3fSmrg
20101e04c3fSmrg   ir_variable **new_var = NULL;
20201e04c3fSmrg
20301e04c3fSmrg   if (this->old_tess_level_outer_var) {
20401e04c3fSmrg      if (ir->variable_referenced() == this->old_tess_level_outer_var)
20501e04c3fSmrg         new_var = &this->new_tess_level_outer_var;
20601e04c3fSmrg   }
20701e04c3fSmrg   if (this->old_tess_level_inner_var) {
20801e04c3fSmrg      if (ir->variable_referenced() == this->old_tess_level_inner_var)
20901e04c3fSmrg         new_var = &this->new_tess_level_inner_var;
21001e04c3fSmrg   }
21101e04c3fSmrg
21201e04c3fSmrg   if (new_var == NULL)
21301e04c3fSmrg      return NULL;
21401e04c3fSmrg
21501e04c3fSmrg   assert(ir->as_dereference_variable());
21601e04c3fSmrg   return new(ralloc_parent(ir)) ir_dereference_variable(*new_var);
21701e04c3fSmrg}
21801e04c3fSmrg
21901e04c3fSmrg
22001e04c3fSmrgvoid
22101e04c3fSmrglower_tess_level_visitor::handle_rvalue(ir_rvalue **rv)
22201e04c3fSmrg{
22301e04c3fSmrg   if (*rv == NULL)
22401e04c3fSmrg      return;
22501e04c3fSmrg
22601e04c3fSmrg   ir_dereference_array *const array_deref = (*rv)->as_dereference_array();
22701e04c3fSmrg   if (array_deref == NULL)
22801e04c3fSmrg      return;
22901e04c3fSmrg
23001e04c3fSmrg   /* Replace any expression that indexes one of the floats in gl_TessLevel*
23101e04c3fSmrg    * with an expression that indexes into one of the vec4's
23201e04c3fSmrg    * gl_TessLevel*MESA and accesses the appropriate component.
23301e04c3fSmrg    */
23401e04c3fSmrg   ir_rvalue *lowered_vec4 =
23501e04c3fSmrg      this->lower_tess_level_array(array_deref->array);
23601e04c3fSmrg   if (lowered_vec4 != NULL) {
23701e04c3fSmrg      this->progress = true;
23801e04c3fSmrg      void *mem_ctx = ralloc_parent(array_deref);
23901e04c3fSmrg
24001e04c3fSmrg      ir_expression *const expr =
24101e04c3fSmrg         new(mem_ctx) ir_expression(ir_binop_vector_extract,
24201e04c3fSmrg                                    lowered_vec4,
24301e04c3fSmrg                                    array_deref->array_index);
24401e04c3fSmrg
24501e04c3fSmrg      *rv = expr;
24601e04c3fSmrg   }
24701e04c3fSmrg}
24801e04c3fSmrg
24901e04c3fSmrgvoid
25001e04c3fSmrglower_tess_level_visitor::fix_lhs(ir_assignment *ir)
25101e04c3fSmrg{
25201e04c3fSmrg   if (ir->lhs->ir_type != ir_type_expression)
25301e04c3fSmrg      return;
25401e04c3fSmrg   void *mem_ctx = ralloc_parent(ir);
25501e04c3fSmrg   ir_expression *const expr = (ir_expression *) ir->lhs;
25601e04c3fSmrg
25701e04c3fSmrg   /* The expression must be of the form:
25801e04c3fSmrg    *
25901e04c3fSmrg    *     (vector_extract gl_TessLevel*MESA, j).
26001e04c3fSmrg    */
26101e04c3fSmrg   assert(expr->operation == ir_binop_vector_extract);
26201e04c3fSmrg   assert(expr->operands[0]->ir_type == ir_type_dereference_variable);
26301e04c3fSmrg   assert((expr->operands[0]->type == glsl_type::vec4_type) ||
26401e04c3fSmrg          (expr->operands[0]->type == glsl_type::vec2_type));
26501e04c3fSmrg
26601e04c3fSmrg   ir_dereference *const new_lhs = (ir_dereference *) expr->operands[0];
26701e04c3fSmrg
26801e04c3fSmrg   ir_constant *old_index_constant =
26901e04c3fSmrg      expr->operands[1]->constant_expression_value(mem_ctx);
27001e04c3fSmrg   if (!old_index_constant) {
27101e04c3fSmrg      ir->rhs = new(mem_ctx) ir_expression(ir_triop_vector_insert,
27201e04c3fSmrg                                           expr->operands[0]->type,
27301e04c3fSmrg                                           new_lhs->clone(mem_ctx, NULL),
27401e04c3fSmrg                                           ir->rhs,
27501e04c3fSmrg                                           expr->operands[1]);
27601e04c3fSmrg   }
27701e04c3fSmrg   ir->set_lhs(new_lhs);
27801e04c3fSmrg
27901e04c3fSmrg   if (old_index_constant) {
28001e04c3fSmrg      /* gl_TessLevel* is being accessed via a constant index.  Don't bother
28101e04c3fSmrg       * creating a vector insert op. Just use a write mask.
28201e04c3fSmrg       */
28301e04c3fSmrg      ir->write_mask = 1 << old_index_constant->get_int_component(0);
28401e04c3fSmrg   } else {
28501e04c3fSmrg      ir->write_mask = (1 << expr->operands[0]->type->vector_elements) - 1;
28601e04c3fSmrg   }
28701e04c3fSmrg}
28801e04c3fSmrg
28901e04c3fSmrg/**
29001e04c3fSmrg * Replace any assignment having a gl_TessLevel* (undereferenced) as
29101e04c3fSmrg * its LHS or RHS with a sequence of assignments, one for each component of
29201e04c3fSmrg * the array.  Each of these assignments is lowered to refer to
29301e04c3fSmrg * gl_TessLevel*MESA as appropriate.
29401e04c3fSmrg */
29501e04c3fSmrgir_visitor_status
29601e04c3fSmrglower_tess_level_visitor::visit_leave(ir_assignment *ir)
29701e04c3fSmrg{
29801e04c3fSmrg   /* First invoke the base class visitor.  This causes handle_rvalue() to be
29901e04c3fSmrg    * called on ir->rhs and ir->condition.
30001e04c3fSmrg    */
30101e04c3fSmrg   ir_rvalue_visitor::visit_leave(ir);
30201e04c3fSmrg
30301e04c3fSmrg   if (this->is_tess_level_array(ir->lhs) ||
30401e04c3fSmrg       this->is_tess_level_array(ir->rhs)) {
30501e04c3fSmrg      /* LHS or RHS of the assignment is the entire gl_TessLevel* array.
30601e04c3fSmrg       * Since we are
30701e04c3fSmrg       * reshaping gl_TessLevel* from an array of floats to a
30801e04c3fSmrg       * vec4, this isn't going to work as a bulk assignment anymore, so
30901e04c3fSmrg       * unroll it to element-by-element assignments and lower each of them.
31001e04c3fSmrg       *
31101e04c3fSmrg       * Note: to unroll into element-by-element assignments, we need to make
31201e04c3fSmrg       * clones of the LHS and RHS.  This is safe because expressions and
31301e04c3fSmrg       * l-values are side-effect free.
31401e04c3fSmrg       */
31501e04c3fSmrg      void *ctx = ralloc_parent(ir);
31601e04c3fSmrg      int array_size = ir->lhs->type->array_size();
31701e04c3fSmrg      for (int i = 0; i < array_size; ++i) {
31801e04c3fSmrg         ir_dereference_array *new_lhs = new(ctx) ir_dereference_array(
31901e04c3fSmrg            ir->lhs->clone(ctx, NULL), new(ctx) ir_constant(i));
32001e04c3fSmrg         ir_dereference_array *new_rhs = new(ctx) ir_dereference_array(
32101e04c3fSmrg            ir->rhs->clone(ctx, NULL), new(ctx) ir_constant(i));
32201e04c3fSmrg         this->handle_rvalue((ir_rvalue **) &new_rhs);
32301e04c3fSmrg
32401e04c3fSmrg         /* Handle the LHS after creating the new assignment.  This must
32501e04c3fSmrg          * happen in this order because handle_rvalue may replace the old LHS
32601e04c3fSmrg          * with an ir_expression of ir_binop_vector_extract.  Since this is
32701e04c3fSmrg          * not a valide l-value, this will cause an assertion in the
32801e04c3fSmrg          * ir_assignment constructor to fail.
32901e04c3fSmrg          *
33001e04c3fSmrg          * If this occurs, replace the mangled LHS with a dereference of the
33101e04c3fSmrg          * vector, and replace the RHS with an ir_triop_vector_insert.
33201e04c3fSmrg          */
33301e04c3fSmrg         ir_assignment *const assign = new(ctx) ir_assignment(new_lhs, new_rhs);
33401e04c3fSmrg         this->handle_rvalue((ir_rvalue **) &assign->lhs);
33501e04c3fSmrg         this->fix_lhs(assign);
33601e04c3fSmrg
33701e04c3fSmrg         this->base_ir->insert_before(assign);
33801e04c3fSmrg      }
33901e04c3fSmrg      ir->remove();
34001e04c3fSmrg
34101e04c3fSmrg      return visit_continue;
34201e04c3fSmrg   }
34301e04c3fSmrg
34401e04c3fSmrg   /* Handle the LHS as if it were an r-value.  Normally
34501e04c3fSmrg    * rvalue_visit(ir_assignment *) only visits the RHS, but we need to lower
34601e04c3fSmrg    * expressions in the LHS as well.
34701e04c3fSmrg    *
34801e04c3fSmrg    * This may cause the LHS to get replaced with an ir_expression of
34901e04c3fSmrg    * ir_binop_vector_extract.  If this occurs, replace it with a dereference
35001e04c3fSmrg    * of the vector, and replace the RHS with an ir_triop_vector_insert.
35101e04c3fSmrg    */
35201e04c3fSmrg   handle_rvalue((ir_rvalue **)&ir->lhs);
35301e04c3fSmrg   this->fix_lhs(ir);
35401e04c3fSmrg
35501e04c3fSmrg   return rvalue_visit(ir);
35601e04c3fSmrg}
35701e04c3fSmrg
35801e04c3fSmrg
35901e04c3fSmrg/**
36001e04c3fSmrg * Set up base_ir properly and call visit_leave() on a newly created
36101e04c3fSmrg * ir_assignment node.  This is used in cases where we have to insert an
36201e04c3fSmrg * ir_assignment in a place where we know the hierarchical visitor won't see
36301e04c3fSmrg * it.
36401e04c3fSmrg */
36501e04c3fSmrgvoid
36601e04c3fSmrglower_tess_level_visitor::visit_new_assignment(ir_assignment *ir)
36701e04c3fSmrg{
36801e04c3fSmrg   ir_instruction *old_base_ir = this->base_ir;
36901e04c3fSmrg   this->base_ir = ir;
37001e04c3fSmrg   ir->accept(this);
37101e04c3fSmrg   this->base_ir = old_base_ir;
37201e04c3fSmrg}
37301e04c3fSmrg
37401e04c3fSmrg
37501e04c3fSmrg/**
37601e04c3fSmrg * If a gl_TessLevel* variable appears as an argument in an ir_call
37701e04c3fSmrg * expression, replace it with a temporary variable, and make sure the ir_call
37801e04c3fSmrg * is preceded and/or followed by assignments that copy the contents of the
37901e04c3fSmrg * temporary variable to and/or from gl_TessLevel*.  Each of these
38001e04c3fSmrg * assignments is then lowered to refer to gl_TessLevel*MESA.
38101e04c3fSmrg */
38201e04c3fSmrgir_visitor_status
38301e04c3fSmrglower_tess_level_visitor::visit_leave(ir_call *ir)
38401e04c3fSmrg{
38501e04c3fSmrg   void *ctx = ralloc_parent(ir);
38601e04c3fSmrg
38701e04c3fSmrg   const exec_node *formal_param_node = ir->callee->parameters.get_head_raw();
38801e04c3fSmrg   const exec_node *actual_param_node = ir->actual_parameters.get_head_raw();
38901e04c3fSmrg   while (!actual_param_node->is_tail_sentinel()) {
39001e04c3fSmrg      ir_variable *formal_param = (ir_variable *) formal_param_node;
39101e04c3fSmrg      ir_rvalue *actual_param = (ir_rvalue *) actual_param_node;
39201e04c3fSmrg
39301e04c3fSmrg      /* Advance formal_param_node and actual_param_node now so that we can
39401e04c3fSmrg       * safely replace actual_param with another node, if necessary, below.
39501e04c3fSmrg       */
39601e04c3fSmrg      formal_param_node = formal_param_node->next;
39701e04c3fSmrg      actual_param_node = actual_param_node->next;
39801e04c3fSmrg
39901e04c3fSmrg      if (!this->is_tess_level_array(actual_param))
40001e04c3fSmrg         continue;
40101e04c3fSmrg
40201e04c3fSmrg      /* User is trying to pass a whole gl_TessLevel* array to a function
40301e04c3fSmrg       * call.  Since we are reshaping gl_TessLevel* from an array of floats
40401e04c3fSmrg       * to a vec4, this isn't going to work anymore, so use a temporary
40501e04c3fSmrg       * array instead.
40601e04c3fSmrg       */
40701e04c3fSmrg      ir_variable *temp = new(ctx) ir_variable(
40801e04c3fSmrg         actual_param->type, "temp_tess_level", ir_var_temporary);
40901e04c3fSmrg      this->base_ir->insert_before(temp);
41001e04c3fSmrg      actual_param->replace_with(
41101e04c3fSmrg         new(ctx) ir_dereference_variable(temp));
41201e04c3fSmrg      if (formal_param->data.mode == ir_var_function_in
41301e04c3fSmrg          || formal_param->data.mode == ir_var_function_inout) {
41401e04c3fSmrg         /* Copy from gl_TessLevel* to the temporary before the call.
41501e04c3fSmrg          * Since we are going to insert this copy before the current
41601e04c3fSmrg          * instruction, we need to visit it afterwards to make sure it
41701e04c3fSmrg          * gets lowered.
41801e04c3fSmrg          */
41901e04c3fSmrg         ir_assignment *new_assignment = new(ctx) ir_assignment(
42001e04c3fSmrg            new(ctx) ir_dereference_variable(temp),
42101e04c3fSmrg            actual_param->clone(ctx, NULL));
42201e04c3fSmrg         this->base_ir->insert_before(new_assignment);
42301e04c3fSmrg         this->visit_new_assignment(new_assignment);
42401e04c3fSmrg      }
42501e04c3fSmrg      if (formal_param->data.mode == ir_var_function_out
42601e04c3fSmrg          || formal_param->data.mode == ir_var_function_inout) {
42701e04c3fSmrg         /* Copy from the temporary to gl_TessLevel* after the call.
42801e04c3fSmrg          * Since visit_list_elements() has already decided which
42901e04c3fSmrg          * instruction it's going to visit next, we need to visit
43001e04c3fSmrg          * afterwards to make sure it gets lowered.
43101e04c3fSmrg          */
43201e04c3fSmrg         ir_assignment *new_assignment = new(ctx) ir_assignment(
43301e04c3fSmrg            actual_param->clone(ctx, NULL),
43401e04c3fSmrg            new(ctx) ir_dereference_variable(temp));
43501e04c3fSmrg         this->base_ir->insert_after(new_assignment);
43601e04c3fSmrg         this->visit_new_assignment(new_assignment);
43701e04c3fSmrg      }
43801e04c3fSmrg   }
43901e04c3fSmrg
44001e04c3fSmrg   return rvalue_visit(ir);
44101e04c3fSmrg}
44201e04c3fSmrg
44301e04c3fSmrg
44401e04c3fSmrgbool
44501e04c3fSmrglower_tess_level(gl_linked_shader *shader)
44601e04c3fSmrg{
44701e04c3fSmrg   if ((shader->Stage != MESA_SHADER_TESS_CTRL) &&
44801e04c3fSmrg       (shader->Stage != MESA_SHADER_TESS_EVAL))
44901e04c3fSmrg      return false;
45001e04c3fSmrg
45101e04c3fSmrg   lower_tess_level_visitor v(shader->Stage);
45201e04c3fSmrg
45301e04c3fSmrg   visit_list_elements(&v, shader->ir);
45401e04c3fSmrg
45501e04c3fSmrg   if (v.new_tess_level_outer_var)
45601e04c3fSmrg      shader->symbols->add_variable(v.new_tess_level_outer_var);
45701e04c3fSmrg   if (v.new_tess_level_inner_var)
45801e04c3fSmrg      shader->symbols->add_variable(v.new_tess_level_inner_var);
45901e04c3fSmrg
46001e04c3fSmrg   return v.progress;
46101e04c3fSmrg}
462