1b8e80941Smrg/*
2b8e80941Smrg * Copyright © 2014 Intel Corporation
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21b8e80941Smrg * DEALINGS IN THE SOFTWARE.
22b8e80941Smrg */
23b8e80941Smrg
24b8e80941Smrg/**
25b8e80941Smrg * \file lower_tess_level.cpp
26b8e80941Smrg *
27b8e80941Smrg * This pass accounts for the difference between the way gl_TessLevelOuter
28b8e80941Smrg * and gl_TessLevelInner is declared in standard GLSL (as an array of
29b8e80941Smrg * floats), and the way it is frequently implemented in hardware (as a vec4
30b8e80941Smrg * and vec2).
31b8e80941Smrg *
32b8e80941Smrg * The declaration of gl_TessLevel* is replaced with a declaration
33b8e80941Smrg * of gl_TessLevel*MESA, and any references to gl_TessLevel* are
34b8e80941Smrg * translated to refer to gl_TessLevel*MESA with the appropriate
35b8e80941Smrg * swizzling of array indices.  For instance:
36b8e80941Smrg *
37b8e80941Smrg *   gl_TessLevelOuter[i]
38b8e80941Smrg *
39b8e80941Smrg * is translated into:
40b8e80941Smrg *
41b8e80941Smrg *   gl_TessLevelOuterMESA[i]
42b8e80941Smrg *
43b8e80941Smrg * Since some hardware may not internally represent gl_TessLevel* as a pair
44b8e80941Smrg * of vec4's, this lowering pass is optional.  To enable it, set the
45b8e80941Smrg * LowerTessLevel flag in gl_shader_compiler_options to true.
46b8e80941Smrg */
47b8e80941Smrg
48b8e80941Smrg#include "glsl_symbol_table.h"
49b8e80941Smrg#include "ir_rvalue_visitor.h"
50b8e80941Smrg#include "ir.h"
51b8e80941Smrg#include "program/prog_instruction.h" /* For WRITEMASK_* */
52b8e80941Smrg#include "main/mtypes.h"
53b8e80941Smrg
54b8e80941Smrgnamespace {
55b8e80941Smrg
56b8e80941Smrgclass lower_tess_level_visitor : public ir_rvalue_visitor {
57b8e80941Smrgpublic:
58b8e80941Smrg   explicit lower_tess_level_visitor(gl_shader_stage shader_stage)
59b8e80941Smrg      : progress(false), old_tess_level_outer_var(NULL),
60b8e80941Smrg        old_tess_level_inner_var(NULL), new_tess_level_outer_var(NULL),
61b8e80941Smrg        new_tess_level_inner_var(NULL), shader_stage(shader_stage)
62b8e80941Smrg   {
63b8e80941Smrg   }
64b8e80941Smrg
65b8e80941Smrg   virtual ir_visitor_status visit(ir_variable *);
66b8e80941Smrg   bool is_tess_level_array(ir_rvalue *ir);
67b8e80941Smrg   ir_rvalue *lower_tess_level_array(ir_rvalue *ir);
68b8e80941Smrg   virtual ir_visitor_status visit_leave(ir_assignment *);
69b8e80941Smrg   void visit_new_assignment(ir_assignment *ir);
70b8e80941Smrg   virtual ir_visitor_status visit_leave(ir_call *);
71b8e80941Smrg
72b8e80941Smrg   virtual void handle_rvalue(ir_rvalue **rvalue);
73b8e80941Smrg
74b8e80941Smrg   void fix_lhs(ir_assignment *);
75b8e80941Smrg
76b8e80941Smrg   bool progress;
77b8e80941Smrg
78b8e80941Smrg   /**
79b8e80941Smrg    * Pointer to the declaration of gl_TessLevel*, if found.
80b8e80941Smrg    */
81b8e80941Smrg   ir_variable *old_tess_level_outer_var;
82b8e80941Smrg   ir_variable *old_tess_level_inner_var;
83b8e80941Smrg
84b8e80941Smrg   /**
85b8e80941Smrg    * Pointer to the newly-created gl_TessLevel*MESA variables.
86b8e80941Smrg    */
87b8e80941Smrg   ir_variable *new_tess_level_outer_var;
88b8e80941Smrg   ir_variable *new_tess_level_inner_var;
89b8e80941Smrg
90b8e80941Smrg   /**
91b8e80941Smrg    * Type of shader we are compiling (e.g. MESA_SHADER_TESS_CTRL)
92b8e80941Smrg    */
93b8e80941Smrg   const gl_shader_stage shader_stage;
94b8e80941Smrg};
95b8e80941Smrg
96b8e80941Smrg} /* anonymous namespace */
97b8e80941Smrg
98b8e80941Smrg/**
99b8e80941Smrg * Replace any declaration of gl_TessLevel* as an array of floats with a
100b8e80941Smrg * declaration of gl_TessLevel*MESA as a vec4.
101b8e80941Smrg */
102b8e80941Smrgir_visitor_status
103b8e80941Smrglower_tess_level_visitor::visit(ir_variable *ir)
104b8e80941Smrg{
105b8e80941Smrg   if ((!ir->name) ||
106b8e80941Smrg       ((strcmp(ir->name, "gl_TessLevelInner") != 0) &&
107b8e80941Smrg        (strcmp(ir->name, "gl_TessLevelOuter") != 0)))
108b8e80941Smrg      return visit_continue;
109b8e80941Smrg
110b8e80941Smrg   assert (ir->type->is_array());
111b8e80941Smrg
112b8e80941Smrg   if (strcmp(ir->name, "gl_TessLevelOuter") == 0) {
113b8e80941Smrg      if (this->old_tess_level_outer_var)
114b8e80941Smrg         return visit_continue;
115b8e80941Smrg
116b8e80941Smrg      old_tess_level_outer_var = ir;
117b8e80941Smrg      assert(ir->type->fields.array == glsl_type::float_type);
118b8e80941Smrg
119b8e80941Smrg      /* Clone the old var so that we inherit all of its properties */
120b8e80941Smrg      new_tess_level_outer_var = ir->clone(ralloc_parent(ir), NULL);
121b8e80941Smrg
122b8e80941Smrg      /* And change the properties that we need to change */
123b8e80941Smrg      new_tess_level_outer_var->name = ralloc_strdup(new_tess_level_outer_var,
124b8e80941Smrg                                                "gl_TessLevelOuterMESA");
125b8e80941Smrg      new_tess_level_outer_var->type = glsl_type::vec4_type;
126b8e80941Smrg      new_tess_level_outer_var->data.max_array_access = 0;
127b8e80941Smrg
128b8e80941Smrg      ir->replace_with(new_tess_level_outer_var);
129b8e80941Smrg   } else if (strcmp(ir->name, "gl_TessLevelInner") == 0) {
130b8e80941Smrg      if (this->old_tess_level_inner_var)
131b8e80941Smrg         return visit_continue;
132b8e80941Smrg
133b8e80941Smrg      old_tess_level_inner_var = ir;
134b8e80941Smrg      assert(ir->type->fields.array == glsl_type::float_type);
135b8e80941Smrg
136b8e80941Smrg      /* Clone the old var so that we inherit all of its properties */
137b8e80941Smrg      new_tess_level_inner_var = ir->clone(ralloc_parent(ir), NULL);
138b8e80941Smrg
139b8e80941Smrg      /* And change the properties that we need to change */
140b8e80941Smrg      new_tess_level_inner_var->name = ralloc_strdup(new_tess_level_inner_var,
141b8e80941Smrg                                                "gl_TessLevelInnerMESA");
142b8e80941Smrg      new_tess_level_inner_var->type = glsl_type::vec2_type;
143b8e80941Smrg      new_tess_level_inner_var->data.max_array_access = 0;
144b8e80941Smrg
145b8e80941Smrg      ir->replace_with(new_tess_level_inner_var);
146b8e80941Smrg   } else {
147b8e80941Smrg      assert(0);
148b8e80941Smrg   }
149b8e80941Smrg
150b8e80941Smrg   this->progress = true;
151b8e80941Smrg
152b8e80941Smrg   return visit_continue;
153b8e80941Smrg}
154b8e80941Smrg
155b8e80941Smrg
156b8e80941Smrg/**
157b8e80941Smrg * Determine whether the given rvalue describes an array of floats that
158b8e80941Smrg * needs to be lowered to a vec4; that is, determine whether it
159b8e80941Smrg * matches one of the following patterns:
160b8e80941Smrg *
161b8e80941Smrg * - gl_TessLevelOuter
162b8e80941Smrg * - gl_TessLevelInner
163b8e80941Smrg */
164b8e80941Smrgbool
165b8e80941Smrglower_tess_level_visitor::is_tess_level_array(ir_rvalue *ir)
166b8e80941Smrg{
167b8e80941Smrg   if (!ir->type->is_array())
168b8e80941Smrg      return false;
169b8e80941Smrg   if (ir->type->fields.array != glsl_type::float_type)
170b8e80941Smrg      return false;
171b8e80941Smrg
172b8e80941Smrg   if (this->old_tess_level_outer_var) {
173b8e80941Smrg      if (ir->variable_referenced() == this->old_tess_level_outer_var)
174b8e80941Smrg         return true;
175b8e80941Smrg   }
176b8e80941Smrg   if (this->old_tess_level_inner_var) {
177b8e80941Smrg      if (ir->variable_referenced() == this->old_tess_level_inner_var)
178b8e80941Smrg         return true;
179b8e80941Smrg   }
180b8e80941Smrg   return false;
181b8e80941Smrg}
182b8e80941Smrg
183b8e80941Smrg
184b8e80941Smrg/**
185b8e80941Smrg * If the given ir satisfies is_tess_level_array(), return new ir
186b8e80941Smrg * representing its lowered equivalent.  That is, map:
187b8e80941Smrg *
188b8e80941Smrg * - gl_TessLevelOuter => gl_TessLevelOuterMESA
189b8e80941Smrg * - gl_TessLevelInner => gl_TessLevelInnerMESA
190b8e80941Smrg *
191b8e80941Smrg * Otherwise return NULL.
192b8e80941Smrg */
193b8e80941Smrgir_rvalue *
194b8e80941Smrglower_tess_level_visitor::lower_tess_level_array(ir_rvalue *ir)
195b8e80941Smrg{
196b8e80941Smrg   if (!ir->type->is_array())
197b8e80941Smrg      return NULL;
198b8e80941Smrg   if (ir->type->fields.array != glsl_type::float_type)
199b8e80941Smrg      return NULL;
200b8e80941Smrg
201b8e80941Smrg   ir_variable **new_var = NULL;
202b8e80941Smrg
203b8e80941Smrg   if (this->old_tess_level_outer_var) {
204b8e80941Smrg      if (ir->variable_referenced() == this->old_tess_level_outer_var)
205b8e80941Smrg         new_var = &this->new_tess_level_outer_var;
206b8e80941Smrg   }
207b8e80941Smrg   if (this->old_tess_level_inner_var) {
208b8e80941Smrg      if (ir->variable_referenced() == this->old_tess_level_inner_var)
209b8e80941Smrg         new_var = &this->new_tess_level_inner_var;
210b8e80941Smrg   }
211b8e80941Smrg
212b8e80941Smrg   if (new_var == NULL)
213b8e80941Smrg      return NULL;
214b8e80941Smrg
215b8e80941Smrg   assert(ir->as_dereference_variable());
216b8e80941Smrg   return new(ralloc_parent(ir)) ir_dereference_variable(*new_var);
217b8e80941Smrg}
218b8e80941Smrg
219b8e80941Smrg
220b8e80941Smrgvoid
221b8e80941Smrglower_tess_level_visitor::handle_rvalue(ir_rvalue **rv)
222b8e80941Smrg{
223b8e80941Smrg   if (*rv == NULL)
224b8e80941Smrg      return;
225b8e80941Smrg
226b8e80941Smrg   ir_dereference_array *const array_deref = (*rv)->as_dereference_array();
227b8e80941Smrg   if (array_deref == NULL)
228b8e80941Smrg      return;
229b8e80941Smrg
230b8e80941Smrg   /* Replace any expression that indexes one of the floats in gl_TessLevel*
231b8e80941Smrg    * with an expression that indexes into one of the vec4's
232b8e80941Smrg    * gl_TessLevel*MESA and accesses the appropriate component.
233b8e80941Smrg    */
234b8e80941Smrg   ir_rvalue *lowered_vec4 =
235b8e80941Smrg      this->lower_tess_level_array(array_deref->array);
236b8e80941Smrg   if (lowered_vec4 != NULL) {
237b8e80941Smrg      this->progress = true;
238b8e80941Smrg      void *mem_ctx = ralloc_parent(array_deref);
239b8e80941Smrg
240b8e80941Smrg      ir_expression *const expr =
241b8e80941Smrg         new(mem_ctx) ir_expression(ir_binop_vector_extract,
242b8e80941Smrg                                    lowered_vec4,
243b8e80941Smrg                                    array_deref->array_index);
244b8e80941Smrg
245b8e80941Smrg      *rv = expr;
246b8e80941Smrg   }
247b8e80941Smrg}
248b8e80941Smrg
249b8e80941Smrgvoid
250b8e80941Smrglower_tess_level_visitor::fix_lhs(ir_assignment *ir)
251b8e80941Smrg{
252b8e80941Smrg   if (ir->lhs->ir_type != ir_type_expression)
253b8e80941Smrg      return;
254b8e80941Smrg   void *mem_ctx = ralloc_parent(ir);
255b8e80941Smrg   ir_expression *const expr = (ir_expression *) ir->lhs;
256b8e80941Smrg
257b8e80941Smrg   /* The expression must be of the form:
258b8e80941Smrg    *
259b8e80941Smrg    *     (vector_extract gl_TessLevel*MESA, j).
260b8e80941Smrg    */
261b8e80941Smrg   assert(expr->operation == ir_binop_vector_extract);
262b8e80941Smrg   assert(expr->operands[0]->ir_type == ir_type_dereference_variable);
263b8e80941Smrg   assert((expr->operands[0]->type == glsl_type::vec4_type) ||
264b8e80941Smrg          (expr->operands[0]->type == glsl_type::vec2_type));
265b8e80941Smrg
266b8e80941Smrg   ir_dereference *const new_lhs = (ir_dereference *) expr->operands[0];
267b8e80941Smrg
268b8e80941Smrg   ir_constant *old_index_constant =
269b8e80941Smrg      expr->operands[1]->constant_expression_value(mem_ctx);
270b8e80941Smrg   if (!old_index_constant) {
271b8e80941Smrg      ir->rhs = new(mem_ctx) ir_expression(ir_triop_vector_insert,
272b8e80941Smrg                                           expr->operands[0]->type,
273b8e80941Smrg                                           new_lhs->clone(mem_ctx, NULL),
274b8e80941Smrg                                           ir->rhs,
275b8e80941Smrg                                           expr->operands[1]);
276b8e80941Smrg   }
277b8e80941Smrg   ir->set_lhs(new_lhs);
278b8e80941Smrg
279b8e80941Smrg   if (old_index_constant) {
280b8e80941Smrg      /* gl_TessLevel* is being accessed via a constant index.  Don't bother
281b8e80941Smrg       * creating a vector insert op. Just use a write mask.
282b8e80941Smrg       */
283b8e80941Smrg      ir->write_mask = 1 << old_index_constant->get_int_component(0);
284b8e80941Smrg   } else {
285b8e80941Smrg      ir->write_mask = (1 << expr->operands[0]->type->vector_elements) - 1;
286b8e80941Smrg   }
287b8e80941Smrg}
288b8e80941Smrg
289b8e80941Smrg/**
290b8e80941Smrg * Replace any assignment having a gl_TessLevel* (undereferenced) as
291b8e80941Smrg * its LHS or RHS with a sequence of assignments, one for each component of
292b8e80941Smrg * the array.  Each of these assignments is lowered to refer to
293b8e80941Smrg * gl_TessLevel*MESA as appropriate.
294b8e80941Smrg */
295b8e80941Smrgir_visitor_status
296b8e80941Smrglower_tess_level_visitor::visit_leave(ir_assignment *ir)
297b8e80941Smrg{
298b8e80941Smrg   /* First invoke the base class visitor.  This causes handle_rvalue() to be
299b8e80941Smrg    * called on ir->rhs and ir->condition.
300b8e80941Smrg    */
301b8e80941Smrg   ir_rvalue_visitor::visit_leave(ir);
302b8e80941Smrg
303b8e80941Smrg   if (this->is_tess_level_array(ir->lhs) ||
304b8e80941Smrg       this->is_tess_level_array(ir->rhs)) {
305b8e80941Smrg      /* LHS or RHS of the assignment is the entire gl_TessLevel* array.
306b8e80941Smrg       * Since we are
307b8e80941Smrg       * reshaping gl_TessLevel* from an array of floats to a
308b8e80941Smrg       * vec4, this isn't going to work as a bulk assignment anymore, so
309b8e80941Smrg       * unroll it to element-by-element assignments and lower each of them.
310b8e80941Smrg       *
311b8e80941Smrg       * Note: to unroll into element-by-element assignments, we need to make
312b8e80941Smrg       * clones of the LHS and RHS.  This is safe because expressions and
313b8e80941Smrg       * l-values are side-effect free.
314b8e80941Smrg       */
315b8e80941Smrg      void *ctx = ralloc_parent(ir);
316b8e80941Smrg      int array_size = ir->lhs->type->array_size();
317b8e80941Smrg      for (int i = 0; i < array_size; ++i) {
318b8e80941Smrg         ir_dereference_array *new_lhs = new(ctx) ir_dereference_array(
319b8e80941Smrg            ir->lhs->clone(ctx, NULL), new(ctx) ir_constant(i));
320b8e80941Smrg         ir_dereference_array *new_rhs = new(ctx) ir_dereference_array(
321b8e80941Smrg            ir->rhs->clone(ctx, NULL), new(ctx) ir_constant(i));
322b8e80941Smrg         this->handle_rvalue((ir_rvalue **) &new_rhs);
323b8e80941Smrg
324b8e80941Smrg         /* Handle the LHS after creating the new assignment.  This must
325b8e80941Smrg          * happen in this order because handle_rvalue may replace the old LHS
326b8e80941Smrg          * with an ir_expression of ir_binop_vector_extract.  Since this is
327b8e80941Smrg          * not a valide l-value, this will cause an assertion in the
328b8e80941Smrg          * ir_assignment constructor to fail.
329b8e80941Smrg          *
330b8e80941Smrg          * If this occurs, replace the mangled LHS with a dereference of the
331b8e80941Smrg          * vector, and replace the RHS with an ir_triop_vector_insert.
332b8e80941Smrg          */
333b8e80941Smrg         ir_assignment *const assign = new(ctx) ir_assignment(new_lhs, new_rhs);
334b8e80941Smrg         this->handle_rvalue((ir_rvalue **) &assign->lhs);
335b8e80941Smrg         this->fix_lhs(assign);
336b8e80941Smrg
337b8e80941Smrg         this->base_ir->insert_before(assign);
338b8e80941Smrg      }
339b8e80941Smrg      ir->remove();
340b8e80941Smrg
341b8e80941Smrg      return visit_continue;
342b8e80941Smrg   }
343b8e80941Smrg
344b8e80941Smrg   /* Handle the LHS as if it were an r-value.  Normally
345b8e80941Smrg    * rvalue_visit(ir_assignment *) only visits the RHS, but we need to lower
346b8e80941Smrg    * expressions in the LHS as well.
347b8e80941Smrg    *
348b8e80941Smrg    * This may cause the LHS to get replaced with an ir_expression of
349b8e80941Smrg    * ir_binop_vector_extract.  If this occurs, replace it with a dereference
350b8e80941Smrg    * of the vector, and replace the RHS with an ir_triop_vector_insert.
351b8e80941Smrg    */
352b8e80941Smrg   handle_rvalue((ir_rvalue **)&ir->lhs);
353b8e80941Smrg   this->fix_lhs(ir);
354b8e80941Smrg
355b8e80941Smrg   return rvalue_visit(ir);
356b8e80941Smrg}
357b8e80941Smrg
358b8e80941Smrg
359b8e80941Smrg/**
360b8e80941Smrg * Set up base_ir properly and call visit_leave() on a newly created
361b8e80941Smrg * ir_assignment node.  This is used in cases where we have to insert an
362b8e80941Smrg * ir_assignment in a place where we know the hierarchical visitor won't see
363b8e80941Smrg * it.
364b8e80941Smrg */
365b8e80941Smrgvoid
366b8e80941Smrglower_tess_level_visitor::visit_new_assignment(ir_assignment *ir)
367b8e80941Smrg{
368b8e80941Smrg   ir_instruction *old_base_ir = this->base_ir;
369b8e80941Smrg   this->base_ir = ir;
370b8e80941Smrg   ir->accept(this);
371b8e80941Smrg   this->base_ir = old_base_ir;
372b8e80941Smrg}
373b8e80941Smrg
374b8e80941Smrg
375b8e80941Smrg/**
376b8e80941Smrg * If a gl_TessLevel* variable appears as an argument in an ir_call
377b8e80941Smrg * expression, replace it with a temporary variable, and make sure the ir_call
378b8e80941Smrg * is preceded and/or followed by assignments that copy the contents of the
379b8e80941Smrg * temporary variable to and/or from gl_TessLevel*.  Each of these
380b8e80941Smrg * assignments is then lowered to refer to gl_TessLevel*MESA.
381b8e80941Smrg */
382b8e80941Smrgir_visitor_status
383b8e80941Smrglower_tess_level_visitor::visit_leave(ir_call *ir)
384b8e80941Smrg{
385b8e80941Smrg   void *ctx = ralloc_parent(ir);
386b8e80941Smrg
387b8e80941Smrg   const exec_node *formal_param_node = ir->callee->parameters.get_head_raw();
388b8e80941Smrg   const exec_node *actual_param_node = ir->actual_parameters.get_head_raw();
389b8e80941Smrg   while (!actual_param_node->is_tail_sentinel()) {
390b8e80941Smrg      ir_variable *formal_param = (ir_variable *) formal_param_node;
391b8e80941Smrg      ir_rvalue *actual_param = (ir_rvalue *) actual_param_node;
392b8e80941Smrg
393b8e80941Smrg      /* Advance formal_param_node and actual_param_node now so that we can
394b8e80941Smrg       * safely replace actual_param with another node, if necessary, below.
395b8e80941Smrg       */
396b8e80941Smrg      formal_param_node = formal_param_node->next;
397b8e80941Smrg      actual_param_node = actual_param_node->next;
398b8e80941Smrg
399b8e80941Smrg      if (!this->is_tess_level_array(actual_param))
400b8e80941Smrg         continue;
401b8e80941Smrg
402b8e80941Smrg      /* User is trying to pass a whole gl_TessLevel* array to a function
403b8e80941Smrg       * call.  Since we are reshaping gl_TessLevel* from an array of floats
404b8e80941Smrg       * to a vec4, this isn't going to work anymore, so use a temporary
405b8e80941Smrg       * array instead.
406b8e80941Smrg       */
407b8e80941Smrg      ir_variable *temp = new(ctx) ir_variable(
408b8e80941Smrg         actual_param->type, "temp_tess_level", ir_var_temporary);
409b8e80941Smrg      this->base_ir->insert_before(temp);
410b8e80941Smrg      actual_param->replace_with(
411b8e80941Smrg         new(ctx) ir_dereference_variable(temp));
412b8e80941Smrg      if (formal_param->data.mode == ir_var_function_in
413b8e80941Smrg          || formal_param->data.mode == ir_var_function_inout) {
414b8e80941Smrg         /* Copy from gl_TessLevel* to the temporary before the call.
415b8e80941Smrg          * Since we are going to insert this copy before the current
416b8e80941Smrg          * instruction, we need to visit it afterwards to make sure it
417b8e80941Smrg          * gets lowered.
418b8e80941Smrg          */
419b8e80941Smrg         ir_assignment *new_assignment = new(ctx) ir_assignment(
420b8e80941Smrg            new(ctx) ir_dereference_variable(temp),
421b8e80941Smrg            actual_param->clone(ctx, NULL));
422b8e80941Smrg         this->base_ir->insert_before(new_assignment);
423b8e80941Smrg         this->visit_new_assignment(new_assignment);
424b8e80941Smrg      }
425b8e80941Smrg      if (formal_param->data.mode == ir_var_function_out
426b8e80941Smrg          || formal_param->data.mode == ir_var_function_inout) {
427b8e80941Smrg         /* Copy from the temporary to gl_TessLevel* after the call.
428b8e80941Smrg          * Since visit_list_elements() has already decided which
429b8e80941Smrg          * instruction it's going to visit next, we need to visit
430b8e80941Smrg          * afterwards to make sure it gets lowered.
431b8e80941Smrg          */
432b8e80941Smrg         ir_assignment *new_assignment = new(ctx) ir_assignment(
433b8e80941Smrg            actual_param->clone(ctx, NULL),
434b8e80941Smrg            new(ctx) ir_dereference_variable(temp));
435b8e80941Smrg         this->base_ir->insert_after(new_assignment);
436b8e80941Smrg         this->visit_new_assignment(new_assignment);
437b8e80941Smrg      }
438b8e80941Smrg   }
439b8e80941Smrg
440b8e80941Smrg   return rvalue_visit(ir);
441b8e80941Smrg}
442b8e80941Smrg
443b8e80941Smrg
444b8e80941Smrgbool
445b8e80941Smrglower_tess_level(gl_linked_shader *shader)
446b8e80941Smrg{
447b8e80941Smrg   if ((shader->Stage != MESA_SHADER_TESS_CTRL) &&
448b8e80941Smrg       (shader->Stage != MESA_SHADER_TESS_EVAL))
449b8e80941Smrg      return false;
450b8e80941Smrg
451b8e80941Smrg   lower_tess_level_visitor v(shader->Stage);
452b8e80941Smrg
453b8e80941Smrg   visit_list_elements(&v, shader->ir);
454b8e80941Smrg
455b8e80941Smrg   if (v.new_tess_level_outer_var)
456b8e80941Smrg      shader->symbols->add_variable(v.new_tess_level_outer_var);
457b8e80941Smrg   if (v.new_tess_level_inner_var)
458b8e80941Smrg      shader->symbols->add_variable(v.new_tess_level_inner_var);
459b8e80941Smrg
460b8e80941Smrg   return v.progress;
461b8e80941Smrg}
462