2 * Copyright © 2011 Intel Corporation
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:
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
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.
25 * \file lower_varyings_to_packed.cpp
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
31 * For example, the following shader:
33 * out mat3x2 foo; // location=4, location_frac=0
34 * out vec3 bar[2]; // location=5, location_frac=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
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];
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
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).
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.
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.
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:
86 * } arr[3]; // location=4, location_frac=0
88 * Would get lowered like this if it occurred in a fragment shader:
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
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;
119 * But it would get lowered like this if it occurred in a geometry shader:
126 * in vec4 packed4[3]; // location=4, location_frac=0
127 * in vec4 packed5[3]; // location=5, location_frac=0
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;
147 #include "glsl_symbol_table.h"
149 #include "ir_builder.h"
150 #include "ir_optimization.h"
151 #include "program/prog_instruction.h"
153 using namespace ir_builder
;
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
164 class lower_packed_varyings_visitor
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
);
174 void run(struct gl_shader
*shader
);
177 void bitwise_assign_pack(ir_rvalue
*lhs
, ir_rvalue
*rhs
);
178 void bitwise_assign_unpack(ir_rvalue
*lhs
, ir_rvalue
*rhs
);
179 unsigned lower_rvalue(ir_rvalue
*rvalue
, unsigned fine_location
,
180 ir_variable
*unpacked_var
, const char *name
,
181 bool gs_input_toplevel
, unsigned vertex_index
);
182 unsigned lower_arraylike(ir_rvalue
*rvalue
, unsigned array_size
,
183 unsigned fine_location
,
184 ir_variable
*unpacked_var
, const char *name
,
185 bool gs_input_toplevel
, unsigned vertex_index
);
186 ir_dereference
*get_packed_varying_deref(unsigned location
,
187 ir_variable
*unpacked_var
,
189 unsigned vertex_index
);
190 bool needs_lowering(ir_variable
*var
);
193 * Memory context used to allocate new instructions for the shader.
195 void * const mem_ctx
;
198 * Number of generic varying slots which are used by this shader. This is
199 * used to allocate temporary intermediate data structures. If any varying
200 * used by this shader has a location greater than or equal to
201 * VARYING_SLOT_VAR0 + locations_used, an assertion will fire.
203 const unsigned locations_used
;
206 * Array of pointers to the packed varyings that have been created for each
207 * generic varying slot. NULL entries in this array indicate varying slots
208 * for which a packed varying has not been created yet.
210 ir_variable
**packed_varyings
;
213 * Type of varying which is being lowered in this pass (either
214 * ir_var_shader_in or ir_var_shader_out).
216 const ir_variable_mode mode
;
219 * If we are currently lowering geometry shader inputs, the number of input
220 * vertices the geometry shader accepts. Otherwise zero.
222 const unsigned gs_input_vertices
;
225 * Exec list into which the visitor should insert the packing instructions.
226 * Caller provides this list; it should insert the instructions into the
227 * appropriate place in the shader once the visitor has finished running.
229 exec_list
*out_instructions
;
232 * Exec list into which the visitor should insert any new variables.
234 exec_list
*out_variables
;
236 bool disable_varying_packing
;
239 } /* anonymous namespace */
241 lower_packed_varyings_visitor::lower_packed_varyings_visitor(
242 void *mem_ctx
, unsigned locations_used
, ir_variable_mode mode
,
243 unsigned gs_input_vertices
, exec_list
*out_instructions
,
244 exec_list
*out_variables
, bool disable_varying_packing
)
246 locations_used(locations_used
),
247 packed_varyings((ir_variable
**)
248 rzalloc_array_size(mem_ctx
, sizeof(*packed_varyings
),
251 gs_input_vertices(gs_input_vertices
),
252 out_instructions(out_instructions
),
253 out_variables(out_variables
),
254 disable_varying_packing(disable_varying_packing
)
259 lower_packed_varyings_visitor::run(struct gl_shader
*shader
)
261 foreach_in_list(ir_instruction
, node
, shader
->ir
) {
262 ir_variable
*var
= node
->as_variable();
266 if (var
->data
.mode
!= this->mode
||
267 var
->data
.location
< VARYING_SLOT_VAR0
||
268 !this->needs_lowering(var
))
271 /* This lowering pass is only capable of packing floats and ints
272 * together when their interpolation mode is "flat". Therefore, to be
273 * safe, caller should ensure that integral varyings always use flat
274 * interpolation, even when this is not required by GLSL.
276 assert(var
->data
.interpolation
== INTERP_QUALIFIER_FLAT
||
277 !var
->type
->contains_integer());
279 /* Clone the variable for program resource list before
280 * it gets modified and lost.
282 if (!shader
->packed_varyings
)
283 shader
->packed_varyings
= new (shader
) exec_list
;
285 shader
->packed_varyings
->push_tail(var
->clone(shader
, NULL
));
287 /* Change the old varying into an ordinary global. */
288 assert(var
->data
.mode
!= ir_var_temporary
);
289 var
->data
.mode
= ir_var_auto
;
291 /* Create a reference to the old varying. */
292 ir_dereference_variable
*deref
293 = new(this->mem_ctx
) ir_dereference_variable(var
);
295 /* Recursively pack or unpack it. */
296 this->lower_rvalue(deref
, var
->data
.location
* 4 + var
->data
.location_frac
, var
,
297 var
->name
, this->gs_input_vertices
!= 0, 0);
301 #define SWIZZLE_ZWZW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_Z, SWIZZLE_W)
304 * Make an ir_assignment from \c rhs to \c lhs, performing appropriate
305 * bitcasts if necessary to match up types.
307 * This function is called when packing varyings.
310 lower_packed_varyings_visitor::bitwise_assign_pack(ir_rvalue
*lhs
,
313 if (lhs
->type
->base_type
!= rhs
->type
->base_type
) {
314 /* Since we only mix types in flat varyings, and we always store flat
315 * varyings as type ivec4, we need only produce conversions from (uint
318 assert(lhs
->type
->base_type
== GLSL_TYPE_INT
);
319 switch (rhs
->type
->base_type
) {
321 rhs
= new(this->mem_ctx
)
322 ir_expression(ir_unop_u2i
, lhs
->type
, rhs
);
324 case GLSL_TYPE_FLOAT
:
325 rhs
= new(this->mem_ctx
)
326 ir_expression(ir_unop_bitcast_f2i
, lhs
->type
, rhs
);
328 case GLSL_TYPE_DOUBLE
:
329 assert(rhs
->type
->vector_elements
<= 2);
330 if (rhs
->type
->vector_elements
== 2) {
331 ir_variable
*t
= new(mem_ctx
) ir_variable(lhs
->type
, "pack", ir_var_temporary
);
333 assert(lhs
->type
->vector_elements
== 4);
334 this->out_variables
->push_tail(t
);
335 this->out_instructions
->push_tail(
336 assign(t
, u2i(expr(ir_unop_unpack_double_2x32
, swizzle_x(rhs
->clone(mem_ctx
, NULL
)))), 0x3));
337 this->out_instructions
->push_tail(
338 assign(t
, u2i(expr(ir_unop_unpack_double_2x32
, swizzle_y(rhs
))), 0xc));
341 rhs
= u2i(expr(ir_unop_unpack_double_2x32
, rhs
));
345 assert(!"Unexpected type conversion while lowering varyings");
349 this->out_instructions
->push_tail(new (this->mem_ctx
) ir_assignment(lhs
, rhs
));
354 * Make an ir_assignment from \c rhs to \c lhs, performing appropriate
355 * bitcasts if necessary to match up types.
357 * This function is called when unpacking varyings.
360 lower_packed_varyings_visitor::bitwise_assign_unpack(ir_rvalue
*lhs
,
363 if (lhs
->type
->base_type
!= rhs
->type
->base_type
) {
364 /* Since we only mix types in flat varyings, and we always store flat
365 * varyings as type ivec4, we need only produce conversions from int to
368 assert(rhs
->type
->base_type
== GLSL_TYPE_INT
);
369 switch (lhs
->type
->base_type
) {
371 rhs
= new(this->mem_ctx
)
372 ir_expression(ir_unop_i2u
, lhs
->type
, rhs
);
374 case GLSL_TYPE_FLOAT
:
375 rhs
= new(this->mem_ctx
)
376 ir_expression(ir_unop_bitcast_i2f
, lhs
->type
, rhs
);
378 case GLSL_TYPE_DOUBLE
:
379 assert(lhs
->type
->vector_elements
<= 2);
380 if (lhs
->type
->vector_elements
== 2) {
381 ir_variable
*t
= new(mem_ctx
) ir_variable(lhs
->type
, "unpack", ir_var_temporary
);
382 assert(rhs
->type
->vector_elements
== 4);
383 this->out_variables
->push_tail(t
);
384 this->out_instructions
->push_tail(
385 assign(t
, expr(ir_unop_pack_double_2x32
, i2u(swizzle_xy(rhs
->clone(mem_ctx
, NULL
)))), 0x1));
386 this->out_instructions
->push_tail(
387 assign(t
, expr(ir_unop_pack_double_2x32
, i2u(swizzle(rhs
->clone(mem_ctx
, NULL
), SWIZZLE_ZWZW
, 2))), 0x2));
390 rhs
= expr(ir_unop_pack_double_2x32
, i2u(rhs
));
394 assert(!"Unexpected type conversion while lowering varyings");
398 this->out_instructions
->push_tail(new(this->mem_ctx
) ir_assignment(lhs
, rhs
));
403 * Recursively pack or unpack the given varying (or portion of a varying) by
404 * traversing all of its constituent vectors.
406 * \param fine_location is the location where the first constituent vector
407 * should be packed--the word "fine" indicates that this location is expressed
408 * in multiples of a float, rather than multiples of a vec4 as is used
411 * \param gs_input_toplevel should be set to true if we are lowering geometry
412 * shader inputs, and we are currently lowering the whole input variable
413 * (i.e. we are lowering the array whose index selects the vertex).
415 * \param vertex_index: if we are lowering geometry shader inputs, and the
416 * level of the array that we are currently lowering is *not* the top level,
417 * then this indicates which vertex we are currently lowering. Otherwise it
420 * \return the location where the next constituent vector (after this one)
424 lower_packed_varyings_visitor::lower_rvalue(ir_rvalue
*rvalue
,
425 unsigned fine_location
,
426 ir_variable
*unpacked_var
,
428 bool gs_input_toplevel
,
429 unsigned vertex_index
)
431 unsigned dmul
= rvalue
->type
->is_double() ? 2 : 1;
432 /* When gs_input_toplevel is set, we should be looking at a geometry shader
435 assert(!gs_input_toplevel
|| rvalue
->type
->is_array());
437 if (rvalue
->type
->is_record()) {
438 for (unsigned i
= 0; i
< rvalue
->type
->length
; i
++) {
440 rvalue
= rvalue
->clone(this->mem_ctx
, NULL
);
441 const char *field_name
= rvalue
->type
->fields
.structure
[i
].name
;
442 ir_dereference_record
*dereference_record
= new(this->mem_ctx
)
443 ir_dereference_record(rvalue
, field_name
);
445 = ralloc_asprintf(this->mem_ctx
, "%s.%s", name
, field_name
);
446 fine_location
= this->lower_rvalue(dereference_record
, fine_location
,
447 unpacked_var
, deref_name
, false,
450 return fine_location
;
451 } else if (rvalue
->type
->is_array()) {
452 /* Arrays are packed/unpacked by considering each array element in
455 return this->lower_arraylike(rvalue
, rvalue
->type
->array_size(),
456 fine_location
, unpacked_var
, name
,
457 gs_input_toplevel
, vertex_index
);
458 } else if (rvalue
->type
->is_matrix()) {
459 /* Matrices are packed/unpacked by considering each column vector in
462 return this->lower_arraylike(rvalue
, rvalue
->type
->matrix_columns
,
463 fine_location
, unpacked_var
, name
,
464 false, vertex_index
);
465 } else if (rvalue
->type
->vector_elements
* dmul
+
466 fine_location
% 4 > 4) {
467 /* This vector is going to be "double parked" across two varying slots,
468 * so handle it as two separate assignments. For doubles, a dvec3/dvec4
469 * can end up being spread over 3 slots. However the second splitting
470 * will happen later, here we just always want to split into 2.
472 unsigned left_components
, right_components
;
473 unsigned left_swizzle_values
[4] = { 0, 0, 0, 0 };
474 unsigned right_swizzle_values
[4] = { 0, 0, 0, 0 };
475 char left_swizzle_name
[4] = { 0, 0, 0, 0 };
476 char right_swizzle_name
[4] = { 0, 0, 0, 0 };
478 left_components
= 4 - fine_location
% 4;
479 if (rvalue
->type
->is_double()) {
480 /* We might actually end up with 0 left components! */
481 left_components
/= 2;
483 right_components
= rvalue
->type
->vector_elements
- left_components
;
485 for (unsigned i
= 0; i
< left_components
; i
++) {
486 left_swizzle_values
[i
] = i
;
487 left_swizzle_name
[i
] = "xyzw"[i
];
489 for (unsigned i
= 0; i
< right_components
; i
++) {
490 right_swizzle_values
[i
] = i
+ left_components
;
491 right_swizzle_name
[i
] = "xyzw"[i
+ left_components
];
493 ir_swizzle
*left_swizzle
= new(this->mem_ctx
)
494 ir_swizzle(rvalue
, left_swizzle_values
, left_components
);
495 ir_swizzle
*right_swizzle
= new(this->mem_ctx
)
496 ir_swizzle(rvalue
->clone(this->mem_ctx
, NULL
), right_swizzle_values
,
499 = ralloc_asprintf(this->mem_ctx
, "%s.%s", name
, left_swizzle_name
);
501 = ralloc_asprintf(this->mem_ctx
, "%s.%s", name
, right_swizzle_name
);
503 fine_location
= this->lower_rvalue(left_swizzle
, fine_location
,
504 unpacked_var
, left_name
, false,
507 /* Top up the fine location to the next slot */
509 return this->lower_rvalue(right_swizzle
, fine_location
, unpacked_var
,
510 right_name
, false, vertex_index
);
512 /* No special handling is necessary; pack the rvalue into the
515 unsigned swizzle_values
[4] = { 0, 0, 0, 0 };
516 unsigned components
= rvalue
->type
->vector_elements
* dmul
;
517 unsigned location
= fine_location
/ 4;
518 unsigned location_frac
= fine_location
% 4;
519 for (unsigned i
= 0; i
< components
; ++i
)
520 swizzle_values
[i
] = i
+ location_frac
;
521 ir_dereference
*packed_deref
=
522 this->get_packed_varying_deref(location
, unpacked_var
, name
,
524 ir_swizzle
*swizzle
= new(this->mem_ctx
)
525 ir_swizzle(packed_deref
, swizzle_values
, components
);
526 if (this->mode
== ir_var_shader_out
) {
527 this->bitwise_assign_pack(swizzle
, rvalue
);
529 this->bitwise_assign_unpack(rvalue
, swizzle
);
531 return fine_location
+ components
;
536 * Recursively pack or unpack a varying for which we need to iterate over its
537 * constituent elements, accessing each one using an ir_dereference_array.
538 * This takes care of both arrays and matrices, since ir_dereference_array
539 * treats a matrix like an array of its column vectors.
541 * \param gs_input_toplevel should be set to true if we are lowering geometry
542 * shader inputs, and we are currently lowering the whole input variable
543 * (i.e. we are lowering the array whose index selects the vertex).
545 * \param vertex_index: if we are lowering geometry shader inputs, and the
546 * level of the array that we are currently lowering is *not* the top level,
547 * then this indicates which vertex we are currently lowering. Otherwise it
551 lower_packed_varyings_visitor::lower_arraylike(ir_rvalue
*rvalue
,
553 unsigned fine_location
,
554 ir_variable
*unpacked_var
,
556 bool gs_input_toplevel
,
557 unsigned vertex_index
)
559 for (unsigned i
= 0; i
< array_size
; i
++) {
561 rvalue
= rvalue
->clone(this->mem_ctx
, NULL
);
562 ir_constant
*constant
= new(this->mem_ctx
) ir_constant(i
);
563 ir_dereference_array
*dereference_array
= new(this->mem_ctx
)
564 ir_dereference_array(rvalue
, constant
);
565 if (gs_input_toplevel
) {
566 /* Geometry shader inputs are a special case. Instead of storing
567 * each element of the array at a different location, all elements
568 * are at the same location, but with a different vertex index.
570 (void) this->lower_rvalue(dereference_array
, fine_location
,
571 unpacked_var
, name
, false, i
);
573 char *subscripted_name
574 = ralloc_asprintf(this->mem_ctx
, "%s[%d]", name
, i
);
576 this->lower_rvalue(dereference_array
, fine_location
,
577 unpacked_var
, subscripted_name
,
578 false, vertex_index
);
581 return fine_location
;
585 * Retrieve the packed varying corresponding to the given varying location.
586 * If no packed varying has been created for the given varying location yet,
587 * create it and add it to the shader before returning it.
589 * The newly created varying inherits its interpolation parameters from \c
590 * unpacked_var. Its base type is ivec4 if we are lowering a flat varying,
593 * \param vertex_index: if we are lowering geometry shader inputs, then this
594 * indicates which vertex we are currently lowering. Otherwise it is ignored.
597 lower_packed_varyings_visitor::get_packed_varying_deref(
598 unsigned location
, ir_variable
*unpacked_var
, const char *name
,
599 unsigned vertex_index
)
601 unsigned slot
= location
- VARYING_SLOT_VAR0
;
602 assert(slot
< locations_used
);
603 if (this->packed_varyings
[slot
] == NULL
) {
604 char *packed_name
= ralloc_asprintf(this->mem_ctx
, "packed:%s", name
);
605 const glsl_type
*packed_type
;
606 if (unpacked_var
->data
.interpolation
== INTERP_QUALIFIER_FLAT
)
607 packed_type
= glsl_type::ivec4_type
;
609 packed_type
= glsl_type::vec4_type
;
610 if (this->gs_input_vertices
!= 0) {
612 glsl_type::get_array_instance(packed_type
,
613 this->gs_input_vertices
);
615 ir_variable
*packed_var
= new(this->mem_ctx
)
616 ir_variable(packed_type
, packed_name
, this->mode
);
617 if (this->gs_input_vertices
!= 0) {
618 /* Prevent update_array_sizes() from messing with the size of the
621 packed_var
->data
.max_array_access
= this->gs_input_vertices
- 1;
623 packed_var
->data
.centroid
= unpacked_var
->data
.centroid
;
624 packed_var
->data
.sample
= unpacked_var
->data
.sample
;
625 packed_var
->data
.patch
= unpacked_var
->data
.patch
;
626 packed_var
->data
.interpolation
= unpacked_var
->data
.interpolation
;
627 packed_var
->data
.location
= location
;
628 packed_var
->data
.precision
= unpacked_var
->data
.precision
;
629 packed_var
->data
.always_active_io
= unpacked_var
->data
.always_active_io
;
630 unpacked_var
->insert_before(packed_var
);
631 this->packed_varyings
[slot
] = packed_var
;
633 /* For geometry shader inputs, only update the packed variable name the
634 * first time we visit each component.
636 if (this->gs_input_vertices
== 0 || vertex_index
== 0) {
637 ralloc_asprintf_append((char **) &this->packed_varyings
[slot
]->name
,
642 ir_dereference
*deref
= new(this->mem_ctx
)
643 ir_dereference_variable(this->packed_varyings
[slot
]);
644 if (this->gs_input_vertices
!= 0) {
645 /* When lowering GS inputs, the packed variable is an array, so we need
646 * to dereference it using vertex_index.
648 ir_constant
*constant
= new(this->mem_ctx
) ir_constant(vertex_index
);
649 deref
= new(this->mem_ctx
) ir_dereference_array(deref
, constant
);
655 lower_packed_varyings_visitor::needs_lowering(ir_variable
*var
)
657 /* Things composed of vec4's and varyings with explicitly assigned
658 * locations don't need lowering. Everything else does.
660 if (var
->data
.explicit_location
)
663 if (disable_varying_packing
)
666 const glsl_type
*type
= var
->type
->without_array();
667 if (type
->vector_elements
== 4 && !type
->is_double())
674 * Visitor that splices varying packing code before every use of EmitVertex()
675 * in a geometry shader.
677 class lower_packed_varyings_gs_splicer
: public ir_hierarchical_visitor
680 explicit lower_packed_varyings_gs_splicer(void *mem_ctx
,
681 const exec_list
*instructions
);
683 virtual ir_visitor_status
visit_leave(ir_emit_vertex
*ev
);
687 * Memory context used to allocate new instructions for the shader.
689 void * const mem_ctx
;
692 * Instructions that should be spliced into place before each EmitVertex()
695 const exec_list
*instructions
;
699 lower_packed_varyings_gs_splicer::lower_packed_varyings_gs_splicer(
700 void *mem_ctx
, const exec_list
*instructions
)
701 : mem_ctx(mem_ctx
), instructions(instructions
)
707 lower_packed_varyings_gs_splicer::visit_leave(ir_emit_vertex
*ev
)
709 foreach_in_list(ir_instruction
, ir
, this->instructions
) {
710 ev
->insert_before(ir
->clone(this->mem_ctx
, NULL
));
712 return visit_continue
;
717 lower_packed_varyings(void *mem_ctx
, unsigned locations_used
,
718 ir_variable_mode mode
, unsigned gs_input_vertices
,
719 gl_shader
*shader
, bool disable_varying_packing
)
721 exec_list
*instructions
= shader
->ir
;
722 ir_function
*main_func
= shader
->symbols
->get_function("main");
723 exec_list void_parameters
;
724 ir_function_signature
*main_func_sig
725 = main_func
->matching_signature(NULL
, &void_parameters
, false);
726 exec_list new_instructions
, new_variables
;
727 lower_packed_varyings_visitor
visitor(mem_ctx
, locations_used
, mode
,
731 disable_varying_packing
);
733 if (mode
== ir_var_shader_out
) {
734 if (shader
->Stage
== MESA_SHADER_GEOMETRY
) {
735 /* For geometry shaders, outputs need to be lowered before each call
738 lower_packed_varyings_gs_splicer
splicer(mem_ctx
, &new_instructions
);
740 /* Add all the variables in first. */
741 main_func_sig
->body
.head
->insert_before(&new_variables
);
743 /* Now update all the EmitVertex instances */
744 splicer
.run(instructions
);
746 /* For other shader types, outputs need to be lowered at the end of
749 main_func_sig
->body
.append_list(&new_variables
);
750 main_func_sig
->body
.append_list(&new_instructions
);
753 /* Shader inputs need to be lowered at the beginning of main() */
754 main_func_sig
->body
.head
->insert_before(&new_instructions
);
755 main_func_sig
->body
.head
->insert_before(&new_variables
);