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_typed(ast_node
, ast
, link
, parameters
) {
45 ir_rvalue
*result
= ast
->hir(instructions
, state
);
47 ir_constant
*const constant
= result
->constant_expression_value();
51 actual_parameters
->push_tail(result
);
60 * Generate a source prototype for a function signature
62 * \param return_type Return type of the function. May be \c NULL.
63 * \param name Name of the function.
64 * \param parameters List of \c ir_instruction nodes representing the
65 * parameter list for the function. This may be either a
66 * formal (\c ir_variable) or actual (\c ir_rvalue)
67 * parameter list. Only the type is used.
70 * A ralloced string representing the prototype of the function.
73 prototype_string(const glsl_type
*return_type
, const char *name
,
74 exec_list
*parameters
)
78 if (return_type
!= NULL
)
79 str
= ralloc_asprintf(NULL
, "%s ", return_type
->name
);
81 ralloc_asprintf_append(&str
, "%s(", name
);
83 const char *comma
= "";
84 foreach_in_list(const ir_variable
, param
, parameters
) {
85 ralloc_asprintf_append(&str
, "%s%s", comma
, param
->type
->name
);
89 ralloc_strcat(&str
, ")");
94 verify_image_parameter(YYLTYPE
*loc
, _mesa_glsl_parse_state
*state
,
95 const ir_variable
*formal
, const ir_variable
*actual
)
98 * From the ARB_shader_image_load_store specification:
100 * "The values of image variables qualified with coherent,
101 * volatile, restrict, readonly, or writeonly may not be passed
102 * to functions whose formal parameters lack such
103 * qualifiers. [...] It is legal to have additional qualifiers
104 * on a formal parameter, but not to have fewer."
106 if (actual
->data
.image
.coherent
&& !formal
->data
.image
.coherent
) {
107 _mesa_glsl_error(loc
, state
,
108 "function call parameter `%s' drops "
109 "`coherent' qualifier", formal
->name
);
113 if (actual
->data
.image
._volatile
&& !formal
->data
.image
._volatile
) {
114 _mesa_glsl_error(loc
, state
,
115 "function call parameter `%s' drops "
116 "`volatile' qualifier", formal
->name
);
120 if (actual
->data
.image
.restrict_flag
&& !formal
->data
.image
.restrict_flag
) {
121 _mesa_glsl_error(loc
, state
,
122 "function call parameter `%s' drops "
123 "`restrict' qualifier", formal
->name
);
127 if (actual
->data
.image
.read_only
&& !formal
->data
.image
.read_only
) {
128 _mesa_glsl_error(loc
, state
,
129 "function call parameter `%s' drops "
130 "`readonly' qualifier", formal
->name
);
134 if (actual
->data
.image
.write_only
&& !formal
->data
.image
.write_only
) {
135 _mesa_glsl_error(loc
, state
,
136 "function call parameter `%s' drops "
137 "`writeonly' qualifier", formal
->name
);
145 * Verify that 'out' and 'inout' actual parameters are lvalues. Also, verify
146 * that 'const_in' formal parameters (an extension in our IR) correspond to
147 * ir_constant actual parameters.
150 verify_parameter_modes(_mesa_glsl_parse_state
*state
,
151 ir_function_signature
*sig
,
152 exec_list
&actual_ir_parameters
,
153 exec_list
&actual_ast_parameters
)
155 exec_node
*actual_ir_node
= actual_ir_parameters
.head
;
156 exec_node
*actual_ast_node
= actual_ast_parameters
.head
;
158 foreach_in_list(const ir_variable
, formal
, &sig
->parameters
) {
159 /* The lists must be the same length. */
160 assert(!actual_ir_node
->is_tail_sentinel());
161 assert(!actual_ast_node
->is_tail_sentinel());
163 const ir_rvalue
*const actual
= (ir_rvalue
*) actual_ir_node
;
164 const ast_expression
*const actual_ast
=
165 exec_node_data(ast_expression
, actual_ast_node
, link
);
167 /* FIXME: 'loc' is incorrect (as of 2011-01-21). It is always
170 YYLTYPE loc
= actual_ast
->get_location();
172 /* Verify that 'const_in' parameters are ir_constants. */
173 if (formal
->data
.mode
== ir_var_const_in
&&
174 actual
->ir_type
!= ir_type_constant
) {
175 _mesa_glsl_error(&loc
, state
,
176 "parameter `in %s' must be a constant expression",
181 /* Verify that 'out' and 'inout' actual parameters are lvalues. */
182 if (formal
->data
.mode
== ir_var_function_out
183 || formal
->data
.mode
== ir_var_function_inout
) {
184 const char *mode
= NULL
;
185 switch (formal
->data
.mode
) {
186 case ir_var_function_out
: mode
= "out"; break;
187 case ir_var_function_inout
: mode
= "inout"; break;
188 default: assert(false); break;
191 /* This AST-based check catches errors like f(i++). The IR-based
192 * is_lvalue() is insufficient because the actual parameter at the
193 * IR-level is just a temporary value, which is an l-value.
195 if (actual_ast
->non_lvalue_description
!= NULL
) {
196 _mesa_glsl_error(&loc
, state
,
197 "function parameter '%s %s' references a %s",
199 actual_ast
->non_lvalue_description
);
203 ir_variable
*var
= actual
->variable_referenced();
205 var
->data
.assigned
= true;
207 if (var
&& var
->data
.read_only
) {
208 _mesa_glsl_error(&loc
, state
,
209 "function parameter '%s %s' references the "
210 "read-only variable '%s'",
212 actual
->variable_referenced()->name
);
214 } else if (!actual
->is_lvalue()) {
215 /* Even though ir_binop_vector_extract is not an l-value, let it
216 * slop through. generate_call will handle it correctly.
218 ir_expression
*const expr
= ((ir_rvalue
*) actual
)->as_expression();
220 || expr
->operation
!= ir_binop_vector_extract
221 || !expr
->operands
[0]->is_lvalue()) {
222 _mesa_glsl_error(&loc
, state
,
223 "function parameter '%s %s' is not an lvalue",
230 if (formal
->type
->is_image() &&
231 actual
->variable_referenced()) {
232 if (!verify_image_parameter(&loc
, state
, formal
,
233 actual
->variable_referenced()))
237 actual_ir_node
= actual_ir_node
->next
;
238 actual_ast_node
= actual_ast_node
->next
;
244 fix_parameter(void *mem_ctx
, ir_rvalue
*actual
, const glsl_type
*formal_type
,
245 exec_list
*before_instructions
, exec_list
*after_instructions
,
246 bool parameter_is_inout
)
248 ir_expression
*const expr
= actual
->as_expression();
250 /* If the types match exactly and the parameter is not a vector-extract,
251 * nothing needs to be done to fix the parameter.
253 if (formal_type
== actual
->type
254 && (expr
== NULL
|| expr
->operation
!= ir_binop_vector_extract
))
257 /* To convert an out parameter, we need to create a temporary variable to
258 * hold the value before conversion, and then perform the conversion after
259 * the function call returns.
261 * This has the effect of transforming code like this:
267 * Into IR that's equivalent to this:
271 * int out_parameter_conversion;
272 * f(out_parameter_conversion);
273 * value = float(out_parameter_conversion);
275 * If the parameter is an ir_expression of ir_binop_vector_extract,
276 * additional conversion is needed in the post-call re-write.
279 new(mem_ctx
) ir_variable(formal_type
, "inout_tmp", ir_var_temporary
);
281 before_instructions
->push_tail(tmp
);
283 /* If the parameter is an inout parameter, copy the value of the actual
284 * parameter to the new temporary. Note that no type conversion is allowed
285 * here because inout parameters must match types exactly.
287 if (parameter_is_inout
) {
288 /* Inout parameters should never require conversion, since that would
289 * require an implicit conversion to exist both to and from the formal
290 * parameter type, and there are no bidirectional implicit conversions.
292 assert (actual
->type
== formal_type
);
294 ir_dereference_variable
*const deref_tmp_1
=
295 new(mem_ctx
) ir_dereference_variable(tmp
);
296 ir_assignment
*const assignment
=
297 new(mem_ctx
) ir_assignment(deref_tmp_1
, actual
);
298 before_instructions
->push_tail(assignment
);
301 /* Replace the parameter in the call with a dereference of the new
304 ir_dereference_variable
*const deref_tmp_2
=
305 new(mem_ctx
) ir_dereference_variable(tmp
);
306 actual
->replace_with(deref_tmp_2
);
309 /* Copy the temporary variable to the actual parameter with optional
310 * type conversion applied.
312 ir_rvalue
*rhs
= new(mem_ctx
) ir_dereference_variable(tmp
);
313 if (actual
->type
!= formal_type
)
314 rhs
= convert_component(rhs
, actual
->type
);
316 ir_rvalue
*lhs
= actual
;
317 if (expr
!= NULL
&& expr
->operation
== ir_binop_vector_extract
) {
318 rhs
= new(mem_ctx
) ir_expression(ir_triop_vector_insert
,
319 expr
->operands
[0]->type
,
320 expr
->operands
[0]->clone(mem_ctx
, NULL
),
322 expr
->operands
[1]->clone(mem_ctx
, NULL
));
323 lhs
= expr
->operands
[0]->clone(mem_ctx
, NULL
);
326 ir_assignment
*const assignment_2
= new(mem_ctx
) ir_assignment(lhs
, rhs
);
327 after_instructions
->push_tail(assignment_2
);
331 * Generate a function call.
333 * For non-void functions, this returns a dereference of the temporary variable
334 * which stores the return value for the call. For void functions, this returns
338 generate_call(exec_list
*instructions
, ir_function_signature
*sig
,
339 exec_list
*actual_parameters
,
340 struct _mesa_glsl_parse_state
*state
)
343 exec_list post_call_conversions
;
345 /* Perform implicit conversion of arguments. For out parameters, we need
346 * to place them in a temporary variable and do the conversion after the
347 * call takes place. Since we haven't emitted the call yet, we'll place
348 * the post-call conversions in a temporary exec_list, and emit them later.
350 foreach_two_lists(formal_node
, &sig
->parameters
,
351 actual_node
, actual_parameters
) {
352 ir_rvalue
*actual
= (ir_rvalue
*) actual_node
;
353 ir_variable
*formal
= (ir_variable
*) formal_node
;
355 if (formal
->type
->is_numeric() || formal
->type
->is_boolean()) {
356 switch (formal
->data
.mode
) {
357 case ir_var_const_in
:
358 case ir_var_function_in
: {
360 = convert_component(actual
, formal
->type
);
361 actual
->replace_with(converted
);
364 case ir_var_function_out
:
365 case ir_var_function_inout
:
366 fix_parameter(ctx
, actual
, formal
->type
,
367 instructions
, &post_call_conversions
,
368 formal
->data
.mode
== ir_var_function_inout
);
371 assert (!"Illegal formal parameter mode");
377 /* If the function call is a constant expression, don't generate any
378 * instructions; just generate an ir_constant.
380 * Function calls were first allowed to be constant expressions in GLSL
381 * 1.20 and GLSL ES 3.00.
383 if (state
->is_version(120, 300)) {
384 ir_constant
*value
= sig
->constant_expression_value(actual_parameters
, NULL
);
390 ir_dereference_variable
*deref
= NULL
;
391 if (!sig
->return_type
->is_void()) {
392 /* Create a new temporary to hold the return value. */
395 var
= new(ctx
) ir_variable(sig
->return_type
,
396 ralloc_asprintf(ctx
, "%s_retval",
397 sig
->function_name()),
399 instructions
->push_tail(var
);
401 deref
= new(ctx
) ir_dereference_variable(var
);
403 ir_call
*call
= new(ctx
) ir_call(sig
, deref
, actual_parameters
);
404 instructions
->push_tail(call
);
406 /* Also emit any necessary out-parameter conversions. */
407 instructions
->append_list(&post_call_conversions
);
409 return deref
? deref
->clone(ctx
, NULL
) : NULL
;
413 * Given a function name and parameter list, find the matching signature.
415 static ir_function_signature
*
416 match_function_by_name(const char *name
,
417 exec_list
*actual_parameters
,
418 struct _mesa_glsl_parse_state
*state
)
421 ir_function
*f
= state
->symbols
->get_function(name
);
422 ir_function_signature
*local_sig
= NULL
;
423 ir_function_signature
*sig
= NULL
;
425 /* Is the function hidden by a record type constructor? */
426 if (state
->symbols
->get_type(name
))
427 goto done
; /* no match */
429 /* Is the function hidden by a variable (impossible in 1.10)? */
430 if (!state
->symbols
->separate_function_namespace
431 && state
->symbols
->get_variable(name
))
432 goto done
; /* no match */
435 /* Look for a match in the local shader. If exact, we're done. */
436 bool is_exact
= false;
437 sig
= local_sig
= f
->matching_signature(state
, actual_parameters
,
442 if (!state
->es_shader
&& f
->has_user_signature()) {
443 /* In desktop GL, the presence of a user-defined signature hides any
444 * built-in signatures, so we must ignore them. In contrast, in ES2
445 * user-defined signatures add new overloads, so we must proceed.
451 /* Local shader has no exact candidates; check the built-ins. */
452 _mesa_glsl_initialize_builtin_functions();
453 sig
= _mesa_glsl_find_builtin_function(state
, name
, actual_parameters
);
457 /* If the match is from a linked built-in shader, import the prototype. */
458 if (sig
!= local_sig
) {
460 f
= new(ctx
) ir_function(name
);
461 state
->symbols
->add_global_function(f
);
462 emit_function(state
, f
);
464 f
->add_signature(sig
->clone_prototype(f
, NULL
));
471 print_function_prototypes(_mesa_glsl_parse_state
*state
, YYLTYPE
*loc
,
477 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
478 if (sig
->is_builtin() && !sig
->is_builtin_available(state
))
481 char *str
= prototype_string(sig
->return_type
, f
->name
, &sig
->parameters
);
482 _mesa_glsl_error(loc
, state
, " %s", str
);
488 * Raise a "no matching function" error, listing all possible overloads the
489 * compiler considered so developers can figure out what went wrong.
492 no_matching_function_error(const char *name
,
494 exec_list
*actual_parameters
,
495 _mesa_glsl_parse_state
*state
)
497 gl_shader
*sh
= _mesa_glsl_get_builtin_function_shader();
499 if (state
->symbols
->get_function(name
) == NULL
500 && (!state
->uses_builtin_functions
501 || sh
->symbols
->get_function(name
) == NULL
)) {
502 _mesa_glsl_error(loc
, state
, "no function with name '%s'", name
);
504 char *str
= prototype_string(NULL
, name
, actual_parameters
);
505 _mesa_glsl_error(loc
, state
,
506 "no matching function for call to `%s'; candidates are:",
510 print_function_prototypes(state
, loc
, state
->symbols
->get_function(name
));
512 if (state
->uses_builtin_functions
) {
513 print_function_prototypes(state
, loc
, sh
->symbols
->get_function(name
));
519 * Perform automatic type conversion of constructor parameters
521 * This implements the rules in the "Conversion and Scalar Constructors"
522 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
525 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
)
527 void *ctx
= ralloc_parent(src
);
528 const unsigned a
= desired_type
->base_type
;
529 const unsigned b
= src
->type
->base_type
;
530 ir_expression
*result
= NULL
;
532 if (src
->type
->is_error())
535 assert(a
<= GLSL_TYPE_BOOL
);
536 assert(b
<= GLSL_TYPE_BOOL
);
545 result
= new(ctx
) ir_expression(ir_unop_i2u
, src
);
547 case GLSL_TYPE_FLOAT
:
548 result
= new(ctx
) ir_expression(ir_unop_f2u
, src
);
551 result
= new(ctx
) ir_expression(ir_unop_i2u
,
552 new(ctx
) ir_expression(ir_unop_b2i
, src
));
559 result
= new(ctx
) ir_expression(ir_unop_u2i
, src
);
561 case GLSL_TYPE_FLOAT
:
562 result
= new(ctx
) ir_expression(ir_unop_f2i
, src
);
565 result
= new(ctx
) ir_expression(ir_unop_b2i
, src
);
569 case GLSL_TYPE_FLOAT
:
572 result
= new(ctx
) ir_expression(ir_unop_u2f
, desired_type
, src
, NULL
);
575 result
= new(ctx
) ir_expression(ir_unop_i2f
, desired_type
, src
, NULL
);
578 result
= new(ctx
) ir_expression(ir_unop_b2f
, desired_type
, src
, NULL
);
585 result
= new(ctx
) ir_expression(ir_unop_i2b
,
586 new(ctx
) ir_expression(ir_unop_u2i
, src
));
589 result
= new(ctx
) ir_expression(ir_unop_i2b
, desired_type
, src
, NULL
);
591 case GLSL_TYPE_FLOAT
:
592 result
= new(ctx
) ir_expression(ir_unop_f2b
, desired_type
, src
, NULL
);
598 assert(result
!= NULL
);
599 assert(result
->type
== desired_type
);
601 /* Try constant folding; it may fold in the conversion we just added. */
602 ir_constant
*const constant
= result
->constant_expression_value();
603 return (constant
!= NULL
) ? (ir_rvalue
*) constant
: (ir_rvalue
*) result
;
607 * Dereference a specific component from a scalar, vector, or matrix
610 dereference_component(ir_rvalue
*src
, unsigned component
)
612 void *ctx
= ralloc_parent(src
);
613 assert(component
< src
->type
->components());
615 /* If the source is a constant, just create a new constant instead of a
616 * dereference of the existing constant.
618 ir_constant
*constant
= src
->as_constant();
620 return new(ctx
) ir_constant(constant
, component
);
622 if (src
->type
->is_scalar()) {
624 } else if (src
->type
->is_vector()) {
625 return new(ctx
) ir_swizzle(src
, component
, 0, 0, 0, 1);
627 assert(src
->type
->is_matrix());
629 /* Dereference a row of the matrix, then call this function again to get
630 * a specific element from that row.
632 const int c
= component
/ src
->type
->column_type()->vector_elements
;
633 const int r
= component
% src
->type
->column_type()->vector_elements
;
634 ir_constant
*const col_index
= new(ctx
) ir_constant(c
);
635 ir_dereference
*const col
= new(ctx
) ir_dereference_array(src
, col_index
);
637 col
->type
= src
->type
->column_type();
639 return dereference_component(col
, r
);
642 assert(!"Should not get here.");
648 process_vec_mat_constructor(exec_list
*instructions
,
649 const glsl_type
*constructor_type
,
650 YYLTYPE
*loc
, exec_list
*parameters
,
651 struct _mesa_glsl_parse_state
*state
)
655 /* The ARB_shading_language_420pack spec says:
657 * "If an initializer is a list of initializers enclosed in curly braces,
658 * the variable being declared must be a vector, a matrix, an array, or a
661 * int i = { 1 }; // illegal, i is not an aggregate"
663 if (constructor_type
->vector_elements
<= 1) {
664 _mesa_glsl_error(loc
, state
, "aggregates can only initialize vectors, "
665 "matrices, arrays, and structs");
666 return ir_rvalue::error_value(ctx
);
669 exec_list actual_parameters
;
670 const unsigned parameter_count
=
671 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
673 if (parameter_count
== 0
674 || (constructor_type
->is_vector() &&
675 constructor_type
->vector_elements
!= parameter_count
)
676 || (constructor_type
->is_matrix() &&
677 constructor_type
->matrix_columns
!= parameter_count
)) {
678 _mesa_glsl_error(loc
, state
, "%s constructor must have %u parameters",
679 constructor_type
->is_vector() ? "vector" : "matrix",
680 constructor_type
->vector_elements
);
681 return ir_rvalue::error_value(ctx
);
684 bool all_parameters_are_constant
= true;
686 /* Type cast each parameter and, if possible, fold constants. */
687 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
688 ir_rvalue
*result
= ir
;
690 /* Apply implicit conversions (not the scalar constructor rules!). See
691 * the spec quote above. */
692 if (constructor_type
->is_float()) {
693 const glsl_type
*desired_type
=
694 glsl_type::get_instance(GLSL_TYPE_FLOAT
,
695 ir
->type
->vector_elements
,
696 ir
->type
->matrix_columns
);
697 if (result
->type
->can_implicitly_convert_to(desired_type
, state
)) {
698 /* Even though convert_component() implements the constructor
699 * conversion rules (not the implicit conversion rules), its safe
700 * to use it here because we already checked that the implicit
701 * conversion is legal.
703 result
= convert_component(ir
, desired_type
);
707 if (constructor_type
->is_matrix()) {
708 if (result
->type
!= constructor_type
->column_type()) {
709 _mesa_glsl_error(loc
, state
, "type error in matrix constructor: "
710 "expected: %s, found %s",
711 constructor_type
->column_type()->name
,
713 return ir_rvalue::error_value(ctx
);
715 } else if (result
->type
!= constructor_type
->get_scalar_type()) {
716 _mesa_glsl_error(loc
, state
, "type error in vector constructor: "
717 "expected: %s, found %s",
718 constructor_type
->get_scalar_type()->name
,
720 return ir_rvalue::error_value(ctx
);
723 /* Attempt to convert the parameter to a constant valued expression.
724 * After doing so, track whether or not all the parameters to the
725 * constructor are trivially constant valued expressions.
727 ir_rvalue
*const constant
= result
->constant_expression_value();
729 if (constant
!= NULL
)
732 all_parameters_are_constant
= false;
734 ir
->replace_with(result
);
737 if (all_parameters_are_constant
)
738 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
740 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "vec_mat_ctor",
742 instructions
->push_tail(var
);
745 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
746 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
747 new(ctx
) ir_constant(i
));
749 ir_instruction
*assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
750 instructions
->push_tail(assignment
);
755 return new(ctx
) ir_dereference_variable(var
);
760 process_array_constructor(exec_list
*instructions
,
761 const glsl_type
*constructor_type
,
762 YYLTYPE
*loc
, exec_list
*parameters
,
763 struct _mesa_glsl_parse_state
*state
)
766 /* Array constructors come in two forms: sized and unsized. Sized array
767 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
768 * variables. In this case the number of parameters must exactly match the
769 * specified size of the array.
771 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
772 * are vec4 variables. In this case the size of the array being constructed
773 * is determined by the number of parameters.
775 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
777 * "There must be exactly the same number of arguments as the size of
778 * the array being constructed. If no size is present in the
779 * constructor, then the array is explicitly sized to the number of
780 * arguments provided. The arguments are assigned in order, starting at
781 * element 0, to the elements of the constructed array. Each argument
782 * must be the same type as the element type of the array, or be a type
783 * that can be converted to the element type of the array according to
784 * Section 4.1.10 "Implicit Conversions.""
786 exec_list actual_parameters
;
787 const unsigned parameter_count
=
788 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
789 bool is_unsized_array
= constructor_type
->is_unsized_array();
791 if ((parameter_count
== 0) ||
792 (!is_unsized_array
&& (constructor_type
->length
!= parameter_count
))) {
793 const unsigned min_param
= is_unsized_array
794 ? 1 : constructor_type
->length
;
796 _mesa_glsl_error(loc
, state
, "array constructor must have %s %u "
798 is_unsized_array
? "at least" : "exactly",
799 min_param
, (min_param
<= 1) ? "" : "s");
800 return ir_rvalue::error_value(ctx
);
803 if (is_unsized_array
) {
805 glsl_type::get_array_instance(constructor_type
->element_type(),
807 assert(constructor_type
!= NULL
);
808 assert(constructor_type
->length
== parameter_count
);
811 bool all_parameters_are_constant
= true;
813 /* Type cast each parameter and, if possible, fold constants. */
814 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
815 ir_rvalue
*result
= ir
;
817 /* Apply implicit conversions (not the scalar constructor rules!). See
818 * the spec quote above. */
819 if (constructor_type
->element_type()->is_float()) {
820 const glsl_type
*desired_type
=
821 glsl_type::get_instance(GLSL_TYPE_FLOAT
,
822 ir
->type
->vector_elements
,
823 ir
->type
->matrix_columns
);
824 if (result
->type
->can_implicitly_convert_to(desired_type
, state
)) {
825 /* Even though convert_component() implements the constructor
826 * conversion rules (not the implicit conversion rules), its safe
827 * to use it here because we already checked that the implicit
828 * conversion is legal.
830 result
= convert_component(ir
, desired_type
);
834 if (result
->type
!= constructor_type
->element_type()) {
835 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
836 "expected: %s, found %s",
837 constructor_type
->element_type()->name
,
839 return ir_rvalue::error_value(ctx
);
842 /* Attempt to convert the parameter to a constant valued expression.
843 * After doing so, track whether or not all the parameters to the
844 * constructor are trivially constant valued expressions.
846 ir_rvalue
*const constant
= result
->constant_expression_value();
848 if (constant
!= NULL
)
851 all_parameters_are_constant
= false;
853 ir
->replace_with(result
);
856 if (all_parameters_are_constant
)
857 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
859 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "array_ctor",
861 instructions
->push_tail(var
);
864 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
865 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
866 new(ctx
) ir_constant(i
));
868 ir_instruction
*assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
869 instructions
->push_tail(assignment
);
874 return new(ctx
) ir_dereference_variable(var
);
879 * Try to convert a record constructor to a constant expression
882 constant_record_constructor(const glsl_type
*constructor_type
,
883 exec_list
*parameters
, void *mem_ctx
)
885 foreach_in_list(ir_instruction
, node
, parameters
) {
886 ir_constant
*constant
= node
->as_constant();
887 if (constant
== NULL
)
889 node
->replace_with(constant
);
892 return new(mem_ctx
) ir_constant(constructor_type
, parameters
);
897 * Determine if a list consists of a single scalar r-value
900 single_scalar_parameter(exec_list
*parameters
)
902 const ir_rvalue
*const p
= (ir_rvalue
*) parameters
->head
;
903 assert(((ir_rvalue
*)p
)->as_rvalue() != NULL
);
905 return (p
->type
->is_scalar() && p
->next
->is_tail_sentinel());
910 * Generate inline code for a vector constructor
912 * The generated constructor code will consist of a temporary variable
913 * declaration of the same type as the constructor. A sequence of assignments
914 * from constructor parameters to the temporary will follow.
917 * An \c ir_dereference_variable of the temprorary generated in the constructor
921 emit_inline_vector_constructor(const glsl_type
*type
,
922 exec_list
*instructions
,
923 exec_list
*parameters
,
926 assert(!parameters
->is_empty());
928 ir_variable
*var
= new(ctx
) ir_variable(type
, "vec_ctor", ir_var_temporary
);
929 instructions
->push_tail(var
);
931 /* There are two kinds of vector constructors.
933 * - Construct a vector from a single scalar by replicating that scalar to
934 * all components of the vector.
936 * - Construct a vector from an arbirary combination of vectors and
937 * scalars. The components of the constructor parameters are assigned
938 * to the vector in order until the vector is full.
940 const unsigned lhs_components
= type
->components();
941 if (single_scalar_parameter(parameters
)) {
942 ir_rvalue
*first_param
= (ir_rvalue
*)parameters
->head
;
943 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(first_param
, 0, 0, 0, 0,
945 ir_dereference_variable
*lhs
= new(ctx
) ir_dereference_variable(var
);
946 const unsigned mask
= (1U << lhs_components
) - 1;
948 assert(rhs
->type
== lhs
->type
);
950 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
, mask
);
951 instructions
->push_tail(inst
);
953 unsigned base_component
= 0;
954 unsigned base_lhs_component
= 0;
955 ir_constant_data data
;
956 unsigned constant_mask
= 0, constant_components
= 0;
958 memset(&data
, 0, sizeof(data
));
960 foreach_in_list(ir_rvalue
, param
, parameters
) {
961 unsigned rhs_components
= param
->type
->components();
963 /* Do not try to assign more components to the vector than it has!
965 if ((rhs_components
+ base_lhs_component
) > lhs_components
) {
966 rhs_components
= lhs_components
- base_lhs_component
;
969 const ir_constant
*const c
= param
->as_constant();
971 for (unsigned i
= 0; i
< rhs_components
; i
++) {
972 switch (c
->type
->base_type
) {
974 data
.u
[i
+ base_component
] = c
->get_uint_component(i
);
977 data
.i
[i
+ base_component
] = c
->get_int_component(i
);
979 case GLSL_TYPE_FLOAT
:
980 data
.f
[i
+ base_component
] = c
->get_float_component(i
);
983 data
.b
[i
+ base_component
] = c
->get_bool_component(i
);
986 assert(!"Should not get here.");
991 /* Mask of fields to be written in the assignment.
993 constant_mask
|= ((1U << rhs_components
) - 1) << base_lhs_component
;
994 constant_components
+= rhs_components
;
996 base_component
+= rhs_components
;
998 /* Advance the component index by the number of components
999 * that were just assigned.
1001 base_lhs_component
+= rhs_components
;
1004 if (constant_mask
!= 0) {
1005 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1006 const glsl_type
*rhs_type
= glsl_type::get_instance(var
->type
->base_type
,
1007 constant_components
,
1009 ir_rvalue
*rhs
= new(ctx
) ir_constant(rhs_type
, &data
);
1011 ir_instruction
*inst
=
1012 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, constant_mask
);
1013 instructions
->push_tail(inst
);
1017 foreach_in_list(ir_rvalue
, param
, parameters
) {
1018 unsigned rhs_components
= param
->type
->components();
1020 /* Do not try to assign more components to the vector than it has!
1022 if ((rhs_components
+ base_component
) > lhs_components
) {
1023 rhs_components
= lhs_components
- base_component
;
1026 const ir_constant
*const c
= param
->as_constant();
1028 /* Mask of fields to be written in the assignment.
1030 const unsigned write_mask
= ((1U << rhs_components
) - 1)
1033 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1035 /* Generate a swizzle so that LHS and RHS sizes match.
1038 new(ctx
) ir_swizzle(param
, 0, 1, 2, 3, rhs_components
);
1040 ir_instruction
*inst
=
1041 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1042 instructions
->push_tail(inst
);
1045 /* Advance the component index by the number of components that were
1048 base_component
+= rhs_components
;
1051 return new(ctx
) ir_dereference_variable(var
);
1056 * Generate assignment of a portion of a vector to a portion of a matrix column
1058 * \param src_base First component of the source to be used in assignment
1059 * \param column Column of destination to be assiged
1060 * \param row_base First component of the destination column to be assigned
1061 * \param count Number of components to be assigned
1064 * \c src_base + \c count must be less than or equal to the number of components
1065 * in the source vector.
1068 assign_to_matrix_column(ir_variable
*var
, unsigned column
, unsigned row_base
,
1069 ir_rvalue
*src
, unsigned src_base
, unsigned count
,
1072 ir_constant
*col_idx
= new(mem_ctx
) ir_constant(column
);
1073 ir_dereference
*column_ref
= new(mem_ctx
) ir_dereference_array(var
, col_idx
);
1075 assert(column_ref
->type
->components() >= (row_base
+ count
));
1076 assert(src
->type
->components() >= (src_base
+ count
));
1078 /* Generate a swizzle that extracts the number of components from the source
1079 * that are to be assigned to the column of the matrix.
1081 if (count
< src
->type
->vector_elements
) {
1082 src
= new(mem_ctx
) ir_swizzle(src
,
1083 src_base
+ 0, src_base
+ 1,
1084 src_base
+ 2, src_base
+ 3,
1088 /* Mask of fields to be written in the assignment.
1090 const unsigned write_mask
= ((1U << count
) - 1) << row_base
;
1092 return new(mem_ctx
) ir_assignment(column_ref
, src
, NULL
, write_mask
);
1097 * Generate inline code for a matrix constructor
1099 * The generated constructor code will consist of a temporary variable
1100 * declaration of the same type as the constructor. A sequence of assignments
1101 * from constructor parameters to the temporary will follow.
1104 * An \c ir_dereference_variable of the temprorary generated in the constructor
1108 emit_inline_matrix_constructor(const glsl_type
*type
,
1109 exec_list
*instructions
,
1110 exec_list
*parameters
,
1113 assert(!parameters
->is_empty());
1115 ir_variable
*var
= new(ctx
) ir_variable(type
, "mat_ctor", ir_var_temporary
);
1116 instructions
->push_tail(var
);
1118 /* There are three kinds of matrix constructors.
1120 * - Construct a matrix from a single scalar by replicating that scalar to
1121 * along the diagonal of the matrix and setting all other components to
1124 * - Construct a matrix from an arbirary combination of vectors and
1125 * scalars. The components of the constructor parameters are assigned
1126 * to the matrix in colum-major order until the matrix is full.
1128 * - Construct a matrix from a single matrix. The source matrix is copied
1129 * to the upper left portion of the constructed matrix, and the remaining
1130 * elements take values from the identity matrix.
1132 ir_rvalue
*const first_param
= (ir_rvalue
*) parameters
->head
;
1133 if (single_scalar_parameter(parameters
)) {
1134 /* Assign the scalar to the X component of a vec4, and fill the remaining
1135 * components with zero.
1137 ir_variable
*rhs_var
=
1138 new(ctx
) ir_variable(glsl_type::vec4_type
, "mat_ctor_vec",
1140 instructions
->push_tail(rhs_var
);
1142 ir_constant_data zero
;
1148 ir_instruction
*inst
=
1149 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(rhs_var
),
1150 new(ctx
) ir_constant(rhs_var
->type
, &zero
),
1152 instructions
->push_tail(inst
);
1154 ir_dereference
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1156 inst
= new(ctx
) ir_assignment(rhs_ref
, first_param
, NULL
, 0x01);
1157 instructions
->push_tail(inst
);
1159 /* Assign the temporary vector to each column of the destination matrix
1160 * with a swizzle that puts the X component on the diagonal of the
1161 * matrix. In some cases this may mean that the X component does not
1162 * get assigned into the column at all (i.e., when the matrix has more
1163 * columns than rows).
1165 static const unsigned rhs_swiz
[4][4] = {
1172 const unsigned cols_to_init
= MIN2(type
->matrix_columns
,
1173 type
->vector_elements
);
1174 for (unsigned i
= 0; i
< cols_to_init
; i
++) {
1175 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1176 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
1178 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1179 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, rhs_swiz
[i
],
1180 type
->vector_elements
);
1182 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
1183 instructions
->push_tail(inst
);
1186 for (unsigned i
= cols_to_init
; i
< type
->matrix_columns
; i
++) {
1187 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1188 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
1190 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1191 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, 1, 1, 1, 1,
1192 type
->vector_elements
);
1194 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
1195 instructions
->push_tail(inst
);
1197 } else if (first_param
->type
->is_matrix()) {
1198 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
1200 * "If a matrix is constructed from a matrix, then each component
1201 * (column i, row j) in the result that has a corresponding
1202 * component (column i, row j) in the argument will be initialized
1203 * from there. All other components will be initialized to the
1204 * identity matrix. If a matrix argument is given to a matrix
1205 * constructor, it is an error to have any other arguments."
1207 assert(first_param
->next
->is_tail_sentinel());
1208 ir_rvalue
*const src_matrix
= first_param
;
1210 /* If the source matrix is smaller, pre-initialize the relavent parts of
1211 * the destination matrix to the identity matrix.
1213 if ((src_matrix
->type
->matrix_columns
< var
->type
->matrix_columns
)
1214 || (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)) {
1216 /* If the source matrix has fewer rows, every column of the destination
1217 * must be initialized. Otherwise only the columns in the destination
1218 * that do not exist in the source must be initialized.
1221 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)
1222 ? 0 : src_matrix
->type
->matrix_columns
;
1224 const glsl_type
*const col_type
= var
->type
->column_type();
1225 for (/* empty */; col
< var
->type
->matrix_columns
; col
++) {
1226 ir_constant_data ident
;
1235 ir_rvalue
*const rhs
= new(ctx
) ir_constant(col_type
, &ident
);
1237 ir_rvalue
*const lhs
=
1238 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(col
));
1240 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
1241 instructions
->push_tail(inst
);
1245 /* Assign columns from the source matrix to the destination matrix.
1247 * Since the parameter will be used in the RHS of multiple assignments,
1248 * generate a temporary and copy the paramter there.
1250 ir_variable
*const rhs_var
=
1251 new(ctx
) ir_variable(first_param
->type
, "mat_ctor_mat",
1253 instructions
->push_tail(rhs_var
);
1255 ir_dereference
*const rhs_var_ref
=
1256 new(ctx
) ir_dereference_variable(rhs_var
);
1257 ir_instruction
*const inst
=
1258 new(ctx
) ir_assignment(rhs_var_ref
, first_param
, NULL
);
1259 instructions
->push_tail(inst
);
1261 const unsigned last_row
= MIN2(src_matrix
->type
->vector_elements
,
1262 var
->type
->vector_elements
);
1263 const unsigned last_col
= MIN2(src_matrix
->type
->matrix_columns
,
1264 var
->type
->matrix_columns
);
1266 unsigned swiz
[4] = { 0, 0, 0, 0 };
1267 for (unsigned i
= 1; i
< last_row
; i
++)
1270 const unsigned write_mask
= (1U << last_row
) - 1;
1272 for (unsigned i
= 0; i
< last_col
; i
++) {
1273 ir_dereference
*const lhs
=
1274 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
1275 ir_rvalue
*const rhs_col
=
1276 new(ctx
) ir_dereference_array(rhs_var
, new(ctx
) ir_constant(i
));
1278 /* If one matrix has columns that are smaller than the columns of the
1279 * other matrix, wrap the column access of the larger with a swizzle
1280 * so that the LHS and RHS of the assignment have the same size (and
1281 * therefore have the same type).
1283 * It would be perfectly valid to unconditionally generate the
1284 * swizzles, this this will typically result in a more compact IR tree.
1287 if (lhs
->type
->vector_elements
!= rhs_col
->type
->vector_elements
) {
1288 rhs
= new(ctx
) ir_swizzle(rhs_col
, swiz
, last_row
);
1293 ir_instruction
*inst
=
1294 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1295 instructions
->push_tail(inst
);
1298 const unsigned cols
= type
->matrix_columns
;
1299 const unsigned rows
= type
->vector_elements
;
1300 unsigned col_idx
= 0;
1301 unsigned row_idx
= 0;
1303 foreach_in_list(ir_rvalue
, rhs
, parameters
) {
1304 const unsigned components_remaining_this_column
= rows
- row_idx
;
1305 unsigned rhs_components
= rhs
->type
->components();
1306 unsigned rhs_base
= 0;
1308 /* Since the parameter might be used in the RHS of two assignments,
1309 * generate a temporary and copy the paramter there.
1311 ir_variable
*rhs_var
=
1312 new(ctx
) ir_variable(rhs
->type
, "mat_ctor_vec", ir_var_temporary
);
1313 instructions
->push_tail(rhs_var
);
1315 ir_dereference
*rhs_var_ref
=
1316 new(ctx
) ir_dereference_variable(rhs_var
);
1317 ir_instruction
*inst
= new(ctx
) ir_assignment(rhs_var_ref
, rhs
, NULL
);
1318 instructions
->push_tail(inst
);
1320 /* Assign the current parameter to as many components of the matrix
1323 * NOTE: A single vector parameter can span two matrix columns. A
1324 * single vec4, for example, can completely fill a mat2.
1326 if (rhs_components
>= components_remaining_this_column
) {
1327 const unsigned count
= MIN2(rhs_components
,
1328 components_remaining_this_column
);
1330 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1332 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
1336 instructions
->push_tail(inst
);
1344 /* If there is data left in the parameter and components left to be
1345 * set in the destination, emit another assignment. It is possible
1346 * that the assignment could be of a vec4 to the last element of the
1347 * matrix. In this case col_idx==cols, but there is still data
1348 * left in the source parameter. Obviously, don't emit an assignment
1349 * to data outside the destination matrix.
1351 if ((col_idx
< cols
) && (rhs_base
< rhs_components
)) {
1352 const unsigned count
= rhs_components
- rhs_base
;
1354 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1356 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
1361 instructions
->push_tail(inst
);
1368 return new(ctx
) ir_dereference_variable(var
);
1373 emit_inline_record_constructor(const glsl_type
*type
,
1374 exec_list
*instructions
,
1375 exec_list
*parameters
,
1378 ir_variable
*const var
=
1379 new(mem_ctx
) ir_variable(type
, "record_ctor", ir_var_temporary
);
1380 ir_dereference_variable
*const d
= new(mem_ctx
) ir_dereference_variable(var
);
1382 instructions
->push_tail(var
);
1384 exec_node
*node
= parameters
->head
;
1385 for (unsigned i
= 0; i
< type
->length
; i
++) {
1386 assert(!node
->is_tail_sentinel());
1388 ir_dereference
*const lhs
=
1389 new(mem_ctx
) ir_dereference_record(d
->clone(mem_ctx
, NULL
),
1390 type
->fields
.structure
[i
].name
);
1392 ir_rvalue
*const rhs
= ((ir_instruction
*) node
)->as_rvalue();
1393 assert(rhs
!= NULL
);
1395 ir_instruction
*const assign
= new(mem_ctx
) ir_assignment(lhs
, rhs
, NULL
);
1397 instructions
->push_tail(assign
);
1406 process_record_constructor(exec_list
*instructions
,
1407 const glsl_type
*constructor_type
,
1408 YYLTYPE
*loc
, exec_list
*parameters
,
1409 struct _mesa_glsl_parse_state
*state
)
1412 exec_list actual_parameters
;
1414 process_parameters(instructions
, &actual_parameters
,
1417 exec_node
*node
= actual_parameters
.head
;
1418 for (unsigned i
= 0; i
< constructor_type
->length
; i
++) {
1419 ir_rvalue
*ir
= (ir_rvalue
*) node
;
1421 if (node
->is_tail_sentinel()) {
1422 _mesa_glsl_error(loc
, state
,
1423 "insufficient parameters to constructor for `%s'",
1424 constructor_type
->name
);
1425 return ir_rvalue::error_value(ctx
);
1428 if (apply_implicit_conversion(constructor_type
->fields
.structure
[i
].type
,
1430 node
->replace_with(ir
);
1432 _mesa_glsl_error(loc
, state
,
1433 "parameter type mismatch in constructor for `%s.%s' "
1435 constructor_type
->name
,
1436 constructor_type
->fields
.structure
[i
].name
,
1438 constructor_type
->fields
.structure
[i
].type
->name
);
1439 return ir_rvalue::error_value(ctx
);;
1445 if (!node
->is_tail_sentinel()) {
1446 _mesa_glsl_error(loc
, state
, "too many parameters in constructor "
1447 "for `%s'", constructor_type
->name
);
1448 return ir_rvalue::error_value(ctx
);
1451 ir_rvalue
*const constant
=
1452 constant_record_constructor(constructor_type
, &actual_parameters
,
1455 return (constant
!= NULL
)
1457 : emit_inline_record_constructor(constructor_type
, instructions
,
1458 &actual_parameters
, state
);
1463 ast_function_expression::hir(exec_list
*instructions
,
1464 struct _mesa_glsl_parse_state
*state
)
1467 /* There are three sorts of function calls.
1469 * 1. constructors - The first subexpression is an ast_type_specifier.
1470 * 2. methods - Only the .length() method of array types.
1471 * 3. functions - Calls to regular old functions.
1473 * Method calls are actually detected when the ast_field_selection
1474 * expression is handled.
1476 if (is_constructor()) {
1477 const ast_type_specifier
*type
= (ast_type_specifier
*) subexpressions
[0];
1478 YYLTYPE loc
= type
->get_location();
1481 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
1483 /* constructor_type can be NULL if a variable with the same name as the
1484 * structure has come into scope.
1486 if (constructor_type
== NULL
) {
1487 _mesa_glsl_error(& loc
, state
, "unknown type `%s' (structure name "
1488 "may be shadowed by a variable with the same name)",
1490 return ir_rvalue::error_value(ctx
);
1494 /* Constructors for samplers are illegal.
1496 if (constructor_type
->is_sampler()) {
1497 _mesa_glsl_error(& loc
, state
, "cannot construct sampler type `%s'",
1498 constructor_type
->name
);
1499 return ir_rvalue::error_value(ctx
);
1502 if (constructor_type
->is_array()) {
1503 if (!state
->check_version(120, 300, &loc
,
1504 "array constructors forbidden")) {
1505 return ir_rvalue::error_value(ctx
);
1508 return process_array_constructor(instructions
, constructor_type
,
1509 & loc
, &this->expressions
, state
);
1513 /* There are two kinds of constructor calls. Constructors for arrays and
1514 * structures must have the exact number of arguments with matching types
1515 * in the correct order. These constructors follow essentially the same
1516 * type matching rules as functions.
1518 * Constructors for built-in language types, such as mat4 and vec2, are
1519 * free form. The only requirements are that the parameters must provide
1520 * enough values of the correct scalar type and that no arguments are
1521 * given past the last used argument.
1523 * When using the C-style initializer syntax from GLSL 4.20, constructors
1524 * must have the exact number of arguments with matching types in the
1527 if (constructor_type
->is_record()) {
1528 return process_record_constructor(instructions
, constructor_type
,
1529 &loc
, &this->expressions
,
1533 if (!constructor_type
->is_numeric() && !constructor_type
->is_boolean())
1534 return ir_rvalue::error_value(ctx
);
1536 /* Total number of components of the type being constructed. */
1537 const unsigned type_components
= constructor_type
->components();
1539 /* Number of components from parameters that have actually been
1540 * consumed. This is used to perform several kinds of error checking.
1542 unsigned components_used
= 0;
1544 unsigned matrix_parameters
= 0;
1545 unsigned nonmatrix_parameters
= 0;
1546 exec_list actual_parameters
;
1548 foreach_list_typed(ast_node
, ast
, link
, &this->expressions
) {
1549 ir_rvalue
*result
= ast
->hir(instructions
, state
);
1551 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1553 * "It is an error to provide extra arguments beyond this
1554 * last used argument."
1556 if (components_used
>= type_components
) {
1557 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
1559 constructor_type
->name
);
1560 return ir_rvalue::error_value(ctx
);
1563 if (!result
->type
->is_numeric() && !result
->type
->is_boolean()) {
1564 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
1565 "non-numeric data type",
1566 constructor_type
->name
);
1567 return ir_rvalue::error_value(ctx
);
1570 /* Count the number of matrix and nonmatrix parameters. This
1571 * is used below to enforce some of the constructor rules.
1573 if (result
->type
->is_matrix())
1574 matrix_parameters
++;
1576 nonmatrix_parameters
++;
1578 actual_parameters
.push_tail(result
);
1579 components_used
+= result
->type
->components();
1582 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1584 * "It is an error to construct matrices from other matrices. This
1585 * is reserved for future use."
1587 if (matrix_parameters
> 0
1588 && constructor_type
->is_matrix()
1589 && !state
->check_version(120, 100, &loc
,
1590 "cannot construct `%s' from a matrix",
1591 constructor_type
->name
)) {
1592 return ir_rvalue::error_value(ctx
);
1595 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1597 * "If a matrix argument is given to a matrix constructor, it is
1598 * an error to have any other arguments."
1600 if ((matrix_parameters
> 0)
1601 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
1602 && constructor_type
->is_matrix()) {
1603 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
1604 "matrix must be only parameter",
1605 constructor_type
->name
);
1606 return ir_rvalue::error_value(ctx
);
1609 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1611 * "In these cases, there must be enough components provided in the
1612 * arguments to provide an initializer for every component in the
1613 * constructed value."
1615 if (components_used
< type_components
&& components_used
!= 1
1616 && matrix_parameters
== 0) {
1617 _mesa_glsl_error(& loc
, state
, "too few components to construct "
1619 constructor_type
->name
);
1620 return ir_rvalue::error_value(ctx
);
1623 /* Later, we cast each parameter to the same base type as the
1624 * constructor. Since there are no non-floating point matrices, we
1625 * need to break them up into a series of column vectors.
1627 if (constructor_type
->base_type
!= GLSL_TYPE_FLOAT
) {
1628 foreach_in_list_safe(ir_rvalue
, matrix
, &actual_parameters
) {
1629 if (!matrix
->type
->is_matrix())
1632 /* Create a temporary containing the matrix. */
1633 ir_variable
*var
= new(ctx
) ir_variable(matrix
->type
, "matrix_tmp",
1635 instructions
->push_tail(var
);
1636 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
)
1637 ir_dereference_variable(var
), matrix
, NULL
));
1638 var
->constant_value
= matrix
->constant_expression_value();
1640 /* Replace the matrix with dereferences of its columns. */
1641 for (int i
= 0; i
< matrix
->type
->matrix_columns
; i
++) {
1642 matrix
->insert_before(new (ctx
) ir_dereference_array(var
,
1643 new(ctx
) ir_constant(i
)));
1649 bool all_parameters_are_constant
= true;
1651 /* Type cast each parameter and, if possible, fold constants.*/
1652 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1653 const glsl_type
*desired_type
=
1654 glsl_type::get_instance(constructor_type
->base_type
,
1655 ir
->type
->vector_elements
,
1656 ir
->type
->matrix_columns
);
1657 ir_rvalue
*result
= convert_component(ir
, desired_type
);
1659 /* Attempt to convert the parameter to a constant valued expression.
1660 * After doing so, track whether or not all the parameters to the
1661 * constructor are trivially constant valued expressions.
1663 ir_rvalue
*const constant
= result
->constant_expression_value();
1665 if (constant
!= NULL
)
1668 all_parameters_are_constant
= false;
1671 ir
->replace_with(result
);
1675 /* If all of the parameters are trivially constant, create a
1676 * constant representing the complete collection of parameters.
1678 if (all_parameters_are_constant
) {
1679 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1680 } else if (constructor_type
->is_scalar()) {
1681 return dereference_component((ir_rvalue
*) actual_parameters
.head
,
1683 } else if (constructor_type
->is_vector()) {
1684 return emit_inline_vector_constructor(constructor_type
,
1689 assert(constructor_type
->is_matrix());
1690 return emit_inline_matrix_constructor(constructor_type
,
1696 const ast_expression
*id
= subexpressions
[0];
1697 const char *func_name
= id
->primary_expression
.identifier
;
1698 YYLTYPE loc
= get_location();
1699 exec_list actual_parameters
;
1701 process_parameters(instructions
, &actual_parameters
, &this->expressions
,
1704 ir_function_signature
*sig
=
1705 match_function_by_name(func_name
, &actual_parameters
, state
);
1707 ir_rvalue
*value
= NULL
;
1709 no_matching_function_error(func_name
, &loc
, &actual_parameters
, state
);
1710 value
= ir_rvalue::error_value(ctx
);
1711 } else if (!verify_parameter_modes(state
, sig
, actual_parameters
, this->expressions
)) {
1712 /* an error has already been emitted */
1713 value
= ir_rvalue::error_value(ctx
);
1715 value
= generate_call(instructions
, sig
, &actual_parameters
, state
);
1721 return ir_rvalue::error_value(ctx
);
1725 ast_aggregate_initializer::hir(exec_list
*instructions
,
1726 struct _mesa_glsl_parse_state
*state
)
1729 YYLTYPE loc
= this->get_location();
1731 if (!this->constructor_type
) {
1732 _mesa_glsl_error(&loc
, state
, "type of C-style initializer unknown");
1733 return ir_rvalue::error_value(ctx
);
1735 const glsl_type
*const constructor_type
= this->constructor_type
;
1737 if (!state
->ARB_shading_language_420pack_enable
) {
1738 _mesa_glsl_error(&loc
, state
, "C-style initialization requires the "
1739 "GL_ARB_shading_language_420pack extension");
1740 return ir_rvalue::error_value(ctx
);
1743 if (constructor_type
->is_array()) {
1744 return process_array_constructor(instructions
, constructor_type
, &loc
,
1745 &this->expressions
, state
);
1748 if (constructor_type
->is_record()) {
1749 return process_record_constructor(instructions
, constructor_type
, &loc
,
1750 &this->expressions
, state
);
1753 return process_vec_mat_constructor(instructions
, constructor_type
, &loc
,
1754 &this->expressions
, state
);