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"
29 inline unsigned min(unsigned a
, unsigned b
)
31 return (a
< b
) ? a
: b
;
35 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
);
38 process_parameters(exec_list
*instructions
, exec_list
*actual_parameters
,
39 exec_list
*parameters
,
40 struct _mesa_glsl_parse_state
*state
)
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();
52 actual_parameters
->push_tail(result
);
61 process_call(exec_list
*instructions
, ir_function
*f
,
62 YYLTYPE
*loc
, exec_list
*actual_parameters
,
63 struct _mesa_glsl_parse_state
*state
)
67 ir_function_signature
*sig
= f
->matching_signature(actual_parameters
);
69 /* The instructions param will be used when the FINISHMEs below are done */
73 /* Verify that 'out' and 'inout' actual parameters are lvalues. This
74 * isn't done in ir_function::matching_signature because that function
75 * cannot generate the necessary diagnostics.
77 exec_list_iterator actual_iter
= actual_parameters
->iterator();
78 exec_list_iterator formal_iter
= sig
->parameters
.iterator();
80 while (actual_iter
.has_next()) {
81 ir_rvalue
*actual
= (ir_rvalue
*) actual_iter
.get();
82 ir_variable
*formal
= (ir_variable
*) formal_iter
.get();
84 assert(actual
!= NULL
);
85 assert(formal
!= NULL
);
87 if ((formal
->mode
== ir_var_out
)
88 || (formal
->mode
== ir_var_inout
)) {
89 if (! actual
->is_lvalue()) {
90 /* FINISHME: Log a better diagnostic here. There is no way
91 * FINISHME: to tell the user which parameter is invalid.
93 _mesa_glsl_error(loc
, state
, "`%s' parameter is not lvalue",
94 (formal
->mode
== ir_var_out
) ? "out" : "inout");
98 if (formal
->type
->is_numeric() || formal
->type
->is_boolean()) {
99 ir_rvalue
*converted
= convert_component(actual
, formal
->type
);
100 actual
->replace_with(converted
);
107 /* Always insert the call in the instruction stream, and return a deref
108 * of its return val if it returns a value, since we don't know if
109 * the rvalue is going to be assigned to anything or not.
111 ir_call
*call
= new(ctx
) ir_call(sig
, actual_parameters
);
112 if (!sig
->return_type
->is_void()) {
114 ir_dereference_variable
*deref
;
116 var
= new(ctx
) ir_variable(sig
->return_type
,
117 talloc_asprintf(ctx
, "%s_retval",
118 sig
->function_name()),
120 instructions
->push_tail(var
);
122 deref
= new(ctx
) ir_dereference_variable(var
);
123 ir_assignment
*assign
= new(ctx
) ir_assignment(deref
, call
, NULL
);
124 instructions
->push_tail(assign
);
126 deref
= new(ctx
) ir_dereference_variable(var
);
129 instructions
->push_tail(call
);
133 /* FINISHME: Log a better error message here. G++ will show the types
134 * FINISHME: of the actual parameters and the set of candidate
135 * FINISHME: functions. A different error should also be logged when
136 * FINISHME: multiple functions match.
138 _mesa_glsl_error(loc
, state
, "no matching function for call to `%s'",
140 return ir_call::get_error_instruction(ctx
);
146 match_function_by_name(exec_list
*instructions
, const char *name
,
147 YYLTYPE
*loc
, exec_list
*actual_parameters
,
148 struct _mesa_glsl_parse_state
*state
)
151 ir_function
*f
= state
->symbols
->get_function(name
);
154 _mesa_glsl_error(loc
, state
, "function `%s' undeclared", name
);
155 return ir_call::get_error_instruction(ctx
);
158 /* Once we've determined that the function being called might exist, try
159 * to find an overload of the function that matches the parameters.
161 return process_call(instructions
, f
, loc
, actual_parameters
, state
);
166 * Perform automatic type conversion of constructor parameters
168 * This implements the rules in the "Conversion and Scalar Constructors"
169 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
172 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
)
174 void *ctx
= talloc_parent(src
);
175 const unsigned a
= desired_type
->base_type
;
176 const unsigned b
= src
->type
->base_type
;
177 ir_expression
*result
= NULL
;
179 if (src
->type
->is_error())
182 assert(a
<= GLSL_TYPE_BOOL
);
183 assert(b
<= GLSL_TYPE_BOOL
);
185 if ((a
== b
) || (src
->type
->is_integer() && desired_type
->is_integer()))
191 if (b
== GLSL_TYPE_FLOAT
)
192 result
= new(ctx
) ir_expression(ir_unop_f2i
, desired_type
, src
, NULL
);
194 assert(b
== GLSL_TYPE_BOOL
);
195 result
= new(ctx
) ir_expression(ir_unop_b2i
, desired_type
, src
, NULL
);
198 case GLSL_TYPE_FLOAT
:
201 result
= new(ctx
) ir_expression(ir_unop_u2f
, desired_type
, src
, NULL
);
204 result
= new(ctx
) ir_expression(ir_unop_i2f
, desired_type
, src
, NULL
);
207 result
= new(ctx
) ir_expression(ir_unop_b2f
, desired_type
, src
, NULL
);
215 result
= new(ctx
) ir_expression(ir_unop_i2b
, desired_type
, src
, NULL
);
217 case GLSL_TYPE_FLOAT
:
218 result
= new(ctx
) ir_expression(ir_unop_f2b
, desired_type
, src
, NULL
);
224 assert(result
!= NULL
);
226 /* Try constant folding; it may fold in the conversion we just added. */
227 ir_constant
*const constant
= result
->constant_expression_value();
228 return (constant
!= NULL
) ? (ir_rvalue
*) constant
: (ir_rvalue
*) result
;
232 * Dereference a specific component from a scalar, vector, or matrix
235 dereference_component(ir_rvalue
*src
, unsigned component
)
237 void *ctx
= talloc_parent(src
);
238 assert(component
< src
->type
->components());
240 /* If the source is a constant, just create a new constant instead of a
241 * dereference of the existing constant.
243 ir_constant
*constant
= src
->as_constant();
245 return new(ctx
) ir_constant(constant
, component
);
247 if (src
->type
->is_scalar()) {
249 } else if (src
->type
->is_vector()) {
250 return new(ctx
) ir_swizzle(src
, component
, 0, 0, 0, 1);
252 assert(src
->type
->is_matrix());
254 /* Dereference a row of the matrix, then call this function again to get
255 * a specific element from that row.
257 const int c
= component
/ src
->type
->column_type()->vector_elements
;
258 const int r
= component
% src
->type
->column_type()->vector_elements
;
259 ir_constant
*const col_index
= new(ctx
) ir_constant(c
);
260 ir_dereference
*const col
= new(ctx
) ir_dereference_array(src
, col_index
);
262 col
->type
= src
->type
->column_type();
264 return dereference_component(col
, r
);
267 assert(!"Should not get here.");
273 process_array_constructor(exec_list
*instructions
,
274 const glsl_type
*constructor_type
,
275 YYLTYPE
*loc
, exec_list
*parameters
,
276 struct _mesa_glsl_parse_state
*state
)
279 /* Array constructors come in two forms: sized and unsized. Sized array
280 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
281 * variables. In this case the number of parameters must exactly match the
282 * specified size of the array.
284 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
285 * are vec4 variables. In this case the size of the array being constructed
286 * is determined by the number of parameters.
288 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
290 * "There must be exactly the same number of arguments as the size of
291 * the array being constructed. If no size is present in the
292 * constructor, then the array is explicitly sized to the number of
293 * arguments provided. The arguments are assigned in order, starting at
294 * element 0, to the elements of the constructed array. Each argument
295 * must be the same type as the element type of the array, or be a type
296 * that can be converted to the element type of the array according to
297 * Section 4.1.10 "Implicit Conversions.""
299 exec_list actual_parameters
;
300 const unsigned parameter_count
=
301 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
303 if ((parameter_count
== 0)
304 || ((constructor_type
->length
!= 0)
305 && (constructor_type
->length
!= parameter_count
))) {
306 const unsigned min_param
= (constructor_type
->length
== 0)
307 ? 1 : constructor_type
->length
;
309 _mesa_glsl_error(loc
, state
, "array constructor must have %s %u "
311 (constructor_type
->length
!= 0) ? "at least" : "exactly",
312 min_param
, (min_param
<= 1) ? "" : "s");
313 return ir_call::get_error_instruction(ctx
);
316 if (constructor_type
->length
== 0) {
318 glsl_type::get_array_instance(constructor_type
->element_type(),
320 assert(constructor_type
!= NULL
);
321 assert(constructor_type
->length
== parameter_count
);
324 bool all_parameters_are_constant
= true;
326 /* Type cast each parameter and, if possible, fold constants. */
327 foreach_list_safe(n
, &actual_parameters
) {
328 ir_rvalue
*ir
= (ir_rvalue
*) n
;
329 ir_rvalue
*result
= ir
;
331 /* Apply implicit conversions (not the scalar constructor rules!) */
332 if (constructor_type
->element_type()->is_float()) {
333 const glsl_type
*desired_type
=
334 glsl_type::get_instance(GLSL_TYPE_FLOAT
,
335 ir
->type
->vector_elements
,
336 ir
->type
->matrix_columns
);
337 result
= convert_component(ir
, desired_type
);
340 if (result
->type
!= constructor_type
->element_type()) {
341 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
342 "expected: %s, found %s",
343 constructor_type
->element_type()->name
,
347 /* Attempt to convert the parameter to a constant valued expression.
348 * After doing so, track whether or not all the parameters to the
349 * constructor are trivially constant valued expressions.
351 ir_rvalue
*const constant
= result
->constant_expression_value();
353 if (constant
!= NULL
)
356 all_parameters_are_constant
= false;
358 ir
->replace_with(result
);
361 if (all_parameters_are_constant
) {
362 /* FINISHME: Add support for generating constant arrays. */
365 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "array_ctor",
367 instructions
->push_tail(var
);
370 foreach_list(node
, &actual_parameters
) {
371 ir_rvalue
*rhs
= (ir_rvalue
*) node
;
372 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
373 new(ctx
) ir_constant(i
));
375 ir_instruction
*assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
376 instructions
->push_tail(assignment
);
381 return new(ctx
) ir_dereference_variable(var
);
386 * Try to convert a record constructor to a constant expression
389 constant_record_constructor(const glsl_type
*constructor_type
,
390 YYLTYPE
*loc
, exec_list
*parameters
,
391 struct _mesa_glsl_parse_state
*state
)
394 bool all_parameters_are_constant
= true;
396 exec_node
*node
= parameters
->head
;
397 for (unsigned i
= 0; i
< constructor_type
->length
; i
++) {
398 ir_instruction
*ir
= (ir_instruction
*) node
;
400 if (node
->is_tail_sentinal()) {
401 _mesa_glsl_error(loc
, state
,
402 "insufficient parameters to constructor for `%s'",
403 constructor_type
->name
);
407 if (ir
->type
!= constructor_type
->fields
.structure
[i
].type
) {
408 _mesa_glsl_error(loc
, state
,
409 "parameter type mismatch in constructor for `%s' "
411 constructor_type
->name
,
413 constructor_type
->fields
.structure
[i
].type
->name
);
417 if (ir
->as_constant() == NULL
)
418 all_parameters_are_constant
= false;
423 if (!all_parameters_are_constant
)
426 return new(ctx
) ir_constant(constructor_type
, parameters
);
431 * Generate data for a constant matrix constructor w/a single scalar parameter
433 * Matrix constructors in GLSL can be passed a single scalar of the
434 * approriate type. In these cases, the resulting matrix is the identity
435 * matrix multipled by the specified scalar. This function generates data for
438 * \param type Type of the desired matrix.
439 * \param initializer Scalar value used to initialize the matrix diagonal.
440 * \param data Location to store the resulting matrix.
443 generate_constructor_matrix(const glsl_type
*type
, ir_constant
*initializer
,
444 ir_constant_data
*data
)
446 switch (type
->base_type
) {
449 for (unsigned i
= 0; i
< type
->components(); i
++)
452 for (unsigned i
= 0; i
< type
->matrix_columns
; i
++) {
453 /* The array offset of the ith row and column of the matrix.
455 const unsigned idx
= (i
* type
->vector_elements
) + i
;
457 data
->u
[idx
] = initializer
->value
.u
[0];
461 case GLSL_TYPE_FLOAT
:
462 for (unsigned i
= 0; i
< type
->components(); i
++)
465 for (unsigned i
= 0; i
< type
->matrix_columns
; i
++) {
466 /* The array offset of the ith row and column of the matrix.
468 const unsigned idx
= (i
* type
->vector_elements
) + i
;
470 data
->f
[idx
] = initializer
->value
.f
[0];
476 assert(!"Should not get here.");
483 * Generate data for a constant vector constructor w/a single scalar parameter
485 * Vector constructors in GLSL can be passed a single scalar of the
486 * approriate type. In these cases, the resulting vector contains the specified
487 * value in all components. This function generates data for that vector.
489 * \param type Type of the desired vector.
490 * \param initializer Scalar value used to initialize the vector.
491 * \param data Location to store the resulting vector data.
494 generate_constructor_vector(const glsl_type
*type
, ir_constant
*initializer
,
495 ir_constant_data
*data
)
497 switch (type
->base_type
) {
500 for (unsigned i
= 0; i
< type
->components(); i
++)
501 data
->u
[i
] = initializer
->value
.u
[0];
505 case GLSL_TYPE_FLOAT
:
506 for (unsigned i
= 0; i
< type
->components(); i
++)
507 data
->f
[i
] = initializer
->value
.f
[0];
512 for (unsigned i
= 0; i
< type
->components(); i
++)
513 data
->b
[i
] = initializer
->value
.b
[0];
518 assert(!"Should not get here.");
525 * Determine if a list consists of a single scalar r-value
528 single_scalar_parameter(exec_list
*parameters
)
530 const ir_rvalue
*const p
= (ir_rvalue
*) parameters
->head
;
531 assert(((ir_rvalue
*)p
)->as_rvalue() != NULL
);
533 return (p
->type
->is_scalar() && p
->next
->is_tail_sentinal());
538 * Generate inline code for a vector constructor
540 * The generated constructor code will consist of a temporary variable
541 * declaration of the same type as the constructor. A sequence of assignments
542 * from constructor parameters to the temporary will follow.
545 * An \c ir_dereference_variable of the temprorary generated in the constructor
549 emit_inline_vector_constructor(const glsl_type
*type
,
550 exec_list
*instructions
,
551 exec_list
*parameters
,
554 assert(!parameters
->is_empty());
556 ir_variable
*var
= new(ctx
) ir_variable(type
,
557 talloc_strdup(ctx
, "vec_ctor"),
559 instructions
->push_tail(var
);
561 /* There are two kinds of vector constructors.
563 * - Construct a vector from a single scalar by replicating that scalar to
564 * all components of the vector.
566 * - Construct a vector from an arbirary combination of vectors and
567 * scalars. The components of the constructor parameters are assigned
568 * to the vector in order until the vector is full.
570 const unsigned lhs_components
= type
->components();
571 if (single_scalar_parameter(parameters
)) {
572 ir_rvalue
*first_param
= (ir_rvalue
*)parameters
->head
;
573 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(first_param
, 0, 0, 0, 0,
575 ir_dereference_variable
*lhs
= new(ctx
) ir_dereference_variable(var
);
577 assert(rhs
->type
== lhs
->type
);
579 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
580 instructions
->push_tail(inst
);
582 unsigned base_component
= 0;
583 foreach_list(node
, parameters
) {
584 ir_rvalue
*rhs
= (ir_rvalue
*) node
;
585 unsigned rhs_components
= rhs
->type
->components();
587 /* Do not try to assign more components to the vector than it has!
589 if ((rhs_components
+ base_component
) > lhs_components
) {
590 rhs_components
= lhs_components
- base_component
;
593 /* Emit an assignment of the constructor parameter to the next set of
594 * components in the temporary variable.
596 unsigned mask
[4] = { 0, 0, 0, 0 };
597 for (unsigned i
= 0; i
< rhs_components
; i
++) {
598 mask
[i
] = i
+ base_component
;
602 ir_rvalue
*lhs_ref
= new(ctx
) ir_dereference_variable(var
);
603 ir_swizzle
*lhs
= new(ctx
) ir_swizzle(lhs_ref
, mask
, rhs_components
);
605 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
606 instructions
->push_tail(inst
);
608 /* Advance the component index by the number of components that were
611 base_component
+= rhs_components
;
614 return new(ctx
) ir_dereference_variable(var
);
619 * Generate assignment of a portion of a vector to a portion of a matrix column
621 * \param src_base First component of the source to be used in assignment
622 * \param column Column of destination to be assiged
623 * \param row_base First component of the destination column to be assigned
624 * \param count Number of components to be assigned
627 * \c src_base + \c count must be less than or equal to the number of components
628 * in the source vector.
631 assign_to_matrix_column(ir_variable
*var
, unsigned column
, unsigned row_base
,
632 ir_rvalue
*src
, unsigned src_base
, unsigned count
,
635 const unsigned mask
[8] = { 0, 1, 2, 3, 0, 0, 0, 0 };
637 ir_constant
*col_idx
= new(ctx
) ir_constant(column
);
638 ir_rvalue
*column_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
640 assert(column_ref
->type
->components() >= (row_base
+ count
));
641 ir_rvalue
*lhs
= new(ctx
) ir_swizzle(column_ref
, &mask
[row_base
], count
);
643 assert(src
->type
->components() >= (src_base
+ count
));
644 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(src
, &mask
[src_base
], count
);
646 return new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
651 * Generate inline code for a matrix constructor
653 * The generated constructor code will consist of a temporary variable
654 * declaration of the same type as the constructor. A sequence of assignments
655 * from constructor parameters to the temporary will follow.
658 * An \c ir_dereference_variable of the temprorary generated in the constructor
662 emit_inline_matrix_constructor(const glsl_type
*type
,
663 exec_list
*instructions
,
664 exec_list
*parameters
,
667 assert(!parameters
->is_empty());
669 ir_variable
*var
= new(ctx
) ir_variable(type
,
670 talloc_strdup(ctx
, "mat_ctor"),
672 instructions
->push_tail(var
);
674 /* There are three kinds of matrix constructors.
676 * - Construct a matrix from a single scalar by replicating that scalar to
677 * along the diagonal of the matrix and setting all other components to
680 * - Construct a matrix from an arbirary combination of vectors and
681 * scalars. The components of the constructor parameters are assigned
682 * to the matrix in colum-major order until the matrix is full.
684 * - Construct a matrix from a single matrix. The source matrix is copied
685 * to the upper left portion of the constructed matrix, and the remaining
686 * elements take values from the identity matrix.
688 ir_rvalue
*const first_param
= (ir_rvalue
*) parameters
->head
;
689 if (single_scalar_parameter(parameters
)) {
690 /* Assign the scalar to the X component of a vec4, and fill the remaining
691 * components with zero.
693 ir_variable
*rhs_var
=
694 new(ctx
) ir_variable(glsl_type::vec4_type
,
695 talloc_strdup(ctx
, "mat_ctor_vec"),
697 instructions
->push_tail(rhs_var
);
699 ir_constant_data zero
;
705 ir_instruction
*inst
=
706 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(rhs_var
),
707 new(ctx
) ir_constant(rhs_var
->type
, &zero
),
709 instructions
->push_tail(inst
);
711 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
712 ir_rvalue
*const x_of_rhs
= new(ctx
) ir_swizzle(rhs_ref
, 0, 0, 0, 0, 1);
714 inst
= new(ctx
) ir_assignment(x_of_rhs
, first_param
, NULL
);
715 instructions
->push_tail(inst
);
717 /* Assign the temporary vector to each column of the destination matrix
718 * with a swizzle that puts the X component on the diagonal of the
719 * matrix. In some cases this may mean that the X component does not
720 * get assigned into the column at all (i.e., when the matrix has more
721 * columns than rows).
723 static const unsigned rhs_swiz
[4][4] = {
730 const unsigned cols_to_init
= min(type
->matrix_columns
,
731 type
->vector_elements
);
732 for (unsigned i
= 0; i
< cols_to_init
; 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
, rhs_swiz
[i
],
738 type
->vector_elements
);
740 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
741 instructions
->push_tail(inst
);
744 for (unsigned i
= cols_to_init
; i
< type
->matrix_columns
; i
++) {
745 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
746 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
748 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
749 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, 1, 1, 1, 1,
750 type
->vector_elements
);
752 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
753 instructions
->push_tail(inst
);
755 } else if (first_param
->type
->is_matrix()) {
756 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
758 * "If a matrix is constructed from a matrix, then each component
759 * (column i, row j) in the result that has a corresponding
760 * component (column i, row j) in the argument will be initialized
761 * from there. All other components will be initialized to the
762 * identity matrix. If a matrix argument is given to a matrix
763 * constructor, it is an error to have any other arguments."
765 assert(first_param
->next
->is_tail_sentinal());
766 ir_rvalue
*const src_matrix
= first_param
;
768 /* If the source matrix is smaller, pre-initialize the relavent parts of
769 * the destination matrix to the identity matrix.
771 if ((src_matrix
->type
->matrix_columns
< var
->type
->matrix_columns
)
772 || (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)) {
774 /* If the source matrix has fewer rows, every column of the destination
775 * must be initialized. Otherwise only the columns in the destination
776 * that do not exist in the source must be initialized.
779 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)
780 ? 0 : src_matrix
->type
->matrix_columns
;
782 const glsl_type
*const col_type
= var
->type
->column_type();
783 for (/* empty */; col
< var
->type
->matrix_columns
; col
++) {
784 ir_constant_data ident
;
793 ir_rvalue
*const rhs
= new(ctx
) ir_constant(col_type
, &ident
);
795 ir_rvalue
*const lhs
=
796 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(col
));
798 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
799 instructions
->push_tail(inst
);
803 /* Assign columns from the source matrix to the destination matrix.
805 * Since the parameter will be used in the RHS of multiple assignments,
806 * generate a temporary and copy the paramter there.
808 ir_variable
*const rhs_var
=
809 new(ctx
) ir_variable(first_param
->type
,
810 talloc_strdup(ctx
, "mat_ctor_mat"),
812 instructions
->push_tail(rhs_var
);
814 ir_dereference
*const rhs_var_ref
=
815 new(ctx
) ir_dereference_variable(rhs_var
);
816 ir_instruction
*const inst
=
817 new(ctx
) ir_assignment(rhs_var_ref
, first_param
, NULL
);
818 instructions
->push_tail(inst
);
821 const unsigned swiz
[4] = { 0, 1, 2, 3 };
822 const unsigned last_col
= min(src_matrix
->type
->matrix_columns
,
823 var
->type
->matrix_columns
);
824 for (unsigned i
= 0; i
< last_col
; i
++) {
825 ir_rvalue
*const lhs_col
=
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.
840 if (lhs_col
->type
->vector_elements
< rhs_col
->type
->vector_elements
) {
843 rhs
= new(ctx
) ir_swizzle(rhs_col
, swiz
,
844 lhs_col
->type
->vector_elements
);
845 } else if (lhs_col
->type
->vector_elements
846 > rhs_col
->type
->vector_elements
) {
847 lhs
= new(ctx
) ir_swizzle(lhs_col
, swiz
,
848 rhs_col
->type
->vector_elements
);
855 assert(lhs
->type
== rhs
->type
);
857 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
858 instructions
->push_tail(inst
);
861 const unsigned rows
= type
->matrix_columns
;
862 const unsigned cols
= type
->vector_elements
;
863 unsigned col_idx
= 0;
864 unsigned row_idx
= 0;
866 foreach_list (node
, parameters
) {
867 ir_rvalue
*const rhs
= (ir_rvalue
*) node
;
868 const unsigned components_remaining_this_column
= rows
- row_idx
;
869 unsigned rhs_components
= rhs
->type
->components();
870 unsigned rhs_base
= 0;
872 /* Since the parameter might be used in the RHS of two assignments,
873 * generate a temporary and copy the paramter there.
875 ir_variable
*rhs_var
=
876 new(ctx
) ir_variable(rhs
->type
,
877 talloc_strdup(ctx
, "mat_ctor_vec"),
879 instructions
->push_tail(rhs_var
);
881 ir_dereference
*rhs_var_ref
=
882 new(ctx
) ir_dereference_variable(rhs_var
);
883 ir_instruction
*inst
= new(ctx
) ir_assignment(rhs_var_ref
, rhs
, NULL
);
884 instructions
->push_tail(inst
);
886 /* Assign the current parameter to as many components of the matrix
889 * NOTE: A single vector parameter can span two matrix columns. A
890 * single vec4, for example, can completely fill a mat2.
892 if (rhs_components
>= components_remaining_this_column
) {
893 const unsigned count
= min(rhs_components
,
894 components_remaining_this_column
);
896 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
898 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
902 instructions
->push_tail(inst
);
910 /* If there is data left in the parameter and components left to be
911 * set in the destination, emit another assignment. It is possible
912 * that the assignment could be of a vec4 to the last element of the
913 * matrix. In this case col_idx==cols, but there is still data
914 * left in the source parameter. Obviously, don't emit an assignment
915 * to data outside the destination matrix.
917 if ((col_idx
< cols
) && (rhs_base
< rhs_components
)) {
918 const unsigned count
= rhs_components
- rhs_base
;
920 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
922 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
927 instructions
->push_tail(inst
);
934 return new(ctx
) ir_dereference_variable(var
);
939 ast_function_expression::hir(exec_list
*instructions
,
940 struct _mesa_glsl_parse_state
*state
)
943 /* There are three sorts of function calls.
945 * 1. constructors - The first subexpression is an ast_type_specifier.
946 * 2. methods - Only the .length() method of array types.
947 * 3. functions - Calls to regular old functions.
949 * Method calls are actually detected when the ast_field_selection
950 * expression is handled.
952 if (is_constructor()) {
953 const ast_type_specifier
*type
= (ast_type_specifier
*) subexpressions
[0];
954 YYLTYPE loc
= type
->get_location();
957 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
960 /* Constructors for samplers are illegal.
962 if (constructor_type
->is_sampler()) {
963 _mesa_glsl_error(& loc
, state
, "cannot construct sampler type `%s'",
964 constructor_type
->name
);
965 return ir_call::get_error_instruction(ctx
);
968 if (constructor_type
->is_array()) {
969 if (state
->language_version
<= 110) {
970 _mesa_glsl_error(& loc
, state
,
971 "array constructors forbidden in GLSL 1.10");
972 return ir_call::get_error_instruction(ctx
);
975 return process_array_constructor(instructions
, constructor_type
,
976 & loc
, &this->expressions
, state
);
979 /* There are two kinds of constructor call. Constructors for built-in
980 * language types, such as mat4 and vec2, are free form. The only
981 * requirement is that the parameters must provide enough values of the
982 * correct scalar type. Constructors for arrays and structures must
983 * have the exact number of parameters with matching types in the
984 * correct order. These constructors follow essentially the same type
985 * matching rules as functions.
987 if (!constructor_type
->is_numeric() && !constructor_type
->is_boolean())
988 return ir_call::get_error_instruction(ctx
);
990 /* Total number of components of the type being constructed. */
991 const unsigned type_components
= constructor_type
->components();
993 /* Number of components from parameters that have actually been
994 * consumed. This is used to perform several kinds of error checking.
996 unsigned components_used
= 0;
998 unsigned matrix_parameters
= 0;
999 unsigned nonmatrix_parameters
= 0;
1000 exec_list actual_parameters
;
1002 foreach_list (n
, &this->expressions
) {
1003 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
1004 ir_rvalue
*result
= ast
->hir(instructions
, state
)->as_rvalue();
1006 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1008 * "It is an error to provide extra arguments beyond this
1009 * last used argument."
1011 if (components_used
>= type_components
) {
1012 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
1014 constructor_type
->name
);
1015 return ir_call::get_error_instruction(ctx
);
1018 if (!result
->type
->is_numeric() && !result
->type
->is_boolean()) {
1019 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
1020 "non-numeric data type",
1021 constructor_type
->name
);
1022 return ir_call::get_error_instruction(ctx
);
1025 /* Count the number of matrix and nonmatrix parameters. This
1026 * is used below to enforce some of the constructor rules.
1028 if (result
->type
->is_matrix())
1029 matrix_parameters
++;
1031 nonmatrix_parameters
++;
1033 actual_parameters
.push_tail(result
);
1034 components_used
+= result
->type
->components();
1037 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1039 * "It is an error to construct matrices from other matrices. This
1040 * is reserved for future use."
1042 if ((state
->language_version
<= 110) && (matrix_parameters
> 0)
1043 && constructor_type
->is_matrix()) {
1044 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
1045 "matrix in GLSL 1.10",
1046 constructor_type
->name
);
1047 return ir_call::get_error_instruction(ctx
);
1050 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1052 * "If a matrix argument is given to a matrix constructor, it is
1053 * an error to have any other arguments."
1055 if ((matrix_parameters
> 0)
1056 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
1057 && constructor_type
->is_matrix()) {
1058 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
1059 "matrix must be only parameter",
1060 constructor_type
->name
);
1061 return ir_call::get_error_instruction(ctx
);
1064 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1066 * "In these cases, there must be enough components provided in the
1067 * arguments to provide an initializer for every component in the
1068 * constructed value."
1070 if ((components_used
< type_components
) && (components_used
!= 1)) {
1071 _mesa_glsl_error(& loc
, state
, "too few components to construct "
1073 constructor_type
->name
);
1074 return ir_call::get_error_instruction(ctx
);
1077 /* Later, we cast each parameter to the same base type as the
1078 * constructor. Since there are no non-floating point matrices, we
1079 * need to break them up into a series of column vectors.
1081 if (constructor_type
->base_type
!= GLSL_TYPE_FLOAT
) {
1082 foreach_list_safe(n
, &actual_parameters
) {
1083 ir_rvalue
*matrix
= (ir_rvalue
*) n
;
1085 if (!matrix
->type
->is_matrix())
1088 /* Create a temporary containing the matrix. */
1089 ir_variable
*var
= new(ctx
) ir_variable(matrix
->type
, "matrix_tmp",
1091 instructions
->push_tail(var
);
1092 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
)
1093 ir_dereference_variable(var
), matrix
, NULL
));
1094 var
->constant_value
= matrix
->constant_expression_value();
1096 /* Replace the matrix with dereferences of its columns. */
1097 for (int i
= 0; i
< matrix
->type
->matrix_columns
; i
++) {
1098 matrix
->insert_before(new (ctx
) ir_dereference_array(var
,
1099 new(ctx
) ir_constant(i
)));
1105 bool all_parameters_are_constant
= true;
1107 /* Type cast each parameter and, if possible, fold constants.*/
1108 foreach_list_safe(n
, &actual_parameters
) {
1109 ir_rvalue
*ir
= (ir_rvalue
*) n
;
1111 const glsl_type
*desired_type
=
1112 glsl_type::get_instance(constructor_type
->base_type
,
1113 ir
->type
->vector_elements
,
1114 ir
->type
->matrix_columns
);
1115 ir_rvalue
*result
= convert_component(ir
, desired_type
);
1117 /* Attempt to convert the parameter to a constant valued expression.
1118 * After doing so, track whether or not all the parameters to the
1119 * constructor are trivially constant valued expressions.
1121 ir_rvalue
*const constant
= result
->constant_expression_value();
1123 if (constant
!= NULL
)
1126 all_parameters_are_constant
= false;
1129 ir
->replace_with(result
);
1133 /* If all of the parameters are trivially constant, create a
1134 * constant representing the complete collection of parameters.
1136 if (all_parameters_are_constant
) {
1137 if (components_used
>= type_components
)
1138 return new(ctx
) ir_constant(constructor_type
,
1139 & actual_parameters
);
1141 /* The above case must handle all scalar constructors.
1143 assert(constructor_type
->is_vector()
1144 || constructor_type
->is_matrix());
1146 /* Constructors with exactly one component are special for
1147 * vectors and matrices. For vectors it causes all elements of
1148 * the vector to be filled with the value. For matrices it
1149 * causes the matrix to be filled with 0 and the diagonal to be
1150 * filled with the value.
1152 ir_constant_data data
;
1153 ir_constant
*const initializer
=
1154 (ir_constant
*) actual_parameters
.head
;
1155 if (constructor_type
->is_matrix())
1156 generate_constructor_matrix(constructor_type
, initializer
,
1159 generate_constructor_vector(constructor_type
, initializer
,
1162 return new(ctx
) ir_constant(constructor_type
, &data
);
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
,
1172 assert(constructor_type
->is_matrix());
1173 return emit_inline_matrix_constructor(constructor_type
,
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
,
1186 const glsl_type
*const type
=
1187 state
->symbols
->get_type(id
->primary_expression
.identifier
);
1189 if ((type
!= NULL
) && type
->is_record()) {
1190 ir_constant
*constant
=
1191 constant_record_constructor(type
, &loc
, &actual_parameters
, state
);
1193 if (constant
!= NULL
)
1197 return match_function_by_name(instructions
,
1198 id
->primary_expression
.identifier
, & loc
,
1199 &actual_parameters
, state
);
1202 return ir_call::get_error_instruction(ctx
);