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 ir_function
*f
= state
->symbols
->get_function(constructor_type
->name
);
326 /* If the constructor for this type of array does not exist, generate the
327 * prototype and add it to the symbol table.
330 f
= constructor_type
->generate_constructor(state
->symbols
);
334 process_call(instructions
, f
, loc
, &actual_parameters
, state
);
337 assert(r
->type
->is_error() || (r
->type
== constructor_type
));
344 * Try to convert a record constructor to a constant expression
347 constant_record_constructor(const glsl_type
*constructor_type
,
348 YYLTYPE
*loc
, exec_list
*parameters
,
349 struct _mesa_glsl_parse_state
*state
)
352 bool all_parameters_are_constant
= true;
354 exec_node
*node
= parameters
->head
;
355 for (unsigned i
= 0; i
< constructor_type
->length
; i
++) {
356 ir_instruction
*ir
= (ir_instruction
*) node
;
358 if (node
->is_tail_sentinal()) {
359 _mesa_glsl_error(loc
, state
,
360 "insufficient parameters to constructor for `%s'",
361 constructor_type
->name
);
365 if (ir
->type
!= constructor_type
->fields
.structure
[i
].type
) {
366 _mesa_glsl_error(loc
, state
,
367 "parameter type mismatch in constructor for `%s' "
369 constructor_type
->name
,
371 constructor_type
->fields
.structure
[i
].type
->name
);
375 if (ir
->as_constant() == NULL
)
376 all_parameters_are_constant
= false;
381 if (!all_parameters_are_constant
)
384 return new(ctx
) ir_constant(constructor_type
, parameters
);
389 * Generate data for a constant matrix constructor w/a single scalar parameter
391 * Matrix constructors in GLSL can be passed a single scalar of the
392 * approriate type. In these cases, the resulting matrix is the identity
393 * matrix multipled by the specified scalar. This function generates data for
396 * \param type Type of the desired matrix.
397 * \param initializer Scalar value used to initialize the matrix diagonal.
398 * \param data Location to store the resulting matrix.
401 generate_constructor_matrix(const glsl_type
*type
, ir_constant
*initializer
,
402 ir_constant_data
*data
)
404 switch (type
->base_type
) {
407 for (unsigned i
= 0; i
< type
->components(); i
++)
410 for (unsigned i
= 0; i
< type
->matrix_columns
; i
++) {
411 /* The array offset of the ith row and column of the matrix.
413 const unsigned idx
= (i
* type
->vector_elements
) + i
;
415 data
->u
[idx
] = initializer
->value
.u
[0];
419 case GLSL_TYPE_FLOAT
:
420 for (unsigned i
= 0; i
< type
->components(); i
++)
423 for (unsigned i
= 0; i
< type
->matrix_columns
; i
++) {
424 /* The array offset of the ith row and column of the matrix.
426 const unsigned idx
= (i
* type
->vector_elements
) + i
;
428 data
->f
[idx
] = initializer
->value
.f
[0];
434 assert(!"Should not get here.");
441 * Generate data for a constant vector constructor w/a single scalar parameter
443 * Vector constructors in GLSL can be passed a single scalar of the
444 * approriate type. In these cases, the resulting vector contains the specified
445 * value in all components. This function generates data for that vector.
447 * \param type Type of the desired vector.
448 * \param initializer Scalar value used to initialize the vector.
449 * \param data Location to store the resulting vector data.
452 generate_constructor_vector(const glsl_type
*type
, ir_constant
*initializer
,
453 ir_constant_data
*data
)
455 switch (type
->base_type
) {
458 for (unsigned i
= 0; i
< type
->components(); i
++)
459 data
->u
[i
] = initializer
->value
.u
[0];
463 case GLSL_TYPE_FLOAT
:
464 for (unsigned i
= 0; i
< type
->components(); i
++)
465 data
->f
[i
] = initializer
->value
.f
[0];
470 for (unsigned i
= 0; i
< type
->components(); i
++)
471 data
->b
[i
] = initializer
->value
.b
[0];
476 assert(!"Should not get here.");
483 * Determine if a list consists of a single scalar r-value
486 single_scalar_parameter(exec_list
*parameters
)
488 const ir_rvalue
*const p
= (ir_rvalue
*) parameters
->head
;
489 assert(((ir_rvalue
*)p
)->as_rvalue() != NULL
);
491 return (p
->type
->is_scalar() && p
->next
->is_tail_sentinal());
496 * Generate inline code for a vector constructor
498 * The generated constructor code will consist of a temporary variable
499 * declaration of the same type as the constructor. A sequence of assignments
500 * from constructor parameters to the temporary will follow.
503 * An \c ir_dereference_variable of the temprorary generated in the constructor
507 emit_inline_vector_constructor(const glsl_type
*type
,
508 exec_list
*instructions
,
509 exec_list
*parameters
,
512 assert(!parameters
->is_empty());
514 ir_variable
*var
= new(ctx
) ir_variable(type
,
515 talloc_strdup(ctx
, "vec_ctor"),
517 instructions
->push_tail(var
);
519 /* There are two kinds of vector constructors.
521 * - Construct a vector from a single scalar by replicating that scalar to
522 * all components of the vector.
524 * - Construct a vector from an arbirary combination of vectors and
525 * scalars. The components of the constructor parameters are assigned
526 * to the vector in order until the vector is full.
528 const unsigned lhs_components
= type
->components();
529 if (single_scalar_parameter(parameters
)) {
530 ir_rvalue
*first_param
= (ir_rvalue
*)parameters
->head
;
531 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(first_param
, 0, 0, 0, 0,
533 ir_dereference_variable
*lhs
= new(ctx
) ir_dereference_variable(var
);
535 assert(rhs
->type
== lhs
->type
);
537 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
538 instructions
->push_tail(inst
);
540 unsigned base_component
= 0;
541 foreach_list(node
, parameters
) {
542 ir_rvalue
*rhs
= (ir_rvalue
*) node
;
543 unsigned rhs_components
= rhs
->type
->components();
545 /* Do not try to assign more components to the vector than it has!
547 if ((rhs_components
+ base_component
) > lhs_components
) {
548 rhs_components
= lhs_components
- base_component
;
551 /* Emit an assignment of the constructor parameter to the next set of
552 * components in the temporary variable.
554 unsigned mask
[4] = { 0, 0, 0, 0 };
555 for (unsigned i
= 0; i
< rhs_components
; i
++) {
556 mask
[i
] = i
+ base_component
;
560 ir_rvalue
*lhs_ref
= new(ctx
) ir_dereference_variable(var
);
561 ir_swizzle
*lhs
= new(ctx
) ir_swizzle(lhs_ref
, mask
, rhs_components
);
563 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
564 instructions
->push_tail(inst
);
566 /* Advance the component index by the number of components that were
569 base_component
+= rhs_components
;
572 return new(ctx
) ir_dereference_variable(var
);
577 * Generate assignment of a portion of a vector to a portion of a matrix column
579 * \param src_base First component of the source to be used in assignment
580 * \param column Column of destination to be assiged
581 * \param row_base First component of the destination column to be assigned
582 * \param count Number of components to be assigned
585 * \c src_base + \c count must be less than or equal to the number of components
586 * in the source vector.
589 assign_to_matrix_column(ir_variable
*var
, unsigned column
, unsigned row_base
,
590 ir_rvalue
*src
, unsigned src_base
, unsigned count
,
593 const unsigned mask
[8] = { 0, 1, 2, 3, 0, 0, 0, 0 };
595 ir_constant
*col_idx
= new(ctx
) ir_constant(column
);
596 ir_rvalue
*column_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
598 assert(column_ref
->type
->components() >= (row_base
+ count
));
599 ir_rvalue
*lhs
= new(ctx
) ir_swizzle(column_ref
, &mask
[row_base
], count
);
601 assert(src
->type
->components() >= (src_base
+ count
));
602 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(src
, &mask
[src_base
], count
);
604 return new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
609 * Generate inline code for a matrix constructor
611 * The generated constructor code will consist of a temporary variable
612 * declaration of the same type as the constructor. A sequence of assignments
613 * from constructor parameters to the temporary will follow.
616 * An \c ir_dereference_variable of the temprorary generated in the constructor
620 emit_inline_matrix_constructor(const glsl_type
*type
,
621 exec_list
*instructions
,
622 exec_list
*parameters
,
625 assert(!parameters
->is_empty());
627 ir_variable
*var
= new(ctx
) ir_variable(type
,
628 talloc_strdup(ctx
, "mat_ctor"),
630 instructions
->push_tail(var
);
632 /* There are three kinds of matrix constructors.
634 * - Construct a matrix from a single scalar by replicating that scalar to
635 * along the diagonal of the matrix and setting all other components to
638 * - Construct a matrix from an arbirary combination of vectors and
639 * scalars. The components of the constructor parameters are assigned
640 * to the matrix in colum-major order until the matrix is full.
642 * - Construct a matrix from a single matrix. The source matrix is copied
643 * to the upper left portion of the constructed matrix, and the remaining
644 * elements take values from the identity matrix.
646 ir_rvalue
*const first_param
= (ir_rvalue
*) parameters
->head
;
647 if (single_scalar_parameter(parameters
)) {
648 /* Assign the scalar to the X component of a vec4, and fill the remaining
649 * components with zero.
651 ir_variable
*rhs_var
=
652 new(ctx
) ir_variable(glsl_type::vec4_type
,
653 talloc_strdup(ctx
, "mat_ctor_vec"),
655 instructions
->push_tail(rhs_var
);
657 ir_constant_data zero
;
663 ir_instruction
*inst
=
664 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(rhs_var
),
665 new(ctx
) ir_constant(rhs_var
->type
, &zero
),
667 instructions
->push_tail(inst
);
669 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
670 ir_rvalue
*const x_of_rhs
= new(ctx
) ir_swizzle(rhs_ref
, 0, 0, 0, 0, 1);
672 inst
= new(ctx
) ir_assignment(x_of_rhs
, first_param
, NULL
);
673 instructions
->push_tail(inst
);
675 /* Assign the temporary vector to each column of the destination matrix
676 * with a swizzle that puts the X component on the diagonal of the
677 * matrix. In some cases this may mean that the X component does not
678 * get assigned into the column at all (i.e., when the matrix has more
679 * columns than rows).
681 static const unsigned rhs_swiz
[4][4] = {
688 const unsigned cols_to_init
= min(type
->matrix_columns
,
689 type
->vector_elements
);
690 for (unsigned i
= 0; i
< cols_to_init
; i
++) {
691 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
692 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
694 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
695 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, rhs_swiz
[i
],
696 type
->vector_elements
);
698 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
699 instructions
->push_tail(inst
);
702 for (unsigned i
= cols_to_init
; i
< type
->matrix_columns
; i
++) {
703 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
704 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
706 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
707 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, 1, 1, 1, 1,
708 type
->vector_elements
);
710 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
711 instructions
->push_tail(inst
);
713 } else if (first_param
->type
->is_matrix()) {
714 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
716 * "If a matrix is constructed from a matrix, then each component
717 * (column i, row j) in the result that has a corresponding
718 * component (column i, row j) in the argument will be initialized
719 * from there. All other components will be initialized to the
720 * identity matrix. If a matrix argument is given to a matrix
721 * constructor, it is an error to have any other arguments."
723 assert(first_param
->next
->is_tail_sentinal());
724 ir_rvalue
*const src_matrix
= first_param
;
726 /* If the source matrix is smaller, pre-initialize the relavent parts of
727 * the destination matrix to the identity matrix.
729 if ((src_matrix
->type
->matrix_columns
< var
->type
->matrix_columns
)
730 || (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)) {
732 /* If the source matrix has fewer rows, every column of the destination
733 * must be initialized. Otherwise only the columns in the destination
734 * that do not exist in the source must be initialized.
737 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)
738 ? 0 : src_matrix
->type
->matrix_columns
;
740 const glsl_type
*const col_type
= var
->type
->column_type();
741 for (/* empty */; col
< var
->type
->matrix_columns
; col
++) {
742 ir_constant_data ident
;
751 ir_rvalue
*const rhs
= new(ctx
) ir_constant(col_type
, &ident
);
753 ir_rvalue
*const lhs
=
754 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(col
));
756 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
757 instructions
->push_tail(inst
);
761 /* Assign columns from the source matrix to the destination matrix.
763 * Since the parameter will be used in the RHS of multiple assignments,
764 * generate a temporary and copy the paramter there.
766 ir_variable
*const rhs_var
=
767 new(ctx
) ir_variable(first_param
->type
,
768 talloc_strdup(ctx
, "mat_ctor_mat"),
770 instructions
->push_tail(rhs_var
);
772 ir_dereference
*const rhs_var_ref
=
773 new(ctx
) ir_dereference_variable(rhs_var
);
774 ir_instruction
*const inst
=
775 new(ctx
) ir_assignment(rhs_var_ref
, first_param
, NULL
);
776 instructions
->push_tail(inst
);
779 const unsigned swiz
[4] = { 0, 1, 2, 3 };
780 const unsigned last_col
= min(src_matrix
->type
->matrix_columns
,
781 var
->type
->matrix_columns
);
782 for (unsigned i
= 0; i
< last_col
; i
++) {
783 ir_rvalue
*const lhs_col
=
784 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
785 ir_rvalue
*const rhs_col
=
786 new(ctx
) ir_dereference_array(rhs_var
, new(ctx
) ir_constant(i
));
788 /* If one matrix has columns that are smaller than the columns of the
789 * other matrix, wrap the column access of the larger with a swizzle
790 * so that the LHS and RHS of the assignment have the same size (and
791 * therefore have the same type).
793 * It would be perfectly valid to unconditionally generate the
794 * swizzles, this this will typically result in a more compact IR tree.
798 if (lhs_col
->type
->vector_elements
< rhs_col
->type
->vector_elements
) {
801 rhs
= new(ctx
) ir_swizzle(rhs_col
, swiz
,
802 lhs_col
->type
->vector_elements
);
803 } else if (lhs_col
->type
->vector_elements
804 > rhs_col
->type
->vector_elements
) {
805 lhs
= new(ctx
) ir_swizzle(lhs_col
, swiz
,
806 rhs_col
->type
->vector_elements
);
813 assert(lhs
->type
== rhs
->type
);
815 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
816 instructions
->push_tail(inst
);
819 const unsigned rows
= type
->matrix_columns
;
820 const unsigned cols
= type
->vector_elements
;
821 unsigned col_idx
= 0;
822 unsigned row_idx
= 0;
824 foreach_list (node
, parameters
) {
825 ir_rvalue
*const rhs
= (ir_rvalue
*) node
;
826 const unsigned components_remaining_this_column
= rows
- row_idx
;
827 unsigned rhs_components
= rhs
->type
->components();
828 unsigned rhs_base
= 0;
830 /* Since the parameter might be used in the RHS of two assignments,
831 * generate a temporary and copy the paramter there.
833 ir_variable
*rhs_var
=
834 new(ctx
) ir_variable(rhs
->type
,
835 talloc_strdup(ctx
, "mat_ctor_vec"),
837 instructions
->push_tail(rhs_var
);
839 ir_dereference
*rhs_var_ref
=
840 new(ctx
) ir_dereference_variable(rhs_var
);
841 ir_instruction
*inst
= new(ctx
) ir_assignment(rhs_var_ref
, rhs
, NULL
);
842 instructions
->push_tail(inst
);
844 /* Assign the current parameter to as many components of the matrix
847 * NOTE: A single vector parameter can span two matrix columns. A
848 * single vec4, for example, can completely fill a mat2.
850 if (rhs_components
>= components_remaining_this_column
) {
851 const unsigned count
= min(rhs_components
,
852 components_remaining_this_column
);
854 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
856 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
860 instructions
->push_tail(inst
);
868 /* If there is data left in the parameter and components left to be
869 * set in the destination, emit another assignment. It is possible
870 * that the assignment could be of a vec4 to the last element of the
871 * matrix. In this case col_idx==cols, but there is still data
872 * left in the source parameter. Obviously, don't emit an assignment
873 * to data outside the destination matrix.
875 if ((col_idx
< cols
) && (rhs_base
< rhs_components
)) {
876 const unsigned count
= rhs_components
- rhs_base
;
878 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
880 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
885 instructions
->push_tail(inst
);
892 return new(ctx
) ir_dereference_variable(var
);
897 ast_function_expression::hir(exec_list
*instructions
,
898 struct _mesa_glsl_parse_state
*state
)
901 /* There are three sorts of function calls.
903 * 1. constructors - The first subexpression is an ast_type_specifier.
904 * 2. methods - Only the .length() method of array types.
905 * 3. functions - Calls to regular old functions.
907 * Method calls are actually detected when the ast_field_selection
908 * expression is handled.
910 if (is_constructor()) {
911 const ast_type_specifier
*type
= (ast_type_specifier
*) subexpressions
[0];
912 YYLTYPE loc
= type
->get_location();
915 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
918 /* Constructors for samplers are illegal.
920 if (constructor_type
->is_sampler()) {
921 _mesa_glsl_error(& loc
, state
, "cannot construct sampler type `%s'",
922 constructor_type
->name
);
923 return ir_call::get_error_instruction(ctx
);
926 if (constructor_type
->is_array()) {
927 if (state
->language_version
<= 110) {
928 _mesa_glsl_error(& loc
, state
,
929 "array constructors forbidden in GLSL 1.10");
930 return ir_call::get_error_instruction(ctx
);
933 return process_array_constructor(instructions
, constructor_type
,
934 & loc
, &this->expressions
, state
);
937 /* There are two kinds of constructor call. Constructors for built-in
938 * language types, such as mat4 and vec2, are free form. The only
939 * requirement is that the parameters must provide enough values of the
940 * correct scalar type. Constructors for arrays and structures must
941 * have the exact number of parameters with matching types in the
942 * correct order. These constructors follow essentially the same type
943 * matching rules as functions.
945 if (!constructor_type
->is_numeric() && !constructor_type
->is_boolean())
946 return ir_call::get_error_instruction(ctx
);
948 /* Total number of components of the type being constructed. */
949 const unsigned type_components
= constructor_type
->components();
951 /* Number of components from parameters that have actually been
952 * consumed. This is used to perform several kinds of error checking.
954 unsigned components_used
= 0;
956 unsigned matrix_parameters
= 0;
957 unsigned nonmatrix_parameters
= 0;
958 exec_list actual_parameters
;
960 foreach_list (n
, &this->expressions
) {
961 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
962 ir_rvalue
*result
= ast
->hir(instructions
, state
)->as_rvalue();
964 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
966 * "It is an error to provide extra arguments beyond this
967 * last used argument."
969 if (components_used
>= type_components
) {
970 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
972 constructor_type
->name
);
973 return ir_call::get_error_instruction(ctx
);
976 if (!result
->type
->is_numeric() && !result
->type
->is_boolean()) {
977 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
978 "non-numeric data type",
979 constructor_type
->name
);
980 return ir_call::get_error_instruction(ctx
);
983 /* Count the number of matrix and nonmatrix parameters. This
984 * is used below to enforce some of the constructor rules.
986 if (result
->type
->is_matrix())
989 nonmatrix_parameters
++;
991 actual_parameters
.push_tail(result
);
992 components_used
+= result
->type
->components();
995 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
997 * "It is an error to construct matrices from other matrices. This
998 * is reserved for future use."
1000 if ((state
->language_version
<= 110) && (matrix_parameters
> 0)
1001 && constructor_type
->is_matrix()) {
1002 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
1003 "matrix in GLSL 1.10",
1004 constructor_type
->name
);
1005 return ir_call::get_error_instruction(ctx
);
1008 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1010 * "If a matrix argument is given to a matrix constructor, it is
1011 * an error to have any other arguments."
1013 if ((matrix_parameters
> 0)
1014 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
1015 && constructor_type
->is_matrix()) {
1016 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
1017 "matrix must be only parameter",
1018 constructor_type
->name
);
1019 return ir_call::get_error_instruction(ctx
);
1022 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1024 * "In these cases, there must be enough components provided in the
1025 * arguments to provide an initializer for every component in the
1026 * constructed value."
1028 if ((components_used
< type_components
) && (components_used
!= 1)) {
1029 _mesa_glsl_error(& loc
, state
, "too few components to construct "
1031 constructor_type
->name
);
1032 return ir_call::get_error_instruction(ctx
);
1035 /* Later, we cast each parameter to the same base type as the
1036 * constructor. Since there are no non-floating point matrices, we
1037 * need to break them up into a series of column vectors.
1039 if (constructor_type
->base_type
!= GLSL_TYPE_FLOAT
) {
1040 foreach_list_safe(n
, &actual_parameters
) {
1041 ir_rvalue
*matrix
= (ir_rvalue
*) n
;
1043 if (!matrix
->type
->is_matrix())
1046 /* Create a temporary containing the matrix. */
1047 ir_variable
*var
= new(ctx
) ir_variable(matrix
->type
, "matrix_tmp",
1049 instructions
->push_tail(var
);
1050 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
)
1051 ir_dereference_variable(var
), matrix
, NULL
));
1052 var
->constant_value
= matrix
->constant_expression_value();
1054 /* Replace the matrix with dereferences of its columns. */
1055 for (int i
= 0; i
< matrix
->type
->matrix_columns
; i
++) {
1056 matrix
->insert_before(new (ctx
) ir_dereference_array(var
,
1057 new(ctx
) ir_constant(i
)));
1063 bool all_parameters_are_constant
= true;
1065 /* Type cast each parameter and, if possible, fold constants.*/
1066 foreach_list_safe(n
, &actual_parameters
) {
1067 ir_rvalue
*ir
= (ir_rvalue
*) n
;
1069 const glsl_type
*desired_type
=
1070 glsl_type::get_instance(constructor_type
->base_type
,
1071 ir
->type
->vector_elements
,
1072 ir
->type
->matrix_columns
);
1073 ir_rvalue
*result
= convert_component(ir
, desired_type
);
1075 /* Attempt to convert the parameter to a constant valued expression.
1076 * After doing so, track whether or not all the parameters to the
1077 * constructor are trivially constant valued expressions.
1079 ir_rvalue
*const constant
= result
->constant_expression_value();
1081 if (constant
!= NULL
)
1084 all_parameters_are_constant
= false;
1087 ir
->replace_with(result
);
1091 /* If all of the parameters are trivially constant, create a
1092 * constant representing the complete collection of parameters.
1094 if (all_parameters_are_constant
) {
1095 if (components_used
>= type_components
)
1096 return new(ctx
) ir_constant(constructor_type
,
1097 & actual_parameters
);
1099 /* The above case must handle all scalar constructors.
1101 assert(constructor_type
->is_vector()
1102 || constructor_type
->is_matrix());
1104 /* Constructors with exactly one component are special for
1105 * vectors and matrices. For vectors it causes all elements of
1106 * the vector to be filled with the value. For matrices it
1107 * causes the matrix to be filled with 0 and the diagonal to be
1108 * filled with the value.
1110 ir_constant_data data
;
1111 ir_constant
*const initializer
=
1112 (ir_constant
*) actual_parameters
.head
;
1113 if (constructor_type
->is_matrix())
1114 generate_constructor_matrix(constructor_type
, initializer
,
1117 generate_constructor_vector(constructor_type
, initializer
,
1120 return new(ctx
) ir_constant(constructor_type
, &data
);
1121 } else if (constructor_type
->is_scalar()) {
1122 return dereference_component((ir_rvalue
*) actual_parameters
.head
,
1124 } else if (constructor_type
->is_vector()) {
1125 return emit_inline_vector_constructor(constructor_type
,
1130 assert(constructor_type
->is_matrix());
1131 return emit_inline_matrix_constructor(constructor_type
,
1137 const ast_expression
*id
= subexpressions
[0];
1138 YYLTYPE loc
= id
->get_location();
1139 exec_list actual_parameters
;
1141 process_parameters(instructions
, &actual_parameters
, &this->expressions
,
1144 const glsl_type
*const type
=
1145 state
->symbols
->get_type(id
->primary_expression
.identifier
);
1147 if ((type
!= NULL
) && type
->is_record()) {
1148 ir_constant
*constant
=
1149 constant_record_constructor(type
, &loc
, &actual_parameters
, state
);
1151 if (constant
!= NULL
)
1155 return match_function_by_name(instructions
,
1156 id
->primary_expression
.identifier
, & loc
,
1157 &actual_parameters
, state
);
1160 return ir_call::get_error_instruction(ctx
);