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 "main/core.h" /* for MAX2, MIN2, CLAMP */
38 #include "util/rounding.h" /* for _mesa_roundeven */
40 #include "glsl_types.h"
41 #include "program/hash_table.h"
43 #if defined(__SUNPRO_CC) && !defined(isnormal)
45 static int isnormal(double x
)
47 return fpclass(x
) == FP_NORMAL
;
52 dot_f(ir_constant
*op0
, ir_constant
*op1
)
54 assert(op0
->type
->is_float() && op1
->type
->is_float());
57 for (unsigned c
= 0; c
< op0
->type
->components(); c
++)
58 result
+= op0
->value
.f
[c
] * op1
->value
.f
[c
];
64 dot_d(ir_constant
*op0
, ir_constant
*op1
)
66 assert(op0
->type
->is_double() && op1
->type
->is_double());
69 for (unsigned c
= 0; c
< op0
->type
->components(); c
++)
70 result
+= op0
->value
.d
[c
] * op1
->value
.d
[c
];
75 /* This method is the only one supported by gcc. Unions in particular
76 * are iffy, and read-through-converted-pointer is killed by strict
77 * aliasing. OTOH, the compiler sees through the memcpy, so the
78 * resulting asm is reasonable.
81 bitcast_u2f(unsigned int u
)
83 assert(sizeof(float) == sizeof(unsigned int));
85 memcpy(&f
, &u
, sizeof(f
));
92 assert(sizeof(float) == sizeof(unsigned int));
94 memcpy(&u
, &f
, sizeof(f
));
99 * Evaluate one component of a floating-point 4x8 unpacking function.
102 (*pack_1x8_func_t
)(float);
105 * Evaluate one component of a floating-point 2x16 unpacking function.
108 (*pack_1x16_func_t
)(float);
111 * Evaluate one component of a floating-point 4x8 unpacking function.
114 (*unpack_1x8_func_t
)(uint8_t);
117 * Evaluate one component of a floating-point 2x16 unpacking function.
120 (*unpack_1x16_func_t
)(uint16_t);
123 * Evaluate a 2x16 floating-point packing function.
126 pack_2x16(pack_1x16_func_t pack_1x16
,
129 /* From section 8.4 of the GLSL ES 3.00 spec:
133 * The first component of the vector will be written to the least
134 * significant bits of the output; the last component will be written to
135 * the most significant bits.
137 * The specifications for the other packing functions contain similar
141 u
|= ((uint32_t) pack_1x16(x
) << 0);
142 u
|= ((uint32_t) pack_1x16(y
) << 16);
147 * Evaluate a 4x8 floating-point packing function.
150 pack_4x8(pack_1x8_func_t pack_1x8
,
151 float x
, float y
, float z
, float w
)
153 /* From section 8.4 of the GLSL 4.30 spec:
157 * The first component of the vector will be written to the least
158 * significant bits of the output; the last component will be written to
159 * the most significant bits.
161 * The specifications for the other packing functions contain similar
165 u
|= ((uint32_t) pack_1x8(x
) << 0);
166 u
|= ((uint32_t) pack_1x8(y
) << 8);
167 u
|= ((uint32_t) pack_1x8(z
) << 16);
168 u
|= ((uint32_t) pack_1x8(w
) << 24);
173 * Evaluate a 2x16 floating-point unpacking function.
176 unpack_2x16(unpack_1x16_func_t unpack_1x16
,
180 /* From section 8.4 of the GLSL ES 3.00 spec:
184 * The first component of the returned vector will be extracted from
185 * the least significant bits of the input; the last component will be
186 * extracted from the most significant bits.
188 * The specifications for the other unpacking functions contain similar
191 *x
= unpack_1x16((uint16_t) (u
& 0xffff));
192 *y
= unpack_1x16((uint16_t) (u
>> 16));
196 * Evaluate a 4x8 floating-point unpacking function.
199 unpack_4x8(unpack_1x8_func_t unpack_1x8
, uint32_t u
,
200 float *x
, float *y
, float *z
, float *w
)
202 /* From section 8.4 of the GLSL 4.30 spec:
206 * The first component of the returned vector will be extracted from
207 * the least significant bits of the input; the last component will be
208 * extracted from the most significant bits.
210 * The specifications for the other unpacking functions contain similar
213 *x
= unpack_1x8((uint8_t) (u
& 0xff));
214 *y
= unpack_1x8((uint8_t) (u
>> 8));
215 *z
= unpack_1x8((uint8_t) (u
>> 16));
216 *w
= unpack_1x8((uint8_t) (u
>> 24));
220 * Evaluate one component of packSnorm4x8.
223 pack_snorm_1x8(float x
)
225 /* From section 8.4 of the GLSL 4.30 spec:
229 * The conversion for component c of v to fixed point is done as
232 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
235 _mesa_lroundevenf(CLAMP(x
, -1.0f
, +1.0f
) * 127.0f
);
239 * Evaluate one component of packSnorm2x16.
242 pack_snorm_1x16(float x
)
244 /* From section 8.4 of the GLSL ES 3.00 spec:
248 * The conversion for component c of v to fixed point is done as
251 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
254 _mesa_lroundevenf(CLAMP(x
, -1.0f
, +1.0f
) * 32767.0f
);
258 * Evaluate one component of unpackSnorm4x8.
261 unpack_snorm_1x8(uint8_t u
)
263 /* From section 8.4 of the GLSL 4.30 spec:
267 * The conversion for unpacked fixed-point value f to floating point is
270 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
272 return CLAMP((int8_t) u
/ 127.0f
, -1.0f
, +1.0f
);
276 * Evaluate one component of unpackSnorm2x16.
279 unpack_snorm_1x16(uint16_t u
)
281 /* From section 8.4 of the GLSL ES 3.00 spec:
285 * The conversion for unpacked fixed-point value f to floating point is
288 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
290 return CLAMP((int16_t) u
/ 32767.0f
, -1.0f
, +1.0f
);
294 * Evaluate one component packUnorm4x8.
297 pack_unorm_1x8(float x
)
299 /* From section 8.4 of the GLSL 4.30 spec:
303 * The conversion for component c of v to fixed point is done as
306 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
308 return (uint8_t) (int) _mesa_roundevenf(CLAMP(x
, 0.0f
, 1.0f
) * 255.0f
);
312 * Evaluate one component packUnorm2x16.
315 pack_unorm_1x16(float x
)
317 /* From section 8.4 of the GLSL ES 3.00 spec:
321 * The conversion for component c of v to fixed point is done as
324 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
326 return (uint16_t) (int)
327 _mesa_roundevenf(CLAMP(x
, 0.0f
, 1.0f
) * 65535.0f
);
331 * Evaluate one component of unpackUnorm4x8.
334 unpack_unorm_1x8(uint8_t u
)
336 /* From section 8.4 of the GLSL 4.30 spec:
340 * The conversion for unpacked fixed-point value f to floating point is
343 * unpackUnorm4x8: f / 255.0
345 return (float) u
/ 255.0f
;
349 * Evaluate one component of unpackUnorm2x16.
352 unpack_unorm_1x16(uint16_t u
)
354 /* From section 8.4 of the GLSL ES 3.00 spec:
358 * The conversion for unpacked fixed-point value f to floating point is
361 * unpackUnorm2x16: f / 65535.0
363 return (float) u
/ 65535.0f
;
367 * Evaluate one component of packHalf2x16.
370 pack_half_1x16(float x
)
372 return _mesa_float_to_half(x
);
376 * Evaluate one component of unpackHalf2x16.
379 unpack_half_1x16(uint16_t u
)
381 return _mesa_half_to_float(u
);
385 * Get the constant that is ultimately referenced by an r-value, in a constant
386 * expression evaluation context.
388 * The offset is used when the reference is to a specific column of a matrix.
391 constant_referenced(const ir_dereference
*deref
,
392 struct hash_table
*variable_context
,
393 ir_constant
*&store
, int &offset
)
398 if (variable_context
== NULL
)
401 switch (deref
->ir_type
) {
402 case ir_type_dereference_array
: {
403 const ir_dereference_array
*const da
=
404 (const ir_dereference_array
*) deref
;
406 ir_constant
*const index_c
=
407 da
->array_index
->constant_expression_value(variable_context
);
409 if (!index_c
|| !index_c
->type
->is_scalar() || !index_c
->type
->is_integer())
412 const int index
= index_c
->type
->base_type
== GLSL_TYPE_INT
?
413 index_c
->get_int_component(0) :
414 index_c
->get_uint_component(0);
416 ir_constant
*substore
;
419 const ir_dereference
*const deref
= da
->array
->as_dereference();
423 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
426 const glsl_type
*const vt
= da
->array
->type
;
427 if (vt
->is_array()) {
428 store
= substore
->get_array_element(index
);
430 } else if (vt
->is_matrix()) {
432 offset
= index
* vt
->vector_elements
;
433 } else if (vt
->is_vector()) {
435 offset
= suboffset
+ index
;
441 case ir_type_dereference_record
: {
442 const ir_dereference_record
*const dr
=
443 (const ir_dereference_record
*) deref
;
445 const ir_dereference
*const deref
= dr
->record
->as_dereference();
449 ir_constant
*substore
;
452 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
455 /* Since we're dropping it on the floor...
457 assert(suboffset
== 0);
459 store
= substore
->get_record_field(dr
->field
);
463 case ir_type_dereference_variable
: {
464 const ir_dereference_variable
*const dv
=
465 (const ir_dereference_variable
*) deref
;
467 store
= (ir_constant
*) hash_table_find(variable_context
, dv
->var
);
472 assert(!"Should not get here.");
476 return store
!= NULL
;
481 ir_rvalue::constant_expression_value(struct hash_table
*)
483 assert(this->type
->is_error());
488 ir_expression::constant_expression_value(struct hash_table
*variable_context
)
490 if (this->type
->is_error())
493 ir_constant
*op
[ARRAY_SIZE(this->operands
)] = { NULL
, };
494 ir_constant_data data
;
496 memset(&data
, 0, sizeof(data
));
498 for (unsigned operand
= 0; operand
< this->get_num_operands(); operand
++) {
499 op
[operand
] = this->operands
[operand
]->constant_expression_value(variable_context
);
505 switch (this->operation
) {
506 case ir_binop_lshift
:
507 case ir_binop_rshift
:
509 case ir_binop_interpolate_at_offset
:
510 case ir_binop_interpolate_at_sample
:
511 case ir_binop_vector_extract
:
513 case ir_triop_bitfield_extract
:
517 assert(op
[0]->type
->base_type
== op
[1]->type
->base_type
);
521 bool op0_scalar
= op
[0]->type
->is_scalar();
522 bool op1_scalar
= op
[1] != NULL
&& op
[1]->type
->is_scalar();
524 /* When iterating over a vector or matrix's components, we want to increase
525 * the loop counter. However, for scalars, we want to stay at 0.
527 unsigned c0_inc
= op0_scalar
? 0 : 1;
528 unsigned c1_inc
= op1_scalar
? 0 : 1;
530 if (op1_scalar
|| !op
[1]) {
531 components
= op
[0]->type
->components();
533 components
= op
[1]->type
->components();
536 void *ctx
= ralloc_parent(this);
538 /* Handle array operations here, rather than below. */
539 if (op
[0]->type
->is_array()) {
540 assert(op
[1] != NULL
&& op
[1]->type
->is_array());
541 switch (this->operation
) {
542 case ir_binop_all_equal
:
543 return new(ctx
) ir_constant(op
[0]->has_value(op
[1]));
544 case ir_binop_any_nequal
:
545 return new(ctx
) ir_constant(!op
[0]->has_value(op
[1]));
552 switch (this->operation
) {
553 case ir_unop_bit_not
:
554 switch (op
[0]->type
->base_type
) {
556 for (unsigned c
= 0; c
< components
; c
++)
557 data
.i
[c
] = ~ op
[0]->value
.i
[c
];
560 for (unsigned c
= 0; c
< components
; c
++)
561 data
.u
[c
] = ~ op
[0]->value
.u
[c
];
568 case ir_unop_logic_not
:
569 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
570 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
571 data
.b
[c
] = !op
[0]->value
.b
[c
];
575 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
576 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
577 data
.i
[c
] = (int) op
[0]->value
.f
[c
];
581 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
582 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
583 data
.i
[c
] = (unsigned) op
[0]->value
.f
[c
];
587 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
588 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
589 data
.f
[c
] = (float) op
[0]->value
.i
[c
];
593 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
594 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
595 data
.f
[c
] = (float) op
[0]->value
.u
[c
];
599 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
600 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
601 data
.f
[c
] = op
[0]->value
.b
[c
] ? 1.0F
: 0.0F
;
605 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
606 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
607 data
.b
[c
] = op
[0]->value
.f
[c
] != 0.0F
? true : false;
611 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
612 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
613 data
.u
[c
] = op
[0]->value
.b
[c
] ? 1 : 0;
617 assert(op
[0]->type
->is_integer());
618 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
619 data
.b
[c
] = op
[0]->value
.u
[c
] ? true : false;
623 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
624 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
625 data
.i
[c
] = op
[0]->value
.u
[c
];
629 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
630 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
631 data
.u
[c
] = op
[0]->value
.i
[c
];
634 case ir_unop_bitcast_i2f
:
635 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
636 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
637 data
.f
[c
] = bitcast_u2f(op
[0]->value
.i
[c
]);
640 case ir_unop_bitcast_f2i
:
641 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
642 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
643 data
.i
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
646 case ir_unop_bitcast_u2f
:
647 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
648 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
649 data
.f
[c
] = bitcast_u2f(op
[0]->value
.u
[c
]);
652 case ir_unop_bitcast_f2u
:
653 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
654 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
655 data
.u
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
659 assert(op
[0]->type
->is_boolean());
661 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
662 if (op
[0]->value
.b
[c
])
667 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
668 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
669 data
.f
[c
] = op
[0]->value
.d
[c
];
673 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
674 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
675 data
.d
[c
] = op
[0]->value
.f
[c
];
679 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
680 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
681 data
.i
[c
] = op
[0]->value
.d
[c
];
685 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
686 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
687 data
.d
[c
] = op
[0]->value
.i
[c
];
691 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
692 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
693 data
.u
[c
] = op
[0]->value
.d
[c
];
697 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
698 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
699 data
.d
[c
] = op
[0]->value
.u
[c
];
703 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
704 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
705 data
.b
[c
] = op
[0]->value
.d
[c
] != 0.0;
709 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
710 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
711 data
.d
[c
] = trunc(op
[0]->value
.d
[c
]);
713 data
.f
[c
] = truncf(op
[0]->value
.f
[c
]);
717 case ir_unop_round_even
:
718 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
719 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
720 data
.d
[c
] = _mesa_roundeven(op
[0]->value
.d
[c
]);
722 data
.f
[c
] = _mesa_roundevenf(op
[0]->value
.f
[c
]);
727 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
728 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
729 data
.d
[c
] = ceil(op
[0]->value
.d
[c
]);
731 data
.f
[c
] = ceilf(op
[0]->value
.f
[c
]);
736 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
737 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
738 data
.d
[c
] = floor(op
[0]->value
.d
[c
]);
740 data
.f
[c
] = floorf(op
[0]->value
.f
[c
]);
745 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
746 switch (this->type
->base_type
) {
753 case GLSL_TYPE_FLOAT
:
754 data
.f
[c
] = op
[0]->value
.f
[c
] - floor(op
[0]->value
.f
[c
]);
756 case GLSL_TYPE_DOUBLE
:
757 data
.d
[c
] = op
[0]->value
.d
[c
] - floor(op
[0]->value
.d
[c
]);
766 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
767 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
768 data
.f
[c
] = sinf(op
[0]->value
.f
[c
]);
773 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
774 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
775 data
.f
[c
] = cosf(op
[0]->value
.f
[c
]);
780 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
781 switch (this->type
->base_type
) {
783 data
.u
[c
] = -((int) op
[0]->value
.u
[c
]);
786 data
.i
[c
] = -op
[0]->value
.i
[c
];
788 case GLSL_TYPE_FLOAT
:
789 data
.f
[c
] = -op
[0]->value
.f
[c
];
791 case GLSL_TYPE_DOUBLE
:
792 data
.d
[c
] = -op
[0]->value
.d
[c
];
801 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
802 switch (this->type
->base_type
) {
804 data
.u
[c
] = op
[0]->value
.u
[c
];
807 data
.i
[c
] = op
[0]->value
.i
[c
];
809 data
.i
[c
] = -data
.i
[c
];
811 case GLSL_TYPE_FLOAT
:
812 data
.f
[c
] = fabs(op
[0]->value
.f
[c
]);
814 case GLSL_TYPE_DOUBLE
:
815 data
.d
[c
] = fabs(op
[0]->value
.d
[c
]);
824 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
825 switch (this->type
->base_type
) {
827 data
.u
[c
] = op
[0]->value
.i
[c
] > 0;
830 data
.i
[c
] = (op
[0]->value
.i
[c
] > 0) - (op
[0]->value
.i
[c
] < 0);
832 case GLSL_TYPE_FLOAT
:
833 data
.f
[c
] = float((op
[0]->value
.f
[c
] > 0)-(op
[0]->value
.f
[c
] < 0));
835 case GLSL_TYPE_DOUBLE
:
836 data
.d
[c
] = double((op
[0]->value
.d
[c
] > 0)-(op
[0]->value
.d
[c
] < 0));
845 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
846 switch (this->type
->base_type
) {
848 if (op
[0]->value
.u
[c
] != 0.0)
849 data
.u
[c
] = 1 / op
[0]->value
.u
[c
];
852 if (op
[0]->value
.i
[c
] != 0.0)
853 data
.i
[c
] = 1 / op
[0]->value
.i
[c
];
855 case GLSL_TYPE_FLOAT
:
856 if (op
[0]->value
.f
[c
] != 0.0)
857 data
.f
[c
] = 1.0F
/ op
[0]->value
.f
[c
];
859 case GLSL_TYPE_DOUBLE
:
860 if (op
[0]->value
.d
[c
] != 0.0)
861 data
.d
[c
] = 1.0 / op
[0]->value
.d
[c
];
870 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
871 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
872 data
.d
[c
] = 1.0 / sqrt(op
[0]->value
.d
[c
]);
874 data
.f
[c
] = 1.0F
/ sqrtf(op
[0]->value
.f
[c
]);
879 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
880 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
881 data
.d
[c
] = sqrt(op
[0]->value
.d
[c
]);
883 data
.f
[c
] = sqrtf(op
[0]->value
.f
[c
]);
888 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
889 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
890 data
.f
[c
] = expf(op
[0]->value
.f
[c
]);
895 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
896 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
897 data
.f
[c
] = exp2f(op
[0]->value
.f
[c
]);
902 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
903 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
904 data
.f
[c
] = logf(op
[0]->value
.f
[c
]);
909 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
910 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
911 data
.f
[c
] = log2f(op
[0]->value
.f
[c
]);
916 case ir_unop_dFdx_coarse
:
917 case ir_unop_dFdx_fine
:
919 case ir_unop_dFdy_coarse
:
920 case ir_unop_dFdy_fine
:
921 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
922 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
927 case ir_unop_pack_snorm_2x16
:
928 assert(op
[0]->type
== glsl_type::vec2_type
);
929 data
.u
[0] = pack_2x16(pack_snorm_1x16
,
933 case ir_unop_pack_snorm_4x8
:
934 assert(op
[0]->type
== glsl_type::vec4_type
);
935 data
.u
[0] = pack_4x8(pack_snorm_1x8
,
941 case ir_unop_unpack_snorm_2x16
:
942 assert(op
[0]->type
== glsl_type::uint_type
);
943 unpack_2x16(unpack_snorm_1x16
,
945 &data
.f
[0], &data
.f
[1]);
947 case ir_unop_unpack_snorm_4x8
:
948 assert(op
[0]->type
== glsl_type::uint_type
);
949 unpack_4x8(unpack_snorm_1x8
,
951 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
953 case ir_unop_pack_unorm_2x16
:
954 assert(op
[0]->type
== glsl_type::vec2_type
);
955 data
.u
[0] = pack_2x16(pack_unorm_1x16
,
959 case ir_unop_pack_unorm_4x8
:
960 assert(op
[0]->type
== glsl_type::vec4_type
);
961 data
.u
[0] = pack_4x8(pack_unorm_1x8
,
967 case ir_unop_unpack_unorm_2x16
:
968 assert(op
[0]->type
== glsl_type::uint_type
);
969 unpack_2x16(unpack_unorm_1x16
,
971 &data
.f
[0], &data
.f
[1]);
973 case ir_unop_unpack_unorm_4x8
:
974 assert(op
[0]->type
== glsl_type::uint_type
);
975 unpack_4x8(unpack_unorm_1x8
,
977 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
979 case ir_unop_pack_half_2x16
:
980 assert(op
[0]->type
== glsl_type::vec2_type
);
981 data
.u
[0] = pack_2x16(pack_half_1x16
,
985 case ir_unop_unpack_half_2x16
:
986 assert(op
[0]->type
== glsl_type::uint_type
);
987 unpack_2x16(unpack_half_1x16
,
989 &data
.f
[0], &data
.f
[1]);
992 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
993 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
994 data
.f
[c
] = powf(op
[0]->value
.f
[c
], op
[1]->value
.f
[c
]);
999 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1000 data
.d
[0] = dot_d(op
[0], op
[1]);
1002 data
.f
[0] = dot_f(op
[0], op
[1]);
1006 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1007 for (unsigned c
= 0, c0
= 0, c1
= 0;
1009 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1011 switch (op
[0]->type
->base_type
) {
1012 case GLSL_TYPE_UINT
:
1013 data
.u
[c
] = MIN2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
1016 data
.i
[c
] = MIN2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
1018 case GLSL_TYPE_FLOAT
:
1019 data
.f
[c
] = MIN2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
1021 case GLSL_TYPE_DOUBLE
:
1022 data
.d
[c
] = MIN2(op
[0]->value
.d
[c0
], op
[1]->value
.d
[c1
]);
1031 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1032 for (unsigned c
= 0, c0
= 0, c1
= 0;
1034 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1036 switch (op
[0]->type
->base_type
) {
1037 case GLSL_TYPE_UINT
:
1038 data
.u
[c
] = MAX2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
1041 data
.i
[c
] = MAX2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
1043 case GLSL_TYPE_FLOAT
:
1044 data
.f
[c
] = MAX2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
1046 case GLSL_TYPE_DOUBLE
:
1047 data
.d
[c
] = MAX2(op
[0]->value
.d
[c0
], op
[1]->value
.d
[c1
]);
1056 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1057 for (unsigned c
= 0, c0
= 0, c1
= 0;
1059 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1061 switch (op
[0]->type
->base_type
) {
1062 case GLSL_TYPE_UINT
:
1063 data
.u
[c
] = op
[0]->value
.u
[c0
] + op
[1]->value
.u
[c1
];
1066 data
.i
[c
] = op
[0]->value
.i
[c0
] + op
[1]->value
.i
[c1
];
1068 case GLSL_TYPE_FLOAT
:
1069 data
.f
[c
] = op
[0]->value
.f
[c0
] + op
[1]->value
.f
[c1
];
1071 case GLSL_TYPE_DOUBLE
:
1072 data
.d
[c
] = op
[0]->value
.d
[c0
] + op
[1]->value
.d
[c1
];
1081 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1082 for (unsigned c
= 0, c0
= 0, c1
= 0;
1084 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1086 switch (op
[0]->type
->base_type
) {
1087 case GLSL_TYPE_UINT
:
1088 data
.u
[c
] = op
[0]->value
.u
[c0
] - op
[1]->value
.u
[c1
];
1091 data
.i
[c
] = op
[0]->value
.i
[c0
] - op
[1]->value
.i
[c1
];
1093 case GLSL_TYPE_FLOAT
:
1094 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
];
1096 case GLSL_TYPE_DOUBLE
:
1097 data
.d
[c
] = op
[0]->value
.d
[c0
] - op
[1]->value
.d
[c1
];
1106 /* Check for equal types, or unequal types involving scalars */
1107 if ((op
[0]->type
== op
[1]->type
&& !op
[0]->type
->is_matrix())
1108 || op0_scalar
|| op1_scalar
) {
1109 for (unsigned c
= 0, c0
= 0, c1
= 0;
1111 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1113 switch (op
[0]->type
->base_type
) {
1114 case GLSL_TYPE_UINT
:
1115 data
.u
[c
] = op
[0]->value
.u
[c0
] * op
[1]->value
.u
[c1
];
1118 data
.i
[c
] = op
[0]->value
.i
[c0
] * op
[1]->value
.i
[c1
];
1120 case GLSL_TYPE_FLOAT
:
1121 data
.f
[c
] = op
[0]->value
.f
[c0
] * op
[1]->value
.f
[c1
];
1123 case GLSL_TYPE_DOUBLE
:
1124 data
.d
[c
] = op
[0]->value
.d
[c0
] * op
[1]->value
.d
[c1
];
1131 assert(op
[0]->type
->is_matrix() || op
[1]->type
->is_matrix());
1133 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
1134 * matrix can be a GLSL vector, either N or P can be 1.
1136 * For vec*mat, the vector is treated as a row vector. This
1137 * means the vector is a 1-row x M-column matrix.
1139 * For mat*vec, the vector is treated as a column vector. Since
1140 * matrix_columns is 1 for vectors, this just works.
1142 const unsigned n
= op
[0]->type
->is_vector()
1143 ? 1 : op
[0]->type
->vector_elements
;
1144 const unsigned m
= op
[1]->type
->vector_elements
;
1145 const unsigned p
= op
[1]->type
->matrix_columns
;
1146 for (unsigned j
= 0; j
< p
; j
++) {
1147 for (unsigned i
= 0; i
< n
; i
++) {
1148 for (unsigned k
= 0; k
< m
; k
++) {
1149 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1150 data
.d
[i
+n
*j
] += op
[0]->value
.d
[i
+n
*k
]*op
[1]->value
.d
[k
+m
*j
];
1152 data
.f
[i
+n
*j
] += op
[0]->value
.f
[i
+n
*k
]*op
[1]->value
.f
[k
+m
*j
];
1160 /* FINISHME: Emit warning when division-by-zero is detected. */
1161 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1162 for (unsigned c
= 0, c0
= 0, c1
= 0;
1164 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1166 switch (op
[0]->type
->base_type
) {
1167 case GLSL_TYPE_UINT
:
1168 if (op
[1]->value
.u
[c1
] == 0) {
1171 data
.u
[c
] = op
[0]->value
.u
[c0
] / op
[1]->value
.u
[c1
];
1175 if (op
[1]->value
.i
[c1
] == 0) {
1178 data
.i
[c
] = op
[0]->value
.i
[c0
] / op
[1]->value
.i
[c1
];
1181 case GLSL_TYPE_FLOAT
:
1182 data
.f
[c
] = op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
];
1184 case GLSL_TYPE_DOUBLE
:
1185 data
.d
[c
] = op
[0]->value
.d
[c0
] / op
[1]->value
.d
[c1
];
1194 /* FINISHME: Emit warning when division-by-zero is detected. */
1195 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1196 for (unsigned c
= 0, c0
= 0, c1
= 0;
1198 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1200 switch (op
[0]->type
->base_type
) {
1201 case GLSL_TYPE_UINT
:
1202 if (op
[1]->value
.u
[c1
] == 0) {
1205 data
.u
[c
] = op
[0]->value
.u
[c0
] % op
[1]->value
.u
[c1
];
1209 if (op
[1]->value
.i
[c1
] == 0) {
1212 data
.i
[c
] = op
[0]->value
.i
[c0
] % op
[1]->value
.i
[c1
];
1215 case GLSL_TYPE_FLOAT
:
1216 /* We don't use fmod because it rounds toward zero; GLSL specifies
1219 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
]
1220 * floorf(op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
]);
1222 case GLSL_TYPE_DOUBLE
:
1223 /* We don't use fmod because it rounds toward zero; GLSL specifies
1226 data
.d
[c
] = op
[0]->value
.d
[c0
] - op
[1]->value
.d
[c1
]
1227 * floor(op
[0]->value
.d
[c0
] / op
[1]->value
.d
[c1
]);
1236 case ir_binop_logic_and
:
1237 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1238 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1239 data
.b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
1241 case ir_binop_logic_xor
:
1242 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1243 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1244 data
.b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
1246 case ir_binop_logic_or
:
1247 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1248 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1249 data
.b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
1253 assert(op
[0]->type
== op
[1]->type
);
1254 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1255 switch (op
[0]->type
->base_type
) {
1256 case GLSL_TYPE_UINT
:
1257 data
.b
[c
] = op
[0]->value
.u
[c
] < op
[1]->value
.u
[c
];
1260 data
.b
[c
] = op
[0]->value
.i
[c
] < op
[1]->value
.i
[c
];
1262 case GLSL_TYPE_FLOAT
:
1263 data
.b
[c
] = op
[0]->value
.f
[c
] < op
[1]->value
.f
[c
];
1265 case GLSL_TYPE_DOUBLE
:
1266 data
.b
[c
] = op
[0]->value
.d
[c
] < op
[1]->value
.d
[c
];
1273 case ir_binop_greater
:
1274 assert(op
[0]->type
== op
[1]->type
);
1275 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1276 switch (op
[0]->type
->base_type
) {
1277 case GLSL_TYPE_UINT
:
1278 data
.b
[c
] = op
[0]->value
.u
[c
] > op
[1]->value
.u
[c
];
1281 data
.b
[c
] = op
[0]->value
.i
[c
] > op
[1]->value
.i
[c
];
1283 case GLSL_TYPE_FLOAT
:
1284 data
.b
[c
] = op
[0]->value
.f
[c
] > op
[1]->value
.f
[c
];
1286 case GLSL_TYPE_DOUBLE
:
1287 data
.b
[c
] = op
[0]->value
.d
[c
] > op
[1]->value
.d
[c
];
1294 case ir_binop_lequal
:
1295 assert(op
[0]->type
== op
[1]->type
);
1296 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1297 switch (op
[0]->type
->base_type
) {
1298 case GLSL_TYPE_UINT
:
1299 data
.b
[c
] = op
[0]->value
.u
[c
] <= op
[1]->value
.u
[c
];
1302 data
.b
[c
] = op
[0]->value
.i
[c
] <= op
[1]->value
.i
[c
];
1304 case GLSL_TYPE_FLOAT
:
1305 data
.b
[c
] = op
[0]->value
.f
[c
] <= op
[1]->value
.f
[c
];
1307 case GLSL_TYPE_DOUBLE
:
1308 data
.b
[c
] = op
[0]->value
.d
[c
] <= op
[1]->value
.d
[c
];
1315 case ir_binop_gequal
:
1316 assert(op
[0]->type
== op
[1]->type
);
1317 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1318 switch (op
[0]->type
->base_type
) {
1319 case GLSL_TYPE_UINT
:
1320 data
.b
[c
] = op
[0]->value
.u
[c
] >= op
[1]->value
.u
[c
];
1323 data
.b
[c
] = op
[0]->value
.i
[c
] >= op
[1]->value
.i
[c
];
1325 case GLSL_TYPE_FLOAT
:
1326 data
.b
[c
] = op
[0]->value
.f
[c
] >= op
[1]->value
.f
[c
];
1328 case GLSL_TYPE_DOUBLE
:
1329 data
.b
[c
] = op
[0]->value
.d
[c
] >= op
[1]->value
.d
[c
];
1336 case ir_binop_equal
:
1337 assert(op
[0]->type
== op
[1]->type
);
1338 for (unsigned c
= 0; c
< components
; c
++) {
1339 switch (op
[0]->type
->base_type
) {
1340 case GLSL_TYPE_UINT
:
1341 data
.b
[c
] = op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
1344 data
.b
[c
] = op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
1346 case GLSL_TYPE_FLOAT
:
1347 data
.b
[c
] = op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
1349 case GLSL_TYPE_BOOL
:
1350 data
.b
[c
] = op
[0]->value
.b
[c
] == op
[1]->value
.b
[c
];
1352 case GLSL_TYPE_DOUBLE
:
1353 data
.b
[c
] = op
[0]->value
.d
[c
] == op
[1]->value
.d
[c
];
1360 case ir_binop_nequal
:
1361 assert(op
[0]->type
== op
[1]->type
);
1362 for (unsigned c
= 0; c
< components
; c
++) {
1363 switch (op
[0]->type
->base_type
) {
1364 case GLSL_TYPE_UINT
:
1365 data
.b
[c
] = op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
1368 data
.b
[c
] = op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
1370 case GLSL_TYPE_FLOAT
:
1371 data
.b
[c
] = op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
1373 case GLSL_TYPE_BOOL
:
1374 data
.b
[c
] = op
[0]->value
.b
[c
] != op
[1]->value
.b
[c
];
1376 case GLSL_TYPE_DOUBLE
:
1377 data
.b
[c
] = op
[0]->value
.d
[c
] != op
[1]->value
.d
[c
];
1384 case ir_binop_all_equal
:
1385 data
.b
[0] = op
[0]->has_value(op
[1]);
1387 case ir_binop_any_nequal
:
1388 data
.b
[0] = !op
[0]->has_value(op
[1]);
1391 case ir_binop_lshift
:
1392 for (unsigned c
= 0, c0
= 0, c1
= 0;
1394 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1396 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1397 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1398 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.i
[c1
];
1400 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1401 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1402 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.u
[c1
];
1404 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1405 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1406 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.i
[c1
];
1408 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1409 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1410 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.u
[c1
];
1415 case ir_binop_rshift
:
1416 for (unsigned c
= 0, c0
= 0, c1
= 0;
1418 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1420 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1421 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1422 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.i
[c1
];
1424 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1425 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1426 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.u
[c1
];
1428 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1429 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1430 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.i
[c1
];
1432 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1433 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1434 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.u
[c1
];
1439 case ir_binop_bit_and
:
1440 for (unsigned c
= 0, c0
= 0, c1
= 0;
1442 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1444 switch (op
[0]->type
->base_type
) {
1446 data
.i
[c
] = op
[0]->value
.i
[c0
] & op
[1]->value
.i
[c1
];
1448 case GLSL_TYPE_UINT
:
1449 data
.u
[c
] = op
[0]->value
.u
[c0
] & op
[1]->value
.u
[c1
];
1457 case ir_binop_bit_or
:
1458 for (unsigned c
= 0, c0
= 0, c1
= 0;
1460 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1462 switch (op
[0]->type
->base_type
) {
1464 data
.i
[c
] = op
[0]->value
.i
[c0
] | op
[1]->value
.i
[c1
];
1466 case GLSL_TYPE_UINT
:
1467 data
.u
[c
] = op
[0]->value
.u
[c0
] | op
[1]->value
.u
[c1
];
1475 case ir_binop_vector_extract
: {
1476 const int c
= CLAMP(op
[1]->value
.i
[0], 0,
1477 (int) op
[0]->type
->vector_elements
- 1);
1479 switch (op
[0]->type
->base_type
) {
1480 case GLSL_TYPE_UINT
:
1481 data
.u
[0] = op
[0]->value
.u
[c
];
1484 data
.i
[0] = op
[0]->value
.i
[c
];
1486 case GLSL_TYPE_FLOAT
:
1487 data
.f
[0] = op
[0]->value
.f
[c
];
1489 case GLSL_TYPE_DOUBLE
:
1490 data
.d
[0] = op
[0]->value
.d
[c
];
1492 case GLSL_TYPE_BOOL
:
1493 data
.b
[0] = op
[0]->value
.b
[c
];
1501 case ir_binop_bit_xor
:
1502 for (unsigned c
= 0, c0
= 0, c1
= 0;
1504 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1506 switch (op
[0]->type
->base_type
) {
1508 data
.i
[c
] = op
[0]->value
.i
[c0
] ^ op
[1]->value
.i
[c1
];
1510 case GLSL_TYPE_UINT
:
1511 data
.u
[c
] = op
[0]->value
.u
[c0
] ^ op
[1]->value
.u
[c1
];
1519 case ir_unop_bitfield_reverse
:
1520 /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
1521 for (unsigned c
= 0; c
< components
; c
++) {
1522 unsigned int v
= op
[0]->value
.u
[c
]; // input bits to be reversed
1523 unsigned int r
= v
; // r will be reversed bits of v; first get LSB of v
1524 int s
= sizeof(v
) * CHAR_BIT
- 1; // extra shift needed at end
1526 for (v
>>= 1; v
; v
>>= 1) {
1531 r
<<= s
; // shift when v's highest bits are zero
1537 case ir_unop_bit_count
:
1538 for (unsigned c
= 0; c
< components
; c
++) {
1540 unsigned v
= op
[0]->value
.u
[c
];
1542 for (; v
; count
++) {
1549 case ir_unop_find_msb
:
1550 for (unsigned c
= 0; c
< components
; c
++) {
1551 int v
= op
[0]->value
.i
[c
];
1553 if (v
== 0 || (op
[0]->type
->base_type
== GLSL_TYPE_INT
&& v
== -1))
1557 int top_bit
= op
[0]->type
->base_type
== GLSL_TYPE_UINT
1558 ? 0 : v
& (1 << 31);
1560 while (((v
& (1 << 31)) == top_bit
) && count
!= 32) {
1565 data
.i
[c
] = 31 - count
;
1570 case ir_unop_find_lsb
:
1571 for (unsigned c
= 0; c
< components
; c
++) {
1572 if (op
[0]->value
.i
[c
] == 0)
1576 unsigned v
= op
[0]->value
.u
[c
];
1578 for (; !(v
& 1); v
>>= 1) {
1586 case ir_unop_saturate
:
1587 for (unsigned c
= 0; c
< components
; c
++) {
1588 data
.f
[c
] = CLAMP(op
[0]->value
.f
[c
], 0.0f
, 1.0f
);
1591 case ir_unop_pack_double_2x32
: {
1592 /* XXX needs to be checked on big-endian */
1594 temp
= (uint64_t)op
[0]->value
.u
[0] | ((uint64_t)op
[0]->value
.u
[1] << 32);
1595 data
.d
[0] = *(double *)&temp
;
1599 case ir_unop_unpack_double_2x32
:
1600 /* XXX needs to be checked on big-endian */
1601 data
.u
[0] = *(uint32_t *)&op
[0]->value
.d
[0];
1602 data
.u
[1] = *((uint32_t *)&op
[0]->value
.d
[0] + 1);
1605 case ir_triop_bitfield_extract
: {
1606 int offset
= op
[1]->value
.i
[0];
1607 int bits
= op
[2]->value
.i
[0];
1609 for (unsigned c
= 0; c
< components
; c
++) {
1612 else if (offset
< 0 || bits
< 0)
1613 data
.u
[c
] = 0; /* Undefined, per spec. */
1614 else if (offset
+ bits
> 32)
1615 data
.u
[c
] = 0; /* Undefined, per spec. */
1617 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
) {
1618 /* int so that the right shift will sign-extend. */
1619 int value
= op
[0]->value
.i
[c
];
1620 value
<<= 32 - bits
- offset
;
1621 value
>>= 32 - bits
;
1624 unsigned value
= op
[0]->value
.u
[c
];
1625 value
<<= 32 - bits
- offset
;
1626 value
>>= 32 - bits
;
1634 case ir_binop_bfm
: {
1635 int bits
= op
[0]->value
.i
[0];
1636 int offset
= op
[1]->value
.i
[0];
1638 for (unsigned c
= 0; c
< components
; c
++) {
1640 data
.u
[c
] = op
[0]->value
.u
[c
];
1641 else if (offset
< 0 || bits
< 0)
1642 data
.u
[c
] = 0; /* Undefined for bitfieldInsert, per spec. */
1643 else if (offset
+ bits
> 32)
1644 data
.u
[c
] = 0; /* Undefined for bitfieldInsert, per spec. */
1646 data
.u
[c
] = ((1 << bits
) - 1) << offset
;
1651 case ir_binop_ldexp
:
1652 for (unsigned c
= 0; c
< components
; c
++) {
1653 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
) {
1654 data
.d
[c
] = ldexp(op
[0]->value
.d
[c
], op
[1]->value
.i
[c
]);
1655 /* Flush subnormal values to zero. */
1656 if (!isnormal(data
.d
[c
]))
1657 data
.d
[c
] = copysign(0.0, op
[0]->value
.d
[c
]);
1659 data
.f
[c
] = ldexpf(op
[0]->value
.f
[c
], op
[1]->value
.i
[c
]);
1660 /* Flush subnormal values to zero. */
1661 if (!isnormal(data
.f
[c
]))
1662 data
.f
[c
] = copysignf(0.0f
, op
[0]->value
.f
[c
]);
1668 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
1669 op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1670 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
||
1671 op
[1]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1672 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
||
1673 op
[2]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1675 for (unsigned c
= 0; c
< components
; c
++) {
1676 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1677 data
.d
[c
] = op
[0]->value
.d
[c
] * op
[1]->value
.d
[c
]
1678 + op
[2]->value
.d
[c
];
1680 data
.f
[c
] = op
[0]->value
.f
[c
] * op
[1]->value
.f
[c
]
1681 + op
[2]->value
.f
[c
];
1685 case ir_triop_lrp
: {
1686 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
1687 op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1688 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
||
1689 op
[1]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1690 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
||
1691 op
[2]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1693 unsigned c2_inc
= op
[2]->type
->is_scalar() ? 0 : 1;
1694 for (unsigned c
= 0, c2
= 0; c
< components
; c2
+= c2_inc
, c
++) {
1695 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1696 data
.d
[c
] = op
[0]->value
.d
[c
] * (1.0 - op
[2]->value
.d
[c2
]) +
1697 (op
[1]->value
.d
[c
] * op
[2]->value
.d
[c2
]);
1699 data
.f
[c
] = op
[0]->value
.f
[c
] * (1.0f
- op
[2]->value
.f
[c2
]) +
1700 (op
[1]->value
.f
[c
] * op
[2]->value
.f
[c2
]);
1706 for (unsigned c
= 0; c
< components
; c
++) {
1707 if (op
[1]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1708 data
.d
[c
] = op
[0]->value
.b
[c
] ? op
[1]->value
.d
[c
]
1709 : op
[2]->value
.d
[c
];
1711 data
.u
[c
] = op
[0]->value
.b
[c
] ? op
[1]->value
.u
[c
]
1712 : op
[2]->value
.u
[c
];
1716 case ir_triop_vector_insert
: {
1717 const unsigned idx
= op
[2]->value
.u
[0];
1719 memcpy(&data
, &op
[0]->value
, sizeof(data
));
1721 switch (this->type
->base_type
) {
1723 data
.i
[idx
] = op
[1]->value
.i
[0];
1725 case GLSL_TYPE_UINT
:
1726 data
.u
[idx
] = op
[1]->value
.u
[0];
1728 case GLSL_TYPE_FLOAT
:
1729 data
.f
[idx
] = op
[1]->value
.f
[0];
1731 case GLSL_TYPE_BOOL
:
1732 data
.b
[idx
] = op
[1]->value
.b
[0];
1734 case GLSL_TYPE_DOUBLE
:
1735 data
.d
[idx
] = op
[1]->value
.d
[0];
1738 assert(!"Should not get here.");
1744 case ir_quadop_bitfield_insert
: {
1745 int offset
= op
[2]->value
.i
[0];
1746 int bits
= op
[3]->value
.i
[0];
1748 for (unsigned c
= 0; c
< components
; c
++) {
1750 data
.u
[c
] = op
[0]->value
.u
[c
];
1751 else if (offset
< 0 || bits
< 0)
1752 data
.u
[c
] = 0; /* Undefined, per spec. */
1753 else if (offset
+ bits
> 32)
1754 data
.u
[c
] = 0; /* Undefined, per spec. */
1756 unsigned insert_mask
= ((1 << bits
) - 1) << offset
;
1758 unsigned insert
= op
[1]->value
.u
[c
];
1760 insert
&= insert_mask
;
1762 unsigned base
= op
[0]->value
.u
[c
];
1763 base
&= ~insert_mask
;
1765 data
.u
[c
] = base
| insert
;
1771 case ir_quadop_vector
:
1772 for (unsigned c
= 0; c
< this->type
->vector_elements
; c
++) {
1773 switch (this->type
->base_type
) {
1775 data
.i
[c
] = op
[c
]->value
.i
[0];
1777 case GLSL_TYPE_UINT
:
1778 data
.u
[c
] = op
[c
]->value
.u
[0];
1780 case GLSL_TYPE_FLOAT
:
1781 data
.f
[c
] = op
[c
]->value
.f
[0];
1783 case GLSL_TYPE_DOUBLE
:
1784 data
.d
[c
] = op
[c
]->value
.d
[0];
1793 /* FINISHME: Should handle all expression types. */
1797 return new(ctx
) ir_constant(this->type
, &data
);
1802 ir_texture::constant_expression_value(struct hash_table
*)
1804 /* texture lookups aren't constant expressions */
1810 ir_swizzle::constant_expression_value(struct hash_table
*variable_context
)
1812 ir_constant
*v
= this->val
->constant_expression_value(variable_context
);
1815 ir_constant_data data
= { { 0 } };
1817 const unsigned swiz_idx
[4] = {
1818 this->mask
.x
, this->mask
.y
, this->mask
.z
, this->mask
.w
1821 for (unsigned i
= 0; i
< this->mask
.num_components
; i
++) {
1822 switch (v
->type
->base_type
) {
1823 case GLSL_TYPE_UINT
:
1824 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
1825 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
1826 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
1827 case GLSL_TYPE_DOUBLE
:data
.d
[i
] = v
->value
.d
[swiz_idx
[i
]]; break;
1828 default: assert(!"Should not get here."); break;
1832 void *ctx
= ralloc_parent(this);
1833 return new(ctx
) ir_constant(this->type
, &data
);
1840 ir_dereference_variable::constant_expression_value(struct hash_table
*variable_context
)
1842 /* This may occur during compile and var->type is glsl_type::error_type */
1846 /* Give priority to the context hashtable, if it exists */
1847 if (variable_context
) {
1848 ir_constant
*value
= (ir_constant
*)hash_table_find(variable_context
, var
);
1853 /* The constant_value of a uniform variable is its initializer,
1854 * not the lifetime constant value of the uniform.
1856 if (var
->data
.mode
== ir_var_uniform
)
1859 if (!var
->constant_value
)
1862 return var
->constant_value
->clone(ralloc_parent(var
), NULL
);
1867 ir_dereference_array::constant_expression_value(struct hash_table
*variable_context
)
1869 ir_constant
*array
= this->array
->constant_expression_value(variable_context
);
1870 ir_constant
*idx
= this->array_index
->constant_expression_value(variable_context
);
1872 if ((array
!= NULL
) && (idx
!= NULL
)) {
1873 void *ctx
= ralloc_parent(this);
1874 if (array
->type
->is_matrix()) {
1875 /* Array access of a matrix results in a vector.
1877 const unsigned column
= idx
->value
.u
[0];
1879 const glsl_type
*const column_type
= array
->type
->column_type();
1881 /* Offset in the constant matrix to the first element of the column
1884 const unsigned mat_idx
= column
* column_type
->vector_elements
;
1886 ir_constant_data data
= { { 0 } };
1888 switch (column_type
->base_type
) {
1889 case GLSL_TYPE_UINT
:
1891 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1892 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
1896 case GLSL_TYPE_FLOAT
:
1897 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1898 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
1902 case GLSL_TYPE_DOUBLE
:
1903 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1904 data
.d
[i
] = array
->value
.d
[mat_idx
+ i
];
1909 assert(!"Should not get here.");
1913 return new(ctx
) ir_constant(column_type
, &data
);
1914 } else if (array
->type
->is_vector()) {
1915 const unsigned component
= idx
->value
.u
[0];
1917 return new(ctx
) ir_constant(array
, component
);
1919 const unsigned index
= idx
->value
.u
[0];
1920 return array
->get_array_element(index
)->clone(ctx
, NULL
);
1928 ir_dereference_record::constant_expression_value(struct hash_table
*)
1930 ir_constant
*v
= this->record
->constant_expression_value();
1932 return (v
!= NULL
) ? v
->get_record_field(this->field
) : NULL
;
1937 ir_assignment::constant_expression_value(struct hash_table
*)
1939 /* FINISHME: Handle CEs involving assignment (return RHS) */
1945 ir_constant::constant_expression_value(struct hash_table
*)
1952 ir_call::constant_expression_value(struct hash_table
*variable_context
)
1954 return this->callee
->constant_expression_value(&this->actual_parameters
, variable_context
);
1958 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list
&body
,
1959 struct hash_table
*variable_context
,
1960 ir_constant
**result
)
1962 foreach_in_list(ir_instruction
, inst
, &body
) {
1963 switch(inst
->ir_type
) {
1965 /* (declare () type symbol) */
1966 case ir_type_variable
: {
1967 ir_variable
*var
= inst
->as_variable();
1968 hash_table_insert(variable_context
, ir_constant::zero(this, var
->type
), var
);
1972 /* (assign [condition] (write-mask) (ref) (value)) */
1973 case ir_type_assignment
: {
1974 ir_assignment
*asg
= inst
->as_assignment();
1975 if (asg
->condition
) {
1976 ir_constant
*cond
= asg
->condition
->constant_expression_value(variable_context
);
1979 if (!cond
->get_bool_component(0))
1983 ir_constant
*store
= NULL
;
1986 if (!constant_referenced(asg
->lhs
, variable_context
, store
, offset
))
1989 ir_constant
*value
= asg
->rhs
->constant_expression_value(variable_context
);
1994 store
->copy_masked_offset(value
, offset
, asg
->write_mask
);
1998 /* (return (expression)) */
1999 case ir_type_return
:
2001 *result
= inst
->as_return()->value
->constant_expression_value(variable_context
);
2002 return *result
!= NULL
;
2004 /* (call name (ref) (params))*/
2005 case ir_type_call
: {
2006 ir_call
*call
= inst
->as_call();
2008 /* Just say no to void functions in constant expressions. We
2009 * don't need them at that point.
2012 if (!call
->return_deref
)
2015 ir_constant
*store
= NULL
;
2018 if (!constant_referenced(call
->return_deref
, variable_context
,
2022 ir_constant
*value
= call
->constant_expression_value(variable_context
);
2027 store
->copy_offset(value
, offset
);
2031 /* (if condition (then-instructions) (else-instructions)) */
2033 ir_if
*iif
= inst
->as_if();
2035 ir_constant
*cond
= iif
->condition
->constant_expression_value(variable_context
);
2036 if (!cond
|| !cond
->type
->is_boolean())
2039 exec_list
&branch
= cond
->get_bool_component(0) ? iif
->then_instructions
: iif
->else_instructions
;
2042 if (!constant_expression_evaluate_expression_list(branch
, variable_context
, result
))
2045 /* If there was a return in the branch chosen, drop out now. */
2052 /* Every other expression type, we drop out. */
2058 /* Reaching the end of the block is not an error condition */
2066 ir_function_signature::constant_expression_value(exec_list
*actual_parameters
, struct hash_table
*variable_context
)
2068 const glsl_type
*type
= this->return_type
;
2069 if (type
== glsl_type::void_type
)
2072 /* From the GLSL 1.20 spec, page 23:
2073 * "Function calls to user-defined functions (non-built-in functions)
2074 * cannot be used to form constant expressions."
2076 if (!this->is_builtin())
2080 * Of the builtin functions, only the texture lookups and the noise
2081 * ones must not be used in constant expressions. They all include
2082 * specific opcodes so they don't need to be special-cased at this
2086 /* Initialize the table of dereferencable names with the function
2087 * parameters. Verify their const-ness on the way.
2089 * We expect the correctness of the number of parameters to have
2090 * been checked earlier.
2092 hash_table
*deref_hash
= hash_table_ctor(8, hash_table_pointer_hash
,
2093 hash_table_pointer_compare
);
2095 /* If "origin" is non-NULL, then the function body is there. So we
2096 * have to use the variable objects from the object with the body,
2097 * but the parameter instanciation on the current object.
2099 const exec_node
*parameter_info
= origin
? origin
->parameters
.head
: parameters
.head
;
2101 foreach_in_list(ir_rvalue
, n
, actual_parameters
) {
2102 ir_constant
*constant
= n
->constant_expression_value(variable_context
);
2103 if (constant
== NULL
) {
2104 hash_table_dtor(deref_hash
);
2109 ir_variable
*var
= (ir_variable
*)parameter_info
;
2110 hash_table_insert(deref_hash
, constant
, var
);
2112 parameter_info
= parameter_info
->next
;
2115 ir_constant
*result
= NULL
;
2117 /* Now run the builtin function until something non-constant
2118 * happens or we get the result.
2120 if (constant_expression_evaluate_expression_list(origin
? origin
->body
: body
, deref_hash
, &result
) && result
)
2121 result
= result
->clone(ralloc_parent(this), NULL
);
2123 hash_table_dtor(deref_hash
);