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 */
39 #include "util/half_float.h"
41 #include "glsl_types.h"
42 #include "program/hash_table.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 assert(sizeof(float) == sizeof(unsigned int));
78 memcpy(&f
, &u
, sizeof(f
));
85 assert(sizeof(float) == sizeof(unsigned int));
87 memcpy(&u
, &f
, sizeof(f
));
92 * Evaluate one component of a floating-point 4x8 unpacking function.
95 (*pack_1x8_func_t
)(float);
98 * Evaluate one component of a floating-point 2x16 unpacking function.
101 (*pack_1x16_func_t
)(float);
104 * Evaluate one component of a floating-point 4x8 unpacking function.
107 (*unpack_1x8_func_t
)(uint8_t);
110 * Evaluate one component of a floating-point 2x16 unpacking function.
113 (*unpack_1x16_func_t
)(uint16_t);
116 * Evaluate a 2x16 floating-point packing function.
119 pack_2x16(pack_1x16_func_t pack_1x16
,
122 /* From section 8.4 of the GLSL ES 3.00 spec:
126 * The first component of the vector will be written to the least
127 * significant bits of the output; the last component will be written to
128 * the most significant bits.
130 * The specifications for the other packing functions contain similar
134 u
|= ((uint32_t) pack_1x16(x
) << 0);
135 u
|= ((uint32_t) pack_1x16(y
) << 16);
140 * Evaluate a 4x8 floating-point packing function.
143 pack_4x8(pack_1x8_func_t pack_1x8
,
144 float x
, float y
, float z
, float w
)
146 /* From section 8.4 of the GLSL 4.30 spec:
150 * The first component of the vector will be written to the least
151 * significant bits of the output; the last component will be written to
152 * the most significant bits.
154 * The specifications for the other packing functions contain similar
158 u
|= ((uint32_t) pack_1x8(x
) << 0);
159 u
|= ((uint32_t) pack_1x8(y
) << 8);
160 u
|= ((uint32_t) pack_1x8(z
) << 16);
161 u
|= ((uint32_t) pack_1x8(w
) << 24);
166 * Evaluate a 2x16 floating-point unpacking function.
169 unpack_2x16(unpack_1x16_func_t unpack_1x16
,
173 /* From section 8.4 of the GLSL ES 3.00 spec:
177 * The first component of the returned vector will be extracted from
178 * the least significant bits of the input; the last component will be
179 * extracted from the most significant bits.
181 * The specifications for the other unpacking functions contain similar
184 *x
= unpack_1x16((uint16_t) (u
& 0xffff));
185 *y
= unpack_1x16((uint16_t) (u
>> 16));
189 * Evaluate a 4x8 floating-point unpacking function.
192 unpack_4x8(unpack_1x8_func_t unpack_1x8
, uint32_t u
,
193 float *x
, float *y
, float *z
, float *w
)
195 /* From section 8.4 of the GLSL 4.30 spec:
199 * The first component of the returned vector will be extracted from
200 * the least significant bits of the input; the last component will be
201 * extracted from the most significant bits.
203 * The specifications for the other unpacking functions contain similar
206 *x
= unpack_1x8((uint8_t) (u
& 0xff));
207 *y
= unpack_1x8((uint8_t) (u
>> 8));
208 *z
= unpack_1x8((uint8_t) (u
>> 16));
209 *w
= unpack_1x8((uint8_t) (u
>> 24));
213 * Evaluate one component of packSnorm4x8.
216 pack_snorm_1x8(float x
)
218 /* From section 8.4 of the GLSL 4.30 spec:
222 * The conversion for component c of v to fixed point is done as
225 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
228 _mesa_lroundevenf(CLAMP(x
, -1.0f
, +1.0f
) * 127.0f
);
232 * Evaluate one component of packSnorm2x16.
235 pack_snorm_1x16(float x
)
237 /* From section 8.4 of the GLSL ES 3.00 spec:
241 * The conversion for component c of v to fixed point is done as
244 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
247 _mesa_lroundevenf(CLAMP(x
, -1.0f
, +1.0f
) * 32767.0f
);
251 * Evaluate one component of unpackSnorm4x8.
254 unpack_snorm_1x8(uint8_t u
)
256 /* From section 8.4 of the GLSL 4.30 spec:
260 * The conversion for unpacked fixed-point value f to floating point is
263 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
265 return CLAMP((int8_t) u
/ 127.0f
, -1.0f
, +1.0f
);
269 * Evaluate one component of unpackSnorm2x16.
272 unpack_snorm_1x16(uint16_t u
)
274 /* From section 8.4 of the GLSL ES 3.00 spec:
278 * The conversion for unpacked fixed-point value f to floating point is
281 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
283 return CLAMP((int16_t) u
/ 32767.0f
, -1.0f
, +1.0f
);
287 * Evaluate one component packUnorm4x8.
290 pack_unorm_1x8(float x
)
292 /* From section 8.4 of the GLSL 4.30 spec:
296 * The conversion for component c of v to fixed point is done as
299 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
301 return (uint8_t) (int) _mesa_roundevenf(CLAMP(x
, 0.0f
, 1.0f
) * 255.0f
);
305 * Evaluate one component packUnorm2x16.
308 pack_unorm_1x16(float x
)
310 /* From section 8.4 of the GLSL ES 3.00 spec:
314 * The conversion for component c of v to fixed point is done as
317 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
319 return (uint16_t) (int)
320 _mesa_roundevenf(CLAMP(x
, 0.0f
, 1.0f
) * 65535.0f
);
324 * Evaluate one component of unpackUnorm4x8.
327 unpack_unorm_1x8(uint8_t u
)
329 /* From section 8.4 of the GLSL 4.30 spec:
333 * The conversion for unpacked fixed-point value f to floating point is
336 * unpackUnorm4x8: f / 255.0
338 return (float) u
/ 255.0f
;
342 * Evaluate one component of unpackUnorm2x16.
345 unpack_unorm_1x16(uint16_t u
)
347 /* From section 8.4 of the GLSL ES 3.00 spec:
351 * The conversion for unpacked fixed-point value f to floating point is
354 * unpackUnorm2x16: f / 65535.0
356 return (float) u
/ 65535.0f
;
360 * Evaluate one component of packHalf2x16.
363 pack_half_1x16(float x
)
365 return _mesa_float_to_half(x
);
369 * Evaluate one component of unpackHalf2x16.
372 unpack_half_1x16(uint16_t u
)
374 return _mesa_half_to_float(u
);
378 * Get the constant that is ultimately referenced by an r-value, in a constant
379 * expression evaluation context.
381 * The offset is used when the reference is to a specific column of a matrix.
384 constant_referenced(const ir_dereference
*deref
,
385 struct hash_table
*variable_context
,
386 ir_constant
*&store
, int &offset
)
391 if (variable_context
== NULL
)
394 switch (deref
->ir_type
) {
395 case ir_type_dereference_array
: {
396 const ir_dereference_array
*const da
=
397 (const ir_dereference_array
*) deref
;
399 ir_constant
*const index_c
=
400 da
->array_index
->constant_expression_value(variable_context
);
402 if (!index_c
|| !index_c
->type
->is_scalar() || !index_c
->type
->is_integer())
405 const int index
= index_c
->type
->base_type
== GLSL_TYPE_INT
?
406 index_c
->get_int_component(0) :
407 index_c
->get_uint_component(0);
409 ir_constant
*substore
;
412 const ir_dereference
*const deref
= da
->array
->as_dereference();
416 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
419 const glsl_type
*const vt
= da
->array
->type
;
420 if (vt
->is_array()) {
421 store
= substore
->get_array_element(index
);
423 } else if (vt
->is_matrix()) {
425 offset
= index
* vt
->vector_elements
;
426 } else if (vt
->is_vector()) {
428 offset
= suboffset
+ index
;
434 case ir_type_dereference_record
: {
435 const ir_dereference_record
*const dr
=
436 (const ir_dereference_record
*) deref
;
438 const ir_dereference
*const deref
= dr
->record
->as_dereference();
442 ir_constant
*substore
;
445 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
448 /* Since we're dropping it on the floor...
450 assert(suboffset
== 0);
452 store
= substore
->get_record_field(dr
->field
);
456 case ir_type_dereference_variable
: {
457 const ir_dereference_variable
*const dv
=
458 (const ir_dereference_variable
*) deref
;
460 store
= (ir_constant
*) hash_table_find(variable_context
, dv
->var
);
465 assert(!"Should not get here.");
469 return store
!= NULL
;
474 ir_rvalue::constant_expression_value(struct hash_table
*)
476 assert(this->type
->is_error());
481 ir_expression::constant_expression_value(struct hash_table
*variable_context
)
483 if (this->type
->is_error())
486 ir_constant
*op
[ARRAY_SIZE(this->operands
)] = { NULL
, };
487 ir_constant_data data
;
489 memset(&data
, 0, sizeof(data
));
491 for (unsigned operand
= 0; operand
< this->get_num_operands(); operand
++) {
492 op
[operand
] = this->operands
[operand
]->constant_expression_value(variable_context
);
498 switch (this->operation
) {
499 case ir_binop_lshift
:
500 case ir_binop_rshift
:
502 case ir_binop_interpolate_at_offset
:
503 case ir_binop_interpolate_at_sample
:
504 case ir_binop_vector_extract
:
506 case ir_triop_bitfield_extract
:
510 assert(op
[0]->type
->base_type
== op
[1]->type
->base_type
);
514 bool op0_scalar
= op
[0]->type
->is_scalar();
515 bool op1_scalar
= op
[1] != NULL
&& op
[1]->type
->is_scalar();
517 /* When iterating over a vector or matrix's components, we want to increase
518 * the loop counter. However, for scalars, we want to stay at 0.
520 unsigned c0_inc
= op0_scalar
? 0 : 1;
521 unsigned c1_inc
= op1_scalar
? 0 : 1;
523 if (op1_scalar
|| !op
[1]) {
524 components
= op
[0]->type
->components();
526 components
= op
[1]->type
->components();
529 void *ctx
= ralloc_parent(this);
531 /* Handle array operations here, rather than below. */
532 if (op
[0]->type
->is_array()) {
533 assert(op
[1] != NULL
&& op
[1]->type
->is_array());
534 switch (this->operation
) {
535 case ir_binop_all_equal
:
536 return new(ctx
) ir_constant(op
[0]->has_value(op
[1]));
537 case ir_binop_any_nequal
:
538 return new(ctx
) ir_constant(!op
[0]->has_value(op
[1]));
545 switch (this->operation
) {
546 case ir_unop_bit_not
:
547 switch (op
[0]->type
->base_type
) {
549 for (unsigned c
= 0; c
< components
; c
++)
550 data
.i
[c
] = ~ op
[0]->value
.i
[c
];
553 for (unsigned c
= 0; c
< components
; c
++)
554 data
.u
[c
] = ~ op
[0]->value
.u
[c
];
561 case ir_unop_logic_not
:
562 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
563 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
564 data
.b
[c
] = !op
[0]->value
.b
[c
];
568 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
569 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
570 data
.i
[c
] = (int) op
[0]->value
.f
[c
];
574 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
575 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
576 data
.i
[c
] = (unsigned) op
[0]->value
.f
[c
];
580 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
581 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
582 data
.f
[c
] = (float) op
[0]->value
.i
[c
];
586 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
587 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
588 data
.f
[c
] = (float) op
[0]->value
.u
[c
];
592 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
593 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
594 data
.f
[c
] = op
[0]->value
.b
[c
] ? 1.0F
: 0.0F
;
598 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
599 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
600 data
.b
[c
] = op
[0]->value
.f
[c
] != 0.0F
? true : false;
604 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
605 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
606 data
.u
[c
] = op
[0]->value
.b
[c
] ? 1 : 0;
610 assert(op
[0]->type
->is_integer());
611 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
612 data
.b
[c
] = op
[0]->value
.u
[c
] ? true : false;
616 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
617 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
618 data
.i
[c
] = op
[0]->value
.u
[c
];
622 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
623 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
624 data
.u
[c
] = op
[0]->value
.i
[c
];
627 case ir_unop_bitcast_i2f
:
628 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
629 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
630 data
.f
[c
] = bitcast_u2f(op
[0]->value
.i
[c
]);
633 case ir_unop_bitcast_f2i
:
634 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
635 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
636 data
.i
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
639 case ir_unop_bitcast_u2f
:
640 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
641 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
642 data
.f
[c
] = bitcast_u2f(op
[0]->value
.u
[c
]);
645 case ir_unop_bitcast_f2u
:
646 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
647 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
648 data
.u
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
652 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
653 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
654 data
.f
[c
] = op
[0]->value
.d
[c
];
658 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
659 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
660 data
.d
[c
] = op
[0]->value
.f
[c
];
664 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
665 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
666 data
.i
[c
] = op
[0]->value
.d
[c
];
670 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
671 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
672 data
.d
[c
] = op
[0]->value
.i
[c
];
676 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
677 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
678 data
.u
[c
] = op
[0]->value
.d
[c
];
682 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
683 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
684 data
.d
[c
] = op
[0]->value
.u
[c
];
688 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
689 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
690 data
.b
[c
] = op
[0]->value
.d
[c
] != 0.0;
694 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
695 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
696 data
.d
[c
] = trunc(op
[0]->value
.d
[c
]);
698 data
.f
[c
] = truncf(op
[0]->value
.f
[c
]);
702 case ir_unop_round_even
:
703 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
704 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
705 data
.d
[c
] = _mesa_roundeven(op
[0]->value
.d
[c
]);
707 data
.f
[c
] = _mesa_roundevenf(op
[0]->value
.f
[c
]);
712 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
713 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
714 data
.d
[c
] = ceil(op
[0]->value
.d
[c
]);
716 data
.f
[c
] = ceilf(op
[0]->value
.f
[c
]);
721 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
722 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
723 data
.d
[c
] = floor(op
[0]->value
.d
[c
]);
725 data
.f
[c
] = floorf(op
[0]->value
.f
[c
]);
730 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
731 switch (this->type
->base_type
) {
738 case GLSL_TYPE_FLOAT
:
739 data
.f
[c
] = op
[0]->value
.f
[c
] - floor(op
[0]->value
.f
[c
]);
741 case GLSL_TYPE_DOUBLE
:
742 data
.d
[c
] = op
[0]->value
.d
[c
] - floor(op
[0]->value
.d
[c
]);
751 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
752 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
753 data
.f
[c
] = sinf(op
[0]->value
.f
[c
]);
758 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
759 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
760 data
.f
[c
] = cosf(op
[0]->value
.f
[c
]);
765 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
766 switch (this->type
->base_type
) {
768 data
.u
[c
] = -((int) op
[0]->value
.u
[c
]);
771 data
.i
[c
] = -op
[0]->value
.i
[c
];
773 case GLSL_TYPE_FLOAT
:
774 data
.f
[c
] = -op
[0]->value
.f
[c
];
776 case GLSL_TYPE_DOUBLE
:
777 data
.d
[c
] = -op
[0]->value
.d
[c
];
786 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
787 switch (this->type
->base_type
) {
789 data
.u
[c
] = op
[0]->value
.u
[c
];
792 data
.i
[c
] = op
[0]->value
.i
[c
];
794 data
.i
[c
] = -data
.i
[c
];
796 case GLSL_TYPE_FLOAT
:
797 data
.f
[c
] = fabs(op
[0]->value
.f
[c
]);
799 case GLSL_TYPE_DOUBLE
:
800 data
.d
[c
] = fabs(op
[0]->value
.d
[c
]);
809 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
810 switch (this->type
->base_type
) {
812 data
.u
[c
] = op
[0]->value
.i
[c
] > 0;
815 data
.i
[c
] = (op
[0]->value
.i
[c
] > 0) - (op
[0]->value
.i
[c
] < 0);
817 case GLSL_TYPE_FLOAT
:
818 data
.f
[c
] = float((op
[0]->value
.f
[c
] > 0)-(op
[0]->value
.f
[c
] < 0));
820 case GLSL_TYPE_DOUBLE
:
821 data
.d
[c
] = double((op
[0]->value
.d
[c
] > 0)-(op
[0]->value
.d
[c
] < 0));
830 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
831 switch (this->type
->base_type
) {
833 if (op
[0]->value
.u
[c
] != 0.0)
834 data
.u
[c
] = 1 / op
[0]->value
.u
[c
];
837 if (op
[0]->value
.i
[c
] != 0.0)
838 data
.i
[c
] = 1 / op
[0]->value
.i
[c
];
840 case GLSL_TYPE_FLOAT
:
841 if (op
[0]->value
.f
[c
] != 0.0)
842 data
.f
[c
] = 1.0F
/ op
[0]->value
.f
[c
];
844 case GLSL_TYPE_DOUBLE
:
845 if (op
[0]->value
.d
[c
] != 0.0)
846 data
.d
[c
] = 1.0 / op
[0]->value
.d
[c
];
855 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
856 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
857 data
.d
[c
] = 1.0 / sqrt(op
[0]->value
.d
[c
]);
859 data
.f
[c
] = 1.0F
/ sqrtf(op
[0]->value
.f
[c
]);
864 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
865 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
866 data
.d
[c
] = sqrt(op
[0]->value
.d
[c
]);
868 data
.f
[c
] = sqrtf(op
[0]->value
.f
[c
]);
873 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
874 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
875 data
.f
[c
] = expf(op
[0]->value
.f
[c
]);
880 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
881 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
882 data
.f
[c
] = exp2f(op
[0]->value
.f
[c
]);
887 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
888 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
889 data
.f
[c
] = logf(op
[0]->value
.f
[c
]);
894 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
895 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
896 data
.f
[c
] = log2f(op
[0]->value
.f
[c
]);
901 case ir_unop_dFdx_coarse
:
902 case ir_unop_dFdx_fine
:
904 case ir_unop_dFdy_coarse
:
905 case ir_unop_dFdy_fine
:
906 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
907 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
912 case ir_unop_pack_snorm_2x16
:
913 assert(op
[0]->type
== glsl_type::vec2_type
);
914 data
.u
[0] = pack_2x16(pack_snorm_1x16
,
918 case ir_unop_pack_snorm_4x8
:
919 assert(op
[0]->type
== glsl_type::vec4_type
);
920 data
.u
[0] = pack_4x8(pack_snorm_1x8
,
926 case ir_unop_unpack_snorm_2x16
:
927 assert(op
[0]->type
== glsl_type::uint_type
);
928 unpack_2x16(unpack_snorm_1x16
,
930 &data
.f
[0], &data
.f
[1]);
932 case ir_unop_unpack_snorm_4x8
:
933 assert(op
[0]->type
== glsl_type::uint_type
);
934 unpack_4x8(unpack_snorm_1x8
,
936 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
938 case ir_unop_pack_unorm_2x16
:
939 assert(op
[0]->type
== glsl_type::vec2_type
);
940 data
.u
[0] = pack_2x16(pack_unorm_1x16
,
944 case ir_unop_pack_unorm_4x8
:
945 assert(op
[0]->type
== glsl_type::vec4_type
);
946 data
.u
[0] = pack_4x8(pack_unorm_1x8
,
952 case ir_unop_unpack_unorm_2x16
:
953 assert(op
[0]->type
== glsl_type::uint_type
);
954 unpack_2x16(unpack_unorm_1x16
,
956 &data
.f
[0], &data
.f
[1]);
958 case ir_unop_unpack_unorm_4x8
:
959 assert(op
[0]->type
== glsl_type::uint_type
);
960 unpack_4x8(unpack_unorm_1x8
,
962 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
964 case ir_unop_pack_half_2x16
:
965 assert(op
[0]->type
== glsl_type::vec2_type
);
966 data
.u
[0] = pack_2x16(pack_half_1x16
,
970 case ir_unop_unpack_half_2x16
:
971 assert(op
[0]->type
== glsl_type::uint_type
);
972 unpack_2x16(unpack_half_1x16
,
974 &data
.f
[0], &data
.f
[1]);
977 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
978 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
979 data
.f
[c
] = powf(op
[0]->value
.f
[c
], op
[1]->value
.f
[c
]);
984 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
985 data
.d
[0] = dot_d(op
[0], op
[1]);
987 data
.f
[0] = dot_f(op
[0], op
[1]);
991 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
992 for (unsigned c
= 0, c0
= 0, c1
= 0;
994 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
996 switch (op
[0]->type
->base_type
) {
998 data
.u
[c
] = MIN2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
1001 data
.i
[c
] = MIN2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
1003 case GLSL_TYPE_FLOAT
:
1004 data
.f
[c
] = MIN2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
1006 case GLSL_TYPE_DOUBLE
:
1007 data
.d
[c
] = MIN2(op
[0]->value
.d
[c0
], op
[1]->value
.d
[c1
]);
1016 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1017 for (unsigned c
= 0, c0
= 0, c1
= 0;
1019 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1021 switch (op
[0]->type
->base_type
) {
1022 case GLSL_TYPE_UINT
:
1023 data
.u
[c
] = MAX2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
1026 data
.i
[c
] = MAX2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
1028 case GLSL_TYPE_FLOAT
:
1029 data
.f
[c
] = MAX2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
1031 case GLSL_TYPE_DOUBLE
:
1032 data
.d
[c
] = MAX2(op
[0]->value
.d
[c0
], op
[1]->value
.d
[c1
]);
1041 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1042 for (unsigned c
= 0, c0
= 0, c1
= 0;
1044 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1046 switch (op
[0]->type
->base_type
) {
1047 case GLSL_TYPE_UINT
:
1048 data
.u
[c
] = op
[0]->value
.u
[c0
] + op
[1]->value
.u
[c1
];
1051 data
.i
[c
] = op
[0]->value
.i
[c0
] + op
[1]->value
.i
[c1
];
1053 case GLSL_TYPE_FLOAT
:
1054 data
.f
[c
] = op
[0]->value
.f
[c0
] + op
[1]->value
.f
[c1
];
1056 case GLSL_TYPE_DOUBLE
:
1057 data
.d
[c
] = op
[0]->value
.d
[c0
] + op
[1]->value
.d
[c1
];
1066 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1067 for (unsigned c
= 0, c0
= 0, c1
= 0;
1069 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1071 switch (op
[0]->type
->base_type
) {
1072 case GLSL_TYPE_UINT
:
1073 data
.u
[c
] = op
[0]->value
.u
[c0
] - op
[1]->value
.u
[c1
];
1076 data
.i
[c
] = op
[0]->value
.i
[c0
] - op
[1]->value
.i
[c1
];
1078 case GLSL_TYPE_FLOAT
:
1079 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
];
1081 case GLSL_TYPE_DOUBLE
:
1082 data
.d
[c
] = op
[0]->value
.d
[c0
] - op
[1]->value
.d
[c1
];
1091 /* Check for equal types, or unequal types involving scalars */
1092 if ((op
[0]->type
== op
[1]->type
&& !op
[0]->type
->is_matrix())
1093 || op0_scalar
|| op1_scalar
) {
1094 for (unsigned c
= 0, c0
= 0, c1
= 0;
1096 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1098 switch (op
[0]->type
->base_type
) {
1099 case GLSL_TYPE_UINT
:
1100 data
.u
[c
] = op
[0]->value
.u
[c0
] * op
[1]->value
.u
[c1
];
1103 data
.i
[c
] = op
[0]->value
.i
[c0
] * op
[1]->value
.i
[c1
];
1105 case GLSL_TYPE_FLOAT
:
1106 data
.f
[c
] = op
[0]->value
.f
[c0
] * op
[1]->value
.f
[c1
];
1108 case GLSL_TYPE_DOUBLE
:
1109 data
.d
[c
] = op
[0]->value
.d
[c0
] * op
[1]->value
.d
[c1
];
1116 assert(op
[0]->type
->is_matrix() || op
[1]->type
->is_matrix());
1118 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
1119 * matrix can be a GLSL vector, either N or P can be 1.
1121 * For vec*mat, the vector is treated as a row vector. This
1122 * means the vector is a 1-row x M-column matrix.
1124 * For mat*vec, the vector is treated as a column vector. Since
1125 * matrix_columns is 1 for vectors, this just works.
1127 const unsigned n
= op
[0]->type
->is_vector()
1128 ? 1 : op
[0]->type
->vector_elements
;
1129 const unsigned m
= op
[1]->type
->vector_elements
;
1130 const unsigned p
= op
[1]->type
->matrix_columns
;
1131 for (unsigned j
= 0; j
< p
; j
++) {
1132 for (unsigned i
= 0; i
< n
; i
++) {
1133 for (unsigned k
= 0; k
< m
; k
++) {
1134 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1135 data
.d
[i
+n
*j
] += op
[0]->value
.d
[i
+n
*k
]*op
[1]->value
.d
[k
+m
*j
];
1137 data
.f
[i
+n
*j
] += op
[0]->value
.f
[i
+n
*k
]*op
[1]->value
.f
[k
+m
*j
];
1145 /* FINISHME: Emit warning when division-by-zero is detected. */
1146 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1147 for (unsigned c
= 0, c0
= 0, c1
= 0;
1149 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1151 switch (op
[0]->type
->base_type
) {
1152 case GLSL_TYPE_UINT
:
1153 if (op
[1]->value
.u
[c1
] == 0) {
1156 data
.u
[c
] = op
[0]->value
.u
[c0
] / op
[1]->value
.u
[c1
];
1160 if (op
[1]->value
.i
[c1
] == 0) {
1163 data
.i
[c
] = op
[0]->value
.i
[c0
] / op
[1]->value
.i
[c1
];
1166 case GLSL_TYPE_FLOAT
:
1167 data
.f
[c
] = op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
];
1169 case GLSL_TYPE_DOUBLE
:
1170 data
.d
[c
] = op
[0]->value
.d
[c0
] / op
[1]->value
.d
[c1
];
1179 /* FINISHME: Emit warning when division-by-zero is detected. */
1180 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1181 for (unsigned c
= 0, c0
= 0, c1
= 0;
1183 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1185 switch (op
[0]->type
->base_type
) {
1186 case GLSL_TYPE_UINT
:
1187 if (op
[1]->value
.u
[c1
] == 0) {
1190 data
.u
[c
] = op
[0]->value
.u
[c0
] % op
[1]->value
.u
[c1
];
1194 if (op
[1]->value
.i
[c1
] == 0) {
1197 data
.i
[c
] = op
[0]->value
.i
[c0
] % op
[1]->value
.i
[c1
];
1200 case GLSL_TYPE_FLOAT
:
1201 /* We don't use fmod because it rounds toward zero; GLSL specifies
1204 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
]
1205 * floorf(op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
]);
1207 case GLSL_TYPE_DOUBLE
:
1208 /* We don't use fmod because it rounds toward zero; GLSL specifies
1211 data
.d
[c
] = op
[0]->value
.d
[c0
] - op
[1]->value
.d
[c1
]
1212 * floor(op
[0]->value
.d
[c0
] / op
[1]->value
.d
[c1
]);
1221 case ir_binop_logic_and
:
1222 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1223 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1224 data
.b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
1226 case ir_binop_logic_xor
:
1227 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1228 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1229 data
.b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
1231 case ir_binop_logic_or
:
1232 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1233 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1234 data
.b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
1238 assert(op
[0]->type
== op
[1]->type
);
1239 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1240 switch (op
[0]->type
->base_type
) {
1241 case GLSL_TYPE_UINT
:
1242 data
.b
[c
] = op
[0]->value
.u
[c
] < op
[1]->value
.u
[c
];
1245 data
.b
[c
] = op
[0]->value
.i
[c
] < op
[1]->value
.i
[c
];
1247 case GLSL_TYPE_FLOAT
:
1248 data
.b
[c
] = op
[0]->value
.f
[c
] < op
[1]->value
.f
[c
];
1250 case GLSL_TYPE_DOUBLE
:
1251 data
.b
[c
] = op
[0]->value
.d
[c
] < op
[1]->value
.d
[c
];
1258 case ir_binop_greater
:
1259 assert(op
[0]->type
== op
[1]->type
);
1260 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1261 switch (op
[0]->type
->base_type
) {
1262 case GLSL_TYPE_UINT
:
1263 data
.b
[c
] = op
[0]->value
.u
[c
] > op
[1]->value
.u
[c
];
1266 data
.b
[c
] = op
[0]->value
.i
[c
] > op
[1]->value
.i
[c
];
1268 case GLSL_TYPE_FLOAT
:
1269 data
.b
[c
] = op
[0]->value
.f
[c
] > op
[1]->value
.f
[c
];
1271 case GLSL_TYPE_DOUBLE
:
1272 data
.b
[c
] = op
[0]->value
.d
[c
] > op
[1]->value
.d
[c
];
1279 case ir_binop_lequal
:
1280 assert(op
[0]->type
== op
[1]->type
);
1281 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1282 switch (op
[0]->type
->base_type
) {
1283 case GLSL_TYPE_UINT
:
1284 data
.b
[c
] = op
[0]->value
.u
[c
] <= op
[1]->value
.u
[c
];
1287 data
.b
[c
] = op
[0]->value
.i
[c
] <= op
[1]->value
.i
[c
];
1289 case GLSL_TYPE_FLOAT
:
1290 data
.b
[c
] = op
[0]->value
.f
[c
] <= op
[1]->value
.f
[c
];
1292 case GLSL_TYPE_DOUBLE
:
1293 data
.b
[c
] = op
[0]->value
.d
[c
] <= op
[1]->value
.d
[c
];
1300 case ir_binop_gequal
:
1301 assert(op
[0]->type
== op
[1]->type
);
1302 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1303 switch (op
[0]->type
->base_type
) {
1304 case GLSL_TYPE_UINT
:
1305 data
.b
[c
] = op
[0]->value
.u
[c
] >= op
[1]->value
.u
[c
];
1308 data
.b
[c
] = op
[0]->value
.i
[c
] >= op
[1]->value
.i
[c
];
1310 case GLSL_TYPE_FLOAT
:
1311 data
.b
[c
] = op
[0]->value
.f
[c
] >= op
[1]->value
.f
[c
];
1313 case GLSL_TYPE_DOUBLE
:
1314 data
.b
[c
] = op
[0]->value
.d
[c
] >= op
[1]->value
.d
[c
];
1321 case ir_binop_equal
:
1322 assert(op
[0]->type
== op
[1]->type
);
1323 for (unsigned c
= 0; c
< components
; c
++) {
1324 switch (op
[0]->type
->base_type
) {
1325 case GLSL_TYPE_UINT
:
1326 data
.b
[c
] = op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
1329 data
.b
[c
] = op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
1331 case GLSL_TYPE_FLOAT
:
1332 data
.b
[c
] = op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
1334 case GLSL_TYPE_BOOL
:
1335 data
.b
[c
] = op
[0]->value
.b
[c
] == op
[1]->value
.b
[c
];
1337 case GLSL_TYPE_DOUBLE
:
1338 data
.b
[c
] = op
[0]->value
.d
[c
] == op
[1]->value
.d
[c
];
1345 case ir_binop_nequal
:
1346 assert(op
[0]->type
== op
[1]->type
);
1347 for (unsigned c
= 0; c
< components
; c
++) {
1348 switch (op
[0]->type
->base_type
) {
1349 case GLSL_TYPE_UINT
:
1350 data
.b
[c
] = op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
1353 data
.b
[c
] = op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
1355 case GLSL_TYPE_FLOAT
:
1356 data
.b
[c
] = op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
1358 case GLSL_TYPE_BOOL
:
1359 data
.b
[c
] = op
[0]->value
.b
[c
] != op
[1]->value
.b
[c
];
1361 case GLSL_TYPE_DOUBLE
:
1362 data
.b
[c
] = op
[0]->value
.d
[c
] != op
[1]->value
.d
[c
];
1369 case ir_binop_all_equal
:
1370 data
.b
[0] = op
[0]->has_value(op
[1]);
1372 case ir_binop_any_nequal
:
1373 data
.b
[0] = !op
[0]->has_value(op
[1]);
1376 case ir_binop_lshift
:
1377 for (unsigned c
= 0, c0
= 0, c1
= 0;
1379 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1381 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1382 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1383 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.i
[c1
];
1385 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1386 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1387 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.u
[c1
];
1389 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1390 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1391 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.i
[c1
];
1393 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1394 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1395 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.u
[c1
];
1400 case ir_binop_rshift
:
1401 for (unsigned c
= 0, c0
= 0, c1
= 0;
1403 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1405 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1406 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1407 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.i
[c1
];
1409 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1410 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1411 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.u
[c1
];
1413 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1414 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1415 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.i
[c1
];
1417 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1418 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1419 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.u
[c1
];
1424 case ir_binop_bit_and
:
1425 for (unsigned c
= 0, c0
= 0, c1
= 0;
1427 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1429 switch (op
[0]->type
->base_type
) {
1431 data
.i
[c
] = op
[0]->value
.i
[c0
] & op
[1]->value
.i
[c1
];
1433 case GLSL_TYPE_UINT
:
1434 data
.u
[c
] = op
[0]->value
.u
[c0
] & op
[1]->value
.u
[c1
];
1442 case ir_binop_bit_or
:
1443 for (unsigned c
= 0, c0
= 0, c1
= 0;
1445 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1447 switch (op
[0]->type
->base_type
) {
1449 data
.i
[c
] = op
[0]->value
.i
[c0
] | op
[1]->value
.i
[c1
];
1451 case GLSL_TYPE_UINT
:
1452 data
.u
[c
] = op
[0]->value
.u
[c0
] | op
[1]->value
.u
[c1
];
1460 case ir_binop_vector_extract
: {
1461 const int c
= CLAMP(op
[1]->value
.i
[0], 0,
1462 (int) op
[0]->type
->vector_elements
- 1);
1464 switch (op
[0]->type
->base_type
) {
1465 case GLSL_TYPE_UINT
:
1466 data
.u
[0] = op
[0]->value
.u
[c
];
1469 data
.i
[0] = op
[0]->value
.i
[c
];
1471 case GLSL_TYPE_FLOAT
:
1472 data
.f
[0] = op
[0]->value
.f
[c
];
1474 case GLSL_TYPE_DOUBLE
:
1475 data
.d
[0] = op
[0]->value
.d
[c
];
1477 case GLSL_TYPE_BOOL
:
1478 data
.b
[0] = op
[0]->value
.b
[c
];
1486 case ir_binop_bit_xor
:
1487 for (unsigned c
= 0, c0
= 0, c1
= 0;
1489 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1491 switch (op
[0]->type
->base_type
) {
1493 data
.i
[c
] = op
[0]->value
.i
[c0
] ^ op
[1]->value
.i
[c1
];
1495 case GLSL_TYPE_UINT
:
1496 data
.u
[c
] = op
[0]->value
.u
[c0
] ^ op
[1]->value
.u
[c1
];
1504 case ir_unop_bitfield_reverse
:
1505 /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
1506 for (unsigned c
= 0; c
< components
; c
++) {
1507 unsigned int v
= op
[0]->value
.u
[c
]; // input bits to be reversed
1508 unsigned int r
= v
; // r will be reversed bits of v; first get LSB of v
1509 int s
= sizeof(v
) * CHAR_BIT
- 1; // extra shift needed at end
1511 for (v
>>= 1; v
; v
>>= 1) {
1516 r
<<= s
; // shift when v's highest bits are zero
1522 case ir_unop_bit_count
:
1523 for (unsigned c
= 0; c
< components
; c
++) {
1525 unsigned v
= op
[0]->value
.u
[c
];
1527 for (; v
; count
++) {
1534 case ir_unop_find_msb
:
1535 for (unsigned c
= 0; c
< components
; c
++) {
1536 int v
= op
[0]->value
.i
[c
];
1538 if (v
== 0 || (op
[0]->type
->base_type
== GLSL_TYPE_INT
&& v
== -1))
1542 int top_bit
= op
[0]->type
->base_type
== GLSL_TYPE_UINT
1543 ? 0 : v
& (1 << 31);
1545 while (((v
& (1 << 31)) == top_bit
) && count
!= 32) {
1550 data
.i
[c
] = 31 - count
;
1555 case ir_unop_find_lsb
:
1556 for (unsigned c
= 0; c
< components
; c
++) {
1557 if (op
[0]->value
.i
[c
] == 0)
1561 unsigned v
= op
[0]->value
.u
[c
];
1563 for (; !(v
& 1); v
>>= 1) {
1571 case ir_unop_saturate
:
1572 for (unsigned c
= 0; c
< components
; c
++) {
1573 data
.f
[c
] = CLAMP(op
[0]->value
.f
[c
], 0.0f
, 1.0f
);
1576 case ir_unop_pack_double_2x32
: {
1577 /* XXX needs to be checked on big-endian */
1579 temp
= (uint64_t)op
[0]->value
.u
[0] | ((uint64_t)op
[0]->value
.u
[1] << 32);
1580 data
.d
[0] = *(double *)&temp
;
1584 case ir_unop_unpack_double_2x32
:
1585 /* XXX needs to be checked on big-endian */
1586 data
.u
[0] = *(uint32_t *)&op
[0]->value
.d
[0];
1587 data
.u
[1] = *((uint32_t *)&op
[0]->value
.d
[0] + 1);
1590 case ir_triop_bitfield_extract
: {
1591 int offset
= op
[1]->value
.i
[0];
1592 int bits
= op
[2]->value
.i
[0];
1594 for (unsigned c
= 0; c
< components
; c
++) {
1597 else if (offset
< 0 || bits
< 0)
1598 data
.u
[c
] = 0; /* Undefined, per spec. */
1599 else if (offset
+ bits
> 32)
1600 data
.u
[c
] = 0; /* Undefined, per spec. */
1602 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
) {
1603 /* int so that the right shift will sign-extend. */
1604 int value
= op
[0]->value
.i
[c
];
1605 value
<<= 32 - bits
- offset
;
1606 value
>>= 32 - bits
;
1609 unsigned value
= op
[0]->value
.u
[c
];
1610 value
<<= 32 - bits
- offset
;
1611 value
>>= 32 - bits
;
1619 case ir_binop_bfm
: {
1620 int bits
= op
[0]->value
.i
[0];
1621 int offset
= op
[1]->value
.i
[0];
1623 for (unsigned c
= 0; c
< components
; c
++) {
1625 data
.u
[c
] = op
[0]->value
.u
[c
];
1626 else if (offset
< 0 || bits
< 0)
1627 data
.u
[c
] = 0; /* Undefined for bitfieldInsert, per spec. */
1628 else if (offset
+ bits
> 32)
1629 data
.u
[c
] = 0; /* Undefined for bitfieldInsert, per spec. */
1631 data
.u
[c
] = ((1 << bits
) - 1) << offset
;
1636 case ir_binop_ldexp
:
1637 for (unsigned c
= 0; c
< components
; c
++) {
1638 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
) {
1639 data
.d
[c
] = ldexp(op
[0]->value
.d
[c
], op
[1]->value
.i
[c
]);
1640 /* Flush subnormal values to zero. */
1641 if (!isnormal(data
.d
[c
]))
1642 data
.d
[c
] = copysign(0.0, op
[0]->value
.d
[c
]);
1644 data
.f
[c
] = ldexpf(op
[0]->value
.f
[c
], op
[1]->value
.i
[c
]);
1645 /* Flush subnormal values to zero. */
1646 if (!isnormal(data
.f
[c
]))
1647 data
.f
[c
] = copysignf(0.0f
, op
[0]->value
.f
[c
]);
1653 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
1654 op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1655 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
||
1656 op
[1]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1657 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
||
1658 op
[2]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1660 for (unsigned c
= 0; c
< components
; c
++) {
1661 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1662 data
.d
[c
] = op
[0]->value
.d
[c
] * op
[1]->value
.d
[c
]
1663 + op
[2]->value
.d
[c
];
1665 data
.f
[c
] = op
[0]->value
.f
[c
] * op
[1]->value
.f
[c
]
1666 + op
[2]->value
.f
[c
];
1670 case ir_triop_lrp
: {
1671 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
1672 op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1673 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
||
1674 op
[1]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1675 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
||
1676 op
[2]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1678 unsigned c2_inc
= op
[2]->type
->is_scalar() ? 0 : 1;
1679 for (unsigned c
= 0, c2
= 0; c
< components
; c2
+= c2_inc
, c
++) {
1680 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1681 data
.d
[c
] = op
[0]->value
.d
[c
] * (1.0 - op
[2]->value
.d
[c2
]) +
1682 (op
[1]->value
.d
[c
] * op
[2]->value
.d
[c2
]);
1684 data
.f
[c
] = op
[0]->value
.f
[c
] * (1.0f
- op
[2]->value
.f
[c2
]) +
1685 (op
[1]->value
.f
[c
] * op
[2]->value
.f
[c2
]);
1691 for (unsigned c
= 0; c
< components
; c
++) {
1692 if (op
[1]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1693 data
.d
[c
] = op
[0]->value
.b
[c
] ? op
[1]->value
.d
[c
]
1694 : op
[2]->value
.d
[c
];
1696 data
.u
[c
] = op
[0]->value
.b
[c
] ? op
[1]->value
.u
[c
]
1697 : op
[2]->value
.u
[c
];
1701 case ir_triop_vector_insert
: {
1702 const unsigned idx
= op
[2]->value
.u
[0];
1704 memcpy(&data
, &op
[0]->value
, sizeof(data
));
1706 switch (this->type
->base_type
) {
1708 data
.i
[idx
] = op
[1]->value
.i
[0];
1710 case GLSL_TYPE_UINT
:
1711 data
.u
[idx
] = op
[1]->value
.u
[0];
1713 case GLSL_TYPE_FLOAT
:
1714 data
.f
[idx
] = op
[1]->value
.f
[0];
1716 case GLSL_TYPE_BOOL
:
1717 data
.b
[idx
] = op
[1]->value
.b
[0];
1719 case GLSL_TYPE_DOUBLE
:
1720 data
.d
[idx
] = op
[1]->value
.d
[0];
1723 assert(!"Should not get here.");
1729 case ir_quadop_bitfield_insert
: {
1730 int offset
= op
[2]->value
.i
[0];
1731 int bits
= op
[3]->value
.i
[0];
1733 for (unsigned c
= 0; c
< components
; c
++) {
1735 data
.u
[c
] = op
[0]->value
.u
[c
];
1736 else if (offset
< 0 || bits
< 0)
1737 data
.u
[c
] = 0; /* Undefined, per spec. */
1738 else if (offset
+ bits
> 32)
1739 data
.u
[c
] = 0; /* Undefined, per spec. */
1741 unsigned insert_mask
= ((1 << bits
) - 1) << offset
;
1743 unsigned insert
= op
[1]->value
.u
[c
];
1745 insert
&= insert_mask
;
1747 unsigned base
= op
[0]->value
.u
[c
];
1748 base
&= ~insert_mask
;
1750 data
.u
[c
] = base
| insert
;
1756 case ir_quadop_vector
:
1757 for (unsigned c
= 0; c
< this->type
->vector_elements
; c
++) {
1758 switch (this->type
->base_type
) {
1760 data
.i
[c
] = op
[c
]->value
.i
[0];
1762 case GLSL_TYPE_UINT
:
1763 data
.u
[c
] = op
[c
]->value
.u
[0];
1765 case GLSL_TYPE_FLOAT
:
1766 data
.f
[c
] = op
[c
]->value
.f
[0];
1768 case GLSL_TYPE_DOUBLE
:
1769 data
.d
[c
] = op
[c
]->value
.d
[0];
1778 /* FINISHME: Should handle all expression types. */
1782 return new(ctx
) ir_constant(this->type
, &data
);
1787 ir_texture::constant_expression_value(struct hash_table
*)
1789 /* texture lookups aren't constant expressions */
1795 ir_swizzle::constant_expression_value(struct hash_table
*variable_context
)
1797 ir_constant
*v
= this->val
->constant_expression_value(variable_context
);
1800 ir_constant_data data
= { { 0 } };
1802 const unsigned swiz_idx
[4] = {
1803 this->mask
.x
, this->mask
.y
, this->mask
.z
, this->mask
.w
1806 for (unsigned i
= 0; i
< this->mask
.num_components
; i
++) {
1807 switch (v
->type
->base_type
) {
1808 case GLSL_TYPE_UINT
:
1809 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
1810 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
1811 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
1812 case GLSL_TYPE_DOUBLE
:data
.d
[i
] = v
->value
.d
[swiz_idx
[i
]]; break;
1813 default: assert(!"Should not get here."); break;
1817 void *ctx
= ralloc_parent(this);
1818 return new(ctx
) ir_constant(this->type
, &data
);
1825 ir_dereference_variable::constant_expression_value(struct hash_table
*variable_context
)
1827 /* This may occur during compile and var->type is glsl_type::error_type */
1831 /* Give priority to the context hashtable, if it exists */
1832 if (variable_context
) {
1833 ir_constant
*value
= (ir_constant
*)hash_table_find(variable_context
, var
);
1838 /* The constant_value of a uniform variable is its initializer,
1839 * not the lifetime constant value of the uniform.
1841 if (var
->data
.mode
== ir_var_uniform
)
1844 if (!var
->constant_value
)
1847 return var
->constant_value
->clone(ralloc_parent(var
), NULL
);
1852 ir_dereference_array::constant_expression_value(struct hash_table
*variable_context
)
1854 ir_constant
*array
= this->array
->constant_expression_value(variable_context
);
1855 ir_constant
*idx
= this->array_index
->constant_expression_value(variable_context
);
1857 if ((array
!= NULL
) && (idx
!= NULL
)) {
1858 void *ctx
= ralloc_parent(this);
1859 if (array
->type
->is_matrix()) {
1860 /* Array access of a matrix results in a vector.
1862 const unsigned column
= idx
->value
.u
[0];
1864 const glsl_type
*const column_type
= array
->type
->column_type();
1866 /* Offset in the constant matrix to the first element of the column
1869 const unsigned mat_idx
= column
* column_type
->vector_elements
;
1871 ir_constant_data data
= { { 0 } };
1873 switch (column_type
->base_type
) {
1874 case GLSL_TYPE_UINT
:
1876 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1877 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
1881 case GLSL_TYPE_FLOAT
:
1882 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1883 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
1887 case GLSL_TYPE_DOUBLE
:
1888 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1889 data
.d
[i
] = array
->value
.d
[mat_idx
+ i
];
1894 assert(!"Should not get here.");
1898 return new(ctx
) ir_constant(column_type
, &data
);
1899 } else if (array
->type
->is_vector()) {
1900 const unsigned component
= idx
->value
.u
[0];
1902 return new(ctx
) ir_constant(array
, component
);
1904 const unsigned index
= idx
->value
.u
[0];
1905 return array
->get_array_element(index
)->clone(ctx
, NULL
);
1913 ir_dereference_record::constant_expression_value(struct hash_table
*)
1915 ir_constant
*v
= this->record
->constant_expression_value();
1917 return (v
!= NULL
) ? v
->get_record_field(this->field
) : NULL
;
1922 ir_assignment::constant_expression_value(struct hash_table
*)
1924 /* FINISHME: Handle CEs involving assignment (return RHS) */
1930 ir_constant::constant_expression_value(struct hash_table
*)
1937 ir_call::constant_expression_value(struct hash_table
*variable_context
)
1939 return this->callee
->constant_expression_value(&this->actual_parameters
, variable_context
);
1943 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list
&body
,
1944 struct hash_table
*variable_context
,
1945 ir_constant
**result
)
1947 foreach_in_list(ir_instruction
, inst
, &body
) {
1948 switch(inst
->ir_type
) {
1950 /* (declare () type symbol) */
1951 case ir_type_variable
: {
1952 ir_variable
*var
= inst
->as_variable();
1953 hash_table_insert(variable_context
, ir_constant::zero(this, var
->type
), var
);
1957 /* (assign [condition] (write-mask) (ref) (value)) */
1958 case ir_type_assignment
: {
1959 ir_assignment
*asg
= inst
->as_assignment();
1960 if (asg
->condition
) {
1961 ir_constant
*cond
= asg
->condition
->constant_expression_value(variable_context
);
1964 if (!cond
->get_bool_component(0))
1968 ir_constant
*store
= NULL
;
1971 if (!constant_referenced(asg
->lhs
, variable_context
, store
, offset
))
1974 ir_constant
*value
= asg
->rhs
->constant_expression_value(variable_context
);
1979 store
->copy_masked_offset(value
, offset
, asg
->write_mask
);
1983 /* (return (expression)) */
1984 case ir_type_return
:
1986 *result
= inst
->as_return()->value
->constant_expression_value(variable_context
);
1987 return *result
!= NULL
;
1989 /* (call name (ref) (params))*/
1990 case ir_type_call
: {
1991 ir_call
*call
= inst
->as_call();
1993 /* Just say no to void functions in constant expressions. We
1994 * don't need them at that point.
1997 if (!call
->return_deref
)
2000 ir_constant
*store
= NULL
;
2003 if (!constant_referenced(call
->return_deref
, variable_context
,
2007 ir_constant
*value
= call
->constant_expression_value(variable_context
);
2012 store
->copy_offset(value
, offset
);
2016 /* (if condition (then-instructions) (else-instructions)) */
2018 ir_if
*iif
= inst
->as_if();
2020 ir_constant
*cond
= iif
->condition
->constant_expression_value(variable_context
);
2021 if (!cond
|| !cond
->type
->is_boolean())
2024 exec_list
&branch
= cond
->get_bool_component(0) ? iif
->then_instructions
: iif
->else_instructions
;
2027 if (!constant_expression_evaluate_expression_list(branch
, variable_context
, result
))
2030 /* If there was a return in the branch chosen, drop out now. */
2037 /* Every other expression type, we drop out. */
2043 /* Reaching the end of the block is not an error condition */
2051 ir_function_signature::constant_expression_value(exec_list
*actual_parameters
, struct hash_table
*variable_context
)
2053 const glsl_type
*type
= this->return_type
;
2054 if (type
== glsl_type::void_type
)
2057 /* From the GLSL 1.20 spec, page 23:
2058 * "Function calls to user-defined functions (non-built-in functions)
2059 * cannot be used to form constant expressions."
2061 if (!this->is_builtin())
2065 * Of the builtin functions, only the texture lookups and the noise
2066 * ones must not be used in constant expressions. They all include
2067 * specific opcodes so they don't need to be special-cased at this
2071 /* Initialize the table of dereferencable names with the function
2072 * parameters. Verify their const-ness on the way.
2074 * We expect the correctness of the number of parameters to have
2075 * been checked earlier.
2077 hash_table
*deref_hash
= hash_table_ctor(8, hash_table_pointer_hash
,
2078 hash_table_pointer_compare
);
2080 /* If "origin" is non-NULL, then the function body is there. So we
2081 * have to use the variable objects from the object with the body,
2082 * but the parameter instanciation on the current object.
2084 const exec_node
*parameter_info
= origin
? origin
->parameters
.head
: parameters
.head
;
2086 foreach_in_list(ir_rvalue
, n
, actual_parameters
) {
2087 ir_constant
*constant
= n
->constant_expression_value(variable_context
);
2088 if (constant
== NULL
) {
2089 hash_table_dtor(deref_hash
);
2094 ir_variable
*var
= (ir_variable
*)parameter_info
;
2095 hash_table_insert(deref_hash
, constant
, var
);
2097 parameter_info
= parameter_info
->next
;
2100 ir_constant
*result
= NULL
;
2102 /* Now run the builtin function until something non-constant
2103 * happens or we get the result.
2105 if (constant_expression_evaluate_expression_list(origin
? origin
->body
: body
, deref_hash
, &result
) && result
)
2106 result
= result
->clone(ralloc_parent(this), NULL
);
2108 hash_table_dtor(deref_hash
);