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