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_atomic_image_parameter_qualifier(YYLTYPE
*loc
, _mesa_glsl_parse_state
*state
,
190 (var
->data
.image_format
!= PIPE_FORMAT_R32_UINT
&&
191 var
->data
.image_format
!= PIPE_FORMAT_R32_SINT
&&
192 var
->data
.image_format
!= PIPE_FORMAT_R32_FLOAT
)) {
193 _mesa_glsl_error(loc
, state
, "Image atomic functions should use r32i/r32ui "
201 is_atomic_image_function(const char *func_name
)
203 return !strcmp(func_name
, "imageAtomicAdd") ||
204 !strcmp(func_name
, "imageAtomicMin") ||
205 !strcmp(func_name
, "imageAtomicMax") ||
206 !strcmp(func_name
, "imageAtomicAnd") ||
207 !strcmp(func_name
, "imageAtomicOr") ||
208 !strcmp(func_name
, "imageAtomicXor") ||
209 !strcmp(func_name
, "imageAtomicExchange") ||
210 !strcmp(func_name
, "imageAtomicCompSwap") ||
211 !strcmp(func_name
, "imageAtomicIncWrap") ||
212 !strcmp(func_name
, "imageAtomicDecWrap");
217 * Verify that 'out' and 'inout' actual parameters are lvalues. Also, verify
218 * that 'const_in' formal parameters (an extension in our IR) correspond to
219 * ir_constant actual parameters.
222 verify_parameter_modes(_mesa_glsl_parse_state
*state
,
223 ir_function_signature
*sig
,
224 exec_list
&actual_ir_parameters
,
225 exec_list
&actual_ast_parameters
)
227 exec_node
*actual_ir_node
= actual_ir_parameters
.get_head_raw();
228 exec_node
*actual_ast_node
= actual_ast_parameters
.get_head_raw();
230 foreach_in_list(const ir_variable
, formal
, &sig
->parameters
) {
231 /* The lists must be the same length. */
232 assert(!actual_ir_node
->is_tail_sentinel());
233 assert(!actual_ast_node
->is_tail_sentinel());
235 const ir_rvalue
*const actual
= (ir_rvalue
*) actual_ir_node
;
236 const ast_expression
*const actual_ast
=
237 exec_node_data(ast_expression
, actual_ast_node
, link
);
239 YYLTYPE loc
= actual_ast
->get_location();
241 /* Verify that 'const_in' parameters are ir_constants. */
242 if (formal
->data
.mode
== ir_var_const_in
&&
243 actual
->ir_type
!= ir_type_constant
) {
244 _mesa_glsl_error(&loc
, state
,
245 "parameter `in %s' must be a constant expression",
250 /* Verify that shader_in parameters are shader inputs */
251 if (formal
->data
.must_be_shader_input
) {
252 const ir_rvalue
*val
= actual
;
254 /* GLSL 4.40 allows swizzles, while earlier GLSL versions do not. */
255 if (val
->ir_type
== ir_type_swizzle
) {
256 if (!state
->is_version(440, 0)) {
257 _mesa_glsl_error(&loc
, state
,
258 "parameter `%s` must not be swizzled",
262 val
= ((ir_swizzle
*)val
)->val
;
266 if (val
->ir_type
== ir_type_dereference_array
) {
267 val
= ((ir_dereference_array
*)val
)->array
;
268 } else if (val
->ir_type
== ir_type_dereference_record
&&
270 val
= ((ir_dereference_record
*)val
)->record
;
275 ir_variable
*var
= NULL
;
276 if (const ir_dereference_variable
*deref_var
= val
->as_dereference_variable())
277 var
= deref_var
->variable_referenced();
279 if (!var
|| var
->data
.mode
!= ir_var_shader_in
) {
280 _mesa_glsl_error(&loc
, state
,
281 "parameter `%s` must be a shader input",
286 var
->data
.must_be_shader_input
= 1;
289 /* Verify that 'out' and 'inout' actual parameters are lvalues. */
290 if (formal
->data
.mode
== ir_var_function_out
291 || formal
->data
.mode
== ir_var_function_inout
) {
292 const char *mode
= NULL
;
293 switch (formal
->data
.mode
) {
294 case ir_var_function_out
: mode
= "out"; break;
295 case ir_var_function_inout
: mode
= "inout"; break;
296 default: assert(false); break;
299 /* This AST-based check catches errors like f(i++). The IR-based
300 * is_lvalue() is insufficient because the actual parameter at the
301 * IR-level is just a temporary value, which is an l-value.
303 if (actual_ast
->non_lvalue_description
!= NULL
) {
304 _mesa_glsl_error(&loc
, state
,
305 "function parameter '%s %s' references a %s",
307 actual_ast
->non_lvalue_description
);
311 ir_variable
*var
= actual
->variable_referenced();
313 if (var
&& formal
->data
.mode
== ir_var_function_inout
) {
314 if ((var
->data
.mode
== ir_var_auto
||
315 var
->data
.mode
== ir_var_shader_out
) &&
316 !var
->data
.assigned
&&
317 !is_gl_identifier(var
->name
)) {
318 _mesa_glsl_warning(&loc
, state
, "`%s' used uninitialized",
324 var
->data
.assigned
= true;
326 if (var
&& var
->data
.read_only
) {
327 _mesa_glsl_error(&loc
, state
,
328 "function parameter '%s %s' references the "
329 "read-only variable '%s'",
331 actual
->variable_referenced()->name
);
333 } else if (!actual
->is_lvalue(state
)) {
334 _mesa_glsl_error(&loc
, state
,
335 "function parameter '%s %s' is not an lvalue",
340 assert(formal
->data
.mode
== ir_var_function_in
||
341 formal
->data
.mode
== ir_var_const_in
);
342 ir_variable
*var
= actual
->variable_referenced();
344 if ((var
->data
.mode
== ir_var_auto
||
345 var
->data
.mode
== ir_var_shader_out
) &&
346 !var
->data
.assigned
&&
347 !is_gl_identifier(var
->name
)) {
348 _mesa_glsl_warning(&loc
, state
, "`%s' used uninitialized",
354 if (formal
->type
->is_image() &&
355 actual
->variable_referenced()) {
356 if (!verify_image_parameter(&loc
, state
, formal
,
357 actual
->variable_referenced()))
361 actual_ir_node
= actual_ir_node
->next
;
362 actual_ast_node
= actual_ast_node
->next
;
365 /* The first parameter of atomic functions must be a buffer variable */
366 const char *func_name
= sig
->function_name();
367 bool is_atomic
= is_atomic_function(func_name
);
369 const ir_rvalue
*const actual
=
370 (ir_rvalue
*) actual_ir_parameters
.get_head_raw();
372 const ast_expression
*const actual_ast
=
373 exec_node_data(ast_expression
,
374 actual_ast_parameters
.get_head_raw(), link
);
375 YYLTYPE loc
= actual_ast
->get_location();
377 if (!verify_first_atomic_parameter(&loc
, state
,
378 actual
->variable_referenced())) {
381 } else if (is_atomic_image_function(func_name
)) {
382 const ir_rvalue
*const actual
=
383 (ir_rvalue
*) actual_ir_parameters
.get_head_raw();
385 const ast_expression
*const actual_ast
=
386 exec_node_data(ast_expression
,
387 actual_ast_parameters
.get_head_raw(), link
);
388 YYLTYPE loc
= actual_ast
->get_location();
390 if (!verify_atomic_image_parameter_qualifier(&loc
, state
,
391 actual
->variable_referenced())) {
399 struct copy_index_deref_data
{
401 exec_list
*before_instructions
;
405 copy_index_derefs_to_temps(ir_instruction
*ir
, void *data
)
407 struct copy_index_deref_data
*d
= (struct copy_index_deref_data
*)data
;
409 if (ir
->ir_type
== ir_type_dereference_array
) {
410 ir_dereference_array
*a
= (ir_dereference_array
*) ir
;
411 ir
= a
->array
->as_dereference();
413 ir_rvalue
*idx
= a
->array_index
;
414 ir_variable
*var
= idx
->variable_referenced();
416 /* If the index is read only it cannot change so there is no need
419 if (!var
|| var
->data
.read_only
|| var
->data
.memory_read_only
)
422 ir_variable
*tmp
= new(d
->mem_ctx
) ir_variable(idx
->type
, "idx_tmp",
424 d
->before_instructions
->push_tail(tmp
);
426 ir_dereference_variable
*const deref_tmp_1
=
427 new(d
->mem_ctx
) ir_dereference_variable(tmp
);
428 ir_assignment
*const assignment
=
429 new(d
->mem_ctx
) ir_assignment(deref_tmp_1
,
430 idx
->clone(d
->mem_ctx
, NULL
));
431 d
->before_instructions
->push_tail(assignment
);
433 /* Replace the array index with a dereference of the new temporary */
434 ir_dereference_variable
*const deref_tmp_2
=
435 new(d
->mem_ctx
) ir_dereference_variable(tmp
);
436 a
->array_index
= deref_tmp_2
;
441 fix_parameter(void *mem_ctx
, ir_rvalue
*actual
, const glsl_type
*formal_type
,
442 exec_list
*before_instructions
, exec_list
*after_instructions
,
443 bool parameter_is_inout
)
445 ir_expression
*const expr
= actual
->as_expression();
447 /* If the types match exactly and the parameter is not a vector-extract,
448 * nothing needs to be done to fix the parameter.
450 if (formal_type
== actual
->type
451 && (expr
== NULL
|| expr
->operation
!= ir_binop_vector_extract
)
452 && actual
->as_dereference_variable())
455 /* An array index could also be an out variable so we need to make a copy
456 * of them before the function is called.
458 if (!actual
->as_dereference_variable()) {
459 struct copy_index_deref_data data
;
460 data
.mem_ctx
= mem_ctx
;
461 data
.before_instructions
= before_instructions
;
463 visit_tree(actual
, copy_index_derefs_to_temps
, &data
);
466 /* To convert an out parameter, we need to create a temporary variable to
467 * hold the value before conversion, and then perform the conversion after
468 * the function call returns.
470 * This has the effect of transforming code like this:
476 * Into IR that's equivalent to this:
480 * int out_parameter_conversion;
481 * f(out_parameter_conversion);
482 * value = float(out_parameter_conversion);
484 * If the parameter is an ir_expression of ir_binop_vector_extract,
485 * additional conversion is needed in the post-call re-write.
488 new(mem_ctx
) ir_variable(formal_type
, "inout_tmp", ir_var_temporary
);
490 before_instructions
->push_tail(tmp
);
492 /* If the parameter is an inout parameter, copy the value of the actual
493 * parameter to the new temporary. Note that no type conversion is allowed
494 * here because inout parameters must match types exactly.
496 if (parameter_is_inout
) {
497 /* Inout parameters should never require conversion, since that would
498 * require an implicit conversion to exist both to and from the formal
499 * parameter type, and there are no bidirectional implicit conversions.
501 assert (actual
->type
== formal_type
);
503 ir_dereference_variable
*const deref_tmp_1
=
504 new(mem_ctx
) ir_dereference_variable(tmp
);
505 ir_assignment
*const assignment
=
506 new(mem_ctx
) ir_assignment(deref_tmp_1
, actual
->clone(mem_ctx
, NULL
));
507 before_instructions
->push_tail(assignment
);
510 /* Replace the parameter in the call with a dereference of the new
513 ir_dereference_variable
*const deref_tmp_2
=
514 new(mem_ctx
) ir_dereference_variable(tmp
);
515 actual
->replace_with(deref_tmp_2
);
518 /* Copy the temporary variable to the actual parameter with optional
519 * type conversion applied.
521 ir_rvalue
*rhs
= new(mem_ctx
) ir_dereference_variable(tmp
);
522 if (actual
->type
!= formal_type
)
523 rhs
= convert_component(rhs
, actual
->type
);
525 ir_rvalue
*lhs
= actual
;
526 if (expr
!= NULL
&& expr
->operation
== ir_binop_vector_extract
) {
527 lhs
= new(mem_ctx
) ir_dereference_array(expr
->operands
[0]->clone(mem_ctx
,
529 expr
->operands
[1]->clone(mem_ctx
,
533 ir_assignment
*const assignment_2
= new(mem_ctx
) ir_assignment(lhs
, rhs
);
534 after_instructions
->push_tail(assignment_2
);
538 * Generate a function call.
540 * For non-void functions, this returns a dereference of the temporary
541 * variable which stores the return value for the call. For void functions,
545 generate_call(exec_list
*instructions
, ir_function_signature
*sig
,
546 exec_list
*actual_parameters
,
547 ir_variable
*sub_var
,
548 ir_rvalue
*array_idx
,
549 struct _mesa_glsl_parse_state
*state
)
552 exec_list post_call_conversions
;
554 /* Perform implicit conversion of arguments. For out parameters, we need
555 * to place them in a temporary variable and do the conversion after the
556 * call takes place. Since we haven't emitted the call yet, we'll place
557 * the post-call conversions in a temporary exec_list, and emit them later.
559 foreach_two_lists(formal_node
, &sig
->parameters
,
560 actual_node
, actual_parameters
) {
561 ir_rvalue
*actual
= (ir_rvalue
*) actual_node
;
562 ir_variable
*formal
= (ir_variable
*) formal_node
;
564 if (formal
->type
->is_numeric() || formal
->type
->is_boolean()) {
565 switch (formal
->data
.mode
) {
566 case ir_var_const_in
:
567 case ir_var_function_in
: {
569 = convert_component(actual
, formal
->type
);
570 actual
->replace_with(converted
);
573 case ir_var_function_out
:
574 case ir_var_function_inout
:
575 fix_parameter(ctx
, actual
, formal
->type
,
576 instructions
, &post_call_conversions
,
577 formal
->data
.mode
== ir_var_function_inout
);
580 assert (!"Illegal formal parameter mode");
586 /* Section 4.3.2 (Const) of the GLSL 1.10.59 spec says:
588 * "Initializers for const declarations must be formed from literal
589 * values, other const variables (not including function call
590 * paramaters), or expressions of these.
592 * Constructors may be used in such expressions, but function calls may
595 * Section 4.3.3 (Constant Expressions) of the GLSL 1.20.8 spec says:
597 * "A constant expression is one of
601 * - a built-in function call whose arguments are all constant
602 * expressions, with the exception of the texture lookup
603 * functions, the noise functions, and ftransform. The built-in
604 * functions dFdx, dFdy, and fwidth must return 0 when evaluated
605 * inside an initializer with an argument that is a constant
608 * Section 5.10 (Constant Expressions) of the GLSL ES 1.00.17 spec says:
610 * "A constant expression is one of
614 * - a built-in function call whose arguments are all constant
615 * expressions, with the exception of the texture lookup
618 * Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec says:
620 * "A constant expression is one of
624 * - a built-in function call whose arguments are all constant
625 * expressions, with the exception of the texture lookup
626 * functions. The built-in functions dFdx, dFdy, and fwidth must
627 * return 0 when evaluated inside an initializer with an argument
628 * that is a constant expression."
630 * If the function call is a constant expression, don't generate any
631 * instructions; just generate an ir_constant.
633 if (state
->is_version(120, 100) ||
634 state
->ctx
->Const
.AllowGLSLBuiltinConstantExpression
) {
635 ir_constant
*value
= sig
->constant_expression_value(ctx
,
643 ir_dereference_variable
*deref
= NULL
;
644 if (!sig
->return_type
->is_void()) {
645 /* Create a new temporary to hold the return value. */
646 char *const name
= ir_variable::temporaries_allocate_names
647 ? ralloc_asprintf(ctx
, "%s_retval", sig
->function_name())
652 var
= new(ctx
) ir_variable(sig
->return_type
, name
, ir_var_temporary
);
653 instructions
->push_tail(var
);
657 deref
= new(ctx
) ir_dereference_variable(var
);
660 ir_call
*call
= new(ctx
) ir_call(sig
, deref
,
661 actual_parameters
, sub_var
, array_idx
);
662 instructions
->push_tail(call
);
664 /* Also emit any necessary out-parameter conversions. */
665 instructions
->append_list(&post_call_conversions
);
667 return deref
? deref
->clone(ctx
, NULL
) : NULL
;
671 * Given a function name and parameter list, find the matching signature.
673 static ir_function_signature
*
674 match_function_by_name(const char *name
,
675 exec_list
*actual_parameters
,
676 struct _mesa_glsl_parse_state
*state
)
678 ir_function
*f
= state
->symbols
->get_function(name
);
679 ir_function_signature
*local_sig
= NULL
;
680 ir_function_signature
*sig
= NULL
;
682 /* Is the function hidden by a record type constructor? */
683 if (state
->symbols
->get_type(name
))
684 return sig
; /* no match */
686 /* Is the function hidden by a variable (impossible in 1.10)? */
687 if (!state
->symbols
->separate_function_namespace
688 && state
->symbols
->get_variable(name
))
689 return sig
; /* no match */
692 /* In desktop GL, the presence of a user-defined signature hides any
693 * built-in signatures, so we must ignore them. In contrast, in ES2
694 * user-defined signatures add new overloads, so we must consider them.
696 bool allow_builtins
= state
->es_shader
|| !f
->has_user_signature();
698 /* Look for a match in the local shader. If exact, we're done. */
699 bool is_exact
= false;
700 sig
= local_sig
= f
->matching_signature(state
, actual_parameters
,
701 allow_builtins
, &is_exact
);
709 /* Local shader has no exact candidates; check the built-ins. */
710 sig
= _mesa_glsl_find_builtin_function(state
, name
, actual_parameters
);
712 /* if _mesa_glsl_find_builtin_function failed, fall back to the result
713 * of choose_best_inexact_overload() instead. This should only affect
716 return sig
? sig
: local_sig
;
719 static ir_function_signature
*
720 match_subroutine_by_name(const char *name
,
721 exec_list
*actual_parameters
,
722 struct _mesa_glsl_parse_state
*state
,
726 ir_function_signature
*sig
= NULL
;
727 ir_function
*f
, *found
= NULL
;
728 const char *new_name
;
730 bool is_exact
= false;
733 ralloc_asprintf(ctx
, "%s_%s",
734 _mesa_shader_stage_to_subroutine_prefix(state
->stage
),
736 var
= state
->symbols
->get_variable(new_name
);
740 for (int i
= 0; i
< state
->num_subroutine_types
; i
++) {
741 f
= state
->subroutine_types
[i
];
742 if (strcmp(f
->name
, var
->type
->without_array()->name
))
751 sig
= found
->matching_signature(state
, actual_parameters
,
757 generate_array_index(void *mem_ctx
, exec_list
*instructions
,
758 struct _mesa_glsl_parse_state
*state
, YYLTYPE loc
,
759 const ast_expression
*array
, ast_expression
*idx
,
760 const char **function_name
, exec_list
*actual_parameters
)
762 if (array
->oper
== ast_array_index
) {
763 /* This handles arrays of arrays */
764 ir_rvalue
*outer_array
= generate_array_index(mem_ctx
, instructions
,
766 array
->subexpressions
[0],
767 array
->subexpressions
[1],
770 ir_rvalue
*outer_array_idx
= idx
->hir(instructions
, state
);
772 YYLTYPE index_loc
= idx
->get_location();
773 return _mesa_ast_array_index_to_hir(mem_ctx
, state
, outer_array
,
774 outer_array_idx
, loc
,
777 ir_variable
*sub_var
= NULL
;
778 *function_name
= array
->primary_expression
.identifier
;
780 if (!match_subroutine_by_name(*function_name
, actual_parameters
,
782 _mesa_glsl_error(&loc
, state
, "Unknown subroutine `%s'",
784 *function_name
= NULL
; /* indicate error condition to caller */
788 ir_rvalue
*outer_array_idx
= idx
->hir(instructions
, state
);
789 return new(mem_ctx
) ir_dereference_array(sub_var
, outer_array_idx
);
794 function_exists(_mesa_glsl_parse_state
*state
,
795 struct glsl_symbol_table
*symbols
, const char *name
)
797 ir_function
*f
= symbols
->get_function(name
);
799 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
800 if (sig
->is_builtin() && !sig
->is_builtin_available(state
))
809 print_function_prototypes(_mesa_glsl_parse_state
*state
, YYLTYPE
*loc
,
815 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
816 if (sig
->is_builtin() && !sig
->is_builtin_available(state
))
819 char *str
= prototype_string(sig
->return_type
, f
->name
,
821 _mesa_glsl_error(loc
, state
, " %s", str
);
827 * Raise a "no matching function" error, listing all possible overloads the
828 * compiler considered so developers can figure out what went wrong.
831 no_matching_function_error(const char *name
,
833 exec_list
*actual_parameters
,
834 _mesa_glsl_parse_state
*state
)
836 gl_shader
*sh
= _mesa_glsl_get_builtin_function_shader();
838 if (!function_exists(state
, state
->symbols
, name
)
839 && (!state
->uses_builtin_functions
840 || !function_exists(state
, sh
->symbols
, name
))) {
841 _mesa_glsl_error(loc
, state
, "no function with name '%s'", name
);
843 char *str
= prototype_string(NULL
, name
, actual_parameters
);
844 _mesa_glsl_error(loc
, state
,
845 "no matching function for call to `%s';"
850 print_function_prototypes(state
, loc
,
851 state
->symbols
->get_function(name
));
853 if (state
->uses_builtin_functions
) {
854 print_function_prototypes(state
, loc
,
855 sh
->symbols
->get_function(name
));
861 * Perform automatic type conversion of constructor parameters
863 * This implements the rules in the "Conversion and Scalar Constructors"
864 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
867 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
)
869 void *ctx
= ralloc_parent(src
);
870 const unsigned a
= desired_type
->base_type
;
871 const unsigned b
= src
->type
->base_type
;
872 ir_expression
*result
= NULL
;
874 if (src
->type
->is_error())
877 assert(a
<= GLSL_TYPE_IMAGE
);
878 assert(b
<= GLSL_TYPE_IMAGE
);
887 result
= new(ctx
) ir_expression(ir_unop_i2u
, src
);
889 case GLSL_TYPE_FLOAT
:
890 result
= new(ctx
) ir_expression(ir_unop_f2u
, src
);
893 result
= new(ctx
) ir_expression(ir_unop_i2u
,
894 new(ctx
) ir_expression(ir_unop_b2i
,
897 case GLSL_TYPE_DOUBLE
:
898 result
= new(ctx
) ir_expression(ir_unop_d2u
, src
);
900 case GLSL_TYPE_UINT64
:
901 result
= new(ctx
) ir_expression(ir_unop_u642u
, src
);
903 case GLSL_TYPE_INT64
:
904 result
= new(ctx
) ir_expression(ir_unop_i642u
, src
);
906 case GLSL_TYPE_SAMPLER
:
907 result
= new(ctx
) ir_expression(ir_unop_unpack_sampler_2x32
, src
);
909 case GLSL_TYPE_IMAGE
:
910 result
= new(ctx
) ir_expression(ir_unop_unpack_image_2x32
, src
);
917 result
= new(ctx
) ir_expression(ir_unop_u2i
, src
);
919 case GLSL_TYPE_FLOAT
:
920 result
= new(ctx
) ir_expression(ir_unop_f2i
, src
);
923 result
= new(ctx
) ir_expression(ir_unop_b2i
, src
);
925 case GLSL_TYPE_DOUBLE
:
926 result
= new(ctx
) ir_expression(ir_unop_d2i
, src
);
928 case GLSL_TYPE_UINT64
:
929 result
= new(ctx
) ir_expression(ir_unop_u642i
, src
);
931 case GLSL_TYPE_INT64
:
932 result
= new(ctx
) ir_expression(ir_unop_i642i
, src
);
936 case GLSL_TYPE_FLOAT
:
939 result
= new(ctx
) ir_expression(ir_unop_u2f
, desired_type
, src
, NULL
);
942 result
= new(ctx
) ir_expression(ir_unop_i2f
, desired_type
, src
, NULL
);
945 result
= new(ctx
) ir_expression(ir_unop_b2f
, desired_type
, src
, NULL
);
947 case GLSL_TYPE_DOUBLE
:
948 result
= new(ctx
) ir_expression(ir_unop_d2f
, desired_type
, src
, NULL
);
950 case GLSL_TYPE_UINT64
:
951 result
= new(ctx
) ir_expression(ir_unop_u642f
, desired_type
, src
, NULL
);
953 case GLSL_TYPE_INT64
:
954 result
= new(ctx
) ir_expression(ir_unop_i642f
, desired_type
, src
, NULL
);
961 result
= new(ctx
) ir_expression(ir_unop_i2b
,
962 new(ctx
) ir_expression(ir_unop_u2i
,
966 result
= new(ctx
) ir_expression(ir_unop_i2b
, desired_type
, src
, NULL
);
968 case GLSL_TYPE_FLOAT
:
969 result
= new(ctx
) ir_expression(ir_unop_f2b
, desired_type
, src
, NULL
);
971 case GLSL_TYPE_DOUBLE
:
972 result
= new(ctx
) ir_expression(ir_unop_d2b
, desired_type
, src
, NULL
);
974 case GLSL_TYPE_UINT64
:
975 result
= new(ctx
) ir_expression(ir_unop_i642b
,
976 new(ctx
) ir_expression(ir_unop_u642i64
,
979 case GLSL_TYPE_INT64
:
980 result
= new(ctx
) ir_expression(ir_unop_i642b
, desired_type
, src
, NULL
);
984 case GLSL_TYPE_DOUBLE
:
987 result
= new(ctx
) ir_expression(ir_unop_i2d
, src
);
990 result
= new(ctx
) ir_expression(ir_unop_u2d
, src
);
993 result
= new(ctx
) ir_expression(ir_unop_f2d
,
994 new(ctx
) ir_expression(ir_unop_b2f
,
997 case GLSL_TYPE_FLOAT
:
998 result
= new(ctx
) ir_expression(ir_unop_f2d
, desired_type
, src
, NULL
);
1000 case GLSL_TYPE_UINT64
:
1001 result
= new(ctx
) ir_expression(ir_unop_u642d
, desired_type
, src
, NULL
);
1003 case GLSL_TYPE_INT64
:
1004 result
= new(ctx
) ir_expression(ir_unop_i642d
, desired_type
, src
, NULL
);
1008 case GLSL_TYPE_UINT64
:
1011 result
= new(ctx
) ir_expression(ir_unop_i2u64
, src
);
1013 case GLSL_TYPE_UINT
:
1014 result
= new(ctx
) ir_expression(ir_unop_u2u64
, src
);
1016 case GLSL_TYPE_BOOL
:
1017 result
= new(ctx
) ir_expression(ir_unop_i642u64
,
1018 new(ctx
) ir_expression(ir_unop_b2i64
,
1021 case GLSL_TYPE_FLOAT
:
1022 result
= new(ctx
) ir_expression(ir_unop_f2u64
, src
);
1024 case GLSL_TYPE_DOUBLE
:
1025 result
= new(ctx
) ir_expression(ir_unop_d2u64
, src
);
1027 case GLSL_TYPE_INT64
:
1028 result
= new(ctx
) ir_expression(ir_unop_i642u64
, src
);
1032 case GLSL_TYPE_INT64
:
1035 result
= new(ctx
) ir_expression(ir_unop_i2i64
, src
);
1037 case GLSL_TYPE_UINT
:
1038 result
= new(ctx
) ir_expression(ir_unop_u2i64
, src
);
1040 case GLSL_TYPE_BOOL
:
1041 result
= new(ctx
) ir_expression(ir_unop_b2i64
, src
);
1043 case GLSL_TYPE_FLOAT
:
1044 result
= new(ctx
) ir_expression(ir_unop_f2i64
, src
);
1046 case GLSL_TYPE_DOUBLE
:
1047 result
= new(ctx
) ir_expression(ir_unop_d2i64
, src
);
1049 case GLSL_TYPE_UINT64
:
1050 result
= new(ctx
) ir_expression(ir_unop_u642i64
, src
);
1054 case GLSL_TYPE_SAMPLER
:
1056 case GLSL_TYPE_UINT
:
1058 ir_expression(ir_unop_pack_sampler_2x32
, desired_type
, src
);
1062 case GLSL_TYPE_IMAGE
:
1064 case GLSL_TYPE_UINT
:
1066 ir_expression(ir_unop_pack_image_2x32
, desired_type
, src
);
1072 assert(result
!= NULL
);
1073 assert(result
->type
== desired_type
);
1075 /* Try constant folding; it may fold in the conversion we just added. */
1076 ir_constant
*const constant
= result
->constant_expression_value(ctx
);
1077 return (constant
!= NULL
) ? (ir_rvalue
*) constant
: (ir_rvalue
*) result
;
1082 * Perform automatic type and constant conversion of constructor parameters
1084 * This implements the rules in the "Implicit Conversions" rules, not the
1085 * "Conversion and Scalar Constructors".
1087 * After attempting the implicit conversion, an attempt to convert into a
1088 * constant valued expression is also done.
1090 * The \c from \c ir_rvalue is converted "in place".
1092 * \param from Operand that is being converted
1093 * \param to Base type the operand will be converted to
1094 * \param state GLSL compiler state
1097 * If the attempt to convert into a constant expression succeeds, \c true is
1098 * returned. Otherwise \c false is returned.
1101 implicitly_convert_component(ir_rvalue
* &from
, const glsl_base_type to
,
1102 struct _mesa_glsl_parse_state
*state
)
1104 void *mem_ctx
= state
;
1105 ir_rvalue
*result
= from
;
1107 if (to
!= from
->type
->base_type
) {
1108 const glsl_type
*desired_type
=
1109 glsl_type::get_instance(to
,
1110 from
->type
->vector_elements
,
1111 from
->type
->matrix_columns
);
1113 if (from
->type
->can_implicitly_convert_to(desired_type
, state
)) {
1114 /* Even though convert_component() implements the constructor
1115 * conversion rules (not the implicit conversion rules), its safe
1116 * to use it here because we already checked that the implicit
1117 * conversion is legal.
1119 result
= convert_component(from
, desired_type
);
1123 ir_rvalue
*const constant
= result
->constant_expression_value(mem_ctx
);
1125 if (constant
!= NULL
)
1128 if (from
!= result
) {
1129 from
->replace_with(result
);
1133 return constant
!= NULL
;
1138 * Dereference a specific component from a scalar, vector, or matrix
1141 dereference_component(ir_rvalue
*src
, unsigned component
)
1143 void *ctx
= ralloc_parent(src
);
1144 assert(component
< src
->type
->components());
1146 /* If the source is a constant, just create a new constant instead of a
1147 * dereference of the existing constant.
1149 ir_constant
*constant
= src
->as_constant();
1151 return new(ctx
) ir_constant(constant
, component
);
1153 if (src
->type
->is_scalar()) {
1155 } else if (src
->type
->is_vector()) {
1156 return new(ctx
) ir_swizzle(src
, component
, 0, 0, 0, 1);
1158 assert(src
->type
->is_matrix());
1160 /* Dereference a row of the matrix, then call this function again to get
1161 * a specific element from that row.
1163 const int c
= component
/ src
->type
->column_type()->vector_elements
;
1164 const int r
= component
% src
->type
->column_type()->vector_elements
;
1165 ir_constant
*const col_index
= new(ctx
) ir_constant(c
);
1166 ir_dereference
*const col
= new(ctx
) ir_dereference_array(src
,
1169 col
->type
= src
->type
->column_type();
1171 return dereference_component(col
, r
);
1174 assert(!"Should not get here.");
1180 process_vec_mat_constructor(exec_list
*instructions
,
1181 const glsl_type
*constructor_type
,
1182 YYLTYPE
*loc
, exec_list
*parameters
,
1183 struct _mesa_glsl_parse_state
*state
)
1187 /* The ARB_shading_language_420pack spec says:
1189 * "If an initializer is a list of initializers enclosed in curly braces,
1190 * the variable being declared must be a vector, a matrix, an array, or a
1193 * int i = { 1 }; // illegal, i is not an aggregate"
1195 if (constructor_type
->vector_elements
<= 1) {
1196 _mesa_glsl_error(loc
, state
, "aggregates can only initialize vectors, "
1197 "matrices, arrays, and structs");
1198 return ir_rvalue::error_value(ctx
);
1201 exec_list actual_parameters
;
1202 const unsigned parameter_count
=
1203 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
1205 if (parameter_count
== 0
1206 || (constructor_type
->is_vector() &&
1207 constructor_type
->vector_elements
!= parameter_count
)
1208 || (constructor_type
->is_matrix() &&
1209 constructor_type
->matrix_columns
!= parameter_count
)) {
1210 _mesa_glsl_error(loc
, state
, "%s constructor must have %u parameters",
1211 constructor_type
->is_vector() ? "vector" : "matrix",
1212 constructor_type
->vector_elements
);
1213 return ir_rvalue::error_value(ctx
);
1216 bool all_parameters_are_constant
= true;
1218 /* Type cast each parameter and, if possible, fold constants. */
1219 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1220 /* Apply implicit conversions (not the scalar constructor rules, see the
1221 * spec quote above!) and attempt to convert the parameter to a constant
1222 * valued expression. After doing so, track whether or not all the
1223 * parameters to the constructor are trivially constant valued
1226 all_parameters_are_constant
&=
1227 implicitly_convert_component(ir
, constructor_type
->base_type
, state
);
1229 if (constructor_type
->is_matrix()) {
1230 if (ir
->type
!= constructor_type
->column_type()) {
1231 _mesa_glsl_error(loc
, state
, "type error in matrix constructor: "
1232 "expected: %s, found %s",
1233 constructor_type
->column_type()->name
,
1235 return ir_rvalue::error_value(ctx
);
1237 } else if (ir
->type
!= constructor_type
->get_scalar_type()) {
1238 _mesa_glsl_error(loc
, state
, "type error in vector constructor: "
1239 "expected: %s, found %s",
1240 constructor_type
->get_scalar_type()->name
,
1242 return ir_rvalue::error_value(ctx
);
1246 if (all_parameters_are_constant
)
1247 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1249 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "vec_mat_ctor",
1251 instructions
->push_tail(var
);
1255 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
1256 ir_instruction
*assignment
= NULL
;
1258 if (var
->type
->is_matrix()) {
1260 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
1261 assignment
= new(ctx
) ir_assignment(lhs
, rhs
);
1263 /* use writemask rather than index for vector */
1264 assert(var
->type
->is_vector());
1266 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1267 assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
,
1268 (unsigned)(1 << i
));
1271 instructions
->push_tail(assignment
);
1276 return new(ctx
) ir_dereference_variable(var
);
1281 process_array_constructor(exec_list
*instructions
,
1282 const glsl_type
*constructor_type
,
1283 YYLTYPE
*loc
, exec_list
*parameters
,
1284 struct _mesa_glsl_parse_state
*state
)
1287 /* Array constructors come in two forms: sized and unsized. Sized array
1288 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
1289 * variables. In this case the number of parameters must exactly match the
1290 * specified size of the array.
1292 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
1293 * are vec4 variables. In this case the size of the array being constructed
1294 * is determined by the number of parameters.
1296 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
1298 * "There must be exactly the same number of arguments as the size of
1299 * the array being constructed. If no size is present in the
1300 * constructor, then the array is explicitly sized to the number of
1301 * arguments provided. The arguments are assigned in order, starting at
1302 * element 0, to the elements of the constructed array. Each argument
1303 * must be the same type as the element type of the array, or be a type
1304 * that can be converted to the element type of the array according to
1305 * Section 4.1.10 "Implicit Conversions.""
1307 exec_list actual_parameters
;
1308 const unsigned parameter_count
=
1309 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
1310 bool is_unsized_array
= constructor_type
->is_unsized_array();
1312 if ((parameter_count
== 0) ||
1313 (!is_unsized_array
&& (constructor_type
->length
!= parameter_count
))) {
1314 const unsigned min_param
= is_unsized_array
1315 ? 1 : constructor_type
->length
;
1317 _mesa_glsl_error(loc
, state
, "array constructor must have %s %u "
1319 is_unsized_array
? "at least" : "exactly",
1320 min_param
, (min_param
<= 1) ? "" : "s");
1321 return ir_rvalue::error_value(ctx
);
1324 if (is_unsized_array
) {
1326 glsl_type::get_array_instance(constructor_type
->fields
.array
,
1328 assert(constructor_type
!= NULL
);
1329 assert(constructor_type
->length
== parameter_count
);
1332 bool all_parameters_are_constant
= true;
1333 const glsl_type
*element_type
= constructor_type
->fields
.array
;
1335 /* Type cast each parameter and, if possible, fold constants. */
1336 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1337 /* Apply implicit conversions (not the scalar constructor rules, see the
1338 * spec quote above!) and attempt to convert the parameter to a constant
1339 * valued expression. After doing so, track whether or not all the
1340 * parameters to the constructor are trivially constant valued
1343 all_parameters_are_constant
&=
1344 implicitly_convert_component(ir
, element_type
->base_type
, state
);
1346 if (constructor_type
->fields
.array
->is_unsized_array()) {
1347 /* As the inner parameters of the constructor are created without
1348 * knowledge of each other we need to check to make sure unsized
1349 * parameters of unsized constructors all end up with the same size.
1351 * e.g we make sure to fail for a constructor like this:
1352 * vec4[][] a = vec4[][](vec4[](vec4(0.0), vec4(1.0)),
1353 * vec4[](vec4(0.0), vec4(1.0), vec4(1.0)),
1354 * vec4[](vec4(0.0), vec4(1.0)));
1356 if (element_type
->is_unsized_array()) {
1357 /* This is the first parameter so just get the type */
1358 element_type
= ir
->type
;
1359 } else if (element_type
!= ir
->type
) {
1360 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
1361 "expected: %s, found %s",
1364 return ir_rvalue::error_value(ctx
);
1366 } else if (ir
->type
!= constructor_type
->fields
.array
) {
1367 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
1368 "expected: %s, found %s",
1369 constructor_type
->fields
.array
->name
,
1371 return ir_rvalue::error_value(ctx
);
1373 element_type
= ir
->type
;
1377 if (constructor_type
->fields
.array
->is_unsized_array()) {
1379 glsl_type::get_array_instance(element_type
,
1381 assert(constructor_type
!= NULL
);
1382 assert(constructor_type
->length
== parameter_count
);
1385 if (all_parameters_are_constant
)
1386 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1388 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "array_ctor",
1390 instructions
->push_tail(var
);
1393 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
1394 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
1395 new(ctx
) ir_constant(i
));
1397 ir_instruction
*assignment
= new(ctx
) ir_assignment(lhs
, rhs
);
1398 instructions
->push_tail(assignment
);
1403 return new(ctx
) ir_dereference_variable(var
);
1408 * Determine if a list consists of a single scalar r-value
1411 single_scalar_parameter(exec_list
*parameters
)
1413 const ir_rvalue
*const p
= (ir_rvalue
*) parameters
->get_head_raw();
1414 assert(((ir_rvalue
*)p
)->as_rvalue() != NULL
);
1416 return (p
->type
->is_scalar() && p
->next
->is_tail_sentinel());
1421 * Generate inline code for a vector constructor
1423 * The generated constructor code will consist of a temporary variable
1424 * declaration of the same type as the constructor. A sequence of assignments
1425 * from constructor parameters to the temporary will follow.
1428 * An \c ir_dereference_variable of the temprorary generated in the constructor
1432 emit_inline_vector_constructor(const glsl_type
*type
,
1433 exec_list
*instructions
,
1434 exec_list
*parameters
,
1437 assert(!parameters
->is_empty());
1439 ir_variable
*var
= new(ctx
) ir_variable(type
, "vec_ctor", ir_var_temporary
);
1440 instructions
->push_tail(var
);
1442 /* There are three kinds of vector constructors.
1444 * - Construct a vector from a single scalar by replicating that scalar to
1445 * all components of the vector.
1447 * - Construct a vector from at least a matrix. This case should already
1448 * have been taken care of in ast_function_expression::hir by breaking
1449 * down the matrix into a series of column vectors.
1451 * - Construct a vector from an arbirary combination of vectors and
1452 * scalars. The components of the constructor parameters are assigned
1453 * to the vector in order until the vector is full.
1455 const unsigned lhs_components
= type
->components();
1456 if (single_scalar_parameter(parameters
)) {
1457 ir_rvalue
*first_param
= (ir_rvalue
*)parameters
->get_head_raw();
1458 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(first_param
, 0, 0, 0, 0,
1460 ir_dereference_variable
*lhs
= new(ctx
) ir_dereference_variable(var
);
1461 const unsigned mask
= (1U << lhs_components
) - 1;
1463 assert(rhs
->type
== lhs
->type
);
1465 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
, mask
);
1466 instructions
->push_tail(inst
);
1468 unsigned base_component
= 0;
1469 unsigned base_lhs_component
= 0;
1470 ir_constant_data data
;
1471 unsigned constant_mask
= 0, constant_components
= 0;
1473 memset(&data
, 0, sizeof(data
));
1475 foreach_in_list(ir_rvalue
, param
, parameters
) {
1476 unsigned rhs_components
= param
->type
->components();
1478 /* Do not try to assign more components to the vector than it has! */
1479 if ((rhs_components
+ base_lhs_component
) > lhs_components
) {
1480 rhs_components
= lhs_components
- base_lhs_component
;
1483 const ir_constant
*const c
= param
->as_constant();
1485 for (unsigned i
= 0; i
< rhs_components
; i
++) {
1486 switch (c
->type
->base_type
) {
1487 case GLSL_TYPE_UINT
:
1488 data
.u
[i
+ base_component
] = c
->get_uint_component(i
);
1491 data
.i
[i
+ base_component
] = c
->get_int_component(i
);
1493 case GLSL_TYPE_FLOAT
:
1494 data
.f
[i
+ base_component
] = c
->get_float_component(i
);
1496 case GLSL_TYPE_DOUBLE
:
1497 data
.d
[i
+ base_component
] = c
->get_double_component(i
);
1499 case GLSL_TYPE_BOOL
:
1500 data
.b
[i
+ base_component
] = c
->get_bool_component(i
);
1502 case GLSL_TYPE_UINT64
:
1503 data
.u64
[i
+ base_component
] = c
->get_uint64_component(i
);
1505 case GLSL_TYPE_INT64
:
1506 data
.i64
[i
+ base_component
] = c
->get_int64_component(i
);
1509 assert(!"Should not get here.");
1514 /* Mask of fields to be written in the assignment. */
1515 constant_mask
|= ((1U << rhs_components
) - 1) << base_lhs_component
;
1516 constant_components
+= rhs_components
;
1518 base_component
+= rhs_components
;
1520 /* Advance the component index by the number of components
1521 * that were just assigned.
1523 base_lhs_component
+= rhs_components
;
1526 if (constant_mask
!= 0) {
1527 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1528 const glsl_type
*rhs_type
=
1529 glsl_type::get_instance(var
->type
->base_type
,
1530 constant_components
,
1532 ir_rvalue
*rhs
= new(ctx
) ir_constant(rhs_type
, &data
);
1534 ir_instruction
*inst
=
1535 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, constant_mask
);
1536 instructions
->push_tail(inst
);
1540 foreach_in_list(ir_rvalue
, param
, parameters
) {
1541 unsigned rhs_components
= param
->type
->components();
1543 /* Do not try to assign more components to the vector than it has! */
1544 if ((rhs_components
+ base_component
) > lhs_components
) {
1545 rhs_components
= lhs_components
- base_component
;
1548 /* If we do not have any components left to copy, break out of the
1549 * loop. This can happen when initializing a vec4 with a mat3 as the
1550 * mat3 would have been broken into a series of column vectors.
1552 if (rhs_components
== 0) {
1556 const ir_constant
*const c
= param
->as_constant();
1558 /* Mask of fields to be written in the assignment. */
1559 const unsigned write_mask
= ((1U << rhs_components
) - 1)
1562 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1564 /* Generate a swizzle so that LHS and RHS sizes match. */
1566 new(ctx
) ir_swizzle(param
, 0, 1, 2, 3, rhs_components
);
1568 ir_instruction
*inst
=
1569 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1570 instructions
->push_tail(inst
);
1573 /* Advance the component index by the number of components that were
1576 base_component
+= rhs_components
;
1579 return new(ctx
) ir_dereference_variable(var
);
1584 * Generate assignment of a portion of a vector to a portion of a matrix column
1586 * \param src_base First component of the source to be used in assignment
1587 * \param column Column of destination to be assiged
1588 * \param row_base First component of the destination column to be assigned
1589 * \param count Number of components to be assigned
1592 * \c src_base + \c count must be less than or equal to the number of
1593 * components in the source vector.
1595 static ir_instruction
*
1596 assign_to_matrix_column(ir_variable
*var
, unsigned column
, unsigned row_base
,
1597 ir_rvalue
*src
, unsigned src_base
, unsigned count
,
1600 ir_constant
*col_idx
= new(mem_ctx
) ir_constant(column
);
1601 ir_dereference
*column_ref
= new(mem_ctx
) ir_dereference_array(var
,
1604 assert(column_ref
->type
->components() >= (row_base
+ count
));
1605 assert(src
->type
->components() >= (src_base
+ count
));
1607 /* Generate a swizzle that extracts the number of components from the source
1608 * that are to be assigned to the column of the matrix.
1610 if (count
< src
->type
->vector_elements
) {
1611 src
= new(mem_ctx
) ir_swizzle(src
,
1612 src_base
+ 0, src_base
+ 1,
1613 src_base
+ 2, src_base
+ 3,
1617 /* Mask of fields to be written in the assignment. */
1618 const unsigned write_mask
= ((1U << count
) - 1) << row_base
;
1620 return new(mem_ctx
) ir_assignment(column_ref
, src
, NULL
, write_mask
);
1625 * Generate inline code for a matrix constructor
1627 * The generated constructor code will consist of a temporary variable
1628 * declaration of the same type as the constructor. A sequence of assignments
1629 * from constructor parameters to the temporary will follow.
1632 * An \c ir_dereference_variable of the temprorary generated in the constructor
1636 emit_inline_matrix_constructor(const glsl_type
*type
,
1637 exec_list
*instructions
,
1638 exec_list
*parameters
,
1641 assert(!parameters
->is_empty());
1643 ir_variable
*var
= new(ctx
) ir_variable(type
, "mat_ctor", ir_var_temporary
);
1644 instructions
->push_tail(var
);
1646 /* There are three kinds of matrix constructors.
1648 * - Construct a matrix from a single scalar by replicating that scalar to
1649 * along the diagonal of the matrix and setting all other components to
1652 * - Construct a matrix from an arbirary combination of vectors and
1653 * scalars. The components of the constructor parameters are assigned
1654 * to the matrix in column-major order until the matrix is full.
1656 * - Construct a matrix from a single matrix. The source matrix is copied
1657 * to the upper left portion of the constructed matrix, and the remaining
1658 * elements take values from the identity matrix.
1660 ir_rvalue
*const first_param
= (ir_rvalue
*) parameters
->get_head_raw();
1661 if (single_scalar_parameter(parameters
)) {
1662 /* Assign the scalar to the X component of a vec4, and fill the remaining
1663 * components with zero.
1665 glsl_base_type param_base_type
= first_param
->type
->base_type
;
1666 assert(first_param
->type
->is_float() || first_param
->type
->is_double());
1667 ir_variable
*rhs_var
=
1668 new(ctx
) ir_variable(glsl_type::get_instance(param_base_type
, 4, 1),
1671 instructions
->push_tail(rhs_var
);
1673 ir_constant_data zero
;
1674 for (unsigned i
= 0; i
< 4; i
++)
1675 if (first_param
->type
->is_float())
1680 ir_instruction
*inst
=
1681 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(rhs_var
),
1682 new(ctx
) ir_constant(rhs_var
->type
, &zero
));
1683 instructions
->push_tail(inst
);
1685 ir_dereference
*const rhs_ref
=
1686 new(ctx
) ir_dereference_variable(rhs_var
);
1688 inst
= new(ctx
) ir_assignment(rhs_ref
, first_param
, NULL
, 0x01);
1689 instructions
->push_tail(inst
);
1691 /* Assign the temporary vector to each column of the destination matrix
1692 * with a swizzle that puts the X component on the diagonal of the
1693 * matrix. In some cases this may mean that the X component does not
1694 * get assigned into the column at all (i.e., when the matrix has more
1695 * columns than rows).
1697 static const unsigned rhs_swiz
[4][4] = {
1704 const unsigned cols_to_init
= MIN2(type
->matrix_columns
,
1705 type
->vector_elements
);
1706 for (unsigned i
= 0; i
< cols_to_init
; i
++) {
1707 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1708 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
,
1711 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1712 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, rhs_swiz
[i
],
1713 type
->vector_elements
);
1715 inst
= new(ctx
) ir_assignment(col_ref
, rhs
);
1716 instructions
->push_tail(inst
);
1719 for (unsigned i
= cols_to_init
; i
< type
->matrix_columns
; i
++) {
1720 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1721 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
,
1724 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1725 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, 1, 1, 1, 1,
1726 type
->vector_elements
);
1728 inst
= new(ctx
) ir_assignment(col_ref
, rhs
);
1729 instructions
->push_tail(inst
);
1731 } else if (first_param
->type
->is_matrix()) {
1732 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
1734 * "If a matrix is constructed from a matrix, then each component
1735 * (column i, row j) in the result that has a corresponding
1736 * component (column i, row j) in the argument will be initialized
1737 * from there. All other components will be initialized to the
1738 * identity matrix. If a matrix argument is given to a matrix
1739 * constructor, it is an error to have any other arguments."
1741 assert(first_param
->next
->is_tail_sentinel());
1742 ir_rvalue
*const src_matrix
= first_param
;
1744 /* If the source matrix is smaller, pre-initialize the relavent parts of
1745 * the destination matrix to the identity matrix.
1747 if ((src_matrix
->type
->matrix_columns
< var
->type
->matrix_columns
) ||
1748 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)) {
1750 /* If the source matrix has fewer rows, every column of the
1751 * destination must be initialized. Otherwise only the columns in
1752 * the destination that do not exist in the source must be
1756 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)
1757 ? 0 : src_matrix
->type
->matrix_columns
;
1759 const glsl_type
*const col_type
= var
->type
->column_type();
1760 for (/* empty */; col
< var
->type
->matrix_columns
; col
++) {
1761 ir_constant_data ident
;
1763 if (!col_type
->is_double()) {
1768 ident
.f
[col
] = 1.0f
;
1777 ir_rvalue
*const rhs
= new(ctx
) ir_constant(col_type
, &ident
);
1779 ir_rvalue
*const lhs
=
1780 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(col
));
1782 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
);
1783 instructions
->push_tail(inst
);
1787 /* Assign columns from the source matrix to the destination matrix.
1789 * Since the parameter will be used in the RHS of multiple assignments,
1790 * generate a temporary and copy the paramter there.
1792 ir_variable
*const rhs_var
=
1793 new(ctx
) ir_variable(first_param
->type
, "mat_ctor_mat",
1795 instructions
->push_tail(rhs_var
);
1797 ir_dereference
*const rhs_var_ref
=
1798 new(ctx
) ir_dereference_variable(rhs_var
);
1799 ir_instruction
*const inst
=
1800 new(ctx
) ir_assignment(rhs_var_ref
, first_param
);
1801 instructions
->push_tail(inst
);
1803 const unsigned last_row
= MIN2(src_matrix
->type
->vector_elements
,
1804 var
->type
->vector_elements
);
1805 const unsigned last_col
= MIN2(src_matrix
->type
->matrix_columns
,
1806 var
->type
->matrix_columns
);
1808 unsigned swiz
[4] = { 0, 0, 0, 0 };
1809 for (unsigned i
= 1; i
< last_row
; i
++)
1812 const unsigned write_mask
= (1U << last_row
) - 1;
1814 for (unsigned i
= 0; i
< last_col
; i
++) {
1815 ir_dereference
*const lhs
=
1816 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
1817 ir_rvalue
*const rhs_col
=
1818 new(ctx
) ir_dereference_array(rhs_var
, new(ctx
) ir_constant(i
));
1820 /* If one matrix has columns that are smaller than the columns of the
1821 * other matrix, wrap the column access of the larger with a swizzle
1822 * so that the LHS and RHS of the assignment have the same size (and
1823 * therefore have the same type).
1825 * It would be perfectly valid to unconditionally generate the
1826 * swizzles, this this will typically result in a more compact IR
1830 if (lhs
->type
->vector_elements
!= rhs_col
->type
->vector_elements
) {
1831 rhs
= new(ctx
) ir_swizzle(rhs_col
, swiz
, last_row
);
1836 ir_instruction
*inst
=
1837 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1838 instructions
->push_tail(inst
);
1841 const unsigned cols
= type
->matrix_columns
;
1842 const unsigned rows
= type
->vector_elements
;
1843 unsigned remaining_slots
= rows
* cols
;
1844 unsigned col_idx
= 0;
1845 unsigned row_idx
= 0;
1847 foreach_in_list(ir_rvalue
, rhs
, parameters
) {
1848 unsigned rhs_components
= rhs
->type
->components();
1849 unsigned rhs_base
= 0;
1851 if (remaining_slots
== 0)
1854 /* Since the parameter might be used in the RHS of two assignments,
1855 * generate a temporary and copy the paramter there.
1857 ir_variable
*rhs_var
=
1858 new(ctx
) ir_variable(rhs
->type
, "mat_ctor_vec", ir_var_temporary
);
1859 instructions
->push_tail(rhs_var
);
1861 ir_dereference
*rhs_var_ref
=
1862 new(ctx
) ir_dereference_variable(rhs_var
);
1863 ir_instruction
*inst
= new(ctx
) ir_assignment(rhs_var_ref
, rhs
);
1864 instructions
->push_tail(inst
);
1867 /* Assign the current parameter to as many components of the matrix
1870 * NOTE: A single vector parameter can span two matrix columns. A
1871 * single vec4, for example, can completely fill a mat2.
1873 unsigned count
= MIN2(rows
- row_idx
,
1874 rhs_components
- rhs_base
);
1876 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1877 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
1882 instructions
->push_tail(inst
);
1885 remaining_slots
-= count
;
1887 /* Sometimes, there is still data left in the parameters and
1888 * components left to be set in the destination but in other
1891 if (row_idx
>= rows
) {
1895 } while(remaining_slots
> 0 && rhs_base
< rhs_components
);
1899 return new(ctx
) ir_dereference_variable(var
);
1904 emit_inline_record_constructor(const glsl_type
*type
,
1905 exec_list
*instructions
,
1906 exec_list
*parameters
,
1909 ir_variable
*const var
=
1910 new(mem_ctx
) ir_variable(type
, "record_ctor", ir_var_temporary
);
1911 ir_dereference_variable
*const d
=
1912 new(mem_ctx
) ir_dereference_variable(var
);
1914 instructions
->push_tail(var
);
1916 exec_node
*node
= parameters
->get_head_raw();
1917 for (unsigned i
= 0; i
< type
->length
; i
++) {
1918 assert(!node
->is_tail_sentinel());
1920 ir_dereference
*const lhs
=
1921 new(mem_ctx
) ir_dereference_record(d
->clone(mem_ctx
, NULL
),
1922 type
->fields
.structure
[i
].name
);
1924 ir_rvalue
*const rhs
= ((ir_instruction
*) node
)->as_rvalue();
1925 assert(rhs
!= NULL
);
1927 ir_instruction
*const assign
= new(mem_ctx
) ir_assignment(lhs
, rhs
);
1929 instructions
->push_tail(assign
);
1938 process_record_constructor(exec_list
*instructions
,
1939 const glsl_type
*constructor_type
,
1940 YYLTYPE
*loc
, exec_list
*parameters
,
1941 struct _mesa_glsl_parse_state
*state
)
1944 /* From page 32 (page 38 of the PDF) of the GLSL 1.20 spec:
1946 * "The arguments to the constructor will be used to set the structure's
1947 * fields, in order, using one argument per field. Each argument must
1948 * be the same type as the field it sets, or be a type that can be
1949 * converted to the field's type according to Section 4.1.10 “Implicit
1952 * From page 35 (page 41 of the PDF) of the GLSL 4.20 spec:
1954 * "In all cases, the innermost initializer (i.e., not a list of
1955 * initializers enclosed in curly braces) applied to an object must
1956 * have the same type as the object being initialized or be a type that
1957 * can be converted to the object's type according to section 4.1.10
1958 * "Implicit Conversions". In the latter case, an implicit conversion
1959 * will be done on the initializer before the assignment is done."
1961 exec_list actual_parameters
;
1963 const unsigned parameter_count
=
1964 process_parameters(instructions
, &actual_parameters
, parameters
,
1967 if (parameter_count
!= constructor_type
->length
) {
1968 _mesa_glsl_error(loc
, state
,
1969 "%s parameters in constructor for `%s'",
1970 parameter_count
> constructor_type
->length
1971 ? "too many": "insufficient",
1972 constructor_type
->name
);
1973 return ir_rvalue::error_value(ctx
);
1976 bool all_parameters_are_constant
= true;
1979 /* Type cast each parameter and, if possible, fold constants. */
1980 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1982 const glsl_struct_field
*struct_field
=
1983 &constructor_type
->fields
.structure
[i
];
1985 /* Apply implicit conversions (not the scalar constructor rules, see the
1986 * spec quote above!) and attempt to convert the parameter to a constant
1987 * valued expression. After doing so, track whether or not all the
1988 * parameters to the constructor are trivially constant valued
1991 all_parameters_are_constant
&=
1992 implicitly_convert_component(ir
, struct_field
->type
->base_type
,
1995 if (ir
->type
!= struct_field
->type
) {
1996 _mesa_glsl_error(loc
, state
,
1997 "parameter type mismatch in constructor for `%s.%s' "
1999 constructor_type
->name
,
2002 struct_field
->type
->name
);
2003 return ir_rvalue::error_value(ctx
);
2009 if (all_parameters_are_constant
) {
2010 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
2012 return emit_inline_record_constructor(constructor_type
, instructions
,
2013 &actual_parameters
, state
);
2018 ast_function_expression::handle_method(exec_list
*instructions
,
2019 struct _mesa_glsl_parse_state
*state
)
2021 const ast_expression
*field
= subexpressions
[0];
2025 /* Handle "method calls" in GLSL 1.20 - namely, array.length() */
2026 YYLTYPE loc
= get_location();
2027 state
->check_version(120, 300, &loc
, "methods not supported");
2030 method
= field
->primary_expression
.identifier
;
2032 /* This would prevent to raise "uninitialized variable" warnings when
2033 * calling array.length.
2035 field
->subexpressions
[0]->set_is_lhs(true);
2036 op
= field
->subexpressions
[0]->hir(instructions
, state
);
2037 if (strcmp(method
, "length") == 0) {
2038 if (!this->expressions
.is_empty()) {
2039 _mesa_glsl_error(&loc
, state
, "length method takes no arguments");
2043 if (op
->type
->is_array()) {
2044 if (op
->type
->is_unsized_array()) {
2045 if (!state
->has_shader_storage_buffer_objects()) {
2046 _mesa_glsl_error(&loc
, state
,
2047 "length called on unsized array"
2048 " only available with"
2049 " ARB_shader_storage_buffer_object");
2051 /* Calculate length of an unsized array in run-time */
2052 result
= new(ctx
) ir_expression(ir_unop_ssbo_unsized_array_length
,
2055 result
= new(ctx
) ir_constant(op
->type
->array_size());
2057 } else if (op
->type
->is_vector()) {
2058 if (state
->has_420pack()) {
2059 /* .length() returns int. */
2060 result
= new(ctx
) ir_constant((int) op
->type
->vector_elements
);
2062 _mesa_glsl_error(&loc
, state
, "length method on matrix only"
2063 " available with ARB_shading_language_420pack");
2066 } else if (op
->type
->is_matrix()) {
2067 if (state
->has_420pack()) {
2068 /* .length() returns int. */
2069 result
= new(ctx
) ir_constant((int) op
->type
->matrix_columns
);
2071 _mesa_glsl_error(&loc
, state
, "length method on matrix only"
2072 " available with ARB_shading_language_420pack");
2076 _mesa_glsl_error(&loc
, state
, "length called on scalar.");
2080 _mesa_glsl_error(&loc
, state
, "unknown method: `%s'", method
);
2085 return ir_rvalue::error_value(ctx
);
2088 static inline bool is_valid_constructor(const glsl_type
*type
,
2089 struct _mesa_glsl_parse_state
*state
)
2091 return type
->is_numeric() || type
->is_boolean() ||
2092 (state
->has_bindless() && (type
->is_sampler() || type
->is_image()));
2096 ast_function_expression::hir(exec_list
*instructions
,
2097 struct _mesa_glsl_parse_state
*state
)
2100 /* There are three sorts of function calls.
2102 * 1. constructors - The first subexpression is an ast_type_specifier.
2103 * 2. methods - Only the .length() method of array types.
2104 * 3. functions - Calls to regular old functions.
2107 if (is_constructor()) {
2108 const ast_type_specifier
*type
=
2109 (ast_type_specifier
*) subexpressions
[0];
2110 YYLTYPE loc
= type
->get_location();
2113 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
2115 /* constructor_type can be NULL if a variable with the same name as the
2116 * structure has come into scope.
2118 if (constructor_type
== NULL
) {
2119 _mesa_glsl_error(& loc
, state
, "unknown type `%s' (structure name "
2120 "may be shadowed by a variable with the same name)",
2122 return ir_rvalue::error_value(ctx
);
2126 /* Constructors for opaque types are illegal.
2128 * From section 4.1.7 of the ARB_bindless_texture spec:
2130 * "Samplers are represented using 64-bit integer handles, and may be "
2131 * converted to and from 64-bit integers using constructors."
2133 * From section 4.1.X of the ARB_bindless_texture spec:
2135 * "Images are represented using 64-bit integer handles, and may be
2136 * converted to and from 64-bit integers using constructors."
2138 if (constructor_type
->contains_atomic() ||
2139 (!state
->has_bindless() && constructor_type
->contains_opaque())) {
2140 _mesa_glsl_error(& loc
, state
, "cannot construct %s type `%s'",
2141 state
->has_bindless() ? "atomic" : "opaque",
2142 constructor_type
->name
);
2143 return ir_rvalue::error_value(ctx
);
2146 if (constructor_type
->is_subroutine()) {
2147 _mesa_glsl_error(& loc
, state
,
2148 "subroutine name cannot be a constructor `%s'",
2149 constructor_type
->name
);
2150 return ir_rvalue::error_value(ctx
);
2153 if (constructor_type
->is_array()) {
2154 if (!state
->check_version(state
->allow_glsl_120_subset_in_110
? 110 : 120,
2155 300, &loc
, "array constructors forbidden")) {
2156 return ir_rvalue::error_value(ctx
);
2159 return process_array_constructor(instructions
, constructor_type
,
2160 & loc
, &this->expressions
, state
);
2164 /* There are two kinds of constructor calls. Constructors for arrays and
2165 * structures must have the exact number of arguments with matching types
2166 * in the correct order. These constructors follow essentially the same
2167 * type matching rules as functions.
2169 * Constructors for built-in language types, such as mat4 and vec2, are
2170 * free form. The only requirements are that the parameters must provide
2171 * enough values of the correct scalar type and that no arguments are
2172 * given past the last used argument.
2174 * When using the C-style initializer syntax from GLSL 4.20, constructors
2175 * must have the exact number of arguments with matching types in the
2178 if (constructor_type
->is_struct()) {
2179 return process_record_constructor(instructions
, constructor_type
,
2180 &loc
, &this->expressions
,
2184 if (!is_valid_constructor(constructor_type
, state
))
2185 return ir_rvalue::error_value(ctx
);
2187 /* Total number of components of the type being constructed. */
2188 const unsigned type_components
= constructor_type
->components();
2190 /* Number of components from parameters that have actually been
2191 * consumed. This is used to perform several kinds of error checking.
2193 unsigned components_used
= 0;
2195 unsigned matrix_parameters
= 0;
2196 unsigned nonmatrix_parameters
= 0;
2197 exec_list actual_parameters
;
2199 foreach_list_typed(ast_node
, ast
, link
, &this->expressions
) {
2200 ir_rvalue
*result
= ast
->hir(instructions
, state
);
2202 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
2204 * "It is an error to provide extra arguments beyond this
2205 * last used argument."
2207 if (components_used
>= type_components
) {
2208 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
2210 constructor_type
->name
);
2211 return ir_rvalue::error_value(ctx
);
2214 if (!is_valid_constructor(result
->type
, state
)) {
2215 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
2216 "non-numeric data type",
2217 constructor_type
->name
);
2218 return ir_rvalue::error_value(ctx
);
2221 /* Count the number of matrix and nonmatrix parameters. This
2222 * is used below to enforce some of the constructor rules.
2224 if (result
->type
->is_matrix())
2225 matrix_parameters
++;
2227 nonmatrix_parameters
++;
2229 actual_parameters
.push_tail(result
);
2230 components_used
+= result
->type
->components();
2233 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
2235 * "It is an error to construct matrices from other matrices. This
2236 * is reserved for future use."
2238 if (matrix_parameters
> 0
2239 && constructor_type
->is_matrix()
2240 && !state
->check_version(120, 100, &loc
,
2241 "cannot construct `%s' from a matrix",
2242 constructor_type
->name
)) {
2243 return ir_rvalue::error_value(ctx
);
2246 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
2248 * "If a matrix argument is given to a matrix constructor, it is
2249 * an error to have any other arguments."
2251 if ((matrix_parameters
> 0)
2252 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
2253 && constructor_type
->is_matrix()) {
2254 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
2255 "matrix must be only parameter",
2256 constructor_type
->name
);
2257 return ir_rvalue::error_value(ctx
);
2260 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
2262 * "In these cases, there must be enough components provided in the
2263 * arguments to provide an initializer for every component in the
2264 * constructed value."
2266 if (components_used
< type_components
&& components_used
!= 1
2267 && matrix_parameters
== 0) {
2268 _mesa_glsl_error(& loc
, state
, "too few components to construct "
2270 constructor_type
->name
);
2271 return ir_rvalue::error_value(ctx
);
2274 /* Matrices can never be consumed as is by any constructor but matrix
2275 * constructors. If the constructor type is not matrix, always break the
2276 * matrix up into a series of column vectors.
2278 if (!constructor_type
->is_matrix()) {
2279 foreach_in_list_safe(ir_rvalue
, matrix
, &actual_parameters
) {
2280 if (!matrix
->type
->is_matrix())
2283 /* Create a temporary containing the matrix. */
2284 ir_variable
*var
= new(ctx
) ir_variable(matrix
->type
, "matrix_tmp",
2286 instructions
->push_tail(var
);
2287 instructions
->push_tail(
2288 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
2290 var
->constant_value
= matrix
->constant_expression_value(ctx
);
2292 /* Replace the matrix with dereferences of its columns. */
2293 for (int i
= 0; i
< matrix
->type
->matrix_columns
; i
++) {
2294 matrix
->insert_before(
2295 new (ctx
) ir_dereference_array(var
,
2296 new(ctx
) ir_constant(i
)));
2302 bool all_parameters_are_constant
= true;
2304 /* Type cast each parameter and, if possible, fold constants.*/
2305 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
2306 const glsl_type
*desired_type
;
2308 /* From section 5.4.1 of the ARB_bindless_texture spec:
2310 * "In the following four constructors, the low 32 bits of the sampler
2311 * type correspond to the .x component of the uvec2 and the high 32
2312 * bits correspond to the .y component."
2314 * uvec2(any sampler type) // Converts a sampler type to a
2315 * // pair of 32-bit unsigned integers
2316 * any sampler type(uvec2) // Converts a pair of 32-bit unsigned integers to
2318 * uvec2(any image type) // Converts an image type to a
2319 * // pair of 32-bit unsigned integers
2320 * any image type(uvec2) // Converts a pair of 32-bit unsigned integers to
2323 if (ir
->type
->is_sampler() || ir
->type
->is_image()) {
2324 /* Convert a sampler/image type to a pair of 32-bit unsigned
2325 * integers as defined by ARB_bindless_texture.
2327 if (constructor_type
!= glsl_type::uvec2_type
) {
2328 _mesa_glsl_error(&loc
, state
, "sampler and image types can only "
2329 "be converted to a pair of 32-bit unsigned "
2332 desired_type
= glsl_type::uvec2_type
;
2333 } else if (constructor_type
->is_sampler() ||
2334 constructor_type
->is_image()) {
2335 /* Convert a pair of 32-bit unsigned integers to a sampler or image
2336 * type as defined by ARB_bindless_texture.
2338 if (ir
->type
!= glsl_type::uvec2_type
) {
2339 _mesa_glsl_error(&loc
, state
, "sampler and image types can only "
2340 "be converted from a pair of 32-bit unsigned "
2343 desired_type
= constructor_type
;
2346 glsl_type::get_instance(constructor_type
->base_type
,
2347 ir
->type
->vector_elements
,
2348 ir
->type
->matrix_columns
);
2351 ir_rvalue
*result
= convert_component(ir
, desired_type
);
2353 /* Attempt to convert the parameter to a constant valued expression.
2354 * After doing so, track whether or not all the parameters to the
2355 * constructor are trivially constant valued expressions.
2357 ir_rvalue
*const constant
= result
->constant_expression_value(ctx
);
2359 if (constant
!= NULL
)
2362 all_parameters_are_constant
= false;
2365 ir
->replace_with(result
);
2369 /* If all of the parameters are trivially constant, create a
2370 * constant representing the complete collection of parameters.
2372 if (all_parameters_are_constant
) {
2373 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
2374 } else if (constructor_type
->is_scalar()) {
2375 return dereference_component((ir_rvalue
*)
2376 actual_parameters
.get_head_raw(),
2378 } else if (constructor_type
->is_vector()) {
2379 return emit_inline_vector_constructor(constructor_type
,
2384 assert(constructor_type
->is_matrix());
2385 return emit_inline_matrix_constructor(constructor_type
,
2390 } else if (subexpressions
[0]->oper
== ast_field_selection
) {
2391 return handle_method(instructions
, state
);
2393 const ast_expression
*id
= subexpressions
[0];
2394 const char *func_name
= NULL
;
2395 YYLTYPE loc
= get_location();
2396 exec_list actual_parameters
;
2397 ir_variable
*sub_var
= NULL
;
2398 ir_rvalue
*array_idx
= NULL
;
2400 process_parameters(instructions
, &actual_parameters
, &this->expressions
,
2403 if (id
->oper
== ast_array_index
) {
2404 array_idx
= generate_array_index(ctx
, instructions
, state
, loc
,
2405 id
->subexpressions
[0],
2406 id
->subexpressions
[1], &func_name
,
2407 &actual_parameters
);
2408 } else if (id
->oper
== ast_identifier
) {
2409 func_name
= id
->primary_expression
.identifier
;
2411 _mesa_glsl_error(&loc
, state
, "function name is not an identifier");
2414 /* an error was emitted earlier */
2416 return ir_rvalue::error_value(ctx
);
2418 ir_function_signature
*sig
=
2419 match_function_by_name(func_name
, &actual_parameters
, state
);
2421 ir_rvalue
*value
= NULL
;
2423 sig
= match_subroutine_by_name(func_name
, &actual_parameters
,
2428 no_matching_function_error(func_name
, &loc
,
2429 &actual_parameters
, state
);
2430 value
= ir_rvalue::error_value(ctx
);
2431 } else if (!verify_parameter_modes(state
, sig
,
2433 this->expressions
)) {
2434 /* an error has already been emitted */
2435 value
= ir_rvalue::error_value(ctx
);
2436 } else if (sig
->is_builtin() && strcmp(func_name
, "ftransform") == 0) {
2437 /* ftransform refers to global variables, and we don't have any code
2438 * for remapping the variable references in the built-in shader.
2441 state
->symbols
->get_variable("gl_ModelViewProjectionMatrix");
2442 ir_variable
*vtx
= state
->symbols
->get_variable("gl_Vertex");
2443 value
= new(ctx
) ir_expression(ir_binop_mul
, glsl_type::vec4_type
,
2444 new(ctx
) ir_dereference_variable(mvp
),
2445 new(ctx
) ir_dereference_variable(vtx
));
2447 bool is_begin_interlock
= false;
2448 bool is_end_interlock
= false;
2449 if (sig
->is_builtin() &&
2450 state
->stage
== MESA_SHADER_FRAGMENT
&&
2451 state
->ARB_fragment_shader_interlock_enable
) {
2452 is_begin_interlock
= strcmp(func_name
, "beginInvocationInterlockARB") == 0;
2453 is_end_interlock
= strcmp(func_name
, "endInvocationInterlockARB") == 0;
2456 if (sig
->is_builtin() &&
2457 ((state
->stage
== MESA_SHADER_TESS_CTRL
&&
2458 strcmp(func_name
, "barrier") == 0) ||
2459 is_begin_interlock
|| is_end_interlock
)) {
2460 if (state
->current_function
== NULL
||
2461 strcmp(state
->current_function
->function_name(), "main") != 0) {
2462 _mesa_glsl_error(&loc
, state
,
2463 "%s() may only be used in main()", func_name
);
2466 if (state
->found_return
) {
2467 _mesa_glsl_error(&loc
, state
,
2468 "%s() may not be used after return", func_name
);
2471 if (instructions
!= &state
->current_function
->body
) {
2472 _mesa_glsl_error(&loc
, state
,
2473 "%s() may not be used in control flow", func_name
);
2477 /* There can be only one begin/end interlock pair in the function. */
2478 if (is_begin_interlock
) {
2479 if (state
->found_begin_interlock
)
2480 _mesa_glsl_error(&loc
, state
,
2481 "beginInvocationInterlockARB may not be used twice");
2482 state
->found_begin_interlock
= true;
2483 } else if (is_end_interlock
) {
2484 if (!state
->found_begin_interlock
)
2485 _mesa_glsl_error(&loc
, state
,
2486 "endInvocationInterlockARB may not be used "
2487 "before beginInvocationInterlockARB");
2488 if (state
->found_end_interlock
)
2489 _mesa_glsl_error(&loc
, state
,
2490 "endInvocationInterlockARB may not be used twice");
2491 state
->found_end_interlock
= true;
2494 value
= generate_call(instructions
, sig
, &actual_parameters
, sub_var
,
2497 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::void_type
,
2500 instructions
->push_tail(tmp
);
2501 value
= new(ctx
) ir_dereference_variable(tmp
);
2508 unreachable("not reached");
2512 ast_function_expression::has_sequence_subexpression() const
2514 foreach_list_typed(const ast_node
, ast
, link
, &this->expressions
) {
2515 if (ast
->has_sequence_subexpression())
2523 ast_aggregate_initializer::hir(exec_list
*instructions
,
2524 struct _mesa_glsl_parse_state
*state
)
2527 YYLTYPE loc
= this->get_location();
2529 if (!this->constructor_type
) {
2530 _mesa_glsl_error(&loc
, state
, "type of C-style initializer unknown");
2531 return ir_rvalue::error_value(ctx
);
2533 const glsl_type
*const constructor_type
= this->constructor_type
;
2535 if (!state
->has_420pack()) {
2536 _mesa_glsl_error(&loc
, state
, "C-style initialization requires the "
2537 "GL_ARB_shading_language_420pack extension");
2538 return ir_rvalue::error_value(ctx
);
2541 if (constructor_type
->is_array()) {
2542 return process_array_constructor(instructions
, constructor_type
, &loc
,
2543 &this->expressions
, state
);
2546 if (constructor_type
->is_struct()) {
2547 return process_record_constructor(instructions
, constructor_type
, &loc
,
2548 &this->expressions
, state
);
2551 return process_vec_mat_constructor(instructions
, constructor_type
, &loc
,
2552 &this->expressions
, state
);