1122521e0dc31227d9019ee85f54e177ea364b10
[mesa.git] / src / glsl / ast_function.cpp
1 /*
2 * Copyright © 2010 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 #include "glsl_symbol_table.h"
25 #include "ast.h"
26 #include "glsl_types.h"
27 #include "ir.h"
28
29 inline unsigned min(unsigned a, unsigned b)
30 {
31 return (a < b) ? a : b;
32 }
33
34 static ir_rvalue *
35 convert_component(ir_rvalue *src, const glsl_type *desired_type);
36
37 static unsigned
38 process_parameters(exec_list *instructions, exec_list *actual_parameters,
39 exec_list *parameters,
40 struct _mesa_glsl_parse_state *state)
41 {
42 unsigned count = 0;
43
44 foreach_list (n, parameters) {
45 ast_node *const ast = exec_node_data(ast_node, n, link);
46 ir_rvalue *result = ast->hir(instructions, state);
47
48 ir_constant *const constant = result->constant_expression_value();
49 if (constant != NULL)
50 result = constant;
51
52 actual_parameters->push_tail(result);
53 count++;
54 }
55
56 return count;
57 }
58
59
60 static ir_rvalue *
61 process_call(exec_list *instructions, ir_function *f,
62 YYLTYPE *loc, exec_list *actual_parameters,
63 struct _mesa_glsl_parse_state *state)
64 {
65 void *ctx = state;
66
67 ir_function_signature *sig = f->matching_signature(actual_parameters);
68
69 /* The instructions param will be used when the FINISHMEs below are done */
70 (void) instructions;
71
72 if (sig != NULL) {
73 /* Verify that 'out' and 'inout' actual parameters are lvalues. This
74 * isn't done in ir_function::matching_signature because that function
75 * cannot generate the necessary diagnostics.
76 */
77 exec_list_iterator actual_iter = actual_parameters->iterator();
78 exec_list_iterator formal_iter = sig->parameters.iterator();
79
80 while (actual_iter.has_next()) {
81 ir_rvalue *actual = (ir_rvalue *) actual_iter.get();
82 ir_variable *formal = (ir_variable *) formal_iter.get();
83
84 assert(actual != NULL);
85 assert(formal != NULL);
86
87 if ((formal->mode == ir_var_out)
88 || (formal->mode == ir_var_inout)) {
89 if (! actual->is_lvalue()) {
90 /* FINISHME: Log a better diagnostic here. There is no way
91 * FINISHME: to tell the user which parameter is invalid.
92 */
93 _mesa_glsl_error(loc, state, "`%s' parameter is not lvalue",
94 (formal->mode == ir_var_out) ? "out" : "inout");
95 }
96 }
97
98 if (formal->type->is_numeric() || formal->type->is_boolean()) {
99 ir_rvalue *converted = convert_component(actual, formal->type);
100 actual->replace_with(converted);
101 }
102
103 actual_iter.next();
104 formal_iter.next();
105 }
106
107 /* Always insert the call in the instruction stream, and return a deref
108 * of its return val if it returns a value, since we don't know if
109 * the rvalue is going to be assigned to anything or not.
110 */
111 ir_call *call = new(ctx) ir_call(sig, actual_parameters);
112 if (!sig->return_type->is_void()) {
113 ir_variable *var;
114 ir_dereference_variable *deref;
115
116 var = new(ctx) ir_variable(sig->return_type,
117 talloc_asprintf(ctx, "%s_retval",
118 sig->function_name()),
119 ir_var_temporary);
120 instructions->push_tail(var);
121
122 deref = new(ctx) ir_dereference_variable(var);
123 ir_assignment *assign = new(ctx) ir_assignment(deref, call, NULL);
124 instructions->push_tail(assign);
125
126 deref = new(ctx) ir_dereference_variable(var);
127 return deref;
128 } else {
129 instructions->push_tail(call);
130 return NULL;
131 }
132 } else {
133 /* FINISHME: Log a better error message here. G++ will show the types
134 * FINISHME: of the actual parameters and the set of candidate
135 * FINISHME: functions. A different error should also be logged when
136 * FINISHME: multiple functions match.
137 */
138 _mesa_glsl_error(loc, state, "no matching function for call to `%s'",
139 f->name);
140 return ir_call::get_error_instruction(ctx);
141 }
142 }
143
144
145 static ir_rvalue *
146 match_function_by_name(exec_list *instructions, const char *name,
147 YYLTYPE *loc, exec_list *actual_parameters,
148 struct _mesa_glsl_parse_state *state)
149 {
150 void *ctx = state;
151 ir_function *f = state->symbols->get_function(name);
152
153 if (f == NULL) {
154 _mesa_glsl_error(loc, state, "function `%s' undeclared", name);
155 return ir_call::get_error_instruction(ctx);
156 }
157
158 /* Once we've determined that the function being called might exist, try
159 * to find an overload of the function that matches the parameters.
160 */
161 return process_call(instructions, f, loc, actual_parameters, state);
162 }
163
164
165 /**
166 * Perform automatic type conversion of constructor parameters
167 *
168 * This implements the rules in the "Conversion and Scalar Constructors"
169 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
170 */
171 static ir_rvalue *
172 convert_component(ir_rvalue *src, const glsl_type *desired_type)
173 {
174 void *ctx = talloc_parent(src);
175 const unsigned a = desired_type->base_type;
176 const unsigned b = src->type->base_type;
177 ir_expression *result = NULL;
178
179 if (src->type->is_error())
180 return src;
181
182 assert(a <= GLSL_TYPE_BOOL);
183 assert(b <= GLSL_TYPE_BOOL);
184
185 if ((a == b) || (src->type->is_integer() && desired_type->is_integer()))
186 return src;
187
188 switch (a) {
189 case GLSL_TYPE_UINT:
190 case GLSL_TYPE_INT:
191 if (b == GLSL_TYPE_FLOAT)
192 result = new(ctx) ir_expression(ir_unop_f2i, desired_type, src, NULL);
193 else {
194 assert(b == GLSL_TYPE_BOOL);
195 result = new(ctx) ir_expression(ir_unop_b2i, desired_type, src, NULL);
196 }
197 break;
198 case GLSL_TYPE_FLOAT:
199 switch (b) {
200 case GLSL_TYPE_UINT:
201 result = new(ctx) ir_expression(ir_unop_u2f, desired_type, src, NULL);
202 break;
203 case GLSL_TYPE_INT:
204 result = new(ctx) ir_expression(ir_unop_i2f, desired_type, src, NULL);
205 break;
206 case GLSL_TYPE_BOOL:
207 result = new(ctx) ir_expression(ir_unop_b2f, desired_type, src, NULL);
208 break;
209 }
210 break;
211 case GLSL_TYPE_BOOL:
212 switch (b) {
213 case GLSL_TYPE_UINT:
214 case GLSL_TYPE_INT:
215 result = new(ctx) ir_expression(ir_unop_i2b, desired_type, src, NULL);
216 break;
217 case GLSL_TYPE_FLOAT:
218 result = new(ctx) ir_expression(ir_unop_f2b, desired_type, src, NULL);
219 break;
220 }
221 break;
222 }
223
224 assert(result != NULL);
225
226 /* Try constant folding; it may fold in the conversion we just added. */
227 ir_constant *const constant = result->constant_expression_value();
228 return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result;
229 }
230
231 /**
232 * Dereference a specific component from a scalar, vector, or matrix
233 */
234 static ir_rvalue *
235 dereference_component(ir_rvalue *src, unsigned component)
236 {
237 void *ctx = talloc_parent(src);
238 assert(component < src->type->components());
239
240 /* If the source is a constant, just create a new constant instead of a
241 * dereference of the existing constant.
242 */
243 ir_constant *constant = src->as_constant();
244 if (constant)
245 return new(ctx) ir_constant(constant, component);
246
247 if (src->type->is_scalar()) {
248 return src;
249 } else if (src->type->is_vector()) {
250 return new(ctx) ir_swizzle(src, component, 0, 0, 0, 1);
251 } else {
252 assert(src->type->is_matrix());
253
254 /* Dereference a row of the matrix, then call this function again to get
255 * a specific element from that row.
256 */
257 const int c = component / src->type->column_type()->vector_elements;
258 const int r = component % src->type->column_type()->vector_elements;
259 ir_constant *const col_index = new(ctx) ir_constant(c);
260 ir_dereference *const col = new(ctx) ir_dereference_array(src, col_index);
261
262 col->type = src->type->column_type();
263
264 return dereference_component(col, r);
265 }
266
267 assert(!"Should not get here.");
268 return NULL;
269 }
270
271
272 static ir_rvalue *
273 process_array_constructor(exec_list *instructions,
274 const glsl_type *constructor_type,
275 YYLTYPE *loc, exec_list *parameters,
276 struct _mesa_glsl_parse_state *state)
277 {
278 void *ctx = state;
279 /* Array constructors come in two forms: sized and unsized. Sized array
280 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
281 * variables. In this case the number of parameters must exactly match the
282 * specified size of the array.
283 *
284 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
285 * are vec4 variables. In this case the size of the array being constructed
286 * is determined by the number of parameters.
287 *
288 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
289 *
290 * "There must be exactly the same number of arguments as the size of
291 * the array being constructed. If no size is present in the
292 * constructor, then the array is explicitly sized to the number of
293 * arguments provided. The arguments are assigned in order, starting at
294 * element 0, to the elements of the constructed array. Each argument
295 * must be the same type as the element type of the array, or be a type
296 * that can be converted to the element type of the array according to
297 * Section 4.1.10 "Implicit Conversions.""
298 */
299 exec_list actual_parameters;
300 const unsigned parameter_count =
301 process_parameters(instructions, &actual_parameters, parameters, state);
302
303 if ((parameter_count == 0)
304 || ((constructor_type->length != 0)
305 && (constructor_type->length != parameter_count))) {
306 const unsigned min_param = (constructor_type->length == 0)
307 ? 1 : constructor_type->length;
308
309 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
310 "parameter%s",
311 (constructor_type->length != 0) ? "at least" : "exactly",
312 min_param, (min_param <= 1) ? "" : "s");
313 return ir_call::get_error_instruction(ctx);
314 }
315
316 if (constructor_type->length == 0) {
317 constructor_type =
318 glsl_type::get_array_instance(constructor_type->element_type(),
319 parameter_count);
320 assert(constructor_type != NULL);
321 assert(constructor_type->length == parameter_count);
322 }
323
324 ir_function *f = state->symbols->get_function(constructor_type->name);
325
326 /* If the constructor for this type of array does not exist, generate the
327 * prototype and add it to the symbol table.
328 */
329 if (f == NULL) {
330 f = constructor_type->generate_constructor(state->symbols);
331 }
332
333 ir_rvalue *const r =
334 process_call(instructions, f, loc, &actual_parameters, state);
335
336 assert(r != NULL);
337 assert(r->type->is_error() || (r->type == constructor_type));
338
339 return r;
340 }
341
342
343 /**
344 * Try to convert a record constructor to a constant expression
345 */
346 static ir_constant *
347 constant_record_constructor(const glsl_type *constructor_type,
348 YYLTYPE *loc, exec_list *parameters,
349 struct _mesa_glsl_parse_state *state)
350 {
351 void *ctx = state;
352 bool all_parameters_are_constant = true;
353
354 exec_node *node = parameters->head;
355 for (unsigned i = 0; i < constructor_type->length; i++) {
356 ir_instruction *ir = (ir_instruction *) node;
357
358 if (node->is_tail_sentinal()) {
359 _mesa_glsl_error(loc, state,
360 "insufficient parameters to constructor for `%s'",
361 constructor_type->name);
362 return NULL;
363 }
364
365 if (ir->type != constructor_type->fields.structure[i].type) {
366 _mesa_glsl_error(loc, state,
367 "parameter type mismatch in constructor for `%s' "
368 " (%s vs %s)",
369 constructor_type->name,
370 ir->type->name,
371 constructor_type->fields.structure[i].type->name);
372 return NULL;
373 }
374
375 if (ir->as_constant() == NULL)
376 all_parameters_are_constant = false;
377
378 node = node->next;
379 }
380
381 if (!all_parameters_are_constant)
382 return NULL;
383
384 return new(ctx) ir_constant(constructor_type, parameters);
385 }
386
387
388 /**
389 * Generate data for a constant matrix constructor w/a single scalar parameter
390 *
391 * Matrix constructors in GLSL can be passed a single scalar of the
392 * approriate type. In these cases, the resulting matrix is the identity
393 * matrix multipled by the specified scalar. This function generates data for
394 * that matrix.
395 *
396 * \param type Type of the desired matrix.
397 * \param initializer Scalar value used to initialize the matrix diagonal.
398 * \param data Location to store the resulting matrix.
399 */
400 void
401 generate_constructor_matrix(const glsl_type *type, ir_constant *initializer,
402 ir_constant_data *data)
403 {
404 switch (type->base_type) {
405 case GLSL_TYPE_UINT:
406 case GLSL_TYPE_INT:
407 for (unsigned i = 0; i < type->components(); i++)
408 data->u[i] = 0;
409
410 for (unsigned i = 0; i < type->matrix_columns; i++) {
411 /* The array offset of the ith row and column of the matrix.
412 */
413 const unsigned idx = (i * type->vector_elements) + i;
414
415 data->u[idx] = initializer->value.u[0];
416 }
417 break;
418
419 case GLSL_TYPE_FLOAT:
420 for (unsigned i = 0; i < type->components(); i++)
421 data->f[i] = 0;
422
423 for (unsigned i = 0; i < type->matrix_columns; i++) {
424 /* The array offset of the ith row and column of the matrix.
425 */
426 const unsigned idx = (i * type->vector_elements) + i;
427
428 data->f[idx] = initializer->value.f[0];
429 }
430
431 break;
432
433 default:
434 assert(!"Should not get here.");
435 break;
436 }
437 }
438
439
440 /**
441 * Generate data for a constant vector constructor w/a single scalar parameter
442 *
443 * Vector constructors in GLSL can be passed a single scalar of the
444 * approriate type. In these cases, the resulting vector contains the specified
445 * value in all components. This function generates data for that vector.
446 *
447 * \param type Type of the desired vector.
448 * \param initializer Scalar value used to initialize the vector.
449 * \param data Location to store the resulting vector data.
450 */
451 void
452 generate_constructor_vector(const glsl_type *type, ir_constant *initializer,
453 ir_constant_data *data)
454 {
455 switch (type->base_type) {
456 case GLSL_TYPE_UINT:
457 case GLSL_TYPE_INT:
458 for (unsigned i = 0; i < type->components(); i++)
459 data->u[i] = initializer->value.u[0];
460
461 break;
462
463 case GLSL_TYPE_FLOAT:
464 for (unsigned i = 0; i < type->components(); i++)
465 data->f[i] = initializer->value.f[0];
466
467 break;
468
469 case GLSL_TYPE_BOOL:
470 for (unsigned i = 0; i < type->components(); i++)
471 data->b[i] = initializer->value.b[0];
472
473 break;
474
475 default:
476 assert(!"Should not get here.");
477 break;
478 }
479 }
480
481
482 /**
483 * Determine if a list consists of a single scalar r-value
484 */
485 bool
486 single_scalar_parameter(exec_list *parameters)
487 {
488 const ir_rvalue *const p = (ir_rvalue *) parameters->head;
489 assert(((ir_rvalue *)p)->as_rvalue() != NULL);
490
491 return (p->type->is_scalar() && p->next->is_tail_sentinal());
492 }
493
494
495 /**
496 * Generate inline code for a vector constructor
497 *
498 * The generated constructor code will consist of a temporary variable
499 * declaration of the same type as the constructor. A sequence of assignments
500 * from constructor parameters to the temporary will follow.
501 *
502 * \return
503 * An \c ir_dereference_variable of the temprorary generated in the constructor
504 * body.
505 */
506 ir_rvalue *
507 emit_inline_vector_constructor(const glsl_type *type,
508 exec_list *instructions,
509 exec_list *parameters,
510 void *ctx)
511 {
512 assert(!parameters->is_empty());
513
514 ir_variable *var = new(ctx) ir_variable(type,
515 talloc_strdup(ctx, "vec_ctor"),
516 ir_var_temporary);
517 instructions->push_tail(var);
518
519 /* There are two kinds of vector constructors.
520 *
521 * - Construct a vector from a single scalar by replicating that scalar to
522 * all components of the vector.
523 *
524 * - Construct a vector from an arbirary combination of vectors and
525 * scalars. The components of the constructor parameters are assigned
526 * to the vector in order until the vector is full.
527 */
528 const unsigned lhs_components = type->components();
529 if (single_scalar_parameter(parameters)) {
530 ir_rvalue *first_param = (ir_rvalue *)parameters->head;
531 ir_rvalue *rhs = new(ctx) ir_swizzle(first_param, 0, 0, 0, 0,
532 lhs_components);
533 ir_dereference_variable *lhs = new(ctx) ir_dereference_variable(var);
534
535 assert(rhs->type == lhs->type);
536
537 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
538 instructions->push_tail(inst);
539 } else {
540 unsigned base_component = 0;
541 foreach_list(node, parameters) {
542 ir_rvalue *rhs = (ir_rvalue *) node;
543 unsigned rhs_components = rhs->type->components();
544
545 /* Do not try to assign more components to the vector than it has!
546 */
547 if ((rhs_components + base_component) > lhs_components) {
548 rhs_components = lhs_components - base_component;
549 }
550
551 /* Emit an assignment of the constructor parameter to the next set of
552 * components in the temporary variable.
553 */
554 unsigned mask[4] = { 0, 0, 0, 0 };
555 for (unsigned i = 0; i < rhs_components; i++) {
556 mask[i] = i + base_component;
557 }
558
559
560 ir_rvalue *lhs_ref = new(ctx) ir_dereference_variable(var);
561 ir_swizzle *lhs = new(ctx) ir_swizzle(lhs_ref, mask, rhs_components);
562
563 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
564 instructions->push_tail(inst);
565
566 /* Advance the component index by the number of components that were
567 * just assigned.
568 */
569 base_component += rhs_components;
570 }
571 }
572 return new(ctx) ir_dereference_variable(var);
573 }
574
575
576 /**
577 * Generate assignment of a portion of a vector to a portion of a matrix column
578 *
579 * \param src_base First component of the source to be used in assignment
580 * \param column Column of destination to be assiged
581 * \param row_base First component of the destination column to be assigned
582 * \param count Number of components to be assigned
583 *
584 * \note
585 * \c src_base + \c count must be less than or equal to the number of components
586 * in the source vector.
587 */
588 ir_instruction *
589 assign_to_matrix_column(ir_variable *var, unsigned column, unsigned row_base,
590 ir_rvalue *src, unsigned src_base, unsigned count,
591 TALLOC_CTX *ctx)
592 {
593 const unsigned mask[8] = { 0, 1, 2, 3, 0, 0, 0, 0 };
594
595 ir_constant *col_idx = new(ctx) ir_constant(column);
596 ir_rvalue *column_ref = new(ctx) ir_dereference_array(var, col_idx);
597
598 assert(column_ref->type->components() >= (row_base + count));
599 ir_rvalue *lhs = new(ctx) ir_swizzle(column_ref, &mask[row_base], count);
600
601 assert(src->type->components() >= (src_base + count));
602 ir_rvalue *rhs = new(ctx) ir_swizzle(src, &mask[src_base], count);
603
604 return new(ctx) ir_assignment(lhs, rhs, NULL);
605 }
606
607
608 /**
609 * Generate inline code for a matrix constructor
610 *
611 * The generated constructor code will consist of a temporary variable
612 * declaration of the same type as the constructor. A sequence of assignments
613 * from constructor parameters to the temporary will follow.
614 *
615 * \return
616 * An \c ir_dereference_variable of the temprorary generated in the constructor
617 * body.
618 */
619 ir_rvalue *
620 emit_inline_matrix_constructor(const glsl_type *type,
621 exec_list *instructions,
622 exec_list *parameters,
623 void *ctx)
624 {
625 assert(!parameters->is_empty());
626
627 ir_variable *var = new(ctx) ir_variable(type,
628 talloc_strdup(ctx, "mat_ctor"),
629 ir_var_temporary);
630 instructions->push_tail(var);
631
632 /* There are three kinds of matrix constructors.
633 *
634 * - Construct a matrix from a single scalar by replicating that scalar to
635 * along the diagonal of the matrix and setting all other components to
636 * zero.
637 *
638 * - Construct a matrix from an arbirary combination of vectors and
639 * scalars. The components of the constructor parameters are assigned
640 * to the matrix in colum-major order until the matrix is full.
641 *
642 * - Construct a matrix from a single matrix. The source matrix is copied
643 * to the upper left portion of the constructed matrix, and the remaining
644 * elements take values from the identity matrix.
645 */
646 ir_rvalue *const first_param = (ir_rvalue *) parameters->head;
647 if (single_scalar_parameter(parameters)) {
648 /* Assign the scalar to the X component of a vec4, and fill the remaining
649 * components with zero.
650 */
651 ir_variable *rhs_var =
652 new(ctx) ir_variable(glsl_type::vec4_type,
653 talloc_strdup(ctx, "mat_ctor_vec"),
654 ir_var_temporary);
655 instructions->push_tail(rhs_var);
656
657 ir_constant_data zero;
658 zero.f[0] = 0.0;
659 zero.f[1] = 0.0;
660 zero.f[2] = 0.0;
661 zero.f[3] = 0.0;
662
663 ir_instruction *inst =
664 new(ctx) ir_assignment(new(ctx) ir_dereference_variable(rhs_var),
665 new(ctx) ir_constant(rhs_var->type, &zero),
666 NULL);
667 instructions->push_tail(inst);
668
669 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
670 ir_rvalue *const x_of_rhs = new(ctx) ir_swizzle(rhs_ref, 0, 0, 0, 0, 1);
671
672 inst = new(ctx) ir_assignment(x_of_rhs, first_param, NULL);
673 instructions->push_tail(inst);
674
675 /* Assign the temporary vector to each column of the destination matrix
676 * with a swizzle that puts the X component on the diagonal of the
677 * matrix. In some cases this may mean that the X component does not
678 * get assigned into the column at all (i.e., when the matrix has more
679 * columns than rows).
680 */
681 static const unsigned rhs_swiz[4][4] = {
682 { 0, 1, 1, 1 },
683 { 1, 0, 1, 1 },
684 { 1, 1, 0, 1 },
685 { 1, 1, 1, 0 }
686 };
687
688 const unsigned cols_to_init = min(type->matrix_columns,
689 type->vector_elements);
690 for (unsigned i = 0; i < cols_to_init; i++) {
691 ir_constant *const col_idx = new(ctx) ir_constant(i);
692 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
693
694 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
695 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, rhs_swiz[i],
696 type->vector_elements);
697
698 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
699 instructions->push_tail(inst);
700 }
701
702 for (unsigned i = cols_to_init; i < type->matrix_columns; i++) {
703 ir_constant *const col_idx = new(ctx) ir_constant(i);
704 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
705
706 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
707 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, 1, 1, 1, 1,
708 type->vector_elements);
709
710 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
711 instructions->push_tail(inst);
712 }
713 } else if (first_param->type->is_matrix()) {
714 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
715 *
716 * "If a matrix is constructed from a matrix, then each component
717 * (column i, row j) in the result that has a corresponding
718 * component (column i, row j) in the argument will be initialized
719 * from there. All other components will be initialized to the
720 * identity matrix. If a matrix argument is given to a matrix
721 * constructor, it is an error to have any other arguments."
722 */
723 assert(first_param->next->is_tail_sentinal());
724 ir_rvalue *const src_matrix = first_param;
725
726 /* If the source matrix is smaller, pre-initialize the relavent parts of
727 * the destination matrix to the identity matrix.
728 */
729 if ((src_matrix->type->matrix_columns < var->type->matrix_columns)
730 || (src_matrix->type->vector_elements < var->type->vector_elements)) {
731
732 /* If the source matrix has fewer rows, every column of the destination
733 * must be initialized. Otherwise only the columns in the destination
734 * that do not exist in the source must be initialized.
735 */
736 unsigned col =
737 (src_matrix->type->vector_elements < var->type->vector_elements)
738 ? 0 : src_matrix->type->matrix_columns;
739
740 const glsl_type *const col_type = var->type->column_type();
741 for (/* empty */; col < var->type->matrix_columns; col++) {
742 ir_constant_data ident;
743
744 ident.f[0] = 0.0;
745 ident.f[1] = 0.0;
746 ident.f[2] = 0.0;
747 ident.f[3] = 0.0;
748
749 ident.f[col] = 1.0;
750
751 ir_rvalue *const rhs = new(ctx) ir_constant(col_type, &ident);
752
753 ir_rvalue *const lhs =
754 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(col));
755
756 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
757 instructions->push_tail(inst);
758 }
759 }
760
761 /* Assign columns from the source matrix to the destination matrix.
762 *
763 * Since the parameter will be used in the RHS of multiple assignments,
764 * generate a temporary and copy the paramter there.
765 */
766 ir_variable *const rhs_var =
767 new(ctx) ir_variable(first_param->type,
768 talloc_strdup(ctx, "mat_ctor_mat"),
769 ir_var_temporary);
770 instructions->push_tail(rhs_var);
771
772 ir_dereference *const rhs_var_ref =
773 new(ctx) ir_dereference_variable(rhs_var);
774 ir_instruction *const inst =
775 new(ctx) ir_assignment(rhs_var_ref, first_param, NULL);
776 instructions->push_tail(inst);
777
778
779 const unsigned swiz[4] = { 0, 1, 2, 3 };
780 const unsigned last_col = min(src_matrix->type->matrix_columns,
781 var->type->matrix_columns);
782 for (unsigned i = 0; i < last_col; i++) {
783 ir_rvalue *const lhs_col =
784 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(i));
785 ir_rvalue *const rhs_col =
786 new(ctx) ir_dereference_array(rhs_var, new(ctx) ir_constant(i));
787
788 /* If one matrix has columns that are smaller than the columns of the
789 * other matrix, wrap the column access of the larger with a swizzle
790 * so that the LHS and RHS of the assignment have the same size (and
791 * therefore have the same type).
792 *
793 * It would be perfectly valid to unconditionally generate the
794 * swizzles, this this will typically result in a more compact IR tree.
795 */
796 ir_rvalue *lhs;
797 ir_rvalue *rhs;
798 if (lhs_col->type->vector_elements < rhs_col->type->vector_elements) {
799 lhs = lhs_col;
800
801 rhs = new(ctx) ir_swizzle(rhs_col, swiz,
802 lhs_col->type->vector_elements);
803 } else if (lhs_col->type->vector_elements
804 > rhs_col->type->vector_elements) {
805 lhs = new(ctx) ir_swizzle(lhs_col, swiz,
806 rhs_col->type->vector_elements);
807 rhs = rhs_col;
808 } else {
809 lhs = lhs_col;
810 rhs = rhs_col;
811 }
812
813 assert(lhs->type == rhs->type);
814
815 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
816 instructions->push_tail(inst);
817 }
818 } else {
819 const unsigned rows = type->matrix_columns;
820 const unsigned cols = type->vector_elements;
821 unsigned col_idx = 0;
822 unsigned row_idx = 0;
823
824 foreach_list (node, parameters) {
825 ir_rvalue *const rhs = (ir_rvalue *) node;
826 const unsigned components_remaining_this_column = rows - row_idx;
827 unsigned rhs_components = rhs->type->components();
828 unsigned rhs_base = 0;
829
830 /* Since the parameter might be used in the RHS of two assignments,
831 * generate a temporary and copy the paramter there.
832 */
833 ir_variable *rhs_var =
834 new(ctx) ir_variable(rhs->type,
835 talloc_strdup(ctx, "mat_ctor_vec"),
836 ir_var_temporary);
837 instructions->push_tail(rhs_var);
838
839 ir_dereference *rhs_var_ref =
840 new(ctx) ir_dereference_variable(rhs_var);
841 ir_instruction *inst = new(ctx) ir_assignment(rhs_var_ref, rhs, NULL);
842 instructions->push_tail(inst);
843
844 /* Assign the current parameter to as many components of the matrix
845 * as it will fill.
846 *
847 * NOTE: A single vector parameter can span two matrix columns. A
848 * single vec4, for example, can completely fill a mat2.
849 */
850 if (rhs_components >= components_remaining_this_column) {
851 const unsigned count = min(rhs_components,
852 components_remaining_this_column);
853
854 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
855
856 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
857 row_idx,
858 rhs_var_ref, 0,
859 count, ctx);
860 instructions->push_tail(inst);
861
862 rhs_base = count;
863
864 col_idx++;
865 row_idx = 0;
866 }
867
868 /* If there is data left in the parameter and components left to be
869 * set in the destination, emit another assignment. It is possible
870 * that the assignment could be of a vec4 to the last element of the
871 * matrix. In this case col_idx==cols, but there is still data
872 * left in the source parameter. Obviously, don't emit an assignment
873 * to data outside the destination matrix.
874 */
875 if ((col_idx < cols) && (rhs_base < rhs_components)) {
876 const unsigned count = rhs_components - rhs_base;
877
878 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
879
880 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
881 row_idx,
882 rhs_var_ref,
883 rhs_base,
884 count, ctx);
885 instructions->push_tail(inst);
886
887 row_idx += count;
888 }
889 }
890 }
891
892 return new(ctx) ir_dereference_variable(var);
893 }
894
895
896 ir_rvalue *
897 ast_function_expression::hir(exec_list *instructions,
898 struct _mesa_glsl_parse_state *state)
899 {
900 void *ctx = state;
901 /* There are three sorts of function calls.
902 *
903 * 1. constructors - The first subexpression is an ast_type_specifier.
904 * 2. methods - Only the .length() method of array types.
905 * 3. functions - Calls to regular old functions.
906 *
907 * Method calls are actually detected when the ast_field_selection
908 * expression is handled.
909 */
910 if (is_constructor()) {
911 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
912 YYLTYPE loc = type->get_location();
913 const char *name;
914
915 const glsl_type *const constructor_type = type->glsl_type(& name, state);
916
917
918 /* Constructors for samplers are illegal.
919 */
920 if (constructor_type->is_sampler()) {
921 _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
922 constructor_type->name);
923 return ir_call::get_error_instruction(ctx);
924 }
925
926 if (constructor_type->is_array()) {
927 if (state->language_version <= 110) {
928 _mesa_glsl_error(& loc, state,
929 "array constructors forbidden in GLSL 1.10");
930 return ir_call::get_error_instruction(ctx);
931 }
932
933 return process_array_constructor(instructions, constructor_type,
934 & loc, &this->expressions, state);
935 }
936
937 /* There are two kinds of constructor call. Constructors for built-in
938 * language types, such as mat4 and vec2, are free form. The only
939 * requirement is that the parameters must provide enough values of the
940 * correct scalar type. Constructors for arrays and structures must
941 * have the exact number of parameters with matching types in the
942 * correct order. These constructors follow essentially the same type
943 * matching rules as functions.
944 */
945 if (!constructor_type->is_numeric() && !constructor_type->is_boolean())
946 return ir_call::get_error_instruction(ctx);
947
948 /* Total number of components of the type being constructed. */
949 const unsigned type_components = constructor_type->components();
950
951 /* Number of components from parameters that have actually been
952 * consumed. This is used to perform several kinds of error checking.
953 */
954 unsigned components_used = 0;
955
956 unsigned matrix_parameters = 0;
957 unsigned nonmatrix_parameters = 0;
958 exec_list actual_parameters;
959
960 foreach_list (n, &this->expressions) {
961 ast_node *ast = exec_node_data(ast_node, n, link);
962 ir_rvalue *result = ast->hir(instructions, state)->as_rvalue();
963
964 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
965 *
966 * "It is an error to provide extra arguments beyond this
967 * last used argument."
968 */
969 if (components_used >= type_components) {
970 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
971 "constructor",
972 constructor_type->name);
973 return ir_call::get_error_instruction(ctx);
974 }
975
976 if (!result->type->is_numeric() && !result->type->is_boolean()) {
977 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
978 "non-numeric data type",
979 constructor_type->name);
980 return ir_call::get_error_instruction(ctx);
981 }
982
983 /* Count the number of matrix and nonmatrix parameters. This
984 * is used below to enforce some of the constructor rules.
985 */
986 if (result->type->is_matrix())
987 matrix_parameters++;
988 else
989 nonmatrix_parameters++;
990
991 actual_parameters.push_tail(result);
992 components_used += result->type->components();
993 }
994
995 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
996 *
997 * "It is an error to construct matrices from other matrices. This
998 * is reserved for future use."
999 */
1000 if ((state->language_version <= 110) && (matrix_parameters > 0)
1001 && constructor_type->is_matrix()) {
1002 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
1003 "matrix in GLSL 1.10",
1004 constructor_type->name);
1005 return ir_call::get_error_instruction(ctx);
1006 }
1007
1008 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1009 *
1010 * "If a matrix argument is given to a matrix constructor, it is
1011 * an error to have any other arguments."
1012 */
1013 if ((matrix_parameters > 0)
1014 && ((matrix_parameters + nonmatrix_parameters) > 1)
1015 && constructor_type->is_matrix()) {
1016 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
1017 "matrix must be only parameter",
1018 constructor_type->name);
1019 return ir_call::get_error_instruction(ctx);
1020 }
1021
1022 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1023 *
1024 * "In these cases, there must be enough components provided in the
1025 * arguments to provide an initializer for every component in the
1026 * constructed value."
1027 */
1028 if ((components_used < type_components) && (components_used != 1)) {
1029 _mesa_glsl_error(& loc, state, "too few components to construct "
1030 "`%s'",
1031 constructor_type->name);
1032 return ir_call::get_error_instruction(ctx);
1033 }
1034
1035 /* Later, we cast each parameter to the same base type as the
1036 * constructor. Since there are no non-floating point matrices, we
1037 * need to break them up into a series of column vectors.
1038 */
1039 if (constructor_type->base_type != GLSL_TYPE_FLOAT) {
1040 foreach_list_safe(n, &actual_parameters) {
1041 ir_rvalue *matrix = (ir_rvalue *) n;
1042
1043 if (!matrix->type->is_matrix())
1044 continue;
1045
1046 /* Create a temporary containing the matrix. */
1047 ir_variable *var = new(ctx) ir_variable(matrix->type, "matrix_tmp",
1048 ir_var_temporary);
1049 instructions->push_tail(var);
1050 instructions->push_tail(new(ctx) ir_assignment(new(ctx)
1051 ir_dereference_variable(var), matrix, NULL));
1052 var->constant_value = matrix->constant_expression_value();
1053
1054 /* Replace the matrix with dereferences of its columns. */
1055 for (int i = 0; i < matrix->type->matrix_columns; i++) {
1056 matrix->insert_before(new (ctx) ir_dereference_array(var,
1057 new(ctx) ir_constant(i)));
1058 }
1059 matrix->remove();
1060 }
1061 }
1062
1063 bool all_parameters_are_constant = true;
1064
1065 /* Type cast each parameter and, if possible, fold constants.*/
1066 foreach_list_safe(n, &actual_parameters) {
1067 ir_rvalue *ir = (ir_rvalue *) n;
1068
1069 const glsl_type *desired_type =
1070 glsl_type::get_instance(constructor_type->base_type,
1071 ir->type->vector_elements,
1072 ir->type->matrix_columns);
1073 ir_rvalue *result = convert_component(ir, desired_type);
1074
1075 /* Attempt to convert the parameter to a constant valued expression.
1076 * After doing so, track whether or not all the parameters to the
1077 * constructor are trivially constant valued expressions.
1078 */
1079 ir_rvalue *const constant = result->constant_expression_value();
1080
1081 if (constant != NULL)
1082 result = constant;
1083 else
1084 all_parameters_are_constant = false;
1085
1086 if (result != ir) {
1087 ir->replace_with(result);
1088 }
1089 }
1090
1091 /* If all of the parameters are trivially constant, create a
1092 * constant representing the complete collection of parameters.
1093 */
1094 if (all_parameters_are_constant) {
1095 if (components_used >= type_components)
1096 return new(ctx) ir_constant(constructor_type,
1097 & actual_parameters);
1098
1099 /* The above case must handle all scalar constructors.
1100 */
1101 assert(constructor_type->is_vector()
1102 || constructor_type->is_matrix());
1103
1104 /* Constructors with exactly one component are special for
1105 * vectors and matrices. For vectors it causes all elements of
1106 * the vector to be filled with the value. For matrices it
1107 * causes the matrix to be filled with 0 and the diagonal to be
1108 * filled with the value.
1109 */
1110 ir_constant_data data;
1111 ir_constant *const initializer =
1112 (ir_constant *) actual_parameters.head;
1113 if (constructor_type->is_matrix())
1114 generate_constructor_matrix(constructor_type, initializer,
1115 &data);
1116 else
1117 generate_constructor_vector(constructor_type, initializer,
1118 &data);
1119
1120 return new(ctx) ir_constant(constructor_type, &data);
1121 } else if (constructor_type->is_scalar()) {
1122 return dereference_component((ir_rvalue *) actual_parameters.head,
1123 0);
1124 } else if (constructor_type->is_vector()) {
1125 return emit_inline_vector_constructor(constructor_type,
1126 instructions,
1127 &actual_parameters,
1128 ctx);
1129 } else {
1130 assert(constructor_type->is_matrix());
1131 return emit_inline_matrix_constructor(constructor_type,
1132 instructions,
1133 &actual_parameters,
1134 ctx);
1135 }
1136 } else {
1137 const ast_expression *id = subexpressions[0];
1138 YYLTYPE loc = id->get_location();
1139 exec_list actual_parameters;
1140
1141 process_parameters(instructions, &actual_parameters, &this->expressions,
1142 state);
1143
1144 const glsl_type *const type =
1145 state->symbols->get_type(id->primary_expression.identifier);
1146
1147 if ((type != NULL) && type->is_record()) {
1148 ir_constant *constant =
1149 constant_record_constructor(type, &loc, &actual_parameters, state);
1150
1151 if (constant != NULL)
1152 return constant;
1153 }
1154
1155 return match_function_by_name(instructions,
1156 id->primary_expression.identifier, & loc,
1157 &actual_parameters, state);
1158 }
1159
1160 return ir_call::get_error_instruction(ctx);
1161 }