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