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 process_parameters(exec_list
*instructions
, exec_list
*actual_parameters
,
36 exec_list
*parameters
,
37 struct _mesa_glsl_parse_state
*state
)
41 foreach_list (n
, parameters
) {
42 ast_node
*const ast
= exec_node_data(ast_node
, n
, link
);
43 ir_rvalue
*result
= ast
->hir(instructions
, state
);
45 ir_constant
*const constant
= result
->constant_expression_value();
49 actual_parameters
->push_tail(result
);
58 process_call(exec_list
*instructions
, ir_function
*f
,
59 YYLTYPE
*loc
, exec_list
*actual_parameters
,
60 struct _mesa_glsl_parse_state
*state
)
62 void *ctx
= talloc_parent(state
);
64 const ir_function_signature
*sig
=
65 f
->matching_signature(actual_parameters
);
67 /* The instructions param will be used when the FINISHMEs below are done */
71 /* Verify that 'out' and 'inout' actual parameters are lvalues. This
72 * isn't done in ir_function::matching_signature because that function
73 * cannot generate the necessary diagnostics.
75 exec_list_iterator actual_iter
= actual_parameters
->iterator();
76 exec_list_iterator formal_iter
= sig
->parameters
.iterator();
78 while (actual_iter
.has_next()) {
79 ir_rvalue
*actual
= (ir_rvalue
*) actual_iter
.get();
80 ir_variable
*formal
= (ir_variable
*) formal_iter
.get();
82 assert(actual
!= NULL
);
83 assert(formal
!= NULL
);
85 if ((formal
->mode
== ir_var_out
)
86 || (formal
->mode
== ir_var_inout
)) {
87 if (! actual
->is_lvalue()) {
88 /* FINISHME: Log a better diagnostic here. There is no way
89 * FINISHME: to tell the user which parameter is invalid.
91 _mesa_glsl_error(loc
, state
, "`%s' parameter is not lvalue",
92 (formal
->mode
== ir_var_out
) ? "out" : "inout");
100 /* FINISHME: The list of actual parameters needs to be modified to
101 * FINISHME: include any necessary conversions.
103 return new(ctx
) ir_call(sig
, actual_parameters
);
105 /* FINISHME: Log a better error message here. G++ will show the types
106 * FINISHME: of the actual parameters and the set of candidate
107 * FINISHME: functions. A different error should also be logged when
108 * FINISHME: multiple functions match.
110 _mesa_glsl_error(loc
, state
, "no matching function for call to `%s'",
112 return ir_call::get_error_instruction(ctx
);
118 match_function_by_name(exec_list
*instructions
, const char *name
,
119 YYLTYPE
*loc
, exec_list
*actual_parameters
,
120 struct _mesa_glsl_parse_state
*state
)
122 void *ctx
= talloc_parent(state
);
123 ir_function
*f
= state
->symbols
->get_function(name
);
126 _mesa_glsl_error(loc
, state
, "function `%s' undeclared", name
);
127 return ir_call::get_error_instruction(ctx
);
130 /* Once we've determined that the function being called might exist, try
131 * to find an overload of the function that matches the parameters.
133 return process_call(instructions
, f
, loc
, actual_parameters
, state
);
138 * Perform automatic type conversion of constructor parameters
141 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
)
143 void *ctx
= talloc_parent(src
);
144 const unsigned a
= desired_type
->base_type
;
145 const unsigned b
= src
->type
->base_type
;
146 ir_expression
*result
= NULL
;
148 if (src
->type
->is_error())
151 assert(a
<= GLSL_TYPE_BOOL
);
152 assert(b
<= GLSL_TYPE_BOOL
);
154 if ((a
== b
) || (src
->type
->is_integer() && desired_type
->is_integer()))
160 if (b
== GLSL_TYPE_FLOAT
)
161 result
= new(ctx
) ir_expression(ir_unop_f2i
, desired_type
, src
, NULL
);
163 assert(b
== GLSL_TYPE_BOOL
);
164 result
= new(ctx
) ir_expression(ir_unop_b2i
, desired_type
, src
, NULL
);
167 case GLSL_TYPE_FLOAT
:
170 result
= new(ctx
) ir_expression(ir_unop_u2f
, desired_type
, src
, NULL
);
173 result
= new(ctx
) ir_expression(ir_unop_i2f
, desired_type
, src
, NULL
);
176 result
= new(ctx
) ir_expression(ir_unop_b2f
, desired_type
, src
, NULL
);
184 result
= new(ctx
) ir_expression(ir_unop_i2b
, desired_type
, src
, NULL
);
186 case GLSL_TYPE_FLOAT
:
187 result
= new(ctx
) ir_expression(ir_unop_f2b
, desired_type
, src
, NULL
);
193 assert(result
!= NULL
);
195 ir_constant
*const constant
= result
->constant_expression_value();
196 return (constant
!= NULL
) ? (ir_rvalue
*) constant
: (ir_rvalue
*) result
;
201 * Dereference a specific component from a scalar, vector, or matrix
204 dereference_component(ir_rvalue
*src
, unsigned component
)
206 void *ctx
= talloc_parent(src
);
207 assert(component
< src
->type
->components());
209 /* If the source is a constant, just create a new constant instead of a
210 * dereference of the existing constant.
212 ir_constant
*constant
= src
->as_constant();
214 return new(ctx
) ir_constant(constant
, component
);
216 if (src
->type
->is_scalar()) {
218 } else if (src
->type
->is_vector()) {
219 return new(ctx
) ir_swizzle(src
, component
, 0, 0, 0, 1);
221 assert(src
->type
->is_matrix());
223 /* Dereference a row of the matrix, then call this function again to get
224 * a specific element from that row.
226 const int c
= component
/ src
->type
->column_type()->vector_elements
;
227 const int r
= component
% src
->type
->column_type()->vector_elements
;
228 ir_constant
*const col_index
= new(ctx
) ir_constant(c
);
229 ir_dereference
*const col
= new(ctx
) ir_dereference_array(src
, col_index
);
231 col
->type
= src
->type
->column_type();
233 return dereference_component(col
, r
);
236 assert(!"Should not get here.");
242 process_array_constructor(exec_list
*instructions
,
243 const glsl_type
*constructor_type
,
244 YYLTYPE
*loc
, exec_list
*parameters
,
245 struct _mesa_glsl_parse_state
*state
)
247 void *ctx
= talloc_parent(state
);
248 /* Array constructors come in two forms: sized and unsized. Sized array
249 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
250 * variables. In this case the number of parameters must exactly match the
251 * specified size of the array.
253 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
254 * are vec4 variables. In this case the size of the array being constructed
255 * is determined by the number of parameters.
257 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
259 * "There must be exactly the same number of arguments as the size of
260 * the array being constructed. If no size is present in the
261 * constructor, then the array is explicitly sized to the number of
262 * arguments provided. The arguments are assigned in order, starting at
263 * element 0, to the elements of the constructed array. Each argument
264 * must be the same type as the element type of the array, or be a type
265 * that can be converted to the element type of the array according to
266 * Section 4.1.10 "Implicit Conversions.""
268 exec_list actual_parameters
;
269 const unsigned parameter_count
=
270 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
272 if ((parameter_count
== 0)
273 || ((constructor_type
->length
!= 0)
274 && (constructor_type
->length
!= parameter_count
))) {
275 const unsigned min_param
= (constructor_type
->length
== 0)
276 ? 1 : constructor_type
->length
;
278 _mesa_glsl_error(loc
, state
, "array constructor must have %s %u "
280 (constructor_type
->length
!= 0) ? "at least" : "exactly",
281 min_param
, (min_param
<= 1) ? "" : "s");
282 return ir_call::get_error_instruction(ctx
);
285 if (constructor_type
->length
== 0) {
287 glsl_type::get_array_instance(state
,
288 constructor_type
->element_type(),
290 assert(constructor_type
!= NULL
);
291 assert(constructor_type
->length
== parameter_count
);
294 ir_function
*f
= state
->symbols
->get_function(constructor_type
->name
);
296 /* If the constructor for this type of array does not exist, generate the
297 * prototype and add it to the symbol table.
300 f
= constructor_type
->generate_constructor(state
->symbols
);
304 process_call(instructions
, f
, loc
, &actual_parameters
, state
);
307 assert(r
->type
->is_error() || (r
->type
== constructor_type
));
314 * Try to convert a record constructor to a constant expression
317 constant_record_constructor(const glsl_type
*constructor_type
,
318 YYLTYPE
*loc
, exec_list
*parameters
,
319 struct _mesa_glsl_parse_state
*state
)
321 void *ctx
= talloc_parent(state
);
322 bool all_parameters_are_constant
= true;
324 exec_node
*node
= parameters
->head
;
325 for (unsigned i
= 0; i
< constructor_type
->length
; i
++) {
326 ir_instruction
*ir
= (ir_instruction
*) node
;
328 if (node
->is_tail_sentinal()) {
329 _mesa_glsl_error(loc
, state
,
330 "insufficient parameters to constructor for `%s'",
331 constructor_type
->name
);
335 if (ir
->type
!= constructor_type
->fields
.structure
[i
].type
) {
336 _mesa_glsl_error(loc
, state
,
337 "parameter type mismatch in constructor for `%s' "
339 constructor_type
->name
,
341 constructor_type
->fields
.structure
[i
].type
->name
);
345 if (ir
->as_constant() == NULL
)
346 all_parameters_are_constant
= false;
351 if (!all_parameters_are_constant
)
354 return new(ctx
) ir_constant(constructor_type
, parameters
);
359 * Generate data for a constant matrix constructor w/a single scalar parameter
361 * Matrix constructors in GLSL can be passed a single scalar of the
362 * approriate type. In these cases, the resulting matrix is the identity
363 * matrix multipled by the specified scalar. This function generates data for
366 * \param type Type of the desired matrix.
367 * \param initializer Scalar value used to initialize the matrix diagonal.
368 * \param data Location to store the resulting matrix.
371 generate_constructor_matrix(const glsl_type
*type
, ir_constant
*initializer
,
372 ir_constant_data
*data
)
374 switch (type
->base_type
) {
377 for (unsigned i
= 0; i
< type
->components(); i
++)
380 for (unsigned i
= 0; i
< type
->matrix_columns
; i
++) {
381 /* The array offset of the ith row and column of the matrix.
383 const unsigned idx
= (i
* type
->vector_elements
) + i
;
385 data
->u
[idx
] = initializer
->value
.u
[0];
389 case GLSL_TYPE_FLOAT
:
390 for (unsigned i
= 0; i
< type
->components(); i
++)
393 for (unsigned i
= 0; i
< type
->matrix_columns
; i
++) {
394 /* The array offset of the ith row and column of the matrix.
396 const unsigned idx
= (i
* type
->vector_elements
) + i
;
398 data
->f
[idx
] = initializer
->value
.f
[0];
404 assert(!"Should not get here.");
411 * Generate data for a constant vector constructor w/a single scalar parameter
413 * Vector constructors in GLSL can be passed a single scalar of the
414 * approriate type. In these cases, the resulting vector contains the specified
415 * value in all components. This function generates data for that vector.
417 * \param type Type of the desired vector.
418 * \param initializer Scalar value used to initialize the vector.
419 * \param data Location to store the resulting vector data.
422 generate_constructor_vector(const glsl_type
*type
, ir_constant
*initializer
,
423 ir_constant_data
*data
)
425 switch (type
->base_type
) {
428 for (unsigned i
= 0; i
< type
->components(); i
++)
429 data
->u
[i
] = initializer
->value
.u
[0];
433 case GLSL_TYPE_FLOAT
:
434 for (unsigned i
= 0; i
< type
->components(); i
++)
435 data
->f
[i
] = initializer
->value
.f
[0];
440 for (unsigned i
= 0; i
< type
->components(); i
++)
441 data
->b
[i
] = initializer
->value
.b
[0];
446 assert(!"Should not get here.");
453 * Determine if a list consists of a single scalar r-value
456 single_scalar_parameter(exec_list
*parameters
)
458 const ir_rvalue
*const p
= (ir_rvalue
*) parameters
->head
;
459 assert(((ir_rvalue
*)p
)->as_rvalue() != NULL
);
461 return (p
->type
->is_scalar() && p
->next
->is_tail_sentinal());
466 * Generate inline code for a vector constructor
468 * The generated constructor code will consist of a temporary variable
469 * declaration of the same type as the constructor. A sequence of assignments
470 * from constructor parameters to the temporary will follow.
473 * An \c ir_dereference_variable of the temprorary generated in the constructor
477 emit_inline_vector_constructor(const glsl_type
*type
,
478 exec_list
*instructions
,
479 exec_list
*parameters
,
482 assert(!parameters
->is_empty());
484 ir_variable
*var
= new(ctx
) ir_variable(type
, strdup("vec_ctor"));
485 instructions
->push_tail(var
);
487 /* There are two kinds of vector constructors.
489 * - Construct a vector from a single scalar by replicating that scalar to
490 * all components of the vector.
492 * - Construct a vector from an arbirary combination of vectors and
493 * scalars. The components of the constructor parameters are assigned
494 * to the vector in order until the vector is full.
496 const unsigned lhs_components
= type
->components();
497 if (single_scalar_parameter(parameters
)) {
498 ir_rvalue
*first_param
= (ir_rvalue
*)parameters
->head
;
499 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(first_param
, 0, 0, 0, 0,
501 ir_dereference_variable
*lhs
= new(ctx
) ir_dereference_variable(var
);
503 assert(rhs
->type
== lhs
->type
);
505 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
506 instructions
->push_tail(inst
);
508 unsigned base_component
= 0;
509 foreach_list(node
, parameters
) {
510 ir_rvalue
*rhs
= (ir_rvalue
*) node
;
511 unsigned rhs_components
= rhs
->type
->components();
513 /* Do not try to assign more components to the vector than it has!
515 if ((rhs_components
+ base_component
) > lhs_components
) {
516 rhs_components
= lhs_components
- base_component
;
519 /* Emit an assignment of the constructor parameter to the next set of
520 * components in the temporary variable.
522 unsigned mask
[4] = { 0, 0, 0, 0 };
523 for (unsigned i
= 0; i
< rhs_components
; i
++) {
524 mask
[i
] = i
+ base_component
;
528 ir_rvalue
*lhs_ref
= new(ctx
) ir_dereference_variable(var
);
529 ir_swizzle
*lhs
= new(ctx
) ir_swizzle(lhs_ref
, mask
, rhs_components
);
531 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
532 instructions
->push_tail(inst
);
534 /* Advance the component index by the number of components that were
537 base_component
+= rhs_components
;
540 return new(ctx
) ir_dereference_variable(var
);
545 * Generate assignment of a portion of a vector to a portion of a matrix column
547 * \param src_base First component of the source to be used in assignment
548 * \param column Column of destination to be assiged
549 * \param row_base First component of the destination column to be assigned
550 * \param count Number of components to be assigned
553 * \c src_base + \c count must be less than or equal to the number of components
554 * in the source vector.
557 assign_to_matrix_column(ir_variable
*var
, unsigned column
, unsigned row_base
,
558 ir_rvalue
*src
, unsigned src_base
, unsigned count
,
561 const unsigned mask
[8] = { 0, 1, 2, 3, 0, 0, 0, 0 };
563 ir_constant
*col_idx
= new(ctx
) ir_constant(column
);
564 ir_rvalue
*column_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
566 assert(column_ref
->type
->components() >= (row_base
+ count
));
567 ir_rvalue
*lhs
= new(ctx
) ir_swizzle(column_ref
, &mask
[row_base
], count
);
569 assert(src
->type
->components() >= (src_base
+ count
));
570 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(src
, &mask
[src_base
], count
);
572 return new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
577 * Generate inline code for a matrix constructor
579 * The generated constructor code will consist of a temporary variable
580 * declaration of the same type as the constructor. A sequence of assignments
581 * from constructor parameters to the temporary will follow.
584 * An \c ir_dereference_variable of the temprorary generated in the constructor
588 emit_inline_matrix_constructor(const glsl_type
*type
,
589 exec_list
*instructions
,
590 exec_list
*parameters
,
593 assert(!parameters
->is_empty());
595 ir_variable
*var
= new(ctx
) ir_variable(type
, strdup("mat_ctor"));
596 instructions
->push_tail(var
);
598 /* There are three kinds of matrix constructors.
600 * - Construct a matrix from a single scalar by replicating that scalar to
601 * along the diagonal of the matrix and setting all other components to
604 * - Construct a matrix from an arbirary combination of vectors and
605 * scalars. The components of the constructor parameters are assigned
606 * to the matrix in colum-major order until the matrix is full.
608 * - Construct a matrix from a single matrix. The source matrix is copied
609 * to the upper left portion of the constructed matrix, and the remaining
610 * elements take values from the identity matrix.
612 ir_rvalue
*const first_param
= (ir_rvalue
*) parameters
->head
;
613 if (single_scalar_parameter(parameters
)) {
614 /* Assign the scalar to the X component of a vec4, and fill the remaining
615 * components with zero.
617 ir_variable
*rhs_var
= new(ctx
) ir_variable(glsl_type::vec4_type
,
618 strdup("mat_ctor_vec"));
619 instructions
->push_tail(rhs_var
);
621 ir_constant_data zero
;
627 ir_instruction
*inst
=
628 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(rhs_var
),
629 new(ctx
) ir_constant(rhs_var
->type
, &zero
),
631 instructions
->push_tail(inst
);
633 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
634 ir_rvalue
*const x_of_rhs
= new(ctx
) ir_swizzle(rhs_ref
, 0, 0, 0, 0, 1);
636 inst
= new(ctx
) ir_assignment(x_of_rhs
, first_param
, NULL
);
637 instructions
->push_tail(inst
);
639 /* Assign the temporary vector to each column of the destination matrix
640 * with a swizzle that puts the X component on the diagonal of the
641 * matrix. In some cases this may mean that the X component does not
642 * get assigned into the column at all (i.e., when the matrix has more
643 * columns than rows).
645 static const unsigned rhs_swiz
[4][4] = {
652 const unsigned cols_to_init
= min(type
->matrix_columns
,
653 type
->vector_elements
);
654 for (unsigned i
= 0; i
< cols_to_init
; i
++) {
655 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
656 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
658 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
659 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, rhs_swiz
[i
],
660 type
->vector_elements
);
662 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
663 instructions
->push_tail(inst
);
666 for (unsigned i
= cols_to_init
; i
< type
->matrix_columns
; i
++) {
667 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
668 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
670 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
671 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, 1, 1, 1, 1,
672 type
->vector_elements
);
674 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
675 instructions
->push_tail(inst
);
677 } else if (first_param
->type
->is_matrix()) {
678 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
680 * "If a matrix is constructed from a matrix, then each component
681 * (column i, row j) in the result that has a corresponding
682 * component (column i, row j) in the argument will be initialized
683 * from there. All other components will be initialized to the
684 * identity matrix. If a matrix argument is given to a matrix
685 * constructor, it is an error to have any other arguments."
687 assert(first_param
->next
->is_tail_sentinal());
688 ir_rvalue
*const src_matrix
= first_param
;
690 /* If the source matrix is smaller, pre-initialize the relavent parts of
691 * the destination matrix to the identity matrix.
693 if ((src_matrix
->type
->matrix_columns
< var
->type
->matrix_columns
)
694 || (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)) {
696 /* If the source matrix has fewer rows, every column of the destination
697 * must be initialized. Otherwise only the columns in the destination
698 * that do not exist in the source must be initialized.
701 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)
702 ? 0 : src_matrix
->type
->matrix_columns
;
704 const glsl_type
*const col_type
= var
->type
->column_type();
705 for (/* empty */; col
< var
->type
->matrix_columns
; col
++) {
706 ir_constant_data ident
;
715 ir_rvalue
*const rhs
= new(ctx
) ir_constant(col_type
, &ident
);
717 ir_rvalue
*const lhs
=
718 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(col
));
720 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
721 instructions
->push_tail(inst
);
725 /* Assign columns from the source matrix to the destination matrix.
727 * Since the parameter will be used in the RHS of multiple assignments,
728 * generate a temporary and copy the paramter there.
730 ir_variable
*const rhs_var
= new(ctx
) ir_variable(first_param
->type
,
731 strdup("mat_ctor_mat"));
732 instructions
->push_tail(rhs_var
);
734 ir_dereference
*const rhs_var_ref
=
735 new(ctx
) ir_dereference_variable(rhs_var
);
736 ir_instruction
*const inst
=
737 new(ctx
) ir_assignment(rhs_var_ref
, first_param
, NULL
);
738 instructions
->push_tail(inst
);
741 const unsigned swiz
[4] = { 0, 1, 2, 3 };
742 const unsigned last_col
= min(src_matrix
->type
->matrix_columns
,
743 var
->type
->matrix_columns
);
744 for (unsigned i
= 0; i
< last_col
; i
++) {
745 ir_rvalue
*const lhs_col
=
746 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
747 ir_rvalue
*const rhs_col
=
748 new(ctx
) ir_dereference_array(rhs_var
, new(ctx
) ir_constant(i
));
750 /* If one matrix has columns that are smaller than the columns of the
751 * other matrix, wrap the column access of the larger with a swizzle
752 * so that the LHS and RHS of the assignment have the same size (and
753 * therefore have the same type).
755 * It would be perfectly valid to unconditionally generate the
756 * swizzles, this this will typically result in a more compact IR tree.
760 if (lhs_col
->type
->vector_elements
< rhs_col
->type
->vector_elements
) {
763 rhs
= new(ctx
) ir_swizzle(rhs_col
, swiz
,
764 lhs_col
->type
->vector_elements
);
765 } else if (lhs_col
->type
->vector_elements
766 > rhs_col
->type
->vector_elements
) {
767 lhs
= new(ctx
) ir_swizzle(lhs_col
, swiz
,
768 rhs_col
->type
->vector_elements
);
775 assert(lhs
->type
== rhs
->type
);
777 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
778 instructions
->push_tail(inst
);
781 const unsigned rows
= type
->matrix_columns
;
782 const unsigned cols
= type
->vector_elements
;
783 unsigned col_idx
= 0;
784 unsigned row_idx
= 0;
786 foreach_list (node
, parameters
) {
787 ir_rvalue
*const rhs
= (ir_rvalue
*) node
;
788 const unsigned components_remaining_this_column
= rows
- row_idx
;
789 unsigned rhs_components
= rhs
->type
->components();
790 unsigned rhs_base
= 0;
792 /* Since the parameter might be used in the RHS of two assignments,
793 * generate a temporary and copy the paramter there.
795 ir_variable
*rhs_var
= new(ctx
) ir_variable(rhs
->type
,
796 strdup("mat_ctor_vec"));
797 instructions
->push_tail(rhs_var
);
799 ir_dereference
*rhs_var_ref
=
800 new(ctx
) ir_dereference_variable(rhs_var
);
801 ir_instruction
*inst
= new(ctx
) ir_assignment(rhs_var_ref
, rhs
, NULL
);
802 instructions
->push_tail(inst
);
804 /* Assign the current parameter to as many components of the matrix
807 * NOTE: A single vector parameter can span two matrix columns. A
808 * single vec4, for example, can completely fill a mat2.
810 if (rhs_components
>= components_remaining_this_column
) {
811 const unsigned count
= min(rhs_components
,
812 components_remaining_this_column
);
814 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
816 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
820 instructions
->push_tail(inst
);
828 /* If there is data left in the parameter and components left to be
829 * set in the destination, emit another assignment. It is possible
830 * that the assignment could be of a vec4 to the last element of the
831 * matrix. In this case col_idx==cols, but there is still data
832 * left in the source parameter. Obviously, don't emit an assignment
833 * to data outside the destination matrix.
835 if ((col_idx
< cols
) && (rhs_base
< rhs_components
)) {
836 const unsigned count
= rhs_components
- rhs_base
;
838 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
840 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
845 instructions
->push_tail(inst
);
852 return new(ctx
) ir_dereference_variable(var
);
857 ast_function_expression::hir(exec_list
*instructions
,
858 struct _mesa_glsl_parse_state
*state
)
860 void *ctx
= talloc_parent(state
);
861 /* There are three sorts of function calls.
863 * 1. contstructors - The first subexpression is an ast_type_specifier.
864 * 2. methods - Only the .length() method of array types.
865 * 3. functions - Calls to regular old functions.
867 * Method calls are actually detected when the ast_field_selection
868 * expression is handled.
870 if (is_constructor()) {
871 const ast_type_specifier
*type
= (ast_type_specifier
*) subexpressions
[0];
872 YYLTYPE loc
= type
->get_location();
875 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
878 /* Constructors for samplers are illegal.
880 if (constructor_type
->is_sampler()) {
881 _mesa_glsl_error(& loc
, state
, "cannot construct sampler type `%s'",
882 constructor_type
->name
);
883 return ir_call::get_error_instruction(ctx
);
886 if (constructor_type
->is_array()) {
887 if (state
->language_version
<= 110) {
888 _mesa_glsl_error(& loc
, state
,
889 "array constructors forbidden in GLSL 1.10");
890 return ir_call::get_error_instruction(ctx
);
893 return process_array_constructor(instructions
, constructor_type
,
894 & loc
, &this->expressions
, state
);
897 /* There are two kinds of constructor call. Constructors for built-in
898 * language types, such as mat4 and vec2, are free form. The only
899 * requirement is that the parameters must provide enough values of the
900 * correct scalar type. Constructors for arrays and structures must
901 * have the exact number of parameters with matching types in the
902 * correct order. These constructors follow essentially the same type
903 * matching rules as functions.
905 if (constructor_type
->is_numeric() || constructor_type
->is_boolean()) {
906 /* Total number of components of the type being constructed.
908 const unsigned type_components
= constructor_type
->components();
910 /* Number of components from parameters that have actually been
911 * consumed. This is used to perform several kinds of error checking.
913 unsigned components_used
= 0;
915 unsigned matrix_parameters
= 0;
916 unsigned nonmatrix_parameters
= 0;
917 exec_list actual_parameters
;
919 bool all_parameters_are_constant
= true;
921 /* This handles invalid constructor calls such as 'vec4 v = vec4();'
923 if (this->expressions
.is_empty()) {
924 _mesa_glsl_error(& loc
, state
, "too few components to construct "
926 constructor_type
->name
);
927 return ir_call::get_error_instruction(ctx
);
930 foreach_list (n
, &this->expressions
) {
931 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
933 ast
->hir(instructions
, state
)->as_rvalue();
935 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
937 * "It is an error to provide extra arguments beyond this
938 * last used argument."
940 if (components_used
>= type_components
) {
941 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
943 constructor_type
->name
);
944 return ir_call::get_error_instruction(ctx
);
947 if (!result
->type
->is_numeric() && !result
->type
->is_boolean()) {
948 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
949 "non-numeric data type",
950 constructor_type
->name
);
951 return ir_call::get_error_instruction(ctx
);
954 /* Count the number of matrix and nonmatrix parameters. This
955 * is used below to enforce some of the constructor rules.
957 if (result
->type
->is_matrix())
960 nonmatrix_parameters
++;
962 /* Type cast the parameter and add it to the parameter list for
965 const glsl_type
*desired_type
=
966 glsl_type::get_instance(constructor_type
->base_type
,
967 result
->type
->vector_elements
,
968 result
->type
->matrix_columns
);
969 result
= convert_component(result
, desired_type
);
971 /* Attempt to convert the parameter to a constant valued expression.
972 * After doing so, track whether or not all the parameters to the
973 * constructor are trivially constant valued expressions.
975 ir_rvalue
*const constant
= result
->constant_expression_value();
977 if (constant
!= NULL
)
980 all_parameters_are_constant
= false;
982 actual_parameters
.push_tail(result
);
983 components_used
+= result
->type
->components();
986 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
988 * "It is an error to construct matrices from other matrices. This
989 * is reserved for future use."
991 if ((state
->language_version
<= 110) && (matrix_parameters
> 0)
992 && constructor_type
->is_matrix()) {
993 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
994 "matrix in GLSL 1.10",
995 constructor_type
->name
);
996 return ir_call::get_error_instruction(ctx
);
999 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1001 * "If a matrix argument is given to a matrix constructor, it is
1002 * an error to have any other arguments."
1004 if ((matrix_parameters
> 0)
1005 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
1006 && constructor_type
->is_matrix()) {
1007 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
1008 "matrix must be only parameter",
1009 constructor_type
->name
);
1010 return ir_call::get_error_instruction(ctx
);
1013 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1015 * "In these cases, there must be enough components provided in the
1016 * arguments to provide an initializer for every component in the
1017 * constructed value."
1019 if ((components_used
< type_components
) && (components_used
!= 1)) {
1020 _mesa_glsl_error(& loc
, state
, "too few components to construct "
1022 constructor_type
->name
);
1023 return ir_call::get_error_instruction(ctx
);
1027 /* If all of the parameters are trivially constant, create a
1028 * constant representing the complete collection of parameters.
1030 if (all_parameters_are_constant
) {
1031 if (components_used
>= type_components
)
1032 return new(ctx
) ir_constant(constructor_type
,
1033 & actual_parameters
);
1035 /* The above case must handle all scalar constructors.
1037 assert(constructor_type
->is_vector()
1038 || constructor_type
->is_matrix());
1040 /* Constructors with exactly one component are special for
1041 * vectors and matrices. For vectors it causes all elements of
1042 * the vector to be filled with the value. For matrices it
1043 * causes the matrix to be filled with 0 and the diagonal to be
1044 * filled with the value.
1046 ir_constant_data data
;
1047 ir_constant
*const initializer
=
1048 (ir_constant
*) actual_parameters
.head
;
1049 if (constructor_type
->is_matrix())
1050 generate_constructor_matrix(constructor_type
, initializer
,
1053 generate_constructor_vector(constructor_type
, initializer
,
1056 return new(ctx
) ir_constant(constructor_type
, &data
);
1057 } else if (constructor_type
->is_scalar()) {
1058 return dereference_component((ir_rvalue
*) actual_parameters
.head
,
1060 } else if (constructor_type
->is_vector()) {
1061 return emit_inline_vector_constructor(constructor_type
,
1066 assert(constructor_type
->is_matrix());
1067 return emit_inline_matrix_constructor(constructor_type
,
1074 return ir_call::get_error_instruction(ctx
);
1076 const ast_expression
*id
= subexpressions
[0];
1077 YYLTYPE loc
= id
->get_location();
1078 exec_list actual_parameters
;
1080 process_parameters(instructions
, &actual_parameters
, &this->expressions
,
1083 const glsl_type
*const type
=
1084 state
->symbols
->get_type(id
->primary_expression
.identifier
);
1086 if ((type
!= NULL
) && type
->is_record()) {
1087 ir_constant
*constant
=
1088 constant_record_constructor(type
, &loc
, &actual_parameters
, state
);
1090 if (constant
!= NULL
)
1094 return match_function_by_name(instructions
,
1095 id
->primary_expression
.identifier
, & loc
,
1096 &actual_parameters
, state
);
1099 return ir_call::get_error_instruction(ctx
);