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
;
51 static double copysign(double x
, double y
)
53 return _copysign(x
, y
);
58 dot(ir_constant
*op0
, ir_constant
*op1
)
60 assert(op0
->type
->is_float() && op1
->type
->is_float());
63 for (unsigned c
= 0; c
< op0
->type
->components(); c
++)
64 result
+= op0
->value
.f
[c
] * op1
->value
.f
[c
];
69 /* This method is the only one supported by gcc. Unions in particular
70 * are iffy, and read-through-converted-pointer is killed by strict
71 * aliasing. OTOH, the compiler sees through the memcpy, so the
72 * resulting asm is reasonable.
75 bitcast_u2f(unsigned int u
)
77 assert(sizeof(float) == sizeof(unsigned int));
79 memcpy(&f
, &u
, sizeof(f
));
86 assert(sizeof(float) == sizeof(unsigned int));
88 memcpy(&u
, &f
, sizeof(f
));
93 * Evaluate one component of a floating-point 4x8 unpacking function.
96 (*pack_1x8_func_t
)(float);
99 * Evaluate one component of a floating-point 2x16 unpacking function.
102 (*pack_1x16_func_t
)(float);
105 * Evaluate one component of a floating-point 4x8 unpacking function.
108 (*unpack_1x8_func_t
)(uint8_t);
111 * Evaluate one component of a floating-point 2x16 unpacking function.
114 (*unpack_1x16_func_t
)(uint16_t);
117 * Evaluate a 2x16 floating-point packing function.
120 pack_2x16(pack_1x16_func_t pack_1x16
,
123 /* From section 8.4 of the GLSL ES 3.00 spec:
127 * The first component of the vector will be written to the least
128 * significant bits of the output; the last component will be written to
129 * the most significant bits.
131 * The specifications for the other packing functions contain similar
135 u
|= ((uint32_t) pack_1x16(x
) << 0);
136 u
|= ((uint32_t) pack_1x16(y
) << 16);
141 * Evaluate a 4x8 floating-point packing function.
144 pack_4x8(pack_1x8_func_t pack_1x8
,
145 float x
, float y
, float z
, float w
)
147 /* From section 8.4 of the GLSL 4.30 spec:
151 * The first component of the vector will be written to the least
152 * significant bits of the output; the last component will be written to
153 * the most significant bits.
155 * The specifications for the other packing functions contain similar
159 u
|= ((uint32_t) pack_1x8(x
) << 0);
160 u
|= ((uint32_t) pack_1x8(y
) << 8);
161 u
|= ((uint32_t) pack_1x8(z
) << 16);
162 u
|= ((uint32_t) pack_1x8(w
) << 24);
167 * Evaluate a 2x16 floating-point unpacking function.
170 unpack_2x16(unpack_1x16_func_t unpack_1x16
,
174 /* From section 8.4 of the GLSL ES 3.00 spec:
178 * The first component of the returned vector will be extracted from
179 * the least significant bits of the input; the last component will be
180 * extracted from the most significant bits.
182 * The specifications for the other unpacking functions contain similar
185 *x
= unpack_1x16((uint16_t) (u
& 0xffff));
186 *y
= unpack_1x16((uint16_t) (u
>> 16));
190 * Evaluate a 4x8 floating-point unpacking function.
193 unpack_4x8(unpack_1x8_func_t unpack_1x8
, uint32_t u
,
194 float *x
, float *y
, float *z
, float *w
)
196 /* From section 8.4 of the GLSL 4.30 spec:
200 * The first component of the returned vector will be extracted from
201 * the least significant bits of the input; the last component will be
202 * extracted from the most significant bits.
204 * The specifications for the other unpacking functions contain similar
207 *x
= unpack_1x8((uint8_t) (u
& 0xff));
208 *y
= unpack_1x8((uint8_t) (u
>> 8));
209 *z
= unpack_1x8((uint8_t) (u
>> 16));
210 *w
= unpack_1x8((uint8_t) (u
>> 24));
214 * Evaluate one component of packSnorm4x8.
217 pack_snorm_1x8(float x
)
219 /* From section 8.4 of the GLSL 4.30 spec:
223 * The conversion for component c of v to fixed point is done as
226 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
228 * We must first cast the float to an int, because casting a negative
229 * float to a uint is undefined.
231 return (uint8_t) (int8_t)
232 _mesa_round_to_even(CLAMP(x
, -1.0f
, +1.0f
) * 127.0f
);
236 * Evaluate one component of packSnorm2x16.
239 pack_snorm_1x16(float x
)
241 /* From section 8.4 of the GLSL ES 3.00 spec:
245 * The conversion for component c of v to fixed point is done as
248 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
250 * We must first cast the float to an int, because casting a negative
251 * float to a uint is undefined.
253 return (uint16_t) (int16_t)
254 _mesa_round_to_even(CLAMP(x
, -1.0f
, +1.0f
) * 32767.0f
);
258 * Evaluate one component of unpackSnorm4x8.
261 unpack_snorm_1x8(uint8_t u
)
263 /* From section 8.4 of the GLSL 4.30 spec:
267 * The conversion for unpacked fixed-point value f to floating point is
270 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
272 return CLAMP((int8_t) u
/ 127.0f
, -1.0f
, +1.0f
);
276 * Evaluate one component of unpackSnorm2x16.
279 unpack_snorm_1x16(uint16_t u
)
281 /* From section 8.4 of the GLSL ES 3.00 spec:
285 * The conversion for unpacked fixed-point value f to floating point is
288 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
290 return CLAMP((int16_t) u
/ 32767.0f
, -1.0f
, +1.0f
);
294 * Evaluate one component packUnorm4x8.
297 pack_unorm_1x8(float x
)
299 /* From section 8.4 of the GLSL 4.30 spec:
303 * The conversion for component c of v to fixed point is done as
306 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
308 return (uint8_t) _mesa_round_to_even(CLAMP(x
, 0.0f
, 1.0f
) * 255.0f
);
312 * Evaluate one component packUnorm2x16.
315 pack_unorm_1x16(float x
)
317 /* From section 8.4 of the GLSL ES 3.00 spec:
321 * The conversion for component c of v to fixed point is done as
324 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
326 return (uint16_t) _mesa_round_to_even(CLAMP(x
, 0.0f
, 1.0f
) * 65535.0f
);
330 * Evaluate one component of unpackUnorm4x8.
333 unpack_unorm_1x8(uint8_t u
)
335 /* From section 8.4 of the GLSL 4.30 spec:
339 * The conversion for unpacked fixed-point value f to floating point is
342 * unpackUnorm4x8: f / 255.0
344 return (float) u
/ 255.0f
;
348 * Evaluate one component of unpackUnorm2x16.
351 unpack_unorm_1x16(uint16_t u
)
353 /* From section 8.4 of the GLSL ES 3.00 spec:
357 * The conversion for unpacked fixed-point value f to floating point is
360 * unpackUnorm2x16: f / 65535.0
362 return (float) u
/ 65535.0f
;
366 * Evaluate one component of packHalf2x16.
369 pack_half_1x16(float x
)
371 return _mesa_float_to_half(x
);
375 * Evaluate one component of unpackHalf2x16.
378 unpack_half_1x16(uint16_t u
)
380 return _mesa_half_to_float(u
);
384 ir_rvalue::constant_expression_value(struct hash_table
*variable_context
)
386 assert(this->type
->is_error());
391 ir_expression::constant_expression_value(struct hash_table
*variable_context
)
393 if (this->type
->is_error())
396 ir_constant
*op
[Elements(this->operands
)] = { NULL
, };
397 ir_constant_data data
;
399 memset(&data
, 0, sizeof(data
));
401 for (unsigned operand
= 0; operand
< this->get_num_operands(); operand
++) {
402 op
[operand
] = this->operands
[operand
]->constant_expression_value(variable_context
);
408 switch (this->operation
) {
409 case ir_binop_lshift
:
410 case ir_binop_rshift
:
412 case ir_binop_vector_extract
:
414 case ir_triop_bitfield_extract
:
418 assert(op
[0]->type
->base_type
== op
[1]->type
->base_type
);
422 bool op0_scalar
= op
[0]->type
->is_scalar();
423 bool op1_scalar
= op
[1] != NULL
&& op
[1]->type
->is_scalar();
425 /* When iterating over a vector or matrix's components, we want to increase
426 * the loop counter. However, for scalars, we want to stay at 0.
428 unsigned c0_inc
= op0_scalar
? 0 : 1;
429 unsigned c1_inc
= op1_scalar
? 0 : 1;
431 if (op1_scalar
|| !op
[1]) {
432 components
= op
[0]->type
->components();
434 components
= op
[1]->type
->components();
437 void *ctx
= ralloc_parent(this);
439 /* Handle array operations here, rather than below. */
440 if (op
[0]->type
->is_array()) {
441 assert(op
[1] != NULL
&& op
[1]->type
->is_array());
442 switch (this->operation
) {
443 case ir_binop_all_equal
:
444 return new(ctx
) ir_constant(op
[0]->has_value(op
[1]));
445 case ir_binop_any_nequal
:
446 return new(ctx
) ir_constant(!op
[0]->has_value(op
[1]));
453 switch (this->operation
) {
454 case ir_unop_bit_not
:
455 switch (op
[0]->type
->base_type
) {
457 for (unsigned c
= 0; c
< components
; c
++)
458 data
.i
[c
] = ~ op
[0]->value
.i
[c
];
461 for (unsigned c
= 0; c
< components
; c
++)
462 data
.u
[c
] = ~ op
[0]->value
.u
[c
];
469 case ir_unop_logic_not
:
470 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
471 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
472 data
.b
[c
] = !op
[0]->value
.b
[c
];
476 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
477 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
478 data
.i
[c
] = (int) op
[0]->value
.f
[c
];
482 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
483 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
484 data
.i
[c
] = (unsigned) op
[0]->value
.f
[c
];
488 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
489 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
490 data
.f
[c
] = (float) op
[0]->value
.i
[c
];
494 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
495 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
496 data
.f
[c
] = (float) op
[0]->value
.u
[c
];
500 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
501 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
502 data
.f
[c
] = op
[0]->value
.b
[c
] ? 1.0F
: 0.0F
;
506 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
507 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
508 data
.b
[c
] = op
[0]->value
.f
[c
] != 0.0F
? true : false;
512 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
513 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
514 data
.u
[c
] = op
[0]->value
.b
[c
] ? 1 : 0;
518 assert(op
[0]->type
->is_integer());
519 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
520 data
.b
[c
] = op
[0]->value
.u
[c
] ? true : false;
524 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
525 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
526 data
.i
[c
] = op
[0]->value
.u
[c
];
530 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
531 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
532 data
.u
[c
] = op
[0]->value
.i
[c
];
535 case ir_unop_bitcast_i2f
:
536 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
537 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
538 data
.f
[c
] = bitcast_u2f(op
[0]->value
.i
[c
]);
541 case ir_unop_bitcast_f2i
:
542 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
543 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
544 data
.i
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
547 case ir_unop_bitcast_u2f
:
548 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
549 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
550 data
.f
[c
] = bitcast_u2f(op
[0]->value
.u
[c
]);
553 case ir_unop_bitcast_f2u
:
554 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
555 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
556 data
.u
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
560 assert(op
[0]->type
->is_boolean());
562 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
563 if (op
[0]->value
.b
[c
])
569 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
570 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
571 data
.f
[c
] = truncf(op
[0]->value
.f
[c
]);
575 case ir_unop_round_even
:
576 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
577 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
578 data
.f
[c
] = _mesa_round_to_even(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
.f
[c
] = ceilf(op
[0]->value
.f
[c
]);
590 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
591 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
592 data
.f
[c
] = floorf(op
[0]->value
.f
[c
]);
597 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
598 switch (this->type
->base_type
) {
605 case GLSL_TYPE_FLOAT
:
606 data
.f
[c
] = op
[0]->value
.f
[c
] - floor(op
[0]->value
.f
[c
]);
615 case ir_unop_sin_reduced
:
616 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
617 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
618 data
.f
[c
] = sinf(op
[0]->value
.f
[c
]);
623 case ir_unop_cos_reduced
:
624 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
625 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
626 data
.f
[c
] = cosf(op
[0]->value
.f
[c
]);
631 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
632 switch (this->type
->base_type
) {
634 data
.u
[c
] = -((int) op
[0]->value
.u
[c
]);
637 data
.i
[c
] = -op
[0]->value
.i
[c
];
639 case GLSL_TYPE_FLOAT
:
640 data
.f
[c
] = -op
[0]->value
.f
[c
];
649 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
650 switch (this->type
->base_type
) {
652 data
.u
[c
] = op
[0]->value
.u
[c
];
655 data
.i
[c
] = op
[0]->value
.i
[c
];
657 data
.i
[c
] = -data
.i
[c
];
659 case GLSL_TYPE_FLOAT
:
660 data
.f
[c
] = fabs(op
[0]->value
.f
[c
]);
669 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
670 switch (this->type
->base_type
) {
672 data
.u
[c
] = op
[0]->value
.i
[c
] > 0;
675 data
.i
[c
] = (op
[0]->value
.i
[c
] > 0) - (op
[0]->value
.i
[c
] < 0);
677 case GLSL_TYPE_FLOAT
:
678 data
.f
[c
] = float((op
[0]->value
.f
[c
] > 0)-(op
[0]->value
.f
[c
] < 0));
687 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
688 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
689 switch (this->type
->base_type
) {
691 if (op
[0]->value
.u
[c
] != 0.0)
692 data
.u
[c
] = 1 / op
[0]->value
.u
[c
];
695 if (op
[0]->value
.i
[c
] != 0.0)
696 data
.i
[c
] = 1 / op
[0]->value
.i
[c
];
698 case GLSL_TYPE_FLOAT
:
699 if (op
[0]->value
.f
[c
] != 0.0)
700 data
.f
[c
] = 1.0F
/ op
[0]->value
.f
[c
];
709 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
710 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
711 data
.f
[c
] = 1.0F
/ sqrtf(op
[0]->value
.f
[c
]);
716 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
717 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
718 data
.f
[c
] = sqrtf(op
[0]->value
.f
[c
]);
723 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
724 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
725 data
.f
[c
] = expf(op
[0]->value
.f
[c
]);
730 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
731 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
732 data
.f
[c
] = exp2f(op
[0]->value
.f
[c
]);
737 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
738 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
739 data
.f
[c
] = logf(op
[0]->value
.f
[c
]);
744 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
745 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
746 data
.f
[c
] = log2f(op
[0]->value
.f
[c
]);
752 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
753 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
758 case ir_unop_pack_snorm_2x16
:
759 assert(op
[0]->type
== glsl_type::vec2_type
);
760 data
.u
[0] = pack_2x16(pack_snorm_1x16
,
764 case ir_unop_pack_snorm_4x8
:
765 assert(op
[0]->type
== glsl_type::vec4_type
);
766 data
.u
[0] = pack_4x8(pack_snorm_1x8
,
772 case ir_unop_unpack_snorm_2x16
:
773 assert(op
[0]->type
== glsl_type::uint_type
);
774 unpack_2x16(unpack_snorm_1x16
,
776 &data
.f
[0], &data
.f
[1]);
778 case ir_unop_unpack_snorm_4x8
:
779 assert(op
[0]->type
== glsl_type::uint_type
);
780 unpack_4x8(unpack_snorm_1x8
,
782 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
784 case ir_unop_pack_unorm_2x16
:
785 assert(op
[0]->type
== glsl_type::vec2_type
);
786 data
.u
[0] = pack_2x16(pack_unorm_1x16
,
790 case ir_unop_pack_unorm_4x8
:
791 assert(op
[0]->type
== glsl_type::vec4_type
);
792 data
.u
[0] = pack_4x8(pack_unorm_1x8
,
798 case ir_unop_unpack_unorm_2x16
:
799 assert(op
[0]->type
== glsl_type::uint_type
);
800 unpack_2x16(unpack_unorm_1x16
,
802 &data
.f
[0], &data
.f
[1]);
804 case ir_unop_unpack_unorm_4x8
:
805 assert(op
[0]->type
== glsl_type::uint_type
);
806 unpack_4x8(unpack_unorm_1x8
,
808 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
810 case ir_unop_pack_half_2x16
:
811 assert(op
[0]->type
== glsl_type::vec2_type
);
812 data
.u
[0] = pack_2x16(pack_half_1x16
,
816 case ir_unop_unpack_half_2x16
:
817 assert(op
[0]->type
== glsl_type::uint_type
);
818 unpack_2x16(unpack_half_1x16
,
820 &data
.f
[0], &data
.f
[1]);
823 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
824 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
825 data
.f
[c
] = powf(op
[0]->value
.f
[c
], op
[1]->value
.f
[c
]);
830 data
.f
[0] = dot(op
[0], op
[1]);
834 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
835 for (unsigned c
= 0, c0
= 0, c1
= 0;
837 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
839 switch (op
[0]->type
->base_type
) {
841 data
.u
[c
] = MIN2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
844 data
.i
[c
] = MIN2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
846 case GLSL_TYPE_FLOAT
:
847 data
.f
[c
] = MIN2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
856 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
857 for (unsigned c
= 0, c0
= 0, c1
= 0;
859 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
861 switch (op
[0]->type
->base_type
) {
863 data
.u
[c
] = MAX2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
866 data
.i
[c
] = MAX2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
868 case GLSL_TYPE_FLOAT
:
869 data
.f
[c
] = MAX2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
878 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
879 for (unsigned c
= 0, c0
= 0, c1
= 0;
881 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
883 switch (op
[0]->type
->base_type
) {
885 data
.u
[c
] = op
[0]->value
.u
[c0
] + op
[1]->value
.u
[c1
];
888 data
.i
[c
] = op
[0]->value
.i
[c0
] + op
[1]->value
.i
[c1
];
890 case GLSL_TYPE_FLOAT
:
891 data
.f
[c
] = op
[0]->value
.f
[c0
] + op
[1]->value
.f
[c1
];
900 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
901 for (unsigned c
= 0, c0
= 0, c1
= 0;
903 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
905 switch (op
[0]->type
->base_type
) {
907 data
.u
[c
] = op
[0]->value
.u
[c0
] - op
[1]->value
.u
[c1
];
910 data
.i
[c
] = op
[0]->value
.i
[c0
] - op
[1]->value
.i
[c1
];
912 case GLSL_TYPE_FLOAT
:
913 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
];
922 /* Check for equal types, or unequal types involving scalars */
923 if ((op
[0]->type
== op
[1]->type
&& !op
[0]->type
->is_matrix())
924 || op0_scalar
|| op1_scalar
) {
925 for (unsigned c
= 0, c0
= 0, c1
= 0;
927 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
929 switch (op
[0]->type
->base_type
) {
931 data
.u
[c
] = op
[0]->value
.u
[c0
] * op
[1]->value
.u
[c1
];
934 data
.i
[c
] = op
[0]->value
.i
[c0
] * op
[1]->value
.i
[c1
];
936 case GLSL_TYPE_FLOAT
:
937 data
.f
[c
] = op
[0]->value
.f
[c0
] * op
[1]->value
.f
[c1
];
944 assert(op
[0]->type
->is_matrix() || op
[1]->type
->is_matrix());
946 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
947 * matrix can be a GLSL vector, either N or P can be 1.
949 * For vec*mat, the vector is treated as a row vector. This
950 * means the vector is a 1-row x M-column matrix.
952 * For mat*vec, the vector is treated as a column vector. Since
953 * matrix_columns is 1 for vectors, this just works.
955 const unsigned n
= op
[0]->type
->is_vector()
956 ? 1 : op
[0]->type
->vector_elements
;
957 const unsigned m
= op
[1]->type
->vector_elements
;
958 const unsigned p
= op
[1]->type
->matrix_columns
;
959 for (unsigned j
= 0; j
< p
; j
++) {
960 for (unsigned i
= 0; i
< n
; i
++) {
961 for (unsigned k
= 0; k
< m
; k
++) {
962 data
.f
[i
+n
*j
] += op
[0]->value
.f
[i
+n
*k
]*op
[1]->value
.f
[k
+m
*j
];
970 /* FINISHME: Emit warning when division-by-zero is detected. */
971 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
972 for (unsigned c
= 0, c0
= 0, c1
= 0;
974 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
976 switch (op
[0]->type
->base_type
) {
978 if (op
[1]->value
.u
[c1
] == 0) {
981 data
.u
[c
] = op
[0]->value
.u
[c0
] / op
[1]->value
.u
[c1
];
985 if (op
[1]->value
.i
[c1
] == 0) {
988 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 /* FINISHME: Emit warning when division-by-zero is detected. */
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 if (op
[1]->value
.u
[c1
] == 0) {
1012 data
.u
[c
] = op
[0]->value
.u
[c0
] % op
[1]->value
.u
[c1
];
1016 if (op
[1]->value
.i
[c1
] == 0) {
1019 data
.i
[c
] = op
[0]->value
.i
[c0
] % op
[1]->value
.i
[c1
];
1022 case GLSL_TYPE_FLOAT
:
1023 /* We don't use fmod because it rounds toward zero; GLSL specifies
1026 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
]
1027 * floorf(op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
]);
1036 case ir_binop_logic_and
:
1037 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1038 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1039 data
.b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
1041 case ir_binop_logic_xor
:
1042 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1043 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1044 data
.b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
1046 case ir_binop_logic_or
:
1047 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1048 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1049 data
.b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
1053 assert(op
[0]->type
== op
[1]->type
);
1054 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1055 switch (op
[0]->type
->base_type
) {
1056 case GLSL_TYPE_UINT
:
1057 data
.b
[c
] = op
[0]->value
.u
[c
] < op
[1]->value
.u
[c
];
1060 data
.b
[c
] = op
[0]->value
.i
[c
] < op
[1]->value
.i
[c
];
1062 case GLSL_TYPE_FLOAT
:
1063 data
.b
[c
] = op
[0]->value
.f
[c
] < op
[1]->value
.f
[c
];
1070 case ir_binop_greater
:
1071 assert(op
[0]->type
== op
[1]->type
);
1072 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1073 switch (op
[0]->type
->base_type
) {
1074 case GLSL_TYPE_UINT
:
1075 data
.b
[c
] = op
[0]->value
.u
[c
] > op
[1]->value
.u
[c
];
1078 data
.b
[c
] = op
[0]->value
.i
[c
] > op
[1]->value
.i
[c
];
1080 case GLSL_TYPE_FLOAT
:
1081 data
.b
[c
] = op
[0]->value
.f
[c
] > op
[1]->value
.f
[c
];
1088 case ir_binop_lequal
:
1089 assert(op
[0]->type
== op
[1]->type
);
1090 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1091 switch (op
[0]->type
->base_type
) {
1092 case GLSL_TYPE_UINT
:
1093 data
.b
[c
] = op
[0]->value
.u
[c
] <= op
[1]->value
.u
[c
];
1096 data
.b
[c
] = op
[0]->value
.i
[c
] <= op
[1]->value
.i
[c
];
1098 case GLSL_TYPE_FLOAT
:
1099 data
.b
[c
] = op
[0]->value
.f
[c
] <= op
[1]->value
.f
[c
];
1106 case ir_binop_gequal
:
1107 assert(op
[0]->type
== op
[1]->type
);
1108 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1109 switch (op
[0]->type
->base_type
) {
1110 case GLSL_TYPE_UINT
:
1111 data
.b
[c
] = op
[0]->value
.u
[c
] >= op
[1]->value
.u
[c
];
1114 data
.b
[c
] = op
[0]->value
.i
[c
] >= op
[1]->value
.i
[c
];
1116 case GLSL_TYPE_FLOAT
:
1117 data
.b
[c
] = op
[0]->value
.f
[c
] >= op
[1]->value
.f
[c
];
1124 case ir_binop_equal
:
1125 assert(op
[0]->type
== op
[1]->type
);
1126 for (unsigned c
= 0; c
< components
; c
++) {
1127 switch (op
[0]->type
->base_type
) {
1128 case GLSL_TYPE_UINT
:
1129 data
.b
[c
] = op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
1132 data
.b
[c
] = op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
1134 case GLSL_TYPE_FLOAT
:
1135 data
.b
[c
] = op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
1137 case GLSL_TYPE_BOOL
:
1138 data
.b
[c
] = op
[0]->value
.b
[c
] == op
[1]->value
.b
[c
];
1145 case ir_binop_nequal
:
1146 assert(op
[0]->type
== op
[1]->type
);
1147 for (unsigned c
= 0; c
< components
; c
++) {
1148 switch (op
[0]->type
->base_type
) {
1149 case GLSL_TYPE_UINT
:
1150 data
.b
[c
] = op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
1153 data
.b
[c
] = op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
1155 case GLSL_TYPE_FLOAT
:
1156 data
.b
[c
] = op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
1158 case GLSL_TYPE_BOOL
:
1159 data
.b
[c
] = op
[0]->value
.b
[c
] != op
[1]->value
.b
[c
];
1166 case ir_binop_all_equal
:
1167 data
.b
[0] = op
[0]->has_value(op
[1]);
1169 case ir_binop_any_nequal
:
1170 data
.b
[0] = !op
[0]->has_value(op
[1]);
1173 case ir_binop_lshift
:
1174 for (unsigned c
= 0, c0
= 0, c1
= 0;
1176 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1178 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1179 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1180 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.i
[c1
];
1182 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1183 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1184 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.u
[c1
];
1186 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1187 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1188 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.i
[c1
];
1190 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1191 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1192 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.u
[c1
];
1197 case ir_binop_rshift
:
1198 for (unsigned c
= 0, c0
= 0, c1
= 0;
1200 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1202 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1203 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1204 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.i
[c1
];
1206 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1207 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1208 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.u
[c1
];
1210 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1211 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1212 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.i
[c1
];
1214 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1215 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1216 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.u
[c1
];
1221 case ir_binop_bit_and
:
1222 for (unsigned c
= 0, c0
= 0, c1
= 0;
1224 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1226 switch (op
[0]->type
->base_type
) {
1228 data
.i
[c
] = op
[0]->value
.i
[c0
] & op
[1]->value
.i
[c1
];
1230 case GLSL_TYPE_UINT
:
1231 data
.u
[c
] = op
[0]->value
.u
[c0
] & op
[1]->value
.u
[c1
];
1239 case ir_binop_bit_or
:
1240 for (unsigned c
= 0, c0
= 0, c1
= 0;
1242 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1244 switch (op
[0]->type
->base_type
) {
1246 data
.i
[c
] = op
[0]->value
.i
[c0
] | op
[1]->value
.i
[c1
];
1248 case GLSL_TYPE_UINT
:
1249 data
.u
[c
] = op
[0]->value
.u
[c0
] | op
[1]->value
.u
[c1
];
1257 case ir_binop_vector_extract
: {
1258 const int c
= CLAMP(op
[1]->value
.i
[0], 0,
1259 (int) op
[0]->type
->vector_elements
- 1);
1261 switch (op
[0]->type
->base_type
) {
1262 case GLSL_TYPE_UINT
:
1263 data
.u
[0] = op
[0]->value
.u
[c
];
1266 data
.i
[0] = op
[0]->value
.i
[c
];
1268 case GLSL_TYPE_FLOAT
:
1269 data
.f
[0] = op
[0]->value
.f
[c
];
1271 case GLSL_TYPE_BOOL
:
1272 data
.b
[0] = op
[0]->value
.b
[c
];
1280 case ir_binop_bit_xor
:
1281 for (unsigned c
= 0, c0
= 0, c1
= 0;
1283 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1285 switch (op
[0]->type
->base_type
) {
1287 data
.i
[c
] = op
[0]->value
.i
[c0
] ^ op
[1]->value
.i
[c1
];
1289 case GLSL_TYPE_UINT
:
1290 data
.u
[c
] = op
[0]->value
.u
[c0
] ^ op
[1]->value
.u
[c1
];
1298 case ir_unop_bitfield_reverse
:
1299 /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
1300 for (unsigned c
= 0; c
< components
; c
++) {
1301 unsigned int v
= op
[0]->value
.u
[c
]; // input bits to be reversed
1302 unsigned int r
= v
; // r will be reversed bits of v; first get LSB of v
1303 int s
= sizeof(v
) * CHAR_BIT
- 1; // extra shift needed at end
1305 for (v
>>= 1; v
; v
>>= 1) {
1310 r
<<= s
; // shift when v's highest bits are zero
1316 case ir_unop_bit_count
:
1317 for (unsigned c
= 0; c
< components
; c
++) {
1319 unsigned v
= op
[0]->value
.u
[c
];
1321 for (; v
; count
++) {
1328 case ir_unop_find_msb
:
1329 for (unsigned c
= 0; c
< components
; c
++) {
1330 int v
= op
[0]->value
.i
[c
];
1332 if (v
== 0 || (op
[0]->type
->base_type
== GLSL_TYPE_INT
&& v
== -1))
1336 int top_bit
= op
[0]->type
->base_type
== GLSL_TYPE_UINT
1337 ? 0 : v
& (1 << 31);
1339 while (((v
& (1 << 31)) == top_bit
) && count
!= 32) {
1344 data
.i
[c
] = 31 - count
;
1349 case ir_unop_find_lsb
:
1350 for (unsigned c
= 0; c
< components
; c
++) {
1351 if (op
[0]->value
.i
[c
] == 0)
1355 unsigned v
= op
[0]->value
.u
[c
];
1357 for (; !(v
& 1); v
>>= 1) {
1365 case ir_triop_bitfield_extract
: {
1366 int offset
= op
[1]->value
.i
[0];
1367 int bits
= op
[2]->value
.i
[0];
1369 for (unsigned c
= 0; c
< components
; c
++) {
1372 else if (offset
< 0 || bits
< 0)
1373 data
.u
[c
] = 0; /* Undefined, per spec. */
1374 else if (offset
+ bits
> 32)
1375 data
.u
[c
] = 0; /* Undefined, per spec. */
1377 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
) {
1378 /* int so that the right shift will sign-extend. */
1379 int value
= op
[0]->value
.i
[c
];
1380 value
<<= 32 - bits
- offset
;
1381 value
>>= 32 - bits
;
1384 unsigned value
= op
[0]->value
.u
[c
];
1385 value
<<= 32 - bits
- offset
;
1386 value
>>= 32 - bits
;
1394 case ir_binop_ldexp
:
1395 for (unsigned c
= 0; c
< components
; c
++) {
1396 data
.f
[c
] = ldexp(op
[0]->value
.f
[c
], op
[1]->value
.i
[c
]);
1397 /* Flush subnormal values to zero. */
1398 if (!isnormal(data
.f
[c
]))
1399 data
.f
[c
] = copysign(0.0, op
[0]->value
.f
[c
]);
1404 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1405 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
);
1406 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
);
1408 for (unsigned c
= 0; c
< components
; c
++) {
1409 data
.f
[c
] = op
[0]->value
.f
[c
] * op
[1]->value
.f
[c
]
1410 + op
[2]->value
.f
[c
];
1414 case ir_triop_lrp
: {
1415 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1416 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
);
1417 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
);
1419 unsigned c2_inc
= op
[2]->type
->is_scalar() ? 0 : 1;
1420 for (unsigned c
= 0, c2
= 0; c
< components
; c2
+= c2_inc
, c
++) {
1421 data
.f
[c
] = op
[0]->value
.f
[c
] * (1.0f
- op
[2]->value
.f
[c2
]) +
1422 (op
[1]->value
.f
[c
] * op
[2]->value
.f
[c2
]);
1428 for (unsigned c
= 0; c
< components
; c
++) {
1429 data
.u
[c
] = op
[0]->value
.b
[c
] ? op
[1]->value
.u
[c
]
1430 : op
[2]->value
.u
[c
];
1434 case ir_triop_vector_insert
: {
1435 const unsigned idx
= op
[2]->value
.u
[0];
1437 memcpy(&data
, &op
[0]->value
, sizeof(data
));
1439 switch (this->type
->base_type
) {
1441 data
.i
[idx
] = op
[1]->value
.i
[0];
1443 case GLSL_TYPE_UINT
:
1444 data
.u
[idx
] = op
[1]->value
.u
[0];
1446 case GLSL_TYPE_FLOAT
:
1447 data
.f
[idx
] = op
[1]->value
.f
[0];
1449 case GLSL_TYPE_BOOL
:
1450 data
.b
[idx
] = op
[1]->value
.b
[0];
1453 assert(!"Should not get here.");
1459 case ir_quadop_bitfield_insert
: {
1460 int offset
= op
[2]->value
.i
[0];
1461 int bits
= op
[3]->value
.i
[0];
1463 for (unsigned c
= 0; c
< components
; c
++) {
1465 data
.u
[c
] = op
[0]->value
.u
[c
];
1466 else if (offset
< 0 || bits
< 0)
1467 data
.u
[c
] = 0; /* Undefined, per spec. */
1468 else if (offset
+ bits
> 32)
1469 data
.u
[c
] = 0; /* Undefined, per spec. */
1471 unsigned insert_mask
= ((1 << bits
) - 1) << offset
;
1473 unsigned insert
= op
[1]->value
.u
[c
];
1475 insert
&= insert_mask
;
1477 unsigned base
= op
[0]->value
.u
[c
];
1478 base
&= ~insert_mask
;
1480 data
.u
[c
] = base
| insert
;
1486 case ir_quadop_vector
:
1487 for (unsigned c
= 0; c
< this->type
->vector_elements
; c
++) {
1488 switch (this->type
->base_type
) {
1490 data
.i
[c
] = op
[c
]->value
.i
[0];
1492 case GLSL_TYPE_UINT
:
1493 data
.u
[c
] = op
[c
]->value
.u
[0];
1495 case GLSL_TYPE_FLOAT
:
1496 data
.f
[c
] = op
[c
]->value
.f
[0];
1505 /* FINISHME: Should handle all expression types. */
1509 return new(ctx
) ir_constant(this->type
, &data
);
1514 ir_texture::constant_expression_value(struct hash_table
*variable_context
)
1516 /* texture lookups aren't constant expressions */
1522 ir_swizzle::constant_expression_value(struct hash_table
*variable_context
)
1524 ir_constant
*v
= this->val
->constant_expression_value(variable_context
);
1527 ir_constant_data data
= { { 0 } };
1529 const unsigned swiz_idx
[4] = {
1530 this->mask
.x
, this->mask
.y
, this->mask
.z
, this->mask
.w
1533 for (unsigned i
= 0; i
< this->mask
.num_components
; i
++) {
1534 switch (v
->type
->base_type
) {
1535 case GLSL_TYPE_UINT
:
1536 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
1537 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
1538 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
1539 default: assert(!"Should not get here."); break;
1543 void *ctx
= ralloc_parent(this);
1544 return new(ctx
) ir_constant(this->type
, &data
);
1551 ir_dereference_variable::constant_referenced(struct hash_table
*variable_context
,
1552 ir_constant
*&store
, int &offset
) const
1554 if (variable_context
) {
1555 store
= (ir_constant
*)hash_table_find(variable_context
, var
);
1564 ir_dereference_variable::constant_expression_value(struct hash_table
*variable_context
)
1566 /* This may occur during compile and var->type is glsl_type::error_type */
1570 /* Give priority to the context hashtable, if it exists */
1571 if (variable_context
) {
1572 ir_constant
*value
= (ir_constant
*)hash_table_find(variable_context
, var
);
1577 /* The constant_value of a uniform variable is its initializer,
1578 * not the lifetime constant value of the uniform.
1580 if (var
->mode
== ir_var_uniform
)
1583 if (!var
->constant_value
)
1586 return var
->constant_value
->clone(ralloc_parent(var
), NULL
);
1591 ir_dereference_array::constant_referenced(struct hash_table
*variable_context
,
1592 ir_constant
*&store
, int &offset
) const
1594 ir_constant
*index_c
= array_index
->constant_expression_value(variable_context
);
1596 if (!index_c
|| !index_c
->type
->is_scalar() || !index_c
->type
->is_integer()) {
1602 int index
= index_c
->type
->base_type
== GLSL_TYPE_INT
?
1603 index_c
->get_int_component(0) :
1604 index_c
->get_uint_component(0);
1606 ir_constant
*substore
;
1608 const ir_dereference
*deref
= array
->as_dereference();
1615 deref
->constant_referenced(variable_context
, substore
, suboffset
);
1623 const glsl_type
*vt
= array
->type
;
1624 if (vt
->is_array()) {
1625 store
= substore
->get_array_element(index
);
1629 if (vt
->is_matrix()) {
1631 offset
= index
* vt
->vector_elements
;
1634 if (vt
->is_vector()) {
1636 offset
= suboffset
+ index
;
1645 ir_dereference_array::constant_expression_value(struct hash_table
*variable_context
)
1647 ir_constant
*array
= this->array
->constant_expression_value(variable_context
);
1648 ir_constant
*idx
= this->array_index
->constant_expression_value(variable_context
);
1650 if ((array
!= NULL
) && (idx
!= NULL
)) {
1651 void *ctx
= ralloc_parent(this);
1652 if (array
->type
->is_matrix()) {
1653 /* Array access of a matrix results in a vector.
1655 const unsigned column
= idx
->value
.u
[0];
1657 const glsl_type
*const column_type
= array
->type
->column_type();
1659 /* Offset in the constant matrix to the first element of the column
1662 const unsigned mat_idx
= column
* column_type
->vector_elements
;
1664 ir_constant_data data
= { { 0 } };
1666 switch (column_type
->base_type
) {
1667 case GLSL_TYPE_UINT
:
1669 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1670 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
1674 case GLSL_TYPE_FLOAT
:
1675 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1676 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
1681 assert(!"Should not get here.");
1685 return new(ctx
) ir_constant(column_type
, &data
);
1686 } else if (array
->type
->is_vector()) {
1687 const unsigned component
= idx
->value
.u
[0];
1689 return new(ctx
) ir_constant(array
, component
);
1691 const unsigned index
= idx
->value
.u
[0];
1692 return array
->get_array_element(index
)->clone(ctx
, NULL
);
1700 ir_dereference_record::constant_referenced(struct hash_table
*variable_context
,
1701 ir_constant
*&store
, int &offset
) const
1703 ir_constant
*substore
;
1705 const ir_dereference
*deref
= record
->as_dereference();
1712 deref
->constant_referenced(variable_context
, substore
, suboffset
);
1720 store
= substore
->get_record_field(field
);
1725 ir_dereference_record::constant_expression_value(struct hash_table
*variable_context
)
1727 ir_constant
*v
= this->record
->constant_expression_value();
1729 return (v
!= NULL
) ? v
->get_record_field(this->field
) : NULL
;
1734 ir_assignment::constant_expression_value(struct hash_table
*variable_context
)
1736 /* FINISHME: Handle CEs involving assignment (return RHS) */
1742 ir_constant::constant_expression_value(struct hash_table
*variable_context
)
1749 ir_call::constant_expression_value(struct hash_table
*variable_context
)
1751 return this->callee
->constant_expression_value(&this->actual_parameters
, variable_context
);
1755 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list
&body
,
1756 struct hash_table
*variable_context
,
1757 ir_constant
**result
)
1759 foreach_list(n
, &body
) {
1760 ir_instruction
*inst
= (ir_instruction
*)n
;
1761 switch(inst
->ir_type
) {
1763 /* (declare () type symbol) */
1764 case ir_type_variable
: {
1765 ir_variable
*var
= inst
->as_variable();
1766 hash_table_insert(variable_context
, ir_constant::zero(this, var
->type
), var
);
1770 /* (assign [condition] (write-mask) (ref) (value)) */
1771 case ir_type_assignment
: {
1772 ir_assignment
*asg
= inst
->as_assignment();
1773 if (asg
->condition
) {
1774 ir_constant
*cond
= asg
->condition
->constant_expression_value(variable_context
);
1777 if (!cond
->get_bool_component(0))
1781 ir_constant
*store
= NULL
;
1783 asg
->lhs
->constant_referenced(variable_context
, store
, offset
);
1788 ir_constant
*value
= asg
->rhs
->constant_expression_value(variable_context
);
1793 store
->copy_masked_offset(value
, offset
, asg
->write_mask
);
1797 /* (return (expression)) */
1798 case ir_type_return
:
1800 *result
= inst
->as_return()->value
->constant_expression_value(variable_context
);
1801 return *result
!= NULL
;
1803 /* (call name (ref) (params))*/
1804 case ir_type_call
: {
1805 ir_call
*call
= inst
->as_call();
1807 /* Just say no to void functions in constant expressions. We
1808 * don't need them at that point.
1811 if (!call
->return_deref
)
1814 ir_constant
*store
= NULL
;
1816 call
->return_deref
->constant_referenced(variable_context
, store
, offset
);
1821 ir_constant
*value
= call
->constant_expression_value(variable_context
);
1826 store
->copy_offset(value
, offset
);
1830 /* (if condition (then-instructions) (else-instructions)) */
1832 ir_if
*iif
= inst
->as_if();
1834 ir_constant
*cond
= iif
->condition
->constant_expression_value(variable_context
);
1835 if (!cond
|| !cond
->type
->is_boolean())
1838 exec_list
&branch
= cond
->get_bool_component(0) ? iif
->then_instructions
: iif
->else_instructions
;
1841 if (!constant_expression_evaluate_expression_list(branch
, variable_context
, result
))
1844 /* If there was a return in the branch chosen, drop out now. */
1851 /* Every other expression type, we drop out. */
1857 /* Reaching the end of the block is not an error condition */
1865 ir_function_signature::constant_expression_value(exec_list
*actual_parameters
, struct hash_table
*variable_context
)
1867 const glsl_type
*type
= this->return_type
;
1868 if (type
== glsl_type::void_type
)
1871 /* From the GLSL 1.20 spec, page 23:
1872 * "Function calls to user-defined functions (non-built-in functions)
1873 * cannot be used to form constant expressions."
1875 if (!this->is_builtin())
1879 * Of the builtin functions, only the texture lookups and the noise
1880 * ones must not be used in constant expressions. They all include
1881 * specific opcodes so they don't need to be special-cased at this
1885 /* Initialize the table of dereferencable names with the function
1886 * parameters. Verify their const-ness on the way.
1888 * We expect the correctness of the number of parameters to have
1889 * been checked earlier.
1891 hash_table
*deref_hash
= hash_table_ctor(8, hash_table_pointer_hash
,
1892 hash_table_pointer_compare
);
1894 /* If "origin" is non-NULL, then the function body is there. So we
1895 * have to use the variable objects from the object with the body,
1896 * but the parameter instanciation on the current object.
1898 const exec_node
*parameter_info
= origin
? origin
->parameters
.head
: parameters
.head
;
1900 foreach_list(n
, actual_parameters
) {
1901 ir_constant
*constant
= ((ir_rvalue
*) n
)->constant_expression_value(variable_context
);
1902 if (constant
== NULL
) {
1903 hash_table_dtor(deref_hash
);
1908 ir_variable
*var
= (ir_variable
*)parameter_info
;
1909 hash_table_insert(deref_hash
, constant
, var
);
1911 parameter_info
= parameter_info
->next
;
1914 ir_constant
*result
= NULL
;
1916 /* Now run the builtin function until something non-constant
1917 * happens or we get the result.
1919 if (constant_expression_evaluate_expression_list(origin
? origin
->body
: body
, deref_hash
, &result
) && result
)
1920 result
= result
->clone(ralloc_parent(this), NULL
);
1922 hash_table_dtor(deref_hash
);