glsl: Pass _mesa_glsl_parse_state into matching_signature and such.
[mesa.git] / src / 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_optimization.h"
150
151 /**
152 * Visitor that performs varying packing. For each varying declared in the
153 * shader, this visitor determines whether it needs to be packed. If so, it
154 * demotes it to an ordinary global, creates new packed varyings, and
155 * generates assignments to convert between the original varying and the
156 * packed varying.
157 */
158 class lower_packed_varyings_visitor
159 {
160 public:
161 lower_packed_varyings_visitor(void *mem_ctx, unsigned location_base,
162 unsigned locations_used,
163 ir_variable_mode mode,
164 unsigned gs_input_vertices,
165 exec_list *out_instructions);
166
167 void run(exec_list *instructions);
168
169 private:
170 ir_assignment *bitwise_assign_pack(ir_rvalue *lhs, ir_rvalue *rhs);
171 ir_assignment *bitwise_assign_unpack(ir_rvalue *lhs, ir_rvalue *rhs);
172 unsigned lower_rvalue(ir_rvalue *rvalue, unsigned fine_location,
173 ir_variable *unpacked_var, const char *name,
174 bool gs_input_toplevel, unsigned vertex_index);
175 unsigned lower_arraylike(ir_rvalue *rvalue, unsigned array_size,
176 unsigned fine_location,
177 ir_variable *unpacked_var, const char *name,
178 bool gs_input_toplevel, unsigned vertex_index);
179 ir_dereference *get_packed_varying_deref(unsigned location,
180 ir_variable *unpacked_var,
181 const char *name,
182 unsigned vertex_index);
183 bool needs_lowering(ir_variable *var);
184
185 /**
186 * Memory context used to allocate new instructions for the shader.
187 */
188 void * const mem_ctx;
189
190 /**
191 * Location representing the first generic varying slot for this shader
192 * stage (e.g. VARYING_SLOT_VAR0 if we are packing vertex shader outputs).
193 * Varyings whose location is less than this value are assumed to
194 * correspond to special fixed function hardware, so they are not lowered.
195 */
196 const unsigned location_base;
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 any
201 * varying used by this shader has a location greater than or equal to
202 * location_base + 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 lower_packed_varyings_visitor::lower_packed_varyings_visitor(
234 void *mem_ctx, unsigned location_base, unsigned locations_used,
235 ir_variable_mode mode, unsigned gs_input_vertices,
236 exec_list *out_instructions)
237 : mem_ctx(mem_ctx),
238 location_base(location_base),
239 locations_used(locations_used),
240 packed_varyings((ir_variable **)
241 rzalloc_array_size(mem_ctx, sizeof(*packed_varyings),
242 locations_used)),
243 mode(mode),
244 gs_input_vertices(gs_input_vertices),
245 out_instructions(out_instructions)
246 {
247 }
248
249 void
250 lower_packed_varyings_visitor::run(exec_list *instructions)
251 {
252 foreach_list (node, instructions) {
253 ir_variable *var = ((ir_instruction *) node)->as_variable();
254 if (var == NULL)
255 continue;
256
257 if (var->mode != this->mode ||
258 var->location < (int) this->location_base ||
259 !this->needs_lowering(var))
260 continue;
261
262 /* This lowering pass is only capable of packing floats and ints
263 * together when their interpolation mode is "flat". Therefore, to be
264 * safe, caller should ensure that integral varyings always use flat
265 * interpolation, even when this is not required by GLSL.
266 */
267 assert(var->interpolation == INTERP_QUALIFIER_FLAT ||
268 !var->type->contains_integer());
269
270 /* Change the old varying into an ordinary global. */
271 var->mode = ir_var_auto;
272
273 /* Create a reference to the old varying. */
274 ir_dereference_variable *deref
275 = new(this->mem_ctx) ir_dereference_variable(var);
276
277 /* Recursively pack or unpack it. */
278 this->lower_rvalue(deref, var->location * 4 + var->location_frac, var,
279 var->name, this->gs_input_vertices != 0, 0);
280 }
281 }
282
283
284 /**
285 * Make an ir_assignment from \c rhs to \c lhs, performing appropriate
286 * bitcasts if necessary to match up types.
287 *
288 * This function is called when packing varyings.
289 */
290 ir_assignment *
291 lower_packed_varyings_visitor::bitwise_assign_pack(ir_rvalue *lhs,
292 ir_rvalue *rhs)
293 {
294 if (lhs->type->base_type != rhs->type->base_type) {
295 /* Since we only mix types in flat varyings, and we always store flat
296 * varyings as type ivec4, we need only produce conversions from (uint
297 * or float) to int.
298 */
299 assert(lhs->type->base_type == GLSL_TYPE_INT);
300 switch (rhs->type->base_type) {
301 case GLSL_TYPE_UINT:
302 rhs = new(this->mem_ctx)
303 ir_expression(ir_unop_u2i, lhs->type, rhs);
304 break;
305 case GLSL_TYPE_FLOAT:
306 rhs = new(this->mem_ctx)
307 ir_expression(ir_unop_bitcast_f2i, lhs->type, rhs);
308 break;
309 default:
310 assert(!"Unexpected type conversion while lowering varyings");
311 break;
312 }
313 }
314 return new(this->mem_ctx) ir_assignment(lhs, rhs);
315 }
316
317
318 /**
319 * Make an ir_assignment from \c rhs to \c lhs, performing appropriate
320 * bitcasts if necessary to match up types.
321 *
322 * This function is called when unpacking varyings.
323 */
324 ir_assignment *
325 lower_packed_varyings_visitor::bitwise_assign_unpack(ir_rvalue *lhs,
326 ir_rvalue *rhs)
327 {
328 if (lhs->type->base_type != rhs->type->base_type) {
329 /* Since we only mix types in flat varyings, and we always store flat
330 * varyings as type ivec4, we need only produce conversions from int to
331 * (uint or float).
332 */
333 assert(rhs->type->base_type == GLSL_TYPE_INT);
334 switch (lhs->type->base_type) {
335 case GLSL_TYPE_UINT:
336 rhs = new(this->mem_ctx)
337 ir_expression(ir_unop_i2u, lhs->type, rhs);
338 break;
339 case GLSL_TYPE_FLOAT:
340 rhs = new(this->mem_ctx)
341 ir_expression(ir_unop_bitcast_i2f, lhs->type, rhs);
342 break;
343 default:
344 assert(!"Unexpected type conversion while lowering varyings");
345 break;
346 }
347 }
348 return new(this->mem_ctx) ir_assignment(lhs, rhs);
349 }
350
351
352 /**
353 * Recursively pack or unpack the given varying (or portion of a varying) by
354 * traversing all of its constituent vectors.
355 *
356 * \param fine_location is the location where the first constituent vector
357 * should be packed--the word "fine" indicates that this location is expressed
358 * in multiples of a float, rather than multiples of a vec4 as is used
359 * elsewhere in Mesa.
360 *
361 * \param gs_input_toplevel should be set to true if we are lowering geometry
362 * shader inputs, and we are currently lowering the whole input variable
363 * (i.e. we are lowering the array whose index selects the vertex).
364 *
365 * \param vertex_index: if we are lowering geometry shader inputs, and the
366 * level of the array that we are currently lowering is *not* the top level,
367 * then this indicates which vertex we are currently lowering. Otherwise it
368 * is ignored.
369 *
370 * \return the location where the next constituent vector (after this one)
371 * should be packed.
372 */
373 unsigned
374 lower_packed_varyings_visitor::lower_rvalue(ir_rvalue *rvalue,
375 unsigned fine_location,
376 ir_variable *unpacked_var,
377 const char *name,
378 bool gs_input_toplevel,
379 unsigned vertex_index)
380 {
381 /* When gs_input_toplevel is set, we should be looking at a geometry shader
382 * input array.
383 */
384 assert(!gs_input_toplevel || rvalue->type->is_array());
385
386 if (rvalue->type->is_record()) {
387 for (unsigned i = 0; i < rvalue->type->length; i++) {
388 if (i != 0)
389 rvalue = rvalue->clone(this->mem_ctx, NULL);
390 const char *field_name = rvalue->type->fields.structure[i].name;
391 ir_dereference_record *dereference_record = new(this->mem_ctx)
392 ir_dereference_record(rvalue, field_name);
393 char *deref_name
394 = ralloc_asprintf(this->mem_ctx, "%s.%s", name, field_name);
395 fine_location = this->lower_rvalue(dereference_record, fine_location,
396 unpacked_var, deref_name, false,
397 vertex_index);
398 }
399 return fine_location;
400 } else if (rvalue->type->is_array()) {
401 /* Arrays are packed/unpacked by considering each array element in
402 * sequence.
403 */
404 return this->lower_arraylike(rvalue, rvalue->type->array_size(),
405 fine_location, unpacked_var, name,
406 gs_input_toplevel, vertex_index);
407 } else if (rvalue->type->is_matrix()) {
408 /* Matrices are packed/unpacked by considering each column vector in
409 * sequence.
410 */
411 return this->lower_arraylike(rvalue, rvalue->type->matrix_columns,
412 fine_location, unpacked_var, name,
413 false, vertex_index);
414 } else if (rvalue->type->vector_elements + fine_location % 4 > 4) {
415 /* This vector is going to be "double parked" across two varying slots,
416 * so handle it as two separate assignments.
417 */
418 unsigned left_components = 4 - fine_location % 4;
419 unsigned right_components
420 = rvalue->type->vector_elements - left_components;
421 unsigned left_swizzle_values[4] = { 0, 0, 0, 0 };
422 unsigned right_swizzle_values[4] = { 0, 0, 0, 0 };
423 char left_swizzle_name[4] = { 0, 0, 0, 0 };
424 char right_swizzle_name[4] = { 0, 0, 0, 0 };
425 for (unsigned i = 0; i < left_components; i++) {
426 left_swizzle_values[i] = i;
427 left_swizzle_name[i] = "xyzw"[i];
428 }
429 for (unsigned i = 0; i < right_components; i++) {
430 right_swizzle_values[i] = i + left_components;
431 right_swizzle_name[i] = "xyzw"[i + left_components];
432 }
433 ir_swizzle *left_swizzle = new(this->mem_ctx)
434 ir_swizzle(rvalue, left_swizzle_values, left_components);
435 ir_swizzle *right_swizzle = new(this->mem_ctx)
436 ir_swizzle(rvalue->clone(this->mem_ctx, NULL), right_swizzle_values,
437 right_components);
438 char *left_name
439 = ralloc_asprintf(this->mem_ctx, "%s.%s", name, left_swizzle_name);
440 char *right_name
441 = ralloc_asprintf(this->mem_ctx, "%s.%s", name, right_swizzle_name);
442 fine_location = this->lower_rvalue(left_swizzle, fine_location,
443 unpacked_var, left_name, false,
444 vertex_index);
445 return this->lower_rvalue(right_swizzle, fine_location, unpacked_var,
446 right_name, false, vertex_index);
447 } else {
448 /* No special handling is necessary; pack the rvalue into the
449 * varying.
450 */
451 unsigned swizzle_values[4] = { 0, 0, 0, 0 };
452 unsigned components = rvalue->type->vector_elements;
453 unsigned location = fine_location / 4;
454 unsigned location_frac = fine_location % 4;
455 for (unsigned i = 0; i < components; ++i)
456 swizzle_values[i] = i + location_frac;
457 ir_dereference *packed_deref =
458 this->get_packed_varying_deref(location, unpacked_var, name,
459 vertex_index);
460 ir_swizzle *swizzle = new(this->mem_ctx)
461 ir_swizzle(packed_deref, swizzle_values, components);
462 if (this->mode == ir_var_shader_out) {
463 ir_assignment *assignment
464 = this->bitwise_assign_pack(swizzle, rvalue);
465 this->out_instructions->push_tail(assignment);
466 } else {
467 ir_assignment *assignment
468 = this->bitwise_assign_unpack(rvalue, swizzle);
469 this->out_instructions->push_tail(assignment);
470 }
471 return fine_location + components;
472 }
473 }
474
475 /**
476 * Recursively pack or unpack a varying for which we need to iterate over its
477 * constituent elements, accessing each one using an ir_dereference_array.
478 * This takes care of both arrays and matrices, since ir_dereference_array
479 * treats a matrix like an array of its column vectors.
480 *
481 * \param gs_input_toplevel should be set to true if we are lowering geometry
482 * shader inputs, and we are currently lowering the whole input variable
483 * (i.e. we are lowering the array whose index selects the vertex).
484 *
485 * \param vertex_index: if we are lowering geometry shader inputs, and the
486 * level of the array that we are currently lowering is *not* the top level,
487 * then this indicates which vertex we are currently lowering. Otherwise it
488 * is ignored.
489 */
490 unsigned
491 lower_packed_varyings_visitor::lower_arraylike(ir_rvalue *rvalue,
492 unsigned array_size,
493 unsigned fine_location,
494 ir_variable *unpacked_var,
495 const char *name,
496 bool gs_input_toplevel,
497 unsigned vertex_index)
498 {
499 for (unsigned i = 0; i < array_size; i++) {
500 if (i != 0)
501 rvalue = rvalue->clone(this->mem_ctx, NULL);
502 ir_constant *constant = new(this->mem_ctx) ir_constant(i);
503 ir_dereference_array *dereference_array = new(this->mem_ctx)
504 ir_dereference_array(rvalue, constant);
505 char *subscripted_name
506 = ralloc_asprintf(this->mem_ctx, "%s[%d]", name, i);
507 if (gs_input_toplevel) {
508 /* Geometry shader inputs are a special case. Instead of storing
509 * each element of the array at a different location, all elements
510 * are at the same location, but with a different vertex index.
511 */
512 (void) this->lower_rvalue(dereference_array, fine_location,
513 unpacked_var, subscripted_name,
514 false, i);
515 } else {
516 fine_location =
517 this->lower_rvalue(dereference_array, fine_location,
518 unpacked_var, subscripted_name,
519 false, vertex_index);
520 }
521 }
522 return fine_location;
523 }
524
525 /**
526 * Retrieve the packed varying corresponding to the given varying location.
527 * If no packed varying has been created for the given varying location yet,
528 * create it and add it to the shader before returning it.
529 *
530 * The newly created varying inherits its interpolation parameters from \c
531 * unpacked_var. Its base type is ivec4 if we are lowering a flat varying,
532 * vec4 otherwise.
533 *
534 * \param vertex_index: if we are lowering geometry shader inputs, then this
535 * indicates which vertex we are currently lowering. Otherwise it is ignored.
536 */
537 ir_dereference *
538 lower_packed_varyings_visitor::get_packed_varying_deref(
539 unsigned location, ir_variable *unpacked_var, const char *name,
540 unsigned vertex_index)
541 {
542 unsigned slot = location - this->location_base;
543 assert(slot < locations_used);
544 if (this->packed_varyings[slot] == NULL) {
545 char *packed_name = ralloc_asprintf(this->mem_ctx, "packed:%s", name);
546 const glsl_type *packed_type;
547 if (unpacked_var->interpolation == INTERP_QUALIFIER_FLAT)
548 packed_type = glsl_type::ivec4_type;
549 else
550 packed_type = glsl_type::vec4_type;
551 if (this->gs_input_vertices != 0) {
552 packed_type =
553 glsl_type::get_array_instance(packed_type,
554 this->gs_input_vertices);
555 }
556 ir_variable *packed_var = new(this->mem_ctx)
557 ir_variable(packed_type, packed_name, this->mode);
558 if (this->gs_input_vertices != 0) {
559 /* Prevent update_array_sizes() from messing with the size of the
560 * array.
561 */
562 packed_var->max_array_access = this->gs_input_vertices - 1;
563 }
564 packed_var->centroid = unpacked_var->centroid;
565 packed_var->interpolation = unpacked_var->interpolation;
566 packed_var->location = location;
567 unpacked_var->insert_before(packed_var);
568 this->packed_varyings[slot] = packed_var;
569 } else {
570 /* For geometry shader inputs, only update the packed variable name the
571 * first time we visit each component.
572 */
573 if (this->gs_input_vertices == 0 || vertex_index == 0) {
574 ralloc_asprintf_append((char **) &this->packed_varyings[slot]->name,
575 ",%s", name);
576 }
577 }
578
579 ir_dereference *deref = new(this->mem_ctx)
580 ir_dereference_variable(this->packed_varyings[slot]);
581 if (this->gs_input_vertices != 0) {
582 /* When lowering GS inputs, the packed variable is an array, so we need
583 * to dereference it using vertex_index.
584 */
585 ir_constant *constant = new(this->mem_ctx) ir_constant(vertex_index);
586 deref = new(this->mem_ctx) ir_dereference_array(deref, constant);
587 }
588 return deref;
589 }
590
591 bool
592 lower_packed_varyings_visitor::needs_lowering(ir_variable *var)
593 {
594 /* Things composed of vec4's don't need lowering. Everything else does. */
595 const glsl_type *type = var->type;
596 if (this->gs_input_vertices != 0) {
597 assert(type->is_array());
598 type = type->element_type();
599 }
600 if (type->is_array())
601 type = type->fields.array;
602 if (type->vector_elements == 4)
603 return false;
604 return true;
605 }
606
607
608 /**
609 * Visitor that splices varying packing code before every use of EmitVertex()
610 * in a geometry shader.
611 */
612 class lower_packed_varyings_gs_splicer : public ir_hierarchical_visitor
613 {
614 public:
615 explicit lower_packed_varyings_gs_splicer(void *mem_ctx,
616 const exec_list *instructions);
617
618 virtual ir_visitor_status visit(ir_emit_vertex *ev);
619
620 private:
621 /**
622 * Memory context used to allocate new instructions for the shader.
623 */
624 void * const mem_ctx;
625
626 /**
627 * Instructions that should be spliced into place before each EmitVertex()
628 * call.
629 */
630 const exec_list *instructions;
631 };
632
633
634 lower_packed_varyings_gs_splicer::lower_packed_varyings_gs_splicer(
635 void *mem_ctx, const exec_list *instructions)
636 : mem_ctx(mem_ctx), instructions(instructions)
637 {
638 }
639
640
641 ir_visitor_status
642 lower_packed_varyings_gs_splicer::visit(ir_emit_vertex *ev)
643 {
644 foreach_list(node, this->instructions) {
645 ir_instruction *ir = (ir_instruction *) node;
646 ev->insert_before(ir->clone(this->mem_ctx, NULL));
647 }
648 return visit_continue;
649 }
650
651
652 void
653 lower_packed_varyings(void *mem_ctx, unsigned location_base,
654 unsigned locations_used, ir_variable_mode mode,
655 unsigned gs_input_vertices, gl_shader *shader)
656 {
657 exec_list *instructions = shader->ir;
658 ir_function *main_func = shader->symbols->get_function("main");
659 exec_list void_parameters;
660 ir_function_signature *main_func_sig
661 = main_func->matching_signature(NULL, &void_parameters);
662 exec_list new_instructions;
663 lower_packed_varyings_visitor visitor(mem_ctx, location_base,
664 locations_used, mode,
665 gs_input_vertices, &new_instructions);
666 visitor.run(instructions);
667 if (mode == ir_var_shader_out) {
668 if (shader->Type == GL_GEOMETRY_SHADER) {
669 /* For geometry shaders, outputs need to be lowered before each call
670 * to EmitVertex()
671 */
672 lower_packed_varyings_gs_splicer splicer(mem_ctx, &new_instructions);
673 splicer.run(instructions);
674 } else {
675 /* For other shader types, outputs need to be lowered at the end of
676 * main()
677 */
678 main_func_sig->body.append_list(&new_instructions);
679 }
680 } else {
681 /* Shader inputs need to be lowered at the beginning of main() */
682 main_func_sig->body.head->insert_before(&new_instructions);
683 }
684 }