ea9e84ab7fd8df945d27f09765beaffb7d8558d2
[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 *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 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 *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 *vt = da->array->type;
432 if (vt->is_array()) {
433 store = substore->get_array_element(index);
434 offset = 0;
435 break;
436 }
437 if (vt->is_matrix()) {
438 store = substore;
439 offset = index * vt->vector_elements;
440 break;
441 }
442 if (vt->is_vector()) {
443 store = substore;
444 offset = suboffset + index;
445 break;
446 }
447
448 break;
449 }
450
451 case ir_type_dereference_record: {
452 const ir_dereference_record *const dr =
453 (const ir_dereference_record *) deref;
454
455 const ir_dereference *const deref = dr->record->as_dereference();
456 if (!deref)
457 break;
458
459 ir_constant *substore;
460 int suboffset;
461
462 if (!constant_referenced(deref, variable_context, substore, suboffset))
463 break;
464
465 /* Since we're dropping it on the floor...
466 */
467 assert(suboffset == 0);
468
469 store = substore->get_record_field(dr->field);
470 break;
471 }
472
473 case ir_type_dereference_variable: {
474 const ir_dereference_variable *const dv =
475 (const ir_dereference_variable *) deref;
476
477 store = (ir_constant *) hash_table_find(variable_context, dv->var);
478 break;
479 }
480
481 default:
482 assert(!"Should not get here.");
483 break;
484 }
485
486 return store != NULL;
487 }
488
489
490 ir_constant *
491 ir_rvalue::constant_expression_value(struct hash_table *variable_context)
492 {
493 assert(this->type->is_error());
494 return NULL;
495 }
496
497 ir_constant *
498 ir_expression::constant_expression_value(struct hash_table *variable_context)
499 {
500 if (this->type->is_error())
501 return NULL;
502
503 ir_constant *op[Elements(this->operands)] = { NULL, };
504 ir_constant_data data;
505
506 memset(&data, 0, sizeof(data));
507
508 for (unsigned operand = 0; operand < this->get_num_operands(); operand++) {
509 op[operand] = this->operands[operand]->constant_expression_value(variable_context);
510 if (!op[operand])
511 return NULL;
512 }
513
514 if (op[1] != NULL)
515 switch (this->operation) {
516 case ir_binop_lshift:
517 case ir_binop_rshift:
518 case ir_binop_ldexp:
519 case ir_binop_vector_extract:
520 case ir_triop_csel:
521 case ir_triop_bitfield_extract:
522 break;
523
524 default:
525 assert(op[0]->type->base_type == op[1]->type->base_type);
526 break;
527 }
528
529 bool op0_scalar = op[0]->type->is_scalar();
530 bool op1_scalar = op[1] != NULL && op[1]->type->is_scalar();
531
532 /* When iterating over a vector or matrix's components, we want to increase
533 * the loop counter. However, for scalars, we want to stay at 0.
534 */
535 unsigned c0_inc = op0_scalar ? 0 : 1;
536 unsigned c1_inc = op1_scalar ? 0 : 1;
537 unsigned components;
538 if (op1_scalar || !op[1]) {
539 components = op[0]->type->components();
540 } else {
541 components = op[1]->type->components();
542 }
543
544 void *ctx = ralloc_parent(this);
545
546 /* Handle array operations here, rather than below. */
547 if (op[0]->type->is_array()) {
548 assert(op[1] != NULL && op[1]->type->is_array());
549 switch (this->operation) {
550 case ir_binop_all_equal:
551 return new(ctx) ir_constant(op[0]->has_value(op[1]));
552 case ir_binop_any_nequal:
553 return new(ctx) ir_constant(!op[0]->has_value(op[1]));
554 default:
555 break;
556 }
557 return NULL;
558 }
559
560 switch (this->operation) {
561 case ir_unop_bit_not:
562 switch (op[0]->type->base_type) {
563 case GLSL_TYPE_INT:
564 for (unsigned c = 0; c < components; c++)
565 data.i[c] = ~ op[0]->value.i[c];
566 break;
567 case GLSL_TYPE_UINT:
568 for (unsigned c = 0; c < components; c++)
569 data.u[c] = ~ op[0]->value.u[c];
570 break;
571 default:
572 assert(0);
573 }
574 break;
575
576 case ir_unop_logic_not:
577 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
578 for (unsigned c = 0; c < op[0]->type->components(); c++)
579 data.b[c] = !op[0]->value.b[c];
580 break;
581
582 case ir_unop_f2i:
583 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
584 for (unsigned c = 0; c < op[0]->type->components(); c++) {
585 data.i[c] = (int) op[0]->value.f[c];
586 }
587 break;
588 case ir_unop_f2u:
589 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
590 for (unsigned c = 0; c < op[0]->type->components(); c++) {
591 data.i[c] = (unsigned) op[0]->value.f[c];
592 }
593 break;
594 case ir_unop_i2f:
595 assert(op[0]->type->base_type == GLSL_TYPE_INT);
596 for (unsigned c = 0; c < op[0]->type->components(); c++) {
597 data.f[c] = (float) op[0]->value.i[c];
598 }
599 break;
600 case ir_unop_u2f:
601 assert(op[0]->type->base_type == GLSL_TYPE_UINT);
602 for (unsigned c = 0; c < op[0]->type->components(); c++) {
603 data.f[c] = (float) op[0]->value.u[c];
604 }
605 break;
606 case ir_unop_b2f:
607 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
608 for (unsigned c = 0; c < op[0]->type->components(); c++) {
609 data.f[c] = op[0]->value.b[c] ? 1.0F : 0.0F;
610 }
611 break;
612 case ir_unop_f2b:
613 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
614 for (unsigned c = 0; c < op[0]->type->components(); c++) {
615 data.b[c] = op[0]->value.f[c] != 0.0F ? true : false;
616 }
617 break;
618 case ir_unop_b2i:
619 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
620 for (unsigned c = 0; c < op[0]->type->components(); c++) {
621 data.u[c] = op[0]->value.b[c] ? 1 : 0;
622 }
623 break;
624 case ir_unop_i2b:
625 assert(op[0]->type->is_integer());
626 for (unsigned c = 0; c < op[0]->type->components(); c++) {
627 data.b[c] = op[0]->value.u[c] ? true : false;
628 }
629 break;
630 case ir_unop_u2i:
631 assert(op[0]->type->base_type == GLSL_TYPE_UINT);
632 for (unsigned c = 0; c < op[0]->type->components(); c++) {
633 data.i[c] = op[0]->value.u[c];
634 }
635 break;
636 case ir_unop_i2u:
637 assert(op[0]->type->base_type == GLSL_TYPE_INT);
638 for (unsigned c = 0; c < op[0]->type->components(); c++) {
639 data.u[c] = op[0]->value.i[c];
640 }
641 break;
642 case ir_unop_bitcast_i2f:
643 assert(op[0]->type->base_type == GLSL_TYPE_INT);
644 for (unsigned c = 0; c < op[0]->type->components(); c++) {
645 data.f[c] = bitcast_u2f(op[0]->value.i[c]);
646 }
647 break;
648 case ir_unop_bitcast_f2i:
649 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
650 for (unsigned c = 0; c < op[0]->type->components(); c++) {
651 data.i[c] = bitcast_f2u(op[0]->value.f[c]);
652 }
653 break;
654 case ir_unop_bitcast_u2f:
655 assert(op[0]->type->base_type == GLSL_TYPE_UINT);
656 for (unsigned c = 0; c < op[0]->type->components(); c++) {
657 data.f[c] = bitcast_u2f(op[0]->value.u[c]);
658 }
659 break;
660 case ir_unop_bitcast_f2u:
661 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
662 for (unsigned c = 0; c < op[0]->type->components(); c++) {
663 data.u[c] = bitcast_f2u(op[0]->value.f[c]);
664 }
665 break;
666 case ir_unop_any:
667 assert(op[0]->type->is_boolean());
668 data.b[0] = false;
669 for (unsigned c = 0; c < op[0]->type->components(); c++) {
670 if (op[0]->value.b[c])
671 data.b[0] = true;
672 }
673 break;
674
675 case ir_unop_trunc:
676 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
677 for (unsigned c = 0; c < op[0]->type->components(); c++) {
678 data.f[c] = truncf(op[0]->value.f[c]);
679 }
680 break;
681
682 case ir_unop_round_even:
683 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
684 for (unsigned c = 0; c < op[0]->type->components(); c++) {
685 data.f[c] = _mesa_round_to_even(op[0]->value.f[c]);
686 }
687 break;
688
689 case ir_unop_ceil:
690 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
691 for (unsigned c = 0; c < op[0]->type->components(); c++) {
692 data.f[c] = ceilf(op[0]->value.f[c]);
693 }
694 break;
695
696 case ir_unop_floor:
697 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
698 for (unsigned c = 0; c < op[0]->type->components(); c++) {
699 data.f[c] = floorf(op[0]->value.f[c]);
700 }
701 break;
702
703 case ir_unop_fract:
704 for (unsigned c = 0; c < op[0]->type->components(); c++) {
705 switch (this->type->base_type) {
706 case GLSL_TYPE_UINT:
707 data.u[c] = 0;
708 break;
709 case GLSL_TYPE_INT:
710 data.i[c] = 0;
711 break;
712 case GLSL_TYPE_FLOAT:
713 data.f[c] = op[0]->value.f[c] - floor(op[0]->value.f[c]);
714 break;
715 default:
716 assert(0);
717 }
718 }
719 break;
720
721 case ir_unop_sin:
722 case ir_unop_sin_reduced:
723 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
724 for (unsigned c = 0; c < op[0]->type->components(); c++) {
725 data.f[c] = sinf(op[0]->value.f[c]);
726 }
727 break;
728
729 case ir_unop_cos:
730 case ir_unop_cos_reduced:
731 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
732 for (unsigned c = 0; c < op[0]->type->components(); c++) {
733 data.f[c] = cosf(op[0]->value.f[c]);
734 }
735 break;
736
737 case ir_unop_neg:
738 for (unsigned c = 0; c < op[0]->type->components(); c++) {
739 switch (this->type->base_type) {
740 case GLSL_TYPE_UINT:
741 data.u[c] = -((int) op[0]->value.u[c]);
742 break;
743 case GLSL_TYPE_INT:
744 data.i[c] = -op[0]->value.i[c];
745 break;
746 case GLSL_TYPE_FLOAT:
747 data.f[c] = -op[0]->value.f[c];
748 break;
749 default:
750 assert(0);
751 }
752 }
753 break;
754
755 case ir_unop_abs:
756 for (unsigned c = 0; c < op[0]->type->components(); c++) {
757 switch (this->type->base_type) {
758 case GLSL_TYPE_UINT:
759 data.u[c] = op[0]->value.u[c];
760 break;
761 case GLSL_TYPE_INT:
762 data.i[c] = op[0]->value.i[c];
763 if (data.i[c] < 0)
764 data.i[c] = -data.i[c];
765 break;
766 case GLSL_TYPE_FLOAT:
767 data.f[c] = fabs(op[0]->value.f[c]);
768 break;
769 default:
770 assert(0);
771 }
772 }
773 break;
774
775 case ir_unop_sign:
776 for (unsigned c = 0; c < op[0]->type->components(); c++) {
777 switch (this->type->base_type) {
778 case GLSL_TYPE_UINT:
779 data.u[c] = op[0]->value.i[c] > 0;
780 break;
781 case GLSL_TYPE_INT:
782 data.i[c] = (op[0]->value.i[c] > 0) - (op[0]->value.i[c] < 0);
783 break;
784 case GLSL_TYPE_FLOAT:
785 data.f[c] = float((op[0]->value.f[c] > 0)-(op[0]->value.f[c] < 0));
786 break;
787 default:
788 assert(0);
789 }
790 }
791 break;
792
793 case ir_unop_rcp:
794 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
795 for (unsigned c = 0; c < op[0]->type->components(); c++) {
796 switch (this->type->base_type) {
797 case GLSL_TYPE_UINT:
798 if (op[0]->value.u[c] != 0.0)
799 data.u[c] = 1 / op[0]->value.u[c];
800 break;
801 case GLSL_TYPE_INT:
802 if (op[0]->value.i[c] != 0.0)
803 data.i[c] = 1 / op[0]->value.i[c];
804 break;
805 case GLSL_TYPE_FLOAT:
806 if (op[0]->value.f[c] != 0.0)
807 data.f[c] = 1.0F / op[0]->value.f[c];
808 break;
809 default:
810 assert(0);
811 }
812 }
813 break;
814
815 case ir_unop_rsq:
816 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
817 for (unsigned c = 0; c < op[0]->type->components(); c++) {
818 data.f[c] = 1.0F / sqrtf(op[0]->value.f[c]);
819 }
820 break;
821
822 case ir_unop_sqrt:
823 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
824 for (unsigned c = 0; c < op[0]->type->components(); c++) {
825 data.f[c] = sqrtf(op[0]->value.f[c]);
826 }
827 break;
828
829 case ir_unop_exp:
830 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
831 for (unsigned c = 0; c < op[0]->type->components(); c++) {
832 data.f[c] = expf(op[0]->value.f[c]);
833 }
834 break;
835
836 case ir_unop_exp2:
837 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
838 for (unsigned c = 0; c < op[0]->type->components(); c++) {
839 data.f[c] = exp2f(op[0]->value.f[c]);
840 }
841 break;
842
843 case ir_unop_log:
844 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
845 for (unsigned c = 0; c < op[0]->type->components(); c++) {
846 data.f[c] = logf(op[0]->value.f[c]);
847 }
848 break;
849
850 case ir_unop_log2:
851 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
852 for (unsigned c = 0; c < op[0]->type->components(); c++) {
853 data.f[c] = log2f(op[0]->value.f[c]);
854 }
855 break;
856
857 case ir_unop_dFdx:
858 case ir_unop_dFdy:
859 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
860 for (unsigned c = 0; c < op[0]->type->components(); c++) {
861 data.f[c] = 0.0;
862 }
863 break;
864
865 case ir_unop_pack_snorm_2x16:
866 assert(op[0]->type == glsl_type::vec2_type);
867 data.u[0] = pack_2x16(pack_snorm_1x16,
868 op[0]->value.f[0],
869 op[0]->value.f[1]);
870 break;
871 case ir_unop_pack_snorm_4x8:
872 assert(op[0]->type == glsl_type::vec4_type);
873 data.u[0] = pack_4x8(pack_snorm_1x8,
874 op[0]->value.f[0],
875 op[0]->value.f[1],
876 op[0]->value.f[2],
877 op[0]->value.f[3]);
878 break;
879 case ir_unop_unpack_snorm_2x16:
880 assert(op[0]->type == glsl_type::uint_type);
881 unpack_2x16(unpack_snorm_1x16,
882 op[0]->value.u[0],
883 &data.f[0], &data.f[1]);
884 break;
885 case ir_unop_unpack_snorm_4x8:
886 assert(op[0]->type == glsl_type::uint_type);
887 unpack_4x8(unpack_snorm_1x8,
888 op[0]->value.u[0],
889 &data.f[0], &data.f[1], &data.f[2], &data.f[3]);
890 break;
891 case ir_unop_pack_unorm_2x16:
892 assert(op[0]->type == glsl_type::vec2_type);
893 data.u[0] = pack_2x16(pack_unorm_1x16,
894 op[0]->value.f[0],
895 op[0]->value.f[1]);
896 break;
897 case ir_unop_pack_unorm_4x8:
898 assert(op[0]->type == glsl_type::vec4_type);
899 data.u[0] = pack_4x8(pack_unorm_1x8,
900 op[0]->value.f[0],
901 op[0]->value.f[1],
902 op[0]->value.f[2],
903 op[0]->value.f[3]);
904 break;
905 case ir_unop_unpack_unorm_2x16:
906 assert(op[0]->type == glsl_type::uint_type);
907 unpack_2x16(unpack_unorm_1x16,
908 op[0]->value.u[0],
909 &data.f[0], &data.f[1]);
910 break;
911 case ir_unop_unpack_unorm_4x8:
912 assert(op[0]->type == glsl_type::uint_type);
913 unpack_4x8(unpack_unorm_1x8,
914 op[0]->value.u[0],
915 &data.f[0], &data.f[1], &data.f[2], &data.f[3]);
916 break;
917 case ir_unop_pack_half_2x16:
918 assert(op[0]->type == glsl_type::vec2_type);
919 data.u[0] = pack_2x16(pack_half_1x16,
920 op[0]->value.f[0],
921 op[0]->value.f[1]);
922 break;
923 case ir_unop_unpack_half_2x16:
924 assert(op[0]->type == glsl_type::uint_type);
925 unpack_2x16(unpack_half_1x16,
926 op[0]->value.u[0],
927 &data.f[0], &data.f[1]);
928 break;
929 case ir_binop_pow:
930 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
931 for (unsigned c = 0; c < op[0]->type->components(); c++) {
932 data.f[c] = powf(op[0]->value.f[c], op[1]->value.f[c]);
933 }
934 break;
935
936 case ir_binop_dot:
937 data.f[0] = dot(op[0], op[1]);
938 break;
939
940 case ir_binop_min:
941 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
942 for (unsigned c = 0, c0 = 0, c1 = 0;
943 c < components;
944 c0 += c0_inc, c1 += c1_inc, c++) {
945
946 switch (op[0]->type->base_type) {
947 case GLSL_TYPE_UINT:
948 data.u[c] = MIN2(op[0]->value.u[c0], op[1]->value.u[c1]);
949 break;
950 case GLSL_TYPE_INT:
951 data.i[c] = MIN2(op[0]->value.i[c0], op[1]->value.i[c1]);
952 break;
953 case GLSL_TYPE_FLOAT:
954 data.f[c] = MIN2(op[0]->value.f[c0], op[1]->value.f[c1]);
955 break;
956 default:
957 assert(0);
958 }
959 }
960
961 break;
962 case ir_binop_max:
963 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
964 for (unsigned c = 0, c0 = 0, c1 = 0;
965 c < components;
966 c0 += c0_inc, c1 += c1_inc, c++) {
967
968 switch (op[0]->type->base_type) {
969 case GLSL_TYPE_UINT:
970 data.u[c] = MAX2(op[0]->value.u[c0], op[1]->value.u[c1]);
971 break;
972 case GLSL_TYPE_INT:
973 data.i[c] = MAX2(op[0]->value.i[c0], op[1]->value.i[c1]);
974 break;
975 case GLSL_TYPE_FLOAT:
976 data.f[c] = MAX2(op[0]->value.f[c0], op[1]->value.f[c1]);
977 break;
978 default:
979 assert(0);
980 }
981 }
982 break;
983
984 case ir_binop_add:
985 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
986 for (unsigned c = 0, c0 = 0, c1 = 0;
987 c < components;
988 c0 += c0_inc, c1 += c1_inc, c++) {
989
990 switch (op[0]->type->base_type) {
991 case GLSL_TYPE_UINT:
992 data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1];
993 break;
994 case GLSL_TYPE_INT:
995 data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1];
996 break;
997 case GLSL_TYPE_FLOAT:
998 data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1];
999 break;
1000 default:
1001 assert(0);
1002 }
1003 }
1004
1005 break;
1006 case ir_binop_sub:
1007 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
1008 for (unsigned c = 0, c0 = 0, c1 = 0;
1009 c < components;
1010 c0 += c0_inc, c1 += c1_inc, c++) {
1011
1012 switch (op[0]->type->base_type) {
1013 case GLSL_TYPE_UINT:
1014 data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1];
1015 break;
1016 case GLSL_TYPE_INT:
1017 data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1];
1018 break;
1019 case GLSL_TYPE_FLOAT:
1020 data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1];
1021 break;
1022 default:
1023 assert(0);
1024 }
1025 }
1026
1027 break;
1028 case ir_binop_mul:
1029 /* Check for equal types, or unequal types involving scalars */
1030 if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix())
1031 || op0_scalar || op1_scalar) {
1032 for (unsigned c = 0, c0 = 0, c1 = 0;
1033 c < components;
1034 c0 += c0_inc, c1 += c1_inc, c++) {
1035
1036 switch (op[0]->type->base_type) {
1037 case GLSL_TYPE_UINT:
1038 data.u[c] = op[0]->value.u[c0] * op[1]->value.u[c1];
1039 break;
1040 case GLSL_TYPE_INT:
1041 data.i[c] = op[0]->value.i[c0] * op[1]->value.i[c1];
1042 break;
1043 case GLSL_TYPE_FLOAT:
1044 data.f[c] = op[0]->value.f[c0] * op[1]->value.f[c1];
1045 break;
1046 default:
1047 assert(0);
1048 }
1049 }
1050 } else {
1051 assert(op[0]->type->is_matrix() || op[1]->type->is_matrix());
1052
1053 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
1054 * matrix can be a GLSL vector, either N or P can be 1.
1055 *
1056 * For vec*mat, the vector is treated as a row vector. This
1057 * means the vector is a 1-row x M-column matrix.
1058 *
1059 * For mat*vec, the vector is treated as a column vector. Since
1060 * matrix_columns is 1 for vectors, this just works.
1061 */
1062 const unsigned n = op[0]->type->is_vector()
1063 ? 1 : op[0]->type->vector_elements;
1064 const unsigned m = op[1]->type->vector_elements;
1065 const unsigned p = op[1]->type->matrix_columns;
1066 for (unsigned j = 0; j < p; j++) {
1067 for (unsigned i = 0; i < n; i++) {
1068 for (unsigned k = 0; k < m; k++) {
1069 data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j];
1070 }
1071 }
1072 }
1073 }
1074
1075 break;
1076 case ir_binop_div:
1077 /* FINISHME: Emit warning when division-by-zero is detected. */
1078 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
1079 for (unsigned c = 0, c0 = 0, c1 = 0;
1080 c < components;
1081 c0 += c0_inc, c1 += c1_inc, c++) {
1082
1083 switch (op[0]->type->base_type) {
1084 case GLSL_TYPE_UINT:
1085 if (op[1]->value.u[c1] == 0) {
1086 data.u[c] = 0;
1087 } else {
1088 data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1];
1089 }
1090 break;
1091 case GLSL_TYPE_INT:
1092 if (op[1]->value.i[c1] == 0) {
1093 data.i[c] = 0;
1094 } else {
1095 data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1];
1096 }
1097 break;
1098 case GLSL_TYPE_FLOAT:
1099 data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1];
1100 break;
1101 default:
1102 assert(0);
1103 }
1104 }
1105
1106 break;
1107 case ir_binop_mod:
1108 /* FINISHME: Emit warning when division-by-zero is detected. */
1109 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
1110 for (unsigned c = 0, c0 = 0, c1 = 0;
1111 c < components;
1112 c0 += c0_inc, c1 += c1_inc, c++) {
1113
1114 switch (op[0]->type->base_type) {
1115 case GLSL_TYPE_UINT:
1116 if (op[1]->value.u[c1] == 0) {
1117 data.u[c] = 0;
1118 } else {
1119 data.u[c] = op[0]->value.u[c0] % op[1]->value.u[c1];
1120 }
1121 break;
1122 case GLSL_TYPE_INT:
1123 if (op[1]->value.i[c1] == 0) {
1124 data.i[c] = 0;
1125 } else {
1126 data.i[c] = op[0]->value.i[c0] % op[1]->value.i[c1];
1127 }
1128 break;
1129 case GLSL_TYPE_FLOAT:
1130 /* We don't use fmod because it rounds toward zero; GLSL specifies
1131 * the use of floor.
1132 */
1133 data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1]
1134 * floorf(op[0]->value.f[c0] / op[1]->value.f[c1]);
1135 break;
1136 default:
1137 assert(0);
1138 }
1139 }
1140
1141 break;
1142
1143 case ir_binop_logic_and:
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_xor:
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 case ir_binop_logic_or:
1154 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
1155 for (unsigned c = 0; c < op[0]->type->components(); c++)
1156 data.b[c] = op[0]->value.b[c] || op[1]->value.b[c];
1157 break;
1158
1159 case ir_binop_less:
1160 assert(op[0]->type == op[1]->type);
1161 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1162 switch (op[0]->type->base_type) {
1163 case GLSL_TYPE_UINT:
1164 data.b[c] = op[0]->value.u[c] < op[1]->value.u[c];
1165 break;
1166 case GLSL_TYPE_INT:
1167 data.b[c] = op[0]->value.i[c] < op[1]->value.i[c];
1168 break;
1169 case GLSL_TYPE_FLOAT:
1170 data.b[c] = op[0]->value.f[c] < op[1]->value.f[c];
1171 break;
1172 default:
1173 assert(0);
1174 }
1175 }
1176 break;
1177 case ir_binop_greater:
1178 assert(op[0]->type == op[1]->type);
1179 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1180 switch (op[0]->type->base_type) {
1181 case GLSL_TYPE_UINT:
1182 data.b[c] = op[0]->value.u[c] > op[1]->value.u[c];
1183 break;
1184 case GLSL_TYPE_INT:
1185 data.b[c] = op[0]->value.i[c] > op[1]->value.i[c];
1186 break;
1187 case GLSL_TYPE_FLOAT:
1188 data.b[c] = op[0]->value.f[c] > op[1]->value.f[c];
1189 break;
1190 default:
1191 assert(0);
1192 }
1193 }
1194 break;
1195 case ir_binop_lequal:
1196 assert(op[0]->type == op[1]->type);
1197 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1198 switch (op[0]->type->base_type) {
1199 case GLSL_TYPE_UINT:
1200 data.b[c] = op[0]->value.u[c] <= op[1]->value.u[c];
1201 break;
1202 case GLSL_TYPE_INT:
1203 data.b[c] = op[0]->value.i[c] <= op[1]->value.i[c];
1204 break;
1205 case GLSL_TYPE_FLOAT:
1206 data.b[c] = op[0]->value.f[c] <= op[1]->value.f[c];
1207 break;
1208 default:
1209 assert(0);
1210 }
1211 }
1212 break;
1213 case ir_binop_gequal:
1214 assert(op[0]->type == op[1]->type);
1215 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1216 switch (op[0]->type->base_type) {
1217 case GLSL_TYPE_UINT:
1218 data.b[c] = op[0]->value.u[c] >= op[1]->value.u[c];
1219 break;
1220 case GLSL_TYPE_INT:
1221 data.b[c] = op[0]->value.i[c] >= op[1]->value.i[c];
1222 break;
1223 case GLSL_TYPE_FLOAT:
1224 data.b[c] = op[0]->value.f[c] >= op[1]->value.f[c];
1225 break;
1226 default:
1227 assert(0);
1228 }
1229 }
1230 break;
1231 case ir_binop_equal:
1232 assert(op[0]->type == op[1]->type);
1233 for (unsigned c = 0; c < components; c++) {
1234 switch (op[0]->type->base_type) {
1235 case GLSL_TYPE_UINT:
1236 data.b[c] = op[0]->value.u[c] == op[1]->value.u[c];
1237 break;
1238 case GLSL_TYPE_INT:
1239 data.b[c] = op[0]->value.i[c] == op[1]->value.i[c];
1240 break;
1241 case GLSL_TYPE_FLOAT:
1242 data.b[c] = op[0]->value.f[c] == op[1]->value.f[c];
1243 break;
1244 case GLSL_TYPE_BOOL:
1245 data.b[c] = op[0]->value.b[c] == op[1]->value.b[c];
1246 break;
1247 default:
1248 assert(0);
1249 }
1250 }
1251 break;
1252 case ir_binop_nequal:
1253 assert(op[0]->type == op[1]->type);
1254 for (unsigned c = 0; c < components; c++) {
1255 switch (op[0]->type->base_type) {
1256 case GLSL_TYPE_UINT:
1257 data.b[c] = op[0]->value.u[c] != op[1]->value.u[c];
1258 break;
1259 case GLSL_TYPE_INT:
1260 data.b[c] = op[0]->value.i[c] != op[1]->value.i[c];
1261 break;
1262 case GLSL_TYPE_FLOAT:
1263 data.b[c] = op[0]->value.f[c] != op[1]->value.f[c];
1264 break;
1265 case GLSL_TYPE_BOOL:
1266 data.b[c] = op[0]->value.b[c] != op[1]->value.b[c];
1267 break;
1268 default:
1269 assert(0);
1270 }
1271 }
1272 break;
1273 case ir_binop_all_equal:
1274 data.b[0] = op[0]->has_value(op[1]);
1275 break;
1276 case ir_binop_any_nequal:
1277 data.b[0] = !op[0]->has_value(op[1]);
1278 break;
1279
1280 case ir_binop_lshift:
1281 for (unsigned c = 0, c0 = 0, c1 = 0;
1282 c < components;
1283 c0 += c0_inc, c1 += c1_inc, c++) {
1284
1285 if (op[0]->type->base_type == GLSL_TYPE_INT &&
1286 op[1]->type->base_type == GLSL_TYPE_INT) {
1287 data.i[c] = op[0]->value.i[c0] << op[1]->value.i[c1];
1288
1289 } else if (op[0]->type->base_type == GLSL_TYPE_INT &&
1290 op[1]->type->base_type == GLSL_TYPE_UINT) {
1291 data.i[c] = op[0]->value.i[c0] << op[1]->value.u[c1];
1292
1293 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
1294 op[1]->type->base_type == GLSL_TYPE_INT) {
1295 data.u[c] = op[0]->value.u[c0] << op[1]->value.i[c1];
1296
1297 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
1298 op[1]->type->base_type == GLSL_TYPE_UINT) {
1299 data.u[c] = op[0]->value.u[c0] << op[1]->value.u[c1];
1300 }
1301 }
1302 break;
1303
1304 case ir_binop_rshift:
1305 for (unsigned c = 0, c0 = 0, c1 = 0;
1306 c < components;
1307 c0 += c0_inc, c1 += c1_inc, c++) {
1308
1309 if (op[0]->type->base_type == GLSL_TYPE_INT &&
1310 op[1]->type->base_type == GLSL_TYPE_INT) {
1311 data.i[c] = op[0]->value.i[c0] >> op[1]->value.i[c1];
1312
1313 } else if (op[0]->type->base_type == GLSL_TYPE_INT &&
1314 op[1]->type->base_type == GLSL_TYPE_UINT) {
1315 data.i[c] = op[0]->value.i[c0] >> op[1]->value.u[c1];
1316
1317 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
1318 op[1]->type->base_type == GLSL_TYPE_INT) {
1319 data.u[c] = op[0]->value.u[c0] >> op[1]->value.i[c1];
1320
1321 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
1322 op[1]->type->base_type == GLSL_TYPE_UINT) {
1323 data.u[c] = op[0]->value.u[c0] >> op[1]->value.u[c1];
1324 }
1325 }
1326 break;
1327
1328 case ir_binop_bit_and:
1329 for (unsigned c = 0, c0 = 0, c1 = 0;
1330 c < components;
1331 c0 += c0_inc, c1 += c1_inc, c++) {
1332
1333 switch (op[0]->type->base_type) {
1334 case GLSL_TYPE_INT:
1335 data.i[c] = op[0]->value.i[c0] & op[1]->value.i[c1];
1336 break;
1337 case GLSL_TYPE_UINT:
1338 data.u[c] = op[0]->value.u[c0] & op[1]->value.u[c1];
1339 break;
1340 default:
1341 assert(0);
1342 }
1343 }
1344 break;
1345
1346 case ir_binop_bit_or:
1347 for (unsigned c = 0, c0 = 0, c1 = 0;
1348 c < components;
1349 c0 += c0_inc, c1 += c1_inc, c++) {
1350
1351 switch (op[0]->type->base_type) {
1352 case GLSL_TYPE_INT:
1353 data.i[c] = op[0]->value.i[c0] | op[1]->value.i[c1];
1354 break;
1355 case GLSL_TYPE_UINT:
1356 data.u[c] = op[0]->value.u[c0] | op[1]->value.u[c1];
1357 break;
1358 default:
1359 assert(0);
1360 }
1361 }
1362 break;
1363
1364 case ir_binop_vector_extract: {
1365 const int c = CLAMP(op[1]->value.i[0], 0,
1366 (int) op[0]->type->vector_elements - 1);
1367
1368 switch (op[0]->type->base_type) {
1369 case GLSL_TYPE_UINT:
1370 data.u[0] = op[0]->value.u[c];
1371 break;
1372 case GLSL_TYPE_INT:
1373 data.i[0] = op[0]->value.i[c];
1374 break;
1375 case GLSL_TYPE_FLOAT:
1376 data.f[0] = op[0]->value.f[c];
1377 break;
1378 case GLSL_TYPE_BOOL:
1379 data.b[0] = op[0]->value.b[c];
1380 break;
1381 default:
1382 assert(0);
1383 }
1384 break;
1385 }
1386
1387 case ir_binop_bit_xor:
1388 for (unsigned c = 0, c0 = 0, c1 = 0;
1389 c < components;
1390 c0 += c0_inc, c1 += c1_inc, c++) {
1391
1392 switch (op[0]->type->base_type) {
1393 case GLSL_TYPE_INT:
1394 data.i[c] = op[0]->value.i[c0] ^ op[1]->value.i[c1];
1395 break;
1396 case GLSL_TYPE_UINT:
1397 data.u[c] = op[0]->value.u[c0] ^ op[1]->value.u[c1];
1398 break;
1399 default:
1400 assert(0);
1401 }
1402 }
1403 break;
1404
1405 case ir_unop_bitfield_reverse:
1406 /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
1407 for (unsigned c = 0; c < components; c++) {
1408 unsigned int v = op[0]->value.u[c]; // input bits to be reversed
1409 unsigned int r = v; // r will be reversed bits of v; first get LSB of v
1410 int s = sizeof(v) * CHAR_BIT - 1; // extra shift needed at end
1411
1412 for (v >>= 1; v; v >>= 1) {
1413 r <<= 1;
1414 r |= v & 1;
1415 s--;
1416 }
1417 r <<= s; // shift when v's highest bits are zero
1418
1419 data.u[c] = r;
1420 }
1421 break;
1422
1423 case ir_unop_bit_count:
1424 for (unsigned c = 0; c < components; c++) {
1425 unsigned count = 0;
1426 unsigned v = op[0]->value.u[c];
1427
1428 for (; v; count++) {
1429 v &= v - 1;
1430 }
1431 data.u[c] = count;
1432 }
1433 break;
1434
1435 case ir_unop_find_msb:
1436 for (unsigned c = 0; c < components; c++) {
1437 int v = op[0]->value.i[c];
1438
1439 if (v == 0 || (op[0]->type->base_type == GLSL_TYPE_INT && v == -1))
1440 data.i[c] = -1;
1441 else {
1442 int count = 0;
1443 int top_bit = op[0]->type->base_type == GLSL_TYPE_UINT
1444 ? 0 : v & (1 << 31);
1445
1446 while (((v & (1 << 31)) == top_bit) && count != 32) {
1447 count++;
1448 v <<= 1;
1449 }
1450
1451 data.i[c] = 31 - count;
1452 }
1453 }
1454 break;
1455
1456 case ir_unop_find_lsb:
1457 for (unsigned c = 0; c < components; c++) {
1458 if (op[0]->value.i[c] == 0)
1459 data.i[c] = -1;
1460 else {
1461 unsigned pos = 0;
1462 unsigned v = op[0]->value.u[c];
1463
1464 for (; !(v & 1); v >>= 1) {
1465 pos++;
1466 }
1467 data.u[c] = pos;
1468 }
1469 }
1470 break;
1471
1472 case ir_triop_bitfield_extract: {
1473 int offset = op[1]->value.i[0];
1474 int bits = op[2]->value.i[0];
1475
1476 for (unsigned c = 0; c < components; c++) {
1477 if (bits == 0)
1478 data.u[c] = 0;
1479 else if (offset < 0 || bits < 0)
1480 data.u[c] = 0; /* Undefined, per spec. */
1481 else if (offset + bits > 32)
1482 data.u[c] = 0; /* Undefined, per spec. */
1483 else {
1484 if (op[0]->type->base_type == GLSL_TYPE_INT) {
1485 /* int so that the right shift will sign-extend. */
1486 int value = op[0]->value.i[c];
1487 value <<= 32 - bits - offset;
1488 value >>= 32 - bits;
1489 data.i[c] = value;
1490 } else {
1491 unsigned value = op[0]->value.u[c];
1492 value <<= 32 - bits - offset;
1493 value >>= 32 - bits;
1494 data.u[c] = value;
1495 }
1496 }
1497 }
1498 break;
1499 }
1500
1501 case ir_binop_bfm: {
1502 int bits = op[0]->value.i[0];
1503 int offset = op[1]->value.i[0];
1504
1505 for (unsigned c = 0; c < components; c++) {
1506 if (bits == 0)
1507 data.u[c] = op[0]->value.u[c];
1508 else if (offset < 0 || bits < 0)
1509 data.u[c] = 0; /* Undefined for bitfieldInsert, per spec. */
1510 else if (offset + bits > 32)
1511 data.u[c] = 0; /* Undefined for bitfieldInsert, per spec. */
1512 else
1513 data.u[c] = ((1 << bits) - 1) << offset;
1514 }
1515 break;
1516 }
1517
1518 case ir_binop_ldexp:
1519 for (unsigned c = 0; c < components; c++) {
1520 data.f[c] = ldexp(op[0]->value.f[c], op[1]->value.i[c]);
1521 /* Flush subnormal values to zero. */
1522 if (!isnormal(data.f[c]))
1523 data.f[c] = copysign(0.0f, op[0]->value.f[c]);
1524 }
1525 break;
1526
1527 case ir_triop_fma:
1528 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
1529 assert(op[1]->type->base_type == GLSL_TYPE_FLOAT);
1530 assert(op[2]->type->base_type == GLSL_TYPE_FLOAT);
1531
1532 for (unsigned c = 0; c < components; c++) {
1533 data.f[c] = op[0]->value.f[c] * op[1]->value.f[c]
1534 + op[2]->value.f[c];
1535 }
1536 break;
1537
1538 case ir_triop_lrp: {
1539 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
1540 assert(op[1]->type->base_type == GLSL_TYPE_FLOAT);
1541 assert(op[2]->type->base_type == GLSL_TYPE_FLOAT);
1542
1543 unsigned c2_inc = op[2]->type->is_scalar() ? 0 : 1;
1544 for (unsigned c = 0, c2 = 0; c < components; c2 += c2_inc, c++) {
1545 data.f[c] = op[0]->value.f[c] * (1.0f - op[2]->value.f[c2]) +
1546 (op[1]->value.f[c] * op[2]->value.f[c2]);
1547 }
1548 break;
1549 }
1550
1551 case ir_triop_csel:
1552 for (unsigned c = 0; c < components; c++) {
1553 data.u[c] = op[0]->value.b[c] ? op[1]->value.u[c]
1554 : op[2]->value.u[c];
1555 }
1556 break;
1557
1558 case ir_triop_vector_insert: {
1559 const unsigned idx = op[2]->value.u[0];
1560
1561 memcpy(&data, &op[0]->value, sizeof(data));
1562
1563 switch (this->type->base_type) {
1564 case GLSL_TYPE_INT:
1565 data.i[idx] = op[1]->value.i[0];
1566 break;
1567 case GLSL_TYPE_UINT:
1568 data.u[idx] = op[1]->value.u[0];
1569 break;
1570 case GLSL_TYPE_FLOAT:
1571 data.f[idx] = op[1]->value.f[0];
1572 break;
1573 case GLSL_TYPE_BOOL:
1574 data.b[idx] = op[1]->value.b[0];
1575 break;
1576 default:
1577 assert(!"Should not get here.");
1578 break;
1579 }
1580 break;
1581 }
1582
1583 case ir_quadop_bitfield_insert: {
1584 int offset = op[2]->value.i[0];
1585 int bits = op[3]->value.i[0];
1586
1587 for (unsigned c = 0; c < components; c++) {
1588 if (bits == 0)
1589 data.u[c] = op[0]->value.u[c];
1590 else if (offset < 0 || bits < 0)
1591 data.u[c] = 0; /* Undefined, per spec. */
1592 else if (offset + bits > 32)
1593 data.u[c] = 0; /* Undefined, per spec. */
1594 else {
1595 unsigned insert_mask = ((1 << bits) - 1) << offset;
1596
1597 unsigned insert = op[1]->value.u[c];
1598 insert <<= offset;
1599 insert &= insert_mask;
1600
1601 unsigned base = op[0]->value.u[c];
1602 base &= ~insert_mask;
1603
1604 data.u[c] = base | insert;
1605 }
1606 }
1607 break;
1608 }
1609
1610 case ir_quadop_vector:
1611 for (unsigned c = 0; c < this->type->vector_elements; c++) {
1612 switch (this->type->base_type) {
1613 case GLSL_TYPE_INT:
1614 data.i[c] = op[c]->value.i[0];
1615 break;
1616 case GLSL_TYPE_UINT:
1617 data.u[c] = op[c]->value.u[0];
1618 break;
1619 case GLSL_TYPE_FLOAT:
1620 data.f[c] = op[c]->value.f[0];
1621 break;
1622 default:
1623 assert(0);
1624 }
1625 }
1626 break;
1627
1628 default:
1629 /* FINISHME: Should handle all expression types. */
1630 return NULL;
1631 }
1632
1633 return new(ctx) ir_constant(this->type, &data);
1634 }
1635
1636
1637 ir_constant *
1638 ir_texture::constant_expression_value(struct hash_table *variable_context)
1639 {
1640 /* texture lookups aren't constant expressions */
1641 return NULL;
1642 }
1643
1644
1645 ir_constant *
1646 ir_swizzle::constant_expression_value(struct hash_table *variable_context)
1647 {
1648 ir_constant *v = this->val->constant_expression_value(variable_context);
1649
1650 if (v != NULL) {
1651 ir_constant_data data = { { 0 } };
1652
1653 const unsigned swiz_idx[4] = {
1654 this->mask.x, this->mask.y, this->mask.z, this->mask.w
1655 };
1656
1657 for (unsigned i = 0; i < this->mask.num_components; i++) {
1658 switch (v->type->base_type) {
1659 case GLSL_TYPE_UINT:
1660 case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break;
1661 case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
1662 case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break;
1663 default: assert(!"Should not get here."); break;
1664 }
1665 }
1666
1667 void *ctx = ralloc_parent(this);
1668 return new(ctx) ir_constant(this->type, &data);
1669 }
1670 return NULL;
1671 }
1672
1673
1674 ir_constant *
1675 ir_dereference_variable::constant_expression_value(struct hash_table *variable_context)
1676 {
1677 /* This may occur during compile and var->type is glsl_type::error_type */
1678 if (!var)
1679 return NULL;
1680
1681 /* Give priority to the context hashtable, if it exists */
1682 if (variable_context) {
1683 ir_constant *value = (ir_constant *)hash_table_find(variable_context, var);
1684 if(value)
1685 return value;
1686 }
1687
1688 /* The constant_value of a uniform variable is its initializer,
1689 * not the lifetime constant value of the uniform.
1690 */
1691 if (var->data.mode == ir_var_uniform)
1692 return NULL;
1693
1694 if (!var->constant_value)
1695 return NULL;
1696
1697 return var->constant_value->clone(ralloc_parent(var), NULL);
1698 }
1699
1700
1701 ir_constant *
1702 ir_dereference_array::constant_expression_value(struct hash_table *variable_context)
1703 {
1704 ir_constant *array = this->array->constant_expression_value(variable_context);
1705 ir_constant *idx = this->array_index->constant_expression_value(variable_context);
1706
1707 if ((array != NULL) && (idx != NULL)) {
1708 void *ctx = ralloc_parent(this);
1709 if (array->type->is_matrix()) {
1710 /* Array access of a matrix results in a vector.
1711 */
1712 const unsigned column = idx->value.u[0];
1713
1714 const glsl_type *const column_type = array->type->column_type();
1715
1716 /* Offset in the constant matrix to the first element of the column
1717 * to be extracted.
1718 */
1719 const unsigned mat_idx = column * column_type->vector_elements;
1720
1721 ir_constant_data data = { { 0 } };
1722
1723 switch (column_type->base_type) {
1724 case GLSL_TYPE_UINT:
1725 case GLSL_TYPE_INT:
1726 for (unsigned i = 0; i < column_type->vector_elements; i++)
1727 data.u[i] = array->value.u[mat_idx + i];
1728
1729 break;
1730
1731 case GLSL_TYPE_FLOAT:
1732 for (unsigned i = 0; i < column_type->vector_elements; i++)
1733 data.f[i] = array->value.f[mat_idx + i];
1734
1735 break;
1736
1737 default:
1738 assert(!"Should not get here.");
1739 break;
1740 }
1741
1742 return new(ctx) ir_constant(column_type, &data);
1743 } else if (array->type->is_vector()) {
1744 const unsigned component = idx->value.u[0];
1745
1746 return new(ctx) ir_constant(array, component);
1747 } else {
1748 const unsigned index = idx->value.u[0];
1749 return array->get_array_element(index)->clone(ctx, NULL);
1750 }
1751 }
1752 return NULL;
1753 }
1754
1755
1756 ir_constant *
1757 ir_dereference_record::constant_expression_value(struct hash_table *variable_context)
1758 {
1759 ir_constant *v = this->record->constant_expression_value();
1760
1761 return (v != NULL) ? v->get_record_field(this->field) : NULL;
1762 }
1763
1764
1765 ir_constant *
1766 ir_assignment::constant_expression_value(struct hash_table *variable_context)
1767 {
1768 /* FINISHME: Handle CEs involving assignment (return RHS) */
1769 return NULL;
1770 }
1771
1772
1773 ir_constant *
1774 ir_constant::constant_expression_value(struct hash_table *variable_context)
1775 {
1776 return this;
1777 }
1778
1779
1780 ir_constant *
1781 ir_call::constant_expression_value(struct hash_table *variable_context)
1782 {
1783 return this->callee->constant_expression_value(&this->actual_parameters, variable_context);
1784 }
1785
1786
1787 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list &body,
1788 struct hash_table *variable_context,
1789 ir_constant **result)
1790 {
1791 foreach_list(n, &body) {
1792 ir_instruction *inst = (ir_instruction *)n;
1793 switch(inst->ir_type) {
1794
1795 /* (declare () type symbol) */
1796 case ir_type_variable: {
1797 ir_variable *var = inst->as_variable();
1798 hash_table_insert(variable_context, ir_constant::zero(this, var->type), var);
1799 break;
1800 }
1801
1802 /* (assign [condition] (write-mask) (ref) (value)) */
1803 case ir_type_assignment: {
1804 ir_assignment *asg = inst->as_assignment();
1805 if (asg->condition) {
1806 ir_constant *cond = asg->condition->constant_expression_value(variable_context);
1807 if (!cond)
1808 return false;
1809 if (!cond->get_bool_component(0))
1810 break;
1811 }
1812
1813 ir_constant *store = NULL;
1814 int offset = 0;
1815
1816 if (!constant_referenced(asg->lhs, variable_context, store, offset))
1817 return false;
1818
1819 ir_constant *value = asg->rhs->constant_expression_value(variable_context);
1820
1821 if (!value)
1822 return false;
1823
1824 store->copy_masked_offset(value, offset, asg->write_mask);
1825 break;
1826 }
1827
1828 /* (return (expression)) */
1829 case ir_type_return:
1830 assert (result);
1831 *result = inst->as_return()->value->constant_expression_value(variable_context);
1832 return *result != NULL;
1833
1834 /* (call name (ref) (params))*/
1835 case ir_type_call: {
1836 ir_call *call = inst->as_call();
1837
1838 /* Just say no to void functions in constant expressions. We
1839 * don't need them at that point.
1840 */
1841
1842 if (!call->return_deref)
1843 return false;
1844
1845 ir_constant *store = NULL;
1846 int offset = 0;
1847
1848 if (!constant_referenced(call->return_deref, variable_context,
1849 store, offset))
1850 return false;
1851
1852 ir_constant *value = call->constant_expression_value(variable_context);
1853
1854 if(!value)
1855 return false;
1856
1857 store->copy_offset(value, offset);
1858 break;
1859 }
1860
1861 /* (if condition (then-instructions) (else-instructions)) */
1862 case ir_type_if: {
1863 ir_if *iif = inst->as_if();
1864
1865 ir_constant *cond = iif->condition->constant_expression_value(variable_context);
1866 if (!cond || !cond->type->is_boolean())
1867 return false;
1868
1869 exec_list &branch = cond->get_bool_component(0) ? iif->then_instructions : iif->else_instructions;
1870
1871 *result = NULL;
1872 if (!constant_expression_evaluate_expression_list(branch, variable_context, result))
1873 return false;
1874
1875 /* If there was a return in the branch chosen, drop out now. */
1876 if (*result)
1877 return true;
1878
1879 break;
1880 }
1881
1882 /* Every other expression type, we drop out. */
1883 default:
1884 return false;
1885 }
1886 }
1887
1888 /* Reaching the end of the block is not an error condition */
1889 if (result)
1890 *result = NULL;
1891
1892 return true;
1893 }
1894
1895 ir_constant *
1896 ir_function_signature::constant_expression_value(exec_list *actual_parameters, struct hash_table *variable_context)
1897 {
1898 const glsl_type *type = this->return_type;
1899 if (type == glsl_type::void_type)
1900 return NULL;
1901
1902 /* From the GLSL 1.20 spec, page 23:
1903 * "Function calls to user-defined functions (non-built-in functions)
1904 * cannot be used to form constant expressions."
1905 */
1906 if (!this->is_builtin())
1907 return NULL;
1908
1909 /*
1910 * Of the builtin functions, only the texture lookups and the noise
1911 * ones must not be used in constant expressions. They all include
1912 * specific opcodes so they don't need to be special-cased at this
1913 * point.
1914 */
1915
1916 /* Initialize the table of dereferencable names with the function
1917 * parameters. Verify their const-ness on the way.
1918 *
1919 * We expect the correctness of the number of parameters to have
1920 * been checked earlier.
1921 */
1922 hash_table *deref_hash = hash_table_ctor(8, hash_table_pointer_hash,
1923 hash_table_pointer_compare);
1924
1925 /* If "origin" is non-NULL, then the function body is there. So we
1926 * have to use the variable objects from the object with the body,
1927 * but the parameter instanciation on the current object.
1928 */
1929 const exec_node *parameter_info = origin ? origin->parameters.head : parameters.head;
1930
1931 foreach_list(n, actual_parameters) {
1932 ir_constant *constant = ((ir_rvalue *) n)->constant_expression_value(variable_context);
1933 if (constant == NULL) {
1934 hash_table_dtor(deref_hash);
1935 return NULL;
1936 }
1937
1938
1939 ir_variable *var = (ir_variable *)parameter_info;
1940 hash_table_insert(deref_hash, constant, var);
1941
1942 parameter_info = parameter_info->next;
1943 }
1944
1945 ir_constant *result = NULL;
1946
1947 /* Now run the builtin function until something non-constant
1948 * happens or we get the result.
1949 */
1950 if (constant_expression_evaluate_expression_list(origin ? origin->body : body, deref_hash, &result) && result)
1951 result = result->clone(ralloc_parent(this), NULL);
1952
1953 hash_table_dtor(deref_hash);
1954
1955 return result;
1956 }