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_in_list(const ir_variable
, param
, parameters
) {
86 ralloc_asprintf_append(&str
, "%s%s", comma
, param
->type
->name
);
90 ralloc_strcat(&str
, ")");
95 verify_image_parameter(YYLTYPE
*loc
, _mesa_glsl_parse_state
*state
,
96 const ir_variable
*formal
, const ir_variable
*actual
)
99 * From the ARB_shader_image_load_store specification:
101 * "The values of image variables qualified with coherent,
102 * volatile, restrict, readonly, or writeonly may not be passed
103 * to functions whose formal parameters lack such
104 * qualifiers. [...] It is legal to have additional qualifiers
105 * on a formal parameter, but not to have fewer."
107 if (actual
->data
.image
.coherent
&& !formal
->data
.image
.coherent
) {
108 _mesa_glsl_error(loc
, state
,
109 "function call parameter `%s' drops "
110 "`coherent' qualifier", formal
->name
);
114 if (actual
->data
.image
._volatile
&& !formal
->data
.image
._volatile
) {
115 _mesa_glsl_error(loc
, state
,
116 "function call parameter `%s' drops "
117 "`volatile' qualifier", formal
->name
);
121 if (actual
->data
.image
.restrict_flag
&& !formal
->data
.image
.restrict_flag
) {
122 _mesa_glsl_error(loc
, state
,
123 "function call parameter `%s' drops "
124 "`restrict' qualifier", formal
->name
);
128 if (actual
->data
.image
.read_only
&& !formal
->data
.image
.read_only
) {
129 _mesa_glsl_error(loc
, state
,
130 "function call parameter `%s' drops "
131 "`readonly' qualifier", formal
->name
);
135 if (actual
->data
.image
.write_only
&& !formal
->data
.image
.write_only
) {
136 _mesa_glsl_error(loc
, state
,
137 "function call parameter `%s' drops "
138 "`writeonly' qualifier", formal
->name
);
146 * Verify that 'out' and 'inout' actual parameters are lvalues. Also, verify
147 * that 'const_in' formal parameters (an extension in our IR) correspond to
148 * ir_constant actual parameters.
151 verify_parameter_modes(_mesa_glsl_parse_state
*state
,
152 ir_function_signature
*sig
,
153 exec_list
&actual_ir_parameters
,
154 exec_list
&actual_ast_parameters
)
156 exec_node
*actual_ir_node
= actual_ir_parameters
.head
;
157 exec_node
*actual_ast_node
= actual_ast_parameters
.head
;
159 foreach_in_list(const ir_variable
, formal
, &sig
->parameters
) {
160 /* The lists must be the same length. */
161 assert(!actual_ir_node
->is_tail_sentinel());
162 assert(!actual_ast_node
->is_tail_sentinel());
164 const ir_rvalue
*const actual
= (ir_rvalue
*) actual_ir_node
;
165 const ast_expression
*const actual_ast
=
166 exec_node_data(ast_expression
, actual_ast_node
, link
);
168 /* FIXME: 'loc' is incorrect (as of 2011-01-21). It is always
171 YYLTYPE loc
= actual_ast
->get_location();
173 /* Verify that 'const_in' parameters are ir_constants. */
174 if (formal
->data
.mode
== ir_var_const_in
&&
175 actual
->ir_type
!= ir_type_constant
) {
176 _mesa_glsl_error(&loc
, state
,
177 "parameter `in %s' must be a constant expression",
182 /* Verify that 'out' and 'inout' actual parameters are lvalues. */
183 if (formal
->data
.mode
== ir_var_function_out
184 || formal
->data
.mode
== ir_var_function_inout
) {
185 const char *mode
= NULL
;
186 switch (formal
->data
.mode
) {
187 case ir_var_function_out
: mode
= "out"; break;
188 case ir_var_function_inout
: mode
= "inout"; break;
189 default: assert(false); break;
192 /* This AST-based check catches errors like f(i++). The IR-based
193 * is_lvalue() is insufficient because the actual parameter at the
194 * IR-level is just a temporary value, which is an l-value.
196 if (actual_ast
->non_lvalue_description
!= NULL
) {
197 _mesa_glsl_error(&loc
, state
,
198 "function parameter '%s %s' references a %s",
200 actual_ast
->non_lvalue_description
);
204 ir_variable
*var
= actual
->variable_referenced();
206 var
->data
.assigned
= true;
208 if (var
&& var
->data
.read_only
) {
209 _mesa_glsl_error(&loc
, state
,
210 "function parameter '%s %s' references the "
211 "read-only variable '%s'",
213 actual
->variable_referenced()->name
);
215 } else if (!actual
->is_lvalue()) {
216 /* Even though ir_binop_vector_extract is not an l-value, let it
217 * slop through. generate_call will handle it correctly.
219 ir_expression
*const expr
= ((ir_rvalue
*) actual
)->as_expression();
221 || expr
->operation
!= ir_binop_vector_extract
222 || !expr
->operands
[0]->is_lvalue()) {
223 _mesa_glsl_error(&loc
, state
,
224 "function parameter '%s %s' is not an lvalue",
231 if (formal
->type
->is_image() &&
232 actual
->variable_referenced()) {
233 if (!verify_image_parameter(&loc
, state
, formal
,
234 actual
->variable_referenced()))
238 actual_ir_node
= actual_ir_node
->next
;
239 actual_ast_node
= actual_ast_node
->next
;
245 fix_parameter(void *mem_ctx
, ir_rvalue
*actual
, const glsl_type
*formal_type
,
246 exec_list
*before_instructions
, exec_list
*after_instructions
,
247 bool parameter_is_inout
)
249 ir_expression
*const expr
= actual
->as_expression();
251 /* If the types match exactly and the parameter is not a vector-extract,
252 * nothing needs to be done to fix the parameter.
254 if (formal_type
== actual
->type
255 && (expr
== NULL
|| expr
->operation
!= ir_binop_vector_extract
))
258 /* To convert an out parameter, we need to create a temporary variable to
259 * hold the value before conversion, and then perform the conversion after
260 * the function call returns.
262 * This has the effect of transforming code like this:
268 * Into IR that's equivalent to this:
272 * int out_parameter_conversion;
273 * f(out_parameter_conversion);
274 * value = float(out_parameter_conversion);
276 * If the parameter is an ir_expression of ir_binop_vector_extract,
277 * additional conversion is needed in the post-call re-write.
280 new(mem_ctx
) ir_variable(formal_type
, "inout_tmp", ir_var_temporary
);
282 before_instructions
->push_tail(tmp
);
284 /* If the parameter is an inout parameter, copy the value of the actual
285 * parameter to the new temporary. Note that no type conversion is allowed
286 * here because inout parameters must match types exactly.
288 if (parameter_is_inout
) {
289 /* Inout parameters should never require conversion, since that would
290 * require an implicit conversion to exist both to and from the formal
291 * parameter type, and there are no bidirectional implicit conversions.
293 assert (actual
->type
== formal_type
);
295 ir_dereference_variable
*const deref_tmp_1
=
296 new(mem_ctx
) ir_dereference_variable(tmp
);
297 ir_assignment
*const assignment
=
298 new(mem_ctx
) ir_assignment(deref_tmp_1
, actual
);
299 before_instructions
->push_tail(assignment
);
302 /* Replace the parameter in the call with a dereference of the new
305 ir_dereference_variable
*const deref_tmp_2
=
306 new(mem_ctx
) ir_dereference_variable(tmp
);
307 actual
->replace_with(deref_tmp_2
);
310 /* Copy the temporary variable to the actual parameter with optional
311 * type conversion applied.
313 ir_rvalue
*rhs
= new(mem_ctx
) ir_dereference_variable(tmp
);
314 if (actual
->type
!= formal_type
)
315 rhs
= convert_component(rhs
, actual
->type
);
317 ir_rvalue
*lhs
= actual
;
318 if (expr
!= NULL
&& expr
->operation
== ir_binop_vector_extract
) {
319 rhs
= new(mem_ctx
) ir_expression(ir_triop_vector_insert
,
320 expr
->operands
[0]->type
,
321 expr
->operands
[0]->clone(mem_ctx
, NULL
),
323 expr
->operands
[1]->clone(mem_ctx
, NULL
));
324 lhs
= expr
->operands
[0]->clone(mem_ctx
, NULL
);
327 ir_assignment
*const assignment_2
= new(mem_ctx
) ir_assignment(lhs
, rhs
);
328 after_instructions
->push_tail(assignment_2
);
332 * Generate a function call.
334 * For non-void functions, this returns a dereference of the temporary variable
335 * which stores the return value for the call. For void functions, this returns
339 generate_call(exec_list
*instructions
, ir_function_signature
*sig
,
340 exec_list
*actual_parameters
,
341 struct _mesa_glsl_parse_state
*state
)
344 exec_list post_call_conversions
;
346 /* Perform implicit conversion of arguments. For out parameters, we need
347 * to place them in a temporary variable and do the conversion after the
348 * call takes place. Since we haven't emitted the call yet, we'll place
349 * the post-call conversions in a temporary exec_list, and emit them later.
351 foreach_two_lists(formal_node
, &sig
->parameters
,
352 actual_node
, actual_parameters
) {
353 ir_rvalue
*actual
= (ir_rvalue
*) actual_node
;
354 ir_variable
*formal
= (ir_variable
*) formal_node
;
356 if (formal
->type
->is_numeric() || formal
->type
->is_boolean()) {
357 switch (formal
->data
.mode
) {
358 case ir_var_const_in
:
359 case ir_var_function_in
: {
361 = convert_component(actual
, formal
->type
);
362 actual
->replace_with(converted
);
365 case ir_var_function_out
:
366 case ir_var_function_inout
:
367 fix_parameter(ctx
, actual
, formal
->type
,
368 instructions
, &post_call_conversions
,
369 formal
->data
.mode
== ir_var_function_inout
);
372 assert (!"Illegal formal parameter mode");
378 /* If the function call is a constant expression, don't generate any
379 * instructions; just generate an ir_constant.
381 * Function calls were first allowed to be constant expressions in GLSL
382 * 1.20 and GLSL ES 3.00.
384 if (state
->is_version(120, 300)) {
385 ir_constant
*value
= sig
->constant_expression_value(actual_parameters
, NULL
);
391 ir_dereference_variable
*deref
= NULL
;
392 if (!sig
->return_type
->is_void()) {
393 /* Create a new temporary to hold the return value. */
396 var
= new(ctx
) ir_variable(sig
->return_type
,
397 ralloc_asprintf(ctx
, "%s_retval",
398 sig
->function_name()),
400 instructions
->push_tail(var
);
402 deref
= new(ctx
) ir_dereference_variable(var
);
404 ir_call
*call
= new(ctx
) ir_call(sig
, deref
, actual_parameters
);
405 instructions
->push_tail(call
);
407 /* Also emit any necessary out-parameter conversions. */
408 instructions
->append_list(&post_call_conversions
);
410 return deref
? deref
->clone(ctx
, NULL
) : NULL
;
414 * Given a function name and parameter list, find the matching signature.
416 static ir_function_signature
*
417 match_function_by_name(const char *name
,
418 exec_list
*actual_parameters
,
419 struct _mesa_glsl_parse_state
*state
)
422 ir_function
*f
= state
->symbols
->get_function(name
);
423 ir_function_signature
*local_sig
= NULL
;
424 ir_function_signature
*sig
= NULL
;
426 /* Is the function hidden by a record type constructor? */
427 if (state
->symbols
->get_type(name
))
428 goto done
; /* no match */
430 /* Is the function hidden by a variable (impossible in 1.10)? */
431 if (!state
->symbols
->separate_function_namespace
432 && state
->symbols
->get_variable(name
))
433 goto done
; /* no match */
436 /* Look for a match in the local shader. If exact, we're done. */
437 bool is_exact
= false;
438 sig
= local_sig
= f
->matching_signature(state
, actual_parameters
,
443 if (!state
->es_shader
&& f
->has_user_signature()) {
444 /* In desktop GL, the presence of a user-defined signature hides any
445 * built-in signatures, so we must ignore them. In contrast, in ES2
446 * user-defined signatures add new overloads, so we must proceed.
452 /* Local shader has no exact candidates; check the built-ins. */
453 _mesa_glsl_initialize_builtin_functions();
454 sig
= _mesa_glsl_find_builtin_function(state
, name
, actual_parameters
);
458 /* If the match is from a linked built-in shader, import the prototype. */
459 if (sig
!= local_sig
) {
461 f
= new(ctx
) ir_function(name
);
462 state
->symbols
->add_global_function(f
);
463 emit_function(state
, f
);
465 f
->add_signature(sig
->clone_prototype(f
, NULL
));
472 print_function_prototypes(_mesa_glsl_parse_state
*state
, YYLTYPE
*loc
,
478 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
479 if (sig
->is_builtin() && !sig
->is_builtin_available(state
))
482 char *str
= prototype_string(sig
->return_type
, f
->name
, &sig
->parameters
);
483 _mesa_glsl_error(loc
, state
, " %s", str
);
489 * Raise a "no matching function" error, listing all possible overloads the
490 * compiler considered so developers can figure out what went wrong.
493 no_matching_function_error(const char *name
,
495 exec_list
*actual_parameters
,
496 _mesa_glsl_parse_state
*state
)
498 gl_shader
*sh
= _mesa_glsl_get_builtin_function_shader();
500 if (state
->symbols
->get_function(name
) == NULL
501 && (!state
->uses_builtin_functions
502 || sh
->symbols
->get_function(name
) == NULL
)) {
503 _mesa_glsl_error(loc
, state
, "no function with name '%s'", name
);
505 char *str
= prototype_string(NULL
, name
, actual_parameters
);
506 _mesa_glsl_error(loc
, state
,
507 "no matching function for call to `%s'; candidates are:",
511 print_function_prototypes(state
, loc
, state
->symbols
->get_function(name
));
513 if (state
->uses_builtin_functions
) {
514 print_function_prototypes(state
, loc
, sh
->symbols
->get_function(name
));
520 * Perform automatic type conversion of constructor parameters
522 * This implements the rules in the "Conversion and Scalar Constructors"
523 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
526 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
)
528 void *ctx
= ralloc_parent(src
);
529 const unsigned a
= desired_type
->base_type
;
530 const unsigned b
= src
->type
->base_type
;
531 ir_expression
*result
= NULL
;
533 if (src
->type
->is_error())
536 assert(a
<= GLSL_TYPE_BOOL
);
537 assert(b
<= GLSL_TYPE_BOOL
);
546 result
= new(ctx
) ir_expression(ir_unop_i2u
, src
);
548 case GLSL_TYPE_FLOAT
:
549 result
= new(ctx
) ir_expression(ir_unop_f2u
, src
);
552 result
= new(ctx
) ir_expression(ir_unop_i2u
,
553 new(ctx
) ir_expression(ir_unop_b2i
, src
));
560 result
= new(ctx
) ir_expression(ir_unop_u2i
, src
);
562 case GLSL_TYPE_FLOAT
:
563 result
= new(ctx
) ir_expression(ir_unop_f2i
, src
);
566 result
= new(ctx
) ir_expression(ir_unop_b2i
, src
);
570 case GLSL_TYPE_FLOAT
:
573 result
= new(ctx
) ir_expression(ir_unop_u2f
, desired_type
, src
, NULL
);
576 result
= new(ctx
) ir_expression(ir_unop_i2f
, desired_type
, src
, NULL
);
579 result
= new(ctx
) ir_expression(ir_unop_b2f
, desired_type
, src
, NULL
);
586 result
= new(ctx
) ir_expression(ir_unop_i2b
,
587 new(ctx
) ir_expression(ir_unop_u2i
, src
));
590 result
= new(ctx
) ir_expression(ir_unop_i2b
, desired_type
, src
, NULL
);
592 case GLSL_TYPE_FLOAT
:
593 result
= new(ctx
) ir_expression(ir_unop_f2b
, desired_type
, src
, NULL
);
599 assert(result
!= NULL
);
600 assert(result
->type
== desired_type
);
602 /* Try constant folding; it may fold in the conversion we just added. */
603 ir_constant
*const constant
= result
->constant_expression_value();
604 return (constant
!= NULL
) ? (ir_rvalue
*) constant
: (ir_rvalue
*) result
;
608 * Dereference a specific component from a scalar, vector, or matrix
611 dereference_component(ir_rvalue
*src
, unsigned component
)
613 void *ctx
= ralloc_parent(src
);
614 assert(component
< src
->type
->components());
616 /* If the source is a constant, just create a new constant instead of a
617 * dereference of the existing constant.
619 ir_constant
*constant
= src
->as_constant();
621 return new(ctx
) ir_constant(constant
, component
);
623 if (src
->type
->is_scalar()) {
625 } else if (src
->type
->is_vector()) {
626 return new(ctx
) ir_swizzle(src
, component
, 0, 0, 0, 1);
628 assert(src
->type
->is_matrix());
630 /* Dereference a row of the matrix, then call this function again to get
631 * a specific element from that row.
633 const int c
= component
/ src
->type
->column_type()->vector_elements
;
634 const int r
= component
% src
->type
->column_type()->vector_elements
;
635 ir_constant
*const col_index
= new(ctx
) ir_constant(c
);
636 ir_dereference
*const col
= new(ctx
) ir_dereference_array(src
, col_index
);
638 col
->type
= src
->type
->column_type();
640 return dereference_component(col
, r
);
643 assert(!"Should not get here.");
649 process_vec_mat_constructor(exec_list
*instructions
,
650 const glsl_type
*constructor_type
,
651 YYLTYPE
*loc
, exec_list
*parameters
,
652 struct _mesa_glsl_parse_state
*state
)
656 /* The ARB_shading_language_420pack spec says:
658 * "If an initializer is a list of initializers enclosed in curly braces,
659 * the variable being declared must be a vector, a matrix, an array, or a
662 * int i = { 1 }; // illegal, i is not an aggregate"
664 if (constructor_type
->vector_elements
<= 1) {
665 _mesa_glsl_error(loc
, state
, "aggregates can only initialize vectors, "
666 "matrices, arrays, and structs");
667 return ir_rvalue::error_value(ctx
);
670 exec_list actual_parameters
;
671 const unsigned parameter_count
=
672 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
674 if (parameter_count
== 0
675 || (constructor_type
->is_vector() &&
676 constructor_type
->vector_elements
!= parameter_count
)
677 || (constructor_type
->is_matrix() &&
678 constructor_type
->matrix_columns
!= parameter_count
)) {
679 _mesa_glsl_error(loc
, state
, "%s constructor must have %u parameters",
680 constructor_type
->is_vector() ? "vector" : "matrix",
681 constructor_type
->vector_elements
);
682 return ir_rvalue::error_value(ctx
);
685 bool all_parameters_are_constant
= true;
687 /* Type cast each parameter and, if possible, fold constants. */
688 foreach_list_safe(n
, &actual_parameters
) {
689 ir_rvalue
*ir
= (ir_rvalue
*) n
;
690 ir_rvalue
*result
= ir
;
692 /* Apply implicit conversions (not the scalar constructor rules!). See
693 * the spec quote above. */
694 if (constructor_type
->is_float()) {
695 const glsl_type
*desired_type
=
696 glsl_type::get_instance(GLSL_TYPE_FLOAT
,
697 ir
->type
->vector_elements
,
698 ir
->type
->matrix_columns
);
699 if (result
->type
->can_implicitly_convert_to(desired_type
, state
)) {
700 /* Even though convert_component() implements the constructor
701 * conversion rules (not the implicit conversion rules), its safe
702 * to use it here because we already checked that the implicit
703 * conversion is legal.
705 result
= convert_component(ir
, desired_type
);
709 if (constructor_type
->is_matrix()) {
710 if (result
->type
!= constructor_type
->column_type()) {
711 _mesa_glsl_error(loc
, state
, "type error in matrix constructor: "
712 "expected: %s, found %s",
713 constructor_type
->column_type()->name
,
715 return ir_rvalue::error_value(ctx
);
717 } else if (result
->type
!= constructor_type
->get_scalar_type()) {
718 _mesa_glsl_error(loc
, state
, "type error in vector constructor: "
719 "expected: %s, found %s",
720 constructor_type
->get_scalar_type()->name
,
722 return ir_rvalue::error_value(ctx
);
725 /* Attempt to convert the parameter to a constant valued expression.
726 * After doing so, track whether or not all the parameters to the
727 * constructor are trivially constant valued expressions.
729 ir_rvalue
*const constant
= result
->constant_expression_value();
731 if (constant
!= NULL
)
734 all_parameters_are_constant
= false;
736 ir
->replace_with(result
);
739 if (all_parameters_are_constant
)
740 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
742 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "vec_mat_ctor",
744 instructions
->push_tail(var
);
747 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
748 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
749 new(ctx
) ir_constant(i
));
751 ir_instruction
*assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
752 instructions
->push_tail(assignment
);
757 return new(ctx
) ir_dereference_variable(var
);
762 process_array_constructor(exec_list
*instructions
,
763 const glsl_type
*constructor_type
,
764 YYLTYPE
*loc
, exec_list
*parameters
,
765 struct _mesa_glsl_parse_state
*state
)
768 /* Array constructors come in two forms: sized and unsized. Sized array
769 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
770 * variables. In this case the number of parameters must exactly match the
771 * specified size of the array.
773 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
774 * are vec4 variables. In this case the size of the array being constructed
775 * is determined by the number of parameters.
777 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
779 * "There must be exactly the same number of arguments as the size of
780 * the array being constructed. If no size is present in the
781 * constructor, then the array is explicitly sized to the number of
782 * arguments provided. The arguments are assigned in order, starting at
783 * element 0, to the elements of the constructed array. Each argument
784 * must be the same type as the element type of the array, or be a type
785 * that can be converted to the element type of the array according to
786 * Section 4.1.10 "Implicit Conversions.""
788 exec_list actual_parameters
;
789 const unsigned parameter_count
=
790 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
791 bool is_unsized_array
= constructor_type
->is_unsized_array();
793 if ((parameter_count
== 0) ||
794 (!is_unsized_array
&& (constructor_type
->length
!= parameter_count
))) {
795 const unsigned min_param
= is_unsized_array
796 ? 1 : constructor_type
->length
;
798 _mesa_glsl_error(loc
, state
, "array constructor must have %s %u "
800 is_unsized_array
? "at least" : "exactly",
801 min_param
, (min_param
<= 1) ? "" : "s");
802 return ir_rvalue::error_value(ctx
);
805 if (is_unsized_array
) {
807 glsl_type::get_array_instance(constructor_type
->element_type(),
809 assert(constructor_type
!= NULL
);
810 assert(constructor_type
->length
== parameter_count
);
813 bool all_parameters_are_constant
= true;
815 /* Type cast each parameter and, if possible, fold constants. */
816 foreach_list_safe(n
, &actual_parameters
) {
817 ir_rvalue
*ir
= (ir_rvalue
*) n
;
818 ir_rvalue
*result
= ir
;
820 /* Apply implicit conversions (not the scalar constructor rules!). See
821 * the spec quote above. */
822 if (constructor_type
->element_type()->is_float()) {
823 const glsl_type
*desired_type
=
824 glsl_type::get_instance(GLSL_TYPE_FLOAT
,
825 ir
->type
->vector_elements
,
826 ir
->type
->matrix_columns
);
827 if (result
->type
->can_implicitly_convert_to(desired_type
, state
)) {
828 /* Even though convert_component() implements the constructor
829 * conversion rules (not the implicit conversion rules), its safe
830 * to use it here because we already checked that the implicit
831 * conversion is legal.
833 result
= convert_component(ir
, desired_type
);
837 if (result
->type
!= constructor_type
->element_type()) {
838 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
839 "expected: %s, found %s",
840 constructor_type
->element_type()->name
,
842 return ir_rvalue::error_value(ctx
);
845 /* Attempt to convert the parameter to a constant valued expression.
846 * After doing so, track whether or not all the parameters to the
847 * constructor are trivially constant valued expressions.
849 ir_rvalue
*const constant
= result
->constant_expression_value();
851 if (constant
!= NULL
)
854 all_parameters_are_constant
= false;
856 ir
->replace_with(result
);
859 if (all_parameters_are_constant
)
860 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
862 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "array_ctor",
864 instructions
->push_tail(var
);
867 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
868 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
869 new(ctx
) ir_constant(i
));
871 ir_instruction
*assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
872 instructions
->push_tail(assignment
);
877 return new(ctx
) ir_dereference_variable(var
);
882 * Try to convert a record constructor to a constant expression
885 constant_record_constructor(const glsl_type
*constructor_type
,
886 exec_list
*parameters
, void *mem_ctx
)
888 foreach_in_list(ir_instruction
, node
, parameters
) {
889 ir_constant
*constant
= node
->as_constant();
890 if (constant
== NULL
)
892 node
->replace_with(constant
);
895 return new(mem_ctx
) ir_constant(constructor_type
, parameters
);
900 * Determine if a list consists of a single scalar r-value
903 single_scalar_parameter(exec_list
*parameters
)
905 const ir_rvalue
*const p
= (ir_rvalue
*) parameters
->head
;
906 assert(((ir_rvalue
*)p
)->as_rvalue() != NULL
);
908 return (p
->type
->is_scalar() && p
->next
->is_tail_sentinel());
913 * Generate inline code for a vector constructor
915 * The generated constructor code will consist of a temporary variable
916 * declaration of the same type as the constructor. A sequence of assignments
917 * from constructor parameters to the temporary will follow.
920 * An \c ir_dereference_variable of the temprorary generated in the constructor
924 emit_inline_vector_constructor(const glsl_type
*type
,
925 exec_list
*instructions
,
926 exec_list
*parameters
,
929 assert(!parameters
->is_empty());
931 ir_variable
*var
= new(ctx
) ir_variable(type
, "vec_ctor", ir_var_temporary
);
932 instructions
->push_tail(var
);
934 /* There are two kinds of vector constructors.
936 * - Construct a vector from a single scalar by replicating that scalar to
937 * all components of the vector.
939 * - Construct a vector from an arbirary combination of vectors and
940 * scalars. The components of the constructor parameters are assigned
941 * to the vector in order until the vector is full.
943 const unsigned lhs_components
= type
->components();
944 if (single_scalar_parameter(parameters
)) {
945 ir_rvalue
*first_param
= (ir_rvalue
*)parameters
->head
;
946 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(first_param
, 0, 0, 0, 0,
948 ir_dereference_variable
*lhs
= new(ctx
) ir_dereference_variable(var
);
949 const unsigned mask
= (1U << lhs_components
) - 1;
951 assert(rhs
->type
== lhs
->type
);
953 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
, mask
);
954 instructions
->push_tail(inst
);
956 unsigned base_component
= 0;
957 unsigned base_lhs_component
= 0;
958 ir_constant_data data
;
959 unsigned constant_mask
= 0, constant_components
= 0;
961 memset(&data
, 0, sizeof(data
));
963 foreach_in_list(ir_rvalue
, param
, parameters
) {
964 unsigned rhs_components
= param
->type
->components();
966 /* Do not try to assign more components to the vector than it has!
968 if ((rhs_components
+ base_lhs_component
) > lhs_components
) {
969 rhs_components
= lhs_components
- base_lhs_component
;
972 const ir_constant
*const c
= param
->as_constant();
974 for (unsigned i
= 0; i
< rhs_components
; i
++) {
975 switch (c
->type
->base_type
) {
977 data
.u
[i
+ base_component
] = c
->get_uint_component(i
);
980 data
.i
[i
+ base_component
] = c
->get_int_component(i
);
982 case GLSL_TYPE_FLOAT
:
983 data
.f
[i
+ base_component
] = c
->get_float_component(i
);
986 data
.b
[i
+ base_component
] = c
->get_bool_component(i
);
989 assert(!"Should not get here.");
994 /* Mask of fields to be written in the assignment.
996 constant_mask
|= ((1U << rhs_components
) - 1) << base_lhs_component
;
997 constant_components
+= rhs_components
;
999 base_component
+= rhs_components
;
1001 /* Advance the component index by the number of components
1002 * that were just assigned.
1004 base_lhs_component
+= rhs_components
;
1007 if (constant_mask
!= 0) {
1008 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1009 const glsl_type
*rhs_type
= glsl_type::get_instance(var
->type
->base_type
,
1010 constant_components
,
1012 ir_rvalue
*rhs
= new(ctx
) ir_constant(rhs_type
, &data
);
1014 ir_instruction
*inst
=
1015 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, constant_mask
);
1016 instructions
->push_tail(inst
);
1020 foreach_in_list(ir_rvalue
, param
, parameters
) {
1021 unsigned rhs_components
= param
->type
->components();
1023 /* Do not try to assign more components to the vector than it has!
1025 if ((rhs_components
+ base_component
) > lhs_components
) {
1026 rhs_components
= lhs_components
- base_component
;
1029 const ir_constant
*const c
= param
->as_constant();
1031 /* Mask of fields to be written in the assignment.
1033 const unsigned write_mask
= ((1U << rhs_components
) - 1)
1036 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1038 /* Generate a swizzle so that LHS and RHS sizes match.
1041 new(ctx
) ir_swizzle(param
, 0, 1, 2, 3, rhs_components
);
1043 ir_instruction
*inst
=
1044 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1045 instructions
->push_tail(inst
);
1048 /* Advance the component index by the number of components that were
1051 base_component
+= rhs_components
;
1054 return new(ctx
) ir_dereference_variable(var
);
1059 * Generate assignment of a portion of a vector to a portion of a matrix column
1061 * \param src_base First component of the source to be used in assignment
1062 * \param column Column of destination to be assiged
1063 * \param row_base First component of the destination column to be assigned
1064 * \param count Number of components to be assigned
1067 * \c src_base + \c count must be less than or equal to the number of components
1068 * in the source vector.
1071 assign_to_matrix_column(ir_variable
*var
, unsigned column
, unsigned row_base
,
1072 ir_rvalue
*src
, unsigned src_base
, unsigned count
,
1075 ir_constant
*col_idx
= new(mem_ctx
) ir_constant(column
);
1076 ir_dereference
*column_ref
= new(mem_ctx
) ir_dereference_array(var
, col_idx
);
1078 assert(column_ref
->type
->components() >= (row_base
+ count
));
1079 assert(src
->type
->components() >= (src_base
+ count
));
1081 /* Generate a swizzle that extracts the number of components from the source
1082 * that are to be assigned to the column of the matrix.
1084 if (count
< src
->type
->vector_elements
) {
1085 src
= new(mem_ctx
) ir_swizzle(src
,
1086 src_base
+ 0, src_base
+ 1,
1087 src_base
+ 2, src_base
+ 3,
1091 /* Mask of fields to be written in the assignment.
1093 const unsigned write_mask
= ((1U << count
) - 1) << row_base
;
1095 return new(mem_ctx
) ir_assignment(column_ref
, src
, NULL
, write_mask
);
1100 * Generate inline code for a matrix constructor
1102 * The generated constructor code will consist of a temporary variable
1103 * declaration of the same type as the constructor. A sequence of assignments
1104 * from constructor parameters to the temporary will follow.
1107 * An \c ir_dereference_variable of the temprorary generated in the constructor
1111 emit_inline_matrix_constructor(const glsl_type
*type
,
1112 exec_list
*instructions
,
1113 exec_list
*parameters
,
1116 assert(!parameters
->is_empty());
1118 ir_variable
*var
= new(ctx
) ir_variable(type
, "mat_ctor", ir_var_temporary
);
1119 instructions
->push_tail(var
);
1121 /* There are three kinds of matrix constructors.
1123 * - Construct a matrix from a single scalar by replicating that scalar to
1124 * along the diagonal of the matrix and setting all other components to
1127 * - Construct a matrix from an arbirary combination of vectors and
1128 * scalars. The components of the constructor parameters are assigned
1129 * to the matrix in colum-major order until the matrix is full.
1131 * - Construct a matrix from a single matrix. The source matrix is copied
1132 * to the upper left portion of the constructed matrix, and the remaining
1133 * elements take values from the identity matrix.
1135 ir_rvalue
*const first_param
= (ir_rvalue
*) parameters
->head
;
1136 if (single_scalar_parameter(parameters
)) {
1137 /* Assign the scalar to the X component of a vec4, and fill the remaining
1138 * components with zero.
1140 ir_variable
*rhs_var
=
1141 new(ctx
) ir_variable(glsl_type::vec4_type
, "mat_ctor_vec",
1143 instructions
->push_tail(rhs_var
);
1145 ir_constant_data zero
;
1151 ir_instruction
*inst
=
1152 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(rhs_var
),
1153 new(ctx
) ir_constant(rhs_var
->type
, &zero
),
1155 instructions
->push_tail(inst
);
1157 ir_dereference
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1159 inst
= new(ctx
) ir_assignment(rhs_ref
, first_param
, NULL
, 0x01);
1160 instructions
->push_tail(inst
);
1162 /* Assign the temporary vector to each column of the destination matrix
1163 * with a swizzle that puts the X component on the diagonal of the
1164 * matrix. In some cases this may mean that the X component does not
1165 * get assigned into the column at all (i.e., when the matrix has more
1166 * columns than rows).
1168 static const unsigned rhs_swiz
[4][4] = {
1175 const unsigned cols_to_init
= MIN2(type
->matrix_columns
,
1176 type
->vector_elements
);
1177 for (unsigned i
= 0; i
< cols_to_init
; i
++) {
1178 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1179 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
1181 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1182 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, rhs_swiz
[i
],
1183 type
->vector_elements
);
1185 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
1186 instructions
->push_tail(inst
);
1189 for (unsigned i
= cols_to_init
; i
< type
->matrix_columns
; i
++) {
1190 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1191 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
1193 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1194 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, 1, 1, 1, 1,
1195 type
->vector_elements
);
1197 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
1198 instructions
->push_tail(inst
);
1200 } else if (first_param
->type
->is_matrix()) {
1201 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
1203 * "If a matrix is constructed from a matrix, then each component
1204 * (column i, row j) in the result that has a corresponding
1205 * component (column i, row j) in the argument will be initialized
1206 * from there. All other components will be initialized to the
1207 * identity matrix. If a matrix argument is given to a matrix
1208 * constructor, it is an error to have any other arguments."
1210 assert(first_param
->next
->is_tail_sentinel());
1211 ir_rvalue
*const src_matrix
= first_param
;
1213 /* If the source matrix is smaller, pre-initialize the relavent parts of
1214 * the destination matrix to the identity matrix.
1216 if ((src_matrix
->type
->matrix_columns
< var
->type
->matrix_columns
)
1217 || (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)) {
1219 /* If the source matrix has fewer rows, every column of the destination
1220 * must be initialized. Otherwise only the columns in the destination
1221 * that do not exist in the source must be initialized.
1224 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)
1225 ? 0 : src_matrix
->type
->matrix_columns
;
1227 const glsl_type
*const col_type
= var
->type
->column_type();
1228 for (/* empty */; col
< var
->type
->matrix_columns
; col
++) {
1229 ir_constant_data ident
;
1238 ir_rvalue
*const rhs
= new(ctx
) ir_constant(col_type
, &ident
);
1240 ir_rvalue
*const lhs
=
1241 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(col
));
1243 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
1244 instructions
->push_tail(inst
);
1248 /* Assign columns from the source matrix to the destination matrix.
1250 * Since the parameter will be used in the RHS of multiple assignments,
1251 * generate a temporary and copy the paramter there.
1253 ir_variable
*const rhs_var
=
1254 new(ctx
) ir_variable(first_param
->type
, "mat_ctor_mat",
1256 instructions
->push_tail(rhs_var
);
1258 ir_dereference
*const rhs_var_ref
=
1259 new(ctx
) ir_dereference_variable(rhs_var
);
1260 ir_instruction
*const inst
=
1261 new(ctx
) ir_assignment(rhs_var_ref
, first_param
, NULL
);
1262 instructions
->push_tail(inst
);
1264 const unsigned last_row
= MIN2(src_matrix
->type
->vector_elements
,
1265 var
->type
->vector_elements
);
1266 const unsigned last_col
= MIN2(src_matrix
->type
->matrix_columns
,
1267 var
->type
->matrix_columns
);
1269 unsigned swiz
[4] = { 0, 0, 0, 0 };
1270 for (unsigned i
= 1; i
< last_row
; i
++)
1273 const unsigned write_mask
= (1U << last_row
) - 1;
1275 for (unsigned i
= 0; i
< last_col
; i
++) {
1276 ir_dereference
*const lhs
=
1277 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
1278 ir_rvalue
*const rhs_col
=
1279 new(ctx
) ir_dereference_array(rhs_var
, new(ctx
) ir_constant(i
));
1281 /* If one matrix has columns that are smaller than the columns of the
1282 * other matrix, wrap the column access of the larger with a swizzle
1283 * so that the LHS and RHS of the assignment have the same size (and
1284 * therefore have the same type).
1286 * It would be perfectly valid to unconditionally generate the
1287 * swizzles, this this will typically result in a more compact IR tree.
1290 if (lhs
->type
->vector_elements
!= rhs_col
->type
->vector_elements
) {
1291 rhs
= new(ctx
) ir_swizzle(rhs_col
, swiz
, last_row
);
1296 ir_instruction
*inst
=
1297 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1298 instructions
->push_tail(inst
);
1301 const unsigned cols
= type
->matrix_columns
;
1302 const unsigned rows
= type
->vector_elements
;
1303 unsigned col_idx
= 0;
1304 unsigned row_idx
= 0;
1306 foreach_in_list(ir_rvalue
, rhs
, parameters
) {
1307 const unsigned components_remaining_this_column
= rows
- row_idx
;
1308 unsigned rhs_components
= rhs
->type
->components();
1309 unsigned rhs_base
= 0;
1311 /* Since the parameter might be used in the RHS of two assignments,
1312 * generate a temporary and copy the paramter there.
1314 ir_variable
*rhs_var
=
1315 new(ctx
) ir_variable(rhs
->type
, "mat_ctor_vec", ir_var_temporary
);
1316 instructions
->push_tail(rhs_var
);
1318 ir_dereference
*rhs_var_ref
=
1319 new(ctx
) ir_dereference_variable(rhs_var
);
1320 ir_instruction
*inst
= new(ctx
) ir_assignment(rhs_var_ref
, rhs
, NULL
);
1321 instructions
->push_tail(inst
);
1323 /* Assign the current parameter to as many components of the matrix
1326 * NOTE: A single vector parameter can span two matrix columns. A
1327 * single vec4, for example, can completely fill a mat2.
1329 if (rhs_components
>= components_remaining_this_column
) {
1330 const unsigned count
= MIN2(rhs_components
,
1331 components_remaining_this_column
);
1333 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1335 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
1339 instructions
->push_tail(inst
);
1347 /* If there is data left in the parameter and components left to be
1348 * set in the destination, emit another assignment. It is possible
1349 * that the assignment could be of a vec4 to the last element of the
1350 * matrix. In this case col_idx==cols, but there is still data
1351 * left in the source parameter. Obviously, don't emit an assignment
1352 * to data outside the destination matrix.
1354 if ((col_idx
< cols
) && (rhs_base
< rhs_components
)) {
1355 const unsigned count
= rhs_components
- rhs_base
;
1357 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1359 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
1364 instructions
->push_tail(inst
);
1371 return new(ctx
) ir_dereference_variable(var
);
1376 emit_inline_record_constructor(const glsl_type
*type
,
1377 exec_list
*instructions
,
1378 exec_list
*parameters
,
1381 ir_variable
*const var
=
1382 new(mem_ctx
) ir_variable(type
, "record_ctor", ir_var_temporary
);
1383 ir_dereference_variable
*const d
= new(mem_ctx
) ir_dereference_variable(var
);
1385 instructions
->push_tail(var
);
1387 exec_node
*node
= parameters
->head
;
1388 for (unsigned i
= 0; i
< type
->length
; i
++) {
1389 assert(!node
->is_tail_sentinel());
1391 ir_dereference
*const lhs
=
1392 new(mem_ctx
) ir_dereference_record(d
->clone(mem_ctx
, NULL
),
1393 type
->fields
.structure
[i
].name
);
1395 ir_rvalue
*const rhs
= ((ir_instruction
*) node
)->as_rvalue();
1396 assert(rhs
!= NULL
);
1398 ir_instruction
*const assign
= new(mem_ctx
) ir_assignment(lhs
, rhs
, NULL
);
1400 instructions
->push_tail(assign
);
1409 process_record_constructor(exec_list
*instructions
,
1410 const glsl_type
*constructor_type
,
1411 YYLTYPE
*loc
, exec_list
*parameters
,
1412 struct _mesa_glsl_parse_state
*state
)
1415 exec_list actual_parameters
;
1417 process_parameters(instructions
, &actual_parameters
,
1420 exec_node
*node
= actual_parameters
.head
;
1421 for (unsigned i
= 0; i
< constructor_type
->length
; i
++) {
1422 ir_rvalue
*ir
= (ir_rvalue
*) node
;
1424 if (node
->is_tail_sentinel()) {
1425 _mesa_glsl_error(loc
, state
,
1426 "insufficient parameters to constructor for `%s'",
1427 constructor_type
->name
);
1428 return ir_rvalue::error_value(ctx
);
1431 if (apply_implicit_conversion(constructor_type
->fields
.structure
[i
].type
,
1433 node
->replace_with(ir
);
1435 _mesa_glsl_error(loc
, state
,
1436 "parameter type mismatch in constructor for `%s.%s' "
1438 constructor_type
->name
,
1439 constructor_type
->fields
.structure
[i
].name
,
1441 constructor_type
->fields
.structure
[i
].type
->name
);
1442 return ir_rvalue::error_value(ctx
);;
1448 if (!node
->is_tail_sentinel()) {
1449 _mesa_glsl_error(loc
, state
, "too many parameters in constructor "
1450 "for `%s'", constructor_type
->name
);
1451 return ir_rvalue::error_value(ctx
);
1454 ir_rvalue
*const constant
=
1455 constant_record_constructor(constructor_type
, &actual_parameters
,
1458 return (constant
!= NULL
)
1460 : emit_inline_record_constructor(constructor_type
, instructions
,
1461 &actual_parameters
, state
);
1466 ast_function_expression::hir(exec_list
*instructions
,
1467 struct _mesa_glsl_parse_state
*state
)
1470 /* There are three sorts of function calls.
1472 * 1. constructors - The first subexpression is an ast_type_specifier.
1473 * 2. methods - Only the .length() method of array types.
1474 * 3. functions - Calls to regular old functions.
1476 * Method calls are actually detected when the ast_field_selection
1477 * expression is handled.
1479 if (is_constructor()) {
1480 const ast_type_specifier
*type
= (ast_type_specifier
*) subexpressions
[0];
1481 YYLTYPE loc
= type
->get_location();
1484 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
1486 /* constructor_type can be NULL if a variable with the same name as the
1487 * structure has come into scope.
1489 if (constructor_type
== NULL
) {
1490 _mesa_glsl_error(& loc
, state
, "unknown type `%s' (structure name "
1491 "may be shadowed by a variable with the same name)",
1493 return ir_rvalue::error_value(ctx
);
1497 /* Constructors for samplers are illegal.
1499 if (constructor_type
->is_sampler()) {
1500 _mesa_glsl_error(& loc
, state
, "cannot construct sampler type `%s'",
1501 constructor_type
->name
);
1502 return ir_rvalue::error_value(ctx
);
1505 if (constructor_type
->is_array()) {
1506 if (!state
->check_version(120, 300, &loc
,
1507 "array constructors forbidden")) {
1508 return ir_rvalue::error_value(ctx
);
1511 return process_array_constructor(instructions
, constructor_type
,
1512 & loc
, &this->expressions
, state
);
1516 /* There are two kinds of constructor calls. Constructors for arrays and
1517 * structures must have the exact number of arguments with matching types
1518 * in the correct order. These constructors follow essentially the same
1519 * type matching rules as functions.
1521 * Constructors for built-in language types, such as mat4 and vec2, are
1522 * free form. The only requirements are that the parameters must provide
1523 * enough values of the correct scalar type and that no arguments are
1524 * given past the last used argument.
1526 * When using the C-style initializer syntax from GLSL 4.20, constructors
1527 * must have the exact number of arguments with matching types in the
1530 if (constructor_type
->is_record()) {
1531 return process_record_constructor(instructions
, constructor_type
,
1532 &loc
, &this->expressions
,
1536 if (!constructor_type
->is_numeric() && !constructor_type
->is_boolean())
1537 return ir_rvalue::error_value(ctx
);
1539 /* Total number of components of the type being constructed. */
1540 const unsigned type_components
= constructor_type
->components();
1542 /* Number of components from parameters that have actually been
1543 * consumed. This is used to perform several kinds of error checking.
1545 unsigned components_used
= 0;
1547 unsigned matrix_parameters
= 0;
1548 unsigned nonmatrix_parameters
= 0;
1549 exec_list actual_parameters
;
1551 foreach_list (n
, &this->expressions
) {
1552 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
1553 ir_rvalue
*result
= ast
->hir(instructions
, state
);
1555 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1557 * "It is an error to provide extra arguments beyond this
1558 * last used argument."
1560 if (components_used
>= type_components
) {
1561 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
1563 constructor_type
->name
);
1564 return ir_rvalue::error_value(ctx
);
1567 if (!result
->type
->is_numeric() && !result
->type
->is_boolean()) {
1568 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
1569 "non-numeric data type",
1570 constructor_type
->name
);
1571 return ir_rvalue::error_value(ctx
);
1574 /* Count the number of matrix and nonmatrix parameters. This
1575 * is used below to enforce some of the constructor rules.
1577 if (result
->type
->is_matrix())
1578 matrix_parameters
++;
1580 nonmatrix_parameters
++;
1582 actual_parameters
.push_tail(result
);
1583 components_used
+= result
->type
->components();
1586 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1588 * "It is an error to construct matrices from other matrices. This
1589 * is reserved for future use."
1591 if (matrix_parameters
> 0
1592 && constructor_type
->is_matrix()
1593 && !state
->check_version(120, 100, &loc
,
1594 "cannot construct `%s' from a matrix",
1595 constructor_type
->name
)) {
1596 return ir_rvalue::error_value(ctx
);
1599 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1601 * "If a matrix argument is given to a matrix constructor, it is
1602 * an error to have any other arguments."
1604 if ((matrix_parameters
> 0)
1605 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
1606 && constructor_type
->is_matrix()) {
1607 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
1608 "matrix must be only parameter",
1609 constructor_type
->name
);
1610 return ir_rvalue::error_value(ctx
);
1613 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1615 * "In these cases, there must be enough components provided in the
1616 * arguments to provide an initializer for every component in the
1617 * constructed value."
1619 if (components_used
< type_components
&& components_used
!= 1
1620 && matrix_parameters
== 0) {
1621 _mesa_glsl_error(& loc
, state
, "too few components to construct "
1623 constructor_type
->name
);
1624 return ir_rvalue::error_value(ctx
);
1627 /* Later, we cast each parameter to the same base type as the
1628 * constructor. Since there are no non-floating point matrices, we
1629 * need to break them up into a series of column vectors.
1631 if (constructor_type
->base_type
!= GLSL_TYPE_FLOAT
) {
1632 foreach_list_safe(n
, &actual_parameters
) {
1633 ir_rvalue
*matrix
= (ir_rvalue
*) n
;
1635 if (!matrix
->type
->is_matrix())
1638 /* Create a temporary containing the matrix. */
1639 ir_variable
*var
= new(ctx
) ir_variable(matrix
->type
, "matrix_tmp",
1641 instructions
->push_tail(var
);
1642 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
)
1643 ir_dereference_variable(var
), matrix
, NULL
));
1644 var
->constant_value
= matrix
->constant_expression_value();
1646 /* Replace the matrix with dereferences of its columns. */
1647 for (int i
= 0; i
< matrix
->type
->matrix_columns
; i
++) {
1648 matrix
->insert_before(new (ctx
) ir_dereference_array(var
,
1649 new(ctx
) ir_constant(i
)));
1655 bool all_parameters_are_constant
= true;
1657 /* Type cast each parameter and, if possible, fold constants.*/
1658 foreach_list_safe(n
, &actual_parameters
) {
1659 ir_rvalue
*ir
= (ir_rvalue
*) n
;
1661 const glsl_type
*desired_type
=
1662 glsl_type::get_instance(constructor_type
->base_type
,
1663 ir
->type
->vector_elements
,
1664 ir
->type
->matrix_columns
);
1665 ir_rvalue
*result
= convert_component(ir
, desired_type
);
1667 /* Attempt to convert the parameter to a constant valued expression.
1668 * After doing so, track whether or not all the parameters to the
1669 * constructor are trivially constant valued expressions.
1671 ir_rvalue
*const constant
= result
->constant_expression_value();
1673 if (constant
!= NULL
)
1676 all_parameters_are_constant
= false;
1679 ir
->replace_with(result
);
1683 /* If all of the parameters are trivially constant, create a
1684 * constant representing the complete collection of parameters.
1686 if (all_parameters_are_constant
) {
1687 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1688 } else if (constructor_type
->is_scalar()) {
1689 return dereference_component((ir_rvalue
*) actual_parameters
.head
,
1691 } else if (constructor_type
->is_vector()) {
1692 return emit_inline_vector_constructor(constructor_type
,
1697 assert(constructor_type
->is_matrix());
1698 return emit_inline_matrix_constructor(constructor_type
,
1704 const ast_expression
*id
= subexpressions
[0];
1705 const char *func_name
= id
->primary_expression
.identifier
;
1706 YYLTYPE loc
= get_location();
1707 exec_list actual_parameters
;
1709 process_parameters(instructions
, &actual_parameters
, &this->expressions
,
1712 ir_function_signature
*sig
=
1713 match_function_by_name(func_name
, &actual_parameters
, state
);
1715 ir_rvalue
*value
= NULL
;
1717 no_matching_function_error(func_name
, &loc
, &actual_parameters
, state
);
1718 value
= ir_rvalue::error_value(ctx
);
1719 } else if (!verify_parameter_modes(state
, sig
, actual_parameters
, this->expressions
)) {
1720 /* an error has already been emitted */
1721 value
= ir_rvalue::error_value(ctx
);
1723 value
= generate_call(instructions
, sig
, &actual_parameters
, state
);
1729 return ir_rvalue::error_value(ctx
);
1733 ast_aggregate_initializer::hir(exec_list
*instructions
,
1734 struct _mesa_glsl_parse_state
*state
)
1737 YYLTYPE loc
= this->get_location();
1739 if (!this->constructor_type
) {
1740 _mesa_glsl_error(&loc
, state
, "type of C-style initializer unknown");
1741 return ir_rvalue::error_value(ctx
);
1743 const glsl_type
*const constructor_type
= this->constructor_type
;
1745 if (!state
->ARB_shading_language_420pack_enable
) {
1746 _mesa_glsl_error(&loc
, state
, "C-style initialization requires the "
1747 "GL_ARB_shading_language_420pack extension");
1748 return ir_rvalue::error_value(ctx
);
1751 if (constructor_type
->is_array()) {
1752 return process_array_constructor(instructions
, constructor_type
, &loc
,
1753 &this->expressions
, state
);
1756 if (constructor_type
->is_record()) {
1757 return process_record_constructor(instructions
, constructor_type
, &loc
,
1758 &this->expressions
, state
);
1761 return process_vec_mat_constructor(instructions
, constructor_type
, &loc
,
1762 &this->expressions
, state
);