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"
44 dot(ir_constant
*op0
, ir_constant
*op1
)
46 assert(op0
->type
->is_float() && op1
->type
->is_float());
49 for (unsigned c
= 0; c
< op0
->type
->components(); c
++)
50 result
+= op0
->value
.f
[c
] * op1
->value
.f
[c
];
55 /* This method is the only one supported by gcc. Unions in particular
56 * are iffy, and read-through-converted-pointer is killed by strict
57 * aliasing. OTOH, the compiler sees through the memcpy, so the
58 * resulting asm is reasonable.
61 bitcast_u2f(unsigned int u
)
63 assert(sizeof(float) == sizeof(unsigned int));
65 memcpy(&f
, &u
, sizeof(f
));
72 assert(sizeof(float) == sizeof(unsigned int));
74 memcpy(&u
, &f
, sizeof(f
));
79 * Evaluate one component of a floating-point 4x8 unpacking function.
82 (*pack_1x8_func_t
)(float);
85 * Evaluate one component of a floating-point 2x16 unpacking function.
88 (*pack_1x16_func_t
)(float);
91 * Evaluate one component of a floating-point 4x8 unpacking function.
94 (*unpack_1x8_func_t
)(uint8_t);
97 * Evaluate one component of a floating-point 2x16 unpacking function.
100 (*unpack_1x16_func_t
)(uint16_t);
103 * Evaluate a 2x16 floating-point packing function.
106 pack_2x16(pack_1x16_func_t pack_1x16
,
109 /* From section 8.4 of the GLSL ES 3.00 spec:
113 * The first component of the vector will be written to the least
114 * significant bits of the output; the last component will be written to
115 * the most significant bits.
117 * The specifications for the other packing functions contain similar
121 u
|= ((uint32_t) pack_1x16(x
) << 0);
122 u
|= ((uint32_t) pack_1x16(y
) << 16);
127 * Evaluate a 4x8 floating-point packing function.
130 pack_4x8(pack_1x8_func_t pack_1x8
,
131 float x
, float y
, float z
, float w
)
133 /* From section 8.4 of the GLSL 4.30 spec:
137 * The first component of the vector will be written to the least
138 * significant bits of the output; the last component will be written to
139 * the most significant bits.
141 * The specifications for the other packing functions contain similar
145 u
|= ((uint32_t) pack_1x8(x
) << 0);
146 u
|= ((uint32_t) pack_1x8(y
) << 8);
147 u
|= ((uint32_t) pack_1x8(z
) << 16);
148 u
|= ((uint32_t) pack_1x8(w
) << 24);
153 * Evaluate a 2x16 floating-point unpacking function.
156 unpack_2x16(unpack_1x16_func_t unpack_1x16
,
160 /* From section 8.4 of the GLSL ES 3.00 spec:
164 * The first component of the returned vector will be extracted from
165 * the least significant bits of the input; the last component will be
166 * extracted from the most significant bits.
168 * The specifications for the other unpacking functions contain similar
171 *x
= unpack_1x16((uint16_t) (u
& 0xffff));
172 *y
= unpack_1x16((uint16_t) (u
>> 16));
176 * Evaluate a 4x8 floating-point unpacking function.
179 unpack_4x8(unpack_1x8_func_t unpack_1x8
, uint32_t u
,
180 float *x
, float *y
, float *z
, float *w
)
182 /* From section 8.4 of the GLSL 4.30 spec:
186 * The first component of the returned vector will be extracted from
187 * the least significant bits of the input; the last component will be
188 * extracted from the most significant bits.
190 * The specifications for the other unpacking functions contain similar
193 *x
= unpack_1x8((uint8_t) (u
& 0xff));
194 *y
= unpack_1x8((uint8_t) (u
>> 8));
195 *z
= unpack_1x8((uint8_t) (u
>> 16));
196 *w
= unpack_1x8((uint8_t) (u
>> 24));
200 * Evaluate one component of packSnorm4x8.
203 pack_snorm_1x8(float x
)
205 /* From section 8.4 of the GLSL 4.30 spec:
209 * The conversion for component c of v to fixed point is done as
212 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
214 * We must first cast the float to an int, because casting a negative
215 * float to a uint is undefined.
217 return (uint8_t) (int8_t)
218 _mesa_round_to_even(CLAMP(x
, -1.0f
, +1.0f
) * 127.0f
);
222 * Evaluate one component of packSnorm2x16.
225 pack_snorm_1x16(float x
)
227 /* From section 8.4 of the GLSL ES 3.00 spec:
231 * The conversion for component c of v to fixed point is done as
234 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
236 * We must first cast the float to an int, because casting a negative
237 * float to a uint is undefined.
239 return (uint16_t) (int16_t)
240 _mesa_round_to_even(CLAMP(x
, -1.0f
, +1.0f
) * 32767.0f
);
244 * Evaluate one component of unpackSnorm4x8.
247 unpack_snorm_1x8(uint8_t u
)
249 /* From section 8.4 of the GLSL 4.30 spec:
253 * The conversion for unpacked fixed-point value f to floating point is
256 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
258 return CLAMP((int8_t) u
/ 127.0f
, -1.0f
, +1.0f
);
262 * Evaluate one component of unpackSnorm2x16.
265 unpack_snorm_1x16(uint16_t u
)
267 /* From section 8.4 of the GLSL ES 3.00 spec:
271 * The conversion for unpacked fixed-point value f to floating point is
274 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
276 return CLAMP((int16_t) u
/ 32767.0f
, -1.0f
, +1.0f
);
280 * Evaluate one component packUnorm4x8.
283 pack_unorm_1x8(float x
)
285 /* From section 8.4 of the GLSL 4.30 spec:
289 * The conversion for component c of v to fixed point is done as
292 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
294 return (uint8_t) _mesa_round_to_even(CLAMP(x
, 0.0f
, 1.0f
) * 255.0f
);
298 * Evaluate one component packUnorm2x16.
301 pack_unorm_1x16(float x
)
303 /* From section 8.4 of the GLSL ES 3.00 spec:
307 * The conversion for component c of v to fixed point is done as
310 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
312 return (uint16_t) _mesa_round_to_even(CLAMP(x
, 0.0f
, 1.0f
) * 65535.0f
);
316 * Evaluate one component of unpackUnorm4x8.
319 unpack_unorm_1x8(uint8_t u
)
321 /* From section 8.4 of the GLSL 4.30 spec:
325 * The conversion for unpacked fixed-point value f to floating point is
328 * unpackUnorm4x8: f / 255.0
330 return (float) u
/ 255.0f
;
334 * Evaluate one component of unpackUnorm2x16.
337 unpack_unorm_1x16(uint16_t u
)
339 /* From section 8.4 of the GLSL ES 3.00 spec:
343 * The conversion for unpacked fixed-point value f to floating point is
346 * unpackUnorm2x16: f / 65535.0
348 return (float) u
/ 65535.0f
;
352 * Evaluate one component of packHalf2x16.
355 pack_half_1x16(float x
)
357 return _mesa_float_to_half(x
);
361 * Evaluate one component of unpackHalf2x16.
364 unpack_half_1x16(uint16_t u
)
366 return _mesa_half_to_float(u
);
370 ir_rvalue::constant_expression_value(struct hash_table
*variable_context
)
372 assert(this->type
->is_error());
377 ir_expression::constant_expression_value(struct hash_table
*variable_context
)
379 if (this->type
->is_error())
382 ir_constant
*op
[Elements(this->operands
)] = { NULL
, };
383 ir_constant_data data
;
385 memset(&data
, 0, sizeof(data
));
387 for (unsigned operand
= 0; operand
< this->get_num_operands(); operand
++) {
388 op
[operand
] = this->operands
[operand
]->constant_expression_value(variable_context
);
394 switch (this->operation
) {
395 case ir_binop_lshift
:
396 case ir_binop_rshift
:
397 case ir_binop_vector_extract
:
398 case ir_triop_bitfield_extract
:
402 assert(op
[0]->type
->base_type
== op
[1]->type
->base_type
);
406 bool op0_scalar
= op
[0]->type
->is_scalar();
407 bool op1_scalar
= op
[1] != NULL
&& op
[1]->type
->is_scalar();
409 /* When iterating over a vector or matrix's components, we want to increase
410 * the loop counter. However, for scalars, we want to stay at 0.
412 unsigned c0_inc
= op0_scalar
? 0 : 1;
413 unsigned c1_inc
= op1_scalar
? 0 : 1;
415 if (op1_scalar
|| !op
[1]) {
416 components
= op
[0]->type
->components();
418 components
= op
[1]->type
->components();
421 void *ctx
= ralloc_parent(this);
423 /* Handle array operations here, rather than below. */
424 if (op
[0]->type
->is_array()) {
425 assert(op
[1] != NULL
&& op
[1]->type
->is_array());
426 switch (this->operation
) {
427 case ir_binop_all_equal
:
428 return new(ctx
) ir_constant(op
[0]->has_value(op
[1]));
429 case ir_binop_any_nequal
:
430 return new(ctx
) ir_constant(!op
[0]->has_value(op
[1]));
437 switch (this->operation
) {
438 case ir_unop_bit_not
:
439 switch (op
[0]->type
->base_type
) {
441 for (unsigned c
= 0; c
< components
; c
++)
442 data
.i
[c
] = ~ op
[0]->value
.i
[c
];
445 for (unsigned c
= 0; c
< components
; c
++)
446 data
.u
[c
] = ~ op
[0]->value
.u
[c
];
453 case ir_unop_logic_not
:
454 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
455 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
456 data
.b
[c
] = !op
[0]->value
.b
[c
];
460 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
461 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
462 data
.i
[c
] = (int) op
[0]->value
.f
[c
];
466 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
467 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
468 data
.i
[c
] = (unsigned) op
[0]->value
.f
[c
];
472 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
473 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
474 data
.f
[c
] = (float) op
[0]->value
.i
[c
];
478 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
479 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
480 data
.f
[c
] = (float) op
[0]->value
.u
[c
];
484 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
485 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
486 data
.f
[c
] = op
[0]->value
.b
[c
] ? 1.0F
: 0.0F
;
490 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
491 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
492 data
.b
[c
] = op
[0]->value
.f
[c
] != 0.0F
? true : false;
496 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
497 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
498 data
.u
[c
] = op
[0]->value
.b
[c
] ? 1 : 0;
502 assert(op
[0]->type
->is_integer());
503 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
504 data
.b
[c
] = op
[0]->value
.u
[c
] ? true : false;
508 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
509 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
510 data
.i
[c
] = op
[0]->value
.u
[c
];
514 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
515 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
516 data
.u
[c
] = op
[0]->value
.i
[c
];
519 case ir_unop_bitcast_i2f
:
520 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
521 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
522 data
.f
[c
] = bitcast_u2f(op
[0]->value
.i
[c
]);
525 case ir_unop_bitcast_f2i
:
526 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
527 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
528 data
.i
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
531 case ir_unop_bitcast_u2f
:
532 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
533 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
534 data
.f
[c
] = bitcast_u2f(op
[0]->value
.u
[c
]);
537 case ir_unop_bitcast_f2u
:
538 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
539 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
540 data
.u
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
544 assert(op
[0]->type
->is_boolean());
546 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
547 if (op
[0]->value
.b
[c
])
553 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
554 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
555 data
.f
[c
] = truncf(op
[0]->value
.f
[c
]);
559 case ir_unop_round_even
:
560 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
561 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
562 data
.f
[c
] = _mesa_round_to_even(op
[0]->value
.f
[c
]);
567 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
568 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
569 data
.f
[c
] = ceilf(op
[0]->value
.f
[c
]);
574 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
575 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
576 data
.f
[c
] = floorf(op
[0]->value
.f
[c
]);
581 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
582 switch (this->type
->base_type
) {
589 case GLSL_TYPE_FLOAT
:
590 data
.f
[c
] = op
[0]->value
.f
[c
] - floor(op
[0]->value
.f
[c
]);
599 case ir_unop_sin_reduced
:
600 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
601 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
602 data
.f
[c
] = sinf(op
[0]->value
.f
[c
]);
607 case ir_unop_cos_reduced
:
608 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
609 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
610 data
.f
[c
] = cosf(op
[0]->value
.f
[c
]);
615 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
616 switch (this->type
->base_type
) {
618 data
.u
[c
] = -((int) op
[0]->value
.u
[c
]);
621 data
.i
[c
] = -op
[0]->value
.i
[c
];
623 case GLSL_TYPE_FLOAT
:
624 data
.f
[c
] = -op
[0]->value
.f
[c
];
633 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
634 switch (this->type
->base_type
) {
636 data
.u
[c
] = op
[0]->value
.u
[c
];
639 data
.i
[c
] = op
[0]->value
.i
[c
];
641 data
.i
[c
] = -data
.i
[c
];
643 case GLSL_TYPE_FLOAT
:
644 data
.f
[c
] = fabs(op
[0]->value
.f
[c
]);
653 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
654 switch (this->type
->base_type
) {
656 data
.u
[c
] = op
[0]->value
.i
[c
] > 0;
659 data
.i
[c
] = (op
[0]->value
.i
[c
] > 0) - (op
[0]->value
.i
[c
] < 0);
661 case GLSL_TYPE_FLOAT
:
662 data
.f
[c
] = float((op
[0]->value
.f
[c
] > 0)-(op
[0]->value
.f
[c
] < 0));
671 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
672 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
673 switch (this->type
->base_type
) {
675 if (op
[0]->value
.u
[c
] != 0.0)
676 data
.u
[c
] = 1 / op
[0]->value
.u
[c
];
679 if (op
[0]->value
.i
[c
] != 0.0)
680 data
.i
[c
] = 1 / op
[0]->value
.i
[c
];
682 case GLSL_TYPE_FLOAT
:
683 if (op
[0]->value
.f
[c
] != 0.0)
684 data
.f
[c
] = 1.0F
/ op
[0]->value
.f
[c
];
693 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
694 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
695 data
.f
[c
] = 1.0F
/ sqrtf(op
[0]->value
.f
[c
]);
700 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
701 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
702 data
.f
[c
] = sqrtf(op
[0]->value
.f
[c
]);
707 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
708 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
709 data
.f
[c
] = expf(op
[0]->value
.f
[c
]);
714 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
715 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
716 data
.f
[c
] = exp2f(op
[0]->value
.f
[c
]);
721 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
722 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
723 data
.f
[c
] = logf(op
[0]->value
.f
[c
]);
728 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
729 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
730 data
.f
[c
] = log2f(op
[0]->value
.f
[c
]);
736 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
737 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
742 case ir_unop_pack_snorm_2x16
:
743 assert(op
[0]->type
== glsl_type::vec2_type
);
744 data
.u
[0] = pack_2x16(pack_snorm_1x16
,
748 case ir_unop_pack_snorm_4x8
:
749 assert(op
[0]->type
== glsl_type::vec4_type
);
750 data
.u
[0] = pack_4x8(pack_snorm_1x8
,
756 case ir_unop_unpack_snorm_2x16
:
757 assert(op
[0]->type
== glsl_type::uint_type
);
758 unpack_2x16(unpack_snorm_1x16
,
760 &data
.f
[0], &data
.f
[1]);
762 case ir_unop_unpack_snorm_4x8
:
763 assert(op
[0]->type
== glsl_type::uint_type
);
764 unpack_4x8(unpack_snorm_1x8
,
766 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
768 case ir_unop_pack_unorm_2x16
:
769 assert(op
[0]->type
== glsl_type::vec2_type
);
770 data
.u
[0] = pack_2x16(pack_unorm_1x16
,
774 case ir_unop_pack_unorm_4x8
:
775 assert(op
[0]->type
== glsl_type::vec4_type
);
776 data
.u
[0] = pack_4x8(pack_unorm_1x8
,
782 case ir_unop_unpack_unorm_2x16
:
783 assert(op
[0]->type
== glsl_type::uint_type
);
784 unpack_2x16(unpack_unorm_1x16
,
786 &data
.f
[0], &data
.f
[1]);
788 case ir_unop_unpack_unorm_4x8
:
789 assert(op
[0]->type
== glsl_type::uint_type
);
790 unpack_4x8(unpack_unorm_1x8
,
792 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
794 case ir_unop_pack_half_2x16
:
795 assert(op
[0]->type
== glsl_type::vec2_type
);
796 data
.u
[0] = pack_2x16(pack_half_1x16
,
800 case ir_unop_unpack_half_2x16
:
801 assert(op
[0]->type
== glsl_type::uint_type
);
802 unpack_2x16(unpack_half_1x16
,
804 &data
.f
[0], &data
.f
[1]);
807 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
808 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
809 data
.f
[c
] = powf(op
[0]->value
.f
[c
], op
[1]->value
.f
[c
]);
814 data
.f
[0] = dot(op
[0], op
[1]);
818 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
819 for (unsigned c
= 0, c0
= 0, c1
= 0;
821 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
823 switch (op
[0]->type
->base_type
) {
825 data
.u
[c
] = MIN2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
828 data
.i
[c
] = MIN2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
830 case GLSL_TYPE_FLOAT
:
831 data
.f
[c
] = MIN2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
840 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
841 for (unsigned c
= 0, c0
= 0, c1
= 0;
843 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
845 switch (op
[0]->type
->base_type
) {
847 data
.u
[c
] = MAX2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
850 data
.i
[c
] = MAX2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
852 case GLSL_TYPE_FLOAT
:
853 data
.f
[c
] = MAX2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
862 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
863 for (unsigned c
= 0, c0
= 0, c1
= 0;
865 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
867 switch (op
[0]->type
->base_type
) {
869 data
.u
[c
] = op
[0]->value
.u
[c0
] + op
[1]->value
.u
[c1
];
872 data
.i
[c
] = op
[0]->value
.i
[c0
] + op
[1]->value
.i
[c1
];
874 case GLSL_TYPE_FLOAT
:
875 data
.f
[c
] = op
[0]->value
.f
[c0
] + op
[1]->value
.f
[c1
];
884 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
885 for (unsigned c
= 0, c0
= 0, c1
= 0;
887 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
889 switch (op
[0]->type
->base_type
) {
891 data
.u
[c
] = op
[0]->value
.u
[c0
] - op
[1]->value
.u
[c1
];
894 data
.i
[c
] = op
[0]->value
.i
[c0
] - op
[1]->value
.i
[c1
];
896 case GLSL_TYPE_FLOAT
:
897 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
];
906 /* Check for equal types, or unequal types involving scalars */
907 if ((op
[0]->type
== op
[1]->type
&& !op
[0]->type
->is_matrix())
908 || op0_scalar
|| op1_scalar
) {
909 for (unsigned c
= 0, c0
= 0, c1
= 0;
911 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
913 switch (op
[0]->type
->base_type
) {
915 data
.u
[c
] = op
[0]->value
.u
[c0
] * op
[1]->value
.u
[c1
];
918 data
.i
[c
] = op
[0]->value
.i
[c0
] * op
[1]->value
.i
[c1
];
920 case GLSL_TYPE_FLOAT
:
921 data
.f
[c
] = op
[0]->value
.f
[c0
] * op
[1]->value
.f
[c1
];
928 assert(op
[0]->type
->is_matrix() || op
[1]->type
->is_matrix());
930 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
931 * matrix can be a GLSL vector, either N or P can be 1.
933 * For vec*mat, the vector is treated as a row vector. This
934 * means the vector is a 1-row x M-column matrix.
936 * For mat*vec, the vector is treated as a column vector. Since
937 * matrix_columns is 1 for vectors, this just works.
939 const unsigned n
= op
[0]->type
->is_vector()
940 ? 1 : op
[0]->type
->vector_elements
;
941 const unsigned m
= op
[1]->type
->vector_elements
;
942 const unsigned p
= op
[1]->type
->matrix_columns
;
943 for (unsigned j
= 0; j
< p
; j
++) {
944 for (unsigned i
= 0; i
< n
; i
++) {
945 for (unsigned k
= 0; k
< m
; k
++) {
946 data
.f
[i
+n
*j
] += op
[0]->value
.f
[i
+n
*k
]*op
[1]->value
.f
[k
+m
*j
];
954 /* FINISHME: Emit warning when division-by-zero is detected. */
955 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
956 for (unsigned c
= 0, c0
= 0, c1
= 0;
958 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
960 switch (op
[0]->type
->base_type
) {
962 if (op
[1]->value
.u
[c1
] == 0) {
965 data
.u
[c
] = op
[0]->value
.u
[c0
] / op
[1]->value
.u
[c1
];
969 if (op
[1]->value
.i
[c1
] == 0) {
972 data
.i
[c
] = op
[0]->value
.i
[c0
] / op
[1]->value
.i
[c1
];
975 case GLSL_TYPE_FLOAT
:
976 data
.f
[c
] = op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
];
985 /* FINISHME: Emit warning when division-by-zero is detected. */
986 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
987 for (unsigned c
= 0, c0
= 0, c1
= 0;
989 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
991 switch (op
[0]->type
->base_type
) {
993 if (op
[1]->value
.u
[c1
] == 0) {
996 data
.u
[c
] = op
[0]->value
.u
[c0
] % op
[1]->value
.u
[c1
];
1000 if (op
[1]->value
.i
[c1
] == 0) {
1003 data
.i
[c
] = op
[0]->value
.i
[c0
] % op
[1]->value
.i
[c1
];
1006 case GLSL_TYPE_FLOAT
:
1007 /* We don't use fmod because it rounds toward zero; GLSL specifies
1010 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
]
1011 * floorf(op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
]);
1020 case ir_binop_logic_and
:
1021 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1022 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1023 data
.b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
1025 case ir_binop_logic_xor
:
1026 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1027 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1028 data
.b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
1030 case ir_binop_logic_or
:
1031 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1032 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1033 data
.b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
1037 assert(op
[0]->type
== op
[1]->type
);
1038 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1039 switch (op
[0]->type
->base_type
) {
1040 case GLSL_TYPE_UINT
:
1041 data
.b
[c
] = op
[0]->value
.u
[c
] < op
[1]->value
.u
[c
];
1044 data
.b
[c
] = op
[0]->value
.i
[c
] < op
[1]->value
.i
[c
];
1046 case GLSL_TYPE_FLOAT
:
1047 data
.b
[c
] = op
[0]->value
.f
[c
] < op
[1]->value
.f
[c
];
1054 case ir_binop_greater
:
1055 assert(op
[0]->type
== op
[1]->type
);
1056 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1057 switch (op
[0]->type
->base_type
) {
1058 case GLSL_TYPE_UINT
:
1059 data
.b
[c
] = op
[0]->value
.u
[c
] > op
[1]->value
.u
[c
];
1062 data
.b
[c
] = op
[0]->value
.i
[c
] > op
[1]->value
.i
[c
];
1064 case GLSL_TYPE_FLOAT
:
1065 data
.b
[c
] = op
[0]->value
.f
[c
] > op
[1]->value
.f
[c
];
1072 case ir_binop_lequal
:
1073 assert(op
[0]->type
== op
[1]->type
);
1074 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1075 switch (op
[0]->type
->base_type
) {
1076 case GLSL_TYPE_UINT
:
1077 data
.b
[c
] = op
[0]->value
.u
[c
] <= op
[1]->value
.u
[c
];
1080 data
.b
[c
] = op
[0]->value
.i
[c
] <= op
[1]->value
.i
[c
];
1082 case GLSL_TYPE_FLOAT
:
1083 data
.b
[c
] = op
[0]->value
.f
[c
] <= op
[1]->value
.f
[c
];
1090 case ir_binop_gequal
:
1091 assert(op
[0]->type
== op
[1]->type
);
1092 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1093 switch (op
[0]->type
->base_type
) {
1094 case GLSL_TYPE_UINT
:
1095 data
.b
[c
] = op
[0]->value
.u
[c
] >= op
[1]->value
.u
[c
];
1098 data
.b
[c
] = op
[0]->value
.i
[c
] >= op
[1]->value
.i
[c
];
1100 case GLSL_TYPE_FLOAT
:
1101 data
.b
[c
] = op
[0]->value
.f
[c
] >= op
[1]->value
.f
[c
];
1108 case ir_binop_equal
:
1109 assert(op
[0]->type
== op
[1]->type
);
1110 for (unsigned c
= 0; c
< components
; c
++) {
1111 switch (op
[0]->type
->base_type
) {
1112 case GLSL_TYPE_UINT
:
1113 data
.b
[c
] = op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
1116 data
.b
[c
] = op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
1118 case GLSL_TYPE_FLOAT
:
1119 data
.b
[c
] = op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
1121 case GLSL_TYPE_BOOL
:
1122 data
.b
[c
] = op
[0]->value
.b
[c
] == op
[1]->value
.b
[c
];
1129 case ir_binop_nequal
:
1130 assert(op
[0]->type
== op
[1]->type
);
1131 for (unsigned c
= 0; c
< components
; c
++) {
1132 switch (op
[0]->type
->base_type
) {
1133 case GLSL_TYPE_UINT
:
1134 data
.b
[c
] = op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
1137 data
.b
[c
] = op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
1139 case GLSL_TYPE_FLOAT
:
1140 data
.b
[c
] = op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
1142 case GLSL_TYPE_BOOL
:
1143 data
.b
[c
] = op
[0]->value
.b
[c
] != op
[1]->value
.b
[c
];
1150 case ir_binop_all_equal
:
1151 data
.b
[0] = op
[0]->has_value(op
[1]);
1153 case ir_binop_any_nequal
:
1154 data
.b
[0] = !op
[0]->has_value(op
[1]);
1157 case ir_binop_lshift
:
1158 for (unsigned c
= 0, c0
= 0, c1
= 0;
1160 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1162 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1163 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1164 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.i
[c1
];
1166 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1167 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1168 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.u
[c1
];
1170 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1171 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1172 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.i
[c1
];
1174 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1175 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1176 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.u
[c1
];
1181 case ir_binop_rshift
:
1182 for (unsigned c
= 0, c0
= 0, c1
= 0;
1184 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1186 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1187 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1188 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.i
[c1
];
1190 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1191 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1192 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.u
[c1
];
1194 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1195 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1196 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.i
[c1
];
1198 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1199 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1200 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.u
[c1
];
1205 case ir_binop_bit_and
:
1206 for (unsigned c
= 0, c0
= 0, c1
= 0;
1208 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1210 switch (op
[0]->type
->base_type
) {
1212 data
.i
[c
] = op
[0]->value
.i
[c0
] & op
[1]->value
.i
[c1
];
1214 case GLSL_TYPE_UINT
:
1215 data
.u
[c
] = op
[0]->value
.u
[c0
] & op
[1]->value
.u
[c1
];
1223 case ir_binop_bit_or
:
1224 for (unsigned c
= 0, c0
= 0, c1
= 0;
1226 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1228 switch (op
[0]->type
->base_type
) {
1230 data
.i
[c
] = op
[0]->value
.i
[c0
] | op
[1]->value
.i
[c1
];
1232 case GLSL_TYPE_UINT
:
1233 data
.u
[c
] = op
[0]->value
.u
[c0
] | op
[1]->value
.u
[c1
];
1241 case ir_binop_vector_extract
: {
1242 const int c
= CLAMP(op
[1]->value
.i
[0], 0,
1243 (int) op
[0]->type
->vector_elements
- 1);
1245 switch (op
[0]->type
->base_type
) {
1246 case GLSL_TYPE_UINT
:
1247 data
.u
[0] = op
[0]->value
.u
[c
];
1250 data
.i
[0] = op
[0]->value
.i
[c
];
1252 case GLSL_TYPE_FLOAT
:
1253 data
.f
[0] = op
[0]->value
.f
[c
];
1255 case GLSL_TYPE_BOOL
:
1256 data
.b
[0] = op
[0]->value
.b
[c
];
1264 case ir_binop_bit_xor
:
1265 for (unsigned c
= 0, c0
= 0, c1
= 0;
1267 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1269 switch (op
[0]->type
->base_type
) {
1271 data
.i
[c
] = op
[0]->value
.i
[c0
] ^ op
[1]->value
.i
[c1
];
1273 case GLSL_TYPE_UINT
:
1274 data
.u
[c
] = op
[0]->value
.u
[c0
] ^ op
[1]->value
.u
[c1
];
1282 case ir_unop_bitfield_reverse
:
1283 /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
1284 for (unsigned c
= 0; c
< components
; c
++) {
1285 unsigned int v
= op
[0]->value
.u
[c
]; // input bits to be reversed
1286 unsigned int r
= v
; // r will be reversed bits of v; first get LSB of v
1287 int s
= sizeof(v
) * CHAR_BIT
- 1; // extra shift needed at end
1289 for (v
>>= 1; v
; v
>>= 1) {
1294 r
<<= s
; // shift when v's highest bits are zero
1300 case ir_unop_bit_count
:
1301 for (unsigned c
= 0; c
< components
; c
++) {
1303 unsigned v
= op
[0]->value
.u
[c
];
1305 for (; v
; count
++) {
1312 case ir_unop_find_msb
:
1313 for (unsigned c
= 0; c
< components
; c
++) {
1314 int v
= op
[0]->value
.i
[c
];
1316 if (v
== 0 || (op
[0]->type
->base_type
== GLSL_TYPE_INT
&& v
== -1))
1320 int top_bit
= op
[0]->type
->base_type
== GLSL_TYPE_UINT
1321 ? 0 : v
& (1 << 31);
1323 while (((v
& (1 << 31)) == top_bit
) && count
!= 32) {
1328 data
.i
[c
] = 31 - count
;
1333 case ir_unop_find_lsb
:
1334 for (unsigned c
= 0; c
< components
; c
++) {
1335 if (op
[0]->value
.i
[c
] == 0)
1339 unsigned v
= op
[0]->value
.u
[c
];
1341 for (; !(v
& 1); v
>>= 1) {
1349 case ir_triop_bitfield_extract
: {
1350 int offset
= op
[1]->value
.i
[0];
1351 int bits
= op
[2]->value
.i
[0];
1353 for (unsigned c
= 0; c
< components
; c
++) {
1356 else if (offset
< 0 || bits
< 0)
1357 data
.u
[c
] = 0; /* Undefined, per spec. */
1358 else if (offset
+ bits
> 32)
1359 data
.u
[c
] = 0; /* Undefined, per spec. */
1361 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
) {
1362 /* int so that the right shift will sign-extend. */
1363 int value
= op
[0]->value
.i
[c
];
1364 value
<<= 32 - bits
- offset
;
1365 value
>>= 32 - bits
;
1368 unsigned value
= op
[0]->value
.u
[c
];
1369 value
<<= 32 - bits
- offset
;
1370 value
>>= 32 - bits
;
1378 case ir_triop_lrp
: {
1379 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1380 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
);
1381 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
);
1383 unsigned c2_inc
= op
[2]->type
->is_scalar() ? 0 : 1;
1384 for (unsigned c
= 0, c2
= 0; c
< components
; c2
+= c2_inc
, c
++) {
1385 data
.f
[c
] = op
[0]->value
.f
[c
] * (1.0f
- op
[2]->value
.f
[c2
]) +
1386 (op
[1]->value
.f
[c
] * op
[2]->value
.f
[c2
]);
1391 case ir_triop_vector_insert
: {
1392 const unsigned idx
= op
[2]->value
.u
[0];
1394 memcpy(&data
, &op
[0]->value
, sizeof(data
));
1396 switch (this->type
->base_type
) {
1398 data
.i
[idx
] = op
[1]->value
.i
[0];
1400 case GLSL_TYPE_UINT
:
1401 data
.u
[idx
] = op
[1]->value
.u
[0];
1403 case GLSL_TYPE_FLOAT
:
1404 data
.f
[idx
] = op
[1]->value
.f
[0];
1406 case GLSL_TYPE_BOOL
:
1407 data
.b
[idx
] = op
[1]->value
.b
[0];
1410 assert(!"Should not get here.");
1416 case ir_quadop_bitfield_insert
: {
1417 int offset
= op
[2]->value
.i
[0];
1418 int bits
= op
[3]->value
.i
[0];
1420 for (unsigned c
= 0; c
< components
; c
++) {
1422 data
.u
[c
] = op
[0]->value
.u
[c
];
1423 else if (offset
< 0 || bits
< 0)
1424 data
.u
[c
] = 0; /* Undefined, per spec. */
1425 else if (offset
+ bits
> 32)
1426 data
.u
[c
] = 0; /* Undefined, per spec. */
1428 unsigned insert_mask
= ((1 << bits
) - 1) << offset
;
1430 unsigned insert
= op
[1]->value
.u
[c
];
1432 insert
&= insert_mask
;
1434 unsigned base
= op
[0]->value
.u
[c
];
1435 base
&= ~insert_mask
;
1437 data
.u
[c
] = base
| insert
;
1443 case ir_quadop_vector
:
1444 for (unsigned c
= 0; c
< this->type
->vector_elements
; c
++) {
1445 switch (this->type
->base_type
) {
1447 data
.i
[c
] = op
[c
]->value
.i
[0];
1449 case GLSL_TYPE_UINT
:
1450 data
.u
[c
] = op
[c
]->value
.u
[0];
1452 case GLSL_TYPE_FLOAT
:
1453 data
.f
[c
] = op
[c
]->value
.f
[0];
1462 /* FINISHME: Should handle all expression types. */
1466 return new(ctx
) ir_constant(this->type
, &data
);
1471 ir_texture::constant_expression_value(struct hash_table
*variable_context
)
1473 /* texture lookups aren't constant expressions */
1479 ir_swizzle::constant_expression_value(struct hash_table
*variable_context
)
1481 ir_constant
*v
= this->val
->constant_expression_value(variable_context
);
1484 ir_constant_data data
= { { 0 } };
1486 const unsigned swiz_idx
[4] = {
1487 this->mask
.x
, this->mask
.y
, this->mask
.z
, this->mask
.w
1490 for (unsigned i
= 0; i
< this->mask
.num_components
; i
++) {
1491 switch (v
->type
->base_type
) {
1492 case GLSL_TYPE_UINT
:
1493 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
1494 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
1495 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
1496 default: assert(!"Should not get here."); break;
1500 void *ctx
= ralloc_parent(this);
1501 return new(ctx
) ir_constant(this->type
, &data
);
1508 ir_dereference_variable::constant_referenced(struct hash_table
*variable_context
,
1509 ir_constant
*&store
, int &offset
) const
1511 if (variable_context
) {
1512 store
= (ir_constant
*)hash_table_find(variable_context
, var
);
1521 ir_dereference_variable::constant_expression_value(struct hash_table
*variable_context
)
1523 /* This may occur during compile and var->type is glsl_type::error_type */
1527 /* Give priority to the context hashtable, if it exists */
1528 if (variable_context
) {
1529 ir_constant
*value
= (ir_constant
*)hash_table_find(variable_context
, var
);
1534 /* The constant_value of a uniform variable is its initializer,
1535 * not the lifetime constant value of the uniform.
1537 if (var
->mode
== ir_var_uniform
)
1540 if (!var
->constant_value
)
1543 return var
->constant_value
->clone(ralloc_parent(var
), NULL
);
1548 ir_dereference_array::constant_referenced(struct hash_table
*variable_context
,
1549 ir_constant
*&store
, int &offset
) const
1551 ir_constant
*index_c
= array_index
->constant_expression_value(variable_context
);
1553 if (!index_c
|| !index_c
->type
->is_scalar() || !index_c
->type
->is_integer()) {
1559 int index
= index_c
->type
->base_type
== GLSL_TYPE_INT
?
1560 index_c
->get_int_component(0) :
1561 index_c
->get_uint_component(0);
1563 ir_constant
*substore
;
1565 const ir_dereference
*deref
= array
->as_dereference();
1572 deref
->constant_referenced(variable_context
, substore
, suboffset
);
1580 const glsl_type
*vt
= array
->type
;
1581 if (vt
->is_array()) {
1582 store
= substore
->get_array_element(index
);
1586 if (vt
->is_matrix()) {
1588 offset
= index
* vt
->vector_elements
;
1591 if (vt
->is_vector()) {
1593 offset
= suboffset
+ index
;
1602 ir_dereference_array::constant_expression_value(struct hash_table
*variable_context
)
1604 ir_constant
*array
= this->array
->constant_expression_value(variable_context
);
1605 ir_constant
*idx
= this->array_index
->constant_expression_value(variable_context
);
1607 if ((array
!= NULL
) && (idx
!= NULL
)) {
1608 void *ctx
= ralloc_parent(this);
1609 if (array
->type
->is_matrix()) {
1610 /* Array access of a matrix results in a vector.
1612 const unsigned column
= idx
->value
.u
[0];
1614 const glsl_type
*const column_type
= array
->type
->column_type();
1616 /* Offset in the constant matrix to the first element of the column
1619 const unsigned mat_idx
= column
* column_type
->vector_elements
;
1621 ir_constant_data data
= { { 0 } };
1623 switch (column_type
->base_type
) {
1624 case GLSL_TYPE_UINT
:
1626 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1627 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
1631 case GLSL_TYPE_FLOAT
:
1632 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1633 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
1638 assert(!"Should not get here.");
1642 return new(ctx
) ir_constant(column_type
, &data
);
1643 } else if (array
->type
->is_vector()) {
1644 const unsigned component
= idx
->value
.u
[0];
1646 return new(ctx
) ir_constant(array
, component
);
1648 const unsigned index
= idx
->value
.u
[0];
1649 return array
->get_array_element(index
)->clone(ctx
, NULL
);
1657 ir_dereference_record::constant_referenced(struct hash_table
*variable_context
,
1658 ir_constant
*&store
, int &offset
) const
1660 ir_constant
*substore
;
1662 const ir_dereference
*deref
= record
->as_dereference();
1669 deref
->constant_referenced(variable_context
, substore
, suboffset
);
1677 store
= substore
->get_record_field(field
);
1682 ir_dereference_record::constant_expression_value(struct hash_table
*variable_context
)
1684 ir_constant
*v
= this->record
->constant_expression_value();
1686 return (v
!= NULL
) ? v
->get_record_field(this->field
) : NULL
;
1691 ir_assignment::constant_expression_value(struct hash_table
*variable_context
)
1693 /* FINISHME: Handle CEs involving assignment (return RHS) */
1699 ir_constant::constant_expression_value(struct hash_table
*variable_context
)
1706 ir_call::constant_expression_value(struct hash_table
*variable_context
)
1708 return this->callee
->constant_expression_value(&this->actual_parameters
, variable_context
);
1712 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list
&body
,
1713 struct hash_table
*variable_context
,
1714 ir_constant
**result
)
1716 foreach_list(n
, &body
) {
1717 ir_instruction
*inst
= (ir_instruction
*)n
;
1718 switch(inst
->ir_type
) {
1720 /* (declare () type symbol) */
1721 case ir_type_variable
: {
1722 ir_variable
*var
= inst
->as_variable();
1723 hash_table_insert(variable_context
, ir_constant::zero(this, var
->type
), var
);
1727 /* (assign [condition] (write-mask) (ref) (value)) */
1728 case ir_type_assignment
: {
1729 ir_assignment
*asg
= inst
->as_assignment();
1730 if (asg
->condition
) {
1731 ir_constant
*cond
= asg
->condition
->constant_expression_value(variable_context
);
1734 if (!cond
->get_bool_component(0))
1738 ir_constant
*store
= NULL
;
1740 asg
->lhs
->constant_referenced(variable_context
, store
, offset
);
1745 ir_constant
*value
= asg
->rhs
->constant_expression_value(variable_context
);
1750 store
->copy_masked_offset(value
, offset
, asg
->write_mask
);
1754 /* (return (expression)) */
1755 case ir_type_return
:
1757 *result
= inst
->as_return()->value
->constant_expression_value(variable_context
);
1758 return *result
!= NULL
;
1760 /* (call name (ref) (params))*/
1761 case ir_type_call
: {
1762 ir_call
*call
= inst
->as_call();
1764 /* Just say no to void functions in constant expressions. We
1765 * don't need them at that point.
1768 if (!call
->return_deref
)
1771 ir_constant
*store
= NULL
;
1773 call
->return_deref
->constant_referenced(variable_context
, store
, offset
);
1778 ir_constant
*value
= call
->constant_expression_value(variable_context
);
1783 store
->copy_offset(value
, offset
);
1787 /* (if condition (then-instructions) (else-instructions)) */
1789 ir_if
*iif
= inst
->as_if();
1791 ir_constant
*cond
= iif
->condition
->constant_expression_value(variable_context
);
1792 if (!cond
|| !cond
->type
->is_boolean())
1795 exec_list
&branch
= cond
->get_bool_component(0) ? iif
->then_instructions
: iif
->else_instructions
;
1798 if (!constant_expression_evaluate_expression_list(branch
, variable_context
, result
))
1801 /* If there was a return in the branch chosen, drop out now. */
1808 /* Every other expression type, we drop out. */
1814 /* Reaching the end of the block is not an error condition */
1822 ir_function_signature::constant_expression_value(exec_list
*actual_parameters
, struct hash_table
*variable_context
)
1824 const glsl_type
*type
= this->return_type
;
1825 if (type
== glsl_type::void_type
)
1828 /* From the GLSL 1.20 spec, page 23:
1829 * "Function calls to user-defined functions (non-built-in functions)
1830 * cannot be used to form constant expressions."
1832 if (!this->is_builtin
)
1836 * Of the builtin functions, only the texture lookups and the noise
1837 * ones must not be used in constant expressions. They all include
1838 * specific opcodes so they don't need to be special-cased at this
1842 /* Initialize the table of dereferencable names with the function
1843 * parameters. Verify their const-ness on the way.
1845 * We expect the correctness of the number of parameters to have
1846 * been checked earlier.
1848 hash_table
*deref_hash
= hash_table_ctor(8, hash_table_pointer_hash
,
1849 hash_table_pointer_compare
);
1851 /* If "origin" is non-NULL, then the function body is there. So we
1852 * have to use the variable objects from the object with the body,
1853 * but the parameter instanciation on the current object.
1855 const exec_node
*parameter_info
= origin
? origin
->parameters
.head
: parameters
.head
;
1857 foreach_list(n
, actual_parameters
) {
1858 ir_constant
*constant
= ((ir_rvalue
*) n
)->constant_expression_value(variable_context
);
1859 if (constant
== NULL
) {
1860 hash_table_dtor(deref_hash
);
1865 ir_variable
*var
= (ir_variable
*)parameter_info
;
1866 hash_table_insert(deref_hash
, constant
, var
);
1868 parameter_info
= parameter_info
->next
;
1871 ir_constant
*result
= NULL
;
1873 /* Now run the builtin function until something non-constant
1874 * happens or we get the result.
1876 if (constant_expression_evaluate_expression_list(origin
? origin
->body
: body
, deref_hash
, &result
) && result
)
1877 result
= result
->clone(ralloc_parent(this), NULL
);
1879 hash_table_dtor(deref_hash
);