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 */
39 #include "ir_visitor.h"
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)
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(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
];
75 /* This method is the only one supported by gcc. Unions in particular
76 * are iffy, and read-through-converted-pointer is killed by strict
77 * aliasing. OTOH, the compiler sees through the memcpy, so the
78 * resulting asm is reasonable.
81 bitcast_u2f(unsigned int u
)
83 assert(sizeof(float) == sizeof(unsigned int));
85 memcpy(&f
, &u
, sizeof(f
));
92 assert(sizeof(float) == sizeof(unsigned int));
94 memcpy(&u
, &f
, sizeof(f
));
99 * Evaluate one component of a floating-point 4x8 unpacking function.
102 (*pack_1x8_func_t
)(float);
105 * Evaluate one component of a floating-point 2x16 unpacking function.
108 (*pack_1x16_func_t
)(float);
111 * Evaluate one component of a floating-point 4x8 unpacking function.
114 (*unpack_1x8_func_t
)(uint8_t);
117 * Evaluate one component of a floating-point 2x16 unpacking function.
120 (*unpack_1x16_func_t
)(uint16_t);
123 * Evaluate a 2x16 floating-point packing function.
126 pack_2x16(pack_1x16_func_t pack_1x16
,
129 /* From section 8.4 of the GLSL ES 3.00 spec:
133 * The first component of the vector will be written to the least
134 * significant bits of the output; the last component will be written to
135 * the most significant bits.
137 * The specifications for the other packing functions contain similar
141 u
|= ((uint32_t) pack_1x16(x
) << 0);
142 u
|= ((uint32_t) pack_1x16(y
) << 16);
147 * Evaluate a 4x8 floating-point packing function.
150 pack_4x8(pack_1x8_func_t pack_1x8
,
151 float x
, float y
, float z
, float w
)
153 /* From section 8.4 of the GLSL 4.30 spec:
157 * The first component of the vector will be written to the least
158 * significant bits of the output; the last component will be written to
159 * the most significant bits.
161 * The specifications for the other packing functions contain similar
165 u
|= ((uint32_t) pack_1x8(x
) << 0);
166 u
|= ((uint32_t) pack_1x8(y
) << 8);
167 u
|= ((uint32_t) pack_1x8(z
) << 16);
168 u
|= ((uint32_t) pack_1x8(w
) << 24);
173 * Evaluate a 2x16 floating-point unpacking function.
176 unpack_2x16(unpack_1x16_func_t unpack_1x16
,
180 /* From section 8.4 of the GLSL ES 3.00 spec:
184 * The first component of the returned vector will be extracted from
185 * the least significant bits of the input; the last component will be
186 * extracted from the most significant bits.
188 * The specifications for the other unpacking functions contain similar
191 *x
= unpack_1x16((uint16_t) (u
& 0xffff));
192 *y
= unpack_1x16((uint16_t) (u
>> 16));
196 * Evaluate a 4x8 floating-point unpacking function.
199 unpack_4x8(unpack_1x8_func_t unpack_1x8
, uint32_t u
,
200 float *x
, float *y
, float *z
, float *w
)
202 /* From section 8.4 of the GLSL 4.30 spec:
206 * The first component of the returned vector will be extracted from
207 * the least significant bits of the input; the last component will be
208 * extracted from the most significant bits.
210 * The specifications for the other unpacking functions contain similar
213 *x
= unpack_1x8((uint8_t) (u
& 0xff));
214 *y
= unpack_1x8((uint8_t) (u
>> 8));
215 *z
= unpack_1x8((uint8_t) (u
>> 16));
216 *w
= unpack_1x8((uint8_t) (u
>> 24));
220 * Evaluate one component of packSnorm4x8.
223 pack_snorm_1x8(float x
)
225 /* From section 8.4 of the GLSL 4.30 spec:
229 * The conversion for component c of v to fixed point is done as
232 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
234 * We must first cast the float to an int, because casting a negative
235 * float to a uint is undefined.
237 return (uint8_t) (int8_t)
238 _mesa_round_to_even(CLAMP(x
, -1.0f
, +1.0f
) * 127.0f
);
242 * Evaluate one component of packSnorm2x16.
245 pack_snorm_1x16(float x
)
247 /* From section 8.4 of the GLSL ES 3.00 spec:
251 * The conversion for component c of v to fixed point is done as
254 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
256 * We must first cast the float to an int, because casting a negative
257 * float to a uint is undefined.
259 return (uint16_t) (int16_t)
260 _mesa_round_to_even(CLAMP(x
, -1.0f
, +1.0f
) * 32767.0f
);
264 * Evaluate one component of unpackSnorm4x8.
267 unpack_snorm_1x8(uint8_t u
)
269 /* From section 8.4 of the GLSL 4.30 spec:
273 * The conversion for unpacked fixed-point value f to floating point is
276 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
278 return CLAMP((int8_t) u
/ 127.0f
, -1.0f
, +1.0f
);
282 * Evaluate one component of unpackSnorm2x16.
285 unpack_snorm_1x16(uint16_t u
)
287 /* From section 8.4 of the GLSL ES 3.00 spec:
291 * The conversion for unpacked fixed-point value f to floating point is
294 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
296 return CLAMP((int16_t) u
/ 32767.0f
, -1.0f
, +1.0f
);
300 * Evaluate one component packUnorm4x8.
303 pack_unorm_1x8(float x
)
305 /* From section 8.4 of the GLSL 4.30 spec:
309 * The conversion for component c of v to fixed point is done as
312 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
314 return (uint8_t) _mesa_round_to_even(CLAMP(x
, 0.0f
, 1.0f
) * 255.0f
);
318 * Evaluate one component packUnorm2x16.
321 pack_unorm_1x16(float x
)
323 /* From section 8.4 of the GLSL ES 3.00 spec:
327 * The conversion for component c of v to fixed point is done as
330 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
332 return (uint16_t) _mesa_round_to_even(CLAMP(x
, 0.0f
, 1.0f
) * 65535.0f
);
336 * Evaluate one component of unpackUnorm4x8.
339 unpack_unorm_1x8(uint8_t u
)
341 /* From section 8.4 of the GLSL 4.30 spec:
345 * The conversion for unpacked fixed-point value f to floating point is
348 * unpackUnorm4x8: f / 255.0
350 return (float) u
/ 255.0f
;
354 * Evaluate one component of unpackUnorm2x16.
357 unpack_unorm_1x16(uint16_t u
)
359 /* From section 8.4 of the GLSL ES 3.00 spec:
363 * The conversion for unpacked fixed-point value f to floating point is
366 * unpackUnorm2x16: f / 65535.0
368 return (float) u
/ 65535.0f
;
372 * Evaluate one component of packHalf2x16.
375 pack_half_1x16(float x
)
377 return _mesa_float_to_half(x
);
381 * Evaluate one component of unpackHalf2x16.
384 unpack_half_1x16(uint16_t u
)
386 return _mesa_half_to_float(u
);
390 * Get the constant that is ultimately referenced by an r-value, in a constant
391 * expression evaluation context.
393 * The offset is used when the reference is to a specific column of a matrix.
396 constant_referenced(const ir_dereference
*deref
,
397 struct hash_table
*variable_context
,
398 ir_constant
*&store
, int &offset
)
403 if (variable_context
== NULL
)
406 switch (deref
->ir_type
) {
407 case ir_type_dereference_array
: {
408 const ir_dereference_array
*const da
=
409 (const ir_dereference_array
*) deref
;
411 ir_constant
*const index_c
=
412 da
->array_index
->constant_expression_value(variable_context
);
414 if (!index_c
|| !index_c
->type
->is_scalar() || !index_c
->type
->is_integer())
417 const int index
= index_c
->type
->base_type
== GLSL_TYPE_INT
?
418 index_c
->get_int_component(0) :
419 index_c
->get_uint_component(0);
421 ir_constant
*substore
;
424 const ir_dereference
*const deref
= da
->array
->as_dereference();
428 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
431 const glsl_type
*const vt
= da
->array
->type
;
432 if (vt
->is_array()) {
433 store
= substore
->get_array_element(index
);
435 } else if (vt
->is_matrix()) {
437 offset
= index
* vt
->vector_elements
;
438 } else if (vt
->is_vector()) {
440 offset
= suboffset
+ index
;
446 case ir_type_dereference_record
: {
447 const ir_dereference_record
*const dr
=
448 (const ir_dereference_record
*) deref
;
450 const ir_dereference
*const deref
= dr
->record
->as_dereference();
454 ir_constant
*substore
;
457 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
460 /* Since we're dropping it on the floor...
462 assert(suboffset
== 0);
464 store
= substore
->get_record_field(dr
->field
);
468 case ir_type_dereference_variable
: {
469 const ir_dereference_variable
*const dv
=
470 (const ir_dereference_variable
*) deref
;
472 store
= (ir_constant
*) hash_table_find(variable_context
, dv
->var
);
477 assert(!"Should not get here.");
481 return store
!= NULL
;
486 ir_rvalue::constant_expression_value(struct hash_table
*)
488 assert(this->type
->is_error());
493 ir_expression::constant_expression_value(struct hash_table
*variable_context
)
495 if (this->type
->is_error())
498 ir_constant
*op
[Elements(this->operands
)] = { NULL
, };
499 ir_constant_data data
;
501 memset(&data
, 0, sizeof(data
));
503 for (unsigned operand
= 0; operand
< this->get_num_operands(); operand
++) {
504 op
[operand
] = this->operands
[operand
]->constant_expression_value(variable_context
);
510 switch (this->operation
) {
511 case ir_binop_lshift
:
512 case ir_binop_rshift
:
514 case ir_binop_vector_extract
:
516 case ir_triop_bitfield_extract
:
520 assert(op
[0]->type
->base_type
== op
[1]->type
->base_type
);
524 bool op0_scalar
= op
[0]->type
->is_scalar();
525 bool op1_scalar
= op
[1] != NULL
&& op
[1]->type
->is_scalar();
527 /* When iterating over a vector or matrix's components, we want to increase
528 * the loop counter. However, for scalars, we want to stay at 0.
530 unsigned c0_inc
= op0_scalar
? 0 : 1;
531 unsigned c1_inc
= op1_scalar
? 0 : 1;
533 if (op1_scalar
|| !op
[1]) {
534 components
= op
[0]->type
->components();
536 components
= op
[1]->type
->components();
539 void *ctx
= ralloc_parent(this);
541 /* Handle array operations here, rather than below. */
542 if (op
[0]->type
->is_array()) {
543 assert(op
[1] != NULL
&& op
[1]->type
->is_array());
544 switch (this->operation
) {
545 case ir_binop_all_equal
:
546 return new(ctx
) ir_constant(op
[0]->has_value(op
[1]));
547 case ir_binop_any_nequal
:
548 return new(ctx
) ir_constant(!op
[0]->has_value(op
[1]));
555 switch (this->operation
) {
556 case ir_unop_bit_not
:
557 switch (op
[0]->type
->base_type
) {
559 for (unsigned c
= 0; c
< components
; c
++)
560 data
.i
[c
] = ~ op
[0]->value
.i
[c
];
563 for (unsigned c
= 0; c
< components
; c
++)
564 data
.u
[c
] = ~ op
[0]->value
.u
[c
];
571 case ir_unop_logic_not
:
572 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
573 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
574 data
.b
[c
] = !op
[0]->value
.b
[c
];
578 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
579 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
580 data
.i
[c
] = (int) op
[0]->value
.f
[c
];
584 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
585 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
586 data
.i
[c
] = (unsigned) op
[0]->value
.f
[c
];
590 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
591 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
592 data
.f
[c
] = (float) op
[0]->value
.i
[c
];
596 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
597 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
598 data
.f
[c
] = (float) op
[0]->value
.u
[c
];
602 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
603 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
604 data
.f
[c
] = op
[0]->value
.b
[c
] ? 1.0F
: 0.0F
;
608 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
609 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
610 data
.b
[c
] = op
[0]->value
.f
[c
] != 0.0F
? true : false;
614 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
615 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
616 data
.u
[c
] = op
[0]->value
.b
[c
] ? 1 : 0;
620 assert(op
[0]->type
->is_integer());
621 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
622 data
.b
[c
] = op
[0]->value
.u
[c
] ? true : false;
626 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
627 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
628 data
.i
[c
] = op
[0]->value
.u
[c
];
632 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
633 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
634 data
.u
[c
] = op
[0]->value
.i
[c
];
637 case ir_unop_bitcast_i2f
:
638 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
639 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
640 data
.f
[c
] = bitcast_u2f(op
[0]->value
.i
[c
]);
643 case ir_unop_bitcast_f2i
:
644 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
645 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
646 data
.i
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
649 case ir_unop_bitcast_u2f
:
650 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
651 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
652 data
.f
[c
] = bitcast_u2f(op
[0]->value
.u
[c
]);
655 case ir_unop_bitcast_f2u
:
656 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
657 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
658 data
.u
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
662 assert(op
[0]->type
->is_boolean());
664 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
665 if (op
[0]->value
.b
[c
])
671 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
672 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
673 data
.f
[c
] = truncf(op
[0]->value
.f
[c
]);
677 case ir_unop_round_even
:
678 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
679 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
680 data
.f
[c
] = _mesa_round_to_even(op
[0]->value
.f
[c
]);
685 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
686 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
687 data
.f
[c
] = ceilf(op
[0]->value
.f
[c
]);
692 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
693 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
694 data
.f
[c
] = floorf(op
[0]->value
.f
[c
]);
699 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
700 switch (this->type
->base_type
) {
707 case GLSL_TYPE_FLOAT
:
708 data
.f
[c
] = op
[0]->value
.f
[c
] - floor(op
[0]->value
.f
[c
]);
717 case ir_unop_sin_reduced
:
718 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
719 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
720 data
.f
[c
] = sinf(op
[0]->value
.f
[c
]);
725 case ir_unop_cos_reduced
:
726 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
727 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
728 data
.f
[c
] = cosf(op
[0]->value
.f
[c
]);
733 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
734 switch (this->type
->base_type
) {
736 data
.u
[c
] = -((int) op
[0]->value
.u
[c
]);
739 data
.i
[c
] = -op
[0]->value
.i
[c
];
741 case GLSL_TYPE_FLOAT
:
742 data
.f
[c
] = -op
[0]->value
.f
[c
];
751 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
752 switch (this->type
->base_type
) {
754 data
.u
[c
] = op
[0]->value
.u
[c
];
757 data
.i
[c
] = op
[0]->value
.i
[c
];
759 data
.i
[c
] = -data
.i
[c
];
761 case GLSL_TYPE_FLOAT
:
762 data
.f
[c
] = fabs(op
[0]->value
.f
[c
]);
771 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
772 switch (this->type
->base_type
) {
774 data
.u
[c
] = op
[0]->value
.i
[c
] > 0;
777 data
.i
[c
] = (op
[0]->value
.i
[c
] > 0) - (op
[0]->value
.i
[c
] < 0);
779 case GLSL_TYPE_FLOAT
:
780 data
.f
[c
] = float((op
[0]->value
.f
[c
] > 0)-(op
[0]->value
.f
[c
] < 0));
789 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
790 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
791 switch (this->type
->base_type
) {
793 if (op
[0]->value
.u
[c
] != 0.0)
794 data
.u
[c
] = 1 / op
[0]->value
.u
[c
];
797 if (op
[0]->value
.i
[c
] != 0.0)
798 data
.i
[c
] = 1 / op
[0]->value
.i
[c
];
800 case GLSL_TYPE_FLOAT
:
801 if (op
[0]->value
.f
[c
] != 0.0)
802 data
.f
[c
] = 1.0F
/ op
[0]->value
.f
[c
];
811 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
812 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
813 data
.f
[c
] = 1.0F
/ sqrtf(op
[0]->value
.f
[c
]);
818 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
819 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
820 data
.f
[c
] = sqrtf(op
[0]->value
.f
[c
]);
825 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
826 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
827 data
.f
[c
] = expf(op
[0]->value
.f
[c
]);
832 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
833 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
834 data
.f
[c
] = exp2f(op
[0]->value
.f
[c
]);
839 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
840 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
841 data
.f
[c
] = logf(op
[0]->value
.f
[c
]);
846 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
847 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
848 data
.f
[c
] = log2f(op
[0]->value
.f
[c
]);
854 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
855 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
860 case ir_unop_pack_snorm_2x16
:
861 assert(op
[0]->type
== glsl_type::vec2_type
);
862 data
.u
[0] = pack_2x16(pack_snorm_1x16
,
866 case ir_unop_pack_snorm_4x8
:
867 assert(op
[0]->type
== glsl_type::vec4_type
);
868 data
.u
[0] = pack_4x8(pack_snorm_1x8
,
874 case ir_unop_unpack_snorm_2x16
:
875 assert(op
[0]->type
== glsl_type::uint_type
);
876 unpack_2x16(unpack_snorm_1x16
,
878 &data
.f
[0], &data
.f
[1]);
880 case ir_unop_unpack_snorm_4x8
:
881 assert(op
[0]->type
== glsl_type::uint_type
);
882 unpack_4x8(unpack_snorm_1x8
,
884 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
886 case ir_unop_pack_unorm_2x16
:
887 assert(op
[0]->type
== glsl_type::vec2_type
);
888 data
.u
[0] = pack_2x16(pack_unorm_1x16
,
892 case ir_unop_pack_unorm_4x8
:
893 assert(op
[0]->type
== glsl_type::vec4_type
);
894 data
.u
[0] = pack_4x8(pack_unorm_1x8
,
900 case ir_unop_unpack_unorm_2x16
:
901 assert(op
[0]->type
== glsl_type::uint_type
);
902 unpack_2x16(unpack_unorm_1x16
,
904 &data
.f
[0], &data
.f
[1]);
906 case ir_unop_unpack_unorm_4x8
:
907 assert(op
[0]->type
== glsl_type::uint_type
);
908 unpack_4x8(unpack_unorm_1x8
,
910 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
912 case ir_unop_pack_half_2x16
:
913 assert(op
[0]->type
== glsl_type::vec2_type
);
914 data
.u
[0] = pack_2x16(pack_half_1x16
,
918 case ir_unop_unpack_half_2x16
:
919 assert(op
[0]->type
== glsl_type::uint_type
);
920 unpack_2x16(unpack_half_1x16
,
922 &data
.f
[0], &data
.f
[1]);
925 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
926 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
927 data
.f
[c
] = powf(op
[0]->value
.f
[c
], op
[1]->value
.f
[c
]);
932 data
.f
[0] = dot(op
[0], op
[1]);
936 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
937 for (unsigned c
= 0, c0
= 0, c1
= 0;
939 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
941 switch (op
[0]->type
->base_type
) {
943 data
.u
[c
] = MIN2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
946 data
.i
[c
] = MIN2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
948 case GLSL_TYPE_FLOAT
:
949 data
.f
[c
] = MIN2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
958 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
959 for (unsigned c
= 0, c0
= 0, c1
= 0;
961 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
963 switch (op
[0]->type
->base_type
) {
965 data
.u
[c
] = MAX2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
968 data
.i
[c
] = MAX2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
970 case GLSL_TYPE_FLOAT
:
971 data
.f
[c
] = MAX2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
980 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
981 for (unsigned c
= 0, c0
= 0, c1
= 0;
983 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
985 switch (op
[0]->type
->base_type
) {
987 data
.u
[c
] = op
[0]->value
.u
[c0
] + op
[1]->value
.u
[c1
];
990 data
.i
[c
] = op
[0]->value
.i
[c0
] + op
[1]->value
.i
[c1
];
992 case GLSL_TYPE_FLOAT
:
993 data
.f
[c
] = op
[0]->value
.f
[c0
] + op
[1]->value
.f
[c1
];
1002 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1003 for (unsigned c
= 0, c0
= 0, c1
= 0;
1005 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1007 switch (op
[0]->type
->base_type
) {
1008 case GLSL_TYPE_UINT
:
1009 data
.u
[c
] = op
[0]->value
.u
[c0
] - op
[1]->value
.u
[c1
];
1012 data
.i
[c
] = op
[0]->value
.i
[c0
] - op
[1]->value
.i
[c1
];
1014 case GLSL_TYPE_FLOAT
:
1015 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
];
1024 /* Check for equal types, or unequal types involving scalars */
1025 if ((op
[0]->type
== op
[1]->type
&& !op
[0]->type
->is_matrix())
1026 || op0_scalar
|| op1_scalar
) {
1027 for (unsigned c
= 0, c0
= 0, c1
= 0;
1029 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1031 switch (op
[0]->type
->base_type
) {
1032 case GLSL_TYPE_UINT
:
1033 data
.u
[c
] = op
[0]->value
.u
[c0
] * op
[1]->value
.u
[c1
];
1036 data
.i
[c
] = op
[0]->value
.i
[c0
] * op
[1]->value
.i
[c1
];
1038 case GLSL_TYPE_FLOAT
:
1039 data
.f
[c
] = op
[0]->value
.f
[c0
] * op
[1]->value
.f
[c1
];
1046 assert(op
[0]->type
->is_matrix() || op
[1]->type
->is_matrix());
1048 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
1049 * matrix can be a GLSL vector, either N or P can be 1.
1051 * For vec*mat, the vector is treated as a row vector. This
1052 * means the vector is a 1-row x M-column matrix.
1054 * For mat*vec, the vector is treated as a column vector. Since
1055 * matrix_columns is 1 for vectors, this just works.
1057 const unsigned n
= op
[0]->type
->is_vector()
1058 ? 1 : op
[0]->type
->vector_elements
;
1059 const unsigned m
= op
[1]->type
->vector_elements
;
1060 const unsigned p
= op
[1]->type
->matrix_columns
;
1061 for (unsigned j
= 0; j
< p
; j
++) {
1062 for (unsigned i
= 0; i
< n
; i
++) {
1063 for (unsigned k
= 0; k
< m
; k
++) {
1064 data
.f
[i
+n
*j
] += op
[0]->value
.f
[i
+n
*k
]*op
[1]->value
.f
[k
+m
*j
];
1072 /* FINISHME: Emit warning when division-by-zero is detected. */
1073 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1074 for (unsigned c
= 0, c0
= 0, c1
= 0;
1076 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1078 switch (op
[0]->type
->base_type
) {
1079 case GLSL_TYPE_UINT
:
1080 if (op
[1]->value
.u
[c1
] == 0) {
1083 data
.u
[c
] = op
[0]->value
.u
[c0
] / op
[1]->value
.u
[c1
];
1087 if (op
[1]->value
.i
[c1
] == 0) {
1090 data
.i
[c
] = op
[0]->value
.i
[c0
] / op
[1]->value
.i
[c1
];
1093 case GLSL_TYPE_FLOAT
:
1094 data
.f
[c
] = op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
];
1103 /* FINISHME: Emit warning when division-by-zero is detected. */
1104 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1105 for (unsigned c
= 0, c0
= 0, c1
= 0;
1107 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1109 switch (op
[0]->type
->base_type
) {
1110 case GLSL_TYPE_UINT
:
1111 if (op
[1]->value
.u
[c1
] == 0) {
1114 data
.u
[c
] = op
[0]->value
.u
[c0
] % op
[1]->value
.u
[c1
];
1118 if (op
[1]->value
.i
[c1
] == 0) {
1121 data
.i
[c
] = op
[0]->value
.i
[c0
] % op
[1]->value
.i
[c1
];
1124 case GLSL_TYPE_FLOAT
:
1125 /* We don't use fmod because it rounds toward zero; GLSL specifies
1128 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
]
1129 * floorf(op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
]);
1138 case ir_binop_logic_and
:
1139 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1140 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1141 data
.b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
1143 case ir_binop_logic_xor
:
1144 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1145 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1146 data
.b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
1148 case ir_binop_logic_or
:
1149 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1150 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1151 data
.b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
1155 assert(op
[0]->type
== op
[1]->type
);
1156 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1157 switch (op
[0]->type
->base_type
) {
1158 case GLSL_TYPE_UINT
:
1159 data
.b
[c
] = op
[0]->value
.u
[c
] < op
[1]->value
.u
[c
];
1162 data
.b
[c
] = op
[0]->value
.i
[c
] < op
[1]->value
.i
[c
];
1164 case GLSL_TYPE_FLOAT
:
1165 data
.b
[c
] = op
[0]->value
.f
[c
] < op
[1]->value
.f
[c
];
1172 case ir_binop_greater
:
1173 assert(op
[0]->type
== op
[1]->type
);
1174 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1175 switch (op
[0]->type
->base_type
) {
1176 case GLSL_TYPE_UINT
:
1177 data
.b
[c
] = op
[0]->value
.u
[c
] > op
[1]->value
.u
[c
];
1180 data
.b
[c
] = op
[0]->value
.i
[c
] > op
[1]->value
.i
[c
];
1182 case GLSL_TYPE_FLOAT
:
1183 data
.b
[c
] = op
[0]->value
.f
[c
] > op
[1]->value
.f
[c
];
1190 case ir_binop_lequal
:
1191 assert(op
[0]->type
== op
[1]->type
);
1192 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1193 switch (op
[0]->type
->base_type
) {
1194 case GLSL_TYPE_UINT
:
1195 data
.b
[c
] = op
[0]->value
.u
[c
] <= op
[1]->value
.u
[c
];
1198 data
.b
[c
] = op
[0]->value
.i
[c
] <= op
[1]->value
.i
[c
];
1200 case GLSL_TYPE_FLOAT
:
1201 data
.b
[c
] = op
[0]->value
.f
[c
] <= op
[1]->value
.f
[c
];
1208 case ir_binop_gequal
:
1209 assert(op
[0]->type
== op
[1]->type
);
1210 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1211 switch (op
[0]->type
->base_type
) {
1212 case GLSL_TYPE_UINT
:
1213 data
.b
[c
] = op
[0]->value
.u
[c
] >= op
[1]->value
.u
[c
];
1216 data
.b
[c
] = op
[0]->value
.i
[c
] >= op
[1]->value
.i
[c
];
1218 case GLSL_TYPE_FLOAT
:
1219 data
.b
[c
] = op
[0]->value
.f
[c
] >= op
[1]->value
.f
[c
];
1226 case ir_binop_equal
:
1227 assert(op
[0]->type
== op
[1]->type
);
1228 for (unsigned c
= 0; c
< components
; c
++) {
1229 switch (op
[0]->type
->base_type
) {
1230 case GLSL_TYPE_UINT
:
1231 data
.b
[c
] = op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
1234 data
.b
[c
] = op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
1236 case GLSL_TYPE_FLOAT
:
1237 data
.b
[c
] = op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
1239 case GLSL_TYPE_BOOL
:
1240 data
.b
[c
] = op
[0]->value
.b
[c
] == op
[1]->value
.b
[c
];
1247 case ir_binop_nequal
:
1248 assert(op
[0]->type
== op
[1]->type
);
1249 for (unsigned c
= 0; c
< components
; c
++) {
1250 switch (op
[0]->type
->base_type
) {
1251 case GLSL_TYPE_UINT
:
1252 data
.b
[c
] = op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
1255 data
.b
[c
] = op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
1257 case GLSL_TYPE_FLOAT
:
1258 data
.b
[c
] = op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
1260 case GLSL_TYPE_BOOL
:
1261 data
.b
[c
] = op
[0]->value
.b
[c
] != op
[1]->value
.b
[c
];
1268 case ir_binop_all_equal
:
1269 data
.b
[0] = op
[0]->has_value(op
[1]);
1271 case ir_binop_any_nequal
:
1272 data
.b
[0] = !op
[0]->has_value(op
[1]);
1275 case ir_binop_lshift
:
1276 for (unsigned c
= 0, c0
= 0, c1
= 0;
1278 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1280 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1281 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1282 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.i
[c1
];
1284 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1285 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1286 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.u
[c1
];
1288 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1289 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1290 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.i
[c1
];
1292 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1293 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1294 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.u
[c1
];
1299 case ir_binop_rshift
:
1300 for (unsigned c
= 0, c0
= 0, c1
= 0;
1302 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1304 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1305 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1306 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.i
[c1
];
1308 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1309 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1310 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.u
[c1
];
1312 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1313 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1314 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.i
[c1
];
1316 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1317 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1318 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.u
[c1
];
1323 case ir_binop_bit_and
:
1324 for (unsigned c
= 0, c0
= 0, c1
= 0;
1326 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1328 switch (op
[0]->type
->base_type
) {
1330 data
.i
[c
] = op
[0]->value
.i
[c0
] & op
[1]->value
.i
[c1
];
1332 case GLSL_TYPE_UINT
:
1333 data
.u
[c
] = op
[0]->value
.u
[c0
] & op
[1]->value
.u
[c1
];
1341 case ir_binop_bit_or
:
1342 for (unsigned c
= 0, c0
= 0, c1
= 0;
1344 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1346 switch (op
[0]->type
->base_type
) {
1348 data
.i
[c
] = op
[0]->value
.i
[c0
] | op
[1]->value
.i
[c1
];
1350 case GLSL_TYPE_UINT
:
1351 data
.u
[c
] = op
[0]->value
.u
[c0
] | op
[1]->value
.u
[c1
];
1359 case ir_binop_vector_extract
: {
1360 const int c
= CLAMP(op
[1]->value
.i
[0], 0,
1361 (int) op
[0]->type
->vector_elements
- 1);
1363 switch (op
[0]->type
->base_type
) {
1364 case GLSL_TYPE_UINT
:
1365 data
.u
[0] = op
[0]->value
.u
[c
];
1368 data
.i
[0] = op
[0]->value
.i
[c
];
1370 case GLSL_TYPE_FLOAT
:
1371 data
.f
[0] = op
[0]->value
.f
[c
];
1373 case GLSL_TYPE_BOOL
:
1374 data
.b
[0] = op
[0]->value
.b
[c
];
1382 case ir_binop_bit_xor
:
1383 for (unsigned c
= 0, c0
= 0, c1
= 0;
1385 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1387 switch (op
[0]->type
->base_type
) {
1389 data
.i
[c
] = op
[0]->value
.i
[c0
] ^ op
[1]->value
.i
[c1
];
1391 case GLSL_TYPE_UINT
:
1392 data
.u
[c
] = op
[0]->value
.u
[c0
] ^ op
[1]->value
.u
[c1
];
1400 case ir_unop_bitfield_reverse
:
1401 /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
1402 for (unsigned c
= 0; c
< components
; c
++) {
1403 unsigned int v
= op
[0]->value
.u
[c
]; // input bits to be reversed
1404 unsigned int r
= v
; // r will be reversed bits of v; first get LSB of v
1405 int s
= sizeof(v
) * CHAR_BIT
- 1; // extra shift needed at end
1407 for (v
>>= 1; v
; v
>>= 1) {
1412 r
<<= s
; // shift when v's highest bits are zero
1418 case ir_unop_bit_count
:
1419 for (unsigned c
= 0; c
< components
; c
++) {
1421 unsigned v
= op
[0]->value
.u
[c
];
1423 for (; v
; count
++) {
1430 case ir_unop_find_msb
:
1431 for (unsigned c
= 0; c
< components
; c
++) {
1432 int v
= op
[0]->value
.i
[c
];
1434 if (v
== 0 || (op
[0]->type
->base_type
== GLSL_TYPE_INT
&& v
== -1))
1438 int top_bit
= op
[0]->type
->base_type
== GLSL_TYPE_UINT
1439 ? 0 : v
& (1 << 31);
1441 while (((v
& (1 << 31)) == top_bit
) && count
!= 32) {
1446 data
.i
[c
] = 31 - count
;
1451 case ir_unop_find_lsb
:
1452 for (unsigned c
= 0; c
< components
; c
++) {
1453 if (op
[0]->value
.i
[c
] == 0)
1457 unsigned v
= op
[0]->value
.u
[c
];
1459 for (; !(v
& 1); v
>>= 1) {
1467 case ir_triop_bitfield_extract
: {
1468 int offset
= op
[1]->value
.i
[0];
1469 int bits
= op
[2]->value
.i
[0];
1471 for (unsigned c
= 0; c
< components
; c
++) {
1474 else if (offset
< 0 || bits
< 0)
1475 data
.u
[c
] = 0; /* Undefined, per spec. */
1476 else if (offset
+ bits
> 32)
1477 data
.u
[c
] = 0; /* Undefined, per spec. */
1479 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
) {
1480 /* int so that the right shift will sign-extend. */
1481 int value
= op
[0]->value
.i
[c
];
1482 value
<<= 32 - bits
- offset
;
1483 value
>>= 32 - bits
;
1486 unsigned value
= op
[0]->value
.u
[c
];
1487 value
<<= 32 - bits
- offset
;
1488 value
>>= 32 - bits
;
1496 case ir_binop_bfm
: {
1497 int bits
= op
[0]->value
.i
[0];
1498 int offset
= op
[1]->value
.i
[0];
1500 for (unsigned c
= 0; c
< components
; c
++) {
1502 data
.u
[c
] = op
[0]->value
.u
[c
];
1503 else if (offset
< 0 || bits
< 0)
1504 data
.u
[c
] = 0; /* Undefined for bitfieldInsert, per spec. */
1505 else if (offset
+ bits
> 32)
1506 data
.u
[c
] = 0; /* Undefined for bitfieldInsert, per spec. */
1508 data
.u
[c
] = ((1 << bits
) - 1) << offset
;
1513 case ir_binop_ldexp
:
1514 for (unsigned c
= 0; c
< components
; c
++) {
1515 data
.f
[c
] = ldexp(op
[0]->value
.f
[c
], op
[1]->value
.i
[c
]);
1516 /* Flush subnormal values to zero. */
1517 if (!isnormal(data
.f
[c
]))
1518 data
.f
[c
] = copysign(0.0f
, op
[0]->value
.f
[c
]);
1523 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1524 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
);
1525 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
);
1527 for (unsigned c
= 0; c
< components
; c
++) {
1528 data
.f
[c
] = op
[0]->value
.f
[c
] * op
[1]->value
.f
[c
]
1529 + op
[2]->value
.f
[c
];
1533 case ir_triop_lrp
: {
1534 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1535 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
);
1536 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
);
1538 unsigned c2_inc
= op
[2]->type
->is_scalar() ? 0 : 1;
1539 for (unsigned c
= 0, c2
= 0; c
< components
; c2
+= c2_inc
, c
++) {
1540 data
.f
[c
] = op
[0]->value
.f
[c
] * (1.0f
- op
[2]->value
.f
[c2
]) +
1541 (op
[1]->value
.f
[c
] * op
[2]->value
.f
[c2
]);
1547 for (unsigned c
= 0; c
< components
; c
++) {
1548 data
.u
[c
] = op
[0]->value
.b
[c
] ? op
[1]->value
.u
[c
]
1549 : op
[2]->value
.u
[c
];
1553 case ir_triop_vector_insert
: {
1554 const unsigned idx
= op
[2]->value
.u
[0];
1556 memcpy(&data
, &op
[0]->value
, sizeof(data
));
1558 switch (this->type
->base_type
) {
1560 data
.i
[idx
] = op
[1]->value
.i
[0];
1562 case GLSL_TYPE_UINT
:
1563 data
.u
[idx
] = op
[1]->value
.u
[0];
1565 case GLSL_TYPE_FLOAT
:
1566 data
.f
[idx
] = op
[1]->value
.f
[0];
1568 case GLSL_TYPE_BOOL
:
1569 data
.b
[idx
] = op
[1]->value
.b
[0];
1572 assert(!"Should not get here.");
1578 case ir_quadop_bitfield_insert
: {
1579 int offset
= op
[2]->value
.i
[0];
1580 int bits
= op
[3]->value
.i
[0];
1582 for (unsigned c
= 0; c
< components
; c
++) {
1584 data
.u
[c
] = op
[0]->value
.u
[c
];
1585 else if (offset
< 0 || bits
< 0)
1586 data
.u
[c
] = 0; /* Undefined, per spec. */
1587 else if (offset
+ bits
> 32)
1588 data
.u
[c
] = 0; /* Undefined, per spec. */
1590 unsigned insert_mask
= ((1 << bits
) - 1) << offset
;
1592 unsigned insert
= op
[1]->value
.u
[c
];
1594 insert
&= insert_mask
;
1596 unsigned base
= op
[0]->value
.u
[c
];
1597 base
&= ~insert_mask
;
1599 data
.u
[c
] = base
| insert
;
1605 case ir_quadop_vector
:
1606 for (unsigned c
= 0; c
< this->type
->vector_elements
; c
++) {
1607 switch (this->type
->base_type
) {
1609 data
.i
[c
] = op
[c
]->value
.i
[0];
1611 case GLSL_TYPE_UINT
:
1612 data
.u
[c
] = op
[c
]->value
.u
[0];
1614 case GLSL_TYPE_FLOAT
:
1615 data
.f
[c
] = op
[c
]->value
.f
[0];
1624 /* FINISHME: Should handle all expression types. */
1628 return new(ctx
) ir_constant(this->type
, &data
);
1633 ir_texture::constant_expression_value(struct hash_table
*)
1635 /* texture lookups aren't constant expressions */
1641 ir_swizzle::constant_expression_value(struct hash_table
*variable_context
)
1643 ir_constant
*v
= this->val
->constant_expression_value(variable_context
);
1646 ir_constant_data data
= { { 0 } };
1648 const unsigned swiz_idx
[4] = {
1649 this->mask
.x
, this->mask
.y
, this->mask
.z
, this->mask
.w
1652 for (unsigned i
= 0; i
< this->mask
.num_components
; i
++) {
1653 switch (v
->type
->base_type
) {
1654 case GLSL_TYPE_UINT
:
1655 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
1656 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
1657 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
1658 default: assert(!"Should not get here."); break;
1662 void *ctx
= ralloc_parent(this);
1663 return new(ctx
) ir_constant(this->type
, &data
);
1670 ir_dereference_variable::constant_expression_value(struct hash_table
*variable_context
)
1672 /* This may occur during compile and var->type is glsl_type::error_type */
1676 /* Give priority to the context hashtable, if it exists */
1677 if (variable_context
) {
1678 ir_constant
*value
= (ir_constant
*)hash_table_find(variable_context
, var
);
1683 /* The constant_value of a uniform variable is its initializer,
1684 * not the lifetime constant value of the uniform.
1686 if (var
->data
.mode
== ir_var_uniform
)
1689 if (!var
->constant_value
)
1692 return var
->constant_value
->clone(ralloc_parent(var
), NULL
);
1697 ir_dereference_array::constant_expression_value(struct hash_table
*variable_context
)
1699 ir_constant
*array
= this->array
->constant_expression_value(variable_context
);
1700 ir_constant
*idx
= this->array_index
->constant_expression_value(variable_context
);
1702 if ((array
!= NULL
) && (idx
!= NULL
)) {
1703 void *ctx
= ralloc_parent(this);
1704 if (array
->type
->is_matrix()) {
1705 /* Array access of a matrix results in a vector.
1707 const unsigned column
= idx
->value
.u
[0];
1709 const glsl_type
*const column_type
= array
->type
->column_type();
1711 /* Offset in the constant matrix to the first element of the column
1714 const unsigned mat_idx
= column
* column_type
->vector_elements
;
1716 ir_constant_data data
= { { 0 } };
1718 switch (column_type
->base_type
) {
1719 case GLSL_TYPE_UINT
:
1721 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1722 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
1726 case GLSL_TYPE_FLOAT
:
1727 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1728 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
1733 assert(!"Should not get here.");
1737 return new(ctx
) ir_constant(column_type
, &data
);
1738 } else if (array
->type
->is_vector()) {
1739 const unsigned component
= idx
->value
.u
[0];
1741 return new(ctx
) ir_constant(array
, component
);
1743 const unsigned index
= idx
->value
.u
[0];
1744 return array
->get_array_element(index
)->clone(ctx
, NULL
);
1752 ir_dereference_record::constant_expression_value(struct hash_table
*)
1754 ir_constant
*v
= this->record
->constant_expression_value();
1756 return (v
!= NULL
) ? v
->get_record_field(this->field
) : NULL
;
1761 ir_assignment::constant_expression_value(struct hash_table
*)
1763 /* FINISHME: Handle CEs involving assignment (return RHS) */
1769 ir_constant::constant_expression_value(struct hash_table
*)
1776 ir_call::constant_expression_value(struct hash_table
*variable_context
)
1778 return this->callee
->constant_expression_value(&this->actual_parameters
, variable_context
);
1782 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list
&body
,
1783 struct hash_table
*variable_context
,
1784 ir_constant
**result
)
1786 foreach_list(n
, &body
) {
1787 ir_instruction
*inst
= (ir_instruction
*)n
;
1788 switch(inst
->ir_type
) {
1790 /* (declare () type symbol) */
1791 case ir_type_variable
: {
1792 ir_variable
*var
= inst
->as_variable();
1793 hash_table_insert(variable_context
, ir_constant::zero(this, var
->type
), var
);
1797 /* (assign [condition] (write-mask) (ref) (value)) */
1798 case ir_type_assignment
: {
1799 ir_assignment
*asg
= inst
->as_assignment();
1800 if (asg
->condition
) {
1801 ir_constant
*cond
= asg
->condition
->constant_expression_value(variable_context
);
1804 if (!cond
->get_bool_component(0))
1808 ir_constant
*store
= NULL
;
1811 if (!constant_referenced(asg
->lhs
, variable_context
, store
, offset
))
1814 ir_constant
*value
= asg
->rhs
->constant_expression_value(variable_context
);
1819 store
->copy_masked_offset(value
, offset
, asg
->write_mask
);
1823 /* (return (expression)) */
1824 case ir_type_return
:
1826 *result
= inst
->as_return()->value
->constant_expression_value(variable_context
);
1827 return *result
!= NULL
;
1829 /* (call name (ref) (params))*/
1830 case ir_type_call
: {
1831 ir_call
*call
= inst
->as_call();
1833 /* Just say no to void functions in constant expressions. We
1834 * don't need them at that point.
1837 if (!call
->return_deref
)
1840 ir_constant
*store
= NULL
;
1843 if (!constant_referenced(call
->return_deref
, variable_context
,
1847 ir_constant
*value
= call
->constant_expression_value(variable_context
);
1852 store
->copy_offset(value
, offset
);
1856 /* (if condition (then-instructions) (else-instructions)) */
1858 ir_if
*iif
= inst
->as_if();
1860 ir_constant
*cond
= iif
->condition
->constant_expression_value(variable_context
);
1861 if (!cond
|| !cond
->type
->is_boolean())
1864 exec_list
&branch
= cond
->get_bool_component(0) ? iif
->then_instructions
: iif
->else_instructions
;
1867 if (!constant_expression_evaluate_expression_list(branch
, variable_context
, result
))
1870 /* If there was a return in the branch chosen, drop out now. */
1877 /* Every other expression type, we drop out. */
1883 /* Reaching the end of the block is not an error condition */
1891 ir_function_signature::constant_expression_value(exec_list
*actual_parameters
, struct hash_table
*variable_context
)
1893 const glsl_type
*type
= this->return_type
;
1894 if (type
== glsl_type::void_type
)
1897 /* From the GLSL 1.20 spec, page 23:
1898 * "Function calls to user-defined functions (non-built-in functions)
1899 * cannot be used to form constant expressions."
1901 if (!this->is_builtin())
1905 * Of the builtin functions, only the texture lookups and the noise
1906 * ones must not be used in constant expressions. They all include
1907 * specific opcodes so they don't need to be special-cased at this
1911 /* Initialize the table of dereferencable names with the function
1912 * parameters. Verify their const-ness on the way.
1914 * We expect the correctness of the number of parameters to have
1915 * been checked earlier.
1917 hash_table
*deref_hash
= hash_table_ctor(8, hash_table_pointer_hash
,
1918 hash_table_pointer_compare
);
1920 /* If "origin" is non-NULL, then the function body is there. So we
1921 * have to use the variable objects from the object with the body,
1922 * but the parameter instanciation on the current object.
1924 const exec_node
*parameter_info
= origin
? origin
->parameters
.head
: parameters
.head
;
1926 foreach_list(n
, actual_parameters
) {
1927 ir_constant
*constant
= ((ir_rvalue
*) n
)->constant_expression_value(variable_context
);
1928 if (constant
== NULL
) {
1929 hash_table_dtor(deref_hash
);
1934 ir_variable
*var
= (ir_variable
*)parameter_info
;
1935 hash_table_insert(deref_hash
, constant
, var
);
1937 parameter_info
= parameter_info
->next
;
1940 ir_constant
*result
= NULL
;
1942 /* Now run the builtin function until something non-constant
1943 * happens or we get the result.
1945 if (constant_expression_evaluate_expression_list(origin
? origin
->body
: body
, deref_hash
, &result
) && result
)
1946 result
= result
->clone(ralloc_parent(this), NULL
);
1948 hash_table_dtor(deref_hash
);