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
169 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
)
171 void *ctx
= talloc_parent(src
);
172 const unsigned a
= desired_type
->base_type
;
173 const unsigned b
= src
->type
->base_type
;
174 ir_expression
*result
= NULL
;
176 if (src
->type
->is_error())
179 assert(a
<= GLSL_TYPE_BOOL
);
180 assert(b
<= GLSL_TYPE_BOOL
);
182 if ((a
== b
) || (src
->type
->is_integer() && desired_type
->is_integer()))
188 if (b
== GLSL_TYPE_FLOAT
)
189 result
= new(ctx
) ir_expression(ir_unop_f2i
, desired_type
, src
, NULL
);
191 assert(b
== GLSL_TYPE_BOOL
);
192 result
= new(ctx
) ir_expression(ir_unop_b2i
, desired_type
, src
, NULL
);
195 case GLSL_TYPE_FLOAT
:
198 result
= new(ctx
) ir_expression(ir_unop_u2f
, desired_type
, src
, NULL
);
201 result
= new(ctx
) ir_expression(ir_unop_i2f
, desired_type
, src
, NULL
);
204 result
= new(ctx
) ir_expression(ir_unop_b2f
, desired_type
, src
, NULL
);
212 result
= new(ctx
) ir_expression(ir_unop_i2b
, desired_type
, src
, NULL
);
214 case GLSL_TYPE_FLOAT
:
215 result
= new(ctx
) ir_expression(ir_unop_f2b
, desired_type
, src
, NULL
);
221 assert(result
!= NULL
);
223 ir_constant
*const constant
= result
->constant_expression_value();
224 return (constant
!= NULL
) ? (ir_rvalue
*) constant
: (ir_rvalue
*) result
;
229 * Dereference a specific component from a scalar, vector, or matrix
232 dereference_component(ir_rvalue
*src
, unsigned component
)
234 void *ctx
= talloc_parent(src
);
235 assert(component
< src
->type
->components());
237 /* If the source is a constant, just create a new constant instead of a
238 * dereference of the existing constant.
240 ir_constant
*constant
= src
->as_constant();
242 return new(ctx
) ir_constant(constant
, component
);
244 if (src
->type
->is_scalar()) {
246 } else if (src
->type
->is_vector()) {
247 return new(ctx
) ir_swizzle(src
, component
, 0, 0, 0, 1);
249 assert(src
->type
->is_matrix());
251 /* Dereference a row of the matrix, then call this function again to get
252 * a specific element from that row.
254 const int c
= component
/ src
->type
->column_type()->vector_elements
;
255 const int r
= component
% src
->type
->column_type()->vector_elements
;
256 ir_constant
*const col_index
= new(ctx
) ir_constant(c
);
257 ir_dereference
*const col
= new(ctx
) ir_dereference_array(src
, col_index
);
259 col
->type
= src
->type
->column_type();
261 return dereference_component(col
, r
);
264 assert(!"Should not get here.");
270 process_array_constructor(exec_list
*instructions
,
271 const glsl_type
*constructor_type
,
272 YYLTYPE
*loc
, exec_list
*parameters
,
273 struct _mesa_glsl_parse_state
*state
)
276 /* Array constructors come in two forms: sized and unsized. Sized array
277 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
278 * variables. In this case the number of parameters must exactly match the
279 * specified size of the array.
281 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
282 * are vec4 variables. In this case the size of the array being constructed
283 * is determined by the number of parameters.
285 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
287 * "There must be exactly the same number of arguments as the size of
288 * the array being constructed. If no size is present in the
289 * constructor, then the array is explicitly sized to the number of
290 * arguments provided. The arguments are assigned in order, starting at
291 * element 0, to the elements of the constructed array. Each argument
292 * must be the same type as the element type of the array, or be a type
293 * that can be converted to the element type of the array according to
294 * Section 4.1.10 "Implicit Conversions.""
296 exec_list actual_parameters
;
297 const unsigned parameter_count
=
298 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
300 if ((parameter_count
== 0)
301 || ((constructor_type
->length
!= 0)
302 && (constructor_type
->length
!= parameter_count
))) {
303 const unsigned min_param
= (constructor_type
->length
== 0)
304 ? 1 : constructor_type
->length
;
306 _mesa_glsl_error(loc
, state
, "array constructor must have %s %u "
308 (constructor_type
->length
!= 0) ? "at least" : "exactly",
309 min_param
, (min_param
<= 1) ? "" : "s");
310 return ir_call::get_error_instruction(ctx
);
313 if (constructor_type
->length
== 0) {
315 glsl_type::get_array_instance(state
,
316 constructor_type
->element_type(),
318 assert(constructor_type
!= NULL
);
319 assert(constructor_type
->length
== parameter_count
);
322 ir_function
*f
= state
->symbols
->get_function(constructor_type
->name
);
324 /* If the constructor for this type of array does not exist, generate the
325 * prototype and add it to the symbol table.
328 f
= constructor_type
->generate_constructor(state
->symbols
);
332 process_call(instructions
, f
, loc
, &actual_parameters
, state
);
335 assert(r
->type
->is_error() || (r
->type
== constructor_type
));
342 * Try to convert a record constructor to a constant expression
345 constant_record_constructor(const glsl_type
*constructor_type
,
346 YYLTYPE
*loc
, exec_list
*parameters
,
347 struct _mesa_glsl_parse_state
*state
)
350 bool all_parameters_are_constant
= true;
352 exec_node
*node
= parameters
->head
;
353 for (unsigned i
= 0; i
< constructor_type
->length
; i
++) {
354 ir_instruction
*ir
= (ir_instruction
*) node
;
356 if (node
->is_tail_sentinal()) {
357 _mesa_glsl_error(loc
, state
,
358 "insufficient parameters to constructor for `%s'",
359 constructor_type
->name
);
363 if (ir
->type
!= constructor_type
->fields
.structure
[i
].type
) {
364 _mesa_glsl_error(loc
, state
,
365 "parameter type mismatch in constructor for `%s' "
367 constructor_type
->name
,
369 constructor_type
->fields
.structure
[i
].type
->name
);
373 if (ir
->as_constant() == NULL
)
374 all_parameters_are_constant
= false;
379 if (!all_parameters_are_constant
)
382 return new(ctx
) ir_constant(constructor_type
, parameters
);
387 * Generate data for a constant matrix constructor w/a single scalar parameter
389 * Matrix constructors in GLSL can be passed a single scalar of the
390 * approriate type. In these cases, the resulting matrix is the identity
391 * matrix multipled by the specified scalar. This function generates data for
394 * \param type Type of the desired matrix.
395 * \param initializer Scalar value used to initialize the matrix diagonal.
396 * \param data Location to store the resulting matrix.
399 generate_constructor_matrix(const glsl_type
*type
, ir_constant
*initializer
,
400 ir_constant_data
*data
)
402 switch (type
->base_type
) {
405 for (unsigned i
= 0; i
< type
->components(); i
++)
408 for (unsigned i
= 0; i
< type
->matrix_columns
; i
++) {
409 /* The array offset of the ith row and column of the matrix.
411 const unsigned idx
= (i
* type
->vector_elements
) + i
;
413 data
->u
[idx
] = initializer
->value
.u
[0];
417 case GLSL_TYPE_FLOAT
:
418 for (unsigned i
= 0; i
< type
->components(); i
++)
421 for (unsigned i
= 0; i
< type
->matrix_columns
; i
++) {
422 /* The array offset of the ith row and column of the matrix.
424 const unsigned idx
= (i
* type
->vector_elements
) + i
;
426 data
->f
[idx
] = initializer
->value
.f
[0];
432 assert(!"Should not get here.");
439 * Generate data for a constant vector constructor w/a single scalar parameter
441 * Vector constructors in GLSL can be passed a single scalar of the
442 * approriate type. In these cases, the resulting vector contains the specified
443 * value in all components. This function generates data for that vector.
445 * \param type Type of the desired vector.
446 * \param initializer Scalar value used to initialize the vector.
447 * \param data Location to store the resulting vector data.
450 generate_constructor_vector(const glsl_type
*type
, ir_constant
*initializer
,
451 ir_constant_data
*data
)
453 switch (type
->base_type
) {
456 for (unsigned i
= 0; i
< type
->components(); i
++)
457 data
->u
[i
] = initializer
->value
.u
[0];
461 case GLSL_TYPE_FLOAT
:
462 for (unsigned i
= 0; i
< type
->components(); i
++)
463 data
->f
[i
] = initializer
->value
.f
[0];
468 for (unsigned i
= 0; i
< type
->components(); i
++)
469 data
->b
[i
] = initializer
->value
.b
[0];
474 assert(!"Should not get here.");
481 * Determine if a list consists of a single scalar r-value
484 single_scalar_parameter(exec_list
*parameters
)
486 const ir_rvalue
*const p
= (ir_rvalue
*) parameters
->head
;
487 assert(((ir_rvalue
*)p
)->as_rvalue() != NULL
);
489 return (p
->type
->is_scalar() && p
->next
->is_tail_sentinal());
494 * Generate inline code for a vector constructor
496 * The generated constructor code will consist of a temporary variable
497 * declaration of the same type as the constructor. A sequence of assignments
498 * from constructor parameters to the temporary will follow.
501 * An \c ir_dereference_variable of the temprorary generated in the constructor
505 emit_inline_vector_constructor(const glsl_type
*type
,
506 exec_list
*instructions
,
507 exec_list
*parameters
,
510 assert(!parameters
->is_empty());
512 ir_variable
*var
= new(ctx
) ir_variable(type
,
513 talloc_strdup(ctx
, "vec_ctor"),
515 instructions
->push_tail(var
);
517 /* There are two kinds of vector constructors.
519 * - Construct a vector from a single scalar by replicating that scalar to
520 * all components of the vector.
522 * - Construct a vector from an arbirary combination of vectors and
523 * scalars. The components of the constructor parameters are assigned
524 * to the vector in order until the vector is full.
526 const unsigned lhs_components
= type
->components();
527 if (single_scalar_parameter(parameters
)) {
528 ir_rvalue
*first_param
= (ir_rvalue
*)parameters
->head
;
529 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(first_param
, 0, 0, 0, 0,
531 ir_dereference_variable
*lhs
= new(ctx
) ir_dereference_variable(var
);
533 assert(rhs
->type
== lhs
->type
);
535 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
536 instructions
->push_tail(inst
);
538 unsigned base_component
= 0;
539 foreach_list(node
, parameters
) {
540 ir_rvalue
*rhs
= (ir_rvalue
*) node
;
541 unsigned rhs_components
= rhs
->type
->components();
543 /* Do not try to assign more components to the vector than it has!
545 if ((rhs_components
+ base_component
) > lhs_components
) {
546 rhs_components
= lhs_components
- base_component
;
549 /* Emit an assignment of the constructor parameter to the next set of
550 * components in the temporary variable.
552 unsigned mask
[4] = { 0, 0, 0, 0 };
553 for (unsigned i
= 0; i
< rhs_components
; i
++) {
554 mask
[i
] = i
+ base_component
;
558 ir_rvalue
*lhs_ref
= new(ctx
) ir_dereference_variable(var
);
559 ir_swizzle
*lhs
= new(ctx
) ir_swizzle(lhs_ref
, mask
, rhs_components
);
561 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
562 instructions
->push_tail(inst
);
564 /* Advance the component index by the number of components that were
567 base_component
+= rhs_components
;
570 return new(ctx
) ir_dereference_variable(var
);
575 * Generate assignment of a portion of a vector to a portion of a matrix column
577 * \param src_base First component of the source to be used in assignment
578 * \param column Column of destination to be assiged
579 * \param row_base First component of the destination column to be assigned
580 * \param count Number of components to be assigned
583 * \c src_base + \c count must be less than or equal to the number of components
584 * in the source vector.
587 assign_to_matrix_column(ir_variable
*var
, unsigned column
, unsigned row_base
,
588 ir_rvalue
*src
, unsigned src_base
, unsigned count
,
591 const unsigned mask
[8] = { 0, 1, 2, 3, 0, 0, 0, 0 };
593 ir_constant
*col_idx
= new(ctx
) ir_constant(column
);
594 ir_rvalue
*column_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
596 assert(column_ref
->type
->components() >= (row_base
+ count
));
597 ir_rvalue
*lhs
= new(ctx
) ir_swizzle(column_ref
, &mask
[row_base
], count
);
599 assert(src
->type
->components() >= (src_base
+ count
));
600 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(src
, &mask
[src_base
], count
);
602 return new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
607 * Generate inline code for a matrix constructor
609 * The generated constructor code will consist of a temporary variable
610 * declaration of the same type as the constructor. A sequence of assignments
611 * from constructor parameters to the temporary will follow.
614 * An \c ir_dereference_variable of the temprorary generated in the constructor
618 emit_inline_matrix_constructor(const glsl_type
*type
,
619 exec_list
*instructions
,
620 exec_list
*parameters
,
623 assert(!parameters
->is_empty());
625 ir_variable
*var
= new(ctx
) ir_variable(type
,
626 talloc_strdup(ctx
, "mat_ctor"),
628 instructions
->push_tail(var
);
630 /* There are three kinds of matrix constructors.
632 * - Construct a matrix from a single scalar by replicating that scalar to
633 * along the diagonal of the matrix and setting all other components to
636 * - Construct a matrix from an arbirary combination of vectors and
637 * scalars. The components of the constructor parameters are assigned
638 * to the matrix in colum-major order until the matrix is full.
640 * - Construct a matrix from a single matrix. The source matrix is copied
641 * to the upper left portion of the constructed matrix, and the remaining
642 * elements take values from the identity matrix.
644 ir_rvalue
*const first_param
= (ir_rvalue
*) parameters
->head
;
645 if (single_scalar_parameter(parameters
)) {
646 /* Assign the scalar to the X component of a vec4, and fill the remaining
647 * components with zero.
649 ir_variable
*rhs_var
=
650 new(ctx
) ir_variable(glsl_type::vec4_type
,
651 talloc_strdup(ctx
, "mat_ctor_vec"),
653 instructions
->push_tail(rhs_var
);
655 ir_constant_data zero
;
661 ir_instruction
*inst
=
662 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(rhs_var
),
663 new(ctx
) ir_constant(rhs_var
->type
, &zero
),
665 instructions
->push_tail(inst
);
667 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
668 ir_rvalue
*const x_of_rhs
= new(ctx
) ir_swizzle(rhs_ref
, 0, 0, 0, 0, 1);
670 inst
= new(ctx
) ir_assignment(x_of_rhs
, first_param
, NULL
);
671 instructions
->push_tail(inst
);
673 /* Assign the temporary vector to each column of the destination matrix
674 * with a swizzle that puts the X component on the diagonal of the
675 * matrix. In some cases this may mean that the X component does not
676 * get assigned into the column at all (i.e., when the matrix has more
677 * columns than rows).
679 static const unsigned rhs_swiz
[4][4] = {
686 const unsigned cols_to_init
= min(type
->matrix_columns
,
687 type
->vector_elements
);
688 for (unsigned i
= 0; i
< cols_to_init
; i
++) {
689 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
690 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
692 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
693 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, rhs_swiz
[i
],
694 type
->vector_elements
);
696 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
697 instructions
->push_tail(inst
);
700 for (unsigned i
= cols_to_init
; i
< type
->matrix_columns
; i
++) {
701 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
702 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
704 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
705 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, 1, 1, 1, 1,
706 type
->vector_elements
);
708 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
709 instructions
->push_tail(inst
);
711 } else if (first_param
->type
->is_matrix()) {
712 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
714 * "If a matrix is constructed from a matrix, then each component
715 * (column i, row j) in the result that has a corresponding
716 * component (column i, row j) in the argument will be initialized
717 * from there. All other components will be initialized to the
718 * identity matrix. If a matrix argument is given to a matrix
719 * constructor, it is an error to have any other arguments."
721 assert(first_param
->next
->is_tail_sentinal());
722 ir_rvalue
*const src_matrix
= first_param
;
724 /* If the source matrix is smaller, pre-initialize the relavent parts of
725 * the destination matrix to the identity matrix.
727 if ((src_matrix
->type
->matrix_columns
< var
->type
->matrix_columns
)
728 || (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)) {
730 /* If the source matrix has fewer rows, every column of the destination
731 * must be initialized. Otherwise only the columns in the destination
732 * that do not exist in the source must be initialized.
735 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)
736 ? 0 : src_matrix
->type
->matrix_columns
;
738 const glsl_type
*const col_type
= var
->type
->column_type();
739 for (/* empty */; col
< var
->type
->matrix_columns
; col
++) {
740 ir_constant_data ident
;
749 ir_rvalue
*const rhs
= new(ctx
) ir_constant(col_type
, &ident
);
751 ir_rvalue
*const lhs
=
752 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(col
));
754 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
755 instructions
->push_tail(inst
);
759 /* Assign columns from the source matrix to the destination matrix.
761 * Since the parameter will be used in the RHS of multiple assignments,
762 * generate a temporary and copy the paramter there.
764 ir_variable
*const rhs_var
=
765 new(ctx
) ir_variable(first_param
->type
,
766 talloc_strdup(ctx
, "mat_ctor_mat"),
768 instructions
->push_tail(rhs_var
);
770 ir_dereference
*const rhs_var_ref
=
771 new(ctx
) ir_dereference_variable(rhs_var
);
772 ir_instruction
*const inst
=
773 new(ctx
) ir_assignment(rhs_var_ref
, first_param
, NULL
);
774 instructions
->push_tail(inst
);
777 const unsigned swiz
[4] = { 0, 1, 2, 3 };
778 const unsigned last_col
= min(src_matrix
->type
->matrix_columns
,
779 var
->type
->matrix_columns
);
780 for (unsigned i
= 0; i
< last_col
; i
++) {
781 ir_rvalue
*const lhs_col
=
782 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
783 ir_rvalue
*const rhs_col
=
784 new(ctx
) ir_dereference_array(rhs_var
, new(ctx
) ir_constant(i
));
786 /* If one matrix has columns that are smaller than the columns of the
787 * other matrix, wrap the column access of the larger with a swizzle
788 * so that the LHS and RHS of the assignment have the same size (and
789 * therefore have the same type).
791 * It would be perfectly valid to unconditionally generate the
792 * swizzles, this this will typically result in a more compact IR tree.
796 if (lhs_col
->type
->vector_elements
< rhs_col
->type
->vector_elements
) {
799 rhs
= new(ctx
) ir_swizzle(rhs_col
, swiz
,
800 lhs_col
->type
->vector_elements
);
801 } else if (lhs_col
->type
->vector_elements
802 > rhs_col
->type
->vector_elements
) {
803 lhs
= new(ctx
) ir_swizzle(lhs_col
, swiz
,
804 rhs_col
->type
->vector_elements
);
811 assert(lhs
->type
== rhs
->type
);
813 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
814 instructions
->push_tail(inst
);
817 const unsigned rows
= type
->matrix_columns
;
818 const unsigned cols
= type
->vector_elements
;
819 unsigned col_idx
= 0;
820 unsigned row_idx
= 0;
822 foreach_list (node
, parameters
) {
823 ir_rvalue
*const rhs
= (ir_rvalue
*) node
;
824 const unsigned components_remaining_this_column
= rows
- row_idx
;
825 unsigned rhs_components
= rhs
->type
->components();
826 unsigned rhs_base
= 0;
828 /* Since the parameter might be used in the RHS of two assignments,
829 * generate a temporary and copy the paramter there.
831 ir_variable
*rhs_var
=
832 new(ctx
) ir_variable(rhs
->type
,
833 talloc_strdup(ctx
, "mat_ctor_vec"),
835 instructions
->push_tail(rhs_var
);
837 ir_dereference
*rhs_var_ref
=
838 new(ctx
) ir_dereference_variable(rhs_var
);
839 ir_instruction
*inst
= new(ctx
) ir_assignment(rhs_var_ref
, rhs
, NULL
);
840 instructions
->push_tail(inst
);
842 /* Assign the current parameter to as many components of the matrix
845 * NOTE: A single vector parameter can span two matrix columns. A
846 * single vec4, for example, can completely fill a mat2.
848 if (rhs_components
>= components_remaining_this_column
) {
849 const unsigned count
= min(rhs_components
,
850 components_remaining_this_column
);
852 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
854 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
858 instructions
->push_tail(inst
);
866 /* If there is data left in the parameter and components left to be
867 * set in the destination, emit another assignment. It is possible
868 * that the assignment could be of a vec4 to the last element of the
869 * matrix. In this case col_idx==cols, but there is still data
870 * left in the source parameter. Obviously, don't emit an assignment
871 * to data outside the destination matrix.
873 if ((col_idx
< cols
) && (rhs_base
< rhs_components
)) {
874 const unsigned count
= rhs_components
- rhs_base
;
876 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
878 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
883 instructions
->push_tail(inst
);
890 return new(ctx
) ir_dereference_variable(var
);
895 ast_function_expression::hir(exec_list
*instructions
,
896 struct _mesa_glsl_parse_state
*state
)
899 /* There are three sorts of function calls.
901 * 1. constructors - The first subexpression is an ast_type_specifier.
902 * 2. methods - Only the .length() method of array types.
903 * 3. functions - Calls to regular old functions.
905 * Method calls are actually detected when the ast_field_selection
906 * expression is handled.
908 if (is_constructor()) {
909 const ast_type_specifier
*type
= (ast_type_specifier
*) subexpressions
[0];
910 YYLTYPE loc
= type
->get_location();
913 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
916 /* Constructors for samplers are illegal.
918 if (constructor_type
->is_sampler()) {
919 _mesa_glsl_error(& loc
, state
, "cannot construct sampler type `%s'",
920 constructor_type
->name
);
921 return ir_call::get_error_instruction(ctx
);
924 if (constructor_type
->is_array()) {
925 if (state
->language_version
<= 110) {
926 _mesa_glsl_error(& loc
, state
,
927 "array constructors forbidden in GLSL 1.10");
928 return ir_call::get_error_instruction(ctx
);
931 return process_array_constructor(instructions
, constructor_type
,
932 & loc
, &this->expressions
, state
);
935 /* There are two kinds of constructor call. Constructors for built-in
936 * language types, such as mat4 and vec2, are free form. The only
937 * requirement is that the parameters must provide enough values of the
938 * correct scalar type. Constructors for arrays and structures must
939 * have the exact number of parameters with matching types in the
940 * correct order. These constructors follow essentially the same type
941 * matching rules as functions.
943 if (!constructor_type
->is_numeric() && !constructor_type
->is_boolean())
944 return ir_call::get_error_instruction(ctx
);
946 /* Total number of components of the type being constructed. */
947 const unsigned type_components
= constructor_type
->components();
949 /* Number of components from parameters that have actually been
950 * consumed. This is used to perform several kinds of error checking.
952 unsigned components_used
= 0;
954 unsigned matrix_parameters
= 0;
955 unsigned nonmatrix_parameters
= 0;
956 exec_list actual_parameters
;
958 foreach_list (n
, &this->expressions
) {
959 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
960 ir_rvalue
*result
= ast
->hir(instructions
, state
)->as_rvalue();
962 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
964 * "It is an error to provide extra arguments beyond this
965 * last used argument."
967 if (components_used
>= type_components
) {
968 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
970 constructor_type
->name
);
971 return ir_call::get_error_instruction(ctx
);
974 if (!result
->type
->is_numeric() && !result
->type
->is_boolean()) {
975 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
976 "non-numeric data type",
977 constructor_type
->name
);
978 return ir_call::get_error_instruction(ctx
);
981 /* Count the number of matrix and nonmatrix parameters. This
982 * is used below to enforce some of the constructor rules.
984 if (result
->type
->is_matrix())
987 nonmatrix_parameters
++;
989 actual_parameters
.push_tail(result
);
990 components_used
+= result
->type
->components();
993 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
995 * "It is an error to construct matrices from other matrices. This
996 * is reserved for future use."
998 if ((state
->language_version
<= 110) && (matrix_parameters
> 0)
999 && constructor_type
->is_matrix()) {
1000 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
1001 "matrix in GLSL 1.10",
1002 constructor_type
->name
);
1003 return ir_call::get_error_instruction(ctx
);
1006 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1008 * "If a matrix argument is given to a matrix constructor, it is
1009 * an error to have any other arguments."
1011 if ((matrix_parameters
> 0)
1012 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
1013 && constructor_type
->is_matrix()) {
1014 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
1015 "matrix must be only parameter",
1016 constructor_type
->name
);
1017 return ir_call::get_error_instruction(ctx
);
1020 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1022 * "In these cases, there must be enough components provided in the
1023 * arguments to provide an initializer for every component in the
1024 * constructed value."
1026 if ((components_used
< type_components
) && (components_used
!= 1)) {
1027 _mesa_glsl_error(& loc
, state
, "too few components to construct "
1029 constructor_type
->name
);
1030 return ir_call::get_error_instruction(ctx
);
1033 /* Later, we cast each parameter to the same base type as the
1034 * constructor. Since there are no non-floating point matrices, we
1035 * need to break them up into a series of column vectors.
1037 if (constructor_type
->base_type
!= GLSL_TYPE_FLOAT
) {
1038 foreach_list_safe(n
, &actual_parameters
) {
1039 ir_rvalue
*matrix
= (ir_rvalue
*) n
;
1041 if (!matrix
->type
->is_matrix())
1044 /* Create a temporary containing the matrix. */
1045 ir_variable
*var
= new(ctx
) ir_variable(matrix
->type
, "matrix_tmp",
1047 instructions
->push_tail(var
);
1048 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
)
1049 ir_dereference_variable(var
), matrix
, NULL
));
1050 var
->constant_value
= matrix
->constant_expression_value();
1052 /* Replace the matrix with dereferences of its columns. */
1053 for (int i
= 0; i
< matrix
->type
->matrix_columns
; i
++) {
1054 matrix
->insert_before(new (ctx
) ir_dereference_array(var
,
1055 new(ctx
) ir_constant(i
)));
1061 bool all_parameters_are_constant
= true;
1063 /* Type cast each parameter and, if possible, fold constants.*/
1064 foreach_list_safe(n
, &actual_parameters
) {
1065 ir_rvalue
*ir
= (ir_rvalue
*) n
;
1067 const glsl_type
*desired_type
=
1068 glsl_type::get_instance(constructor_type
->base_type
,
1069 ir
->type
->vector_elements
,
1070 ir
->type
->matrix_columns
);
1071 ir_rvalue
*result
= convert_component(ir
, desired_type
);
1073 /* Attempt to convert the parameter to a constant valued expression.
1074 * After doing so, track whether or not all the parameters to the
1075 * constructor are trivially constant valued expressions.
1077 ir_rvalue
*const constant
= result
->constant_expression_value();
1079 if (constant
!= NULL
)
1082 all_parameters_are_constant
= false;
1085 ir
->insert_before(result
);
1090 /* If all of the parameters are trivially constant, create a
1091 * constant representing the complete collection of parameters.
1093 if (all_parameters_are_constant
) {
1094 if (components_used
>= type_components
)
1095 return new(ctx
) ir_constant(constructor_type
,
1096 & actual_parameters
);
1098 /* The above case must handle all scalar constructors.
1100 assert(constructor_type
->is_vector()
1101 || constructor_type
->is_matrix());
1103 /* Constructors with exactly one component are special for
1104 * vectors and matrices. For vectors it causes all elements of
1105 * the vector to be filled with the value. For matrices it
1106 * causes the matrix to be filled with 0 and the diagonal to be
1107 * filled with the value.
1109 ir_constant_data data
;
1110 ir_constant
*const initializer
=
1111 (ir_constant
*) actual_parameters
.head
;
1112 if (constructor_type
->is_matrix())
1113 generate_constructor_matrix(constructor_type
, initializer
,
1116 generate_constructor_vector(constructor_type
, initializer
,
1119 return new(ctx
) ir_constant(constructor_type
, &data
);
1120 } else if (constructor_type
->is_scalar()) {
1121 return dereference_component((ir_rvalue
*) actual_parameters
.head
,
1123 } else if (constructor_type
->is_vector()) {
1124 return emit_inline_vector_constructor(constructor_type
,
1129 assert(constructor_type
->is_matrix());
1130 return emit_inline_matrix_constructor(constructor_type
,
1136 const ast_expression
*id
= subexpressions
[0];
1137 YYLTYPE loc
= id
->get_location();
1138 exec_list actual_parameters
;
1140 process_parameters(instructions
, &actual_parameters
, &this->expressions
,
1143 const glsl_type
*const type
=
1144 state
->symbols
->get_type(id
->primary_expression
.identifier
);
1146 if ((type
!= NULL
) && type
->is_record()) {
1147 ir_constant
*constant
=
1148 constant_record_constructor(type
, &loc
, &actual_parameters
, state
);
1150 if (constant
!= NULL
)
1154 return match_function_by_name(instructions
,
1155 id
->primary_expression
.identifier
, & loc
,
1156 &actual_parameters
, state
);
1159 return ir_call::get_error_instruction(ctx
);