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