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