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 "compiler/glsl_types.h"
28 #include "main/mtypes.h"
29 #include "main/shaderobj.h"
30 #include "builtin_functions.h"
33 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
);
36 process_parameters(exec_list
*instructions
, exec_list
*actual_parameters
,
37 exec_list
*parameters
,
38 struct _mesa_glsl_parse_state
*state
)
40 void *mem_ctx
= state
;
43 foreach_list_typed(ast_node
, ast
, link
, parameters
) {
44 /* We need to process the parameters first in order to know if we can
45 * raise or not a unitialized warning. Calling set_is_lhs silence the
46 * warning for now. Raising the warning or not will be checked at
47 * verify_parameter_modes.
49 ast
->set_is_lhs(true);
50 ir_rvalue
*result
= ast
->hir(instructions
, state
);
52 ir_constant
*const constant
=
53 result
->constant_expression_value(mem_ctx
);
58 actual_parameters
->push_tail(result
);
67 * Generate a source prototype for a function signature
69 * \param return_type Return type of the function. May be \c NULL.
70 * \param name Name of the function.
71 * \param parameters List of \c ir_instruction nodes representing the
72 * parameter list for the function. This may be either a
73 * formal (\c ir_variable) or actual (\c ir_rvalue)
74 * parameter list. Only the type is used.
77 * A ralloced string representing the prototype of the function.
80 prototype_string(const glsl_type
*return_type
, const char *name
,
81 exec_list
*parameters
)
85 if (return_type
!= NULL
)
86 str
= ralloc_asprintf(NULL
, "%s ", return_type
->name
);
88 ralloc_asprintf_append(&str
, "%s(", name
);
90 const char *comma
= "";
91 foreach_in_list(const ir_variable
, param
, parameters
) {
92 ralloc_asprintf_append(&str
, "%s%s", comma
, param
->type
->name
);
96 ralloc_strcat(&str
, ")");
101 verify_image_parameter(YYLTYPE
*loc
, _mesa_glsl_parse_state
*state
,
102 const ir_variable
*formal
, const ir_variable
*actual
)
105 * From the ARB_shader_image_load_store specification:
107 * "The values of image variables qualified with coherent,
108 * volatile, restrict, readonly, or writeonly may not be passed
109 * to functions whose formal parameters lack such
110 * qualifiers. [...] It is legal to have additional qualifiers
111 * on a formal parameter, but not to have fewer."
113 if (actual
->data
.memory_coherent
&& !formal
->data
.memory_coherent
) {
114 _mesa_glsl_error(loc
, state
,
115 "function call parameter `%s' drops "
116 "`coherent' qualifier", formal
->name
);
120 if (actual
->data
.memory_volatile
&& !formal
->data
.memory_volatile
) {
121 _mesa_glsl_error(loc
, state
,
122 "function call parameter `%s' drops "
123 "`volatile' qualifier", formal
->name
);
127 if (actual
->data
.memory_restrict
&& !formal
->data
.memory_restrict
) {
128 _mesa_glsl_error(loc
, state
,
129 "function call parameter `%s' drops "
130 "`restrict' qualifier", formal
->name
);
134 if (actual
->data
.memory_read_only
&& !formal
->data
.memory_read_only
) {
135 _mesa_glsl_error(loc
, state
,
136 "function call parameter `%s' drops "
137 "`readonly' qualifier", formal
->name
);
141 if (actual
->data
.memory_write_only
&& !formal
->data
.memory_write_only
) {
142 _mesa_glsl_error(loc
, state
,
143 "function call parameter `%s' drops "
144 "`writeonly' qualifier", formal
->name
);
152 verify_first_atomic_parameter(YYLTYPE
*loc
, _mesa_glsl_parse_state
*state
,
156 (!var
->is_in_shader_storage_block() &&
157 var
->data
.mode
!= ir_var_shader_shared
)) {
158 _mesa_glsl_error(loc
, state
, "First argument to atomic function "
159 "must be a buffer or shared variable");
166 is_atomic_function(const char *func_name
)
168 return !strcmp(func_name
, "atomicAdd") ||
169 !strcmp(func_name
, "atomicMin") ||
170 !strcmp(func_name
, "atomicMax") ||
171 !strcmp(func_name
, "atomicAnd") ||
172 !strcmp(func_name
, "atomicOr") ||
173 !strcmp(func_name
, "atomicXor") ||
174 !strcmp(func_name
, "atomicExchange") ||
175 !strcmp(func_name
, "atomicCompSwap");
179 * Verify that 'out' and 'inout' actual parameters are lvalues. Also, verify
180 * that 'const_in' formal parameters (an extension in our IR) correspond to
181 * ir_constant actual parameters.
184 verify_parameter_modes(_mesa_glsl_parse_state
*state
,
185 ir_function_signature
*sig
,
186 exec_list
&actual_ir_parameters
,
187 exec_list
&actual_ast_parameters
)
189 exec_node
*actual_ir_node
= actual_ir_parameters
.get_head_raw();
190 exec_node
*actual_ast_node
= actual_ast_parameters
.get_head_raw();
192 foreach_in_list(const ir_variable
, formal
, &sig
->parameters
) {
193 /* The lists must be the same length. */
194 assert(!actual_ir_node
->is_tail_sentinel());
195 assert(!actual_ast_node
->is_tail_sentinel());
197 const ir_rvalue
*const actual
= (ir_rvalue
*) actual_ir_node
;
198 const ast_expression
*const actual_ast
=
199 exec_node_data(ast_expression
, actual_ast_node
, link
);
201 /* FIXME: 'loc' is incorrect (as of 2011-01-21). It is always
204 YYLTYPE loc
= actual_ast
->get_location();
206 /* Verify that 'const_in' parameters are ir_constants. */
207 if (formal
->data
.mode
== ir_var_const_in
&&
208 actual
->ir_type
!= ir_type_constant
) {
209 _mesa_glsl_error(&loc
, state
,
210 "parameter `in %s' must be a constant expression",
215 /* Verify that shader_in parameters are shader inputs */
216 if (formal
->data
.must_be_shader_input
) {
217 const ir_rvalue
*val
= actual
;
219 /* GLSL 4.40 allows swizzles, while earlier GLSL versions do not. */
220 if (val
->ir_type
== ir_type_swizzle
) {
221 if (!state
->is_version(440, 0)) {
222 _mesa_glsl_error(&loc
, state
,
223 "parameter `%s` must not be swizzled",
227 val
= ((ir_swizzle
*)val
)->val
;
231 if (val
->ir_type
== ir_type_dereference_array
) {
232 val
= ((ir_dereference_array
*)val
)->array
;
233 } else if (val
->ir_type
== ir_type_dereference_record
&&
235 val
= ((ir_dereference_record
*)val
)->record
;
240 ir_variable
*var
= NULL
;
241 if (const ir_dereference_variable
*deref_var
= val
->as_dereference_variable())
242 var
= deref_var
->variable_referenced();
244 if (!var
|| var
->data
.mode
!= ir_var_shader_in
) {
245 _mesa_glsl_error(&loc
, state
,
246 "parameter `%s` must be a shader input",
251 var
->data
.must_be_shader_input
= 1;
254 /* Verify that 'out' and 'inout' actual parameters are lvalues. */
255 if (formal
->data
.mode
== ir_var_function_out
256 || formal
->data
.mode
== ir_var_function_inout
) {
257 const char *mode
= NULL
;
258 switch (formal
->data
.mode
) {
259 case ir_var_function_out
: mode
= "out"; break;
260 case ir_var_function_inout
: mode
= "inout"; break;
261 default: assert(false); break;
264 /* This AST-based check catches errors like f(i++). The IR-based
265 * is_lvalue() is insufficient because the actual parameter at the
266 * IR-level is just a temporary value, which is an l-value.
268 if (actual_ast
->non_lvalue_description
!= NULL
) {
269 _mesa_glsl_error(&loc
, state
,
270 "function parameter '%s %s' references a %s",
272 actual_ast
->non_lvalue_description
);
276 ir_variable
*var
= actual
->variable_referenced();
278 if (var
&& formal
->data
.mode
== ir_var_function_inout
) {
279 if ((var
->data
.mode
== ir_var_auto
||
280 var
->data
.mode
== ir_var_shader_out
) &&
281 !var
->data
.assigned
&&
282 !is_gl_identifier(var
->name
)) {
283 _mesa_glsl_warning(&loc
, state
, "`%s' used uninitialized",
289 var
->data
.assigned
= true;
291 if (var
&& var
->data
.read_only
) {
292 _mesa_glsl_error(&loc
, state
,
293 "function parameter '%s %s' references the "
294 "read-only variable '%s'",
296 actual
->variable_referenced()->name
);
298 } else if (!actual
->is_lvalue(state
)) {
299 _mesa_glsl_error(&loc
, state
,
300 "function parameter '%s %s' is not an lvalue",
305 assert(formal
->data
.mode
== ir_var_function_in
||
306 formal
->data
.mode
== ir_var_const_in
);
307 ir_variable
*var
= actual
->variable_referenced();
309 if ((var
->data
.mode
== ir_var_auto
||
310 var
->data
.mode
== ir_var_shader_out
) &&
311 !var
->data
.assigned
&&
312 !is_gl_identifier(var
->name
)) {
313 _mesa_glsl_warning(&loc
, state
, "`%s' used uninitialized",
319 if (formal
->type
->is_image() &&
320 actual
->variable_referenced()) {
321 if (!verify_image_parameter(&loc
, state
, formal
,
322 actual
->variable_referenced()))
326 actual_ir_node
= actual_ir_node
->next
;
327 actual_ast_node
= actual_ast_node
->next
;
330 /* The first parameter of atomic functions must be a buffer variable */
331 const char *func_name
= sig
->function_name();
332 bool is_atomic
= is_atomic_function(func_name
);
334 const ir_rvalue
*const actual
=
335 (ir_rvalue
*) actual_ir_parameters
.get_head_raw();
337 const ast_expression
*const actual_ast
=
338 exec_node_data(ast_expression
,
339 actual_ast_parameters
.get_head_raw(), link
);
340 YYLTYPE loc
= actual_ast
->get_location();
342 if (!verify_first_atomic_parameter(&loc
, state
,
343 actual
->variable_referenced())) {
351 struct copy_index_deref_data
{
353 exec_list
*before_instructions
;
357 copy_index_derefs_to_temps(ir_instruction
*ir
, void *data
)
359 struct copy_index_deref_data
*d
= (struct copy_index_deref_data
*)data
;
361 if (ir
->ir_type
== ir_type_dereference_array
) {
362 ir_dereference_array
*a
= (ir_dereference_array
*) ir
;
363 ir
= a
->array
->as_dereference();
365 ir_rvalue
*idx
= a
->array_index
;
366 ir_variable
*var
= idx
->variable_referenced();
368 /* If the index is read only it cannot change so there is no need
371 if (!var
|| var
->data
.read_only
|| var
->data
.memory_read_only
)
374 ir_variable
*tmp
= new(d
->mem_ctx
) ir_variable(idx
->type
, "idx_tmp",
376 d
->before_instructions
->push_tail(tmp
);
378 ir_dereference_variable
*const deref_tmp_1
=
379 new(d
->mem_ctx
) ir_dereference_variable(tmp
);
380 ir_assignment
*const assignment
=
381 new(d
->mem_ctx
) ir_assignment(deref_tmp_1
,
382 idx
->clone(d
->mem_ctx
, NULL
));
383 d
->before_instructions
->push_tail(assignment
);
385 /* Replace the array index with a dereference of the new temporary */
386 ir_dereference_variable
*const deref_tmp_2
=
387 new(d
->mem_ctx
) ir_dereference_variable(tmp
);
388 a
->array_index
= deref_tmp_2
;
393 fix_parameter(void *mem_ctx
, ir_rvalue
*actual
, const glsl_type
*formal_type
,
394 exec_list
*before_instructions
, exec_list
*after_instructions
,
395 bool parameter_is_inout
)
397 ir_expression
*const expr
= actual
->as_expression();
399 /* If the types match exactly and the parameter is not a vector-extract,
400 * nothing needs to be done to fix the parameter.
402 if (formal_type
== actual
->type
403 && (expr
== NULL
|| expr
->operation
!= ir_binop_vector_extract
)
404 && actual
->as_dereference_variable())
407 /* An array index could also be an out variable so we need to make a copy
408 * of them before the function is called.
410 if (!actual
->as_dereference_variable()) {
411 struct copy_index_deref_data data
;
412 data
.mem_ctx
= mem_ctx
;
413 data
.before_instructions
= before_instructions
;
415 visit_tree(actual
, copy_index_derefs_to_temps
, &data
);
418 /* To convert an out parameter, we need to create a temporary variable to
419 * hold the value before conversion, and then perform the conversion after
420 * the function call returns.
422 * This has the effect of transforming code like this:
428 * Into IR that's equivalent to this:
432 * int out_parameter_conversion;
433 * f(out_parameter_conversion);
434 * value = float(out_parameter_conversion);
436 * If the parameter is an ir_expression of ir_binop_vector_extract,
437 * additional conversion is needed in the post-call re-write.
440 new(mem_ctx
) ir_variable(formal_type
, "inout_tmp", ir_var_temporary
);
442 before_instructions
->push_tail(tmp
);
444 /* If the parameter is an inout parameter, copy the value of the actual
445 * parameter to the new temporary. Note that no type conversion is allowed
446 * here because inout parameters must match types exactly.
448 if (parameter_is_inout
) {
449 /* Inout parameters should never require conversion, since that would
450 * require an implicit conversion to exist both to and from the formal
451 * parameter type, and there are no bidirectional implicit conversions.
453 assert (actual
->type
== formal_type
);
455 ir_dereference_variable
*const deref_tmp_1
=
456 new(mem_ctx
) ir_dereference_variable(tmp
);
457 ir_assignment
*const assignment
=
458 new(mem_ctx
) ir_assignment(deref_tmp_1
, actual
->clone(mem_ctx
, NULL
));
459 before_instructions
->push_tail(assignment
);
462 /* Replace the parameter in the call with a dereference of the new
465 ir_dereference_variable
*const deref_tmp_2
=
466 new(mem_ctx
) ir_dereference_variable(tmp
);
467 actual
->replace_with(deref_tmp_2
);
470 /* Copy the temporary variable to the actual parameter with optional
471 * type conversion applied.
473 ir_rvalue
*rhs
= new(mem_ctx
) ir_dereference_variable(tmp
);
474 if (actual
->type
!= formal_type
)
475 rhs
= convert_component(rhs
, actual
->type
);
477 ir_rvalue
*lhs
= actual
;
478 if (expr
!= NULL
&& expr
->operation
== ir_binop_vector_extract
) {
479 lhs
= new(mem_ctx
) ir_dereference_array(expr
->operands
[0]->clone(mem_ctx
,
481 expr
->operands
[1]->clone(mem_ctx
,
485 ir_assignment
*const assignment_2
= new(mem_ctx
) ir_assignment(lhs
, rhs
);
486 after_instructions
->push_tail(assignment_2
);
490 * Generate a function call.
492 * For non-void functions, this returns a dereference of the temporary
493 * variable which stores the return value for the call. For void functions,
497 generate_call(exec_list
*instructions
, ir_function_signature
*sig
,
498 exec_list
*actual_parameters
,
499 ir_variable
*sub_var
,
500 ir_rvalue
*array_idx
,
501 struct _mesa_glsl_parse_state
*state
)
504 exec_list post_call_conversions
;
506 /* Perform implicit conversion of arguments. For out parameters, we need
507 * to place them in a temporary variable and do the conversion after the
508 * call takes place. Since we haven't emitted the call yet, we'll place
509 * the post-call conversions in a temporary exec_list, and emit them later.
511 foreach_two_lists(formal_node
, &sig
->parameters
,
512 actual_node
, actual_parameters
) {
513 ir_rvalue
*actual
= (ir_rvalue
*) actual_node
;
514 ir_variable
*formal
= (ir_variable
*) formal_node
;
516 if (formal
->type
->is_numeric() || formal
->type
->is_boolean()) {
517 switch (formal
->data
.mode
) {
518 case ir_var_const_in
:
519 case ir_var_function_in
: {
521 = convert_component(actual
, formal
->type
);
522 actual
->replace_with(converted
);
525 case ir_var_function_out
:
526 case ir_var_function_inout
:
527 fix_parameter(ctx
, actual
, formal
->type
,
528 instructions
, &post_call_conversions
,
529 formal
->data
.mode
== ir_var_function_inout
);
532 assert (!"Illegal formal parameter mode");
538 /* Section 4.3.2 (Const) of the GLSL 1.10.59 spec says:
540 * "Initializers for const declarations must be formed from literal
541 * values, other const variables (not including function call
542 * paramaters), or expressions of these.
544 * Constructors may be used in such expressions, but function calls may
547 * Section 4.3.3 (Constant Expressions) of the GLSL 1.20.8 spec says:
549 * "A constant expression is one of
553 * - a built-in function call whose arguments are all constant
554 * expressions, with the exception of the texture lookup
555 * functions, the noise functions, and ftransform. The built-in
556 * functions dFdx, dFdy, and fwidth must return 0 when evaluated
557 * inside an initializer with an argument that is a constant
560 * Section 5.10 (Constant Expressions) of the GLSL ES 1.00.17 spec says:
562 * "A constant expression is one of
566 * - a built-in function call whose arguments are all constant
567 * expressions, with the exception of the texture lookup
570 * Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec says:
572 * "A constant expression is one of
576 * - a built-in function call whose arguments are all constant
577 * expressions, with the exception of the texture lookup
578 * functions. The built-in functions dFdx, dFdy, and fwidth must
579 * return 0 when evaluated inside an initializer with an argument
580 * that is a constant expression."
582 * If the function call is a constant expression, don't generate any
583 * instructions; just generate an ir_constant.
585 if (state
->is_version(120, 100) ||
586 state
->ctx
->Const
.AllowGLSLBuiltinConstantExpression
) {
587 ir_constant
*value
= sig
->constant_expression_value(ctx
,
595 ir_dereference_variable
*deref
= NULL
;
596 if (!sig
->return_type
->is_void()) {
597 /* Create a new temporary to hold the return value. */
598 char *const name
= ir_variable::temporaries_allocate_names
599 ? ralloc_asprintf(ctx
, "%s_retval", sig
->function_name())
604 var
= new(ctx
) ir_variable(sig
->return_type
, name
, ir_var_temporary
);
605 instructions
->push_tail(var
);
609 deref
= new(ctx
) ir_dereference_variable(var
);
612 ir_call
*call
= new(ctx
) ir_call(sig
, deref
,
613 actual_parameters
, sub_var
, array_idx
);
614 instructions
->push_tail(call
);
615 if (sig
->is_builtin()) {
616 /* inline immediately */
617 call
->generate_inline(call
);
621 /* Also emit any necessary out-parameter conversions. */
622 instructions
->append_list(&post_call_conversions
);
624 return deref
? deref
->clone(ctx
, NULL
) : NULL
;
628 * Given a function name and parameter list, find the matching signature.
630 static ir_function_signature
*
631 match_function_by_name(const char *name
,
632 exec_list
*actual_parameters
,
633 struct _mesa_glsl_parse_state
*state
)
635 ir_function
*f
= state
->symbols
->get_function(name
);
636 ir_function_signature
*local_sig
= NULL
;
637 ir_function_signature
*sig
= NULL
;
639 /* Is the function hidden by a record type constructor? */
640 if (state
->symbols
->get_type(name
))
641 return sig
; /* no match */
643 /* Is the function hidden by a variable (impossible in 1.10)? */
644 if (!state
->symbols
->separate_function_namespace
645 && state
->symbols
->get_variable(name
))
646 return sig
; /* no match */
649 /* In desktop GL, the presence of a user-defined signature hides any
650 * built-in signatures, so we must ignore them. In contrast, in ES2
651 * user-defined signatures add new overloads, so we must consider them.
653 bool allow_builtins
= state
->es_shader
|| !f
->has_user_signature();
655 /* Look for a match in the local shader. If exact, we're done. */
656 bool is_exact
= false;
657 sig
= local_sig
= f
->matching_signature(state
, actual_parameters
,
658 allow_builtins
, &is_exact
);
666 /* Local shader has no exact candidates; check the built-ins. */
667 sig
= _mesa_glsl_find_builtin_function(state
, name
, actual_parameters
);
669 /* if _mesa_glsl_find_builtin_function failed, fall back to the result
670 * of choose_best_inexact_overload() instead. This should only affect
673 return sig
? sig
: local_sig
;
676 static ir_function_signature
*
677 match_subroutine_by_name(const char *name
,
678 exec_list
*actual_parameters
,
679 struct _mesa_glsl_parse_state
*state
,
683 ir_function_signature
*sig
= NULL
;
684 ir_function
*f
, *found
= NULL
;
685 const char *new_name
;
687 bool is_exact
= false;
690 ralloc_asprintf(ctx
, "%s_%s",
691 _mesa_shader_stage_to_subroutine_prefix(state
->stage
),
693 var
= state
->symbols
->get_variable(new_name
);
697 for (int i
= 0; i
< state
->num_subroutine_types
; i
++) {
698 f
= state
->subroutine_types
[i
];
699 if (strcmp(f
->name
, var
->type
->without_array()->name
))
708 sig
= found
->matching_signature(state
, actual_parameters
,
714 generate_array_index(void *mem_ctx
, exec_list
*instructions
,
715 struct _mesa_glsl_parse_state
*state
, YYLTYPE loc
,
716 const ast_expression
*array
, ast_expression
*idx
,
717 const char **function_name
, exec_list
*actual_parameters
)
719 if (array
->oper
== ast_array_index
) {
720 /* This handles arrays of arrays */
721 ir_rvalue
*outer_array
= generate_array_index(mem_ctx
, instructions
,
723 array
->subexpressions
[0],
724 array
->subexpressions
[1],
727 ir_rvalue
*outer_array_idx
= idx
->hir(instructions
, state
);
729 YYLTYPE index_loc
= idx
->get_location();
730 return _mesa_ast_array_index_to_hir(mem_ctx
, state
, outer_array
,
731 outer_array_idx
, loc
,
734 ir_variable
*sub_var
= NULL
;
735 *function_name
= array
->primary_expression
.identifier
;
737 if (!match_subroutine_by_name(*function_name
, actual_parameters
,
739 _mesa_glsl_error(&loc
, state
, "Unknown subroutine `%s'",
741 *function_name
= NULL
; /* indicate error condition to caller */
745 ir_rvalue
*outer_array_idx
= idx
->hir(instructions
, state
);
746 return new(mem_ctx
) ir_dereference_array(sub_var
, outer_array_idx
);
751 function_exists(_mesa_glsl_parse_state
*state
,
752 struct glsl_symbol_table
*symbols
, const char *name
)
754 ir_function
*f
= symbols
->get_function(name
);
756 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
757 if (sig
->is_builtin() && !sig
->is_builtin_available(state
))
766 print_function_prototypes(_mesa_glsl_parse_state
*state
, YYLTYPE
*loc
,
772 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
773 if (sig
->is_builtin() && !sig
->is_builtin_available(state
))
776 char *str
= prototype_string(sig
->return_type
, f
->name
,
778 _mesa_glsl_error(loc
, state
, " %s", str
);
784 * Raise a "no matching function" error, listing all possible overloads the
785 * compiler considered so developers can figure out what went wrong.
788 no_matching_function_error(const char *name
,
790 exec_list
*actual_parameters
,
791 _mesa_glsl_parse_state
*state
)
793 gl_shader
*sh
= _mesa_glsl_get_builtin_function_shader();
795 if (!function_exists(state
, state
->symbols
, name
)
796 && (!state
->uses_builtin_functions
797 || !function_exists(state
, sh
->symbols
, name
))) {
798 _mesa_glsl_error(loc
, state
, "no function with name '%s'", name
);
800 char *str
= prototype_string(NULL
, name
, actual_parameters
);
801 _mesa_glsl_error(loc
, state
,
802 "no matching function for call to `%s';"
807 print_function_prototypes(state
, loc
,
808 state
->symbols
->get_function(name
));
810 if (state
->uses_builtin_functions
) {
811 print_function_prototypes(state
, loc
,
812 sh
->symbols
->get_function(name
));
818 * Perform automatic type conversion of constructor parameters
820 * This implements the rules in the "Conversion and Scalar Constructors"
821 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
824 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
)
826 void *ctx
= ralloc_parent(src
);
827 const unsigned a
= desired_type
->base_type
;
828 const unsigned b
= src
->type
->base_type
;
829 ir_expression
*result
= NULL
;
831 if (src
->type
->is_error())
834 assert(a
<= GLSL_TYPE_IMAGE
);
835 assert(b
<= GLSL_TYPE_IMAGE
);
844 result
= new(ctx
) ir_expression(ir_unop_i2u
, src
);
846 case GLSL_TYPE_FLOAT
:
847 result
= new(ctx
) ir_expression(ir_unop_f2u
, src
);
850 result
= new(ctx
) ir_expression(ir_unop_i2u
,
851 new(ctx
) ir_expression(ir_unop_b2i
,
854 case GLSL_TYPE_DOUBLE
:
855 result
= new(ctx
) ir_expression(ir_unop_d2u
, src
);
857 case GLSL_TYPE_UINT64
:
858 result
= new(ctx
) ir_expression(ir_unop_u642u
, src
);
860 case GLSL_TYPE_INT64
:
861 result
= new(ctx
) ir_expression(ir_unop_i642u
, src
);
863 case GLSL_TYPE_SAMPLER
:
864 result
= new(ctx
) ir_expression(ir_unop_unpack_sampler_2x32
, src
);
866 case GLSL_TYPE_IMAGE
:
867 result
= new(ctx
) ir_expression(ir_unop_unpack_image_2x32
, src
);
874 result
= new(ctx
) ir_expression(ir_unop_u2i
, src
);
876 case GLSL_TYPE_FLOAT
:
877 result
= new(ctx
) ir_expression(ir_unop_f2i
, src
);
880 result
= new(ctx
) ir_expression(ir_unop_b2i
, src
);
882 case GLSL_TYPE_DOUBLE
:
883 result
= new(ctx
) ir_expression(ir_unop_d2i
, src
);
885 case GLSL_TYPE_UINT64
:
886 result
= new(ctx
) ir_expression(ir_unop_u642i
, src
);
888 case GLSL_TYPE_INT64
:
889 result
= new(ctx
) ir_expression(ir_unop_i642i
, src
);
893 case GLSL_TYPE_FLOAT
:
896 result
= new(ctx
) ir_expression(ir_unop_u2f
, desired_type
, src
, NULL
);
899 result
= new(ctx
) ir_expression(ir_unop_i2f
, desired_type
, src
, NULL
);
902 result
= new(ctx
) ir_expression(ir_unop_b2f
, desired_type
, src
, NULL
);
904 case GLSL_TYPE_DOUBLE
:
905 result
= new(ctx
) ir_expression(ir_unop_d2f
, desired_type
, src
, NULL
);
907 case GLSL_TYPE_UINT64
:
908 result
= new(ctx
) ir_expression(ir_unop_u642f
, desired_type
, src
, NULL
);
910 case GLSL_TYPE_INT64
:
911 result
= new(ctx
) ir_expression(ir_unop_i642f
, desired_type
, src
, NULL
);
918 result
= new(ctx
) ir_expression(ir_unop_i2b
,
919 new(ctx
) ir_expression(ir_unop_u2i
,
923 result
= new(ctx
) ir_expression(ir_unop_i2b
, desired_type
, src
, NULL
);
925 case GLSL_TYPE_FLOAT
:
926 result
= new(ctx
) ir_expression(ir_unop_f2b
, desired_type
, src
, NULL
);
928 case GLSL_TYPE_DOUBLE
:
929 result
= new(ctx
) ir_expression(ir_unop_d2b
, desired_type
, src
, NULL
);
931 case GLSL_TYPE_UINT64
:
932 result
= new(ctx
) ir_expression(ir_unop_i642b
,
933 new(ctx
) ir_expression(ir_unop_u642i64
,
936 case GLSL_TYPE_INT64
:
937 result
= new(ctx
) ir_expression(ir_unop_i642b
, desired_type
, src
, NULL
);
941 case GLSL_TYPE_DOUBLE
:
944 result
= new(ctx
) ir_expression(ir_unop_i2d
, src
);
947 result
= new(ctx
) ir_expression(ir_unop_u2d
, src
);
950 result
= new(ctx
) ir_expression(ir_unop_f2d
,
951 new(ctx
) ir_expression(ir_unop_b2f
,
954 case GLSL_TYPE_FLOAT
:
955 result
= new(ctx
) ir_expression(ir_unop_f2d
, desired_type
, src
, NULL
);
957 case GLSL_TYPE_UINT64
:
958 result
= new(ctx
) ir_expression(ir_unop_u642d
, desired_type
, src
, NULL
);
960 case GLSL_TYPE_INT64
:
961 result
= new(ctx
) ir_expression(ir_unop_i642d
, desired_type
, src
, NULL
);
965 case GLSL_TYPE_UINT64
:
968 result
= new(ctx
) ir_expression(ir_unop_i2u64
, src
);
971 result
= new(ctx
) ir_expression(ir_unop_u2u64
, src
);
974 result
= new(ctx
) ir_expression(ir_unop_i642u64
,
975 new(ctx
) ir_expression(ir_unop_b2i64
,
978 case GLSL_TYPE_FLOAT
:
979 result
= new(ctx
) ir_expression(ir_unop_f2u64
, src
);
981 case GLSL_TYPE_DOUBLE
:
982 result
= new(ctx
) ir_expression(ir_unop_d2u64
, src
);
984 case GLSL_TYPE_INT64
:
985 result
= new(ctx
) ir_expression(ir_unop_i642u64
, src
);
989 case GLSL_TYPE_INT64
:
992 result
= new(ctx
) ir_expression(ir_unop_i2i64
, src
);
995 result
= new(ctx
) ir_expression(ir_unop_u2i64
, src
);
998 result
= new(ctx
) ir_expression(ir_unop_b2i64
, src
);
1000 case GLSL_TYPE_FLOAT
:
1001 result
= new(ctx
) ir_expression(ir_unop_f2i64
, src
);
1003 case GLSL_TYPE_DOUBLE
:
1004 result
= new(ctx
) ir_expression(ir_unop_d2i64
, src
);
1006 case GLSL_TYPE_UINT64
:
1007 result
= new(ctx
) ir_expression(ir_unop_u642i64
, src
);
1011 case GLSL_TYPE_SAMPLER
:
1013 case GLSL_TYPE_UINT
:
1015 ir_expression(ir_unop_pack_sampler_2x32
, desired_type
, src
);
1019 case GLSL_TYPE_IMAGE
:
1021 case GLSL_TYPE_UINT
:
1023 ir_expression(ir_unop_pack_image_2x32
, desired_type
, src
);
1029 assert(result
!= NULL
);
1030 assert(result
->type
== desired_type
);
1032 /* Try constant folding; it may fold in the conversion we just added. */
1033 ir_constant
*const constant
= result
->constant_expression_value(ctx
);
1034 return (constant
!= NULL
) ? (ir_rvalue
*) constant
: (ir_rvalue
*) result
;
1039 * Perform automatic type and constant conversion of constructor parameters
1041 * This implements the rules in the "Implicit Conversions" rules, not the
1042 * "Conversion and Scalar Constructors".
1044 * After attempting the implicit conversion, an attempt to convert into a
1045 * constant valued expression is also done.
1047 * The \c from \c ir_rvalue is converted "in place".
1049 * \param from Operand that is being converted
1050 * \param to Base type the operand will be converted to
1051 * \param state GLSL compiler state
1054 * If the attempt to convert into a constant expression succeeds, \c true is
1055 * returned. Otherwise \c false is returned.
1058 implicitly_convert_component(ir_rvalue
* &from
, const glsl_base_type to
,
1059 struct _mesa_glsl_parse_state
*state
)
1061 void *mem_ctx
= state
;
1062 ir_rvalue
*result
= from
;
1064 if (to
!= from
->type
->base_type
) {
1065 const glsl_type
*desired_type
=
1066 glsl_type::get_instance(to
,
1067 from
->type
->vector_elements
,
1068 from
->type
->matrix_columns
);
1070 if (from
->type
->can_implicitly_convert_to(desired_type
, state
)) {
1071 /* Even though convert_component() implements the constructor
1072 * conversion rules (not the implicit conversion rules), its safe
1073 * to use it here because we already checked that the implicit
1074 * conversion is legal.
1076 result
= convert_component(from
, desired_type
);
1080 ir_rvalue
*const constant
= result
->constant_expression_value(mem_ctx
);
1082 if (constant
!= NULL
)
1085 if (from
!= result
) {
1086 from
->replace_with(result
);
1090 return constant
!= NULL
;
1095 * Dereference a specific component from a scalar, vector, or matrix
1098 dereference_component(ir_rvalue
*src
, unsigned component
)
1100 void *ctx
= ralloc_parent(src
);
1101 assert(component
< src
->type
->components());
1103 /* If the source is a constant, just create a new constant instead of a
1104 * dereference of the existing constant.
1106 ir_constant
*constant
= src
->as_constant();
1108 return new(ctx
) ir_constant(constant
, component
);
1110 if (src
->type
->is_scalar()) {
1112 } else if (src
->type
->is_vector()) {
1113 return new(ctx
) ir_swizzle(src
, component
, 0, 0, 0, 1);
1115 assert(src
->type
->is_matrix());
1117 /* Dereference a row of the matrix, then call this function again to get
1118 * a specific element from that row.
1120 const int c
= component
/ src
->type
->column_type()->vector_elements
;
1121 const int r
= component
% src
->type
->column_type()->vector_elements
;
1122 ir_constant
*const col_index
= new(ctx
) ir_constant(c
);
1123 ir_dereference
*const col
= new(ctx
) ir_dereference_array(src
,
1126 col
->type
= src
->type
->column_type();
1128 return dereference_component(col
, r
);
1131 assert(!"Should not get here.");
1137 process_vec_mat_constructor(exec_list
*instructions
,
1138 const glsl_type
*constructor_type
,
1139 YYLTYPE
*loc
, exec_list
*parameters
,
1140 struct _mesa_glsl_parse_state
*state
)
1144 /* The ARB_shading_language_420pack spec says:
1146 * "If an initializer is a list of initializers enclosed in curly braces,
1147 * the variable being declared must be a vector, a matrix, an array, or a
1150 * int i = { 1 }; // illegal, i is not an aggregate"
1152 if (constructor_type
->vector_elements
<= 1) {
1153 _mesa_glsl_error(loc
, state
, "aggregates can only initialize vectors, "
1154 "matrices, arrays, and structs");
1155 return ir_rvalue::error_value(ctx
);
1158 exec_list actual_parameters
;
1159 const unsigned parameter_count
=
1160 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
1162 if (parameter_count
== 0
1163 || (constructor_type
->is_vector() &&
1164 constructor_type
->vector_elements
!= parameter_count
)
1165 || (constructor_type
->is_matrix() &&
1166 constructor_type
->matrix_columns
!= parameter_count
)) {
1167 _mesa_glsl_error(loc
, state
, "%s constructor must have %u parameters",
1168 constructor_type
->is_vector() ? "vector" : "matrix",
1169 constructor_type
->vector_elements
);
1170 return ir_rvalue::error_value(ctx
);
1173 bool all_parameters_are_constant
= true;
1175 /* Type cast each parameter and, if possible, fold constants. */
1176 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1177 /* Apply implicit conversions (not the scalar constructor rules, see the
1178 * spec quote above!) and attempt to convert the parameter to a constant
1179 * valued expression. After doing so, track whether or not all the
1180 * parameters to the constructor are trivially constant valued
1183 all_parameters_are_constant
&=
1184 implicitly_convert_component(ir
, constructor_type
->base_type
, state
);
1186 if (constructor_type
->is_matrix()) {
1187 if (ir
->type
!= constructor_type
->column_type()) {
1188 _mesa_glsl_error(loc
, state
, "type error in matrix constructor: "
1189 "expected: %s, found %s",
1190 constructor_type
->column_type()->name
,
1192 return ir_rvalue::error_value(ctx
);
1194 } else if (ir
->type
!= constructor_type
->get_scalar_type()) {
1195 _mesa_glsl_error(loc
, state
, "type error in vector constructor: "
1196 "expected: %s, found %s",
1197 constructor_type
->get_scalar_type()->name
,
1199 return ir_rvalue::error_value(ctx
);
1203 if (all_parameters_are_constant
)
1204 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1206 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "vec_mat_ctor",
1208 instructions
->push_tail(var
);
1212 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
1213 ir_instruction
*assignment
= NULL
;
1215 if (var
->type
->is_matrix()) {
1217 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
1218 assignment
= new(ctx
) ir_assignment(lhs
, rhs
);
1220 /* use writemask rather than index for vector */
1221 assert(var
->type
->is_vector());
1223 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1224 assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
,
1225 (unsigned)(1 << i
));
1228 instructions
->push_tail(assignment
);
1233 return new(ctx
) ir_dereference_variable(var
);
1238 process_array_constructor(exec_list
*instructions
,
1239 const glsl_type
*constructor_type
,
1240 YYLTYPE
*loc
, exec_list
*parameters
,
1241 struct _mesa_glsl_parse_state
*state
)
1244 /* Array constructors come in two forms: sized and unsized. Sized array
1245 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
1246 * variables. In this case the number of parameters must exactly match the
1247 * specified size of the array.
1249 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
1250 * are vec4 variables. In this case the size of the array being constructed
1251 * is determined by the number of parameters.
1253 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
1255 * "There must be exactly the same number of arguments as the size of
1256 * the array being constructed. If no size is present in the
1257 * constructor, then the array is explicitly sized to the number of
1258 * arguments provided. The arguments are assigned in order, starting at
1259 * element 0, to the elements of the constructed array. Each argument
1260 * must be the same type as the element type of the array, or be a type
1261 * that can be converted to the element type of the array according to
1262 * Section 4.1.10 "Implicit Conversions.""
1264 exec_list actual_parameters
;
1265 const unsigned parameter_count
=
1266 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
1267 bool is_unsized_array
= constructor_type
->is_unsized_array();
1269 if ((parameter_count
== 0) ||
1270 (!is_unsized_array
&& (constructor_type
->length
!= parameter_count
))) {
1271 const unsigned min_param
= is_unsized_array
1272 ? 1 : constructor_type
->length
;
1274 _mesa_glsl_error(loc
, state
, "array constructor must have %s %u "
1276 is_unsized_array
? "at least" : "exactly",
1277 min_param
, (min_param
<= 1) ? "" : "s");
1278 return ir_rvalue::error_value(ctx
);
1281 if (is_unsized_array
) {
1283 glsl_type::get_array_instance(constructor_type
->fields
.array
,
1285 assert(constructor_type
!= NULL
);
1286 assert(constructor_type
->length
== parameter_count
);
1289 bool all_parameters_are_constant
= true;
1290 const glsl_type
*element_type
= constructor_type
->fields
.array
;
1292 /* Type cast each parameter and, if possible, fold constants. */
1293 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1294 /* Apply implicit conversions (not the scalar constructor rules, see the
1295 * spec quote above!) and attempt to convert the parameter to a constant
1296 * valued expression. After doing so, track whether or not all the
1297 * parameters to the constructor are trivially constant valued
1300 all_parameters_are_constant
&=
1301 implicitly_convert_component(ir
, element_type
->base_type
, state
);
1303 if (constructor_type
->fields
.array
->is_unsized_array()) {
1304 /* As the inner parameters of the constructor are created without
1305 * knowledge of each other we need to check to make sure unsized
1306 * parameters of unsized constructors all end up with the same size.
1308 * e.g we make sure to fail for a constructor like this:
1309 * vec4[][] a = vec4[][](vec4[](vec4(0.0), vec4(1.0)),
1310 * vec4[](vec4(0.0), vec4(1.0), vec4(1.0)),
1311 * vec4[](vec4(0.0), vec4(1.0)));
1313 if (element_type
->is_unsized_array()) {
1314 /* This is the first parameter so just get the type */
1315 element_type
= ir
->type
;
1316 } else if (element_type
!= ir
->type
) {
1317 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
1318 "expected: %s, found %s",
1321 return ir_rvalue::error_value(ctx
);
1323 } else if (ir
->type
!= constructor_type
->fields
.array
) {
1324 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
1325 "expected: %s, found %s",
1326 constructor_type
->fields
.array
->name
,
1328 return ir_rvalue::error_value(ctx
);
1330 element_type
= ir
->type
;
1334 if (constructor_type
->fields
.array
->is_unsized_array()) {
1336 glsl_type::get_array_instance(element_type
,
1338 assert(constructor_type
!= NULL
);
1339 assert(constructor_type
->length
== parameter_count
);
1342 if (all_parameters_are_constant
)
1343 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1345 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "array_ctor",
1347 instructions
->push_tail(var
);
1350 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
1351 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
1352 new(ctx
) ir_constant(i
));
1354 ir_instruction
*assignment
= new(ctx
) ir_assignment(lhs
, rhs
);
1355 instructions
->push_tail(assignment
);
1360 return new(ctx
) ir_dereference_variable(var
);
1365 * Determine if a list consists of a single scalar r-value
1368 single_scalar_parameter(exec_list
*parameters
)
1370 const ir_rvalue
*const p
= (ir_rvalue
*) parameters
->get_head_raw();
1371 assert(((ir_rvalue
*)p
)->as_rvalue() != NULL
);
1373 return (p
->type
->is_scalar() && p
->next
->is_tail_sentinel());
1378 * Generate inline code for a vector constructor
1380 * The generated constructor code will consist of a temporary variable
1381 * declaration of the same type as the constructor. A sequence of assignments
1382 * from constructor parameters to the temporary will follow.
1385 * An \c ir_dereference_variable of the temprorary generated in the constructor
1389 emit_inline_vector_constructor(const glsl_type
*type
,
1390 exec_list
*instructions
,
1391 exec_list
*parameters
,
1394 assert(!parameters
->is_empty());
1396 ir_variable
*var
= new(ctx
) ir_variable(type
, "vec_ctor", ir_var_temporary
);
1397 instructions
->push_tail(var
);
1399 /* There are three kinds of vector constructors.
1401 * - Construct a vector from a single scalar by replicating that scalar to
1402 * all components of the vector.
1404 * - Construct a vector from at least a matrix. This case should already
1405 * have been taken care of in ast_function_expression::hir by breaking
1406 * down the matrix into a series of column vectors.
1408 * - Construct a vector from an arbirary combination of vectors and
1409 * scalars. The components of the constructor parameters are assigned
1410 * to the vector in order until the vector is full.
1412 const unsigned lhs_components
= type
->components();
1413 if (single_scalar_parameter(parameters
)) {
1414 ir_rvalue
*first_param
= (ir_rvalue
*)parameters
->get_head_raw();
1415 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(first_param
, 0, 0, 0, 0,
1417 ir_dereference_variable
*lhs
= new(ctx
) ir_dereference_variable(var
);
1418 const unsigned mask
= (1U << lhs_components
) - 1;
1420 assert(rhs
->type
== lhs
->type
);
1422 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
, mask
);
1423 instructions
->push_tail(inst
);
1425 unsigned base_component
= 0;
1426 unsigned base_lhs_component
= 0;
1427 ir_constant_data data
;
1428 unsigned constant_mask
= 0, constant_components
= 0;
1430 memset(&data
, 0, sizeof(data
));
1432 foreach_in_list(ir_rvalue
, param
, parameters
) {
1433 unsigned rhs_components
= param
->type
->components();
1435 /* Do not try to assign more components to the vector than it has! */
1436 if ((rhs_components
+ base_lhs_component
) > lhs_components
) {
1437 rhs_components
= lhs_components
- base_lhs_component
;
1440 const ir_constant
*const c
= param
->as_constant();
1442 for (unsigned i
= 0; i
< rhs_components
; i
++) {
1443 switch (c
->type
->base_type
) {
1444 case GLSL_TYPE_UINT
:
1445 data
.u
[i
+ base_component
] = c
->get_uint_component(i
);
1448 data
.i
[i
+ base_component
] = c
->get_int_component(i
);
1450 case GLSL_TYPE_FLOAT
:
1451 data
.f
[i
+ base_component
] = c
->get_float_component(i
);
1453 case GLSL_TYPE_DOUBLE
:
1454 data
.d
[i
+ base_component
] = c
->get_double_component(i
);
1456 case GLSL_TYPE_BOOL
:
1457 data
.b
[i
+ base_component
] = c
->get_bool_component(i
);
1459 case GLSL_TYPE_UINT64
:
1460 data
.u64
[i
+ base_component
] = c
->get_uint64_component(i
);
1462 case GLSL_TYPE_INT64
:
1463 data
.i64
[i
+ base_component
] = c
->get_int64_component(i
);
1466 assert(!"Should not get here.");
1471 /* Mask of fields to be written in the assignment. */
1472 constant_mask
|= ((1U << rhs_components
) - 1) << base_lhs_component
;
1473 constant_components
+= rhs_components
;
1475 base_component
+= rhs_components
;
1477 /* Advance the component index by the number of components
1478 * that were just assigned.
1480 base_lhs_component
+= rhs_components
;
1483 if (constant_mask
!= 0) {
1484 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1485 const glsl_type
*rhs_type
=
1486 glsl_type::get_instance(var
->type
->base_type
,
1487 constant_components
,
1489 ir_rvalue
*rhs
= new(ctx
) ir_constant(rhs_type
, &data
);
1491 ir_instruction
*inst
=
1492 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, constant_mask
);
1493 instructions
->push_tail(inst
);
1497 foreach_in_list(ir_rvalue
, param
, parameters
) {
1498 unsigned rhs_components
= param
->type
->components();
1500 /* Do not try to assign more components to the vector than it has! */
1501 if ((rhs_components
+ base_component
) > lhs_components
) {
1502 rhs_components
= lhs_components
- base_component
;
1505 /* If we do not have any components left to copy, break out of the
1506 * loop. This can happen when initializing a vec4 with a mat3 as the
1507 * mat3 would have been broken into a series of column vectors.
1509 if (rhs_components
== 0) {
1513 const ir_constant
*const c
= param
->as_constant();
1515 /* Mask of fields to be written in the assignment. */
1516 const unsigned write_mask
= ((1U << rhs_components
) - 1)
1519 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1521 /* Generate a swizzle so that LHS and RHS sizes match. */
1523 new(ctx
) ir_swizzle(param
, 0, 1, 2, 3, rhs_components
);
1525 ir_instruction
*inst
=
1526 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1527 instructions
->push_tail(inst
);
1530 /* Advance the component index by the number of components that were
1533 base_component
+= rhs_components
;
1536 return new(ctx
) ir_dereference_variable(var
);
1541 * Generate assignment of a portion of a vector to a portion of a matrix column
1543 * \param src_base First component of the source to be used in assignment
1544 * \param column Column of destination to be assiged
1545 * \param row_base First component of the destination column to be assigned
1546 * \param count Number of components to be assigned
1549 * \c src_base + \c count must be less than or equal to the number of
1550 * components in the source vector.
1552 static ir_instruction
*
1553 assign_to_matrix_column(ir_variable
*var
, unsigned column
, unsigned row_base
,
1554 ir_rvalue
*src
, unsigned src_base
, unsigned count
,
1557 ir_constant
*col_idx
= new(mem_ctx
) ir_constant(column
);
1558 ir_dereference
*column_ref
= new(mem_ctx
) ir_dereference_array(var
,
1561 assert(column_ref
->type
->components() >= (row_base
+ count
));
1562 assert(src
->type
->components() >= (src_base
+ count
));
1564 /* Generate a swizzle that extracts the number of components from the source
1565 * that are to be assigned to the column of the matrix.
1567 if (count
< src
->type
->vector_elements
) {
1568 src
= new(mem_ctx
) ir_swizzle(src
,
1569 src_base
+ 0, src_base
+ 1,
1570 src_base
+ 2, src_base
+ 3,
1574 /* Mask of fields to be written in the assignment. */
1575 const unsigned write_mask
= ((1U << count
) - 1) << row_base
;
1577 return new(mem_ctx
) ir_assignment(column_ref
, src
, NULL
, write_mask
);
1582 * Generate inline code for a matrix constructor
1584 * The generated constructor code will consist of a temporary variable
1585 * declaration of the same type as the constructor. A sequence of assignments
1586 * from constructor parameters to the temporary will follow.
1589 * An \c ir_dereference_variable of the temprorary generated in the constructor
1593 emit_inline_matrix_constructor(const glsl_type
*type
,
1594 exec_list
*instructions
,
1595 exec_list
*parameters
,
1598 assert(!parameters
->is_empty());
1600 ir_variable
*var
= new(ctx
) ir_variable(type
, "mat_ctor", ir_var_temporary
);
1601 instructions
->push_tail(var
);
1603 /* There are three kinds of matrix constructors.
1605 * - Construct a matrix from a single scalar by replicating that scalar to
1606 * along the diagonal of the matrix and setting all other components to
1609 * - Construct a matrix from an arbirary combination of vectors and
1610 * scalars. The components of the constructor parameters are assigned
1611 * to the matrix in column-major order until the matrix is full.
1613 * - Construct a matrix from a single matrix. The source matrix is copied
1614 * to the upper left portion of the constructed matrix, and the remaining
1615 * elements take values from the identity matrix.
1617 ir_rvalue
*const first_param
= (ir_rvalue
*) parameters
->get_head_raw();
1618 if (single_scalar_parameter(parameters
)) {
1619 /* Assign the scalar to the X component of a vec4, and fill the remaining
1620 * components with zero.
1622 glsl_base_type param_base_type
= first_param
->type
->base_type
;
1623 assert(first_param
->type
->is_float() || first_param
->type
->is_double());
1624 ir_variable
*rhs_var
=
1625 new(ctx
) ir_variable(glsl_type::get_instance(param_base_type
, 4, 1),
1628 instructions
->push_tail(rhs_var
);
1630 ir_constant_data zero
;
1631 for (unsigned i
= 0; i
< 4; i
++)
1632 if (first_param
->type
->is_float())
1637 ir_instruction
*inst
=
1638 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(rhs_var
),
1639 new(ctx
) ir_constant(rhs_var
->type
, &zero
));
1640 instructions
->push_tail(inst
);
1642 ir_dereference
*const rhs_ref
=
1643 new(ctx
) ir_dereference_variable(rhs_var
);
1645 inst
= new(ctx
) ir_assignment(rhs_ref
, first_param
, NULL
, 0x01);
1646 instructions
->push_tail(inst
);
1648 /* Assign the temporary vector to each column of the destination matrix
1649 * with a swizzle that puts the X component on the diagonal of the
1650 * matrix. In some cases this may mean that the X component does not
1651 * get assigned into the column at all (i.e., when the matrix has more
1652 * columns than rows).
1654 static const unsigned rhs_swiz
[4][4] = {
1661 const unsigned cols_to_init
= MIN2(type
->matrix_columns
,
1662 type
->vector_elements
);
1663 for (unsigned i
= 0; i
< cols_to_init
; i
++) {
1664 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1665 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
,
1668 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1669 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, rhs_swiz
[i
],
1670 type
->vector_elements
);
1672 inst
= new(ctx
) ir_assignment(col_ref
, rhs
);
1673 instructions
->push_tail(inst
);
1676 for (unsigned i
= cols_to_init
; i
< type
->matrix_columns
; i
++) {
1677 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1678 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
,
1681 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1682 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, 1, 1, 1, 1,
1683 type
->vector_elements
);
1685 inst
= new(ctx
) ir_assignment(col_ref
, rhs
);
1686 instructions
->push_tail(inst
);
1688 } else if (first_param
->type
->is_matrix()) {
1689 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
1691 * "If a matrix is constructed from a matrix, then each component
1692 * (column i, row j) in the result that has a corresponding
1693 * component (column i, row j) in the argument will be initialized
1694 * from there. All other components will be initialized to the
1695 * identity matrix. If a matrix argument is given to a matrix
1696 * constructor, it is an error to have any other arguments."
1698 assert(first_param
->next
->is_tail_sentinel());
1699 ir_rvalue
*const src_matrix
= first_param
;
1701 /* If the source matrix is smaller, pre-initialize the relavent parts of
1702 * the destination matrix to the identity matrix.
1704 if ((src_matrix
->type
->matrix_columns
< var
->type
->matrix_columns
) ||
1705 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)) {
1707 /* If the source matrix has fewer rows, every column of the
1708 * destination must be initialized. Otherwise only the columns in
1709 * the destination that do not exist in the source must be
1713 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)
1714 ? 0 : src_matrix
->type
->matrix_columns
;
1716 const glsl_type
*const col_type
= var
->type
->column_type();
1717 for (/* empty */; col
< var
->type
->matrix_columns
; col
++) {
1718 ir_constant_data ident
;
1720 if (!col_type
->is_double()) {
1725 ident
.f
[col
] = 1.0f
;
1734 ir_rvalue
*const rhs
= new(ctx
) ir_constant(col_type
, &ident
);
1736 ir_rvalue
*const lhs
=
1737 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(col
));
1739 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
);
1740 instructions
->push_tail(inst
);
1744 /* Assign columns from the source matrix to the destination matrix.
1746 * Since the parameter will be used in the RHS of multiple assignments,
1747 * generate a temporary and copy the paramter there.
1749 ir_variable
*const rhs_var
=
1750 new(ctx
) ir_variable(first_param
->type
, "mat_ctor_mat",
1752 instructions
->push_tail(rhs_var
);
1754 ir_dereference
*const rhs_var_ref
=
1755 new(ctx
) ir_dereference_variable(rhs_var
);
1756 ir_instruction
*const inst
=
1757 new(ctx
) ir_assignment(rhs_var_ref
, first_param
);
1758 instructions
->push_tail(inst
);
1760 const unsigned last_row
= MIN2(src_matrix
->type
->vector_elements
,
1761 var
->type
->vector_elements
);
1762 const unsigned last_col
= MIN2(src_matrix
->type
->matrix_columns
,
1763 var
->type
->matrix_columns
);
1765 unsigned swiz
[4] = { 0, 0, 0, 0 };
1766 for (unsigned i
= 1; i
< last_row
; i
++)
1769 const unsigned write_mask
= (1U << last_row
) - 1;
1771 for (unsigned i
= 0; i
< last_col
; i
++) {
1772 ir_dereference
*const lhs
=
1773 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
1774 ir_rvalue
*const rhs_col
=
1775 new(ctx
) ir_dereference_array(rhs_var
, new(ctx
) ir_constant(i
));
1777 /* If one matrix has columns that are smaller than the columns of the
1778 * other matrix, wrap the column access of the larger with a swizzle
1779 * so that the LHS and RHS of the assignment have the same size (and
1780 * therefore have the same type).
1782 * It would be perfectly valid to unconditionally generate the
1783 * swizzles, this this will typically result in a more compact IR
1787 if (lhs
->type
->vector_elements
!= rhs_col
->type
->vector_elements
) {
1788 rhs
= new(ctx
) ir_swizzle(rhs_col
, swiz
, last_row
);
1793 ir_instruction
*inst
=
1794 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1795 instructions
->push_tail(inst
);
1798 const unsigned cols
= type
->matrix_columns
;
1799 const unsigned rows
= type
->vector_elements
;
1800 unsigned remaining_slots
= rows
* cols
;
1801 unsigned col_idx
= 0;
1802 unsigned row_idx
= 0;
1804 foreach_in_list(ir_rvalue
, rhs
, parameters
) {
1805 unsigned rhs_components
= rhs
->type
->components();
1806 unsigned rhs_base
= 0;
1808 if (remaining_slots
== 0)
1811 /* Since the parameter might be used in the RHS of two assignments,
1812 * generate a temporary and copy the paramter there.
1814 ir_variable
*rhs_var
=
1815 new(ctx
) ir_variable(rhs
->type
, "mat_ctor_vec", ir_var_temporary
);
1816 instructions
->push_tail(rhs_var
);
1818 ir_dereference
*rhs_var_ref
=
1819 new(ctx
) ir_dereference_variable(rhs_var
);
1820 ir_instruction
*inst
= new(ctx
) ir_assignment(rhs_var_ref
, rhs
);
1821 instructions
->push_tail(inst
);
1824 /* Assign the current parameter to as many components of the matrix
1827 * NOTE: A single vector parameter can span two matrix columns. A
1828 * single vec4, for example, can completely fill a mat2.
1830 unsigned count
= MIN2(rows
- row_idx
,
1831 rhs_components
- rhs_base
);
1833 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1834 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
1839 instructions
->push_tail(inst
);
1842 remaining_slots
-= count
;
1844 /* Sometimes, there is still data left in the parameters and
1845 * components left to be set in the destination but in other
1848 if (row_idx
>= rows
) {
1852 } while(remaining_slots
> 0 && rhs_base
< rhs_components
);
1856 return new(ctx
) ir_dereference_variable(var
);
1861 emit_inline_record_constructor(const glsl_type
*type
,
1862 exec_list
*instructions
,
1863 exec_list
*parameters
,
1866 ir_variable
*const var
=
1867 new(mem_ctx
) ir_variable(type
, "record_ctor", ir_var_temporary
);
1868 ir_dereference_variable
*const d
=
1869 new(mem_ctx
) ir_dereference_variable(var
);
1871 instructions
->push_tail(var
);
1873 exec_node
*node
= parameters
->get_head_raw();
1874 for (unsigned i
= 0; i
< type
->length
; i
++) {
1875 assert(!node
->is_tail_sentinel());
1877 ir_dereference
*const lhs
=
1878 new(mem_ctx
) ir_dereference_record(d
->clone(mem_ctx
, NULL
),
1879 type
->fields
.structure
[i
].name
);
1881 ir_rvalue
*const rhs
= ((ir_instruction
*) node
)->as_rvalue();
1882 assert(rhs
!= NULL
);
1884 ir_instruction
*const assign
= new(mem_ctx
) ir_assignment(lhs
, rhs
);
1886 instructions
->push_tail(assign
);
1895 process_record_constructor(exec_list
*instructions
,
1896 const glsl_type
*constructor_type
,
1897 YYLTYPE
*loc
, exec_list
*parameters
,
1898 struct _mesa_glsl_parse_state
*state
)
1901 /* From page 32 (page 38 of the PDF) of the GLSL 1.20 spec:
1903 * "The arguments to the constructor will be used to set the structure's
1904 * fields, in order, using one argument per field. Each argument must
1905 * be the same type as the field it sets, or be a type that can be
1906 * converted to the field's type according to Section 4.1.10 “Implicit
1909 * From page 35 (page 41 of the PDF) of the GLSL 4.20 spec:
1911 * "In all cases, the innermost initializer (i.e., not a list of
1912 * initializers enclosed in curly braces) applied to an object must
1913 * have the same type as the object being initialized or be a type that
1914 * can be converted to the object's type according to section 4.1.10
1915 * "Implicit Conversions". In the latter case, an implicit conversion
1916 * will be done on the initializer before the assignment is done."
1918 exec_list actual_parameters
;
1920 const unsigned parameter_count
=
1921 process_parameters(instructions
, &actual_parameters
, parameters
,
1924 if (parameter_count
!= constructor_type
->length
) {
1925 _mesa_glsl_error(loc
, state
,
1926 "%s parameters in constructor for `%s'",
1927 parameter_count
> constructor_type
->length
1928 ? "too many": "insufficient",
1929 constructor_type
->name
);
1930 return ir_rvalue::error_value(ctx
);
1933 bool all_parameters_are_constant
= true;
1936 /* Type cast each parameter and, if possible, fold constants. */
1937 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1939 const glsl_struct_field
*struct_field
=
1940 &constructor_type
->fields
.structure
[i
];
1942 /* Apply implicit conversions (not the scalar constructor rules, see the
1943 * spec quote above!) and attempt to convert the parameter to a constant
1944 * valued expression. After doing so, track whether or not all the
1945 * parameters to the constructor are trivially constant valued
1948 all_parameters_are_constant
&=
1949 implicitly_convert_component(ir
, struct_field
->type
->base_type
,
1952 if (ir
->type
!= struct_field
->type
) {
1953 _mesa_glsl_error(loc
, state
,
1954 "parameter type mismatch in constructor for `%s.%s' "
1956 constructor_type
->name
,
1959 struct_field
->type
->name
);
1960 return ir_rvalue::error_value(ctx
);
1966 if (all_parameters_are_constant
) {
1967 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1969 return emit_inline_record_constructor(constructor_type
, instructions
,
1970 &actual_parameters
, state
);
1975 ast_function_expression::handle_method(exec_list
*instructions
,
1976 struct _mesa_glsl_parse_state
*state
)
1978 const ast_expression
*field
= subexpressions
[0];
1982 /* Handle "method calls" in GLSL 1.20 - namely, array.length() */
1983 YYLTYPE loc
= get_location();
1984 state
->check_version(120, 300, &loc
, "methods not supported");
1987 method
= field
->primary_expression
.identifier
;
1989 /* This would prevent to raise "uninitialized variable" warnings when
1990 * calling array.length.
1992 field
->subexpressions
[0]->set_is_lhs(true);
1993 op
= field
->subexpressions
[0]->hir(instructions
, state
);
1994 if (strcmp(method
, "length") == 0) {
1995 if (!this->expressions
.is_empty()) {
1996 _mesa_glsl_error(&loc
, state
, "length method takes no arguments");
2000 if (op
->type
->is_array()) {
2001 if (op
->type
->is_unsized_array()) {
2002 if (!state
->has_shader_storage_buffer_objects()) {
2003 _mesa_glsl_error(&loc
, state
,
2004 "length called on unsized array"
2005 " only available with"
2006 " ARB_shader_storage_buffer_object");
2008 /* Calculate length of an unsized array in run-time */
2009 result
= new(ctx
) ir_expression(ir_unop_ssbo_unsized_array_length
,
2012 result
= new(ctx
) ir_constant(op
->type
->array_size());
2014 } else if (op
->type
->is_vector()) {
2015 if (state
->has_420pack()) {
2016 /* .length() returns int. */
2017 result
= new(ctx
) ir_constant((int) op
->type
->vector_elements
);
2019 _mesa_glsl_error(&loc
, state
, "length method on matrix only"
2020 " available with ARB_shading_language_420pack");
2023 } else if (op
->type
->is_matrix()) {
2024 if (state
->has_420pack()) {
2025 /* .length() returns int. */
2026 result
= new(ctx
) ir_constant((int) op
->type
->matrix_columns
);
2028 _mesa_glsl_error(&loc
, state
, "length method on matrix only"
2029 " available with ARB_shading_language_420pack");
2033 _mesa_glsl_error(&loc
, state
, "length called on scalar.");
2037 _mesa_glsl_error(&loc
, state
, "unknown method: `%s'", method
);
2042 return ir_rvalue::error_value(ctx
);
2045 static inline bool is_valid_constructor(const glsl_type
*type
,
2046 struct _mesa_glsl_parse_state
*state
)
2048 return type
->is_numeric() || type
->is_boolean() ||
2049 (state
->has_bindless() && (type
->is_sampler() || type
->is_image()));
2053 ast_function_expression::hir(exec_list
*instructions
,
2054 struct _mesa_glsl_parse_state
*state
)
2057 /* There are three sorts of function calls.
2059 * 1. constructors - The first subexpression is an ast_type_specifier.
2060 * 2. methods - Only the .length() method of array types.
2061 * 3. functions - Calls to regular old functions.
2064 if (is_constructor()) {
2065 const ast_type_specifier
*type
=
2066 (ast_type_specifier
*) subexpressions
[0];
2067 YYLTYPE loc
= type
->get_location();
2070 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
2072 /* constructor_type can be NULL if a variable with the same name as the
2073 * structure has come into scope.
2075 if (constructor_type
== NULL
) {
2076 _mesa_glsl_error(& loc
, state
, "unknown type `%s' (structure name "
2077 "may be shadowed by a variable with the same name)",
2079 return ir_rvalue::error_value(ctx
);
2083 /* Constructors for opaque types are illegal.
2085 * From section 4.1.7 of the ARB_bindless_texture spec:
2087 * "Samplers are represented using 64-bit integer handles, and may be "
2088 * converted to and from 64-bit integers using constructors."
2090 * From section 4.1.X of the ARB_bindless_texture spec:
2092 * "Images are represented using 64-bit integer handles, and may be
2093 * converted to and from 64-bit integers using constructors."
2095 if (constructor_type
->contains_atomic() ||
2096 (!state
->has_bindless() && constructor_type
->contains_opaque())) {
2097 _mesa_glsl_error(& loc
, state
, "cannot construct %s type `%s'",
2098 state
->has_bindless() ? "atomic" : "opaque",
2099 constructor_type
->name
);
2100 return ir_rvalue::error_value(ctx
);
2103 if (constructor_type
->is_subroutine()) {
2104 _mesa_glsl_error(& loc
, state
,
2105 "subroutine name cannot be a constructor `%s'",
2106 constructor_type
->name
);
2107 return ir_rvalue::error_value(ctx
);
2110 if (constructor_type
->is_array()) {
2111 if (!state
->check_version(120, 300, &loc
,
2112 "array constructors forbidden")) {
2113 return ir_rvalue::error_value(ctx
);
2116 return process_array_constructor(instructions
, constructor_type
,
2117 & loc
, &this->expressions
, state
);
2121 /* There are two kinds of constructor calls. Constructors for arrays and
2122 * structures must have the exact number of arguments with matching types
2123 * in the correct order. These constructors follow essentially the same
2124 * type matching rules as functions.
2126 * Constructors for built-in language types, such as mat4 and vec2, are
2127 * free form. The only requirements are that the parameters must provide
2128 * enough values of the correct scalar type and that no arguments are
2129 * given past the last used argument.
2131 * When using the C-style initializer syntax from GLSL 4.20, constructors
2132 * must have the exact number of arguments with matching types in the
2135 if (constructor_type
->is_struct()) {
2136 return process_record_constructor(instructions
, constructor_type
,
2137 &loc
, &this->expressions
,
2141 if (!is_valid_constructor(constructor_type
, state
))
2142 return ir_rvalue::error_value(ctx
);
2144 /* Total number of components of the type being constructed. */
2145 const unsigned type_components
= constructor_type
->components();
2147 /* Number of components from parameters that have actually been
2148 * consumed. This is used to perform several kinds of error checking.
2150 unsigned components_used
= 0;
2152 unsigned matrix_parameters
= 0;
2153 unsigned nonmatrix_parameters
= 0;
2154 exec_list actual_parameters
;
2156 foreach_list_typed(ast_node
, ast
, link
, &this->expressions
) {
2157 ir_rvalue
*result
= ast
->hir(instructions
, state
);
2159 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
2161 * "It is an error to provide extra arguments beyond this
2162 * last used argument."
2164 if (components_used
>= type_components
) {
2165 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
2167 constructor_type
->name
);
2168 return ir_rvalue::error_value(ctx
);
2171 if (!is_valid_constructor(result
->type
, state
)) {
2172 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
2173 "non-numeric data type",
2174 constructor_type
->name
);
2175 return ir_rvalue::error_value(ctx
);
2178 /* Count the number of matrix and nonmatrix parameters. This
2179 * is used below to enforce some of the constructor rules.
2181 if (result
->type
->is_matrix())
2182 matrix_parameters
++;
2184 nonmatrix_parameters
++;
2186 actual_parameters
.push_tail(result
);
2187 components_used
+= result
->type
->components();
2190 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
2192 * "It is an error to construct matrices from other matrices. This
2193 * is reserved for future use."
2195 if (matrix_parameters
> 0
2196 && constructor_type
->is_matrix()
2197 && !state
->check_version(120, 100, &loc
,
2198 "cannot construct `%s' from a matrix",
2199 constructor_type
->name
)) {
2200 return ir_rvalue::error_value(ctx
);
2203 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
2205 * "If a matrix argument is given to a matrix constructor, it is
2206 * an error to have any other arguments."
2208 if ((matrix_parameters
> 0)
2209 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
2210 && constructor_type
->is_matrix()) {
2211 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
2212 "matrix must be only parameter",
2213 constructor_type
->name
);
2214 return ir_rvalue::error_value(ctx
);
2217 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
2219 * "In these cases, there must be enough components provided in the
2220 * arguments to provide an initializer for every component in the
2221 * constructed value."
2223 if (components_used
< type_components
&& components_used
!= 1
2224 && matrix_parameters
== 0) {
2225 _mesa_glsl_error(& loc
, state
, "too few components to construct "
2227 constructor_type
->name
);
2228 return ir_rvalue::error_value(ctx
);
2231 /* Matrices can never be consumed as is by any constructor but matrix
2232 * constructors. If the constructor type is not matrix, always break the
2233 * matrix up into a series of column vectors.
2235 if (!constructor_type
->is_matrix()) {
2236 foreach_in_list_safe(ir_rvalue
, matrix
, &actual_parameters
) {
2237 if (!matrix
->type
->is_matrix())
2240 /* Create a temporary containing the matrix. */
2241 ir_variable
*var
= new(ctx
) ir_variable(matrix
->type
, "matrix_tmp",
2243 instructions
->push_tail(var
);
2244 instructions
->push_tail(
2245 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
2247 var
->constant_value
= matrix
->constant_expression_value(ctx
);
2249 /* Replace the matrix with dereferences of its columns. */
2250 for (int i
= 0; i
< matrix
->type
->matrix_columns
; i
++) {
2251 matrix
->insert_before(
2252 new (ctx
) ir_dereference_array(var
,
2253 new(ctx
) ir_constant(i
)));
2259 bool all_parameters_are_constant
= true;
2261 /* Type cast each parameter and, if possible, fold constants.*/
2262 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
2263 const glsl_type
*desired_type
;
2265 /* From section 5.4.1 of the ARB_bindless_texture spec:
2267 * "In the following four constructors, the low 32 bits of the sampler
2268 * type correspond to the .x component of the uvec2 and the high 32
2269 * bits correspond to the .y component."
2271 * uvec2(any sampler type) // Converts a sampler type to a
2272 * // pair of 32-bit unsigned integers
2273 * any sampler type(uvec2) // Converts a pair of 32-bit unsigned integers to
2275 * uvec2(any image type) // Converts an image type to a
2276 * // pair of 32-bit unsigned integers
2277 * any image type(uvec2) // Converts a pair of 32-bit unsigned integers to
2280 if (ir
->type
->is_sampler() || ir
->type
->is_image()) {
2281 /* Convert a sampler/image type to a pair of 32-bit unsigned
2282 * integers as defined by ARB_bindless_texture.
2284 if (constructor_type
!= glsl_type::uvec2_type
) {
2285 _mesa_glsl_error(&loc
, state
, "sampler and image types can only "
2286 "be converted to a pair of 32-bit unsigned "
2289 desired_type
= glsl_type::uvec2_type
;
2290 } else if (constructor_type
->is_sampler() ||
2291 constructor_type
->is_image()) {
2292 /* Convert a pair of 32-bit unsigned integers to a sampler or image
2293 * type as defined by ARB_bindless_texture.
2295 if (ir
->type
!= glsl_type::uvec2_type
) {
2296 _mesa_glsl_error(&loc
, state
, "sampler and image types can only "
2297 "be converted from a pair of 32-bit unsigned "
2300 desired_type
= constructor_type
;
2303 glsl_type::get_instance(constructor_type
->base_type
,
2304 ir
->type
->vector_elements
,
2305 ir
->type
->matrix_columns
);
2308 ir_rvalue
*result
= convert_component(ir
, desired_type
);
2310 /* Attempt to convert the parameter to a constant valued expression.
2311 * After doing so, track whether or not all the parameters to the
2312 * constructor are trivially constant valued expressions.
2314 ir_rvalue
*const constant
= result
->constant_expression_value(ctx
);
2316 if (constant
!= NULL
)
2319 all_parameters_are_constant
= false;
2322 ir
->replace_with(result
);
2326 /* If all of the parameters are trivially constant, create a
2327 * constant representing the complete collection of parameters.
2329 if (all_parameters_are_constant
) {
2330 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
2331 } else if (constructor_type
->is_scalar()) {
2332 return dereference_component((ir_rvalue
*)
2333 actual_parameters
.get_head_raw(),
2335 } else if (constructor_type
->is_vector()) {
2336 return emit_inline_vector_constructor(constructor_type
,
2341 assert(constructor_type
->is_matrix());
2342 return emit_inline_matrix_constructor(constructor_type
,
2347 } else if (subexpressions
[0]->oper
== ast_field_selection
) {
2348 return handle_method(instructions
, state
);
2350 const ast_expression
*id
= subexpressions
[0];
2351 const char *func_name
= NULL
;
2352 YYLTYPE loc
= get_location();
2353 exec_list actual_parameters
;
2354 ir_variable
*sub_var
= NULL
;
2355 ir_rvalue
*array_idx
= NULL
;
2357 process_parameters(instructions
, &actual_parameters
, &this->expressions
,
2360 if (id
->oper
== ast_array_index
) {
2361 array_idx
= generate_array_index(ctx
, instructions
, state
, loc
,
2362 id
->subexpressions
[0],
2363 id
->subexpressions
[1], &func_name
,
2364 &actual_parameters
);
2365 } else if (id
->oper
== ast_identifier
) {
2366 func_name
= id
->primary_expression
.identifier
;
2368 _mesa_glsl_error(&loc
, state
, "function name is not an identifier");
2371 /* an error was emitted earlier */
2373 return ir_rvalue::error_value(ctx
);
2375 ir_function_signature
*sig
=
2376 match_function_by_name(func_name
, &actual_parameters
, state
);
2378 ir_rvalue
*value
= NULL
;
2380 sig
= match_subroutine_by_name(func_name
, &actual_parameters
,
2385 no_matching_function_error(func_name
, &loc
,
2386 &actual_parameters
, state
);
2387 value
= ir_rvalue::error_value(ctx
);
2388 } else if (!verify_parameter_modes(state
, sig
,
2390 this->expressions
)) {
2391 /* an error has already been emitted */
2392 value
= ir_rvalue::error_value(ctx
);
2393 } else if (sig
->is_builtin() && strcmp(func_name
, "ftransform") == 0) {
2394 /* ftransform refers to global variables, and we don't have any code
2395 * for remapping the variable references in the built-in shader.
2398 state
->symbols
->get_variable("gl_ModelViewProjectionMatrix");
2399 ir_variable
*vtx
= state
->symbols
->get_variable("gl_Vertex");
2400 value
= new(ctx
) ir_expression(ir_binop_mul
, glsl_type::vec4_type
,
2401 new(ctx
) ir_dereference_variable(mvp
),
2402 new(ctx
) ir_dereference_variable(vtx
));
2404 bool is_begin_interlock
= false;
2405 bool is_end_interlock
= false;
2406 if (sig
->is_builtin() &&
2407 state
->stage
== MESA_SHADER_FRAGMENT
&&
2408 state
->ARB_fragment_shader_interlock_enable
) {
2409 is_begin_interlock
= strcmp(func_name
, "beginInvocationInterlockARB") == 0;
2410 is_end_interlock
= strcmp(func_name
, "endInvocationInterlockARB") == 0;
2413 if (sig
->is_builtin() &&
2414 ((state
->stage
== MESA_SHADER_TESS_CTRL
&&
2415 strcmp(func_name
, "barrier") == 0) ||
2416 is_begin_interlock
|| is_end_interlock
)) {
2417 if (state
->current_function
== NULL
||
2418 strcmp(state
->current_function
->function_name(), "main") != 0) {
2419 _mesa_glsl_error(&loc
, state
,
2420 "%s() may only be used in main()", func_name
);
2423 if (state
->found_return
) {
2424 _mesa_glsl_error(&loc
, state
,
2425 "%s() may not be used after return", func_name
);
2428 if (instructions
!= &state
->current_function
->body
) {
2429 _mesa_glsl_error(&loc
, state
,
2430 "%s() may not be used in control flow", func_name
);
2434 /* There can be only one begin/end interlock pair in the function. */
2435 if (is_begin_interlock
) {
2436 if (state
->found_begin_interlock
)
2437 _mesa_glsl_error(&loc
, state
,
2438 "beginInvocationInterlockARB may not be used twice");
2439 state
->found_begin_interlock
= true;
2440 } else if (is_end_interlock
) {
2441 if (!state
->found_begin_interlock
)
2442 _mesa_glsl_error(&loc
, state
,
2443 "endInvocationInterlockARB may not be used "
2444 "before beginInvocationInterlockARB");
2445 if (state
->found_end_interlock
)
2446 _mesa_glsl_error(&loc
, state
,
2447 "endInvocationInterlockARB may not be used twice");
2448 state
->found_end_interlock
= true;
2451 value
= generate_call(instructions
, sig
, &actual_parameters
, sub_var
,
2454 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::void_type
,
2457 instructions
->push_tail(tmp
);
2458 value
= new(ctx
) ir_dereference_variable(tmp
);
2465 unreachable("not reached");
2469 ast_function_expression::has_sequence_subexpression() const
2471 foreach_list_typed(const ast_node
, ast
, link
, &this->expressions
) {
2472 if (ast
->has_sequence_subexpression())
2480 ast_aggregate_initializer::hir(exec_list
*instructions
,
2481 struct _mesa_glsl_parse_state
*state
)
2484 YYLTYPE loc
= this->get_location();
2486 if (!this->constructor_type
) {
2487 _mesa_glsl_error(&loc
, state
, "type of C-style initializer unknown");
2488 return ir_rvalue::error_value(ctx
);
2490 const glsl_type
*const constructor_type
= this->constructor_type
;
2492 if (!state
->has_420pack()) {
2493 _mesa_glsl_error(&loc
, state
, "C-style initialization requires the "
2494 "GL_ARB_shading_language_420pack extension");
2495 return ir_rvalue::error_value(ctx
);
2498 if (constructor_type
->is_array()) {
2499 return process_array_constructor(instructions
, constructor_type
, &loc
,
2500 &this->expressions
, state
);
2503 if (constructor_type
->is_struct()) {
2504 return process_record_constructor(instructions
, constructor_type
, &loc
,
2505 &this->expressions
, state
);
2508 return process_vec_mat_constructor(instructions
, constructor_type
, &loc
,
2509 &this->expressions
, state
);