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. VERT_RESULT_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 /* Change the old varying into an ordinary global. */
182 var
->mode
= ir_var_auto
;
184 /* Create a reference to the old varying. */
185 ir_dereference_variable
*deref
186 = new(this->mem_ctx
) ir_dereference_variable(var
);
188 /* Recursively pack or unpack it. */
189 this->lower_rvalue(deref
, var
->location
* 4 + var
->location_frac
, var
,
196 * Make an ir_assignment from \c rhs to \c lhs, performing appropriate
197 * bitcasts if necessary to match up types.
199 * This function is called when packing varyings.
202 lower_packed_varyings_visitor::bitwise_assign_pack(ir_rvalue
*lhs
,
205 if (lhs
->type
->base_type
!= rhs
->type
->base_type
) {
206 /* Since we only mix types in flat varyings, and we always store flat
207 * varyings as type ivec4, we need only produce conversions from (uint
210 assert(lhs
->type
->base_type
== GLSL_TYPE_INT
);
211 switch (rhs
->type
->base_type
) {
213 rhs
= new(this->mem_ctx
)
214 ir_expression(ir_unop_u2i
, lhs
->type
, rhs
);
216 case GLSL_TYPE_FLOAT
:
217 rhs
= new(this->mem_ctx
)
218 ir_expression(ir_unop_bitcast_f2i
, lhs
->type
, rhs
);
221 assert(!"Unexpected type conversion while lowering varyings");
225 return new(this->mem_ctx
) ir_assignment(lhs
, rhs
);
230 * Make an ir_assignment from \c rhs to \c lhs, performing appropriate
231 * bitcasts if necessary to match up types.
233 * This function is called when unpacking varyings.
236 lower_packed_varyings_visitor::bitwise_assign_unpack(ir_rvalue
*lhs
,
239 if (lhs
->type
->base_type
!= rhs
->type
->base_type
) {
240 /* Since we only mix types in flat varyings, and we always store flat
241 * varyings as type ivec4, we need only produce conversions from int to
244 assert(rhs
->type
->base_type
== GLSL_TYPE_INT
);
245 switch (lhs
->type
->base_type
) {
247 rhs
= new(this->mem_ctx
)
248 ir_expression(ir_unop_i2u
, lhs
->type
, rhs
);
250 case GLSL_TYPE_FLOAT
:
251 rhs
= new(this->mem_ctx
)
252 ir_expression(ir_unop_bitcast_i2f
, lhs
->type
, rhs
);
255 assert(!"Unexpected type conversion while lowering varyings");
259 return new(this->mem_ctx
) ir_assignment(lhs
, rhs
);
264 * Recursively pack or unpack the given varying (or portion of a varying) by
265 * traversing all of its constituent vectors.
267 * \param fine_location is the location where the first constituent vector
268 * should be packed--the word "fine" indicates that this location is expressed
269 * in multiples of a float, rather than multiples of a vec4 as is used
272 * \return the location where the next constituent vector (after this one)
276 lower_packed_varyings_visitor::lower_rvalue(ir_rvalue
*rvalue
,
277 unsigned fine_location
,
278 ir_variable
*unpacked_var
,
281 if (rvalue
->type
->is_record()) {
282 for (unsigned i
= 0; i
< rvalue
->type
->length
; i
++) {
284 rvalue
= rvalue
->clone(this->mem_ctx
, NULL
);
285 const char *field_name
= rvalue
->type
->fields
.structure
[i
].name
;
286 ir_dereference_record
*dereference_record
= new(this->mem_ctx
)
287 ir_dereference_record(rvalue
, field_name
);
289 = ralloc_asprintf(this->mem_ctx
, "%s.%s", name
, field_name
);
290 fine_location
= this->lower_rvalue(dereference_record
, fine_location
,
291 unpacked_var
, deref_name
);
293 return fine_location
;
294 } else if (rvalue
->type
->is_array()) {
295 /* Arrays are packed/unpacked by considering each array element in
298 return this->lower_arraylike(rvalue
, rvalue
->type
->array_size(),
299 fine_location
, unpacked_var
, name
);
300 } else if (rvalue
->type
->is_matrix()) {
301 /* Matrices are packed/unpacked by considering each column vector in
304 return this->lower_arraylike(rvalue
, rvalue
->type
->matrix_columns
,
305 fine_location
, unpacked_var
, name
);
306 } else if (rvalue
->type
->vector_elements
+ fine_location
% 4 > 4) {
307 /* This vector is going to be "double parked" across two varying slots,
308 * so handle it as two separate assignments.
310 unsigned left_components
= 4 - fine_location
% 4;
311 unsigned right_components
312 = rvalue
->type
->vector_elements
- left_components
;
313 unsigned left_swizzle_values
[4] = { 0, 0, 0, 0 };
314 unsigned right_swizzle_values
[4] = { 0, 0, 0, 0 };
315 char left_swizzle_name
[4] = { 0, 0, 0, 0 };
316 char right_swizzle_name
[4] = { 0, 0, 0, 0 };
317 for (unsigned i
= 0; i
< left_components
; i
++) {
318 left_swizzle_values
[i
] = i
;
319 left_swizzle_name
[i
] = "xyzw"[i
];
321 for (unsigned i
= 0; i
< right_components
; i
++) {
322 right_swizzle_values
[i
] = i
+ left_components
;
323 right_swizzle_name
[i
] = "xyzw"[i
+ left_components
];
325 ir_swizzle
*left_swizzle
= new(this->mem_ctx
)
326 ir_swizzle(rvalue
, left_swizzle_values
, left_components
);
327 ir_swizzle
*right_swizzle
= new(this->mem_ctx
)
328 ir_swizzle(rvalue
->clone(this->mem_ctx
, NULL
), right_swizzle_values
,
331 = ralloc_asprintf(this->mem_ctx
, "%s.%s", name
, left_swizzle_name
);
333 = ralloc_asprintf(this->mem_ctx
, "%s.%s", name
, right_swizzle_name
);
334 fine_location
= this->lower_rvalue(left_swizzle
, fine_location
,
335 unpacked_var
, left_name
);
336 return this->lower_rvalue(right_swizzle
, fine_location
, unpacked_var
,
339 /* No special handling is necessary; pack the rvalue into the
342 unsigned swizzle_values
[4] = { 0, 0, 0, 0 };
343 unsigned components
= rvalue
->type
->vector_elements
;
344 unsigned location
= fine_location
/ 4;
345 unsigned location_frac
= fine_location
% 4;
346 for (unsigned i
= 0; i
< components
; ++i
)
347 swizzle_values
[i
] = i
+ location_frac
;
348 ir_dereference_variable
*packed_deref
= new(this->mem_ctx
)
349 ir_dereference_variable(this->get_packed_varying(location
,
350 unpacked_var
, name
));
351 ir_swizzle
*swizzle
= new(this->mem_ctx
)
352 ir_swizzle(packed_deref
, swizzle_values
, components
);
353 if (this->mode
== ir_var_shader_out
) {
354 ir_assignment
*assignment
355 = this->bitwise_assign_pack(swizzle
, rvalue
);
356 this->main_instructions
->push_tail(assignment
);
358 ir_assignment
*assignment
359 = this->bitwise_assign_unpack(rvalue
, swizzle
);
360 this->main_instructions
->push_head(assignment
);
362 return fine_location
+ components
;
367 * Recursively pack or unpack a varying for which we need to iterate over its
368 * constituent elements, accessing each one using an ir_dereference_array.
369 * This takes care of both arrays and matrices, since ir_dereference_array
370 * treats a matrix like an array of its column vectors.
373 lower_packed_varyings_visitor::lower_arraylike(ir_rvalue
*rvalue
,
375 unsigned fine_location
,
376 ir_variable
*unpacked_var
,
379 for (unsigned i
= 0; i
< array_size
; i
++) {
381 rvalue
= rvalue
->clone(this->mem_ctx
, NULL
);
382 ir_constant
*constant
= new(this->mem_ctx
) ir_constant(i
);
383 ir_dereference_array
*dereference_array
= new(this->mem_ctx
)
384 ir_dereference_array(rvalue
, constant
);
385 char *subscripted_name
386 = ralloc_asprintf(this->mem_ctx
, "%s[%d]", name
, i
);
387 fine_location
= this->lower_rvalue(dereference_array
, fine_location
,
388 unpacked_var
, subscripted_name
);
390 return fine_location
;
394 * Retrieve the packed varying corresponding to the given varying location.
395 * If no packed varying has been created for the given varying location yet,
396 * create it and add it to the shader before returning it.
398 * The newly created varying inherits its interpolation parameters from \c
399 * unpacked_var. Its base type is ivec4 if we are lowering a flat varying,
403 lower_packed_varyings_visitor::get_packed_varying(unsigned location
,
404 ir_variable
*unpacked_var
,
407 unsigned slot
= location
- this->location_base
;
408 assert(slot
< locations_used
);
409 if (this->packed_varyings
[slot
] == NULL
) {
410 char *packed_name
= ralloc_asprintf(this->mem_ctx
, "packed:%s", name
);
411 const glsl_type
*packed_type
;
412 if (unpacked_var
->interpolation
== INTERP_QUALIFIER_FLAT
)
413 packed_type
= glsl_type::ivec4_type
;
415 packed_type
= glsl_type::vec4_type
;
416 ir_variable
*packed_var
= new(this->mem_ctx
)
417 ir_variable(packed_type
, packed_name
, this->mode
);
418 packed_var
->centroid
= unpacked_var
->centroid
;
419 packed_var
->interpolation
= unpacked_var
->interpolation
;
420 packed_var
->location
= location
;
421 unpacked_var
->insert_before(packed_var
);
422 this->packed_varyings
[slot
] = packed_var
;
424 ralloc_asprintf_append((char **) &this->packed_varyings
[slot
]->name
,
427 return this->packed_varyings
[slot
];
431 lower_packed_varyings_visitor::needs_lowering(ir_variable
*var
)
433 /* Things composed of vec4's don't need lowering. Everything else does. */
434 const glsl_type
*type
= var
->type
;
435 if (type
->is_array())
436 type
= type
->fields
.array
;
437 if (type
->vector_elements
== 4)
443 lower_packed_varyings(void *mem_ctx
, unsigned location_base
,
444 unsigned locations_used
, ir_variable_mode mode
,
447 exec_list
*instructions
= shader
->ir
;
448 ir_function
*main_func
= shader
->symbols
->get_function("main");
449 exec_list void_parameters
;
450 ir_function_signature
*main_func_sig
451 = main_func
->matching_signature(&void_parameters
);
452 exec_list
*main_instructions
= &main_func_sig
->body
;
453 lower_packed_varyings_visitor
visitor(mem_ctx
, location_base
,
454 locations_used
, mode
,
456 visitor
.run(instructions
);