i965: Request that returns be lowered in shader main
[mesa/nouveau-pmpeg.git] / src / glsl / ast_function.cpp
blob5d9d35b2d9b0058a4406537927aad98b2bc454c9
1 /*
2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
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.
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 */
30 static ir_rvalue *
31 convert_component(ir_rvalue *src, const glsl_type *desired_type);
33 bool
34 apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
35 struct _mesa_glsl_parse_state *state);
37 static unsigned
38 process_parameters(exec_list *instructions, exec_list *actual_parameters,
39 exec_list *parameters,
40 struct _mesa_glsl_parse_state *state)
42 unsigned count = 0;
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);
48 ir_constant *const constant = result->constant_expression_value();
49 if (constant != NULL)
50 result = constant;
52 actual_parameters->push_tail(result);
53 count++;
56 return count;
60 /**
61 * Generate a source prototype for a function signature
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.
68 * \return
69 * A talloced string representing the prototype of the function.
71 char *
72 prototype_string(const glsl_type *return_type, const char *name,
73 exec_list *parameters)
75 char *str = NULL;
77 if (return_type != NULL)
78 str = talloc_asprintf(str, "%s ", return_type->name);
80 str = talloc_asprintf_append(str, "%s(", name);
82 const char *comma = "";
83 foreach_list(node, parameters) {
84 const ir_instruction *const param = (ir_instruction *) node;
86 str = talloc_asprintf_append(str, "%s%s", comma, param->type->name);
87 comma = ", ";
90 str = talloc_strdup_append(str, ")");
91 return str;
95 static ir_rvalue *
96 process_call(exec_list *instructions, ir_function *f,
97 YYLTYPE *loc, exec_list *actual_parameters,
98 struct _mesa_glsl_parse_state *state)
100 void *ctx = state;
102 ir_function_signature *sig = f->matching_signature(actual_parameters);
104 /* The instructions param will be used when the FINISHMEs below are done */
105 (void) instructions;
107 if (sig != NULL) {
108 /* Verify that 'out' and 'inout' actual parameters are lvalues. This
109 * isn't done in ir_function::matching_signature because that function
110 * cannot generate the necessary diagnostics.
112 exec_list_iterator actual_iter = actual_parameters->iterator();
113 exec_list_iterator formal_iter = sig->parameters.iterator();
115 while (actual_iter.has_next()) {
116 ir_rvalue *actual = (ir_rvalue *) actual_iter.get();
117 ir_variable *formal = (ir_variable *) formal_iter.get();
119 assert(actual != NULL);
120 assert(formal != NULL);
122 if ((formal->mode == ir_var_out)
123 || (formal->mode == ir_var_inout)) {
124 if (! actual->is_lvalue()) {
125 /* FINISHME: Log a better diagnostic here. There is no way
126 * FINISHME: to tell the user which parameter is invalid.
128 _mesa_glsl_error(loc, state, "`%s' parameter is not lvalue",
129 (formal->mode == ir_var_out) ? "out" : "inout");
133 if (formal->type->is_numeric() || formal->type->is_boolean()) {
134 ir_rvalue *converted = convert_component(actual, formal->type);
135 actual->replace_with(converted);
138 actual_iter.next();
139 formal_iter.next();
142 /* Always insert the call in the instruction stream, and return a deref
143 * of its return val if it returns a value, since we don't know if
144 * the rvalue is going to be assigned to anything or not.
146 ir_call *call = new(ctx) ir_call(sig, actual_parameters);
147 if (!sig->return_type->is_void()) {
148 ir_variable *var;
149 ir_dereference_variable *deref;
151 var = new(ctx) ir_variable(sig->return_type,
152 talloc_asprintf(ctx, "%s_retval",
153 sig->function_name()),
154 ir_var_temporary);
155 instructions->push_tail(var);
157 deref = new(ctx) ir_dereference_variable(var);
158 ir_assignment *assign = new(ctx) ir_assignment(deref, call, NULL);
159 instructions->push_tail(assign);
160 if (state->language_version >= 120)
161 var->constant_value = call->constant_expression_value();
163 deref = new(ctx) ir_dereference_variable(var);
164 return deref;
165 } else {
166 instructions->push_tail(call);
167 return NULL;
169 } else {
170 char *str = prototype_string(NULL, f->name, actual_parameters);
172 _mesa_glsl_error(loc, state, "no matching function for call to `%s'",
173 str);
174 talloc_free(str);
176 const char *prefix = "candidates are: ";
177 foreach_list (node, &f->signatures) {
178 ir_function_signature *sig = (ir_function_signature *) node;
180 str = prototype_string(sig->return_type, f->name, &sig->parameters);
181 _mesa_glsl_error(loc, state, "%s%s\n", prefix, str);
182 talloc_free(str);
184 prefix = " ";
187 return ir_call::get_error_instruction(ctx);
192 static ir_rvalue *
193 match_function_by_name(exec_list *instructions, const char *name,
194 YYLTYPE *loc, exec_list *actual_parameters,
195 struct _mesa_glsl_parse_state *state)
197 void *ctx = state;
198 ir_function *f = state->symbols->get_function(name);
200 if (f == NULL) {
201 _mesa_glsl_error(loc, state, "function `%s' undeclared", name);
202 return ir_call::get_error_instruction(ctx);
205 /* Once we've determined that the function being called might exist, try
206 * to find an overload of the function that matches the parameters.
208 return process_call(instructions, f, loc, actual_parameters, state);
213 * Perform automatic type conversion of constructor parameters
215 * This implements the rules in the "Conversion and Scalar Constructors"
216 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
218 static ir_rvalue *
219 convert_component(ir_rvalue *src, const glsl_type *desired_type)
221 void *ctx = talloc_parent(src);
222 const unsigned a = desired_type->base_type;
223 const unsigned b = src->type->base_type;
224 ir_expression *result = NULL;
226 if (src->type->is_error())
227 return src;
229 assert(a <= GLSL_TYPE_BOOL);
230 assert(b <= GLSL_TYPE_BOOL);
232 if ((a == b) || (src->type->is_integer() && desired_type->is_integer()))
233 return src;
235 switch (a) {
236 case GLSL_TYPE_UINT:
237 case GLSL_TYPE_INT:
238 if (b == GLSL_TYPE_FLOAT)
239 result = new(ctx) ir_expression(ir_unop_f2i, desired_type, src, NULL);
240 else {
241 assert(b == GLSL_TYPE_BOOL);
242 result = new(ctx) ir_expression(ir_unop_b2i, desired_type, src, NULL);
244 break;
245 case GLSL_TYPE_FLOAT:
246 switch (b) {
247 case GLSL_TYPE_UINT:
248 result = new(ctx) ir_expression(ir_unop_u2f, desired_type, src, NULL);
249 break;
250 case GLSL_TYPE_INT:
251 result = new(ctx) ir_expression(ir_unop_i2f, desired_type, src, NULL);
252 break;
253 case GLSL_TYPE_BOOL:
254 result = new(ctx) ir_expression(ir_unop_b2f, desired_type, src, NULL);
255 break;
257 break;
258 case GLSL_TYPE_BOOL:
259 switch (b) {
260 case GLSL_TYPE_UINT:
261 case GLSL_TYPE_INT:
262 result = new(ctx) ir_expression(ir_unop_i2b, desired_type, src, NULL);
263 break;
264 case GLSL_TYPE_FLOAT:
265 result = new(ctx) ir_expression(ir_unop_f2b, desired_type, src, NULL);
266 break;
268 break;
271 assert(result != NULL);
273 /* Try constant folding; it may fold in the conversion we just added. */
274 ir_constant *const constant = result->constant_expression_value();
275 return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result;
279 * Dereference a specific component from a scalar, vector, or matrix
281 static ir_rvalue *
282 dereference_component(ir_rvalue *src, unsigned component)
284 void *ctx = talloc_parent(src);
285 assert(component < src->type->components());
287 /* If the source is a constant, just create a new constant instead of a
288 * dereference of the existing constant.
290 ir_constant *constant = src->as_constant();
291 if (constant)
292 return new(ctx) ir_constant(constant, component);
294 if (src->type->is_scalar()) {
295 return src;
296 } else if (src->type->is_vector()) {
297 return new(ctx) ir_swizzle(src, component, 0, 0, 0, 1);
298 } else {
299 assert(src->type->is_matrix());
301 /* Dereference a row of the matrix, then call this function again to get
302 * a specific element from that row.
304 const int c = component / src->type->column_type()->vector_elements;
305 const int r = component % src->type->column_type()->vector_elements;
306 ir_constant *const col_index = new(ctx) ir_constant(c);
307 ir_dereference *const col = new(ctx) ir_dereference_array(src, col_index);
309 col->type = src->type->column_type();
311 return dereference_component(col, r);
314 assert(!"Should not get here.");
315 return NULL;
319 static ir_rvalue *
320 process_array_constructor(exec_list *instructions,
321 const glsl_type *constructor_type,
322 YYLTYPE *loc, exec_list *parameters,
323 struct _mesa_glsl_parse_state *state)
325 void *ctx = state;
326 /* Array constructors come in two forms: sized and unsized. Sized array
327 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
328 * variables. In this case the number of parameters must exactly match the
329 * specified size of the array.
331 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
332 * are vec4 variables. In this case the size of the array being constructed
333 * is determined by the number of parameters.
335 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
337 * "There must be exactly the same number of arguments as the size of
338 * the array being constructed. If no size is present in the
339 * constructor, then the array is explicitly sized to the number of
340 * arguments provided. The arguments are assigned in order, starting at
341 * element 0, to the elements of the constructed array. Each argument
342 * must be the same type as the element type of the array, or be a type
343 * that can be converted to the element type of the array according to
344 * Section 4.1.10 "Implicit Conversions.""
346 exec_list actual_parameters;
347 const unsigned parameter_count =
348 process_parameters(instructions, &actual_parameters, parameters, state);
350 if ((parameter_count == 0)
351 || ((constructor_type->length != 0)
352 && (constructor_type->length != parameter_count))) {
353 const unsigned min_param = (constructor_type->length == 0)
354 ? 1 : constructor_type->length;
356 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
357 "parameter%s",
358 (constructor_type->length != 0) ? "at least" : "exactly",
359 min_param, (min_param <= 1) ? "" : "s");
360 return ir_call::get_error_instruction(ctx);
363 if (constructor_type->length == 0) {
364 constructor_type =
365 glsl_type::get_array_instance(constructor_type->element_type(),
366 parameter_count);
367 assert(constructor_type != NULL);
368 assert(constructor_type->length == parameter_count);
371 bool all_parameters_are_constant = true;
373 /* Type cast each parameter and, if possible, fold constants. */
374 foreach_list_safe(n, &actual_parameters) {
375 ir_rvalue *ir = (ir_rvalue *) n;
376 ir_rvalue *result = ir;
378 /* Apply implicit conversions (not the scalar constructor rules!) */
379 if (constructor_type->element_type()->is_float()) {
380 const glsl_type *desired_type =
381 glsl_type::get_instance(GLSL_TYPE_FLOAT,
382 ir->type->vector_elements,
383 ir->type->matrix_columns);
384 result = convert_component(ir, desired_type);
387 if (result->type != constructor_type->element_type()) {
388 _mesa_glsl_error(loc, state, "type error in array constructor: "
389 "expected: %s, found %s",
390 constructor_type->element_type()->name,
391 result->type->name);
394 /* Attempt to convert the parameter to a constant valued expression.
395 * After doing so, track whether or not all the parameters to the
396 * constructor are trivially constant valued expressions.
398 ir_rvalue *const constant = result->constant_expression_value();
400 if (constant != NULL)
401 result = constant;
402 else
403 all_parameters_are_constant = false;
405 ir->replace_with(result);
408 if (all_parameters_are_constant)
409 return new(ctx) ir_constant(constructor_type, &actual_parameters);
411 ir_variable *var = new(ctx) ir_variable(constructor_type, "array_ctor",
412 ir_var_temporary);
413 instructions->push_tail(var);
415 int i = 0;
416 foreach_list(node, &actual_parameters) {
417 ir_rvalue *rhs = (ir_rvalue *) node;
418 ir_rvalue *lhs = new(ctx) ir_dereference_array(var,
419 new(ctx) ir_constant(i));
421 ir_instruction *assignment = new(ctx) ir_assignment(lhs, rhs, NULL);
422 instructions->push_tail(assignment);
424 i++;
427 return new(ctx) ir_dereference_variable(var);
432 * Try to convert a record constructor to a constant expression
434 static ir_constant *
435 constant_record_constructor(const glsl_type *constructor_type,
436 exec_list *parameters, void *mem_ctx)
438 foreach_list(node, parameters) {
439 ir_constant *constant = ((ir_instruction *) node)->as_constant();
440 if (constant == NULL)
441 return NULL;
442 node->replace_with(constant);
445 return new(mem_ctx) ir_constant(constructor_type, parameters);
450 * Determine if a list consists of a single scalar r-value
452 bool
453 single_scalar_parameter(exec_list *parameters)
455 const ir_rvalue *const p = (ir_rvalue *) parameters->head;
456 assert(((ir_rvalue *)p)->as_rvalue() != NULL);
458 return (p->type->is_scalar() && p->next->is_tail_sentinel());
463 * Generate inline code for a vector constructor
465 * The generated constructor code will consist of a temporary variable
466 * declaration of the same type as the constructor. A sequence of assignments
467 * from constructor parameters to the temporary will follow.
469 * \return
470 * An \c ir_dereference_variable of the temprorary generated in the constructor
471 * body.
473 ir_rvalue *
474 emit_inline_vector_constructor(const glsl_type *type,
475 exec_list *instructions,
476 exec_list *parameters,
477 void *ctx)
479 assert(!parameters->is_empty());
481 ir_variable *var = new(ctx) ir_variable(type, "vec_ctor", ir_var_temporary);
482 instructions->push_tail(var);
484 /* There are two kinds of vector constructors.
486 * - Construct a vector from a single scalar by replicating that scalar to
487 * all components of the vector.
489 * - Construct a vector from an arbirary combination of vectors and
490 * scalars. The components of the constructor parameters are assigned
491 * to the vector in order until the vector is full.
493 const unsigned lhs_components = type->components();
494 if (single_scalar_parameter(parameters)) {
495 ir_rvalue *first_param = (ir_rvalue *)parameters->head;
496 ir_rvalue *rhs = new(ctx) ir_swizzle(first_param, 0, 0, 0, 0,
497 lhs_components);
498 ir_dereference_variable *lhs = new(ctx) ir_dereference_variable(var);
499 const unsigned mask = (1U << lhs_components) - 1;
501 assert(rhs->type == lhs->type);
503 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL, mask);
504 instructions->push_tail(inst);
505 } else {
506 unsigned base_component = 0;
507 ir_constant_data data;
508 unsigned constant_mask = 0;
510 memset(&data, 0, sizeof(data));
512 foreach_list(node, parameters) {
513 ir_rvalue *param = (ir_rvalue *) node;
514 unsigned rhs_components = param->type->components();
516 /* Do not try to assign more components to the vector than it has!
518 if ((rhs_components + base_component) > lhs_components) {
519 rhs_components = lhs_components - base_component;
522 const ir_constant *const c = param->as_constant();
523 if (c != NULL) {
524 for (unsigned i = 0; i < rhs_components; i++) {
525 switch (c->type->base_type) {
526 case GLSL_TYPE_UINT:
527 data.u[i + base_component] = c->get_uint_component(i);
528 break;
529 case GLSL_TYPE_INT:
530 data.i[i + base_component] = c->get_int_component(i);
531 break;
532 case GLSL_TYPE_FLOAT:
533 data.f[i + base_component] = c->get_float_component(i);
534 break;
535 case GLSL_TYPE_BOOL:
536 data.b[i + base_component] = c->get_bool_component(i);
537 break;
538 default:
539 assert(!"Should not get here.");
540 break;
544 /* Mask of fields to be written in the assignment.
546 constant_mask |= ((1U << rhs_components) - 1) << base_component;
549 /* Advance the component index by the number of components that were
550 * just assigned.
552 base_component += rhs_components;
555 if (constant_mask != 0) {
556 ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
557 ir_rvalue *rhs = new(ctx) ir_constant(var->type, &data);
559 ir_instruction *inst =
560 new(ctx) ir_assignment(lhs, rhs, NULL, constant_mask);
561 instructions->push_tail(inst);
564 base_component = 0;
565 foreach_list(node, parameters) {
566 ir_rvalue *param = (ir_rvalue *) node;
567 unsigned rhs_components = param->type->components();
569 /* Do not try to assign more components to the vector than it has!
571 if ((rhs_components + base_component) > lhs_components) {
572 rhs_components = lhs_components - base_component;
575 const ir_constant *const c = param->as_constant();
576 if (c == NULL) {
577 /* Generate a swizzle that puts the first element of the source at
578 * the location of the first element of the destination.
580 unsigned swiz[4] = { 0, 0, 0, 0 };
581 for (unsigned i = 0; i < rhs_components; i++)
582 swiz[i + base_component] = i;
584 /* Mask of fields to be written in the assignment.
586 const unsigned write_mask = ((1U << rhs_components) - 1)
587 << base_component;
589 ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
590 ir_rvalue *rhs = new(ctx) ir_swizzle(param, swiz, lhs_components);
592 ir_instruction *inst =
593 new(ctx) ir_assignment(lhs, rhs, NULL, write_mask);
594 instructions->push_tail(inst);
597 /* Advance the component index by the number of components that were
598 * just assigned.
600 base_component += rhs_components;
603 return new(ctx) ir_dereference_variable(var);
608 * Generate assignment of a portion of a vector to a portion of a matrix column
610 * \param src_base First component of the source to be used in assignment
611 * \param column Column of destination to be assiged
612 * \param row_base First component of the destination column to be assigned
613 * \param count Number of components to be assigned
615 * \note
616 * \c src_base + \c count must be less than or equal to the number of components
617 * in the source vector.
619 ir_instruction *
620 assign_to_matrix_column(ir_variable *var, unsigned column, unsigned row_base,
621 ir_rvalue *src, unsigned src_base, unsigned count,
622 void *mem_ctx)
624 ir_constant *col_idx = new(mem_ctx) ir_constant(column);
625 ir_dereference *column_ref = new(mem_ctx) ir_dereference_array(var, col_idx);
627 assert(column_ref->type->components() >= (row_base + count));
628 assert(src->type->components() >= (src_base + count));
630 /* Generate a swizzle that puts the first element of the source at the
631 * location of the first element of the destination.
633 unsigned swiz[4] = { src_base, src_base, src_base, src_base };
634 for (unsigned i = 0; i < count; i++)
635 swiz[i + row_base] = src_base + i;
637 ir_rvalue *const rhs =
638 new(mem_ctx) ir_swizzle(src, swiz, column_ref->type->components());
640 /* Mask of fields to be written in the assignment.
642 const unsigned write_mask = ((1U << count) - 1) << row_base;
644 return new(mem_ctx) ir_assignment(column_ref, rhs, NULL, write_mask);
649 * Generate inline code for a matrix constructor
651 * The generated constructor code will consist of a temporary variable
652 * declaration of the same type as the constructor. A sequence of assignments
653 * from constructor parameters to the temporary will follow.
655 * \return
656 * An \c ir_dereference_variable of the temprorary generated in the constructor
657 * body.
659 ir_rvalue *
660 emit_inline_matrix_constructor(const glsl_type *type,
661 exec_list *instructions,
662 exec_list *parameters,
663 void *ctx)
665 assert(!parameters->is_empty());
667 ir_variable *var = new(ctx) ir_variable(type, "mat_ctor", ir_var_temporary);
668 instructions->push_tail(var);
670 /* There are three kinds of matrix constructors.
672 * - Construct a matrix from a single scalar by replicating that scalar to
673 * along the diagonal of the matrix and setting all other components to
674 * zero.
676 * - Construct a matrix from an arbirary combination of vectors and
677 * scalars. The components of the constructor parameters are assigned
678 * to the matrix in colum-major order until the matrix is full.
680 * - Construct a matrix from a single matrix. The source matrix is copied
681 * to the upper left portion of the constructed matrix, and the remaining
682 * elements take values from the identity matrix.
684 ir_rvalue *const first_param = (ir_rvalue *) parameters->head;
685 if (single_scalar_parameter(parameters)) {
686 /* Assign the scalar to the X component of a vec4, and fill the remaining
687 * components with zero.
689 ir_variable *rhs_var =
690 new(ctx) ir_variable(glsl_type::vec4_type, "mat_ctor_vec",
691 ir_var_temporary);
692 instructions->push_tail(rhs_var);
694 ir_constant_data zero;
695 zero.f[0] = 0.0;
696 zero.f[1] = 0.0;
697 zero.f[2] = 0.0;
698 zero.f[3] = 0.0;
700 ir_instruction *inst =
701 new(ctx) ir_assignment(new(ctx) ir_dereference_variable(rhs_var),
702 new(ctx) ir_constant(rhs_var->type, &zero),
703 NULL);
704 instructions->push_tail(inst);
706 ir_dereference *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
708 inst = new(ctx) ir_assignment(rhs_ref, first_param, NULL, 0x01);
709 instructions->push_tail(inst);
711 /* Assign the temporary vector to each column of the destination matrix
712 * with a swizzle that puts the X component on the diagonal of the
713 * matrix. In some cases this may mean that the X component does not
714 * get assigned into the column at all (i.e., when the matrix has more
715 * columns than rows).
717 static const unsigned rhs_swiz[4][4] = {
718 { 0, 1, 1, 1 },
719 { 1, 0, 1, 1 },
720 { 1, 1, 0, 1 },
721 { 1, 1, 1, 0 }
724 const unsigned cols_to_init = MIN2(type->matrix_columns,
725 type->vector_elements);
726 for (unsigned i = 0; i < cols_to_init; i++) {
727 ir_constant *const col_idx = new(ctx) ir_constant(i);
728 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
730 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
731 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, rhs_swiz[i],
732 type->vector_elements);
734 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
735 instructions->push_tail(inst);
738 for (unsigned i = cols_to_init; i < type->matrix_columns; i++) {
739 ir_constant *const col_idx = new(ctx) ir_constant(i);
740 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
742 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
743 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, 1, 1, 1, 1,
744 type->vector_elements);
746 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
747 instructions->push_tail(inst);
749 } else if (first_param->type->is_matrix()) {
750 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
752 * "If a matrix is constructed from a matrix, then each component
753 * (column i, row j) in the result that has a corresponding
754 * component (column i, row j) in the argument will be initialized
755 * from there. All other components will be initialized to the
756 * identity matrix. If a matrix argument is given to a matrix
757 * constructor, it is an error to have any other arguments."
759 assert(first_param->next->is_tail_sentinel());
760 ir_rvalue *const src_matrix = first_param;
762 /* If the source matrix is smaller, pre-initialize the relavent parts of
763 * the destination matrix to the identity matrix.
765 if ((src_matrix->type->matrix_columns < var->type->matrix_columns)
766 || (src_matrix->type->vector_elements < var->type->vector_elements)) {
768 /* If the source matrix has fewer rows, every column of the destination
769 * must be initialized. Otherwise only the columns in the destination
770 * that do not exist in the source must be initialized.
772 unsigned col =
773 (src_matrix->type->vector_elements < var->type->vector_elements)
774 ? 0 : src_matrix->type->matrix_columns;
776 const glsl_type *const col_type = var->type->column_type();
777 for (/* empty */; col < var->type->matrix_columns; col++) {
778 ir_constant_data ident;
780 ident.f[0] = 0.0;
781 ident.f[1] = 0.0;
782 ident.f[2] = 0.0;
783 ident.f[3] = 0.0;
785 ident.f[col] = 1.0;
787 ir_rvalue *const rhs = new(ctx) ir_constant(col_type, &ident);
789 ir_rvalue *const lhs =
790 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(col));
792 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
793 instructions->push_tail(inst);
797 /* Assign columns from the source matrix to the destination matrix.
799 * Since the parameter will be used in the RHS of multiple assignments,
800 * generate a temporary and copy the paramter there.
802 ir_variable *const rhs_var =
803 new(ctx) ir_variable(first_param->type, "mat_ctor_mat",
804 ir_var_temporary);
805 instructions->push_tail(rhs_var);
807 ir_dereference *const rhs_var_ref =
808 new(ctx) ir_dereference_variable(rhs_var);
809 ir_instruction *const inst =
810 new(ctx) ir_assignment(rhs_var_ref, first_param, NULL);
811 instructions->push_tail(inst);
813 const unsigned last_row = MIN2(src_matrix->type->vector_elements,
814 var->type->vector_elements);
815 const unsigned last_col = MIN2(src_matrix->type->matrix_columns,
816 var->type->matrix_columns);
818 unsigned swiz[4] = { 0, 0, 0, 0 };
819 for (unsigned i = 1; i < src_matrix->type->vector_elements; i++)
820 swiz[i] = i;
822 const unsigned write_mask = (1U << last_row) - 1;
824 for (unsigned i = 0; i < last_col; i++) {
825 ir_dereference *const lhs =
826 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(i));
827 ir_rvalue *const rhs_col =
828 new(ctx) ir_dereference_array(rhs_var, new(ctx) ir_constant(i));
830 /* If one matrix has columns that are smaller than the columns of the
831 * other matrix, wrap the column access of the larger with a swizzle
832 * so that the LHS and RHS of the assignment have the same size (and
833 * therefore have the same type).
835 * It would be perfectly valid to unconditionally generate the
836 * swizzles, this this will typically result in a more compact IR tree.
838 ir_rvalue *rhs;
839 if (lhs->type->vector_elements != rhs_col->type->vector_elements) {
840 rhs = new(ctx) ir_swizzle(rhs_col, swiz,
841 lhs->type->vector_elements);
842 } else {
843 rhs = rhs_col;
846 assert(lhs->type == rhs->type);
848 ir_instruction *inst =
849 new(ctx) ir_assignment(lhs, rhs, NULL, write_mask);
850 instructions->push_tail(inst);
852 } else {
853 const unsigned cols = type->matrix_columns;
854 const unsigned rows = type->vector_elements;
855 unsigned col_idx = 0;
856 unsigned row_idx = 0;
858 foreach_list (node, parameters) {
859 ir_rvalue *const rhs = (ir_rvalue *) node;
860 const unsigned components_remaining_this_column = rows - row_idx;
861 unsigned rhs_components = rhs->type->components();
862 unsigned rhs_base = 0;
864 /* Since the parameter might be used in the RHS of two assignments,
865 * generate a temporary and copy the paramter there.
867 ir_variable *rhs_var =
868 new(ctx) ir_variable(rhs->type, "mat_ctor_vec", ir_var_temporary);
869 instructions->push_tail(rhs_var);
871 ir_dereference *rhs_var_ref =
872 new(ctx) ir_dereference_variable(rhs_var);
873 ir_instruction *inst = new(ctx) ir_assignment(rhs_var_ref, rhs, NULL);
874 instructions->push_tail(inst);
876 /* Assign the current parameter to as many components of the matrix
877 * as it will fill.
879 * NOTE: A single vector parameter can span two matrix columns. A
880 * single vec4, for example, can completely fill a mat2.
882 if (rhs_components >= components_remaining_this_column) {
883 const unsigned count = MIN2(rhs_components,
884 components_remaining_this_column);
886 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
888 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
889 row_idx,
890 rhs_var_ref, 0,
891 count, ctx);
892 instructions->push_tail(inst);
894 rhs_base = count;
896 col_idx++;
897 row_idx = 0;
900 /* If there is data left in the parameter and components left to be
901 * set in the destination, emit another assignment. It is possible
902 * that the assignment could be of a vec4 to the last element of the
903 * matrix. In this case col_idx==cols, but there is still data
904 * left in the source parameter. Obviously, don't emit an assignment
905 * to data outside the destination matrix.
907 if ((col_idx < cols) && (rhs_base < rhs_components)) {
908 const unsigned count = rhs_components - rhs_base;
910 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
912 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
913 row_idx,
914 rhs_var_ref,
915 rhs_base,
916 count, ctx);
917 instructions->push_tail(inst);
919 row_idx += count;
924 return new(ctx) ir_dereference_variable(var);
928 ir_rvalue *
929 emit_inline_record_constructor(const glsl_type *type,
930 exec_list *instructions,
931 exec_list *parameters,
932 void *mem_ctx)
934 ir_variable *const var =
935 new(mem_ctx) ir_variable(type, "record_ctor", ir_var_temporary);
936 ir_dereference_variable *const d = new(mem_ctx) ir_dereference_variable(var);
938 instructions->push_tail(var);
940 exec_node *node = parameters->head;
941 for (unsigned i = 0; i < type->length; i++) {
942 assert(!node->is_tail_sentinel());
944 ir_dereference *const lhs =
945 new(mem_ctx) ir_dereference_record(d->clone(mem_ctx, NULL),
946 type->fields.structure[i].name);
948 ir_rvalue *const rhs = ((ir_instruction *) node)->as_rvalue();
949 assert(rhs != NULL);
951 ir_instruction *const assign = new(mem_ctx) ir_assignment(lhs, rhs, NULL);
953 instructions->push_tail(assign);
954 node = node->next;
957 return d;
961 ir_rvalue *
962 ast_function_expression::hir(exec_list *instructions,
963 struct _mesa_glsl_parse_state *state)
965 void *ctx = state;
966 /* There are three sorts of function calls.
968 * 1. constructors - The first subexpression is an ast_type_specifier.
969 * 2. methods - Only the .length() method of array types.
970 * 3. functions - Calls to regular old functions.
972 * Method calls are actually detected when the ast_field_selection
973 * expression is handled.
975 if (is_constructor()) {
976 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
977 YYLTYPE loc = type->get_location();
978 const char *name;
980 const glsl_type *const constructor_type = type->glsl_type(& name, state);
983 /* Constructors for samplers are illegal.
985 if (constructor_type->is_sampler()) {
986 _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
987 constructor_type->name);
988 return ir_call::get_error_instruction(ctx);
991 if (constructor_type->is_array()) {
992 if (state->language_version <= 110) {
993 _mesa_glsl_error(& loc, state,
994 "array constructors forbidden in GLSL 1.10");
995 return ir_call::get_error_instruction(ctx);
998 return process_array_constructor(instructions, constructor_type,
999 & loc, &this->expressions, state);
1003 /* There are two kinds of constructor call. Constructors for built-in
1004 * language types, such as mat4 and vec2, are free form. The only
1005 * requirement is that the parameters must provide enough values of the
1006 * correct scalar type. Constructors for arrays and structures must
1007 * have the exact number of parameters with matching types in the
1008 * correct order. These constructors follow essentially the same type
1009 * matching rules as functions.
1011 if (!constructor_type->is_numeric() && !constructor_type->is_boolean())
1012 return ir_call::get_error_instruction(ctx);
1014 /* Total number of components of the type being constructed. */
1015 const unsigned type_components = constructor_type->components();
1017 /* Number of components from parameters that have actually been
1018 * consumed. This is used to perform several kinds of error checking.
1020 unsigned components_used = 0;
1022 unsigned matrix_parameters = 0;
1023 unsigned nonmatrix_parameters = 0;
1024 exec_list actual_parameters;
1026 foreach_list (n, &this->expressions) {
1027 ast_node *ast = exec_node_data(ast_node, n, link);
1028 ir_rvalue *result = ast->hir(instructions, state)->as_rvalue();
1030 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1032 * "It is an error to provide extra arguments beyond this
1033 * last used argument."
1035 if (components_used >= type_components) {
1036 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
1037 "constructor",
1038 constructor_type->name);
1039 return ir_call::get_error_instruction(ctx);
1042 if (!result->type->is_numeric() && !result->type->is_boolean()) {
1043 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
1044 "non-numeric data type",
1045 constructor_type->name);
1046 return ir_call::get_error_instruction(ctx);
1049 /* Count the number of matrix and nonmatrix parameters. This
1050 * is used below to enforce some of the constructor rules.
1052 if (result->type->is_matrix())
1053 matrix_parameters++;
1054 else
1055 nonmatrix_parameters++;
1057 actual_parameters.push_tail(result);
1058 components_used += result->type->components();
1061 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1063 * "It is an error to construct matrices from other matrices. This
1064 * is reserved for future use."
1066 if (state->language_version == 110 && matrix_parameters > 0
1067 && constructor_type->is_matrix()) {
1068 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
1069 "matrix in GLSL 1.10",
1070 constructor_type->name);
1071 return ir_call::get_error_instruction(ctx);
1074 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1076 * "If a matrix argument is given to a matrix constructor, it is
1077 * an error to have any other arguments."
1079 if ((matrix_parameters > 0)
1080 && ((matrix_parameters + nonmatrix_parameters) > 1)
1081 && constructor_type->is_matrix()) {
1082 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
1083 "matrix must be only parameter",
1084 constructor_type->name);
1085 return ir_call::get_error_instruction(ctx);
1088 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1090 * "In these cases, there must be enough components provided in the
1091 * arguments to provide an initializer for every component in the
1092 * constructed value."
1094 if (components_used < type_components && components_used != 1
1095 && matrix_parameters == 0) {
1096 _mesa_glsl_error(& loc, state, "too few components to construct "
1097 "`%s'",
1098 constructor_type->name);
1099 return ir_call::get_error_instruction(ctx);
1102 /* Later, we cast each parameter to the same base type as the
1103 * constructor. Since there are no non-floating point matrices, we
1104 * need to break them up into a series of column vectors.
1106 if (constructor_type->base_type != GLSL_TYPE_FLOAT) {
1107 foreach_list_safe(n, &actual_parameters) {
1108 ir_rvalue *matrix = (ir_rvalue *) n;
1110 if (!matrix->type->is_matrix())
1111 continue;
1113 /* Create a temporary containing the matrix. */
1114 ir_variable *var = new(ctx) ir_variable(matrix->type, "matrix_tmp",
1115 ir_var_temporary);
1116 instructions->push_tail(var);
1117 instructions->push_tail(new(ctx) ir_assignment(new(ctx)
1118 ir_dereference_variable(var), matrix, NULL));
1119 var->constant_value = matrix->constant_expression_value();
1121 /* Replace the matrix with dereferences of its columns. */
1122 for (int i = 0; i < matrix->type->matrix_columns; i++) {
1123 matrix->insert_before(new (ctx) ir_dereference_array(var,
1124 new(ctx) ir_constant(i)));
1126 matrix->remove();
1130 bool all_parameters_are_constant = true;
1132 /* Type cast each parameter and, if possible, fold constants.*/
1133 foreach_list_safe(n, &actual_parameters) {
1134 ir_rvalue *ir = (ir_rvalue *) n;
1136 const glsl_type *desired_type =
1137 glsl_type::get_instance(constructor_type->base_type,
1138 ir->type->vector_elements,
1139 ir->type->matrix_columns);
1140 ir_rvalue *result = convert_component(ir, desired_type);
1142 /* Attempt to convert the parameter to a constant valued expression.
1143 * After doing so, track whether or not all the parameters to the
1144 * constructor are trivially constant valued expressions.
1146 ir_rvalue *const constant = result->constant_expression_value();
1148 if (constant != NULL)
1149 result = constant;
1150 else
1151 all_parameters_are_constant = false;
1153 if (result != ir) {
1154 ir->replace_with(result);
1158 /* If all of the parameters are trivially constant, create a
1159 * constant representing the complete collection of parameters.
1161 if (all_parameters_are_constant) {
1162 return new(ctx) ir_constant(constructor_type, &actual_parameters);
1163 } else if (constructor_type->is_scalar()) {
1164 return dereference_component((ir_rvalue *) actual_parameters.head,
1166 } else if (constructor_type->is_vector()) {
1167 return emit_inline_vector_constructor(constructor_type,
1168 instructions,
1169 &actual_parameters,
1170 ctx);
1171 } else {
1172 assert(constructor_type->is_matrix());
1173 return emit_inline_matrix_constructor(constructor_type,
1174 instructions,
1175 &actual_parameters,
1176 ctx);
1178 } else {
1179 const ast_expression *id = subexpressions[0];
1180 YYLTYPE loc = id->get_location();
1181 exec_list actual_parameters;
1183 process_parameters(instructions, &actual_parameters, &this->expressions,
1184 state);
1186 const glsl_type *const type =
1187 state->symbols->get_type(id->primary_expression.identifier);
1189 if ((type != NULL) && type->is_record()) {
1190 exec_node *node = actual_parameters.head;
1191 for (unsigned i = 0; i < type->length; i++) {
1192 ir_rvalue *ir = (ir_rvalue *) node;
1194 if (node->is_tail_sentinel()) {
1195 _mesa_glsl_error(&loc, state,
1196 "insufficient parameters to constructor "
1197 "for `%s'",
1198 type->name);
1199 return ir_call::get_error_instruction(ctx);
1202 if (apply_implicit_conversion(type->fields.structure[i].type, ir,
1203 state)) {
1204 node->replace_with(ir);
1205 } else {
1206 _mesa_glsl_error(&loc, state,
1207 "parameter type mismatch in constructor "
1208 "for `%s.%s' (%s vs %s)",
1209 type->name,
1210 type->fields.structure[i].name,
1211 ir->type->name,
1212 type->fields.structure[i].type->name);
1213 return ir_call::get_error_instruction(ctx);;
1216 node = node->next;
1219 if (!node->is_tail_sentinel()) {
1220 _mesa_glsl_error(&loc, state, "too many parameters in constructor "
1221 "for `%s'", type->name);
1222 return ir_call::get_error_instruction(ctx);
1225 ir_rvalue *const constant =
1226 constant_record_constructor(type, &actual_parameters, state);
1228 return (constant != NULL)
1229 ? constant
1230 : emit_inline_record_constructor(type, instructions,
1231 &actual_parameters, state);
1234 return match_function_by_name(instructions,
1235 id->primary_expression.identifier, & loc,
1236 &actual_parameters, state);
1239 return ir_call::get_error_instruction(ctx);