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