glsl: Use find_msb_uint to implement ir_unop_find_lsb
[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 switch (op[0]->type->base_type) {
1564 case GLSL_TYPE_UINT:
1565 data.i[c] = find_msb_uint(op[0]->value.u[c] & -op[0]->value.u[c]);
1566 break;
1567 case GLSL_TYPE_INT:
1568 data.i[c] = find_msb_uint(op[0]->value.i[c] & -op[0]->value.i[c]);
1569 break;
1570 default:
1571 assert(0);
1572 }
1573 }
1574 break;
1575
1576 case ir_unop_saturate:
1577 for (unsigned c = 0; c < components; c++) {
1578 data.f[c] = CLAMP(op[0]->value.f[c], 0.0f, 1.0f);
1579 }
1580 break;
1581 case ir_unop_pack_double_2x32:
1582 /* XXX needs to be checked on big-endian */
1583 memcpy(&data.d[0], &op[0]->value.u[0], sizeof(double));
1584 break;
1585 case ir_unop_unpack_double_2x32:
1586 /* XXX needs to be checked on big-endian */
1587 memcpy(&data.u[0], &op[0]->value.d[0], sizeof(double));
1588 break;
1589
1590 case ir_triop_bitfield_extract: {
1591 for (unsigned c = 0; c < components; c++) {
1592 int offset = op[1]->value.i[c];
1593 int bits = op[2]->value.i[c];
1594
1595 if (bits == 0)
1596 data.u[c] = 0;
1597 else if (offset < 0 || bits < 0)
1598 data.u[c] = 0; /* Undefined, per spec. */
1599 else if (offset + bits > 32)
1600 data.u[c] = 0; /* Undefined, per spec. */
1601 else {
1602 if (op[0]->type->base_type == GLSL_TYPE_INT) {
1603 /* int so that the right shift will sign-extend. */
1604 int value = op[0]->value.i[c];
1605 value <<= 32 - bits - offset;
1606 value >>= 32 - bits;
1607 data.i[c] = value;
1608 } else {
1609 unsigned value = op[0]->value.u[c];
1610 value <<= 32 - bits - offset;
1611 value >>= 32 - bits;
1612 data.u[c] = value;
1613 }
1614 }
1615 }
1616 break;
1617 }
1618
1619 case ir_binop_ldexp:
1620 for (unsigned c = 0; c < components; c++) {
1621 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE) {
1622 data.d[c] = ldexp(op[0]->value.d[c], op[1]->value.i[c]);
1623 /* Flush subnormal values to zero. */
1624 if (!isnormal(data.d[c]))
1625 data.d[c] = copysign(0.0, op[0]->value.d[c]);
1626 } else {
1627 data.f[c] = ldexpf(op[0]->value.f[c], op[1]->value.i[c]);
1628 /* Flush subnormal values to zero. */
1629 if (!isnormal(data.f[c]))
1630 data.f[c] = copysignf(0.0f, op[0]->value.f[c]);
1631 }
1632 }
1633 break;
1634
1635 case ir_triop_fma:
1636 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT ||
1637 op[0]->type->base_type == GLSL_TYPE_DOUBLE);
1638 assert(op[1]->type->base_type == GLSL_TYPE_FLOAT ||
1639 op[1]->type->base_type == GLSL_TYPE_DOUBLE);
1640 assert(op[2]->type->base_type == GLSL_TYPE_FLOAT ||
1641 op[2]->type->base_type == GLSL_TYPE_DOUBLE);
1642
1643 for (unsigned c = 0; c < components; c++) {
1644 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
1645 data.d[c] = op[0]->value.d[c] * op[1]->value.d[c]
1646 + op[2]->value.d[c];
1647 else
1648 data.f[c] = op[0]->value.f[c] * op[1]->value.f[c]
1649 + op[2]->value.f[c];
1650 }
1651 break;
1652
1653 case ir_triop_lrp: {
1654 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT ||
1655 op[0]->type->base_type == GLSL_TYPE_DOUBLE);
1656 assert(op[1]->type->base_type == GLSL_TYPE_FLOAT ||
1657 op[1]->type->base_type == GLSL_TYPE_DOUBLE);
1658 assert(op[2]->type->base_type == GLSL_TYPE_FLOAT ||
1659 op[2]->type->base_type == GLSL_TYPE_DOUBLE);
1660
1661 unsigned c2_inc = op[2]->type->is_scalar() ? 0 : 1;
1662 for (unsigned c = 0, c2 = 0; c < components; c2 += c2_inc, c++) {
1663 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
1664 data.d[c] = op[0]->value.d[c] * (1.0 - op[2]->value.d[c2]) +
1665 (op[1]->value.d[c] * op[2]->value.d[c2]);
1666 else
1667 data.f[c] = op[0]->value.f[c] * (1.0f - op[2]->value.f[c2]) +
1668 (op[1]->value.f[c] * op[2]->value.f[c2]);
1669 }
1670 break;
1671 }
1672
1673 case ir_triop_csel:
1674 for (unsigned c = 0; c < components; c++) {
1675 if (op[1]->type->base_type == GLSL_TYPE_DOUBLE)
1676 data.d[c] = op[0]->value.b[c] ? op[1]->value.d[c]
1677 : op[2]->value.d[c];
1678 else
1679 data.u[c] = op[0]->value.b[c] ? op[1]->value.u[c]
1680 : op[2]->value.u[c];
1681 }
1682 break;
1683
1684 case ir_triop_vector_insert: {
1685 const unsigned idx = op[2]->value.u[0];
1686
1687 memcpy(&data, &op[0]->value, sizeof(data));
1688
1689 switch (this->type->base_type) {
1690 case GLSL_TYPE_INT:
1691 data.i[idx] = op[1]->value.i[0];
1692 break;
1693 case GLSL_TYPE_UINT:
1694 data.u[idx] = op[1]->value.u[0];
1695 break;
1696 case GLSL_TYPE_FLOAT:
1697 data.f[idx] = op[1]->value.f[0];
1698 break;
1699 case GLSL_TYPE_BOOL:
1700 data.b[idx] = op[1]->value.b[0];
1701 break;
1702 case GLSL_TYPE_DOUBLE:
1703 data.d[idx] = op[1]->value.d[0];
1704 break;
1705 default:
1706 assert(!"Should not get here.");
1707 break;
1708 }
1709 break;
1710 }
1711
1712 case ir_quadop_bitfield_insert: {
1713 for (unsigned c = 0; c < components; c++) {
1714 int offset = op[2]->value.i[c];
1715 int bits = op[3]->value.i[c];
1716
1717 if (bits == 0)
1718 data.u[c] = op[0]->value.u[c];
1719 else if (offset < 0 || bits < 0)
1720 data.u[c] = 0; /* Undefined, per spec. */
1721 else if (offset + bits > 32)
1722 data.u[c] = 0; /* Undefined, per spec. */
1723 else {
1724 unsigned insert_mask = ((1ull << bits) - 1) << offset;
1725
1726 unsigned insert = op[1]->value.u[c];
1727 insert <<= offset;
1728 insert &= insert_mask;
1729
1730 unsigned base = op[0]->value.u[c];
1731 base &= ~insert_mask;
1732
1733 data.u[c] = base | insert;
1734 }
1735 }
1736 break;
1737 }
1738
1739 case ir_quadop_vector:
1740 for (unsigned c = 0; c < this->type->vector_elements; c++) {
1741 switch (this->type->base_type) {
1742 case GLSL_TYPE_INT:
1743 data.i[c] = op[c]->value.i[0];
1744 break;
1745 case GLSL_TYPE_UINT:
1746 data.u[c] = op[c]->value.u[0];
1747 break;
1748 case GLSL_TYPE_FLOAT:
1749 data.f[c] = op[c]->value.f[0];
1750 break;
1751 case GLSL_TYPE_DOUBLE:
1752 data.d[c] = op[c]->value.d[0];
1753 break;
1754 case GLSL_TYPE_BOOL:
1755 data.b[c] = op[c]->value.b[0];
1756 break;
1757 default:
1758 assert(0);
1759 }
1760 }
1761 break;
1762
1763 default:
1764 /* FINISHME: Should handle all expression types. */
1765 return NULL;
1766 }
1767
1768 return new(ctx) ir_constant(this->type, &data);
1769 }
1770
1771
1772 ir_constant *
1773 ir_texture::constant_expression_value(struct hash_table *)
1774 {
1775 /* texture lookups aren't constant expressions */
1776 return NULL;
1777 }
1778
1779
1780 ir_constant *
1781 ir_swizzle::constant_expression_value(struct hash_table *variable_context)
1782 {
1783 ir_constant *v = this->val->constant_expression_value(variable_context);
1784
1785 if (v != NULL) {
1786 ir_constant_data data = { { 0 } };
1787
1788 const unsigned swiz_idx[4] = {
1789 this->mask.x, this->mask.y, this->mask.z, this->mask.w
1790 };
1791
1792 for (unsigned i = 0; i < this->mask.num_components; i++) {
1793 switch (v->type->base_type) {
1794 case GLSL_TYPE_UINT:
1795 case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break;
1796 case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
1797 case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break;
1798 case GLSL_TYPE_DOUBLE:data.d[i] = v->value.d[swiz_idx[i]]; break;
1799 default: assert(!"Should not get here."); break;
1800 }
1801 }
1802
1803 void *ctx = ralloc_parent(this);
1804 return new(ctx) ir_constant(this->type, &data);
1805 }
1806 return NULL;
1807 }
1808
1809
1810 ir_constant *
1811 ir_dereference_variable::constant_expression_value(struct hash_table *variable_context)
1812 {
1813 assert(var);
1814
1815 /* Give priority to the context hashtable, if it exists */
1816 if (variable_context) {
1817 ir_constant *value = (ir_constant *)hash_table_find(variable_context, var);
1818 if(value)
1819 return value;
1820 }
1821
1822 /* The constant_value of a uniform variable is its initializer,
1823 * not the lifetime constant value of the uniform.
1824 */
1825 if (var->data.mode == ir_var_uniform)
1826 return NULL;
1827
1828 if (!var->constant_value)
1829 return NULL;
1830
1831 return var->constant_value->clone(ralloc_parent(var), NULL);
1832 }
1833
1834
1835 ir_constant *
1836 ir_dereference_array::constant_expression_value(struct hash_table *variable_context)
1837 {
1838 ir_constant *array = this->array->constant_expression_value(variable_context);
1839 ir_constant *idx = this->array_index->constant_expression_value(variable_context);
1840
1841 if ((array != NULL) && (idx != NULL)) {
1842 void *ctx = ralloc_parent(this);
1843 if (array->type->is_matrix()) {
1844 /* Array access of a matrix results in a vector.
1845 */
1846 const unsigned column = idx->value.u[0];
1847
1848 const glsl_type *const column_type = array->type->column_type();
1849
1850 /* Offset in the constant matrix to the first element of the column
1851 * to be extracted.
1852 */
1853 const unsigned mat_idx = column * column_type->vector_elements;
1854
1855 ir_constant_data data = { { 0 } };
1856
1857 switch (column_type->base_type) {
1858 case GLSL_TYPE_UINT:
1859 case GLSL_TYPE_INT:
1860 for (unsigned i = 0; i < column_type->vector_elements; i++)
1861 data.u[i] = array->value.u[mat_idx + i];
1862
1863 break;
1864
1865 case GLSL_TYPE_FLOAT:
1866 for (unsigned i = 0; i < column_type->vector_elements; i++)
1867 data.f[i] = array->value.f[mat_idx + i];
1868
1869 break;
1870
1871 case GLSL_TYPE_DOUBLE:
1872 for (unsigned i = 0; i < column_type->vector_elements; i++)
1873 data.d[i] = array->value.d[mat_idx + i];
1874
1875 break;
1876
1877 default:
1878 assert(!"Should not get here.");
1879 break;
1880 }
1881
1882 return new(ctx) ir_constant(column_type, &data);
1883 } else if (array->type->is_vector()) {
1884 const unsigned component = idx->value.u[0];
1885
1886 return new(ctx) ir_constant(array, component);
1887 } else {
1888 const unsigned index = idx->value.u[0];
1889 return array->get_array_element(index)->clone(ctx, NULL);
1890 }
1891 }
1892 return NULL;
1893 }
1894
1895
1896 ir_constant *
1897 ir_dereference_record::constant_expression_value(struct hash_table *)
1898 {
1899 ir_constant *v = this->record->constant_expression_value();
1900
1901 return (v != NULL) ? v->get_record_field(this->field) : NULL;
1902 }
1903
1904
1905 ir_constant *
1906 ir_assignment::constant_expression_value(struct hash_table *)
1907 {
1908 /* FINISHME: Handle CEs involving assignment (return RHS) */
1909 return NULL;
1910 }
1911
1912
1913 ir_constant *
1914 ir_constant::constant_expression_value(struct hash_table *)
1915 {
1916 return this;
1917 }
1918
1919
1920 ir_constant *
1921 ir_call::constant_expression_value(struct hash_table *variable_context)
1922 {
1923 return this->callee->constant_expression_value(&this->actual_parameters, variable_context);
1924 }
1925
1926
1927 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list &body,
1928 struct hash_table *variable_context,
1929 ir_constant **result)
1930 {
1931 foreach_in_list(ir_instruction, inst, &body) {
1932 switch(inst->ir_type) {
1933
1934 /* (declare () type symbol) */
1935 case ir_type_variable: {
1936 ir_variable *var = inst->as_variable();
1937 hash_table_insert(variable_context, ir_constant::zero(this, var->type), var);
1938 break;
1939 }
1940
1941 /* (assign [condition] (write-mask) (ref) (value)) */
1942 case ir_type_assignment: {
1943 ir_assignment *asg = inst->as_assignment();
1944 if (asg->condition) {
1945 ir_constant *cond = asg->condition->constant_expression_value(variable_context);
1946 if (!cond)
1947 return false;
1948 if (!cond->get_bool_component(0))
1949 break;
1950 }
1951
1952 ir_constant *store = NULL;
1953 int offset = 0;
1954
1955 if (!constant_referenced(asg->lhs, variable_context, store, offset))
1956 return false;
1957
1958 ir_constant *value = asg->rhs->constant_expression_value(variable_context);
1959
1960 if (!value)
1961 return false;
1962
1963 store->copy_masked_offset(value, offset, asg->write_mask);
1964 break;
1965 }
1966
1967 /* (return (expression)) */
1968 case ir_type_return:
1969 assert (result);
1970 *result = inst->as_return()->value->constant_expression_value(variable_context);
1971 return *result != NULL;
1972
1973 /* (call name (ref) (params))*/
1974 case ir_type_call: {
1975 ir_call *call = inst->as_call();
1976
1977 /* Just say no to void functions in constant expressions. We
1978 * don't need them at that point.
1979 */
1980
1981 if (!call->return_deref)
1982 return false;
1983
1984 ir_constant *store = NULL;
1985 int offset = 0;
1986
1987 if (!constant_referenced(call->return_deref, variable_context,
1988 store, offset))
1989 return false;
1990
1991 ir_constant *value = call->constant_expression_value(variable_context);
1992
1993 if(!value)
1994 return false;
1995
1996 store->copy_offset(value, offset);
1997 break;
1998 }
1999
2000 /* (if condition (then-instructions) (else-instructions)) */
2001 case ir_type_if: {
2002 ir_if *iif = inst->as_if();
2003
2004 ir_constant *cond = iif->condition->constant_expression_value(variable_context);
2005 if (!cond || !cond->type->is_boolean())
2006 return false;
2007
2008 exec_list &branch = cond->get_bool_component(0) ? iif->then_instructions : iif->else_instructions;
2009
2010 *result = NULL;
2011 if (!constant_expression_evaluate_expression_list(branch, variable_context, result))
2012 return false;
2013
2014 /* If there was a return in the branch chosen, drop out now. */
2015 if (*result)
2016 return true;
2017
2018 break;
2019 }
2020
2021 /* Every other expression type, we drop out. */
2022 default:
2023 return false;
2024 }
2025 }
2026
2027 /* Reaching the end of the block is not an error condition */
2028 if (result)
2029 *result = NULL;
2030
2031 return true;
2032 }
2033
2034 ir_constant *
2035 ir_function_signature::constant_expression_value(exec_list *actual_parameters, struct hash_table *variable_context)
2036 {
2037 const glsl_type *type = this->return_type;
2038 if (type == glsl_type::void_type)
2039 return NULL;
2040
2041 /* From the GLSL 1.20 spec, page 23:
2042 * "Function calls to user-defined functions (non-built-in functions)
2043 * cannot be used to form constant expressions."
2044 */
2045 if (!this->is_builtin())
2046 return NULL;
2047
2048 /*
2049 * Of the builtin functions, only the texture lookups and the noise
2050 * ones must not be used in constant expressions. They all include
2051 * specific opcodes so they don't need to be special-cased at this
2052 * point.
2053 */
2054
2055 /* Initialize the table of dereferencable names with the function
2056 * parameters. Verify their const-ness on the way.
2057 *
2058 * We expect the correctness of the number of parameters to have
2059 * been checked earlier.
2060 */
2061 hash_table *deref_hash = hash_table_ctor(8, hash_table_pointer_hash,
2062 hash_table_pointer_compare);
2063
2064 /* If "origin" is non-NULL, then the function body is there. So we
2065 * have to use the variable objects from the object with the body,
2066 * but the parameter instanciation on the current object.
2067 */
2068 const exec_node *parameter_info = origin ? origin->parameters.get_head_raw() : parameters.get_head_raw();
2069
2070 foreach_in_list(ir_rvalue, n, actual_parameters) {
2071 ir_constant *constant = n->constant_expression_value(variable_context);
2072 if (constant == NULL) {
2073 hash_table_dtor(deref_hash);
2074 return NULL;
2075 }
2076
2077
2078 ir_variable *var = (ir_variable *)parameter_info;
2079 hash_table_insert(deref_hash, constant, var);
2080
2081 parameter_info = parameter_info->next;
2082 }
2083
2084 ir_constant *result = NULL;
2085
2086 /* Now run the builtin function until something non-constant
2087 * happens or we get the result.
2088 */
2089 if (constant_expression_evaluate_expression_list(origin ? origin->body : body, deref_hash, &result) && result)
2090 result = result->clone(ralloc_parent(this), NULL);
2091
2092 hash_table_dtor(deref_hash);
2093
2094 return result;
2095 }