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.
25 * \file ir_constant_expression.cpp
26 * Evaluate and process constant valued expressions
28 * In GLSL, constant valued expressions are used in several places. These
29 * must be processed and evaluated very early in the compilation process.
32 * * Initializers for uniforms
33 * * Initializers for \c const variables
37 #include "util/rounding.h" /* for _mesa_roundeven */
38 #include "util/half_float.h"
40 #include "compiler/glsl_types.h"
41 #include "util/hash_table.h"
42 #include "util/u_math.h"
45 dot_f(ir_constant
*op0
, ir_constant
*op1
)
47 assert(op0
->type
->is_float() && op1
->type
->is_float());
50 for (unsigned c
= 0; c
< op0
->type
->components(); c
++)
51 result
+= op0
->value
.f
[c
] * op1
->value
.f
[c
];
57 dot_d(ir_constant
*op0
, ir_constant
*op1
)
59 assert(op0
->type
->is_double() && op1
->type
->is_double());
62 for (unsigned c
= 0; c
< op0
->type
->components(); c
++)
63 result
+= op0
->value
.d
[c
] * op1
->value
.d
[c
];
68 /* This method is the only one supported by gcc. Unions in particular
69 * are iffy, and read-through-converted-pointer is killed by strict
70 * aliasing. OTOH, the compiler sees through the memcpy, so the
71 * resulting asm is reasonable.
74 bitcast_u2f(unsigned int u
)
76 static_assert(sizeof(float) == sizeof(unsigned int),
77 "float and unsigned int size mismatch");
79 memcpy(&f
, &u
, sizeof(f
));
86 static_assert(sizeof(float) == sizeof(unsigned int),
87 "float and unsigned int size mismatch");
89 memcpy(&u
, &f
, sizeof(f
));
94 bitcast_u642d(uint64_t u
)
96 static_assert(sizeof(double) == sizeof(uint64_t),
97 "double and uint64_t size mismatch");
99 memcpy(&d
, &u
, sizeof(d
));
104 bitcast_i642d(int64_t i
)
106 static_assert(sizeof(double) == sizeof(int64_t),
107 "double and int64_t size mismatch");
109 memcpy(&d
, &i
, sizeof(d
));
114 bitcast_d2u64(double d
)
116 static_assert(sizeof(double) == sizeof(uint64_t),
117 "double and uint64_t size mismatch");
119 memcpy(&u
, &d
, sizeof(d
));
124 bitcast_d2i64(double d
)
126 static_assert(sizeof(double) == sizeof(int64_t),
127 "double and int64_t size mismatch");
129 memcpy(&i
, &d
, sizeof(d
));
134 * Evaluate one component of a floating-point 4x8 unpacking function.
137 (*pack_1x8_func_t
)(float);
140 * Evaluate one component of a floating-point 2x16 unpacking function.
143 (*pack_1x16_func_t
)(float);
146 * Evaluate one component of a floating-point 4x8 unpacking function.
149 (*unpack_1x8_func_t
)(uint8_t);
152 * Evaluate one component of a floating-point 2x16 unpacking function.
155 (*unpack_1x16_func_t
)(uint16_t);
158 * Evaluate a 2x16 floating-point packing function.
161 pack_2x16(pack_1x16_func_t pack_1x16
,
164 /* From section 8.4 of the GLSL ES 3.00 spec:
168 * The first component of the vector will be written to the least
169 * significant bits of the output; the last component will be written to
170 * the most significant bits.
172 * The specifications for the other packing functions contain similar
176 u
|= ((uint32_t) pack_1x16(x
) << 0);
177 u
|= ((uint32_t) pack_1x16(y
) << 16);
182 * Evaluate a 4x8 floating-point packing function.
185 pack_4x8(pack_1x8_func_t pack_1x8
,
186 float x
, float y
, float z
, float w
)
188 /* From section 8.4 of the GLSL 4.30 spec:
192 * The first component of the vector will be written to the least
193 * significant bits of the output; the last component will be written to
194 * the most significant bits.
196 * The specifications for the other packing functions contain similar
200 u
|= ((uint32_t) pack_1x8(x
) << 0);
201 u
|= ((uint32_t) pack_1x8(y
) << 8);
202 u
|= ((uint32_t) pack_1x8(z
) << 16);
203 u
|= ((uint32_t) pack_1x8(w
) << 24);
208 * Evaluate a 2x16 floating-point unpacking function.
211 unpack_2x16(unpack_1x16_func_t unpack_1x16
,
215 /* From section 8.4 of the GLSL ES 3.00 spec:
219 * The first component of the returned vector will be extracted from
220 * the least significant bits of the input; the last component will be
221 * extracted from the most significant bits.
223 * The specifications for the other unpacking functions contain similar
226 *x
= unpack_1x16((uint16_t) (u
& 0xffff));
227 *y
= unpack_1x16((uint16_t) (u
>> 16));
231 * Evaluate a 4x8 floating-point unpacking function.
234 unpack_4x8(unpack_1x8_func_t unpack_1x8
, uint32_t u
,
235 float *x
, float *y
, float *z
, float *w
)
237 /* From section 8.4 of the GLSL 4.30 spec:
241 * The first component of the returned vector will be extracted from
242 * the least significant bits of the input; the last component will be
243 * extracted from the most significant bits.
245 * The specifications for the other unpacking functions contain similar
248 *x
= unpack_1x8((uint8_t) (u
& 0xff));
249 *y
= unpack_1x8((uint8_t) (u
>> 8));
250 *z
= unpack_1x8((uint8_t) (u
>> 16));
251 *w
= unpack_1x8((uint8_t) (u
>> 24));
255 * Evaluate one component of packSnorm4x8.
258 pack_snorm_1x8(float x
)
260 /* From section 8.4 of the GLSL 4.30 spec:
264 * The conversion for component c of v to fixed point is done as
267 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
270 _mesa_lroundevenf(CLAMP(x
, -1.0f
, +1.0f
) * 127.0f
);
274 * Evaluate one component of packSnorm2x16.
277 pack_snorm_1x16(float x
)
279 /* From section 8.4 of the GLSL ES 3.00 spec:
283 * The conversion for component c of v to fixed point is done as
286 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
289 _mesa_lroundevenf(CLAMP(x
, -1.0f
, +1.0f
) * 32767.0f
);
293 * Evaluate one component of unpackSnorm4x8.
296 unpack_snorm_1x8(uint8_t u
)
298 /* From section 8.4 of the GLSL 4.30 spec:
302 * The conversion for unpacked fixed-point value f to floating point is
305 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
307 return CLAMP((int8_t) u
/ 127.0f
, -1.0f
, +1.0f
);
311 * Evaluate one component of unpackSnorm2x16.
314 unpack_snorm_1x16(uint16_t u
)
316 /* From section 8.4 of the GLSL ES 3.00 spec:
320 * The conversion for unpacked fixed-point value f to floating point is
323 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
325 return CLAMP((int16_t) u
/ 32767.0f
, -1.0f
, +1.0f
);
329 * Evaluate one component packUnorm4x8.
332 pack_unorm_1x8(float x
)
334 /* From section 8.4 of the GLSL 4.30 spec:
338 * The conversion for component c of v to fixed point is done as
341 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
343 return (uint8_t) (int) _mesa_roundevenf(CLAMP(x
, 0.0f
, 1.0f
) * 255.0f
);
347 * Evaluate one component packUnorm2x16.
350 pack_unorm_1x16(float x
)
352 /* From section 8.4 of the GLSL ES 3.00 spec:
356 * The conversion for component c of v to fixed point is done as
359 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
361 return (uint16_t) (int)
362 _mesa_roundevenf(CLAMP(x
, 0.0f
, 1.0f
) * 65535.0f
);
366 * Evaluate one component of unpackUnorm4x8.
369 unpack_unorm_1x8(uint8_t u
)
371 /* From section 8.4 of the GLSL 4.30 spec:
375 * The conversion for unpacked fixed-point value f to floating point is
378 * unpackUnorm4x8: f / 255.0
380 return (float) u
/ 255.0f
;
384 * Evaluate one component of unpackUnorm2x16.
387 unpack_unorm_1x16(uint16_t u
)
389 /* From section 8.4 of the GLSL ES 3.00 spec:
393 * The conversion for unpacked fixed-point value f to floating point is
396 * unpackUnorm2x16: f / 65535.0
398 return (float) u
/ 65535.0f
;
402 * Evaluate one component of packHalf2x16.
405 pack_half_1x16(float x
)
407 return _mesa_float_to_half(x
);
411 * Evaluate one component of unpackHalf2x16.
414 unpack_half_1x16(uint16_t u
)
416 return _mesa_half_to_float(u
);
420 iadd_saturate(int32_t a
, int32_t b
)
422 return CLAMP(int64_t(a
) + int64_t(b
), INT32_MIN
, INT32_MAX
);
426 iadd64_saturate(int64_t a
, int64_t b
)
428 if (a
< 0 && b
< INT64_MIN
- a
)
431 if (a
> 0 && b
> INT64_MAX
- a
)
438 isub_saturate(int32_t a
, int32_t b
)
440 return CLAMP(int64_t(a
) - int64_t(b
), INT32_MIN
, INT32_MAX
);
444 isub64_saturate(int64_t a
, int64_t b
)
446 if (b
> 0 && a
< INT64_MIN
+ b
)
449 if (b
< 0 && a
> INT64_MAX
+ b
)
456 * Get the constant that is ultimately referenced by an r-value, in a constant
457 * expression evaluation context.
459 * The offset is used when the reference is to a specific column of a matrix.
462 constant_referenced(const ir_dereference
*deref
,
463 struct hash_table
*variable_context
,
464 ir_constant
*&store
, int &offset
)
469 if (variable_context
== NULL
)
472 switch (deref
->ir_type
) {
473 case ir_type_dereference_array
: {
474 const ir_dereference_array
*const da
=
475 (const ir_dereference_array
*) deref
;
477 ir_constant
*const index_c
=
478 da
->array_index
->constant_expression_value(variable_context
);
480 if (!index_c
|| !index_c
->type
->is_scalar() ||
481 !index_c
->type
->is_integer_32())
484 const int index
= index_c
->type
->base_type
== GLSL_TYPE_INT
?
485 index_c
->get_int_component(0) :
486 index_c
->get_uint_component(0);
488 ir_constant
*substore
;
491 const ir_dereference
*const deref
= da
->array
->as_dereference();
495 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
498 const glsl_type
*const vt
= da
->array
->type
;
499 if (vt
->is_array()) {
500 store
= substore
->get_array_element(index
);
502 } else if (vt
->is_matrix()) {
504 offset
= index
* vt
->vector_elements
;
505 } else if (vt
->is_vector()) {
507 offset
= suboffset
+ index
;
513 case ir_type_dereference_record
: {
514 const ir_dereference_record
*const dr
=
515 (const ir_dereference_record
*) deref
;
517 const ir_dereference
*const deref
= dr
->record
->as_dereference();
521 ir_constant
*substore
;
524 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
527 /* Since we're dropping it on the floor...
529 assert(suboffset
== 0);
531 store
= substore
->get_record_field(dr
->field_idx
);
535 case ir_type_dereference_variable
: {
536 const ir_dereference_variable
*const dv
=
537 (const ir_dereference_variable
*) deref
;
539 hash_entry
*entry
= _mesa_hash_table_search(variable_context
, dv
->var
);
541 store
= (ir_constant
*) entry
->data
;
546 assert(!"Should not get here.");
550 return store
!= NULL
;
555 ir_rvalue::constant_expression_value(void *, struct hash_table
*)
557 assert(this->type
->is_error());
562 bitfield_reverse(uint32_t v
)
564 /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
565 uint32_t r
= v
; // r will be reversed bits of v; first get LSB of v
566 int s
= sizeof(v
) * CHAR_BIT
- 1; // extra shift needed at end
568 for (v
>>= 1; v
; v
>>= 1) {
573 r
<<= s
; // shift when v's highest bits are zero
579 find_msb_uint(uint32_t v
)
583 /* If v == 0, then the loop will terminate when count == 32. In that case
584 * 31-count will produce the -1 result required by GLSL findMSB().
586 while (((v
& (1u << 31)) == 0) && count
!= 32) {
595 find_msb_int(int32_t v
)
597 /* If v is signed, findMSB() returns the position of the most significant
600 return find_msb_uint(v
< 0 ? ~v
: v
);
604 ldexpf_flush_subnormal(float x
, int exp
)
606 const float result
= ldexpf(x
, exp
);
608 /* Flush subnormal values to zero. */
609 return !isnormal(result
) ? copysignf(0.0f
, x
) : result
;
613 ldexp_flush_subnormal(double x
, int exp
)
615 const double result
= ldexp(x
, exp
);
617 /* Flush subnormal values to zero. */
618 return !isnormal(result
) ? copysign(0.0, x
) : result
;
622 bitfield_extract_uint(uint32_t value
, int offset
, int bits
)
626 else if (offset
< 0 || bits
< 0)
627 return 0; /* Undefined, per spec. */
628 else if (offset
+ bits
> 32)
629 return 0; /* Undefined, per spec. */
631 value
<<= 32 - bits
- offset
;
638 bitfield_extract_int(int32_t value
, int offset
, int bits
)
642 else if (offset
< 0 || bits
< 0)
643 return 0; /* Undefined, per spec. */
644 else if (offset
+ bits
> 32)
645 return 0; /* Undefined, per spec. */
647 value
<<= 32 - bits
- offset
;
654 bitfield_insert(uint32_t base
, uint32_t insert
, int offset
, int bits
)
658 else if (offset
< 0 || bits
< 0)
659 return 0; /* Undefined, per spec. */
660 else if (offset
+ bits
> 32)
661 return 0; /* Undefined, per spec. */
663 unsigned insert_mask
= ((1ull << bits
) - 1) << offset
;
666 insert
&= insert_mask
;
667 base
&= ~insert_mask
;
669 return base
| insert
;
674 ir_expression::constant_expression_value(void *mem_ctx
,
675 struct hash_table
*variable_context
)
679 if (this->type
->is_error())
682 ir_constant
*op
[ARRAY_SIZE(this->operands
)] = { NULL
, };
683 ir_constant_data data
;
685 memset(&data
, 0, sizeof(data
));
687 for (unsigned operand
= 0; operand
< this->num_operands
; operand
++) {
689 this->operands
[operand
]->constant_expression_value(mem_ctx
,
695 for (unsigned operand
= 0; operand
< this->num_operands
; operand
++) {
696 if (op
[operand
]->type
->base_type
== GLSL_TYPE_FLOAT16
) {
697 const struct glsl_type
*float_type
=
698 glsl_type::get_instance(GLSL_TYPE_FLOAT
,
699 op
[operand
]->type
->vector_elements
,
700 op
[operand
]->type
->matrix_columns
,
701 op
[operand
]->type
->explicit_stride
,
702 op
[operand
]->type
->interface_row_major
);
705 for (unsigned i
= 0; i
< ARRAY_SIZE(f
.f
); i
++)
706 f
.f
[i
] = _mesa_half_to_float(op
[operand
]->value
.f16
[i
]);
708 op
[operand
] = new(mem_ctx
) ir_constant(float_type
, &f
);
713 switch (this->operation
) {
714 case ir_binop_lshift
:
715 case ir_binop_rshift
:
717 case ir_binop_interpolate_at_offset
:
718 case ir_binop_interpolate_at_sample
:
719 case ir_binop_vector_extract
:
721 case ir_triop_bitfield_extract
:
725 assert(op
[0]->type
->base_type
== op
[1]->type
->base_type
);
729 bool op0_scalar
= op
[0]->type
->is_scalar();
730 bool op1_scalar
= op
[1] != NULL
&& op
[1]->type
->is_scalar();
732 /* When iterating over a vector or matrix's components, we want to increase
733 * the loop counter. However, for scalars, we want to stay at 0.
735 unsigned c0_inc
= op0_scalar
? 0 : 1;
736 unsigned c1_inc
= op1_scalar
? 0 : 1;
738 if (op1_scalar
|| !op
[1]) {
739 components
= op
[0]->type
->components();
741 components
= op
[1]->type
->components();
744 /* Handle array operations here, rather than below. */
745 if (op
[0]->type
->is_array()) {
746 assert(op
[1] != NULL
&& op
[1]->type
->is_array());
747 switch (this->operation
) {
748 case ir_binop_all_equal
:
749 return new(mem_ctx
) ir_constant(op
[0]->has_value(op
[1]));
750 case ir_binop_any_nequal
:
751 return new(mem_ctx
) ir_constant(!op
[0]->has_value(op
[1]));
758 #include "ir_expression_operation_constant.h"
760 if (this->type
->base_type
== GLSL_TYPE_FLOAT16
) {
762 for (unsigned i
= 0; i
< ARRAY_SIZE(f
.f16
); i
++)
763 f
.f16
[i
] = _mesa_float_to_half(data
.f
[i
]);
765 return new(mem_ctx
) ir_constant(this->type
, &f
);
769 return new(mem_ctx
) ir_constant(this->type
, &data
);
774 ir_texture::constant_expression_value(void *, struct hash_table
*)
776 /* texture lookups aren't constant expressions */
782 ir_swizzle::constant_expression_value(void *mem_ctx
,
783 struct hash_table
*variable_context
)
787 ir_constant
*v
= this->val
->constant_expression_value(mem_ctx
,
791 ir_constant_data data
= { { 0 } };
793 const unsigned swiz_idx
[4] = {
794 this->mask
.x
, this->mask
.y
, this->mask
.z
, this->mask
.w
797 for (unsigned i
= 0; i
< this->mask
.num_components
; i
++) {
798 switch (v
->type
->base_type
) {
800 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
801 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
802 case GLSL_TYPE_FLOAT16
: data
.f16
[i
] = v
->value
.f16
[swiz_idx
[i
]]; break;
803 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
804 case GLSL_TYPE_DOUBLE
:data
.d
[i
] = v
->value
.d
[swiz_idx
[i
]]; break;
805 case GLSL_TYPE_UINT64
:data
.u64
[i
] = v
->value
.u64
[swiz_idx
[i
]]; break;
806 case GLSL_TYPE_INT64
: data
.i64
[i
] = v
->value
.i64
[swiz_idx
[i
]]; break;
807 default: assert(!"Should not get here."); break;
811 return new(mem_ctx
) ir_constant(this->type
, &data
);
818 ir_dereference_variable::constant_expression_value(void *mem_ctx
,
819 struct hash_table
*variable_context
)
824 /* Give priority to the context hashtable, if it exists */
825 if (variable_context
) {
826 hash_entry
*entry
= _mesa_hash_table_search(variable_context
, var
);
829 return (ir_constant
*) entry
->data
;
832 /* The constant_value of a uniform variable is its initializer,
833 * not the lifetime constant value of the uniform.
835 if (var
->data
.mode
== ir_var_uniform
)
838 if (!var
->constant_value
)
841 return var
->constant_value
->clone(mem_ctx
, NULL
);
846 ir_dereference_array::constant_expression_value(void *mem_ctx
,
847 struct hash_table
*variable_context
)
851 ir_constant
*array
= this->array
->constant_expression_value(mem_ctx
, variable_context
);
852 ir_constant
*idx
= this->array_index
->constant_expression_value(mem_ctx
, variable_context
);
854 if ((array
!= NULL
) && (idx
!= NULL
)) {
855 if (array
->type
->is_matrix()) {
856 /* Array access of a matrix results in a vector.
858 const unsigned column
= idx
->value
.u
[0];
860 const glsl_type
*const column_type
= array
->type
->column_type();
862 /* Offset in the constant matrix to the first element of the column
865 const unsigned mat_idx
= column
* column_type
->vector_elements
;
867 ir_constant_data data
= { { 0 } };
869 switch (column_type
->base_type
) {
872 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
873 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
877 case GLSL_TYPE_FLOAT
:
878 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
879 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
883 case GLSL_TYPE_DOUBLE
:
884 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
885 data
.d
[i
] = array
->value
.d
[mat_idx
+ i
];
890 assert(!"Should not get here.");
894 return new(mem_ctx
) ir_constant(column_type
, &data
);
895 } else if (array
->type
->is_vector()) {
896 const unsigned component
= idx
->value
.u
[0];
898 return new(mem_ctx
) ir_constant(array
, component
);
899 } else if (array
->type
->is_array()) {
900 const unsigned index
= idx
->value
.u
[0];
901 return array
->get_array_element(index
)->clone(mem_ctx
, NULL
);
909 ir_dereference_record::constant_expression_value(void *mem_ctx
,
914 ir_constant
*v
= this->record
->constant_expression_value(mem_ctx
);
916 return (v
!= NULL
) ? v
->get_record_field(this->field_idx
) : NULL
;
921 ir_assignment::constant_expression_value(void *, struct hash_table
*)
923 /* FINISHME: Handle CEs involving assignment (return RHS) */
929 ir_constant::constant_expression_value(void *, struct hash_table
*)
936 ir_call::constant_expression_value(void *mem_ctx
, struct hash_table
*variable_context
)
940 return this->callee
->constant_expression_value(mem_ctx
,
941 &this->actual_parameters
,
946 bool ir_function_signature::constant_expression_evaluate_expression_list(void *mem_ctx
,
947 const struct exec_list
&body
,
948 struct hash_table
*variable_context
,
949 ir_constant
**result
)
953 foreach_in_list(ir_instruction
, inst
, &body
) {
954 switch(inst
->ir_type
) {
956 /* (declare () type symbol) */
957 case ir_type_variable
: {
958 ir_variable
*var
= inst
->as_variable();
959 _mesa_hash_table_insert(variable_context
, var
, ir_constant::zero(this, var
->type
));
963 /* (assign [condition] (write-mask) (ref) (value)) */
964 case ir_type_assignment
: {
965 ir_assignment
*asg
= inst
->as_assignment();
966 if (asg
->condition
) {
968 asg
->condition
->constant_expression_value(mem_ctx
,
972 if (!cond
->get_bool_component(0))
976 ir_constant
*store
= NULL
;
979 if (!constant_referenced(asg
->lhs
, variable_context
, store
, offset
))
983 asg
->rhs
->constant_expression_value(mem_ctx
, variable_context
);
988 store
->copy_masked_offset(value
, offset
, asg
->write_mask
);
992 /* (return (expression)) */
996 inst
->as_return()->value
->constant_expression_value(mem_ctx
,
998 return *result
!= NULL
;
1000 /* (call name (ref) (params))*/
1001 case ir_type_call
: {
1002 ir_call
*call
= inst
->as_call();
1004 /* Just say no to void functions in constant expressions. We
1005 * don't need them at that point.
1008 if (!call
->return_deref
)
1011 ir_constant
*store
= NULL
;
1014 if (!constant_referenced(call
->return_deref
, variable_context
,
1018 ir_constant
*value
=
1019 call
->constant_expression_value(mem_ctx
, variable_context
);
1024 store
->copy_offset(value
, offset
);
1028 /* (if condition (then-instructions) (else-instructions)) */
1030 ir_if
*iif
= inst
->as_if();
1033 iif
->condition
->constant_expression_value(mem_ctx
,
1035 if (!cond
|| !cond
->type
->is_boolean())
1038 exec_list
&branch
= cond
->get_bool_component(0) ? iif
->then_instructions
: iif
->else_instructions
;
1041 if (!constant_expression_evaluate_expression_list(mem_ctx
, branch
,
1046 /* If there was a return in the branch chosen, drop out now. */
1053 /* Every other expression type, we drop out. */
1059 /* Reaching the end of the block is not an error condition */
1067 ir_function_signature::constant_expression_value(void *mem_ctx
,
1068 exec_list
*actual_parameters
,
1069 struct hash_table
*variable_context
)
1073 const glsl_type
*type
= this->return_type
;
1074 if (type
== glsl_type::void_type
)
1077 /* From the GLSL 1.20 spec, page 23:
1078 * "Function calls to user-defined functions (non-built-in functions)
1079 * cannot be used to form constant expressions."
1081 if (!this->is_builtin())
1085 * Of the builtin functions, only the texture lookups and the noise
1086 * ones must not be used in constant expressions. Texture instructions
1087 * include special ir_texture opcodes which can't be constant-folded (see
1088 * ir_texture::constant_expression_value). Noise functions, however, we
1089 * have to special case here.
1091 if (strcmp(this->function_name(), "noise1") == 0 ||
1092 strcmp(this->function_name(), "noise2") == 0 ||
1093 strcmp(this->function_name(), "noise3") == 0 ||
1094 strcmp(this->function_name(), "noise4") == 0)
1097 /* Initialize the table of dereferencable names with the function
1098 * parameters. Verify their const-ness on the way.
1100 * We expect the correctness of the number of parameters to have
1101 * been checked earlier.
1103 hash_table
*deref_hash
= _mesa_pointer_hash_table_create(NULL
);
1105 /* If "origin" is non-NULL, then the function body is there. So we
1106 * have to use the variable objects from the object with the body,
1107 * but the parameter instanciation on the current object.
1109 const exec_node
*parameter_info
= origin
? origin
->parameters
.get_head_raw() : parameters
.get_head_raw();
1111 foreach_in_list(ir_rvalue
, n
, actual_parameters
) {
1112 ir_constant
*constant
=
1113 n
->constant_expression_value(mem_ctx
, variable_context
);
1114 if (constant
== NULL
) {
1115 _mesa_hash_table_destroy(deref_hash
, NULL
);
1120 ir_variable
*var
= (ir_variable
*)parameter_info
;
1121 _mesa_hash_table_insert(deref_hash
, var
, constant
);
1123 parameter_info
= parameter_info
->next
;
1126 ir_constant
*result
= NULL
;
1128 /* Now run the builtin function until something non-constant
1129 * happens or we get the result.
1131 if (constant_expression_evaluate_expression_list(mem_ctx
, origin
? origin
->body
: body
, deref_hash
, &result
) &&
1133 result
= result
->clone(mem_ctx
, NULL
);
1135 _mesa_hash_table_destroy(deref_hash
, NULL
);