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