1/*
2 * Copyright © 2011 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 * Software is furnished to do so, subject to the following conditions:
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_varyings_to_packed.cpp
26 *
27 * This lowering pass generates GLSL code that manually packs varyings into
28 * vec4 slots, for the benefit of back-ends that don't support packed varyings
29 * natively.
30 *
31 * For example, the following shader:
32 *
33 *   out mat3x2 foo;  // location=4, location_frac=0
34 *   out vec3 bar[2]; // location=5, location_frac=2
35 *
36 *   main()
37 *   {
38 *     ...
39 *   }
40 *
41 * Is rewritten to:
42 *
43 *   mat3x2 foo;
44 *   vec3 bar[2];
45 *   out vec4 packed4; // location=4, location_frac=0
46 *   out vec4 packed5; // location=5, location_frac=0
47 *   out vec4 packed6; // location=6, location_frac=0
48 *
49 *   main()
50 *   {
51 *     ...
52 *     packed4.xy = foo[0];
53 *     packed4.zw = foo[1];
54 *     packed5.xy = foo[2];
55 *     packed5.zw = bar[0].xy;
56 *     packed6.x = bar[0].z;
57 *     packed6.yzw = bar[1];
58 *   }
59 *
60 * This lowering pass properly handles "double parking" of a varying vector
61 * across two varying slots.  For example, in the code above, two of the
62 * components of bar[0] are stored in packed5, and the remaining component is
63 * stored in packed6.
64 *
65 * Note that in theory, the extra instructions may cause some loss of
66 * performance.  However, hopefully in most cases the performance loss will
67 * either be absorbed by a later optimization pass, or it will be offset by
68 * memory bandwidth savings (because fewer varyings are used).
69 *
70 * This lowering pass also packs flat floats, ints, and uints together, by
71 * using ivec4 as the base type of flat "varyings", and using appropriate
72 * casts to convert floats and uints into ints.
73 *
74 * This lowering pass also handles varyings whose type is a struct or an array
75 * of struct.  Structs are packed in order and with no gaps, so there may be a
76 * performance penalty due to structure elements being double-parked.
77 *
78 * Lowering of geometry shader inputs is slightly more complex, since geometry
79 * inputs are always arrays, so we need to lower arrays to arrays.  For
80 * example, the following input:
81 *
82 *   in struct Foo {
83 *     float f;
84 *     vec3 v;
85 *     vec2 a[2];
86 *   } arr[3];         // location=4, location_frac=0
87 *
88 * Would get lowered like this if it occurred in a fragment shader:
89 *
90 *   struct Foo {
91 *     float f;
92 *     vec3 v;
93 *     vec2 a[2];
94 *   } arr[3];
95 *   in vec4 packed4;  // location=4, location_frac=0
96 *   in vec4 packed5;  // location=5, location_frac=0
97 *   in vec4 packed6;  // location=6, location_frac=0
98 *   in vec4 packed7;  // location=7, location_frac=0
99 *   in vec4 packed8;  // location=8, location_frac=0
100 *   in vec4 packed9;  // location=9, location_frac=0
101 *
102 *   main()
103 *   {
104 *     arr[0].f = packed4.x;
105 *     arr[0].v = packed4.yzw;
106 *     arr[0].a[0] = packed5.xy;
107 *     arr[0].a[1] = packed5.zw;
108 *     arr[1].f = packed6.x;
109 *     arr[1].v = packed6.yzw;
110 *     arr[1].a[0] = packed7.xy;
111 *     arr[1].a[1] = packed7.zw;
112 *     arr[2].f = packed8.x;
113 *     arr[2].v = packed8.yzw;
114 *     arr[2].a[0] = packed9.xy;
115 *     arr[2].a[1] = packed9.zw;
116 *     ...
117 *   }
118 *
119 * But it would get lowered like this if it occurred in a geometry shader:
120 *
121 *   struct Foo {
122 *     float f;
123 *     vec3 v;
124 *     vec2 a[2];
125 *   } arr[3];
126 *   in vec4 packed4[3];  // location=4, location_frac=0
127 *   in vec4 packed5[3];  // location=5, location_frac=0
128 *
129 *   main()
130 *   {
131 *     arr[0].f = packed4[0].x;
132 *     arr[0].v = packed4[0].yzw;
133 *     arr[0].a[0] = packed5[0].xy;
134 *     arr[0].a[1] = packed5[0].zw;
135 *     arr[1].f = packed4[1].x;
136 *     arr[1].v = packed4[1].yzw;
137 *     arr[1].a[0] = packed5[1].xy;
138 *     arr[1].a[1] = packed5[1].zw;
139 *     arr[2].f = packed4[2].x;
140 *     arr[2].v = packed4[2].yzw;
141 *     arr[2].a[0] = packed5[2].xy;
142 *     arr[2].a[1] = packed5[2].zw;
143 *     ...
144 *   }
145 */
146
147#include "glsl_symbol_table.h"
148#include "ir.h"
149#include "ir_builder.h"
150#include "ir_optimization.h"
151#include "program/prog_instruction.h"
152#include "main/mtypes.h"
153
154using namespace ir_builder;
155
156namespace {
157
158/**
159 * Visitor that performs varying packing.  For each varying declared in the
160 * shader, this visitor determines whether it needs to be packed.  If so, it
161 * demotes it to an ordinary global, creates new packed varyings, and
162 * generates assignments to convert between the original varying and the
163 * packed varying.
164 */
165class lower_packed_varyings_visitor
166{
167public:
168   lower_packed_varyings_visitor(void *mem_ctx,
169                                 unsigned locations_used,
170                                 const uint8_t *components,
171                                 ir_variable_mode mode,
172                                 unsigned gs_input_vertices,
173                                 exec_list *out_instructions,
174                                 exec_list *out_variables,
175                                 bool disable_varying_packing,
176                                 bool disable_xfb_packing,
177                                 bool xfb_enabled);
178
179   void run(struct gl_linked_shader *shader);
180
181private:
182   void bitwise_assign_pack(ir_rvalue *lhs, ir_rvalue *rhs);
183   void bitwise_assign_unpack(ir_rvalue *lhs, ir_rvalue *rhs);
184   unsigned lower_rvalue(ir_rvalue *rvalue, unsigned fine_location,
185                         ir_variable *unpacked_var, const char *name,
186                         bool gs_input_toplevel, unsigned vertex_index);
187   unsigned lower_arraylike(ir_rvalue *rvalue, unsigned array_size,
188                            unsigned fine_location,
189                            ir_variable *unpacked_var, const char *name,
190                            bool gs_input_toplevel, unsigned vertex_index);
191   ir_dereference *get_packed_varying_deref(unsigned location,
192                                            ir_variable *unpacked_var,
193                                            const char *name,
194                                            unsigned vertex_index);
195   bool needs_lowering(ir_variable *var);
196
197   /**
198    * Memory context used to allocate new instructions for the shader.
199    */
200   void * const mem_ctx;
201
202   /**
203    * Number of generic varying slots which are used by this shader.  This is
204    * used to allocate temporary intermediate data structures.  If any varying
205    * used by this shader has a location greater than or equal to
206    * VARYING_SLOT_VAR0 + locations_used, an assertion will fire.
207    */
208   const unsigned locations_used;
209
210   const uint8_t* components;
211
212   /**
213    * Array of pointers to the packed varyings that have been created for each
214    * generic varying slot.  NULL entries in this array indicate varying slots
215    * for which a packed varying has not been created yet.
216    */
217   ir_variable **packed_varyings;
218
219   /**
220    * Type of varying which is being lowered in this pass (either
221    * ir_var_shader_in or ir_var_shader_out).
222    */
223   const ir_variable_mode mode;
224
225   /**
226    * If we are currently lowering geometry shader inputs, the number of input
227    * vertices the geometry shader accepts.  Otherwise zero.
228    */
229   const unsigned gs_input_vertices;
230
231   /**
232    * Exec list into which the visitor should insert the packing instructions.
233    * Caller provides this list; it should insert the instructions into the
234    * appropriate place in the shader once the visitor has finished running.
235    */
236   exec_list *out_instructions;
237
238   /**
239    * Exec list into which the visitor should insert any new variables.
240    */
241   exec_list *out_variables;
242
243   bool disable_varying_packing;
244   bool disable_xfb_packing;
245   bool xfb_enabled;
246};
247
248} /* anonymous namespace */
249
250lower_packed_varyings_visitor::lower_packed_varyings_visitor(
251      void *mem_ctx, unsigned locations_used, const uint8_t *components,
252      ir_variable_mode mode,
253      unsigned gs_input_vertices, exec_list *out_instructions,
254      exec_list *out_variables, bool disable_varying_packing,
255      bool disable_xfb_packing, bool xfb_enabled)
256   : mem_ctx(mem_ctx),
257     locations_used(locations_used),
258     components(components),
259     packed_varyings((ir_variable **)
260                     rzalloc_array_size(mem_ctx, sizeof(*packed_varyings),
261                                        locations_used)),
262     mode(mode),
263     gs_input_vertices(gs_input_vertices),
264     out_instructions(out_instructions),
265     out_variables(out_variables),
266     disable_varying_packing(disable_varying_packing),
267     disable_xfb_packing(disable_xfb_packing),
268     xfb_enabled(xfb_enabled)
269{
270}
271
272void
273lower_packed_varyings_visitor::run(struct gl_linked_shader *shader)
274{
275   foreach_in_list(ir_instruction, node, shader->ir) {
276      ir_variable *var = node->as_variable();
277      if (var == NULL)
278         continue;
279
280      if (var->data.mode != this->mode ||
281          var->data.location < VARYING_SLOT_VAR0 ||
282          !this->needs_lowering(var))
283         continue;
284
285      /* This lowering pass is only capable of packing floats and ints
286       * together when their interpolation mode is "flat".  Treat integers as
287       * being flat when the interpolation mode is none.
288       */
289      assert(var->data.interpolation == INTERP_MODE_FLAT ||
290             var->data.interpolation == INTERP_MODE_NONE ||
291             !var->type->contains_integer());
292
293      /* Clone the variable for program resource list before
294       * it gets modified and lost.
295       */
296      if (!shader->packed_varyings)
297         shader->packed_varyings = new (shader) exec_list;
298
299      shader->packed_varyings->push_tail(var->clone(shader, NULL));
300
301      /* Change the old varying into an ordinary global. */
302      assert(var->data.mode != ir_var_temporary);
303      var->data.mode = ir_var_auto;
304
305      /* Create a reference to the old varying. */
306      ir_dereference_variable *deref
307         = new(this->mem_ctx) ir_dereference_variable(var);
308
309      /* Recursively pack or unpack it. */
310      this->lower_rvalue(deref, var->data.location * 4 + var->data.location_frac, var,
311                         var->name, this->gs_input_vertices != 0, 0);
312   }
313}
314
315#define SWIZZLE_ZWZW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_Z, SWIZZLE_W)
316
317/**
318 * Make an ir_assignment from \c rhs to \c lhs, performing appropriate
319 * bitcasts if necessary to match up types.
320 *
321 * This function is called when packing varyings.
322 */
323void
324lower_packed_varyings_visitor::bitwise_assign_pack(ir_rvalue *lhs,
325                                                   ir_rvalue *rhs)
326{
327   if (lhs->type->base_type != rhs->type->base_type) {
328      /* Since we only mix types in flat varyings, and we always store flat
329       * varyings as type ivec4, we need only produce conversions from (uint
330       * or float) to int.
331       */
332      assert(lhs->type->base_type == GLSL_TYPE_INT);
333      switch (rhs->type->base_type) {
334      case GLSL_TYPE_UINT:
335         rhs = new(this->mem_ctx)
336            ir_expression(ir_unop_u2i, lhs->type, rhs);
337         break;
338      case GLSL_TYPE_FLOAT:
339         rhs = new(this->mem_ctx)
340            ir_expression(ir_unop_bitcast_f2i, lhs->type, rhs);
341         break;
342      case GLSL_TYPE_DOUBLE:
343         assert(rhs->type->vector_elements <= 2);
344         if (rhs->type->vector_elements == 2) {
345            ir_variable *t = new(mem_ctx) ir_variable(lhs->type, "pack", ir_var_temporary);
346
347            assert(lhs->type->vector_elements == 4);
348            this->out_variables->push_tail(t);
349            this->out_instructions->push_tail(
350                  assign(t, u2i(expr(ir_unop_unpack_double_2x32, swizzle_x(rhs->clone(mem_ctx, NULL)))), 0x3));
351            this->out_instructions->push_tail(
352                  assign(t,  u2i(expr(ir_unop_unpack_double_2x32, swizzle_y(rhs))), 0xc));
353            rhs = deref(t).val;
354         } else {
355            rhs = u2i(expr(ir_unop_unpack_double_2x32, rhs));
356         }
357         break;
358      case GLSL_TYPE_INT64:
359         assert(rhs->type->vector_elements <= 2);
360         if (rhs->type->vector_elements == 2) {
361            ir_variable *t = new(mem_ctx) ir_variable(lhs->type, "pack", ir_var_temporary);
362
363            assert(lhs->type->vector_elements == 4);
364            this->out_variables->push_tail(t);
365            this->out_instructions->push_tail(
366               assign(t, expr(ir_unop_unpack_int_2x32, swizzle_x(rhs->clone(mem_ctx, NULL))), 0x3));
367            this->out_instructions->push_tail(
368               assign(t,  expr(ir_unop_unpack_int_2x32, swizzle_y(rhs)), 0xc));
369            rhs = deref(t).val;
370         } else {
371            rhs = expr(ir_unop_unpack_int_2x32, rhs);
372         }
373         break;
374      case GLSL_TYPE_UINT64:
375         assert(rhs->type->vector_elements <= 2);
376         if (rhs->type->vector_elements == 2) {
377            ir_variable *t = new(mem_ctx) ir_variable(lhs->type, "pack", ir_var_temporary);
378
379            assert(lhs->type->vector_elements == 4);
380            this->out_variables->push_tail(t);
381            this->out_instructions->push_tail(
382                  assign(t, u2i(expr(ir_unop_unpack_uint_2x32, swizzle_x(rhs->clone(mem_ctx, NULL)))), 0x3));
383            this->out_instructions->push_tail(
384                  assign(t,  u2i(expr(ir_unop_unpack_uint_2x32, swizzle_y(rhs))), 0xc));
385            rhs = deref(t).val;
386         } else {
387            rhs = u2i(expr(ir_unop_unpack_uint_2x32, rhs));
388         }
389         break;
390      case GLSL_TYPE_SAMPLER:
391         rhs = u2i(expr(ir_unop_unpack_sampler_2x32, rhs));
392         break;
393      case GLSL_TYPE_IMAGE:
394         rhs = u2i(expr(ir_unop_unpack_image_2x32, rhs));
395         break;
396      default:
397         assert(!"Unexpected type conversion while lowering varyings");
398         break;
399      }
400   }
401   this->out_instructions->push_tail(new (this->mem_ctx) ir_assignment(lhs, rhs));
402}
403
404
405/**
406 * Make an ir_assignment from \c rhs to \c lhs, performing appropriate
407 * bitcasts if necessary to match up types.
408 *
409 * This function is called when unpacking varyings.
410 */
411void
412lower_packed_varyings_visitor::bitwise_assign_unpack(ir_rvalue *lhs,
413                                                     ir_rvalue *rhs)
414{
415   if (lhs->type->base_type != rhs->type->base_type) {
416      /* Since we only mix types in flat varyings, and we always store flat
417       * varyings as type ivec4, we need only produce conversions from int to
418       * (uint or float).
419       */
420      assert(rhs->type->base_type == GLSL_TYPE_INT);
421      switch (lhs->type->base_type) {
422      case GLSL_TYPE_UINT:
423         rhs = new(this->mem_ctx)
424            ir_expression(ir_unop_i2u, lhs->type, rhs);
425         break;
426      case GLSL_TYPE_FLOAT:
427         rhs = new(this->mem_ctx)
428            ir_expression(ir_unop_bitcast_i2f, lhs->type, rhs);
429         break;
430      case GLSL_TYPE_DOUBLE:
431         assert(lhs->type->vector_elements <= 2);
432         if (lhs->type->vector_elements == 2) {
433            ir_variable *t = new(mem_ctx) ir_variable(lhs->type, "unpack", ir_var_temporary);
434            assert(rhs->type->vector_elements == 4);
435            this->out_variables->push_tail(t);
436            this->out_instructions->push_tail(
437                  assign(t, expr(ir_unop_pack_double_2x32, i2u(swizzle_xy(rhs->clone(mem_ctx, NULL)))), 0x1));
438            this->out_instructions->push_tail(
439                  assign(t, expr(ir_unop_pack_double_2x32, i2u(swizzle(rhs->clone(mem_ctx, NULL), SWIZZLE_ZWZW, 2))), 0x2));
440            rhs = deref(t).val;
441         } else {
442            rhs = expr(ir_unop_pack_double_2x32, i2u(rhs));
443         }
444         break;
445      case GLSL_TYPE_INT64:
446         assert(lhs->type->vector_elements <= 2);
447         if (lhs->type->vector_elements == 2) {
448            ir_variable *t = new(mem_ctx) ir_variable(lhs->type, "unpack", ir_var_temporary);
449            assert(rhs->type->vector_elements == 4);
450            this->out_variables->push_tail(t);
451            this->out_instructions->push_tail(
452                  assign(t, expr(ir_unop_pack_int_2x32, swizzle_xy(rhs->clone(mem_ctx, NULL))), 0x1));
453            this->out_instructions->push_tail(
454                  assign(t, expr(ir_unop_pack_int_2x32, swizzle(rhs->clone(mem_ctx, NULL), SWIZZLE_ZWZW, 2)), 0x2));
455            rhs = deref(t).val;
456         } else {
457            rhs = expr(ir_unop_pack_int_2x32, rhs);
458         }
459         break;
460      case GLSL_TYPE_UINT64:
461         assert(lhs->type->vector_elements <= 2);
462         if (lhs->type->vector_elements == 2) {
463            ir_variable *t = new(mem_ctx) ir_variable(lhs->type, "unpack", ir_var_temporary);
464            assert(rhs->type->vector_elements == 4);
465            this->out_variables->push_tail(t);
466            this->out_instructions->push_tail(
467                  assign(t, expr(ir_unop_pack_uint_2x32, i2u(swizzle_xy(rhs->clone(mem_ctx, NULL)))), 0x1));
468            this->out_instructions->push_tail(
469                  assign(t, expr(ir_unop_pack_uint_2x32, i2u(swizzle(rhs->clone(mem_ctx, NULL), SWIZZLE_ZWZW, 2))), 0x2));
470            rhs = deref(t).val;
471         } else {
472            rhs = expr(ir_unop_pack_uint_2x32, i2u(rhs));
473         }
474         break;
475      case GLSL_TYPE_SAMPLER:
476         rhs = new(mem_ctx)
477            ir_expression(ir_unop_pack_sampler_2x32, lhs->type, i2u(rhs));
478         break;
479      case GLSL_TYPE_IMAGE:
480         rhs = new(mem_ctx)
481            ir_expression(ir_unop_pack_image_2x32, lhs->type, i2u(rhs));
482         break;
483      default:
484         assert(!"Unexpected type conversion while lowering varyings");
485         break;
486      }
487   }
488   this->out_instructions->push_tail(new(this->mem_ctx) ir_assignment(lhs, rhs));
489}
490
491
492/**
493 * Recursively pack or unpack the given varying (or portion of a varying) by
494 * traversing all of its constituent vectors.
495 *
496 * \param fine_location is the location where the first constituent vector
497 * should be packed--the word "fine" indicates that this location is expressed
498 * in multiples of a float, rather than multiples of a vec4 as is used
499 * elsewhere in Mesa.
500 *
501 * \param gs_input_toplevel should be set to true if we are lowering geometry
502 * shader inputs, and we are currently lowering the whole input variable
503 * (i.e. we are lowering the array whose index selects the vertex).
504 *
505 * \param vertex_index: if we are lowering geometry shader inputs, and the
506 * level of the array that we are currently lowering is *not* the top level,
507 * then this indicates which vertex we are currently lowering.  Otherwise it
508 * is ignored.
509 *
510 * \return the location where the next constituent vector (after this one)
511 * should be packed.
512 */
513unsigned
514lower_packed_varyings_visitor::lower_rvalue(ir_rvalue *rvalue,
515                                            unsigned fine_location,
516                                            ir_variable *unpacked_var,
517                                            const char *name,
518                                            bool gs_input_toplevel,
519                                            unsigned vertex_index)
520{
521   unsigned dmul = rvalue->type->is_64bit() ? 2 : 1;
522   /* When gs_input_toplevel is set, we should be looking at a geometry shader
523    * input array.
524    */
525   assert(!gs_input_toplevel || rvalue->type->is_array());
526
527   if (rvalue->type->is_struct()) {
528      for (unsigned i = 0; i < rvalue->type->length; i++) {
529         if (i != 0)
530            rvalue = rvalue->clone(this->mem_ctx, NULL);
531         const char *field_name = rvalue->type->fields.structure[i].name;
532         ir_dereference_record *dereference_record = new(this->mem_ctx)
533            ir_dereference_record(rvalue, field_name);
534         char *deref_name
535            = ralloc_asprintf(this->mem_ctx, "%s.%s", name, field_name);
536         fine_location = this->lower_rvalue(dereference_record, fine_location,
537                                            unpacked_var, deref_name, false,
538                                            vertex_index);
539      }
540      return fine_location;
541   } else if (rvalue->type->is_array()) {
542      /* Arrays are packed/unpacked by considering each array element in
543       * sequence.
544       */
545      return this->lower_arraylike(rvalue, rvalue->type->array_size(),
546                                   fine_location, unpacked_var, name,
547                                   gs_input_toplevel, vertex_index);
548   } else if (rvalue->type->is_matrix()) {
549      /* Matrices are packed/unpacked by considering each column vector in
550       * sequence.
551       */
552      return this->lower_arraylike(rvalue, rvalue->type->matrix_columns,
553                                   fine_location, unpacked_var, name,
554                                   false, vertex_index);
555   } else if (rvalue->type->vector_elements * dmul +
556              fine_location % 4 > 4) {
557      /* We don't have code to split up 64bit variable between two
558       * varying slots, instead we add padding if necessary.
559       */
560      unsigned aligned_fine_location = ALIGN_POT(fine_location, dmul);
561      if (aligned_fine_location != fine_location) {
562         return this->lower_rvalue(rvalue, aligned_fine_location,
563                                   unpacked_var, name, false,
564                                   vertex_index);
565      }
566
567      /* This vector is going to be "double parked" across two varying slots,
568       * so handle it as two separate assignments. For doubles, a dvec3/dvec4
569       * can end up being spread over 3 slots. However the second splitting
570       * will happen later, here we just always want to split into 2.
571       */
572      unsigned left_components, right_components;
573      unsigned left_swizzle_values[4] = { 0, 0, 0, 0 };
574      unsigned right_swizzle_values[4] = { 0, 0, 0, 0 };
575      char left_swizzle_name[4] = { 0, 0, 0, 0 };
576      char right_swizzle_name[4] = { 0, 0, 0, 0 };
577
578      left_components = 4 - fine_location % 4;
579      if (rvalue->type->is_64bit()) {
580         left_components /= 2;
581         assert(left_components > 0);
582      }
583      right_components = rvalue->type->vector_elements - left_components;
584
585      for (unsigned i = 0; i < left_components; i++) {
586         left_swizzle_values[i] = i;
587         left_swizzle_name[i] = "xyzw"[i];
588      }
589      for (unsigned i = 0; i < right_components; i++) {
590         right_swizzle_values[i] = i + left_components;
591         right_swizzle_name[i] = "xyzw"[i + left_components];
592      }
593
594      ir_swizzle *right_swizzle = new(this->mem_ctx)
595         ir_swizzle(rvalue->clone(this->mem_ctx, NULL), right_swizzle_values,
596                    right_components);
597      char *right_name
598         = ralloc_asprintf(this->mem_ctx, "%s.%s", name, right_swizzle_name);
599      if (left_components) {
600         char *left_name
601            = ralloc_asprintf(this->mem_ctx, "%s.%s", name, left_swizzle_name);
602         ir_swizzle *left_swizzle = new(this->mem_ctx)
603                                    ir_swizzle(rvalue, left_swizzle_values, left_components);
604         fine_location = this->lower_rvalue(left_swizzle, fine_location,
605                                            unpacked_var, left_name, false,
606                                            vertex_index);
607      } else
608         /* Top up the fine location to the next slot */
609         fine_location++;
610      return this->lower_rvalue(right_swizzle, fine_location, unpacked_var,
611                                right_name, false, vertex_index);
612   } else {
613      /* No special handling is necessary; pack the rvalue into the
614       * varying.
615       */
616      unsigned swizzle_values[4] = { 0, 0, 0, 0 };
617      unsigned components = rvalue->type->vector_elements * dmul;
618      unsigned location = fine_location / 4;
619      unsigned location_frac = fine_location % 4;
620      for (unsigned i = 0; i < components; ++i)
621         swizzle_values[i] = i + location_frac;
622      assert(this->components[location - VARYING_SLOT_VAR0] >= components);
623      ir_dereference *packed_deref =
624         this->get_packed_varying_deref(location, unpacked_var, name,
625                                        vertex_index);
626      if (unpacked_var->data.stream != 0) {
627         assert(unpacked_var->data.stream < 4);
628         ir_variable *packed_var = packed_deref->variable_referenced();
629         for (unsigned i = 0; i < components; ++i) {
630            packed_var->data.stream |=
631               unpacked_var->data.stream << (2 * (location_frac + i));
632         }
633      }
634      ir_swizzle *swizzle = new(this->mem_ctx)
635         ir_swizzle(packed_deref, swizzle_values, components);
636      if (this->mode == ir_var_shader_out) {
637         this->bitwise_assign_pack(swizzle, rvalue);
638      } else {
639         this->bitwise_assign_unpack(rvalue, swizzle);
640      }
641      return fine_location + components;
642   }
643}
644
645/**
646 * Recursively pack or unpack a varying for which we need to iterate over its
647 * constituent elements, accessing each one using an ir_dereference_array.
648 * This takes care of both arrays and matrices, since ir_dereference_array
649 * treats a matrix like an array of its column vectors.
650 *
651 * \param gs_input_toplevel should be set to true if we are lowering geometry
652 * shader inputs, and we are currently lowering the whole input variable
653 * (i.e. we are lowering the array whose index selects the vertex).
654 *
655 * \param vertex_index: if we are lowering geometry shader inputs, and the
656 * level of the array that we are currently lowering is *not* the top level,
657 * then this indicates which vertex we are currently lowering.  Otherwise it
658 * is ignored.
659 */
660unsigned
661lower_packed_varyings_visitor::lower_arraylike(ir_rvalue *rvalue,
662                                               unsigned array_size,
663                                               unsigned fine_location,
664                                               ir_variable *unpacked_var,
665                                               const char *name,
666                                               bool gs_input_toplevel,
667                                               unsigned vertex_index)
668{
669   unsigned dmul = rvalue->type->without_array()->is_64bit() ? 2 : 1;
670   if (array_size * dmul + fine_location % 4 > 4) {
671      fine_location = ALIGN_POT(fine_location, dmul);
672   }
673
674   for (unsigned i = 0; i < array_size; i++) {
675      if (i != 0)
676         rvalue = rvalue->clone(this->mem_ctx, NULL);
677      ir_constant *constant = new(this->mem_ctx) ir_constant(i);
678      ir_dereference_array *dereference_array = new(this->mem_ctx)
679         ir_dereference_array(rvalue, constant);
680      if (gs_input_toplevel) {
681         /* Geometry shader inputs are a special case.  Instead of storing
682          * each element of the array at a different location, all elements
683          * are at the same location, but with a different vertex index.
684          */
685         (void) this->lower_rvalue(dereference_array, fine_location,
686                                   unpacked_var, name, false, i);
687      } else {
688         char *subscripted_name
689            = ralloc_asprintf(this->mem_ctx, "%s[%d]", name, i);
690         fine_location =
691            this->lower_rvalue(dereference_array, fine_location,
692                               unpacked_var, subscripted_name,
693                               false, vertex_index);
694      }
695   }
696   return fine_location;
697}
698
699/**
700 * Retrieve the packed varying corresponding to the given varying location.
701 * If no packed varying has been created for the given varying location yet,
702 * create it and add it to the shader before returning it.
703 *
704 * The newly created varying inherits its interpolation parameters from \c
705 * unpacked_var.  Its base type is ivec4 if we are lowering a flat varying,
706 * vec4 otherwise.
707 *
708 * \param vertex_index: if we are lowering geometry shader inputs, then this
709 * indicates which vertex we are currently lowering.  Otherwise it is ignored.
710 */
711ir_dereference *
712lower_packed_varyings_visitor::get_packed_varying_deref(
713      unsigned location, ir_variable *unpacked_var, const char *name,
714      unsigned vertex_index)
715{
716   unsigned slot = location - VARYING_SLOT_VAR0;
717   assert(slot < locations_used);
718   if (this->packed_varyings[slot] == NULL) {
719      char *packed_name = ralloc_asprintf(this->mem_ctx, "packed:%s", name);
720      const glsl_type *packed_type;
721      assert(components[slot] != 0);
722      if (unpacked_var->is_interpolation_flat())
723         packed_type = glsl_type::get_instance(GLSL_TYPE_INT, components[slot], 1);
724      else
725         packed_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, components[slot], 1);
726      if (this->gs_input_vertices != 0) {
727         packed_type =
728            glsl_type::get_array_instance(packed_type,
729                                          this->gs_input_vertices);
730      }
731      ir_variable *packed_var = new(this->mem_ctx)
732         ir_variable(packed_type, packed_name, this->mode);
733      if (this->gs_input_vertices != 0) {
734         /* Prevent update_array_sizes() from messing with the size of the
735          * array.
736          */
737         packed_var->data.max_array_access = this->gs_input_vertices - 1;
738      }
739      packed_var->data.centroid = unpacked_var->data.centroid;
740      packed_var->data.sample = unpacked_var->data.sample;
741      packed_var->data.patch = unpacked_var->data.patch;
742      packed_var->data.interpolation =
743         packed_type->without_array() == glsl_type::ivec4_type
744         ? unsigned(INTERP_MODE_FLAT) : unpacked_var->data.interpolation;
745      packed_var->data.location = location;
746      packed_var->data.precision = unpacked_var->data.precision;
747      packed_var->data.always_active_io = unpacked_var->data.always_active_io;
748      packed_var->data.stream = 1u << 31;
749      unpacked_var->insert_before(packed_var);
750      this->packed_varyings[slot] = packed_var;
751   } else {
752      ir_variable *var = this->packed_varyings[slot];
753
754      /* The slot needs to be marked as always active if any variable that got
755       * packed there was.
756       */
757      var->data.always_active_io |= unpacked_var->data.always_active_io;
758
759      /* For geometry shader inputs, only update the packed variable name the
760       * first time we visit each component.
761       */
762      if (this->gs_input_vertices == 0 || vertex_index == 0) {
763         if (var->is_name_ralloced())
764            ralloc_asprintf_append((char **) &var->name, ",%s", name);
765         else
766            var->name = ralloc_asprintf(var, "%s,%s", var->name, name);
767      }
768   }
769
770   ir_dereference *deref = new(this->mem_ctx)
771      ir_dereference_variable(this->packed_varyings[slot]);
772   if (this->gs_input_vertices != 0) {
773      /* When lowering GS inputs, the packed variable is an array, so we need
774       * to dereference it using vertex_index.
775       */
776      ir_constant *constant = new(this->mem_ctx) ir_constant(vertex_index);
777      deref = new(this->mem_ctx) ir_dereference_array(deref, constant);
778   }
779   return deref;
780}
781
782bool
783lower_packed_varyings_visitor::needs_lowering(ir_variable *var)
784{
785   /* Things composed of vec4's, varyings with explicitly assigned
786    * locations or varyings marked as must_be_shader_input (which might be used
787    * by interpolateAt* functions) shouldn't be lowered. Everything else can be.
788    */
789   if (var->data.explicit_location || var->data.must_be_shader_input)
790      return false;
791
792   const glsl_type *type = var->type;
793
794   /* Some drivers (e.g. panfrost) don't support packing of transform
795    * feedback varyings.
796    */
797   if (disable_xfb_packing && var->data.is_xfb &&
798       !(type->is_array() || type->is_struct() || type->is_matrix()) &&
799       xfb_enabled)
800      return false;
801
802   /* Override disable_varying_packing if the var is only used by transform
803    * feedback. Also override it if transform feedback is enabled and the
804    * variable is an array, struct or matrix as the elements of these types
805    * will always have the same interpolation and therefore are safe to pack.
806    */
807   if (disable_varying_packing && !var->data.is_xfb_only &&
808       !((type->is_array() || type->is_struct() || type->is_matrix()) &&
809         xfb_enabled))
810      return false;
811
812   type = type->without_array();
813   if (type->vector_elements == 4 && !type->is_64bit())
814      return false;
815   return true;
816}
817
818
819/**
820 * Visitor that splices varying packing code before every use of EmitVertex()
821 * in a geometry shader.
822 */
823class lower_packed_varyings_gs_splicer : public ir_hierarchical_visitor
824{
825public:
826   explicit lower_packed_varyings_gs_splicer(void *mem_ctx,
827                                             const exec_list *instructions);
828
829   virtual ir_visitor_status visit_leave(ir_emit_vertex *ev);
830
831private:
832   /**
833    * Memory context used to allocate new instructions for the shader.
834    */
835   void * const mem_ctx;
836
837   /**
838    * Instructions that should be spliced into place before each EmitVertex()
839    * call.
840    */
841   const exec_list *instructions;
842};
843
844
845lower_packed_varyings_gs_splicer::lower_packed_varyings_gs_splicer(
846      void *mem_ctx, const exec_list *instructions)
847   : mem_ctx(mem_ctx), instructions(instructions)
848{
849}
850
851
852ir_visitor_status
853lower_packed_varyings_gs_splicer::visit_leave(ir_emit_vertex *ev)
854{
855   foreach_in_list(ir_instruction, ir, this->instructions) {
856      ev->insert_before(ir->clone(this->mem_ctx, NULL));
857   }
858   return visit_continue;
859}
860
861/**
862 * Visitor that splices varying packing code before every return.
863 */
864class lower_packed_varyings_return_splicer : public ir_hierarchical_visitor
865{
866public:
867   explicit lower_packed_varyings_return_splicer(void *mem_ctx,
868                                                 const exec_list *instructions);
869
870   virtual ir_visitor_status visit_leave(ir_return *ret);
871
872private:
873   /**
874    * Memory context used to allocate new instructions for the shader.
875    */
876   void * const mem_ctx;
877
878   /**
879    * Instructions that should be spliced into place before each return.
880    */
881   const exec_list *instructions;
882};
883
884
885lower_packed_varyings_return_splicer::lower_packed_varyings_return_splicer(
886      void *mem_ctx, const exec_list *instructions)
887   : mem_ctx(mem_ctx), instructions(instructions)
888{
889}
890
891
892ir_visitor_status
893lower_packed_varyings_return_splicer::visit_leave(ir_return *ret)
894{
895   foreach_in_list(ir_instruction, ir, this->instructions) {
896      ret->insert_before(ir->clone(this->mem_ctx, NULL));
897   }
898   return visit_continue;
899}
900
901void
902lower_packed_varyings(void *mem_ctx, unsigned locations_used,
903                      const uint8_t *components,
904                      ir_variable_mode mode, unsigned gs_input_vertices,
905                      gl_linked_shader *shader, bool disable_varying_packing,
906                      bool disable_xfb_packing, bool xfb_enabled)
907{
908   exec_list *instructions = shader->ir;
909   ir_function *main_func = shader->symbols->get_function("main");
910   exec_list void_parameters;
911   ir_function_signature *main_func_sig
912      = main_func->matching_signature(NULL, &void_parameters, false);
913   exec_list new_instructions, new_variables;
914   lower_packed_varyings_visitor visitor(mem_ctx,
915                                         locations_used,
916                                         components,
917                                         mode,
918                                         gs_input_vertices,
919                                         &new_instructions,
920                                         &new_variables,
921                                         disable_varying_packing,
922                                         disable_xfb_packing,
923                                         xfb_enabled);
924   visitor.run(shader);
925   if (mode == ir_var_shader_out) {
926      if (shader->Stage == MESA_SHADER_GEOMETRY) {
927         /* For geometry shaders, outputs need to be lowered before each call
928          * to EmitVertex()
929          */
930         lower_packed_varyings_gs_splicer splicer(mem_ctx, &new_instructions);
931
932         /* Add all the variables in first. */
933         main_func_sig->body.get_head_raw()->insert_before(&new_variables);
934
935         /* Now update all the EmitVertex instances */
936         splicer.run(instructions);
937      } else {
938         /* For other shader types, outputs need to be lowered before each
939          * return statement and at the end of main()
940          */
941
942         lower_packed_varyings_return_splicer splicer(mem_ctx, &new_instructions);
943
944         main_func_sig->body.get_head_raw()->insert_before(&new_variables);
945
946         splicer.run(instructions);
947
948         /* Lower outputs at the end of main() if the last instruction is not
949          * a return statement
950          */
951         if (((ir_instruction*)instructions->get_tail())->ir_type != ir_type_return) {
952            main_func_sig->body.append_list(&new_instructions);
953         }
954      }
955   } else {
956      /* Shader inputs need to be lowered at the beginning of main() */
957      main_func_sig->body.get_head_raw()->insert_before(&new_instructions);
958      main_func_sig->body.get_head_raw()->insert_before(&new_variables);
959   }
960}
961