egl/main: Stop using EGLNative types internally
[mesa.git] / src / glsl / ir_constant_expression.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 /**
25 * \file ir_constant_expression.cpp
26 * Evaluate and process constant valued expressions
27 *
28 * In GLSL, constant valued expressions are used in several places. These
29 * must be processed and evaluated very early in the compilation process.
30 *
31 * * Sizes of arrays
32 * * Initializers for uniforms
33 * * Initializers for \c const variables
34 */
35
36 #include <math.h>
37 #include "main/core.h" /* for MAX2, MIN2, CLAMP */
38 #include "ir.h"
39 #include "ir_visitor.h"
40 #include "glsl_types.h"
41 #include "program/hash_table.h"
42
43 #if defined(_MSC_VER) && (_MSC_VER < 1800)
44 static int isnormal(double x)
45 {
46 return _fpclass(x) == _FPCLASS_NN || _fpclass(x) == _FPCLASS_PN;
47 }
48 #elif defined(__SUNPRO_CC)
49 #include <ieeefp.h>
50 static int isnormal(double x)
51 {
52 return fpclass(x) == FP_NORMAL;
53 }
54 #endif
55
56 #if defined(_MSC_VER)
57 static double copysign(double x, double y)
58 {
59 return _copysign(x, y);
60 }
61 #endif
62
63 static float
64 dot(ir_constant *op0, ir_constant *op1)
65 {
66 assert(op0->type->is_float() && op1->type->is_float());
67
68 float result = 0;
69 for (unsigned c = 0; c < op0->type->components(); c++)
70 result += op0->value.f[c] * op1->value.f[c];
71
72 return result;
73 }
74
75 /* This method is the only one supported by gcc. Unions in particular
76 * are iffy, and read-through-converted-pointer is killed by strict
77 * aliasing. OTOH, the compiler sees through the memcpy, so the
78 * resulting asm is reasonable.
79 */
80 static float
81 bitcast_u2f(unsigned int u)
82 {
83 assert(sizeof(float) == sizeof(unsigned int));
84 float f;
85 memcpy(&f, &u, sizeof(f));
86 return f;
87 }
88
89 static unsigned int
90 bitcast_f2u(float f)
91 {
92 assert(sizeof(float) == sizeof(unsigned int));
93 unsigned int u;
94 memcpy(&u, &f, sizeof(f));
95 return u;
96 }
97
98 /**
99 * Evaluate one component of a floating-point 4x8 unpacking function.
100 */
101 typedef uint8_t
102 (*pack_1x8_func_t)(float);
103
104 /**
105 * Evaluate one component of a floating-point 2x16 unpacking function.
106 */
107 typedef uint16_t
108 (*pack_1x16_func_t)(float);
109
110 /**
111 * Evaluate one component of a floating-point 4x8 unpacking function.
112 */
113 typedef float
114 (*unpack_1x8_func_t)(uint8_t);
115
116 /**
117 * Evaluate one component of a floating-point 2x16 unpacking function.
118 */
119 typedef float
120 (*unpack_1x16_func_t)(uint16_t);
121
122 /**
123 * Evaluate a 2x16 floating-point packing function.
124 */
125 static uint32_t
126 pack_2x16(pack_1x16_func_t pack_1x16,
127 float x, float y)
128 {
129 /* From section 8.4 of the GLSL ES 3.00 spec:
130 *
131 * packSnorm2x16
132 * -------------
133 * The first component of the vector will be written to the least
134 * significant bits of the output; the last component will be written to
135 * the most significant bits.
136 *
137 * The specifications for the other packing functions contain similar
138 * language.
139 */
140 uint32_t u = 0;
141 u |= ((uint32_t) pack_1x16(x) << 0);
142 u |= ((uint32_t) pack_1x16(y) << 16);
143 return u;
144 }
145
146 /**
147 * Evaluate a 4x8 floating-point packing function.
148 */
149 static uint32_t
150 pack_4x8(pack_1x8_func_t pack_1x8,
151 float x, float y, float z, float w)
152 {
153 /* From section 8.4 of the GLSL 4.30 spec:
154 *
155 * packSnorm4x8
156 * ------------
157 * The first component of the vector will be written to the least
158 * significant bits of the output; the last component will be written to
159 * the most significant bits.
160 *
161 * The specifications for the other packing functions contain similar
162 * language.
163 */
164 uint32_t u = 0;
165 u |= ((uint32_t) pack_1x8(x) << 0);
166 u |= ((uint32_t) pack_1x8(y) << 8);
167 u |= ((uint32_t) pack_1x8(z) << 16);
168 u |= ((uint32_t) pack_1x8(w) << 24);
169 return u;
170 }
171
172 /**
173 * Evaluate a 2x16 floating-point unpacking function.
174 */
175 static void
176 unpack_2x16(unpack_1x16_func_t unpack_1x16,
177 uint32_t u,
178 float *x, float *y)
179 {
180 /* From section 8.4 of the GLSL ES 3.00 spec:
181 *
182 * unpackSnorm2x16
183 * ---------------
184 * The first component of the returned vector will be extracted from
185 * the least significant bits of the input; the last component will be
186 * extracted from the most significant bits.
187 *
188 * The specifications for the other unpacking functions contain similar
189 * language.
190 */
191 *x = unpack_1x16((uint16_t) (u & 0xffff));
192 *y = unpack_1x16((uint16_t) (u >> 16));
193 }
194
195 /**
196 * Evaluate a 4x8 floating-point unpacking function.
197 */
198 static void
199 unpack_4x8(unpack_1x8_func_t unpack_1x8, uint32_t u,
200 float *x, float *y, float *z, float *w)
201 {
202 /* From section 8.4 of the GLSL 4.30 spec:
203 *
204 * unpackSnorm4x8
205 * --------------
206 * The first component of the returned vector will be extracted from
207 * the least significant bits of the input; the last component will be
208 * extracted from the most significant bits.
209 *
210 * The specifications for the other unpacking functions contain similar
211 * language.
212 */
213 *x = unpack_1x8((uint8_t) (u & 0xff));
214 *y = unpack_1x8((uint8_t) (u >> 8));
215 *z = unpack_1x8((uint8_t) (u >> 16));
216 *w = unpack_1x8((uint8_t) (u >> 24));
217 }
218
219 /**
220 * Evaluate one component of packSnorm4x8.
221 */
222 static uint8_t
223 pack_snorm_1x8(float x)
224 {
225 /* From section 8.4 of the GLSL 4.30 spec:
226 *
227 * packSnorm4x8
228 * ------------
229 * The conversion for component c of v to fixed point is done as
230 * follows:
231 *
232 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
233 *
234 * We must first cast the float to an int, because casting a negative
235 * float to a uint is undefined.
236 */
237 return (uint8_t) (int8_t)
238 _mesa_round_to_even(CLAMP(x, -1.0f, +1.0f) * 127.0f);
239 }
240
241 /**
242 * Evaluate one component of packSnorm2x16.
243 */
244 static uint16_t
245 pack_snorm_1x16(float x)
246 {
247 /* From section 8.4 of the GLSL ES 3.00 spec:
248 *
249 * packSnorm2x16
250 * -------------
251 * The conversion for component c of v to fixed point is done as
252 * follows:
253 *
254 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
255 *
256 * We must first cast the float to an int, because casting a negative
257 * float to a uint is undefined.
258 */
259 return (uint16_t) (int16_t)
260 _mesa_round_to_even(CLAMP(x, -1.0f, +1.0f) * 32767.0f);
261 }
262
263 /**
264 * Evaluate one component of unpackSnorm4x8.
265 */
266 static float
267 unpack_snorm_1x8(uint8_t u)
268 {
269 /* From section 8.4 of the GLSL 4.30 spec:
270 *
271 * unpackSnorm4x8
272 * --------------
273 * The conversion for unpacked fixed-point value f to floating point is
274 * done as follows:
275 *
276 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
277 */
278 return CLAMP((int8_t) u / 127.0f, -1.0f, +1.0f);
279 }
280
281 /**
282 * Evaluate one component of unpackSnorm2x16.
283 */
284 static float
285 unpack_snorm_1x16(uint16_t u)
286 {
287 /* From section 8.4 of the GLSL ES 3.00 spec:
288 *
289 * unpackSnorm2x16
290 * ---------------
291 * The conversion for unpacked fixed-point value f to floating point is
292 * done as follows:
293 *
294 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
295 */
296 return CLAMP((int16_t) u / 32767.0f, -1.0f, +1.0f);
297 }
298
299 /**
300 * Evaluate one component packUnorm4x8.
301 */
302 static uint8_t
303 pack_unorm_1x8(float x)
304 {
305 /* From section 8.4 of the GLSL 4.30 spec:
306 *
307 * packUnorm4x8
308 * ------------
309 * The conversion for component c of v to fixed point is done as
310 * follows:
311 *
312 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
313 */
314 return (uint8_t) _mesa_round_to_even(CLAMP(x, 0.0f, 1.0f) * 255.0f);
315 }
316
317 /**
318 * Evaluate one component packUnorm2x16.
319 */
320 static uint16_t
321 pack_unorm_1x16(float x)
322 {
323 /* From section 8.4 of the GLSL ES 3.00 spec:
324 *
325 * packUnorm2x16
326 * -------------
327 * The conversion for component c of v to fixed point is done as
328 * follows:
329 *
330 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
331 */
332 return (uint16_t) _mesa_round_to_even(CLAMP(x, 0.0f, 1.0f) * 65535.0f);
333 }
334
335 /**
336 * Evaluate one component of unpackUnorm4x8.
337 */
338 static float
339 unpack_unorm_1x8(uint8_t u)
340 {
341 /* From section 8.4 of the GLSL 4.30 spec:
342 *
343 * unpackUnorm4x8
344 * --------------
345 * The conversion for unpacked fixed-point value f to floating point is
346 * done as follows:
347 *
348 * unpackUnorm4x8: f / 255.0
349 */
350 return (float) u / 255.0f;
351 }
352
353 /**
354 * Evaluate one component of unpackUnorm2x16.
355 */
356 static float
357 unpack_unorm_1x16(uint16_t u)
358 {
359 /* From section 8.4 of the GLSL ES 3.00 spec:
360 *
361 * unpackUnorm2x16
362 * ---------------
363 * The conversion for unpacked fixed-point value f to floating point is
364 * done as follows:
365 *
366 * unpackUnorm2x16: f / 65535.0
367 */
368 return (float) u / 65535.0f;
369 }
370
371 /**
372 * Evaluate one component of packHalf2x16.
373 */
374 static uint16_t
375 pack_half_1x16(float x)
376 {
377 return _mesa_float_to_half(x);
378 }
379
380 /**
381 * Evaluate one component of unpackHalf2x16.
382 */
383 static float
384 unpack_half_1x16(uint16_t u)
385 {
386 return _mesa_half_to_float(u);
387 }
388
389 ir_constant *
390 ir_rvalue::constant_expression_value(struct hash_table *variable_context)
391 {
392 assert(this->type->is_error());
393 return NULL;
394 }
395
396 ir_constant *
397 ir_expression::constant_expression_value(struct hash_table *variable_context)
398 {
399 if (this->type->is_error())
400 return NULL;
401
402 ir_constant *op[Elements(this->operands)] = { NULL, };
403 ir_constant_data data;
404
405 memset(&data, 0, sizeof(data));
406
407 for (unsigned operand = 0; operand < this->get_num_operands(); operand++) {
408 op[operand] = this->operands[operand]->constant_expression_value(variable_context);
409 if (!op[operand])
410 return NULL;
411 }
412
413 if (op[1] != NULL)
414 switch (this->operation) {
415 case ir_binop_lshift:
416 case ir_binop_rshift:
417 case ir_binop_ldexp:
418 case ir_binop_vector_extract:
419 case ir_triop_csel:
420 case ir_triop_bitfield_extract:
421 break;
422
423 default:
424 assert(op[0]->type->base_type == op[1]->type->base_type);
425 break;
426 }
427
428 bool op0_scalar = op[0]->type->is_scalar();
429 bool op1_scalar = op[1] != NULL && op[1]->type->is_scalar();
430
431 /* When iterating over a vector or matrix's components, we want to increase
432 * the loop counter. However, for scalars, we want to stay at 0.
433 */
434 unsigned c0_inc = op0_scalar ? 0 : 1;
435 unsigned c1_inc = op1_scalar ? 0 : 1;
436 unsigned components;
437 if (op1_scalar || !op[1]) {
438 components = op[0]->type->components();
439 } else {
440 components = op[1]->type->components();
441 }
442
443 void *ctx = ralloc_parent(this);
444
445 /* Handle array operations here, rather than below. */
446 if (op[0]->type->is_array()) {
447 assert(op[1] != NULL && op[1]->type->is_array());
448 switch (this->operation) {
449 case ir_binop_all_equal:
450 return new(ctx) ir_constant(op[0]->has_value(op[1]));
451 case ir_binop_any_nequal:
452 return new(ctx) ir_constant(!op[0]->has_value(op[1]));
453 default:
454 break;
455 }
456 return NULL;
457 }
458
459 switch (this->operation) {
460 case ir_unop_bit_not:
461 switch (op[0]->type->base_type) {
462 case GLSL_TYPE_INT:
463 for (unsigned c = 0; c < components; c++)
464 data.i[c] = ~ op[0]->value.i[c];
465 break;
466 case GLSL_TYPE_UINT:
467 for (unsigned c = 0; c < components; c++)
468 data.u[c] = ~ op[0]->value.u[c];
469 break;
470 default:
471 assert(0);
472 }
473 break;
474
475 case ir_unop_logic_not:
476 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
477 for (unsigned c = 0; c < op[0]->type->components(); c++)
478 data.b[c] = !op[0]->value.b[c];
479 break;
480
481 case ir_unop_f2i:
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] = (int) op[0]->value.f[c];
485 }
486 break;
487 case ir_unop_f2u:
488 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
489 for (unsigned c = 0; c < op[0]->type->components(); c++) {
490 data.i[c] = (unsigned) op[0]->value.f[c];
491 }
492 break;
493 case ir_unop_i2f:
494 assert(op[0]->type->base_type == GLSL_TYPE_INT);
495 for (unsigned c = 0; c < op[0]->type->components(); c++) {
496 data.f[c] = (float) op[0]->value.i[c];
497 }
498 break;
499 case ir_unop_u2f:
500 assert(op[0]->type->base_type == GLSL_TYPE_UINT);
501 for (unsigned c = 0; c < op[0]->type->components(); c++) {
502 data.f[c] = (float) op[0]->value.u[c];
503 }
504 break;
505 case ir_unop_b2f:
506 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
507 for (unsigned c = 0; c < op[0]->type->components(); c++) {
508 data.f[c] = op[0]->value.b[c] ? 1.0F : 0.0F;
509 }
510 break;
511 case ir_unop_f2b:
512 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
513 for (unsigned c = 0; c < op[0]->type->components(); c++) {
514 data.b[c] = op[0]->value.f[c] != 0.0F ? true : false;
515 }
516 break;
517 case ir_unop_b2i:
518 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
519 for (unsigned c = 0; c < op[0]->type->components(); c++) {
520 data.u[c] = op[0]->value.b[c] ? 1 : 0;
521 }
522 break;
523 case ir_unop_i2b:
524 assert(op[0]->type->is_integer());
525 for (unsigned c = 0; c < op[0]->type->components(); c++) {
526 data.b[c] = op[0]->value.u[c] ? true : false;
527 }
528 break;
529 case ir_unop_u2i:
530 assert(op[0]->type->base_type == GLSL_TYPE_UINT);
531 for (unsigned c = 0; c < op[0]->type->components(); c++) {
532 data.i[c] = op[0]->value.u[c];
533 }
534 break;
535 case ir_unop_i2u:
536 assert(op[0]->type->base_type == GLSL_TYPE_INT);
537 for (unsigned c = 0; c < op[0]->type->components(); c++) {
538 data.u[c] = op[0]->value.i[c];
539 }
540 break;
541 case ir_unop_bitcast_i2f:
542 assert(op[0]->type->base_type == GLSL_TYPE_INT);
543 for (unsigned c = 0; c < op[0]->type->components(); c++) {
544 data.f[c] = bitcast_u2f(op[0]->value.i[c]);
545 }
546 break;
547 case ir_unop_bitcast_f2i:
548 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
549 for (unsigned c = 0; c < op[0]->type->components(); c++) {
550 data.i[c] = bitcast_f2u(op[0]->value.f[c]);
551 }
552 break;
553 case ir_unop_bitcast_u2f:
554 assert(op[0]->type->base_type == GLSL_TYPE_UINT);
555 for (unsigned c = 0; c < op[0]->type->components(); c++) {
556 data.f[c] = bitcast_u2f(op[0]->value.u[c]);
557 }
558 break;
559 case ir_unop_bitcast_f2u:
560 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
561 for (unsigned c = 0; c < op[0]->type->components(); c++) {
562 data.u[c] = bitcast_f2u(op[0]->value.f[c]);
563 }
564 break;
565 case ir_unop_any:
566 assert(op[0]->type->is_boolean());
567 data.b[0] = false;
568 for (unsigned c = 0; c < op[0]->type->components(); c++) {
569 if (op[0]->value.b[c])
570 data.b[0] = true;
571 }
572 break;
573
574 case ir_unop_trunc:
575 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
576 for (unsigned c = 0; c < op[0]->type->components(); c++) {
577 data.f[c] = truncf(op[0]->value.f[c]);
578 }
579 break;
580
581 case ir_unop_round_even:
582 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
583 for (unsigned c = 0; c < op[0]->type->components(); c++) {
584 data.f[c] = _mesa_round_to_even(op[0]->value.f[c]);
585 }
586 break;
587
588 case ir_unop_ceil:
589 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
590 for (unsigned c = 0; c < op[0]->type->components(); c++) {
591 data.f[c] = ceilf(op[0]->value.f[c]);
592 }
593 break;
594
595 case ir_unop_floor:
596 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
597 for (unsigned c = 0; c < op[0]->type->components(); c++) {
598 data.f[c] = floorf(op[0]->value.f[c]);
599 }
600 break;
601
602 case ir_unop_fract:
603 for (unsigned c = 0; c < op[0]->type->components(); c++) {
604 switch (this->type->base_type) {
605 case GLSL_TYPE_UINT:
606 data.u[c] = 0;
607 break;
608 case GLSL_TYPE_INT:
609 data.i[c] = 0;
610 break;
611 case GLSL_TYPE_FLOAT:
612 data.f[c] = op[0]->value.f[c] - floor(op[0]->value.f[c]);
613 break;
614 default:
615 assert(0);
616 }
617 }
618 break;
619
620 case ir_unop_sin:
621 case ir_unop_sin_reduced:
622 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
623 for (unsigned c = 0; c < op[0]->type->components(); c++) {
624 data.f[c] = sinf(op[0]->value.f[c]);
625 }
626 break;
627
628 case ir_unop_cos:
629 case ir_unop_cos_reduced:
630 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
631 for (unsigned c = 0; c < op[0]->type->components(); c++) {
632 data.f[c] = cosf(op[0]->value.f[c]);
633 }
634 break;
635
636 case ir_unop_neg:
637 for (unsigned c = 0; c < op[0]->type->components(); c++) {
638 switch (this->type->base_type) {
639 case GLSL_TYPE_UINT:
640 data.u[c] = -((int) op[0]->value.u[c]);
641 break;
642 case GLSL_TYPE_INT:
643 data.i[c] = -op[0]->value.i[c];
644 break;
645 case GLSL_TYPE_FLOAT:
646 data.f[c] = -op[0]->value.f[c];
647 break;
648 default:
649 assert(0);
650 }
651 }
652 break;
653
654 case ir_unop_abs:
655 for (unsigned c = 0; c < op[0]->type->components(); c++) {
656 switch (this->type->base_type) {
657 case GLSL_TYPE_UINT:
658 data.u[c] = op[0]->value.u[c];
659 break;
660 case GLSL_TYPE_INT:
661 data.i[c] = op[0]->value.i[c];
662 if (data.i[c] < 0)
663 data.i[c] = -data.i[c];
664 break;
665 case GLSL_TYPE_FLOAT:
666 data.f[c] = fabs(op[0]->value.f[c]);
667 break;
668 default:
669 assert(0);
670 }
671 }
672 break;
673
674 case ir_unop_sign:
675 for (unsigned c = 0; c < op[0]->type->components(); c++) {
676 switch (this->type->base_type) {
677 case GLSL_TYPE_UINT:
678 data.u[c] = op[0]->value.i[c] > 0;
679 break;
680 case GLSL_TYPE_INT:
681 data.i[c] = (op[0]->value.i[c] > 0) - (op[0]->value.i[c] < 0);
682 break;
683 case GLSL_TYPE_FLOAT:
684 data.f[c] = float((op[0]->value.f[c] > 0)-(op[0]->value.f[c] < 0));
685 break;
686 default:
687 assert(0);
688 }
689 }
690 break;
691
692 case ir_unop_rcp:
693 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
694 for (unsigned c = 0; c < op[0]->type->components(); c++) {
695 switch (this->type->base_type) {
696 case GLSL_TYPE_UINT:
697 if (op[0]->value.u[c] != 0.0)
698 data.u[c] = 1 / op[0]->value.u[c];
699 break;
700 case GLSL_TYPE_INT:
701 if (op[0]->value.i[c] != 0.0)
702 data.i[c] = 1 / op[0]->value.i[c];
703 break;
704 case GLSL_TYPE_FLOAT:
705 if (op[0]->value.f[c] != 0.0)
706 data.f[c] = 1.0F / op[0]->value.f[c];
707 break;
708 default:
709 assert(0);
710 }
711 }
712 break;
713
714 case ir_unop_rsq:
715 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
716 for (unsigned c = 0; c < op[0]->type->components(); c++) {
717 data.f[c] = 1.0F / sqrtf(op[0]->value.f[c]);
718 }
719 break;
720
721 case ir_unop_sqrt:
722 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
723 for (unsigned c = 0; c < op[0]->type->components(); c++) {
724 data.f[c] = sqrtf(op[0]->value.f[c]);
725 }
726 break;
727
728 case ir_unop_exp:
729 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
730 for (unsigned c = 0; c < op[0]->type->components(); c++) {
731 data.f[c] = expf(op[0]->value.f[c]);
732 }
733 break;
734
735 case ir_unop_exp2:
736 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
737 for (unsigned c = 0; c < op[0]->type->components(); c++) {
738 data.f[c] = exp2f(op[0]->value.f[c]);
739 }
740 break;
741
742 case ir_unop_log:
743 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
744 for (unsigned c = 0; c < op[0]->type->components(); c++) {
745 data.f[c] = logf(op[0]->value.f[c]);
746 }
747 break;
748
749 case ir_unop_log2:
750 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
751 for (unsigned c = 0; c < op[0]->type->components(); c++) {
752 data.f[c] = log2f(op[0]->value.f[c]);
753 }
754 break;
755
756 case ir_unop_dFdx:
757 case ir_unop_dFdy:
758 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
759 for (unsigned c = 0; c < op[0]->type->components(); c++) {
760 data.f[c] = 0.0;
761 }
762 break;
763
764 case ir_unop_pack_snorm_2x16:
765 assert(op[0]->type == glsl_type::vec2_type);
766 data.u[0] = pack_2x16(pack_snorm_1x16,
767 op[0]->value.f[0],
768 op[0]->value.f[1]);
769 break;
770 case ir_unop_pack_snorm_4x8:
771 assert(op[0]->type == glsl_type::vec4_type);
772 data.u[0] = pack_4x8(pack_snorm_1x8,
773 op[0]->value.f[0],
774 op[0]->value.f[1],
775 op[0]->value.f[2],
776 op[0]->value.f[3]);
777 break;
778 case ir_unop_unpack_snorm_2x16:
779 assert(op[0]->type == glsl_type::uint_type);
780 unpack_2x16(unpack_snorm_1x16,
781 op[0]->value.u[0],
782 &data.f[0], &data.f[1]);
783 break;
784 case ir_unop_unpack_snorm_4x8:
785 assert(op[0]->type == glsl_type::uint_type);
786 unpack_4x8(unpack_snorm_1x8,
787 op[0]->value.u[0],
788 &data.f[0], &data.f[1], &data.f[2], &data.f[3]);
789 break;
790 case ir_unop_pack_unorm_2x16:
791 assert(op[0]->type == glsl_type::vec2_type);
792 data.u[0] = pack_2x16(pack_unorm_1x16,
793 op[0]->value.f[0],
794 op[0]->value.f[1]);
795 break;
796 case ir_unop_pack_unorm_4x8:
797 assert(op[0]->type == glsl_type::vec4_type);
798 data.u[0] = pack_4x8(pack_unorm_1x8,
799 op[0]->value.f[0],
800 op[0]->value.f[1],
801 op[0]->value.f[2],
802 op[0]->value.f[3]);
803 break;
804 case ir_unop_unpack_unorm_2x16:
805 assert(op[0]->type == glsl_type::uint_type);
806 unpack_2x16(unpack_unorm_1x16,
807 op[0]->value.u[0],
808 &data.f[0], &data.f[1]);
809 break;
810 case ir_unop_unpack_unorm_4x8:
811 assert(op[0]->type == glsl_type::uint_type);
812 unpack_4x8(unpack_unorm_1x8,
813 op[0]->value.u[0],
814 &data.f[0], &data.f[1], &data.f[2], &data.f[3]);
815 break;
816 case ir_unop_pack_half_2x16:
817 assert(op[0]->type == glsl_type::vec2_type);
818 data.u[0] = pack_2x16(pack_half_1x16,
819 op[0]->value.f[0],
820 op[0]->value.f[1]);
821 break;
822 case ir_unop_unpack_half_2x16:
823 assert(op[0]->type == glsl_type::uint_type);
824 unpack_2x16(unpack_half_1x16,
825 op[0]->value.u[0],
826 &data.f[0], &data.f[1]);
827 break;
828 case ir_binop_pow:
829 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
830 for (unsigned c = 0; c < op[0]->type->components(); c++) {
831 data.f[c] = powf(op[0]->value.f[c], op[1]->value.f[c]);
832 }
833 break;
834
835 case ir_binop_dot:
836 data.f[0] = dot(op[0], op[1]);
837 break;
838
839 case ir_binop_min:
840 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
841 for (unsigned c = 0, c0 = 0, c1 = 0;
842 c < components;
843 c0 += c0_inc, c1 += c1_inc, c++) {
844
845 switch (op[0]->type->base_type) {
846 case GLSL_TYPE_UINT:
847 data.u[c] = MIN2(op[0]->value.u[c0], op[1]->value.u[c1]);
848 break;
849 case GLSL_TYPE_INT:
850 data.i[c] = MIN2(op[0]->value.i[c0], op[1]->value.i[c1]);
851 break;
852 case GLSL_TYPE_FLOAT:
853 data.f[c] = MIN2(op[0]->value.f[c0], op[1]->value.f[c1]);
854 break;
855 default:
856 assert(0);
857 }
858 }
859
860 break;
861 case ir_binop_max:
862 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
863 for (unsigned c = 0, c0 = 0, c1 = 0;
864 c < components;
865 c0 += c0_inc, c1 += c1_inc, c++) {
866
867 switch (op[0]->type->base_type) {
868 case GLSL_TYPE_UINT:
869 data.u[c] = MAX2(op[0]->value.u[c0], op[1]->value.u[c1]);
870 break;
871 case GLSL_TYPE_INT:
872 data.i[c] = MAX2(op[0]->value.i[c0], op[1]->value.i[c1]);
873 break;
874 case GLSL_TYPE_FLOAT:
875 data.f[c] = MAX2(op[0]->value.f[c0], op[1]->value.f[c1]);
876 break;
877 default:
878 assert(0);
879 }
880 }
881 break;
882
883 case ir_binop_add:
884 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
885 for (unsigned c = 0, c0 = 0, c1 = 0;
886 c < components;
887 c0 += c0_inc, c1 += c1_inc, c++) {
888
889 switch (op[0]->type->base_type) {
890 case GLSL_TYPE_UINT:
891 data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1];
892 break;
893 case GLSL_TYPE_INT:
894 data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1];
895 break;
896 case GLSL_TYPE_FLOAT:
897 data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1];
898 break;
899 default:
900 assert(0);
901 }
902 }
903
904 break;
905 case ir_binop_sub:
906 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
907 for (unsigned c = 0, c0 = 0, c1 = 0;
908 c < components;
909 c0 += c0_inc, c1 += c1_inc, c++) {
910
911 switch (op[0]->type->base_type) {
912 case GLSL_TYPE_UINT:
913 data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1];
914 break;
915 case GLSL_TYPE_INT:
916 data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1];
917 break;
918 case GLSL_TYPE_FLOAT:
919 data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1];
920 break;
921 default:
922 assert(0);
923 }
924 }
925
926 break;
927 case ir_binop_mul:
928 /* Check for equal types, or unequal types involving scalars */
929 if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix())
930 || op0_scalar || op1_scalar) {
931 for (unsigned c = 0, c0 = 0, c1 = 0;
932 c < components;
933 c0 += c0_inc, c1 += c1_inc, c++) {
934
935 switch (op[0]->type->base_type) {
936 case GLSL_TYPE_UINT:
937 data.u[c] = op[0]->value.u[c0] * op[1]->value.u[c1];
938 break;
939 case GLSL_TYPE_INT:
940 data.i[c] = op[0]->value.i[c0] * op[1]->value.i[c1];
941 break;
942 case GLSL_TYPE_FLOAT:
943 data.f[c] = op[0]->value.f[c0] * op[1]->value.f[c1];
944 break;
945 default:
946 assert(0);
947 }
948 }
949 } else {
950 assert(op[0]->type->is_matrix() || op[1]->type->is_matrix());
951
952 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
953 * matrix can be a GLSL vector, either N or P can be 1.
954 *
955 * For vec*mat, the vector is treated as a row vector. This
956 * means the vector is a 1-row x M-column matrix.
957 *
958 * For mat*vec, the vector is treated as a column vector. Since
959 * matrix_columns is 1 for vectors, this just works.
960 */
961 const unsigned n = op[0]->type->is_vector()
962 ? 1 : op[0]->type->vector_elements;
963 const unsigned m = op[1]->type->vector_elements;
964 const unsigned p = op[1]->type->matrix_columns;
965 for (unsigned j = 0; j < p; j++) {
966 for (unsigned i = 0; i < n; i++) {
967 for (unsigned k = 0; k < m; k++) {
968 data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j];
969 }
970 }
971 }
972 }
973
974 break;
975 case ir_binop_div:
976 /* FINISHME: Emit warning when division-by-zero is detected. */
977 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
978 for (unsigned c = 0, c0 = 0, c1 = 0;
979 c < components;
980 c0 += c0_inc, c1 += c1_inc, c++) {
981
982 switch (op[0]->type->base_type) {
983 case GLSL_TYPE_UINT:
984 if (op[1]->value.u[c1] == 0) {
985 data.u[c] = 0;
986 } else {
987 data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1];
988 }
989 break;
990 case GLSL_TYPE_INT:
991 if (op[1]->value.i[c1] == 0) {
992 data.i[c] = 0;
993 } else {
994 data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1];
995 }
996 break;
997 case GLSL_TYPE_FLOAT:
998 data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1];
999 break;
1000 default:
1001 assert(0);
1002 }
1003 }
1004
1005 break;
1006 case ir_binop_mod:
1007 /* FINISHME: Emit warning when division-by-zero is detected. */
1008 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
1009 for (unsigned c = 0, c0 = 0, c1 = 0;
1010 c < components;
1011 c0 += c0_inc, c1 += c1_inc, c++) {
1012
1013 switch (op[0]->type->base_type) {
1014 case GLSL_TYPE_UINT:
1015 if (op[1]->value.u[c1] == 0) {
1016 data.u[c] = 0;
1017 } else {
1018 data.u[c] = op[0]->value.u[c0] % op[1]->value.u[c1];
1019 }
1020 break;
1021 case GLSL_TYPE_INT:
1022 if (op[1]->value.i[c1] == 0) {
1023 data.i[c] = 0;
1024 } else {
1025 data.i[c] = op[0]->value.i[c0] % op[1]->value.i[c1];
1026 }
1027 break;
1028 case GLSL_TYPE_FLOAT:
1029 /* We don't use fmod because it rounds toward zero; GLSL specifies
1030 * the use of floor.
1031 */
1032 data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1]
1033 * floorf(op[0]->value.f[c0] / op[1]->value.f[c1]);
1034 break;
1035 default:
1036 assert(0);
1037 }
1038 }
1039
1040 break;
1041
1042 case ir_binop_logic_and:
1043 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
1044 for (unsigned c = 0; c < op[0]->type->components(); c++)
1045 data.b[c] = op[0]->value.b[c] && op[1]->value.b[c];
1046 break;
1047 case ir_binop_logic_xor:
1048 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
1049 for (unsigned c = 0; c < op[0]->type->components(); c++)
1050 data.b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
1051 break;
1052 case ir_binop_logic_or:
1053 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
1054 for (unsigned c = 0; c < op[0]->type->components(); c++)
1055 data.b[c] = op[0]->value.b[c] || op[1]->value.b[c];
1056 break;
1057
1058 case ir_binop_less:
1059 assert(op[0]->type == op[1]->type);
1060 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1061 switch (op[0]->type->base_type) {
1062 case GLSL_TYPE_UINT:
1063 data.b[c] = op[0]->value.u[c] < op[1]->value.u[c];
1064 break;
1065 case GLSL_TYPE_INT:
1066 data.b[c] = op[0]->value.i[c] < op[1]->value.i[c];
1067 break;
1068 case GLSL_TYPE_FLOAT:
1069 data.b[c] = op[0]->value.f[c] < op[1]->value.f[c];
1070 break;
1071 default:
1072 assert(0);
1073 }
1074 }
1075 break;
1076 case ir_binop_greater:
1077 assert(op[0]->type == op[1]->type);
1078 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1079 switch (op[0]->type->base_type) {
1080 case GLSL_TYPE_UINT:
1081 data.b[c] = op[0]->value.u[c] > op[1]->value.u[c];
1082 break;
1083 case GLSL_TYPE_INT:
1084 data.b[c] = op[0]->value.i[c] > op[1]->value.i[c];
1085 break;
1086 case GLSL_TYPE_FLOAT:
1087 data.b[c] = op[0]->value.f[c] > op[1]->value.f[c];
1088 break;
1089 default:
1090 assert(0);
1091 }
1092 }
1093 break;
1094 case ir_binop_lequal:
1095 assert(op[0]->type == op[1]->type);
1096 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1097 switch (op[0]->type->base_type) {
1098 case GLSL_TYPE_UINT:
1099 data.b[c] = op[0]->value.u[c] <= op[1]->value.u[c];
1100 break;
1101 case GLSL_TYPE_INT:
1102 data.b[c] = op[0]->value.i[c] <= op[1]->value.i[c];
1103 break;
1104 case GLSL_TYPE_FLOAT:
1105 data.b[c] = op[0]->value.f[c] <= op[1]->value.f[c];
1106 break;
1107 default:
1108 assert(0);
1109 }
1110 }
1111 break;
1112 case ir_binop_gequal:
1113 assert(op[0]->type == op[1]->type);
1114 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1115 switch (op[0]->type->base_type) {
1116 case GLSL_TYPE_UINT:
1117 data.b[c] = op[0]->value.u[c] >= op[1]->value.u[c];
1118 break;
1119 case GLSL_TYPE_INT:
1120 data.b[c] = op[0]->value.i[c] >= op[1]->value.i[c];
1121 break;
1122 case GLSL_TYPE_FLOAT:
1123 data.b[c] = op[0]->value.f[c] >= op[1]->value.f[c];
1124 break;
1125 default:
1126 assert(0);
1127 }
1128 }
1129 break;
1130 case ir_binop_equal:
1131 assert(op[0]->type == op[1]->type);
1132 for (unsigned c = 0; c < components; c++) {
1133 switch (op[0]->type->base_type) {
1134 case GLSL_TYPE_UINT:
1135 data.b[c] = op[0]->value.u[c] == op[1]->value.u[c];
1136 break;
1137 case GLSL_TYPE_INT:
1138 data.b[c] = op[0]->value.i[c] == op[1]->value.i[c];
1139 break;
1140 case GLSL_TYPE_FLOAT:
1141 data.b[c] = op[0]->value.f[c] == op[1]->value.f[c];
1142 break;
1143 case GLSL_TYPE_BOOL:
1144 data.b[c] = op[0]->value.b[c] == op[1]->value.b[c];
1145 break;
1146 default:
1147 assert(0);
1148 }
1149 }
1150 break;
1151 case ir_binop_nequal:
1152 assert(op[0]->type == op[1]->type);
1153 for (unsigned c = 0; c < components; c++) {
1154 switch (op[0]->type->base_type) {
1155 case GLSL_TYPE_UINT:
1156 data.b[c] = op[0]->value.u[c] != op[1]->value.u[c];
1157 break;
1158 case GLSL_TYPE_INT:
1159 data.b[c] = op[0]->value.i[c] != op[1]->value.i[c];
1160 break;
1161 case GLSL_TYPE_FLOAT:
1162 data.b[c] = op[0]->value.f[c] != op[1]->value.f[c];
1163 break;
1164 case GLSL_TYPE_BOOL:
1165 data.b[c] = op[0]->value.b[c] != op[1]->value.b[c];
1166 break;
1167 default:
1168 assert(0);
1169 }
1170 }
1171 break;
1172 case ir_binop_all_equal:
1173 data.b[0] = op[0]->has_value(op[1]);
1174 break;
1175 case ir_binop_any_nequal:
1176 data.b[0] = !op[0]->has_value(op[1]);
1177 break;
1178
1179 case ir_binop_lshift:
1180 for (unsigned c = 0, c0 = 0, c1 = 0;
1181 c < components;
1182 c0 += c0_inc, c1 += c1_inc, c++) {
1183
1184 if (op[0]->type->base_type == GLSL_TYPE_INT &&
1185 op[1]->type->base_type == GLSL_TYPE_INT) {
1186 data.i[c] = op[0]->value.i[c0] << op[1]->value.i[c1];
1187
1188 } else if (op[0]->type->base_type == GLSL_TYPE_INT &&
1189 op[1]->type->base_type == GLSL_TYPE_UINT) {
1190 data.i[c] = op[0]->value.i[c0] << op[1]->value.u[c1];
1191
1192 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
1193 op[1]->type->base_type == GLSL_TYPE_INT) {
1194 data.u[c] = op[0]->value.u[c0] << op[1]->value.i[c1];
1195
1196 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
1197 op[1]->type->base_type == GLSL_TYPE_UINT) {
1198 data.u[c] = op[0]->value.u[c0] << op[1]->value.u[c1];
1199 }
1200 }
1201 break;
1202
1203 case ir_binop_rshift:
1204 for (unsigned c = 0, c0 = 0, c1 = 0;
1205 c < components;
1206 c0 += c0_inc, c1 += c1_inc, c++) {
1207
1208 if (op[0]->type->base_type == GLSL_TYPE_INT &&
1209 op[1]->type->base_type == GLSL_TYPE_INT) {
1210 data.i[c] = op[0]->value.i[c0] >> op[1]->value.i[c1];
1211
1212 } else if (op[0]->type->base_type == GLSL_TYPE_INT &&
1213 op[1]->type->base_type == GLSL_TYPE_UINT) {
1214 data.i[c] = op[0]->value.i[c0] >> op[1]->value.u[c1];
1215
1216 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
1217 op[1]->type->base_type == GLSL_TYPE_INT) {
1218 data.u[c] = op[0]->value.u[c0] >> op[1]->value.i[c1];
1219
1220 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
1221 op[1]->type->base_type == GLSL_TYPE_UINT) {
1222 data.u[c] = op[0]->value.u[c0] >> op[1]->value.u[c1];
1223 }
1224 }
1225 break;
1226
1227 case ir_binop_bit_and:
1228 for (unsigned c = 0, c0 = 0, c1 = 0;
1229 c < components;
1230 c0 += c0_inc, c1 += c1_inc, c++) {
1231
1232 switch (op[0]->type->base_type) {
1233 case GLSL_TYPE_INT:
1234 data.i[c] = op[0]->value.i[c0] & op[1]->value.i[c1];
1235 break;
1236 case GLSL_TYPE_UINT:
1237 data.u[c] = op[0]->value.u[c0] & op[1]->value.u[c1];
1238 break;
1239 default:
1240 assert(0);
1241 }
1242 }
1243 break;
1244
1245 case ir_binop_bit_or:
1246 for (unsigned c = 0, c0 = 0, c1 = 0;
1247 c < components;
1248 c0 += c0_inc, c1 += c1_inc, c++) {
1249
1250 switch (op[0]->type->base_type) {
1251 case GLSL_TYPE_INT:
1252 data.i[c] = op[0]->value.i[c0] | op[1]->value.i[c1];
1253 break;
1254 case GLSL_TYPE_UINT:
1255 data.u[c] = op[0]->value.u[c0] | op[1]->value.u[c1];
1256 break;
1257 default:
1258 assert(0);
1259 }
1260 }
1261 break;
1262
1263 case ir_binop_vector_extract: {
1264 const int c = CLAMP(op[1]->value.i[0], 0,
1265 (int) op[0]->type->vector_elements - 1);
1266
1267 switch (op[0]->type->base_type) {
1268 case GLSL_TYPE_UINT:
1269 data.u[0] = op[0]->value.u[c];
1270 break;
1271 case GLSL_TYPE_INT:
1272 data.i[0] = op[0]->value.i[c];
1273 break;
1274 case GLSL_TYPE_FLOAT:
1275 data.f[0] = op[0]->value.f[c];
1276 break;
1277 case GLSL_TYPE_BOOL:
1278 data.b[0] = op[0]->value.b[c];
1279 break;
1280 default:
1281 assert(0);
1282 }
1283 break;
1284 }
1285
1286 case ir_binop_bit_xor:
1287 for (unsigned c = 0, c0 = 0, c1 = 0;
1288 c < components;
1289 c0 += c0_inc, c1 += c1_inc, c++) {
1290
1291 switch (op[0]->type->base_type) {
1292 case GLSL_TYPE_INT:
1293 data.i[c] = op[0]->value.i[c0] ^ op[1]->value.i[c1];
1294 break;
1295 case GLSL_TYPE_UINT:
1296 data.u[c] = op[0]->value.u[c0] ^ op[1]->value.u[c1];
1297 break;
1298 default:
1299 assert(0);
1300 }
1301 }
1302 break;
1303
1304 case ir_unop_bitfield_reverse:
1305 /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
1306 for (unsigned c = 0; c < components; c++) {
1307 unsigned int v = op[0]->value.u[c]; // input bits to be reversed
1308 unsigned int r = v; // r will be reversed bits of v; first get LSB of v
1309 int s = sizeof(v) * CHAR_BIT - 1; // extra shift needed at end
1310
1311 for (v >>= 1; v; v >>= 1) {
1312 r <<= 1;
1313 r |= v & 1;
1314 s--;
1315 }
1316 r <<= s; // shift when v's highest bits are zero
1317
1318 data.u[c] = r;
1319 }
1320 break;
1321
1322 case ir_unop_bit_count:
1323 for (unsigned c = 0; c < components; c++) {
1324 unsigned count = 0;
1325 unsigned v = op[0]->value.u[c];
1326
1327 for (; v; count++) {
1328 v &= v - 1;
1329 }
1330 data.u[c] = count;
1331 }
1332 break;
1333
1334 case ir_unop_find_msb:
1335 for (unsigned c = 0; c < components; c++) {
1336 int v = op[0]->value.i[c];
1337
1338 if (v == 0 || (op[0]->type->base_type == GLSL_TYPE_INT && v == -1))
1339 data.i[c] = -1;
1340 else {
1341 int count = 0;
1342 int top_bit = op[0]->type->base_type == GLSL_TYPE_UINT
1343 ? 0 : v & (1 << 31);
1344
1345 while (((v & (1 << 31)) == top_bit) && count != 32) {
1346 count++;
1347 v <<= 1;
1348 }
1349
1350 data.i[c] = 31 - count;
1351 }
1352 }
1353 break;
1354
1355 case ir_unop_find_lsb:
1356 for (unsigned c = 0; c < components; c++) {
1357 if (op[0]->value.i[c] == 0)
1358 data.i[c] = -1;
1359 else {
1360 unsigned pos = 0;
1361 unsigned v = op[0]->value.u[c];
1362
1363 for (; !(v & 1); v >>= 1) {
1364 pos++;
1365 }
1366 data.u[c] = pos;
1367 }
1368 }
1369 break;
1370
1371 case ir_triop_bitfield_extract: {
1372 int offset = op[1]->value.i[0];
1373 int bits = op[2]->value.i[0];
1374
1375 for (unsigned c = 0; c < components; c++) {
1376 if (bits == 0)
1377 data.u[c] = 0;
1378 else if (offset < 0 || bits < 0)
1379 data.u[c] = 0; /* Undefined, per spec. */
1380 else if (offset + bits > 32)
1381 data.u[c] = 0; /* Undefined, per spec. */
1382 else {
1383 if (op[0]->type->base_type == GLSL_TYPE_INT) {
1384 /* int so that the right shift will sign-extend. */
1385 int value = op[0]->value.i[c];
1386 value <<= 32 - bits - offset;
1387 value >>= 32 - bits;
1388 data.i[c] = value;
1389 } else {
1390 unsigned value = op[0]->value.u[c];
1391 value <<= 32 - bits - offset;
1392 value >>= 32 - bits;
1393 data.u[c] = value;
1394 }
1395 }
1396 }
1397 break;
1398 }
1399
1400 case ir_binop_bfm: {
1401 int bits = op[0]->value.i[0];
1402 int offset = op[1]->value.i[0];
1403
1404 for (unsigned c = 0; c < components; c++) {
1405 if (bits == 0)
1406 data.u[c] = op[0]->value.u[c];
1407 else if (offset < 0 || bits < 0)
1408 data.u[c] = 0; /* Undefined for bitfieldInsert, per spec. */
1409 else if (offset + bits > 32)
1410 data.u[c] = 0; /* Undefined for bitfieldInsert, per spec. */
1411 else
1412 data.u[c] = ((1 << bits) - 1) << offset;
1413 }
1414 break;
1415 }
1416
1417 case ir_binop_ldexp:
1418 for (unsigned c = 0; c < components; c++) {
1419 data.f[c] = ldexp(op[0]->value.f[c], op[1]->value.i[c]);
1420 /* Flush subnormal values to zero. */
1421 if (!isnormal(data.f[c]))
1422 data.f[c] = copysign(0.0f, op[0]->value.f[c]);
1423 }
1424 break;
1425
1426 case ir_triop_fma:
1427 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
1428 assert(op[1]->type->base_type == GLSL_TYPE_FLOAT);
1429 assert(op[2]->type->base_type == GLSL_TYPE_FLOAT);
1430
1431 for (unsigned c = 0; c < components; c++) {
1432 data.f[c] = op[0]->value.f[c] * op[1]->value.f[c]
1433 + op[2]->value.f[c];
1434 }
1435 break;
1436
1437 case ir_triop_lrp: {
1438 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
1439 assert(op[1]->type->base_type == GLSL_TYPE_FLOAT);
1440 assert(op[2]->type->base_type == GLSL_TYPE_FLOAT);
1441
1442 unsigned c2_inc = op[2]->type->is_scalar() ? 0 : 1;
1443 for (unsigned c = 0, c2 = 0; c < components; c2 += c2_inc, c++) {
1444 data.f[c] = op[0]->value.f[c] * (1.0f - op[2]->value.f[c2]) +
1445 (op[1]->value.f[c] * op[2]->value.f[c2]);
1446 }
1447 break;
1448 }
1449
1450 case ir_triop_csel:
1451 for (unsigned c = 0; c < components; c++) {
1452 data.u[c] = op[0]->value.b[c] ? op[1]->value.u[c]
1453 : op[2]->value.u[c];
1454 }
1455 break;
1456
1457 case ir_triop_vector_insert: {
1458 const unsigned idx = op[2]->value.u[0];
1459
1460 memcpy(&data, &op[0]->value, sizeof(data));
1461
1462 switch (this->type->base_type) {
1463 case GLSL_TYPE_INT:
1464 data.i[idx] = op[1]->value.i[0];
1465 break;
1466 case GLSL_TYPE_UINT:
1467 data.u[idx] = op[1]->value.u[0];
1468 break;
1469 case GLSL_TYPE_FLOAT:
1470 data.f[idx] = op[1]->value.f[0];
1471 break;
1472 case GLSL_TYPE_BOOL:
1473 data.b[idx] = op[1]->value.b[0];
1474 break;
1475 default:
1476 assert(!"Should not get here.");
1477 break;
1478 }
1479 break;
1480 }
1481
1482 case ir_quadop_bitfield_insert: {
1483 int offset = op[2]->value.i[0];
1484 int bits = op[3]->value.i[0];
1485
1486 for (unsigned c = 0; c < components; c++) {
1487 if (bits == 0)
1488 data.u[c] = op[0]->value.u[c];
1489 else if (offset < 0 || bits < 0)
1490 data.u[c] = 0; /* Undefined, per spec. */
1491 else if (offset + bits > 32)
1492 data.u[c] = 0; /* Undefined, per spec. */
1493 else {
1494 unsigned insert_mask = ((1 << bits) - 1) << offset;
1495
1496 unsigned insert = op[1]->value.u[c];
1497 insert <<= offset;
1498 insert &= insert_mask;
1499
1500 unsigned base = op[0]->value.u[c];
1501 base &= ~insert_mask;
1502
1503 data.u[c] = base | insert;
1504 }
1505 }
1506 break;
1507 }
1508
1509 case ir_quadop_vector:
1510 for (unsigned c = 0; c < this->type->vector_elements; c++) {
1511 switch (this->type->base_type) {
1512 case GLSL_TYPE_INT:
1513 data.i[c] = op[c]->value.i[0];
1514 break;
1515 case GLSL_TYPE_UINT:
1516 data.u[c] = op[c]->value.u[0];
1517 break;
1518 case GLSL_TYPE_FLOAT:
1519 data.f[c] = op[c]->value.f[0];
1520 break;
1521 default:
1522 assert(0);
1523 }
1524 }
1525 break;
1526
1527 default:
1528 /* FINISHME: Should handle all expression types. */
1529 return NULL;
1530 }
1531
1532 return new(ctx) ir_constant(this->type, &data);
1533 }
1534
1535
1536 ir_constant *
1537 ir_texture::constant_expression_value(struct hash_table *variable_context)
1538 {
1539 /* texture lookups aren't constant expressions */
1540 return NULL;
1541 }
1542
1543
1544 ir_constant *
1545 ir_swizzle::constant_expression_value(struct hash_table *variable_context)
1546 {
1547 ir_constant *v = this->val->constant_expression_value(variable_context);
1548
1549 if (v != NULL) {
1550 ir_constant_data data = { { 0 } };
1551
1552 const unsigned swiz_idx[4] = {
1553 this->mask.x, this->mask.y, this->mask.z, this->mask.w
1554 };
1555
1556 for (unsigned i = 0; i < this->mask.num_components; i++) {
1557 switch (v->type->base_type) {
1558 case GLSL_TYPE_UINT:
1559 case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break;
1560 case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
1561 case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break;
1562 default: assert(!"Should not get here."); break;
1563 }
1564 }
1565
1566 void *ctx = ralloc_parent(this);
1567 return new(ctx) ir_constant(this->type, &data);
1568 }
1569 return NULL;
1570 }
1571
1572
1573 void
1574 ir_dereference_variable::constant_referenced(struct hash_table *variable_context,
1575 ir_constant *&store, int &offset) const
1576 {
1577 if (variable_context) {
1578 store = (ir_constant *)hash_table_find(variable_context, var);
1579 offset = 0;
1580 } else {
1581 store = NULL;
1582 offset = 0;
1583 }
1584 }
1585
1586 ir_constant *
1587 ir_dereference_variable::constant_expression_value(struct hash_table *variable_context)
1588 {
1589 /* This may occur during compile and var->type is glsl_type::error_type */
1590 if (!var)
1591 return NULL;
1592
1593 /* Give priority to the context hashtable, if it exists */
1594 if (variable_context) {
1595 ir_constant *value = (ir_constant *)hash_table_find(variable_context, var);
1596 if(value)
1597 return value;
1598 }
1599
1600 /* The constant_value of a uniform variable is its initializer,
1601 * not the lifetime constant value of the uniform.
1602 */
1603 if (var->data.mode == ir_var_uniform)
1604 return NULL;
1605
1606 if (!var->constant_value)
1607 return NULL;
1608
1609 return var->constant_value->clone(ralloc_parent(var), NULL);
1610 }
1611
1612
1613 void
1614 ir_dereference_array::constant_referenced(struct hash_table *variable_context,
1615 ir_constant *&store, int &offset) const
1616 {
1617 ir_constant *index_c = array_index->constant_expression_value(variable_context);
1618
1619 if (!index_c || !index_c->type->is_scalar() || !index_c->type->is_integer()) {
1620 store = 0;
1621 offset = 0;
1622 return;
1623 }
1624
1625 int index = index_c->type->base_type == GLSL_TYPE_INT ?
1626 index_c->get_int_component(0) :
1627 index_c->get_uint_component(0);
1628
1629 ir_constant *substore;
1630 int suboffset;
1631 const ir_dereference *deref = array->as_dereference();
1632 if (!deref) {
1633 store = 0;
1634 offset = 0;
1635 return;
1636 }
1637
1638 deref->constant_referenced(variable_context, substore, suboffset);
1639
1640 if (!substore) {
1641 store = 0;
1642 offset = 0;
1643 return;
1644 }
1645
1646 const glsl_type *vt = array->type;
1647 if (vt->is_array()) {
1648 store = substore->get_array_element(index);
1649 offset = 0;
1650 return;
1651 }
1652 if (vt->is_matrix()) {
1653 store = substore;
1654 offset = index * vt->vector_elements;
1655 return;
1656 }
1657 if (vt->is_vector()) {
1658 store = substore;
1659 offset = suboffset + index;
1660 return;
1661 }
1662
1663 store = 0;
1664 offset = 0;
1665 }
1666
1667 ir_constant *
1668 ir_dereference_array::constant_expression_value(struct hash_table *variable_context)
1669 {
1670 ir_constant *array = this->array->constant_expression_value(variable_context);
1671 ir_constant *idx = this->array_index->constant_expression_value(variable_context);
1672
1673 if ((array != NULL) && (idx != NULL)) {
1674 void *ctx = ralloc_parent(this);
1675 if (array->type->is_matrix()) {
1676 /* Array access of a matrix results in a vector.
1677 */
1678 const unsigned column = idx->value.u[0];
1679
1680 const glsl_type *const column_type = array->type->column_type();
1681
1682 /* Offset in the constant matrix to the first element of the column
1683 * to be extracted.
1684 */
1685 const unsigned mat_idx = column * column_type->vector_elements;
1686
1687 ir_constant_data data = { { 0 } };
1688
1689 switch (column_type->base_type) {
1690 case GLSL_TYPE_UINT:
1691 case GLSL_TYPE_INT:
1692 for (unsigned i = 0; i < column_type->vector_elements; i++)
1693 data.u[i] = array->value.u[mat_idx + i];
1694
1695 break;
1696
1697 case GLSL_TYPE_FLOAT:
1698 for (unsigned i = 0; i < column_type->vector_elements; i++)
1699 data.f[i] = array->value.f[mat_idx + i];
1700
1701 break;
1702
1703 default:
1704 assert(!"Should not get here.");
1705 break;
1706 }
1707
1708 return new(ctx) ir_constant(column_type, &data);
1709 } else if (array->type->is_vector()) {
1710 const unsigned component = idx->value.u[0];
1711
1712 return new(ctx) ir_constant(array, component);
1713 } else {
1714 const unsigned index = idx->value.u[0];
1715 return array->get_array_element(index)->clone(ctx, NULL);
1716 }
1717 }
1718 return NULL;
1719 }
1720
1721
1722 void
1723 ir_dereference_record::constant_referenced(struct hash_table *variable_context,
1724 ir_constant *&store, int &offset) const
1725 {
1726 ir_constant *substore;
1727 int suboffset;
1728 const ir_dereference *deref = record->as_dereference();
1729 if (!deref) {
1730 store = 0;
1731 offset = 0;
1732 return;
1733 }
1734
1735 deref->constant_referenced(variable_context, substore, suboffset);
1736
1737 if (!substore) {
1738 store = 0;
1739 offset = 0;
1740 return;
1741 }
1742
1743 store = substore->get_record_field(field);
1744 offset = 0;
1745 }
1746
1747 ir_constant *
1748 ir_dereference_record::constant_expression_value(struct hash_table *variable_context)
1749 {
1750 ir_constant *v = this->record->constant_expression_value();
1751
1752 return (v != NULL) ? v->get_record_field(this->field) : NULL;
1753 }
1754
1755
1756 ir_constant *
1757 ir_assignment::constant_expression_value(struct hash_table *variable_context)
1758 {
1759 /* FINISHME: Handle CEs involving assignment (return RHS) */
1760 return NULL;
1761 }
1762
1763
1764 ir_constant *
1765 ir_constant::constant_expression_value(struct hash_table *variable_context)
1766 {
1767 return this;
1768 }
1769
1770
1771 ir_constant *
1772 ir_call::constant_expression_value(struct hash_table *variable_context)
1773 {
1774 return this->callee->constant_expression_value(&this->actual_parameters, variable_context);
1775 }
1776
1777
1778 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list &body,
1779 struct hash_table *variable_context,
1780 ir_constant **result)
1781 {
1782 foreach_list(n, &body) {
1783 ir_instruction *inst = (ir_instruction *)n;
1784 switch(inst->ir_type) {
1785
1786 /* (declare () type symbol) */
1787 case ir_type_variable: {
1788 ir_variable *var = inst->as_variable();
1789 hash_table_insert(variable_context, ir_constant::zero(this, var->type), var);
1790 break;
1791 }
1792
1793 /* (assign [condition] (write-mask) (ref) (value)) */
1794 case ir_type_assignment: {
1795 ir_assignment *asg = inst->as_assignment();
1796 if (asg->condition) {
1797 ir_constant *cond = asg->condition->constant_expression_value(variable_context);
1798 if (!cond)
1799 return false;
1800 if (!cond->get_bool_component(0))
1801 break;
1802 }
1803
1804 ir_constant *store = NULL;
1805 int offset = 0;
1806 asg->lhs->constant_referenced(variable_context, store, offset);
1807
1808 if (!store)
1809 return false;
1810
1811 ir_constant *value = asg->rhs->constant_expression_value(variable_context);
1812
1813 if (!value)
1814 return false;
1815
1816 store->copy_masked_offset(value, offset, asg->write_mask);
1817 break;
1818 }
1819
1820 /* (return (expression)) */
1821 case ir_type_return:
1822 assert (result);
1823 *result = inst->as_return()->value->constant_expression_value(variable_context);
1824 return *result != NULL;
1825
1826 /* (call name (ref) (params))*/
1827 case ir_type_call: {
1828 ir_call *call = inst->as_call();
1829
1830 /* Just say no to void functions in constant expressions. We
1831 * don't need them at that point.
1832 */
1833
1834 if (!call->return_deref)
1835 return false;
1836
1837 ir_constant *store = NULL;
1838 int offset = 0;
1839 call->return_deref->constant_referenced(variable_context, store, offset);
1840
1841 if (!store)
1842 return false;
1843
1844 ir_constant *value = call->constant_expression_value(variable_context);
1845
1846 if(!value)
1847 return false;
1848
1849 store->copy_offset(value, offset);
1850 break;
1851 }
1852
1853 /* (if condition (then-instructions) (else-instructions)) */
1854 case ir_type_if: {
1855 ir_if *iif = inst->as_if();
1856
1857 ir_constant *cond = iif->condition->constant_expression_value(variable_context);
1858 if (!cond || !cond->type->is_boolean())
1859 return false;
1860
1861 exec_list &branch = cond->get_bool_component(0) ? iif->then_instructions : iif->else_instructions;
1862
1863 *result = NULL;
1864 if (!constant_expression_evaluate_expression_list(branch, variable_context, result))
1865 return false;
1866
1867 /* If there was a return in the branch chosen, drop out now. */
1868 if (*result)
1869 return true;
1870
1871 break;
1872 }
1873
1874 /* Every other expression type, we drop out. */
1875 default:
1876 return false;
1877 }
1878 }
1879
1880 /* Reaching the end of the block is not an error condition */
1881 if (result)
1882 *result = NULL;
1883
1884 return true;
1885 }
1886
1887 ir_constant *
1888 ir_function_signature::constant_expression_value(exec_list *actual_parameters, struct hash_table *variable_context)
1889 {
1890 const glsl_type *type = this->return_type;
1891 if (type == glsl_type::void_type)
1892 return NULL;
1893
1894 /* From the GLSL 1.20 spec, page 23:
1895 * "Function calls to user-defined functions (non-built-in functions)
1896 * cannot be used to form constant expressions."
1897 */
1898 if (!this->is_builtin())
1899 return NULL;
1900
1901 /*
1902 * Of the builtin functions, only the texture lookups and the noise
1903 * ones must not be used in constant expressions. They all include
1904 * specific opcodes so they don't need to be special-cased at this
1905 * point.
1906 */
1907
1908 /* Initialize the table of dereferencable names with the function
1909 * parameters. Verify their const-ness on the way.
1910 *
1911 * We expect the correctness of the number of parameters to have
1912 * been checked earlier.
1913 */
1914 hash_table *deref_hash = hash_table_ctor(8, hash_table_pointer_hash,
1915 hash_table_pointer_compare);
1916
1917 /* If "origin" is non-NULL, then the function body is there. So we
1918 * have to use the variable objects from the object with the body,
1919 * but the parameter instanciation on the current object.
1920 */
1921 const exec_node *parameter_info = origin ? origin->parameters.head : parameters.head;
1922
1923 foreach_list(n, actual_parameters) {
1924 ir_constant *constant = ((ir_rvalue *) n)->constant_expression_value(variable_context);
1925 if (constant == NULL) {
1926 hash_table_dtor(deref_hash);
1927 return NULL;
1928 }
1929
1930
1931 ir_variable *var = (ir_variable *)parameter_info;
1932 hash_table_insert(deref_hash, constant, var);
1933
1934 parameter_info = parameter_info->next;
1935 }
1936
1937 ir_constant *result = NULL;
1938
1939 /* Now run the builtin function until something non-constant
1940 * happens or we get the result.
1941 */
1942 if (constant_expression_evaluate_expression_list(origin ? origin->body : body, deref_hash, &result) && result)
1943 result = result->clone(ralloc_parent(this), NULL);
1944
1945 hash_table_dtor(deref_hash);
1946
1947 return result;
1948 }