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(_MSC_VER) && (_MSC_VER < 1800)
44 static int isnormal(double x
)
46 return _fpclass(x
) == _FPCLASS_NN
|| _fpclass(x
) == _FPCLASS_PN
;
48 #elif defined(__SUNPRO_CC) && !defined(isnormal)
50 static int isnormal(double x
)
52 return fpclass(x
) == FP_NORMAL
;
57 static double copysign(double x
, double y
)
59 return _copysign(x
, y
);
64 dot_f(ir_constant
*op0
, ir_constant
*op1
)
66 assert(op0
->type
->is_float() && op1
->type
->is_float());
69 for (unsigned c
= 0; c
< op0
->type
->components(); c
++)
70 result
+= op0
->value
.f
[c
] * op1
->value
.f
[c
];
76 dot_d(ir_constant
*op0
, ir_constant
*op1
)
78 assert(op0
->type
->is_double() && op1
->type
->is_double());
81 for (unsigned c
= 0; c
< op0
->type
->components(); c
++)
82 result
+= op0
->value
.d
[c
] * op1
->value
.d
[c
];
87 /* This method is the only one supported by gcc. Unions in particular
88 * are iffy, and read-through-converted-pointer is killed by strict
89 * aliasing. OTOH, the compiler sees through the memcpy, so the
90 * resulting asm is reasonable.
93 bitcast_u2f(unsigned int u
)
95 assert(sizeof(float) == sizeof(unsigned int));
97 memcpy(&f
, &u
, sizeof(f
));
104 assert(sizeof(float) == sizeof(unsigned int));
106 memcpy(&u
, &f
, sizeof(f
));
111 * Evaluate one component of a floating-point 4x8 unpacking function.
114 (*pack_1x8_func_t
)(float);
117 * Evaluate one component of a floating-point 2x16 unpacking function.
120 (*pack_1x16_func_t
)(float);
123 * Evaluate one component of a floating-point 4x8 unpacking function.
126 (*unpack_1x8_func_t
)(uint8_t);
129 * Evaluate one component of a floating-point 2x16 unpacking function.
132 (*unpack_1x16_func_t
)(uint16_t);
135 * Evaluate a 2x16 floating-point packing function.
138 pack_2x16(pack_1x16_func_t pack_1x16
,
141 /* From section 8.4 of the GLSL ES 3.00 spec:
145 * The first component of the vector will be written to the least
146 * significant bits of the output; the last component will be written to
147 * the most significant bits.
149 * The specifications for the other packing functions contain similar
153 u
|= ((uint32_t) pack_1x16(x
) << 0);
154 u
|= ((uint32_t) pack_1x16(y
) << 16);
159 * Evaluate a 4x8 floating-point packing function.
162 pack_4x8(pack_1x8_func_t pack_1x8
,
163 float x
, float y
, float z
, float w
)
165 /* From section 8.4 of the GLSL 4.30 spec:
169 * The first component of the vector will be written to the least
170 * significant bits of the output; the last component will be written to
171 * the most significant bits.
173 * The specifications for the other packing functions contain similar
177 u
|= ((uint32_t) pack_1x8(x
) << 0);
178 u
|= ((uint32_t) pack_1x8(y
) << 8);
179 u
|= ((uint32_t) pack_1x8(z
) << 16);
180 u
|= ((uint32_t) pack_1x8(w
) << 24);
185 * Evaluate a 2x16 floating-point unpacking function.
188 unpack_2x16(unpack_1x16_func_t unpack_1x16
,
192 /* From section 8.4 of the GLSL ES 3.00 spec:
196 * The first component of the returned vector will be extracted from
197 * the least significant bits of the input; the last component will be
198 * extracted from the most significant bits.
200 * The specifications for the other unpacking functions contain similar
203 *x
= unpack_1x16((uint16_t) (u
& 0xffff));
204 *y
= unpack_1x16((uint16_t) (u
>> 16));
208 * Evaluate a 4x8 floating-point unpacking function.
211 unpack_4x8(unpack_1x8_func_t unpack_1x8
, uint32_t u
,
212 float *x
, float *y
, float *z
, float *w
)
214 /* From section 8.4 of the GLSL 4.30 spec:
218 * The first component of the returned vector will be extracted from
219 * the least significant bits of the input; the last component will be
220 * extracted from the most significant bits.
222 * The specifications for the other unpacking functions contain similar
225 *x
= unpack_1x8((uint8_t) (u
& 0xff));
226 *y
= unpack_1x8((uint8_t) (u
>> 8));
227 *z
= unpack_1x8((uint8_t) (u
>> 16));
228 *w
= unpack_1x8((uint8_t) (u
>> 24));
232 * Evaluate one component of packSnorm4x8.
235 pack_snorm_1x8(float x
)
237 /* From section 8.4 of the GLSL 4.30 spec:
241 * The conversion for component c of v to fixed point is done as
244 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
246 * We must first cast the float to an int, because casting a negative
247 * float to a uint is undefined.
249 return (uint8_t) (int)
250 _mesa_roundevenf(CLAMP(x
, -1.0f
, +1.0f
) * 127.0f
);
254 * Evaluate one component of packSnorm2x16.
257 pack_snorm_1x16(float x
)
259 /* From section 8.4 of the GLSL ES 3.00 spec:
263 * The conversion for component c of v to fixed point is done as
266 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
268 * We must first cast the float to an int, because casting a negative
269 * float to a uint is undefined.
271 return (uint16_t) (int)
272 _mesa_roundevenf(CLAMP(x
, -1.0f
, +1.0f
) * 32767.0f
);
276 * Evaluate one component of unpackSnorm4x8.
279 unpack_snorm_1x8(uint8_t u
)
281 /* From section 8.4 of the GLSL 4.30 spec:
285 * The conversion for unpacked fixed-point value f to floating point is
288 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
290 return CLAMP((int8_t) u
/ 127.0f
, -1.0f
, +1.0f
);
294 * Evaluate one component of unpackSnorm2x16.
297 unpack_snorm_1x16(uint16_t u
)
299 /* From section 8.4 of the GLSL ES 3.00 spec:
303 * The conversion for unpacked fixed-point value f to floating point is
306 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
308 return CLAMP((int16_t) u
/ 32767.0f
, -1.0f
, +1.0f
);
312 * Evaluate one component packUnorm4x8.
315 pack_unorm_1x8(float x
)
317 /* From section 8.4 of the GLSL 4.30 spec:
321 * The conversion for component c of v to fixed point is done as
324 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
326 return (uint8_t) (int) _mesa_roundevenf(CLAMP(x
, 0.0f
, 1.0f
) * 255.0f
);
330 * Evaluate one component packUnorm2x16.
333 pack_unorm_1x16(float x
)
335 /* From section 8.4 of the GLSL ES 3.00 spec:
339 * The conversion for component c of v to fixed point is done as
342 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
344 return (uint16_t) (int)
345 _mesa_roundevenf(CLAMP(x
, 0.0f
, 1.0f
) * 65535.0f
);
349 * Evaluate one component of unpackUnorm4x8.
352 unpack_unorm_1x8(uint8_t u
)
354 /* From section 8.4 of the GLSL 4.30 spec:
358 * The conversion for unpacked fixed-point value f to floating point is
361 * unpackUnorm4x8: f / 255.0
363 return (float) u
/ 255.0f
;
367 * Evaluate one component of unpackUnorm2x16.
370 unpack_unorm_1x16(uint16_t u
)
372 /* From section 8.4 of the GLSL ES 3.00 spec:
376 * The conversion for unpacked fixed-point value f to floating point is
379 * unpackUnorm2x16: f / 65535.0
381 return (float) u
/ 65535.0f
;
385 * Evaluate one component of packHalf2x16.
388 pack_half_1x16(float x
)
390 return _mesa_float_to_half(x
);
394 * Evaluate one component of unpackHalf2x16.
397 unpack_half_1x16(uint16_t u
)
399 return _mesa_half_to_float(u
);
403 * Get the constant that is ultimately referenced by an r-value, in a constant
404 * expression evaluation context.
406 * The offset is used when the reference is to a specific column of a matrix.
409 constant_referenced(const ir_dereference
*deref
,
410 struct hash_table
*variable_context
,
411 ir_constant
*&store
, int &offset
)
416 if (variable_context
== NULL
)
419 switch (deref
->ir_type
) {
420 case ir_type_dereference_array
: {
421 const ir_dereference_array
*const da
=
422 (const ir_dereference_array
*) deref
;
424 ir_constant
*const index_c
=
425 da
->array_index
->constant_expression_value(variable_context
);
427 if (!index_c
|| !index_c
->type
->is_scalar() || !index_c
->type
->is_integer())
430 const int index
= index_c
->type
->base_type
== GLSL_TYPE_INT
?
431 index_c
->get_int_component(0) :
432 index_c
->get_uint_component(0);
434 ir_constant
*substore
;
437 const ir_dereference
*const deref
= da
->array
->as_dereference();
441 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
444 const glsl_type
*const vt
= da
->array
->type
;
445 if (vt
->is_array()) {
446 store
= substore
->get_array_element(index
);
448 } else if (vt
->is_matrix()) {
450 offset
= index
* vt
->vector_elements
;
451 } else if (vt
->is_vector()) {
453 offset
= suboffset
+ index
;
459 case ir_type_dereference_record
: {
460 const ir_dereference_record
*const dr
=
461 (const ir_dereference_record
*) deref
;
463 const ir_dereference
*const deref
= dr
->record
->as_dereference();
467 ir_constant
*substore
;
470 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
473 /* Since we're dropping it on the floor...
475 assert(suboffset
== 0);
477 store
= substore
->get_record_field(dr
->field
);
481 case ir_type_dereference_variable
: {
482 const ir_dereference_variable
*const dv
=
483 (const ir_dereference_variable
*) deref
;
485 store
= (ir_constant
*) hash_table_find(variable_context
, dv
->var
);
490 assert(!"Should not get here.");
494 return store
!= NULL
;
499 ir_rvalue::constant_expression_value(struct hash_table
*)
501 assert(this->type
->is_error());
506 ir_expression::constant_expression_value(struct hash_table
*variable_context
)
508 if (this->type
->is_error())
511 ir_constant
*op
[ARRAY_SIZE(this->operands
)] = { NULL
, };
512 ir_constant_data data
;
514 memset(&data
, 0, sizeof(data
));
516 for (unsigned operand
= 0; operand
< this->get_num_operands(); operand
++) {
517 op
[operand
] = this->operands
[operand
]->constant_expression_value(variable_context
);
523 switch (this->operation
) {
524 case ir_binop_lshift
:
525 case ir_binop_rshift
:
527 case ir_binop_interpolate_at_offset
:
528 case ir_binop_interpolate_at_sample
:
529 case ir_binop_vector_extract
:
531 case ir_triop_bitfield_extract
:
535 assert(op
[0]->type
->base_type
== op
[1]->type
->base_type
);
539 bool op0_scalar
= op
[0]->type
->is_scalar();
540 bool op1_scalar
= op
[1] != NULL
&& op
[1]->type
->is_scalar();
542 /* When iterating over a vector or matrix's components, we want to increase
543 * the loop counter. However, for scalars, we want to stay at 0.
545 unsigned c0_inc
= op0_scalar
? 0 : 1;
546 unsigned c1_inc
= op1_scalar
? 0 : 1;
548 if (op1_scalar
|| !op
[1]) {
549 components
= op
[0]->type
->components();
551 components
= op
[1]->type
->components();
554 void *ctx
= ralloc_parent(this);
556 /* Handle array operations here, rather than below. */
557 if (op
[0]->type
->is_array()) {
558 assert(op
[1] != NULL
&& op
[1]->type
->is_array());
559 switch (this->operation
) {
560 case ir_binop_all_equal
:
561 return new(ctx
) ir_constant(op
[0]->has_value(op
[1]));
562 case ir_binop_any_nequal
:
563 return new(ctx
) ir_constant(!op
[0]->has_value(op
[1]));
570 switch (this->operation
) {
571 case ir_unop_bit_not
:
572 switch (op
[0]->type
->base_type
) {
574 for (unsigned c
= 0; c
< components
; c
++)
575 data
.i
[c
] = ~ op
[0]->value
.i
[c
];
578 for (unsigned c
= 0; c
< components
; c
++)
579 data
.u
[c
] = ~ op
[0]->value
.u
[c
];
586 case ir_unop_logic_not
:
587 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
588 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
589 data
.b
[c
] = !op
[0]->value
.b
[c
];
593 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
594 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
595 data
.i
[c
] = (int) op
[0]->value
.f
[c
];
599 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
600 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
601 data
.i
[c
] = (unsigned) op
[0]->value
.f
[c
];
605 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
606 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
607 data
.f
[c
] = (float) op
[0]->value
.i
[c
];
611 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
612 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
613 data
.f
[c
] = (float) op
[0]->value
.u
[c
];
617 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
618 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
619 data
.f
[c
] = op
[0]->value
.b
[c
] ? 1.0F
: 0.0F
;
623 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
624 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
625 data
.b
[c
] = op
[0]->value
.f
[c
] != 0.0F
? true : false;
629 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
630 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
631 data
.u
[c
] = op
[0]->value
.b
[c
] ? 1 : 0;
635 assert(op
[0]->type
->is_integer());
636 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
637 data
.b
[c
] = op
[0]->value
.u
[c
] ? true : false;
641 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
642 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
643 data
.i
[c
] = op
[0]->value
.u
[c
];
647 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
648 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
649 data
.u
[c
] = op
[0]->value
.i
[c
];
652 case ir_unop_bitcast_i2f
:
653 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
654 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
655 data
.f
[c
] = bitcast_u2f(op
[0]->value
.i
[c
]);
658 case ir_unop_bitcast_f2i
:
659 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
660 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
661 data
.i
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
664 case ir_unop_bitcast_u2f
:
665 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
666 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
667 data
.f
[c
] = bitcast_u2f(op
[0]->value
.u
[c
]);
670 case ir_unop_bitcast_f2u
:
671 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
672 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
673 data
.u
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
677 assert(op
[0]->type
->is_boolean());
679 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
680 if (op
[0]->value
.b
[c
])
685 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
686 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
687 data
.f
[c
] = op
[0]->value
.d
[c
];
691 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
692 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
693 data
.d
[c
] = op
[0]->value
.f
[c
];
697 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
698 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
699 data
.i
[c
] = op
[0]->value
.d
[c
];
703 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
704 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
705 data
.d
[c
] = op
[0]->value
.i
[c
];
709 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
710 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
711 data
.u
[c
] = op
[0]->value
.d
[c
];
715 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
716 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
717 data
.d
[c
] = op
[0]->value
.u
[c
];
721 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
722 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
723 data
.b
[c
] = op
[0]->value
.d
[c
] != 0.0;
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
] = trunc(op
[0]->value
.d
[c
]);
731 data
.f
[c
] = truncf(op
[0]->value
.f
[c
]);
735 case ir_unop_round_even
:
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
] = _mesa_roundeven(op
[0]->value
.d
[c
]);
740 data
.f
[c
] = _mesa_roundevenf(op
[0]->value
.f
[c
]);
745 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
746 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
747 data
.d
[c
] = ceil(op
[0]->value
.d
[c
]);
749 data
.f
[c
] = ceilf(op
[0]->value
.f
[c
]);
754 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
755 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
756 data
.d
[c
] = floor(op
[0]->value
.d
[c
]);
758 data
.f
[c
] = floorf(op
[0]->value
.f
[c
]);
763 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
764 switch (this->type
->base_type
) {
771 case GLSL_TYPE_FLOAT
:
772 data
.f
[c
] = op
[0]->value
.f
[c
] - floor(op
[0]->value
.f
[c
]);
774 case GLSL_TYPE_DOUBLE
:
775 data
.d
[c
] = op
[0]->value
.d
[c
] - floor(op
[0]->value
.d
[c
]);
784 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
785 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
786 data
.f
[c
] = sinf(op
[0]->value
.f
[c
]);
791 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
792 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
793 data
.f
[c
] = cosf(op
[0]->value
.f
[c
]);
798 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
799 switch (this->type
->base_type
) {
801 data
.u
[c
] = -((int) op
[0]->value
.u
[c
]);
804 data
.i
[c
] = -op
[0]->value
.i
[c
];
806 case GLSL_TYPE_FLOAT
:
807 data
.f
[c
] = -op
[0]->value
.f
[c
];
809 case GLSL_TYPE_DOUBLE
:
810 data
.d
[c
] = -op
[0]->value
.d
[c
];
819 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
820 switch (this->type
->base_type
) {
822 data
.u
[c
] = op
[0]->value
.u
[c
];
825 data
.i
[c
] = op
[0]->value
.i
[c
];
827 data
.i
[c
] = -data
.i
[c
];
829 case GLSL_TYPE_FLOAT
:
830 data
.f
[c
] = fabs(op
[0]->value
.f
[c
]);
832 case GLSL_TYPE_DOUBLE
:
833 data
.d
[c
] = fabs(op
[0]->value
.d
[c
]);
842 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
843 switch (this->type
->base_type
) {
845 data
.u
[c
] = op
[0]->value
.i
[c
] > 0;
848 data
.i
[c
] = (op
[0]->value
.i
[c
] > 0) - (op
[0]->value
.i
[c
] < 0);
850 case GLSL_TYPE_FLOAT
:
851 data
.f
[c
] = float((op
[0]->value
.f
[c
] > 0)-(op
[0]->value
.f
[c
] < 0));
853 case GLSL_TYPE_DOUBLE
:
854 data
.d
[c
] = double((op
[0]->value
.d
[c
] > 0)-(op
[0]->value
.d
[c
] < 0));
863 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
864 switch (this->type
->base_type
) {
866 if (op
[0]->value
.u
[c
] != 0.0)
867 data
.u
[c
] = 1 / op
[0]->value
.u
[c
];
870 if (op
[0]->value
.i
[c
] != 0.0)
871 data
.i
[c
] = 1 / op
[0]->value
.i
[c
];
873 case GLSL_TYPE_FLOAT
:
874 if (op
[0]->value
.f
[c
] != 0.0)
875 data
.f
[c
] = 1.0F
/ op
[0]->value
.f
[c
];
877 case GLSL_TYPE_DOUBLE
:
878 if (op
[0]->value
.d
[c
] != 0.0)
879 data
.d
[c
] = 1.0 / op
[0]->value
.d
[c
];
888 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
889 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
890 data
.d
[c
] = 1.0 / sqrt(op
[0]->value
.d
[c
]);
892 data
.f
[c
] = 1.0F
/ sqrtf(op
[0]->value
.f
[c
]);
897 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
898 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
899 data
.d
[c
] = sqrt(op
[0]->value
.d
[c
]);
901 data
.f
[c
] = sqrtf(op
[0]->value
.f
[c
]);
906 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
907 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
908 data
.f
[c
] = expf(op
[0]->value
.f
[c
]);
913 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
914 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
915 data
.f
[c
] = exp2f(op
[0]->value
.f
[c
]);
920 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
921 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
922 data
.f
[c
] = logf(op
[0]->value
.f
[c
]);
927 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
928 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
929 data
.f
[c
] = log2f(op
[0]->value
.f
[c
]);
934 case ir_unop_dFdx_coarse
:
935 case ir_unop_dFdx_fine
:
937 case ir_unop_dFdy_coarse
:
938 case ir_unop_dFdy_fine
:
939 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
940 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
945 case ir_unop_pack_snorm_2x16
:
946 assert(op
[0]->type
== glsl_type::vec2_type
);
947 data
.u
[0] = pack_2x16(pack_snorm_1x16
,
951 case ir_unop_pack_snorm_4x8
:
952 assert(op
[0]->type
== glsl_type::vec4_type
);
953 data
.u
[0] = pack_4x8(pack_snorm_1x8
,
959 case ir_unop_unpack_snorm_2x16
:
960 assert(op
[0]->type
== glsl_type::uint_type
);
961 unpack_2x16(unpack_snorm_1x16
,
963 &data
.f
[0], &data
.f
[1]);
965 case ir_unop_unpack_snorm_4x8
:
966 assert(op
[0]->type
== glsl_type::uint_type
);
967 unpack_4x8(unpack_snorm_1x8
,
969 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
971 case ir_unop_pack_unorm_2x16
:
972 assert(op
[0]->type
== glsl_type::vec2_type
);
973 data
.u
[0] = pack_2x16(pack_unorm_1x16
,
977 case ir_unop_pack_unorm_4x8
:
978 assert(op
[0]->type
== glsl_type::vec4_type
);
979 data
.u
[0] = pack_4x8(pack_unorm_1x8
,
985 case ir_unop_unpack_unorm_2x16
:
986 assert(op
[0]->type
== glsl_type::uint_type
);
987 unpack_2x16(unpack_unorm_1x16
,
989 &data
.f
[0], &data
.f
[1]);
991 case ir_unop_unpack_unorm_4x8
:
992 assert(op
[0]->type
== glsl_type::uint_type
);
993 unpack_4x8(unpack_unorm_1x8
,
995 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
997 case ir_unop_pack_half_2x16
:
998 assert(op
[0]->type
== glsl_type::vec2_type
);
999 data
.u
[0] = pack_2x16(pack_half_1x16
,
1003 case ir_unop_unpack_half_2x16
:
1004 assert(op
[0]->type
== glsl_type::uint_type
);
1005 unpack_2x16(unpack_half_1x16
,
1007 &data
.f
[0], &data
.f
[1]);
1010 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1011 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1012 data
.f
[c
] = powf(op
[0]->value
.f
[c
], op
[1]->value
.f
[c
]);
1017 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1018 data
.d
[0] = dot_d(op
[0], op
[1]);
1020 data
.f
[0] = dot_f(op
[0], op
[1]);
1024 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1025 for (unsigned c
= 0, c0
= 0, c1
= 0;
1027 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1029 switch (op
[0]->type
->base_type
) {
1030 case GLSL_TYPE_UINT
:
1031 data
.u
[c
] = MIN2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
1034 data
.i
[c
] = MIN2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
1036 case GLSL_TYPE_FLOAT
:
1037 data
.f
[c
] = MIN2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
1039 case GLSL_TYPE_DOUBLE
:
1040 data
.d
[c
] = MIN2(op
[0]->value
.d
[c0
], op
[1]->value
.d
[c1
]);
1049 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1050 for (unsigned c
= 0, c0
= 0, c1
= 0;
1052 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1054 switch (op
[0]->type
->base_type
) {
1055 case GLSL_TYPE_UINT
:
1056 data
.u
[c
] = MAX2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
1059 data
.i
[c
] = MAX2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
1061 case GLSL_TYPE_FLOAT
:
1062 data
.f
[c
] = MAX2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
1064 case GLSL_TYPE_DOUBLE
:
1065 data
.d
[c
] = MAX2(op
[0]->value
.d
[c0
], op
[1]->value
.d
[c1
]);
1074 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1075 for (unsigned c
= 0, c0
= 0, c1
= 0;
1077 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1079 switch (op
[0]->type
->base_type
) {
1080 case GLSL_TYPE_UINT
:
1081 data
.u
[c
] = op
[0]->value
.u
[c0
] + op
[1]->value
.u
[c1
];
1084 data
.i
[c
] = op
[0]->value
.i
[c0
] + op
[1]->value
.i
[c1
];
1086 case GLSL_TYPE_FLOAT
:
1087 data
.f
[c
] = op
[0]->value
.f
[c0
] + op
[1]->value
.f
[c1
];
1089 case GLSL_TYPE_DOUBLE
:
1090 data
.d
[c
] = op
[0]->value
.d
[c0
] + op
[1]->value
.d
[c1
];
1099 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1100 for (unsigned c
= 0, c0
= 0, c1
= 0;
1102 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1104 switch (op
[0]->type
->base_type
) {
1105 case GLSL_TYPE_UINT
:
1106 data
.u
[c
] = op
[0]->value
.u
[c0
] - op
[1]->value
.u
[c1
];
1109 data
.i
[c
] = op
[0]->value
.i
[c0
] - op
[1]->value
.i
[c1
];
1111 case GLSL_TYPE_FLOAT
:
1112 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
];
1114 case GLSL_TYPE_DOUBLE
:
1115 data
.d
[c
] = op
[0]->value
.d
[c0
] - op
[1]->value
.d
[c1
];
1124 /* Check for equal types, or unequal types involving scalars */
1125 if ((op
[0]->type
== op
[1]->type
&& !op
[0]->type
->is_matrix())
1126 || op0_scalar
|| op1_scalar
) {
1127 for (unsigned c
= 0, c0
= 0, c1
= 0;
1129 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1131 switch (op
[0]->type
->base_type
) {
1132 case GLSL_TYPE_UINT
:
1133 data
.u
[c
] = op
[0]->value
.u
[c0
] * op
[1]->value
.u
[c1
];
1136 data
.i
[c
] = op
[0]->value
.i
[c0
] * op
[1]->value
.i
[c1
];
1138 case GLSL_TYPE_FLOAT
:
1139 data
.f
[c
] = op
[0]->value
.f
[c0
] * op
[1]->value
.f
[c1
];
1141 case GLSL_TYPE_DOUBLE
:
1142 data
.d
[c
] = op
[0]->value
.d
[c0
] * op
[1]->value
.d
[c1
];
1149 assert(op
[0]->type
->is_matrix() || op
[1]->type
->is_matrix());
1151 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
1152 * matrix can be a GLSL vector, either N or P can be 1.
1154 * For vec*mat, the vector is treated as a row vector. This
1155 * means the vector is a 1-row x M-column matrix.
1157 * For mat*vec, the vector is treated as a column vector. Since
1158 * matrix_columns is 1 for vectors, this just works.
1160 const unsigned n
= op
[0]->type
->is_vector()
1161 ? 1 : op
[0]->type
->vector_elements
;
1162 const unsigned m
= op
[1]->type
->vector_elements
;
1163 const unsigned p
= op
[1]->type
->matrix_columns
;
1164 for (unsigned j
= 0; j
< p
; j
++) {
1165 for (unsigned i
= 0; i
< n
; i
++) {
1166 for (unsigned k
= 0; k
< m
; k
++) {
1167 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1168 data
.d
[i
+n
*j
] += op
[0]->value
.d
[i
+n
*k
]*op
[1]->value
.d
[k
+m
*j
];
1170 data
.f
[i
+n
*j
] += op
[0]->value
.f
[i
+n
*k
]*op
[1]->value
.f
[k
+m
*j
];
1178 /* FINISHME: Emit warning when division-by-zero is detected. */
1179 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1180 for (unsigned c
= 0, c0
= 0, c1
= 0;
1182 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1184 switch (op
[0]->type
->base_type
) {
1185 case GLSL_TYPE_UINT
:
1186 if (op
[1]->value
.u
[c1
] == 0) {
1189 data
.u
[c
] = op
[0]->value
.u
[c0
] / op
[1]->value
.u
[c1
];
1193 if (op
[1]->value
.i
[c1
] == 0) {
1196 data
.i
[c
] = op
[0]->value
.i
[c0
] / op
[1]->value
.i
[c1
];
1199 case GLSL_TYPE_FLOAT
:
1200 data
.f
[c
] = op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
];
1202 case GLSL_TYPE_DOUBLE
:
1203 data
.d
[c
] = op
[0]->value
.d
[c0
] / op
[1]->value
.d
[c1
];
1212 /* FINISHME: Emit warning when division-by-zero is detected. */
1213 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1214 for (unsigned c
= 0, c0
= 0, c1
= 0;
1216 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1218 switch (op
[0]->type
->base_type
) {
1219 case GLSL_TYPE_UINT
:
1220 if (op
[1]->value
.u
[c1
] == 0) {
1223 data
.u
[c
] = op
[0]->value
.u
[c0
] % op
[1]->value
.u
[c1
];
1227 if (op
[1]->value
.i
[c1
] == 0) {
1230 data
.i
[c
] = op
[0]->value
.i
[c0
] % op
[1]->value
.i
[c1
];
1233 case GLSL_TYPE_FLOAT
:
1234 /* We don't use fmod because it rounds toward zero; GLSL specifies
1237 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
]
1238 * floorf(op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
]);
1240 case GLSL_TYPE_DOUBLE
:
1241 /* We don't use fmod because it rounds toward zero; GLSL specifies
1244 data
.d
[c
] = op
[0]->value
.d
[c0
] - op
[1]->value
.d
[c1
]
1245 * floor(op
[0]->value
.d
[c0
] / op
[1]->value
.d
[c1
]);
1254 case ir_binop_logic_and
:
1255 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1256 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1257 data
.b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
1259 case ir_binop_logic_xor
:
1260 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1261 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1262 data
.b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
1264 case ir_binop_logic_or
:
1265 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1266 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1267 data
.b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
1271 assert(op
[0]->type
== op
[1]->type
);
1272 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1273 switch (op
[0]->type
->base_type
) {
1274 case GLSL_TYPE_UINT
:
1275 data
.b
[c
] = op
[0]->value
.u
[c
] < op
[1]->value
.u
[c
];
1278 data
.b
[c
] = op
[0]->value
.i
[c
] < op
[1]->value
.i
[c
];
1280 case GLSL_TYPE_FLOAT
:
1281 data
.b
[c
] = op
[0]->value
.f
[c
] < op
[1]->value
.f
[c
];
1283 case GLSL_TYPE_DOUBLE
:
1284 data
.b
[c
] = op
[0]->value
.d
[c
] < op
[1]->value
.d
[c
];
1291 case ir_binop_greater
:
1292 assert(op
[0]->type
== op
[1]->type
);
1293 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1294 switch (op
[0]->type
->base_type
) {
1295 case GLSL_TYPE_UINT
:
1296 data
.b
[c
] = op
[0]->value
.u
[c
] > op
[1]->value
.u
[c
];
1299 data
.b
[c
] = op
[0]->value
.i
[c
] > op
[1]->value
.i
[c
];
1301 case GLSL_TYPE_FLOAT
:
1302 data
.b
[c
] = op
[0]->value
.f
[c
] > op
[1]->value
.f
[c
];
1304 case GLSL_TYPE_DOUBLE
:
1305 data
.b
[c
] = op
[0]->value
.d
[c
] > op
[1]->value
.d
[c
];
1312 case ir_binop_lequal
:
1313 assert(op
[0]->type
== op
[1]->type
);
1314 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1315 switch (op
[0]->type
->base_type
) {
1316 case GLSL_TYPE_UINT
:
1317 data
.b
[c
] = op
[0]->value
.u
[c
] <= op
[1]->value
.u
[c
];
1320 data
.b
[c
] = op
[0]->value
.i
[c
] <= op
[1]->value
.i
[c
];
1322 case GLSL_TYPE_FLOAT
:
1323 data
.b
[c
] = op
[0]->value
.f
[c
] <= op
[1]->value
.f
[c
];
1325 case GLSL_TYPE_DOUBLE
:
1326 data
.b
[c
] = op
[0]->value
.d
[c
] <= op
[1]->value
.d
[c
];
1333 case ir_binop_gequal
:
1334 assert(op
[0]->type
== op
[1]->type
);
1335 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1336 switch (op
[0]->type
->base_type
) {
1337 case GLSL_TYPE_UINT
:
1338 data
.b
[c
] = op
[0]->value
.u
[c
] >= op
[1]->value
.u
[c
];
1341 data
.b
[c
] = op
[0]->value
.i
[c
] >= op
[1]->value
.i
[c
];
1343 case GLSL_TYPE_FLOAT
:
1344 data
.b
[c
] = op
[0]->value
.f
[c
] >= op
[1]->value
.f
[c
];
1346 case GLSL_TYPE_DOUBLE
:
1347 data
.b
[c
] = op
[0]->value
.d
[c
] >= op
[1]->value
.d
[c
];
1354 case ir_binop_equal
:
1355 assert(op
[0]->type
== op
[1]->type
);
1356 for (unsigned c
= 0; c
< components
; c
++) {
1357 switch (op
[0]->type
->base_type
) {
1358 case GLSL_TYPE_UINT
:
1359 data
.b
[c
] = op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
1362 data
.b
[c
] = op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
1364 case GLSL_TYPE_FLOAT
:
1365 data
.b
[c
] = op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
1367 case GLSL_TYPE_BOOL
:
1368 data
.b
[c
] = op
[0]->value
.b
[c
] == op
[1]->value
.b
[c
];
1370 case GLSL_TYPE_DOUBLE
:
1371 data
.b
[c
] = op
[0]->value
.d
[c
] == op
[1]->value
.d
[c
];
1378 case ir_binop_nequal
:
1379 assert(op
[0]->type
== op
[1]->type
);
1380 for (unsigned c
= 0; c
< components
; c
++) {
1381 switch (op
[0]->type
->base_type
) {
1382 case GLSL_TYPE_UINT
:
1383 data
.b
[c
] = op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
1386 data
.b
[c
] = op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
1388 case GLSL_TYPE_FLOAT
:
1389 data
.b
[c
] = op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
1391 case GLSL_TYPE_BOOL
:
1392 data
.b
[c
] = op
[0]->value
.b
[c
] != op
[1]->value
.b
[c
];
1394 case GLSL_TYPE_DOUBLE
:
1395 data
.b
[c
] = op
[0]->value
.d
[c
] != op
[1]->value
.d
[c
];
1402 case ir_binop_all_equal
:
1403 data
.b
[0] = op
[0]->has_value(op
[1]);
1405 case ir_binop_any_nequal
:
1406 data
.b
[0] = !op
[0]->has_value(op
[1]);
1409 case ir_binop_lshift
:
1410 for (unsigned c
= 0, c0
= 0, c1
= 0;
1412 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1414 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1415 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1416 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.i
[c1
];
1418 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1419 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1420 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.u
[c1
];
1422 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1423 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1424 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.i
[c1
];
1426 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1427 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1428 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.u
[c1
];
1433 case ir_binop_rshift
:
1434 for (unsigned c
= 0, c0
= 0, c1
= 0;
1436 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1438 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1439 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1440 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.i
[c1
];
1442 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1443 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1444 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.u
[c1
];
1446 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1447 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1448 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.i
[c1
];
1450 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1451 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1452 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.u
[c1
];
1457 case ir_binop_bit_and
:
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_bit_or
:
1476 for (unsigned c
= 0, c0
= 0, c1
= 0;
1478 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1480 switch (op
[0]->type
->base_type
) {
1482 data
.i
[c
] = op
[0]->value
.i
[c0
] | op
[1]->value
.i
[c1
];
1484 case GLSL_TYPE_UINT
:
1485 data
.u
[c
] = op
[0]->value
.u
[c0
] | op
[1]->value
.u
[c1
];
1493 case ir_binop_vector_extract
: {
1494 const int c
= CLAMP(op
[1]->value
.i
[0], 0,
1495 (int) op
[0]->type
->vector_elements
- 1);
1497 switch (op
[0]->type
->base_type
) {
1498 case GLSL_TYPE_UINT
:
1499 data
.u
[0] = op
[0]->value
.u
[c
];
1502 data
.i
[0] = op
[0]->value
.i
[c
];
1504 case GLSL_TYPE_FLOAT
:
1505 data
.f
[0] = op
[0]->value
.f
[c
];
1507 case GLSL_TYPE_DOUBLE
:
1508 data
.d
[0] = op
[0]->value
.d
[c
];
1510 case GLSL_TYPE_BOOL
:
1511 data
.b
[0] = op
[0]->value
.b
[c
];
1519 case ir_binop_bit_xor
:
1520 for (unsigned c
= 0, c0
= 0, c1
= 0;
1522 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1524 switch (op
[0]->type
->base_type
) {
1526 data
.i
[c
] = op
[0]->value
.i
[c0
] ^ op
[1]->value
.i
[c1
];
1528 case GLSL_TYPE_UINT
:
1529 data
.u
[c
] = op
[0]->value
.u
[c0
] ^ op
[1]->value
.u
[c1
];
1537 case ir_unop_bitfield_reverse
:
1538 /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
1539 for (unsigned c
= 0; c
< components
; c
++) {
1540 unsigned int v
= op
[0]->value
.u
[c
]; // input bits to be reversed
1541 unsigned int r
= v
; // r will be reversed bits of v; first get LSB of v
1542 int s
= sizeof(v
) * CHAR_BIT
- 1; // extra shift needed at end
1544 for (v
>>= 1; v
; v
>>= 1) {
1549 r
<<= s
; // shift when v's highest bits are zero
1555 case ir_unop_bit_count
:
1556 for (unsigned c
= 0; c
< components
; c
++) {
1558 unsigned v
= op
[0]->value
.u
[c
];
1560 for (; v
; count
++) {
1567 case ir_unop_find_msb
:
1568 for (unsigned c
= 0; c
< components
; c
++) {
1569 int v
= op
[0]->value
.i
[c
];
1571 if (v
== 0 || (op
[0]->type
->base_type
== GLSL_TYPE_INT
&& v
== -1))
1575 int top_bit
= op
[0]->type
->base_type
== GLSL_TYPE_UINT
1576 ? 0 : v
& (1 << 31);
1578 while (((v
& (1 << 31)) == top_bit
) && count
!= 32) {
1583 data
.i
[c
] = 31 - count
;
1588 case ir_unop_find_lsb
:
1589 for (unsigned c
= 0; c
< components
; c
++) {
1590 if (op
[0]->value
.i
[c
] == 0)
1594 unsigned v
= op
[0]->value
.u
[c
];
1596 for (; !(v
& 1); v
>>= 1) {
1604 case ir_unop_saturate
:
1605 for (unsigned c
= 0; c
< components
; c
++) {
1606 data
.f
[c
] = CLAMP(op
[0]->value
.f
[c
], 0.0f
, 1.0f
);
1609 case ir_unop_pack_double_2x32
: {
1610 /* XXX needs to be checked on big-endian */
1612 temp
= (uint64_t)op
[0]->value
.u
[0] | ((uint64_t)op
[0]->value
.u
[1] << 32);
1613 data
.d
[0] = *(double *)&temp
;
1617 case ir_unop_unpack_double_2x32
:
1618 /* XXX needs to be checked on big-endian */
1619 data
.u
[0] = *(uint32_t *)&op
[0]->value
.d
[0];
1620 data
.u
[1] = *((uint32_t *)&op
[0]->value
.d
[0] + 1);
1623 case ir_triop_bitfield_extract
: {
1624 int offset
= op
[1]->value
.i
[0];
1625 int bits
= op
[2]->value
.i
[0];
1627 for (unsigned c
= 0; c
< components
; c
++) {
1630 else if (offset
< 0 || bits
< 0)
1631 data
.u
[c
] = 0; /* Undefined, per spec. */
1632 else if (offset
+ bits
> 32)
1633 data
.u
[c
] = 0; /* Undefined, per spec. */
1635 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
) {
1636 /* int so that the right shift will sign-extend. */
1637 int value
= op
[0]->value
.i
[c
];
1638 value
<<= 32 - bits
- offset
;
1639 value
>>= 32 - bits
;
1642 unsigned value
= op
[0]->value
.u
[c
];
1643 value
<<= 32 - bits
- offset
;
1644 value
>>= 32 - bits
;
1652 case ir_binop_bfm
: {
1653 int bits
= op
[0]->value
.i
[0];
1654 int offset
= op
[1]->value
.i
[0];
1656 for (unsigned c
= 0; c
< components
; c
++) {
1658 data
.u
[c
] = op
[0]->value
.u
[c
];
1659 else if (offset
< 0 || bits
< 0)
1660 data
.u
[c
] = 0; /* Undefined for bitfieldInsert, per spec. */
1661 else if (offset
+ bits
> 32)
1662 data
.u
[c
] = 0; /* Undefined for bitfieldInsert, per spec. */
1664 data
.u
[c
] = ((1 << bits
) - 1) << offset
;
1669 case ir_binop_ldexp
:
1670 for (unsigned c
= 0; c
< components
; c
++) {
1671 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
) {
1672 data
.d
[c
] = ldexp(op
[0]->value
.d
[c
], op
[1]->value
.i
[c
]);
1673 /* Flush subnormal values to zero. */
1674 if (!isnormal(data
.d
[c
]))
1675 data
.d
[c
] = copysign(0.0, op
[0]->value
.d
[c
]);
1677 data
.f
[c
] = ldexp(op
[0]->value
.f
[c
], op
[1]->value
.i
[c
]);
1678 /* Flush subnormal values to zero. */
1679 if (!isnormal(data
.f
[c
]))
1680 data
.f
[c
] = copysign(0.0f
, op
[0]->value
.f
[c
]);
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 for (unsigned c
= 0; c
< components
; c
++) {
1694 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1695 data
.d
[c
] = op
[0]->value
.d
[c
] * op
[1]->value
.d
[c
]
1696 + op
[2]->value
.d
[c
];
1698 data
.f
[c
] = op
[0]->value
.f
[c
] * op
[1]->value
.f
[c
]
1699 + op
[2]->value
.f
[c
];
1703 case ir_triop_lrp
: {
1704 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
1705 op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1706 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
||
1707 op
[1]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1708 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
||
1709 op
[2]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1711 unsigned c2_inc
= op
[2]->type
->is_scalar() ? 0 : 1;
1712 for (unsigned c
= 0, c2
= 0; c
< components
; c2
+= c2_inc
, c
++) {
1713 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1714 data
.d
[c
] = op
[0]->value
.d
[c
] * (1.0 - op
[2]->value
.d
[c2
]) +
1715 (op
[1]->value
.d
[c
] * op
[2]->value
.d
[c2
]);
1717 data
.f
[c
] = op
[0]->value
.f
[c
] * (1.0f
- op
[2]->value
.f
[c2
]) +
1718 (op
[1]->value
.f
[c
] * op
[2]->value
.f
[c2
]);
1724 for (unsigned c
= 0; c
< components
; c
++) {
1725 if (op
[1]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1726 data
.d
[c
] = op
[0]->value
.b
[c
] ? op
[1]->value
.d
[c
]
1727 : op
[2]->value
.d
[c
];
1729 data
.u
[c
] = op
[0]->value
.b
[c
] ? op
[1]->value
.u
[c
]
1730 : op
[2]->value
.u
[c
];
1734 case ir_triop_vector_insert
: {
1735 const unsigned idx
= op
[2]->value
.u
[0];
1737 memcpy(&data
, &op
[0]->value
, sizeof(data
));
1739 switch (this->type
->base_type
) {
1741 data
.i
[idx
] = op
[1]->value
.i
[0];
1743 case GLSL_TYPE_UINT
:
1744 data
.u
[idx
] = op
[1]->value
.u
[0];
1746 case GLSL_TYPE_FLOAT
:
1747 data
.f
[idx
] = op
[1]->value
.f
[0];
1749 case GLSL_TYPE_BOOL
:
1750 data
.b
[idx
] = op
[1]->value
.b
[0];
1752 case GLSL_TYPE_DOUBLE
:
1753 data
.d
[idx
] = op
[1]->value
.d
[0];
1756 assert(!"Should not get here.");
1762 case ir_quadop_bitfield_insert
: {
1763 int offset
= op
[2]->value
.i
[0];
1764 int bits
= op
[3]->value
.i
[0];
1766 for (unsigned c
= 0; c
< components
; c
++) {
1768 data
.u
[c
] = op
[0]->value
.u
[c
];
1769 else if (offset
< 0 || bits
< 0)
1770 data
.u
[c
] = 0; /* Undefined, per spec. */
1771 else if (offset
+ bits
> 32)
1772 data
.u
[c
] = 0; /* Undefined, per spec. */
1774 unsigned insert_mask
= ((1 << bits
) - 1) << offset
;
1776 unsigned insert
= op
[1]->value
.u
[c
];
1778 insert
&= insert_mask
;
1780 unsigned base
= op
[0]->value
.u
[c
];
1781 base
&= ~insert_mask
;
1783 data
.u
[c
] = base
| insert
;
1789 case ir_quadop_vector
:
1790 for (unsigned c
= 0; c
< this->type
->vector_elements
; c
++) {
1791 switch (this->type
->base_type
) {
1793 data
.i
[c
] = op
[c
]->value
.i
[0];
1795 case GLSL_TYPE_UINT
:
1796 data
.u
[c
] = op
[c
]->value
.u
[0];
1798 case GLSL_TYPE_FLOAT
:
1799 data
.f
[c
] = op
[c
]->value
.f
[0];
1801 case GLSL_TYPE_DOUBLE
:
1802 data
.d
[c
] = op
[c
]->value
.d
[0];
1811 /* FINISHME: Should handle all expression types. */
1815 return new(ctx
) ir_constant(this->type
, &data
);
1820 ir_texture::constant_expression_value(struct hash_table
*)
1822 /* texture lookups aren't constant expressions */
1828 ir_swizzle::constant_expression_value(struct hash_table
*variable_context
)
1830 ir_constant
*v
= this->val
->constant_expression_value(variable_context
);
1833 ir_constant_data data
= { { 0 } };
1835 const unsigned swiz_idx
[4] = {
1836 this->mask
.x
, this->mask
.y
, this->mask
.z
, this->mask
.w
1839 for (unsigned i
= 0; i
< this->mask
.num_components
; i
++) {
1840 switch (v
->type
->base_type
) {
1841 case GLSL_TYPE_UINT
:
1842 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
1843 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
1844 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
1845 case GLSL_TYPE_DOUBLE
:data
.d
[i
] = v
->value
.d
[swiz_idx
[i
]]; break;
1846 default: assert(!"Should not get here."); break;
1850 void *ctx
= ralloc_parent(this);
1851 return new(ctx
) ir_constant(this->type
, &data
);
1858 ir_dereference_variable::constant_expression_value(struct hash_table
*variable_context
)
1860 /* This may occur during compile and var->type is glsl_type::error_type */
1864 /* Give priority to the context hashtable, if it exists */
1865 if (variable_context
) {
1866 ir_constant
*value
= (ir_constant
*)hash_table_find(variable_context
, var
);
1871 /* The constant_value of a uniform variable is its initializer,
1872 * not the lifetime constant value of the uniform.
1874 if (var
->data
.mode
== ir_var_uniform
)
1877 if (!var
->constant_value
)
1880 return var
->constant_value
->clone(ralloc_parent(var
), NULL
);
1885 ir_dereference_array::constant_expression_value(struct hash_table
*variable_context
)
1887 ir_constant
*array
= this->array
->constant_expression_value(variable_context
);
1888 ir_constant
*idx
= this->array_index
->constant_expression_value(variable_context
);
1890 if ((array
!= NULL
) && (idx
!= NULL
)) {
1891 void *ctx
= ralloc_parent(this);
1892 if (array
->type
->is_matrix()) {
1893 /* Array access of a matrix results in a vector.
1895 const unsigned column
= idx
->value
.u
[0];
1897 const glsl_type
*const column_type
= array
->type
->column_type();
1899 /* Offset in the constant matrix to the first element of the column
1902 const unsigned mat_idx
= column
* column_type
->vector_elements
;
1904 ir_constant_data data
= { { 0 } };
1906 switch (column_type
->base_type
) {
1907 case GLSL_TYPE_UINT
:
1909 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1910 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
1914 case GLSL_TYPE_FLOAT
:
1915 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1916 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
1920 case GLSL_TYPE_DOUBLE
:
1921 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1922 data
.d
[i
] = array
->value
.d
[mat_idx
+ i
];
1927 assert(!"Should not get here.");
1931 return new(ctx
) ir_constant(column_type
, &data
);
1932 } else if (array
->type
->is_vector()) {
1933 const unsigned component
= idx
->value
.u
[0];
1935 return new(ctx
) ir_constant(array
, component
);
1937 const unsigned index
= idx
->value
.u
[0];
1938 return array
->get_array_element(index
)->clone(ctx
, NULL
);
1946 ir_dereference_record::constant_expression_value(struct hash_table
*)
1948 ir_constant
*v
= this->record
->constant_expression_value();
1950 return (v
!= NULL
) ? v
->get_record_field(this->field
) : NULL
;
1955 ir_assignment::constant_expression_value(struct hash_table
*)
1957 /* FINISHME: Handle CEs involving assignment (return RHS) */
1963 ir_constant::constant_expression_value(struct hash_table
*)
1970 ir_call::constant_expression_value(struct hash_table
*variable_context
)
1972 return this->callee
->constant_expression_value(&this->actual_parameters
, variable_context
);
1976 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list
&body
,
1977 struct hash_table
*variable_context
,
1978 ir_constant
**result
)
1980 foreach_in_list(ir_instruction
, inst
, &body
) {
1981 switch(inst
->ir_type
) {
1983 /* (declare () type symbol) */
1984 case ir_type_variable
: {
1985 ir_variable
*var
= inst
->as_variable();
1986 hash_table_insert(variable_context
, ir_constant::zero(this, var
->type
), var
);
1990 /* (assign [condition] (write-mask) (ref) (value)) */
1991 case ir_type_assignment
: {
1992 ir_assignment
*asg
= inst
->as_assignment();
1993 if (asg
->condition
) {
1994 ir_constant
*cond
= asg
->condition
->constant_expression_value(variable_context
);
1997 if (!cond
->get_bool_component(0))
2001 ir_constant
*store
= NULL
;
2004 if (!constant_referenced(asg
->lhs
, variable_context
, store
, offset
))
2007 ir_constant
*value
= asg
->rhs
->constant_expression_value(variable_context
);
2012 store
->copy_masked_offset(value
, offset
, asg
->write_mask
);
2016 /* (return (expression)) */
2017 case ir_type_return
:
2019 *result
= inst
->as_return()->value
->constant_expression_value(variable_context
);
2020 return *result
!= NULL
;
2022 /* (call name (ref) (params))*/
2023 case ir_type_call
: {
2024 ir_call
*call
= inst
->as_call();
2026 /* Just say no to void functions in constant expressions. We
2027 * don't need them at that point.
2030 if (!call
->return_deref
)
2033 ir_constant
*store
= NULL
;
2036 if (!constant_referenced(call
->return_deref
, variable_context
,
2040 ir_constant
*value
= call
->constant_expression_value(variable_context
);
2045 store
->copy_offset(value
, offset
);
2049 /* (if condition (then-instructions) (else-instructions)) */
2051 ir_if
*iif
= inst
->as_if();
2053 ir_constant
*cond
= iif
->condition
->constant_expression_value(variable_context
);
2054 if (!cond
|| !cond
->type
->is_boolean())
2057 exec_list
&branch
= cond
->get_bool_component(0) ? iif
->then_instructions
: iif
->else_instructions
;
2060 if (!constant_expression_evaluate_expression_list(branch
, variable_context
, result
))
2063 /* If there was a return in the branch chosen, drop out now. */
2070 /* Every other expression type, we drop out. */
2076 /* Reaching the end of the block is not an error condition */
2084 ir_function_signature::constant_expression_value(exec_list
*actual_parameters
, struct hash_table
*variable_context
)
2086 const glsl_type
*type
= this->return_type
;
2087 if (type
== glsl_type::void_type
)
2090 /* From the GLSL 1.20 spec, page 23:
2091 * "Function calls to user-defined functions (non-built-in functions)
2092 * cannot be used to form constant expressions."
2094 if (!this->is_builtin())
2098 * Of the builtin functions, only the texture lookups and the noise
2099 * ones must not be used in constant expressions. They all include
2100 * specific opcodes so they don't need to be special-cased at this
2104 /* Initialize the table of dereferencable names with the function
2105 * parameters. Verify their const-ness on the way.
2107 * We expect the correctness of the number of parameters to have
2108 * been checked earlier.
2110 hash_table
*deref_hash
= hash_table_ctor(8, hash_table_pointer_hash
,
2111 hash_table_pointer_compare
);
2113 /* If "origin" is non-NULL, then the function body is there. So we
2114 * have to use the variable objects from the object with the body,
2115 * but the parameter instanciation on the current object.
2117 const exec_node
*parameter_info
= origin
? origin
->parameters
.head
: parameters
.head
;
2119 foreach_in_list(ir_rvalue
, n
, actual_parameters
) {
2120 ir_constant
*constant
= n
->constant_expression_value(variable_context
);
2121 if (constant
== NULL
) {
2122 hash_table_dtor(deref_hash
);
2127 ir_variable
*var
= (ir_variable
*)parameter_info
;
2128 hash_table_insert(deref_hash
, constant
, var
);
2130 parameter_info
= parameter_info
->next
;
2133 ir_constant
*result
= NULL
;
2135 /* Now run the builtin function until something non-constant
2136 * happens or we get the result.
2138 if (constant_expression_evaluate_expression_list(origin
? origin
->body
: body
, deref_hash
, &result
) && result
)
2139 result
= result
->clone(ralloc_parent(this), NULL
);
2141 hash_table_dtor(deref_hash
);