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 ralloced 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
= ralloc_asprintf(NULL
, "%s ", return_type
->name
);
80 ralloc_asprintf_append(&str
, "%s(", name
);
82 const char *comma
= "";
83 foreach_list(node
, parameters
) {
84 const ir_instruction
*const param
= (ir_instruction
*) node
;
86 ralloc_asprintf_append(&str
, "%s%s", comma
, param
->type
->name
);
90 ralloc_strcat(&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
;
104 sig
= f
? f
->matching_signature(actual_parameters
) : NULL
;
106 /* FINISHME: This doesn't handle the case where shader X contains a
107 * FINISHME: matching signature but shader X + N contains an _exact_
108 * FINISHME: matching signature.
110 if (sig
== NULL
&& (f
== NULL
|| state
->es_shader
|| !f
->has_user_signature()) && 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 ir_function
*builtin
;
116 builtin
= state
->builtins_to_link
[i
]->symbols
->get_function(name
);
117 sig
= builtin
? builtin
->matching_signature(actual_parameters
) : NULL
;
120 f
= new(ctx
) ir_function(name
);
121 state
->symbols
->add_global_function(f
);
122 emit_function(state
, instructions
, f
);
125 f
->add_signature(sig
->clone_prototype(f
, NULL
));
132 /* Verify that 'out' and 'inout' actual parameters are lvalues. This
133 * isn't done in ir_function::matching_signature because that function
134 * cannot generate the necessary diagnostics.
136 * Also, validate that 'const_in' formal parameters (an extension of our
137 * IR) correspond to ir_constant actual parameters.
139 exec_list_iterator actual_iter
= actual_parameters
->iterator();
140 exec_list_iterator formal_iter
= sig
->parameters
.iterator();
142 while (actual_iter
.has_next()) {
143 ir_rvalue
*actual
= (ir_rvalue
*) actual_iter
.get();
144 ir_variable
*formal
= (ir_variable
*) formal_iter
.get();
146 assert(actual
!= NULL
);
147 assert(formal
!= NULL
);
149 if (formal
->mode
== ir_var_const_in
&& !actual
->as_constant()) {
150 _mesa_glsl_error(loc
, state
,
151 "parameter `%s' must be a constant expression",
155 if ((formal
->mode
== ir_var_out
)
156 || (formal
->mode
== ir_var_inout
)) {
157 const char *mode
= NULL
;
158 switch (formal
->mode
) {
159 case ir_var_out
: mode
= "out"; break;
160 case ir_var_inout
: mode
= "inout"; break;
161 default: assert(false); break;
163 /* FIXME: 'loc' is incorrect (as of 2011-01-21). It is always
166 if (actual
->variable_referenced()
167 && actual
->variable_referenced()->read_only
) {
168 _mesa_glsl_error(loc
, state
,
169 "function parameter '%s %s' references the "
170 "read-only variable '%s'",
172 actual
->variable_referenced()->name
);
174 } else if (!actual
->is_lvalue()) {
175 _mesa_glsl_error(loc
, state
,
176 "function parameter '%s %s' is not an lvalue",
181 if (formal
->type
->is_numeric() || formal
->type
->is_boolean()) {
182 ir_rvalue
*converted
= convert_component(actual
, formal
->type
);
183 actual
->replace_with(converted
);
190 /* Always insert the call in the instruction stream, and return a deref
191 * of its return val if it returns a value, since we don't know if
192 * the rvalue is going to be assigned to anything or not.
194 ir_call
*call
= new(ctx
) ir_call(sig
, actual_parameters
);
195 if (!sig
->return_type
->is_void()) {
197 ir_dereference_variable
*deref
;
199 var
= new(ctx
) ir_variable(sig
->return_type
,
200 ralloc_asprintf(ctx
, "%s_retval",
201 sig
->function_name()),
203 instructions
->push_tail(var
);
205 deref
= new(ctx
) ir_dereference_variable(var
);
206 ir_assignment
*assign
= new(ctx
) ir_assignment(deref
, call
, NULL
);
207 instructions
->push_tail(assign
);
208 if (state
->language_version
>= 120)
209 var
->constant_value
= call
->constant_expression_value();
211 deref
= new(ctx
) ir_dereference_variable(var
);
214 instructions
->push_tail(call
);
218 char *str
= prototype_string(NULL
, name
, actual_parameters
);
220 _mesa_glsl_error(loc
, state
, "no matching function for call to `%s'",
224 const char *prefix
= "candidates are: ";
226 for (int i
= -1; i
< (int) state
->num_builtins_to_link
; i
++) {
227 glsl_symbol_table
*syms
= i
>= 0 ? state
->builtins_to_link
[i
]->symbols
229 f
= syms
->get_function(name
);
233 foreach_list (node
, &f
->signatures
) {
234 ir_function_signature
*sig
= (ir_function_signature
*) node
;
236 str
= prototype_string(sig
->return_type
, f
->name
, &sig
->parameters
);
237 _mesa_glsl_error(loc
, state
, "%s%s\n", prefix
, str
);
245 return ir_call::get_error_instruction(ctx
);
251 * Perform automatic type conversion of constructor parameters
253 * This implements the rules in the "Conversion and Scalar Constructors"
254 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
257 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
)
259 void *ctx
= ralloc_parent(src
);
260 const unsigned a
= desired_type
->base_type
;
261 const unsigned b
= src
->type
->base_type
;
262 ir_expression
*result
= NULL
;
264 if (src
->type
->is_error())
267 assert(a
<= GLSL_TYPE_BOOL
);
268 assert(b
<= GLSL_TYPE_BOOL
);
270 if ((a
== b
) || (src
->type
->is_integer() && desired_type
->is_integer()))
276 if (b
== GLSL_TYPE_FLOAT
)
277 result
= new(ctx
) ir_expression(ir_unop_f2i
, desired_type
, src
, NULL
);
279 assert(b
== GLSL_TYPE_BOOL
);
280 result
= new(ctx
) ir_expression(ir_unop_b2i
, desired_type
, src
, NULL
);
283 case GLSL_TYPE_FLOAT
:
286 result
= new(ctx
) ir_expression(ir_unop_u2f
, desired_type
, src
, NULL
);
289 result
= new(ctx
) ir_expression(ir_unop_i2f
, desired_type
, src
, NULL
);
292 result
= new(ctx
) ir_expression(ir_unop_b2f
, desired_type
, src
, NULL
);
300 result
= new(ctx
) ir_expression(ir_unop_i2b
, desired_type
, src
, NULL
);
302 case GLSL_TYPE_FLOAT
:
303 result
= new(ctx
) ir_expression(ir_unop_f2b
, desired_type
, src
, NULL
);
309 assert(result
!= NULL
);
311 /* Try constant folding; it may fold in the conversion we just added. */
312 ir_constant
*const constant
= result
->constant_expression_value();
313 return (constant
!= NULL
) ? (ir_rvalue
*) constant
: (ir_rvalue
*) result
;
317 * Dereference a specific component from a scalar, vector, or matrix
320 dereference_component(ir_rvalue
*src
, unsigned component
)
322 void *ctx
= ralloc_parent(src
);
323 assert(component
< src
->type
->components());
325 /* If the source is a constant, just create a new constant instead of a
326 * dereference of the existing constant.
328 ir_constant
*constant
= src
->as_constant();
330 return new(ctx
) ir_constant(constant
, component
);
332 if (src
->type
->is_scalar()) {
334 } else if (src
->type
->is_vector()) {
335 return new(ctx
) ir_swizzle(src
, component
, 0, 0, 0, 1);
337 assert(src
->type
->is_matrix());
339 /* Dereference a row of the matrix, then call this function again to get
340 * a specific element from that row.
342 const int c
= component
/ src
->type
->column_type()->vector_elements
;
343 const int r
= component
% src
->type
->column_type()->vector_elements
;
344 ir_constant
*const col_index
= new(ctx
) ir_constant(c
);
345 ir_dereference
*const col
= new(ctx
) ir_dereference_array(src
, col_index
);
347 col
->type
= src
->type
->column_type();
349 return dereference_component(col
, r
);
352 assert(!"Should not get here.");
358 process_array_constructor(exec_list
*instructions
,
359 const glsl_type
*constructor_type
,
360 YYLTYPE
*loc
, exec_list
*parameters
,
361 struct _mesa_glsl_parse_state
*state
)
364 /* Array constructors come in two forms: sized and unsized. Sized array
365 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
366 * variables. In this case the number of parameters must exactly match the
367 * specified size of the array.
369 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
370 * are vec4 variables. In this case the size of the array being constructed
371 * is determined by the number of parameters.
373 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
375 * "There must be exactly the same number of arguments as the size of
376 * the array being constructed. If no size is present in the
377 * constructor, then the array is explicitly sized to the number of
378 * arguments provided. The arguments are assigned in order, starting at
379 * element 0, to the elements of the constructed array. Each argument
380 * must be the same type as the element type of the array, or be a type
381 * that can be converted to the element type of the array according to
382 * Section 4.1.10 "Implicit Conversions.""
384 exec_list actual_parameters
;
385 const unsigned parameter_count
=
386 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
388 if ((parameter_count
== 0)
389 || ((constructor_type
->length
!= 0)
390 && (constructor_type
->length
!= parameter_count
))) {
391 const unsigned min_param
= (constructor_type
->length
== 0)
392 ? 1 : constructor_type
->length
;
394 _mesa_glsl_error(loc
, state
, "array constructor must have %s %u "
396 (constructor_type
->length
!= 0) ? "at least" : "exactly",
397 min_param
, (min_param
<= 1) ? "" : "s");
398 return ir_call::get_error_instruction(ctx
);
401 if (constructor_type
->length
== 0) {
403 glsl_type::get_array_instance(constructor_type
->element_type(),
405 assert(constructor_type
!= NULL
);
406 assert(constructor_type
->length
== parameter_count
);
409 bool all_parameters_are_constant
= true;
411 /* Type cast each parameter and, if possible, fold constants. */
412 foreach_list_safe(n
, &actual_parameters
) {
413 ir_rvalue
*ir
= (ir_rvalue
*) n
;
414 ir_rvalue
*result
= ir
;
416 /* Apply implicit conversions (not the scalar constructor rules!) */
417 if (constructor_type
->element_type()->is_float()) {
418 const glsl_type
*desired_type
=
419 glsl_type::get_instance(GLSL_TYPE_FLOAT
,
420 ir
->type
->vector_elements
,
421 ir
->type
->matrix_columns
);
422 result
= convert_component(ir
, desired_type
);
425 if (result
->type
!= constructor_type
->element_type()) {
426 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
427 "expected: %s, found %s",
428 constructor_type
->element_type()->name
,
432 /* Attempt to convert the parameter to a constant valued expression.
433 * After doing so, track whether or not all the parameters to the
434 * constructor are trivially constant valued expressions.
436 ir_rvalue
*const constant
= result
->constant_expression_value();
438 if (constant
!= NULL
)
441 all_parameters_are_constant
= false;
443 ir
->replace_with(result
);
446 if (all_parameters_are_constant
)
447 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
449 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "array_ctor",
451 instructions
->push_tail(var
);
454 foreach_list(node
, &actual_parameters
) {
455 ir_rvalue
*rhs
= (ir_rvalue
*) node
;
456 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
457 new(ctx
) ir_constant(i
));
459 ir_instruction
*assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
460 instructions
->push_tail(assignment
);
465 return new(ctx
) ir_dereference_variable(var
);
470 * Try to convert a record constructor to a constant expression
473 constant_record_constructor(const glsl_type
*constructor_type
,
474 exec_list
*parameters
, void *mem_ctx
)
476 foreach_list(node
, parameters
) {
477 ir_constant
*constant
= ((ir_instruction
*) node
)->as_constant();
478 if (constant
== NULL
)
480 node
->replace_with(constant
);
483 return new(mem_ctx
) ir_constant(constructor_type
, parameters
);
488 * Determine if a list consists of a single scalar r-value
491 single_scalar_parameter(exec_list
*parameters
)
493 const ir_rvalue
*const p
= (ir_rvalue
*) parameters
->head
;
494 assert(((ir_rvalue
*)p
)->as_rvalue() != NULL
);
496 return (p
->type
->is_scalar() && p
->next
->is_tail_sentinel());
501 * Generate inline code for a vector constructor
503 * The generated constructor code will consist of a temporary variable
504 * declaration of the same type as the constructor. A sequence of assignments
505 * from constructor parameters to the temporary will follow.
508 * An \c ir_dereference_variable of the temprorary generated in the constructor
512 emit_inline_vector_constructor(const glsl_type
*type
,
513 exec_list
*instructions
,
514 exec_list
*parameters
,
517 assert(!parameters
->is_empty());
519 ir_variable
*var
= new(ctx
) ir_variable(type
, "vec_ctor", ir_var_temporary
);
520 instructions
->push_tail(var
);
522 /* There are two kinds of vector constructors.
524 * - Construct a vector from a single scalar by replicating that scalar to
525 * all components of the vector.
527 * - Construct a vector from an arbirary combination of vectors and
528 * scalars. The components of the constructor parameters are assigned
529 * to the vector in order until the vector is full.
531 const unsigned lhs_components
= type
->components();
532 if (single_scalar_parameter(parameters
)) {
533 ir_rvalue
*first_param
= (ir_rvalue
*)parameters
->head
;
534 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(first_param
, 0, 0, 0, 0,
536 ir_dereference_variable
*lhs
= new(ctx
) ir_dereference_variable(var
);
537 const unsigned mask
= (1U << lhs_components
) - 1;
539 assert(rhs
->type
== lhs
->type
);
541 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
, mask
);
542 instructions
->push_tail(inst
);
544 unsigned base_component
= 0;
545 unsigned base_lhs_component
= 0;
546 ir_constant_data data
;
547 unsigned constant_mask
= 0, constant_components
= 0;
549 memset(&data
, 0, sizeof(data
));
551 foreach_list(node
, parameters
) {
552 ir_rvalue
*param
= (ir_rvalue
*) node
;
553 unsigned rhs_components
= param
->type
->components();
555 /* Do not try to assign more components to the vector than it has!
557 if ((rhs_components
+ base_lhs_component
) > lhs_components
) {
558 rhs_components
= lhs_components
- base_lhs_component
;
561 const ir_constant
*const c
= param
->as_constant();
563 for (unsigned i
= 0; i
< rhs_components
; i
++) {
564 switch (c
->type
->base_type
) {
566 data
.u
[i
+ base_component
] = c
->get_uint_component(i
);
569 data
.i
[i
+ base_component
] = c
->get_int_component(i
);
571 case GLSL_TYPE_FLOAT
:
572 data
.f
[i
+ base_component
] = c
->get_float_component(i
);
575 data
.b
[i
+ base_component
] = c
->get_bool_component(i
);
578 assert(!"Should not get here.");
583 /* Mask of fields to be written in the assignment.
585 constant_mask
|= ((1U << rhs_components
) - 1) << base_lhs_component
;
586 constant_components
+= rhs_components
;
588 base_component
+= rhs_components
;
590 /* Advance the component index by the number of components
591 * that were just assigned.
593 base_lhs_component
+= rhs_components
;
596 if (constant_mask
!= 0) {
597 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
598 const glsl_type
*rhs_type
= glsl_type::get_instance(var
->type
->base_type
,
601 ir_rvalue
*rhs
= new(ctx
) ir_constant(rhs_type
, &data
);
603 ir_instruction
*inst
=
604 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, constant_mask
);
605 instructions
->push_tail(inst
);
609 foreach_list(node
, parameters
) {
610 ir_rvalue
*param
= (ir_rvalue
*) node
;
611 unsigned rhs_components
= param
->type
->components();
613 /* Do not try to assign more components to the vector than it has!
615 if ((rhs_components
+ base_component
) > lhs_components
) {
616 rhs_components
= lhs_components
- base_component
;
619 const ir_constant
*const c
= param
->as_constant();
621 /* Mask of fields to be written in the assignment.
623 const unsigned write_mask
= ((1U << rhs_components
) - 1)
626 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
628 /* Generate a swizzle so that LHS and RHS sizes match.
631 new(ctx
) ir_swizzle(param
, 0, 1, 2, 3, rhs_components
);
633 ir_instruction
*inst
=
634 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
635 instructions
->push_tail(inst
);
638 /* Advance the component index by the number of components that were
641 base_component
+= rhs_components
;
644 return new(ctx
) ir_dereference_variable(var
);
649 * Generate assignment of a portion of a vector to a portion of a matrix column
651 * \param src_base First component of the source to be used in assignment
652 * \param column Column of destination to be assiged
653 * \param row_base First component of the destination column to be assigned
654 * \param count Number of components to be assigned
657 * \c src_base + \c count must be less than or equal to the number of components
658 * in the source vector.
661 assign_to_matrix_column(ir_variable
*var
, unsigned column
, unsigned row_base
,
662 ir_rvalue
*src
, unsigned src_base
, unsigned count
,
665 ir_constant
*col_idx
= new(mem_ctx
) ir_constant(column
);
666 ir_dereference
*column_ref
= new(mem_ctx
) ir_dereference_array(var
, col_idx
);
668 assert(column_ref
->type
->components() >= (row_base
+ count
));
669 assert(src
->type
->components() >= (src_base
+ count
));
671 /* Generate a swizzle that extracts the number of components from the source
672 * that are to be assigned to the column of the matrix.
674 if (count
< src
->type
->vector_elements
) {
675 src
= new(mem_ctx
) ir_swizzle(src
,
676 src_base
+ 0, src_base
+ 1,
677 src_base
+ 2, src_base
+ 3,
681 /* Mask of fields to be written in the assignment.
683 const unsigned write_mask
= ((1U << count
) - 1) << row_base
;
685 return new(mem_ctx
) ir_assignment(column_ref
, src
, NULL
, write_mask
);
690 * Generate inline code for a matrix constructor
692 * The generated constructor code will consist of a temporary variable
693 * declaration of the same type as the constructor. A sequence of assignments
694 * from constructor parameters to the temporary will follow.
697 * An \c ir_dereference_variable of the temprorary generated in the constructor
701 emit_inline_matrix_constructor(const glsl_type
*type
,
702 exec_list
*instructions
,
703 exec_list
*parameters
,
706 assert(!parameters
->is_empty());
708 ir_variable
*var
= new(ctx
) ir_variable(type
, "mat_ctor", ir_var_temporary
);
709 instructions
->push_tail(var
);
711 /* There are three kinds of matrix constructors.
713 * - Construct a matrix from a single scalar by replicating that scalar to
714 * along the diagonal of the matrix and setting all other components to
717 * - Construct a matrix from an arbirary combination of vectors and
718 * scalars. The components of the constructor parameters are assigned
719 * to the matrix in colum-major order until the matrix is full.
721 * - Construct a matrix from a single matrix. The source matrix is copied
722 * to the upper left portion of the constructed matrix, and the remaining
723 * elements take values from the identity matrix.
725 ir_rvalue
*const first_param
= (ir_rvalue
*) parameters
->head
;
726 if (single_scalar_parameter(parameters
)) {
727 /* Assign the scalar to the X component of a vec4, and fill the remaining
728 * components with zero.
730 ir_variable
*rhs_var
=
731 new(ctx
) ir_variable(glsl_type::vec4_type
, "mat_ctor_vec",
733 instructions
->push_tail(rhs_var
);
735 ir_constant_data zero
;
741 ir_instruction
*inst
=
742 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(rhs_var
),
743 new(ctx
) ir_constant(rhs_var
->type
, &zero
),
745 instructions
->push_tail(inst
);
747 ir_dereference
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
749 inst
= new(ctx
) ir_assignment(rhs_ref
, first_param
, NULL
, 0x01);
750 instructions
->push_tail(inst
);
752 /* Assign the temporary vector to each column of the destination matrix
753 * with a swizzle that puts the X component on the diagonal of the
754 * matrix. In some cases this may mean that the X component does not
755 * get assigned into the column at all (i.e., when the matrix has more
756 * columns than rows).
758 static const unsigned rhs_swiz
[4][4] = {
765 const unsigned cols_to_init
= MIN2(type
->matrix_columns
,
766 type
->vector_elements
);
767 for (unsigned i
= 0; i
< cols_to_init
; i
++) {
768 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
769 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
771 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
772 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, rhs_swiz
[i
],
773 type
->vector_elements
);
775 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
776 instructions
->push_tail(inst
);
779 for (unsigned i
= cols_to_init
; i
< type
->matrix_columns
; i
++) {
780 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
781 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
783 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
784 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, 1, 1, 1, 1,
785 type
->vector_elements
);
787 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
788 instructions
->push_tail(inst
);
790 } else if (first_param
->type
->is_matrix()) {
791 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
793 * "If a matrix is constructed from a matrix, then each component
794 * (column i, row j) in the result that has a corresponding
795 * component (column i, row j) in the argument will be initialized
796 * from there. All other components will be initialized to the
797 * identity matrix. If a matrix argument is given to a matrix
798 * constructor, it is an error to have any other arguments."
800 assert(first_param
->next
->is_tail_sentinel());
801 ir_rvalue
*const src_matrix
= first_param
;
803 /* If the source matrix is smaller, pre-initialize the relavent parts of
804 * the destination matrix to the identity matrix.
806 if ((src_matrix
->type
->matrix_columns
< var
->type
->matrix_columns
)
807 || (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)) {
809 /* If the source matrix has fewer rows, every column of the destination
810 * must be initialized. Otherwise only the columns in the destination
811 * that do not exist in the source must be initialized.
814 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)
815 ? 0 : src_matrix
->type
->matrix_columns
;
817 const glsl_type
*const col_type
= var
->type
->column_type();
818 for (/* empty */; col
< var
->type
->matrix_columns
; col
++) {
819 ir_constant_data ident
;
828 ir_rvalue
*const rhs
= new(ctx
) ir_constant(col_type
, &ident
);
830 ir_rvalue
*const lhs
=
831 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(col
));
833 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
834 instructions
->push_tail(inst
);
838 /* Assign columns from the source matrix to the destination matrix.
840 * Since the parameter will be used in the RHS of multiple assignments,
841 * generate a temporary and copy the paramter there.
843 ir_variable
*const rhs_var
=
844 new(ctx
) ir_variable(first_param
->type
, "mat_ctor_mat",
846 instructions
->push_tail(rhs_var
);
848 ir_dereference
*const rhs_var_ref
=
849 new(ctx
) ir_dereference_variable(rhs_var
);
850 ir_instruction
*const inst
=
851 new(ctx
) ir_assignment(rhs_var_ref
, first_param
, NULL
);
852 instructions
->push_tail(inst
);
854 const unsigned last_row
= MIN2(src_matrix
->type
->vector_elements
,
855 var
->type
->vector_elements
);
856 const unsigned last_col
= MIN2(src_matrix
->type
->matrix_columns
,
857 var
->type
->matrix_columns
);
859 unsigned swiz
[4] = { 0, 0, 0, 0 };
860 for (unsigned i
= 1; i
< last_row
; i
++)
863 const unsigned write_mask
= (1U << last_row
) - 1;
865 for (unsigned i
= 0; i
< last_col
; i
++) {
866 ir_dereference
*const lhs
=
867 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
868 ir_rvalue
*const rhs_col
=
869 new(ctx
) ir_dereference_array(rhs_var
, new(ctx
) ir_constant(i
));
871 /* If one matrix has columns that are smaller than the columns of the
872 * other matrix, wrap the column access of the larger with a swizzle
873 * so that the LHS and RHS of the assignment have the same size (and
874 * therefore have the same type).
876 * It would be perfectly valid to unconditionally generate the
877 * swizzles, this this will typically result in a more compact IR tree.
880 if (lhs
->type
->vector_elements
!= rhs_col
->type
->vector_elements
) {
881 rhs
= new(ctx
) ir_swizzle(rhs_col
, swiz
, last_row
);
886 ir_instruction
*inst
=
887 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
888 instructions
->push_tail(inst
);
891 const unsigned cols
= type
->matrix_columns
;
892 const unsigned rows
= type
->vector_elements
;
893 unsigned col_idx
= 0;
894 unsigned row_idx
= 0;
896 foreach_list (node
, parameters
) {
897 ir_rvalue
*const rhs
= (ir_rvalue
*) node
;
898 const unsigned components_remaining_this_column
= rows
- row_idx
;
899 unsigned rhs_components
= rhs
->type
->components();
900 unsigned rhs_base
= 0;
902 /* Since the parameter might be used in the RHS of two assignments,
903 * generate a temporary and copy the paramter there.
905 ir_variable
*rhs_var
=
906 new(ctx
) ir_variable(rhs
->type
, "mat_ctor_vec", ir_var_temporary
);
907 instructions
->push_tail(rhs_var
);
909 ir_dereference
*rhs_var_ref
=
910 new(ctx
) ir_dereference_variable(rhs_var
);
911 ir_instruction
*inst
= new(ctx
) ir_assignment(rhs_var_ref
, rhs
, NULL
);
912 instructions
->push_tail(inst
);
914 /* Assign the current parameter to as many components of the matrix
917 * NOTE: A single vector parameter can span two matrix columns. A
918 * single vec4, for example, can completely fill a mat2.
920 if (rhs_components
>= components_remaining_this_column
) {
921 const unsigned count
= MIN2(rhs_components
,
922 components_remaining_this_column
);
924 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
926 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
930 instructions
->push_tail(inst
);
938 /* If there is data left in the parameter and components left to be
939 * set in the destination, emit another assignment. It is possible
940 * that the assignment could be of a vec4 to the last element of the
941 * matrix. In this case col_idx==cols, but there is still data
942 * left in the source parameter. Obviously, don't emit an assignment
943 * to data outside the destination matrix.
945 if ((col_idx
< cols
) && (rhs_base
< rhs_components
)) {
946 const unsigned count
= rhs_components
- rhs_base
;
948 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
950 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
955 instructions
->push_tail(inst
);
962 return new(ctx
) ir_dereference_variable(var
);
967 emit_inline_record_constructor(const glsl_type
*type
,
968 exec_list
*instructions
,
969 exec_list
*parameters
,
972 ir_variable
*const var
=
973 new(mem_ctx
) ir_variable(type
, "record_ctor", ir_var_temporary
);
974 ir_dereference_variable
*const d
= new(mem_ctx
) ir_dereference_variable(var
);
976 instructions
->push_tail(var
);
978 exec_node
*node
= parameters
->head
;
979 for (unsigned i
= 0; i
< type
->length
; i
++) {
980 assert(!node
->is_tail_sentinel());
982 ir_dereference
*const lhs
=
983 new(mem_ctx
) ir_dereference_record(d
->clone(mem_ctx
, NULL
),
984 type
->fields
.structure
[i
].name
);
986 ir_rvalue
*const rhs
= ((ir_instruction
*) node
)->as_rvalue();
989 ir_instruction
*const assign
= new(mem_ctx
) ir_assignment(lhs
, rhs
, NULL
);
991 instructions
->push_tail(assign
);
1000 ast_function_expression::hir(exec_list
*instructions
,
1001 struct _mesa_glsl_parse_state
*state
)
1004 /* There are three sorts of function calls.
1006 * 1. constructors - The first subexpression is an ast_type_specifier.
1007 * 2. methods - Only the .length() method of array types.
1008 * 3. functions - Calls to regular old functions.
1010 * Method calls are actually detected when the ast_field_selection
1011 * expression is handled.
1013 if (is_constructor()) {
1014 const ast_type_specifier
*type
= (ast_type_specifier
*) subexpressions
[0];
1015 YYLTYPE loc
= type
->get_location();
1018 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
1020 /* constructor_type can be NULL if a variable with the same name as the
1021 * structure has come into scope.
1023 if (constructor_type
== NULL
) {
1024 _mesa_glsl_error(& loc
, state
, "unknown type `%s' (structure name "
1025 "may be shadowed by a variable with the same name)",
1027 return ir_call::get_error_instruction(ctx
);
1031 /* Constructors for samplers are illegal.
1033 if (constructor_type
->is_sampler()) {
1034 _mesa_glsl_error(& loc
, state
, "cannot construct sampler type `%s'",
1035 constructor_type
->name
);
1036 return ir_call::get_error_instruction(ctx
);
1039 if (constructor_type
->is_array()) {
1040 if (state
->language_version
<= 110) {
1041 _mesa_glsl_error(& loc
, state
,
1042 "array constructors forbidden in GLSL 1.10");
1043 return ir_call::get_error_instruction(ctx
);
1046 return process_array_constructor(instructions
, constructor_type
,
1047 & loc
, &this->expressions
, state
);
1051 /* There are two kinds of constructor call. Constructors for built-in
1052 * language types, such as mat4 and vec2, are free form. The only
1053 * requirement is that the parameters must provide enough values of the
1054 * correct scalar type. Constructors for arrays and structures must
1055 * have the exact number of parameters with matching types in the
1056 * correct order. These constructors follow essentially the same type
1057 * matching rules as functions.
1059 if (constructor_type
->is_record()) {
1060 exec_list actual_parameters
;
1062 process_parameters(instructions
, &actual_parameters
,
1063 &this->expressions
, state
);
1065 exec_node
*node
= actual_parameters
.head
;
1066 for (unsigned i
= 0; i
< constructor_type
->length
; i
++) {
1067 ir_rvalue
*ir
= (ir_rvalue
*) node
;
1069 if (node
->is_tail_sentinel()) {
1070 _mesa_glsl_error(&loc
, state
,
1071 "insufficient parameters to constructor "
1073 constructor_type
->name
);
1074 return ir_call::get_error_instruction(ctx
);
1077 if (apply_implicit_conversion(constructor_type
->fields
.structure
[i
].type
,
1079 node
->replace_with(ir
);
1081 _mesa_glsl_error(&loc
, state
,
1082 "parameter type mismatch in constructor "
1083 "for `%s.%s' (%s vs %s)",
1084 constructor_type
->name
,
1085 constructor_type
->fields
.structure
[i
].name
,
1087 constructor_type
->fields
.structure
[i
].type
->name
);
1088 return ir_call::get_error_instruction(ctx
);;
1094 if (!node
->is_tail_sentinel()) {
1095 _mesa_glsl_error(&loc
, state
, "too many parameters in constructor "
1096 "for `%s'", constructor_type
->name
);
1097 return ir_call::get_error_instruction(ctx
);
1100 ir_rvalue
*const constant
=
1101 constant_record_constructor(constructor_type
, &actual_parameters
,
1104 return (constant
!= NULL
)
1106 : emit_inline_record_constructor(constructor_type
, instructions
,
1107 &actual_parameters
, state
);
1110 if (!constructor_type
->is_numeric() && !constructor_type
->is_boolean())
1111 return ir_call::get_error_instruction(ctx
);
1113 /* Total number of components of the type being constructed. */
1114 const unsigned type_components
= constructor_type
->components();
1116 /* Number of components from parameters that have actually been
1117 * consumed. This is used to perform several kinds of error checking.
1119 unsigned components_used
= 0;
1121 unsigned matrix_parameters
= 0;
1122 unsigned nonmatrix_parameters
= 0;
1123 exec_list actual_parameters
;
1125 foreach_list (n
, &this->expressions
) {
1126 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
1127 ir_rvalue
*result
= ast
->hir(instructions
, state
)->as_rvalue();
1129 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1131 * "It is an error to provide extra arguments beyond this
1132 * last used argument."
1134 if (components_used
>= type_components
) {
1135 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
1137 constructor_type
->name
);
1138 return ir_call::get_error_instruction(ctx
);
1141 if (!result
->type
->is_numeric() && !result
->type
->is_boolean()) {
1142 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
1143 "non-numeric data type",
1144 constructor_type
->name
);
1145 return ir_call::get_error_instruction(ctx
);
1148 /* Count the number of matrix and nonmatrix parameters. This
1149 * is used below to enforce some of the constructor rules.
1151 if (result
->type
->is_matrix())
1152 matrix_parameters
++;
1154 nonmatrix_parameters
++;
1156 actual_parameters
.push_tail(result
);
1157 components_used
+= result
->type
->components();
1160 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1162 * "It is an error to construct matrices from other matrices. This
1163 * is reserved for future use."
1165 if (state
->language_version
== 110 && matrix_parameters
> 0
1166 && constructor_type
->is_matrix()) {
1167 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
1168 "matrix in GLSL 1.10",
1169 constructor_type
->name
);
1170 return ir_call::get_error_instruction(ctx
);
1173 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1175 * "If a matrix argument is given to a matrix constructor, it is
1176 * an error to have any other arguments."
1178 if ((matrix_parameters
> 0)
1179 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
1180 && constructor_type
->is_matrix()) {
1181 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
1182 "matrix must be only parameter",
1183 constructor_type
->name
);
1184 return ir_call::get_error_instruction(ctx
);
1187 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1189 * "In these cases, there must be enough components provided in the
1190 * arguments to provide an initializer for every component in the
1191 * constructed value."
1193 if (components_used
< type_components
&& components_used
!= 1
1194 && matrix_parameters
== 0) {
1195 _mesa_glsl_error(& loc
, state
, "too few components to construct "
1197 constructor_type
->name
);
1198 return ir_call::get_error_instruction(ctx
);
1201 /* Later, we cast each parameter to the same base type as the
1202 * constructor. Since there are no non-floating point matrices, we
1203 * need to break them up into a series of column vectors.
1205 if (constructor_type
->base_type
!= GLSL_TYPE_FLOAT
) {
1206 foreach_list_safe(n
, &actual_parameters
) {
1207 ir_rvalue
*matrix
= (ir_rvalue
*) n
;
1209 if (!matrix
->type
->is_matrix())
1212 /* Create a temporary containing the matrix. */
1213 ir_variable
*var
= new(ctx
) ir_variable(matrix
->type
, "matrix_tmp",
1215 instructions
->push_tail(var
);
1216 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
)
1217 ir_dereference_variable(var
), matrix
, NULL
));
1218 var
->constant_value
= matrix
->constant_expression_value();
1220 /* Replace the matrix with dereferences of its columns. */
1221 for (int i
= 0; i
< matrix
->type
->matrix_columns
; i
++) {
1222 matrix
->insert_before(new (ctx
) ir_dereference_array(var
,
1223 new(ctx
) ir_constant(i
)));
1229 bool all_parameters_are_constant
= true;
1231 /* Type cast each parameter and, if possible, fold constants.*/
1232 foreach_list_safe(n
, &actual_parameters
) {
1233 ir_rvalue
*ir
= (ir_rvalue
*) n
;
1235 const glsl_type
*desired_type
=
1236 glsl_type::get_instance(constructor_type
->base_type
,
1237 ir
->type
->vector_elements
,
1238 ir
->type
->matrix_columns
);
1239 ir_rvalue
*result
= convert_component(ir
, desired_type
);
1241 /* Attempt to convert the parameter to a constant valued expression.
1242 * After doing so, track whether or not all the parameters to the
1243 * constructor are trivially constant valued expressions.
1245 ir_rvalue
*const constant
= result
->constant_expression_value();
1247 if (constant
!= NULL
)
1250 all_parameters_are_constant
= false;
1253 ir
->replace_with(result
);
1257 /* If all of the parameters are trivially constant, create a
1258 * constant representing the complete collection of parameters.
1260 if (all_parameters_are_constant
) {
1261 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1262 } else if (constructor_type
->is_scalar()) {
1263 return dereference_component((ir_rvalue
*) actual_parameters
.head
,
1265 } else if (constructor_type
->is_vector()) {
1266 return emit_inline_vector_constructor(constructor_type
,
1271 assert(constructor_type
->is_matrix());
1272 return emit_inline_matrix_constructor(constructor_type
,
1278 const ast_expression
*id
= subexpressions
[0];
1279 YYLTYPE loc
= id
->get_location();
1280 exec_list actual_parameters
;
1282 process_parameters(instructions
, &actual_parameters
, &this->expressions
,
1285 return match_function_by_name(instructions
,
1286 id
->primary_expression
.identifier
, & loc
,
1287 &actual_parameters
, state
);
1290 return ir_call::get_error_instruction(ctx
);