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 locations_used
,
164 ir_variable_mode mode
,
165 unsigned gs_input_vertices
,
166 exec_list
*out_instructions
);
168 void run(exec_list
*instructions
);
171 ir_assignment
*bitwise_assign_pack(ir_rvalue
*lhs
, ir_rvalue
*rhs
);
172 ir_assignment
*bitwise_assign_unpack(ir_rvalue
*lhs
, ir_rvalue
*rhs
);
173 unsigned lower_rvalue(ir_rvalue
*rvalue
, unsigned fine_location
,
174 ir_variable
*unpacked_var
, const char *name
,
175 bool gs_input_toplevel
, unsigned vertex_index
);
176 unsigned lower_arraylike(ir_rvalue
*rvalue
, unsigned array_size
,
177 unsigned fine_location
,
178 ir_variable
*unpacked_var
, const char *name
,
179 bool gs_input_toplevel
, unsigned vertex_index
);
180 ir_dereference
*get_packed_varying_deref(unsigned location
,
181 ir_variable
*unpacked_var
,
183 unsigned vertex_index
);
184 bool needs_lowering(ir_variable
*var
);
187 * Memory context used to allocate new instructions for the shader.
189 void * const mem_ctx
;
192 * Number of generic varying slots which are used by this shader. This is
193 * used to allocate temporary intermediate data structures. If any varying
194 * used by this shader has a location greater than or equal to
195 * VARYING_SLOT_VAR0 + locations_used, an assertion will fire.
197 const unsigned locations_used
;
200 * Array of pointers to the packed varyings that have been created for each
201 * generic varying slot. NULL entries in this array indicate varying slots
202 * for which a packed varying has not been created yet.
204 ir_variable
**packed_varyings
;
207 * Type of varying which is being lowered in this pass (either
208 * ir_var_shader_in or ir_var_shader_out).
210 const ir_variable_mode mode
;
213 * If we are currently lowering geometry shader inputs, the number of input
214 * vertices the geometry shader accepts. Otherwise zero.
216 const unsigned gs_input_vertices
;
219 * Exec list into which the visitor should insert the packing instructions.
220 * Caller provides this list; it should insert the instructions into the
221 * appropriate place in the shader once the visitor has finished running.
223 exec_list
*out_instructions
;
226 } /* anonymous namespace */
228 lower_packed_varyings_visitor::lower_packed_varyings_visitor(
229 void *mem_ctx
, unsigned locations_used
, ir_variable_mode mode
,
230 unsigned gs_input_vertices
, exec_list
*out_instructions
)
232 locations_used(locations_used
),
233 packed_varyings((ir_variable
**)
234 rzalloc_array_size(mem_ctx
, sizeof(*packed_varyings
),
237 gs_input_vertices(gs_input_vertices
),
238 out_instructions(out_instructions
)
243 lower_packed_varyings_visitor::run(exec_list
*instructions
)
245 foreach_in_list(ir_instruction
, node
, instructions
) {
246 ir_variable
*var
= node
->as_variable();
250 if (var
->data
.mode
!= this->mode
||
251 var
->data
.location
< VARYING_SLOT_VAR0
||
252 !this->needs_lowering(var
))
255 /* This lowering pass is only capable of packing floats and ints
256 * together when their interpolation mode is "flat". Therefore, to be
257 * safe, caller should ensure that integral varyings always use flat
258 * interpolation, even when this is not required by GLSL.
260 assert(var
->data
.interpolation
== INTERP_QUALIFIER_FLAT
||
261 !var
->type
->contains_integer());
263 /* Change the old varying into an ordinary global. */
264 assert(var
->data
.mode
!= ir_var_temporary
);
265 var
->data
.mode
= ir_var_auto
;
267 /* Create a reference to the old varying. */
268 ir_dereference_variable
*deref
269 = new(this->mem_ctx
) ir_dereference_variable(var
);
271 /* Recursively pack or unpack it. */
272 this->lower_rvalue(deref
, var
->data
.location
* 4 + var
->data
.location_frac
, var
,
273 var
->name
, this->gs_input_vertices
!= 0, 0);
279 * Make an ir_assignment from \c rhs to \c lhs, performing appropriate
280 * bitcasts if necessary to match up types.
282 * This function is called when packing varyings.
285 lower_packed_varyings_visitor::bitwise_assign_pack(ir_rvalue
*lhs
,
288 if (lhs
->type
->base_type
!= rhs
->type
->base_type
) {
289 /* Since we only mix types in flat varyings, and we always store flat
290 * varyings as type ivec4, we need only produce conversions from (uint
293 assert(lhs
->type
->base_type
== GLSL_TYPE_INT
);
294 switch (rhs
->type
->base_type
) {
296 rhs
= new(this->mem_ctx
)
297 ir_expression(ir_unop_u2i
, lhs
->type
, rhs
);
299 case GLSL_TYPE_FLOAT
:
300 rhs
= new(this->mem_ctx
)
301 ir_expression(ir_unop_bitcast_f2i
, lhs
->type
, rhs
);
304 assert(!"Unexpected type conversion while lowering varyings");
308 return new(this->mem_ctx
) ir_assignment(lhs
, rhs
);
313 * Make an ir_assignment from \c rhs to \c lhs, performing appropriate
314 * bitcasts if necessary to match up types.
316 * This function is called when unpacking varyings.
319 lower_packed_varyings_visitor::bitwise_assign_unpack(ir_rvalue
*lhs
,
322 if (lhs
->type
->base_type
!= rhs
->type
->base_type
) {
323 /* Since we only mix types in flat varyings, and we always store flat
324 * varyings as type ivec4, we need only produce conversions from int to
327 assert(rhs
->type
->base_type
== GLSL_TYPE_INT
);
328 switch (lhs
->type
->base_type
) {
330 rhs
= new(this->mem_ctx
)
331 ir_expression(ir_unop_i2u
, lhs
->type
, rhs
);
333 case GLSL_TYPE_FLOAT
:
334 rhs
= new(this->mem_ctx
)
335 ir_expression(ir_unop_bitcast_i2f
, lhs
->type
, rhs
);
338 assert(!"Unexpected type conversion while lowering varyings");
342 return new(this->mem_ctx
) ir_assignment(lhs
, rhs
);
347 * Recursively pack or unpack the given varying (or portion of a varying) by
348 * traversing all of its constituent vectors.
350 * \param fine_location is the location where the first constituent vector
351 * should be packed--the word "fine" indicates that this location is expressed
352 * in multiples of a float, rather than multiples of a vec4 as is used
355 * \param gs_input_toplevel should be set to true if we are lowering geometry
356 * shader inputs, and we are currently lowering the whole input variable
357 * (i.e. we are lowering the array whose index selects the vertex).
359 * \param vertex_index: if we are lowering geometry shader inputs, and the
360 * level of the array that we are currently lowering is *not* the top level,
361 * then this indicates which vertex we are currently lowering. Otherwise it
364 * \return the location where the next constituent vector (after this one)
368 lower_packed_varyings_visitor::lower_rvalue(ir_rvalue
*rvalue
,
369 unsigned fine_location
,
370 ir_variable
*unpacked_var
,
372 bool gs_input_toplevel
,
373 unsigned vertex_index
)
375 /* When gs_input_toplevel is set, we should be looking at a geometry shader
378 assert(!gs_input_toplevel
|| rvalue
->type
->is_array());
380 if (rvalue
->type
->is_record()) {
381 for (unsigned i
= 0; i
< rvalue
->type
->length
; i
++) {
383 rvalue
= rvalue
->clone(this->mem_ctx
, NULL
);
384 const char *field_name
= rvalue
->type
->fields
.structure
[i
].name
;
385 ir_dereference_record
*dereference_record
= new(this->mem_ctx
)
386 ir_dereference_record(rvalue
, field_name
);
388 = ralloc_asprintf(this->mem_ctx
, "%s.%s", name
, field_name
);
389 fine_location
= this->lower_rvalue(dereference_record
, fine_location
,
390 unpacked_var
, deref_name
, false,
393 return fine_location
;
394 } else if (rvalue
->type
->is_array()) {
395 /* Arrays are packed/unpacked by considering each array element in
398 return this->lower_arraylike(rvalue
, rvalue
->type
->array_size(),
399 fine_location
, unpacked_var
, name
,
400 gs_input_toplevel
, vertex_index
);
401 } else if (rvalue
->type
->is_matrix()) {
402 /* Matrices are packed/unpacked by considering each column vector in
405 return this->lower_arraylike(rvalue
, rvalue
->type
->matrix_columns
,
406 fine_location
, unpacked_var
, name
,
407 false, vertex_index
);
408 } else if (rvalue
->type
->vector_elements
+ fine_location
% 4 > 4) {
409 /* This vector is going to be "double parked" across two varying slots,
410 * so handle it as two separate assignments.
412 unsigned left_components
= 4 - fine_location
% 4;
413 unsigned right_components
414 = rvalue
->type
->vector_elements
- left_components
;
415 unsigned left_swizzle_values
[4] = { 0, 0, 0, 0 };
416 unsigned right_swizzle_values
[4] = { 0, 0, 0, 0 };
417 char left_swizzle_name
[4] = { 0, 0, 0, 0 };
418 char right_swizzle_name
[4] = { 0, 0, 0, 0 };
419 for (unsigned i
= 0; i
< left_components
; i
++) {
420 left_swizzle_values
[i
] = i
;
421 left_swizzle_name
[i
] = "xyzw"[i
];
423 for (unsigned i
= 0; i
< right_components
; i
++) {
424 right_swizzle_values
[i
] = i
+ left_components
;
425 right_swizzle_name
[i
] = "xyzw"[i
+ left_components
];
427 ir_swizzle
*left_swizzle
= new(this->mem_ctx
)
428 ir_swizzle(rvalue
, left_swizzle_values
, left_components
);
429 ir_swizzle
*right_swizzle
= new(this->mem_ctx
)
430 ir_swizzle(rvalue
->clone(this->mem_ctx
, NULL
), right_swizzle_values
,
433 = ralloc_asprintf(this->mem_ctx
, "%s.%s", name
, left_swizzle_name
);
435 = ralloc_asprintf(this->mem_ctx
, "%s.%s", name
, right_swizzle_name
);
436 fine_location
= this->lower_rvalue(left_swizzle
, fine_location
,
437 unpacked_var
, left_name
, false,
439 return this->lower_rvalue(right_swizzle
, fine_location
, unpacked_var
,
440 right_name
, false, vertex_index
);
442 /* No special handling is necessary; pack the rvalue into the
445 unsigned swizzle_values
[4] = { 0, 0, 0, 0 };
446 unsigned components
= rvalue
->type
->vector_elements
;
447 unsigned location
= fine_location
/ 4;
448 unsigned location_frac
= fine_location
% 4;
449 for (unsigned i
= 0; i
< components
; ++i
)
450 swizzle_values
[i
] = i
+ location_frac
;
451 ir_dereference
*packed_deref
=
452 this->get_packed_varying_deref(location
, unpacked_var
, name
,
454 ir_swizzle
*swizzle
= new(this->mem_ctx
)
455 ir_swizzle(packed_deref
, swizzle_values
, components
);
456 if (this->mode
== ir_var_shader_out
) {
457 ir_assignment
*assignment
458 = this->bitwise_assign_pack(swizzle
, rvalue
);
459 this->out_instructions
->push_tail(assignment
);
461 ir_assignment
*assignment
462 = this->bitwise_assign_unpack(rvalue
, swizzle
);
463 this->out_instructions
->push_tail(assignment
);
465 return fine_location
+ components
;
470 * Recursively pack or unpack a varying for which we need to iterate over its
471 * constituent elements, accessing each one using an ir_dereference_array.
472 * This takes care of both arrays and matrices, since ir_dereference_array
473 * treats a matrix like an array of its column vectors.
475 * \param gs_input_toplevel should be set to true if we are lowering geometry
476 * shader inputs, and we are currently lowering the whole input variable
477 * (i.e. we are lowering the array whose index selects the vertex).
479 * \param vertex_index: if we are lowering geometry shader inputs, and the
480 * level of the array that we are currently lowering is *not* the top level,
481 * then this indicates which vertex we are currently lowering. Otherwise it
485 lower_packed_varyings_visitor::lower_arraylike(ir_rvalue
*rvalue
,
487 unsigned fine_location
,
488 ir_variable
*unpacked_var
,
490 bool gs_input_toplevel
,
491 unsigned vertex_index
)
493 for (unsigned i
= 0; i
< array_size
; i
++) {
495 rvalue
= rvalue
->clone(this->mem_ctx
, NULL
);
496 ir_constant
*constant
= new(this->mem_ctx
) ir_constant(i
);
497 ir_dereference_array
*dereference_array
= new(this->mem_ctx
)
498 ir_dereference_array(rvalue
, constant
);
499 if (gs_input_toplevel
) {
500 /* Geometry shader inputs are a special case. Instead of storing
501 * each element of the array at a different location, all elements
502 * are at the same location, but with a different vertex index.
504 (void) this->lower_rvalue(dereference_array
, fine_location
,
505 unpacked_var
, name
, false, i
);
507 char *subscripted_name
508 = ralloc_asprintf(this->mem_ctx
, "%s[%d]", name
, i
);
510 this->lower_rvalue(dereference_array
, fine_location
,
511 unpacked_var
, subscripted_name
,
512 false, vertex_index
);
515 return fine_location
;
519 * Retrieve the packed varying corresponding to the given varying location.
520 * If no packed varying has been created for the given varying location yet,
521 * create it and add it to the shader before returning it.
523 * The newly created varying inherits its interpolation parameters from \c
524 * unpacked_var. Its base type is ivec4 if we are lowering a flat varying,
527 * \param vertex_index: if we are lowering geometry shader inputs, then this
528 * indicates which vertex we are currently lowering. Otherwise it is ignored.
531 lower_packed_varyings_visitor::get_packed_varying_deref(
532 unsigned location
, ir_variable
*unpacked_var
, const char *name
,
533 unsigned vertex_index
)
535 unsigned slot
= location
- VARYING_SLOT_VAR0
;
536 assert(slot
< locations_used
);
537 if (this->packed_varyings
[slot
] == NULL
) {
538 char *packed_name
= ralloc_asprintf(this->mem_ctx
, "packed:%s", name
);
539 const glsl_type
*packed_type
;
540 if (unpacked_var
->data
.interpolation
== INTERP_QUALIFIER_FLAT
)
541 packed_type
= glsl_type::ivec4_type
;
543 packed_type
= glsl_type::vec4_type
;
544 if (this->gs_input_vertices
!= 0) {
546 glsl_type::get_array_instance(packed_type
,
547 this->gs_input_vertices
);
549 ir_variable
*packed_var
= new(this->mem_ctx
)
550 ir_variable(packed_type
, packed_name
, this->mode
);
551 if (this->gs_input_vertices
!= 0) {
552 /* Prevent update_array_sizes() from messing with the size of the
555 packed_var
->data
.max_array_access
= this->gs_input_vertices
- 1;
557 packed_var
->data
.centroid
= unpacked_var
->data
.centroid
;
558 packed_var
->data
.sample
= unpacked_var
->data
.sample
;
559 packed_var
->data
.interpolation
= unpacked_var
->data
.interpolation
;
560 packed_var
->data
.location
= location
;
561 unpacked_var
->insert_before(packed_var
);
562 this->packed_varyings
[slot
] = packed_var
;
564 /* For geometry shader inputs, only update the packed variable name the
565 * first time we visit each component.
567 if (this->gs_input_vertices
== 0 || vertex_index
== 0) {
568 ralloc_asprintf_append((char **) &this->packed_varyings
[slot
]->name
,
573 ir_dereference
*deref
= new(this->mem_ctx
)
574 ir_dereference_variable(this->packed_varyings
[slot
]);
575 if (this->gs_input_vertices
!= 0) {
576 /* When lowering GS inputs, the packed variable is an array, so we need
577 * to dereference it using vertex_index.
579 ir_constant
*constant
= new(this->mem_ctx
) ir_constant(vertex_index
);
580 deref
= new(this->mem_ctx
) ir_dereference_array(deref
, constant
);
586 lower_packed_varyings_visitor::needs_lowering(ir_variable
*var
)
588 /* Things composed of vec4's and varyings with explicitly assigned
589 * locations don't need lowering. Everything else does.
591 if (var
->data
.explicit_location
)
594 const glsl_type
*type
= var
->type
;
595 if (this->gs_input_vertices
!= 0) {
596 assert(type
->is_array());
597 type
= type
->element_type();
599 if (type
->is_array())
600 type
= type
->fields
.array
;
601 if (type
->vector_elements
== 4)
608 * Visitor that splices varying packing code before every use of EmitVertex()
609 * in a geometry shader.
611 class lower_packed_varyings_gs_splicer
: public ir_hierarchical_visitor
614 explicit lower_packed_varyings_gs_splicer(void *mem_ctx
,
615 const exec_list
*instructions
);
617 virtual ir_visitor_status
visit_leave(ir_emit_vertex
*ev
);
621 * Memory context used to allocate new instructions for the shader.
623 void * const mem_ctx
;
626 * Instructions that should be spliced into place before each EmitVertex()
629 const exec_list
*instructions
;
633 lower_packed_varyings_gs_splicer::lower_packed_varyings_gs_splicer(
634 void *mem_ctx
, const exec_list
*instructions
)
635 : mem_ctx(mem_ctx
), instructions(instructions
)
641 lower_packed_varyings_gs_splicer::visit_leave(ir_emit_vertex
*ev
)
643 foreach_in_list(ir_instruction
, ir
, this->instructions
) {
644 ev
->insert_before(ir
->clone(this->mem_ctx
, NULL
));
646 return visit_continue
;
651 lower_packed_varyings(void *mem_ctx
, unsigned locations_used
,
652 ir_variable_mode mode
, unsigned gs_input_vertices
,
655 exec_list
*instructions
= shader
->ir
;
656 ir_function
*main_func
= shader
->symbols
->get_function("main");
657 exec_list void_parameters
;
658 ir_function_signature
*main_func_sig
659 = main_func
->matching_signature(NULL
, &void_parameters
, false);
660 exec_list new_instructions
;
661 lower_packed_varyings_visitor
visitor(mem_ctx
, locations_used
, mode
,
662 gs_input_vertices
, &new_instructions
);
663 visitor
.run(instructions
);
664 if (mode
== ir_var_shader_out
) {
665 if (shader
->Stage
== MESA_SHADER_GEOMETRY
) {
666 /* For geometry shaders, outputs need to be lowered before each call
669 lower_packed_varyings_gs_splicer
splicer(mem_ctx
, &new_instructions
);
670 splicer
.run(instructions
);
672 /* For other shader types, outputs need to be lowered at the end of
675 main_func_sig
->body
.append_list(&new_instructions
);
678 /* Shader inputs need to be lowered at the beginning of main() */
679 main_func_sig
->body
.head
->insert_before(&new_instructions
);