lower_distance.cpp revision 7a6d55826e94e6c18471c671736f706bddfdb582
1c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry/*
2c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * Copyright © 2011 Intel Corporation
3c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry *
4c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * Permission is hereby granted, free of charge, to any person obtaining a
5c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * copy of this software and associated documentation files (the "Software"),
6c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * to deal in the Software without restriction, including without limitation
7c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * and/or sell copies of the Software, and to permit persons to whom the
9c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * Software is furnished to do so, subject to the following conditions:
10c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry *
11c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * The above copyright notice and this permission notice (including the next
12c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * paragraph) shall be included in all copies or substantial portions of the
13c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * Software.
14c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry *
15c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * DEALINGS IN THE SOFTWARE.
22c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry */
23c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
24c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry/**
25dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie * \file lower_distance.cpp
26c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry *
27c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * This pass accounts for the difference between the way
287a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * gl_ClipDistance is declared in standard GLSL (as an array of
297a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * floats), and the way it is frequently implemented in hardware (as
307a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * a pair of vec4s, with four clip distances packed into each).
31c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry *
327a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * The declaration of gl_ClipDistance is replaced with a declaration
337a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * of gl_ClipDistanceMESA, and any references to gl_ClipDistance are
347a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * translated to refer to gl_ClipDistanceMESA with the appropriate
357a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * swizzling of array indices.  For instance:
36c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry *
37c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry *   gl_ClipDistance[i]
38c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry *
39c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * is translated into:
40c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry *
41c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry *   gl_ClipDistanceMESA[i>>2][i&3]
42c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry *
437a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * Since some hardware may not internally represent gl_ClipDistance as a pair
447a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * of vec4's, this lowering pass is optional.  To enable it, set the
457a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * LowerCombinedClipCullDistance flag in gl_shader_compiler_options to true.
46c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry */
47c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
4818392443d448e083187d085965375e7de910b18aPaul Berry#include "glsl_symbol_table.h"
49065da16508731f6b6a98865f392509be4f9ce07fIan Romanick#include "ir_rvalue_visitor.h"
50c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry#include "ir.h"
51c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick#include "program/prog_instruction.h" /* For WRITEMASK_* */
52c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
5310ef949424809d51c627008bb2feab5a067f8e08Eric Anholtnamespace {
5410ef949424809d51c627008bb2feab5a067f8e08Eric Anholt
55dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlieclass lower_distance_visitor : public ir_rvalue_visitor {
56c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berrypublic:
577a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie   explicit lower_distance_visitor(gl_shader_stage shader_stage)
58dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie      : progress(false), old_distance_out_var(NULL),
59dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie        old_distance_in_var(NULL), new_distance_out_var(NULL),
607a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie        new_distance_in_var(NULL), shader_stage(shader_stage)
61c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   {
62c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   }
63c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
64c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   virtual ir_visitor_status visit(ir_variable *);
65c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   void create_indices(ir_rvalue*, ir_rvalue *&, ir_rvalue *&);
66dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie   bool is_distance_vec8(ir_rvalue *ir);
67dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie   ir_rvalue *lower_distance_vec8(ir_rvalue *ir);
68c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   virtual ir_visitor_status visit_leave(ir_assignment *);
69c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   void visit_new_assignment(ir_assignment *ir);
70c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   virtual ir_visitor_status visit_leave(ir_call *);
71c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
72065da16508731f6b6a98865f392509be4f9ce07fIan Romanick   virtual void handle_rvalue(ir_rvalue **rvalue);
73065da16508731f6b6a98865f392509be4f9ce07fIan Romanick
74c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick   void fix_lhs(ir_assignment *);
75c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick
76c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   bool progress;
77c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
78c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   /**
797a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie    * Pointer to the declaration of gl_ClipDistance, if found.
808f758b0b92f6105c9615740096fa1206befe343bBryan Cain    *
818f758b0b92f6105c9615740096fa1206befe343bBryan Cain    * Note:
828f758b0b92f6105c9615740096fa1206befe343bBryan Cain    *
8354f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler    * - the in_var is for geometry and both tessellation shader inputs only.
848f758b0b92f6105c9615740096fa1206befe343bBryan Cain    *
8554f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler    * - since gl_ClipDistance is available in tessellation control,
8654f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler    *   tessellation evaluation and geometry shaders as both an input
87dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie    *   and an output, it's possible for both old_distance_out_var
88dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie    *   and old_distance_in_var to be non-null.
89c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry    */
90dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie   ir_variable *old_distance_out_var;
91dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie   ir_variable *old_distance_in_var;
92c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
93c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   /**
947a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie    * Pointer to the newly-created gl_ClipDistanceMESA variable.
95c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry    */
96dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie   ir_variable *new_distance_out_var;
97dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie   ir_variable *new_distance_in_var;
988f758b0b92f6105c9615740096fa1206befe343bBryan Cain
998f758b0b92f6105c9615740096fa1206befe343bBryan Cain   /**
1002adb9fea77140fccb7bd3f7a547f026c050333bdPaul Berry    * Type of shader we are compiling (e.g. MESA_SHADER_VERTEX)
1018f758b0b92f6105c9615740096fa1206befe343bBryan Cain    */
1022adb9fea77140fccb7bd3f7a547f026c050333bdPaul Berry   const gl_shader_stage shader_stage;
103c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry};
104c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
10510ef949424809d51c627008bb2feab5a067f8e08Eric Anholt} /* anonymous namespace */
106c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
107c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry/**
1087a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * Replace any declaration of gl_ClipDistance as an array of floats with a
1097a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * declaration of gl_ClipDistanceMESA as an array of vec4's.
110c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry */
111c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berryir_visitor_status
112dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlielower_distance_visitor::visit(ir_variable *ir)
113c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry{
11454f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler   ir_variable **old_var;
11554f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler   ir_variable **new_var;
11654f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler
1177a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie   if (!ir->name || strcmp(ir->name, "gl_ClipDistance") != 0)
118c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      return visit_continue;
1198f758b0b92f6105c9615740096fa1206befe343bBryan Cain   assert (ir->type->is_array());
1208f758b0b92f6105c9615740096fa1206befe343bBryan Cain
12154f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler   if (ir->data.mode == ir_var_shader_out) {
122dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie      if (this->old_distance_out_var)
12354f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler         return visit_continue;
124dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie      old_var = &old_distance_out_var;
125dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie      new_var = &new_distance_out_var;
12654f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler   } else if (ir->data.mode == ir_var_shader_in) {
127dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie      if (this->old_distance_in_var)
1288f758b0b92f6105c9615740096fa1206befe343bBryan Cain         return visit_continue;
129dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie      old_var = &old_distance_in_var;
130dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie      new_var = &new_distance_in_var;
13154f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler   } else {
13254f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler      unreachable("not reached");
13354f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler   }
134c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
13554f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler   this->progress = true;
13654f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler
13754f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler   if (!ir->type->fields.array->is_array()) {
1387a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      /* gl_ClipDistance (used for vertex, tessellation evaluation and
1397a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie       * geometry output, and fragment input).
14054f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler       */
14154f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler      assert((ir->data.mode == ir_var_shader_in &&
14254f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler              this->shader_stage == MESA_SHADER_FRAGMENT) ||
14354f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler             (ir->data.mode == ir_var_shader_out &&
14454f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler              (this->shader_stage == MESA_SHADER_VERTEX ||
14554f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler               this->shader_stage == MESA_SHADER_TESS_EVAL ||
14654f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler               this->shader_stage == MESA_SHADER_GEOMETRY)));
14754f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler
14854f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler      *old_var = ir;
149d67515b7be1ebd9482970ac1867ee4e9bbbf96d5Timothy Arceri      assert (ir->type->fields.array == glsl_type::float_type);
1507a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      unsigned new_size = (ir->type->array_size() + 3) / 4;
1517a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie
1527a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      /* Clone the old var so that we inherit all of its properties */
1537a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      *new_var = ir->clone(ralloc_parent(ir), NULL);
1547a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie
1557a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      /* And change the properties that we need to change */
1567a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      (*new_var)->name = ralloc_strdup(*new_var, "gl_ClipDistanceMESA");
1577a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      (*new_var)->type = glsl_type::get_array_instance(glsl_type::vec4_type,
1587a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie                                                       new_size);
1597a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      (*new_var)->data.max_array_access = ir->data.max_array_access / 4;
1607a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie
1617a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      ir->replace_with(*new_var);
1628f758b0b92f6105c9615740096fa1206befe343bBryan Cain   } else {
1637a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      /* 2D gl_ClipDistance (used for tessellation control, tessellation
1647a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie       * evaluation and geometry input, and tessellation control output).
16554f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler       */
16654f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler      assert((ir->data.mode == ir_var_shader_in &&
16754f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler              (this->shader_stage == MESA_SHADER_GEOMETRY ||
16854f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler               this->shader_stage == MESA_SHADER_TESS_EVAL)) ||
16954f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler             this->shader_stage == MESA_SHADER_TESS_CTRL);
1708f758b0b92f6105c9615740096fa1206befe343bBryan Cain
17154f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler      *old_var = ir;
172d67515b7be1ebd9482970ac1867ee4e9bbbf96d5Timothy Arceri      assert (ir->type->fields.array->fields.array == glsl_type::float_type);
1737a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      unsigned new_size = (ir->type->fields.array->array_size() + 3) / 4;
1747a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie
1757a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      /* Clone the old var so that we inherit all of its properties */
1767a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      *new_var = ir->clone(ralloc_parent(ir), NULL);
1777a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie
1787a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      /* And change the properties that we need to change */
1797a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      (*new_var)->name = ralloc_strdup(*new_var, "gl_ClipDistanceMESA");
1807a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      (*new_var)->type = glsl_type::get_array_instance(
1817a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie         glsl_type::get_array_instance(glsl_type::vec4_type,
1827a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie            new_size),
1837a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie         ir->type->array_size());
1847a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      (*new_var)->data.max_array_access = ir->data.max_array_access / 4;
1857a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie
1867a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      ir->replace_with(*new_var);
187c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   }
18854f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler
189c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   return visit_continue;
190c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry}
191c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
192c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
193c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry/**
1947a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * Create the necessary GLSL rvalues to index into gl_ClipDistanceMESA based
195c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * on the rvalue previously used to index into gl_ClipDistance.
196c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry *
1977a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * \param array_index Selects one of the vec4's in gl_ClipDistanceMESA
198c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * \param swizzle_index Selects a component within the vec4 selected by
199c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry *        array_index.
200c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry */
201c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berryvoid
202dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlielower_distance_visitor::create_indices(ir_rvalue *old_index,
203c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry                                            ir_rvalue *&array_index,
204c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry                                            ir_rvalue *&swizzle_index)
205c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry{
206c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   void *ctx = ralloc_parent(old_index);
207c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
208c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   /* Make sure old_index is a signed int so that the bitwise "shift" and
209c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry    * "and" operations below type check properly.
210c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry    */
211c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   if (old_index->type != glsl_type::int_type) {
212c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      assert (old_index->type == glsl_type::uint_type);
213c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      old_index = new(ctx) ir_expression(ir_unop_u2i, old_index);
214c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   }
215c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
216c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   ir_constant *old_index_constant = old_index->constant_expression_value();
217c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   if (old_index_constant) {
2187a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      /* gl_ClipDistance is being accessed via a constant index.  Don't bother
2197a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie       * creating expressions to calculate the lowered indices.  Just create
2207a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie       * constants.
221c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry       */
222c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      int const_val = old_index_constant->get_int_component(0);
2237a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      array_index = new(ctx) ir_constant(const_val / 4);
2247a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      swizzle_index = new(ctx) ir_constant(const_val % 4);
225c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   } else {
226c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      /* Create a variable to hold the value of old_index (so that we
227c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry       * don't compute it twice).
228c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry       */
229c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      ir_variable *old_index_var = new(ctx) ir_variable(
230dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie         glsl_type::int_type, "distance_index", ir_var_temporary);
231c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      this->base_ir->insert_before(old_index_var);
232c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      this->base_ir->insert_before(new(ctx) ir_assignment(
233c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry         new(ctx) ir_dereference_variable(old_index_var), old_index));
234c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
2357a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      /* Create the expression distance_index / 4.  Do this as a bit
2367a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie       * shift because that's likely to be more efficient.
237c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry       */
238c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      array_index = new(ctx) ir_expression(
239c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry         ir_binop_rshift, new(ctx) ir_dereference_variable(old_index_var),
240c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry         new(ctx) ir_constant(2));
241c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
2427a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      /* Create the expression distance_index % 4.  Do this as a bitwise
2437a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie       * AND because that's likely to be more efficient.
244c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry       */
245c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      swizzle_index = new(ctx) ir_expression(
246c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry         ir_binop_bit_and, new(ctx) ir_dereference_variable(old_index_var),
247c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry         new(ctx) ir_constant(3));
248c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   }
249c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry}
250c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
251c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
2528f758b0b92f6105c9615740096fa1206befe343bBryan Cain/**
2538f758b0b92f6105c9615740096fa1206befe343bBryan Cain * Determine whether the given rvalue describes an array of 8 floats that
2548f758b0b92f6105c9615740096fa1206befe343bBryan Cain * needs to be lowered to an array of 2 vec4's; that is, determine whether it
2558f758b0b92f6105c9615740096fa1206befe343bBryan Cain * matches one of the following patterns:
2568f758b0b92f6105c9615740096fa1206befe343bBryan Cain *
2578f758b0b92f6105c9615740096fa1206befe343bBryan Cain * - gl_ClipDistance (if gl_ClipDistance is 1D)
2588f758b0b92f6105c9615740096fa1206befe343bBryan Cain * - gl_ClipDistance[i] (if gl_ClipDistance is 2D)
2598f758b0b92f6105c9615740096fa1206befe343bBryan Cain */
2608f758b0b92f6105c9615740096fa1206befe343bBryan Cainbool
261dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlielower_distance_visitor::is_distance_vec8(ir_rvalue *ir)
2628f758b0b92f6105c9615740096fa1206befe343bBryan Cain{
2637a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie   /* Note that geometry shaders contain gl_ClipDistance both as an input
2647a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie    * (which is a 2D array) and an output (which is a 1D array), so it's
2657a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie    * possible for both this->old_distance_out_var and
266dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie    * this->old_distance_in_var to be non-NULL in the same shader.
2678f758b0b92f6105c9615740096fa1206befe343bBryan Cain    */
2688f758b0b92f6105c9615740096fa1206befe343bBryan Cain
26954f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler   if (!ir->type->is_array())
27054f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler      return false;
27154f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler   if (ir->type->fields.array != glsl_type::float_type)
27254f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler      return false;
27354f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler
274dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie   if (this->old_distance_out_var) {
275dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie      if (ir->variable_referenced() == this->old_distance_out_var)
2768f758b0b92f6105c9615740096fa1206befe343bBryan Cain         return true;
2778f758b0b92f6105c9615740096fa1206befe343bBryan Cain   }
278dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie   if (this->old_distance_in_var) {
27954f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler      assert(this->shader_stage == MESA_SHADER_TESS_CTRL ||
28054f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler             this->shader_stage == MESA_SHADER_TESS_EVAL ||
28154f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler             this->shader_stage == MESA_SHADER_GEOMETRY ||
28254f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler             this->shader_stage == MESA_SHADER_FRAGMENT);
2838f758b0b92f6105c9615740096fa1206befe343bBryan Cain
284dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie      if (ir->variable_referenced() == this->old_distance_in_var)
28554f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler         return true;
2868f758b0b92f6105c9615740096fa1206befe343bBryan Cain   }
2878f758b0b92f6105c9615740096fa1206befe343bBryan Cain   return false;
2888f758b0b92f6105c9615740096fa1206befe343bBryan Cain}
2898f758b0b92f6105c9615740096fa1206befe343bBryan Cain
2908f758b0b92f6105c9615740096fa1206befe343bBryan Cain
2918f758b0b92f6105c9615740096fa1206befe343bBryan Cain/**
292dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie * If the given ir satisfies is_distance_vec8(), return new ir
2938f758b0b92f6105c9615740096fa1206befe343bBryan Cain * representing its lowered equivalent.  That is, map:
2948f758b0b92f6105c9615740096fa1206befe343bBryan Cain *
2958f758b0b92f6105c9615740096fa1206befe343bBryan Cain * - gl_ClipDistance    => gl_ClipDistanceMESA    (if gl_ClipDistance is 1D)
2968f758b0b92f6105c9615740096fa1206befe343bBryan Cain * - gl_ClipDistance[i] => gl_ClipDistanceMESA[i] (if gl_ClipDistance is 2D)
2978f758b0b92f6105c9615740096fa1206befe343bBryan Cain *
2988f758b0b92f6105c9615740096fa1206befe343bBryan Cain * Otherwise return NULL.
2998f758b0b92f6105c9615740096fa1206befe343bBryan Cain */
3008f758b0b92f6105c9615740096fa1206befe343bBryan Cainir_rvalue *
301dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlielower_distance_visitor::lower_distance_vec8(ir_rvalue *ir)
3028f758b0b92f6105c9615740096fa1206befe343bBryan Cain{
30354f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler   if (!ir->type->is_array())
30454f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler      return NULL;
30554f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler   if (ir->type->fields.array != glsl_type::float_type)
30654f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler      return NULL;
30754f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler
30854f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler   ir_variable **new_var = NULL;
309dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie   if (this->old_distance_out_var) {
310dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie      if (ir->variable_referenced() == this->old_distance_out_var)
311dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie         new_var = &this->new_distance_out_var;
31254f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler   }
313dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie   if (this->old_distance_in_var) {
314dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie      if (ir->variable_referenced() == this->old_distance_in_var)
315dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie         new_var = &this->new_distance_in_var;
3168f758b0b92f6105c9615740096fa1206befe343bBryan Cain   }
31754f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler   if (new_var == NULL)
31854f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler      return NULL;
3198f758b0b92f6105c9615740096fa1206befe343bBryan Cain
32054f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler   if (ir->as_dereference_variable()) {
32154f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler      return new(ralloc_parent(ir)) ir_dereference_variable(*new_var);
32254f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler   } else {
3238f758b0b92f6105c9615740096fa1206befe343bBryan Cain      ir_dereference_array *array_ref = ir->as_dereference_array();
32454f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler      assert(array_ref);
32554f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler      assert(array_ref->array->as_dereference_variable());
32654f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler
32754f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler      return new(ralloc_parent(ir))
32854f29502972cdd33302e69e029c8d07fb31b7bdfFabian Bieler         ir_dereference_array(*new_var, array_ref->array_index);
3298f758b0b92f6105c9615740096fa1206befe343bBryan Cain   }
3308f758b0b92f6105c9615740096fa1206befe343bBryan Cain}
3318f758b0b92f6105c9615740096fa1206befe343bBryan Cain
3328f758b0b92f6105c9615740096fa1206befe343bBryan Cain
333065da16508731f6b6a98865f392509be4f9ce07fIan Romanickvoid
334dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlielower_distance_visitor::handle_rvalue(ir_rvalue **rv)
335c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry{
3368f758b0b92f6105c9615740096fa1206befe343bBryan Cain   if (*rv == NULL)
337065da16508731f6b6a98865f392509be4f9ce07fIan Romanick      return;
338c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
339065da16508731f6b6a98865f392509be4f9ce07fIan Romanick   ir_dereference_array *const array_deref = (*rv)->as_dereference_array();
340065da16508731f6b6a98865f392509be4f9ce07fIan Romanick   if (array_deref == NULL)
341065da16508731f6b6a98865f392509be4f9ce07fIan Romanick      return;
342065da16508731f6b6a98865f392509be4f9ce07fIan Romanick
3437a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie   /* Replace any expression that indexes one of the floats in gl_ClipDistance
3447a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie    * with an expression that indexes into one of the vec4's in
3457a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie    * gl_ClipDistanceMESA and accesses the appropriate component.
346065da16508731f6b6a98865f392509be4f9ce07fIan Romanick    */
3478f758b0b92f6105c9615740096fa1206befe343bBryan Cain   ir_rvalue *lowered_vec8 =
348dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie      this->lower_distance_vec8(array_deref->array);
3498f758b0b92f6105c9615740096fa1206befe343bBryan Cain   if (lowered_vec8 != NULL) {
350c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      this->progress = true;
351c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      ir_rvalue *array_index;
352c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      ir_rvalue *swizzle_index;
353065da16508731f6b6a98865f392509be4f9ce07fIan Romanick      this->create_indices(array_deref->array_index, array_index, swizzle_index);
354065da16508731f6b6a98865f392509be4f9ce07fIan Romanick      void *mem_ctx = ralloc_parent(array_deref);
355c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick
3568f758b0b92f6105c9615740096fa1206befe343bBryan Cain      ir_dereference_array *const new_array_deref =
3578f758b0b92f6105c9615740096fa1206befe343bBryan Cain         new(mem_ctx) ir_dereference_array(lowered_vec8, array_index);
358c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick
359c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick      ir_expression *const expr =
360c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick         new(mem_ctx) ir_expression(ir_binop_vector_extract,
3618f758b0b92f6105c9615740096fa1206befe343bBryan Cain                                    new_array_deref,
362c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick                                    swizzle_index);
363c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick
364c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick      *rv = expr;
365c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   }
366c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry}
367c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
368c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanickvoid
369dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlielower_distance_visitor::fix_lhs(ir_assignment *ir)
370c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick{
371c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick   if (ir->lhs->ir_type == ir_type_expression) {
372c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick      void *mem_ctx = ralloc_parent(ir);
373c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick      ir_expression *const expr = (ir_expression *) ir->lhs;
374c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick
375c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick      /* The expression must be of the form:
376c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick       *
377c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick       *     (vector_extract gl_ClipDistanceMESA[i], j).
378c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick       */
379c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick      assert(expr->operation == ir_binop_vector_extract);
380c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick      assert(expr->operands[0]->ir_type == ir_type_dereference_array);
381c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick      assert(expr->operands[0]->type == glsl_type::vec4_type);
382c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick
383c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick      ir_dereference *const new_lhs = (ir_dereference *) expr->operands[0];
384c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick      ir->rhs = new(mem_ctx) ir_expression(ir_triop_vector_insert,
385c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick					   glsl_type::vec4_type,
386c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick					   new_lhs->clone(mem_ctx, NULL),
387c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick					   ir->rhs,
388c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick					   expr->operands[1]);
389c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick      ir->set_lhs(new_lhs);
390c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick      ir->write_mask = WRITEMASK_XYZW;
391c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick   }
392c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick}
393c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
394c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry/**
3957a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * Replace any assignment having the 1D gl_ClipDistance (undereferenced) as
3968f758b0b92f6105c9615740096fa1206befe343bBryan Cain * its LHS or RHS with a sequence of assignments, one for each component of
3978f758b0b92f6105c9615740096fa1206befe343bBryan Cain * the array.  Each of these assignments is lowered to refer to
3987a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * gl_ClipDistanceMESA as appropriate.
3998f758b0b92f6105c9615740096fa1206befe343bBryan Cain *
4007a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * We need to do a similar replacement for 2D gl_ClipDistance, however since
4018f758b0b92f6105c9615740096fa1206befe343bBryan Cain * it's an input, the only case we need to address is where a 1D slice of it
4028f758b0b92f6105c9615740096fa1206befe343bBryan Cain * is the entire RHS of an assignment, e.g.:
4038f758b0b92f6105c9615740096fa1206befe343bBryan Cain *
4048f758b0b92f6105c9615740096fa1206befe343bBryan Cain *     foo = gl_in[i].gl_ClipDistance
405c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry */
406c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berryir_visitor_status
407dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlielower_distance_visitor::visit_leave(ir_assignment *ir)
408c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry{
4099dfcb05fa649ee7a573eab3d16851ebd4cb96010Paul Berry   /* First invoke the base class visitor.  This causes handle_rvalue() to be
4109dfcb05fa649ee7a573eab3d16851ebd4cb96010Paul Berry    * called on ir->rhs and ir->condition.
4119dfcb05fa649ee7a573eab3d16851ebd4cb96010Paul Berry    */
4129dfcb05fa649ee7a573eab3d16851ebd4cb96010Paul Berry   ir_rvalue_visitor::visit_leave(ir);
4139dfcb05fa649ee7a573eab3d16851ebd4cb96010Paul Berry
414dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie   if (this->is_distance_vec8(ir->lhs) ||
415dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie       this->is_distance_vec8(ir->rhs)) {
4167a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      /* LHS or RHS of the assignment is the entire 1D gl_ClipDistance array
4177a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie       * (or a 1D slice of a 2D gl_ClipDistance input array).  Since we are
4187a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie       * reshaping gl_ClipDistance from an array of floats to an array of
4198f758b0b92f6105c9615740096fa1206befe343bBryan Cain       * vec4's, this isn't going to work as a bulk assignment anymore, so
4208f758b0b92f6105c9615740096fa1206befe343bBryan Cain       * unroll it to element-by-element assignments and lower each of them.
421c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry       *
422c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry       * Note: to unroll into element-by-element assignments, we need to make
423d13fbeea9634b5e3cae0213cc61706ffc316b64fIan Romanick       * clones of the LHS and RHS.  This is safe because expressions and
424d13fbeea9634b5e3cae0213cc61706ffc316b64fIan Romanick       * l-values are side-effect free.
425c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry       */
426c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      void *ctx = ralloc_parent(ir);
4278f758b0b92f6105c9615740096fa1206befe343bBryan Cain      int array_size = ir->lhs->type->array_size();
428c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      for (int i = 0; i < array_size; ++i) {
429c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry         ir_dereference_array *new_lhs = new(ctx) ir_dereference_array(
430c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry            ir->lhs->clone(ctx, NULL), new(ctx) ir_constant(i));
431c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry         ir_dereference_array *new_rhs = new(ctx) ir_dereference_array(
432c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry            ir->rhs->clone(ctx, NULL), new(ctx) ir_constant(i));
433065da16508731f6b6a98865f392509be4f9ce07fIan Romanick         this->handle_rvalue((ir_rvalue **) &new_rhs);
434c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick
435c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick         /* Handle the LHS after creating the new assignment.  This must
436c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick          * happen in this order because handle_rvalue may replace the old LHS
437c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick          * with an ir_expression of ir_binop_vector_extract.  Since this is
438c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick          * not a valide l-value, this will cause an assertion in the
439c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick          * ir_assignment constructor to fail.
440c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick          *
441c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick          * If this occurs, replace the mangled LHS with a dereference of the
442c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick          * vector, and replace the RHS with an ir_triop_vector_insert.
443c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick          */
444c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick         ir_assignment *const assign = new(ctx) ir_assignment(new_lhs, new_rhs);
445c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick         this->handle_rvalue((ir_rvalue **) &assign->lhs);
446c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick         this->fix_lhs(assign);
447c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick
448c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick         this->base_ir->insert_before(assign);
449c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      }
450c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      ir->remove();
451065da16508731f6b6a98865f392509be4f9ce07fIan Romanick
452065da16508731f6b6a98865f392509be4f9ce07fIan Romanick      return visit_continue;
453c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   }
454c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
455065da16508731f6b6a98865f392509be4f9ce07fIan Romanick   /* Handle the LHS as if it were an r-value.  Normally
456065da16508731f6b6a98865f392509be4f9ce07fIan Romanick    * rvalue_visit(ir_assignment *) only visits the RHS, but we need to lower
457065da16508731f6b6a98865f392509be4f9ce07fIan Romanick    * expressions in the LHS as well.
458c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick    *
459c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick    * This may cause the LHS to get replaced with an ir_expression of
460c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick    * ir_binop_vector_extract.  If this occurs, replace it with a dereference
461c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick    * of the vector, and replace the RHS with an ir_triop_vector_insert.
462065da16508731f6b6a98865f392509be4f9ce07fIan Romanick    */
463065da16508731f6b6a98865f392509be4f9ce07fIan Romanick   handle_rvalue((ir_rvalue **)&ir->lhs);
464c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick   this->fix_lhs(ir);
465c3bb07f875075b08a0b13f47be95e0c49edb6555Ian Romanick
466065da16508731f6b6a98865f392509be4f9ce07fIan Romanick   return rvalue_visit(ir);
467c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry}
468c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
469c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
470c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry/**
471c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * Set up base_ir properly and call visit_leave() on a newly created
472c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * ir_assignment node.  This is used in cases where we have to insert an
473c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * ir_assignment in a place where we know the hierarchical visitor won't see
474c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry * it.
475c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry */
476c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berryvoid
477dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlielower_distance_visitor::visit_new_assignment(ir_assignment *ir)
478c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry{
479c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   ir_instruction *old_base_ir = this->base_ir;
480c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   this->base_ir = ir;
481c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   ir->accept(this);
482c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   this->base_ir = old_base_ir;
483c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry}
484c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
485c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
486c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry/**
4877a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * If a 1D gl_ClipDistance variable appears as an argument in an ir_call
4888f758b0b92f6105c9615740096fa1206befe343bBryan Cain * expression, replace it with a temporary variable, and make sure the ir_call
4898f758b0b92f6105c9615740096fa1206befe343bBryan Cain * is preceded and/or followed by assignments that copy the contents of the
4907a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * temporary variable to and/or from gl_ClipDistance.  Each of these
4917a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * assignments is then lowered to refer to gl_ClipDistanceMESA.
4928f758b0b92f6105c9615740096fa1206befe343bBryan Cain *
4937a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie * We need to do a similar replacement for 2D gl_ClipDistance, however since
4948f758b0b92f6105c9615740096fa1206befe343bBryan Cain * it's an input, the only case we need to address is where a 1D slice of it
4958f758b0b92f6105c9615740096fa1206befe343bBryan Cain * is passed as an "in" parameter to an ir_call, e.g.:
4968f758b0b92f6105c9615740096fa1206befe343bBryan Cain *
4978f758b0b92f6105c9615740096fa1206befe343bBryan Cain *     foo(gl_in[i].gl_ClipDistance)
498c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry */
499c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berryir_visitor_status
500dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlielower_distance_visitor::visit_leave(ir_call *ir)
501c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry{
502c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   void *ctx = ralloc_parent(ir);
503c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
50482065fa20ee3f2880a070f1f4f75509b910ceddeKenneth Graunke   const exec_node *formal_param_node = ir->callee->parameters.head;
505c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   const exec_node *actual_param_node = ir->actual_parameters.head;
506c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   while (!actual_param_node->is_tail_sentinel()) {
507c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      ir_variable *formal_param = (ir_variable *) formal_param_node;
508c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      ir_rvalue *actual_param = (ir_rvalue *) actual_param_node;
509c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
510c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      /* Advance formal_param_node and actual_param_node now so that we can
511c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry       * safely replace actual_param with another node, if necessary, below.
512c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry       */
513c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      formal_param_node = formal_param_node->next;
514c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      actual_param_node = actual_param_node->next;
515c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
516dd3390e12fa8c8fa127013a9fc7c83069747aad2Dave Airlie      if (this->is_distance_vec8(actual_param)) {
5177a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie         /* User is trying to pass the whole 1D gl_ClipDistance array (or a 1D
5187a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie          * slice of a 2D gl_ClipDistance array) to a function call.  Since we
5197a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie          * are reshaping gl_ClipDistance from an array of floats to an array
5208f758b0b92f6105c9615740096fa1206befe343bBryan Cain          * of vec4's, this isn't going to work anymore, so use a temporary
5218f758b0b92f6105c9615740096fa1206befe343bBryan Cain          * array instead.
522c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry          */
5237a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie         ir_variable *temp_clip_distance = new(ctx) ir_variable(
5247a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie            actual_param->type, "temp_clip_distance", ir_var_temporary);
5257a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie         this->base_ir->insert_before(temp_clip_distance);
526c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry         actual_param->replace_with(
5277a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie            new(ctx) ir_dereference_variable(temp_clip_distance));
52833ee2c67c0a4e8f2fefbf37dacabd14918060af5Tapani Pälli         if (formal_param->data.mode == ir_var_function_in
52933ee2c67c0a4e8f2fefbf37dacabd14918060af5Tapani Pälli             || formal_param->data.mode == ir_var_function_inout) {
5307a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie            /* Copy from gl_ClipDistance to the temporary before the call.
531c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry             * Since we are going to insert this copy before the current
532c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry             * instruction, we need to visit it afterwards to make sure it
533c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry             * gets lowered.
534c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry             */
535c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry            ir_assignment *new_assignment = new(ctx) ir_assignment(
5367a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie               new(ctx) ir_dereference_variable(temp_clip_distance),
5378f758b0b92f6105c9615740096fa1206befe343bBryan Cain               actual_param->clone(ctx, NULL));
538c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry            this->base_ir->insert_before(new_assignment);
539c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry            this->visit_new_assignment(new_assignment);
540c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry         }
54133ee2c67c0a4e8f2fefbf37dacabd14918060af5Tapani Pälli         if (formal_param->data.mode == ir_var_function_out
54233ee2c67c0a4e8f2fefbf37dacabd14918060af5Tapani Pälli             || formal_param->data.mode == ir_var_function_inout) {
5437a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie            /* Copy from the temporary to gl_ClipDistance after the call.
544c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry             * Since visit_list_elements() has already decided which
545c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry             * instruction it's going to visit next, we need to visit
546c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry             * afterwards to make sure it gets lowered.
547c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry             */
548c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry            ir_assignment *new_assignment = new(ctx) ir_assignment(
5498f758b0b92f6105c9615740096fa1206befe343bBryan Cain               actual_param->clone(ctx, NULL),
5507a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie               new(ctx) ir_dereference_variable(temp_clip_distance));
551c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry            this->base_ir->insert_after(new_assignment);
552c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry            this->visit_new_assignment(new_assignment);
553c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry         }
554c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry      }
555c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry   }
556c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
557065da16508731f6b6a98865f392509be4f9ce07fIan Romanick   return rvalue_visit(ir);
558c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry}
559c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
560c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
561c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berrybool
5627a6d55826e94e6c18471c671736f706bddfdb582Dave Airlielower_clip_distance(gl_shader *shader)
563c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry{
5647a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie   lower_distance_visitor v(shader->Stage);
565c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
5667a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie   visit_list_elements(&v, shader->ir);
56718392443d448e083187d085965375e7de910b18aPaul Berry
5687a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie   if (v.new_distance_out_var)
5697a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      shader->symbols->add_variable(v.new_distance_out_var);
5707a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie   if (v.new_distance_in_var)
5717a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie      shader->symbols->add_variable(v.new_distance_in_var);
572c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry
5737a6d55826e94e6c18471c671736f706bddfdb582Dave Airlie   return v.progress;
574c06e32596735074536b0e613cbddb1c5fd7b367aPaul Berry}
575