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_optimization.h"
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
160 class lower_packed_varyings_visitor
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
);
169 void run(exec_list
*instructions
);
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
,
184 unsigned vertex_index
);
185 bool needs_lowering(ir_variable
*var
);
188 * Memory context used to allocate new instructions for the shader.
190 void * const mem_ctx
;
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.
198 const unsigned location_base
;
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.
206 const unsigned locations_used
;
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.
213 ir_variable
**packed_varyings
;
216 * Type of varying which is being lowered in this pass (either
217 * ir_var_shader_in or ir_var_shader_out).
219 const ir_variable_mode mode
;
222 * If we are currently lowering geometry shader inputs, the number of input
223 * vertices the geometry shader accepts. Otherwise zero.
225 const unsigned gs_input_vertices
;
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.
232 exec_list
*out_instructions
;
235 } /* anonymous namespace */
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
)
242 location_base(location_base
),
243 locations_used(locations_used
),
244 packed_varyings((ir_variable
**)
245 rzalloc_array_size(mem_ctx
, sizeof(*packed_varyings
),
248 gs_input_vertices(gs_input_vertices
),
249 out_instructions(out_instructions
)
254 lower_packed_varyings_visitor::run(exec_list
*instructions
)
256 foreach_list (node
, instructions
) {
257 ir_variable
*var
= ((ir_instruction
*) node
)->as_variable();
261 if (var
->data
.mode
!= this->mode
||
262 var
->data
.location
< (int) this->location_base
||
263 !this->needs_lowering(var
))
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.
271 assert(var
->data
.interpolation
== INTERP_QUALIFIER_FLAT
||
272 !var
->type
->contains_integer());
274 /* Change the old varying into an ordinary global. */
275 var
->data
.mode
= ir_var_auto
;
277 /* Create a reference to the old varying. */
278 ir_dereference_variable
*deref
279 = new(this->mem_ctx
) ir_dereference_variable(var
);
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);
289 * Make an ir_assignment from \c rhs to \c lhs, performing appropriate
290 * bitcasts if necessary to match up types.
292 * This function is called when packing varyings.
295 lower_packed_varyings_visitor::bitwise_assign_pack(ir_rvalue
*lhs
,
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
303 assert(lhs
->type
->base_type
== GLSL_TYPE_INT
);
304 switch (rhs
->type
->base_type
) {
306 rhs
= new(this->mem_ctx
)
307 ir_expression(ir_unop_u2i
, lhs
->type
, rhs
);
309 case GLSL_TYPE_FLOAT
:
310 rhs
= new(this->mem_ctx
)
311 ir_expression(ir_unop_bitcast_f2i
, lhs
->type
, rhs
);
314 assert(!"Unexpected type conversion while lowering varyings");
318 return new(this->mem_ctx
) ir_assignment(lhs
, rhs
);
323 * Make an ir_assignment from \c rhs to \c lhs, performing appropriate
324 * bitcasts if necessary to match up types.
326 * This function is called when unpacking varyings.
329 lower_packed_varyings_visitor::bitwise_assign_unpack(ir_rvalue
*lhs
,
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
337 assert(rhs
->type
->base_type
== GLSL_TYPE_INT
);
338 switch (lhs
->type
->base_type
) {
340 rhs
= new(this->mem_ctx
)
341 ir_expression(ir_unop_i2u
, lhs
->type
, rhs
);
343 case GLSL_TYPE_FLOAT
:
344 rhs
= new(this->mem_ctx
)
345 ir_expression(ir_unop_bitcast_i2f
, lhs
->type
, rhs
);
348 assert(!"Unexpected type conversion while lowering varyings");
352 return new(this->mem_ctx
) ir_assignment(lhs
, rhs
);
357 * Recursively pack or unpack the given varying (or portion of a varying) by
358 * traversing all of its constituent vectors.
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
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).
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
374 * \return the location where the next constituent vector (after this one)
378 lower_packed_varyings_visitor::lower_rvalue(ir_rvalue
*rvalue
,
379 unsigned fine_location
,
380 ir_variable
*unpacked_var
,
382 bool gs_input_toplevel
,
383 unsigned vertex_index
)
385 /* When gs_input_toplevel is set, we should be looking at a geometry shader
388 assert(!gs_input_toplevel
|| rvalue
->type
->is_array());
390 if (rvalue
->type
->is_record()) {
391 for (unsigned i
= 0; i
< rvalue
->type
->length
; i
++) {
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
);
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,
403 return fine_location
;
404 } else if (rvalue
->type
->is_array()) {
405 /* Arrays are packed/unpacked by considering each array element in
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
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.
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
];
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
];
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
,
443 = ralloc_asprintf(this->mem_ctx
, "%s.%s", name
, left_swizzle_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,
449 return this->lower_rvalue(right_swizzle
, fine_location
, unpacked_var
,
450 right_name
, false, vertex_index
);
452 /* No special handling is necessary; pack the rvalue into the
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
,
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
);
471 ir_assignment
*assignment
472 = this->bitwise_assign_unpack(rvalue
, swizzle
);
473 this->out_instructions
->push_tail(assignment
);
475 return fine_location
+ components
;
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.
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).
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
495 lower_packed_varyings_visitor::lower_arraylike(ir_rvalue
*rvalue
,
497 unsigned fine_location
,
498 ir_variable
*unpacked_var
,
500 bool gs_input_toplevel
,
501 unsigned vertex_index
)
503 for (unsigned i
= 0; i
< array_size
; i
++) {
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.
514 (void) this->lower_rvalue(dereference_array
, fine_location
,
515 unpacked_var
, name
, false, i
);
517 char *subscripted_name
518 = ralloc_asprintf(this->mem_ctx
, "%s[%d]", name
, i
);
520 this->lower_rvalue(dereference_array
, fine_location
,
521 unpacked_var
, subscripted_name
,
522 false, vertex_index
);
525 return fine_location
;
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.
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,
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.
541 lower_packed_varyings_visitor::get_packed_varying_deref(
542 unsigned location
, ir_variable
*unpacked_var
, const char *name
,
543 unsigned vertex_index
)
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
;
553 packed_type
= glsl_type::vec4_type
;
554 if (this->gs_input_vertices
!= 0) {
556 glsl_type::get_array_instance(packed_type
,
557 this->gs_input_vertices
);
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
565 packed_var
->data
.max_array_access
= this->gs_input_vertices
- 1;
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
;
574 /* For geometry shader inputs, only update the packed variable name the
575 * first time we visit each component.
577 if (this->gs_input_vertices
== 0 || vertex_index
== 0) {
578 ralloc_asprintf_append((char **) &this->packed_varyings
[slot
]->name
,
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.
589 ir_constant
*constant
= new(this->mem_ctx
) ir_constant(vertex_index
);
590 deref
= new(this->mem_ctx
) ir_dereference_array(deref
, constant
);
596 lower_packed_varyings_visitor::needs_lowering(ir_variable
*var
)
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();
604 if (type
->is_array())
605 type
= type
->fields
.array
;
606 if (type
->vector_elements
== 4)
613 * Visitor that splices varying packing code before every use of EmitVertex()
614 * in a geometry shader.
616 class lower_packed_varyings_gs_splicer
: public ir_hierarchical_visitor
619 explicit lower_packed_varyings_gs_splicer(void *mem_ctx
,
620 const exec_list
*instructions
);
622 virtual ir_visitor_status
visit(ir_emit_vertex
*ev
);
626 * Memory context used to allocate new instructions for the shader.
628 void * const mem_ctx
;
631 * Instructions that should be spliced into place before each EmitVertex()
634 const exec_list
*instructions
;
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
)
646 lower_packed_varyings_gs_splicer::visit(ir_emit_vertex
*ev
)
648 foreach_list(node
, this->instructions
) {
649 ir_instruction
*ir
= (ir_instruction
*) node
;
650 ev
->insert_before(ir
->clone(this->mem_ctx
, NULL
));
652 return visit_continue
;
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
)
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
->Type
== GL_GEOMETRY_SHADER
) {
673 /* For geometry shaders, outputs need to be lowered before each call
676 lower_packed_varyings_gs_splicer
splicer(mem_ctx
, &new_instructions
);
677 splicer
.run(instructions
);
679 /* For other shader types, outputs need to be lowered at the end of
682 main_func_sig
->body
.append_list(&new_instructions
);
685 /* Shader inputs need to be lowered at the beginning of main() */
686 main_func_sig
->body
.head
->insert_before(&new_instructions
);