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 */
29 #include "main/shaderobj.h"
32 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
);
35 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
36 struct _mesa_glsl_parse_state
*state
);
39 process_parameters(exec_list
*instructions
, exec_list
*actual_parameters
,
40 exec_list
*parameters
,
41 struct _mesa_glsl_parse_state
*state
)
45 foreach_list_typed(ast_node
, ast
, link
, parameters
) {
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
&& !formal
->data
.image_restrict
) {
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_first_atomic_ssbo_parameter(YYLTYPE
*loc
, _mesa_glsl_parse_state
*state
,
149 if (!var
|| !var
->is_in_shader_storage_block()) {
150 _mesa_glsl_error(loc
, state
, "First argument to atomic function "
151 "must be a buffer variable");
158 is_atomic_ssbo_function(const char *func_name
)
160 return !strcmp(func_name
, "atomicAdd") ||
161 !strcmp(func_name
, "atomicMin") ||
162 !strcmp(func_name
, "atomicMax") ||
163 !strcmp(func_name
, "atomicAnd") ||
164 !strcmp(func_name
, "atomicOr") ||
165 !strcmp(func_name
, "atomicXor") ||
166 !strcmp(func_name
, "atomicExchange") ||
167 !strcmp(func_name
, "atomicCompSwap");
171 * Verify that 'out' and 'inout' actual parameters are lvalues. Also, verify
172 * that 'const_in' formal parameters (an extension in our IR) correspond to
173 * ir_constant actual parameters.
176 verify_parameter_modes(_mesa_glsl_parse_state
*state
,
177 ir_function_signature
*sig
,
178 exec_list
&actual_ir_parameters
,
179 exec_list
&actual_ast_parameters
)
181 exec_node
*actual_ir_node
= actual_ir_parameters
.head
;
182 exec_node
*actual_ast_node
= actual_ast_parameters
.head
;
184 foreach_in_list(const ir_variable
, formal
, &sig
->parameters
) {
185 /* The lists must be the same length. */
186 assert(!actual_ir_node
->is_tail_sentinel());
187 assert(!actual_ast_node
->is_tail_sentinel());
189 const ir_rvalue
*const actual
= (ir_rvalue
*) actual_ir_node
;
190 const ast_expression
*const actual_ast
=
191 exec_node_data(ast_expression
, actual_ast_node
, link
);
193 /* FIXME: 'loc' is incorrect (as of 2011-01-21). It is always
196 YYLTYPE loc
= actual_ast
->get_location();
198 /* Verify that 'const_in' parameters are ir_constants. */
199 if (formal
->data
.mode
== ir_var_const_in
&&
200 actual
->ir_type
!= ir_type_constant
) {
201 _mesa_glsl_error(&loc
, state
,
202 "parameter `in %s' must be a constant expression",
207 /* Verify that shader_in parameters are shader inputs */
208 if (formal
->data
.must_be_shader_input
) {
209 ir_variable
*var
= actual
->variable_referenced();
210 if (var
&& var
->data
.mode
!= ir_var_shader_in
) {
211 _mesa_glsl_error(&loc
, state
,
212 "parameter `%s` must be a shader input",
217 if (actual
->ir_type
== ir_type_swizzle
) {
218 _mesa_glsl_error(&loc
, state
,
219 "parameter `%s` must not be swizzled",
225 /* Verify that 'out' and 'inout' actual parameters are lvalues. */
226 if (formal
->data
.mode
== ir_var_function_out
227 || formal
->data
.mode
== ir_var_function_inout
) {
228 const char *mode
= NULL
;
229 switch (formal
->data
.mode
) {
230 case ir_var_function_out
: mode
= "out"; break;
231 case ir_var_function_inout
: mode
= "inout"; break;
232 default: assert(false); break;
235 /* This AST-based check catches errors like f(i++). The IR-based
236 * is_lvalue() is insufficient because the actual parameter at the
237 * IR-level is just a temporary value, which is an l-value.
239 if (actual_ast
->non_lvalue_description
!= NULL
) {
240 _mesa_glsl_error(&loc
, state
,
241 "function parameter '%s %s' references a %s",
243 actual_ast
->non_lvalue_description
);
247 ir_variable
*var
= actual
->variable_referenced();
249 var
->data
.assigned
= true;
251 if (var
&& var
->data
.read_only
) {
252 _mesa_glsl_error(&loc
, state
,
253 "function parameter '%s %s' references the "
254 "read-only variable '%s'",
256 actual
->variable_referenced()->name
);
258 } else if (!actual
->is_lvalue()) {
259 _mesa_glsl_error(&loc
, state
,
260 "function parameter '%s %s' is not an lvalue",
266 if (formal
->type
->is_image() &&
267 actual
->variable_referenced()) {
268 if (!verify_image_parameter(&loc
, state
, formal
,
269 actual
->variable_referenced()))
273 actual_ir_node
= actual_ir_node
->next
;
274 actual_ast_node
= actual_ast_node
->next
;
277 /* The first parameter of atomic functions must be a buffer variable */
278 const char *func_name
= sig
->function_name();
279 bool is_atomic_ssbo
= is_atomic_ssbo_function(func_name
);
280 if (is_atomic_ssbo
) {
281 const ir_rvalue
*const actual
= (ir_rvalue
*) actual_ir_parameters
.head
;
283 const ast_expression
*const actual_ast
=
284 exec_node_data(ast_expression
, actual_ast_parameters
.head
, link
);
285 YYLTYPE loc
= actual_ast
->get_location();
287 if (!verify_first_atomic_ssbo_parameter(&loc
, state
,
288 actual
->variable_referenced())) {
297 fix_parameter(void *mem_ctx
, ir_rvalue
*actual
, const glsl_type
*formal_type
,
298 exec_list
*before_instructions
, exec_list
*after_instructions
,
299 bool parameter_is_inout
)
301 ir_expression
*const expr
= actual
->as_expression();
303 /* If the types match exactly and the parameter is not a vector-extract,
304 * nothing needs to be done to fix the parameter.
306 if (formal_type
== actual
->type
307 && (expr
== NULL
|| expr
->operation
!= ir_binop_vector_extract
))
310 /* To convert an out parameter, we need to create a temporary variable to
311 * hold the value before conversion, and then perform the conversion after
312 * the function call returns.
314 * This has the effect of transforming code like this:
320 * Into IR that's equivalent to this:
324 * int out_parameter_conversion;
325 * f(out_parameter_conversion);
326 * value = float(out_parameter_conversion);
328 * If the parameter is an ir_expression of ir_binop_vector_extract,
329 * additional conversion is needed in the post-call re-write.
332 new(mem_ctx
) ir_variable(formal_type
, "inout_tmp", ir_var_temporary
);
334 before_instructions
->push_tail(tmp
);
336 /* If the parameter is an inout parameter, copy the value of the actual
337 * parameter to the new temporary. Note that no type conversion is allowed
338 * here because inout parameters must match types exactly.
340 if (parameter_is_inout
) {
341 /* Inout parameters should never require conversion, since that would
342 * require an implicit conversion to exist both to and from the formal
343 * parameter type, and there are no bidirectional implicit conversions.
345 assert (actual
->type
== formal_type
);
347 ir_dereference_variable
*const deref_tmp_1
=
348 new(mem_ctx
) ir_dereference_variable(tmp
);
349 ir_assignment
*const assignment
=
350 new(mem_ctx
) ir_assignment(deref_tmp_1
, actual
);
351 before_instructions
->push_tail(assignment
);
354 /* Replace the parameter in the call with a dereference of the new
357 ir_dereference_variable
*const deref_tmp_2
=
358 new(mem_ctx
) ir_dereference_variable(tmp
);
359 actual
->replace_with(deref_tmp_2
);
362 /* Copy the temporary variable to the actual parameter with optional
363 * type conversion applied.
365 ir_rvalue
*rhs
= new(mem_ctx
) ir_dereference_variable(tmp
);
366 if (actual
->type
!= formal_type
)
367 rhs
= convert_component(rhs
, actual
->type
);
369 ir_rvalue
*lhs
= actual
;
370 if (expr
!= NULL
&& expr
->operation
== ir_binop_vector_extract
) {
371 lhs
= new(mem_ctx
) ir_dereference_array(expr
->operands
[0]->clone(mem_ctx
, NULL
),
372 expr
->operands
[1]->clone(mem_ctx
, NULL
));
375 ir_assignment
*const assignment_2
= new(mem_ctx
) ir_assignment(lhs
, rhs
);
376 after_instructions
->push_tail(assignment_2
);
380 * Generate a function call.
382 * For non-void functions, this returns a dereference of the temporary variable
383 * which stores the return value for the call. For void functions, this returns
387 generate_call(exec_list
*instructions
, ir_function_signature
*sig
,
388 exec_list
*actual_parameters
,
389 ir_variable
*sub_var
,
390 ir_rvalue
*array_idx
,
391 struct _mesa_glsl_parse_state
*state
)
394 exec_list post_call_conversions
;
396 /* Perform implicit conversion of arguments. For out parameters, we need
397 * to place them in a temporary variable and do the conversion after the
398 * call takes place. Since we haven't emitted the call yet, we'll place
399 * the post-call conversions in a temporary exec_list, and emit them later.
401 foreach_two_lists(formal_node
, &sig
->parameters
,
402 actual_node
, actual_parameters
) {
403 ir_rvalue
*actual
= (ir_rvalue
*) actual_node
;
404 ir_variable
*formal
= (ir_variable
*) formal_node
;
406 if (formal
->type
->is_numeric() || formal
->type
->is_boolean()) {
407 switch (formal
->data
.mode
) {
408 case ir_var_const_in
:
409 case ir_var_function_in
: {
411 = convert_component(actual
, formal
->type
);
412 actual
->replace_with(converted
);
415 case ir_var_function_out
:
416 case ir_var_function_inout
:
417 fix_parameter(ctx
, actual
, formal
->type
,
418 instructions
, &post_call_conversions
,
419 formal
->data
.mode
== ir_var_function_inout
);
422 assert (!"Illegal formal parameter mode");
428 /* Section 4.3.2 (Const) of the GLSL 1.10.59 spec says:
430 * "Initializers for const declarations must be formed from literal
431 * values, other const variables (not including function call
432 * paramaters), or expressions of these.
434 * Constructors may be used in such expressions, but function calls may
437 * Section 4.3.3 (Constant Expressions) of the GLSL 1.20.8 spec says:
439 * "A constant expression is one of
443 * - a built-in function call whose arguments are all constant
444 * expressions, with the exception of the texture lookup
445 * functions, the noise functions, and ftransform. The built-in
446 * functions dFdx, dFdy, and fwidth must return 0 when evaluated
447 * inside an initializer with an argument that is a constant
450 * Section 5.10 (Constant Expressions) of the GLSL ES 1.00.17 spec says:
452 * "A constant expression is one of
456 * - a built-in function call whose arguments are all constant
457 * expressions, with the exception of the texture lookup
460 * Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec says:
462 * "A constant expression is one of
466 * - a built-in function call whose arguments are all constant
467 * expressions, with the exception of the texture lookup
468 * functions. The built-in functions dFdx, dFdy, and fwidth must
469 * return 0 when evaluated inside an initializer with an argument
470 * that is a constant expression."
472 * If the function call is a constant expression, don't generate any
473 * instructions; just generate an ir_constant.
475 if (state
->is_version(120, 100)) {
476 ir_constant
*value
= sig
->constant_expression_value(actual_parameters
, NULL
);
482 ir_dereference_variable
*deref
= NULL
;
483 if (!sig
->return_type
->is_void()) {
484 /* Create a new temporary to hold the return value. */
485 char *const name
= ir_variable::temporaries_allocate_names
486 ? ralloc_asprintf(ctx
, "%s_retval", sig
->function_name())
491 var
= new(ctx
) ir_variable(sig
->return_type
, name
, ir_var_temporary
);
492 instructions
->push_tail(var
);
496 deref
= new(ctx
) ir_dereference_variable(var
);
499 ir_call
*call
= new(ctx
) ir_call(sig
, deref
, actual_parameters
, sub_var
, array_idx
);
500 instructions
->push_tail(call
);
502 /* Also emit any necessary out-parameter conversions. */
503 instructions
->append_list(&post_call_conversions
);
505 return deref
? deref
->clone(ctx
, NULL
) : NULL
;
509 * Given a function name and parameter list, find the matching signature.
511 static ir_function_signature
*
512 match_function_by_name(const char *name
,
513 exec_list
*actual_parameters
,
514 struct _mesa_glsl_parse_state
*state
)
517 ir_function
*f
= state
->symbols
->get_function(name
);
518 ir_function_signature
*local_sig
= NULL
;
519 ir_function_signature
*sig
= NULL
;
521 /* Is the function hidden by a record type constructor? */
522 if (state
->symbols
->get_type(name
))
523 goto done
; /* no match */
525 /* Is the function hidden by a variable (impossible in 1.10)? */
526 if (!state
->symbols
->separate_function_namespace
527 && state
->symbols
->get_variable(name
))
528 goto done
; /* no match */
531 /* In desktop GL, the presence of a user-defined signature hides any
532 * built-in signatures, so we must ignore them. In contrast, in ES2
533 * user-defined signatures add new overloads, so we must consider them.
535 bool allow_builtins
= state
->es_shader
|| !f
->has_user_signature();
537 /* Look for a match in the local shader. If exact, we're done. */
538 bool is_exact
= false;
539 sig
= local_sig
= f
->matching_signature(state
, actual_parameters
,
540 allow_builtins
, &is_exact
);
548 /* Local shader has no exact candidates; check the built-ins. */
549 _mesa_glsl_initialize_builtin_functions();
550 sig
= _mesa_glsl_find_builtin_function(state
, name
, actual_parameters
);
554 /* If the match is from a linked built-in shader, import the prototype. */
555 if (sig
!= local_sig
) {
557 f
= new(ctx
) ir_function(name
);
558 state
->symbols
->add_global_function(f
);
559 emit_function(state
, f
);
561 f
->add_signature(sig
->clone_prototype(f
, NULL
));
567 static ir_function_signature
*
568 match_subroutine_by_name(const char *name
,
569 exec_list
*actual_parameters
,
570 struct _mesa_glsl_parse_state
*state
,
574 ir_function_signature
*sig
= NULL
;
575 ir_function
*f
, *found
= NULL
;
576 const char *new_name
;
578 bool is_exact
= false;
580 new_name
= ralloc_asprintf(ctx
, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state
->stage
), name
);
581 var
= state
->symbols
->get_variable(new_name
);
585 for (int i
= 0; i
< state
->num_subroutine_types
; i
++) {
586 f
= state
->subroutine_types
[i
];
587 if (strcmp(f
->name
, var
->type
->without_array()->name
))
596 sig
= found
->matching_signature(state
, actual_parameters
,
602 generate_array_index(void *mem_ctx
, exec_list
*instructions
,
603 struct _mesa_glsl_parse_state
*state
, YYLTYPE loc
,
604 const ast_expression
*array
, ast_expression
*idx
,
605 const char **function_name
, exec_list
*actual_parameters
)
607 if (array
->oper
== ast_array_index
) {
608 /* This handles arrays of arrays */
609 ir_rvalue
*outer_array
= generate_array_index(mem_ctx
, instructions
,
611 array
->subexpressions
[0],
612 array
->subexpressions
[1],
613 function_name
, actual_parameters
);
614 ir_rvalue
*outer_array_idx
= idx
->hir(instructions
, state
);
616 YYLTYPE index_loc
= idx
->get_location();
617 return _mesa_ast_array_index_to_hir(mem_ctx
, state
, outer_array
,
618 outer_array_idx
, loc
,
621 ir_variable
*sub_var
= NULL
;
622 *function_name
= array
->primary_expression
.identifier
;
624 match_subroutine_by_name(*function_name
, actual_parameters
,
627 ir_rvalue
*outer_array_idx
= idx
->hir(instructions
, state
);
628 return new(mem_ctx
) ir_dereference_array(sub_var
, outer_array_idx
);
633 print_function_prototypes(_mesa_glsl_parse_state
*state
, YYLTYPE
*loc
,
639 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
640 if (sig
->is_builtin() && !sig
->is_builtin_available(state
))
643 char *str
= prototype_string(sig
->return_type
, f
->name
, &sig
->parameters
);
644 _mesa_glsl_error(loc
, state
, " %s", str
);
650 * Raise a "no matching function" error, listing all possible overloads the
651 * compiler considered so developers can figure out what went wrong.
654 no_matching_function_error(const char *name
,
656 exec_list
*actual_parameters
,
657 _mesa_glsl_parse_state
*state
)
659 gl_shader
*sh
= _mesa_glsl_get_builtin_function_shader();
661 if (state
->symbols
->get_function(name
) == NULL
662 && (!state
->uses_builtin_functions
663 || sh
->symbols
->get_function(name
) == NULL
)) {
664 _mesa_glsl_error(loc
, state
, "no function with name '%s'", name
);
666 char *str
= prototype_string(NULL
, name
, actual_parameters
);
667 _mesa_glsl_error(loc
, state
,
668 "no matching function for call to `%s'; candidates are:",
672 print_function_prototypes(state
, loc
, state
->symbols
->get_function(name
));
674 if (state
->uses_builtin_functions
) {
675 print_function_prototypes(state
, loc
, sh
->symbols
->get_function(name
));
681 * Perform automatic type conversion of constructor parameters
683 * This implements the rules in the "Conversion and Scalar Constructors"
684 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
687 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
)
689 void *ctx
= ralloc_parent(src
);
690 const unsigned a
= desired_type
->base_type
;
691 const unsigned b
= src
->type
->base_type
;
692 ir_expression
*result
= NULL
;
694 if (src
->type
->is_error())
697 assert(a
<= GLSL_TYPE_BOOL
);
698 assert(b
<= GLSL_TYPE_BOOL
);
707 result
= new(ctx
) ir_expression(ir_unop_i2u
, src
);
709 case GLSL_TYPE_FLOAT
:
710 result
= new(ctx
) ir_expression(ir_unop_f2u
, src
);
713 result
= new(ctx
) ir_expression(ir_unop_i2u
,
714 new(ctx
) ir_expression(ir_unop_b2i
, src
));
716 case GLSL_TYPE_DOUBLE
:
717 result
= new(ctx
) ir_expression(ir_unop_d2u
, src
);
724 result
= new(ctx
) ir_expression(ir_unop_u2i
, src
);
726 case GLSL_TYPE_FLOAT
:
727 result
= new(ctx
) ir_expression(ir_unop_f2i
, src
);
730 result
= new(ctx
) ir_expression(ir_unop_b2i
, src
);
732 case GLSL_TYPE_DOUBLE
:
733 result
= new(ctx
) ir_expression(ir_unop_d2i
, src
);
737 case GLSL_TYPE_FLOAT
:
740 result
= new(ctx
) ir_expression(ir_unop_u2f
, desired_type
, src
, NULL
);
743 result
= new(ctx
) ir_expression(ir_unop_i2f
, desired_type
, src
, NULL
);
746 result
= new(ctx
) ir_expression(ir_unop_b2f
, desired_type
, src
, NULL
);
748 case GLSL_TYPE_DOUBLE
:
749 result
= new(ctx
) ir_expression(ir_unop_d2f
, desired_type
, src
, NULL
);
756 result
= new(ctx
) ir_expression(ir_unop_i2b
,
757 new(ctx
) ir_expression(ir_unop_u2i
, src
));
760 result
= new(ctx
) ir_expression(ir_unop_i2b
, desired_type
, src
, NULL
);
762 case GLSL_TYPE_FLOAT
:
763 result
= new(ctx
) ir_expression(ir_unop_f2b
, desired_type
, src
, NULL
);
765 case GLSL_TYPE_DOUBLE
:
766 result
= new(ctx
) ir_expression(ir_unop_d2b
, desired_type
, src
, NULL
);
770 case GLSL_TYPE_DOUBLE
:
773 result
= new(ctx
) ir_expression(ir_unop_i2d
, src
);
776 result
= new(ctx
) ir_expression(ir_unop_u2d
, src
);
779 result
= new(ctx
) ir_expression(ir_unop_f2d
,
780 new(ctx
) ir_expression(ir_unop_b2f
, src
));
782 case GLSL_TYPE_FLOAT
:
783 result
= new(ctx
) ir_expression(ir_unop_f2d
, desired_type
, src
, NULL
);
788 assert(result
!= NULL
);
789 assert(result
->type
== desired_type
);
791 /* Try constant folding; it may fold in the conversion we just added. */
792 ir_constant
*const constant
= result
->constant_expression_value();
793 return (constant
!= NULL
) ? (ir_rvalue
*) constant
: (ir_rvalue
*) result
;
797 * Dereference a specific component from a scalar, vector, or matrix
800 dereference_component(ir_rvalue
*src
, unsigned component
)
802 void *ctx
= ralloc_parent(src
);
803 assert(component
< src
->type
->components());
805 /* If the source is a constant, just create a new constant instead of a
806 * dereference of the existing constant.
808 ir_constant
*constant
= src
->as_constant();
810 return new(ctx
) ir_constant(constant
, component
);
812 if (src
->type
->is_scalar()) {
814 } else if (src
->type
->is_vector()) {
815 return new(ctx
) ir_swizzle(src
, component
, 0, 0, 0, 1);
817 assert(src
->type
->is_matrix());
819 /* Dereference a row of the matrix, then call this function again to get
820 * a specific element from that row.
822 const int c
= component
/ src
->type
->column_type()->vector_elements
;
823 const int r
= component
% src
->type
->column_type()->vector_elements
;
824 ir_constant
*const col_index
= new(ctx
) ir_constant(c
);
825 ir_dereference
*const col
= new(ctx
) ir_dereference_array(src
, col_index
);
827 col
->type
= src
->type
->column_type();
829 return dereference_component(col
, r
);
832 assert(!"Should not get here.");
838 process_vec_mat_constructor(exec_list
*instructions
,
839 const glsl_type
*constructor_type
,
840 YYLTYPE
*loc
, exec_list
*parameters
,
841 struct _mesa_glsl_parse_state
*state
)
845 /* The ARB_shading_language_420pack spec says:
847 * "If an initializer is a list of initializers enclosed in curly braces,
848 * the variable being declared must be a vector, a matrix, an array, or a
851 * int i = { 1 }; // illegal, i is not an aggregate"
853 if (constructor_type
->vector_elements
<= 1) {
854 _mesa_glsl_error(loc
, state
, "aggregates can only initialize vectors, "
855 "matrices, arrays, and structs");
856 return ir_rvalue::error_value(ctx
);
859 exec_list actual_parameters
;
860 const unsigned parameter_count
=
861 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
863 if (parameter_count
== 0
864 || (constructor_type
->is_vector() &&
865 constructor_type
->vector_elements
!= parameter_count
)
866 || (constructor_type
->is_matrix() &&
867 constructor_type
->matrix_columns
!= parameter_count
)) {
868 _mesa_glsl_error(loc
, state
, "%s constructor must have %u parameters",
869 constructor_type
->is_vector() ? "vector" : "matrix",
870 constructor_type
->vector_elements
);
871 return ir_rvalue::error_value(ctx
);
874 bool all_parameters_are_constant
= true;
876 /* Type cast each parameter and, if possible, fold constants. */
877 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
878 ir_rvalue
*result
= ir
;
880 /* Apply implicit conversions (not the scalar constructor rules!). See
881 * the spec quote above. */
882 if (constructor_type
->base_type
!= result
->type
->base_type
) {
883 const glsl_type
*desired_type
=
884 glsl_type::get_instance(constructor_type
->base_type
,
885 ir
->type
->vector_elements
,
886 ir
->type
->matrix_columns
);
887 if (result
->type
->can_implicitly_convert_to(desired_type
, state
)) {
888 /* Even though convert_component() implements the constructor
889 * conversion rules (not the implicit conversion rules), its safe
890 * to use it here because we already checked that the implicit
891 * conversion is legal.
893 result
= convert_component(ir
, desired_type
);
897 if (constructor_type
->is_matrix()) {
898 if (result
->type
!= constructor_type
->column_type()) {
899 _mesa_glsl_error(loc
, state
, "type error in matrix constructor: "
900 "expected: %s, found %s",
901 constructor_type
->column_type()->name
,
903 return ir_rvalue::error_value(ctx
);
905 } else if (result
->type
!= constructor_type
->get_scalar_type()) {
906 _mesa_glsl_error(loc
, state
, "type error in vector constructor: "
907 "expected: %s, found %s",
908 constructor_type
->get_scalar_type()->name
,
910 return ir_rvalue::error_value(ctx
);
913 /* Attempt to convert the parameter to a constant valued expression.
914 * After doing so, track whether or not all the parameters to the
915 * constructor are trivially constant valued expressions.
917 ir_rvalue
*const constant
= result
->constant_expression_value();
919 if (constant
!= NULL
)
922 all_parameters_are_constant
= false;
924 ir
->replace_with(result
);
927 if (all_parameters_are_constant
)
928 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
930 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "vec_mat_ctor",
932 instructions
->push_tail(var
);
936 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
937 ir_instruction
*assignment
= NULL
;
939 if (var
->type
->is_matrix()) {
940 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
941 new(ctx
) ir_constant(i
));
942 assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
944 /* use writemask rather than index for vector */
945 assert(var
->type
->is_vector());
947 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
948 assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
, (unsigned)(1 << i
));
951 instructions
->push_tail(assignment
);
956 return new(ctx
) ir_dereference_variable(var
);
961 process_array_constructor(exec_list
*instructions
,
962 const glsl_type
*constructor_type
,
963 YYLTYPE
*loc
, exec_list
*parameters
,
964 struct _mesa_glsl_parse_state
*state
)
967 /* Array constructors come in two forms: sized and unsized. Sized array
968 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
969 * variables. In this case the number of parameters must exactly match the
970 * specified size of the array.
972 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
973 * are vec4 variables. In this case the size of the array being constructed
974 * is determined by the number of parameters.
976 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
978 * "There must be exactly the same number of arguments as the size of
979 * the array being constructed. If no size is present in the
980 * constructor, then the array is explicitly sized to the number of
981 * arguments provided. The arguments are assigned in order, starting at
982 * element 0, to the elements of the constructed array. Each argument
983 * must be the same type as the element type of the array, or be a type
984 * that can be converted to the element type of the array according to
985 * Section 4.1.10 "Implicit Conversions.""
987 exec_list actual_parameters
;
988 const unsigned parameter_count
=
989 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
990 bool is_unsized_array
= constructor_type
->is_unsized_array();
992 if ((parameter_count
== 0) ||
993 (!is_unsized_array
&& (constructor_type
->length
!= parameter_count
))) {
994 const unsigned min_param
= is_unsized_array
995 ? 1 : constructor_type
->length
;
997 _mesa_glsl_error(loc
, state
, "array constructor must have %s %u "
999 is_unsized_array
? "at least" : "exactly",
1000 min_param
, (min_param
<= 1) ? "" : "s");
1001 return ir_rvalue::error_value(ctx
);
1004 if (is_unsized_array
) {
1006 glsl_type::get_array_instance(constructor_type
->fields
.array
,
1008 assert(constructor_type
!= NULL
);
1009 assert(constructor_type
->length
== parameter_count
);
1012 bool all_parameters_are_constant
= true;
1013 const glsl_type
*element_type
= constructor_type
->fields
.array
;
1015 /* Type cast each parameter and, if possible, fold constants. */
1016 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1017 ir_rvalue
*result
= ir
;
1019 const glsl_base_type element_base_type
=
1020 constructor_type
->fields
.array
->base_type
;
1022 /* Apply implicit conversions (not the scalar constructor rules!). See
1023 * the spec quote above. */
1024 if (element_base_type
!= result
->type
->base_type
) {
1025 const glsl_type
*desired_type
=
1026 glsl_type::get_instance(element_base_type
,
1027 ir
->type
->vector_elements
,
1028 ir
->type
->matrix_columns
);
1030 if (result
->type
->can_implicitly_convert_to(desired_type
, state
)) {
1031 /* Even though convert_component() implements the constructor
1032 * conversion rules (not the implicit conversion rules), its safe
1033 * to use it here because we already checked that the implicit
1034 * conversion is legal.
1036 result
= convert_component(ir
, desired_type
);
1040 if (constructor_type
->fields
.array
->is_unsized_array()) {
1041 /* As the inner parameters of the constructor are created without
1042 * knowledge of each other we need to check to make sure unsized
1043 * parameters of unsized constructors all end up with the same size.
1045 * e.g we make sure to fail for a constructor like this:
1046 * vec4[][] a = vec4[][](vec4[](vec4(0.0), vec4(1.0)),
1047 * vec4[](vec4(0.0), vec4(1.0), vec4(1.0)),
1048 * vec4[](vec4(0.0), vec4(1.0)));
1050 if (element_type
->is_unsized_array()) {
1051 /* This is the first parameter so just get the type */
1052 element_type
= result
->type
;
1053 } else if (element_type
!= result
->type
) {
1054 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
1055 "expected: %s, found %s",
1057 result
->type
->name
);
1058 return ir_rvalue::error_value(ctx
);
1060 } else if (result
->type
!= constructor_type
->fields
.array
) {
1061 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
1062 "expected: %s, found %s",
1063 constructor_type
->fields
.array
->name
,
1064 result
->type
->name
);
1065 return ir_rvalue::error_value(ctx
);
1067 element_type
= result
->type
;
1070 /* Attempt to convert the parameter to a constant valued expression.
1071 * After doing so, track whether or not all the parameters to the
1072 * constructor are trivially constant valued expressions.
1074 ir_rvalue
*const constant
= result
->constant_expression_value();
1076 if (constant
!= NULL
)
1079 all_parameters_are_constant
= false;
1081 ir
->replace_with(result
);
1084 if (constructor_type
->fields
.array
->is_unsized_array()) {
1086 glsl_type::get_array_instance(element_type
,
1088 assert(constructor_type
!= NULL
);
1089 assert(constructor_type
->length
== parameter_count
);
1092 if (all_parameters_are_constant
)
1093 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1095 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "array_ctor",
1097 instructions
->push_tail(var
);
1100 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
1101 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
1102 new(ctx
) ir_constant(i
));
1104 ir_instruction
*assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
1105 instructions
->push_tail(assignment
);
1110 return new(ctx
) ir_dereference_variable(var
);
1115 * Try to convert a record constructor to a constant expression
1117 static ir_constant
*
1118 constant_record_constructor(const glsl_type
*constructor_type
,
1119 exec_list
*parameters
, void *mem_ctx
)
1121 foreach_in_list(ir_instruction
, node
, parameters
) {
1122 ir_constant
*constant
= node
->as_constant();
1123 if (constant
== NULL
)
1125 node
->replace_with(constant
);
1128 return new(mem_ctx
) ir_constant(constructor_type
, parameters
);
1133 * Determine if a list consists of a single scalar r-value
1136 single_scalar_parameter(exec_list
*parameters
)
1138 const ir_rvalue
*const p
= (ir_rvalue
*) parameters
->head
;
1139 assert(((ir_rvalue
*)p
)->as_rvalue() != NULL
);
1141 return (p
->type
->is_scalar() && p
->next
->is_tail_sentinel());
1146 * Generate inline code for a vector constructor
1148 * The generated constructor code will consist of a temporary variable
1149 * declaration of the same type as the constructor. A sequence of assignments
1150 * from constructor parameters to the temporary will follow.
1153 * An \c ir_dereference_variable of the temprorary generated in the constructor
1157 emit_inline_vector_constructor(const glsl_type
*type
,
1158 exec_list
*instructions
,
1159 exec_list
*parameters
,
1162 assert(!parameters
->is_empty());
1164 ir_variable
*var
= new(ctx
) ir_variable(type
, "vec_ctor", ir_var_temporary
);
1165 instructions
->push_tail(var
);
1167 /* There are three kinds of vector constructors.
1169 * - Construct a vector from a single scalar by replicating that scalar to
1170 * all components of the vector.
1172 * - Construct a vector from at least a matrix. This case should already
1173 * have been taken care of in ast_function_expression::hir by breaking
1174 * down the matrix into a series of column vectors.
1176 * - Construct a vector from an arbirary combination of vectors and
1177 * scalars. The components of the constructor parameters are assigned
1178 * to the vector in order until the vector is full.
1180 const unsigned lhs_components
= type
->components();
1181 if (single_scalar_parameter(parameters
)) {
1182 ir_rvalue
*first_param
= (ir_rvalue
*)parameters
->head
;
1183 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(first_param
, 0, 0, 0, 0,
1185 ir_dereference_variable
*lhs
= new(ctx
) ir_dereference_variable(var
);
1186 const unsigned mask
= (1U << lhs_components
) - 1;
1188 assert(rhs
->type
== lhs
->type
);
1190 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
, mask
);
1191 instructions
->push_tail(inst
);
1193 unsigned base_component
= 0;
1194 unsigned base_lhs_component
= 0;
1195 ir_constant_data data
;
1196 unsigned constant_mask
= 0, constant_components
= 0;
1198 memset(&data
, 0, sizeof(data
));
1200 foreach_in_list(ir_rvalue
, param
, parameters
) {
1201 unsigned rhs_components
= param
->type
->components();
1203 /* Do not try to assign more components to the vector than it has!
1205 if ((rhs_components
+ base_lhs_component
) > lhs_components
) {
1206 rhs_components
= lhs_components
- base_lhs_component
;
1209 const ir_constant
*const c
= param
->as_constant();
1211 for (unsigned i
= 0; i
< rhs_components
; i
++) {
1212 switch (c
->type
->base_type
) {
1213 case GLSL_TYPE_UINT
:
1214 data
.u
[i
+ base_component
] = c
->get_uint_component(i
);
1217 data
.i
[i
+ base_component
] = c
->get_int_component(i
);
1219 case GLSL_TYPE_FLOAT
:
1220 data
.f
[i
+ base_component
] = c
->get_float_component(i
);
1222 case GLSL_TYPE_DOUBLE
:
1223 data
.d
[i
+ base_component
] = c
->get_double_component(i
);
1225 case GLSL_TYPE_BOOL
:
1226 data
.b
[i
+ base_component
] = c
->get_bool_component(i
);
1229 assert(!"Should not get here.");
1234 /* Mask of fields to be written in the assignment.
1236 constant_mask
|= ((1U << rhs_components
) - 1) << base_lhs_component
;
1237 constant_components
+= rhs_components
;
1239 base_component
+= rhs_components
;
1241 /* Advance the component index by the number of components
1242 * that were just assigned.
1244 base_lhs_component
+= rhs_components
;
1247 if (constant_mask
!= 0) {
1248 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1249 const glsl_type
*rhs_type
= glsl_type::get_instance(var
->type
->base_type
,
1250 constant_components
,
1252 ir_rvalue
*rhs
= new(ctx
) ir_constant(rhs_type
, &data
);
1254 ir_instruction
*inst
=
1255 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, constant_mask
);
1256 instructions
->push_tail(inst
);
1260 foreach_in_list(ir_rvalue
, param
, parameters
) {
1261 unsigned rhs_components
= param
->type
->components();
1263 /* Do not try to assign more components to the vector than it has!
1265 if ((rhs_components
+ base_component
) > lhs_components
) {
1266 rhs_components
= lhs_components
- base_component
;
1269 /* If we do not have any components left to copy, break out of the
1270 * loop. This can happen when initializing a vec4 with a mat3 as the
1271 * mat3 would have been broken into a series of column vectors.
1273 if (rhs_components
== 0) {
1277 const ir_constant
*const c
= param
->as_constant();
1279 /* Mask of fields to be written in the assignment.
1281 const unsigned write_mask
= ((1U << rhs_components
) - 1)
1284 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1286 /* Generate a swizzle so that LHS and RHS sizes match.
1289 new(ctx
) ir_swizzle(param
, 0, 1, 2, 3, rhs_components
);
1291 ir_instruction
*inst
=
1292 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1293 instructions
->push_tail(inst
);
1296 /* Advance the component index by the number of components that were
1299 base_component
+= rhs_components
;
1302 return new(ctx
) ir_dereference_variable(var
);
1307 * Generate assignment of a portion of a vector to a portion of a matrix column
1309 * \param src_base First component of the source to be used in assignment
1310 * \param column Column of destination to be assiged
1311 * \param row_base First component of the destination column to be assigned
1312 * \param count Number of components to be assigned
1315 * \c src_base + \c count must be less than or equal to the number of components
1316 * in the source vector.
1319 assign_to_matrix_column(ir_variable
*var
, unsigned column
, unsigned row_base
,
1320 ir_rvalue
*src
, unsigned src_base
, unsigned count
,
1323 ir_constant
*col_idx
= new(mem_ctx
) ir_constant(column
);
1324 ir_dereference
*column_ref
= new(mem_ctx
) ir_dereference_array(var
, col_idx
);
1326 assert(column_ref
->type
->components() >= (row_base
+ count
));
1327 assert(src
->type
->components() >= (src_base
+ count
));
1329 /* Generate a swizzle that extracts the number of components from the source
1330 * that are to be assigned to the column of the matrix.
1332 if (count
< src
->type
->vector_elements
) {
1333 src
= new(mem_ctx
) ir_swizzle(src
,
1334 src_base
+ 0, src_base
+ 1,
1335 src_base
+ 2, src_base
+ 3,
1339 /* Mask of fields to be written in the assignment.
1341 const unsigned write_mask
= ((1U << count
) - 1) << row_base
;
1343 return new(mem_ctx
) ir_assignment(column_ref
, src
, NULL
, write_mask
);
1348 * Generate inline code for a matrix constructor
1350 * The generated constructor code will consist of a temporary variable
1351 * declaration of the same type as the constructor. A sequence of assignments
1352 * from constructor parameters to the temporary will follow.
1355 * An \c ir_dereference_variable of the temprorary generated in the constructor
1359 emit_inline_matrix_constructor(const glsl_type
*type
,
1360 exec_list
*instructions
,
1361 exec_list
*parameters
,
1364 assert(!parameters
->is_empty());
1366 ir_variable
*var
= new(ctx
) ir_variable(type
, "mat_ctor", ir_var_temporary
);
1367 instructions
->push_tail(var
);
1369 /* There are three kinds of matrix constructors.
1371 * - Construct a matrix from a single scalar by replicating that scalar to
1372 * along the diagonal of the matrix and setting all other components to
1375 * - Construct a matrix from an arbirary combination of vectors and
1376 * scalars. The components of the constructor parameters are assigned
1377 * to the matrix in column-major order until the matrix is full.
1379 * - Construct a matrix from a single matrix. The source matrix is copied
1380 * to the upper left portion of the constructed matrix, and the remaining
1381 * elements take values from the identity matrix.
1383 ir_rvalue
*const first_param
= (ir_rvalue
*) parameters
->head
;
1384 if (single_scalar_parameter(parameters
)) {
1385 /* Assign the scalar to the X component of a vec4, and fill the remaining
1386 * components with zero.
1388 glsl_base_type param_base_type
= first_param
->type
->base_type
;
1389 assert(param_base_type
== GLSL_TYPE_FLOAT
||
1390 param_base_type
== GLSL_TYPE_DOUBLE
);
1391 ir_variable
*rhs_var
=
1392 new(ctx
) ir_variable(glsl_type::get_instance(param_base_type
, 4, 1),
1395 instructions
->push_tail(rhs_var
);
1397 ir_constant_data zero
;
1398 for (unsigned i
= 0; i
< 4; i
++)
1399 if (param_base_type
== GLSL_TYPE_FLOAT
)
1404 ir_instruction
*inst
=
1405 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(rhs_var
),
1406 new(ctx
) ir_constant(rhs_var
->type
, &zero
),
1408 instructions
->push_tail(inst
);
1410 ir_dereference
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1412 inst
= new(ctx
) ir_assignment(rhs_ref
, first_param
, NULL
, 0x01);
1413 instructions
->push_tail(inst
);
1415 /* Assign the temporary vector to each column of the destination matrix
1416 * with a swizzle that puts the X component on the diagonal of the
1417 * matrix. In some cases this may mean that the X component does not
1418 * get assigned into the column at all (i.e., when the matrix has more
1419 * columns than rows).
1421 static const unsigned rhs_swiz
[4][4] = {
1428 const unsigned cols_to_init
= MIN2(type
->matrix_columns
,
1429 type
->vector_elements
);
1430 for (unsigned i
= 0; i
< cols_to_init
; i
++) {
1431 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1432 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
1434 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1435 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, rhs_swiz
[i
],
1436 type
->vector_elements
);
1438 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
1439 instructions
->push_tail(inst
);
1442 for (unsigned i
= cols_to_init
; i
< type
->matrix_columns
; i
++) {
1443 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1444 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
1446 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1447 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, 1, 1, 1, 1,
1448 type
->vector_elements
);
1450 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
1451 instructions
->push_tail(inst
);
1453 } else if (first_param
->type
->is_matrix()) {
1454 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
1456 * "If a matrix is constructed from a matrix, then each component
1457 * (column i, row j) in the result that has a corresponding
1458 * component (column i, row j) in the argument will be initialized
1459 * from there. All other components will be initialized to the
1460 * identity matrix. If a matrix argument is given to a matrix
1461 * constructor, it is an error to have any other arguments."
1463 assert(first_param
->next
->is_tail_sentinel());
1464 ir_rvalue
*const src_matrix
= first_param
;
1466 /* If the source matrix is smaller, pre-initialize the relavent parts of
1467 * the destination matrix to the identity matrix.
1469 if ((src_matrix
->type
->matrix_columns
< var
->type
->matrix_columns
)
1470 || (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)) {
1472 /* If the source matrix has fewer rows, every column of the destination
1473 * must be initialized. Otherwise only the columns in the destination
1474 * that do not exist in the source must be initialized.
1477 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)
1478 ? 0 : src_matrix
->type
->matrix_columns
;
1480 const glsl_type
*const col_type
= var
->type
->column_type();
1481 for (/* empty */; col
< var
->type
->matrix_columns
; col
++) {
1482 ir_constant_data ident
;
1491 ir_rvalue
*const rhs
= new(ctx
) ir_constant(col_type
, &ident
);
1493 ir_rvalue
*const lhs
=
1494 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(col
));
1496 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
1497 instructions
->push_tail(inst
);
1501 /* Assign columns from the source matrix to the destination matrix.
1503 * Since the parameter will be used in the RHS of multiple assignments,
1504 * generate a temporary and copy the paramter there.
1506 ir_variable
*const rhs_var
=
1507 new(ctx
) ir_variable(first_param
->type
, "mat_ctor_mat",
1509 instructions
->push_tail(rhs_var
);
1511 ir_dereference
*const rhs_var_ref
=
1512 new(ctx
) ir_dereference_variable(rhs_var
);
1513 ir_instruction
*const inst
=
1514 new(ctx
) ir_assignment(rhs_var_ref
, first_param
, NULL
);
1515 instructions
->push_tail(inst
);
1517 const unsigned last_row
= MIN2(src_matrix
->type
->vector_elements
,
1518 var
->type
->vector_elements
);
1519 const unsigned last_col
= MIN2(src_matrix
->type
->matrix_columns
,
1520 var
->type
->matrix_columns
);
1522 unsigned swiz
[4] = { 0, 0, 0, 0 };
1523 for (unsigned i
= 1; i
< last_row
; i
++)
1526 const unsigned write_mask
= (1U << last_row
) - 1;
1528 for (unsigned i
= 0; i
< last_col
; i
++) {
1529 ir_dereference
*const lhs
=
1530 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
1531 ir_rvalue
*const rhs_col
=
1532 new(ctx
) ir_dereference_array(rhs_var
, new(ctx
) ir_constant(i
));
1534 /* If one matrix has columns that are smaller than the columns of the
1535 * other matrix, wrap the column access of the larger with a swizzle
1536 * so that the LHS and RHS of the assignment have the same size (and
1537 * therefore have the same type).
1539 * It would be perfectly valid to unconditionally generate the
1540 * swizzles, this this will typically result in a more compact IR tree.
1543 if (lhs
->type
->vector_elements
!= rhs_col
->type
->vector_elements
) {
1544 rhs
= new(ctx
) ir_swizzle(rhs_col
, swiz
, last_row
);
1549 ir_instruction
*inst
=
1550 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1551 instructions
->push_tail(inst
);
1554 const unsigned cols
= type
->matrix_columns
;
1555 const unsigned rows
= type
->vector_elements
;
1556 unsigned remaining_slots
= rows
* cols
;
1557 unsigned col_idx
= 0;
1558 unsigned row_idx
= 0;
1560 foreach_in_list(ir_rvalue
, rhs
, parameters
) {
1561 unsigned rhs_components
= rhs
->type
->components();
1562 unsigned rhs_base
= 0;
1564 if (remaining_slots
== 0)
1567 /* Since the parameter might be used in the RHS of two assignments,
1568 * generate a temporary and copy the paramter there.
1570 ir_variable
*rhs_var
=
1571 new(ctx
) ir_variable(rhs
->type
, "mat_ctor_vec", ir_var_temporary
);
1572 instructions
->push_tail(rhs_var
);
1574 ir_dereference
*rhs_var_ref
=
1575 new(ctx
) ir_dereference_variable(rhs_var
);
1576 ir_instruction
*inst
= new(ctx
) ir_assignment(rhs_var_ref
, rhs
, NULL
);
1577 instructions
->push_tail(inst
);
1580 /* Assign the current parameter to as many components of the matrix
1583 * NOTE: A single vector parameter can span two matrix columns. A
1584 * single vec4, for example, can completely fill a mat2.
1586 unsigned count
= MIN2(rows
- row_idx
,
1587 rhs_components
- rhs_base
);
1589 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1590 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
1595 instructions
->push_tail(inst
);
1598 remaining_slots
-= count
;
1600 /* Sometimes, there is still data left in the parameters and
1601 * components left to be set in the destination but in other
1604 if (row_idx
>= rows
) {
1608 } while(remaining_slots
> 0 && rhs_base
< rhs_components
);
1612 return new(ctx
) ir_dereference_variable(var
);
1617 emit_inline_record_constructor(const glsl_type
*type
,
1618 exec_list
*instructions
,
1619 exec_list
*parameters
,
1622 ir_variable
*const var
=
1623 new(mem_ctx
) ir_variable(type
, "record_ctor", ir_var_temporary
);
1624 ir_dereference_variable
*const d
= new(mem_ctx
) ir_dereference_variable(var
);
1626 instructions
->push_tail(var
);
1628 exec_node
*node
= parameters
->head
;
1629 for (unsigned i
= 0; i
< type
->length
; i
++) {
1630 assert(!node
->is_tail_sentinel());
1632 ir_dereference
*const lhs
=
1633 new(mem_ctx
) ir_dereference_record(d
->clone(mem_ctx
, NULL
),
1634 type
->fields
.structure
[i
].name
);
1636 ir_rvalue
*const rhs
= ((ir_instruction
*) node
)->as_rvalue();
1637 assert(rhs
!= NULL
);
1639 ir_instruction
*const assign
= new(mem_ctx
) ir_assignment(lhs
, rhs
, NULL
);
1641 instructions
->push_tail(assign
);
1650 process_record_constructor(exec_list
*instructions
,
1651 const glsl_type
*constructor_type
,
1652 YYLTYPE
*loc
, exec_list
*parameters
,
1653 struct _mesa_glsl_parse_state
*state
)
1656 exec_list actual_parameters
;
1658 process_parameters(instructions
, &actual_parameters
,
1661 exec_node
*node
= actual_parameters
.head
;
1662 for (unsigned i
= 0; i
< constructor_type
->length
; i
++) {
1663 ir_rvalue
*ir
= (ir_rvalue
*) node
;
1665 if (node
->is_tail_sentinel()) {
1666 _mesa_glsl_error(loc
, state
,
1667 "insufficient parameters to constructor for `%s'",
1668 constructor_type
->name
);
1669 return ir_rvalue::error_value(ctx
);
1672 if (apply_implicit_conversion(constructor_type
->fields
.structure
[i
].type
,
1674 node
->replace_with(ir
);
1676 _mesa_glsl_error(loc
, state
,
1677 "parameter type mismatch in constructor for `%s.%s' "
1679 constructor_type
->name
,
1680 constructor_type
->fields
.structure
[i
].name
,
1682 constructor_type
->fields
.structure
[i
].type
->name
);
1683 return ir_rvalue::error_value(ctx
);;
1689 if (!node
->is_tail_sentinel()) {
1690 _mesa_glsl_error(loc
, state
, "too many parameters in constructor "
1691 "for `%s'", constructor_type
->name
);
1692 return ir_rvalue::error_value(ctx
);
1695 ir_rvalue
*const constant
=
1696 constant_record_constructor(constructor_type
, &actual_parameters
,
1699 return (constant
!= NULL
)
1701 : emit_inline_record_constructor(constructor_type
, instructions
,
1702 &actual_parameters
, state
);
1706 ast_function_expression::handle_method(exec_list
*instructions
,
1707 struct _mesa_glsl_parse_state
*state
)
1709 const ast_expression
*field
= subexpressions
[0];
1713 /* Handle "method calls" in GLSL 1.20 - namely, array.length() */
1714 YYLTYPE loc
= get_location();
1715 state
->check_version(120, 300, &loc
, "methods not supported");
1718 method
= field
->primary_expression
.identifier
;
1720 op
= field
->subexpressions
[0]->hir(instructions
, state
);
1721 if (strcmp(method
, "length") == 0) {
1722 if (!this->expressions
.is_empty()) {
1723 _mesa_glsl_error(&loc
, state
, "length method takes no arguments");
1727 if (op
->type
->is_array()) {
1728 if (op
->type
->is_unsized_array()) {
1729 if (!state
->has_shader_storage_buffer_objects()) {
1730 _mesa_glsl_error(&loc
, state
, "length called on unsized array"
1731 " only available with "
1732 "ARB_shader_storage_buffer_object");
1734 /* Calculate length of an unsized array in run-time */
1735 result
= new(ctx
) ir_expression(ir_unop_ssbo_unsized_array_length
, op
);
1737 result
= new(ctx
) ir_constant(op
->type
->array_size());
1739 } else if (op
->type
->is_vector()) {
1740 if (state
->has_420pack()) {
1741 /* .length() returns int. */
1742 result
= new(ctx
) ir_constant((int) op
->type
->vector_elements
);
1744 _mesa_glsl_error(&loc
, state
, "length method on matrix only available"
1745 "with ARB_shading_language_420pack");
1748 } else if (op
->type
->is_matrix()) {
1749 if (state
->has_420pack()) {
1750 /* .length() returns int. */
1751 result
= new(ctx
) ir_constant((int) op
->type
->matrix_columns
);
1753 _mesa_glsl_error(&loc
, state
, "length method on matrix only available"
1754 "with ARB_shading_language_420pack");
1758 _mesa_glsl_error(&loc
, state
, "length called on scalar.");
1762 _mesa_glsl_error(&loc
, state
, "unknown method: `%s'", method
);
1767 return ir_rvalue::error_value(ctx
);
1771 ast_function_expression::hir(exec_list
*instructions
,
1772 struct _mesa_glsl_parse_state
*state
)
1775 /* There are three sorts of function calls.
1777 * 1. constructors - The first subexpression is an ast_type_specifier.
1778 * 2. methods - Only the .length() method of array types.
1779 * 3. functions - Calls to regular old functions.
1782 if (is_constructor()) {
1783 const ast_type_specifier
*type
= (ast_type_specifier
*) subexpressions
[0];
1784 YYLTYPE loc
= type
->get_location();
1787 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
1789 /* constructor_type can be NULL if a variable with the same name as the
1790 * structure has come into scope.
1792 if (constructor_type
== NULL
) {
1793 _mesa_glsl_error(& loc
, state
, "unknown type `%s' (structure name "
1794 "may be shadowed by a variable with the same name)",
1796 return ir_rvalue::error_value(ctx
);
1800 /* Constructors for opaque types are illegal.
1802 if (constructor_type
->contains_opaque()) {
1803 _mesa_glsl_error(& loc
, state
, "cannot construct opaque type `%s'",
1804 constructor_type
->name
);
1805 return ir_rvalue::error_value(ctx
);
1808 if (constructor_type
->is_array()) {
1809 if (!state
->check_version(120, 300, &loc
,
1810 "array constructors forbidden")) {
1811 return ir_rvalue::error_value(ctx
);
1814 return process_array_constructor(instructions
, constructor_type
,
1815 & loc
, &this->expressions
, state
);
1819 /* There are two kinds of constructor calls. Constructors for arrays and
1820 * structures must have the exact number of arguments with matching types
1821 * in the correct order. These constructors follow essentially the same
1822 * type matching rules as functions.
1824 * Constructors for built-in language types, such as mat4 and vec2, are
1825 * free form. The only requirements are that the parameters must provide
1826 * enough values of the correct scalar type and that no arguments are
1827 * given past the last used argument.
1829 * When using the C-style initializer syntax from GLSL 4.20, constructors
1830 * must have the exact number of arguments with matching types in the
1833 if (constructor_type
->is_record()) {
1834 return process_record_constructor(instructions
, constructor_type
,
1835 &loc
, &this->expressions
,
1839 if (!constructor_type
->is_numeric() && !constructor_type
->is_boolean())
1840 return ir_rvalue::error_value(ctx
);
1842 /* Total number of components of the type being constructed. */
1843 const unsigned type_components
= constructor_type
->components();
1845 /* Number of components from parameters that have actually been
1846 * consumed. This is used to perform several kinds of error checking.
1848 unsigned components_used
= 0;
1850 unsigned matrix_parameters
= 0;
1851 unsigned nonmatrix_parameters
= 0;
1852 exec_list actual_parameters
;
1854 foreach_list_typed(ast_node
, ast
, link
, &this->expressions
) {
1855 ir_rvalue
*result
= ast
->hir(instructions
, state
);
1857 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1859 * "It is an error to provide extra arguments beyond this
1860 * last used argument."
1862 if (components_used
>= type_components
) {
1863 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
1865 constructor_type
->name
);
1866 return ir_rvalue::error_value(ctx
);
1869 if (!result
->type
->is_numeric() && !result
->type
->is_boolean()) {
1870 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
1871 "non-numeric data type",
1872 constructor_type
->name
);
1873 return ir_rvalue::error_value(ctx
);
1876 /* Count the number of matrix and nonmatrix parameters. This
1877 * is used below to enforce some of the constructor rules.
1879 if (result
->type
->is_matrix())
1880 matrix_parameters
++;
1882 nonmatrix_parameters
++;
1884 actual_parameters
.push_tail(result
);
1885 components_used
+= result
->type
->components();
1888 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1890 * "It is an error to construct matrices from other matrices. This
1891 * is reserved for future use."
1893 if (matrix_parameters
> 0
1894 && constructor_type
->is_matrix()
1895 && !state
->check_version(120, 100, &loc
,
1896 "cannot construct `%s' from a matrix",
1897 constructor_type
->name
)) {
1898 return ir_rvalue::error_value(ctx
);
1901 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1903 * "If a matrix argument is given to a matrix constructor, it is
1904 * an error to have any other arguments."
1906 if ((matrix_parameters
> 0)
1907 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
1908 && constructor_type
->is_matrix()) {
1909 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
1910 "matrix must be only parameter",
1911 constructor_type
->name
);
1912 return ir_rvalue::error_value(ctx
);
1915 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1917 * "In these cases, there must be enough components provided in the
1918 * arguments to provide an initializer for every component in the
1919 * constructed value."
1921 if (components_used
< type_components
&& components_used
!= 1
1922 && matrix_parameters
== 0) {
1923 _mesa_glsl_error(& loc
, state
, "too few components to construct "
1925 constructor_type
->name
);
1926 return ir_rvalue::error_value(ctx
);
1929 /* Matrices can never be consumed as is by any constructor but matrix
1930 * constructors. If the constructor type is not matrix, always break the
1931 * matrix up into a series of column vectors.
1933 if (!constructor_type
->is_matrix()) {
1934 foreach_in_list_safe(ir_rvalue
, matrix
, &actual_parameters
) {
1935 if (!matrix
->type
->is_matrix())
1938 /* Create a temporary containing the matrix. */
1939 ir_variable
*var
= new(ctx
) ir_variable(matrix
->type
, "matrix_tmp",
1941 instructions
->push_tail(var
);
1942 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
)
1943 ir_dereference_variable(var
), matrix
, NULL
));
1944 var
->constant_value
= matrix
->constant_expression_value();
1946 /* Replace the matrix with dereferences of its columns. */
1947 for (int i
= 0; i
< matrix
->type
->matrix_columns
; i
++) {
1948 matrix
->insert_before(new (ctx
) ir_dereference_array(var
,
1949 new(ctx
) ir_constant(i
)));
1955 bool all_parameters_are_constant
= true;
1957 /* Type cast each parameter and, if possible, fold constants.*/
1958 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1959 const glsl_type
*desired_type
=
1960 glsl_type::get_instance(constructor_type
->base_type
,
1961 ir
->type
->vector_elements
,
1962 ir
->type
->matrix_columns
);
1963 ir_rvalue
*result
= convert_component(ir
, desired_type
);
1965 /* Attempt to convert the parameter to a constant valued expression.
1966 * After doing so, track whether or not all the parameters to the
1967 * constructor are trivially constant valued expressions.
1969 ir_rvalue
*const constant
= result
->constant_expression_value();
1971 if (constant
!= NULL
)
1974 all_parameters_are_constant
= false;
1977 ir
->replace_with(result
);
1981 /* If all of the parameters are trivially constant, create a
1982 * constant representing the complete collection of parameters.
1984 if (all_parameters_are_constant
) {
1985 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1986 } else if (constructor_type
->is_scalar()) {
1987 return dereference_component((ir_rvalue
*) actual_parameters
.head
,
1989 } else if (constructor_type
->is_vector()) {
1990 return emit_inline_vector_constructor(constructor_type
,
1995 assert(constructor_type
->is_matrix());
1996 return emit_inline_matrix_constructor(constructor_type
,
2001 } else if (subexpressions
[0]->oper
== ast_field_selection
) {
2002 return handle_method(instructions
, state
);
2004 const ast_expression
*id
= subexpressions
[0];
2005 const char *func_name
;
2006 YYLTYPE loc
= get_location();
2007 exec_list actual_parameters
;
2008 ir_variable
*sub_var
= NULL
;
2009 ir_rvalue
*array_idx
= NULL
;
2011 process_parameters(instructions
, &actual_parameters
, &this->expressions
,
2014 if (id
->oper
== ast_array_index
) {
2015 array_idx
= generate_array_index(ctx
, instructions
, state
, loc
,
2016 id
->subexpressions
[0],
2017 id
->subexpressions
[1], &func_name
,
2018 &actual_parameters
);
2020 func_name
= id
->primary_expression
.identifier
;
2023 ir_function_signature
*sig
=
2024 match_function_by_name(func_name
, &actual_parameters
, state
);
2026 ir_rvalue
*value
= NULL
;
2028 sig
= match_subroutine_by_name(func_name
, &actual_parameters
, state
, &sub_var
);
2032 no_matching_function_error(func_name
, &loc
, &actual_parameters
, state
);
2033 value
= ir_rvalue::error_value(ctx
);
2034 } else if (!verify_parameter_modes(state
, sig
, actual_parameters
, this->expressions
)) {
2035 /* an error has already been emitted */
2036 value
= ir_rvalue::error_value(ctx
);
2038 value
= generate_call(instructions
, sig
, &actual_parameters
, sub_var
, array_idx
, state
);
2040 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::void_type
,
2043 instructions
->push_tail(tmp
);
2044 value
= new(ctx
) ir_dereference_variable(tmp
);
2051 unreachable("not reached");
2055 ast_function_expression::has_sequence_subexpression() const
2057 foreach_list_typed(const ast_node
, ast
, link
, &this->expressions
) {
2058 if (ast
->has_sequence_subexpression())
2066 ast_aggregate_initializer::hir(exec_list
*instructions
,
2067 struct _mesa_glsl_parse_state
*state
)
2070 YYLTYPE loc
= this->get_location();
2072 if (!this->constructor_type
) {
2073 _mesa_glsl_error(&loc
, state
, "type of C-style initializer unknown");
2074 return ir_rvalue::error_value(ctx
);
2076 const glsl_type
*const constructor_type
= this->constructor_type
;
2078 if (!state
->has_420pack()) {
2079 _mesa_glsl_error(&loc
, state
, "C-style initialization requires the "
2080 "GL_ARB_shading_language_420pack extension");
2081 return ir_rvalue::error_value(ctx
);
2084 if (constructor_type
->is_array()) {
2085 return process_array_constructor(instructions
, constructor_type
, &loc
,
2086 &this->expressions
, state
);
2089 if (constructor_type
->is_record()) {
2090 return process_record_constructor(instructions
, constructor_type
, &loc
,
2091 &this->expressions
, state
);
2094 return process_vec_mat_constructor(instructions
, constructor_type
, &loc
,
2095 &this->expressions
, state
);