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
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"
26 #include "glsl_types.h"
28 #include "main/core.h" /* for MIN2 */
31 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
);
34 process_parameters(exec_list
*instructions
, exec_list
*actual_parameters
,
35 exec_list
*parameters
,
36 struct _mesa_glsl_parse_state
*state
)
40 foreach_list (n
, parameters
) {
41 ast_node
*const ast
= exec_node_data(ast_node
, n
, link
);
42 ir_rvalue
*result
= ast
->hir(instructions
, state
);
44 ir_constant
*const constant
= result
->constant_expression_value();
48 actual_parameters
->push_tail(result
);
57 * Generate a source prototype for a function signature
59 * \param return_type Return type of the function. May be \c NULL.
60 * \param name Name of the function.
61 * \param parameters Parameter list for the function. This may be either a
62 * formal or actual parameter list. Only the type is used.
65 * A talloced string representing the prototype of the function.
68 prototype_string(const glsl_type
*return_type
, const char *name
,
69 exec_list
*parameters
)
73 if (return_type
!= NULL
)
74 str
= talloc_asprintf(str
, "%s ", return_type
->name
);
76 str
= talloc_asprintf_append(str
, "%s(", name
);
78 const char *comma
= "";
79 foreach_list(node
, parameters
) {
80 const ir_instruction
*const param
= (ir_instruction
*) node
;
82 str
= talloc_asprintf_append(str
, "%s%s", comma
, param
->type
->name
);
86 str
= talloc_strdup_append(str
, ")");
92 process_call(exec_list
*instructions
, ir_function
*f
,
93 YYLTYPE
*loc
, exec_list
*actual_parameters
,
94 struct _mesa_glsl_parse_state
*state
)
98 ir_function_signature
*sig
= f
->matching_signature(actual_parameters
);
100 /* The instructions param will be used when the FINISHMEs below are done */
104 /* Verify that 'out' and 'inout' actual parameters are lvalues. This
105 * isn't done in ir_function::matching_signature because that function
106 * cannot generate the necessary diagnostics.
108 exec_list_iterator actual_iter
= actual_parameters
->iterator();
109 exec_list_iterator formal_iter
= sig
->parameters
.iterator();
111 while (actual_iter
.has_next()) {
112 ir_rvalue
*actual
= (ir_rvalue
*) actual_iter
.get();
113 ir_variable
*formal
= (ir_variable
*) formal_iter
.get();
115 assert(actual
!= NULL
);
116 assert(formal
!= NULL
);
118 if ((formal
->mode
== ir_var_out
)
119 || (formal
->mode
== ir_var_inout
)) {
120 if (! actual
->is_lvalue()) {
121 /* FINISHME: Log a better diagnostic here. There is no way
122 * FINISHME: to tell the user which parameter is invalid.
124 _mesa_glsl_error(loc
, state
, "`%s' parameter is not lvalue",
125 (formal
->mode
== ir_var_out
) ? "out" : "inout");
129 if (formal
->type
->is_numeric() || formal
->type
->is_boolean()) {
130 ir_rvalue
*converted
= convert_component(actual
, formal
->type
);
131 actual
->replace_with(converted
);
138 /* Always insert the call in the instruction stream, and return a deref
139 * of its return val if it returns a value, since we don't know if
140 * the rvalue is going to be assigned to anything or not.
142 ir_call
*call
= new(ctx
) ir_call(sig
, actual_parameters
);
143 if (!sig
->return_type
->is_void()) {
145 ir_dereference_variable
*deref
;
147 var
= new(ctx
) ir_variable(sig
->return_type
,
148 talloc_asprintf(ctx
, "%s_retval",
149 sig
->function_name()),
151 instructions
->push_tail(var
);
153 deref
= new(ctx
) ir_dereference_variable(var
);
154 ir_assignment
*assign
= new(ctx
) ir_assignment(deref
, call
, NULL
);
155 instructions
->push_tail(assign
);
156 if (state
->language_version
>= 120)
157 var
->constant_value
= call
->constant_expression_value();
159 deref
= new(ctx
) ir_dereference_variable(var
);
162 instructions
->push_tail(call
);
166 char *str
= prototype_string(NULL
, f
->name
, actual_parameters
);
168 _mesa_glsl_error(loc
, state
, "no matching function for call to `%s'",
172 const char *prefix
= "candidates are: ";
173 foreach_list (node
, &f
->signatures
) {
174 ir_function_signature
*sig
= (ir_function_signature
*) node
;
176 str
= prototype_string(sig
->return_type
, f
->name
, &sig
->parameters
);
177 _mesa_glsl_error(loc
, state
, "%s%s\n", prefix
, str
);
183 return ir_call::get_error_instruction(ctx
);
189 match_function_by_name(exec_list
*instructions
, const char *name
,
190 YYLTYPE
*loc
, exec_list
*actual_parameters
,
191 struct _mesa_glsl_parse_state
*state
)
194 ir_function
*f
= state
->symbols
->get_function(name
);
197 _mesa_glsl_error(loc
, state
, "function `%s' undeclared", name
);
198 return ir_call::get_error_instruction(ctx
);
201 /* Once we've determined that the function being called might exist, try
202 * to find an overload of the function that matches the parameters.
204 return process_call(instructions
, f
, loc
, actual_parameters
, state
);
209 * Perform automatic type conversion of constructor parameters
211 * This implements the rules in the "Conversion and Scalar Constructors"
212 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
215 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
)
217 void *ctx
= talloc_parent(src
);
218 const unsigned a
= desired_type
->base_type
;
219 const unsigned b
= src
->type
->base_type
;
220 ir_expression
*result
= NULL
;
222 if (src
->type
->is_error())
225 assert(a
<= GLSL_TYPE_BOOL
);
226 assert(b
<= GLSL_TYPE_BOOL
);
228 if ((a
== b
) || (src
->type
->is_integer() && desired_type
->is_integer()))
234 if (b
== GLSL_TYPE_FLOAT
)
235 result
= new(ctx
) ir_expression(ir_unop_f2i
, desired_type
, src
, NULL
);
237 assert(b
== GLSL_TYPE_BOOL
);
238 result
= new(ctx
) ir_expression(ir_unop_b2i
, desired_type
, src
, NULL
);
241 case GLSL_TYPE_FLOAT
:
244 result
= new(ctx
) ir_expression(ir_unop_u2f
, desired_type
, src
, NULL
);
247 result
= new(ctx
) ir_expression(ir_unop_i2f
, desired_type
, src
, NULL
);
250 result
= new(ctx
) ir_expression(ir_unop_b2f
, desired_type
, src
, NULL
);
258 result
= new(ctx
) ir_expression(ir_unop_i2b
, desired_type
, src
, NULL
);
260 case GLSL_TYPE_FLOAT
:
261 result
= new(ctx
) ir_expression(ir_unop_f2b
, desired_type
, src
, NULL
);
267 assert(result
!= NULL
);
269 /* Try constant folding; it may fold in the conversion we just added. */
270 ir_constant
*const constant
= result
->constant_expression_value();
271 return (constant
!= NULL
) ? (ir_rvalue
*) constant
: (ir_rvalue
*) result
;
275 * Dereference a specific component from a scalar, vector, or matrix
278 dereference_component(ir_rvalue
*src
, unsigned component
)
280 void *ctx
= talloc_parent(src
);
281 assert(component
< src
->type
->components());
283 /* If the source is a constant, just create a new constant instead of a
284 * dereference of the existing constant.
286 ir_constant
*constant
= src
->as_constant();
288 return new(ctx
) ir_constant(constant
, component
);
290 if (src
->type
->is_scalar()) {
292 } else if (src
->type
->is_vector()) {
293 return new(ctx
) ir_swizzle(src
, component
, 0, 0, 0, 1);
295 assert(src
->type
->is_matrix());
297 /* Dereference a row of the matrix, then call this function again to get
298 * a specific element from that row.
300 const int c
= component
/ src
->type
->column_type()->vector_elements
;
301 const int r
= component
% src
->type
->column_type()->vector_elements
;
302 ir_constant
*const col_index
= new(ctx
) ir_constant(c
);
303 ir_dereference
*const col
= new(ctx
) ir_dereference_array(src
, col_index
);
305 col
->type
= src
->type
->column_type();
307 return dereference_component(col
, r
);
310 assert(!"Should not get here.");
316 process_array_constructor(exec_list
*instructions
,
317 const glsl_type
*constructor_type
,
318 YYLTYPE
*loc
, exec_list
*parameters
,
319 struct _mesa_glsl_parse_state
*state
)
322 /* Array constructors come in two forms: sized and unsized. Sized array
323 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
324 * variables. In this case the number of parameters must exactly match the
325 * specified size of the array.
327 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
328 * are vec4 variables. In this case the size of the array being constructed
329 * is determined by the number of parameters.
331 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
333 * "There must be exactly the same number of arguments as the size of
334 * the array being constructed. If no size is present in the
335 * constructor, then the array is explicitly sized to the number of
336 * arguments provided. The arguments are assigned in order, starting at
337 * element 0, to the elements of the constructed array. Each argument
338 * must be the same type as the element type of the array, or be a type
339 * that can be converted to the element type of the array according to
340 * Section 4.1.10 "Implicit Conversions.""
342 exec_list actual_parameters
;
343 const unsigned parameter_count
=
344 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
346 if ((parameter_count
== 0)
347 || ((constructor_type
->length
!= 0)
348 && (constructor_type
->length
!= parameter_count
))) {
349 const unsigned min_param
= (constructor_type
->length
== 0)
350 ? 1 : constructor_type
->length
;
352 _mesa_glsl_error(loc
, state
, "array constructor must have %s %u "
354 (constructor_type
->length
!= 0) ? "at least" : "exactly",
355 min_param
, (min_param
<= 1) ? "" : "s");
356 return ir_call::get_error_instruction(ctx
);
359 if (constructor_type
->length
== 0) {
361 glsl_type::get_array_instance(constructor_type
->element_type(),
363 assert(constructor_type
!= NULL
);
364 assert(constructor_type
->length
== parameter_count
);
367 bool all_parameters_are_constant
= true;
369 /* Type cast each parameter and, if possible, fold constants. */
370 foreach_list_safe(n
, &actual_parameters
) {
371 ir_rvalue
*ir
= (ir_rvalue
*) n
;
372 ir_rvalue
*result
= ir
;
374 /* Apply implicit conversions (not the scalar constructor rules!) */
375 if (constructor_type
->element_type()->is_float()) {
376 const glsl_type
*desired_type
=
377 glsl_type::get_instance(GLSL_TYPE_FLOAT
,
378 ir
->type
->vector_elements
,
379 ir
->type
->matrix_columns
);
380 result
= convert_component(ir
, desired_type
);
383 if (result
->type
!= constructor_type
->element_type()) {
384 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
385 "expected: %s, found %s",
386 constructor_type
->element_type()->name
,
390 /* Attempt to convert the parameter to a constant valued expression.
391 * After doing so, track whether or not all the parameters to the
392 * constructor are trivially constant valued expressions.
394 ir_rvalue
*const constant
= result
->constant_expression_value();
396 if (constant
!= NULL
)
399 all_parameters_are_constant
= false;
401 ir
->replace_with(result
);
404 if (all_parameters_are_constant
)
405 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
407 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "array_ctor",
409 instructions
->push_tail(var
);
412 foreach_list(node
, &actual_parameters
) {
413 ir_rvalue
*rhs
= (ir_rvalue
*) node
;
414 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
415 new(ctx
) ir_constant(i
));
417 ir_instruction
*assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
418 instructions
->push_tail(assignment
);
423 return new(ctx
) ir_dereference_variable(var
);
428 * Try to convert a record constructor to a constant expression
431 constant_record_constructor(const glsl_type
*constructor_type
,
432 exec_list
*parameters
, void *mem_ctx
)
434 foreach_list(node
, parameters
) {
435 if (((ir_instruction
*) node
)->as_constant() == NULL
)
439 return new(mem_ctx
) ir_constant(constructor_type
, parameters
);
444 * Determine if a list consists of a single scalar r-value
447 single_scalar_parameter(exec_list
*parameters
)
449 const ir_rvalue
*const p
= (ir_rvalue
*) parameters
->head
;
450 assert(((ir_rvalue
*)p
)->as_rvalue() != NULL
);
452 return (p
->type
->is_scalar() && p
->next
->is_tail_sentinel());
457 * Generate inline code for a vector constructor
459 * The generated constructor code will consist of a temporary variable
460 * declaration of the same type as the constructor. A sequence of assignments
461 * from constructor parameters to the temporary will follow.
464 * An \c ir_dereference_variable of the temprorary generated in the constructor
468 emit_inline_vector_constructor(const glsl_type
*type
,
469 exec_list
*instructions
,
470 exec_list
*parameters
,
473 assert(!parameters
->is_empty());
475 ir_variable
*var
= new(ctx
) ir_variable(type
, "vec_ctor", ir_var_temporary
);
476 instructions
->push_tail(var
);
478 /* There are two kinds of vector constructors.
480 * - Construct a vector from a single scalar by replicating that scalar to
481 * all components of the vector.
483 * - Construct a vector from an arbirary combination of vectors and
484 * scalars. The components of the constructor parameters are assigned
485 * to the vector in order until the vector is full.
487 const unsigned lhs_components
= type
->components();
488 if (single_scalar_parameter(parameters
)) {
489 ir_rvalue
*first_param
= (ir_rvalue
*)parameters
->head
;
490 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(first_param
, 0, 0, 0, 0,
492 ir_dereference_variable
*lhs
= new(ctx
) ir_dereference_variable(var
);
493 const unsigned mask
= (1U << lhs_components
) - 1;
495 assert(rhs
->type
== lhs
->type
);
497 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
, mask
);
498 instructions
->push_tail(inst
);
500 unsigned base_component
= 0;
501 ir_constant_data data
;
502 unsigned constant_mask
= 0;
504 memset(&data
, 0, sizeof(data
));
506 foreach_list(node
, parameters
) {
507 ir_rvalue
*param
= (ir_rvalue
*) node
;
508 unsigned rhs_components
= param
->type
->components();
510 /* Do not try to assign more components to the vector than it has!
512 if ((rhs_components
+ base_component
) > lhs_components
) {
513 rhs_components
= lhs_components
- base_component
;
516 const ir_constant
*const c
= param
->as_constant();
518 for (unsigned i
= 0; i
< rhs_components
; i
++) {
519 switch (c
->type
->base_type
) {
521 data
.u
[i
+ base_component
] = c
->get_uint_component(i
);
524 data
.i
[i
+ base_component
] = c
->get_int_component(i
);
526 case GLSL_TYPE_FLOAT
:
527 data
.f
[i
+ base_component
] = c
->get_float_component(i
);
530 data
.b
[i
+ base_component
] = c
->get_bool_component(i
);
533 assert(!"Should not get here.");
538 /* Mask of fields to be written in the assignment.
540 constant_mask
|= ((1U << rhs_components
) - 1) << base_component
;
543 /* Advance the component index by the number of components that were
546 base_component
+= rhs_components
;
549 if (constant_mask
!= 0) {
550 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
551 ir_rvalue
*rhs
= new(ctx
) ir_constant(var
->type
, &data
);
553 ir_instruction
*inst
=
554 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, constant_mask
);
555 instructions
->push_tail(inst
);
559 foreach_list(node
, parameters
) {
560 ir_rvalue
*param
= (ir_rvalue
*) node
;
561 unsigned rhs_components
= param
->type
->components();
563 /* Do not try to assign more components to the vector than it has!
565 if ((rhs_components
+ base_component
) > lhs_components
) {
566 rhs_components
= lhs_components
- base_component
;
569 const ir_constant
*const c
= param
->as_constant();
571 /* Generate a swizzle that puts the first element of the source at
572 * the location of the first element of the destination.
574 unsigned swiz
[4] = { 0, 0, 0, 0 };
575 for (unsigned i
= 0; i
< rhs_components
; i
++)
576 swiz
[i
+ base_component
] = i
;
578 /* Mask of fields to be written in the assignment.
580 const unsigned write_mask
= ((1U << rhs_components
) - 1)
583 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
584 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(param
, swiz
, lhs_components
);
586 ir_instruction
*inst
=
587 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
588 instructions
->push_tail(inst
);
591 /* Advance the component index by the number of components that were
594 base_component
+= rhs_components
;
597 return new(ctx
) ir_dereference_variable(var
);
602 * Generate assignment of a portion of a vector to a portion of a matrix column
604 * \param src_base First component of the source to be used in assignment
605 * \param column Column of destination to be assiged
606 * \param row_base First component of the destination column to be assigned
607 * \param count Number of components to be assigned
610 * \c src_base + \c count must be less than or equal to the number of components
611 * in the source vector.
614 assign_to_matrix_column(ir_variable
*var
, unsigned column
, unsigned row_base
,
615 ir_rvalue
*src
, unsigned src_base
, unsigned count
,
618 ir_constant
*col_idx
= new(mem_ctx
) ir_constant(column
);
619 ir_dereference
*column_ref
= new(mem_ctx
) ir_dereference_array(var
, col_idx
);
621 assert(column_ref
->type
->components() >= (row_base
+ count
));
622 assert(src
->type
->components() >= (src_base
+ count
));
624 /* Generate a swizzle that puts the first element of the source at the
625 * location of the first element of the destination.
627 unsigned swiz
[4] = { src_base
, src_base
, src_base
, src_base
};
628 for (unsigned i
= 0; i
< count
; i
++)
629 swiz
[i
+ row_base
] = src_base
+ i
;
631 ir_rvalue
*const rhs
=
632 new(mem_ctx
) ir_swizzle(src
, swiz
, column_ref
->type
->components());
634 /* Mask of fields to be written in the assignment.
636 const unsigned write_mask
= ((1U << count
) - 1) << row_base
;
638 return new(mem_ctx
) ir_assignment(column_ref
, rhs
, NULL
, write_mask
);
643 * Generate inline code for a matrix constructor
645 * The generated constructor code will consist of a temporary variable
646 * declaration of the same type as the constructor. A sequence of assignments
647 * from constructor parameters to the temporary will follow.
650 * An \c ir_dereference_variable of the temprorary generated in the constructor
654 emit_inline_matrix_constructor(const glsl_type
*type
,
655 exec_list
*instructions
,
656 exec_list
*parameters
,
659 assert(!parameters
->is_empty());
661 ir_variable
*var
= new(ctx
) ir_variable(type
, "mat_ctor", ir_var_temporary
);
662 instructions
->push_tail(var
);
664 /* There are three kinds of matrix constructors.
666 * - Construct a matrix from a single scalar by replicating that scalar to
667 * along the diagonal of the matrix and setting all other components to
670 * - Construct a matrix from an arbirary combination of vectors and
671 * scalars. The components of the constructor parameters are assigned
672 * to the matrix in colum-major order until the matrix is full.
674 * - Construct a matrix from a single matrix. The source matrix is copied
675 * to the upper left portion of the constructed matrix, and the remaining
676 * elements take values from the identity matrix.
678 ir_rvalue
*const first_param
= (ir_rvalue
*) parameters
->head
;
679 if (single_scalar_parameter(parameters
)) {
680 /* Assign the scalar to the X component of a vec4, and fill the remaining
681 * components with zero.
683 ir_variable
*rhs_var
=
684 new(ctx
) ir_variable(glsl_type::vec4_type
, "mat_ctor_vec",
686 instructions
->push_tail(rhs_var
);
688 ir_constant_data zero
;
694 ir_instruction
*inst
=
695 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(rhs_var
),
696 new(ctx
) ir_constant(rhs_var
->type
, &zero
),
698 instructions
->push_tail(inst
);
700 ir_dereference
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
702 inst
= new(ctx
) ir_assignment(rhs_ref
, first_param
, NULL
, 0x01);
703 instructions
->push_tail(inst
);
705 /* Assign the temporary vector to each column of the destination matrix
706 * with a swizzle that puts the X component on the diagonal of the
707 * matrix. In some cases this may mean that the X component does not
708 * get assigned into the column at all (i.e., when the matrix has more
709 * columns than rows).
711 static const unsigned rhs_swiz
[4][4] = {
718 const unsigned cols_to_init
= MIN2(type
->matrix_columns
,
719 type
->vector_elements
);
720 for (unsigned i
= 0; i
< cols_to_init
; i
++) {
721 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
722 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
724 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
725 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, rhs_swiz
[i
],
726 type
->vector_elements
);
728 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
729 instructions
->push_tail(inst
);
732 for (unsigned i
= cols_to_init
; i
< type
->matrix_columns
; i
++) {
733 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
734 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
736 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
737 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, 1, 1, 1, 1,
738 type
->vector_elements
);
740 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
741 instructions
->push_tail(inst
);
743 } else if (first_param
->type
->is_matrix()) {
744 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
746 * "If a matrix is constructed from a matrix, then each component
747 * (column i, row j) in the result that has a corresponding
748 * component (column i, row j) in the argument will be initialized
749 * from there. All other components will be initialized to the
750 * identity matrix. If a matrix argument is given to a matrix
751 * constructor, it is an error to have any other arguments."
753 assert(first_param
->next
->is_tail_sentinel());
754 ir_rvalue
*const src_matrix
= first_param
;
756 /* If the source matrix is smaller, pre-initialize the relavent parts of
757 * the destination matrix to the identity matrix.
759 if ((src_matrix
->type
->matrix_columns
< var
->type
->matrix_columns
)
760 || (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)) {
762 /* If the source matrix has fewer rows, every column of the destination
763 * must be initialized. Otherwise only the columns in the destination
764 * that do not exist in the source must be initialized.
767 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)
768 ? 0 : src_matrix
->type
->matrix_columns
;
770 const glsl_type
*const col_type
= var
->type
->column_type();
771 for (/* empty */; col
< var
->type
->matrix_columns
; col
++) {
772 ir_constant_data ident
;
781 ir_rvalue
*const rhs
= new(ctx
) ir_constant(col_type
, &ident
);
783 ir_rvalue
*const lhs
=
784 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(col
));
786 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
787 instructions
->push_tail(inst
);
791 /* Assign columns from the source matrix to the destination matrix.
793 * Since the parameter will be used in the RHS of multiple assignments,
794 * generate a temporary and copy the paramter there.
796 ir_variable
*const rhs_var
=
797 new(ctx
) ir_variable(first_param
->type
, "mat_ctor_mat",
799 instructions
->push_tail(rhs_var
);
801 ir_dereference
*const rhs_var_ref
=
802 new(ctx
) ir_dereference_variable(rhs_var
);
803 ir_instruction
*const inst
=
804 new(ctx
) ir_assignment(rhs_var_ref
, first_param
, NULL
);
805 instructions
->push_tail(inst
);
807 const unsigned last_row
= MIN2(src_matrix
->type
->vector_elements
,
808 var
->type
->vector_elements
);
809 const unsigned last_col
= MIN2(src_matrix
->type
->matrix_columns
,
810 var
->type
->matrix_columns
);
812 unsigned swiz
[4] = { 0, 0, 0, 0 };
813 for (unsigned i
= 1; i
< src_matrix
->type
->vector_elements
; i
++)
816 const unsigned write_mask
= (1U << last_row
) - 1;
818 for (unsigned i
= 0; i
< last_col
; i
++) {
819 ir_dereference
*const lhs
=
820 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
821 ir_rvalue
*const rhs_col
=
822 new(ctx
) ir_dereference_array(rhs_var
, new(ctx
) ir_constant(i
));
824 /* If one matrix has columns that are smaller than the columns of the
825 * other matrix, wrap the column access of the larger with a swizzle
826 * so that the LHS and RHS of the assignment have the same size (and
827 * therefore have the same type).
829 * It would be perfectly valid to unconditionally generate the
830 * swizzles, this this will typically result in a more compact IR tree.
833 if (lhs
->type
->vector_elements
!= rhs_col
->type
->vector_elements
) {
834 rhs
= new(ctx
) ir_swizzle(rhs_col
, swiz
,
835 lhs
->type
->vector_elements
);
840 assert(lhs
->type
== rhs
->type
);
842 ir_instruction
*inst
=
843 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
844 instructions
->push_tail(inst
);
847 const unsigned cols
= type
->matrix_columns
;
848 const unsigned rows
= type
->vector_elements
;
849 unsigned col_idx
= 0;
850 unsigned row_idx
= 0;
852 foreach_list (node
, parameters
) {
853 ir_rvalue
*const rhs
= (ir_rvalue
*) node
;
854 const unsigned components_remaining_this_column
= rows
- row_idx
;
855 unsigned rhs_components
= rhs
->type
->components();
856 unsigned rhs_base
= 0;
858 /* Since the parameter might be used in the RHS of two assignments,
859 * generate a temporary and copy the paramter there.
861 ir_variable
*rhs_var
=
862 new(ctx
) ir_variable(rhs
->type
, "mat_ctor_vec", ir_var_temporary
);
863 instructions
->push_tail(rhs_var
);
865 ir_dereference
*rhs_var_ref
=
866 new(ctx
) ir_dereference_variable(rhs_var
);
867 ir_instruction
*inst
= new(ctx
) ir_assignment(rhs_var_ref
, rhs
, NULL
);
868 instructions
->push_tail(inst
);
870 /* Assign the current parameter to as many components of the matrix
873 * NOTE: A single vector parameter can span two matrix columns. A
874 * single vec4, for example, can completely fill a mat2.
876 if (rhs_components
>= components_remaining_this_column
) {
877 const unsigned count
= MIN2(rhs_components
,
878 components_remaining_this_column
);
880 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
882 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
886 instructions
->push_tail(inst
);
894 /* If there is data left in the parameter and components left to be
895 * set in the destination, emit another assignment. It is possible
896 * that the assignment could be of a vec4 to the last element of the
897 * matrix. In this case col_idx==cols, but there is still data
898 * left in the source parameter. Obviously, don't emit an assignment
899 * to data outside the destination matrix.
901 if ((col_idx
< cols
) && (rhs_base
< rhs_components
)) {
902 const unsigned count
= rhs_components
- rhs_base
;
904 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
906 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
911 instructions
->push_tail(inst
);
918 return new(ctx
) ir_dereference_variable(var
);
923 emit_inline_record_constructor(const glsl_type
*type
,
924 exec_list
*instructions
,
925 exec_list
*parameters
,
928 ir_variable
*const var
=
929 new(mem_ctx
) ir_variable(type
, "record_ctor", ir_var_temporary
);
930 ir_dereference_variable
*const d
= new(mem_ctx
) ir_dereference_variable(var
);
932 instructions
->push_tail(var
);
934 exec_node
*node
= parameters
->head
;
935 for (unsigned i
= 0; i
< type
->length
; i
++) {
936 assert(!node
->is_tail_sentinel());
938 ir_dereference
*const lhs
=
939 new(mem_ctx
) ir_dereference_record(d
->clone(mem_ctx
, NULL
),
940 type
->fields
.structure
[i
].name
);
942 ir_rvalue
*const rhs
= ((ir_instruction
*) node
)->as_rvalue();
945 ir_instruction
*const assign
= new(mem_ctx
) ir_assignment(lhs
, rhs
, NULL
);
947 instructions
->push_tail(assign
);
956 ast_function_expression::hir(exec_list
*instructions
,
957 struct _mesa_glsl_parse_state
*state
)
960 /* There are three sorts of function calls.
962 * 1. constructors - The first subexpression is an ast_type_specifier.
963 * 2. methods - Only the .length() method of array types.
964 * 3. functions - Calls to regular old functions.
966 * Method calls are actually detected when the ast_field_selection
967 * expression is handled.
969 if (is_constructor()) {
970 const ast_type_specifier
*type
= (ast_type_specifier
*) subexpressions
[0];
971 YYLTYPE loc
= type
->get_location();
974 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
977 /* Constructors for samplers are illegal.
979 if (constructor_type
->is_sampler()) {
980 _mesa_glsl_error(& loc
, state
, "cannot construct sampler type `%s'",
981 constructor_type
->name
);
982 return ir_call::get_error_instruction(ctx
);
985 if (constructor_type
->is_array()) {
986 if (state
->language_version
<= 110) {
987 _mesa_glsl_error(& loc
, state
,
988 "array constructors forbidden in GLSL 1.10");
989 return ir_call::get_error_instruction(ctx
);
992 return process_array_constructor(instructions
, constructor_type
,
993 & loc
, &this->expressions
, state
);
997 /* There are two kinds of constructor call. Constructors for built-in
998 * language types, such as mat4 and vec2, are free form. The only
999 * requirement is that the parameters must provide enough values of the
1000 * correct scalar type. Constructors for arrays and structures must
1001 * have the exact number of parameters with matching types in the
1002 * correct order. These constructors follow essentially the same type
1003 * matching rules as functions.
1005 if (!constructor_type
->is_numeric() && !constructor_type
->is_boolean())
1006 return ir_call::get_error_instruction(ctx
);
1008 /* Total number of components of the type being constructed. */
1009 const unsigned type_components
= constructor_type
->components();
1011 /* Number of components from parameters that have actually been
1012 * consumed. This is used to perform several kinds of error checking.
1014 unsigned components_used
= 0;
1016 unsigned matrix_parameters
= 0;
1017 unsigned nonmatrix_parameters
= 0;
1018 exec_list actual_parameters
;
1020 foreach_list (n
, &this->expressions
) {
1021 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
1022 ir_rvalue
*result
= ast
->hir(instructions
, state
)->as_rvalue();
1024 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1026 * "It is an error to provide extra arguments beyond this
1027 * last used argument."
1029 if (components_used
>= type_components
) {
1030 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
1032 constructor_type
->name
);
1033 return ir_call::get_error_instruction(ctx
);
1036 if (!result
->type
->is_numeric() && !result
->type
->is_boolean()) {
1037 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
1038 "non-numeric data type",
1039 constructor_type
->name
);
1040 return ir_call::get_error_instruction(ctx
);
1043 /* Count the number of matrix and nonmatrix parameters. This
1044 * is used below to enforce some of the constructor rules.
1046 if (result
->type
->is_matrix())
1047 matrix_parameters
++;
1049 nonmatrix_parameters
++;
1051 actual_parameters
.push_tail(result
);
1052 components_used
+= result
->type
->components();
1055 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1057 * "It is an error to construct matrices from other matrices. This
1058 * is reserved for future use."
1060 if ((state
->language_version
<= 110) && (matrix_parameters
> 0)
1061 && constructor_type
->is_matrix()) {
1062 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
1063 "matrix in GLSL 1.10",
1064 constructor_type
->name
);
1065 return ir_call::get_error_instruction(ctx
);
1068 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1070 * "If a matrix argument is given to a matrix constructor, it is
1071 * an error to have any other arguments."
1073 if ((matrix_parameters
> 0)
1074 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
1075 && constructor_type
->is_matrix()) {
1076 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
1077 "matrix must be only parameter",
1078 constructor_type
->name
);
1079 return ir_call::get_error_instruction(ctx
);
1082 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1084 * "In these cases, there must be enough components provided in the
1085 * arguments to provide an initializer for every component in the
1086 * constructed value."
1088 if (components_used
< type_components
&& components_used
!= 1
1089 && matrix_parameters
== 0) {
1090 _mesa_glsl_error(& loc
, state
, "too few components to construct "
1092 constructor_type
->name
);
1093 return ir_call::get_error_instruction(ctx
);
1096 /* Later, we cast each parameter to the same base type as the
1097 * constructor. Since there are no non-floating point matrices, we
1098 * need to break them up into a series of column vectors.
1100 if (constructor_type
->base_type
!= GLSL_TYPE_FLOAT
) {
1101 foreach_list_safe(n
, &actual_parameters
) {
1102 ir_rvalue
*matrix
= (ir_rvalue
*) n
;
1104 if (!matrix
->type
->is_matrix())
1107 /* Create a temporary containing the matrix. */
1108 ir_variable
*var
= new(ctx
) ir_variable(matrix
->type
, "matrix_tmp",
1110 instructions
->push_tail(var
);
1111 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
)
1112 ir_dereference_variable(var
), matrix
, NULL
));
1113 var
->constant_value
= matrix
->constant_expression_value();
1115 /* Replace the matrix with dereferences of its columns. */
1116 for (int i
= 0; i
< matrix
->type
->matrix_columns
; i
++) {
1117 matrix
->insert_before(new (ctx
) ir_dereference_array(var
,
1118 new(ctx
) ir_constant(i
)));
1124 bool all_parameters_are_constant
= true;
1126 /* Type cast each parameter and, if possible, fold constants.*/
1127 foreach_list_safe(n
, &actual_parameters
) {
1128 ir_rvalue
*ir
= (ir_rvalue
*) n
;
1130 const glsl_type
*desired_type
=
1131 glsl_type::get_instance(constructor_type
->base_type
,
1132 ir
->type
->vector_elements
,
1133 ir
->type
->matrix_columns
);
1134 ir_rvalue
*result
= convert_component(ir
, desired_type
);
1136 /* Attempt to convert the parameter to a constant valued expression.
1137 * After doing so, track whether or not all the parameters to the
1138 * constructor are trivially constant valued expressions.
1140 ir_rvalue
*const constant
= result
->constant_expression_value();
1142 if (constant
!= NULL
)
1145 all_parameters_are_constant
= false;
1148 ir
->replace_with(result
);
1152 /* If all of the parameters are trivially constant, create a
1153 * constant representing the complete collection of parameters.
1155 if (all_parameters_are_constant
) {
1156 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1157 } else if (constructor_type
->is_scalar()) {
1158 return dereference_component((ir_rvalue
*) actual_parameters
.head
,
1160 } else if (constructor_type
->is_vector()) {
1161 return emit_inline_vector_constructor(constructor_type
,
1166 assert(constructor_type
->is_matrix());
1167 return emit_inline_matrix_constructor(constructor_type
,
1173 const ast_expression
*id
= subexpressions
[0];
1174 YYLTYPE loc
= id
->get_location();
1175 exec_list actual_parameters
;
1177 process_parameters(instructions
, &actual_parameters
, &this->expressions
,
1180 const glsl_type
*const type
=
1181 state
->symbols
->get_type(id
->primary_expression
.identifier
);
1183 if ((type
!= NULL
) && type
->is_record()) {
1184 exec_node
*node
= actual_parameters
.head
;
1185 for (unsigned i
= 0; i
< type
->length
; i
++) {
1186 ir_instruction
*ir
= (ir_instruction
*) node
;
1188 if (node
->is_tail_sentinel()) {
1189 _mesa_glsl_error(&loc
, state
,
1190 "insufficient parameters to constructor "
1193 return ir_call::get_error_instruction(ctx
);
1196 if (ir
->type
!= type
->fields
.structure
[i
].type
) {
1197 _mesa_glsl_error(&loc
, state
,
1198 "parameter type mismatch in constructor "
1199 "for `%s.%s' (%s vs %s)",
1201 type
->fields
.structure
[i
].name
,
1203 type
->fields
.structure
[i
].type
->name
);
1204 return ir_call::get_error_instruction(ctx
);;
1210 ir_rvalue
*const constant
=
1211 constant_record_constructor(type
, &actual_parameters
, state
);
1213 return (constant
!= NULL
)
1215 : emit_inline_record_constructor(type
, instructions
,
1216 &actual_parameters
, state
);
1219 return match_function_by_name(instructions
,
1220 id
->primary_expression
.identifier
, & loc
,
1221 &actual_parameters
, state
);
1224 return ir_call::get_error_instruction(ctx
);