use log2 from stdc
[AROS.git] / workbench / libs / mesa / src / glsl / ast_function.cpp
blob9aa8556f3dd67b6d5d3c2a077f2b319a7e502555
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 List of \c ir_instruction nodes representing the
66 * parameter list for the function. This may be either a
67 * formal (\c ir_variable) or actual (\c ir_rvalue)
68 * parameter list. Only the type is used.
70 * \return
71 * A ralloced string representing the prototype of the function.
73 char *
74 prototype_string(const glsl_type *return_type, const char *name,
75 exec_list *parameters)
77 char *str = NULL;
79 if (return_type != NULL)
80 str = ralloc_asprintf(NULL, "%s ", return_type->name);
82 ralloc_asprintf_append(&str, "%s(", name);
84 const char *comma = "";
85 foreach_list(node, parameters) {
86 const ir_instruction *const param = (ir_instruction *) node;
88 ralloc_asprintf_append(&str, "%s%s", comma, param->type->name);
89 comma = ", ";
92 ralloc_strcat(&str, ")");
93 return str;
97 static ir_rvalue *
98 match_function_by_name(exec_list *instructions, const char *name,
99 YYLTYPE *loc, exec_list *actual_parameters,
100 struct _mesa_glsl_parse_state *state)
102 void *ctx = state;
103 ir_function *f = state->symbols->get_function(name);
104 ir_function_signature *sig;
106 sig = f ? f->matching_signature(actual_parameters) : NULL;
108 /* FINISHME: This doesn't handle the case where shader X contains a
109 * FINISHME: matching signature but shader X + N contains an _exact_
110 * FINISHME: matching signature.
112 if (sig == NULL
113 && (f == NULL || state->es_shader || !f->has_user_signature())
114 && state->symbols->get_type(name) == NULL
115 && (state->language_version == 110
116 || state->symbols->get_variable(name) == NULL)) {
117 /* The current shader doesn't contain a matching function or signature.
118 * Before giving up, look for the prototype in the built-in functions.
120 for (unsigned i = 0; i < state->num_builtins_to_link; i++) {
121 ir_function *builtin;
122 builtin = state->builtins_to_link[i]->symbols->get_function(name);
123 sig = builtin ? builtin->matching_signature(actual_parameters) : NULL;
124 if (sig != NULL) {
125 if (f == NULL) {
126 f = new(ctx) ir_function(name);
127 state->symbols->add_global_function(f);
128 emit_function(state, instructions, f);
131 f->add_signature(sig->clone_prototype(f, NULL));
132 break;
137 if (sig != NULL) {
138 /* Verify that 'out' and 'inout' actual parameters are lvalues. This
139 * isn't done in ir_function::matching_signature because that function
140 * cannot generate the necessary diagnostics.
142 * Also, validate that 'const_in' formal parameters (an extension of our
143 * IR) correspond to ir_constant actual parameters.
145 exec_list_iterator actual_iter = actual_parameters->iterator();
146 exec_list_iterator formal_iter = sig->parameters.iterator();
148 while (actual_iter.has_next()) {
149 ir_rvalue *actual = (ir_rvalue *) actual_iter.get();
150 ir_variable *formal = (ir_variable *) formal_iter.get();
152 assert(actual != NULL);
153 assert(formal != NULL);
155 if (formal->mode == ir_var_const_in && !actual->as_constant()) {
156 _mesa_glsl_error(loc, state,
157 "parameter `%s' must be a constant expression",
158 formal->name);
161 if ((formal->mode == ir_var_out)
162 || (formal->mode == ir_var_inout)) {
163 const char *mode = NULL;
164 switch (formal->mode) {
165 case ir_var_out: mode = "out"; break;
166 case ir_var_inout: mode = "inout"; break;
167 default: assert(false); break;
169 /* FIXME: 'loc' is incorrect (as of 2011-01-21). It is always
170 * FIXME: 0:0(0).
172 if (actual->variable_referenced()
173 && actual->variable_referenced()->read_only) {
174 _mesa_glsl_error(loc, state,
175 "function parameter '%s %s' references the "
176 "read-only variable '%s'",
177 mode, formal->name,
178 actual->variable_referenced()->name);
180 } else if (!actual->is_lvalue()) {
181 _mesa_glsl_error(loc, state,
182 "function parameter '%s %s' is not an lvalue",
183 mode, formal->name);
187 if (formal->type->is_numeric() || formal->type->is_boolean()) {
188 ir_rvalue *converted = convert_component(actual, formal->type);
189 actual->replace_with(converted);
192 actual_iter.next();
193 formal_iter.next();
196 /* Always insert the call in the instruction stream, and return a deref
197 * of its return val if it returns a value, since we don't know if
198 * the rvalue is going to be assigned to anything or not.
200 ir_call *call = new(ctx) ir_call(sig, actual_parameters);
201 if (!sig->return_type->is_void()) {
202 ir_variable *var;
203 ir_dereference_variable *deref;
205 var = new(ctx) ir_variable(sig->return_type,
206 ralloc_asprintf(ctx, "%s_retval",
207 sig->function_name()),
208 ir_var_temporary);
209 instructions->push_tail(var);
211 deref = new(ctx) ir_dereference_variable(var);
212 ir_assignment *assign = new(ctx) ir_assignment(deref, call, NULL);
213 instructions->push_tail(assign);
214 if (state->language_version >= 120)
215 var->constant_value = call->constant_expression_value();
217 deref = new(ctx) ir_dereference_variable(var);
218 return deref;
219 } else {
220 instructions->push_tail(call);
221 return NULL;
223 } else {
224 char *str = prototype_string(NULL, name, actual_parameters);
226 _mesa_glsl_error(loc, state, "no matching function for call to `%s'",
227 str);
228 ralloc_free(str);
230 const char *prefix = "candidates are: ";
232 for (int i = -1; i < (int) state->num_builtins_to_link; i++) {
233 glsl_symbol_table *syms = i >= 0 ? state->builtins_to_link[i]->symbols
234 : state->symbols;
235 f = syms->get_function(name);
236 if (f == NULL)
237 continue;
239 foreach_list (node, &f->signatures) {
240 ir_function_signature *sig = (ir_function_signature *) node;
242 str = prototype_string(sig->return_type, f->name, &sig->parameters);
243 _mesa_glsl_error(loc, state, "%s%s", prefix, str);
244 ralloc_free(str);
246 prefix = " ";
251 return ir_call::get_error_instruction(ctx);
257 * Perform automatic type conversion of constructor parameters
259 * This implements the rules in the "Conversion and Scalar Constructors"
260 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
262 static ir_rvalue *
263 convert_component(ir_rvalue *src, const glsl_type *desired_type)
265 void *ctx = ralloc_parent(src);
266 const unsigned a = desired_type->base_type;
267 const unsigned b = src->type->base_type;
268 ir_expression *result = NULL;
270 if (src->type->is_error())
271 return src;
273 assert(a <= GLSL_TYPE_BOOL);
274 assert(b <= GLSL_TYPE_BOOL);
276 if ((a == b) || (src->type->is_integer() && desired_type->is_integer()))
277 return src;
279 switch (a) {
280 case GLSL_TYPE_UINT:
281 case GLSL_TYPE_INT:
282 if (b == GLSL_TYPE_FLOAT)
283 result = new(ctx) ir_expression(ir_unop_f2i, desired_type, src, NULL);
284 else {
285 assert(b == GLSL_TYPE_BOOL);
286 result = new(ctx) ir_expression(ir_unop_b2i, desired_type, src, NULL);
288 break;
289 case GLSL_TYPE_FLOAT:
290 switch (b) {
291 case GLSL_TYPE_UINT:
292 result = new(ctx) ir_expression(ir_unop_u2f, desired_type, src, NULL);
293 break;
294 case GLSL_TYPE_INT:
295 result = new(ctx) ir_expression(ir_unop_i2f, desired_type, src, NULL);
296 break;
297 case GLSL_TYPE_BOOL:
298 result = new(ctx) ir_expression(ir_unop_b2f, desired_type, src, NULL);
299 break;
301 break;
302 case GLSL_TYPE_BOOL:
303 switch (b) {
304 case GLSL_TYPE_UINT:
305 case GLSL_TYPE_INT:
306 result = new(ctx) ir_expression(ir_unop_i2b, desired_type, src, NULL);
307 break;
308 case GLSL_TYPE_FLOAT:
309 result = new(ctx) ir_expression(ir_unop_f2b, desired_type, src, NULL);
310 break;
312 break;
315 assert(result != NULL);
317 /* Try constant folding; it may fold in the conversion we just added. */
318 ir_constant *const constant = result->constant_expression_value();
319 return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result;
323 * Dereference a specific component from a scalar, vector, or matrix
325 static ir_rvalue *
326 dereference_component(ir_rvalue *src, unsigned component)
328 void *ctx = ralloc_parent(src);
329 assert(component < src->type->components());
331 /* If the source is a constant, just create a new constant instead of a
332 * dereference of the existing constant.
334 ir_constant *constant = src->as_constant();
335 if (constant)
336 return new(ctx) ir_constant(constant, component);
338 if (src->type->is_scalar()) {
339 return src;
340 } else if (src->type->is_vector()) {
341 return new(ctx) ir_swizzle(src, component, 0, 0, 0, 1);
342 } else {
343 assert(src->type->is_matrix());
345 /* Dereference a row of the matrix, then call this function again to get
346 * a specific element from that row.
348 const int c = component / src->type->column_type()->vector_elements;
349 const int r = component % src->type->column_type()->vector_elements;
350 ir_constant *const col_index = new(ctx) ir_constant(c);
351 ir_dereference *const col = new(ctx) ir_dereference_array(src, col_index);
353 col->type = src->type->column_type();
355 return dereference_component(col, r);
358 assert(!"Should not get here.");
359 return NULL;
363 static ir_rvalue *
364 process_array_constructor(exec_list *instructions,
365 const glsl_type *constructor_type,
366 YYLTYPE *loc, exec_list *parameters,
367 struct _mesa_glsl_parse_state *state)
369 void *ctx = state;
370 /* Array constructors come in two forms: sized and unsized. Sized array
371 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
372 * variables. In this case the number of parameters must exactly match the
373 * specified size of the array.
375 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
376 * are vec4 variables. In this case the size of the array being constructed
377 * is determined by the number of parameters.
379 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
381 * "There must be exactly the same number of arguments as the size of
382 * the array being constructed. If no size is present in the
383 * constructor, then the array is explicitly sized to the number of
384 * arguments provided. The arguments are assigned in order, starting at
385 * element 0, to the elements of the constructed array. Each argument
386 * must be the same type as the element type of the array, or be a type
387 * that can be converted to the element type of the array according to
388 * Section 4.1.10 "Implicit Conversions.""
390 exec_list actual_parameters;
391 const unsigned parameter_count =
392 process_parameters(instructions, &actual_parameters, parameters, state);
394 if ((parameter_count == 0)
395 || ((constructor_type->length != 0)
396 && (constructor_type->length != parameter_count))) {
397 const unsigned min_param = (constructor_type->length == 0)
398 ? 1 : constructor_type->length;
400 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
401 "parameter%s",
402 (constructor_type->length != 0) ? "at least" : "exactly",
403 min_param, (min_param <= 1) ? "" : "s");
404 return ir_call::get_error_instruction(ctx);
407 if (constructor_type->length == 0) {
408 constructor_type =
409 glsl_type::get_array_instance(constructor_type->element_type(),
410 parameter_count);
411 assert(constructor_type != NULL);
412 assert(constructor_type->length == parameter_count);
415 bool all_parameters_are_constant = true;
417 /* Type cast each parameter and, if possible, fold constants. */
418 foreach_list_safe(n, &actual_parameters) {
419 ir_rvalue *ir = (ir_rvalue *) n;
420 ir_rvalue *result = ir;
422 /* Apply implicit conversions (not the scalar constructor rules!) */
423 if (constructor_type->element_type()->is_float()) {
424 const glsl_type *desired_type =
425 glsl_type::get_instance(GLSL_TYPE_FLOAT,
426 ir->type->vector_elements,
427 ir->type->matrix_columns);
428 result = convert_component(ir, desired_type);
431 if (result->type != constructor_type->element_type()) {
432 _mesa_glsl_error(loc, state, "type error in array constructor: "
433 "expected: %s, found %s",
434 constructor_type->element_type()->name,
435 result->type->name);
438 /* Attempt to convert the parameter to a constant valued expression.
439 * After doing so, track whether or not all the parameters to the
440 * constructor are trivially constant valued expressions.
442 ir_rvalue *const constant = result->constant_expression_value();
444 if (constant != NULL)
445 result = constant;
446 else
447 all_parameters_are_constant = false;
449 ir->replace_with(result);
452 if (all_parameters_are_constant)
453 return new(ctx) ir_constant(constructor_type, &actual_parameters);
455 ir_variable *var = new(ctx) ir_variable(constructor_type, "array_ctor",
456 ir_var_temporary);
457 instructions->push_tail(var);
459 int i = 0;
460 foreach_list(node, &actual_parameters) {
461 ir_rvalue *rhs = (ir_rvalue *) node;
462 ir_rvalue *lhs = new(ctx) ir_dereference_array(var,
463 new(ctx) ir_constant(i));
465 ir_instruction *assignment = new(ctx) ir_assignment(lhs, rhs, NULL);
466 instructions->push_tail(assignment);
468 i++;
471 return new(ctx) ir_dereference_variable(var);
476 * Try to convert a record constructor to a constant expression
478 static ir_constant *
479 constant_record_constructor(const glsl_type *constructor_type,
480 exec_list *parameters, void *mem_ctx)
482 foreach_list(node, parameters) {
483 ir_constant *constant = ((ir_instruction *) node)->as_constant();
484 if (constant == NULL)
485 return NULL;
486 node->replace_with(constant);
489 return new(mem_ctx) ir_constant(constructor_type, parameters);
494 * Determine if a list consists of a single scalar r-value
496 bool
497 single_scalar_parameter(exec_list *parameters)
499 const ir_rvalue *const p = (ir_rvalue *) parameters->head;
500 assert(((ir_rvalue *)p)->as_rvalue() != NULL);
502 return (p->type->is_scalar() && p->next->is_tail_sentinel());
507 * Generate inline code for a vector constructor
509 * The generated constructor code will consist of a temporary variable
510 * declaration of the same type as the constructor. A sequence of assignments
511 * from constructor parameters to the temporary will follow.
513 * \return
514 * An \c ir_dereference_variable of the temprorary generated in the constructor
515 * body.
517 ir_rvalue *
518 emit_inline_vector_constructor(const glsl_type *type,
519 exec_list *instructions,
520 exec_list *parameters,
521 void *ctx)
523 assert(!parameters->is_empty());
525 ir_variable *var = new(ctx) ir_variable(type, "vec_ctor", ir_var_temporary);
526 instructions->push_tail(var);
528 /* There are two kinds of vector constructors.
530 * - Construct a vector from a single scalar by replicating that scalar to
531 * all components of the vector.
533 * - Construct a vector from an arbirary combination of vectors and
534 * scalars. The components of the constructor parameters are assigned
535 * to the vector in order until the vector is full.
537 const unsigned lhs_components = type->components();
538 if (single_scalar_parameter(parameters)) {
539 ir_rvalue *first_param = (ir_rvalue *)parameters->head;
540 ir_rvalue *rhs = new(ctx) ir_swizzle(first_param, 0, 0, 0, 0,
541 lhs_components);
542 ir_dereference_variable *lhs = new(ctx) ir_dereference_variable(var);
543 const unsigned mask = (1U << lhs_components) - 1;
545 assert(rhs->type == lhs->type);
547 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL, mask);
548 instructions->push_tail(inst);
549 } else {
550 unsigned base_component = 0;
551 unsigned base_lhs_component = 0;
552 ir_constant_data data;
553 unsigned constant_mask = 0, constant_components = 0;
555 memset(&data, 0, sizeof(data));
557 foreach_list(node, parameters) {
558 ir_rvalue *param = (ir_rvalue *) node;
559 unsigned rhs_components = param->type->components();
561 /* Do not try to assign more components to the vector than it has!
563 if ((rhs_components + base_lhs_component) > lhs_components) {
564 rhs_components = lhs_components - base_lhs_component;
567 const ir_constant *const c = param->as_constant();
568 if (c != NULL) {
569 for (unsigned i = 0; i < rhs_components; i++) {
570 switch (c->type->base_type) {
571 case GLSL_TYPE_UINT:
572 data.u[i + base_component] = c->get_uint_component(i);
573 break;
574 case GLSL_TYPE_INT:
575 data.i[i + base_component] = c->get_int_component(i);
576 break;
577 case GLSL_TYPE_FLOAT:
578 data.f[i + base_component] = c->get_float_component(i);
579 break;
580 case GLSL_TYPE_BOOL:
581 data.b[i + base_component] = c->get_bool_component(i);
582 break;
583 default:
584 assert(!"Should not get here.");
585 break;
589 /* Mask of fields to be written in the assignment.
591 constant_mask |= ((1U << rhs_components) - 1) << base_lhs_component;
592 constant_components += rhs_components;
594 base_component += rhs_components;
596 /* Advance the component index by the number of components
597 * that were just assigned.
599 base_lhs_component += rhs_components;
602 if (constant_mask != 0) {
603 ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
604 const glsl_type *rhs_type = glsl_type::get_instance(var->type->base_type,
605 constant_components,
607 ir_rvalue *rhs = new(ctx) ir_constant(rhs_type, &data);
609 ir_instruction *inst =
610 new(ctx) ir_assignment(lhs, rhs, NULL, constant_mask);
611 instructions->push_tail(inst);
614 base_component = 0;
615 foreach_list(node, parameters) {
616 ir_rvalue *param = (ir_rvalue *) node;
617 unsigned rhs_components = param->type->components();
619 /* Do not try to assign more components to the vector than it has!
621 if ((rhs_components + base_component) > lhs_components) {
622 rhs_components = lhs_components - base_component;
625 const ir_constant *const c = param->as_constant();
626 if (c == NULL) {
627 /* Mask of fields to be written in the assignment.
629 const unsigned write_mask = ((1U << rhs_components) - 1)
630 << base_component;
632 ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
634 /* Generate a swizzle so that LHS and RHS sizes match.
636 ir_rvalue *rhs =
637 new(ctx) ir_swizzle(param, 0, 1, 2, 3, rhs_components);
639 ir_instruction *inst =
640 new(ctx) ir_assignment(lhs, rhs, NULL, write_mask);
641 instructions->push_tail(inst);
644 /* Advance the component index by the number of components that were
645 * just assigned.
647 base_component += rhs_components;
650 return new(ctx) ir_dereference_variable(var);
655 * Generate assignment of a portion of a vector to a portion of a matrix column
657 * \param src_base First component of the source to be used in assignment
658 * \param column Column of destination to be assiged
659 * \param row_base First component of the destination column to be assigned
660 * \param count Number of components to be assigned
662 * \note
663 * \c src_base + \c count must be less than or equal to the number of components
664 * in the source vector.
666 ir_instruction *
667 assign_to_matrix_column(ir_variable *var, unsigned column, unsigned row_base,
668 ir_rvalue *src, unsigned src_base, unsigned count,
669 void *mem_ctx)
671 ir_constant *col_idx = new(mem_ctx) ir_constant(column);
672 ir_dereference *column_ref = new(mem_ctx) ir_dereference_array(var, col_idx);
674 assert(column_ref->type->components() >= (row_base + count));
675 assert(src->type->components() >= (src_base + count));
677 /* Generate a swizzle that extracts the number of components from the source
678 * that are to be assigned to the column of the matrix.
680 if (count < src->type->vector_elements) {
681 src = new(mem_ctx) ir_swizzle(src,
682 src_base + 0, src_base + 1,
683 src_base + 2, src_base + 3,
684 count);
687 /* Mask of fields to be written in the assignment.
689 const unsigned write_mask = ((1U << count) - 1) << row_base;
691 return new(mem_ctx) ir_assignment(column_ref, src, NULL, write_mask);
696 * Generate inline code for a matrix constructor
698 * The generated constructor code will consist of a temporary variable
699 * declaration of the same type as the constructor. A sequence of assignments
700 * from constructor parameters to the temporary will follow.
702 * \return
703 * An \c ir_dereference_variable of the temprorary generated in the constructor
704 * body.
706 ir_rvalue *
707 emit_inline_matrix_constructor(const glsl_type *type,
708 exec_list *instructions,
709 exec_list *parameters,
710 void *ctx)
712 assert(!parameters->is_empty());
714 ir_variable *var = new(ctx) ir_variable(type, "mat_ctor", ir_var_temporary);
715 instructions->push_tail(var);
717 /* There are three kinds of matrix constructors.
719 * - Construct a matrix from a single scalar by replicating that scalar to
720 * along the diagonal of the matrix and setting all other components to
721 * zero.
723 * - Construct a matrix from an arbirary combination of vectors and
724 * scalars. The components of the constructor parameters are assigned
725 * to the matrix in colum-major order until the matrix is full.
727 * - Construct a matrix from a single matrix. The source matrix is copied
728 * to the upper left portion of the constructed matrix, and the remaining
729 * elements take values from the identity matrix.
731 ir_rvalue *const first_param = (ir_rvalue *) parameters->head;
732 if (single_scalar_parameter(parameters)) {
733 /* Assign the scalar to the X component of a vec4, and fill the remaining
734 * components with zero.
736 ir_variable *rhs_var =
737 new(ctx) ir_variable(glsl_type::vec4_type, "mat_ctor_vec",
738 ir_var_temporary);
739 instructions->push_tail(rhs_var);
741 ir_constant_data zero;
742 zero.f[0] = 0.0;
743 zero.f[1] = 0.0;
744 zero.f[2] = 0.0;
745 zero.f[3] = 0.0;
747 ir_instruction *inst =
748 new(ctx) ir_assignment(new(ctx) ir_dereference_variable(rhs_var),
749 new(ctx) ir_constant(rhs_var->type, &zero),
750 NULL);
751 instructions->push_tail(inst);
753 ir_dereference *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
755 inst = new(ctx) ir_assignment(rhs_ref, first_param, NULL, 0x01);
756 instructions->push_tail(inst);
758 /* Assign the temporary vector to each column of the destination matrix
759 * with a swizzle that puts the X component on the diagonal of the
760 * matrix. In some cases this may mean that the X component does not
761 * get assigned into the column at all (i.e., when the matrix has more
762 * columns than rows).
764 static const unsigned rhs_swiz[4][4] = {
765 { 0, 1, 1, 1 },
766 { 1, 0, 1, 1 },
767 { 1, 1, 0, 1 },
768 { 1, 1, 1, 0 }
771 const unsigned cols_to_init = MIN2(type->matrix_columns,
772 type->vector_elements);
773 for (unsigned i = 0; i < cols_to_init; i++) {
774 ir_constant *const col_idx = new(ctx) ir_constant(i);
775 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
777 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
778 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, rhs_swiz[i],
779 type->vector_elements);
781 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
782 instructions->push_tail(inst);
785 for (unsigned i = cols_to_init; i < type->matrix_columns; i++) {
786 ir_constant *const col_idx = new(ctx) ir_constant(i);
787 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
789 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
790 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, 1, 1, 1, 1,
791 type->vector_elements);
793 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
794 instructions->push_tail(inst);
796 } else if (first_param->type->is_matrix()) {
797 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
799 * "If a matrix is constructed from a matrix, then each component
800 * (column i, row j) in the result that has a corresponding
801 * component (column i, row j) in the argument will be initialized
802 * from there. All other components will be initialized to the
803 * identity matrix. If a matrix argument is given to a matrix
804 * constructor, it is an error to have any other arguments."
806 assert(first_param->next->is_tail_sentinel());
807 ir_rvalue *const src_matrix = first_param;
809 /* If the source matrix is smaller, pre-initialize the relavent parts of
810 * the destination matrix to the identity matrix.
812 if ((src_matrix->type->matrix_columns < var->type->matrix_columns)
813 || (src_matrix->type->vector_elements < var->type->vector_elements)) {
815 /* If the source matrix has fewer rows, every column of the destination
816 * must be initialized. Otherwise only the columns in the destination
817 * that do not exist in the source must be initialized.
819 unsigned col =
820 (src_matrix->type->vector_elements < var->type->vector_elements)
821 ? 0 : src_matrix->type->matrix_columns;
823 const glsl_type *const col_type = var->type->column_type();
824 for (/* empty */; col < var->type->matrix_columns; col++) {
825 ir_constant_data ident;
827 ident.f[0] = 0.0;
828 ident.f[1] = 0.0;
829 ident.f[2] = 0.0;
830 ident.f[3] = 0.0;
832 ident.f[col] = 1.0;
834 ir_rvalue *const rhs = new(ctx) ir_constant(col_type, &ident);
836 ir_rvalue *const lhs =
837 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(col));
839 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
840 instructions->push_tail(inst);
844 /* Assign columns from the source matrix to the destination matrix.
846 * Since the parameter will be used in the RHS of multiple assignments,
847 * generate a temporary and copy the paramter there.
849 ir_variable *const rhs_var =
850 new(ctx) ir_variable(first_param->type, "mat_ctor_mat",
851 ir_var_temporary);
852 instructions->push_tail(rhs_var);
854 ir_dereference *const rhs_var_ref =
855 new(ctx) ir_dereference_variable(rhs_var);
856 ir_instruction *const inst =
857 new(ctx) ir_assignment(rhs_var_ref, first_param, NULL);
858 instructions->push_tail(inst);
860 const unsigned last_row = MIN2(src_matrix->type->vector_elements,
861 var->type->vector_elements);
862 const unsigned last_col = MIN2(src_matrix->type->matrix_columns,
863 var->type->matrix_columns);
865 unsigned swiz[4] = { 0, 0, 0, 0 };
866 for (unsigned i = 1; i < last_row; i++)
867 swiz[i] = i;
869 const unsigned write_mask = (1U << last_row) - 1;
871 for (unsigned i = 0; i < last_col; i++) {
872 ir_dereference *const lhs =
873 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(i));
874 ir_rvalue *const rhs_col =
875 new(ctx) ir_dereference_array(rhs_var, new(ctx) ir_constant(i));
877 /* If one matrix has columns that are smaller than the columns of the
878 * other matrix, wrap the column access of the larger with a swizzle
879 * so that the LHS and RHS of the assignment have the same size (and
880 * therefore have the same type).
882 * It would be perfectly valid to unconditionally generate the
883 * swizzles, this this will typically result in a more compact IR tree.
885 ir_rvalue *rhs;
886 if (lhs->type->vector_elements != rhs_col->type->vector_elements) {
887 rhs = new(ctx) ir_swizzle(rhs_col, swiz, last_row);
888 } else {
889 rhs = rhs_col;
892 ir_instruction *inst =
893 new(ctx) ir_assignment(lhs, rhs, NULL, write_mask);
894 instructions->push_tail(inst);
896 } else {
897 const unsigned cols = type->matrix_columns;
898 const unsigned rows = type->vector_elements;
899 unsigned col_idx = 0;
900 unsigned row_idx = 0;
902 foreach_list (node, parameters) {
903 ir_rvalue *const rhs = (ir_rvalue *) node;
904 const unsigned components_remaining_this_column = rows - row_idx;
905 unsigned rhs_components = rhs->type->components();
906 unsigned rhs_base = 0;
908 /* Since the parameter might be used in the RHS of two assignments,
909 * generate a temporary and copy the paramter there.
911 ir_variable *rhs_var =
912 new(ctx) ir_variable(rhs->type, "mat_ctor_vec", ir_var_temporary);
913 instructions->push_tail(rhs_var);
915 ir_dereference *rhs_var_ref =
916 new(ctx) ir_dereference_variable(rhs_var);
917 ir_instruction *inst = new(ctx) ir_assignment(rhs_var_ref, rhs, NULL);
918 instructions->push_tail(inst);
920 /* Assign the current parameter to as many components of the matrix
921 * as it will fill.
923 * NOTE: A single vector parameter can span two matrix columns. A
924 * single vec4, for example, can completely fill a mat2.
926 if (rhs_components >= components_remaining_this_column) {
927 const unsigned count = MIN2(rhs_components,
928 components_remaining_this_column);
930 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
932 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
933 row_idx,
934 rhs_var_ref, 0,
935 count, ctx);
936 instructions->push_tail(inst);
938 rhs_base = count;
940 col_idx++;
941 row_idx = 0;
944 /* If there is data left in the parameter and components left to be
945 * set in the destination, emit another assignment. It is possible
946 * that the assignment could be of a vec4 to the last element of the
947 * matrix. In this case col_idx==cols, but there is still data
948 * left in the source parameter. Obviously, don't emit an assignment
949 * to data outside the destination matrix.
951 if ((col_idx < cols) && (rhs_base < rhs_components)) {
952 const unsigned count = rhs_components - rhs_base;
954 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
956 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
957 row_idx,
958 rhs_var_ref,
959 rhs_base,
960 count, ctx);
961 instructions->push_tail(inst);
963 row_idx += count;
968 return new(ctx) ir_dereference_variable(var);
972 ir_rvalue *
973 emit_inline_record_constructor(const glsl_type *type,
974 exec_list *instructions,
975 exec_list *parameters,
976 void *mem_ctx)
978 ir_variable *const var =
979 new(mem_ctx) ir_variable(type, "record_ctor", ir_var_temporary);
980 ir_dereference_variable *const d = new(mem_ctx) ir_dereference_variable(var);
982 instructions->push_tail(var);
984 exec_node *node = parameters->head;
985 for (unsigned i = 0; i < type->length; i++) {
986 assert(!node->is_tail_sentinel());
988 ir_dereference *const lhs =
989 new(mem_ctx) ir_dereference_record(d->clone(mem_ctx, NULL),
990 type->fields.structure[i].name);
992 ir_rvalue *const rhs = ((ir_instruction *) node)->as_rvalue();
993 assert(rhs != NULL);
995 ir_instruction *const assign = new(mem_ctx) ir_assignment(lhs, rhs, NULL);
997 instructions->push_tail(assign);
998 node = node->next;
1001 return d;
1005 ir_rvalue *
1006 ast_function_expression::hir(exec_list *instructions,
1007 struct _mesa_glsl_parse_state *state)
1009 void *ctx = state;
1010 /* There are three sorts of function calls.
1012 * 1. constructors - The first subexpression is an ast_type_specifier.
1013 * 2. methods - Only the .length() method of array types.
1014 * 3. functions - Calls to regular old functions.
1016 * Method calls are actually detected when the ast_field_selection
1017 * expression is handled.
1019 if (is_constructor()) {
1020 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
1021 YYLTYPE loc = type->get_location();
1022 const char *name;
1024 const glsl_type *const constructor_type = type->glsl_type(& name, state);
1026 /* constructor_type can be NULL if a variable with the same name as the
1027 * structure has come into scope.
1029 if (constructor_type == NULL) {
1030 _mesa_glsl_error(& loc, state, "unknown type `%s' (structure name "
1031 "may be shadowed by a variable with the same name)",
1032 type->type_name);
1033 return ir_call::get_error_instruction(ctx);
1037 /* Constructors for samplers are illegal.
1039 if (constructor_type->is_sampler()) {
1040 _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
1041 constructor_type->name);
1042 return ir_call::get_error_instruction(ctx);
1045 if (constructor_type->is_array()) {
1046 if (state->language_version <= 110) {
1047 _mesa_glsl_error(& loc, state,
1048 "array constructors forbidden in GLSL 1.10");
1049 return ir_call::get_error_instruction(ctx);
1052 return process_array_constructor(instructions, constructor_type,
1053 & loc, &this->expressions, state);
1057 /* There are two kinds of constructor call. Constructors for built-in
1058 * language types, such as mat4 and vec2, are free form. The only
1059 * requirement is that the parameters must provide enough values of the
1060 * correct scalar type. Constructors for arrays and structures must
1061 * have the exact number of parameters with matching types in the
1062 * correct order. These constructors follow essentially the same type
1063 * matching rules as functions.
1065 if (constructor_type->is_record()) {
1066 exec_list actual_parameters;
1068 process_parameters(instructions, &actual_parameters,
1069 &this->expressions, state);
1071 exec_node *node = actual_parameters.head;
1072 for (unsigned i = 0; i < constructor_type->length; i++) {
1073 ir_rvalue *ir = (ir_rvalue *) node;
1075 if (node->is_tail_sentinel()) {
1076 _mesa_glsl_error(&loc, state,
1077 "insufficient parameters to constructor "
1078 "for `%s'",
1079 constructor_type->name);
1080 return ir_call::get_error_instruction(ctx);
1083 if (apply_implicit_conversion(constructor_type->fields.structure[i].type,
1084 ir, state)) {
1085 node->replace_with(ir);
1086 } else {
1087 _mesa_glsl_error(&loc, state,
1088 "parameter type mismatch in constructor "
1089 "for `%s.%s' (%s vs %s)",
1090 constructor_type->name,
1091 constructor_type->fields.structure[i].name,
1092 ir->type->name,
1093 constructor_type->fields.structure[i].type->name);
1094 return ir_call::get_error_instruction(ctx);;
1097 node = node->next;
1100 if (!node->is_tail_sentinel()) {
1101 _mesa_glsl_error(&loc, state, "too many parameters in constructor "
1102 "for `%s'", constructor_type->name);
1103 return ir_call::get_error_instruction(ctx);
1106 ir_rvalue *const constant =
1107 constant_record_constructor(constructor_type, &actual_parameters,
1108 state);
1110 return (constant != NULL)
1111 ? constant
1112 : emit_inline_record_constructor(constructor_type, instructions,
1113 &actual_parameters, state);
1116 if (!constructor_type->is_numeric() && !constructor_type->is_boolean())
1117 return ir_call::get_error_instruction(ctx);
1119 /* Total number of components of the type being constructed. */
1120 const unsigned type_components = constructor_type->components();
1122 /* Number of components from parameters that have actually been
1123 * consumed. This is used to perform several kinds of error checking.
1125 unsigned components_used = 0;
1127 unsigned matrix_parameters = 0;
1128 unsigned nonmatrix_parameters = 0;
1129 exec_list actual_parameters;
1131 foreach_list (n, &this->expressions) {
1132 ast_node *ast = exec_node_data(ast_node, n, link);
1133 ir_rvalue *result = ast->hir(instructions, state)->as_rvalue();
1135 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1137 * "It is an error to provide extra arguments beyond this
1138 * last used argument."
1140 if (components_used >= type_components) {
1141 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
1142 "constructor",
1143 constructor_type->name);
1144 return ir_call::get_error_instruction(ctx);
1147 if (!result->type->is_numeric() && !result->type->is_boolean()) {
1148 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
1149 "non-numeric data type",
1150 constructor_type->name);
1151 return ir_call::get_error_instruction(ctx);
1154 /* Count the number of matrix and nonmatrix parameters. This
1155 * is used below to enforce some of the constructor rules.
1157 if (result->type->is_matrix())
1158 matrix_parameters++;
1159 else
1160 nonmatrix_parameters++;
1162 actual_parameters.push_tail(result);
1163 components_used += result->type->components();
1166 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1168 * "It is an error to construct matrices from other matrices. This
1169 * is reserved for future use."
1171 if (state->language_version == 110 && matrix_parameters > 0
1172 && constructor_type->is_matrix()) {
1173 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
1174 "matrix in GLSL 1.10",
1175 constructor_type->name);
1176 return ir_call::get_error_instruction(ctx);
1179 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1181 * "If a matrix argument is given to a matrix constructor, it is
1182 * an error to have any other arguments."
1184 if ((matrix_parameters > 0)
1185 && ((matrix_parameters + nonmatrix_parameters) > 1)
1186 && constructor_type->is_matrix()) {
1187 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
1188 "matrix must be only parameter",
1189 constructor_type->name);
1190 return ir_call::get_error_instruction(ctx);
1193 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1195 * "In these cases, there must be enough components provided in the
1196 * arguments to provide an initializer for every component in the
1197 * constructed value."
1199 if (components_used < type_components && components_used != 1
1200 && matrix_parameters == 0) {
1201 _mesa_glsl_error(& loc, state, "too few components to construct "
1202 "`%s'",
1203 constructor_type->name);
1204 return ir_call::get_error_instruction(ctx);
1207 /* Later, we cast each parameter to the same base type as the
1208 * constructor. Since there are no non-floating point matrices, we
1209 * need to break them up into a series of column vectors.
1211 if (constructor_type->base_type != GLSL_TYPE_FLOAT) {
1212 foreach_list_safe(n, &actual_parameters) {
1213 ir_rvalue *matrix = (ir_rvalue *) n;
1215 if (!matrix->type->is_matrix())
1216 continue;
1218 /* Create a temporary containing the matrix. */
1219 ir_variable *var = new(ctx) ir_variable(matrix->type, "matrix_tmp",
1220 ir_var_temporary);
1221 instructions->push_tail(var);
1222 instructions->push_tail(new(ctx) ir_assignment(new(ctx)
1223 ir_dereference_variable(var), matrix, NULL));
1224 var->constant_value = matrix->constant_expression_value();
1226 /* Replace the matrix with dereferences of its columns. */
1227 for (int i = 0; i < matrix->type->matrix_columns; i++) {
1228 matrix->insert_before(new (ctx) ir_dereference_array(var,
1229 new(ctx) ir_constant(i)));
1231 matrix->remove();
1235 bool all_parameters_are_constant = true;
1237 /* Type cast each parameter and, if possible, fold constants.*/
1238 foreach_list_safe(n, &actual_parameters) {
1239 ir_rvalue *ir = (ir_rvalue *) n;
1241 const glsl_type *desired_type =
1242 glsl_type::get_instance(constructor_type->base_type,
1243 ir->type->vector_elements,
1244 ir->type->matrix_columns);
1245 ir_rvalue *result = convert_component(ir, desired_type);
1247 /* Attempt to convert the parameter to a constant valued expression.
1248 * After doing so, track whether or not all the parameters to the
1249 * constructor are trivially constant valued expressions.
1251 ir_rvalue *const constant = result->constant_expression_value();
1253 if (constant != NULL)
1254 result = constant;
1255 else
1256 all_parameters_are_constant = false;
1258 if (result != ir) {
1259 ir->replace_with(result);
1263 /* If all of the parameters are trivially constant, create a
1264 * constant representing the complete collection of parameters.
1266 if (all_parameters_are_constant) {
1267 return new(ctx) ir_constant(constructor_type, &actual_parameters);
1268 } else if (constructor_type->is_scalar()) {
1269 return dereference_component((ir_rvalue *) actual_parameters.head,
1271 } else if (constructor_type->is_vector()) {
1272 return emit_inline_vector_constructor(constructor_type,
1273 instructions,
1274 &actual_parameters,
1275 ctx);
1276 } else {
1277 assert(constructor_type->is_matrix());
1278 return emit_inline_matrix_constructor(constructor_type,
1279 instructions,
1280 &actual_parameters,
1281 ctx);
1283 } else {
1284 const ast_expression *id = subexpressions[0];
1285 YYLTYPE loc = id->get_location();
1286 exec_list actual_parameters;
1288 process_parameters(instructions, &actual_parameters, &this->expressions,
1289 state);
1291 return match_function_by_name(instructions,
1292 id->primary_expression.identifier, & loc,
1293 &actual_parameters, state);
1296 return ir_call::get_error_instruction(ctx);