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