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 "glsl_types.h"
40 #include "program/hash_table.h"
42 #if defined(_MSC_VER) && (_MSC_VER < 1800)
43 static int isnormal(double x
)
45 return _fpclass(x
) == _FPCLASS_NN
|| _fpclass(x
) == _FPCLASS_PN
;
47 #elif defined(__SUNPRO_CC)
49 static int isnormal(double x
)
51 return fpclass(x
) == FP_NORMAL
;
56 static double copysign(double x
, double y
)
58 return _copysign(x
, y
);
63 dot(ir_constant
*op0
, ir_constant
*op1
)
65 assert(op0
->type
->is_float() && op1
->type
->is_float());
68 for (unsigned c
= 0; c
< op0
->type
->components(); c
++)
69 result
+= op0
->value
.f
[c
] * op1
->value
.f
[c
];
74 /* This method is the only one supported by gcc. Unions in particular
75 * are iffy, and read-through-converted-pointer is killed by strict
76 * aliasing. OTOH, the compiler sees through the memcpy, so the
77 * resulting asm is reasonable.
80 bitcast_u2f(unsigned int u
)
82 assert(sizeof(float) == sizeof(unsigned int));
84 memcpy(&f
, &u
, sizeof(f
));
91 assert(sizeof(float) == sizeof(unsigned int));
93 memcpy(&u
, &f
, sizeof(f
));
98 * Evaluate one component of a floating-point 4x8 unpacking function.
101 (*pack_1x8_func_t
)(float);
104 * Evaluate one component of a floating-point 2x16 unpacking function.
107 (*pack_1x16_func_t
)(float);
110 * Evaluate one component of a floating-point 4x8 unpacking function.
113 (*unpack_1x8_func_t
)(uint8_t);
116 * Evaluate one component of a floating-point 2x16 unpacking function.
119 (*unpack_1x16_func_t
)(uint16_t);
122 * Evaluate a 2x16 floating-point packing function.
125 pack_2x16(pack_1x16_func_t pack_1x16
,
128 /* From section 8.4 of the GLSL ES 3.00 spec:
132 * The first component of the vector will be written to the least
133 * significant bits of the output; the last component will be written to
134 * the most significant bits.
136 * The specifications for the other packing functions contain similar
140 u
|= ((uint32_t) pack_1x16(x
) << 0);
141 u
|= ((uint32_t) pack_1x16(y
) << 16);
146 * Evaluate a 4x8 floating-point packing function.
149 pack_4x8(pack_1x8_func_t pack_1x8
,
150 float x
, float y
, float z
, float w
)
152 /* From section 8.4 of the GLSL 4.30 spec:
156 * The first component of the vector will be written to the least
157 * significant bits of the output; the last component will be written to
158 * the most significant bits.
160 * The specifications for the other packing functions contain similar
164 u
|= ((uint32_t) pack_1x8(x
) << 0);
165 u
|= ((uint32_t) pack_1x8(y
) << 8);
166 u
|= ((uint32_t) pack_1x8(z
) << 16);
167 u
|= ((uint32_t) pack_1x8(w
) << 24);
172 * Evaluate a 2x16 floating-point unpacking function.
175 unpack_2x16(unpack_1x16_func_t unpack_1x16
,
179 /* From section 8.4 of the GLSL ES 3.00 spec:
183 * The first component of the returned vector will be extracted from
184 * the least significant bits of the input; the last component will be
185 * extracted from the most significant bits.
187 * The specifications for the other unpacking functions contain similar
190 *x
= unpack_1x16((uint16_t) (u
& 0xffff));
191 *y
= unpack_1x16((uint16_t) (u
>> 16));
195 * Evaluate a 4x8 floating-point unpacking function.
198 unpack_4x8(unpack_1x8_func_t unpack_1x8
, uint32_t u
,
199 float *x
, float *y
, float *z
, float *w
)
201 /* From section 8.4 of the GLSL 4.30 spec:
205 * The first component of the returned vector will be extracted from
206 * the least significant bits of the input; the last component will be
207 * extracted from the most significant bits.
209 * The specifications for the other unpacking functions contain similar
212 *x
= unpack_1x8((uint8_t) (u
& 0xff));
213 *y
= unpack_1x8((uint8_t) (u
>> 8));
214 *z
= unpack_1x8((uint8_t) (u
>> 16));
215 *w
= unpack_1x8((uint8_t) (u
>> 24));
219 * Evaluate one component of packSnorm4x8.
222 pack_snorm_1x8(float x
)
224 /* From section 8.4 of the GLSL 4.30 spec:
228 * The conversion for component c of v to fixed point is done as
231 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
233 * We must first cast the float to an int, because casting a negative
234 * float to a uint is undefined.
236 return (uint8_t) (int8_t)
237 _mesa_round_to_even(CLAMP(x
, -1.0f
, +1.0f
) * 127.0f
);
241 * Evaluate one component of packSnorm2x16.
244 pack_snorm_1x16(float x
)
246 /* From section 8.4 of the GLSL ES 3.00 spec:
250 * The conversion for component c of v to fixed point is done as
253 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
255 * We must first cast the float to an int, because casting a negative
256 * float to a uint is undefined.
258 return (uint16_t) (int16_t)
259 _mesa_round_to_even(CLAMP(x
, -1.0f
, +1.0f
) * 32767.0f
);
263 * Evaluate one component of unpackSnorm4x8.
266 unpack_snorm_1x8(uint8_t u
)
268 /* From section 8.4 of the GLSL 4.30 spec:
272 * The conversion for unpacked fixed-point value f to floating point is
275 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
277 return CLAMP((int8_t) u
/ 127.0f
, -1.0f
, +1.0f
);
281 * Evaluate one component of unpackSnorm2x16.
284 unpack_snorm_1x16(uint16_t u
)
286 /* From section 8.4 of the GLSL ES 3.00 spec:
290 * The conversion for unpacked fixed-point value f to floating point is
293 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
295 return CLAMP((int16_t) u
/ 32767.0f
, -1.0f
, +1.0f
);
299 * Evaluate one component packUnorm4x8.
302 pack_unorm_1x8(float x
)
304 /* From section 8.4 of the GLSL 4.30 spec:
308 * The conversion for component c of v to fixed point is done as
311 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
313 return (uint8_t) _mesa_round_to_even(CLAMP(x
, 0.0f
, 1.0f
) * 255.0f
);
317 * Evaluate one component packUnorm2x16.
320 pack_unorm_1x16(float x
)
322 /* From section 8.4 of the GLSL ES 3.00 spec:
326 * The conversion for component c of v to fixed point is done as
329 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
331 return (uint16_t) _mesa_round_to_even(CLAMP(x
, 0.0f
, 1.0f
) * 65535.0f
);
335 * Evaluate one component of unpackUnorm4x8.
338 unpack_unorm_1x8(uint8_t u
)
340 /* From section 8.4 of the GLSL 4.30 spec:
344 * The conversion for unpacked fixed-point value f to floating point is
347 * unpackUnorm4x8: f / 255.0
349 return (float) u
/ 255.0f
;
353 * Evaluate one component of unpackUnorm2x16.
356 unpack_unorm_1x16(uint16_t u
)
358 /* From section 8.4 of the GLSL ES 3.00 spec:
362 * The conversion for unpacked fixed-point value f to floating point is
365 * unpackUnorm2x16: f / 65535.0
367 return (float) u
/ 65535.0f
;
371 * Evaluate one component of packHalf2x16.
374 pack_half_1x16(float x
)
376 return _mesa_float_to_half(x
);
380 * Evaluate one component of unpackHalf2x16.
383 unpack_half_1x16(uint16_t u
)
385 return _mesa_half_to_float(u
);
389 * Get the constant that is ultimately referenced by an r-value, in a constant
390 * expression evaluation context.
392 * The offset is used when the reference is to a specific column of a matrix.
395 constant_referenced(const ir_dereference
*deref
,
396 struct hash_table
*variable_context
,
397 ir_constant
*&store
, int &offset
)
402 if (variable_context
== NULL
)
405 switch (deref
->ir_type
) {
406 case ir_type_dereference_array
: {
407 const ir_dereference_array
*const da
=
408 (const ir_dereference_array
*) deref
;
410 ir_constant
*const index_c
=
411 da
->array_index
->constant_expression_value(variable_context
);
413 if (!index_c
|| !index_c
->type
->is_scalar() || !index_c
->type
->is_integer())
416 const int index
= index_c
->type
->base_type
== GLSL_TYPE_INT
?
417 index_c
->get_int_component(0) :
418 index_c
->get_uint_component(0);
420 ir_constant
*substore
;
423 const ir_dereference
*const deref
= da
->array
->as_dereference();
427 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
430 const glsl_type
*const vt
= da
->array
->type
;
431 if (vt
->is_array()) {
432 store
= substore
->get_array_element(index
);
434 } else if (vt
->is_matrix()) {
436 offset
= index
* vt
->vector_elements
;
437 } else if (vt
->is_vector()) {
439 offset
= suboffset
+ index
;
445 case ir_type_dereference_record
: {
446 const ir_dereference_record
*const dr
=
447 (const ir_dereference_record
*) deref
;
449 const ir_dereference
*const deref
= dr
->record
->as_dereference();
453 ir_constant
*substore
;
456 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
459 /* Since we're dropping it on the floor...
461 assert(suboffset
== 0);
463 store
= substore
->get_record_field(dr
->field
);
467 case ir_type_dereference_variable
: {
468 const ir_dereference_variable
*const dv
=
469 (const ir_dereference_variable
*) deref
;
471 store
= (ir_constant
*) hash_table_find(variable_context
, dv
->var
);
476 assert(!"Should not get here.");
480 return store
!= NULL
;
485 ir_rvalue::constant_expression_value(struct hash_table
*)
487 assert(this->type
->is_error());
492 ir_expression::constant_expression_value(struct hash_table
*variable_context
)
494 if (this->type
->is_error())
497 ir_constant
*op
[Elements(this->operands
)] = { NULL
, };
498 ir_constant_data data
;
500 memset(&data
, 0, sizeof(data
));
502 for (unsigned operand
= 0; operand
< this->get_num_operands(); operand
++) {
503 op
[operand
] = this->operands
[operand
]->constant_expression_value(variable_context
);
509 switch (this->operation
) {
510 case ir_binop_lshift
:
511 case ir_binop_rshift
:
513 case ir_binop_vector_extract
:
515 case ir_triop_bitfield_extract
:
519 assert(op
[0]->type
->base_type
== op
[1]->type
->base_type
);
523 bool op0_scalar
= op
[0]->type
->is_scalar();
524 bool op1_scalar
= op
[1] != NULL
&& op
[1]->type
->is_scalar();
526 /* When iterating over a vector or matrix's components, we want to increase
527 * the loop counter. However, for scalars, we want to stay at 0.
529 unsigned c0_inc
= op0_scalar
? 0 : 1;
530 unsigned c1_inc
= op1_scalar
? 0 : 1;
532 if (op1_scalar
|| !op
[1]) {
533 components
= op
[0]->type
->components();
535 components
= op
[1]->type
->components();
538 void *ctx
= ralloc_parent(this);
540 /* Handle array operations here, rather than below. */
541 if (op
[0]->type
->is_array()) {
542 assert(op
[1] != NULL
&& op
[1]->type
->is_array());
543 switch (this->operation
) {
544 case ir_binop_all_equal
:
545 return new(ctx
) ir_constant(op
[0]->has_value(op
[1]));
546 case ir_binop_any_nequal
:
547 return new(ctx
) ir_constant(!op
[0]->has_value(op
[1]));
554 switch (this->operation
) {
555 case ir_unop_bit_not
:
556 switch (op
[0]->type
->base_type
) {
558 for (unsigned c
= 0; c
< components
; c
++)
559 data
.i
[c
] = ~ op
[0]->value
.i
[c
];
562 for (unsigned c
= 0; c
< components
; c
++)
563 data
.u
[c
] = ~ op
[0]->value
.u
[c
];
570 case ir_unop_logic_not
:
571 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
572 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
573 data
.b
[c
] = !op
[0]->value
.b
[c
];
577 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
578 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
579 data
.i
[c
] = (int) op
[0]->value
.f
[c
];
583 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
584 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
585 data
.i
[c
] = (unsigned) op
[0]->value
.f
[c
];
589 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
590 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
591 data
.f
[c
] = (float) op
[0]->value
.i
[c
];
595 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
596 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
597 data
.f
[c
] = (float) op
[0]->value
.u
[c
];
601 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
602 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
603 data
.f
[c
] = op
[0]->value
.b
[c
] ? 1.0F
: 0.0F
;
607 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
608 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
609 data
.b
[c
] = op
[0]->value
.f
[c
] != 0.0F
? true : false;
613 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
614 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
615 data
.u
[c
] = op
[0]->value
.b
[c
] ? 1 : 0;
619 assert(op
[0]->type
->is_integer());
620 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
621 data
.b
[c
] = op
[0]->value
.u
[c
] ? true : false;
625 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
626 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
627 data
.i
[c
] = op
[0]->value
.u
[c
];
631 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
632 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
633 data
.u
[c
] = op
[0]->value
.i
[c
];
636 case ir_unop_bitcast_i2f
:
637 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
638 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
639 data
.f
[c
] = bitcast_u2f(op
[0]->value
.i
[c
]);
642 case ir_unop_bitcast_f2i
:
643 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
644 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
645 data
.i
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
648 case ir_unop_bitcast_u2f
:
649 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
650 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
651 data
.f
[c
] = bitcast_u2f(op
[0]->value
.u
[c
]);
654 case ir_unop_bitcast_f2u
:
655 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
656 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
657 data
.u
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
661 assert(op
[0]->type
->is_boolean());
663 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
664 if (op
[0]->value
.b
[c
])
670 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
671 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
672 data
.f
[c
] = truncf(op
[0]->value
.f
[c
]);
676 case ir_unop_round_even
:
677 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
678 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
679 data
.f
[c
] = _mesa_round_to_even(op
[0]->value
.f
[c
]);
684 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
685 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
686 data
.f
[c
] = ceilf(op
[0]->value
.f
[c
]);
691 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
692 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
693 data
.f
[c
] = floorf(op
[0]->value
.f
[c
]);
698 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
699 switch (this->type
->base_type
) {
706 case GLSL_TYPE_FLOAT
:
707 data
.f
[c
] = op
[0]->value
.f
[c
] - floor(op
[0]->value
.f
[c
]);
716 case ir_unop_sin_reduced
:
717 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
718 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
719 data
.f
[c
] = sinf(op
[0]->value
.f
[c
]);
724 case ir_unop_cos_reduced
:
725 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
726 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
727 data
.f
[c
] = cosf(op
[0]->value
.f
[c
]);
732 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
733 switch (this->type
->base_type
) {
735 data
.u
[c
] = -((int) op
[0]->value
.u
[c
]);
738 data
.i
[c
] = -op
[0]->value
.i
[c
];
740 case GLSL_TYPE_FLOAT
:
741 data
.f
[c
] = -op
[0]->value
.f
[c
];
750 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
751 switch (this->type
->base_type
) {
753 data
.u
[c
] = op
[0]->value
.u
[c
];
756 data
.i
[c
] = op
[0]->value
.i
[c
];
758 data
.i
[c
] = -data
.i
[c
];
760 case GLSL_TYPE_FLOAT
:
761 data
.f
[c
] = fabs(op
[0]->value
.f
[c
]);
770 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
771 switch (this->type
->base_type
) {
773 data
.u
[c
] = op
[0]->value
.i
[c
] > 0;
776 data
.i
[c
] = (op
[0]->value
.i
[c
] > 0) - (op
[0]->value
.i
[c
] < 0);
778 case GLSL_TYPE_FLOAT
:
779 data
.f
[c
] = float((op
[0]->value
.f
[c
] > 0)-(op
[0]->value
.f
[c
] < 0));
788 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
789 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
790 switch (this->type
->base_type
) {
792 if (op
[0]->value
.u
[c
] != 0.0)
793 data
.u
[c
] = 1 / op
[0]->value
.u
[c
];
796 if (op
[0]->value
.i
[c
] != 0.0)
797 data
.i
[c
] = 1 / op
[0]->value
.i
[c
];
799 case GLSL_TYPE_FLOAT
:
800 if (op
[0]->value
.f
[c
] != 0.0)
801 data
.f
[c
] = 1.0F
/ op
[0]->value
.f
[c
];
810 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
811 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
812 data
.f
[c
] = 1.0F
/ sqrtf(op
[0]->value
.f
[c
]);
817 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
818 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
819 data
.f
[c
] = sqrtf(op
[0]->value
.f
[c
]);
824 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
825 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
826 data
.f
[c
] = expf(op
[0]->value
.f
[c
]);
831 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
832 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
833 data
.f
[c
] = exp2f(op
[0]->value
.f
[c
]);
838 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
839 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
840 data
.f
[c
] = logf(op
[0]->value
.f
[c
]);
845 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
846 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
847 data
.f
[c
] = log2f(op
[0]->value
.f
[c
]);
853 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
854 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
859 case ir_unop_pack_snorm_2x16
:
860 assert(op
[0]->type
== glsl_type::vec2_type
);
861 data
.u
[0] = pack_2x16(pack_snorm_1x16
,
865 case ir_unop_pack_snorm_4x8
:
866 assert(op
[0]->type
== glsl_type::vec4_type
);
867 data
.u
[0] = pack_4x8(pack_snorm_1x8
,
873 case ir_unop_unpack_snorm_2x16
:
874 assert(op
[0]->type
== glsl_type::uint_type
);
875 unpack_2x16(unpack_snorm_1x16
,
877 &data
.f
[0], &data
.f
[1]);
879 case ir_unop_unpack_snorm_4x8
:
880 assert(op
[0]->type
== glsl_type::uint_type
);
881 unpack_4x8(unpack_snorm_1x8
,
883 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
885 case ir_unop_pack_unorm_2x16
:
886 assert(op
[0]->type
== glsl_type::vec2_type
);
887 data
.u
[0] = pack_2x16(pack_unorm_1x16
,
891 case ir_unop_pack_unorm_4x8
:
892 assert(op
[0]->type
== glsl_type::vec4_type
);
893 data
.u
[0] = pack_4x8(pack_unorm_1x8
,
899 case ir_unop_unpack_unorm_2x16
:
900 assert(op
[0]->type
== glsl_type::uint_type
);
901 unpack_2x16(unpack_unorm_1x16
,
903 &data
.f
[0], &data
.f
[1]);
905 case ir_unop_unpack_unorm_4x8
:
906 assert(op
[0]->type
== glsl_type::uint_type
);
907 unpack_4x8(unpack_unorm_1x8
,
909 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
911 case ir_unop_pack_half_2x16
:
912 assert(op
[0]->type
== glsl_type::vec2_type
);
913 data
.u
[0] = pack_2x16(pack_half_1x16
,
917 case ir_unop_unpack_half_2x16
:
918 assert(op
[0]->type
== glsl_type::uint_type
);
919 unpack_2x16(unpack_half_1x16
,
921 &data
.f
[0], &data
.f
[1]);
924 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
925 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
926 data
.f
[c
] = powf(op
[0]->value
.f
[c
], op
[1]->value
.f
[c
]);
931 data
.f
[0] = dot(op
[0], op
[1]);
935 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
936 for (unsigned c
= 0, c0
= 0, c1
= 0;
938 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
940 switch (op
[0]->type
->base_type
) {
942 data
.u
[c
] = MIN2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
945 data
.i
[c
] = MIN2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
947 case GLSL_TYPE_FLOAT
:
948 data
.f
[c
] = MIN2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
957 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
958 for (unsigned c
= 0, c0
= 0, c1
= 0;
960 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
962 switch (op
[0]->type
->base_type
) {
964 data
.u
[c
] = MAX2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
967 data
.i
[c
] = MAX2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
969 case GLSL_TYPE_FLOAT
:
970 data
.f
[c
] = MAX2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
979 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
980 for (unsigned c
= 0, c0
= 0, c1
= 0;
982 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
984 switch (op
[0]->type
->base_type
) {
986 data
.u
[c
] = op
[0]->value
.u
[c0
] + op
[1]->value
.u
[c1
];
989 data
.i
[c
] = op
[0]->value
.i
[c0
] + op
[1]->value
.i
[c1
];
991 case GLSL_TYPE_FLOAT
:
992 data
.f
[c
] = op
[0]->value
.f
[c0
] + op
[1]->value
.f
[c1
];
1001 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1002 for (unsigned c
= 0, c0
= 0, c1
= 0;
1004 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1006 switch (op
[0]->type
->base_type
) {
1007 case GLSL_TYPE_UINT
:
1008 data
.u
[c
] = op
[0]->value
.u
[c0
] - op
[1]->value
.u
[c1
];
1011 data
.i
[c
] = op
[0]->value
.i
[c0
] - op
[1]->value
.i
[c1
];
1013 case GLSL_TYPE_FLOAT
:
1014 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
];
1023 /* Check for equal types, or unequal types involving scalars */
1024 if ((op
[0]->type
== op
[1]->type
&& !op
[0]->type
->is_matrix())
1025 || op0_scalar
|| op1_scalar
) {
1026 for (unsigned c
= 0, c0
= 0, c1
= 0;
1028 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1030 switch (op
[0]->type
->base_type
) {
1031 case GLSL_TYPE_UINT
:
1032 data
.u
[c
] = op
[0]->value
.u
[c0
] * op
[1]->value
.u
[c1
];
1035 data
.i
[c
] = op
[0]->value
.i
[c0
] * op
[1]->value
.i
[c1
];
1037 case GLSL_TYPE_FLOAT
:
1038 data
.f
[c
] = op
[0]->value
.f
[c0
] * op
[1]->value
.f
[c1
];
1045 assert(op
[0]->type
->is_matrix() || op
[1]->type
->is_matrix());
1047 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
1048 * matrix can be a GLSL vector, either N or P can be 1.
1050 * For vec*mat, the vector is treated as a row vector. This
1051 * means the vector is a 1-row x M-column matrix.
1053 * For mat*vec, the vector is treated as a column vector. Since
1054 * matrix_columns is 1 for vectors, this just works.
1056 const unsigned n
= op
[0]->type
->is_vector()
1057 ? 1 : op
[0]->type
->vector_elements
;
1058 const unsigned m
= op
[1]->type
->vector_elements
;
1059 const unsigned p
= op
[1]->type
->matrix_columns
;
1060 for (unsigned j
= 0; j
< p
; j
++) {
1061 for (unsigned i
= 0; i
< n
; i
++) {
1062 for (unsigned k
= 0; k
< m
; k
++) {
1063 data
.f
[i
+n
*j
] += op
[0]->value
.f
[i
+n
*k
]*op
[1]->value
.f
[k
+m
*j
];
1071 /* FINISHME: Emit warning when division-by-zero is detected. */
1072 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1073 for (unsigned c
= 0, c0
= 0, c1
= 0;
1075 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1077 switch (op
[0]->type
->base_type
) {
1078 case GLSL_TYPE_UINT
:
1079 if (op
[1]->value
.u
[c1
] == 0) {
1082 data
.u
[c
] = op
[0]->value
.u
[c0
] / op
[1]->value
.u
[c1
];
1086 if (op
[1]->value
.i
[c1
] == 0) {
1089 data
.i
[c
] = op
[0]->value
.i
[c0
] / op
[1]->value
.i
[c1
];
1092 case GLSL_TYPE_FLOAT
:
1093 data
.f
[c
] = op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
];
1102 /* FINISHME: Emit warning when division-by-zero is detected. */
1103 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1104 for (unsigned c
= 0, c0
= 0, c1
= 0;
1106 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1108 switch (op
[0]->type
->base_type
) {
1109 case GLSL_TYPE_UINT
:
1110 if (op
[1]->value
.u
[c1
] == 0) {
1113 data
.u
[c
] = op
[0]->value
.u
[c0
] % op
[1]->value
.u
[c1
];
1117 if (op
[1]->value
.i
[c1
] == 0) {
1120 data
.i
[c
] = op
[0]->value
.i
[c0
] % op
[1]->value
.i
[c1
];
1123 case GLSL_TYPE_FLOAT
:
1124 /* We don't use fmod because it rounds toward zero; GLSL specifies
1127 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
]
1128 * floorf(op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
]);
1137 case ir_binop_logic_and
:
1138 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1139 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1140 data
.b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
1142 case ir_binop_logic_xor
:
1143 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1144 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1145 data
.b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
1147 case ir_binop_logic_or
:
1148 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1149 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1150 data
.b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
1154 assert(op
[0]->type
== op
[1]->type
);
1155 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1156 switch (op
[0]->type
->base_type
) {
1157 case GLSL_TYPE_UINT
:
1158 data
.b
[c
] = op
[0]->value
.u
[c
] < op
[1]->value
.u
[c
];
1161 data
.b
[c
] = op
[0]->value
.i
[c
] < op
[1]->value
.i
[c
];
1163 case GLSL_TYPE_FLOAT
:
1164 data
.b
[c
] = op
[0]->value
.f
[c
] < op
[1]->value
.f
[c
];
1171 case ir_binop_greater
:
1172 assert(op
[0]->type
== op
[1]->type
);
1173 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1174 switch (op
[0]->type
->base_type
) {
1175 case GLSL_TYPE_UINT
:
1176 data
.b
[c
] = op
[0]->value
.u
[c
] > op
[1]->value
.u
[c
];
1179 data
.b
[c
] = op
[0]->value
.i
[c
] > op
[1]->value
.i
[c
];
1181 case GLSL_TYPE_FLOAT
:
1182 data
.b
[c
] = op
[0]->value
.f
[c
] > op
[1]->value
.f
[c
];
1189 case ir_binop_lequal
:
1190 assert(op
[0]->type
== op
[1]->type
);
1191 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1192 switch (op
[0]->type
->base_type
) {
1193 case GLSL_TYPE_UINT
:
1194 data
.b
[c
] = op
[0]->value
.u
[c
] <= op
[1]->value
.u
[c
];
1197 data
.b
[c
] = op
[0]->value
.i
[c
] <= op
[1]->value
.i
[c
];
1199 case GLSL_TYPE_FLOAT
:
1200 data
.b
[c
] = op
[0]->value
.f
[c
] <= op
[1]->value
.f
[c
];
1207 case ir_binop_gequal
:
1208 assert(op
[0]->type
== op
[1]->type
);
1209 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1210 switch (op
[0]->type
->base_type
) {
1211 case GLSL_TYPE_UINT
:
1212 data
.b
[c
] = op
[0]->value
.u
[c
] >= op
[1]->value
.u
[c
];
1215 data
.b
[c
] = op
[0]->value
.i
[c
] >= op
[1]->value
.i
[c
];
1217 case GLSL_TYPE_FLOAT
:
1218 data
.b
[c
] = op
[0]->value
.f
[c
] >= op
[1]->value
.f
[c
];
1225 case ir_binop_equal
:
1226 assert(op
[0]->type
== op
[1]->type
);
1227 for (unsigned c
= 0; c
< components
; c
++) {
1228 switch (op
[0]->type
->base_type
) {
1229 case GLSL_TYPE_UINT
:
1230 data
.b
[c
] = op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
1233 data
.b
[c
] = op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
1235 case GLSL_TYPE_FLOAT
:
1236 data
.b
[c
] = op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
1238 case GLSL_TYPE_BOOL
:
1239 data
.b
[c
] = op
[0]->value
.b
[c
] == op
[1]->value
.b
[c
];
1246 case ir_binop_nequal
:
1247 assert(op
[0]->type
== op
[1]->type
);
1248 for (unsigned c
= 0; c
< components
; c
++) {
1249 switch (op
[0]->type
->base_type
) {
1250 case GLSL_TYPE_UINT
:
1251 data
.b
[c
] = op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
1254 data
.b
[c
] = op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
1256 case GLSL_TYPE_FLOAT
:
1257 data
.b
[c
] = op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
1259 case GLSL_TYPE_BOOL
:
1260 data
.b
[c
] = op
[0]->value
.b
[c
] != op
[1]->value
.b
[c
];
1267 case ir_binop_all_equal
:
1268 data
.b
[0] = op
[0]->has_value(op
[1]);
1270 case ir_binop_any_nequal
:
1271 data
.b
[0] = !op
[0]->has_value(op
[1]);
1274 case ir_binop_lshift
:
1275 for (unsigned c
= 0, c0
= 0, c1
= 0;
1277 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1279 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1280 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1281 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.i
[c1
];
1283 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1284 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1285 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.u
[c1
];
1287 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1288 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1289 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.i
[c1
];
1291 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1292 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1293 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.u
[c1
];
1298 case ir_binop_rshift
:
1299 for (unsigned c
= 0, c0
= 0, c1
= 0;
1301 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1303 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1304 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1305 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.i
[c1
];
1307 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1308 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1309 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.u
[c1
];
1311 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1312 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1313 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.i
[c1
];
1315 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1316 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1317 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.u
[c1
];
1322 case ir_binop_bit_and
:
1323 for (unsigned c
= 0, c0
= 0, c1
= 0;
1325 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1327 switch (op
[0]->type
->base_type
) {
1329 data
.i
[c
] = op
[0]->value
.i
[c0
] & op
[1]->value
.i
[c1
];
1331 case GLSL_TYPE_UINT
:
1332 data
.u
[c
] = op
[0]->value
.u
[c0
] & op
[1]->value
.u
[c1
];
1340 case ir_binop_bit_or
:
1341 for (unsigned c
= 0, c0
= 0, c1
= 0;
1343 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1345 switch (op
[0]->type
->base_type
) {
1347 data
.i
[c
] = op
[0]->value
.i
[c0
] | op
[1]->value
.i
[c1
];
1349 case GLSL_TYPE_UINT
:
1350 data
.u
[c
] = op
[0]->value
.u
[c0
] | op
[1]->value
.u
[c1
];
1358 case ir_binop_vector_extract
: {
1359 const int c
= CLAMP(op
[1]->value
.i
[0], 0,
1360 (int) op
[0]->type
->vector_elements
- 1);
1362 switch (op
[0]->type
->base_type
) {
1363 case GLSL_TYPE_UINT
:
1364 data
.u
[0] = op
[0]->value
.u
[c
];
1367 data
.i
[0] = op
[0]->value
.i
[c
];
1369 case GLSL_TYPE_FLOAT
:
1370 data
.f
[0] = op
[0]->value
.f
[c
];
1372 case GLSL_TYPE_BOOL
:
1373 data
.b
[0] = op
[0]->value
.b
[c
];
1381 case ir_binop_bit_xor
:
1382 for (unsigned c
= 0, c0
= 0, c1
= 0;
1384 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1386 switch (op
[0]->type
->base_type
) {
1388 data
.i
[c
] = op
[0]->value
.i
[c0
] ^ op
[1]->value
.i
[c1
];
1390 case GLSL_TYPE_UINT
:
1391 data
.u
[c
] = op
[0]->value
.u
[c0
] ^ op
[1]->value
.u
[c1
];
1399 case ir_unop_bitfield_reverse
:
1400 /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
1401 for (unsigned c
= 0; c
< components
; c
++) {
1402 unsigned int v
= op
[0]->value
.u
[c
]; // input bits to be reversed
1403 unsigned int r
= v
; // r will be reversed bits of v; first get LSB of v
1404 int s
= sizeof(v
) * CHAR_BIT
- 1; // extra shift needed at end
1406 for (v
>>= 1; v
; v
>>= 1) {
1411 r
<<= s
; // shift when v's highest bits are zero
1417 case ir_unop_bit_count
:
1418 for (unsigned c
= 0; c
< components
; c
++) {
1420 unsigned v
= op
[0]->value
.u
[c
];
1422 for (; v
; count
++) {
1429 case ir_unop_find_msb
:
1430 for (unsigned c
= 0; c
< components
; c
++) {
1431 int v
= op
[0]->value
.i
[c
];
1433 if (v
== 0 || (op
[0]->type
->base_type
== GLSL_TYPE_INT
&& v
== -1))
1437 int top_bit
= op
[0]->type
->base_type
== GLSL_TYPE_UINT
1438 ? 0 : v
& (1 << 31);
1440 while (((v
& (1 << 31)) == top_bit
) && count
!= 32) {
1445 data
.i
[c
] = 31 - count
;
1450 case ir_unop_find_lsb
:
1451 for (unsigned c
= 0; c
< components
; c
++) {
1452 if (op
[0]->value
.i
[c
] == 0)
1456 unsigned v
= op
[0]->value
.u
[c
];
1458 for (; !(v
& 1); v
>>= 1) {
1466 case ir_triop_bitfield_extract
: {
1467 int offset
= op
[1]->value
.i
[0];
1468 int bits
= op
[2]->value
.i
[0];
1470 for (unsigned c
= 0; c
< components
; c
++) {
1473 else if (offset
< 0 || bits
< 0)
1474 data
.u
[c
] = 0; /* Undefined, per spec. */
1475 else if (offset
+ bits
> 32)
1476 data
.u
[c
] = 0; /* Undefined, per spec. */
1478 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
) {
1479 /* int so that the right shift will sign-extend. */
1480 int value
= op
[0]->value
.i
[c
];
1481 value
<<= 32 - bits
- offset
;
1482 value
>>= 32 - bits
;
1485 unsigned value
= op
[0]->value
.u
[c
];
1486 value
<<= 32 - bits
- offset
;
1487 value
>>= 32 - bits
;
1495 case ir_binop_bfm
: {
1496 int bits
= op
[0]->value
.i
[0];
1497 int offset
= op
[1]->value
.i
[0];
1499 for (unsigned c
= 0; c
< components
; c
++) {
1501 data
.u
[c
] = op
[0]->value
.u
[c
];
1502 else if (offset
< 0 || bits
< 0)
1503 data
.u
[c
] = 0; /* Undefined for bitfieldInsert, per spec. */
1504 else if (offset
+ bits
> 32)
1505 data
.u
[c
] = 0; /* Undefined for bitfieldInsert, per spec. */
1507 data
.u
[c
] = ((1 << bits
) - 1) << offset
;
1512 case ir_binop_ldexp
:
1513 for (unsigned c
= 0; c
< components
; c
++) {
1514 data
.f
[c
] = ldexp(op
[0]->value
.f
[c
], op
[1]->value
.i
[c
]);
1515 /* Flush subnormal values to zero. */
1516 if (!isnormal(data
.f
[c
]))
1517 data
.f
[c
] = copysign(0.0f
, op
[0]->value
.f
[c
]);
1522 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1523 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
);
1524 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
);
1526 for (unsigned c
= 0; c
< components
; c
++) {
1527 data
.f
[c
] = op
[0]->value
.f
[c
] * op
[1]->value
.f
[c
]
1528 + op
[2]->value
.f
[c
];
1532 case ir_triop_lrp
: {
1533 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1534 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
);
1535 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
);
1537 unsigned c2_inc
= op
[2]->type
->is_scalar() ? 0 : 1;
1538 for (unsigned c
= 0, c2
= 0; c
< components
; c2
+= c2_inc
, c
++) {
1539 data
.f
[c
] = op
[0]->value
.f
[c
] * (1.0f
- op
[2]->value
.f
[c2
]) +
1540 (op
[1]->value
.f
[c
] * op
[2]->value
.f
[c2
]);
1546 for (unsigned c
= 0; c
< components
; c
++) {
1547 data
.u
[c
] = op
[0]->value
.b
[c
] ? op
[1]->value
.u
[c
]
1548 : op
[2]->value
.u
[c
];
1552 case ir_triop_vector_insert
: {
1553 const unsigned idx
= op
[2]->value
.u
[0];
1555 memcpy(&data
, &op
[0]->value
, sizeof(data
));
1557 switch (this->type
->base_type
) {
1559 data
.i
[idx
] = op
[1]->value
.i
[0];
1561 case GLSL_TYPE_UINT
:
1562 data
.u
[idx
] = op
[1]->value
.u
[0];
1564 case GLSL_TYPE_FLOAT
:
1565 data
.f
[idx
] = op
[1]->value
.f
[0];
1567 case GLSL_TYPE_BOOL
:
1568 data
.b
[idx
] = op
[1]->value
.b
[0];
1571 assert(!"Should not get here.");
1577 case ir_quadop_bitfield_insert
: {
1578 int offset
= op
[2]->value
.i
[0];
1579 int bits
= op
[3]->value
.i
[0];
1581 for (unsigned c
= 0; c
< components
; c
++) {
1583 data
.u
[c
] = op
[0]->value
.u
[c
];
1584 else if (offset
< 0 || bits
< 0)
1585 data
.u
[c
] = 0; /* Undefined, per spec. */
1586 else if (offset
+ bits
> 32)
1587 data
.u
[c
] = 0; /* Undefined, per spec. */
1589 unsigned insert_mask
= ((1 << bits
) - 1) << offset
;
1591 unsigned insert
= op
[1]->value
.u
[c
];
1593 insert
&= insert_mask
;
1595 unsigned base
= op
[0]->value
.u
[c
];
1596 base
&= ~insert_mask
;
1598 data
.u
[c
] = base
| insert
;
1604 case ir_quadop_vector
:
1605 for (unsigned c
= 0; c
< this->type
->vector_elements
; c
++) {
1606 switch (this->type
->base_type
) {
1608 data
.i
[c
] = op
[c
]->value
.i
[0];
1610 case GLSL_TYPE_UINT
:
1611 data
.u
[c
] = op
[c
]->value
.u
[0];
1613 case GLSL_TYPE_FLOAT
:
1614 data
.f
[c
] = op
[c
]->value
.f
[0];
1623 /* FINISHME: Should handle all expression types. */
1627 return new(ctx
) ir_constant(this->type
, &data
);
1632 ir_texture::constant_expression_value(struct hash_table
*)
1634 /* texture lookups aren't constant expressions */
1640 ir_swizzle::constant_expression_value(struct hash_table
*variable_context
)
1642 ir_constant
*v
= this->val
->constant_expression_value(variable_context
);
1645 ir_constant_data data
= { { 0 } };
1647 const unsigned swiz_idx
[4] = {
1648 this->mask
.x
, this->mask
.y
, this->mask
.z
, this->mask
.w
1651 for (unsigned i
= 0; i
< this->mask
.num_components
; i
++) {
1652 switch (v
->type
->base_type
) {
1653 case GLSL_TYPE_UINT
:
1654 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
1655 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
1656 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
1657 default: assert(!"Should not get here."); break;
1661 void *ctx
= ralloc_parent(this);
1662 return new(ctx
) ir_constant(this->type
, &data
);
1669 ir_dereference_variable::constant_expression_value(struct hash_table
*variable_context
)
1671 /* This may occur during compile and var->type is glsl_type::error_type */
1675 /* Give priority to the context hashtable, if it exists */
1676 if (variable_context
) {
1677 ir_constant
*value
= (ir_constant
*)hash_table_find(variable_context
, var
);
1682 /* The constant_value of a uniform variable is its initializer,
1683 * not the lifetime constant value of the uniform.
1685 if (var
->data
.mode
== ir_var_uniform
)
1688 if (!var
->constant_value
)
1691 return var
->constant_value
->clone(ralloc_parent(var
), NULL
);
1696 ir_dereference_array::constant_expression_value(struct hash_table
*variable_context
)
1698 ir_constant
*array
= this->array
->constant_expression_value(variable_context
);
1699 ir_constant
*idx
= this->array_index
->constant_expression_value(variable_context
);
1701 if ((array
!= NULL
) && (idx
!= NULL
)) {
1702 void *ctx
= ralloc_parent(this);
1703 if (array
->type
->is_matrix()) {
1704 /* Array access of a matrix results in a vector.
1706 const unsigned column
= idx
->value
.u
[0];
1708 const glsl_type
*const column_type
= array
->type
->column_type();
1710 /* Offset in the constant matrix to the first element of the column
1713 const unsigned mat_idx
= column
* column_type
->vector_elements
;
1715 ir_constant_data data
= { { 0 } };
1717 switch (column_type
->base_type
) {
1718 case GLSL_TYPE_UINT
:
1720 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1721 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
1725 case GLSL_TYPE_FLOAT
:
1726 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1727 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
1732 assert(!"Should not get here.");
1736 return new(ctx
) ir_constant(column_type
, &data
);
1737 } else if (array
->type
->is_vector()) {
1738 const unsigned component
= idx
->value
.u
[0];
1740 return new(ctx
) ir_constant(array
, component
);
1742 const unsigned index
= idx
->value
.u
[0];
1743 return array
->get_array_element(index
)->clone(ctx
, NULL
);
1751 ir_dereference_record::constant_expression_value(struct hash_table
*)
1753 ir_constant
*v
= this->record
->constant_expression_value();
1755 return (v
!= NULL
) ? v
->get_record_field(this->field
) : NULL
;
1760 ir_assignment::constant_expression_value(struct hash_table
*)
1762 /* FINISHME: Handle CEs involving assignment (return RHS) */
1768 ir_constant::constant_expression_value(struct hash_table
*)
1775 ir_call::constant_expression_value(struct hash_table
*variable_context
)
1777 return this->callee
->constant_expression_value(&this->actual_parameters
, variable_context
);
1781 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list
&body
,
1782 struct hash_table
*variable_context
,
1783 ir_constant
**result
)
1785 foreach_in_list(ir_instruction
, inst
, &body
) {
1786 switch(inst
->ir_type
) {
1788 /* (declare () type symbol) */
1789 case ir_type_variable
: {
1790 ir_variable
*var
= inst
->as_variable();
1791 hash_table_insert(variable_context
, ir_constant::zero(this, var
->type
), var
);
1795 /* (assign [condition] (write-mask) (ref) (value)) */
1796 case ir_type_assignment
: {
1797 ir_assignment
*asg
= inst
->as_assignment();
1798 if (asg
->condition
) {
1799 ir_constant
*cond
= asg
->condition
->constant_expression_value(variable_context
);
1802 if (!cond
->get_bool_component(0))
1806 ir_constant
*store
= NULL
;
1809 if (!constant_referenced(asg
->lhs
, variable_context
, store
, offset
))
1812 ir_constant
*value
= asg
->rhs
->constant_expression_value(variable_context
);
1817 store
->copy_masked_offset(value
, offset
, asg
->write_mask
);
1821 /* (return (expression)) */
1822 case ir_type_return
:
1824 *result
= inst
->as_return()->value
->constant_expression_value(variable_context
);
1825 return *result
!= NULL
;
1827 /* (call name (ref) (params))*/
1828 case ir_type_call
: {
1829 ir_call
*call
= inst
->as_call();
1831 /* Just say no to void functions in constant expressions. We
1832 * don't need them at that point.
1835 if (!call
->return_deref
)
1838 ir_constant
*store
= NULL
;
1841 if (!constant_referenced(call
->return_deref
, variable_context
,
1845 ir_constant
*value
= call
->constant_expression_value(variable_context
);
1850 store
->copy_offset(value
, offset
);
1854 /* (if condition (then-instructions) (else-instructions)) */
1856 ir_if
*iif
= inst
->as_if();
1858 ir_constant
*cond
= iif
->condition
->constant_expression_value(variable_context
);
1859 if (!cond
|| !cond
->type
->is_boolean())
1862 exec_list
&branch
= cond
->get_bool_component(0) ? iif
->then_instructions
: iif
->else_instructions
;
1865 if (!constant_expression_evaluate_expression_list(branch
, variable_context
, result
))
1868 /* If there was a return in the branch chosen, drop out now. */
1875 /* Every other expression type, we drop out. */
1881 /* Reaching the end of the block is not an error condition */
1889 ir_function_signature::constant_expression_value(exec_list
*actual_parameters
, struct hash_table
*variable_context
)
1891 const glsl_type
*type
= this->return_type
;
1892 if (type
== glsl_type::void_type
)
1895 /* From the GLSL 1.20 spec, page 23:
1896 * "Function calls to user-defined functions (non-built-in functions)
1897 * cannot be used to form constant expressions."
1899 if (!this->is_builtin())
1903 * Of the builtin functions, only the texture lookups and the noise
1904 * ones must not be used in constant expressions. They all include
1905 * specific opcodes so they don't need to be special-cased at this
1909 /* Initialize the table of dereferencable names with the function
1910 * parameters. Verify their const-ness on the way.
1912 * We expect the correctness of the number of parameters to have
1913 * been checked earlier.
1915 hash_table
*deref_hash
= hash_table_ctor(8, hash_table_pointer_hash
,
1916 hash_table_pointer_compare
);
1918 /* If "origin" is non-NULL, then the function body is there. So we
1919 * have to use the variable objects from the object with the body,
1920 * but the parameter instanciation on the current object.
1922 const exec_node
*parameter_info
= origin
? origin
->parameters
.head
: parameters
.head
;
1924 foreach_in_list(ir_rvalue
, n
, actual_parameters
) {
1925 ir_constant
*constant
= n
->constant_expression_value(variable_context
);
1926 if (constant
== NULL
) {
1927 hash_table_dtor(deref_hash
);
1932 ir_variable
*var
= (ir_variable
*)parameter_info
;
1933 hash_table_insert(deref_hash
, constant
, var
);
1935 parameter_info
= parameter_info
->next
;
1938 ir_constant
*result
= NULL
;
1940 /* Now run the builtin function until something non-constant
1941 * happens or we get the result.
1943 if (constant_expression_evaluate_expression_list(origin
? origin
->body
: body
, deref_hash
, &result
) && result
)
1944 result
= result
->clone(ralloc_parent(this), NULL
);
1946 hash_table_dtor(deref_hash
);