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 /* Error happened processing function parameter */
54 actual_parameters
->push_tail(ir_rvalue::error_value(mem_ctx
));
59 ir_constant
*const constant
=
60 result
->constant_expression_value(mem_ctx
);
65 actual_parameters
->push_tail(result
);
74 * Generate a source prototype for a function signature
76 * \param return_type Return type of the function. May be \c NULL.
77 * \param name Name of the function.
78 * \param parameters List of \c ir_instruction nodes representing the
79 * parameter list for the function. This may be either a
80 * formal (\c ir_variable) or actual (\c ir_rvalue)
81 * parameter list. Only the type is used.
84 * A ralloced string representing the prototype of the function.
87 prototype_string(const glsl_type
*return_type
, const char *name
,
88 exec_list
*parameters
)
92 if (return_type
!= NULL
)
93 str
= ralloc_asprintf(NULL
, "%s ", return_type
->name
);
95 ralloc_asprintf_append(&str
, "%s(", name
);
97 const char *comma
= "";
98 foreach_in_list(const ir_variable
, param
, parameters
) {
99 ralloc_asprintf_append(&str
, "%s%s", comma
, param
->type
->name
);
103 ralloc_strcat(&str
, ")");
108 verify_image_parameter(YYLTYPE
*loc
, _mesa_glsl_parse_state
*state
,
109 const ir_variable
*formal
, const ir_variable
*actual
)
112 * From the ARB_shader_image_load_store specification:
114 * "The values of image variables qualified with coherent,
115 * volatile, restrict, readonly, or writeonly may not be passed
116 * to functions whose formal parameters lack such
117 * qualifiers. [...] It is legal to have additional qualifiers
118 * on a formal parameter, but not to have fewer."
120 if (actual
->data
.memory_coherent
&& !formal
->data
.memory_coherent
) {
121 _mesa_glsl_error(loc
, state
,
122 "function call parameter `%s' drops "
123 "`coherent' qualifier", formal
->name
);
127 if (actual
->data
.memory_volatile
&& !formal
->data
.memory_volatile
) {
128 _mesa_glsl_error(loc
, state
,
129 "function call parameter `%s' drops "
130 "`volatile' qualifier", formal
->name
);
134 if (actual
->data
.memory_restrict
&& !formal
->data
.memory_restrict
) {
135 _mesa_glsl_error(loc
, state
,
136 "function call parameter `%s' drops "
137 "`restrict' qualifier", formal
->name
);
141 if (actual
->data
.memory_read_only
&& !formal
->data
.memory_read_only
) {
142 _mesa_glsl_error(loc
, state
,
143 "function call parameter `%s' drops "
144 "`readonly' qualifier", formal
->name
);
148 if (actual
->data
.memory_write_only
&& !formal
->data
.memory_write_only
) {
149 _mesa_glsl_error(loc
, state
,
150 "function call parameter `%s' drops "
151 "`writeonly' qualifier", formal
->name
);
159 verify_first_atomic_parameter(YYLTYPE
*loc
, _mesa_glsl_parse_state
*state
,
163 (!var
->is_in_shader_storage_block() &&
164 var
->data
.mode
!= ir_var_shader_shared
)) {
165 _mesa_glsl_error(loc
, state
, "First argument to atomic function "
166 "must be a buffer or shared variable");
173 is_atomic_function(const char *func_name
)
175 return !strcmp(func_name
, "atomicAdd") ||
176 !strcmp(func_name
, "atomicMin") ||
177 !strcmp(func_name
, "atomicMax") ||
178 !strcmp(func_name
, "atomicAnd") ||
179 !strcmp(func_name
, "atomicOr") ||
180 !strcmp(func_name
, "atomicXor") ||
181 !strcmp(func_name
, "atomicExchange") ||
182 !strcmp(func_name
, "atomicCompSwap");
186 * Verify that 'out' and 'inout' actual parameters are lvalues. Also, verify
187 * that 'const_in' formal parameters (an extension in our IR) correspond to
188 * ir_constant actual parameters.
191 verify_parameter_modes(_mesa_glsl_parse_state
*state
,
192 ir_function_signature
*sig
,
193 exec_list
&actual_ir_parameters
,
194 exec_list
&actual_ast_parameters
)
196 exec_node
*actual_ir_node
= actual_ir_parameters
.get_head_raw();
197 exec_node
*actual_ast_node
= actual_ast_parameters
.get_head_raw();
199 foreach_in_list(const ir_variable
, formal
, &sig
->parameters
) {
200 /* The lists must be the same length. */
201 assert(!actual_ir_node
->is_tail_sentinel());
202 assert(!actual_ast_node
->is_tail_sentinel());
204 const ir_rvalue
*const actual
= (ir_rvalue
*) actual_ir_node
;
205 const ast_expression
*const actual_ast
=
206 exec_node_data(ast_expression
, actual_ast_node
, link
);
208 YYLTYPE loc
= actual_ast
->get_location();
210 /* Verify that 'const_in' parameters are ir_constants. */
211 if (formal
->data
.mode
== ir_var_const_in
&&
212 actual
->ir_type
!= ir_type_constant
) {
213 _mesa_glsl_error(&loc
, state
,
214 "parameter `in %s' must be a constant expression",
219 /* Verify that shader_in parameters are shader inputs */
220 if (formal
->data
.must_be_shader_input
) {
221 const ir_rvalue
*val
= actual
;
223 /* GLSL 4.40 allows swizzles, while earlier GLSL versions do not. */
224 if (val
->ir_type
== ir_type_swizzle
) {
225 if (!state
->is_version(440, 0)) {
226 _mesa_glsl_error(&loc
, state
,
227 "parameter `%s` must not be swizzled",
231 val
= ((ir_swizzle
*)val
)->val
;
235 if (val
->ir_type
== ir_type_dereference_array
) {
236 val
= ((ir_dereference_array
*)val
)->array
;
237 } else if (val
->ir_type
== ir_type_dereference_record
&&
239 val
= ((ir_dereference_record
*)val
)->record
;
244 ir_variable
*var
= NULL
;
245 if (const ir_dereference_variable
*deref_var
= val
->as_dereference_variable())
246 var
= deref_var
->variable_referenced();
248 if (!var
|| var
->data
.mode
!= ir_var_shader_in
) {
249 _mesa_glsl_error(&loc
, state
,
250 "parameter `%s` must be a shader input",
255 var
->data
.must_be_shader_input
= 1;
258 /* Verify that 'out' and 'inout' actual parameters are lvalues. */
259 if (formal
->data
.mode
== ir_var_function_out
260 || formal
->data
.mode
== ir_var_function_inout
) {
261 const char *mode
= NULL
;
262 switch (formal
->data
.mode
) {
263 case ir_var_function_out
: mode
= "out"; break;
264 case ir_var_function_inout
: mode
= "inout"; break;
265 default: assert(false); break;
268 /* This AST-based check catches errors like f(i++). The IR-based
269 * is_lvalue() is insufficient because the actual parameter at the
270 * IR-level is just a temporary value, which is an l-value.
272 if (actual_ast
->non_lvalue_description
!= NULL
) {
273 _mesa_glsl_error(&loc
, state
,
274 "function parameter '%s %s' references a %s",
276 actual_ast
->non_lvalue_description
);
280 ir_variable
*var
= actual
->variable_referenced();
282 if (var
&& formal
->data
.mode
== ir_var_function_inout
) {
283 if ((var
->data
.mode
== ir_var_auto
||
284 var
->data
.mode
== ir_var_shader_out
) &&
285 !var
->data
.assigned
&&
286 !is_gl_identifier(var
->name
)) {
287 _mesa_glsl_warning(&loc
, state
, "`%s' used uninitialized",
293 var
->data
.assigned
= true;
295 if (var
&& var
->data
.read_only
) {
296 _mesa_glsl_error(&loc
, state
,
297 "function parameter '%s %s' references the "
298 "read-only variable '%s'",
300 actual
->variable_referenced()->name
);
302 } else if (!actual
->is_lvalue(state
)) {
303 _mesa_glsl_error(&loc
, state
,
304 "function parameter '%s %s' is not an lvalue",
309 assert(formal
->data
.mode
== ir_var_function_in
||
310 formal
->data
.mode
== ir_var_const_in
);
311 ir_variable
*var
= actual
->variable_referenced();
313 if ((var
->data
.mode
== ir_var_auto
||
314 var
->data
.mode
== ir_var_shader_out
) &&
315 !var
->data
.assigned
&&
316 !is_gl_identifier(var
->name
)) {
317 _mesa_glsl_warning(&loc
, state
, "`%s' used uninitialized",
323 if (formal
->type
->is_image() &&
324 actual
->variable_referenced()) {
325 if (!verify_image_parameter(&loc
, state
, formal
,
326 actual
->variable_referenced()))
330 actual_ir_node
= actual_ir_node
->next
;
331 actual_ast_node
= actual_ast_node
->next
;
334 /* The first parameter of atomic functions must be a buffer variable */
335 const char *func_name
= sig
->function_name();
336 bool is_atomic
= is_atomic_function(func_name
);
338 const ir_rvalue
*const actual
=
339 (ir_rvalue
*) actual_ir_parameters
.get_head_raw();
341 const ast_expression
*const actual_ast
=
342 exec_node_data(ast_expression
,
343 actual_ast_parameters
.get_head_raw(), link
);
344 YYLTYPE loc
= actual_ast
->get_location();
346 if (!verify_first_atomic_parameter(&loc
, state
,
347 actual
->variable_referenced())) {
355 struct copy_index_deref_data
{
357 exec_list
*before_instructions
;
361 copy_index_derefs_to_temps(ir_instruction
*ir
, void *data
)
363 struct copy_index_deref_data
*d
= (struct copy_index_deref_data
*)data
;
365 if (ir
->ir_type
== ir_type_dereference_array
) {
366 ir_dereference_array
*a
= (ir_dereference_array
*) ir
;
367 ir
= a
->array
->as_dereference();
369 ir_rvalue
*idx
= a
->array_index
;
370 ir_variable
*var
= idx
->variable_referenced();
372 /* If the index is read only it cannot change so there is no need
375 if (!var
|| var
->data
.read_only
|| var
->data
.memory_read_only
)
378 ir_variable
*tmp
= new(d
->mem_ctx
) ir_variable(idx
->type
, "idx_tmp",
380 d
->before_instructions
->push_tail(tmp
);
382 ir_dereference_variable
*const deref_tmp_1
=
383 new(d
->mem_ctx
) ir_dereference_variable(tmp
);
384 ir_assignment
*const assignment
=
385 new(d
->mem_ctx
) ir_assignment(deref_tmp_1
,
386 idx
->clone(d
->mem_ctx
, NULL
));
387 d
->before_instructions
->push_tail(assignment
);
389 /* Replace the array index with a dereference of the new temporary */
390 ir_dereference_variable
*const deref_tmp_2
=
391 new(d
->mem_ctx
) ir_dereference_variable(tmp
);
392 a
->array_index
= deref_tmp_2
;
397 fix_parameter(void *mem_ctx
, ir_rvalue
*actual
, const glsl_type
*formal_type
,
398 exec_list
*before_instructions
, exec_list
*after_instructions
,
399 bool parameter_is_inout
)
401 ir_expression
*const expr
= actual
->as_expression();
403 /* If the types match exactly and the parameter is not a vector-extract,
404 * nothing needs to be done to fix the parameter.
406 if (formal_type
== actual
->type
407 && (expr
== NULL
|| expr
->operation
!= ir_binop_vector_extract
)
408 && actual
->as_dereference_variable())
411 /* An array index could also be an out variable so we need to make a copy
412 * of them before the function is called.
414 if (!actual
->as_dereference_variable()) {
415 struct copy_index_deref_data data
;
416 data
.mem_ctx
= mem_ctx
;
417 data
.before_instructions
= before_instructions
;
419 visit_tree(actual
, copy_index_derefs_to_temps
, &data
);
422 /* To convert an out parameter, we need to create a temporary variable to
423 * hold the value before conversion, and then perform the conversion after
424 * the function call returns.
426 * This has the effect of transforming code like this:
432 * Into IR that's equivalent to this:
436 * int out_parameter_conversion;
437 * f(out_parameter_conversion);
438 * value = float(out_parameter_conversion);
440 * If the parameter is an ir_expression of ir_binop_vector_extract,
441 * additional conversion is needed in the post-call re-write.
444 new(mem_ctx
) ir_variable(formal_type
, "inout_tmp", ir_var_temporary
);
446 before_instructions
->push_tail(tmp
);
448 /* If the parameter is an inout parameter, copy the value of the actual
449 * parameter to the new temporary. Note that no type conversion is allowed
450 * here because inout parameters must match types exactly.
452 if (parameter_is_inout
) {
453 /* Inout parameters should never require conversion, since that would
454 * require an implicit conversion to exist both to and from the formal
455 * parameter type, and there are no bidirectional implicit conversions.
457 assert (actual
->type
== formal_type
);
459 ir_dereference_variable
*const deref_tmp_1
=
460 new(mem_ctx
) ir_dereference_variable(tmp
);
461 ir_assignment
*const assignment
=
462 new(mem_ctx
) ir_assignment(deref_tmp_1
, actual
->clone(mem_ctx
, NULL
));
463 before_instructions
->push_tail(assignment
);
466 /* Replace the parameter in the call with a dereference of the new
469 ir_dereference_variable
*const deref_tmp_2
=
470 new(mem_ctx
) ir_dereference_variable(tmp
);
471 actual
->replace_with(deref_tmp_2
);
474 /* Copy the temporary variable to the actual parameter with optional
475 * type conversion applied.
477 ir_rvalue
*rhs
= new(mem_ctx
) ir_dereference_variable(tmp
);
478 if (actual
->type
!= formal_type
)
479 rhs
= convert_component(rhs
, actual
->type
);
481 ir_rvalue
*lhs
= actual
;
482 if (expr
!= NULL
&& expr
->operation
== ir_binop_vector_extract
) {
483 lhs
= new(mem_ctx
) ir_dereference_array(expr
->operands
[0]->clone(mem_ctx
,
485 expr
->operands
[1]->clone(mem_ctx
,
489 ir_assignment
*const assignment_2
= new(mem_ctx
) ir_assignment(lhs
, rhs
);
490 after_instructions
->push_tail(assignment_2
);
494 * Generate a function call.
496 * For non-void functions, this returns a dereference of the temporary
497 * variable which stores the return value for the call. For void functions,
501 generate_call(exec_list
*instructions
, ir_function_signature
*sig
,
502 exec_list
*actual_parameters
,
503 ir_variable
*sub_var
,
504 ir_rvalue
*array_idx
,
505 struct _mesa_glsl_parse_state
*state
)
508 exec_list post_call_conversions
;
510 /* Perform implicit conversion of arguments. For out parameters, we need
511 * to place them in a temporary variable and do the conversion after the
512 * call takes place. Since we haven't emitted the call yet, we'll place
513 * the post-call conversions in a temporary exec_list, and emit them later.
515 foreach_two_lists(formal_node
, &sig
->parameters
,
516 actual_node
, actual_parameters
) {
517 ir_rvalue
*actual
= (ir_rvalue
*) actual_node
;
518 ir_variable
*formal
= (ir_variable
*) formal_node
;
520 if (formal
->type
->is_numeric() || formal
->type
->is_boolean()) {
521 switch (formal
->data
.mode
) {
522 case ir_var_const_in
:
523 case ir_var_function_in
: {
525 = convert_component(actual
, formal
->type
);
526 actual
->replace_with(converted
);
529 case ir_var_function_out
:
530 case ir_var_function_inout
:
531 fix_parameter(ctx
, actual
, formal
->type
,
532 instructions
, &post_call_conversions
,
533 formal
->data
.mode
== ir_var_function_inout
);
536 assert (!"Illegal formal parameter mode");
542 /* Section 4.3.2 (Const) of the GLSL 1.10.59 spec says:
544 * "Initializers for const declarations must be formed from literal
545 * values, other const variables (not including function call
546 * paramaters), or expressions of these.
548 * Constructors may be used in such expressions, but function calls may
551 * Section 4.3.3 (Constant Expressions) of the GLSL 1.20.8 spec says:
553 * "A constant expression is one of
557 * - a built-in function call whose arguments are all constant
558 * expressions, with the exception of the texture lookup
559 * functions, the noise functions, and ftransform. The built-in
560 * functions dFdx, dFdy, and fwidth must return 0 when evaluated
561 * inside an initializer with an argument that is a constant
564 * Section 5.10 (Constant Expressions) of the GLSL ES 1.00.17 spec says:
566 * "A constant expression is one of
570 * - a built-in function call whose arguments are all constant
571 * expressions, with the exception of the texture lookup
574 * Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec says:
576 * "A constant expression is one of
580 * - a built-in function call whose arguments are all constant
581 * expressions, with the exception of the texture lookup
582 * functions. The built-in functions dFdx, dFdy, and fwidth must
583 * return 0 when evaluated inside an initializer with an argument
584 * that is a constant expression."
586 * If the function call is a constant expression, don't generate any
587 * instructions; just generate an ir_constant.
589 if (state
->is_version(120, 100) ||
590 state
->ctx
->Const
.AllowGLSLBuiltinConstantExpression
) {
591 ir_constant
*value
= sig
->constant_expression_value(ctx
,
599 ir_dereference_variable
*deref
= NULL
;
600 if (!sig
->return_type
->is_void()) {
601 /* Create a new temporary to hold the return value. */
602 char *const name
= ir_variable::temporaries_allocate_names
603 ? ralloc_asprintf(ctx
, "%s_retval", sig
->function_name())
608 var
= new(ctx
) ir_variable(sig
->return_type
, name
, ir_var_temporary
);
609 instructions
->push_tail(var
);
613 deref
= new(ctx
) ir_dereference_variable(var
);
616 ir_call
*call
= new(ctx
) ir_call(sig
, deref
,
617 actual_parameters
, sub_var
, array_idx
);
618 instructions
->push_tail(call
);
620 /* Also emit any necessary out-parameter conversions. */
621 instructions
->append_list(&post_call_conversions
);
623 return deref
? deref
->clone(ctx
, NULL
) : NULL
;
627 * Given a function name and parameter list, find the matching signature.
629 static ir_function_signature
*
630 match_function_by_name(const char *name
,
631 exec_list
*actual_parameters
,
632 struct _mesa_glsl_parse_state
*state
)
634 ir_function
*f
= state
->symbols
->get_function(name
);
635 ir_function_signature
*local_sig
= NULL
;
636 ir_function_signature
*sig
= NULL
;
638 /* Is the function hidden by a record type constructor? */
639 if (state
->symbols
->get_type(name
))
640 return sig
; /* no match */
642 /* Is the function hidden by a variable (impossible in 1.10)? */
643 if (!state
->symbols
->separate_function_namespace
644 && state
->symbols
->get_variable(name
))
645 return sig
; /* no match */
648 /* In desktop GL, the presence of a user-defined signature hides any
649 * built-in signatures, so we must ignore them. In contrast, in ES2
650 * user-defined signatures add new overloads, so we must consider them.
652 bool allow_builtins
= state
->es_shader
|| !f
->has_user_signature();
654 /* Look for a match in the local shader. If exact, we're done. */
655 bool is_exact
= false;
656 sig
= local_sig
= f
->matching_signature(state
, actual_parameters
,
657 allow_builtins
, &is_exact
);
665 /* Local shader has no exact candidates; check the built-ins. */
666 sig
= _mesa_glsl_find_builtin_function(state
, name
, actual_parameters
);
668 /* if _mesa_glsl_find_builtin_function failed, fall back to the result
669 * of choose_best_inexact_overload() instead. This should only affect
672 return sig
? sig
: local_sig
;
675 static ir_function_signature
*
676 match_subroutine_by_name(const char *name
,
677 exec_list
*actual_parameters
,
678 struct _mesa_glsl_parse_state
*state
,
682 ir_function_signature
*sig
= NULL
;
683 ir_function
*f
, *found
= NULL
;
684 const char *new_name
;
686 bool is_exact
= false;
689 ralloc_asprintf(ctx
, "%s_%s",
690 _mesa_shader_stage_to_subroutine_prefix(state
->stage
),
692 var
= state
->symbols
->get_variable(new_name
);
696 for (int i
= 0; i
< state
->num_subroutine_types
; i
++) {
697 f
= state
->subroutine_types
[i
];
698 if (strcmp(f
->name
, var
->type
->without_array()->name
))
707 sig
= found
->matching_signature(state
, actual_parameters
,
713 generate_array_index(void *mem_ctx
, exec_list
*instructions
,
714 struct _mesa_glsl_parse_state
*state
, YYLTYPE loc
,
715 const ast_expression
*array
, ast_expression
*idx
,
716 const char **function_name
, exec_list
*actual_parameters
)
718 if (array
->oper
== ast_array_index
) {
719 /* This handles arrays of arrays */
720 ir_rvalue
*outer_array
= generate_array_index(mem_ctx
, instructions
,
722 array
->subexpressions
[0],
723 array
->subexpressions
[1],
726 ir_rvalue
*outer_array_idx
= idx
->hir(instructions
, state
);
728 YYLTYPE index_loc
= idx
->get_location();
729 return _mesa_ast_array_index_to_hir(mem_ctx
, state
, outer_array
,
730 outer_array_idx
, loc
,
733 ir_variable
*sub_var
= NULL
;
734 *function_name
= array
->primary_expression
.identifier
;
736 if (!match_subroutine_by_name(*function_name
, actual_parameters
,
738 _mesa_glsl_error(&loc
, state
, "Unknown subroutine `%s'",
740 *function_name
= NULL
; /* indicate error condition to caller */
744 ir_rvalue
*outer_array_idx
= idx
->hir(instructions
, state
);
745 return new(mem_ctx
) ir_dereference_array(sub_var
, outer_array_idx
);
750 function_exists(_mesa_glsl_parse_state
*state
,
751 struct glsl_symbol_table
*symbols
, const char *name
)
753 ir_function
*f
= symbols
->get_function(name
);
755 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
756 if (sig
->is_builtin() && !sig
->is_builtin_available(state
))
765 print_function_prototypes(_mesa_glsl_parse_state
*state
, YYLTYPE
*loc
,
771 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
772 if (sig
->is_builtin() && !sig
->is_builtin_available(state
))
775 char *str
= prototype_string(sig
->return_type
, f
->name
,
777 _mesa_glsl_error(loc
, state
, " %s", str
);
783 * Raise a "no matching function" error, listing all possible overloads the
784 * compiler considered so developers can figure out what went wrong.
787 no_matching_function_error(const char *name
,
789 exec_list
*actual_parameters
,
790 _mesa_glsl_parse_state
*state
)
792 gl_shader
*sh
= _mesa_glsl_get_builtin_function_shader();
794 if (!function_exists(state
, state
->symbols
, name
)
795 && (!state
->uses_builtin_functions
796 || !function_exists(state
, sh
->symbols
, name
))) {
797 _mesa_glsl_error(loc
, state
, "no function with name '%s'", name
);
799 char *str
= prototype_string(NULL
, name
, actual_parameters
);
800 _mesa_glsl_error(loc
, state
,
801 "no matching function for call to `%s';"
806 print_function_prototypes(state
, loc
,
807 state
->symbols
->get_function(name
));
809 if (state
->uses_builtin_functions
) {
810 print_function_prototypes(state
, loc
,
811 sh
->symbols
->get_function(name
));
817 * Perform automatic type conversion of constructor parameters
819 * This implements the rules in the "Conversion and Scalar Constructors"
820 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
823 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
)
825 void *ctx
= ralloc_parent(src
);
826 const unsigned a
= desired_type
->base_type
;
827 const unsigned b
= src
->type
->base_type
;
828 ir_expression
*result
= NULL
;
830 if (src
->type
->is_error())
833 assert(a
<= GLSL_TYPE_IMAGE
);
834 assert(b
<= GLSL_TYPE_IMAGE
);
843 result
= new(ctx
) ir_expression(ir_unop_i2u
, src
);
845 case GLSL_TYPE_FLOAT
:
846 result
= new(ctx
) ir_expression(ir_unop_f2u
, src
);
849 result
= new(ctx
) ir_expression(ir_unop_i2u
,
850 new(ctx
) ir_expression(ir_unop_b2i
,
853 case GLSL_TYPE_DOUBLE
:
854 result
= new(ctx
) ir_expression(ir_unop_d2u
, src
);
856 case GLSL_TYPE_UINT64
:
857 result
= new(ctx
) ir_expression(ir_unop_u642u
, src
);
859 case GLSL_TYPE_INT64
:
860 result
= new(ctx
) ir_expression(ir_unop_i642u
, src
);
862 case GLSL_TYPE_SAMPLER
:
863 result
= new(ctx
) ir_expression(ir_unop_unpack_sampler_2x32
, src
);
865 case GLSL_TYPE_IMAGE
:
866 result
= new(ctx
) ir_expression(ir_unop_unpack_image_2x32
, src
);
873 result
= new(ctx
) ir_expression(ir_unop_u2i
, src
);
875 case GLSL_TYPE_FLOAT
:
876 result
= new(ctx
) ir_expression(ir_unop_f2i
, src
);
879 result
= new(ctx
) ir_expression(ir_unop_b2i
, src
);
881 case GLSL_TYPE_DOUBLE
:
882 result
= new(ctx
) ir_expression(ir_unop_d2i
, src
);
884 case GLSL_TYPE_UINT64
:
885 result
= new(ctx
) ir_expression(ir_unop_u642i
, src
);
887 case GLSL_TYPE_INT64
:
888 result
= new(ctx
) ir_expression(ir_unop_i642i
, src
);
892 case GLSL_TYPE_FLOAT
:
895 result
= new(ctx
) ir_expression(ir_unop_u2f
, desired_type
, src
, NULL
);
898 result
= new(ctx
) ir_expression(ir_unop_i2f
, desired_type
, src
, NULL
);
901 result
= new(ctx
) ir_expression(ir_unop_b2f
, desired_type
, src
, NULL
);
903 case GLSL_TYPE_DOUBLE
:
904 result
= new(ctx
) ir_expression(ir_unop_d2f
, desired_type
, src
, NULL
);
906 case GLSL_TYPE_UINT64
:
907 result
= new(ctx
) ir_expression(ir_unop_u642f
, desired_type
, src
, NULL
);
909 case GLSL_TYPE_INT64
:
910 result
= new(ctx
) ir_expression(ir_unop_i642f
, desired_type
, src
, NULL
);
917 result
= new(ctx
) ir_expression(ir_unop_i2b
,
918 new(ctx
) ir_expression(ir_unop_u2i
,
922 result
= new(ctx
) ir_expression(ir_unop_i2b
, desired_type
, src
, NULL
);
924 case GLSL_TYPE_FLOAT
:
925 result
= new(ctx
) ir_expression(ir_unop_f2b
, desired_type
, src
, NULL
);
927 case GLSL_TYPE_DOUBLE
:
928 result
= new(ctx
) ir_expression(ir_unop_d2b
, desired_type
, src
, NULL
);
930 case GLSL_TYPE_UINT64
:
931 result
= new(ctx
) ir_expression(ir_unop_i642b
,
932 new(ctx
) ir_expression(ir_unop_u642i64
,
935 case GLSL_TYPE_INT64
:
936 result
= new(ctx
) ir_expression(ir_unop_i642b
, desired_type
, src
, NULL
);
940 case GLSL_TYPE_DOUBLE
:
943 result
= new(ctx
) ir_expression(ir_unop_i2d
, src
);
946 result
= new(ctx
) ir_expression(ir_unop_u2d
, src
);
949 result
= new(ctx
) ir_expression(ir_unop_f2d
,
950 new(ctx
) ir_expression(ir_unop_b2f
,
953 case GLSL_TYPE_FLOAT
:
954 result
= new(ctx
) ir_expression(ir_unop_f2d
, desired_type
, src
, NULL
);
956 case GLSL_TYPE_UINT64
:
957 result
= new(ctx
) ir_expression(ir_unop_u642d
, desired_type
, src
, NULL
);
959 case GLSL_TYPE_INT64
:
960 result
= new(ctx
) ir_expression(ir_unop_i642d
, desired_type
, src
, NULL
);
964 case GLSL_TYPE_UINT64
:
967 result
= new(ctx
) ir_expression(ir_unop_i2u64
, src
);
970 result
= new(ctx
) ir_expression(ir_unop_u2u64
, src
);
973 result
= new(ctx
) ir_expression(ir_unop_i642u64
,
974 new(ctx
) ir_expression(ir_unop_b2i64
,
977 case GLSL_TYPE_FLOAT
:
978 result
= new(ctx
) ir_expression(ir_unop_f2u64
, src
);
980 case GLSL_TYPE_DOUBLE
:
981 result
= new(ctx
) ir_expression(ir_unop_d2u64
, src
);
983 case GLSL_TYPE_INT64
:
984 result
= new(ctx
) ir_expression(ir_unop_i642u64
, src
);
988 case GLSL_TYPE_INT64
:
991 result
= new(ctx
) ir_expression(ir_unop_i2i64
, src
);
994 result
= new(ctx
) ir_expression(ir_unop_u2i64
, src
);
997 result
= new(ctx
) ir_expression(ir_unop_b2i64
, src
);
999 case GLSL_TYPE_FLOAT
:
1000 result
= new(ctx
) ir_expression(ir_unop_f2i64
, src
);
1002 case GLSL_TYPE_DOUBLE
:
1003 result
= new(ctx
) ir_expression(ir_unop_d2i64
, src
);
1005 case GLSL_TYPE_UINT64
:
1006 result
= new(ctx
) ir_expression(ir_unop_u642i64
, src
);
1010 case GLSL_TYPE_SAMPLER
:
1012 case GLSL_TYPE_UINT
:
1014 ir_expression(ir_unop_pack_sampler_2x32
, desired_type
, src
);
1018 case GLSL_TYPE_IMAGE
:
1020 case GLSL_TYPE_UINT
:
1022 ir_expression(ir_unop_pack_image_2x32
, desired_type
, src
);
1028 assert(result
!= NULL
);
1029 assert(result
->type
== desired_type
);
1031 /* Try constant folding; it may fold in the conversion we just added. */
1032 ir_constant
*const constant
= result
->constant_expression_value(ctx
);
1033 return (constant
!= NULL
) ? (ir_rvalue
*) constant
: (ir_rvalue
*) result
;
1038 * Perform automatic type and constant conversion of constructor parameters
1040 * This implements the rules in the "Implicit Conversions" rules, not the
1041 * "Conversion and Scalar Constructors".
1043 * After attempting the implicit conversion, an attempt to convert into a
1044 * constant valued expression is also done.
1046 * The \c from \c ir_rvalue is converted "in place".
1048 * \param from Operand that is being converted
1049 * \param to Base type the operand will be converted to
1050 * \param state GLSL compiler state
1053 * If the attempt to convert into a constant expression succeeds, \c true is
1054 * returned. Otherwise \c false is returned.
1057 implicitly_convert_component(ir_rvalue
* &from
, const glsl_base_type to
,
1058 struct _mesa_glsl_parse_state
*state
)
1060 void *mem_ctx
= state
;
1061 ir_rvalue
*result
= from
;
1063 if (to
!= from
->type
->base_type
) {
1064 const glsl_type
*desired_type
=
1065 glsl_type::get_instance(to
,
1066 from
->type
->vector_elements
,
1067 from
->type
->matrix_columns
);
1069 if (from
->type
->can_implicitly_convert_to(desired_type
, state
)) {
1070 /* Even though convert_component() implements the constructor
1071 * conversion rules (not the implicit conversion rules), its safe
1072 * to use it here because we already checked that the implicit
1073 * conversion is legal.
1075 result
= convert_component(from
, desired_type
);
1079 ir_rvalue
*const constant
= result
->constant_expression_value(mem_ctx
);
1081 if (constant
!= NULL
)
1084 if (from
!= result
) {
1085 from
->replace_with(result
);
1089 return constant
!= NULL
;
1094 * Dereference a specific component from a scalar, vector, or matrix
1097 dereference_component(ir_rvalue
*src
, unsigned component
)
1099 void *ctx
= ralloc_parent(src
);
1100 assert(component
< src
->type
->components());
1102 /* If the source is a constant, just create a new constant instead of a
1103 * dereference of the existing constant.
1105 ir_constant
*constant
= src
->as_constant();
1107 return new(ctx
) ir_constant(constant
, component
);
1109 if (src
->type
->is_scalar()) {
1111 } else if (src
->type
->is_vector()) {
1112 return new(ctx
) ir_swizzle(src
, component
, 0, 0, 0, 1);
1114 assert(src
->type
->is_matrix());
1116 /* Dereference a row of the matrix, then call this function again to get
1117 * a specific element from that row.
1119 const int c
= component
/ src
->type
->column_type()->vector_elements
;
1120 const int r
= component
% src
->type
->column_type()->vector_elements
;
1121 ir_constant
*const col_index
= new(ctx
) ir_constant(c
);
1122 ir_dereference
*const col
= new(ctx
) ir_dereference_array(src
,
1125 col
->type
= src
->type
->column_type();
1127 return dereference_component(col
, r
);
1130 assert(!"Should not get here.");
1136 process_vec_mat_constructor(exec_list
*instructions
,
1137 const glsl_type
*constructor_type
,
1138 YYLTYPE
*loc
, exec_list
*parameters
,
1139 struct _mesa_glsl_parse_state
*state
)
1143 /* The ARB_shading_language_420pack spec says:
1145 * "If an initializer is a list of initializers enclosed in curly braces,
1146 * the variable being declared must be a vector, a matrix, an array, or a
1149 * int i = { 1 }; // illegal, i is not an aggregate"
1151 if (constructor_type
->vector_elements
<= 1) {
1152 _mesa_glsl_error(loc
, state
, "aggregates can only initialize vectors, "
1153 "matrices, arrays, and structs");
1154 return ir_rvalue::error_value(ctx
);
1157 exec_list actual_parameters
;
1158 const unsigned parameter_count
=
1159 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
1161 if (parameter_count
== 0
1162 || (constructor_type
->is_vector() &&
1163 constructor_type
->vector_elements
!= parameter_count
)
1164 || (constructor_type
->is_matrix() &&
1165 constructor_type
->matrix_columns
!= parameter_count
)) {
1166 _mesa_glsl_error(loc
, state
, "%s constructor must have %u parameters",
1167 constructor_type
->is_vector() ? "vector" : "matrix",
1168 constructor_type
->vector_elements
);
1169 return ir_rvalue::error_value(ctx
);
1172 bool all_parameters_are_constant
= true;
1174 /* Type cast each parameter and, if possible, fold constants. */
1175 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1176 /* Apply implicit conversions (not the scalar constructor rules, see the
1177 * spec quote above!) and attempt to convert the parameter to a constant
1178 * valued expression. After doing so, track whether or not all the
1179 * parameters to the constructor are trivially constant valued
1182 all_parameters_are_constant
&=
1183 implicitly_convert_component(ir
, constructor_type
->base_type
, state
);
1185 if (constructor_type
->is_matrix()) {
1186 if (ir
->type
!= constructor_type
->column_type()) {
1187 _mesa_glsl_error(loc
, state
, "type error in matrix constructor: "
1188 "expected: %s, found %s",
1189 constructor_type
->column_type()->name
,
1191 return ir_rvalue::error_value(ctx
);
1193 } else if (ir
->type
!= constructor_type
->get_scalar_type()) {
1194 _mesa_glsl_error(loc
, state
, "type error in vector constructor: "
1195 "expected: %s, found %s",
1196 constructor_type
->get_scalar_type()->name
,
1198 return ir_rvalue::error_value(ctx
);
1202 if (all_parameters_are_constant
)
1203 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1205 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "vec_mat_ctor",
1207 instructions
->push_tail(var
);
1211 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
1212 ir_instruction
*assignment
= NULL
;
1214 if (var
->type
->is_matrix()) {
1216 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
1217 assignment
= new(ctx
) ir_assignment(lhs
, rhs
);
1219 /* use writemask rather than index for vector */
1220 assert(var
->type
->is_vector());
1222 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1223 assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
,
1224 (unsigned)(1 << i
));
1227 instructions
->push_tail(assignment
);
1232 return new(ctx
) ir_dereference_variable(var
);
1237 process_array_constructor(exec_list
*instructions
,
1238 const glsl_type
*constructor_type
,
1239 YYLTYPE
*loc
, exec_list
*parameters
,
1240 struct _mesa_glsl_parse_state
*state
)
1243 /* Array constructors come in two forms: sized and unsized. Sized array
1244 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
1245 * variables. In this case the number of parameters must exactly match the
1246 * specified size of the array.
1248 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
1249 * are vec4 variables. In this case the size of the array being constructed
1250 * is determined by the number of parameters.
1252 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
1254 * "There must be exactly the same number of arguments as the size of
1255 * the array being constructed. If no size is present in the
1256 * constructor, then the array is explicitly sized to the number of
1257 * arguments provided. The arguments are assigned in order, starting at
1258 * element 0, to the elements of the constructed array. Each argument
1259 * must be the same type as the element type of the array, or be a type
1260 * that can be converted to the element type of the array according to
1261 * Section 4.1.10 "Implicit Conversions.""
1263 exec_list actual_parameters
;
1264 const unsigned parameter_count
=
1265 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
1266 bool is_unsized_array
= constructor_type
->is_unsized_array();
1268 if ((parameter_count
== 0) ||
1269 (!is_unsized_array
&& (constructor_type
->length
!= parameter_count
))) {
1270 const unsigned min_param
= is_unsized_array
1271 ? 1 : constructor_type
->length
;
1273 _mesa_glsl_error(loc
, state
, "array constructor must have %s %u "
1275 is_unsized_array
? "at least" : "exactly",
1276 min_param
, (min_param
<= 1) ? "" : "s");
1277 return ir_rvalue::error_value(ctx
);
1280 if (is_unsized_array
) {
1282 glsl_type::get_array_instance(constructor_type
->fields
.array
,
1284 assert(constructor_type
!= NULL
);
1285 assert(constructor_type
->length
== parameter_count
);
1288 bool all_parameters_are_constant
= true;
1289 const glsl_type
*element_type
= constructor_type
->fields
.array
;
1291 /* Type cast each parameter and, if possible, fold constants. */
1292 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1293 /* Apply implicit conversions (not the scalar constructor rules, see the
1294 * spec quote above!) and attempt to convert the parameter to a constant
1295 * valued expression. After doing so, track whether or not all the
1296 * parameters to the constructor are trivially constant valued
1299 all_parameters_are_constant
&=
1300 implicitly_convert_component(ir
, element_type
->base_type
, state
);
1302 if (constructor_type
->fields
.array
->is_unsized_array()) {
1303 /* As the inner parameters of the constructor are created without
1304 * knowledge of each other we need to check to make sure unsized
1305 * parameters of unsized constructors all end up with the same size.
1307 * e.g we make sure to fail for a constructor like this:
1308 * vec4[][] a = vec4[][](vec4[](vec4(0.0), vec4(1.0)),
1309 * vec4[](vec4(0.0), vec4(1.0), vec4(1.0)),
1310 * vec4[](vec4(0.0), vec4(1.0)));
1312 if (element_type
->is_unsized_array()) {
1313 /* This is the first parameter so just get the type */
1314 element_type
= ir
->type
;
1315 } else if (element_type
!= ir
->type
) {
1316 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
1317 "expected: %s, found %s",
1320 return ir_rvalue::error_value(ctx
);
1322 } else if (ir
->type
!= constructor_type
->fields
.array
) {
1323 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
1324 "expected: %s, found %s",
1325 constructor_type
->fields
.array
->name
,
1327 return ir_rvalue::error_value(ctx
);
1329 element_type
= ir
->type
;
1333 if (constructor_type
->fields
.array
->is_unsized_array()) {
1335 glsl_type::get_array_instance(element_type
,
1337 assert(constructor_type
!= NULL
);
1338 assert(constructor_type
->length
== parameter_count
);
1341 if (all_parameters_are_constant
)
1342 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1344 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "array_ctor",
1346 instructions
->push_tail(var
);
1349 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
1350 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
1351 new(ctx
) ir_constant(i
));
1353 ir_instruction
*assignment
= new(ctx
) ir_assignment(lhs
, rhs
);
1354 instructions
->push_tail(assignment
);
1359 return new(ctx
) ir_dereference_variable(var
);
1364 * Determine if a list consists of a single scalar r-value
1367 single_scalar_parameter(exec_list
*parameters
)
1369 const ir_rvalue
*const p
= (ir_rvalue
*) parameters
->get_head_raw();
1370 assert(((ir_rvalue
*)p
)->as_rvalue() != NULL
);
1372 return (p
->type
->is_scalar() && p
->next
->is_tail_sentinel());
1377 * Generate inline code for a vector constructor
1379 * The generated constructor code will consist of a temporary variable
1380 * declaration of the same type as the constructor. A sequence of assignments
1381 * from constructor parameters to the temporary will follow.
1384 * An \c ir_dereference_variable of the temprorary generated in the constructor
1388 emit_inline_vector_constructor(const glsl_type
*type
,
1389 exec_list
*instructions
,
1390 exec_list
*parameters
,
1393 assert(!parameters
->is_empty());
1395 ir_variable
*var
= new(ctx
) ir_variable(type
, "vec_ctor", ir_var_temporary
);
1396 instructions
->push_tail(var
);
1398 /* There are three kinds of vector constructors.
1400 * - Construct a vector from a single scalar by replicating that scalar to
1401 * all components of the vector.
1403 * - Construct a vector from at least a matrix. This case should already
1404 * have been taken care of in ast_function_expression::hir by breaking
1405 * down the matrix into a series of column vectors.
1407 * - Construct a vector from an arbirary combination of vectors and
1408 * scalars. The components of the constructor parameters are assigned
1409 * to the vector in order until the vector is full.
1411 const unsigned lhs_components
= type
->components();
1412 if (single_scalar_parameter(parameters
)) {
1413 ir_rvalue
*first_param
= (ir_rvalue
*)parameters
->get_head_raw();
1414 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(first_param
, 0, 0, 0, 0,
1416 ir_dereference_variable
*lhs
= new(ctx
) ir_dereference_variable(var
);
1417 const unsigned mask
= (1U << lhs_components
) - 1;
1419 assert(rhs
->type
== lhs
->type
);
1421 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
, mask
);
1422 instructions
->push_tail(inst
);
1424 unsigned base_component
= 0;
1425 unsigned base_lhs_component
= 0;
1426 ir_constant_data data
;
1427 unsigned constant_mask
= 0, constant_components
= 0;
1429 memset(&data
, 0, sizeof(data
));
1431 foreach_in_list(ir_rvalue
, param
, parameters
) {
1432 unsigned rhs_components
= param
->type
->components();
1434 /* Do not try to assign more components to the vector than it has! */
1435 if ((rhs_components
+ base_lhs_component
) > lhs_components
) {
1436 rhs_components
= lhs_components
- base_lhs_component
;
1439 const ir_constant
*const c
= param
->as_constant();
1441 for (unsigned i
= 0; i
< rhs_components
; i
++) {
1442 switch (c
->type
->base_type
) {
1443 case GLSL_TYPE_UINT
:
1444 data
.u
[i
+ base_component
] = c
->get_uint_component(i
);
1447 data
.i
[i
+ base_component
] = c
->get_int_component(i
);
1449 case GLSL_TYPE_FLOAT
:
1450 data
.f
[i
+ base_component
] = c
->get_float_component(i
);
1452 case GLSL_TYPE_DOUBLE
:
1453 data
.d
[i
+ base_component
] = c
->get_double_component(i
);
1455 case GLSL_TYPE_BOOL
:
1456 data
.b
[i
+ base_component
] = c
->get_bool_component(i
);
1458 case GLSL_TYPE_UINT64
:
1459 data
.u64
[i
+ base_component
] = c
->get_uint64_component(i
);
1461 case GLSL_TYPE_INT64
:
1462 data
.i64
[i
+ base_component
] = c
->get_int64_component(i
);
1465 assert(!"Should not get here.");
1470 /* Mask of fields to be written in the assignment. */
1471 constant_mask
|= ((1U << rhs_components
) - 1) << base_lhs_component
;
1472 constant_components
+= rhs_components
;
1474 base_component
+= rhs_components
;
1476 /* Advance the component index by the number of components
1477 * that were just assigned.
1479 base_lhs_component
+= rhs_components
;
1482 if (constant_mask
!= 0) {
1483 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1484 const glsl_type
*rhs_type
=
1485 glsl_type::get_instance(var
->type
->base_type
,
1486 constant_components
,
1488 ir_rvalue
*rhs
= new(ctx
) ir_constant(rhs_type
, &data
);
1490 ir_instruction
*inst
=
1491 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, constant_mask
);
1492 instructions
->push_tail(inst
);
1496 foreach_in_list(ir_rvalue
, param
, parameters
) {
1497 unsigned rhs_components
= param
->type
->components();
1499 /* Do not try to assign more components to the vector than it has! */
1500 if ((rhs_components
+ base_component
) > lhs_components
) {
1501 rhs_components
= lhs_components
- base_component
;
1504 /* If we do not have any components left to copy, break out of the
1505 * loop. This can happen when initializing a vec4 with a mat3 as the
1506 * mat3 would have been broken into a series of column vectors.
1508 if (rhs_components
== 0) {
1512 const ir_constant
*const c
= param
->as_constant();
1514 /* Mask of fields to be written in the assignment. */
1515 const unsigned write_mask
= ((1U << rhs_components
) - 1)
1518 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1520 /* Generate a swizzle so that LHS and RHS sizes match. */
1522 new(ctx
) ir_swizzle(param
, 0, 1, 2, 3, rhs_components
);
1524 ir_instruction
*inst
=
1525 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1526 instructions
->push_tail(inst
);
1529 /* Advance the component index by the number of components that were
1532 base_component
+= rhs_components
;
1535 return new(ctx
) ir_dereference_variable(var
);
1540 * Generate assignment of a portion of a vector to a portion of a matrix column
1542 * \param src_base First component of the source to be used in assignment
1543 * \param column Column of destination to be assiged
1544 * \param row_base First component of the destination column to be assigned
1545 * \param count Number of components to be assigned
1548 * \c src_base + \c count must be less than or equal to the number of
1549 * components in the source vector.
1551 static ir_instruction
*
1552 assign_to_matrix_column(ir_variable
*var
, unsigned column
, unsigned row_base
,
1553 ir_rvalue
*src
, unsigned src_base
, unsigned count
,
1556 ir_constant
*col_idx
= new(mem_ctx
) ir_constant(column
);
1557 ir_dereference
*column_ref
= new(mem_ctx
) ir_dereference_array(var
,
1560 assert(column_ref
->type
->components() >= (row_base
+ count
));
1561 assert(src
->type
->components() >= (src_base
+ count
));
1563 /* Generate a swizzle that extracts the number of components from the source
1564 * that are to be assigned to the column of the matrix.
1566 if (count
< src
->type
->vector_elements
) {
1567 src
= new(mem_ctx
) ir_swizzle(src
,
1568 src_base
+ 0, src_base
+ 1,
1569 src_base
+ 2, src_base
+ 3,
1573 /* Mask of fields to be written in the assignment. */
1574 const unsigned write_mask
= ((1U << count
) - 1) << row_base
;
1576 return new(mem_ctx
) ir_assignment(column_ref
, src
, NULL
, write_mask
);
1581 * Generate inline code for a matrix constructor
1583 * The generated constructor code will consist of a temporary variable
1584 * declaration of the same type as the constructor. A sequence of assignments
1585 * from constructor parameters to the temporary will follow.
1588 * An \c ir_dereference_variable of the temprorary generated in the constructor
1592 emit_inline_matrix_constructor(const glsl_type
*type
,
1593 exec_list
*instructions
,
1594 exec_list
*parameters
,
1597 assert(!parameters
->is_empty());
1599 ir_variable
*var
= new(ctx
) ir_variable(type
, "mat_ctor", ir_var_temporary
);
1600 instructions
->push_tail(var
);
1602 /* There are three kinds of matrix constructors.
1604 * - Construct a matrix from a single scalar by replicating that scalar to
1605 * along the diagonal of the matrix and setting all other components to
1608 * - Construct a matrix from an arbirary combination of vectors and
1609 * scalars. The components of the constructor parameters are assigned
1610 * to the matrix in column-major order until the matrix is full.
1612 * - Construct a matrix from a single matrix. The source matrix is copied
1613 * to the upper left portion of the constructed matrix, and the remaining
1614 * elements take values from the identity matrix.
1616 ir_rvalue
*const first_param
= (ir_rvalue
*) parameters
->get_head_raw();
1617 if (single_scalar_parameter(parameters
)) {
1618 /* Assign the scalar to the X component of a vec4, and fill the remaining
1619 * components with zero.
1621 glsl_base_type param_base_type
= first_param
->type
->base_type
;
1622 assert(first_param
->type
->is_float() || first_param
->type
->is_double());
1623 ir_variable
*rhs_var
=
1624 new(ctx
) ir_variable(glsl_type::get_instance(param_base_type
, 4, 1),
1627 instructions
->push_tail(rhs_var
);
1629 ir_constant_data zero
;
1630 for (unsigned i
= 0; i
< 4; i
++)
1631 if (first_param
->type
->is_float())
1636 ir_instruction
*inst
=
1637 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(rhs_var
),
1638 new(ctx
) ir_constant(rhs_var
->type
, &zero
));
1639 instructions
->push_tail(inst
);
1641 ir_dereference
*const rhs_ref
=
1642 new(ctx
) ir_dereference_variable(rhs_var
);
1644 inst
= new(ctx
) ir_assignment(rhs_ref
, first_param
, NULL
, 0x01);
1645 instructions
->push_tail(inst
);
1647 /* Assign the temporary vector to each column of the destination matrix
1648 * with a swizzle that puts the X component on the diagonal of the
1649 * matrix. In some cases this may mean that the X component does not
1650 * get assigned into the column at all (i.e., when the matrix has more
1651 * columns than rows).
1653 static const unsigned rhs_swiz
[4][4] = {
1660 const unsigned cols_to_init
= MIN2(type
->matrix_columns
,
1661 type
->vector_elements
);
1662 for (unsigned i
= 0; i
< cols_to_init
; i
++) {
1663 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1664 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
,
1667 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1668 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, rhs_swiz
[i
],
1669 type
->vector_elements
);
1671 inst
= new(ctx
) ir_assignment(col_ref
, rhs
);
1672 instructions
->push_tail(inst
);
1675 for (unsigned i
= cols_to_init
; i
< type
->matrix_columns
; i
++) {
1676 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1677 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
,
1680 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1681 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, 1, 1, 1, 1,
1682 type
->vector_elements
);
1684 inst
= new(ctx
) ir_assignment(col_ref
, rhs
);
1685 instructions
->push_tail(inst
);
1687 } else if (first_param
->type
->is_matrix()) {
1688 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
1690 * "If a matrix is constructed from a matrix, then each component
1691 * (column i, row j) in the result that has a corresponding
1692 * component (column i, row j) in the argument will be initialized
1693 * from there. All other components will be initialized to the
1694 * identity matrix. If a matrix argument is given to a matrix
1695 * constructor, it is an error to have any other arguments."
1697 assert(first_param
->next
->is_tail_sentinel());
1698 ir_rvalue
*const src_matrix
= first_param
;
1700 /* If the source matrix is smaller, pre-initialize the relavent parts of
1701 * the destination matrix to the identity matrix.
1703 if ((src_matrix
->type
->matrix_columns
< var
->type
->matrix_columns
) ||
1704 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)) {
1706 /* If the source matrix has fewer rows, every column of the
1707 * destination must be initialized. Otherwise only the columns in
1708 * the destination that do not exist in the source must be
1712 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)
1713 ? 0 : src_matrix
->type
->matrix_columns
;
1715 const glsl_type
*const col_type
= var
->type
->column_type();
1716 for (/* empty */; col
< var
->type
->matrix_columns
; col
++) {
1717 ir_constant_data ident
;
1719 if (!col_type
->is_double()) {
1724 ident
.f
[col
] = 1.0f
;
1733 ir_rvalue
*const rhs
= new(ctx
) ir_constant(col_type
, &ident
);
1735 ir_rvalue
*const lhs
=
1736 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(col
));
1738 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
);
1739 instructions
->push_tail(inst
);
1743 /* Assign columns from the source matrix to the destination matrix.
1745 * Since the parameter will be used in the RHS of multiple assignments,
1746 * generate a temporary and copy the paramter there.
1748 ir_variable
*const rhs_var
=
1749 new(ctx
) ir_variable(first_param
->type
, "mat_ctor_mat",
1751 instructions
->push_tail(rhs_var
);
1753 ir_dereference
*const rhs_var_ref
=
1754 new(ctx
) ir_dereference_variable(rhs_var
);
1755 ir_instruction
*const inst
=
1756 new(ctx
) ir_assignment(rhs_var_ref
, first_param
);
1757 instructions
->push_tail(inst
);
1759 const unsigned last_row
= MIN2(src_matrix
->type
->vector_elements
,
1760 var
->type
->vector_elements
);
1761 const unsigned last_col
= MIN2(src_matrix
->type
->matrix_columns
,
1762 var
->type
->matrix_columns
);
1764 unsigned swiz
[4] = { 0, 0, 0, 0 };
1765 for (unsigned i
= 1; i
< last_row
; i
++)
1768 const unsigned write_mask
= (1U << last_row
) - 1;
1770 for (unsigned i
= 0; i
< last_col
; i
++) {
1771 ir_dereference
*const lhs
=
1772 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
1773 ir_rvalue
*const rhs_col
=
1774 new(ctx
) ir_dereference_array(rhs_var
, new(ctx
) ir_constant(i
));
1776 /* If one matrix has columns that are smaller than the columns of the
1777 * other matrix, wrap the column access of the larger with a swizzle
1778 * so that the LHS and RHS of the assignment have the same size (and
1779 * therefore have the same type).
1781 * It would be perfectly valid to unconditionally generate the
1782 * swizzles, this this will typically result in a more compact IR
1786 if (lhs
->type
->vector_elements
!= rhs_col
->type
->vector_elements
) {
1787 rhs
= new(ctx
) ir_swizzle(rhs_col
, swiz
, last_row
);
1792 ir_instruction
*inst
=
1793 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1794 instructions
->push_tail(inst
);
1797 const unsigned cols
= type
->matrix_columns
;
1798 const unsigned rows
= type
->vector_elements
;
1799 unsigned remaining_slots
= rows
* cols
;
1800 unsigned col_idx
= 0;
1801 unsigned row_idx
= 0;
1803 foreach_in_list(ir_rvalue
, rhs
, parameters
) {
1804 unsigned rhs_components
= rhs
->type
->components();
1805 unsigned rhs_base
= 0;
1807 if (remaining_slots
== 0)
1810 /* Since the parameter might be used in the RHS of two assignments,
1811 * generate a temporary and copy the paramter there.
1813 ir_variable
*rhs_var
=
1814 new(ctx
) ir_variable(rhs
->type
, "mat_ctor_vec", ir_var_temporary
);
1815 instructions
->push_tail(rhs_var
);
1817 ir_dereference
*rhs_var_ref
=
1818 new(ctx
) ir_dereference_variable(rhs_var
);
1819 ir_instruction
*inst
= new(ctx
) ir_assignment(rhs_var_ref
, rhs
);
1820 instructions
->push_tail(inst
);
1823 /* Assign the current parameter to as many components of the matrix
1826 * NOTE: A single vector parameter can span two matrix columns. A
1827 * single vec4, for example, can completely fill a mat2.
1829 unsigned count
= MIN2(rows
- row_idx
,
1830 rhs_components
- rhs_base
);
1832 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1833 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
1838 instructions
->push_tail(inst
);
1841 remaining_slots
-= count
;
1843 /* Sometimes, there is still data left in the parameters and
1844 * components left to be set in the destination but in other
1847 if (row_idx
>= rows
) {
1851 } while(remaining_slots
> 0 && rhs_base
< rhs_components
);
1855 return new(ctx
) ir_dereference_variable(var
);
1860 emit_inline_record_constructor(const glsl_type
*type
,
1861 exec_list
*instructions
,
1862 exec_list
*parameters
,
1865 ir_variable
*const var
=
1866 new(mem_ctx
) ir_variable(type
, "record_ctor", ir_var_temporary
);
1867 ir_dereference_variable
*const d
=
1868 new(mem_ctx
) ir_dereference_variable(var
);
1870 instructions
->push_tail(var
);
1872 exec_node
*node
= parameters
->get_head_raw();
1873 for (unsigned i
= 0; i
< type
->length
; i
++) {
1874 assert(!node
->is_tail_sentinel());
1876 ir_dereference
*const lhs
=
1877 new(mem_ctx
) ir_dereference_record(d
->clone(mem_ctx
, NULL
),
1878 type
->fields
.structure
[i
].name
);
1880 ir_rvalue
*const rhs
= ((ir_instruction
*) node
)->as_rvalue();
1881 assert(rhs
!= NULL
);
1883 ir_instruction
*const assign
= new(mem_ctx
) ir_assignment(lhs
, rhs
);
1885 instructions
->push_tail(assign
);
1894 process_record_constructor(exec_list
*instructions
,
1895 const glsl_type
*constructor_type
,
1896 YYLTYPE
*loc
, exec_list
*parameters
,
1897 struct _mesa_glsl_parse_state
*state
)
1900 /* From page 32 (page 38 of the PDF) of the GLSL 1.20 spec:
1902 * "The arguments to the constructor will be used to set the structure's
1903 * fields, in order, using one argument per field. Each argument must
1904 * be the same type as the field it sets, or be a type that can be
1905 * converted to the field's type according to Section 4.1.10 “Implicit
1908 * From page 35 (page 41 of the PDF) of the GLSL 4.20 spec:
1910 * "In all cases, the innermost initializer (i.e., not a list of
1911 * initializers enclosed in curly braces) applied to an object must
1912 * have the same type as the object being initialized or be a type that
1913 * can be converted to the object's type according to section 4.1.10
1914 * "Implicit Conversions". In the latter case, an implicit conversion
1915 * will be done on the initializer before the assignment is done."
1917 exec_list actual_parameters
;
1919 const unsigned parameter_count
=
1920 process_parameters(instructions
, &actual_parameters
, parameters
,
1923 if (parameter_count
!= constructor_type
->length
) {
1924 _mesa_glsl_error(loc
, state
,
1925 "%s parameters in constructor for `%s'",
1926 parameter_count
> constructor_type
->length
1927 ? "too many": "insufficient",
1928 constructor_type
->name
);
1929 return ir_rvalue::error_value(ctx
);
1932 bool all_parameters_are_constant
= true;
1935 /* Type cast each parameter and, if possible, fold constants. */
1936 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1938 const glsl_struct_field
*struct_field
=
1939 &constructor_type
->fields
.structure
[i
];
1941 /* Apply implicit conversions (not the scalar constructor rules, see the
1942 * spec quote above!) and attempt to convert the parameter to a constant
1943 * valued expression. After doing so, track whether or not all the
1944 * parameters to the constructor are trivially constant valued
1947 all_parameters_are_constant
&=
1948 implicitly_convert_component(ir
, struct_field
->type
->base_type
,
1951 if (ir
->type
!= struct_field
->type
) {
1952 _mesa_glsl_error(loc
, state
,
1953 "parameter type mismatch in constructor for `%s.%s' "
1955 constructor_type
->name
,
1958 struct_field
->type
->name
);
1959 return ir_rvalue::error_value(ctx
);
1965 if (all_parameters_are_constant
) {
1966 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1968 return emit_inline_record_constructor(constructor_type
, instructions
,
1969 &actual_parameters
, state
);
1974 ast_function_expression::handle_method(exec_list
*instructions
,
1975 struct _mesa_glsl_parse_state
*state
)
1977 const ast_expression
*field
= subexpressions
[0];
1981 /* Handle "method calls" in GLSL 1.20 - namely, array.length() */
1982 YYLTYPE loc
= get_location();
1983 state
->check_version(120, 300, &loc
, "methods not supported");
1986 method
= field
->primary_expression
.identifier
;
1988 /* This would prevent to raise "uninitialized variable" warnings when
1989 * calling array.length.
1991 field
->subexpressions
[0]->set_is_lhs(true);
1992 op
= field
->subexpressions
[0]->hir(instructions
, state
);
1993 if (strcmp(method
, "length") == 0) {
1994 if (!this->expressions
.is_empty()) {
1995 _mesa_glsl_error(&loc
, state
, "length method takes no arguments");
1999 if (op
->type
->is_array()) {
2000 if (op
->type
->is_unsized_array()) {
2001 if (!state
->has_shader_storage_buffer_objects()) {
2002 _mesa_glsl_error(&loc
, state
,
2003 "length called on unsized array"
2004 " only available with"
2005 " ARB_shader_storage_buffer_object");
2007 /* Calculate length of an unsized array in run-time */
2008 result
= new(ctx
) ir_expression(ir_unop_ssbo_unsized_array_length
,
2011 result
= new(ctx
) ir_constant(op
->type
->array_size());
2013 } else if (op
->type
->is_vector()) {
2014 if (state
->has_420pack()) {
2015 /* .length() returns int. */
2016 result
= new(ctx
) ir_constant((int) op
->type
->vector_elements
);
2018 _mesa_glsl_error(&loc
, state
, "length method on matrix only"
2019 " available with ARB_shading_language_420pack");
2022 } else if (op
->type
->is_matrix()) {
2023 if (state
->has_420pack()) {
2024 /* .length() returns int. */
2025 result
= new(ctx
) ir_constant((int) op
->type
->matrix_columns
);
2027 _mesa_glsl_error(&loc
, state
, "length method on matrix only"
2028 " available with ARB_shading_language_420pack");
2032 _mesa_glsl_error(&loc
, state
, "length called on scalar.");
2036 _mesa_glsl_error(&loc
, state
, "unknown method: `%s'", method
);
2041 return ir_rvalue::error_value(ctx
);
2044 static inline bool is_valid_constructor(const glsl_type
*type
,
2045 struct _mesa_glsl_parse_state
*state
)
2047 return type
->is_numeric() || type
->is_boolean() ||
2048 (state
->has_bindless() && (type
->is_sampler() || type
->is_image()));
2052 ast_function_expression::hir(exec_list
*instructions
,
2053 struct _mesa_glsl_parse_state
*state
)
2056 /* There are three sorts of function calls.
2058 * 1. constructors - The first subexpression is an ast_type_specifier.
2059 * 2. methods - Only the .length() method of array types.
2060 * 3. functions - Calls to regular old functions.
2063 if (is_constructor()) {
2064 const ast_type_specifier
*type
=
2065 (ast_type_specifier
*) subexpressions
[0];
2066 YYLTYPE loc
= type
->get_location();
2069 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
2071 /* constructor_type can be NULL if a variable with the same name as the
2072 * structure has come into scope.
2074 if (constructor_type
== NULL
) {
2075 _mesa_glsl_error(& loc
, state
, "unknown type `%s' (structure name "
2076 "may be shadowed by a variable with the same name)",
2078 return ir_rvalue::error_value(ctx
);
2082 /* Constructors for opaque types are illegal.
2084 * From section 4.1.7 of the ARB_bindless_texture spec:
2086 * "Samplers are represented using 64-bit integer handles, and may be "
2087 * converted to and from 64-bit integers using constructors."
2089 * From section 4.1.X of the ARB_bindless_texture spec:
2091 * "Images are represented using 64-bit integer handles, and may be
2092 * converted to and from 64-bit integers using constructors."
2094 if (constructor_type
->contains_atomic() ||
2095 (!state
->has_bindless() && constructor_type
->contains_opaque())) {
2096 _mesa_glsl_error(& loc
, state
, "cannot construct %s type `%s'",
2097 state
->has_bindless() ? "atomic" : "opaque",
2098 constructor_type
->name
);
2099 return ir_rvalue::error_value(ctx
);
2102 if (constructor_type
->is_subroutine()) {
2103 _mesa_glsl_error(& loc
, state
,
2104 "subroutine name cannot be a constructor `%s'",
2105 constructor_type
->name
);
2106 return ir_rvalue::error_value(ctx
);
2109 if (constructor_type
->is_array()) {
2110 if (!state
->check_version(state
->allow_glsl_120_subset_in_110
? 110 : 120,
2111 300, &loc
, "array constructors forbidden")) {
2112 return ir_rvalue::error_value(ctx
);
2115 return process_array_constructor(instructions
, constructor_type
,
2116 & loc
, &this->expressions
, state
);
2120 /* There are two kinds of constructor calls. Constructors for arrays and
2121 * structures must have the exact number of arguments with matching types
2122 * in the correct order. These constructors follow essentially the same
2123 * type matching rules as functions.
2125 * Constructors for built-in language types, such as mat4 and vec2, are
2126 * free form. The only requirements are that the parameters must provide
2127 * enough values of the correct scalar type and that no arguments are
2128 * given past the last used argument.
2130 * When using the C-style initializer syntax from GLSL 4.20, constructors
2131 * must have the exact number of arguments with matching types in the
2134 if (constructor_type
->is_struct()) {
2135 return process_record_constructor(instructions
, constructor_type
,
2136 &loc
, &this->expressions
,
2140 if (!is_valid_constructor(constructor_type
, state
))
2141 return ir_rvalue::error_value(ctx
);
2143 /* Total number of components of the type being constructed. */
2144 const unsigned type_components
= constructor_type
->components();
2146 /* Number of components from parameters that have actually been
2147 * consumed. This is used to perform several kinds of error checking.
2149 unsigned components_used
= 0;
2151 unsigned matrix_parameters
= 0;
2152 unsigned nonmatrix_parameters
= 0;
2153 exec_list actual_parameters
;
2155 foreach_list_typed(ast_node
, ast
, link
, &this->expressions
) {
2156 ir_rvalue
*result
= ast
->hir(instructions
, state
);
2158 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
2160 * "It is an error to provide extra arguments beyond this
2161 * last used argument."
2163 if (components_used
>= type_components
) {
2164 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
2166 constructor_type
->name
);
2167 return ir_rvalue::error_value(ctx
);
2170 if (!is_valid_constructor(result
->type
, state
)) {
2171 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
2172 "non-numeric data type",
2173 constructor_type
->name
);
2174 return ir_rvalue::error_value(ctx
);
2177 /* Count the number of matrix and nonmatrix parameters. This
2178 * is used below to enforce some of the constructor rules.
2180 if (result
->type
->is_matrix())
2181 matrix_parameters
++;
2183 nonmatrix_parameters
++;
2185 actual_parameters
.push_tail(result
);
2186 components_used
+= result
->type
->components();
2189 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
2191 * "It is an error to construct matrices from other matrices. This
2192 * is reserved for future use."
2194 if (matrix_parameters
> 0
2195 && constructor_type
->is_matrix()
2196 && !state
->check_version(120, 100, &loc
,
2197 "cannot construct `%s' from a matrix",
2198 constructor_type
->name
)) {
2199 return ir_rvalue::error_value(ctx
);
2202 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
2204 * "If a matrix argument is given to a matrix constructor, it is
2205 * an error to have any other arguments."
2207 if ((matrix_parameters
> 0)
2208 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
2209 && constructor_type
->is_matrix()) {
2210 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
2211 "matrix must be only parameter",
2212 constructor_type
->name
);
2213 return ir_rvalue::error_value(ctx
);
2216 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
2218 * "In these cases, there must be enough components provided in the
2219 * arguments to provide an initializer for every component in the
2220 * constructed value."
2222 if (components_used
< type_components
&& components_used
!= 1
2223 && matrix_parameters
== 0) {
2224 _mesa_glsl_error(& loc
, state
, "too few components to construct "
2226 constructor_type
->name
);
2227 return ir_rvalue::error_value(ctx
);
2230 /* Matrices can never be consumed as is by any constructor but matrix
2231 * constructors. If the constructor type is not matrix, always break the
2232 * matrix up into a series of column vectors.
2234 if (!constructor_type
->is_matrix()) {
2235 foreach_in_list_safe(ir_rvalue
, matrix
, &actual_parameters
) {
2236 if (!matrix
->type
->is_matrix())
2239 /* Create a temporary containing the matrix. */
2240 ir_variable
*var
= new(ctx
) ir_variable(matrix
->type
, "matrix_tmp",
2242 instructions
->push_tail(var
);
2243 instructions
->push_tail(
2244 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
2246 var
->constant_value
= matrix
->constant_expression_value(ctx
);
2248 /* Replace the matrix with dereferences of its columns. */
2249 for (int i
= 0; i
< matrix
->type
->matrix_columns
; i
++) {
2250 matrix
->insert_before(
2251 new (ctx
) ir_dereference_array(var
,
2252 new(ctx
) ir_constant(i
)));
2258 bool all_parameters_are_constant
= true;
2260 /* Type cast each parameter and, if possible, fold constants.*/
2261 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
2262 const glsl_type
*desired_type
;
2264 /* From section 5.4.1 of the ARB_bindless_texture spec:
2266 * "In the following four constructors, the low 32 bits of the sampler
2267 * type correspond to the .x component of the uvec2 and the high 32
2268 * bits correspond to the .y component."
2270 * uvec2(any sampler type) // Converts a sampler type to a
2271 * // pair of 32-bit unsigned integers
2272 * any sampler type(uvec2) // Converts a pair of 32-bit unsigned integers to
2274 * uvec2(any image type) // Converts an image type to a
2275 * // pair of 32-bit unsigned integers
2276 * any image type(uvec2) // Converts a pair of 32-bit unsigned integers to
2279 if (ir
->type
->is_sampler() || ir
->type
->is_image()) {
2280 /* Convert a sampler/image type to a pair of 32-bit unsigned
2281 * integers as defined by ARB_bindless_texture.
2283 if (constructor_type
!= glsl_type::uvec2_type
) {
2284 _mesa_glsl_error(&loc
, state
, "sampler and image types can only "
2285 "be converted to a pair of 32-bit unsigned "
2288 desired_type
= glsl_type::uvec2_type
;
2289 } else if (constructor_type
->is_sampler() ||
2290 constructor_type
->is_image()) {
2291 /* Convert a pair of 32-bit unsigned integers to a sampler or image
2292 * type as defined by ARB_bindless_texture.
2294 if (ir
->type
!= glsl_type::uvec2_type
) {
2295 _mesa_glsl_error(&loc
, state
, "sampler and image types can only "
2296 "be converted from a pair of 32-bit unsigned "
2299 desired_type
= constructor_type
;
2302 glsl_type::get_instance(constructor_type
->base_type
,
2303 ir
->type
->vector_elements
,
2304 ir
->type
->matrix_columns
);
2307 ir_rvalue
*result
= convert_component(ir
, desired_type
);
2309 /* Attempt to convert the parameter to a constant valued expression.
2310 * After doing so, track whether or not all the parameters to the
2311 * constructor are trivially constant valued expressions.
2313 ir_rvalue
*const constant
= result
->constant_expression_value(ctx
);
2315 if (constant
!= NULL
)
2318 all_parameters_are_constant
= false;
2321 ir
->replace_with(result
);
2325 /* If all of the parameters are trivially constant, create a
2326 * constant representing the complete collection of parameters.
2328 if (all_parameters_are_constant
) {
2329 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
2330 } else if (constructor_type
->is_scalar()) {
2331 return dereference_component((ir_rvalue
*)
2332 actual_parameters
.get_head_raw(),
2334 } else if (constructor_type
->is_vector()) {
2335 return emit_inline_vector_constructor(constructor_type
,
2340 assert(constructor_type
->is_matrix());
2341 return emit_inline_matrix_constructor(constructor_type
,
2346 } else if (subexpressions
[0]->oper
== ast_field_selection
) {
2347 return handle_method(instructions
, state
);
2349 const ast_expression
*id
= subexpressions
[0];
2350 const char *func_name
= NULL
;
2351 YYLTYPE loc
= get_location();
2352 exec_list actual_parameters
;
2353 ir_variable
*sub_var
= NULL
;
2354 ir_rvalue
*array_idx
= NULL
;
2356 process_parameters(instructions
, &actual_parameters
, &this->expressions
,
2359 if (id
->oper
== ast_array_index
) {
2360 array_idx
= generate_array_index(ctx
, instructions
, state
, loc
,
2361 id
->subexpressions
[0],
2362 id
->subexpressions
[1], &func_name
,
2363 &actual_parameters
);
2364 } else if (id
->oper
== ast_identifier
) {
2365 func_name
= id
->primary_expression
.identifier
;
2367 _mesa_glsl_error(&loc
, state
, "function name is not an identifier");
2370 /* an error was emitted earlier */
2372 return ir_rvalue::error_value(ctx
);
2374 ir_function_signature
*sig
=
2375 match_function_by_name(func_name
, &actual_parameters
, state
);
2377 ir_rvalue
*value
= NULL
;
2379 sig
= match_subroutine_by_name(func_name
, &actual_parameters
,
2384 no_matching_function_error(func_name
, &loc
,
2385 &actual_parameters
, state
);
2386 value
= ir_rvalue::error_value(ctx
);
2387 } else if (!verify_parameter_modes(state
, sig
,
2389 this->expressions
)) {
2390 /* an error has already been emitted */
2391 value
= ir_rvalue::error_value(ctx
);
2392 } else if (sig
->is_builtin() && strcmp(func_name
, "ftransform") == 0) {
2393 /* ftransform refers to global variables, and we don't have any code
2394 * for remapping the variable references in the built-in shader.
2397 state
->symbols
->get_variable("gl_ModelViewProjectionMatrix");
2398 ir_variable
*vtx
= state
->symbols
->get_variable("gl_Vertex");
2399 value
= new(ctx
) ir_expression(ir_binop_mul
, glsl_type::vec4_type
,
2400 new(ctx
) ir_dereference_variable(mvp
),
2401 new(ctx
) ir_dereference_variable(vtx
));
2403 bool is_begin_interlock
= false;
2404 bool is_end_interlock
= false;
2405 if (sig
->is_builtin() &&
2406 state
->stage
== MESA_SHADER_FRAGMENT
&&
2407 state
->ARB_fragment_shader_interlock_enable
) {
2408 is_begin_interlock
= strcmp(func_name
, "beginInvocationInterlockARB") == 0;
2409 is_end_interlock
= strcmp(func_name
, "endInvocationInterlockARB") == 0;
2412 if (sig
->is_builtin() &&
2413 ((state
->stage
== MESA_SHADER_TESS_CTRL
&&
2414 strcmp(func_name
, "barrier") == 0) ||
2415 is_begin_interlock
|| is_end_interlock
)) {
2416 if (state
->current_function
== NULL
||
2417 strcmp(state
->current_function
->function_name(), "main") != 0) {
2418 _mesa_glsl_error(&loc
, state
,
2419 "%s() may only be used in main()", func_name
);
2422 if (state
->found_return
) {
2423 _mesa_glsl_error(&loc
, state
,
2424 "%s() may not be used after return", func_name
);
2427 if (instructions
!= &state
->current_function
->body
) {
2428 _mesa_glsl_error(&loc
, state
,
2429 "%s() may not be used in control flow", func_name
);
2433 /* There can be only one begin/end interlock pair in the function. */
2434 if (is_begin_interlock
) {
2435 if (state
->found_begin_interlock
)
2436 _mesa_glsl_error(&loc
, state
,
2437 "beginInvocationInterlockARB may not be used twice");
2438 state
->found_begin_interlock
= true;
2439 } else if (is_end_interlock
) {
2440 if (!state
->found_begin_interlock
)
2441 _mesa_glsl_error(&loc
, state
,
2442 "endInvocationInterlockARB may not be used "
2443 "before beginInvocationInterlockARB");
2444 if (state
->found_end_interlock
)
2445 _mesa_glsl_error(&loc
, state
,
2446 "endInvocationInterlockARB may not be used twice");
2447 state
->found_end_interlock
= true;
2450 value
= generate_call(instructions
, sig
, &actual_parameters
, sub_var
,
2453 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::void_type
,
2456 instructions
->push_tail(tmp
);
2457 value
= new(ctx
) ir_dereference_variable(tmp
);
2464 unreachable("not reached");
2468 ast_function_expression::has_sequence_subexpression() const
2470 foreach_list_typed(const ast_node
, ast
, link
, &this->expressions
) {
2471 if (ast
->has_sequence_subexpression())
2479 ast_aggregate_initializer::hir(exec_list
*instructions
,
2480 struct _mesa_glsl_parse_state
*state
)
2483 YYLTYPE loc
= this->get_location();
2485 if (!this->constructor_type
) {
2486 _mesa_glsl_error(&loc
, state
, "type of C-style initializer unknown");
2487 return ir_rvalue::error_value(ctx
);
2489 const glsl_type
*const constructor_type
= this->constructor_type
;
2491 if (!state
->has_420pack()) {
2492 _mesa_glsl_error(&loc
, state
, "C-style initialization requires the "
2493 "GL_ARB_shading_language_420pack extension");
2494 return ir_rvalue::error_value(ctx
);
2497 if (constructor_type
->is_array()) {
2498 return process_array_constructor(instructions
, constructor_type
, &loc
,
2499 &this->expressions
, state
);
2502 if (constructor_type
->is_struct()) {
2503 return process_record_constructor(instructions
, constructor_type
, &loc
,
2504 &this->expressions
, state
);
2507 return process_vec_mat_constructor(instructions
, constructor_type
, &loc
,
2508 &this->expressions
, state
);