2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 #include "glsl_symbol_table.h"
26 #include "glsl_types.h"
28 #include "main/core.h" /* for MIN2 */
31 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
);
34 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
35 struct _mesa_glsl_parse_state
*state
);
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 * Generate a source prototype for a function signature
63 * \param return_type Return type of the function. May be \c NULL.
64 * \param name Name of the function.
65 * \param parameters Parameter list for the function. This may be either a
66 * formal or actual parameter list. Only the type is used.
69 * A talloced string representing the prototype of the function.
72 prototype_string(const glsl_type
*return_type
, const char *name
,
73 exec_list
*parameters
)
77 if (return_type
!= NULL
)
78 str
= talloc_asprintf(str
, "%s ", return_type
->name
);
80 str
= talloc_asprintf_append(str
, "%s(", name
);
82 const char *comma
= "";
83 foreach_list(node
, parameters
) {
84 const ir_instruction
*const param
= (ir_instruction
*) node
;
86 str
= talloc_asprintf_append(str
, "%s%s", comma
, param
->type
->name
);
90 str
= talloc_strdup_append(str
, ")");
96 match_function_by_name(exec_list
*instructions
, const char *name
,
97 YYLTYPE
*loc
, exec_list
*actual_parameters
,
98 struct _mesa_glsl_parse_state
*state
)
101 ir_function
*f
= state
->symbols
->get_function(name
);
102 ir_function_signature
*sig
= NULL
;
104 /* FINISHME: This doesn't handle the case where shader X contains a
105 * FINISHME: matching signature but shader X + N contains an _exact_
106 * FINISHME: matching signature.
109 sig
= f
->matching_signature(actual_parameters
);
110 } else if (state
->symbols
->get_type(name
) == NULL
&& (state
->language_version
== 110 || state
->symbols
->get_variable(name
) == NULL
)) {
111 /* The current shader doesn't contain a matching function or signature.
112 * Before giving up, look for the prototype in the built-in functions.
114 for (unsigned i
= 0; i
< state
->num_builtins_to_link
; i
++) {
115 f
= state
->builtins_to_link
[i
]->symbols
->get_function(name
);
116 sig
= f
? f
->matching_signature(actual_parameters
) : NULL
;
118 f
= new(ctx
) ir_function(name
);
119 f
->add_signature(sig
->clone_prototype(f
, NULL
));
126 /* Verify that 'out' and 'inout' actual parameters are lvalues. This
127 * isn't done in ir_function::matching_signature because that function
128 * cannot generate the necessary diagnostics.
130 exec_list_iterator actual_iter
= actual_parameters
->iterator();
131 exec_list_iterator formal_iter
= sig
->parameters
.iterator();
133 while (actual_iter
.has_next()) {
134 ir_rvalue
*actual
= (ir_rvalue
*) actual_iter
.get();
135 ir_variable
*formal
= (ir_variable
*) formal_iter
.get();
137 assert(actual
!= NULL
);
138 assert(formal
!= NULL
);
140 if ((formal
->mode
== ir_var_out
)
141 || (formal
->mode
== ir_var_inout
)) {
142 if (! actual
->is_lvalue()) {
143 /* FINISHME: Log a better diagnostic here. There is no way
144 * FINISHME: to tell the user which parameter is invalid.
146 _mesa_glsl_error(loc
, state
, "`%s' parameter is not lvalue",
147 (formal
->mode
== ir_var_out
) ? "out" : "inout");
151 if (formal
->type
->is_numeric() || formal
->type
->is_boolean()) {
152 ir_rvalue
*converted
= convert_component(actual
, formal
->type
);
153 actual
->replace_with(converted
);
160 /* Always insert the call in the instruction stream, and return a deref
161 * of its return val if it returns a value, since we don't know if
162 * the rvalue is going to be assigned to anything or not.
164 ir_call
*call
= new(ctx
) ir_call(sig
, actual_parameters
);
165 if (!sig
->return_type
->is_void()) {
167 ir_dereference_variable
*deref
;
169 var
= new(ctx
) ir_variable(sig
->return_type
,
170 talloc_asprintf(ctx
, "%s_retval",
171 sig
->function_name()),
173 instructions
->push_tail(var
);
175 deref
= new(ctx
) ir_dereference_variable(var
);
176 ir_assignment
*assign
= new(ctx
) ir_assignment(deref
, call
, NULL
);
177 instructions
->push_tail(assign
);
178 if (state
->language_version
>= 120)
179 var
->constant_value
= call
->constant_expression_value();
181 deref
= new(ctx
) ir_dereference_variable(var
);
184 instructions
->push_tail(call
);
188 char *str
= prototype_string(NULL
, name
, actual_parameters
);
190 _mesa_glsl_error(loc
, state
, "no matching function for call to `%s'",
194 const char *prefix
= "candidates are: ";
196 for (int i
= -1; i
< state
->num_builtins_to_link
; i
++) {
197 glsl_symbol_table
*syms
= i
>= 0 ? state
->builtins_to_link
[i
]->symbols
199 f
= syms
->get_function(name
);
203 foreach_list (node
, &f
->signatures
) {
204 ir_function_signature
*sig
= (ir_function_signature
*) node
;
206 str
= prototype_string(sig
->return_type
, f
->name
, &sig
->parameters
);
207 _mesa_glsl_error(loc
, state
, "%s%s\n", prefix
, str
);
215 return ir_call::get_error_instruction(ctx
);
221 * Perform automatic type conversion of constructor parameters
223 * This implements the rules in the "Conversion and Scalar Constructors"
224 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
227 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
)
229 void *ctx
= talloc_parent(src
);
230 const unsigned a
= desired_type
->base_type
;
231 const unsigned b
= src
->type
->base_type
;
232 ir_expression
*result
= NULL
;
234 if (src
->type
->is_error())
237 assert(a
<= GLSL_TYPE_BOOL
);
238 assert(b
<= GLSL_TYPE_BOOL
);
240 if ((a
== b
) || (src
->type
->is_integer() && desired_type
->is_integer()))
246 if (b
== GLSL_TYPE_FLOAT
)
247 result
= new(ctx
) ir_expression(ir_unop_f2i
, desired_type
, src
, NULL
);
249 assert(b
== GLSL_TYPE_BOOL
);
250 result
= new(ctx
) ir_expression(ir_unop_b2i
, desired_type
, src
, NULL
);
253 case GLSL_TYPE_FLOAT
:
256 result
= new(ctx
) ir_expression(ir_unop_u2f
, desired_type
, src
, NULL
);
259 result
= new(ctx
) ir_expression(ir_unop_i2f
, desired_type
, src
, NULL
);
262 result
= new(ctx
) ir_expression(ir_unop_b2f
, desired_type
, src
, NULL
);
270 result
= new(ctx
) ir_expression(ir_unop_i2b
, desired_type
, src
, NULL
);
272 case GLSL_TYPE_FLOAT
:
273 result
= new(ctx
) ir_expression(ir_unop_f2b
, desired_type
, src
, NULL
);
279 assert(result
!= NULL
);
281 /* Try constant folding; it may fold in the conversion we just added. */
282 ir_constant
*const constant
= result
->constant_expression_value();
283 return (constant
!= NULL
) ? (ir_rvalue
*) constant
: (ir_rvalue
*) result
;
287 * Dereference a specific component from a scalar, vector, or matrix
290 dereference_component(ir_rvalue
*src
, unsigned component
)
292 void *ctx
= talloc_parent(src
);
293 assert(component
< src
->type
->components());
295 /* If the source is a constant, just create a new constant instead of a
296 * dereference of the existing constant.
298 ir_constant
*constant
= src
->as_constant();
300 return new(ctx
) ir_constant(constant
, component
);
302 if (src
->type
->is_scalar()) {
304 } else if (src
->type
->is_vector()) {
305 return new(ctx
) ir_swizzle(src
, component
, 0, 0, 0, 1);
307 assert(src
->type
->is_matrix());
309 /* Dereference a row of the matrix, then call this function again to get
310 * a specific element from that row.
312 const int c
= component
/ src
->type
->column_type()->vector_elements
;
313 const int r
= component
% src
->type
->column_type()->vector_elements
;
314 ir_constant
*const col_index
= new(ctx
) ir_constant(c
);
315 ir_dereference
*const col
= new(ctx
) ir_dereference_array(src
, col_index
);
317 col
->type
= src
->type
->column_type();
319 return dereference_component(col
, r
);
322 assert(!"Should not get here.");
328 process_array_constructor(exec_list
*instructions
,
329 const glsl_type
*constructor_type
,
330 YYLTYPE
*loc
, exec_list
*parameters
,
331 struct _mesa_glsl_parse_state
*state
)
334 /* Array constructors come in two forms: sized and unsized. Sized array
335 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
336 * variables. In this case the number of parameters must exactly match the
337 * specified size of the array.
339 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
340 * are vec4 variables. In this case the size of the array being constructed
341 * is determined by the number of parameters.
343 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
345 * "There must be exactly the same number of arguments as the size of
346 * the array being constructed. If no size is present in the
347 * constructor, then the array is explicitly sized to the number of
348 * arguments provided. The arguments are assigned in order, starting at
349 * element 0, to the elements of the constructed array. Each argument
350 * must be the same type as the element type of the array, or be a type
351 * that can be converted to the element type of the array according to
352 * Section 4.1.10 "Implicit Conversions.""
354 exec_list actual_parameters
;
355 const unsigned parameter_count
=
356 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
358 if ((parameter_count
== 0)
359 || ((constructor_type
->length
!= 0)
360 && (constructor_type
->length
!= parameter_count
))) {
361 const unsigned min_param
= (constructor_type
->length
== 0)
362 ? 1 : constructor_type
->length
;
364 _mesa_glsl_error(loc
, state
, "array constructor must have %s %u "
366 (constructor_type
->length
!= 0) ? "at least" : "exactly",
367 min_param
, (min_param
<= 1) ? "" : "s");
368 return ir_call::get_error_instruction(ctx
);
371 if (constructor_type
->length
== 0) {
373 glsl_type::get_array_instance(constructor_type
->element_type(),
375 assert(constructor_type
!= NULL
);
376 assert(constructor_type
->length
== parameter_count
);
379 bool all_parameters_are_constant
= true;
381 /* Type cast each parameter and, if possible, fold constants. */
382 foreach_list_safe(n
, &actual_parameters
) {
383 ir_rvalue
*ir
= (ir_rvalue
*) n
;
384 ir_rvalue
*result
= ir
;
386 /* Apply implicit conversions (not the scalar constructor rules!) */
387 if (constructor_type
->element_type()->is_float()) {
388 const glsl_type
*desired_type
=
389 glsl_type::get_instance(GLSL_TYPE_FLOAT
,
390 ir
->type
->vector_elements
,
391 ir
->type
->matrix_columns
);
392 result
= convert_component(ir
, desired_type
);
395 if (result
->type
!= constructor_type
->element_type()) {
396 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
397 "expected: %s, found %s",
398 constructor_type
->element_type()->name
,
402 /* Attempt to convert the parameter to a constant valued expression.
403 * After doing so, track whether or not all the parameters to the
404 * constructor are trivially constant valued expressions.
406 ir_rvalue
*const constant
= result
->constant_expression_value();
408 if (constant
!= NULL
)
411 all_parameters_are_constant
= false;
413 ir
->replace_with(result
);
416 if (all_parameters_are_constant
)
417 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
419 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "array_ctor",
421 instructions
->push_tail(var
);
424 foreach_list(node
, &actual_parameters
) {
425 ir_rvalue
*rhs
= (ir_rvalue
*) node
;
426 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
427 new(ctx
) ir_constant(i
));
429 ir_instruction
*assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
430 instructions
->push_tail(assignment
);
435 return new(ctx
) ir_dereference_variable(var
);
440 * Try to convert a record constructor to a constant expression
443 constant_record_constructor(const glsl_type
*constructor_type
,
444 exec_list
*parameters
, void *mem_ctx
)
446 foreach_list(node
, parameters
) {
447 ir_constant
*constant
= ((ir_instruction
*) node
)->as_constant();
448 if (constant
== NULL
)
450 node
->replace_with(constant
);
453 return new(mem_ctx
) ir_constant(constructor_type
, parameters
);
458 * Determine if a list consists of a single scalar r-value
461 single_scalar_parameter(exec_list
*parameters
)
463 const ir_rvalue
*const p
= (ir_rvalue
*) parameters
->head
;
464 assert(((ir_rvalue
*)p
)->as_rvalue() != NULL
);
466 return (p
->type
->is_scalar() && p
->next
->is_tail_sentinel());
471 * Generate inline code for a vector constructor
473 * The generated constructor code will consist of a temporary variable
474 * declaration of the same type as the constructor. A sequence of assignments
475 * from constructor parameters to the temporary will follow.
478 * An \c ir_dereference_variable of the temprorary generated in the constructor
482 emit_inline_vector_constructor(const glsl_type
*type
,
483 exec_list
*instructions
,
484 exec_list
*parameters
,
487 assert(!parameters
->is_empty());
489 ir_variable
*var
= new(ctx
) ir_variable(type
, "vec_ctor", ir_var_temporary
);
490 instructions
->push_tail(var
);
492 /* There are two kinds of vector constructors.
494 * - Construct a vector from a single scalar by replicating that scalar to
495 * all components of the vector.
497 * - Construct a vector from an arbirary combination of vectors and
498 * scalars. The components of the constructor parameters are assigned
499 * to the vector in order until the vector is full.
501 const unsigned lhs_components
= type
->components();
502 if (single_scalar_parameter(parameters
)) {
503 ir_rvalue
*first_param
= (ir_rvalue
*)parameters
->head
;
504 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(first_param
, 0, 0, 0, 0,
506 ir_dereference_variable
*lhs
= new(ctx
) ir_dereference_variable(var
);
507 const unsigned mask
= (1U << lhs_components
) - 1;
509 assert(rhs
->type
== lhs
->type
);
511 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
, mask
);
512 instructions
->push_tail(inst
);
514 unsigned base_component
= 0;
515 unsigned base_lhs_component
= 0;
516 ir_constant_data data
;
517 unsigned constant_mask
= 0, constant_components
= 0;
519 memset(&data
, 0, sizeof(data
));
521 foreach_list(node
, parameters
) {
522 ir_rvalue
*param
= (ir_rvalue
*) node
;
523 unsigned rhs_components
= param
->type
->components();
525 /* Do not try to assign more components to the vector than it has!
527 if ((rhs_components
+ base_lhs_component
) > lhs_components
) {
528 rhs_components
= lhs_components
- base_lhs_component
;
531 const ir_constant
*const c
= param
->as_constant();
533 for (unsigned i
= 0; i
< rhs_components
; i
++) {
534 switch (c
->type
->base_type
) {
536 data
.u
[i
+ base_component
] = c
->get_uint_component(i
);
539 data
.i
[i
+ base_component
] = c
->get_int_component(i
);
541 case GLSL_TYPE_FLOAT
:
542 data
.f
[i
+ base_component
] = c
->get_float_component(i
);
545 data
.b
[i
+ base_component
] = c
->get_bool_component(i
);
548 assert(!"Should not get here.");
553 /* Mask of fields to be written in the assignment.
555 constant_mask
|= ((1U << rhs_components
) - 1) << base_lhs_component
;
556 constant_components
+= rhs_components
;
558 base_component
+= rhs_components
;
560 /* Advance the component index by the number of components
561 * that were just assigned.
563 base_lhs_component
+= rhs_components
;
566 if (constant_mask
!= 0) {
567 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
568 const glsl_type
*rhs_type
= glsl_type::get_instance(var
->type
->base_type
,
571 ir_rvalue
*rhs
= new(ctx
) ir_constant(rhs_type
, &data
);
573 ir_instruction
*inst
=
574 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, constant_mask
);
575 instructions
->push_tail(inst
);
579 foreach_list(node
, parameters
) {
580 ir_rvalue
*param
= (ir_rvalue
*) node
;
581 unsigned rhs_components
= param
->type
->components();
583 /* Do not try to assign more components to the vector than it has!
585 if ((rhs_components
+ base_component
) > lhs_components
) {
586 rhs_components
= lhs_components
- base_component
;
589 const ir_constant
*const c
= param
->as_constant();
591 /* Mask of fields to be written in the assignment.
593 const unsigned write_mask
= ((1U << rhs_components
) - 1)
596 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
598 /* Generate a swizzle so that LHS and RHS sizes match.
601 new(ctx
) ir_swizzle(param
, 0, 1, 2, 3, rhs_components
);
603 ir_instruction
*inst
=
604 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
605 instructions
->push_tail(inst
);
608 /* Advance the component index by the number of components that were
611 base_component
+= rhs_components
;
614 return new(ctx
) ir_dereference_variable(var
);
619 * Generate assignment of a portion of a vector to a portion of a matrix column
621 * \param src_base First component of the source to be used in assignment
622 * \param column Column of destination to be assiged
623 * \param row_base First component of the destination column to be assigned
624 * \param count Number of components to be assigned
627 * \c src_base + \c count must be less than or equal to the number of components
628 * in the source vector.
631 assign_to_matrix_column(ir_variable
*var
, unsigned column
, unsigned row_base
,
632 ir_rvalue
*src
, unsigned src_base
, unsigned count
,
635 ir_constant
*col_idx
= new(mem_ctx
) ir_constant(column
);
636 ir_dereference
*column_ref
= new(mem_ctx
) ir_dereference_array(var
, col_idx
);
638 assert(column_ref
->type
->components() >= (row_base
+ count
));
639 assert(src
->type
->components() >= (src_base
+ count
));
641 /* Generate a swizzle that extracts the number of components from the source
642 * that are to be assigned to the column of the matrix.
644 if (count
< src
->type
->vector_elements
) {
645 src
= new(mem_ctx
) ir_swizzle(src
,
646 src_base
+ 0, src_base
+ 1,
647 src_base
+ 2, src_base
+ 3,
651 /* Mask of fields to be written in the assignment.
653 const unsigned write_mask
= ((1U << count
) - 1) << row_base
;
655 return new(mem_ctx
) ir_assignment(column_ref
, src
, NULL
, write_mask
);
660 * Generate inline code for a matrix constructor
662 * The generated constructor code will consist of a temporary variable
663 * declaration of the same type as the constructor. A sequence of assignments
664 * from constructor parameters to the temporary will follow.
667 * An \c ir_dereference_variable of the temprorary generated in the constructor
671 emit_inline_matrix_constructor(const glsl_type
*type
,
672 exec_list
*instructions
,
673 exec_list
*parameters
,
676 assert(!parameters
->is_empty());
678 ir_variable
*var
= new(ctx
) ir_variable(type
, "mat_ctor", ir_var_temporary
);
679 instructions
->push_tail(var
);
681 /* There are three kinds of matrix constructors.
683 * - Construct a matrix from a single scalar by replicating that scalar to
684 * along the diagonal of the matrix and setting all other components to
687 * - Construct a matrix from an arbirary combination of vectors and
688 * scalars. The components of the constructor parameters are assigned
689 * to the matrix in colum-major order until the matrix is full.
691 * - Construct a matrix from a single matrix. The source matrix is copied
692 * to the upper left portion of the constructed matrix, and the remaining
693 * elements take values from the identity matrix.
695 ir_rvalue
*const first_param
= (ir_rvalue
*) parameters
->head
;
696 if (single_scalar_parameter(parameters
)) {
697 /* Assign the scalar to the X component of a vec4, and fill the remaining
698 * components with zero.
700 ir_variable
*rhs_var
=
701 new(ctx
) ir_variable(glsl_type::vec4_type
, "mat_ctor_vec",
703 instructions
->push_tail(rhs_var
);
705 ir_constant_data zero
;
711 ir_instruction
*inst
=
712 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(rhs_var
),
713 new(ctx
) ir_constant(rhs_var
->type
, &zero
),
715 instructions
->push_tail(inst
);
717 ir_dereference
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
719 inst
= new(ctx
) ir_assignment(rhs_ref
, first_param
, NULL
, 0x01);
720 instructions
->push_tail(inst
);
722 /* Assign the temporary vector to each column of the destination matrix
723 * with a swizzle that puts the X component on the diagonal of the
724 * matrix. In some cases this may mean that the X component does not
725 * get assigned into the column at all (i.e., when the matrix has more
726 * columns than rows).
728 static const unsigned rhs_swiz
[4][4] = {
735 const unsigned cols_to_init
= MIN2(type
->matrix_columns
,
736 type
->vector_elements
);
737 for (unsigned i
= 0; i
< cols_to_init
; i
++) {
738 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
739 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
741 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
742 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, rhs_swiz
[i
],
743 type
->vector_elements
);
745 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
746 instructions
->push_tail(inst
);
749 for (unsigned i
= cols_to_init
; i
< type
->matrix_columns
; i
++) {
750 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
751 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
753 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
754 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, 1, 1, 1, 1,
755 type
->vector_elements
);
757 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
758 instructions
->push_tail(inst
);
760 } else if (first_param
->type
->is_matrix()) {
761 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
763 * "If a matrix is constructed from a matrix, then each component
764 * (column i, row j) in the result that has a corresponding
765 * component (column i, row j) in the argument will be initialized
766 * from there. All other components will be initialized to the
767 * identity matrix. If a matrix argument is given to a matrix
768 * constructor, it is an error to have any other arguments."
770 assert(first_param
->next
->is_tail_sentinel());
771 ir_rvalue
*const src_matrix
= first_param
;
773 /* If the source matrix is smaller, pre-initialize the relavent parts of
774 * the destination matrix to the identity matrix.
776 if ((src_matrix
->type
->matrix_columns
< var
->type
->matrix_columns
)
777 || (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)) {
779 /* If the source matrix has fewer rows, every column of the destination
780 * must be initialized. Otherwise only the columns in the destination
781 * that do not exist in the source must be initialized.
784 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)
785 ? 0 : src_matrix
->type
->matrix_columns
;
787 const glsl_type
*const col_type
= var
->type
->column_type();
788 for (/* empty */; col
< var
->type
->matrix_columns
; col
++) {
789 ir_constant_data ident
;
798 ir_rvalue
*const rhs
= new(ctx
) ir_constant(col_type
, &ident
);
800 ir_rvalue
*const lhs
=
801 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(col
));
803 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
804 instructions
->push_tail(inst
);
808 /* Assign columns from the source matrix to the destination matrix.
810 * Since the parameter will be used in the RHS of multiple assignments,
811 * generate a temporary and copy the paramter there.
813 ir_variable
*const rhs_var
=
814 new(ctx
) ir_variable(first_param
->type
, "mat_ctor_mat",
816 instructions
->push_tail(rhs_var
);
818 ir_dereference
*const rhs_var_ref
=
819 new(ctx
) ir_dereference_variable(rhs_var
);
820 ir_instruction
*const inst
=
821 new(ctx
) ir_assignment(rhs_var_ref
, first_param
, NULL
);
822 instructions
->push_tail(inst
);
824 const unsigned last_row
= MIN2(src_matrix
->type
->vector_elements
,
825 var
->type
->vector_elements
);
826 const unsigned last_col
= MIN2(src_matrix
->type
->matrix_columns
,
827 var
->type
->matrix_columns
);
829 unsigned swiz
[4] = { 0, 0, 0, 0 };
830 for (unsigned i
= 1; i
< last_row
; i
++)
833 const unsigned write_mask
= (1U << last_row
) - 1;
835 for (unsigned i
= 0; i
< last_col
; i
++) {
836 ir_dereference
*const lhs
=
837 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
838 ir_rvalue
*const rhs_col
=
839 new(ctx
) ir_dereference_array(rhs_var
, new(ctx
) ir_constant(i
));
841 /* If one matrix has columns that are smaller than the columns of the
842 * other matrix, wrap the column access of the larger with a swizzle
843 * so that the LHS and RHS of the assignment have the same size (and
844 * therefore have the same type).
846 * It would be perfectly valid to unconditionally generate the
847 * swizzles, this this will typically result in a more compact IR tree.
850 if (lhs
->type
->vector_elements
!= rhs_col
->type
->vector_elements
) {
851 rhs
= new(ctx
) ir_swizzle(rhs_col
, swiz
, last_row
);
856 ir_instruction
*inst
=
857 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
858 instructions
->push_tail(inst
);
861 const unsigned cols
= type
->matrix_columns
;
862 const unsigned rows
= type
->vector_elements
;
863 unsigned col_idx
= 0;
864 unsigned row_idx
= 0;
866 foreach_list (node
, parameters
) {
867 ir_rvalue
*const rhs
= (ir_rvalue
*) node
;
868 const unsigned components_remaining_this_column
= rows
- row_idx
;
869 unsigned rhs_components
= rhs
->type
->components();
870 unsigned rhs_base
= 0;
872 /* Since the parameter might be used in the RHS of two assignments,
873 * generate a temporary and copy the paramter there.
875 ir_variable
*rhs_var
=
876 new(ctx
) ir_variable(rhs
->type
, "mat_ctor_vec", ir_var_temporary
);
877 instructions
->push_tail(rhs_var
);
879 ir_dereference
*rhs_var_ref
=
880 new(ctx
) ir_dereference_variable(rhs_var
);
881 ir_instruction
*inst
= new(ctx
) ir_assignment(rhs_var_ref
, rhs
, NULL
);
882 instructions
->push_tail(inst
);
884 /* Assign the current parameter to as many components of the matrix
887 * NOTE: A single vector parameter can span two matrix columns. A
888 * single vec4, for example, can completely fill a mat2.
890 if (rhs_components
>= components_remaining_this_column
) {
891 const unsigned count
= MIN2(rhs_components
,
892 components_remaining_this_column
);
894 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
896 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
900 instructions
->push_tail(inst
);
908 /* If there is data left in the parameter and components left to be
909 * set in the destination, emit another assignment. It is possible
910 * that the assignment could be of a vec4 to the last element of the
911 * matrix. In this case col_idx==cols, but there is still data
912 * left in the source parameter. Obviously, don't emit an assignment
913 * to data outside the destination matrix.
915 if ((col_idx
< cols
) && (rhs_base
< rhs_components
)) {
916 const unsigned count
= rhs_components
- rhs_base
;
918 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
920 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
925 instructions
->push_tail(inst
);
932 return new(ctx
) ir_dereference_variable(var
);
937 emit_inline_record_constructor(const glsl_type
*type
,
938 exec_list
*instructions
,
939 exec_list
*parameters
,
942 ir_variable
*const var
=
943 new(mem_ctx
) ir_variable(type
, "record_ctor", ir_var_temporary
);
944 ir_dereference_variable
*const d
= new(mem_ctx
) ir_dereference_variable(var
);
946 instructions
->push_tail(var
);
948 exec_node
*node
= parameters
->head
;
949 for (unsigned i
= 0; i
< type
->length
; i
++) {
950 assert(!node
->is_tail_sentinel());
952 ir_dereference
*const lhs
=
953 new(mem_ctx
) ir_dereference_record(d
->clone(mem_ctx
, NULL
),
954 type
->fields
.structure
[i
].name
);
956 ir_rvalue
*const rhs
= ((ir_instruction
*) node
)->as_rvalue();
959 ir_instruction
*const assign
= new(mem_ctx
) ir_assignment(lhs
, rhs
, NULL
);
961 instructions
->push_tail(assign
);
970 ast_function_expression::hir(exec_list
*instructions
,
971 struct _mesa_glsl_parse_state
*state
)
974 /* There are three sorts of function calls.
976 * 1. constructors - The first subexpression is an ast_type_specifier.
977 * 2. methods - Only the .length() method of array types.
978 * 3. functions - Calls to regular old functions.
980 * Method calls are actually detected when the ast_field_selection
981 * expression is handled.
983 if (is_constructor()) {
984 const ast_type_specifier
*type
= (ast_type_specifier
*) subexpressions
[0];
985 YYLTYPE loc
= type
->get_location();
988 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
991 /* Constructors for samplers are illegal.
993 if (constructor_type
->is_sampler()) {
994 _mesa_glsl_error(& loc
, state
, "cannot construct sampler type `%s'",
995 constructor_type
->name
);
996 return ir_call::get_error_instruction(ctx
);
999 if (constructor_type
->is_array()) {
1000 if (state
->language_version
<= 110) {
1001 _mesa_glsl_error(& loc
, state
,
1002 "array constructors forbidden in GLSL 1.10");
1003 return ir_call::get_error_instruction(ctx
);
1006 return process_array_constructor(instructions
, constructor_type
,
1007 & loc
, &this->expressions
, state
);
1011 /* There are two kinds of constructor call. Constructors for built-in
1012 * language types, such as mat4 and vec2, are free form. The only
1013 * requirement is that the parameters must provide enough values of the
1014 * correct scalar type. Constructors for arrays and structures must
1015 * have the exact number of parameters with matching types in the
1016 * correct order. These constructors follow essentially the same type
1017 * matching rules as functions.
1019 if (!constructor_type
->is_numeric() && !constructor_type
->is_boolean())
1020 return ir_call::get_error_instruction(ctx
);
1022 /* Total number of components of the type being constructed. */
1023 const unsigned type_components
= constructor_type
->components();
1025 /* Number of components from parameters that have actually been
1026 * consumed. This is used to perform several kinds of error checking.
1028 unsigned components_used
= 0;
1030 unsigned matrix_parameters
= 0;
1031 unsigned nonmatrix_parameters
= 0;
1032 exec_list actual_parameters
;
1034 foreach_list (n
, &this->expressions
) {
1035 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
1036 ir_rvalue
*result
= ast
->hir(instructions
, state
)->as_rvalue();
1038 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1040 * "It is an error to provide extra arguments beyond this
1041 * last used argument."
1043 if (components_used
>= type_components
) {
1044 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
1046 constructor_type
->name
);
1047 return ir_call::get_error_instruction(ctx
);
1050 if (!result
->type
->is_numeric() && !result
->type
->is_boolean()) {
1051 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
1052 "non-numeric data type",
1053 constructor_type
->name
);
1054 return ir_call::get_error_instruction(ctx
);
1057 /* Count the number of matrix and nonmatrix parameters. This
1058 * is used below to enforce some of the constructor rules.
1060 if (result
->type
->is_matrix())
1061 matrix_parameters
++;
1063 nonmatrix_parameters
++;
1065 actual_parameters
.push_tail(result
);
1066 components_used
+= result
->type
->components();
1069 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1071 * "It is an error to construct matrices from other matrices. This
1072 * is reserved for future use."
1074 if (state
->language_version
== 110 && matrix_parameters
> 0
1075 && constructor_type
->is_matrix()) {
1076 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
1077 "matrix in GLSL 1.10",
1078 constructor_type
->name
);
1079 return ir_call::get_error_instruction(ctx
);
1082 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1084 * "If a matrix argument is given to a matrix constructor, it is
1085 * an error to have any other arguments."
1087 if ((matrix_parameters
> 0)
1088 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
1089 && constructor_type
->is_matrix()) {
1090 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
1091 "matrix must be only parameter",
1092 constructor_type
->name
);
1093 return ir_call::get_error_instruction(ctx
);
1096 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1098 * "In these cases, there must be enough components provided in the
1099 * arguments to provide an initializer for every component in the
1100 * constructed value."
1102 if (components_used
< type_components
&& components_used
!= 1
1103 && matrix_parameters
== 0) {
1104 _mesa_glsl_error(& loc
, state
, "too few components to construct "
1106 constructor_type
->name
);
1107 return ir_call::get_error_instruction(ctx
);
1110 /* Later, we cast each parameter to the same base type as the
1111 * constructor. Since there are no non-floating point matrices, we
1112 * need to break them up into a series of column vectors.
1114 if (constructor_type
->base_type
!= GLSL_TYPE_FLOAT
) {
1115 foreach_list_safe(n
, &actual_parameters
) {
1116 ir_rvalue
*matrix
= (ir_rvalue
*) n
;
1118 if (!matrix
->type
->is_matrix())
1121 /* Create a temporary containing the matrix. */
1122 ir_variable
*var
= new(ctx
) ir_variable(matrix
->type
, "matrix_tmp",
1124 instructions
->push_tail(var
);
1125 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
)
1126 ir_dereference_variable(var
), matrix
, NULL
));
1127 var
->constant_value
= matrix
->constant_expression_value();
1129 /* Replace the matrix with dereferences of its columns. */
1130 for (int i
= 0; i
< matrix
->type
->matrix_columns
; i
++) {
1131 matrix
->insert_before(new (ctx
) ir_dereference_array(var
,
1132 new(ctx
) ir_constant(i
)));
1138 bool all_parameters_are_constant
= true;
1140 /* Type cast each parameter and, if possible, fold constants.*/
1141 foreach_list_safe(n
, &actual_parameters
) {
1142 ir_rvalue
*ir
= (ir_rvalue
*) n
;
1144 const glsl_type
*desired_type
=
1145 glsl_type::get_instance(constructor_type
->base_type
,
1146 ir
->type
->vector_elements
,
1147 ir
->type
->matrix_columns
);
1148 ir_rvalue
*result
= convert_component(ir
, desired_type
);
1150 /* Attempt to convert the parameter to a constant valued expression.
1151 * After doing so, track whether or not all the parameters to the
1152 * constructor are trivially constant valued expressions.
1154 ir_rvalue
*const constant
= result
->constant_expression_value();
1156 if (constant
!= NULL
)
1159 all_parameters_are_constant
= false;
1162 ir
->replace_with(result
);
1166 /* If all of the parameters are trivially constant, create a
1167 * constant representing the complete collection of parameters.
1169 if (all_parameters_are_constant
) {
1170 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1171 } else if (constructor_type
->is_scalar()) {
1172 return dereference_component((ir_rvalue
*) actual_parameters
.head
,
1174 } else if (constructor_type
->is_vector()) {
1175 return emit_inline_vector_constructor(constructor_type
,
1180 assert(constructor_type
->is_matrix());
1181 return emit_inline_matrix_constructor(constructor_type
,
1187 const ast_expression
*id
= subexpressions
[0];
1188 YYLTYPE loc
= id
->get_location();
1189 exec_list actual_parameters
;
1191 process_parameters(instructions
, &actual_parameters
, &this->expressions
,
1194 const glsl_type
*const type
=
1195 state
->symbols
->get_type(id
->primary_expression
.identifier
);
1197 if ((type
!= NULL
) && type
->is_record()) {
1198 exec_node
*node
= actual_parameters
.head
;
1199 for (unsigned i
= 0; i
< type
->length
; i
++) {
1200 ir_rvalue
*ir
= (ir_rvalue
*) node
;
1202 if (node
->is_tail_sentinel()) {
1203 _mesa_glsl_error(&loc
, state
,
1204 "insufficient parameters to constructor "
1207 return ir_call::get_error_instruction(ctx
);
1210 if (apply_implicit_conversion(type
->fields
.structure
[i
].type
, ir
,
1212 node
->replace_with(ir
);
1214 _mesa_glsl_error(&loc
, state
,
1215 "parameter type mismatch in constructor "
1216 "for `%s.%s' (%s vs %s)",
1218 type
->fields
.structure
[i
].name
,
1220 type
->fields
.structure
[i
].type
->name
);
1221 return ir_call::get_error_instruction(ctx
);;
1227 if (!node
->is_tail_sentinel()) {
1228 _mesa_glsl_error(&loc
, state
, "too many parameters in constructor "
1229 "for `%s'", type
->name
);
1230 return ir_call::get_error_instruction(ctx
);
1233 ir_rvalue
*const constant
=
1234 constant_record_constructor(type
, &actual_parameters
, state
);
1236 return (constant
!= NULL
)
1238 : emit_inline_record_constructor(type
, instructions
,
1239 &actual_parameters
, state
);
1242 return match_function_by_name(instructions
,
1243 id
->primary_expression
.identifier
, & loc
,
1244 &actual_parameters
, state
);
1247 return ir_call::get_error_instruction(ctx
);