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 List of \c ir_instruction nodes representing the
66 * parameter list for the function. This may be either a
67 * formal (\c ir_variable) or actual (\c ir_rvalue)
68 * parameter list. Only the type is used.
71 * A ralloced string representing the prototype of the function.
74 prototype_string(const glsl_type
*return_type
, const char *name
,
75 exec_list
*parameters
)
79 if (return_type
!= NULL
)
80 str
= ralloc_asprintf(NULL
, "%s ", return_type
->name
);
82 ralloc_asprintf_append(&str
, "%s(", name
);
84 const char *comma
= "";
85 foreach_list(node
, parameters
) {
86 const ir_instruction
*const param
= (ir_instruction
*) node
;
88 ralloc_asprintf_append(&str
, "%s%s", comma
, param
->type
->name
);
92 ralloc_strcat(&str
, ")");
97 generate_call(exec_list
*instructions
, ir_function_signature
*sig
,
98 YYLTYPE
*loc
, exec_list
*actual_parameters
,
99 struct _mesa_glsl_parse_state
*state
)
102 exec_list post_call_conversions
;
104 /* Verify that 'out' and 'inout' actual parameters are lvalues. This
105 * isn't done in ir_function::matching_signature because that function
106 * cannot generate the necessary diagnostics.
108 * Also, validate that 'const_in' formal parameters (an extension of our
109 * IR) correspond to ir_constant actual parameters.
111 * Also, perform implicit conversion of arguments. Note: to implicitly
112 * convert out parameters, we need to place them in a temporary
113 * variable, and do the conversion after the call takes place. Since we
114 * haven't emitted the call yet, we'll place the post-call conversions
115 * in a temporary exec_list, and emit them later.
117 exec_list_iterator actual_iter
= actual_parameters
->iterator();
118 exec_list_iterator formal_iter
= sig
->parameters
.iterator();
120 while (actual_iter
.has_next()) {
121 ir_rvalue
*actual
= (ir_rvalue
*) actual_iter
.get();
122 ir_variable
*formal
= (ir_variable
*) formal_iter
.get();
124 assert(actual
!= NULL
);
125 assert(formal
!= NULL
);
127 if (formal
->mode
== ir_var_const_in
&& !actual
->as_constant()) {
128 _mesa_glsl_error(loc
, state
,
129 "parameter `%s' must be a constant expression",
131 return ir_call::get_error_instruction(ctx
);
134 if ((formal
->mode
== ir_var_out
)
135 || (formal
->mode
== ir_var_inout
)) {
136 const char *mode
= NULL
;
137 switch (formal
->mode
) {
138 case ir_var_out
: mode
= "out"; break;
139 case ir_var_inout
: mode
= "inout"; break;
140 default: assert(false); break;
142 /* FIXME: 'loc' is incorrect (as of 2011-01-21). It is always
145 if (actual
->variable_referenced()
146 && actual
->variable_referenced()->read_only
) {
147 _mesa_glsl_error(loc
, state
,
148 "function parameter '%s %s' references the "
149 "read-only variable '%s'",
151 actual
->variable_referenced()->name
);
153 } else if (!actual
->is_lvalue()) {
154 _mesa_glsl_error(loc
, state
,
155 "function parameter '%s %s' is not an lvalue",
160 if (formal
->type
->is_numeric() || formal
->type
->is_boolean()) {
161 switch (formal
->mode
) {
162 case ir_var_const_in
:
165 = convert_component(actual
, formal
->type
);
166 actual
->replace_with(converted
);
170 if (actual
->type
!= formal
->type
) {
171 /* To convert an out parameter, we need to create a
172 * temporary variable to hold the value before conversion,
173 * and then perform the conversion after the function call
176 * This has the effect of transforming code like this:
182 * Into IR that's equivalent to this:
186 * int out_parameter_conversion;
187 * f(out_parameter_conversion);
188 * value = float(out_parameter_conversion);
191 new(ctx
) ir_variable(formal
->type
,
192 "out_parameter_conversion",
194 instructions
->push_tail(tmp
);
195 ir_dereference_variable
*deref_tmp_1
196 = new(ctx
) ir_dereference_variable(tmp
);
197 ir_dereference_variable
*deref_tmp_2
198 = new(ctx
) ir_dereference_variable(tmp
);
199 ir_rvalue
*converted_tmp
200 = convert_component(deref_tmp_1
, actual
->type
);
201 ir_assignment
*assignment
202 = new(ctx
) ir_assignment(actual
, converted_tmp
);
203 post_call_conversions
.push_tail(assignment
);
204 actual
->replace_with(deref_tmp_2
);
208 /* Inout parameters should never require conversion, since that
209 * would require an implicit conversion to exist both to and
210 * from the formal parameter type, and there are no
211 * bidirectional implicit conversions.
213 assert (actual
->type
== formal
->type
);
216 assert (!"Illegal formal parameter mode");
225 /* Always insert the call in the instruction stream, and return a deref
226 * of its return val if it returns a value, since we don't know if
227 * the rvalue is going to be assigned to anything or not.
229 * Also insert any out parameter conversions after the call.
231 ir_call
*call
= new(ctx
) ir_call(sig
, actual_parameters
);
232 ir_dereference_variable
*deref
;
233 if (!sig
->return_type
->is_void()) {
234 /* If the function call is a constant expression, don't
235 * generate the instructions to call it; just generate an
236 * ir_constant representing the constant value.
238 * Function calls can only be constant expressions starting
241 if (state
->language_version
>= 120) {
242 ir_constant
*const_val
= call
->constant_expression_value();
250 var
= new(ctx
) ir_variable(sig
->return_type
,
251 ralloc_asprintf(ctx
, "%s_retval",
252 sig
->function_name()),
254 instructions
->push_tail(var
);
256 deref
= new(ctx
) ir_dereference_variable(var
);
257 ir_assignment
*assign
= new(ctx
) ir_assignment(deref
, call
, NULL
);
258 instructions
->push_tail(assign
);
260 deref
= new(ctx
) ir_dereference_variable(var
);
262 instructions
->push_tail(call
);
265 instructions
->append_list(&post_call_conversions
);
270 match_function_by_name(exec_list
*instructions
, const char *name
,
271 YYLTYPE
*loc
, exec_list
*actual_parameters
,
272 struct _mesa_glsl_parse_state
*state
)
275 ir_function
*f
= state
->symbols
->get_function(name
);
276 ir_function_signature
*local_sig
= NULL
;
277 ir_function_signature
*sig
= NULL
;
279 /* Is the function hidden by a record type constructor? */
280 if (state
->symbols
->get_type(name
))
281 goto done
; /* no match */
283 /* Is the function hidden by a variable (impossible in 1.10)? */
284 if (state
->language_version
!= 110 && state
->symbols
->get_variable(name
))
285 goto done
; /* no match */
288 /* Look for a match in the local shader. If exact, we're done. */
289 bool is_exact
= false;
290 sig
= local_sig
= f
->matching_signature(actual_parameters
, &is_exact
);
294 if (!state
->es_shader
&& f
->has_user_signature()) {
295 /* In desktop GL, the presence of a user-defined signature hides any
296 * built-in signatures, so we must ignore them. In contrast, in ES2
297 * user-defined signatures add new overloads, so we must proceed.
303 /* Local shader has no exact candidates; check the built-ins. */
304 _mesa_glsl_initialize_functions(state
);
305 for (unsigned i
= 0; i
< state
->num_builtins_to_link
; i
++) {
306 ir_function
*builtin
=
307 state
->builtins_to_link
[i
]->symbols
->get_function(name
);
311 bool is_exact
= false;
312 ir_function_signature
*builtin_sig
=
313 builtin
->matching_signature(actual_parameters
, &is_exact
);
315 if (builtin_sig
== NULL
)
318 /* If the built-in signature is exact, we can stop. */
325 /* We found an inexact match, which is better than nothing. However,
326 * we should keep searching for an exact match.
334 /* If the match is from a linked built-in shader, import the prototype. */
335 if (sig
!= local_sig
) {
337 f
= new(ctx
) ir_function(name
);
338 state
->symbols
->add_global_function(f
);
339 emit_function(state
, f
);
341 f
->add_signature(sig
->clone_prototype(f
, NULL
));
344 /* Finally, generate a call instruction. */
345 return generate_call(instructions
, sig
, loc
, actual_parameters
, state
);
347 char *str
= prototype_string(NULL
, name
, actual_parameters
);
349 _mesa_glsl_error(loc
, state
, "no matching function for call to `%s'",
353 const char *prefix
= "candidates are: ";
355 for (int i
= -1; i
< (int) state
->num_builtins_to_link
; i
++) {
356 glsl_symbol_table
*syms
= i
>= 0 ? state
->builtins_to_link
[i
]->symbols
358 f
= syms
->get_function(name
);
362 foreach_list (node
, &f
->signatures
) {
363 ir_function_signature
*sig
= (ir_function_signature
*) node
;
365 str
= prototype_string(sig
->return_type
, f
->name
, &sig
->parameters
);
366 _mesa_glsl_error(loc
, state
, "%s%s", prefix
, str
);
374 return ir_call::get_error_instruction(ctx
);
380 * Perform automatic type conversion of constructor parameters
382 * This implements the rules in the "Conversion and Scalar Constructors"
383 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
386 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
)
388 void *ctx
= ralloc_parent(src
);
389 const unsigned a
= desired_type
->base_type
;
390 const unsigned b
= src
->type
->base_type
;
391 ir_expression
*result
= NULL
;
393 if (src
->type
->is_error())
396 assert(a
<= GLSL_TYPE_BOOL
);
397 assert(b
<= GLSL_TYPE_BOOL
);
406 result
= new(ctx
) ir_expression(ir_unop_i2u
, src
);
408 case GLSL_TYPE_FLOAT
:
409 result
= new(ctx
) ir_expression(ir_unop_i2u
,
410 new(ctx
) ir_expression(ir_unop_f2i
, src
));
413 result
= new(ctx
) ir_expression(ir_unop_i2u
,
414 new(ctx
) ir_expression(ir_unop_b2i
, src
));
421 result
= new(ctx
) ir_expression(ir_unop_u2i
, src
);
423 case GLSL_TYPE_FLOAT
:
424 result
= new(ctx
) ir_expression(ir_unop_f2i
, src
);
427 result
= new(ctx
) ir_expression(ir_unop_b2i
, src
);
431 case GLSL_TYPE_FLOAT
:
434 result
= new(ctx
) ir_expression(ir_unop_u2f
, desired_type
, src
, NULL
);
437 result
= new(ctx
) ir_expression(ir_unop_i2f
, desired_type
, src
, NULL
);
440 result
= new(ctx
) ir_expression(ir_unop_b2f
, desired_type
, src
, NULL
);
447 result
= new(ctx
) ir_expression(ir_unop_i2b
,
448 new(ctx
) ir_expression(ir_unop_u2i
, src
));
451 result
= new(ctx
) ir_expression(ir_unop_i2b
, desired_type
, src
, NULL
);
453 case GLSL_TYPE_FLOAT
:
454 result
= new(ctx
) ir_expression(ir_unop_f2b
, desired_type
, src
, NULL
);
460 assert(result
!= NULL
);
461 assert(result
->type
== desired_type
);
463 /* Try constant folding; it may fold in the conversion we just added. */
464 ir_constant
*const constant
= result
->constant_expression_value();
465 return (constant
!= NULL
) ? (ir_rvalue
*) constant
: (ir_rvalue
*) result
;
469 * Dereference a specific component from a scalar, vector, or matrix
472 dereference_component(ir_rvalue
*src
, unsigned component
)
474 void *ctx
= ralloc_parent(src
);
475 assert(component
< src
->type
->components());
477 /* If the source is a constant, just create a new constant instead of a
478 * dereference of the existing constant.
480 ir_constant
*constant
= src
->as_constant();
482 return new(ctx
) ir_constant(constant
, component
);
484 if (src
->type
->is_scalar()) {
486 } else if (src
->type
->is_vector()) {
487 return new(ctx
) ir_swizzle(src
, component
, 0, 0, 0, 1);
489 assert(src
->type
->is_matrix());
491 /* Dereference a row of the matrix, then call this function again to get
492 * a specific element from that row.
494 const int c
= component
/ src
->type
->column_type()->vector_elements
;
495 const int r
= component
% src
->type
->column_type()->vector_elements
;
496 ir_constant
*const col_index
= new(ctx
) ir_constant(c
);
497 ir_dereference
*const col
= new(ctx
) ir_dereference_array(src
, col_index
);
499 col
->type
= src
->type
->column_type();
501 return dereference_component(col
, r
);
504 assert(!"Should not get here.");
510 process_array_constructor(exec_list
*instructions
,
511 const glsl_type
*constructor_type
,
512 YYLTYPE
*loc
, exec_list
*parameters
,
513 struct _mesa_glsl_parse_state
*state
)
516 /* Array constructors come in two forms: sized and unsized. Sized array
517 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
518 * variables. In this case the number of parameters must exactly match the
519 * specified size of the array.
521 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
522 * are vec4 variables. In this case the size of the array being constructed
523 * is determined by the number of parameters.
525 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
527 * "There must be exactly the same number of arguments as the size of
528 * the array being constructed. If no size is present in the
529 * constructor, then the array is explicitly sized to the number of
530 * arguments provided. The arguments are assigned in order, starting at
531 * element 0, to the elements of the constructed array. Each argument
532 * must be the same type as the element type of the array, or be a type
533 * that can be converted to the element type of the array according to
534 * Section 4.1.10 "Implicit Conversions.""
536 exec_list actual_parameters
;
537 const unsigned parameter_count
=
538 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
540 if ((parameter_count
== 0)
541 || ((constructor_type
->length
!= 0)
542 && (constructor_type
->length
!= parameter_count
))) {
543 const unsigned min_param
= (constructor_type
->length
== 0)
544 ? 1 : constructor_type
->length
;
546 _mesa_glsl_error(loc
, state
, "array constructor must have %s %u "
548 (constructor_type
->length
!= 0) ? "at least" : "exactly",
549 min_param
, (min_param
<= 1) ? "" : "s");
550 return ir_call::get_error_instruction(ctx
);
553 if (constructor_type
->length
== 0) {
555 glsl_type::get_array_instance(constructor_type
->element_type(),
557 assert(constructor_type
!= NULL
);
558 assert(constructor_type
->length
== parameter_count
);
561 bool all_parameters_are_constant
= true;
563 /* Type cast each parameter and, if possible, fold constants. */
564 foreach_list_safe(n
, &actual_parameters
) {
565 ir_rvalue
*ir
= (ir_rvalue
*) n
;
566 ir_rvalue
*result
= ir
;
568 /* Apply implicit conversions (not the scalar constructor rules!). See
569 * the spec quote above. */
570 if (constructor_type
->element_type()->is_float()) {
571 const glsl_type
*desired_type
=
572 glsl_type::get_instance(GLSL_TYPE_FLOAT
,
573 ir
->type
->vector_elements
,
574 ir
->type
->matrix_columns
);
575 if (result
->type
->can_implicitly_convert_to(desired_type
)) {
576 /* Even though convert_component() implements the constructor
577 * conversion rules (not the implicit conversion rules), its safe
578 * to use it here because we already checked that the implicit
579 * conversion is legal.
581 result
= convert_component(ir
, desired_type
);
585 if (result
->type
!= constructor_type
->element_type()) {
586 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
587 "expected: %s, found %s",
588 constructor_type
->element_type()->name
,
592 /* Attempt to convert the parameter to a constant valued expression.
593 * After doing so, track whether or not all the parameters to the
594 * constructor are trivially constant valued expressions.
596 ir_rvalue
*const constant
= result
->constant_expression_value();
598 if (constant
!= NULL
)
601 all_parameters_are_constant
= false;
603 ir
->replace_with(result
);
606 if (all_parameters_are_constant
)
607 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
609 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "array_ctor",
611 instructions
->push_tail(var
);
614 foreach_list(node
, &actual_parameters
) {
615 ir_rvalue
*rhs
= (ir_rvalue
*) node
;
616 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
617 new(ctx
) ir_constant(i
));
619 ir_instruction
*assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
620 instructions
->push_tail(assignment
);
625 return new(ctx
) ir_dereference_variable(var
);
630 * Try to convert a record constructor to a constant expression
633 constant_record_constructor(const glsl_type
*constructor_type
,
634 exec_list
*parameters
, void *mem_ctx
)
636 foreach_list(node
, parameters
) {
637 ir_constant
*constant
= ((ir_instruction
*) node
)->as_constant();
638 if (constant
== NULL
)
640 node
->replace_with(constant
);
643 return new(mem_ctx
) ir_constant(constructor_type
, parameters
);
648 * Determine if a list consists of a single scalar r-value
651 single_scalar_parameter(exec_list
*parameters
)
653 const ir_rvalue
*const p
= (ir_rvalue
*) parameters
->head
;
654 assert(((ir_rvalue
*)p
)->as_rvalue() != NULL
);
656 return (p
->type
->is_scalar() && p
->next
->is_tail_sentinel());
661 * Generate inline code for a vector constructor
663 * The generated constructor code will consist of a temporary variable
664 * declaration of the same type as the constructor. A sequence of assignments
665 * from constructor parameters to the temporary will follow.
668 * An \c ir_dereference_variable of the temprorary generated in the constructor
672 emit_inline_vector_constructor(const glsl_type
*type
,
673 exec_list
*instructions
,
674 exec_list
*parameters
,
677 assert(!parameters
->is_empty());
679 ir_variable
*var
= new(ctx
) ir_variable(type
, "vec_ctor", ir_var_temporary
);
680 instructions
->push_tail(var
);
682 /* There are two kinds of vector constructors.
684 * - Construct a vector from a single scalar by replicating that scalar to
685 * all components of the vector.
687 * - Construct a vector from an arbirary combination of vectors and
688 * scalars. The components of the constructor parameters are assigned
689 * to the vector in order until the vector is full.
691 const unsigned lhs_components
= type
->components();
692 if (single_scalar_parameter(parameters
)) {
693 ir_rvalue
*first_param
= (ir_rvalue
*)parameters
->head
;
694 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(first_param
, 0, 0, 0, 0,
696 ir_dereference_variable
*lhs
= new(ctx
) ir_dereference_variable(var
);
697 const unsigned mask
= (1U << lhs_components
) - 1;
699 assert(rhs
->type
== lhs
->type
);
701 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
, mask
);
702 instructions
->push_tail(inst
);
704 unsigned base_component
= 0;
705 unsigned base_lhs_component
= 0;
706 ir_constant_data data
;
707 unsigned constant_mask
= 0, constant_components
= 0;
709 memset(&data
, 0, sizeof(data
));
711 foreach_list(node
, parameters
) {
712 ir_rvalue
*param
= (ir_rvalue
*) node
;
713 unsigned rhs_components
= param
->type
->components();
715 /* Do not try to assign more components to the vector than it has!
717 if ((rhs_components
+ base_lhs_component
) > lhs_components
) {
718 rhs_components
= lhs_components
- base_lhs_component
;
721 const ir_constant
*const c
= param
->as_constant();
723 for (unsigned i
= 0; i
< rhs_components
; i
++) {
724 switch (c
->type
->base_type
) {
726 data
.u
[i
+ base_component
] = c
->get_uint_component(i
);
729 data
.i
[i
+ base_component
] = c
->get_int_component(i
);
731 case GLSL_TYPE_FLOAT
:
732 data
.f
[i
+ base_component
] = c
->get_float_component(i
);
735 data
.b
[i
+ base_component
] = c
->get_bool_component(i
);
738 assert(!"Should not get here.");
743 /* Mask of fields to be written in the assignment.
745 constant_mask
|= ((1U << rhs_components
) - 1) << base_lhs_component
;
746 constant_components
+= rhs_components
;
748 base_component
+= rhs_components
;
750 /* Advance the component index by the number of components
751 * that were just assigned.
753 base_lhs_component
+= rhs_components
;
756 if (constant_mask
!= 0) {
757 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
758 const glsl_type
*rhs_type
= glsl_type::get_instance(var
->type
->base_type
,
761 ir_rvalue
*rhs
= new(ctx
) ir_constant(rhs_type
, &data
);
763 ir_instruction
*inst
=
764 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, constant_mask
);
765 instructions
->push_tail(inst
);
769 foreach_list(node
, parameters
) {
770 ir_rvalue
*param
= (ir_rvalue
*) node
;
771 unsigned rhs_components
= param
->type
->components();
773 /* Do not try to assign more components to the vector than it has!
775 if ((rhs_components
+ base_component
) > lhs_components
) {
776 rhs_components
= lhs_components
- base_component
;
779 const ir_constant
*const c
= param
->as_constant();
781 /* Mask of fields to be written in the assignment.
783 const unsigned write_mask
= ((1U << rhs_components
) - 1)
786 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
788 /* Generate a swizzle so that LHS and RHS sizes match.
791 new(ctx
) ir_swizzle(param
, 0, 1, 2, 3, rhs_components
);
793 ir_instruction
*inst
=
794 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
795 instructions
->push_tail(inst
);
798 /* Advance the component index by the number of components that were
801 base_component
+= rhs_components
;
804 return new(ctx
) ir_dereference_variable(var
);
809 * Generate assignment of a portion of a vector to a portion of a matrix column
811 * \param src_base First component of the source to be used in assignment
812 * \param column Column of destination to be assiged
813 * \param row_base First component of the destination column to be assigned
814 * \param count Number of components to be assigned
817 * \c src_base + \c count must be less than or equal to the number of components
818 * in the source vector.
821 assign_to_matrix_column(ir_variable
*var
, unsigned column
, unsigned row_base
,
822 ir_rvalue
*src
, unsigned src_base
, unsigned count
,
825 ir_constant
*col_idx
= new(mem_ctx
) ir_constant(column
);
826 ir_dereference
*column_ref
= new(mem_ctx
) ir_dereference_array(var
, col_idx
);
828 assert(column_ref
->type
->components() >= (row_base
+ count
));
829 assert(src
->type
->components() >= (src_base
+ count
));
831 /* Generate a swizzle that extracts the number of components from the source
832 * that are to be assigned to the column of the matrix.
834 if (count
< src
->type
->vector_elements
) {
835 src
= new(mem_ctx
) ir_swizzle(src
,
836 src_base
+ 0, src_base
+ 1,
837 src_base
+ 2, src_base
+ 3,
841 /* Mask of fields to be written in the assignment.
843 const unsigned write_mask
= ((1U << count
) - 1) << row_base
;
845 return new(mem_ctx
) ir_assignment(column_ref
, src
, NULL
, write_mask
);
850 * Generate inline code for a matrix constructor
852 * The generated constructor code will consist of a temporary variable
853 * declaration of the same type as the constructor. A sequence of assignments
854 * from constructor parameters to the temporary will follow.
857 * An \c ir_dereference_variable of the temprorary generated in the constructor
861 emit_inline_matrix_constructor(const glsl_type
*type
,
862 exec_list
*instructions
,
863 exec_list
*parameters
,
866 assert(!parameters
->is_empty());
868 ir_variable
*var
= new(ctx
) ir_variable(type
, "mat_ctor", ir_var_temporary
);
869 instructions
->push_tail(var
);
871 /* There are three kinds of matrix constructors.
873 * - Construct a matrix from a single scalar by replicating that scalar to
874 * along the diagonal of the matrix and setting all other components to
877 * - Construct a matrix from an arbirary combination of vectors and
878 * scalars. The components of the constructor parameters are assigned
879 * to the matrix in colum-major order until the matrix is full.
881 * - Construct a matrix from a single matrix. The source matrix is copied
882 * to the upper left portion of the constructed matrix, and the remaining
883 * elements take values from the identity matrix.
885 ir_rvalue
*const first_param
= (ir_rvalue
*) parameters
->head
;
886 if (single_scalar_parameter(parameters
)) {
887 /* Assign the scalar to the X component of a vec4, and fill the remaining
888 * components with zero.
890 ir_variable
*rhs_var
=
891 new(ctx
) ir_variable(glsl_type::vec4_type
, "mat_ctor_vec",
893 instructions
->push_tail(rhs_var
);
895 ir_constant_data zero
;
901 ir_instruction
*inst
=
902 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(rhs_var
),
903 new(ctx
) ir_constant(rhs_var
->type
, &zero
),
905 instructions
->push_tail(inst
);
907 ir_dereference
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
909 inst
= new(ctx
) ir_assignment(rhs_ref
, first_param
, NULL
, 0x01);
910 instructions
->push_tail(inst
);
912 /* Assign the temporary vector to each column of the destination matrix
913 * with a swizzle that puts the X component on the diagonal of the
914 * matrix. In some cases this may mean that the X component does not
915 * get assigned into the column at all (i.e., when the matrix has more
916 * columns than rows).
918 static const unsigned rhs_swiz
[4][4] = {
925 const unsigned cols_to_init
= MIN2(type
->matrix_columns
,
926 type
->vector_elements
);
927 for (unsigned i
= 0; i
< cols_to_init
; i
++) {
928 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
929 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
931 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
932 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, rhs_swiz
[i
],
933 type
->vector_elements
);
935 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
936 instructions
->push_tail(inst
);
939 for (unsigned i
= cols_to_init
; i
< type
->matrix_columns
; i
++) {
940 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
941 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
943 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
944 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, 1, 1, 1, 1,
945 type
->vector_elements
);
947 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
948 instructions
->push_tail(inst
);
950 } else if (first_param
->type
->is_matrix()) {
951 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
953 * "If a matrix is constructed from a matrix, then each component
954 * (column i, row j) in the result that has a corresponding
955 * component (column i, row j) in the argument will be initialized
956 * from there. All other components will be initialized to the
957 * identity matrix. If a matrix argument is given to a matrix
958 * constructor, it is an error to have any other arguments."
960 assert(first_param
->next
->is_tail_sentinel());
961 ir_rvalue
*const src_matrix
= first_param
;
963 /* If the source matrix is smaller, pre-initialize the relavent parts of
964 * the destination matrix to the identity matrix.
966 if ((src_matrix
->type
->matrix_columns
< var
->type
->matrix_columns
)
967 || (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)) {
969 /* If the source matrix has fewer rows, every column of the destination
970 * must be initialized. Otherwise only the columns in the destination
971 * that do not exist in the source must be initialized.
974 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)
975 ? 0 : src_matrix
->type
->matrix_columns
;
977 const glsl_type
*const col_type
= var
->type
->column_type();
978 for (/* empty */; col
< var
->type
->matrix_columns
; col
++) {
979 ir_constant_data ident
;
988 ir_rvalue
*const rhs
= new(ctx
) ir_constant(col_type
, &ident
);
990 ir_rvalue
*const lhs
=
991 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(col
));
993 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
994 instructions
->push_tail(inst
);
998 /* Assign columns from the source matrix to the destination matrix.
1000 * Since the parameter will be used in the RHS of multiple assignments,
1001 * generate a temporary and copy the paramter there.
1003 ir_variable
*const rhs_var
=
1004 new(ctx
) ir_variable(first_param
->type
, "mat_ctor_mat",
1006 instructions
->push_tail(rhs_var
);
1008 ir_dereference
*const rhs_var_ref
=
1009 new(ctx
) ir_dereference_variable(rhs_var
);
1010 ir_instruction
*const inst
=
1011 new(ctx
) ir_assignment(rhs_var_ref
, first_param
, NULL
);
1012 instructions
->push_tail(inst
);
1014 const unsigned last_row
= MIN2(src_matrix
->type
->vector_elements
,
1015 var
->type
->vector_elements
);
1016 const unsigned last_col
= MIN2(src_matrix
->type
->matrix_columns
,
1017 var
->type
->matrix_columns
);
1019 unsigned swiz
[4] = { 0, 0, 0, 0 };
1020 for (unsigned i
= 1; i
< last_row
; i
++)
1023 const unsigned write_mask
= (1U << last_row
) - 1;
1025 for (unsigned i
= 0; i
< last_col
; i
++) {
1026 ir_dereference
*const lhs
=
1027 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
1028 ir_rvalue
*const rhs_col
=
1029 new(ctx
) ir_dereference_array(rhs_var
, new(ctx
) ir_constant(i
));
1031 /* If one matrix has columns that are smaller than the columns of the
1032 * other matrix, wrap the column access of the larger with a swizzle
1033 * so that the LHS and RHS of the assignment have the same size (and
1034 * therefore have the same type).
1036 * It would be perfectly valid to unconditionally generate the
1037 * swizzles, this this will typically result in a more compact IR tree.
1040 if (lhs
->type
->vector_elements
!= rhs_col
->type
->vector_elements
) {
1041 rhs
= new(ctx
) ir_swizzle(rhs_col
, swiz
, last_row
);
1046 ir_instruction
*inst
=
1047 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1048 instructions
->push_tail(inst
);
1051 const unsigned cols
= type
->matrix_columns
;
1052 const unsigned rows
= type
->vector_elements
;
1053 unsigned col_idx
= 0;
1054 unsigned row_idx
= 0;
1056 foreach_list (node
, parameters
) {
1057 ir_rvalue
*const rhs
= (ir_rvalue
*) node
;
1058 const unsigned components_remaining_this_column
= rows
- row_idx
;
1059 unsigned rhs_components
= rhs
->type
->components();
1060 unsigned rhs_base
= 0;
1062 /* Since the parameter might be used in the RHS of two assignments,
1063 * generate a temporary and copy the paramter there.
1065 ir_variable
*rhs_var
=
1066 new(ctx
) ir_variable(rhs
->type
, "mat_ctor_vec", ir_var_temporary
);
1067 instructions
->push_tail(rhs_var
);
1069 ir_dereference
*rhs_var_ref
=
1070 new(ctx
) ir_dereference_variable(rhs_var
);
1071 ir_instruction
*inst
= new(ctx
) ir_assignment(rhs_var_ref
, rhs
, NULL
);
1072 instructions
->push_tail(inst
);
1074 /* Assign the current parameter to as many components of the matrix
1077 * NOTE: A single vector parameter can span two matrix columns. A
1078 * single vec4, for example, can completely fill a mat2.
1080 if (rhs_components
>= components_remaining_this_column
) {
1081 const unsigned count
= MIN2(rhs_components
,
1082 components_remaining_this_column
);
1084 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1086 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
1090 instructions
->push_tail(inst
);
1098 /* If there is data left in the parameter and components left to be
1099 * set in the destination, emit another assignment. It is possible
1100 * that the assignment could be of a vec4 to the last element of the
1101 * matrix. In this case col_idx==cols, but there is still data
1102 * left in the source parameter. Obviously, don't emit an assignment
1103 * to data outside the destination matrix.
1105 if ((col_idx
< cols
) && (rhs_base
< rhs_components
)) {
1106 const unsigned count
= rhs_components
- rhs_base
;
1108 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1110 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
1115 instructions
->push_tail(inst
);
1122 return new(ctx
) ir_dereference_variable(var
);
1127 emit_inline_record_constructor(const glsl_type
*type
,
1128 exec_list
*instructions
,
1129 exec_list
*parameters
,
1132 ir_variable
*const var
=
1133 new(mem_ctx
) ir_variable(type
, "record_ctor", ir_var_temporary
);
1134 ir_dereference_variable
*const d
= new(mem_ctx
) ir_dereference_variable(var
);
1136 instructions
->push_tail(var
);
1138 exec_node
*node
= parameters
->head
;
1139 for (unsigned i
= 0; i
< type
->length
; i
++) {
1140 assert(!node
->is_tail_sentinel());
1142 ir_dereference
*const lhs
=
1143 new(mem_ctx
) ir_dereference_record(d
->clone(mem_ctx
, NULL
),
1144 type
->fields
.structure
[i
].name
);
1146 ir_rvalue
*const rhs
= ((ir_instruction
*) node
)->as_rvalue();
1147 assert(rhs
!= NULL
);
1149 ir_instruction
*const assign
= new(mem_ctx
) ir_assignment(lhs
, rhs
, NULL
);
1151 instructions
->push_tail(assign
);
1160 ast_function_expression::hir(exec_list
*instructions
,
1161 struct _mesa_glsl_parse_state
*state
)
1164 /* There are three sorts of function calls.
1166 * 1. constructors - The first subexpression is an ast_type_specifier.
1167 * 2. methods - Only the .length() method of array types.
1168 * 3. functions - Calls to regular old functions.
1170 * Method calls are actually detected when the ast_field_selection
1171 * expression is handled.
1173 if (is_constructor()) {
1174 const ast_type_specifier
*type
= (ast_type_specifier
*) subexpressions
[0];
1175 YYLTYPE loc
= type
->get_location();
1178 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
1180 /* constructor_type can be NULL if a variable with the same name as the
1181 * structure has come into scope.
1183 if (constructor_type
== NULL
) {
1184 _mesa_glsl_error(& loc
, state
, "unknown type `%s' (structure name "
1185 "may be shadowed by a variable with the same name)",
1187 return ir_call::get_error_instruction(ctx
);
1191 /* Constructors for samplers are illegal.
1193 if (constructor_type
->is_sampler()) {
1194 _mesa_glsl_error(& loc
, state
, "cannot construct sampler type `%s'",
1195 constructor_type
->name
);
1196 return ir_call::get_error_instruction(ctx
);
1199 if (constructor_type
->is_array()) {
1200 if (state
->language_version
<= 110) {
1201 _mesa_glsl_error(& loc
, state
,
1202 "array constructors forbidden in GLSL 1.10");
1203 return ir_call::get_error_instruction(ctx
);
1206 return process_array_constructor(instructions
, constructor_type
,
1207 & loc
, &this->expressions
, state
);
1211 /* There are two kinds of constructor call. Constructors for built-in
1212 * language types, such as mat4 and vec2, are free form. The only
1213 * requirement is that the parameters must provide enough values of the
1214 * correct scalar type. Constructors for arrays and structures must
1215 * have the exact number of parameters with matching types in the
1216 * correct order. These constructors follow essentially the same type
1217 * matching rules as functions.
1219 if (constructor_type
->is_record()) {
1220 exec_list actual_parameters
;
1222 process_parameters(instructions
, &actual_parameters
,
1223 &this->expressions
, state
);
1225 exec_node
*node
= actual_parameters
.head
;
1226 for (unsigned i
= 0; i
< constructor_type
->length
; i
++) {
1227 ir_rvalue
*ir
= (ir_rvalue
*) node
;
1229 if (node
->is_tail_sentinel()) {
1230 _mesa_glsl_error(&loc
, state
,
1231 "insufficient parameters to constructor "
1233 constructor_type
->name
);
1234 return ir_call::get_error_instruction(ctx
);
1237 if (apply_implicit_conversion(constructor_type
->fields
.structure
[i
].type
,
1239 node
->replace_with(ir
);
1241 _mesa_glsl_error(&loc
, state
,
1242 "parameter type mismatch in constructor "
1243 "for `%s.%s' (%s vs %s)",
1244 constructor_type
->name
,
1245 constructor_type
->fields
.structure
[i
].name
,
1247 constructor_type
->fields
.structure
[i
].type
->name
);
1248 return ir_call::get_error_instruction(ctx
);;
1254 if (!node
->is_tail_sentinel()) {
1255 _mesa_glsl_error(&loc
, state
, "too many parameters in constructor "
1256 "for `%s'", constructor_type
->name
);
1257 return ir_call::get_error_instruction(ctx
);
1260 ir_rvalue
*const constant
=
1261 constant_record_constructor(constructor_type
, &actual_parameters
,
1264 return (constant
!= NULL
)
1266 : emit_inline_record_constructor(constructor_type
, instructions
,
1267 &actual_parameters
, state
);
1270 if (!constructor_type
->is_numeric() && !constructor_type
->is_boolean())
1271 return ir_call::get_error_instruction(ctx
);
1273 /* Total number of components of the type being constructed. */
1274 const unsigned type_components
= constructor_type
->components();
1276 /* Number of components from parameters that have actually been
1277 * consumed. This is used to perform several kinds of error checking.
1279 unsigned components_used
= 0;
1281 unsigned matrix_parameters
= 0;
1282 unsigned nonmatrix_parameters
= 0;
1283 exec_list actual_parameters
;
1285 foreach_list (n
, &this->expressions
) {
1286 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
1287 ir_rvalue
*result
= ast
->hir(instructions
, state
)->as_rvalue();
1289 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1291 * "It is an error to provide extra arguments beyond this
1292 * last used argument."
1294 if (components_used
>= type_components
) {
1295 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
1297 constructor_type
->name
);
1298 return ir_call::get_error_instruction(ctx
);
1301 if (!result
->type
->is_numeric() && !result
->type
->is_boolean()) {
1302 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
1303 "non-numeric data type",
1304 constructor_type
->name
);
1305 return ir_call::get_error_instruction(ctx
);
1308 /* Count the number of matrix and nonmatrix parameters. This
1309 * is used below to enforce some of the constructor rules.
1311 if (result
->type
->is_matrix())
1312 matrix_parameters
++;
1314 nonmatrix_parameters
++;
1316 actual_parameters
.push_tail(result
);
1317 components_used
+= result
->type
->components();
1320 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1322 * "It is an error to construct matrices from other matrices. This
1323 * is reserved for future use."
1325 if (state
->language_version
== 110 && matrix_parameters
> 0
1326 && constructor_type
->is_matrix()) {
1327 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
1328 "matrix in GLSL 1.10",
1329 constructor_type
->name
);
1330 return ir_call::get_error_instruction(ctx
);
1333 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1335 * "If a matrix argument is given to a matrix constructor, it is
1336 * an error to have any other arguments."
1338 if ((matrix_parameters
> 0)
1339 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
1340 && constructor_type
->is_matrix()) {
1341 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
1342 "matrix must be only parameter",
1343 constructor_type
->name
);
1344 return ir_call::get_error_instruction(ctx
);
1347 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1349 * "In these cases, there must be enough components provided in the
1350 * arguments to provide an initializer for every component in the
1351 * constructed value."
1353 if (components_used
< type_components
&& components_used
!= 1
1354 && matrix_parameters
== 0) {
1355 _mesa_glsl_error(& loc
, state
, "too few components to construct "
1357 constructor_type
->name
);
1358 return ir_call::get_error_instruction(ctx
);
1361 /* Later, we cast each parameter to the same base type as the
1362 * constructor. Since there are no non-floating point matrices, we
1363 * need to break them up into a series of column vectors.
1365 if (constructor_type
->base_type
!= GLSL_TYPE_FLOAT
) {
1366 foreach_list_safe(n
, &actual_parameters
) {
1367 ir_rvalue
*matrix
= (ir_rvalue
*) n
;
1369 if (!matrix
->type
->is_matrix())
1372 /* Create a temporary containing the matrix. */
1373 ir_variable
*var
= new(ctx
) ir_variable(matrix
->type
, "matrix_tmp",
1375 instructions
->push_tail(var
);
1376 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
)
1377 ir_dereference_variable(var
), matrix
, NULL
));
1378 var
->constant_value
= matrix
->constant_expression_value();
1380 /* Replace the matrix with dereferences of its columns. */
1381 for (int i
= 0; i
< matrix
->type
->matrix_columns
; i
++) {
1382 matrix
->insert_before(new (ctx
) ir_dereference_array(var
,
1383 new(ctx
) ir_constant(i
)));
1389 bool all_parameters_are_constant
= true;
1391 /* Type cast each parameter and, if possible, fold constants.*/
1392 foreach_list_safe(n
, &actual_parameters
) {
1393 ir_rvalue
*ir
= (ir_rvalue
*) n
;
1395 const glsl_type
*desired_type
=
1396 glsl_type::get_instance(constructor_type
->base_type
,
1397 ir
->type
->vector_elements
,
1398 ir
->type
->matrix_columns
);
1399 ir_rvalue
*result
= convert_component(ir
, desired_type
);
1401 /* Attempt to convert the parameter to a constant valued expression.
1402 * After doing so, track whether or not all the parameters to the
1403 * constructor are trivially constant valued expressions.
1405 ir_rvalue
*const constant
= result
->constant_expression_value();
1407 if (constant
!= NULL
)
1410 all_parameters_are_constant
= false;
1413 ir
->replace_with(result
);
1417 /* If all of the parameters are trivially constant, create a
1418 * constant representing the complete collection of parameters.
1420 if (all_parameters_are_constant
) {
1421 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1422 } else if (constructor_type
->is_scalar()) {
1423 return dereference_component((ir_rvalue
*) actual_parameters
.head
,
1425 } else if (constructor_type
->is_vector()) {
1426 return emit_inline_vector_constructor(constructor_type
,
1431 assert(constructor_type
->is_matrix());
1432 return emit_inline_matrix_constructor(constructor_type
,
1438 const ast_expression
*id
= subexpressions
[0];
1439 YYLTYPE loc
= id
->get_location();
1440 exec_list actual_parameters
;
1442 process_parameters(instructions
, &actual_parameters
, &this->expressions
,
1445 return match_function_by_name(instructions
,
1446 id
->primary_expression
.identifier
, & loc
,
1447 &actual_parameters
, state
);
1450 return ir_call::get_error_instruction(ctx
);