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/core.h" /* for MIN2 */
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
)
42 foreach_list_typed(ast_node
, ast
, link
, parameters
) {
43 /* We need to process the parameters first in order to know if we can
44 * raise or not a unitialized warning. Calling set_is_lhs silence the
45 * warning for now. Raising the warning or not will be checked at
46 * verify_parameter_modes.
48 ast
->set_is_lhs(true);
49 ir_rvalue
*result
= ast
->hir(instructions
, state
);
51 ir_constant
*const constant
= result
->constant_expression_value();
55 actual_parameters
->push_tail(result
);
64 * Generate a source prototype for a function signature
66 * \param return_type Return type of the function. May be \c NULL.
67 * \param name Name of the function.
68 * \param parameters List of \c ir_instruction nodes representing the
69 * parameter list for the function. This may be either a
70 * formal (\c ir_variable) or actual (\c ir_rvalue)
71 * parameter list. Only the type is used.
74 * A ralloced string representing the prototype of the function.
77 prototype_string(const glsl_type
*return_type
, const char *name
,
78 exec_list
*parameters
)
82 if (return_type
!= NULL
)
83 str
= ralloc_asprintf(NULL
, "%s ", return_type
->name
);
85 ralloc_asprintf_append(&str
, "%s(", name
);
87 const char *comma
= "";
88 foreach_in_list(const ir_variable
, param
, parameters
) {
89 ralloc_asprintf_append(&str
, "%s%s", comma
, param
->type
->name
);
93 ralloc_strcat(&str
, ")");
98 verify_image_parameter(YYLTYPE
*loc
, _mesa_glsl_parse_state
*state
,
99 const ir_variable
*formal
, const ir_variable
*actual
)
102 * From the ARB_shader_image_load_store specification:
104 * "The values of image variables qualified with coherent,
105 * volatile, restrict, readonly, or writeonly may not be passed
106 * to functions whose formal parameters lack such
107 * qualifiers. [...] It is legal to have additional qualifiers
108 * on a formal parameter, but not to have fewer."
110 if (actual
->data
.image_coherent
&& !formal
->data
.image_coherent
) {
111 _mesa_glsl_error(loc
, state
,
112 "function call parameter `%s' drops "
113 "`coherent' qualifier", formal
->name
);
117 if (actual
->data
.image_volatile
&& !formal
->data
.image_volatile
) {
118 _mesa_glsl_error(loc
, state
,
119 "function call parameter `%s' drops "
120 "`volatile' qualifier", formal
->name
);
124 if (actual
->data
.image_restrict
&& !formal
->data
.image_restrict
) {
125 _mesa_glsl_error(loc
, state
,
126 "function call parameter `%s' drops "
127 "`restrict' qualifier", formal
->name
);
131 if (actual
->data
.image_read_only
&& !formal
->data
.image_read_only
) {
132 _mesa_glsl_error(loc
, state
,
133 "function call parameter `%s' drops "
134 "`readonly' qualifier", formal
->name
);
138 if (actual
->data
.image_write_only
&& !formal
->data
.image_write_only
) {
139 _mesa_glsl_error(loc
, state
,
140 "function call parameter `%s' drops "
141 "`writeonly' qualifier", formal
->name
);
149 verify_first_atomic_parameter(YYLTYPE
*loc
, _mesa_glsl_parse_state
*state
,
153 (!var
->is_in_shader_storage_block() &&
154 var
->data
.mode
!= ir_var_shader_shared
)) {
155 _mesa_glsl_error(loc
, state
, "First argument to atomic function "
156 "must be a buffer or shared variable");
163 is_atomic_function(const char *func_name
)
165 return !strcmp(func_name
, "atomicAdd") ||
166 !strcmp(func_name
, "atomicMin") ||
167 !strcmp(func_name
, "atomicMax") ||
168 !strcmp(func_name
, "atomicAnd") ||
169 !strcmp(func_name
, "atomicOr") ||
170 !strcmp(func_name
, "atomicXor") ||
171 !strcmp(func_name
, "atomicExchange") ||
172 !strcmp(func_name
, "atomicCompSwap");
176 * Verify that 'out' and 'inout' actual parameters are lvalues. Also, verify
177 * that 'const_in' formal parameters (an extension in our IR) correspond to
178 * ir_constant actual parameters.
181 verify_parameter_modes(_mesa_glsl_parse_state
*state
,
182 ir_function_signature
*sig
,
183 exec_list
&actual_ir_parameters
,
184 exec_list
&actual_ast_parameters
)
186 exec_node
*actual_ir_node
= actual_ir_parameters
.get_head_raw();
187 exec_node
*actual_ast_node
= actual_ast_parameters
.get_head_raw();
189 foreach_in_list(const ir_variable
, formal
, &sig
->parameters
) {
190 /* The lists must be the same length. */
191 assert(!actual_ir_node
->is_tail_sentinel());
192 assert(!actual_ast_node
->is_tail_sentinel());
194 const ir_rvalue
*const actual
= (ir_rvalue
*) actual_ir_node
;
195 const ast_expression
*const actual_ast
=
196 exec_node_data(ast_expression
, actual_ast_node
, link
);
198 /* FIXME: 'loc' is incorrect (as of 2011-01-21). It is always
201 YYLTYPE loc
= actual_ast
->get_location();
203 /* Verify that 'const_in' parameters are ir_constants. */
204 if (formal
->data
.mode
== ir_var_const_in
&&
205 actual
->ir_type
!= ir_type_constant
) {
206 _mesa_glsl_error(&loc
, state
,
207 "parameter `in %s' must be a constant expression",
212 /* Verify that shader_in parameters are shader inputs */
213 if (formal
->data
.must_be_shader_input
) {
214 const ir_rvalue
*val
= actual
;
216 /* GLSL 4.40 allows swizzles, while earlier GLSL versions do not. */
217 if (val
->ir_type
== ir_type_swizzle
) {
218 if (!state
->is_version(440, 0)) {
219 _mesa_glsl_error(&loc
, state
,
220 "parameter `%s` must not be swizzled",
224 val
= ((ir_swizzle
*)val
)->val
;
227 while (val
->ir_type
== ir_type_dereference_array
) {
228 val
= ((ir_dereference_array
*)val
)->array
;
231 if (!val
->as_dereference_variable() ||
232 val
->variable_referenced()->data
.mode
!= ir_var_shader_in
) {
233 _mesa_glsl_error(&loc
, state
,
234 "parameter `%s` must be a shader input",
240 /* Verify that 'out' and 'inout' actual parameters are lvalues. */
241 if (formal
->data
.mode
== ir_var_function_out
242 || formal
->data
.mode
== ir_var_function_inout
) {
243 const char *mode
= NULL
;
244 switch (formal
->data
.mode
) {
245 case ir_var_function_out
: mode
= "out"; break;
246 case ir_var_function_inout
: mode
= "inout"; break;
247 default: assert(false); break;
250 /* This AST-based check catches errors like f(i++). The IR-based
251 * is_lvalue() is insufficient because the actual parameter at the
252 * IR-level is just a temporary value, which is an l-value.
254 if (actual_ast
->non_lvalue_description
!= NULL
) {
255 _mesa_glsl_error(&loc
, state
,
256 "function parameter '%s %s' references a %s",
258 actual_ast
->non_lvalue_description
);
262 ir_variable
*var
= actual
->variable_referenced();
264 if (var
&& formal
->data
.mode
== ir_var_function_inout
) {
265 if ((var
->data
.mode
== ir_var_auto
||
266 var
->data
.mode
== ir_var_shader_out
) &&
267 !var
->data
.assigned
&&
268 !is_gl_identifier(var
->name
)) {
269 _mesa_glsl_warning(&loc
, state
, "`%s' used uninitialized",
275 var
->data
.assigned
= true;
277 if (var
&& var
->data
.read_only
) {
278 _mesa_glsl_error(&loc
, state
,
279 "function parameter '%s %s' references the "
280 "read-only variable '%s'",
282 actual
->variable_referenced()->name
);
284 } else if (!actual
->is_lvalue()) {
285 _mesa_glsl_error(&loc
, state
,
286 "function parameter '%s %s' is not an lvalue",
291 assert(formal
->data
.mode
== ir_var_function_in
||
292 formal
->data
.mode
== ir_var_const_in
);
293 ir_variable
*var
= actual
->variable_referenced();
295 if ((var
->data
.mode
== ir_var_auto
||
296 var
->data
.mode
== ir_var_shader_out
) &&
297 !var
->data
.assigned
&&
298 !is_gl_identifier(var
->name
)) {
299 _mesa_glsl_warning(&loc
, state
, "`%s' used uninitialized",
305 if (formal
->type
->is_image() &&
306 actual
->variable_referenced()) {
307 if (!verify_image_parameter(&loc
, state
, formal
,
308 actual
->variable_referenced()))
312 actual_ir_node
= actual_ir_node
->next
;
313 actual_ast_node
= actual_ast_node
->next
;
316 /* The first parameter of atomic functions must be a buffer variable */
317 const char *func_name
= sig
->function_name();
318 bool is_atomic
= is_atomic_function(func_name
);
320 const ir_rvalue
*const actual
=
321 (ir_rvalue
*) actual_ir_parameters
.get_head_raw();
323 const ast_expression
*const actual_ast
=
324 exec_node_data(ast_expression
,
325 actual_ast_parameters
.get_head_raw(), link
);
326 YYLTYPE loc
= actual_ast
->get_location();
328 if (!verify_first_atomic_parameter(&loc
, state
,
329 actual
->variable_referenced())) {
338 fix_parameter(void *mem_ctx
, ir_rvalue
*actual
, const glsl_type
*formal_type
,
339 exec_list
*before_instructions
, exec_list
*after_instructions
,
340 bool parameter_is_inout
)
342 ir_expression
*const expr
= actual
->as_expression();
344 /* If the types match exactly and the parameter is not a vector-extract,
345 * nothing needs to be done to fix the parameter.
347 if (formal_type
== actual
->type
348 && (expr
== NULL
|| expr
->operation
!= ir_binop_vector_extract
))
351 /* To convert an out parameter, we need to create a temporary variable to
352 * hold the value before conversion, and then perform the conversion after
353 * the function call returns.
355 * This has the effect of transforming code like this:
361 * Into IR that's equivalent to this:
365 * int out_parameter_conversion;
366 * f(out_parameter_conversion);
367 * value = float(out_parameter_conversion);
369 * If the parameter is an ir_expression of ir_binop_vector_extract,
370 * additional conversion is needed in the post-call re-write.
373 new(mem_ctx
) ir_variable(formal_type
, "inout_tmp", ir_var_temporary
);
375 before_instructions
->push_tail(tmp
);
377 /* If the parameter is an inout parameter, copy the value of the actual
378 * parameter to the new temporary. Note that no type conversion is allowed
379 * here because inout parameters must match types exactly.
381 if (parameter_is_inout
) {
382 /* Inout parameters should never require conversion, since that would
383 * require an implicit conversion to exist both to and from the formal
384 * parameter type, and there are no bidirectional implicit conversions.
386 assert (actual
->type
== formal_type
);
388 ir_dereference_variable
*const deref_tmp_1
=
389 new(mem_ctx
) ir_dereference_variable(tmp
);
390 ir_assignment
*const assignment
=
391 new(mem_ctx
) ir_assignment(deref_tmp_1
, actual
);
392 before_instructions
->push_tail(assignment
);
395 /* Replace the parameter in the call with a dereference of the new
398 ir_dereference_variable
*const deref_tmp_2
=
399 new(mem_ctx
) ir_dereference_variable(tmp
);
400 actual
->replace_with(deref_tmp_2
);
403 /* Copy the temporary variable to the actual parameter with optional
404 * type conversion applied.
406 ir_rvalue
*rhs
= new(mem_ctx
) ir_dereference_variable(tmp
);
407 if (actual
->type
!= formal_type
)
408 rhs
= convert_component(rhs
, actual
->type
);
410 ir_rvalue
*lhs
= actual
;
411 if (expr
!= NULL
&& expr
->operation
== ir_binop_vector_extract
) {
412 lhs
= new(mem_ctx
) ir_dereference_array(expr
->operands
[0]->clone(mem_ctx
,
414 expr
->operands
[1]->clone(mem_ctx
,
418 ir_assignment
*const assignment_2
= new(mem_ctx
) ir_assignment(lhs
, rhs
);
419 after_instructions
->push_tail(assignment_2
);
423 * Generate a function call.
425 * For non-void functions, this returns a dereference of the temporary
426 * variable which stores the return value for the call. For void functions,
430 generate_call(exec_list
*instructions
, ir_function_signature
*sig
,
431 exec_list
*actual_parameters
,
432 ir_variable
*sub_var
,
433 ir_rvalue
*array_idx
,
434 struct _mesa_glsl_parse_state
*state
,
435 bool inline_immediately
)
438 exec_list post_call_conversions
;
440 /* Perform implicit conversion of arguments. For out parameters, we need
441 * to place them in a temporary variable and do the conversion after the
442 * call takes place. Since we haven't emitted the call yet, we'll place
443 * the post-call conversions in a temporary exec_list, and emit them later.
445 foreach_two_lists(formal_node
, &sig
->parameters
,
446 actual_node
, actual_parameters
) {
447 ir_rvalue
*actual
= (ir_rvalue
*) actual_node
;
448 ir_variable
*formal
= (ir_variable
*) formal_node
;
450 if (formal
->type
->is_numeric() || formal
->type
->is_boolean()) {
451 switch (formal
->data
.mode
) {
452 case ir_var_const_in
:
453 case ir_var_function_in
: {
455 = convert_component(actual
, formal
->type
);
456 actual
->replace_with(converted
);
459 case ir_var_function_out
:
460 case ir_var_function_inout
:
461 fix_parameter(ctx
, actual
, formal
->type
,
462 instructions
, &post_call_conversions
,
463 formal
->data
.mode
== ir_var_function_inout
);
466 assert (!"Illegal formal parameter mode");
472 /* Section 4.3.2 (Const) of the GLSL 1.10.59 spec says:
474 * "Initializers for const declarations must be formed from literal
475 * values, other const variables (not including function call
476 * paramaters), or expressions of these.
478 * Constructors may be used in such expressions, but function calls may
481 * Section 4.3.3 (Constant Expressions) of the GLSL 1.20.8 spec says:
483 * "A constant expression is one of
487 * - a built-in function call whose arguments are all constant
488 * expressions, with the exception of the texture lookup
489 * functions, the noise functions, and ftransform. The built-in
490 * functions dFdx, dFdy, and fwidth must return 0 when evaluated
491 * inside an initializer with an argument that is a constant
494 * Section 5.10 (Constant Expressions) of the GLSL ES 1.00.17 spec says:
496 * "A constant expression is one of
500 * - a built-in function call whose arguments are all constant
501 * expressions, with the exception of the texture lookup
504 * Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec says:
506 * "A constant expression is one of
510 * - a built-in function call whose arguments are all constant
511 * expressions, with the exception of the texture lookup
512 * functions. The built-in functions dFdx, dFdy, and fwidth must
513 * return 0 when evaluated inside an initializer with an argument
514 * that is a constant expression."
516 * If the function call is a constant expression, don't generate any
517 * instructions; just generate an ir_constant.
519 if (state
->is_version(120, 100)) {
520 ir_constant
*value
= sig
->constant_expression_value(actual_parameters
,
527 ir_dereference_variable
*deref
= NULL
;
528 if (!sig
->return_type
->is_void()) {
529 /* Create a new temporary to hold the return value. */
530 char *const name
= ir_variable::temporaries_allocate_names
531 ? ralloc_asprintf(ctx
, "%s_retval", sig
->function_name())
536 var
= new(ctx
) ir_variable(sig
->return_type
, name
, ir_var_temporary
);
537 instructions
->push_tail(var
);
541 deref
= new(ctx
) ir_dereference_variable(var
);
544 ir_call
*call
= new(ctx
) ir_call(sig
, deref
,
545 actual_parameters
, sub_var
, array_idx
);
546 instructions
->push_tail(call
);
547 if (inline_immediately
) {
548 call
->generate_inline(call
);
552 /* Also emit any necessary out-parameter conversions. */
553 instructions
->append_list(&post_call_conversions
);
555 return deref
? deref
->clone(ctx
, NULL
) : NULL
;
559 * Given a function name and parameter list, find the matching signature.
561 static ir_function_signature
*
562 match_function_by_name(const char *name
,
563 exec_list
*actual_parameters
,
564 struct _mesa_glsl_parse_state
*state
)
566 ir_function
*f
= state
->symbols
->get_function(name
);
567 ir_function_signature
*local_sig
= NULL
;
568 ir_function_signature
*sig
= NULL
;
570 /* Is the function hidden by a record type constructor? */
571 if (state
->symbols
->get_type(name
))
572 return sig
; /* no match */
574 /* Is the function hidden by a variable (impossible in 1.10)? */
575 if (!state
->symbols
->separate_function_namespace
576 && state
->symbols
->get_variable(name
))
577 return sig
; /* no match */
580 /* In desktop GL, the presence of a user-defined signature hides any
581 * built-in signatures, so we must ignore them. In contrast, in ES2
582 * user-defined signatures add new overloads, so we must consider them.
584 bool allow_builtins
= state
->es_shader
|| !f
->has_user_signature();
586 /* Look for a match in the local shader. If exact, we're done. */
587 bool is_exact
= false;
588 sig
= local_sig
= f
->matching_signature(state
, actual_parameters
,
589 allow_builtins
, &is_exact
);
597 /* Local shader has no exact candidates; check the built-ins. */
598 _mesa_glsl_initialize_builtin_functions();
599 sig
= _mesa_glsl_find_builtin_function(state
, name
, actual_parameters
);
603 static ir_function_signature
*
604 match_subroutine_by_name(const char *name
,
605 exec_list
*actual_parameters
,
606 struct _mesa_glsl_parse_state
*state
,
610 ir_function_signature
*sig
= NULL
;
611 ir_function
*f
, *found
= NULL
;
612 const char *new_name
;
614 bool is_exact
= false;
617 ralloc_asprintf(ctx
, "%s_%s",
618 _mesa_shader_stage_to_subroutine_prefix(state
->stage
),
620 var
= state
->symbols
->get_variable(new_name
);
624 for (int i
= 0; i
< state
->num_subroutine_types
; i
++) {
625 f
= state
->subroutine_types
[i
];
626 if (strcmp(f
->name
, var
->type
->without_array()->name
))
635 sig
= found
->matching_signature(state
, actual_parameters
,
641 generate_array_index(void *mem_ctx
, exec_list
*instructions
,
642 struct _mesa_glsl_parse_state
*state
, YYLTYPE loc
,
643 const ast_expression
*array
, ast_expression
*idx
,
644 const char **function_name
, exec_list
*actual_parameters
)
646 if (array
->oper
== ast_array_index
) {
647 /* This handles arrays of arrays */
648 ir_rvalue
*outer_array
= generate_array_index(mem_ctx
, instructions
,
650 array
->subexpressions
[0],
651 array
->subexpressions
[1],
654 ir_rvalue
*outer_array_idx
= idx
->hir(instructions
, state
);
656 YYLTYPE index_loc
= idx
->get_location();
657 return _mesa_ast_array_index_to_hir(mem_ctx
, state
, outer_array
,
658 outer_array_idx
, loc
,
661 ir_variable
*sub_var
= NULL
;
662 *function_name
= array
->primary_expression
.identifier
;
664 match_subroutine_by_name(*function_name
, actual_parameters
,
667 ir_rvalue
*outer_array_idx
= idx
->hir(instructions
, state
);
668 return new(mem_ctx
) ir_dereference_array(sub_var
, outer_array_idx
);
673 print_function_prototypes(_mesa_glsl_parse_state
*state
, YYLTYPE
*loc
,
679 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
680 if (sig
->is_builtin() && !sig
->is_builtin_available(state
))
683 char *str
= prototype_string(sig
->return_type
, f
->name
,
685 _mesa_glsl_error(loc
, state
, " %s", str
);
691 * Raise a "no matching function" error, listing all possible overloads the
692 * compiler considered so developers can figure out what went wrong.
695 no_matching_function_error(const char *name
,
697 exec_list
*actual_parameters
,
698 _mesa_glsl_parse_state
*state
)
700 gl_shader
*sh
= _mesa_glsl_get_builtin_function_shader();
702 if (state
->symbols
->get_function(name
) == NULL
703 && (!state
->uses_builtin_functions
704 || sh
->symbols
->get_function(name
) == NULL
)) {
705 _mesa_glsl_error(loc
, state
, "no function with name '%s'", name
);
707 char *str
= prototype_string(NULL
, name
, actual_parameters
);
708 _mesa_glsl_error(loc
, state
,
709 "no matching function for call to `%s';"
714 print_function_prototypes(state
, loc
,
715 state
->symbols
->get_function(name
));
717 if (state
->uses_builtin_functions
) {
718 print_function_prototypes(state
, loc
,
719 sh
->symbols
->get_function(name
));
725 * Perform automatic type conversion of constructor parameters
727 * This implements the rules in the "Conversion and Scalar Constructors"
728 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
731 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
)
733 void *ctx
= ralloc_parent(src
);
734 const unsigned a
= desired_type
->base_type
;
735 const unsigned b
= src
->type
->base_type
;
736 ir_expression
*result
= NULL
;
738 if (src
->type
->is_error())
741 assert(a
<= GLSL_TYPE_BOOL
);
742 assert(b
<= GLSL_TYPE_BOOL
);
751 result
= new(ctx
) ir_expression(ir_unop_i2u
, src
);
753 case GLSL_TYPE_FLOAT
:
754 result
= new(ctx
) ir_expression(ir_unop_f2u
, src
);
757 result
= new(ctx
) ir_expression(ir_unop_i2u
,
758 new(ctx
) ir_expression(ir_unop_b2i
,
761 case GLSL_TYPE_DOUBLE
:
762 result
= new(ctx
) ir_expression(ir_unop_d2u
, src
);
764 case GLSL_TYPE_UINT64
:
765 result
= new(ctx
) ir_expression(ir_unop_u642u
, src
);
767 case GLSL_TYPE_INT64
:
768 result
= new(ctx
) ir_expression(ir_unop_i642u
, src
);
775 result
= new(ctx
) ir_expression(ir_unop_u2i
, src
);
777 case GLSL_TYPE_FLOAT
:
778 result
= new(ctx
) ir_expression(ir_unop_f2i
, src
);
781 result
= new(ctx
) ir_expression(ir_unop_b2i
, src
);
783 case GLSL_TYPE_DOUBLE
:
784 result
= new(ctx
) ir_expression(ir_unop_d2i
, src
);
786 case GLSL_TYPE_UINT64
:
787 result
= new(ctx
) ir_expression(ir_unop_u642i
, src
);
789 case GLSL_TYPE_INT64
:
790 result
= new(ctx
) ir_expression(ir_unop_i642i
, src
);
794 case GLSL_TYPE_FLOAT
:
797 result
= new(ctx
) ir_expression(ir_unop_u2f
, desired_type
, src
, NULL
);
800 result
= new(ctx
) ir_expression(ir_unop_i2f
, desired_type
, src
, NULL
);
803 result
= new(ctx
) ir_expression(ir_unop_b2f
, desired_type
, src
, NULL
);
805 case GLSL_TYPE_DOUBLE
:
806 result
= new(ctx
) ir_expression(ir_unop_d2f
, desired_type
, src
, NULL
);
808 case GLSL_TYPE_UINT64
:
809 result
= new(ctx
) ir_expression(ir_unop_u642f
, desired_type
, src
, NULL
);
811 case GLSL_TYPE_INT64
:
812 result
= new(ctx
) ir_expression(ir_unop_i642f
, desired_type
, src
, NULL
);
819 result
= new(ctx
) ir_expression(ir_unop_i2b
,
820 new(ctx
) ir_expression(ir_unop_u2i
,
824 result
= new(ctx
) ir_expression(ir_unop_i2b
, desired_type
, src
, NULL
);
826 case GLSL_TYPE_FLOAT
:
827 result
= new(ctx
) ir_expression(ir_unop_f2b
, desired_type
, src
, NULL
);
829 case GLSL_TYPE_DOUBLE
:
830 result
= new(ctx
) ir_expression(ir_unop_d2b
, desired_type
, src
, NULL
);
832 case GLSL_TYPE_UINT64
:
833 result
= new(ctx
) ir_expression(ir_unop_i642b
,
834 new(ctx
) ir_expression(ir_unop_u642i64
,
837 case GLSL_TYPE_INT64
:
838 result
= new(ctx
) ir_expression(ir_unop_i642b
, desired_type
, src
, NULL
);
842 case GLSL_TYPE_DOUBLE
:
845 result
= new(ctx
) ir_expression(ir_unop_i2d
, src
);
848 result
= new(ctx
) ir_expression(ir_unop_u2d
, src
);
851 result
= new(ctx
) ir_expression(ir_unop_f2d
,
852 new(ctx
) ir_expression(ir_unop_b2f
,
855 case GLSL_TYPE_FLOAT
:
856 result
= new(ctx
) ir_expression(ir_unop_f2d
, desired_type
, src
, NULL
);
858 case GLSL_TYPE_UINT64
:
859 result
= new(ctx
) ir_expression(ir_unop_u642d
, desired_type
, src
, NULL
);
861 case GLSL_TYPE_INT64
:
862 result
= new(ctx
) ir_expression(ir_unop_i642d
, desired_type
, src
, NULL
);
866 case GLSL_TYPE_UINT64
:
869 result
= new(ctx
) ir_expression(ir_unop_i2u64
, src
);
872 result
= new(ctx
) ir_expression(ir_unop_u2u64
, src
);
875 result
= new(ctx
) ir_expression(ir_unop_i642u64
,
876 new(ctx
) ir_expression(ir_unop_b2i64
,
879 case GLSL_TYPE_FLOAT
:
880 result
= new(ctx
) ir_expression(ir_unop_f2u64
, src
);
882 case GLSL_TYPE_DOUBLE
:
883 result
= new(ctx
) ir_expression(ir_unop_d2u64
, src
);
885 case GLSL_TYPE_INT64
:
886 result
= new(ctx
) ir_expression(ir_unop_i642u64
, src
);
890 case GLSL_TYPE_INT64
:
893 result
= new(ctx
) ir_expression(ir_unop_i2i64
, src
);
896 result
= new(ctx
) ir_expression(ir_unop_u2i64
, src
);
899 result
= new(ctx
) ir_expression(ir_unop_b2i64
, src
);
901 case GLSL_TYPE_FLOAT
:
902 result
= new(ctx
) ir_expression(ir_unop_f2i64
, src
);
904 case GLSL_TYPE_DOUBLE
:
905 result
= new(ctx
) ir_expression(ir_unop_d2i64
, src
);
907 case GLSL_TYPE_UINT64
:
908 result
= new(ctx
) ir_expression(ir_unop_u642i64
, src
);
914 assert(result
!= NULL
);
915 assert(result
->type
== desired_type
);
917 /* Try constant folding; it may fold in the conversion we just added. */
918 ir_constant
*const constant
= result
->constant_expression_value();
919 return (constant
!= NULL
) ? (ir_rvalue
*) constant
: (ir_rvalue
*) result
;
924 * Perform automatic type and constant conversion of constructor parameters
926 * This implements the rules in the "Implicit Conversions" rules, not the
927 * "Conversion and Scalar Constructors".
929 * After attempting the implicit conversion, an attempt to convert into a
930 * constant valued expression is also done.
932 * The \c from \c ir_rvalue is converted "in place".
934 * \param from Operand that is being converted
935 * \param to Base type the operand will be converted to
936 * \param state GLSL compiler state
939 * If the attempt to convert into a constant expression succeeds, \c true is
940 * returned. Otherwise \c false is returned.
943 implicitly_convert_component(ir_rvalue
* &from
, const glsl_base_type to
,
944 struct _mesa_glsl_parse_state
*state
)
946 ir_rvalue
*result
= from
;
948 if (to
!= from
->type
->base_type
) {
949 const glsl_type
*desired_type
=
950 glsl_type::get_instance(to
,
951 from
->type
->vector_elements
,
952 from
->type
->matrix_columns
);
954 if (from
->type
->can_implicitly_convert_to(desired_type
, state
)) {
955 /* Even though convert_component() implements the constructor
956 * conversion rules (not the implicit conversion rules), its safe
957 * to use it here because we already checked that the implicit
958 * conversion is legal.
960 result
= convert_component(from
, desired_type
);
964 ir_rvalue
*const constant
= result
->constant_expression_value();
966 if (constant
!= NULL
)
969 if (from
!= result
) {
970 from
->replace_with(result
);
974 return constant
!= NULL
;
979 * Dereference a specific component from a scalar, vector, or matrix
982 dereference_component(ir_rvalue
*src
, unsigned component
)
984 void *ctx
= ralloc_parent(src
);
985 assert(component
< src
->type
->components());
987 /* If the source is a constant, just create a new constant instead of a
988 * dereference of the existing constant.
990 ir_constant
*constant
= src
->as_constant();
992 return new(ctx
) ir_constant(constant
, component
);
994 if (src
->type
->is_scalar()) {
996 } else if (src
->type
->is_vector()) {
997 return new(ctx
) ir_swizzle(src
, component
, 0, 0, 0, 1);
999 assert(src
->type
->is_matrix());
1001 /* Dereference a row of the matrix, then call this function again to get
1002 * a specific element from that row.
1004 const int c
= component
/ src
->type
->column_type()->vector_elements
;
1005 const int r
= component
% src
->type
->column_type()->vector_elements
;
1006 ir_constant
*const col_index
= new(ctx
) ir_constant(c
);
1007 ir_dereference
*const col
= new(ctx
) ir_dereference_array(src
,
1010 col
->type
= src
->type
->column_type();
1012 return dereference_component(col
, r
);
1015 assert(!"Should not get here.");
1021 process_vec_mat_constructor(exec_list
*instructions
,
1022 const glsl_type
*constructor_type
,
1023 YYLTYPE
*loc
, exec_list
*parameters
,
1024 struct _mesa_glsl_parse_state
*state
)
1028 /* The ARB_shading_language_420pack spec says:
1030 * "If an initializer is a list of initializers enclosed in curly braces,
1031 * the variable being declared must be a vector, a matrix, an array, or a
1034 * int i = { 1 }; // illegal, i is not an aggregate"
1036 if (constructor_type
->vector_elements
<= 1) {
1037 _mesa_glsl_error(loc
, state
, "aggregates can only initialize vectors, "
1038 "matrices, arrays, and structs");
1039 return ir_rvalue::error_value(ctx
);
1042 exec_list actual_parameters
;
1043 const unsigned parameter_count
=
1044 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
1046 if (parameter_count
== 0
1047 || (constructor_type
->is_vector() &&
1048 constructor_type
->vector_elements
!= parameter_count
)
1049 || (constructor_type
->is_matrix() &&
1050 constructor_type
->matrix_columns
!= parameter_count
)) {
1051 _mesa_glsl_error(loc
, state
, "%s constructor must have %u parameters",
1052 constructor_type
->is_vector() ? "vector" : "matrix",
1053 constructor_type
->vector_elements
);
1054 return ir_rvalue::error_value(ctx
);
1057 bool all_parameters_are_constant
= true;
1059 /* Type cast each parameter and, if possible, fold constants. */
1060 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1061 /* Apply implicit conversions (not the scalar constructor rules, see the
1062 * spec quote above!) and attempt to convert the parameter to a constant
1063 * valued expression. After doing so, track whether or not all the
1064 * parameters to the constructor are trivially constant valued
1067 all_parameters_are_constant
&=
1068 implicitly_convert_component(ir
, constructor_type
->base_type
, state
);
1070 if (constructor_type
->is_matrix()) {
1071 if (ir
->type
!= constructor_type
->column_type()) {
1072 _mesa_glsl_error(loc
, state
, "type error in matrix constructor: "
1073 "expected: %s, found %s",
1074 constructor_type
->column_type()->name
,
1076 return ir_rvalue::error_value(ctx
);
1078 } else if (ir
->type
!= constructor_type
->get_scalar_type()) {
1079 _mesa_glsl_error(loc
, state
, "type error in vector constructor: "
1080 "expected: %s, found %s",
1081 constructor_type
->get_scalar_type()->name
,
1083 return ir_rvalue::error_value(ctx
);
1087 if (all_parameters_are_constant
)
1088 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1090 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "vec_mat_ctor",
1092 instructions
->push_tail(var
);
1096 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
1097 ir_instruction
*assignment
= NULL
;
1099 if (var
->type
->is_matrix()) {
1101 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
1102 assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
1104 /* use writemask rather than index for vector */
1105 assert(var
->type
->is_vector());
1107 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1108 assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
,
1109 (unsigned)(1 << i
));
1112 instructions
->push_tail(assignment
);
1117 return new(ctx
) ir_dereference_variable(var
);
1122 process_array_constructor(exec_list
*instructions
,
1123 const glsl_type
*constructor_type
,
1124 YYLTYPE
*loc
, exec_list
*parameters
,
1125 struct _mesa_glsl_parse_state
*state
)
1128 /* Array constructors come in two forms: sized and unsized. Sized array
1129 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
1130 * variables. In this case the number of parameters must exactly match the
1131 * specified size of the array.
1133 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
1134 * are vec4 variables. In this case the size of the array being constructed
1135 * is determined by the number of parameters.
1137 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
1139 * "There must be exactly the same number of arguments as the size of
1140 * the array being constructed. If no size is present in the
1141 * constructor, then the array is explicitly sized to the number of
1142 * arguments provided. The arguments are assigned in order, starting at
1143 * element 0, to the elements of the constructed array. Each argument
1144 * must be the same type as the element type of the array, or be a type
1145 * that can be converted to the element type of the array according to
1146 * Section 4.1.10 "Implicit Conversions.""
1148 exec_list actual_parameters
;
1149 const unsigned parameter_count
=
1150 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
1151 bool is_unsized_array
= constructor_type
->is_unsized_array();
1153 if ((parameter_count
== 0) ||
1154 (!is_unsized_array
&& (constructor_type
->length
!= parameter_count
))) {
1155 const unsigned min_param
= is_unsized_array
1156 ? 1 : constructor_type
->length
;
1158 _mesa_glsl_error(loc
, state
, "array constructor must have %s %u "
1160 is_unsized_array
? "at least" : "exactly",
1161 min_param
, (min_param
<= 1) ? "" : "s");
1162 return ir_rvalue::error_value(ctx
);
1165 if (is_unsized_array
) {
1167 glsl_type::get_array_instance(constructor_type
->fields
.array
,
1169 assert(constructor_type
!= NULL
);
1170 assert(constructor_type
->length
== parameter_count
);
1173 bool all_parameters_are_constant
= true;
1174 const glsl_type
*element_type
= constructor_type
->fields
.array
;
1176 /* Type cast each parameter and, if possible, fold constants. */
1177 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1178 /* Apply implicit conversions (not the scalar constructor rules, see the
1179 * spec quote above!) and attempt to convert the parameter to a constant
1180 * valued expression. After doing so, track whether or not all the
1181 * parameters to the constructor are trivially constant valued
1184 all_parameters_are_constant
&=
1185 implicitly_convert_component(ir
, element_type
->base_type
, state
);
1187 if (constructor_type
->fields
.array
->is_unsized_array()) {
1188 /* As the inner parameters of the constructor are created without
1189 * knowledge of each other we need to check to make sure unsized
1190 * parameters of unsized constructors all end up with the same size.
1192 * e.g we make sure to fail for a constructor like this:
1193 * vec4[][] a = vec4[][](vec4[](vec4(0.0), vec4(1.0)),
1194 * vec4[](vec4(0.0), vec4(1.0), vec4(1.0)),
1195 * vec4[](vec4(0.0), vec4(1.0)));
1197 if (element_type
->is_unsized_array()) {
1198 /* This is the first parameter so just get the type */
1199 element_type
= ir
->type
;
1200 } else if (element_type
!= ir
->type
) {
1201 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
1202 "expected: %s, found %s",
1205 return ir_rvalue::error_value(ctx
);
1207 } else if (ir
->type
!= constructor_type
->fields
.array
) {
1208 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
1209 "expected: %s, found %s",
1210 constructor_type
->fields
.array
->name
,
1212 return ir_rvalue::error_value(ctx
);
1214 element_type
= ir
->type
;
1218 if (constructor_type
->fields
.array
->is_unsized_array()) {
1220 glsl_type::get_array_instance(element_type
,
1222 assert(constructor_type
!= NULL
);
1223 assert(constructor_type
->length
== parameter_count
);
1226 if (all_parameters_are_constant
)
1227 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1229 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "array_ctor",
1231 instructions
->push_tail(var
);
1234 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
1235 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
1236 new(ctx
) ir_constant(i
));
1238 ir_instruction
*assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
1239 instructions
->push_tail(assignment
);
1244 return new(ctx
) ir_dereference_variable(var
);
1249 * Determine if a list consists of a single scalar r-value
1252 single_scalar_parameter(exec_list
*parameters
)
1254 const ir_rvalue
*const p
= (ir_rvalue
*) parameters
->get_head_raw();
1255 assert(((ir_rvalue
*)p
)->as_rvalue() != NULL
);
1257 return (p
->type
->is_scalar() && p
->next
->is_tail_sentinel());
1262 * Generate inline code for a vector constructor
1264 * The generated constructor code will consist of a temporary variable
1265 * declaration of the same type as the constructor. A sequence of assignments
1266 * from constructor parameters to the temporary will follow.
1269 * An \c ir_dereference_variable of the temprorary generated in the constructor
1273 emit_inline_vector_constructor(const glsl_type
*type
,
1274 exec_list
*instructions
,
1275 exec_list
*parameters
,
1278 assert(!parameters
->is_empty());
1280 ir_variable
*var
= new(ctx
) ir_variable(type
, "vec_ctor", ir_var_temporary
);
1281 instructions
->push_tail(var
);
1283 /* There are three kinds of vector constructors.
1285 * - Construct a vector from a single scalar by replicating that scalar to
1286 * all components of the vector.
1288 * - Construct a vector from at least a matrix. This case should already
1289 * have been taken care of in ast_function_expression::hir by breaking
1290 * down the matrix into a series of column vectors.
1292 * - Construct a vector from an arbirary combination of vectors and
1293 * scalars. The components of the constructor parameters are assigned
1294 * to the vector in order until the vector is full.
1296 const unsigned lhs_components
= type
->components();
1297 if (single_scalar_parameter(parameters
)) {
1298 ir_rvalue
*first_param
= (ir_rvalue
*)parameters
->get_head_raw();
1299 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(first_param
, 0, 0, 0, 0,
1301 ir_dereference_variable
*lhs
= new(ctx
) ir_dereference_variable(var
);
1302 const unsigned mask
= (1U << lhs_components
) - 1;
1304 assert(rhs
->type
== lhs
->type
);
1306 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
, mask
);
1307 instructions
->push_tail(inst
);
1309 unsigned base_component
= 0;
1310 unsigned base_lhs_component
= 0;
1311 ir_constant_data data
;
1312 unsigned constant_mask
= 0, constant_components
= 0;
1314 memset(&data
, 0, sizeof(data
));
1316 foreach_in_list(ir_rvalue
, param
, parameters
) {
1317 unsigned rhs_components
= param
->type
->components();
1319 /* Do not try to assign more components to the vector than it has! */
1320 if ((rhs_components
+ base_lhs_component
) > lhs_components
) {
1321 rhs_components
= lhs_components
- base_lhs_component
;
1324 const ir_constant
*const c
= param
->as_constant();
1326 for (unsigned i
= 0; i
< rhs_components
; i
++) {
1327 switch (c
->type
->base_type
) {
1328 case GLSL_TYPE_UINT
:
1329 data
.u
[i
+ base_component
] = c
->get_uint_component(i
);
1332 data
.i
[i
+ base_component
] = c
->get_int_component(i
);
1334 case GLSL_TYPE_FLOAT
:
1335 data
.f
[i
+ base_component
] = c
->get_float_component(i
);
1337 case GLSL_TYPE_DOUBLE
:
1338 data
.d
[i
+ base_component
] = c
->get_double_component(i
);
1340 case GLSL_TYPE_BOOL
:
1341 data
.b
[i
+ base_component
] = c
->get_bool_component(i
);
1343 case GLSL_TYPE_UINT64
:
1344 data
.u64
[i
+ base_component
] = c
->get_uint64_component(i
);
1346 case GLSL_TYPE_INT64
:
1347 data
.i64
[i
+ base_component
] = c
->get_int64_component(i
);
1350 assert(!"Should not get here.");
1355 /* Mask of fields to be written in the assignment. */
1356 constant_mask
|= ((1U << rhs_components
) - 1) << base_lhs_component
;
1357 constant_components
+= rhs_components
;
1359 base_component
+= rhs_components
;
1361 /* Advance the component index by the number of components
1362 * that were just assigned.
1364 base_lhs_component
+= rhs_components
;
1367 if (constant_mask
!= 0) {
1368 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1369 const glsl_type
*rhs_type
=
1370 glsl_type::get_instance(var
->type
->base_type
,
1371 constant_components
,
1373 ir_rvalue
*rhs
= new(ctx
) ir_constant(rhs_type
, &data
);
1375 ir_instruction
*inst
=
1376 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, constant_mask
);
1377 instructions
->push_tail(inst
);
1381 foreach_in_list(ir_rvalue
, param
, parameters
) {
1382 unsigned rhs_components
= param
->type
->components();
1384 /* Do not try to assign more components to the vector than it has! */
1385 if ((rhs_components
+ base_component
) > lhs_components
) {
1386 rhs_components
= lhs_components
- base_component
;
1389 /* If we do not have any components left to copy, break out of the
1390 * loop. This can happen when initializing a vec4 with a mat3 as the
1391 * mat3 would have been broken into a series of column vectors.
1393 if (rhs_components
== 0) {
1397 const ir_constant
*const c
= param
->as_constant();
1399 /* Mask of fields to be written in the assignment. */
1400 const unsigned write_mask
= ((1U << rhs_components
) - 1)
1403 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1405 /* Generate a swizzle so that LHS and RHS sizes match. */
1407 new(ctx
) ir_swizzle(param
, 0, 1, 2, 3, rhs_components
);
1409 ir_instruction
*inst
=
1410 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1411 instructions
->push_tail(inst
);
1414 /* Advance the component index by the number of components that were
1417 base_component
+= rhs_components
;
1420 return new(ctx
) ir_dereference_variable(var
);
1425 * Generate assignment of a portion of a vector to a portion of a matrix column
1427 * \param src_base First component of the source to be used in assignment
1428 * \param column Column of destination to be assiged
1429 * \param row_base First component of the destination column to be assigned
1430 * \param count Number of components to be assigned
1433 * \c src_base + \c count must be less than or equal to the number of
1434 * components in the source vector.
1437 assign_to_matrix_column(ir_variable
*var
, unsigned column
, unsigned row_base
,
1438 ir_rvalue
*src
, unsigned src_base
, unsigned count
,
1441 ir_constant
*col_idx
= new(mem_ctx
) ir_constant(column
);
1442 ir_dereference
*column_ref
= new(mem_ctx
) ir_dereference_array(var
,
1445 assert(column_ref
->type
->components() >= (row_base
+ count
));
1446 assert(src
->type
->components() >= (src_base
+ count
));
1448 /* Generate a swizzle that extracts the number of components from the source
1449 * that are to be assigned to the column of the matrix.
1451 if (count
< src
->type
->vector_elements
) {
1452 src
= new(mem_ctx
) ir_swizzle(src
,
1453 src_base
+ 0, src_base
+ 1,
1454 src_base
+ 2, src_base
+ 3,
1458 /* Mask of fields to be written in the assignment. */
1459 const unsigned write_mask
= ((1U << count
) - 1) << row_base
;
1461 return new(mem_ctx
) ir_assignment(column_ref
, src
, NULL
, write_mask
);
1466 * Generate inline code for a matrix constructor
1468 * The generated constructor code will consist of a temporary variable
1469 * declaration of the same type as the constructor. A sequence of assignments
1470 * from constructor parameters to the temporary will follow.
1473 * An \c ir_dereference_variable of the temprorary generated in the constructor
1477 emit_inline_matrix_constructor(const glsl_type
*type
,
1478 exec_list
*instructions
,
1479 exec_list
*parameters
,
1482 assert(!parameters
->is_empty());
1484 ir_variable
*var
= new(ctx
) ir_variable(type
, "mat_ctor", ir_var_temporary
);
1485 instructions
->push_tail(var
);
1487 /* There are three kinds of matrix constructors.
1489 * - Construct a matrix from a single scalar by replicating that scalar to
1490 * along the diagonal of the matrix and setting all other components to
1493 * - Construct a matrix from an arbirary combination of vectors and
1494 * scalars. The components of the constructor parameters are assigned
1495 * to the matrix in column-major order until the matrix is full.
1497 * - Construct a matrix from a single matrix. The source matrix is copied
1498 * to the upper left portion of the constructed matrix, and the remaining
1499 * elements take values from the identity matrix.
1501 ir_rvalue
*const first_param
= (ir_rvalue
*) parameters
->get_head_raw();
1502 if (single_scalar_parameter(parameters
)) {
1503 /* Assign the scalar to the X component of a vec4, and fill the remaining
1504 * components with zero.
1506 glsl_base_type param_base_type
= first_param
->type
->base_type
;
1507 assert(param_base_type
== GLSL_TYPE_FLOAT
||
1508 first_param
->type
->is_double());
1509 ir_variable
*rhs_var
=
1510 new(ctx
) ir_variable(glsl_type::get_instance(param_base_type
, 4, 1),
1513 instructions
->push_tail(rhs_var
);
1515 ir_constant_data zero
;
1516 for (unsigned i
= 0; i
< 4; i
++)
1517 if (param_base_type
== GLSL_TYPE_FLOAT
)
1522 ir_instruction
*inst
=
1523 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(rhs_var
),
1524 new(ctx
) ir_constant(rhs_var
->type
, &zero
),
1526 instructions
->push_tail(inst
);
1528 ir_dereference
*const rhs_ref
=
1529 new(ctx
) ir_dereference_variable(rhs_var
);
1531 inst
= new(ctx
) ir_assignment(rhs_ref
, first_param
, NULL
, 0x01);
1532 instructions
->push_tail(inst
);
1534 /* Assign the temporary vector to each column of the destination matrix
1535 * with a swizzle that puts the X component on the diagonal of the
1536 * matrix. In some cases this may mean that the X component does not
1537 * get assigned into the column at all (i.e., when the matrix has more
1538 * columns than rows).
1540 static const unsigned rhs_swiz
[4][4] = {
1547 const unsigned cols_to_init
= MIN2(type
->matrix_columns
,
1548 type
->vector_elements
);
1549 for (unsigned i
= 0; i
< cols_to_init
; i
++) {
1550 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1551 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
,
1554 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1555 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, rhs_swiz
[i
],
1556 type
->vector_elements
);
1558 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
1559 instructions
->push_tail(inst
);
1562 for (unsigned i
= cols_to_init
; i
< type
->matrix_columns
; i
++) {
1563 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1564 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
,
1567 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1568 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, 1, 1, 1, 1,
1569 type
->vector_elements
);
1571 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
1572 instructions
->push_tail(inst
);
1574 } else if (first_param
->type
->is_matrix()) {
1575 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
1577 * "If a matrix is constructed from a matrix, then each component
1578 * (column i, row j) in the result that has a corresponding
1579 * component (column i, row j) in the argument will be initialized
1580 * from there. All other components will be initialized to the
1581 * identity matrix. If a matrix argument is given to a matrix
1582 * constructor, it is an error to have any other arguments."
1584 assert(first_param
->next
->is_tail_sentinel());
1585 ir_rvalue
*const src_matrix
= first_param
;
1587 /* If the source matrix is smaller, pre-initialize the relavent parts of
1588 * the destination matrix to the identity matrix.
1590 if ((src_matrix
->type
->matrix_columns
< var
->type
->matrix_columns
) ||
1591 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)) {
1593 /* If the source matrix has fewer rows, every column of the
1594 * destination must be initialized. Otherwise only the columns in
1595 * the destination that do not exist in the source must be
1599 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)
1600 ? 0 : src_matrix
->type
->matrix_columns
;
1602 const glsl_type
*const col_type
= var
->type
->column_type();
1603 for (/* empty */; col
< var
->type
->matrix_columns
; col
++) {
1604 ir_constant_data ident
;
1606 if (!col_type
->is_double()) {
1611 ident
.f
[col
] = 1.0f
;
1620 ir_rvalue
*const rhs
= new(ctx
) ir_constant(col_type
, &ident
);
1622 ir_rvalue
*const lhs
=
1623 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(col
));
1625 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
1626 instructions
->push_tail(inst
);
1630 /* Assign columns from the source matrix to the destination matrix.
1632 * Since the parameter will be used in the RHS of multiple assignments,
1633 * generate a temporary and copy the paramter there.
1635 ir_variable
*const rhs_var
=
1636 new(ctx
) ir_variable(first_param
->type
, "mat_ctor_mat",
1638 instructions
->push_tail(rhs_var
);
1640 ir_dereference
*const rhs_var_ref
=
1641 new(ctx
) ir_dereference_variable(rhs_var
);
1642 ir_instruction
*const inst
=
1643 new(ctx
) ir_assignment(rhs_var_ref
, first_param
, NULL
);
1644 instructions
->push_tail(inst
);
1646 const unsigned last_row
= MIN2(src_matrix
->type
->vector_elements
,
1647 var
->type
->vector_elements
);
1648 const unsigned last_col
= MIN2(src_matrix
->type
->matrix_columns
,
1649 var
->type
->matrix_columns
);
1651 unsigned swiz
[4] = { 0, 0, 0, 0 };
1652 for (unsigned i
= 1; i
< last_row
; i
++)
1655 const unsigned write_mask
= (1U << last_row
) - 1;
1657 for (unsigned i
= 0; i
< last_col
; i
++) {
1658 ir_dereference
*const lhs
=
1659 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
1660 ir_rvalue
*const rhs_col
=
1661 new(ctx
) ir_dereference_array(rhs_var
, new(ctx
) ir_constant(i
));
1663 /* If one matrix has columns that are smaller than the columns of the
1664 * other matrix, wrap the column access of the larger with a swizzle
1665 * so that the LHS and RHS of the assignment have the same size (and
1666 * therefore have the same type).
1668 * It would be perfectly valid to unconditionally generate the
1669 * swizzles, this this will typically result in a more compact IR
1673 if (lhs
->type
->vector_elements
!= rhs_col
->type
->vector_elements
) {
1674 rhs
= new(ctx
) ir_swizzle(rhs_col
, swiz
, last_row
);
1679 ir_instruction
*inst
=
1680 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1681 instructions
->push_tail(inst
);
1684 const unsigned cols
= type
->matrix_columns
;
1685 const unsigned rows
= type
->vector_elements
;
1686 unsigned remaining_slots
= rows
* cols
;
1687 unsigned col_idx
= 0;
1688 unsigned row_idx
= 0;
1690 foreach_in_list(ir_rvalue
, rhs
, parameters
) {
1691 unsigned rhs_components
= rhs
->type
->components();
1692 unsigned rhs_base
= 0;
1694 if (remaining_slots
== 0)
1697 /* Since the parameter might be used in the RHS of two assignments,
1698 * generate a temporary and copy the paramter there.
1700 ir_variable
*rhs_var
=
1701 new(ctx
) ir_variable(rhs
->type
, "mat_ctor_vec", ir_var_temporary
);
1702 instructions
->push_tail(rhs_var
);
1704 ir_dereference
*rhs_var_ref
=
1705 new(ctx
) ir_dereference_variable(rhs_var
);
1706 ir_instruction
*inst
= new(ctx
) ir_assignment(rhs_var_ref
, rhs
, NULL
);
1707 instructions
->push_tail(inst
);
1710 /* Assign the current parameter to as many components of the matrix
1713 * NOTE: A single vector parameter can span two matrix columns. A
1714 * single vec4, for example, can completely fill a mat2.
1716 unsigned count
= MIN2(rows
- row_idx
,
1717 rhs_components
- rhs_base
);
1719 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1720 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
1725 instructions
->push_tail(inst
);
1728 remaining_slots
-= count
;
1730 /* Sometimes, there is still data left in the parameters and
1731 * components left to be set in the destination but in other
1734 if (row_idx
>= rows
) {
1738 } while(remaining_slots
> 0 && rhs_base
< rhs_components
);
1742 return new(ctx
) ir_dereference_variable(var
);
1747 emit_inline_record_constructor(const glsl_type
*type
,
1748 exec_list
*instructions
,
1749 exec_list
*parameters
,
1752 ir_variable
*const var
=
1753 new(mem_ctx
) ir_variable(type
, "record_ctor", ir_var_temporary
);
1754 ir_dereference_variable
*const d
=
1755 new(mem_ctx
) ir_dereference_variable(var
);
1757 instructions
->push_tail(var
);
1759 exec_node
*node
= parameters
->get_head_raw();
1760 for (unsigned i
= 0; i
< type
->length
; i
++) {
1761 assert(!node
->is_tail_sentinel());
1763 ir_dereference
*const lhs
=
1764 new(mem_ctx
) ir_dereference_record(d
->clone(mem_ctx
, NULL
),
1765 type
->fields
.structure
[i
].name
);
1767 ir_rvalue
*const rhs
= ((ir_instruction
*) node
)->as_rvalue();
1768 assert(rhs
!= NULL
);
1770 ir_instruction
*const assign
=
1771 new(mem_ctx
) ir_assignment(lhs
, rhs
, NULL
);
1773 instructions
->push_tail(assign
);
1782 process_record_constructor(exec_list
*instructions
,
1783 const glsl_type
*constructor_type
,
1784 YYLTYPE
*loc
, exec_list
*parameters
,
1785 struct _mesa_glsl_parse_state
*state
)
1788 /* From page 32 (page 38 of the PDF) of the GLSL 1.20 spec:
1790 * "The arguments to the constructor will be used to set the structure's
1791 * fields, in order, using one argument per field. Each argument must
1792 * be the same type as the field it sets, or be a type that can be
1793 * converted to the field's type according to Section 4.1.10 “Implicit
1796 * From page 35 (page 41 of the PDF) of the GLSL 4.20 spec:
1798 * "In all cases, the innermost initializer (i.e., not a list of
1799 * initializers enclosed in curly braces) applied to an object must
1800 * have the same type as the object being initialized or be a type that
1801 * can be converted to the object's type according to section 4.1.10
1802 * "Implicit Conversions". In the latter case, an implicit conversion
1803 * will be done on the initializer before the assignment is done."
1805 exec_list actual_parameters
;
1807 const unsigned parameter_count
=
1808 process_parameters(instructions
, &actual_parameters
, parameters
,
1811 if (parameter_count
!= constructor_type
->length
) {
1812 _mesa_glsl_error(loc
, state
,
1813 "%s parameters in constructor for `%s'",
1814 parameter_count
> constructor_type
->length
1815 ? "too many": "insufficient",
1816 constructor_type
->name
);
1817 return ir_rvalue::error_value(ctx
);
1820 bool all_parameters_are_constant
= true;
1823 /* Type cast each parameter and, if possible, fold constants. */
1824 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1826 const glsl_struct_field
*struct_field
=
1827 &constructor_type
->fields
.structure
[i
];
1829 /* Apply implicit conversions (not the scalar constructor rules, see the
1830 * spec quote above!) and attempt to convert the parameter to a constant
1831 * valued expression. After doing so, track whether or not all the
1832 * parameters to the constructor are trivially constant valued
1835 all_parameters_are_constant
&=
1836 implicitly_convert_component(ir
, struct_field
->type
->base_type
,
1839 if (ir
->type
!= struct_field
->type
) {
1840 _mesa_glsl_error(loc
, state
,
1841 "parameter type mismatch in constructor for `%s.%s' "
1843 constructor_type
->name
,
1846 struct_field
->type
->name
);
1847 return ir_rvalue::error_value(ctx
);
1853 if (all_parameters_are_constant
) {
1854 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1856 return emit_inline_record_constructor(constructor_type
, instructions
,
1857 &actual_parameters
, state
);
1862 ast_function_expression::handle_method(exec_list
*instructions
,
1863 struct _mesa_glsl_parse_state
*state
)
1865 const ast_expression
*field
= subexpressions
[0];
1869 /* Handle "method calls" in GLSL 1.20 - namely, array.length() */
1870 YYLTYPE loc
= get_location();
1871 state
->check_version(120, 300, &loc
, "methods not supported");
1874 method
= field
->primary_expression
.identifier
;
1876 /* This would prevent to raise "uninitialized variable" warnings when
1877 * calling array.length.
1879 field
->subexpressions
[0]->set_is_lhs(true);
1880 op
= field
->subexpressions
[0]->hir(instructions
, state
);
1881 if (strcmp(method
, "length") == 0) {
1882 if (!this->expressions
.is_empty()) {
1883 _mesa_glsl_error(&loc
, state
, "length method takes no arguments");
1887 if (op
->type
->is_array()) {
1888 if (op
->type
->is_unsized_array()) {
1889 if (!state
->has_shader_storage_buffer_objects()) {
1890 _mesa_glsl_error(&loc
, state
,
1891 "length called on unsized array"
1892 " only available with"
1893 " ARB_shader_storage_buffer_object");
1895 /* Calculate length of an unsized array in run-time */
1896 result
= new(ctx
) ir_expression(ir_unop_ssbo_unsized_array_length
,
1899 result
= new(ctx
) ir_constant(op
->type
->array_size());
1901 } else if (op
->type
->is_vector()) {
1902 if (state
->has_420pack()) {
1903 /* .length() returns int. */
1904 result
= new(ctx
) ir_constant((int) op
->type
->vector_elements
);
1906 _mesa_glsl_error(&loc
, state
, "length method on matrix only"
1907 " available with ARB_shading_language_420pack");
1910 } else if (op
->type
->is_matrix()) {
1911 if (state
->has_420pack()) {
1912 /* .length() returns int. */
1913 result
= new(ctx
) ir_constant((int) op
->type
->matrix_columns
);
1915 _mesa_glsl_error(&loc
, state
, "length method on matrix only"
1916 " available with ARB_shading_language_420pack");
1920 _mesa_glsl_error(&loc
, state
, "length called on scalar.");
1924 _mesa_glsl_error(&loc
, state
, "unknown method: `%s'", method
);
1929 return ir_rvalue::error_value(ctx
);
1933 ast_function_expression::hir(exec_list
*instructions
,
1934 struct _mesa_glsl_parse_state
*state
)
1937 /* There are three sorts of function calls.
1939 * 1. constructors - The first subexpression is an ast_type_specifier.
1940 * 2. methods - Only the .length() method of array types.
1941 * 3. functions - Calls to regular old functions.
1944 if (is_constructor()) {
1945 const ast_type_specifier
*type
=
1946 (ast_type_specifier
*) subexpressions
[0];
1947 YYLTYPE loc
= type
->get_location();
1950 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
1952 /* constructor_type can be NULL if a variable with the same name as the
1953 * structure has come into scope.
1955 if (constructor_type
== NULL
) {
1956 _mesa_glsl_error(& loc
, state
, "unknown type `%s' (structure name "
1957 "may be shadowed by a variable with the same name)",
1959 return ir_rvalue::error_value(ctx
);
1963 /* Constructors for opaque types are illegal.
1965 if (constructor_type
->contains_opaque()) {
1966 _mesa_glsl_error(& loc
, state
, "cannot construct opaque type `%s'",
1967 constructor_type
->name
);
1968 return ir_rvalue::error_value(ctx
);
1971 if (constructor_type
->is_subroutine()) {
1972 _mesa_glsl_error(& loc
, state
,
1973 "subroutine name cannot be a constructor `%s'",
1974 constructor_type
->name
);
1975 return ir_rvalue::error_value(ctx
);
1978 if (constructor_type
->is_array()) {
1979 if (!state
->check_version(120, 300, &loc
,
1980 "array constructors forbidden")) {
1981 return ir_rvalue::error_value(ctx
);
1984 return process_array_constructor(instructions
, constructor_type
,
1985 & loc
, &this->expressions
, state
);
1989 /* There are two kinds of constructor calls. Constructors for arrays and
1990 * structures must have the exact number of arguments with matching types
1991 * in the correct order. These constructors follow essentially the same
1992 * type matching rules as functions.
1994 * Constructors for built-in language types, such as mat4 and vec2, are
1995 * free form. The only requirements are that the parameters must provide
1996 * enough values of the correct scalar type and that no arguments are
1997 * given past the last used argument.
1999 * When using the C-style initializer syntax from GLSL 4.20, constructors
2000 * must have the exact number of arguments with matching types in the
2003 if (constructor_type
->is_record()) {
2004 return process_record_constructor(instructions
, constructor_type
,
2005 &loc
, &this->expressions
,
2009 if (!constructor_type
->is_numeric() && !constructor_type
->is_boolean())
2010 return ir_rvalue::error_value(ctx
);
2012 /* Total number of components of the type being constructed. */
2013 const unsigned type_components
= constructor_type
->components();
2015 /* Number of components from parameters that have actually been
2016 * consumed. This is used to perform several kinds of error checking.
2018 unsigned components_used
= 0;
2020 unsigned matrix_parameters
= 0;
2021 unsigned nonmatrix_parameters
= 0;
2022 exec_list actual_parameters
;
2024 foreach_list_typed(ast_node
, ast
, link
, &this->expressions
) {
2025 ir_rvalue
*result
= ast
->hir(instructions
, state
);
2027 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
2029 * "It is an error to provide extra arguments beyond this
2030 * last used argument."
2032 if (components_used
>= type_components
) {
2033 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
2035 constructor_type
->name
);
2036 return ir_rvalue::error_value(ctx
);
2039 if (!result
->type
->is_numeric() && !result
->type
->is_boolean()) {
2040 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
2041 "non-numeric data type",
2042 constructor_type
->name
);
2043 return ir_rvalue::error_value(ctx
);
2046 /* Count the number of matrix and nonmatrix parameters. This
2047 * is used below to enforce some of the constructor rules.
2049 if (result
->type
->is_matrix())
2050 matrix_parameters
++;
2052 nonmatrix_parameters
++;
2054 actual_parameters
.push_tail(result
);
2055 components_used
+= result
->type
->components();
2058 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
2060 * "It is an error to construct matrices from other matrices. This
2061 * is reserved for future use."
2063 if (matrix_parameters
> 0
2064 && constructor_type
->is_matrix()
2065 && !state
->check_version(120, 100, &loc
,
2066 "cannot construct `%s' from a matrix",
2067 constructor_type
->name
)) {
2068 return ir_rvalue::error_value(ctx
);
2071 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
2073 * "If a matrix argument is given to a matrix constructor, it is
2074 * an error to have any other arguments."
2076 if ((matrix_parameters
> 0)
2077 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
2078 && constructor_type
->is_matrix()) {
2079 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
2080 "matrix must be only parameter",
2081 constructor_type
->name
);
2082 return ir_rvalue::error_value(ctx
);
2085 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
2087 * "In these cases, there must be enough components provided in the
2088 * arguments to provide an initializer for every component in the
2089 * constructed value."
2091 if (components_used
< type_components
&& components_used
!= 1
2092 && matrix_parameters
== 0) {
2093 _mesa_glsl_error(& loc
, state
, "too few components to construct "
2095 constructor_type
->name
);
2096 return ir_rvalue::error_value(ctx
);
2099 /* Matrices can never be consumed as is by any constructor but matrix
2100 * constructors. If the constructor type is not matrix, always break the
2101 * matrix up into a series of column vectors.
2103 if (!constructor_type
->is_matrix()) {
2104 foreach_in_list_safe(ir_rvalue
, matrix
, &actual_parameters
) {
2105 if (!matrix
->type
->is_matrix())
2108 /* Create a temporary containing the matrix. */
2109 ir_variable
*var
= new(ctx
) ir_variable(matrix
->type
, "matrix_tmp",
2111 instructions
->push_tail(var
);
2112 instructions
->push_tail(
2113 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
2115 var
->constant_value
= matrix
->constant_expression_value();
2117 /* Replace the matrix with dereferences of its columns. */
2118 for (int i
= 0; i
< matrix
->type
->matrix_columns
; i
++) {
2119 matrix
->insert_before(
2120 new (ctx
) ir_dereference_array(var
,
2121 new(ctx
) ir_constant(i
)));
2127 bool all_parameters_are_constant
= true;
2129 /* Type cast each parameter and, if possible, fold constants.*/
2130 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
2131 const glsl_type
*desired_type
=
2132 glsl_type::get_instance(constructor_type
->base_type
,
2133 ir
->type
->vector_elements
,
2134 ir
->type
->matrix_columns
);
2135 ir_rvalue
*result
= convert_component(ir
, desired_type
);
2137 /* Attempt to convert the parameter to a constant valued expression.
2138 * After doing so, track whether or not all the parameters to the
2139 * constructor are trivially constant valued expressions.
2141 ir_rvalue
*const constant
= result
->constant_expression_value();
2143 if (constant
!= NULL
)
2146 all_parameters_are_constant
= false;
2149 ir
->replace_with(result
);
2153 /* If all of the parameters are trivially constant, create a
2154 * constant representing the complete collection of parameters.
2156 if (all_parameters_are_constant
) {
2157 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
2158 } else if (constructor_type
->is_scalar()) {
2159 return dereference_component((ir_rvalue
*)
2160 actual_parameters
.get_head_raw(),
2162 } else if (constructor_type
->is_vector()) {
2163 return emit_inline_vector_constructor(constructor_type
,
2168 assert(constructor_type
->is_matrix());
2169 return emit_inline_matrix_constructor(constructor_type
,
2174 } else if (subexpressions
[0]->oper
== ast_field_selection
) {
2175 return handle_method(instructions
, state
);
2177 const ast_expression
*id
= subexpressions
[0];
2178 const char *func_name
= NULL
;
2179 YYLTYPE loc
= get_location();
2180 exec_list actual_parameters
;
2181 ir_variable
*sub_var
= NULL
;
2182 ir_rvalue
*array_idx
= NULL
;
2184 process_parameters(instructions
, &actual_parameters
, &this->expressions
,
2187 if (id
->oper
== ast_array_index
) {
2188 array_idx
= generate_array_index(ctx
, instructions
, state
, loc
,
2189 id
->subexpressions
[0],
2190 id
->subexpressions
[1], &func_name
,
2191 &actual_parameters
);
2192 } else if (id
->oper
== ast_identifier
) {
2193 func_name
= id
->primary_expression
.identifier
;
2195 _mesa_glsl_error(&loc
, state
, "function name is not an identifier");
2198 /* an error was emitted earlier */
2200 return ir_rvalue::error_value(ctx
);
2202 ir_function_signature
*sig
=
2203 match_function_by_name(func_name
, &actual_parameters
, state
);
2205 ir_rvalue
*value
= NULL
;
2207 sig
= match_subroutine_by_name(func_name
, &actual_parameters
,
2212 no_matching_function_error(func_name
, &loc
,
2213 &actual_parameters
, state
);
2214 value
= ir_rvalue::error_value(ctx
);
2215 } else if (!verify_parameter_modes(state
, sig
,
2217 this->expressions
)) {
2218 /* an error has already been emitted */
2219 value
= ir_rvalue::error_value(ctx
);
2220 } else if (sig
->is_builtin() && strcmp(func_name
, "ftransform") == 0) {
2221 /* ftransform refers to global variables, and we don't have any code
2222 * for remapping the variable references in the built-in shader.
2225 state
->symbols
->get_variable("gl_ModelViewProjectionMatrix");
2226 ir_variable
*vtx
= state
->symbols
->get_variable("gl_Vertex");
2227 value
= new(ctx
) ir_expression(ir_binop_mul
, glsl_type::vec4_type
,
2228 new(ctx
) ir_dereference_variable(mvp
),
2229 new(ctx
) ir_dereference_variable(vtx
));
2231 if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
2232 sig
->is_builtin() && strcmp(func_name
, "barrier") == 0) {
2233 if (state
->current_function
== NULL
||
2234 strcmp(state
->current_function
->function_name(), "main") != 0) {
2235 _mesa_glsl_error(&loc
, state
,
2236 "barrier() may only be used in main()");
2239 if (state
->found_return
) {
2240 _mesa_glsl_error(&loc
, state
,
2241 "barrier() may not be used after return");
2244 if (instructions
!= &state
->current_function
->body
) {
2245 _mesa_glsl_error(&loc
, state
,
2246 "barrier() may not be used in control flow");
2250 value
= generate_call(instructions
, sig
, &actual_parameters
, sub_var
,
2251 array_idx
, state
, sig
->is_builtin());
2253 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::void_type
,
2256 instructions
->push_tail(tmp
);
2257 value
= new(ctx
) ir_dereference_variable(tmp
);
2264 unreachable("not reached");
2268 ast_function_expression::has_sequence_subexpression() const
2270 foreach_list_typed(const ast_node
, ast
, link
, &this->expressions
) {
2271 if (ast
->has_sequence_subexpression())
2279 ast_aggregate_initializer::hir(exec_list
*instructions
,
2280 struct _mesa_glsl_parse_state
*state
)
2283 YYLTYPE loc
= this->get_location();
2285 if (!this->constructor_type
) {
2286 _mesa_glsl_error(&loc
, state
, "type of C-style initializer unknown");
2287 return ir_rvalue::error_value(ctx
);
2289 const glsl_type
*const constructor_type
= this->constructor_type
;
2291 if (!state
->has_420pack()) {
2292 _mesa_glsl_error(&loc
, state
, "C-style initialization requires the "
2293 "GL_ARB_shading_language_420pack extension");
2294 return ir_rvalue::error_value(ctx
);
2297 if (constructor_type
->is_array()) {
2298 return process_array_constructor(instructions
, constructor_type
, &loc
,
2299 &this->expressions
, state
);
2302 if (constructor_type
->is_record()) {
2303 return process_record_constructor(instructions
, constructor_type
, &loc
,
2304 &this->expressions
, state
);
2307 return process_vec_mat_constructor(instructions
, constructor_type
, &loc
,
2308 &this->expressions
, state
);