glsl/mesa: split gl_shader in two
[mesa.git] / src / compiler / glsl / lower_packed_varyings.cpp
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
153 using namespace ir_builder;
154
155 namespace {
156
157 /**
158 * Visitor that performs varying packing. For each varying declared in the
159 * shader, this visitor determines whether it needs to be packed. If so, it
160 * demotes it to an ordinary global, creates new packed varyings, and
161 * generates assignments to convert between the original varying and the
162 * packed varying.
163 */
164 class lower_packed_varyings_visitor
165 {
166 public:
167 lower_packed_varyings_visitor(void *mem_ctx, unsigned locations_used,
168 ir_variable_mode mode,
169 unsigned gs_input_vertices,
170 exec_list *out_instructions,
171 exec_list *out_variables,
172 bool disable_varying_packing,
173 bool xfb_enabled);
174
175 void run(struct gl_linked_shader *shader);
176
177 private:
178 void bitwise_assign_pack(ir_rvalue *lhs, ir_rvalue *rhs);
179 void bitwise_assign_unpack(ir_rvalue *lhs, ir_rvalue *rhs);
180 unsigned lower_rvalue(ir_rvalue *rvalue, unsigned fine_location,
181 ir_variable *unpacked_var, const char *name,
182 bool gs_input_toplevel, unsigned vertex_index);
183 unsigned lower_arraylike(ir_rvalue *rvalue, unsigned array_size,
184 unsigned fine_location,
185 ir_variable *unpacked_var, const char *name,
186 bool gs_input_toplevel, unsigned vertex_index);
187 ir_dereference *get_packed_varying_deref(unsigned location,
188 ir_variable *unpacked_var,
189 const char *name,
190 unsigned vertex_index);
191 bool needs_lowering(ir_variable *var);
192
193 /**
194 * Memory context used to allocate new instructions for the shader.
195 */
196 void * const mem_ctx;
197
198 /**
199 * Number of generic varying slots which are used by this shader. This is
200 * used to allocate temporary intermediate data structures. If any varying
201 * used by this shader has a location greater than or equal to
202 * VARYING_SLOT_VAR0 + locations_used, an assertion will fire.
203 */
204 const unsigned locations_used;
205
206 /**
207 * Array of pointers to the packed varyings that have been created for each
208 * generic varying slot. NULL entries in this array indicate varying slots
209 * for which a packed varying has not been created yet.
210 */
211 ir_variable **packed_varyings;
212
213 /**
214 * Type of varying which is being lowered in this pass (either
215 * ir_var_shader_in or ir_var_shader_out).
216 */
217 const ir_variable_mode mode;
218
219 /**
220 * If we are currently lowering geometry shader inputs, the number of input
221 * vertices the geometry shader accepts. Otherwise zero.
222 */
223 const unsigned gs_input_vertices;
224
225 /**
226 * Exec list into which the visitor should insert the packing instructions.
227 * Caller provides this list; it should insert the instructions into the
228 * appropriate place in the shader once the visitor has finished running.
229 */
230 exec_list *out_instructions;
231
232 /**
233 * Exec list into which the visitor should insert any new variables.
234 */
235 exec_list *out_variables;
236
237 bool disable_varying_packing;
238 bool xfb_enabled;
239 };
240
241 } /* anonymous namespace */
242
243 lower_packed_varyings_visitor::lower_packed_varyings_visitor(
244 void *mem_ctx, unsigned locations_used, ir_variable_mode mode,
245 unsigned gs_input_vertices, exec_list *out_instructions,
246 exec_list *out_variables, bool disable_varying_packing,
247 bool xfb_enabled)
248 : mem_ctx(mem_ctx),
249 locations_used(locations_used),
250 packed_varyings((ir_variable **)
251 rzalloc_array_size(mem_ctx, sizeof(*packed_varyings),
252 locations_used)),
253 mode(mode),
254 gs_input_vertices(gs_input_vertices),
255 out_instructions(out_instructions),
256 out_variables(out_variables),
257 disable_varying_packing(disable_varying_packing),
258 xfb_enabled(xfb_enabled)
259 {
260 }
261
262 void
263 lower_packed_varyings_visitor::run(struct gl_linked_shader *shader)
264 {
265 foreach_in_list(ir_instruction, node, shader->ir) {
266 ir_variable *var = node->as_variable();
267 if (var == NULL)
268 continue;
269
270 if (var->data.mode != this->mode ||
271 var->data.location < VARYING_SLOT_VAR0 ||
272 !this->needs_lowering(var))
273 continue;
274
275 /* This lowering pass is only capable of packing floats and ints
276 * together when their interpolation mode is "flat". Therefore, to be
277 * safe, caller should ensure that integral varyings always use flat
278 * interpolation, even when this is not required by GLSL.
279 */
280 assert(var->data.interpolation == INTERP_QUALIFIER_FLAT ||
281 !var->type->contains_integer());
282
283 /* Clone the variable for program resource list before
284 * it gets modified and lost.
285 */
286 if (!shader->packed_varyings)
287 shader->packed_varyings = new (shader) exec_list;
288
289 shader->packed_varyings->push_tail(var->clone(shader, NULL));
290
291 /* Change the old varying into an ordinary global. */
292 assert(var->data.mode != ir_var_temporary);
293 var->data.mode = ir_var_auto;
294
295 /* Create a reference to the old varying. */
296 ir_dereference_variable *deref
297 = new(this->mem_ctx) ir_dereference_variable(var);
298
299 /* Recursively pack or unpack it. */
300 this->lower_rvalue(deref, var->data.location * 4 + var->data.location_frac, var,
301 var->name, this->gs_input_vertices != 0, 0);
302 }
303 }
304
305 #define SWIZZLE_ZWZW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_Z, SWIZZLE_W)
306
307 /**
308 * Make an ir_assignment from \c rhs to \c lhs, performing appropriate
309 * bitcasts if necessary to match up types.
310 *
311 * This function is called when packing varyings.
312 */
313 void
314 lower_packed_varyings_visitor::bitwise_assign_pack(ir_rvalue *lhs,
315 ir_rvalue *rhs)
316 {
317 if (lhs->type->base_type != rhs->type->base_type) {
318 /* Since we only mix types in flat varyings, and we always store flat
319 * varyings as type ivec4, we need only produce conversions from (uint
320 * or float) to int.
321 */
322 assert(lhs->type->base_type == GLSL_TYPE_INT);
323 switch (rhs->type->base_type) {
324 case GLSL_TYPE_UINT:
325 rhs = new(this->mem_ctx)
326 ir_expression(ir_unop_u2i, lhs->type, rhs);
327 break;
328 case GLSL_TYPE_FLOAT:
329 rhs = new(this->mem_ctx)
330 ir_expression(ir_unop_bitcast_f2i, lhs->type, rhs);
331 break;
332 case GLSL_TYPE_DOUBLE:
333 assert(rhs->type->vector_elements <= 2);
334 if (rhs->type->vector_elements == 2) {
335 ir_variable *t = new(mem_ctx) ir_variable(lhs->type, "pack", ir_var_temporary);
336
337 assert(lhs->type->vector_elements == 4);
338 this->out_variables->push_tail(t);
339 this->out_instructions->push_tail(
340 assign(t, u2i(expr(ir_unop_unpack_double_2x32, swizzle_x(rhs->clone(mem_ctx, NULL)))), 0x3));
341 this->out_instructions->push_tail(
342 assign(t, u2i(expr(ir_unop_unpack_double_2x32, swizzle_y(rhs))), 0xc));
343 rhs = deref(t).val;
344 } else {
345 rhs = u2i(expr(ir_unop_unpack_double_2x32, rhs));
346 }
347 break;
348 default:
349 assert(!"Unexpected type conversion while lowering varyings");
350 break;
351 }
352 }
353 this->out_instructions->push_tail(new (this->mem_ctx) ir_assignment(lhs, rhs));
354 }
355
356
357 /**
358 * Make an ir_assignment from \c rhs to \c lhs, performing appropriate
359 * bitcasts if necessary to match up types.
360 *
361 * This function is called when unpacking varyings.
362 */
363 void
364 lower_packed_varyings_visitor::bitwise_assign_unpack(ir_rvalue *lhs,
365 ir_rvalue *rhs)
366 {
367 if (lhs->type->base_type != rhs->type->base_type) {
368 /* Since we only mix types in flat varyings, and we always store flat
369 * varyings as type ivec4, we need only produce conversions from int to
370 * (uint or float).
371 */
372 assert(rhs->type->base_type == GLSL_TYPE_INT);
373 switch (lhs->type->base_type) {
374 case GLSL_TYPE_UINT:
375 rhs = new(this->mem_ctx)
376 ir_expression(ir_unop_i2u, lhs->type, rhs);
377 break;
378 case GLSL_TYPE_FLOAT:
379 rhs = new(this->mem_ctx)
380 ir_expression(ir_unop_bitcast_i2f, lhs->type, rhs);
381 break;
382 case GLSL_TYPE_DOUBLE:
383 assert(lhs->type->vector_elements <= 2);
384 if (lhs->type->vector_elements == 2) {
385 ir_variable *t = new(mem_ctx) ir_variable(lhs->type, "unpack", ir_var_temporary);
386 assert(rhs->type->vector_elements == 4);
387 this->out_variables->push_tail(t);
388 this->out_instructions->push_tail(
389 assign(t, expr(ir_unop_pack_double_2x32, i2u(swizzle_xy(rhs->clone(mem_ctx, NULL)))), 0x1));
390 this->out_instructions->push_tail(
391 assign(t, expr(ir_unop_pack_double_2x32, i2u(swizzle(rhs->clone(mem_ctx, NULL), SWIZZLE_ZWZW, 2))), 0x2));
392 rhs = deref(t).val;
393 } else {
394 rhs = expr(ir_unop_pack_double_2x32, i2u(rhs));
395 }
396 break;
397 default:
398 assert(!"Unexpected type conversion while lowering varyings");
399 break;
400 }
401 }
402 this->out_instructions->push_tail(new(this->mem_ctx) ir_assignment(lhs, rhs));
403 }
404
405
406 /**
407 * Recursively pack or unpack the given varying (or portion of a varying) by
408 * traversing all of its constituent vectors.
409 *
410 * \param fine_location is the location where the first constituent vector
411 * should be packed--the word "fine" indicates that this location is expressed
412 * in multiples of a float, rather than multiples of a vec4 as is used
413 * elsewhere in Mesa.
414 *
415 * \param gs_input_toplevel should be set to true if we are lowering geometry
416 * shader inputs, and we are currently lowering the whole input variable
417 * (i.e. we are lowering the array whose index selects the vertex).
418 *
419 * \param vertex_index: if we are lowering geometry shader inputs, and the
420 * level of the array that we are currently lowering is *not* the top level,
421 * then this indicates which vertex we are currently lowering. Otherwise it
422 * is ignored.
423 *
424 * \return the location where the next constituent vector (after this one)
425 * should be packed.
426 */
427 unsigned
428 lower_packed_varyings_visitor::lower_rvalue(ir_rvalue *rvalue,
429 unsigned fine_location,
430 ir_variable *unpacked_var,
431 const char *name,
432 bool gs_input_toplevel,
433 unsigned vertex_index)
434 {
435 unsigned dmul = rvalue->type->is_64bit() ? 2 : 1;
436 /* When gs_input_toplevel is set, we should be looking at a geometry shader
437 * input array.
438 */
439 assert(!gs_input_toplevel || rvalue->type->is_array());
440
441 if (rvalue->type->is_record()) {
442 for (unsigned i = 0; i < rvalue->type->length; i++) {
443 if (i != 0)
444 rvalue = rvalue->clone(this->mem_ctx, NULL);
445 const char *field_name = rvalue->type->fields.structure[i].name;
446 ir_dereference_record *dereference_record = new(this->mem_ctx)
447 ir_dereference_record(rvalue, field_name);
448 char *deref_name
449 = ralloc_asprintf(this->mem_ctx, "%s.%s", name, field_name);
450 fine_location = this->lower_rvalue(dereference_record, fine_location,
451 unpacked_var, deref_name, false,
452 vertex_index);
453 }
454 return fine_location;
455 } else if (rvalue->type->is_array()) {
456 /* Arrays are packed/unpacked by considering each array element in
457 * sequence.
458 */
459 return this->lower_arraylike(rvalue, rvalue->type->array_size(),
460 fine_location, unpacked_var, name,
461 gs_input_toplevel, vertex_index);
462 } else if (rvalue->type->is_matrix()) {
463 /* Matrices are packed/unpacked by considering each column vector in
464 * sequence.
465 */
466 return this->lower_arraylike(rvalue, rvalue->type->matrix_columns,
467 fine_location, unpacked_var, name,
468 false, vertex_index);
469 } else if (rvalue->type->vector_elements * dmul +
470 fine_location % 4 > 4) {
471 /* This vector is going to be "double parked" across two varying slots,
472 * so handle it as two separate assignments. For doubles, a dvec3/dvec4
473 * can end up being spread over 3 slots. However the second splitting
474 * will happen later, here we just always want to split into 2.
475 */
476 unsigned left_components, right_components;
477 unsigned left_swizzle_values[4] = { 0, 0, 0, 0 };
478 unsigned right_swizzle_values[4] = { 0, 0, 0, 0 };
479 char left_swizzle_name[4] = { 0, 0, 0, 0 };
480 char right_swizzle_name[4] = { 0, 0, 0, 0 };
481
482 left_components = 4 - fine_location % 4;
483 if (rvalue->type->is_64bit()) {
484 /* We might actually end up with 0 left components! */
485 left_components /= 2;
486 }
487 right_components = rvalue->type->vector_elements - left_components;
488
489 for (unsigned i = 0; i < left_components; i++) {
490 left_swizzle_values[i] = i;
491 left_swizzle_name[i] = "xyzw"[i];
492 }
493 for (unsigned i = 0; i < right_components; i++) {
494 right_swizzle_values[i] = i + left_components;
495 right_swizzle_name[i] = "xyzw"[i + left_components];
496 }
497 ir_swizzle *left_swizzle = new(this->mem_ctx)
498 ir_swizzle(rvalue, left_swizzle_values, left_components);
499 ir_swizzle *right_swizzle = new(this->mem_ctx)
500 ir_swizzle(rvalue->clone(this->mem_ctx, NULL), right_swizzle_values,
501 right_components);
502 char *left_name
503 = ralloc_asprintf(this->mem_ctx, "%s.%s", name, left_swizzle_name);
504 char *right_name
505 = ralloc_asprintf(this->mem_ctx, "%s.%s", name, right_swizzle_name);
506 if (left_components)
507 fine_location = this->lower_rvalue(left_swizzle, fine_location,
508 unpacked_var, left_name, false,
509 vertex_index);
510 else
511 /* Top up the fine location to the next slot */
512 fine_location++;
513 return this->lower_rvalue(right_swizzle, fine_location, unpacked_var,
514 right_name, false, vertex_index);
515 } else {
516 /* No special handling is necessary; pack the rvalue into the
517 * varying.
518 */
519 unsigned swizzle_values[4] = { 0, 0, 0, 0 };
520 unsigned components = rvalue->type->vector_elements * dmul;
521 unsigned location = fine_location / 4;
522 unsigned location_frac = fine_location % 4;
523 for (unsigned i = 0; i < components; ++i)
524 swizzle_values[i] = i + location_frac;
525 ir_dereference *packed_deref =
526 this->get_packed_varying_deref(location, unpacked_var, name,
527 vertex_index);
528 ir_swizzle *swizzle = new(this->mem_ctx)
529 ir_swizzle(packed_deref, swizzle_values, components);
530 if (this->mode == ir_var_shader_out) {
531 this->bitwise_assign_pack(swizzle, rvalue);
532 } else {
533 this->bitwise_assign_unpack(rvalue, swizzle);
534 }
535 return fine_location + components;
536 }
537 }
538
539 /**
540 * Recursively pack or unpack a varying for which we need to iterate over its
541 * constituent elements, accessing each one using an ir_dereference_array.
542 * This takes care of both arrays and matrices, since ir_dereference_array
543 * treats a matrix like an array of its column vectors.
544 *
545 * \param gs_input_toplevel should be set to true if we are lowering geometry
546 * shader inputs, and we are currently lowering the whole input variable
547 * (i.e. we are lowering the array whose index selects the vertex).
548 *
549 * \param vertex_index: if we are lowering geometry shader inputs, and the
550 * level of the array that we are currently lowering is *not* the top level,
551 * then this indicates which vertex we are currently lowering. Otherwise it
552 * is ignored.
553 */
554 unsigned
555 lower_packed_varyings_visitor::lower_arraylike(ir_rvalue *rvalue,
556 unsigned array_size,
557 unsigned fine_location,
558 ir_variable *unpacked_var,
559 const char *name,
560 bool gs_input_toplevel,
561 unsigned vertex_index)
562 {
563 for (unsigned i = 0; i < array_size; i++) {
564 if (i != 0)
565 rvalue = rvalue->clone(this->mem_ctx, NULL);
566 ir_constant *constant = new(this->mem_ctx) ir_constant(i);
567 ir_dereference_array *dereference_array = new(this->mem_ctx)
568 ir_dereference_array(rvalue, constant);
569 if (gs_input_toplevel) {
570 /* Geometry shader inputs are a special case. Instead of storing
571 * each element of the array at a different location, all elements
572 * are at the same location, but with a different vertex index.
573 */
574 (void) this->lower_rvalue(dereference_array, fine_location,
575 unpacked_var, name, false, i);
576 } else {
577 char *subscripted_name
578 = ralloc_asprintf(this->mem_ctx, "%s[%d]", name, i);
579 fine_location =
580 this->lower_rvalue(dereference_array, fine_location,
581 unpacked_var, subscripted_name,
582 false, vertex_index);
583 }
584 }
585 return fine_location;
586 }
587
588 /**
589 * Retrieve the packed varying corresponding to the given varying location.
590 * If no packed varying has been created for the given varying location yet,
591 * create it and add it to the shader before returning it.
592 *
593 * The newly created varying inherits its interpolation parameters from \c
594 * unpacked_var. Its base type is ivec4 if we are lowering a flat varying,
595 * vec4 otherwise.
596 *
597 * \param vertex_index: if we are lowering geometry shader inputs, then this
598 * indicates which vertex we are currently lowering. Otherwise it is ignored.
599 */
600 ir_dereference *
601 lower_packed_varyings_visitor::get_packed_varying_deref(
602 unsigned location, ir_variable *unpacked_var, const char *name,
603 unsigned vertex_index)
604 {
605 unsigned slot = location - VARYING_SLOT_VAR0;
606 assert(slot < locations_used);
607 if (this->packed_varyings[slot] == NULL) {
608 char *packed_name = ralloc_asprintf(this->mem_ctx, "packed:%s", name);
609 const glsl_type *packed_type;
610 if (unpacked_var->data.interpolation == INTERP_QUALIFIER_FLAT)
611 packed_type = glsl_type::ivec4_type;
612 else
613 packed_type = glsl_type::vec4_type;
614 if (this->gs_input_vertices != 0) {
615 packed_type =
616 glsl_type::get_array_instance(packed_type,
617 this->gs_input_vertices);
618 }
619 ir_variable *packed_var = new(this->mem_ctx)
620 ir_variable(packed_type, packed_name, this->mode);
621 if (this->gs_input_vertices != 0) {
622 /* Prevent update_array_sizes() from messing with the size of the
623 * array.
624 */
625 packed_var->data.max_array_access = this->gs_input_vertices - 1;
626 }
627 packed_var->data.centroid = unpacked_var->data.centroid;
628 packed_var->data.sample = unpacked_var->data.sample;
629 packed_var->data.patch = unpacked_var->data.patch;
630 packed_var->data.interpolation = unpacked_var->data.interpolation;
631 packed_var->data.location = location;
632 packed_var->data.precision = unpacked_var->data.precision;
633 packed_var->data.always_active_io = unpacked_var->data.always_active_io;
634 unpacked_var->insert_before(packed_var);
635 this->packed_varyings[slot] = packed_var;
636 } else {
637 /* For geometry shader inputs, only update the packed variable name the
638 * first time we visit each component.
639 */
640 if (this->gs_input_vertices == 0 || vertex_index == 0) {
641 ralloc_asprintf_append((char **) &this->packed_varyings[slot]->name,
642 ",%s", name);
643 }
644 }
645
646 ir_dereference *deref = new(this->mem_ctx)
647 ir_dereference_variable(this->packed_varyings[slot]);
648 if (this->gs_input_vertices != 0) {
649 /* When lowering GS inputs, the packed variable is an array, so we need
650 * to dereference it using vertex_index.
651 */
652 ir_constant *constant = new(this->mem_ctx) ir_constant(vertex_index);
653 deref = new(this->mem_ctx) ir_dereference_array(deref, constant);
654 }
655 return deref;
656 }
657
658 bool
659 lower_packed_varyings_visitor::needs_lowering(ir_variable *var)
660 {
661 /* Things composed of vec4's and varyings with explicitly assigned
662 * locations don't need lowering. Everything else does.
663 */
664 if (var->data.explicit_location)
665 return false;
666
667 /* Override disable_varying_packing if the var is only used by transform
668 * feedback. Also override it if transform feedback is enabled and the
669 * variable is an array, struct or matrix as the elements of these types
670 * will always has the same interpolation and therefore asre safe to pack.
671 */
672 const glsl_type *type = var->type;
673 if (disable_varying_packing && !var->data.is_xfb_only &&
674 !((type->is_array() || type->is_record() || type->is_matrix()) &&
675 xfb_enabled))
676 return false;
677
678 type = type->without_array();
679 if (type->vector_elements == 4 && !type->is_64bit())
680 return false;
681 return true;
682 }
683
684
685 /**
686 * Visitor that splices varying packing code before every use of EmitVertex()
687 * in a geometry shader.
688 */
689 class lower_packed_varyings_gs_splicer : public ir_hierarchical_visitor
690 {
691 public:
692 explicit lower_packed_varyings_gs_splicer(void *mem_ctx,
693 const exec_list *instructions);
694
695 virtual ir_visitor_status visit_leave(ir_emit_vertex *ev);
696
697 private:
698 /**
699 * Memory context used to allocate new instructions for the shader.
700 */
701 void * const mem_ctx;
702
703 /**
704 * Instructions that should be spliced into place before each EmitVertex()
705 * call.
706 */
707 const exec_list *instructions;
708 };
709
710
711 lower_packed_varyings_gs_splicer::lower_packed_varyings_gs_splicer(
712 void *mem_ctx, const exec_list *instructions)
713 : mem_ctx(mem_ctx), instructions(instructions)
714 {
715 }
716
717
718 ir_visitor_status
719 lower_packed_varyings_gs_splicer::visit_leave(ir_emit_vertex *ev)
720 {
721 foreach_in_list(ir_instruction, ir, this->instructions) {
722 ev->insert_before(ir->clone(this->mem_ctx, NULL));
723 }
724 return visit_continue;
725 }
726
727 /**
728 * Visitor that splices varying packing code before every return.
729 */
730 class lower_packed_varyings_return_splicer : public ir_hierarchical_visitor
731 {
732 public:
733 explicit lower_packed_varyings_return_splicer(void *mem_ctx,
734 const exec_list *instructions);
735
736 virtual ir_visitor_status visit_leave(ir_return *ret);
737
738 private:
739 /**
740 * Memory context used to allocate new instructions for the shader.
741 */
742 void * const mem_ctx;
743
744 /**
745 * Instructions that should be spliced into place before each return.
746 */
747 const exec_list *instructions;
748 };
749
750
751 lower_packed_varyings_return_splicer::lower_packed_varyings_return_splicer(
752 void *mem_ctx, const exec_list *instructions)
753 : mem_ctx(mem_ctx), instructions(instructions)
754 {
755 }
756
757
758 ir_visitor_status
759 lower_packed_varyings_return_splicer::visit_leave(ir_return *ret)
760 {
761 foreach_in_list(ir_instruction, ir, this->instructions) {
762 ret->insert_before(ir->clone(this->mem_ctx, NULL));
763 }
764 return visit_continue;
765 }
766
767 void
768 lower_packed_varyings(void *mem_ctx, unsigned locations_used,
769 ir_variable_mode mode, unsigned gs_input_vertices,
770 gl_linked_shader *shader, bool disable_varying_packing,
771 bool xfb_enabled)
772 {
773 exec_list *instructions = shader->ir;
774 ir_function *main_func = shader->symbols->get_function("main");
775 exec_list void_parameters;
776 ir_function_signature *main_func_sig
777 = main_func->matching_signature(NULL, &void_parameters, false);
778 exec_list new_instructions, new_variables;
779 lower_packed_varyings_visitor visitor(mem_ctx, locations_used, mode,
780 gs_input_vertices,
781 &new_instructions,
782 &new_variables,
783 disable_varying_packing,
784 xfb_enabled);
785 visitor.run(shader);
786 if (mode == ir_var_shader_out) {
787 if (shader->Stage == MESA_SHADER_GEOMETRY) {
788 /* For geometry shaders, outputs need to be lowered before each call
789 * to EmitVertex()
790 */
791 lower_packed_varyings_gs_splicer splicer(mem_ctx, &new_instructions);
792
793 /* Add all the variables in first. */
794 main_func_sig->body.head->insert_before(&new_variables);
795
796 /* Now update all the EmitVertex instances */
797 splicer.run(instructions);
798 } else {
799 /* For other shader types, outputs need to be lowered before each
800 * return statement and at the end of main()
801 */
802
803 lower_packed_varyings_return_splicer splicer(mem_ctx, &new_instructions);
804
805 main_func_sig->body.head->insert_before(&new_variables);
806
807 splicer.run(instructions);
808
809 /* Lower outputs at the end of main() if the last instruction is not
810 * a return statement
811 */
812 if (((ir_instruction*)instructions->get_tail())->ir_type != ir_type_return) {
813 main_func_sig->body.append_list(&new_instructions);
814 }
815 }
816 } else {
817 /* Shader inputs need to be lowered at the beginning of main() */
818 main_func_sig->body.head->insert_before(&new_instructions);
819 main_func_sig->body.head->insert_before(&new_variables);
820 }
821 }