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