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.
79 #include "glsl_symbol_table.h"
81 #include "ir_optimization.h"
84 * Visitor that performs varying packing. For each varying declared in the
85 * shader, this visitor determines whether it needs to be packed. If so, it
86 * demotes it to an ordinary global, creates new packed varyings, and
87 * generates assignments to convert between the original varying and the
90 class lower_packed_varyings_visitor
93 lower_packed_varyings_visitor(void *mem_ctx
, unsigned location_base
,
94 unsigned locations_used
,
95 ir_variable_mode mode
,
96 exec_list
*main_instructions
);
98 void run(exec_list
*instructions
);
101 ir_assignment
*bitwise_assign_pack(ir_rvalue
*lhs
, ir_rvalue
*rhs
);
102 ir_assignment
*bitwise_assign_unpack(ir_rvalue
*lhs
, ir_rvalue
*rhs
);
103 unsigned lower_rvalue(ir_rvalue
*rvalue
, unsigned fine_location
,
104 ir_variable
*unpacked_var
, const char *name
);
105 unsigned lower_arraylike(ir_rvalue
*rvalue
, unsigned array_size
,
106 unsigned fine_location
,
107 ir_variable
*unpacked_var
, const char *name
);
108 ir_variable
*get_packed_varying(unsigned location
,
109 ir_variable
*unpacked_var
,
111 bool needs_lowering(ir_variable
*var
);
114 * Memory context used to allocate new instructions for the shader.
116 void * const mem_ctx
;
119 * Location representing the first generic varying slot for this shader
120 * stage (e.g. VARYING_SLOT_VAR0 if we are packing vertex shader outputs).
121 * Varyings whose location is less than this value are assumed to
122 * correspond to special fixed function hardware, so they are not lowered.
124 const unsigned location_base
;
127 * Number of generic varying slots which are used by this shader. This is
128 * used to allocate temporary intermediate data structures. If any any
129 * varying used by this shader has a location greater than or equal to
130 * location_base + locations_used, an assertion will fire.
132 const unsigned locations_used
;
135 * Array of pointers to the packed varyings that have been created for each
136 * generic varying slot. NULL entries in this array indicate varying slots
137 * for which a packed varying has not been created yet.
139 ir_variable
**packed_varyings
;
142 * Type of varying which is being lowered in this pass (either
143 * ir_var_shader_in or ir_var_shader_out).
145 const ir_variable_mode mode
;
148 * List of instructions corresponding to the main() function. This is
149 * where we add instructions to pack or unpack the varyings.
151 exec_list
*main_instructions
;
154 lower_packed_varyings_visitor::lower_packed_varyings_visitor(
155 void *mem_ctx
, unsigned location_base
, unsigned locations_used
,
156 ir_variable_mode mode
, exec_list
*main_instructions
)
158 location_base(location_base
),
159 locations_used(locations_used
),
160 packed_varyings((ir_variable
**)
161 rzalloc_array_size(mem_ctx
, sizeof(*packed_varyings
),
164 main_instructions(main_instructions
)
169 lower_packed_varyings_visitor::run(exec_list
*instructions
)
171 foreach_list (node
, instructions
) {
172 ir_variable
*var
= ((ir_instruction
*) node
)->as_variable();
176 if (var
->mode
!= this->mode
||
177 var
->location
< (int) this->location_base
||
178 !this->needs_lowering(var
))
181 /* This lowering pass is only capable of packing floats and ints
182 * together when their interpolation mode is "flat". Therefore, to be
183 * safe, caller should ensure that integral varyings always use flat
184 * interpolation, even when this is not required by GLSL.
186 assert(var
->interpolation
== INTERP_QUALIFIER_FLAT
||
187 !var
->type
->contains_integer());
189 /* Change the old varying into an ordinary global. */
190 var
->mode
= ir_var_auto
;
192 /* Create a reference to the old varying. */
193 ir_dereference_variable
*deref
194 = new(this->mem_ctx
) ir_dereference_variable(var
);
196 /* Recursively pack or unpack it. */
197 this->lower_rvalue(deref
, var
->location
* 4 + var
->location_frac
, var
,
204 * Make an ir_assignment from \c rhs to \c lhs, performing appropriate
205 * bitcasts if necessary to match up types.
207 * This function is called when packing varyings.
210 lower_packed_varyings_visitor::bitwise_assign_pack(ir_rvalue
*lhs
,
213 if (lhs
->type
->base_type
!= rhs
->type
->base_type
) {
214 /* Since we only mix types in flat varyings, and we always store flat
215 * varyings as type ivec4, we need only produce conversions from (uint
218 assert(lhs
->type
->base_type
== GLSL_TYPE_INT
);
219 switch (rhs
->type
->base_type
) {
221 rhs
= new(this->mem_ctx
)
222 ir_expression(ir_unop_u2i
, lhs
->type
, rhs
);
224 case GLSL_TYPE_FLOAT
:
225 rhs
= new(this->mem_ctx
)
226 ir_expression(ir_unop_bitcast_f2i
, lhs
->type
, rhs
);
229 assert(!"Unexpected type conversion while lowering varyings");
233 return new(this->mem_ctx
) ir_assignment(lhs
, rhs
);
238 * Make an ir_assignment from \c rhs to \c lhs, performing appropriate
239 * bitcasts if necessary to match up types.
241 * This function is called when unpacking varyings.
244 lower_packed_varyings_visitor::bitwise_assign_unpack(ir_rvalue
*lhs
,
247 if (lhs
->type
->base_type
!= rhs
->type
->base_type
) {
248 /* Since we only mix types in flat varyings, and we always store flat
249 * varyings as type ivec4, we need only produce conversions from int to
252 assert(rhs
->type
->base_type
== GLSL_TYPE_INT
);
253 switch (lhs
->type
->base_type
) {
255 rhs
= new(this->mem_ctx
)
256 ir_expression(ir_unop_i2u
, lhs
->type
, rhs
);
258 case GLSL_TYPE_FLOAT
:
259 rhs
= new(this->mem_ctx
)
260 ir_expression(ir_unop_bitcast_i2f
, lhs
->type
, rhs
);
263 assert(!"Unexpected type conversion while lowering varyings");
267 return new(this->mem_ctx
) ir_assignment(lhs
, rhs
);
272 * Recursively pack or unpack the given varying (or portion of a varying) by
273 * traversing all of its constituent vectors.
275 * \param fine_location is the location where the first constituent vector
276 * should be packed--the word "fine" indicates that this location is expressed
277 * in multiples of a float, rather than multiples of a vec4 as is used
280 * \return the location where the next constituent vector (after this one)
284 lower_packed_varyings_visitor::lower_rvalue(ir_rvalue
*rvalue
,
285 unsigned fine_location
,
286 ir_variable
*unpacked_var
,
289 if (rvalue
->type
->is_record()) {
290 for (unsigned i
= 0; i
< rvalue
->type
->length
; i
++) {
292 rvalue
= rvalue
->clone(this->mem_ctx
, NULL
);
293 const char *field_name
= rvalue
->type
->fields
.structure
[i
].name
;
294 ir_dereference_record
*dereference_record
= new(this->mem_ctx
)
295 ir_dereference_record(rvalue
, field_name
);
297 = ralloc_asprintf(this->mem_ctx
, "%s.%s", name
, field_name
);
298 fine_location
= this->lower_rvalue(dereference_record
, fine_location
,
299 unpacked_var
, deref_name
);
301 return fine_location
;
302 } else if (rvalue
->type
->is_array()) {
303 /* Arrays are packed/unpacked by considering each array element in
306 return this->lower_arraylike(rvalue
, rvalue
->type
->array_size(),
307 fine_location
, unpacked_var
, name
);
308 } else if (rvalue
->type
->is_matrix()) {
309 /* Matrices are packed/unpacked by considering each column vector in
312 return this->lower_arraylike(rvalue
, rvalue
->type
->matrix_columns
,
313 fine_location
, unpacked_var
, name
);
314 } else if (rvalue
->type
->vector_elements
+ fine_location
% 4 > 4) {
315 /* This vector is going to be "double parked" across two varying slots,
316 * so handle it as two separate assignments.
318 unsigned left_components
= 4 - fine_location
% 4;
319 unsigned right_components
320 = rvalue
->type
->vector_elements
- left_components
;
321 unsigned left_swizzle_values
[4] = { 0, 0, 0, 0 };
322 unsigned right_swizzle_values
[4] = { 0, 0, 0, 0 };
323 char left_swizzle_name
[4] = { 0, 0, 0, 0 };
324 char right_swizzle_name
[4] = { 0, 0, 0, 0 };
325 for (unsigned i
= 0; i
< left_components
; i
++) {
326 left_swizzle_values
[i
] = i
;
327 left_swizzle_name
[i
] = "xyzw"[i
];
329 for (unsigned i
= 0; i
< right_components
; i
++) {
330 right_swizzle_values
[i
] = i
+ left_components
;
331 right_swizzle_name
[i
] = "xyzw"[i
+ left_components
];
333 ir_swizzle
*left_swizzle
= new(this->mem_ctx
)
334 ir_swizzle(rvalue
, left_swizzle_values
, left_components
);
335 ir_swizzle
*right_swizzle
= new(this->mem_ctx
)
336 ir_swizzle(rvalue
->clone(this->mem_ctx
, NULL
), right_swizzle_values
,
339 = ralloc_asprintf(this->mem_ctx
, "%s.%s", name
, left_swizzle_name
);
341 = ralloc_asprintf(this->mem_ctx
, "%s.%s", name
, right_swizzle_name
);
342 fine_location
= this->lower_rvalue(left_swizzle
, fine_location
,
343 unpacked_var
, left_name
);
344 return this->lower_rvalue(right_swizzle
, fine_location
, unpacked_var
,
347 /* No special handling is necessary; pack the rvalue into the
350 unsigned swizzle_values
[4] = { 0, 0, 0, 0 };
351 unsigned components
= rvalue
->type
->vector_elements
;
352 unsigned location
= fine_location
/ 4;
353 unsigned location_frac
= fine_location
% 4;
354 for (unsigned i
= 0; i
< components
; ++i
)
355 swizzle_values
[i
] = i
+ location_frac
;
356 ir_dereference_variable
*packed_deref
= new(this->mem_ctx
)
357 ir_dereference_variable(this->get_packed_varying(location
,
358 unpacked_var
, name
));
359 ir_swizzle
*swizzle
= new(this->mem_ctx
)
360 ir_swizzle(packed_deref
, swizzle_values
, components
);
361 if (this->mode
== ir_var_shader_out
) {
362 ir_assignment
*assignment
363 = this->bitwise_assign_pack(swizzle
, rvalue
);
364 this->main_instructions
->push_tail(assignment
);
366 ir_assignment
*assignment
367 = this->bitwise_assign_unpack(rvalue
, swizzle
);
368 this->main_instructions
->push_head(assignment
);
370 return fine_location
+ components
;
375 * Recursively pack or unpack a varying for which we need to iterate over its
376 * constituent elements, accessing each one using an ir_dereference_array.
377 * This takes care of both arrays and matrices, since ir_dereference_array
378 * treats a matrix like an array of its column vectors.
381 lower_packed_varyings_visitor::lower_arraylike(ir_rvalue
*rvalue
,
383 unsigned fine_location
,
384 ir_variable
*unpacked_var
,
387 for (unsigned i
= 0; i
< array_size
; i
++) {
389 rvalue
= rvalue
->clone(this->mem_ctx
, NULL
);
390 ir_constant
*constant
= new(this->mem_ctx
) ir_constant(i
);
391 ir_dereference_array
*dereference_array
= new(this->mem_ctx
)
392 ir_dereference_array(rvalue
, constant
);
393 char *subscripted_name
394 = ralloc_asprintf(this->mem_ctx
, "%s[%d]", name
, i
);
395 fine_location
= this->lower_rvalue(dereference_array
, fine_location
,
396 unpacked_var
, subscripted_name
);
398 return fine_location
;
402 * Retrieve the packed varying corresponding to the given varying location.
403 * If no packed varying has been created for the given varying location yet,
404 * create it and add it to the shader before returning it.
406 * The newly created varying inherits its interpolation parameters from \c
407 * unpacked_var. Its base type is ivec4 if we are lowering a flat varying,
411 lower_packed_varyings_visitor::get_packed_varying(unsigned location
,
412 ir_variable
*unpacked_var
,
415 unsigned slot
= location
- this->location_base
;
416 assert(slot
< locations_used
);
417 if (this->packed_varyings
[slot
] == NULL
) {
418 char *packed_name
= ralloc_asprintf(this->mem_ctx
, "packed:%s", name
);
419 const glsl_type
*packed_type
;
420 if (unpacked_var
->interpolation
== INTERP_QUALIFIER_FLAT
)
421 packed_type
= glsl_type::ivec4_type
;
423 packed_type
= glsl_type::vec4_type
;
424 ir_variable
*packed_var
= new(this->mem_ctx
)
425 ir_variable(packed_type
, packed_name
, this->mode
);
426 packed_var
->centroid
= unpacked_var
->centroid
;
427 packed_var
->interpolation
= unpacked_var
->interpolation
;
428 packed_var
->location
= location
;
429 unpacked_var
->insert_before(packed_var
);
430 this->packed_varyings
[slot
] = packed_var
;
432 ralloc_asprintf_append((char **) &this->packed_varyings
[slot
]->name
,
435 return this->packed_varyings
[slot
];
439 lower_packed_varyings_visitor::needs_lowering(ir_variable
*var
)
441 /* Things composed of vec4's don't need lowering. Everything else does. */
442 const glsl_type
*type
= var
->type
;
443 if (type
->is_array())
444 type
= type
->fields
.array
;
445 if (type
->vector_elements
== 4)
451 lower_packed_varyings(void *mem_ctx
, unsigned location_base
,
452 unsigned locations_used
, ir_variable_mode mode
,
455 exec_list
*instructions
= shader
->ir
;
456 ir_function
*main_func
= shader
->symbols
->get_function("main");
457 exec_list void_parameters
;
458 ir_function_signature
*main_func_sig
459 = main_func
->matching_signature(&void_parameters
);
460 exec_list
*main_instructions
= &main_func_sig
->body
;
461 lower_packed_varyings_visitor
visitor(mem_ctx
, location_base
,
462 locations_used
, mode
,
464 visitor
.run(instructions
);