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