glsl: Update lower_packed_varyings to handle varying structs.
[mesa.git] / src / glsl / lower_packed_varyings.cpp
1 /*
2 * Copyright © 2011 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 /**
25 * \file lower_varyings_to_packed.cpp
26 *
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
29 * natively.
30 *
31 * For example, the following shader:
32 *
33 * out mat3x2 foo; // location=4, location_frac=0
34 * out vec3 bar[2]; // location=5, location_frac=2
35 *
36 * main()
37 * {
38 * ...
39 * }
40 *
41 * Is rewritten to:
42 *
43 * mat3x2 foo;
44 * vec3 bar[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
48 *
49 * main()
50 * {
51 * ...
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];
58 * }
59 *
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
63 * stored in packed6.
64 *
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).
69 *
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.
73 *
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.
77 */
78
79 #include "glsl_symbol_table.h"
80 #include "ir.h"
81 #include "ir_optimization.h"
82
83 /**
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
88 * packed varying.
89 */
90 class lower_packed_varyings_visitor
91 {
92 public:
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);
97
98 void run(exec_list *instructions);
99
100 private:
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,
110 const char *name);
111 bool needs_lowering(ir_variable *var);
112
113 /**
114 * Memory context used to allocate new instructions for the shader.
115 */
116 void * const mem_ctx;
117
118 /**
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.
123 */
124 const unsigned location_base;
125
126 /**
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.
131 */
132 const unsigned locations_used;
133
134 /**
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.
138 */
139 ir_variable **packed_varyings;
140
141 /**
142 * Type of varying which is being lowered in this pass (either
143 * ir_var_shader_in or ir_var_shader_out).
144 */
145 const ir_variable_mode mode;
146
147 /**
148 * List of instructions corresponding to the main() function. This is
149 * where we add instructions to pack or unpack the varyings.
150 */
151 exec_list *main_instructions;
152 };
153
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)
157 : mem_ctx(mem_ctx),
158 location_base(location_base),
159 locations_used(locations_used),
160 packed_varyings((ir_variable **)
161 rzalloc_array_size(mem_ctx, sizeof(*packed_varyings),
162 locations_used)),
163 mode(mode),
164 main_instructions(main_instructions)
165 {
166 }
167
168 void
169 lower_packed_varyings_visitor::run(exec_list *instructions)
170 {
171 foreach_list (node, instructions) {
172 ir_variable *var = ((ir_instruction *) node)->as_variable();
173 if (var == NULL)
174 continue;
175
176 if (var->mode != this->mode ||
177 var->location < (int) this->location_base ||
178 !this->needs_lowering(var))
179 continue;
180
181 /* Change the old varying into an ordinary global. */
182 var->mode = ir_var_auto;
183
184 /* Create a reference to the old varying. */
185 ir_dereference_variable *deref
186 = new(this->mem_ctx) ir_dereference_variable(var);
187
188 /* Recursively pack or unpack it. */
189 this->lower_rvalue(deref, var->location * 4 + var->location_frac, var,
190 var->name);
191 }
192 }
193
194
195 /**
196 * Make an ir_assignment from \c rhs to \c lhs, performing appropriate
197 * bitcasts if necessary to match up types.
198 *
199 * This function is called when packing varyings.
200 */
201 ir_assignment *
202 lower_packed_varyings_visitor::bitwise_assign_pack(ir_rvalue *lhs,
203 ir_rvalue *rhs)
204 {
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
208 * or float) to int.
209 */
210 assert(lhs->type->base_type == GLSL_TYPE_INT);
211 switch (rhs->type->base_type) {
212 case GLSL_TYPE_UINT:
213 rhs = new(this->mem_ctx)
214 ir_expression(ir_unop_u2i, lhs->type, rhs);
215 break;
216 case GLSL_TYPE_FLOAT:
217 rhs = new(this->mem_ctx)
218 ir_expression(ir_unop_bitcast_f2i, lhs->type, rhs);
219 break;
220 default:
221 assert(!"Unexpected type conversion while lowering varyings");
222 break;
223 }
224 }
225 return new(this->mem_ctx) ir_assignment(lhs, rhs);
226 }
227
228
229 /**
230 * Make an ir_assignment from \c rhs to \c lhs, performing appropriate
231 * bitcasts if necessary to match up types.
232 *
233 * This function is called when unpacking varyings.
234 */
235 ir_assignment *
236 lower_packed_varyings_visitor::bitwise_assign_unpack(ir_rvalue *lhs,
237 ir_rvalue *rhs)
238 {
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
242 * (uint or float).
243 */
244 assert(rhs->type->base_type == GLSL_TYPE_INT);
245 switch (lhs->type->base_type) {
246 case GLSL_TYPE_UINT:
247 rhs = new(this->mem_ctx)
248 ir_expression(ir_unop_i2u, lhs->type, rhs);
249 break;
250 case GLSL_TYPE_FLOAT:
251 rhs = new(this->mem_ctx)
252 ir_expression(ir_unop_bitcast_i2f, lhs->type, rhs);
253 break;
254 default:
255 assert(!"Unexpected type conversion while lowering varyings");
256 break;
257 }
258 }
259 return new(this->mem_ctx) ir_assignment(lhs, rhs);
260 }
261
262
263 /**
264 * Recursively pack or unpack the given varying (or portion of a varying) by
265 * traversing all of its constituent vectors.
266 *
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
270 * elsewhere in Mesa.
271 *
272 * \return the location where the next constituent vector (after this one)
273 * should be packed.
274 */
275 unsigned
276 lower_packed_varyings_visitor::lower_rvalue(ir_rvalue *rvalue,
277 unsigned fine_location,
278 ir_variable *unpacked_var,
279 const char *name)
280 {
281 if (rvalue->type->is_record()) {
282 for (unsigned i = 0; i < rvalue->type->length; i++) {
283 if (i != 0)
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);
288 char *deref_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);
292 }
293 return fine_location;
294 } else if (rvalue->type->is_array()) {
295 /* Arrays are packed/unpacked by considering each array element in
296 * sequence.
297 */
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
302 * sequence.
303 */
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.
309 */
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];
320 }
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];
324 }
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,
329 right_components);
330 char *left_name
331 = ralloc_asprintf(this->mem_ctx, "%s.%s", name, left_swizzle_name);
332 char *right_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,
337 right_name);
338 } else {
339 /* No special handling is necessary; pack the rvalue into the
340 * varying.
341 */
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);
357 } else {
358 ir_assignment *assignment
359 = this->bitwise_assign_unpack(rvalue, swizzle);
360 this->main_instructions->push_head(assignment);
361 }
362 return fine_location + components;
363 }
364 }
365
366 /**
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.
371 */
372 unsigned
373 lower_packed_varyings_visitor::lower_arraylike(ir_rvalue *rvalue,
374 unsigned array_size,
375 unsigned fine_location,
376 ir_variable *unpacked_var,
377 const char *name)
378 {
379 for (unsigned i = 0; i < array_size; i++) {
380 if (i != 0)
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);
389 }
390 return fine_location;
391 }
392
393 /**
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.
397 *
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,
400 * vec4 otherwise.
401 */
402 ir_variable *
403 lower_packed_varyings_visitor::get_packed_varying(unsigned location,
404 ir_variable *unpacked_var,
405 const char *name)
406 {
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;
414 else
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;
423 } else {
424 ralloc_asprintf_append((char **) &this->packed_varyings[slot]->name,
425 ",%s", name);
426 }
427 return this->packed_varyings[slot];
428 }
429
430 bool
431 lower_packed_varyings_visitor::needs_lowering(ir_variable *var)
432 {
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)
438 return false;
439 return true;
440 }
441
442 void
443 lower_packed_varyings(void *mem_ctx, unsigned location_base,
444 unsigned locations_used, ir_variable_mode mode,
445 gl_shader *shader)
446 {
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,
455 main_instructions);
456 visitor.run(instructions);
457 }